blob: 8af97ac02962aff0b63302897bfc75abb6072abe [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
67
Willy Tarreau522d6c02009-12-06 18:49:18 +010068const char HTTP_100[] =
69 "HTTP/1.1 100 Continue\r\n\r\n";
70
71const struct chunk http_100_chunk = {
72 .str = (char *)&HTTP_100,
73 .len = sizeof(HTTP_100)-1
74};
75
Willy Tarreaua9679ac2010-01-03 17:32:57 +010076/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020077const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010078 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020080 "Location: "; /* not terminated since it will be concatenated with the URL */
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Location: "; /* not terminated since it will be concatenated with the URL */
87
88/* same as 302 except that the browser MUST retry with the GET method */
89const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040095
96/* same as 302 except that the browser MUST retry with the same method */
97const char *HTTP_307 =
98 "HTTP/1.1 307 Temporary Redirect\r\n"
99 "Cache-Control: no-cache\r\n"
100 "Content-length: 0\r\n"
101 "Location: "; /* not terminated since it will be concatenated with the URL */
102
103/* same as 301 except that the browser MUST retry with the same method */
104const char *HTTP_308 =
105 "HTTP/1.1 308 Permanent Redirect\r\n"
106 "Content-length: 0\r\n"
107 "Location: "; /* not terminated since it will be concatenated with the URL */
108
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
110const char *HTTP_401_fmt =
111 "HTTP/1.0 401 Unauthorized\r\n"
112 "Cache-Control: no-cache\r\n"
113 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200114 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
116 "\r\n"
117 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
118
Willy Tarreau844a7e72010-01-31 21:46:18 +0100119const char *HTTP_407_fmt =
120 "HTTP/1.0 407 Unauthorized\r\n"
121 "Cache-Control: no-cache\r\n"
122 "Connection: close\r\n"
123 "Content-Type: text/html\r\n"
124 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
125 "\r\n"
126 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
127
Willy Tarreau0f772532006-12-23 20:51:41 +0100128
129const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200130 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 [HTTP_ERR_400] = 400,
132 [HTTP_ERR_403] = 403,
133 [HTTP_ERR_408] = 408,
134 [HTTP_ERR_500] = 500,
135 [HTTP_ERR_502] = 502,
136 [HTTP_ERR_503] = 503,
137 [HTTP_ERR_504] = 504,
138};
139
Willy Tarreau80587432006-12-24 17:47:20 +0100140static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200141 [HTTP_ERR_200] =
142 "HTTP/1.0 200 OK\r\n"
143 "Cache-Control: no-cache\r\n"
144 "Connection: close\r\n"
145 "Content-Type: text/html\r\n"
146 "\r\n"
147 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
148
Willy Tarreau0f772532006-12-23 20:51:41 +0100149 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100150 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100151 "Cache-Control: no-cache\r\n"
152 "Connection: close\r\n"
153 "Content-Type: text/html\r\n"
154 "\r\n"
155 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
156
157 [HTTP_ERR_403] =
158 "HTTP/1.0 403 Forbidden\r\n"
159 "Cache-Control: no-cache\r\n"
160 "Connection: close\r\n"
161 "Content-Type: text/html\r\n"
162 "\r\n"
163 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
164
165 [HTTP_ERR_408] =
166 "HTTP/1.0 408 Request Time-out\r\n"
167 "Cache-Control: no-cache\r\n"
168 "Connection: close\r\n"
169 "Content-Type: text/html\r\n"
170 "\r\n"
171 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
172
173 [HTTP_ERR_500] =
174 "HTTP/1.0 500 Server Error\r\n"
175 "Cache-Control: no-cache\r\n"
176 "Connection: close\r\n"
177 "Content-Type: text/html\r\n"
178 "\r\n"
179 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
180
181 [HTTP_ERR_502] =
182 "HTTP/1.0 502 Bad Gateway\r\n"
183 "Cache-Control: no-cache\r\n"
184 "Connection: close\r\n"
185 "Content-Type: text/html\r\n"
186 "\r\n"
187 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
188
189 [HTTP_ERR_503] =
190 "HTTP/1.0 503 Service Unavailable\r\n"
191 "Cache-Control: no-cache\r\n"
192 "Connection: close\r\n"
193 "Content-Type: text/html\r\n"
194 "\r\n"
195 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
196
197 [HTTP_ERR_504] =
198 "HTTP/1.0 504 Gateway Time-out\r\n"
199 "Cache-Control: no-cache\r\n"
200 "Connection: close\r\n"
201 "Content-Type: text/html\r\n"
202 "\r\n"
203 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
204
205};
206
Cyril Bonté19979e12012-04-04 12:57:21 +0200207/* status codes available for the stats admin page (strictly 4 chars length) */
208const char *stat_status_codes[STAT_STATUS_SIZE] = {
209 [STAT_STATUS_DENY] = "DENY",
210 [STAT_STATUS_DONE] = "DONE",
211 [STAT_STATUS_ERRP] = "ERRP",
212 [STAT_STATUS_EXCD] = "EXCD",
213 [STAT_STATUS_NONE] = "NONE",
214 [STAT_STATUS_PART] = "PART",
215 [STAT_STATUS_UNKN] = "UNKN",
216};
217
218
Willy Tarreau80587432006-12-24 17:47:20 +0100219/* We must put the messages here since GCC cannot initialize consts depending
220 * on strlen().
221 */
222struct chunk http_err_chunks[HTTP_ERR_SIZE];
223
Willy Tarreaua890d072013-04-02 12:01:06 +0200224/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
225static struct hdr_ctx static_hdr_ctx;
226
Willy Tarreau42250582007-04-01 01:30:43 +0200227#define FD_SETS_ARE_BITFIELDS
228#ifdef FD_SETS_ARE_BITFIELDS
229/*
230 * This map is used with all the FD_* macros to check whether a particular bit
231 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
232 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
233 * byte should be encoded. Be careful to always pass bytes from 0 to 255
234 * exclusively to the macros.
235 */
236fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
237fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100238fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200239
240#else
241#error "Check if your OS uses bitfields for fd_sets"
242#endif
243
Willy Tarreau80587432006-12-24 17:47:20 +0100244void init_proto_http()
245{
Willy Tarreau42250582007-04-01 01:30:43 +0200246 int i;
247 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100248 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200249
Willy Tarreau80587432006-12-24 17:47:20 +0100250 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
251 if (!http_err_msgs[msg]) {
252 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
253 abort();
254 }
255
256 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
257 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
258 }
Willy Tarreau42250582007-04-01 01:30:43 +0200259
260 /* initialize the log header encoding map : '{|}"#' should be encoded with
261 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
262 * URL encoding only requires '"', '#' to be encoded as well as non-
263 * printable characters above.
264 */
265 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
266 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100267 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200268 for (i = 0; i < 32; i++) {
269 FD_SET(i, hdr_encode_map);
270 FD_SET(i, url_encode_map);
271 }
272 for (i = 127; i < 256; i++) {
273 FD_SET(i, hdr_encode_map);
274 FD_SET(i, url_encode_map);
275 }
276
277 tmp = "\"#{|}";
278 while (*tmp) {
279 FD_SET(*tmp, hdr_encode_map);
280 tmp++;
281 }
282
283 tmp = "\"#";
284 while (*tmp) {
285 FD_SET(*tmp, url_encode_map);
286 tmp++;
287 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200288
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100289 /* initialize the http header encoding map. The draft httpbis define the
290 * header content as:
291 *
292 * HTTP-message = start-line
293 * *( header-field CRLF )
294 * CRLF
295 * [ message-body ]
296 * header-field = field-name ":" OWS field-value OWS
297 * field-value = *( field-content / obs-fold )
298 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
299 * obs-fold = CRLF 1*( SP / HTAB )
300 * field-vchar = VCHAR / obs-text
301 * VCHAR = %x21-7E
302 * obs-text = %x80-FF
303 *
304 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
305 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
306 * "obs-fold" is volontary forgotten because haproxy remove this.
307 */
308 memset(http_encode_map, 0, sizeof(http_encode_map));
309 for (i = 0x00; i <= 0x08; i++)
310 FD_SET(i, http_encode_map);
311 for (i = 0x0a; i <= 0x1f; i++)
312 FD_SET(i, http_encode_map);
313 FD_SET(0x7f, http_encode_map);
314
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200315 /* memory allocations */
316 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100317 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100318}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319
Willy Tarreau53b6c742006-12-17 13:37:46 +0100320/*
321 * We have 26 list of methods (1 per first letter), each of which can have
322 * up to 3 entries (2 valid, 1 null).
323 */
324struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100325 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100326 int len;
327 const char text[8];
328};
329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100330const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100331 ['C' - 'A'] = {
332 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
333 },
334 ['D' - 'A'] = {
335 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
336 },
337 ['G' - 'A'] = {
338 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
339 },
340 ['H' - 'A'] = {
341 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
342 },
343 ['P' - 'A'] = {
344 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
345 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
346 },
347 ['T' - 'A'] = {
348 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
349 },
350 /* rest is empty like this :
351 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
352 */
353};
354
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100355const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
356 [HTTP_METH_NONE] = { "", 0 },
357 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
358 [HTTP_METH_GET] = { "GET", 3 },
359 [HTTP_METH_HEAD] = { "HEAD", 4 },
360 [HTTP_METH_POST] = { "POST", 4 },
361 [HTTP_METH_PUT] = { "PUT", 3 },
362 [HTTP_METH_DELETE] = { "DELETE", 6 },
363 [HTTP_METH_TRACE] = { "TRACE", 5 },
364 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
365};
366
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100367/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200368 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100369 * RFC2616 for those chars.
370 */
371
372const char http_is_spht[256] = {
373 [' '] = 1, ['\t'] = 1,
374};
375
376const char http_is_crlf[256] = {
377 ['\r'] = 1, ['\n'] = 1,
378};
379
380const char http_is_lws[256] = {
381 [' '] = 1, ['\t'] = 1,
382 ['\r'] = 1, ['\n'] = 1,
383};
384
385const char http_is_sep[256] = {
386 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
387 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
388 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
389 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
390 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
391};
392
393const char http_is_ctl[256] = {
394 [0 ... 31] = 1,
395 [127] = 1,
396};
397
398/*
399 * A token is any ASCII char that is neither a separator nor a CTL char.
400 * Do not overwrite values in assignment since gcc-2.95 will not handle
401 * them correctly. Instead, define every non-CTL char's status.
402 */
403const char http_is_token[256] = {
404 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
405 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
406 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
407 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
408 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
409 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
410 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
411 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
412 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
413 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
414 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
415 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
416 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
417 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
418 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
419 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
420 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
421 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
422 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
423 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
424 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
425 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
426 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
427 ['|'] = 1, ['}'] = 0, ['~'] = 1,
428};
429
430
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100431/*
432 * An http ver_token is any ASCII which can be found in an HTTP version,
433 * which includes 'H', 'T', 'P', '/', '.' and any digit.
434 */
435const char http_is_ver_token[256] = {
436 ['.'] = 1, ['/'] = 1,
437 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
438 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
439 ['H'] = 1, ['P'] = 1, ['T'] = 1,
440};
441
442
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100443/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100444 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
445 */
446#if defined(DEBUG_FSM)
447static void http_silent_debug(int line, struct session *s)
448{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100449 chunk_printf(&trash,
450 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n",
451 line,
452 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
453 s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags);
454 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100455
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100456 chunk_printf(&trash,
457 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n",
458 line,
459 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
460 s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->o, s->rep->to_forward);
461 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100462}
463#else
464#define http_silent_debug(l,s) do { } while (0)
465#endif
466
467/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100468 * Adds a header and its CRLF at the tail of the message's buffer, just before
469 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100470 * The header is also automatically added to the index <hdr_idx>, and the end
471 * of headers is automatically adjusted. The number of bytes added is returned
472 * on success, otherwise <0 is returned indicating an error.
473 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100474int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100475{
476 int bytes, len;
477
478 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200479 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100480 if (!bytes)
481 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100482 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100483 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
484}
485
486/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100487 * Adds a header and its CRLF at the tail of the message's buffer, just before
488 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100489 * the buffer is only opened and the space reserved, but nothing is copied.
490 * The header is also automatically added to the index <hdr_idx>, and the end
491 * of headers is automatically adjusted. The number of bytes added is returned
492 * on success, otherwise <0 is returned indicating an error.
493 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100494int http_header_add_tail2(struct http_msg *msg,
495 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100496{
497 int bytes;
498
Willy Tarreau9b28e032012-10-12 23:49:43 +0200499 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100500 if (!bytes)
501 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100502 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100503 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
504}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200505
506/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100507 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
508 * If so, returns the position of the first non-space character relative to
509 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
510 * to return a pointer to the place after the first space. Returns 0 if the
511 * header name does not match. Checks are case-insensitive.
512 */
513int http_header_match2(const char *hdr, const char *end,
514 const char *name, int len)
515{
516 const char *val;
517
518 if (hdr + len >= end)
519 return 0;
520 if (hdr[len] != ':')
521 return 0;
522 if (strncasecmp(hdr, name, len) != 0)
523 return 0;
524 val = hdr + len + 1;
525 while (val < end && HTTP_IS_SPHT(*val))
526 val++;
527 if ((val >= end) && (len + 2 <= end - hdr))
528 return len + 2; /* we may replace starting from second space */
529 return val - hdr;
530}
531
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200532/* Find the first or next occurrence of header <name> in message buffer <sol>
533 * using headers index <idx>, and return it in the <ctx> structure. This
534 * structure holds everything necessary to use the header and find next
535 * occurrence. If its <idx> member is 0, the header is searched from the
536 * beginning. Otherwise, the next occurrence is returned. The function returns
537 * 1 when it finds a value, and 0 when there is no more. It is very similar to
538 * http_find_header2() except that it is designed to work with full-line headers
539 * whose comma is not a delimiter but is part of the syntax. As a special case,
540 * if ctx->val is NULL when searching for a new values of a header, the current
541 * header is rescanned. This allows rescanning after a header deletion.
542 */
543int http_find_full_header2(const char *name, int len,
544 char *sol, struct hdr_idx *idx,
545 struct hdr_ctx *ctx)
546{
547 char *eol, *sov;
548 int cur_idx, old_idx;
549
550 cur_idx = ctx->idx;
551 if (cur_idx) {
552 /* We have previously returned a header, let's search another one */
553 sol = ctx->line;
554 eol = sol + idx->v[cur_idx].len;
555 goto next_hdr;
556 }
557
558 /* first request for this header */
559 sol += hdr_idx_first_pos(idx);
560 old_idx = 0;
561 cur_idx = hdr_idx_first_idx(idx);
562 while (cur_idx) {
563 eol = sol + idx->v[cur_idx].len;
564
565 if (len == 0) {
566 /* No argument was passed, we want any header.
567 * To achieve this, we simply build a fake request. */
568 while (sol + len < eol && sol[len] != ':')
569 len++;
570 name = sol;
571 }
572
573 if ((len < eol - sol) &&
574 (sol[len] == ':') &&
575 (strncasecmp(sol, name, len) == 0)) {
576 ctx->del = len;
577 sov = sol + len + 1;
578 while (sov < eol && http_is_lws[(unsigned char)*sov])
579 sov++;
580
581 ctx->line = sol;
582 ctx->prev = old_idx;
583 ctx->idx = cur_idx;
584 ctx->val = sov - sol;
585 ctx->tws = 0;
586 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
587 eol--;
588 ctx->tws++;
589 }
590 ctx->vlen = eol - sov;
591 return 1;
592 }
593 next_hdr:
594 sol = eol + idx->v[cur_idx].cr + 1;
595 old_idx = cur_idx;
596 cur_idx = idx->v[cur_idx].next;
597 }
598 return 0;
599}
600
Willy Tarreau68085d82010-01-18 14:54:04 +0100601/* Find the end of the header value contained between <s> and <e>. See RFC2616,
602 * par 2.2 for more information. Note that it requires a valid header to return
603 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200604 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100605char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200606{
607 int quoted, qdpair;
608
609 quoted = qdpair = 0;
610 for (; s < e; s++) {
611 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200612 else if (quoted) {
613 if (*s == '\\') qdpair = 1;
614 else if (*s == '"') quoted = 0;
615 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200616 else if (*s == '"') quoted = 1;
617 else if (*s == ',') return s;
618 }
619 return s;
620}
621
622/* Find the first or next occurrence of header <name> in message buffer <sol>
623 * using headers index <idx>, and return it in the <ctx> structure. This
624 * structure holds everything necessary to use the header and find next
625 * occurrence. If its <idx> member is 0, the header is searched from the
626 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100627 * 1 when it finds a value, and 0 when there is no more. It is designed to work
628 * with headers defined as comma-separated lists. As a special case, if ctx->val
629 * is NULL when searching for a new values of a header, the current header is
630 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200631 */
632int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100633 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200634 struct hdr_ctx *ctx)
635{
Willy Tarreau68085d82010-01-18 14:54:04 +0100636 char *eol, *sov;
637 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200638
Willy Tarreau68085d82010-01-18 14:54:04 +0100639 cur_idx = ctx->idx;
640 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200641 /* We have previously returned a value, let's search
642 * another one on the same line.
643 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200644 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200645 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100646 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200647 eol = sol + idx->v[cur_idx].len;
648
649 if (sov >= eol)
650 /* no more values in this header */
651 goto next_hdr;
652
Willy Tarreau68085d82010-01-18 14:54:04 +0100653 /* values remaining for this header, skip the comma but save it
654 * for later use (eg: for header deletion).
655 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200656 sov++;
657 while (sov < eol && http_is_lws[(unsigned char)*sov])
658 sov++;
659
660 goto return_hdr;
661 }
662
663 /* first request for this header */
664 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100665 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200666 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200667 while (cur_idx) {
668 eol = sol + idx->v[cur_idx].len;
669
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200670 if (len == 0) {
671 /* No argument was passed, we want any header.
672 * To achieve this, we simply build a fake request. */
673 while (sol + len < eol && sol[len] != ':')
674 len++;
675 name = sol;
676 }
677
Willy Tarreau33a7e692007-06-10 19:45:56 +0200678 if ((len < eol - sol) &&
679 (sol[len] == ':') &&
680 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100681 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200682 sov = sol + len + 1;
683 while (sov < eol && http_is_lws[(unsigned char)*sov])
684 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100685
Willy Tarreau33a7e692007-06-10 19:45:56 +0200686 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100687 ctx->prev = old_idx;
688 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200689 ctx->idx = cur_idx;
690 ctx->val = sov - sol;
691
692 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200693 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200694 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200695 eol--;
696 ctx->tws++;
697 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200698 ctx->vlen = eol - sov;
699 return 1;
700 }
701 next_hdr:
702 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100703 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200704 cur_idx = idx->v[cur_idx].next;
705 }
706 return 0;
707}
708
709int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100710 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200711 struct hdr_ctx *ctx)
712{
713 return http_find_header2(name, strlen(name), sol, idx, ctx);
714}
715
Willy Tarreau68085d82010-01-18 14:54:04 +0100716/* Remove one value of a header. This only works on a <ctx> returned by one of
717 * the http_find_header functions. The value is removed, as well as surrounding
718 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100719 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100720 * message <msg>. The new index is returned. If it is zero, it means there is
721 * no more header, so any processing may stop. The ctx is always left in a form
722 * that can be handled by http_find_header2() to find next occurrence.
723 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100724int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100725{
726 int cur_idx = ctx->idx;
727 char *sol = ctx->line;
728 struct hdr_idx_elem *hdr;
729 int delta, skip_comma;
730
731 if (!cur_idx)
732 return 0;
733
734 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200735 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100736 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200737 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100738 http_msg_move_end(msg, delta);
739 idx->used--;
740 hdr->len = 0; /* unused entry */
741 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100742 if (idx->tail == ctx->idx)
743 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100744 ctx->idx = ctx->prev; /* walk back to the end of previous header */
745 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
746 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200747 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100748 return ctx->idx;
749 }
750
751 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200752 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
753 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100754 */
755
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200756 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200757 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200758 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100759 NULL, 0);
760 hdr->len += delta;
761 http_msg_move_end(msg, delta);
762 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200763 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100764 return ctx->idx;
765}
766
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100767/* This function handles a server error at the stream interface level. The
768 * stream interface is assumed to be already in a closed state. An optional
769 * message is copied into the input buffer, and an HTTP status code stored.
770 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100771 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100773static void http_server_error(struct session *t, struct stream_interface *si,
774 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200775{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200776 channel_auto_read(si->ob);
777 channel_abort(si->ob);
778 channel_auto_close(si->ob);
779 channel_erase(si->ob);
780 channel_auto_close(si->ib);
781 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100782 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100783 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200784 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200785 }
786 if (!(t->flags & SN_ERR_MASK))
787 t->flags |= err;
788 if (!(t->flags & SN_FINST_MASK))
789 t->flags |= finst;
790}
791
Willy Tarreau80587432006-12-24 17:47:20 +0100792/* This function returns the appropriate error location for the given session
793 * and message.
794 */
795
Willy Tarreau783f2582012-09-04 12:19:04 +0200796struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100797{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200798 if (s->be->errmsg[msgnum].str)
799 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100800 else if (s->fe->errmsg[msgnum].str)
801 return &s->fe->errmsg[msgnum];
802 else
803 return &http_err_chunks[msgnum];
804}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200805
Willy Tarreau53b6c742006-12-17 13:37:46 +0100806/*
807 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
808 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
809 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100810enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100811{
812 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100813 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100814
815 m = ((unsigned)*str - 'A');
816
817 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100818 for (h = http_methods[m]; h->len > 0; h++) {
819 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100820 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100821 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100822 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100823 };
824 return HTTP_METH_OTHER;
825 }
826 return HTTP_METH_NONE;
827
828}
829
Willy Tarreau21d2af32008-02-14 20:25:24 +0100830/* Parse the URI from the given transaction (which is assumed to be in request
831 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
832 * It is returned otherwise.
833 */
834static char *
835http_get_path(struct http_txn *txn)
836{
837 char *ptr, *end;
838
Willy Tarreau9b28e032012-10-12 23:49:43 +0200839 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100840 end = ptr + txn->req.sl.rq.u_l;
841
842 if (ptr >= end)
843 return NULL;
844
845 /* RFC2616, par. 5.1.2 :
846 * Request-URI = "*" | absuri | abspath | authority
847 */
848
849 if (*ptr == '*')
850 return NULL;
851
852 if (isalpha((unsigned char)*ptr)) {
853 /* this is a scheme as described by RFC3986, par. 3.1 */
854 ptr++;
855 while (ptr < end &&
856 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
857 ptr++;
858 /* skip '://' */
859 if (ptr == end || *ptr++ != ':')
860 return NULL;
861 if (ptr == end || *ptr++ != '/')
862 return NULL;
863 if (ptr == end || *ptr++ != '/')
864 return NULL;
865 }
866 /* skip [user[:passwd]@]host[:[port]] */
867
868 while (ptr < end && *ptr != '/')
869 ptr++;
870
871 if (ptr == end)
872 return NULL;
873
874 /* OK, we got the '/' ! */
875 return ptr;
876}
877
William Lallemand65ad6e12014-01-31 15:08:02 +0100878/* Parse the URI from the given string and look for the "/" beginning the PATH.
879 * If not found, return NULL. It is returned otherwise.
880 */
881static char *
882http_get_path_from_string(char *str)
883{
884 char *ptr = str;
885
886 /* RFC2616, par. 5.1.2 :
887 * Request-URI = "*" | absuri | abspath | authority
888 */
889
890 if (*ptr == '*')
891 return NULL;
892
893 if (isalpha((unsigned char)*ptr)) {
894 /* this is a scheme as described by RFC3986, par. 3.1 */
895 ptr++;
896 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
897 ptr++;
898 /* skip '://' */
899 if (*ptr == '\0' || *ptr++ != ':')
900 return NULL;
901 if (*ptr == '\0' || *ptr++ != '/')
902 return NULL;
903 if (*ptr == '\0' || *ptr++ != '/')
904 return NULL;
905 }
906 /* skip [user[:passwd]@]host[:[port]] */
907
908 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
909 ptr++;
910
911 if (*ptr == '\0' || *ptr == ' ')
912 return NULL;
913
914 /* OK, we got the '/' ! */
915 return ptr;
916}
917
Willy Tarreau71241ab2012-12-27 11:30:54 +0100918/* Returns a 302 for a redirectable request that reaches a server working in
919 * in redirect mode. This may only be called just after the stream interface
920 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
921 * follow normal proxy processing. NOTE: this function is designed to support
922 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100923 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100924void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100925{
926 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100927 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100928 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200929 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100930
931 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100932 trash.len = strlen(HTTP_302);
933 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100934
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100935 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100936
Willy Tarreauefb453c2008-10-26 20:49:47 +0100937 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100938 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 return;
940
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100941 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100942 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100943 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
944 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100945 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100946
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200947 /* 3: add the request URI. Since it was already forwarded, we need
948 * to temporarily rewind the buffer.
949 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100950 txn = &s->txn;
Willy Tarreau211cdec2014-04-17 20:18:08 +0200951 b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200952
Willy Tarreauefb453c2008-10-26 20:49:47 +0100953 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200954 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200955
Willy Tarreau9b28e032012-10-12 23:49:43 +0200956 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200957
Willy Tarreauefb453c2008-10-26 20:49:47 +0100958 if (!path)
959 return;
960
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100961 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100962 return;
963
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100964 memcpy(trash.str + trash.len, path, len);
965 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100966
967 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100968 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
969 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100970 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100971 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
972 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100973 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100974
975 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200976 si_shutr(si);
977 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100978 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100979 si->state = SI_ST_CLO;
980
981 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200982 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100983
984 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100985 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500986 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100987}
988
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100989/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990 * that the server side is closed. Note that err_type is actually a
991 * bitmask, where almost only aborts may be cumulated with other
992 * values. We consider that aborted operations are more important
993 * than timeouts or errors due to the fact that nobody else in the
994 * logs might explain incomplete retries. All others should avoid
995 * being cumulated. It should normally not be possible to have multiple
996 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100997 * Note that connection errors appearing on the second request of a keep-alive
998 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100999 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001000void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001001{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001002 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003
1004 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001005 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001006 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001007 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001008 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001009 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1010 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001011 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001012 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001013 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001014 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001015 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001016 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001017 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001018 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001019 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1020 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001021 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001022 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001023 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001024 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001025 else if (err_type & SI_ET_CONN_RES)
1026 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001027 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1028 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001029 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001030 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001031 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001032}
1033
Willy Tarreau42250582007-04-01 01:30:43 +02001034extern const char sess_term_cond[8];
1035extern const char sess_fin_state[8];
1036extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001037struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001038struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001039struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001040
Willy Tarreau117f59e2007-03-04 18:17:17 +01001041/*
1042 * Capture headers from message starting at <som> according to header list
1043 * <cap_hdr>, and fill the <idx> structure appropriately.
1044 */
1045void capture_headers(char *som, struct hdr_idx *idx,
1046 char **cap, struct cap_hdr *cap_hdr)
1047{
1048 char *eol, *sol, *col, *sov;
1049 int cur_idx;
1050 struct cap_hdr *h;
1051 int len;
1052
1053 sol = som + hdr_idx_first_pos(idx);
1054 cur_idx = hdr_idx_first_idx(idx);
1055
1056 while (cur_idx) {
1057 eol = sol + idx->v[cur_idx].len;
1058
1059 col = sol;
1060 while (col < eol && *col != ':')
1061 col++;
1062
1063 sov = col + 1;
1064 while (sov < eol && http_is_lws[(unsigned char)*sov])
1065 sov++;
1066
1067 for (h = cap_hdr; h; h = h->next) {
1068 if ((h->namelen == col - sol) &&
1069 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1070 if (cap[h->index] == NULL)
1071 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001072 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001073
1074 if (cap[h->index] == NULL) {
1075 Alert("HTTP capture : out of memory.\n");
1076 continue;
1077 }
1078
1079 len = eol - sov;
1080 if (len > h->len)
1081 len = h->len;
1082
1083 memcpy(cap[h->index], sov, len);
1084 cap[h->index][len]=0;
1085 }
1086 }
1087 sol = eol + idx->v[cur_idx].cr + 1;
1088 cur_idx = idx->v[cur_idx].next;
1089 }
1090}
1091
1092
Willy Tarreau42250582007-04-01 01:30:43 +02001093/* either we find an LF at <ptr> or we jump to <bad>.
1094 */
1095#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1096
1097/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1098 * otherwise to <http_msg_ood> with <state> set to <st>.
1099 */
1100#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1101 ptr++; \
1102 if (likely(ptr < end)) \
1103 goto good; \
1104 else { \
1105 state = (st); \
1106 goto http_msg_ood; \
1107 } \
1108 } while (0)
1109
1110
Willy Tarreaubaaee002006-06-26 02:48:02 +02001111/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001112 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001113 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1114 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1115 * will give undefined results.
1116 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1117 * and that msg->sol points to the beginning of the response.
1118 * If a complete line is found (which implies that at least one CR or LF is
1119 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1120 * returned indicating an incomplete line (which does not mean that parts have
1121 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1122 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1123 * upon next call.
1124 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001125 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001126 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1127 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001128 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001129 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001130const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001131 enum ht_state state, const char *ptr, const char *end,
1132 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001133{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001134 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001135
Willy Tarreau8973c702007-01-21 23:58:29 +01001136 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001137 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001138 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001139 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1141
1142 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001143 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001144 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1145 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001146 state = HTTP_MSG_ERROR;
1147 break;
1148
Willy Tarreau8973c702007-01-21 23:58:29 +01001149 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001150 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001151 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001152 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001153 goto http_msg_rpcode;
1154 }
1155 if (likely(HTTP_IS_SPHT(*ptr)))
1156 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1157 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001158 state = HTTP_MSG_ERROR;
1159 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001160
Willy Tarreau8973c702007-01-21 23:58:29 +01001161 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001162 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001163 if (likely(!HTTP_IS_LWS(*ptr)))
1164 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1165
1166 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001167 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1169 }
1170
1171 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001172 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001173 http_msg_rsp_reason:
1174 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001175 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001176 msg->sl.st.r_l = 0;
1177 goto http_msg_rpline_eol;
1178
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001180 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001182 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001183 goto http_msg_rpreason;
1184 }
1185 if (likely(HTTP_IS_SPHT(*ptr)))
1186 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1187 /* so it's a CR/LF, so there is no reason phrase */
1188 goto http_msg_rsp_reason;
1189
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001191 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001192 if (likely(!HTTP_IS_CRLF(*ptr)))
1193 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001194 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001195 http_msg_rpline_eol:
1196 /* We have seen the end of line. Note that we do not
1197 * necessarily have the \n yet, but at least we know that we
1198 * have EITHER \r OR \n, otherwise the response would not be
1199 * complete. We can then record the response length and return
1200 * to the caller which will be able to register it.
1201 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001202 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001203 return ptr;
1204
Willy Tarreau8973c702007-01-21 23:58:29 +01001205 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001206#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001207 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1208 exit(1);
1209#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001210 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001211 }
1212
1213 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001214 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001215 if (ret_state)
1216 *ret_state = state;
1217 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001218 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001219 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001220}
1221
Willy Tarreau8973c702007-01-21 23:58:29 +01001222/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001223 * This function parses a request line between <ptr> and <end>, starting with
1224 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1225 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1226 * will give undefined results.
1227 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1228 * and that msg->sol points to the beginning of the request.
1229 * If a complete line is found (which implies that at least one CR or LF is
1230 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1231 * returned indicating an incomplete line (which does not mean that parts have
1232 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1233 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1234 * upon next call.
1235 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001236 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1238 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001239 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001240 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001241const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001242 enum ht_state state, const char *ptr, const char *end,
1243 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001244{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001245 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001248 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001249 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 if (likely(HTTP_IS_TOKEN(*ptr)))
1251 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001252
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001253 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001254 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001255 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1256 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001258 if (likely(HTTP_IS_CRLF(*ptr))) {
1259 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001260 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001262 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001264 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001266 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001267 msg->sl.rq.v_l = 0;
1268 goto http_msg_rqline_eol;
1269 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001270 state = HTTP_MSG_ERROR;
1271 break;
1272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001274 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001276 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 goto http_msg_rquri;
1278 }
1279 if (likely(HTTP_IS_SPHT(*ptr)))
1280 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1281 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1282 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001285 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001286 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001290 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001291 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1292 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001293
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001294 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001295 /* non-ASCII chars are forbidden unless option
1296 * accept-invalid-http-request is enabled in the frontend.
1297 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001298 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001299 if (msg->err_pos < -1)
1300 goto invalid_char;
1301 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001302 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001303 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1304 }
1305
1306 if (likely(HTTP_IS_CRLF(*ptr))) {
1307 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1308 goto http_msg_req09_uri_e;
1309 }
1310
1311 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001312 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001313 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001314 state = HTTP_MSG_ERROR;
1315 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001318 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001320 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 goto http_msg_rqver;
1322 }
1323 if (likely(HTTP_IS_SPHT(*ptr)))
1324 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1325 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1326 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001329 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001330 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001332
1333 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001334 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001335 http_msg_rqline_eol:
1336 /* We have seen the end of line. Note that we do not
1337 * necessarily have the \n yet, but at least we know that we
1338 * have EITHER \r OR \n, otherwise the request would not be
1339 * complete. We can then record the request length and return
1340 * to the caller which will be able to register it.
1341 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001342 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001343 return ptr;
1344 }
1345
1346 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001347 state = HTTP_MSG_ERROR;
1348 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001349
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001351#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1353 exit(1);
1354#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001355 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001356 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001357
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001358 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001359 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001360 if (ret_state)
1361 *ret_state = state;
1362 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001363 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001366
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001367/*
1368 * Returns the data from Authorization header. Function may be called more
1369 * than once so data is stored in txn->auth_data. When no header is found
1370 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001371 * searching again for something we are unable to find anyway. However, if
1372 * the result if valid, the cache is not reused because we would risk to
1373 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001374 */
1375
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001376/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1377 * set according to global.tune.bufsize.
1378 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001379char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001380
1381int
1382get_http_auth(struct session *s)
1383{
1384
1385 struct http_txn *txn = &s->txn;
1386 struct chunk auth_method;
1387 struct hdr_ctx ctx;
1388 char *h, *p;
1389 int len;
1390
1391#ifdef DEBUG_AUTH
1392 printf("Auth for session %p: %d\n", s, txn->auth.method);
1393#endif
1394
1395 if (txn->auth.method == HTTP_AUTH_WRONG)
1396 return 0;
1397
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001398 txn->auth.method = HTTP_AUTH_WRONG;
1399
1400 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001401
1402 if (txn->flags & TX_USE_PX_CONN) {
1403 h = "Proxy-Authorization";
1404 len = strlen(h);
1405 } else {
1406 h = "Authorization";
1407 len = strlen(h);
1408 }
1409
Willy Tarreau9b28e032012-10-12 23:49:43 +02001410 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001411 return 0;
1412
1413 h = ctx.line + ctx.val;
1414
1415 p = memchr(h, ' ', ctx.vlen);
1416 if (!p || p == h)
1417 return 0;
1418
1419 chunk_initlen(&auth_method, h, 0, p-h);
1420 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1421
1422 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1423
1424 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001425 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001426
1427 if (len < 0)
1428 return 0;
1429
1430
1431 get_http_auth_buff[len] = '\0';
1432
1433 p = strchr(get_http_auth_buff, ':');
1434
1435 if (!p)
1436 return 0;
1437
1438 txn->auth.user = get_http_auth_buff;
1439 *p = '\0';
1440 txn->auth.pass = p+1;
1441
1442 txn->auth.method = HTTP_AUTH_BASIC;
1443 return 1;
1444 }
1445
1446 return 0;
1447}
1448
Willy Tarreau58f10d72006-12-04 02:26:12 +01001449
Willy Tarreau8973c702007-01-21 23:58:29 +01001450/*
1451 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001452 * depending on the initial msg->msg_state. The caller is responsible for
1453 * ensuring that the message does not wrap. The function can be preempted
1454 * everywhere when data are missing and recalled at the exact same location
1455 * with no information loss. The message may even be realigned between two
1456 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001457 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001458 * fields. Note that msg->sol will be initialized after completing the first
1459 * state, so that none of the msg pointers has to be initialized prior to the
1460 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001461 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001462void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463{
Willy Tarreau3770f232013-12-07 00:01:53 +01001464 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001466 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001467
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001468 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001469 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001470 ptr = buf->p + msg->next;
1471 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 if (unlikely(ptr >= end))
1474 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001477 /*
1478 * First, states that are specific to the response only.
1479 * We check them first so that request and headers are
1480 * closer to each other (accessed more often).
1481 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001482 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001483 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001484 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001485 /* we have a start of message, but we have to check
1486 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001487 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001488 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001489 if (unlikely(ptr != buf->p)) {
1490 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001491 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001492 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001493 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001494 }
Willy Tarreau26927362012-05-18 23:22:52 +02001495 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001496 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001497 hdr_idx_init(idx);
1498 state = HTTP_MSG_RPVER;
1499 goto http_msg_rpver;
1500 }
1501
1502 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1503 goto http_msg_invalid;
1504
1505 if (unlikely(*ptr == '\n'))
1506 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1507 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1508 /* stop here */
1509
Willy Tarreau8973c702007-01-21 23:58:29 +01001510 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001511 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001512 EXPECT_LF_HERE(ptr, http_msg_invalid);
1513 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1514 /* stop here */
1515
Willy Tarreau8973c702007-01-21 23:58:29 +01001516 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001517 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001518 case HTTP_MSG_RPVER_SP:
1519 case HTTP_MSG_RPCODE:
1520 case HTTP_MSG_RPCODE_SP:
1521 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001522 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001523 state, ptr, end,
1524 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001525 if (unlikely(!ptr))
1526 return;
1527
1528 /* we have a full response and we know that we have either a CR
1529 * or an LF at <ptr>.
1530 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001531 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1532
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001533 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001534 if (likely(*ptr == '\r'))
1535 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1536 goto http_msg_rpline_end;
1537
Willy Tarreau8973c702007-01-21 23:58:29 +01001538 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001539 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001540 /* msg->sol must point to the first of CR or LF. */
1541 EXPECT_LF_HERE(ptr, http_msg_invalid);
1542 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1543 /* stop here */
1544
1545 /*
1546 * Second, states that are specific to the request only
1547 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001549 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001551 /* we have a start of message, but we have to check
1552 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001553 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001554 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001555 if (likely(ptr != buf->p)) {
1556 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001557 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001558 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001559 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 }
Willy Tarreau26927362012-05-18 23:22:52 +02001561 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001562 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001563 state = HTTP_MSG_RQMETH;
1564 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1568 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 if (unlikely(*ptr == '\n'))
1571 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1572 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001573 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001576 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 EXPECT_LF_HERE(ptr, http_msg_invalid);
1578 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001579 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001582 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 case HTTP_MSG_RQMETH_SP:
1584 case HTTP_MSG_RQURI:
1585 case HTTP_MSG_RQURI_SP:
1586 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001587 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001588 state, ptr, end,
1589 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 if (unlikely(!ptr))
1591 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001592
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 /* we have a full request and we know that we have either a CR
1594 * or an LF at <ptr>.
1595 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001597
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001598 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 if (likely(*ptr == '\r'))
1600 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001604 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 /* check for HTTP/0.9 request : no version information available.
1606 * msg->sol must point to the first of CR or LF.
1607 */
1608 if (unlikely(msg->sl.rq.v_l == 0))
1609 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001610
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 EXPECT_LF_HERE(ptr, http_msg_invalid);
1612 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001613 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001614
Willy Tarreau8973c702007-01-21 23:58:29 +01001615 /*
1616 * Common states below
1617 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001619 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001620 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 if (likely(!HTTP_IS_CRLF(*ptr))) {
1622 goto http_msg_hdr_name;
1623 }
1624
1625 if (likely(*ptr == '\r'))
1626 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1627 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001628
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001630 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 /* assumes msg->sol points to the first char */
1632 if (likely(HTTP_IS_TOKEN(*ptr)))
1633 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001634
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001635 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001637
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001638 if (likely(msg->err_pos < -1) || *ptr == '\n')
1639 goto http_msg_invalid;
1640
1641 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001642 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001643
1644 /* and we still accept this non-token character */
1645 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001646
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001648 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001649 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001650 if (likely(HTTP_IS_SPHT(*ptr)))
1651 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001654 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 if (likely(!HTTP_IS_CRLF(*ptr))) {
1657 goto http_msg_hdr_val;
1658 }
1659
1660 if (likely(*ptr == '\r'))
1661 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1662 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001665 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 EXPECT_LF_HERE(ptr, http_msg_invalid);
1667 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001668
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001670 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 if (likely(HTTP_IS_SPHT(*ptr))) {
1672 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001673 for (; buf->p + msg->sov < ptr; msg->sov++)
1674 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 goto http_msg_hdr_l1_sp;
1676 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001677 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001678 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 goto http_msg_complete_header;
1680
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001682 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001683 /* assumes msg->sol points to the first char, and msg->sov
1684 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 */
1686 if (likely(!HTTP_IS_CRLF(*ptr)))
1687 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001688
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001689 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 /* Note: we could also copy eol into ->eoh so that we have the
1691 * real header end in case it ends with lots of LWS, but is this
1692 * really needed ?
1693 */
1694 if (likely(*ptr == '\r'))
1695 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1696 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001697
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001699 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 EXPECT_LF_HERE(ptr, http_msg_invalid);
1701 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001702
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001704 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1706 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001707 for (; buf->p + msg->eol < ptr; msg->eol++)
1708 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 goto http_msg_hdr_val;
1710 }
1711 http_msg_complete_header:
1712 /*
1713 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001714 * Assumes msg->sol points to the first char, msg->sov points
1715 * to the first character of the value and msg->eol to the
1716 * first CR or LF so we know how the line ends. We insert last
1717 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001718 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001719 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 idx, idx->tail) < 0))
1721 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001722
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001723 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 if (likely(!HTTP_IS_CRLF(*ptr))) {
1725 goto http_msg_hdr_name;
1726 }
1727
1728 if (likely(*ptr == '\r'))
1729 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1730 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001731
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001733 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001734 /* Assumes msg->sol points to the first of either CR or LF.
1735 * Sets ->sov and ->next to the total header length, ->eoh to
1736 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1737 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 EXPECT_LF_HERE(ptr, http_msg_invalid);
1739 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001740 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001741 msg->eoh = msg->sol;
1742 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001743 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001744 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001746
1747 case HTTP_MSG_ERROR:
1748 /* this may only happen if we call http_msg_analyser() twice with an error */
1749 break;
1750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001752#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1754 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001755#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001756 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 }
1758 http_msg_ood:
1759 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001760 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001761 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001763
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 http_msg_invalid:
1765 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001766 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001767 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 return;
1769}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001770
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001771/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1772 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1773 * nothing is done and 1 is returned.
1774 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001775static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001776{
1777 int delta;
1778 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001779 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001780
1781 if (msg->sl.rq.v_l != 0)
1782 return 1;
1783
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001784 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1785 if (txn->meth != HTTP_METH_GET)
1786 return 0;
1787
Willy Tarreau9b28e032012-10-12 23:49:43 +02001788 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001789 delta = 0;
1790
1791 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001792 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1793 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001794 }
1795 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001796 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001797 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001798 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001799 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001800 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001801 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001802 NULL, NULL);
1803 if (unlikely(!cur_end))
1804 return 0;
1805
1806 /* we have a full HTTP/1.0 request now and we know that
1807 * we have either a CR or an LF at <ptr>.
1808 */
1809 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1810 return 1;
1811}
1812
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001813/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001814 * and "keep-alive" values. If we already know that some headers may safely
1815 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001816 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1817 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001818 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001819 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1820 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1821 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001822 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001823 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001824void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001825{
Willy Tarreau5b154472009-12-21 20:11:07 +01001826 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001827 const char *hdr_val = "Connection";
1828 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001829
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001830 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001831 return;
1832
Willy Tarreau88d349d2010-01-25 12:15:43 +01001833 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1834 hdr_val = "Proxy-Connection";
1835 hdr_len = 16;
1836 }
1837
Willy Tarreau5b154472009-12-21 20:11:07 +01001838 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001839 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001840 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001841 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1842 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001843 if (to_del & 2)
1844 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001845 else
1846 txn->flags |= TX_CON_KAL_SET;
1847 }
1848 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1849 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001850 if (to_del & 1)
1851 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001852 else
1853 txn->flags |= TX_CON_CLO_SET;
1854 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001855 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1856 txn->flags |= TX_HDR_CONN_UPG;
1857 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001858 }
1859
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001860 txn->flags |= TX_HDR_CONN_PRS;
1861 return;
1862}
Willy Tarreau5b154472009-12-21 20:11:07 +01001863
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001864/* Apply desired changes on the Connection: header. Values may be removed and/or
1865 * added depending on the <wanted> flags, which are exclusively composed of
1866 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1867 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1868 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001869void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001870{
1871 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001872 const char *hdr_val = "Connection";
1873 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001874
1875 ctx.idx = 0;
1876
Willy Tarreau88d349d2010-01-25 12:15:43 +01001877
1878 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1879 hdr_val = "Proxy-Connection";
1880 hdr_len = 16;
1881 }
1882
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001883 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001884 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001885 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1886 if (wanted & TX_CON_KAL_SET)
1887 txn->flags |= TX_CON_KAL_SET;
1888 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001889 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001890 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001891 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1892 if (wanted & TX_CON_CLO_SET)
1893 txn->flags |= TX_CON_CLO_SET;
1894 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001895 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001896 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001897 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001898
1899 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1900 return;
1901
1902 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1903 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001904 hdr_val = "Connection: close";
1905 hdr_len = 17;
1906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection: close";
1908 hdr_len = 23;
1909 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001910 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001911 }
1912
1913 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1914 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001915 hdr_val = "Connection: keep-alive";
1916 hdr_len = 22;
1917 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1918 hdr_val = "Proxy-Connection: keep-alive";
1919 hdr_len = 28;
1920 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001921 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001922 }
1923 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001924}
1925
Willy Tarreauc24715e2014-04-17 15:21:20 +02001926/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1927 * the first byte of data after the chunk size, so that we know we can forward
1928 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1929 * the chunk size. That way it is always possible to differentiate between the
1930 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001931 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001932 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001933 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001934static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001935{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001936 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001937 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001938 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001939 const char *end = buf->data + buf->size;
1940 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001941 unsigned int chunk = 0;
1942
1943 /* The chunk size is in the following form, though we are only
1944 * interested in the size and CRLF :
1945 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1946 */
1947 while (1) {
1948 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001949 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001950 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001952 if (c < 0) /* not a hex digit anymore */
1953 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001954 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001955 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001956 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001957 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001958 chunk = (chunk << 4) + c;
1959 }
1960
Willy Tarreaud98cf932009-12-27 22:54:55 +01001961 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001962 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001963 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001964
1965 while (http_is_spht[(unsigned char)*ptr]) {
1966 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001967 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001968 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001969 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001970 }
1971
Willy Tarreaud98cf932009-12-27 22:54:55 +01001972 /* Up to there, we know that at least one byte is present at *ptr. Check
1973 * for the end of chunk size.
1974 */
1975 while (1) {
1976 if (likely(HTTP_IS_CRLF(*ptr))) {
1977 /* we now have a CR or an LF at ptr */
1978 if (likely(*ptr == '\r')) {
1979 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001980 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001981 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001982 return 0;
1983 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001984
Willy Tarreaud98cf932009-12-27 22:54:55 +01001985 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001986 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001987 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001988 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001989 /* done */
1990 break;
1991 }
1992 else if (*ptr == ';') {
1993 /* chunk extension, ends at next CRLF */
1994 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001995 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001996 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001997 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001998
1999 while (!HTTP_IS_CRLF(*ptr)) {
2000 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002001 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002002 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002003 return 0;
2004 }
2005 /* we have a CRLF now, loop above */
2006 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002007 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002008 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002009 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002010 }
2011
Willy Tarreaud98cf932009-12-27 22:54:55 +01002012 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002013 * which may or may not be present. We save that into ->next,
2014 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002015 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002016 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002017 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002018 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002019 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002020 msg->chunk_len = chunk;
2021 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002022 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002023 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002024 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002025 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002026 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002027}
2028
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002029/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002030 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002031 * the trailers is found, it is automatically scheduled to be forwarded,
2032 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2033 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002034 * except maybe msg->next if it could parse some lines, and returns zero.
2035 * If a parse error is encountered, the function returns < 0 and does not
2036 * change anything except maybe msg->next. Note that the message must
2037 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002038 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002039 * forwarding, and that msg->next exactly matches the length of trailers
2040 * already parsed and not forwarded. It is also important to note that this
2041 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002043static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002044{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002045 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002046
Willy Tarreaua458b672012-03-05 11:17:50 +01002047 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002048 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002049 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002050 const char *ptr = b_ptr(buf, msg->next);
2051 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002052 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002053
2054 /* scan current line and stop at LF or CRLF */
2055 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002056 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002057 return 0;
2058
2059 if (*ptr == '\n') {
2060 if (!p1)
2061 p1 = ptr;
2062 p2 = ptr;
2063 break;
2064 }
2065
2066 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002067 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002068 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002069 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002070 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002071 p1 = ptr;
2072 }
2073
2074 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002075 if (ptr >= buf->data + buf->size)
2076 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002077 }
2078
2079 /* after LF; point to beginning of next line */
2080 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002081 if (p2 >= buf->data + buf->size)
2082 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002083
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002084 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002085 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002086 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002087
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002088 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002089 /* LF/CRLF at beginning of line => end of trailers at p2.
2090 * Everything was scheduled for forwarding, there's nothing
2091 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002092 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002093 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002094 msg->msg_state = HTTP_MSG_DONE;
2095 return 1;
2096 }
2097 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002098 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002099 }
2100}
2101
Willy Tarreauc24715e2014-04-17 15:21:20 +02002102/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2103 * or a possible LF alone at the end of a chunk. It automatically adjusts
2104 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002105 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002106 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2107 * not enough data are available, the function does not change anything and
2108 * returns zero. If a parse error is encountered, the function returns < 0 and
2109 * does not change anything. Note: this function is designed to parse wrapped
2110 * CRLF at the end of the buffer.
2111 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002112static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002113{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002114 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002115 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002116 int bytes;
2117
2118 /* NB: we'll check data availabilty at the end. It's not a
2119 * problem because whatever we match first will be checked
2120 * against the correct length.
2121 */
2122 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002123 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002124 if (*ptr == '\r') {
2125 bytes++;
2126 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002127 if (ptr >= buf->data + buf->size)
2128 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002129 }
2130
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002131 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002132 return 0;
2133
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002134 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002135 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002137 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002138
2139 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002140 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002141 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002142 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002143 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002144 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2145 return 1;
2146}
Willy Tarreau5b154472009-12-21 20:11:07 +01002147
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002148/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2149 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2150 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2151 * Unparsable qvalues return 1000 as "q=1.000".
2152 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002153int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002154{
2155 int q = 1000;
2156
2157 if (!isdigit(*qvalue))
2158 goto out;
2159 q = (*qvalue++ - '0') * 1000;
2160
2161 if (*qvalue++ != '.')
2162 goto out;
2163
2164 if (!isdigit(*qvalue))
2165 goto out;
2166 q += (*qvalue++ - '0') * 100;
2167
2168 if (!isdigit(*qvalue))
2169 goto out;
2170 q += (*qvalue++ - '0') * 10;
2171
2172 if (!isdigit(*qvalue))
2173 goto out;
2174 q += (*qvalue++ - '0') * 1;
2175 out:
2176 if (q > 1000)
2177 q = 1000;
Thierry FOURNIERad903512014-04-11 17:51:01 +02002178 if (*end)
2179 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002180 return q;
2181}
William Lallemand82fe75c2012-10-23 10:25:10 +02002182
2183/*
2184 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002185 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002186int select_compression_request_header(struct session *s, struct buffer *req)
2187{
2188 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002189 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002190 struct hdr_ctx ctx;
2191 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002192 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002193
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002194 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2195 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002196 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2197 */
2198 ctx.idx = 0;
2199 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2200 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002201 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2202 (ctx.vlen < 31 ||
2203 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2204 ctx.line[ctx.val + 30] < '6' ||
2205 (ctx.line[ctx.val + 30] == '6' &&
2206 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2207 s->comp_algo = NULL;
2208 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002209 }
2210
William Lallemand82fe75c2012-10-23 10:25:10 +02002211 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002212 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002213 int best_q = 0;
2214
William Lallemand82fe75c2012-10-23 10:25:10 +02002215 ctx.idx = 0;
2216 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002217 const char *qval;
2218 int q;
2219 int toklen;
2220
2221 /* try to isolate the token from the optional q-value */
2222 toklen = 0;
2223 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2224 toklen++;
2225
2226 qval = ctx.line + ctx.val + toklen;
2227 while (1) {
2228 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2229 qval++;
2230
2231 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2232 qval = NULL;
2233 break;
2234 }
2235 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002236
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002237 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2238 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002239
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002240 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2241 qval = NULL;
2242 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002243 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002244 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2245 break;
2246
2247 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2248 qval++;
2249 }
2250
2251 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002252 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002253
2254 if (q <= best_q)
2255 continue;
2256
2257 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2258 if (*(ctx.line + ctx.val) == '*' ||
2259 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2260 s->comp_algo = comp_algo;
2261 best_q = q;
2262 break;
2263 }
2264 }
2265 }
2266 }
2267
2268 /* remove all occurrences of the header when "compression offload" is set */
2269 if (s->comp_algo) {
2270 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2271 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2272 ctx.idx = 0;
2273 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2274 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002275 }
2276 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002277 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002278 }
2279
2280 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002281 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2282 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002283 if (comp_algo->add_data == identity_add_data) {
2284 s->comp_algo = comp_algo;
2285 return 1;
2286 }
2287 }
2288 }
2289
2290 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002291 return 0;
2292}
2293
2294/*
2295 * Selects a comression algorithm depending of the server response.
2296 */
2297int select_compression_response_header(struct session *s, struct buffer *res)
2298{
2299 struct http_txn *txn = &s->txn;
2300 struct http_msg *msg = &txn->rsp;
2301 struct hdr_ctx ctx;
2302 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002303
2304 /* no common compression algorithm was found in request header */
2305 if (s->comp_algo == NULL)
2306 goto fail;
2307
2308 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002309 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002310 goto fail;
2311
William Lallemandd3002612012-11-26 14:34:47 +01002312 /* 200 only */
2313 if (txn->status != 200)
2314 goto fail;
2315
William Lallemand82fe75c2012-10-23 10:25:10 +02002316 /* Content-Length is null */
2317 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2318 goto fail;
2319
Willy Tarreau667c2a32013-04-09 08:13:58 +02002320 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2321 if (msg->flags & HTTP_MSGF_TE_CHNK)
2322 goto fail;
2323
William Lallemand82fe75c2012-10-23 10:25:10 +02002324 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002325 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002326 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2327 goto fail;
2328
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002329 /* no compression when Cache-Control: no-transform is present in the message */
2330 ctx.idx = 0;
2331 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2332 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2333 goto fail;
2334 }
2335
William Lallemand82fe75c2012-10-23 10:25:10 +02002336 comp_type = NULL;
2337
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002338 /* we don't want to compress multipart content-types, nor content-types that are
2339 * not listed in the "compression type" directive if any. If no content-type was
2340 * found but configuration requires one, we don't compress either. Backend has
2341 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002342 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002343 ctx.idx = 0;
2344 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2345 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2346 goto fail;
2347
2348 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2349 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002350 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002351 if (ctx.vlen >= comp_type->name_len &&
2352 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002353 /* this Content-Type should be compressed */
2354 break;
2355 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002356 /* this Content-Type should not be compressed */
2357 if (comp_type == NULL)
2358 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002359 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002360 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002361 else { /* no content-type header */
2362 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2363 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002364 }
2365
William Lallemandd85f9172012-11-09 17:05:39 +01002366 /* limit compression rate */
2367 if (global.comp_rate_lim > 0)
2368 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2369 goto fail;
2370
William Lallemand072a2bf2012-11-20 17:01:01 +01002371 /* limit cpu usage */
2372 if (idle_pct < compress_min_idle)
2373 goto fail;
2374
William Lallemand4c49fae2012-11-07 15:00:23 +01002375 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002376 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002377 goto fail;
2378
William Lallemandec3e3892012-11-12 17:02:18 +01002379 s->flags |= SN_COMP_READY;
2380
William Lallemand82fe75c2012-10-23 10:25:10 +02002381 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002382 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002383 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2384 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2385
2386 /* add Transfer-Encoding header */
2387 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2388 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2389
2390 /*
2391 * Add Content-Encoding header when it's not identity encoding.
2392 * RFC 2616 : Identity encoding: This content-coding is used only in the
2393 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2394 * header.
2395 */
2396 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002397 trash.len = 18;
2398 memcpy(trash.str, "Content-Encoding: ", trash.len);
2399 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2400 trash.len += s->comp_algo->name_len;
2401 trash.str[trash.len] = '\0';
2402 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002403 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002404 return 1;
2405
2406fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002407 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002408 return 0;
2409}
2410
2411
Willy Tarreaud787e662009-07-07 10:14:51 +02002412/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2413 * processing can continue on next analysers, or zero if it either needs more
2414 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2415 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2416 * when it has nothing left to do, and may remove any analyser when it wants to
2417 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002418 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002419int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002420{
Willy Tarreau59234e92008-11-30 23:51:27 +01002421 /*
2422 * We will parse the partial (or complete) lines.
2423 * We will check the request syntax, and also join multi-line
2424 * headers. An index of all the lines will be elaborated while
2425 * parsing.
2426 *
2427 * For the parsing, we use a 28 states FSM.
2428 *
2429 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002430 * req->buf->p = beginning of request
2431 * req->buf->p + msg->eoh = end of processed headers / start of current one
2432 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002433 * msg->eol = end of current header or line (LF or CRLF)
2434 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002435 *
2436 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002437 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002438 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2439 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002440 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002441
Willy Tarreau59234e92008-11-30 23:51:27 +01002442 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002443 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002444 struct http_txn *txn = &s->txn;
2445 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002446 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002447
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002448 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002449 now_ms, __FUNCTION__,
2450 s,
2451 req,
2452 req->rex, req->wex,
2453 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002454 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002455 req->analysers);
2456
Willy Tarreau52a0c602009-08-16 22:45:38 +02002457 /* we're speaking HTTP here, so let's speak HTTP to the client */
2458 s->srv_error = http_return_srv_error;
2459
Willy Tarreau83e3af02009-12-28 17:39:57 +01002460 /* There's a protected area at the end of the buffer for rewriting
2461 * purposes. We don't want to start to parse the request if the
2462 * protected area is affected, because we may have to move processed
2463 * data later, which is much more complicated.
2464 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002465 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002466 if (txn->flags & TX_NOT_FIRST) {
2467 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002468 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002469 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002470 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002471 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002472 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002473 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002474 return 0;
2475 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002476 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2477 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2478 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002479 }
2480
Willy Tarreau065e8332010-01-08 00:30:20 +01002481 /* Note that we have the same problem with the response ; we
2482 * may want to send a redirect, error or anything which requires
2483 * some spare space. So we'll ensure that we have at least
2484 * maxrewrite bytes available in the response buffer before
2485 * processing that one. This will only affect pipelined
2486 * keep-alive requests.
2487 */
2488 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002489 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002490 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2491 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2492 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002493 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002494 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002495 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002496 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002497 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002498 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002499 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002500 return 0;
2501 }
2502 }
2503
Willy Tarreau9b28e032012-10-12 23:49:43 +02002504 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002505 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002506 }
2507
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 /* 1: we might have to print this header in debug mode */
2509 if (unlikely((global.mode & MODE_DEBUG) &&
2510 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002511 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002512 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002513
Willy Tarreau9b28e032012-10-12 23:49:43 +02002514 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002515 /* this is a bit complex : in case of error on the request line,
2516 * we know that rq.l is still zero, so we display only the part
2517 * up to the end of the line (truncated by debug_hdr).
2518 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002519 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002520 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002521
Willy Tarreau59234e92008-11-30 23:51:27 +01002522 sol += hdr_idx_first_pos(&txn->hdr_idx);
2523 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002524
Willy Tarreau59234e92008-11-30 23:51:27 +01002525 while (cur_idx) {
2526 eol = sol + txn->hdr_idx.v[cur_idx].len;
2527 debug_hdr("clihdr", s, sol, eol);
2528 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2529 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002530 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002531 }
2532
Willy Tarreau58f10d72006-12-04 02:26:12 +01002533
Willy Tarreau59234e92008-11-30 23:51:27 +01002534 /*
2535 * Now we quickly check if we have found a full valid request.
2536 * If not so, we check the FD and buffer states before leaving.
2537 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002538 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002539 * requests are checked first. When waiting for a second request
2540 * on a keep-alive session, if we encounter and error, close, t/o,
2541 * we note the error in the session flags but don't set any state.
2542 * Since the error will be noted there, it will not be counted by
2543 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002544 * Last, we may increase some tracked counters' http request errors on
2545 * the cases that are deliberately the client's fault. For instance,
2546 * a timeout or connection reset is not counted as an error. However
2547 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002548 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002549
Willy Tarreau655dce92009-11-08 13:10:58 +01002550 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002551 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002552 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002553 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002554 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002555 session_inc_http_req_ctr(s);
2556 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002557 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002558 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002559 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002560
Willy Tarreau59234e92008-11-30 23:51:27 +01002561 /* 1: Since we are in header mode, if there's no space
2562 * left for headers, we won't be able to free more
2563 * later, so the session will never terminate. We
2564 * must terminate it now.
2565 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002566 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002567 /* FIXME: check if URI is set and return Status
2568 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002569 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002570 session_inc_http_req_ctr(s);
2571 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002572 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002573 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002574 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002575 goto return_bad_req;
2576 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002577
Willy Tarreau59234e92008-11-30 23:51:27 +01002578 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002579 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002580 if (!(s->flags & SN_ERR_MASK))
2581 s->flags |= SN_ERR_CLICL;
2582
Willy Tarreaufcffa692010-01-10 14:21:19 +01002583 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002584 goto failed_keep_alive;
2585
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002587 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002588 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002589 session_inc_http_err_ctr(s);
2590 }
2591
Willy Tarreaudc979f22012-12-04 10:39:01 +01002592 txn->status = 400;
2593 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002594 msg->msg_state = HTTP_MSG_ERROR;
2595 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002596
Willy Tarreauda7ff642010-06-23 11:44:09 +02002597 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002598 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002599 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002600 if (s->listener->counters)
2601 s->listener->counters->failed_req++;
2602
Willy Tarreau59234e92008-11-30 23:51:27 +01002603 if (!(s->flags & SN_FINST_MASK))
2604 s->flags |= SN_FINST_R;
2605 return 0;
2606 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002607
Willy Tarreau59234e92008-11-30 23:51:27 +01002608 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002609 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002610 if (!(s->flags & SN_ERR_MASK))
2611 s->flags |= SN_ERR_CLITO;
2612
Willy Tarreaufcffa692010-01-10 14:21:19 +01002613 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002614 goto failed_keep_alive;
2615
Willy Tarreau59234e92008-11-30 23:51:27 +01002616 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002617 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002618 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002619 session_inc_http_err_ctr(s);
2620 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002621 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002622 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002623 msg->msg_state = HTTP_MSG_ERROR;
2624 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002625
Willy Tarreauda7ff642010-06-23 11:44:09 +02002626 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002627 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002628 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002629 if (s->listener->counters)
2630 s->listener->counters->failed_req++;
2631
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 if (!(s->flags & SN_FINST_MASK))
2633 s->flags |= SN_FINST_R;
2634 return 0;
2635 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002636
Willy Tarreau59234e92008-11-30 23:51:27 +01002637 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002638 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002639 if (!(s->flags & SN_ERR_MASK))
2640 s->flags |= SN_ERR_CLICL;
2641
Willy Tarreaufcffa692010-01-10 14:21:19 +01002642 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002643 goto failed_keep_alive;
2644
Willy Tarreau4076a152009-04-02 15:18:36 +02002645 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002646 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002647 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002648 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002649 msg->msg_state = HTTP_MSG_ERROR;
2650 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002651
Willy Tarreauda7ff642010-06-23 11:44:09 +02002652 session_inc_http_err_ctr(s);
2653 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002654 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002655 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002656 if (s->listener->counters)
2657 s->listener->counters->failed_req++;
2658
Willy Tarreau59234e92008-11-30 23:51:27 +01002659 if (!(s->flags & SN_FINST_MASK))
2660 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002661 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002662 }
2663
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002664 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002665 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2666 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002667#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002668 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002669 /* We need more data, we have to re-enable quick-ack in case we
2670 * previously disabled it, otherwise we might cause the client
2671 * to delay next data.
2672 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002673 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002674 }
2675#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002676
Willy Tarreaufcffa692010-01-10 14:21:19 +01002677 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2678 /* If the client starts to talk, let's fall back to
2679 * request timeout processing.
2680 */
2681 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002682 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002683 }
2684
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002686 if (!tick_isset(req->analyse_exp)) {
2687 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2688 (txn->flags & TX_WAIT_NEXT_RQ) &&
2689 tick_isset(s->be->timeout.httpka))
2690 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2691 else
2692 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2693 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002694
Willy Tarreau59234e92008-11-30 23:51:27 +01002695 /* we're not ready yet */
2696 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002697
2698 failed_keep_alive:
2699 /* Here we process low-level errors for keep-alive requests. In
2700 * short, if the request is not the first one and it experiences
2701 * a timeout, read error or shutdown, we just silently close so
2702 * that the client can try again.
2703 */
2704 txn->status = 0;
2705 msg->msg_state = HTTP_MSG_RQBEFORE;
2706 req->analysers = 0;
2707 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002708 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002709 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002710 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002711 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002712 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002713
Willy Tarreaud787e662009-07-07 10:14:51 +02002714 /* OK now we have a complete HTTP request with indexed headers. Let's
2715 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002716 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002717 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002718 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002719 * byte after the last LF. msg->sov points to the first byte of data.
2720 * msg->eol cannot be trusted because it may have been left uninitialized
2721 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002722 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002723
Willy Tarreauda7ff642010-06-23 11:44:09 +02002724 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002725 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2726
Willy Tarreaub16a5742010-01-10 14:46:16 +01002727 if (txn->flags & TX_WAIT_NEXT_RQ) {
2728 /* kill the pending keep-alive timeout */
2729 txn->flags &= ~TX_WAIT_NEXT_RQ;
2730 req->analyse_exp = TICK_ETERNITY;
2731 }
2732
2733
Willy Tarreaud787e662009-07-07 10:14:51 +02002734 /* Maybe we found in invalid header name while we were configured not
2735 * to block on that, so we have to capture it now.
2736 */
2737 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002738 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002739
Willy Tarreau59234e92008-11-30 23:51:27 +01002740 /*
2741 * 1: identify the method
2742 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002743 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002744
2745 /* we can make use of server redirect on GET and HEAD */
2746 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2747 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002748
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 /*
2750 * 2: check if the URI matches the monitor_uri.
2751 * We have to do this for every request which gets in, because
2752 * the monitor-uri is defined by the frontend.
2753 */
2754 if (unlikely((s->fe->monitor_uri_len != 0) &&
2755 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002756 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002757 s->fe->monitor_uri,
2758 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002759 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002760 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002761 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002762 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002763
Willy Tarreau59234e92008-11-30 23:51:27 +01002764 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002765 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002766
Willy Tarreau59234e92008-11-30 23:51:27 +01002767 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002768 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002769 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002770
Willy Tarreau59234e92008-11-30 23:51:27 +01002771 ret = acl_pass(ret);
2772 if (cond->pol == ACL_COND_UNLESS)
2773 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002774
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 if (ret) {
2776 /* we fail this request, let's return 503 service unavail */
2777 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002778 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002779 if (!(s->flags & SN_ERR_MASK))
2780 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002781 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002782 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002783 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002784
Willy Tarreau59234e92008-11-30 23:51:27 +01002785 /* nothing to fail, let's reply normaly */
2786 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002787 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002788 if (!(s->flags & SN_ERR_MASK))
2789 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002790 goto return_prx_cond;
2791 }
2792
2793 /*
2794 * 3: Maybe we have to copy the original REQURI for the logs ?
2795 * Note: we cannot log anymore if the request has been
2796 * classified as invalid.
2797 */
2798 if (unlikely(s->logs.logwait & LW_REQ)) {
2799 /* we have a complete HTTP request that we must log */
2800 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2801 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002802
Willy Tarreau59234e92008-11-30 23:51:27 +01002803 if (urilen >= REQURI_LEN)
2804 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002805 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002806 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002807
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002808 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002809 s->do_log(s);
2810 } else {
2811 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002812 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002813 }
Willy Tarreau06619262006-12-17 08:37:22 +01002814
Willy Tarreau59234e92008-11-30 23:51:27 +01002815 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002816 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002817 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002818
Willy Tarreau5b154472009-12-21 20:11:07 +01002819 /* ... and check if the request is HTTP/1.1 or above */
2820 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002821 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2822 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2823 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002824 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002825
2826 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002827 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002828
Willy Tarreau88d349d2010-01-25 12:15:43 +01002829 /* if the frontend has "option http-use-proxy-header", we'll check if
2830 * we have what looks like a proxied connection instead of a connection,
2831 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2832 * Note that this is *not* RFC-compliant, however browsers and proxies
2833 * happen to do that despite being non-standard :-(
2834 * We consider that a request not beginning with either '/' or '*' is
2835 * a proxied connection, which covers both "scheme://location" and
2836 * CONNECT ip:port.
2837 */
2838 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002839 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002840 txn->flags |= TX_USE_PX_CONN;
2841
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002842 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002843 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002844
Willy Tarreau59234e92008-11-30 23:51:27 +01002845 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002846 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002847 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002848 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002849
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002850 /* 6: determine the transfer-length.
2851 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2852 * the presence of a message-body in a REQUEST and its transfer length
2853 * must be determined that way (in order of precedence) :
2854 * 1. The presence of a message-body in a request is signaled by the
2855 * inclusion of a Content-Length or Transfer-Encoding header field
2856 * in the request's header fields. When a request message contains
2857 * both a message-body of non-zero length and a method that does
2858 * not define any semantics for that request message-body, then an
2859 * origin server SHOULD either ignore the message-body or respond
2860 * with an appropriate error message (e.g., 413). A proxy or
2861 * gateway, when presented the same request, SHOULD either forward
2862 * the request inbound with the message- body or ignore the
2863 * message-body when determining a response.
2864 *
2865 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2866 * and the "chunked" transfer-coding (Section 6.2) is used, the
2867 * transfer-length is defined by the use of this transfer-coding.
2868 * If a Transfer-Encoding header field is present and the "chunked"
2869 * transfer-coding is not present, the transfer-length is defined
2870 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002871 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002872 * 3. If a Content-Length header field is present, its decimal value in
2873 * OCTETs represents both the entity-length and the transfer-length.
2874 * If a message is received with both a Transfer-Encoding header
2875 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002876 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002877 * 4. By the server closing the connection. (Closing the connection
2878 * cannot be used to indicate the end of a request body, since that
2879 * would leave no possibility for the server to send back a response.)
2880 *
2881 * Whenever a transfer-coding is applied to a message-body, the set of
2882 * transfer-codings MUST include "chunked", unless the message indicates
2883 * it is terminated by closing the connection. When the "chunked"
2884 * transfer-coding is used, it MUST be the last transfer-coding applied
2885 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002886 */
2887
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002888 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002889 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002890 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002891 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002892 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002893 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002894 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2895 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002896 /* bad transfer-encoding (chunked followed by something else) */
2897 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002898 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002899 break;
2900 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002901 }
2902
Willy Tarreau32b47f42009-10-18 20:55:02 +02002903 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002904 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002905 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002906 signed long long cl;
2907
Willy Tarreauad14f752011-09-02 20:33:27 +02002908 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002909 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002910 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002911 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002912
Willy Tarreauad14f752011-09-02 20:33:27 +02002913 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002914 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002915 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002916 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002917
Willy Tarreauad14f752011-09-02 20:33:27 +02002918 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002919 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002920 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002921 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002922
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002923 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002924 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002925 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002926 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002927
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002928 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002929 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002930 }
2931
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002932 /* bodyless requests have a known length */
2933 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002934 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002935
Willy Tarreaud787e662009-07-07 10:14:51 +02002936 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002937 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002938 req->analyse_exp = TICK_ETERNITY;
2939 return 1;
2940
2941 return_bad_req:
2942 /* We centralize bad requests processing here */
2943 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2944 /* we detected a parsing error. We want to archive this request
2945 * in the dedicated proxy area for later troubleshooting.
2946 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002947 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002948 }
2949
2950 txn->req.msg_state = HTTP_MSG_ERROR;
2951 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002952 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002953
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002954 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002955 if (s->listener->counters)
2956 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002957
2958 return_prx_cond:
2959 if (!(s->flags & SN_ERR_MASK))
2960 s->flags |= SN_ERR_PRXCOND;
2961 if (!(s->flags & SN_FINST_MASK))
2962 s->flags |= SN_FINST_R;
2963
2964 req->analysers = 0;
2965 req->analyse_exp = TICK_ETERNITY;
2966 return 0;
2967}
2968
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002969
Willy Tarreau347a35d2013-11-22 17:51:09 +01002970/* This function prepares an applet to handle the stats. It can deal with the
2971 * "100-continue" expectation, check that admin rules are met for POST requests,
2972 * and program a response message if something was unexpected. It cannot fail
2973 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002974 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002975 * s->target which is supposed to already point to the stats applet. The caller
2976 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002977 */
2978int http_handle_stats(struct session *s, struct channel *req)
2979{
2980 struct stats_admin_rule *stats_admin_rule;
2981 struct stream_interface *si = s->rep->prod;
2982 struct http_txn *txn = &s->txn;
2983 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002984 struct uri_auth *uri_auth = s->be->uri_auth;
2985 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002986 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002987
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002988 appctx = si_appctx(si);
2989 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2990 appctx->st1 = appctx->st2 = 0;
2991 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2992 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002993
2994 uri = msg->chn->buf->p + msg->sl.rq.u;
2995 lookup = uri + uri_auth->uri_len;
2996
2997 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2998 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002999 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003000 break;
3001 }
3002 }
3003
3004 if (uri_auth->refresh) {
3005 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3006 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003007 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003008 break;
3009 }
3010 }
3011 }
3012
3013 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3014 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003015 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003016 break;
3017 }
3018 }
3019
3020 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3021 if (memcmp(h, ";st=", 4) == 0) {
3022 int i;
3023 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003024 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003025 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3026 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003027 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003028 break;
3029 }
3030 }
3031 break;
3032 }
3033 }
3034
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003035 appctx->ctx.stats.scope_str = 0;
3036 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003037 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3038 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3039 int itx = 0;
3040 const char *h2;
3041 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3042 const char *err;
3043
3044 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3045 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003046 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003047 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3048 itx++;
3049 h++;
3050 }
3051
3052 if (itx > STAT_SCOPE_TXT_MAXLEN)
3053 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003054 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003055
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003056 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003057 memcpy(scope_txt, h2, itx);
3058 scope_txt[itx] = '\0';
3059 err = invalid_char(scope_txt);
3060 if (err) {
3061 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003062 appctx->ctx.stats.scope_str = 0;
3063 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003064 }
3065 break;
3066 }
3067 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003068
3069 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003070 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003071 int ret = 1;
3072
3073 if (stats_admin_rule->cond) {
3074 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3075 ret = acl_pass(ret);
3076 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3077 ret = !ret;
3078 }
3079
3080 if (ret) {
3081 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003082 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003083 break;
3084 }
3085 }
3086
3087 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003088 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003089 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003090 if (msg->msg_state < HTTP_MSG_100_SENT) {
3091 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3092 * send an HTTP/1.1 100 Continue intermediate response.
3093 */
3094 if (msg->flags & HTTP_MSGF_VER_11) {
3095 struct hdr_ctx ctx;
3096 ctx.idx = 0;
3097 /* Expect is allowed in 1.1, look for it */
3098 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3099 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3100 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
3101 }
3102 }
3103 msg->msg_state = HTTP_MSG_100_SENT;
3104 s->logs.tv_request = now; /* update the request timer to reflect full request */
3105 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003106 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003107 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003108 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003109 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3110 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003111 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003112 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003113 else {
3114 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003115 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003116 }
3117
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003118 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003119 return 1;
3120}
3121
Lukas Tribus67db8df2013-06-23 17:37:13 +02003122/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3123 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3124 */
3125static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3126{
3127#ifdef IP_TOS
3128 if (from.ss_family == AF_INET)
3129 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3130#endif
3131#ifdef IPV6_TCLASS
3132 if (from.ss_family == AF_INET6) {
3133 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3134 /* v4-mapped addresses need IP_TOS */
3135 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3136 else
3137 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3138 }
3139#endif
3140}
3141
Willy Tarreau20b0de52012-12-24 15:45:22 +01003142/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003143 * transaction <txn>. Returns the first rule that prevents further processing
3144 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3145 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3146 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003147 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003148static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003149http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003150{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003151 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003152 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003153 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003154
Willy Tarreauff011f22011-01-06 17:51:27 +01003155 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003156 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003157 continue;
3158
Willy Tarreau96257ec2012-12-27 10:46:37 +01003159 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003160 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003161 int ret;
3162
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003163 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003164 ret = acl_pass(ret);
3165
Willy Tarreauff011f22011-01-06 17:51:27 +01003166 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003167 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003168
3169 if (!ret) /* condition not matched */
3170 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003171 }
3172
Willy Tarreau20b0de52012-12-24 15:45:22 +01003173
Willy Tarreau96257ec2012-12-27 10:46:37 +01003174 switch (rule->action) {
3175 case HTTP_REQ_ACT_ALLOW:
3176 return NULL; /* "allow" rules are OK */
3177
3178 case HTTP_REQ_ACT_DENY:
3179 txn->flags |= TX_CLDENY;
3180 return rule;
3181
Willy Tarreauccbcc372012-12-27 12:37:57 +01003182 case HTTP_REQ_ACT_TARPIT:
3183 txn->flags |= TX_CLTARPIT;
3184 return rule;
3185
Willy Tarreau96257ec2012-12-27 10:46:37 +01003186 case HTTP_REQ_ACT_AUTH:
3187 return rule;
3188
Willy Tarreau81499eb2012-12-27 12:19:02 +01003189 case HTTP_REQ_ACT_REDIR:
3190 return rule;
3191
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003192 case HTTP_REQ_ACT_SET_NICE:
3193 s->task->nice = rule->arg.nice;
3194 break;
3195
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003196 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003197 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003198 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003199 break;
3200
Willy Tarreau51347ed2013-06-11 19:34:13 +02003201 case HTTP_REQ_ACT_SET_MARK:
3202#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003203 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003204 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003205#endif
3206 break;
3207
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003208 case HTTP_REQ_ACT_SET_LOGL:
3209 s->logs.level = rule->arg.loglevel;
3210 break;
3211
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003212 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003213 case HTTP_REQ_ACT_SET_HDR:
3214 ctx.idx = 0;
3215 /* remove all occurrences of the header */
3216 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3217 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3218 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003219 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003220 if (rule->action == HTTP_REQ_ACT_DEL_HDR)
3221 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003222 /* now fall through to header addition */
3223
3224 case HTTP_REQ_ACT_ADD_HDR:
3225 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3226 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3227 trash.len = rule->arg.hdr_add.name_len;
3228 trash.str[trash.len++] = ':';
3229 trash.str[trash.len++] = ' ';
3230 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3231 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3232 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003233 }
3234 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003235
3236 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003237 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003238}
3239
Willy Tarreau71241ab2012-12-27 11:30:54 +01003240
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003241/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3242 * transaction <txn>. Returns the first rule that prevents further processing
3243 * of the response (deny, ...) or NULL if it executed all rules or stopped
3244 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3245 * rule.
3246 */
3247static struct http_res_rule *
3248http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3249{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003250 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003251 struct http_res_rule *rule;
3252 struct hdr_ctx ctx;
3253
3254 list_for_each_entry(rule, rules, list) {
3255 if (rule->action >= HTTP_RES_ACT_MAX)
3256 continue;
3257
3258 /* check optional condition */
3259 if (rule->cond) {
3260 int ret;
3261
3262 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3263 ret = acl_pass(ret);
3264
3265 if (rule->cond->pol == ACL_COND_UNLESS)
3266 ret = !ret;
3267
3268 if (!ret) /* condition not matched */
3269 continue;
3270 }
3271
3272
3273 switch (rule->action) {
3274 case HTTP_RES_ACT_ALLOW:
3275 return NULL; /* "allow" rules are OK */
3276
3277 case HTTP_RES_ACT_DENY:
3278 txn->flags |= TX_SVDENY;
3279 return rule;
3280
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003281 case HTTP_RES_ACT_SET_NICE:
3282 s->task->nice = rule->arg.nice;
3283 break;
3284
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003285 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003286 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003287 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003288 break;
3289
Willy Tarreau51347ed2013-06-11 19:34:13 +02003290 case HTTP_RES_ACT_SET_MARK:
3291#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003292 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003293 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003294#endif
3295 break;
3296
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003297 case HTTP_RES_ACT_SET_LOGL:
3298 s->logs.level = rule->arg.loglevel;
3299 break;
3300
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003301 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003302 case HTTP_RES_ACT_SET_HDR:
3303 ctx.idx = 0;
3304 /* remove all occurrences of the header */
3305 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3306 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3307 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3308 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003309 if (rule->action == HTTP_RES_ACT_DEL_HDR)
3310 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003311 /* now fall through to header addition */
3312
3313 case HTTP_RES_ACT_ADD_HDR:
3314 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3315 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3316 trash.len = rule->arg.hdr_add.name_len;
3317 trash.str[trash.len++] = ':';
3318 trash.str[trash.len++] = ' ';
3319 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3320 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3321 break;
3322 }
3323 }
3324
3325 /* we reached the end of the rules, nothing to report */
3326 return NULL;
3327}
3328
3329
Willy Tarreau71241ab2012-12-27 11:30:54 +01003330/* Perform an HTTP redirect based on the information in <rule>. The function
3331 * returns non-zero on success, or zero in case of a, irrecoverable error such
3332 * as too large a request to build a valid response.
3333 */
3334static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3335{
3336 struct http_msg *msg = &txn->req;
3337 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003338 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003339
3340 /* build redirect message */
3341 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003342 case 308:
3343 msg_fmt = HTTP_308;
3344 break;
3345 case 307:
3346 msg_fmt = HTTP_307;
3347 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003348 case 303:
3349 msg_fmt = HTTP_303;
3350 break;
3351 case 301:
3352 msg_fmt = HTTP_301;
3353 break;
3354 case 302:
3355 default:
3356 msg_fmt = HTTP_302;
3357 break;
3358 }
3359
3360 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3361 return 0;
3362
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003363 location = trash.str + trash.len;
3364
Willy Tarreau71241ab2012-12-27 11:30:54 +01003365 switch(rule->type) {
3366 case REDIRECT_TYPE_SCHEME: {
3367 const char *path;
3368 const char *host;
3369 struct hdr_ctx ctx;
3370 int pathlen;
3371 int hostlen;
3372
3373 host = "";
3374 hostlen = 0;
3375 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003376 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003377 host = ctx.line + ctx.val;
3378 hostlen = ctx.vlen;
3379 }
3380
3381 path = http_get_path(txn);
3382 /* build message using path */
3383 if (path) {
3384 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3385 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3386 int qs = 0;
3387 while (qs < pathlen) {
3388 if (path[qs] == '?') {
3389 pathlen = qs;
3390 break;
3391 }
3392 qs++;
3393 }
3394 }
3395 } else {
3396 path = "/";
3397 pathlen = 1;
3398 }
3399
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003400 if (rule->rdr_str) { /* this is an old "redirect" rule */
3401 /* check if we can add scheme + "://" + host + path */
3402 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3403 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003404
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003405 /* add scheme */
3406 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3407 trash.len += rule->rdr_len;
3408 }
3409 else {
3410 /* add scheme with executing log format */
3411 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003412
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003413 /* check if we can add scheme + "://" + host + path */
3414 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3415 return 0;
3416 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003417 /* add "://" */
3418 memcpy(trash.str + trash.len, "://", 3);
3419 trash.len += 3;
3420
3421 /* add host */
3422 memcpy(trash.str + trash.len, host, hostlen);
3423 trash.len += hostlen;
3424
3425 /* add path */
3426 memcpy(trash.str + trash.len, path, pathlen);
3427 trash.len += pathlen;
3428
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003429 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003430 if (trash.len && trash.str[trash.len - 1] != '/' &&
3431 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3432 if (trash.len > trash.size - 5)
3433 return 0;
3434 trash.str[trash.len] = '/';
3435 trash.len++;
3436 }
3437
3438 break;
3439 }
3440 case REDIRECT_TYPE_PREFIX: {
3441 const char *path;
3442 int pathlen;
3443
3444 path = http_get_path(txn);
3445 /* build message using path */
3446 if (path) {
3447 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3448 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3449 int qs = 0;
3450 while (qs < pathlen) {
3451 if (path[qs] == '?') {
3452 pathlen = qs;
3453 break;
3454 }
3455 qs++;
3456 }
3457 }
3458 } else {
3459 path = "/";
3460 pathlen = 1;
3461 }
3462
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003463 if (rule->rdr_str) { /* this is an old "redirect" rule */
3464 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3465 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003466
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003467 /* add prefix. Note that if prefix == "/", we don't want to
3468 * add anything, otherwise it makes it hard for the user to
3469 * configure a self-redirection.
3470 */
3471 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3472 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3473 trash.len += rule->rdr_len;
3474 }
3475 }
3476 else {
3477 /* add prefix with executing log format */
3478 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3479
3480 /* Check length */
3481 if (trash.len + pathlen > trash.size - 4)
3482 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003483 }
3484
3485 /* add path */
3486 memcpy(trash.str + trash.len, path, pathlen);
3487 trash.len += pathlen;
3488
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003489 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003490 if (trash.len && trash.str[trash.len - 1] != '/' &&
3491 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3492 if (trash.len > trash.size - 5)
3493 return 0;
3494 trash.str[trash.len] = '/';
3495 trash.len++;
3496 }
3497
3498 break;
3499 }
3500 case REDIRECT_TYPE_LOCATION:
3501 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003502 if (rule->rdr_str) { /* this is an old "redirect" rule */
3503 if (trash.len + rule->rdr_len > trash.size - 4)
3504 return 0;
3505
3506 /* add location */
3507 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3508 trash.len += rule->rdr_len;
3509 }
3510 else {
3511 /* add location with executing log format */
3512 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003513
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003514 /* Check left length */
3515 if (trash.len > trash.size - 4)
3516 return 0;
3517 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003518 break;
3519 }
3520
3521 if (rule->cookie_len) {
3522 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3523 trash.len += 14;
3524 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3525 trash.len += rule->cookie_len;
3526 memcpy(trash.str + trash.len, "\r\n", 2);
3527 trash.len += 2;
3528 }
3529
3530 /* add end of headers and the keep-alive/close status.
3531 * We may choose to set keep-alive if the Location begins
3532 * with a slash, because the client will come back to the
3533 * same server.
3534 */
3535 txn->status = rule->code;
3536 /* let's log the request time */
3537 s->logs.tv_request = now;
3538
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003539 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003540 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3541 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3542 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3543 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3544 /* keep-alive possible */
3545 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3546 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3547 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3548 trash.len += 30;
3549 } else {
3550 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3551 trash.len += 24;
3552 }
3553 }
3554 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3555 trash.len += 4;
3556 bo_inject(txn->rsp.chn, trash.str, trash.len);
3557 /* "eat" the request */
3558 bi_fast_delete(txn->req.chn->buf, msg->sov);
3559 msg->sov = 0;
3560 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3561 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3562 txn->req.msg_state = HTTP_MSG_CLOSED;
3563 txn->rsp.msg_state = HTTP_MSG_DONE;
3564 } else {
3565 /* keep-alive not possible */
3566 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3567 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3568 trash.len += 29;
3569 } else {
3570 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3571 trash.len += 23;
3572 }
3573 stream_int_retnclose(txn->req.chn->prod, &trash);
3574 txn->req.chn->analysers = 0;
3575 }
3576
3577 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003578 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003579 if (!(s->flags & SN_FINST_MASK))
3580 s->flags |= SN_FINST_R;
3581
3582 return 1;
3583}
3584
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003585/* This stream analyser runs all HTTP request processing which is common to
3586 * frontends and backends, which means blocking ACLs, filters, connection-close,
3587 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003588 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003589 * either needs more data or wants to immediately abort the request (eg: deny,
3590 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003591 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003592int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003593{
Willy Tarreaud787e662009-07-07 10:14:51 +02003594 struct http_txn *txn = &s->txn;
3595 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003596 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003597 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003598 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003599 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003600
Willy Tarreau655dce92009-11-08 13:10:58 +01003601 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003602 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003603 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003604 return 0;
3605 }
3606
Willy Tarreau3a816292009-07-07 10:55:49 +02003607 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003608 req->analyse_exp = TICK_ETERNITY;
3609
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003610 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02003611 now_ms, __FUNCTION__,
3612 s,
3613 req,
3614 req->rex, req->wex,
3615 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003616 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003617 req->analysers);
3618
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003619 /* first check whether we have some ACLs set to block this request */
3620 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003621 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003622
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003623 ret = acl_pass(ret);
3624 if (cond->pol == ACL_COND_UNLESS)
3625 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003626
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003627 if (ret) {
3628 txn->status = 403;
3629 /* let's log the request time */
3630 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003631 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003632 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003633 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003634 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003635 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003636
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003637 /* just in case we have some per-backend tracking */
3638 session_inc_be_http_req_ctr(s);
3639
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003640 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003641 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003642
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003643 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003644 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003645 if (stats_check_uri(s->rep->prod, txn, px)) {
3646 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003647 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3648 txn->status = 500;
3649 s->logs.tv_request = now;
3650 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003651
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003652 if (!(s->flags & SN_ERR_MASK))
3653 s->flags |= SN_ERR_RESOURCE;
3654 goto return_prx_cond;
3655 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003656 /* parse the whole stats request and extract the relevant information */
3657 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003658 http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003659 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003660 }
3661
Willy Tarreau3b44e722013-11-16 10:28:23 +01003662 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3663 * blocked by an http-request rule.
3664 */
3665 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003666 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003667 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003668 }
Willy Tarreau06619262006-12-17 08:37:22 +01003669
Willy Tarreau3b44e722013-11-16 10:28:23 +01003670 /* return a 403 if either rule has blocked */
3671 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003672 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003673 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003674 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003675 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003676 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003677 s->fe->fe_counters.denied_req++;
3678 if (s->fe != s->be)
3679 s->be->be_counters.denied_req++;
3680 if (s->listener->counters)
3681 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003682 goto return_prx_cond;
3683 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003684
3685 /* When a connection is tarpitted, we use the tarpit timeout,
3686 * which may be the same as the connect timeout if unspecified.
3687 * If unset, then set it to zero because we really want it to
3688 * eventually expire. We build the tarpit as an analyser.
3689 */
3690 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003691 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003692 /* wipe the request out so that we can drop the connection early
3693 * if the client closes first.
3694 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003695 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003696 req->analysers = 0; /* remove switching rules etc... */
3697 req->analysers |= AN_REQ_HTTP_TARPIT;
3698 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3699 if (!req->analyse_exp)
3700 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003701 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003702 s->fe->fe_counters.denied_req++;
3703 if (s->fe != s->be)
3704 s->be->be_counters.denied_req++;
3705 if (s->listener->counters)
3706 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003707 return 1;
3708 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003709 }
Willy Tarreau06619262006-12-17 08:37:22 +01003710
Willy Tarreau70dffda2014-01-30 03:07:23 +01003711 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003712 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003713 * Option httpclose by itself sets tunnel mode where headers are mangled.
3714 * However, if another mode is set, it will affect it (eg: server-close/
3715 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3716 * if FE and BE have the same settings (common). The method consists in
3717 * checking if options changed between the two calls (implying that either
3718 * one is non-null, or one of them is non-null and we are there for the first
3719 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003720 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003721
Willy Tarreau416ce612014-01-31 15:45:34 +01003722 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003723 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003724 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003725
Willy Tarreau70dffda2014-01-30 03:07:23 +01003726 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3727 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3728 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3729 tmp = TX_CON_WANT_TUN;
3730
3731 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3732 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3733 tmp = TX_CON_WANT_TUN;
3734 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003735
3736 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003737 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3738 /* option httpclose + server_close => forceclose */
3739 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3740 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3741 tmp = TX_CON_WANT_CLO;
3742 else
3743 tmp = TX_CON_WANT_SCL;
3744 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003745
3746 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3747 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003748 tmp = TX_CON_WANT_CLO;
3749
Willy Tarreau5b154472009-12-21 20:11:07 +01003750 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3751 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003752
Willy Tarreau416ce612014-01-31 15:45:34 +01003753 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3754 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003755 /* parse the Connection header and possibly clean it */
3756 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003757 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003758 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3759 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003760 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003761 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003762 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003763 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003764 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003765
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003766 /* check if client or config asks for explicit close in KAL/SCL */
3767 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3768 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3769 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003770 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003771 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003772 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003773 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3774 }
Willy Tarreau78599912009-10-17 20:12:21 +02003775
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003776 /* we can be blocked here because the request needs to be authenticated,
3777 * either to pass or to access stats.
3778 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003779 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003780 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003781
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003782 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003783 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003784
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003785 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003786 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003787 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003788 /* on 401 we still count one error, because normal browsing
3789 * won't significantly increase the counter but brute force
3790 * attempts will.
3791 */
3792 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003793 goto return_prx_cond;
3794 }
3795
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003796 /* add request headers from the rule sets in the same order */
3797 list_for_each_entry(wl, &px->req_add, list) {
3798 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003799 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003800 ret = acl_pass(ret);
3801 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3802 ret = !ret;
3803 if (!ret)
3804 continue;
3805 }
3806
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003807 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003808 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003809 }
3810
3811 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3812 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3813 goto return_bad_req;
3814 req->analyse_exp = TICK_ETERNITY;
3815 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003816 }
3817
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003818 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003819 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003820 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3821 s->fe->fe_counters.intercepted_req++;
3822
3823 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3824 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3825 if (!(s->flags & SN_FINST_MASK))
3826 s->flags |= SN_FINST_R;
3827
3828 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau51437d22013-12-29 00:43:40 +01003829 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003830 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003831 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003832
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003833 /* check whether we have some ACLs set to redirect this request */
3834 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003835 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003836 int ret;
3837
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003838 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003839 ret = acl_pass(ret);
3840 if (rule->cond->pol == ACL_COND_UNLESS)
3841 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003842 if (!ret)
3843 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003844 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003845 if (!http_apply_redirect_rule(rule, s, txn))
3846 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003847
Willy Tarreau71241ab2012-12-27 11:30:54 +01003848 req->analyse_exp = TICK_ETERNITY;
3849 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003850 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003851
Willy Tarreau2be39392010-01-03 17:24:51 +01003852 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3853 * If this happens, then the data will not come immediately, so we must
3854 * send all what we have without waiting. Note that due to the small gain
3855 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003856 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003857 * itself once used.
3858 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003859 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003860
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003861 /* that's OK for us now, let's move on to next analysers */
3862 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003863
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003864 return_bad_req:
3865 /* We centralize bad requests processing here */
3866 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3867 /* we detected a parsing error. We want to archive this request
3868 * in the dedicated proxy area for later troubleshooting.
3869 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003870 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003871 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003872
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003873 txn->req.msg_state = HTTP_MSG_ERROR;
3874 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003875 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003876
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003877 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003878 if (s->listener->counters)
3879 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003880
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003881 return_prx_cond:
3882 if (!(s->flags & SN_ERR_MASK))
3883 s->flags |= SN_ERR_PRXCOND;
3884 if (!(s->flags & SN_FINST_MASK))
3885 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003886
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003887 req->analysers = 0;
3888 req->analyse_exp = TICK_ETERNITY;
3889 return 0;
3890}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003891
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003892/* This function performs all the processing enabled for the current request.
3893 * It returns 1 if the processing can continue on next analysers, or zero if it
3894 * needs more data, encounters an error, or wants to immediately abort the
3895 * request. It relies on buffers flags, and updates s->req->analysers.
3896 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003897int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003898{
3899 struct http_txn *txn = &s->txn;
3900 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003901 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003902
Willy Tarreau655dce92009-11-08 13:10:58 +01003903 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003904 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003905 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003906 return 0;
3907 }
3908
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003909 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003910 now_ms, __FUNCTION__,
3911 s,
3912 req,
3913 req->rex, req->wex,
3914 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003915 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003916 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003917
William Lallemand82fe75c2012-10-23 10:25:10 +02003918 if (s->fe->comp || s->be->comp)
3919 select_compression_request_header(s, req->buf);
3920
Willy Tarreau59234e92008-11-30 23:51:27 +01003921 /*
3922 * Right now, we know that we have processed the entire headers
3923 * and that unwanted requests have been filtered out. We can do
3924 * whatever we want with the remaining request. Also, now we
3925 * may have separate values for ->fe, ->be.
3926 */
Willy Tarreau06619262006-12-17 08:37:22 +01003927
Willy Tarreau59234e92008-11-30 23:51:27 +01003928 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003929 * If HTTP PROXY is set we simply get remote server address parsing
3930 * incoming request. Note that this requires that a connection is
3931 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003932 */
3933 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003934 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003935 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003936
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003937 /* Note that for now we don't reuse existing proxy connections */
3938 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003939 txn->req.msg_state = HTTP_MSG_ERROR;
3940 txn->status = 500;
3941 req->analysers = 0;
3942 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3943
3944 if (!(s->flags & SN_ERR_MASK))
3945 s->flags |= SN_ERR_RESOURCE;
3946 if (!(s->flags & SN_FINST_MASK))
3947 s->flags |= SN_FINST_R;
3948
3949 return 0;
3950 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003951
3952 path = http_get_path(txn);
3953 url2sa(req->buf->p + msg->sl.rq.u,
3954 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003955 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003956 /* if the path was found, we have to remove everything between
3957 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3958 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3959 * u_l characters by a single "/".
3960 */
3961 if (path) {
3962 char *cur_ptr = req->buf->p;
3963 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3964 int delta;
3965
3966 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3967 http_msg_move_end(&txn->req, delta);
3968 cur_end += delta;
3969 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3970 goto return_bad_req;
3971 }
3972 else {
3973 char *cur_ptr = req->buf->p;
3974 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3975 int delta;
3976
3977 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3978 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3979 http_msg_move_end(&txn->req, delta);
3980 cur_end += delta;
3981 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3982 goto return_bad_req;
3983 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003984 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003985
Willy Tarreau59234e92008-11-30 23:51:27 +01003986 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003987 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003988 * Note that doing so might move headers in the request, but
3989 * the fields will stay coherent and the URI will not move.
3990 * This should only be performed in the backend.
3991 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003992 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003993 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3994 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003995
Willy Tarreau59234e92008-11-30 23:51:27 +01003996 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003997 * 8: the appsession cookie was looked up very early in 1.2,
3998 * so let's do the same now.
3999 */
4000
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004001 /* It needs to look into the URI unless persistence must be ignored */
4002 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004003 get_srv_from_appsession(s, req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01004004 }
4005
William Lallemanda73203e2012-03-12 12:48:57 +01004006 /* add unique-id if "header-unique-id" is specified */
4007
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004008 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4009 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4010 goto return_bad_req;
4011 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004012 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004013 }
William Lallemanda73203e2012-03-12 12:48:57 +01004014
4015 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004016 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4017 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004018 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004019 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004020 goto return_bad_req;
4021 }
4022
Cyril Bontéb21570a2009-11-29 20:04:48 +01004023 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004024 * 9: add X-Forwarded-For if either the frontend or the backend
4025 * asks for it.
4026 */
4027 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004028 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004029 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004030 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4031 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004032 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004033 /* The header is set to be added only if none is present
4034 * and we found it, so don't do anything.
4035 */
4036 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004037 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004038 /* Add an X-Forwarded-For header unless the source IP is
4039 * in the 'except' network range.
4040 */
4041 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004042 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004043 != s->fe->except_net.s_addr) &&
4044 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004045 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004046 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004047 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004048 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004049 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004050
4051 /* Note: we rely on the backend to get the header name to be used for
4052 * x-forwarded-for, because the header is really meant for the backends.
4053 * However, if the backend did not specify any option, we have to rely
4054 * on the frontend's header name.
4055 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004056 if (s->be->fwdfor_hdr_len) {
4057 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004058 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004059 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004060 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004061 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004062 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004063 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01004064
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004065 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004066 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004067 }
4068 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004069 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004070 /* FIXME: for the sake of completeness, we should also support
4071 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004072 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004073 int len;
4074 char pn[INET6_ADDRSTRLEN];
4075 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004076 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004077 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004078
Willy Tarreau59234e92008-11-30 23:51:27 +01004079 /* Note: we rely on the backend to get the header name to be used for
4080 * x-forwarded-for, because the header is really meant for the backends.
4081 * However, if the backend did not specify any option, we have to rely
4082 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004083 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004084 if (s->be->fwdfor_hdr_len) {
4085 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004086 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004087 } else {
4088 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004089 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004090 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004091 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004092
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004093 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004094 goto return_bad_req;
4095 }
4096 }
4097
4098 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004099 * 10: add X-Original-To if either the frontend or the backend
4100 * asks for it.
4101 */
4102 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4103
4104 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004105 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004106 /* Add an X-Original-To header unless the destination IP is
4107 * in the 'except' network range.
4108 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004109 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004110
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004111 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004112 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004113 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004114 != s->fe->except_to.s_addr) &&
4115 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004116 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02004117 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004118 int len;
4119 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004120 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004121
4122 /* Note: we rely on the backend to get the header name to be used for
4123 * x-original-to, because the header is really meant for the backends.
4124 * However, if the backend did not specify any option, we have to rely
4125 * on the frontend's header name.
4126 */
4127 if (s->be->orgto_hdr_len) {
4128 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004129 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004130 } else {
4131 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004132 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004133 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004134 len += snprintf(trash.str + len, trash.size - len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02004135
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004136 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004137 goto return_bad_req;
4138 }
4139 }
4140 }
4141
Willy Tarreau50fc7772012-11-11 22:19:57 +01004142 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4143 * If an "Upgrade" token is found, the header is left untouched in order not to have
4144 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4145 * "Upgrade" is present in the Connection header.
4146 */
4147 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4148 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004149 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4150 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004151 unsigned int want_flags = 0;
4152
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004153 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004154 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004155 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4156 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004157 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004158 want_flags |= TX_CON_CLO_SET;
4159 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004160 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004161 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4162 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004163 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004164 want_flags |= TX_CON_KAL_SET;
4165 }
4166
4167 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004168 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004169 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004170
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004171
Willy Tarreau522d6c02009-12-06 18:49:18 +01004172 /* If we have no server assigned yet and we're balancing on url_param
4173 * with a POST request, we may be interested in checking the body for
4174 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004175 */
4176 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4177 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004178 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004179 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004180 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004181 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004182
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004183 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004184 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004185#ifdef TCP_QUICKACK
4186 /* We expect some data from the client. Unless we know for sure
4187 * we already have a full request, we have to re-enable quick-ack
4188 * in case we previously disabled it, otherwise we might cause
4189 * the client to delay further data.
4190 */
4191 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004192 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004193 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004194 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004195 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004196#endif
4197 }
Willy Tarreau03945942009-12-22 16:50:27 +01004198
Willy Tarreau59234e92008-11-30 23:51:27 +01004199 /*************************************************************
4200 * OK, that's finished for the headers. We have done what we *
4201 * could. Let's switch to the DATA state. *
4202 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004203 req->analyse_exp = TICK_ETERNITY;
4204 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004205
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004206 /* if the server closes the connection, we want to immediately react
4207 * and close the socket to save packets and syscalls.
4208 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004209 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4210 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004211
Willy Tarreau59234e92008-11-30 23:51:27 +01004212 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004213 /* OK let's go on with the BODY now */
4214 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004215
Willy Tarreau59234e92008-11-30 23:51:27 +01004216 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004217 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004218 /* we detected a parsing error. We want to archive this request
4219 * in the dedicated proxy area for later troubleshooting.
4220 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004221 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004222 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004223
Willy Tarreau59234e92008-11-30 23:51:27 +01004224 txn->req.msg_state = HTTP_MSG_ERROR;
4225 txn->status = 400;
4226 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004227 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004228
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004229 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004230 if (s->listener->counters)
4231 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004232
Willy Tarreau59234e92008-11-30 23:51:27 +01004233 if (!(s->flags & SN_ERR_MASK))
4234 s->flags |= SN_ERR_PRXCOND;
4235 if (!(s->flags & SN_FINST_MASK))
4236 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004237 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004238}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004239
Willy Tarreau60b85b02008-11-30 23:28:40 +01004240/* This function is an analyser which processes the HTTP tarpit. It always
4241 * returns zero, at the beginning because it prevents any other processing
4242 * from occurring, and at the end because it terminates the request.
4243 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004244int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004245{
4246 struct http_txn *txn = &s->txn;
4247
4248 /* This connection is being tarpitted. The CLIENT side has
4249 * already set the connect expiration date to the right
4250 * timeout. We just have to check that the client is still
4251 * there and that the timeout has not expired.
4252 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004253 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004254 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004255 !tick_is_expired(req->analyse_exp, now_ms))
4256 return 0;
4257
4258 /* We will set the queue timer to the time spent, just for
4259 * logging purposes. We fake a 500 server error, so that the
4260 * attacker will not suspect his connection has been tarpitted.
4261 * It will not cause trouble to the logs because we can exclude
4262 * the tarpitted connections by filtering on the 'PT' status flags.
4263 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004264 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4265
4266 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004267 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004268 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004269
4270 req->analysers = 0;
4271 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004272
Willy Tarreau60b85b02008-11-30 23:28:40 +01004273 if (!(s->flags & SN_ERR_MASK))
4274 s->flags |= SN_ERR_PRXCOND;
4275 if (!(s->flags & SN_FINST_MASK))
4276 s->flags |= SN_FINST_T;
4277 return 0;
4278}
4279
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004280/* This function is an analyser which waits for the HTTP request body. It waits
4281 * for either the buffer to be full, or the full advertised contents to have
4282 * reached the buffer. It must only be called after the standard HTTP request
4283 * processing has occurred, because it expects the request to be parsed and will
4284 * look for the Expect header. It may send a 100-Continue interim response. It
4285 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4286 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4287 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004288 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004289int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004290{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004291 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004292 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004293
4294 /* We have to parse the HTTP request body to find any required data.
4295 * "balance url_param check_post" should have been the only way to get
4296 * into this. We were brought here after HTTP header analysis, so all
4297 * related structures are ready.
4298 */
4299
Willy Tarreau890988f2014-04-10 11:59:33 +02004300 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4301 /* This is the first call */
4302 if (msg->msg_state < HTTP_MSG_BODY)
4303 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004304
Willy Tarreau890988f2014-04-10 11:59:33 +02004305 if (msg->msg_state < HTTP_MSG_100_SENT) {
4306 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4307 * send an HTTP/1.1 100 Continue intermediate response.
4308 */
4309 if (msg->flags & HTTP_MSGF_VER_11) {
4310 struct hdr_ctx ctx;
4311 ctx.idx = 0;
4312 /* Expect is allowed in 1.1, look for it */
4313 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4314 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4315 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4316 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004317 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004318 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004319 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004320
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004321 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004322 * req->buf->p still points to the beginning of the message. We
4323 * must save the body in msg->next because it survives buffer
4324 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004325 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004326 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004327
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004328 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004329 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4330 else
4331 msg->msg_state = HTTP_MSG_DATA;
4332 }
4333
Willy Tarreau890988f2014-04-10 11:59:33 +02004334 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4335 /* We're in content-length mode, we just have to wait for enough data. */
4336 if (req->buf->i - msg->sov < msg->body_len)
4337 goto missing_data;
4338
4339 /* OK we have everything we need now */
4340 goto http_end;
4341 }
4342
4343 /* OK here we're parsing a chunked-encoded message */
4344
Willy Tarreau522d6c02009-12-06 18:49:18 +01004345 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004346 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004347 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004348 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004349 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004350 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004351
Willy Tarreau115acb92009-12-26 13:56:06 +01004352 if (!ret)
4353 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004354 else if (ret < 0) {
4355 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004356 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004357 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004358 }
4359
Willy Tarreaud98cf932009-12-27 22:54:55 +01004360 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004361 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004362 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004363 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004364 if (msg->msg_state == HTTP_MSG_TRAILERS)
4365 goto http_end;
4366
Willy Tarreau226071e2014-04-10 11:55:45 +02004367 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004368 goto http_end;
4369
4370 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004371 /* we get here if we need to wait for more data. If the buffer is full,
4372 * we have the maximum we can expect.
4373 */
4374 if (buffer_full(req->buf, global.tune.maxrewrite))
4375 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004376
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004377 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004378 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004379 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004380
4381 if (!(s->flags & SN_ERR_MASK))
4382 s->flags |= SN_ERR_CLITO;
4383 if (!(s->flags & SN_FINST_MASK))
4384 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004385 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004386 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004387
4388 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004389 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004390 /* Not enough data. We'll re-use the http-request
4391 * timeout here. Ideally, we should set the timeout
4392 * relative to the accept() date. We just set the
4393 * request timeout once at the beginning of the
4394 * request.
4395 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004396 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004397 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004398 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004399 return 0;
4400 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004401
4402 http_end:
4403 /* The situation will not evolve, so let's give up on the analysis. */
4404 s->logs.tv_request = now; /* update the request timer to reflect full request */
4405 req->analysers &= ~an_bit;
4406 req->analyse_exp = TICK_ETERNITY;
4407 return 1;
4408
4409 return_bad_req: /* let's centralize all bad requests */
4410 txn->req.msg_state = HTTP_MSG_ERROR;
4411 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004412 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004413
Willy Tarreau79ebac62010-06-07 13:47:49 +02004414 if (!(s->flags & SN_ERR_MASK))
4415 s->flags |= SN_ERR_PRXCOND;
4416 if (!(s->flags & SN_FINST_MASK))
4417 s->flags |= SN_FINST_R;
4418
Willy Tarreau522d6c02009-12-06 18:49:18 +01004419 return_err_msg:
4420 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004421 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004422 if (s->listener->counters)
4423 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004424 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004425}
4426
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004427/* send a server's name with an outgoing request over an established connection.
4428 * Note: this function is designed to be called once the request has been scheduled
4429 * for being forwarded. This is the reason why it rewinds the buffer before
4430 * proceeding.
4431 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004432int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004433
4434 struct hdr_ctx ctx;
4435
Mark Lamourinec2247f02012-01-04 13:02:01 -05004436 char *hdr_name = be->server_id_hdr_name;
4437 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004438 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004439 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004440 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004441
William Lallemandd9e90662012-01-30 17:27:17 +01004442 ctx.idx = 0;
4443
Willy Tarreau211cdec2014-04-17 20:18:08 +02004444 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004445 if (old_o) {
4446 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004447 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004448 txn->req.next += old_o;
4449 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004450 }
4451
Willy Tarreau9b28e032012-10-12 23:49:43 +02004452 old_i = chn->buf->i;
4453 while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004454 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004455 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004456 }
4457
4458 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004459 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004460 memcpy(hdr_val, hdr_name, hdr_name_len);
4461 hdr_val += hdr_name_len;
4462 *hdr_val++ = ':';
4463 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004464 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4465 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004466
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004467 if (old_o) {
4468 /* If this was a forwarded request, we must readjust the amount of
4469 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004470 * variations. Note that the current state is >= HTTP_MSG_BODY,
4471 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004472 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004473 old_o += chn->buf->i - old_i;
4474 b_adv(chn->buf, old_o);
4475 txn->req.next -= old_o;
4476 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004477 }
4478
Mark Lamourinec2247f02012-01-04 13:02:01 -05004479 return 0;
4480}
4481
Willy Tarreau610ecce2010-01-04 21:15:02 +01004482/* Terminate current transaction and prepare a new one. This is very tricky
4483 * right now but it works.
4484 */
4485void http_end_txn_clean_session(struct session *s)
4486{
Willy Tarreau068621e2013-12-23 15:11:25 +01004487 int prev_status = s->txn.status;
4488
Willy Tarreau610ecce2010-01-04 21:15:02 +01004489 /* FIXME: We need a more portable way of releasing a backend's and a
4490 * server's connections. We need a safer way to reinitialize buffer
4491 * flags. We also need a more accurate method for computing per-request
4492 * data.
4493 */
4494 http_silent_debug(__LINE__, s);
4495
Willy Tarreau4213a112013-12-15 10:25:42 +01004496 /* unless we're doing keep-alive, we want to quickly close the connection
4497 * to the server.
4498 */
4499 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4500 !si_conn_ready(s->req->cons)) {
4501 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4502 si_shutr(s->req->cons);
4503 si_shutw(s->req->cons);
4504 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004505
4506 http_silent_debug(__LINE__, s);
4507
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004508 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004509 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004510 if (unlikely(s->srv_conn))
4511 sess_change_server(s, NULL);
4512 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004513
4514 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4515 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004516 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004517
4518 if (s->txn.status) {
4519 int n;
4520
4521 n = s->txn.status / 100;
4522 if (n < 1 || n > 5)
4523 n = 0;
4524
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004525 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004526 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004527 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004528 s->fe->fe_counters.p.http.comp_rsp++;
4529 }
Willy Tarreau24657792010-02-26 10:30:28 +01004530 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004531 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004532 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004533 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004534 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004535 s->be->be_counters.p.http.comp_rsp++;
4536 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004537 }
4538
4539 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004540 s->logs.bytes_in -= s->req->buf->i;
4541 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004542
4543 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004544 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004545 !(s->flags & SN_MONITOR) &&
4546 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4547 s->do_log(s);
4548 }
4549
4550 s->logs.accept_date = date; /* user-visible date for logging */
4551 s->logs.tv_accept = now; /* corrected date for internal use */
4552 tv_zero(&s->logs.tv_request);
4553 s->logs.t_queue = -1;
4554 s->logs.t_connect = -1;
4555 s->logs.t_data = -1;
4556 s->logs.t_close = 0;
4557 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4558 s->logs.srv_queue_size = 0; /* we will get this number soon */
4559
Willy Tarreau9b28e032012-10-12 23:49:43 +02004560 s->logs.bytes_in = s->req->total = s->req->buf->i;
4561 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004562
4563 if (s->pend_pos)
4564 pendconn_free(s->pend_pos);
4565
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004566 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004567 if (s->flags & SN_CURR_SESS) {
4568 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004569 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004570 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004571 if (may_dequeue_tasks(objt_server(s->target), s->be))
4572 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004573 }
4574
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004575 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004576
Willy Tarreau4213a112013-12-15 10:25:42 +01004577 /* only release our endpoint if we don't intend to reuse the
4578 * connection.
4579 */
4580 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4581 !si_conn_ready(s->req->cons)) {
4582 si_release_endpoint(s->req->cons);
4583 }
4584
Willy Tarreau610ecce2010-01-04 21:15:02 +01004585 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004586 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004587 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004588 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004589 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004590 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4591 s->rep->flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004592 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST);
Willy Tarreau36346242014-02-24 18:26:30 +01004593 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004594
Willy Tarreau610ecce2010-01-04 21:15:02 +01004595 s->txn.meth = 0;
4596 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004597 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004598
4599 if (prev_status == 401 || prev_status == 407) {
4600 /* In HTTP keep-alive mode, if we receive a 401, we still have
4601 * a chance of being able to send the visitor again to the same
4602 * server over the same connection. This is required by some
4603 * broken protocols such as NTLM, and anyway whenever there is
4604 * an opportunity for sending the challenge to the proper place,
4605 * it's better to do it (at least it helps with debugging).
4606 */
4607 s->txn.flags |= TX_PREFER_LAST;
4608 }
4609
Willy Tarreauee55dc02010-06-01 10:56:34 +02004610 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004611 s->req->cons->flags |= SI_FL_INDEP_STR;
4612
Willy Tarreau96e31212011-05-30 18:10:30 +02004613 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004614 s->req->flags |= CF_NEVER_WAIT;
4615 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004616 }
4617
Willy Tarreau610ecce2010-01-04 21:15:02 +01004618 /* if the request buffer is not empty, it means we're
4619 * about to process another request, so send pending
4620 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004621 * Just don't do this if the buffer is close to be full,
4622 * because the request will wait for it to flush a little
4623 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004624 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004625 if (s->req->buf->i) {
4626 if (s->rep->buf->o &&
4627 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4628 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004629 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004630 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004631
4632 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004633 channel_auto_read(s->req);
4634 channel_auto_close(s->req);
4635 channel_auto_read(s->rep);
4636 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004637
Willy Tarreau27375622013-12-17 00:00:28 +01004638 /* we're in keep-alive with an idle connection, monitor it */
4639 si_idle_conn(s->req->cons);
4640
Willy Tarreau342b11c2010-11-24 16:22:09 +01004641 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004642 s->rep->analysers = 0;
4643
4644 http_silent_debug(__LINE__, s);
4645}
4646
4647
4648/* This function updates the request state machine according to the response
4649 * state machine and buffer flags. It returns 1 if it changes anything (flag
4650 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4651 * it is only used to find when a request/response couple is complete. Both
4652 * this function and its equivalent should loop until both return zero. It
4653 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4654 */
4655int http_sync_req_state(struct session *s)
4656{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004657 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004658 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004659 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004660 unsigned int old_state = txn->req.msg_state;
4661
4662 http_silent_debug(__LINE__, s);
4663 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4664 return 0;
4665
4666 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004667 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004668 * We can shut the read side unless we want to abort_on_close,
4669 * or we have a POST request. The issue with POST requests is
4670 * that some browsers still send a CRLF after the request, and
4671 * this CRLF must be read so that it does not remain in the kernel
4672 * buffers, otherwise a close could cause an RST on some systems
4673 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004674 * Note that if we're using keep-alive on the client side, we'd
4675 * rather poll now and keep the polling enabled for the whole
4676 * session's life than enabling/disabling it between each
4677 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004678 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004679 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4680 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4681 !(s->be->options & PR_O_ABRT_CLOSE) &&
4682 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004683 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004684
Willy Tarreau40f151a2012-12-20 12:10:09 +01004685 /* if the server closes the connection, we want to immediately react
4686 * and close the socket to save packets and syscalls.
4687 */
4688 chn->cons->flags |= SI_FL_NOHALF;
4689
Willy Tarreau610ecce2010-01-04 21:15:02 +01004690 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4691 goto wait_other_side;
4692
4693 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4694 /* The server has not finished to respond, so we
4695 * don't want to move in order not to upset it.
4696 */
4697 goto wait_other_side;
4698 }
4699
4700 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4701 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004702 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004703 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004704 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004705 goto wait_other_side;
4706 }
4707
4708 /* When we get here, it means that both the request and the
4709 * response have finished receiving. Depending on the connection
4710 * mode, we'll have to wait for the last bytes to leave in either
4711 * direction, and sometimes for a close to be effective.
4712 */
4713
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004714 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4715 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004716 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4717 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004718 }
4719 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4720 /* Option forceclose is set, or either side wants to close,
4721 * let's enforce it now that we're not expecting any new
4722 * data to come. The caller knows the session is complete
4723 * once both states are CLOSED.
4724 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004725 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4726 channel_shutr_now(chn);
4727 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004728 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004729 }
4730 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004731 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4732 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004733 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004734 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4735 channel_auto_read(chn);
4736 txn->req.msg_state = HTTP_MSG_TUNNEL;
4737 chn->flags |= CF_NEVER_WAIT;
4738 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004739 }
4740
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004741 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004742 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004743 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004744
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004745 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004746 txn->req.msg_state = HTTP_MSG_CLOSING;
4747 goto http_msg_closing;
4748 }
4749 else {
4750 txn->req.msg_state = HTTP_MSG_CLOSED;
4751 goto http_msg_closed;
4752 }
4753 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004754 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004755 }
4756
4757 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4758 http_msg_closing:
4759 /* nothing else to forward, just waiting for the output buffer
4760 * to be empty and for the shutw_now to take effect.
4761 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004762 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004763 txn->req.msg_state = HTTP_MSG_CLOSED;
4764 goto http_msg_closed;
4765 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004766 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004767 txn->req.msg_state = HTTP_MSG_ERROR;
4768 goto wait_other_side;
4769 }
4770 }
4771
4772 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4773 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004774 /* see above in MSG_DONE why we only do this in these states */
4775 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4776 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4777 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004778 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004779 goto wait_other_side;
4780 }
4781
4782 wait_other_side:
4783 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004784 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004785}
4786
4787
4788/* This function updates the response state machine according to the request
4789 * state machine and buffer flags. It returns 1 if it changes anything (flag
4790 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4791 * it is only used to find when a request/response couple is complete. Both
4792 * this function and its equivalent should loop until both return zero. It
4793 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4794 */
4795int http_sync_res_state(struct session *s)
4796{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004797 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004798 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004799 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004800 unsigned int old_state = txn->rsp.msg_state;
4801
4802 http_silent_debug(__LINE__, s);
4803 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4804 return 0;
4805
4806 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4807 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004808 * still monitor the server connection for a possible close
4809 * while the request is being uploaded, so we don't disable
4810 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004811 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004812 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004813
4814 if (txn->req.msg_state == HTTP_MSG_ERROR)
4815 goto wait_other_side;
4816
4817 if (txn->req.msg_state < HTTP_MSG_DONE) {
4818 /* The client seems to still be sending data, probably
4819 * because we got an error response during an upload.
4820 * We have the choice of either breaking the connection
4821 * or letting it pass through. Let's do the later.
4822 */
4823 goto wait_other_side;
4824 }
4825
4826 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4827 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004828 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004829 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004830 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004831 goto wait_other_side;
4832 }
4833
4834 /* When we get here, it means that both the request and the
4835 * response have finished receiving. Depending on the connection
4836 * mode, we'll have to wait for the last bytes to leave in either
4837 * direction, and sometimes for a close to be effective.
4838 */
4839
4840 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4841 /* Server-close mode : shut read and wait for the request
4842 * side to close its output buffer. The caller will detect
4843 * when we're in DONE and the other is in CLOSED and will
4844 * catch that for the final cleanup.
4845 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004846 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4847 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004848 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004849 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4850 /* Option forceclose is set, or either side wants to close,
4851 * let's enforce it now that we're not expecting any new
4852 * data to come. The caller knows the session is complete
4853 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004854 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004855 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4856 channel_shutr_now(chn);
4857 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004858 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004859 }
4860 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004861 /* The last possible modes are keep-alive and tunnel. Tunnel will
4862 * need to forward remaining data. Keep-alive will need to monitor
4863 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004864 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004865 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004866 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004867 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4868 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004869 }
4870
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004871 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004872 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004873 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004874 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4875 goto http_msg_closing;
4876 }
4877 else {
4878 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4879 goto http_msg_closed;
4880 }
4881 }
4882 goto wait_other_side;
4883 }
4884
4885 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4886 http_msg_closing:
4887 /* nothing else to forward, just waiting for the output buffer
4888 * to be empty and for the shutw_now to take effect.
4889 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004890 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004891 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4892 goto http_msg_closed;
4893 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004894 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004895 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004896 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004897 if (objt_server(s->target))
4898 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004899 goto wait_other_side;
4900 }
4901 }
4902
4903 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4904 http_msg_closed:
4905 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004906 bi_erase(chn);
4907 channel_auto_close(chn);
4908 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004909 goto wait_other_side;
4910 }
4911
4912 wait_other_side:
4913 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004914 /* We force the response to leave immediately if we're waiting for the
4915 * other side, since there is no pending shutdown to push it out.
4916 */
4917 if (!channel_is_empty(chn))
4918 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004919 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004920}
4921
4922
4923/* Resync the request and response state machines. Return 1 if either state
4924 * changes.
4925 */
4926int http_resync_states(struct session *s)
4927{
4928 struct http_txn *txn = &s->txn;
4929 int old_req_state = txn->req.msg_state;
4930 int old_res_state = txn->rsp.msg_state;
4931
4932 http_silent_debug(__LINE__, s);
4933 http_sync_req_state(s);
4934 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004935 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004936 if (!http_sync_res_state(s))
4937 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004938 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004939 if (!http_sync_req_state(s))
4940 break;
4941 }
4942 http_silent_debug(__LINE__, s);
4943 /* OK, both state machines agree on a compatible state.
4944 * There are a few cases we're interested in :
4945 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4946 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4947 * directions, so let's simply disable both analysers.
4948 * - HTTP_MSG_CLOSED on the response only means we must abort the
4949 * request.
4950 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4951 * with server-close mode means we've completed one request and we
4952 * must re-initialize the server connection.
4953 */
4954
4955 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4956 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4957 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4958 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4959 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004960 channel_auto_close(s->req);
4961 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004962 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004963 channel_auto_close(s->rep);
4964 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004965 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004966 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4967 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004968 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004969 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004970 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004971 channel_auto_close(s->rep);
4972 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004973 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004974 channel_abort(s->req);
4975 channel_auto_close(s->req);
4976 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004977 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004978 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004979 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4980 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004981 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004982 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4983 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4984 /* server-close/keep-alive: terminate this transaction,
4985 * possibly killing the server connection and reinitialize
4986 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004987 */
4988 http_end_txn_clean_session(s);
4989 }
4990
4991 http_silent_debug(__LINE__, s);
4992 return txn->req.msg_state != old_req_state ||
4993 txn->rsp.msg_state != old_res_state;
4994}
4995
Willy Tarreaud98cf932009-12-27 22:54:55 +01004996/* This function is an analyser which forwards request body (including chunk
4997 * sizes if any). It is called as soon as we must forward, even if we forward
4998 * zero byte. The only situation where it must not be called is when we're in
4999 * tunnel mode and we want to forward till the close. It's used both to forward
5000 * remaining data and to resync after end of body. It expects the msg_state to
5001 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5002 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005003 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005004 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005005 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005006int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005007{
5008 struct http_txn *txn = &s->txn;
5009 struct http_msg *msg = &s->txn.req;
5010
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005011 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5012 return 0;
5013
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005014 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005015 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005016 /* Output closed while we were sending data. We must abort and
5017 * wake the other side up.
5018 */
5019 msg->msg_state = HTTP_MSG_ERROR;
5020 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005021 return 1;
5022 }
5023
Willy Tarreaud98cf932009-12-27 22:54:55 +01005024 /* Note that we don't have to send 100-continue back because we don't
5025 * need the data to complete our job, and it's up to the server to
5026 * decide whether to return 100, 417 or anything else in return of
5027 * an "Expect: 100-continue" header.
5028 */
5029
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005030 if (msg->sov) {
5031 /* we have msg->sov which points to the first byte of message
5032 * body, and req->buf.p still points to the beginning of the
5033 * message. We forward the headers now, as we don't need them
5034 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005035 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005036 b_adv(req->buf, msg->sov);
5037 msg->next -= msg->sov;
5038 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005039
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005040 /* The previous analysers guarantee that the state is somewhere
5041 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5042 * msg->next are always correct.
5043 */
5044 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5045 if (msg->flags & HTTP_MSGF_TE_CHNK)
5046 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5047 else
5048 msg->msg_state = HTTP_MSG_DATA;
5049 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005050 }
5051
Willy Tarreau7ba23542014-04-17 21:50:00 +02005052 /* Some post-connect processing might want us to refrain from starting to
5053 * forward data. Currently, the only reason for this is "balance url_param"
5054 * whichs need to parse/process the request after we've enabled forwarding.
5055 */
5056 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5057 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5058 channel_auto_connect(req);
5059 goto missing_data;
5060 }
5061 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5062 }
5063
Willy Tarreau80a92c02014-03-12 10:41:13 +01005064 /* in most states, we should abort in case of early close */
5065 channel_auto_close(req);
5066
Willy Tarreaud98cf932009-12-27 22:54:55 +01005067 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005068 http_silent_debug(__LINE__, s);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005069
5070 /* we may have some pending data starting at req->buf->p */
Willy Tarreauc24715e2014-04-17 15:21:20 +02005071 if (msg->chunk_len || msg->next) {
5072 msg->chunk_len += msg->next;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005073 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreauc24715e2014-04-17 15:21:20 +02005074 msg->next = 0;
Willy Tarreau1234f4a2014-04-17 21:14:47 +02005075 msg->sov = 0;
Willy Tarreau638cd022010-01-03 07:42:04 +01005076 }
Willy Tarreau5523b322009-12-29 12:05:52 +01005077
Willy Tarreaucaabe412010-01-03 23:08:28 +01005078 if (msg->msg_state == HTTP_MSG_DATA) {
5079 /* must still forward */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005080 if (req->to_forward) {
5081 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005082 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005083 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01005084
5085 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005086 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005087 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005088 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005089 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005090 }
5091 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005092 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005093 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005094 * TRAILERS state.
5095 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005096 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005097
Willy Tarreau54d23df2012-10-25 19:04:45 +02005098 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005099 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005100 else if (ret < 0) {
5101 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005102 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005103 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005104 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005105 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005106 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005107 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005108 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005109 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005110 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005111
5112 if (ret == 0)
5113 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005114 else if (ret < 0) {
5115 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005116 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005117 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005118 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005119 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005120 /* we're in MSG_CHUNK_SIZE now */
5121 }
5122 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005123 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005124
5125 if (ret == 0)
5126 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005127 else if (ret < 0) {
5128 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005129 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005130 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005131 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005132 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005133 /* we're in HTTP_MSG_DONE now */
5134 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005135 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005136 int old_state = msg->msg_state;
5137
Willy Tarreau610ecce2010-01-04 21:15:02 +01005138 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005139 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005140 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5141 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005142 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005143 if (http_resync_states(s)) {
5144 /* some state changes occurred, maybe the analyser
5145 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005146 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005147 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005148 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005149 /* request errors are most likely due to
5150 * the server aborting the transfer.
5151 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005152 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005153 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005154 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005155 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005156 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005157 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005158 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005159 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005160
5161 /* If "option abortonclose" is set on the backend, we
5162 * want to monitor the client's connection and forward
5163 * any shutdown notification to the server, which will
5164 * decide whether to close or to go on processing the
5165 * request.
5166 */
5167 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005168 channel_auto_read(req);
5169 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005170 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005171 else if (s->txn.meth == HTTP_METH_POST) {
5172 /* POST requests may require to read extra CRLF
5173 * sent by broken browsers and which could cause
5174 * an RST to be sent upon close on some systems
5175 * (eg: Linux).
5176 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005177 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005178 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005179
Willy Tarreau610ecce2010-01-04 21:15:02 +01005180 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005181 }
5182 }
5183
Willy Tarreaud98cf932009-12-27 22:54:55 +01005184 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005185 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005186 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005187 if (!(s->flags & SN_ERR_MASK))
5188 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005189 if (!(s->flags & SN_FINST_MASK)) {
5190 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5191 s->flags |= SN_FINST_H;
5192 else
5193 s->flags |= SN_FINST_D;
5194 }
5195
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005196 s->fe->fe_counters.cli_aborts++;
5197 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005198 if (objt_server(s->target))
5199 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005200
5201 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005202 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005203
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005204 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005205 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005206 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005207
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005208 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005209 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005210 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005211 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005212 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005213
Willy Tarreau5c620922011-05-11 19:56:11 +02005214 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005215 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005216 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005217 * modes are already handled by the stream sock layer. We must not do
5218 * this in content-length mode because it could present the MSG_MORE
5219 * flag with the last block of forwarded data, which would cause an
5220 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005221 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005222 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005223 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005224
Willy Tarreau610ecce2010-01-04 21:15:02 +01005225 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005226 return 0;
5227
Willy Tarreaud98cf932009-12-27 22:54:55 +01005228 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005229 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005230 if (s->listener->counters)
5231 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005232 return_bad_req_stats_ok:
5233 txn->req.msg_state = HTTP_MSG_ERROR;
5234 if (txn->status) {
5235 /* Note: we don't send any error if some data were already sent */
5236 stream_int_retnclose(req->prod, NULL);
5237 } else {
5238 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005239 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005240 }
5241 req->analysers = 0;
5242 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005243
5244 if (!(s->flags & SN_ERR_MASK))
5245 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005246 if (!(s->flags & SN_FINST_MASK)) {
5247 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5248 s->flags |= SN_FINST_H;
5249 else
5250 s->flags |= SN_FINST_D;
5251 }
5252 return 0;
5253
5254 aborted_xfer:
5255 txn->req.msg_state = HTTP_MSG_ERROR;
5256 if (txn->status) {
5257 /* Note: we don't send any error if some data were already sent */
5258 stream_int_retnclose(req->prod, NULL);
5259 } else {
5260 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005261 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005262 }
5263 req->analysers = 0;
5264 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5265
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005266 s->fe->fe_counters.srv_aborts++;
5267 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005268 if (objt_server(s->target))
5269 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005270
5271 if (!(s->flags & SN_ERR_MASK))
5272 s->flags |= SN_ERR_SRVCL;
5273 if (!(s->flags & SN_FINST_MASK)) {
5274 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5275 s->flags |= SN_FINST_H;
5276 else
5277 s->flags |= SN_FINST_D;
5278 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005279 return 0;
5280}
5281
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005282/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5283 * processing can continue on next analysers, or zero if it either needs more
5284 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5285 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5286 * when it has nothing left to do, and may remove any analyser when it wants to
5287 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005288 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005289int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005290{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005291 struct http_txn *txn = &s->txn;
5292 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005293 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005294 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005295 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005296 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005297
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005298 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02005299 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005300 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005301 rep,
5302 rep->rex, rep->wex,
5303 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005304 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005305 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005306
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005307 /*
5308 * Now parse the partial (or complete) lines.
5309 * We will check the response syntax, and also join multi-line
5310 * headers. An index of all the lines will be elaborated while
5311 * parsing.
5312 *
5313 * For the parsing, we use a 28 states FSM.
5314 *
5315 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005316 * rep->buf->p = beginning of response
5317 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5318 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005319 * msg->eol = end of current header or line (LF or CRLF)
5320 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005321 */
5322
Willy Tarreau83e3af02009-12-28 17:39:57 +01005323 /* There's a protected area at the end of the buffer for rewriting
5324 * purposes. We don't want to start to parse the request if the
5325 * protected area is affected, because we may have to move processed
5326 * data later, which is much more complicated.
5327 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005328 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005329 if (unlikely(!channel_reserved(rep))) {
5330 /* some data has still not left the buffer, wake us once that's done */
5331 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5332 goto abort_response;
5333 channel_dont_close(rep);
5334 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005335 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005336 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005337 }
5338
Willy Tarreau379357a2013-06-08 12:55:46 +02005339 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5340 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5341 buffer_slow_realign(rep->buf);
5342
Willy Tarreau9b28e032012-10-12 23:49:43 +02005343 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005344 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005345 }
5346
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005347 /* 1: we might have to print this header in debug mode */
5348 if (unlikely((global.mode & MODE_DEBUG) &&
5349 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005350 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005351 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005352
Willy Tarreau9b28e032012-10-12 23:49:43 +02005353 sol = rep->buf->p;
5354 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005355 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005356
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005357 sol += hdr_idx_first_pos(&txn->hdr_idx);
5358 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005359
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005360 while (cur_idx) {
5361 eol = sol + txn->hdr_idx.v[cur_idx].len;
5362 debug_hdr("srvhdr", s, sol, eol);
5363 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5364 cur_idx = txn->hdr_idx.v[cur_idx].next;
5365 }
5366 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005367
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005368 /*
5369 * Now we quickly check if we have found a full valid response.
5370 * If not so, we check the FD and buffer states before leaving.
5371 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005372 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005373 * responses are checked first.
5374 *
5375 * Depending on whether the client is still there or not, we
5376 * may send an error response back or not. Note that normally
5377 * we should only check for HTTP status there, and check I/O
5378 * errors somewhere else.
5379 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005380
Willy Tarreau655dce92009-11-08 13:10:58 +01005381 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005382 /* Invalid response */
5383 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5384 /* we detected a parsing error. We want to archive this response
5385 * in the dedicated proxy area for later troubleshooting.
5386 */
5387 hdr_response_bad:
5388 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005389 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005390
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005391 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005392 if (objt_server(s->target)) {
5393 objt_server(s->target)->counters.failed_resp++;
5394 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005395 }
Willy Tarreau64648412010-03-05 10:41:54 +01005396 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005397 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005398 rep->analysers = 0;
5399 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005400 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005401 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005402 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005403
5404 if (!(s->flags & SN_ERR_MASK))
5405 s->flags |= SN_ERR_PRXCOND;
5406 if (!(s->flags & SN_FINST_MASK))
5407 s->flags |= SN_FINST_H;
5408
5409 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005410 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005411
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005412 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005413 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005414 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005415 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005416 goto hdr_response_bad;
5417 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005418
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005419 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005420 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005421 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005422 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005423 else if (txn->flags & TX_NOT_FIRST)
5424 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005425
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005426 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005427 if (objt_server(s->target)) {
5428 objt_server(s->target)->counters.failed_resp++;
5429 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005430 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005431
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005432 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005433 rep->analysers = 0;
5434 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005435 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005436 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005437 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005438
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005439 if (!(s->flags & SN_ERR_MASK))
5440 s->flags |= SN_ERR_SRVCL;
5441 if (!(s->flags & SN_FINST_MASK))
5442 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005443 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005444 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005445
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005446 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005447 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005448 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005449 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005450 else if (txn->flags & TX_NOT_FIRST)
5451 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005452
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005453 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005454 if (objt_server(s->target)) {
5455 objt_server(s->target)->counters.failed_resp++;
5456 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005457 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005458
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005459 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005460 rep->analysers = 0;
5461 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005462 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005463 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005464 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005465
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005466 if (!(s->flags & SN_ERR_MASK))
5467 s->flags |= SN_ERR_SRVTO;
5468 if (!(s->flags & SN_FINST_MASK))
5469 s->flags |= SN_FINST_H;
5470 return 0;
5471 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005472
Willy Tarreauf003d372012-11-26 13:35:37 +01005473 /* client abort with an abortonclose */
5474 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5475 s->fe->fe_counters.cli_aborts++;
5476 s->be->be_counters.cli_aborts++;
5477 if (objt_server(s->target))
5478 objt_server(s->target)->counters.cli_aborts++;
5479
5480 rep->analysers = 0;
5481 channel_auto_close(rep);
5482
5483 txn->status = 400;
5484 bi_erase(rep);
5485 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5486
5487 if (!(s->flags & SN_ERR_MASK))
5488 s->flags |= SN_ERR_CLICL;
5489 if (!(s->flags & SN_FINST_MASK))
5490 s->flags |= SN_FINST_H;
5491
5492 /* process_session() will take care of the error */
5493 return 0;
5494 }
5495
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005496 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005497 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005498 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005499 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005500 else if (txn->flags & TX_NOT_FIRST)
5501 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005502
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005503 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005504 if (objt_server(s->target)) {
5505 objt_server(s->target)->counters.failed_resp++;
5506 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005507 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005508
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005509 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005510 rep->analysers = 0;
5511 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005512 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005513 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005514 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005515
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005516 if (!(s->flags & SN_ERR_MASK))
5517 s->flags |= SN_ERR_SRVCL;
5518 if (!(s->flags & SN_FINST_MASK))
5519 s->flags |= SN_FINST_H;
5520 return 0;
5521 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005522
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005523 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005524 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005525 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005526 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005527 else if (txn->flags & TX_NOT_FIRST)
5528 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005529
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005530 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005531 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005532 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005533
5534 if (!(s->flags & SN_ERR_MASK))
5535 s->flags |= SN_ERR_CLICL;
5536 if (!(s->flags & SN_FINST_MASK))
5537 s->flags |= SN_FINST_H;
5538
5539 /* process_session() will take care of the error */
5540 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005541 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005542
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005543 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005544 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005545 return 0;
5546 }
5547
5548 /* More interesting part now : we know that we have a complete
5549 * response which at least looks like HTTP. We have an indicator
5550 * of each header's length, so we can parse them quickly.
5551 */
5552
5553 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005554 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005555
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005556 /*
5557 * 1: get the status code
5558 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005559 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005560 if (n < 1 || n > 5)
5561 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005562 /* when the client triggers a 4xx from the server, it's most often due
5563 * to a missing object or permission. These events should be tracked
5564 * because if they happen often, it may indicate a brute force or a
5565 * vulnerability scan.
5566 */
5567 if (n == 4)
5568 session_inc_http_err_ctr(s);
5569
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005570 if (objt_server(s->target))
5571 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005572
Willy Tarreau5b154472009-12-21 20:11:07 +01005573 /* check if the response is HTTP/1.1 or above */
5574 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005575 ((rep->buf->p[5] > '1') ||
5576 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005577 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005578
5579 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005580 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01005581
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005582 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005583 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005584
Willy Tarreau9b28e032012-10-12 23:49:43 +02005585 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005586
Willy Tarreau39650402010-03-15 19:44:39 +01005587 /* Adjust server's health based on status code. Note: status codes 501
5588 * and 505 are triggered on demand by client request, so we must not
5589 * count them as server failures.
5590 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005591 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005592 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005593 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005594 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005595 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005596 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005597
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005598 /*
5599 * 2: check for cacheability.
5600 */
5601
5602 switch (txn->status) {
5603 case 200:
5604 case 203:
5605 case 206:
5606 case 300:
5607 case 301:
5608 case 410:
5609 /* RFC2616 @13.4:
5610 * "A response received with a status code of
5611 * 200, 203, 206, 300, 301 or 410 MAY be stored
5612 * by a cache (...) unless a cache-control
5613 * directive prohibits caching."
5614 *
5615 * RFC2616 @9.5: POST method :
5616 * "Responses to this method are not cacheable,
5617 * unless the response includes appropriate
5618 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005619 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005620 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005621 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005622 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5623 break;
5624 default:
5625 break;
5626 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005627
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005628 /*
5629 * 3: we may need to capture headers
5630 */
5631 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005632 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005633 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005634 txn->rsp.cap, s->fe->rsp_cap);
5635
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005636 /* 4: determine the transfer-length.
5637 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5638 * the presence of a message-body in a RESPONSE and its transfer length
5639 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005640 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005641 * All responses to the HEAD request method MUST NOT include a
5642 * message-body, even though the presence of entity-header fields
5643 * might lead one to believe they do. All 1xx (informational), 204
5644 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5645 * message-body. All other responses do include a message-body,
5646 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005647 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005648 * 1. Any response which "MUST NOT" include a message-body (such as the
5649 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5650 * always terminated by the first empty line after the header fields,
5651 * regardless of the entity-header fields present in the message.
5652 *
5653 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5654 * the "chunked" transfer-coding (Section 6.2) is used, the
5655 * transfer-length is defined by the use of this transfer-coding.
5656 * If a Transfer-Encoding header field is present and the "chunked"
5657 * transfer-coding is not present, the transfer-length is defined by
5658 * the sender closing the connection.
5659 *
5660 * 3. If a Content-Length header field is present, its decimal value in
5661 * OCTETs represents both the entity-length and the transfer-length.
5662 * If a message is received with both a Transfer-Encoding header
5663 * field and a Content-Length header field, the latter MUST be ignored.
5664 *
5665 * 4. If the message uses the media type "multipart/byteranges", and
5666 * the transfer-length is not otherwise specified, then this self-
5667 * delimiting media type defines the transfer-length. This media
5668 * type MUST NOT be used unless the sender knows that the recipient
5669 * can parse it; the presence in a request of a Range header with
5670 * multiple byte-range specifiers from a 1.1 client implies that the
5671 * client can parse multipart/byteranges responses.
5672 *
5673 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005674 */
5675
5676 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005677 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005678 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005679 * FIXME: should we parse anyway and return an error on chunked encoding ?
5680 */
5681 if (txn->meth == HTTP_METH_HEAD ||
5682 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005683 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005684 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005685 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005686 goto skip_content_length;
5687 }
5688
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005689 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005690 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005691 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005692 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005693 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005694 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5695 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005696 /* bad transfer-encoding (chunked followed by something else) */
5697 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005698 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005699 break;
5700 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005701 }
5702
5703 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5704 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005705 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005706 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005707 signed long long cl;
5708
Willy Tarreauad14f752011-09-02 20:33:27 +02005709 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005710 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005711 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005712 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005713
Willy Tarreauad14f752011-09-02 20:33:27 +02005714 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005715 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005716 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005717 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005718
Willy Tarreauad14f752011-09-02 20:33:27 +02005719 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005720 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005721 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005722 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005723
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005724 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005725 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005726 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005727 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005728
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005729 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005730 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005731 }
5732
William Lallemand82fe75c2012-10-23 10:25:10 +02005733 if (s->fe->comp || s->be->comp)
5734 select_compression_response_header(s, rep->buf);
5735
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005736 /* FIXME: we should also implement the multipart/byterange method.
5737 * For now on, we resort to close mode in this case (unknown length).
5738 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005739skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005740
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005741 /* end of job, return OK */
5742 rep->analysers &= ~an_bit;
5743 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005744 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005745 return 1;
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005746
5747 abort_keep_alive:
5748 /* A keep-alive request to the server failed on a network error.
5749 * The client is required to retry. We need to close without returning
5750 * any other information so that the client retries.
5751 */
5752 txn->status = 0;
5753 rep->analysers = 0;
5754 s->req->analysers = 0;
5755 channel_auto_close(rep);
5756 s->logs.logwait = 0;
5757 s->logs.level = 0;
5758 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5759 bi_erase(rep);
5760 stream_int_retnclose(rep->cons, NULL);
5761 return 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005762}
5763
5764/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005765 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5766 * and updates t->rep->analysers. It might make sense to explode it into several
5767 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005768 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005769int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005770{
5771 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005772 struct http_msg *msg = &txn->rsp;
5773 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005774 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005775 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005776
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005777 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005778 now_ms, __FUNCTION__,
5779 t,
5780 rep,
5781 rep->rex, rep->wex,
5782 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005783 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005784 rep->analysers);
5785
Willy Tarreau655dce92009-11-08 13:10:58 +01005786 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005787 return 0;
5788
5789 rep->analysers &= ~an_bit;
5790 rep->analyse_exp = TICK_ETERNITY;
5791
Willy Tarreau5b154472009-12-21 20:11:07 +01005792 /* Now we have to check if we need to modify the Connection header.
5793 * This is more difficult on the response than it is on the request,
5794 * because we can have two different HTTP versions and we don't know
5795 * how the client will interprete a response. For instance, let's say
5796 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5797 * HTTP/1.1 response without any header. Maybe it will bound itself to
5798 * HTTP/1.0 because it only knows about it, and will consider the lack
5799 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5800 * the lack of header as a keep-alive. Thus we will use two flags
5801 * indicating how a request MAY be understood by the client. In case
5802 * of multiple possibilities, we'll fix the header to be explicit. If
5803 * ambiguous cases such as both close and keepalive are seen, then we
5804 * will fall back to explicit close. Note that we won't take risks with
5805 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005806 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005807 */
5808
Willy Tarreaudc008c52010-02-01 16:20:08 +01005809 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5810 txn->status == 101)) {
5811 /* Either we've established an explicit tunnel, or we're
5812 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005813 * to understand the next protocols. We have to switch to tunnel
5814 * mode, so that we transfer the request and responses then let
5815 * this protocol pass unmodified. When we later implement specific
5816 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005817 * header which contains information about that protocol for
5818 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005819 */
5820 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5821 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005822 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5823 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005824 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5825 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005826 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005827
Willy Tarreau70dffda2014-01-30 03:07:23 +01005828 /* this situation happens when combining pretend-keepalive with httpclose. */
5829 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
5830 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5831 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
5832 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5833
Willy Tarreau60466522010-01-18 19:08:45 +01005834 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005835 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005836 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5837 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005838
Willy Tarreau60466522010-01-18 19:08:45 +01005839 /* now adjust header transformations depending on current state */
5840 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5841 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5842 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005843 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005844 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005845 }
Willy Tarreau60466522010-01-18 19:08:45 +01005846 else { /* SCL / KAL */
5847 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005848 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005849 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005850 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005851
Willy Tarreau60466522010-01-18 19:08:45 +01005852 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005853 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005854
Willy Tarreau60466522010-01-18 19:08:45 +01005855 /* Some keep-alive responses are converted to Server-close if
5856 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005857 */
Willy Tarreau60466522010-01-18 19:08:45 +01005858 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5859 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005860 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005861 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005862 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005863 }
5864
Willy Tarreau7959a552013-09-23 16:44:27 +02005865 /* we want to have the response time before we start processing it */
5866 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5867
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005868 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005869 /*
5870 * 3: we will have to evaluate the filters.
5871 * As opposed to version 1.2, now they will be evaluated in the
5872 * filters order and not in the header order. This means that
5873 * each filter has to be validated among all headers.
5874 *
5875 * Filters are tried with ->be first, then with ->fe if it is
5876 * different from ->be.
5877 */
5878
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005879 cur_proxy = t->be;
5880 while (1) {
5881 struct proxy *rule_set = cur_proxy;
5882
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005883 /* evaluate http-response rules */
5884 if (!http_res_last_rule)
5885 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5886
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005887 /* try headers filters */
5888 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005889 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005890 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005891 if (objt_server(t->target)) {
5892 objt_server(t->target)->counters.failed_resp++;
5893 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005894 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005895 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005896 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005897 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005898 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005899 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005900 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005901 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005902 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005903 if (!(t->flags & SN_ERR_MASK))
5904 t->flags |= SN_ERR_PRXCOND;
5905 if (!(t->flags & SN_FINST_MASK))
5906 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005907 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005908 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005909 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005910
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005911 /* has the response been denied ? */
5912 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005913 if (objt_server(t->target))
5914 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005915
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005916 t->be->be_counters.denied_resp++;
5917 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005918 if (t->listener->counters)
5919 t->listener->counters->denied_resp++;
5920
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005921 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005922 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005923
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005924 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005925 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005926 if (txn->status < 200)
5927 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005928 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005929 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005930 ret = acl_pass(ret);
5931 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5932 ret = !ret;
5933 if (!ret)
5934 continue;
5935 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005936 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005937 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005938 }
5939
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005940 /* check whether we're already working on the frontend */
5941 if (cur_proxy == t->fe)
5942 break;
5943 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005944 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005945
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005946 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005947 * We may be facing a 100-continue response, in which case this
5948 * is not the right response, and we're waiting for the next one.
5949 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005950 * next one.
5951 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005952 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005953 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005954 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005955 msg->msg_state = HTTP_MSG_RPBEFORE;
5956 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005957 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005958 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5959 return 1;
5960 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005961 else if (unlikely(txn->status < 200))
5962 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005963
5964 /* we don't have any 1xx status code now */
5965
5966 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005967 * 4: check for server cookie.
5968 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005969 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5970 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005971 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005972
Willy Tarreaubaaee002006-06-26 02:48:02 +02005973
Willy Tarreaua15645d2007-03-18 16:22:39 +01005974 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005975 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005976 */
Willy Tarreau67402132012-05-31 20:40:20 +02005977 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005978 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005979
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005980 /*
5981 * 6: add server cookie in the response if needed
5982 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005983 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005984 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005985 (!(t->flags & SN_DIRECT) ||
5986 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5987 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5988 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5989 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005990 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005991 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005992 /* the server is known, it's not the one the client requested, or the
5993 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005994 * insert a set-cookie here, except if we want to insert only on POST
5995 * requests and this one isn't. Note that servers which don't have cookies
5996 * (eg: some backup servers) will return a full cookie removal request.
5997 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005998 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005999 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02006000 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6001 t->be->cookie_name);
6002 }
6003 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006004 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006005
6006 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
6007 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006008 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6009 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6010 trash.len += 5;
6011
Willy Tarreauef4f3912010-10-07 21:00:29 +02006012 if (t->be->cookie_maxlife) {
6013 /* emit first_date, which is either the original one or
6014 * the current date.
6015 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006016 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006017 s30tob64(txn->cookie_first_date ?
6018 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006019 (date.tv_sec+3) >> 2, trash.str + trash.len);
6020 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006021 }
6022 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006023 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02006024 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006025
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006026 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006027 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006028
Willy Tarreau4992dd22012-05-31 21:02:17 +02006029 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006030 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02006031
6032 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006033 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02006034
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006035 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006036 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006037
Willy Tarreauf1348312010-10-07 15:54:11 +02006038 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006039 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02006040 /* the server did not change, only the date was updated */
6041 txn->flags |= TX_SCK_UPDATED;
6042 else
6043 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006044
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006045 /* Here, we will tell an eventual cache on the client side that we don't
6046 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6047 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6048 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
6049 */
Willy Tarreau67402132012-05-31 20:40:20 +02006050 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02006051
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006052 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6053
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006054 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01006055 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006056 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006057 }
6058 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006059
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006060 /*
6061 * 7: check if result will be cacheable with a cookie.
6062 * We'll block the response if security checks have caught
6063 * nasty things such as a cacheable cookie.
6064 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006065 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6066 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006067 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006068
6069 /* we're in presence of a cacheable response containing
6070 * a set-cookie header. We'll block it as requested by
6071 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006072 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006073 if (objt_server(t->target))
6074 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006075
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006076 t->be->be_counters.denied_resp++;
6077 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006078 if (t->listener->counters)
6079 t->listener->counters->denied_resp++;
6080
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006081 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006082 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006083 send_log(t->be, LOG_ALERT,
6084 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006085 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006086 goto return_srv_prx_502;
6087 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006088
6089 /*
Willy Tarreau60466522010-01-18 19:08:45 +01006090 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01006091 * If an "Upgrade" token is found, the header is left untouched in order
6092 * not to have to deal with some client bugs : some of them fail an upgrade
6093 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006094 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006095 if (!(txn->flags & TX_HDR_CONN_UPG) &&
6096 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01006097 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6098 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006099 unsigned int want_flags = 0;
6100
6101 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6102 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6103 /* we want a keep-alive response here. Keep-alive header
6104 * required if either side is not 1.1.
6105 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006106 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006107 want_flags |= TX_CON_KAL_SET;
6108 }
6109 else {
6110 /* we want a close response here. Close header required if
6111 * the server is 1.1, regardless of the client.
6112 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006113 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006114 want_flags |= TX_CON_CLO_SET;
6115 }
6116
6117 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006118 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006119 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006120
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006121 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006122 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01006123 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006124 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01006125
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006126 /*************************************************************
6127 * OK, that's finished for the headers. We have done what we *
6128 * could. Let's switch to the DATA state. *
6129 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02006130
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006131 /* if the user wants to log as soon as possible, without counting
6132 * bytes from the server, then this is the right moment. We have
6133 * to temporarily assign bytes_out to log what we currently have.
6134 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01006135 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006136 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
6137 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01006138 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006139 t->logs.bytes_out = 0;
6140 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006141
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006142 /* Note: we must not try to cheat by jumping directly to DATA,
6143 * otherwise we would not let the client side wake up.
6144 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006145
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006146 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006147 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006148 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006149}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006150
Willy Tarreaud98cf932009-12-27 22:54:55 +01006151/* This function is an analyser which forwards response body (including chunk
6152 * sizes if any). It is called as soon as we must forward, even if we forward
6153 * zero byte. The only situation where it must not be called is when we're in
6154 * tunnel mode and we want to forward till the close. It's used both to forward
6155 * remaining data and to resync after end of body. It expects the msg_state to
6156 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6157 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01006158 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02006159 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006160 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006161int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006162{
6163 struct http_txn *txn = &s->txn;
6164 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006165 static struct buffer *tmpbuf = NULL;
6166 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01006167 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006168 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006169
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006170 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6171 return 0;
6172
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006173 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006174 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006175 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006176 /* Output closed while we were sending data. We must abort and
6177 * wake the other side up.
6178 */
6179 msg->msg_state = HTTP_MSG_ERROR;
6180 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006181 return 1;
6182 }
6183
Willy Tarreau4fe41902010-06-07 22:27:41 +02006184 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006185 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006186
William Lallemand82fe75c2012-10-23 10:25:10 +02006187 /* this is the first time we need the compression buffer */
6188 if (s->comp_algo != NULL && tmpbuf == NULL) {
6189 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
6190 goto aborted_xfer; /* no memory */
6191 }
6192
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006193 if (msg->sov) {
6194 /* we have msg->sov which points to the first byte of message
6195 * body, and res->buf.p still points to the beginning of the
6196 * message. We forward the headers now, as we don't need them
6197 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006198 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006199 b_adv(res->buf, msg->sov);
6200 msg->next -= msg->sov;
6201 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006202
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006203 /* The previous analysers guarantee that the state is somewhere
6204 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6205 * msg->next are always correct.
6206 */
6207 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6208 if (msg->flags & HTTP_MSGF_TE_CHNK)
6209 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6210 else
6211 msg->msg_state = HTTP_MSG_DATA;
6212 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006213 }
6214
William Lallemand82fe75c2012-10-23 10:25:10 +02006215 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006216 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006217 if (ret < 0) {
6218 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006219 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006220 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006221 compressing = 1;
6222 }
6223
Willy Tarreaud98cf932009-12-27 22:54:55 +01006224 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006225 http_silent_debug(__LINE__, s);
Willy Tarreau877e78d2013-04-07 18:48:08 +02006226
6227 /* we may have some pending data starting at res->buf->p */
William Lallemand82fe75c2012-10-23 10:25:10 +02006228 if (s->comp_algo == NULL) {
Willy Tarreauc24715e2014-04-17 15:21:20 +02006229 if (msg->chunk_len || msg->next) {
6230 msg->chunk_len += msg->next;
William Lallemand82fe75c2012-10-23 10:25:10 +02006231 msg->chunk_len -= channel_forward(res, msg->chunk_len);
Willy Tarreauc24715e2014-04-17 15:21:20 +02006232 msg->next = 0;
Willy Tarreau1234f4a2014-04-17 21:14:47 +02006233 msg->sov = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02006234 }
Willy Tarreau638cd022010-01-03 07:42:04 +01006235 }
6236
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006237 switch (msg->msg_state - HTTP_MSG_DATA) {
6238 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006239 if (compressing) {
6240 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
6241 if (ret < 0)
6242 goto aborted_xfer;
6243 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006244
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006245 if (res->to_forward || msg->chunk_len) {
6246 res->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006247 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006248 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006249
6250 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006251 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006252 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006253 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006254 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01006255 if (compressing && consumed_data) {
6256 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6257 compressing = 0;
6258 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006259 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006260 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006261 /* fall through for HTTP_MSG_CHUNK_CRLF */
6262
6263 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6264 /* we want the CRLF after the data */
6265
6266 ret = http_skip_chunk_crlf(msg);
6267 if (ret == 0)
6268 goto missing_data;
6269 else if (ret < 0) {
6270 if (msg->err_pos >= 0)
6271 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6272 goto return_bad_res;
6273 }
6274 /* skipping data in buffer for compression */
6275 if (compressing) {
6276 b_adv(res->buf, msg->next);
6277 msg->next = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006278 }
6279 /* we're in MSG_CHUNK_SIZE now, fall through */
6280
6281 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006282 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006283 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006284 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006285 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006286
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006287 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006288 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006289 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006290 else if (ret < 0) {
6291 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006292 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006293 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006294 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006295 if (compressing) {
6296 if (likely(msg->chunk_len > 0)) {
6297 /* skipping data if we are in compression mode */
6298 b_adv(res->buf, msg->next);
6299 msg->next = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01006300 } else {
6301 if (consumed_data) {
6302 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6303 compressing = 0;
6304 }
6305 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006306 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006307 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006308 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006309
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006310 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
6311 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006312 if (ret == 0)
6313 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006314 else if (ret < 0) {
6315 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006316 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006317 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006318 }
William Lallemand00bf1de2012-11-22 17:55:14 +01006319 if (s->comp_algo != NULL) {
6320 /* forwarding trailers */
6321 channel_forward(res, msg->next);
6322 msg->next = 0;
Willy Tarreau1234f4a2014-04-17 21:14:47 +02006323 msg->sov = 0;
William Lallemand00bf1de2012-11-22 17:55:14 +01006324 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02006325 /* we're in HTTP_MSG_DONE now, but we might still have
6326 * some data pending, so let's loop over once.
6327 */
6328 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006329
6330 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006331 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006332
6333 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006334 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006335 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6336 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006337 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006338 if (http_resync_states(s)) {
6339 http_silent_debug(__LINE__, s);
6340 /* some state changes occurred, maybe the analyser
6341 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006342 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006343 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006344 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006345 /* response errors are most likely due to
6346 * the client aborting the transfer.
6347 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006348 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006349 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006350 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006351 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006352 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006353 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006354 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006355 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006356 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006357 }
6358 }
6359
Willy Tarreaud98cf932009-12-27 22:54:55 +01006360 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01006361 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02006362 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6363 compressing = 0;
6364 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006365
6366 if (res->flags & CF_SHUTW)
6367 goto aborted_xfer;
6368
6369 /* stop waiting for data if the input is closed before the end. If the
6370 * client side was already closed, it means that the client has aborted,
6371 * so we don't want to count this as a server abort. Otherwise it's a
6372 * server abort.
6373 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006374 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006375 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6376 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006377 if (!(s->flags & SN_ERR_MASK))
6378 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006379 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006380 if (objt_server(s->target))
6381 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006382 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006383 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006384
Willy Tarreau40dba092010-03-04 18:14:51 +01006385 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006386 if (!s->req->analysers)
6387 goto return_bad_res;
6388
Willy Tarreau877e78d2013-04-07 18:48:08 +02006389 /* forward any pending data starting at res->buf->p */
William Lallemand82fe75c2012-10-23 10:25:10 +02006390 if (s->comp_algo == NULL) {
Willy Tarreauc24715e2014-04-17 15:21:20 +02006391 if (msg->chunk_len || msg->next) {
6392 msg->chunk_len += msg->next;
William Lallemand82fe75c2012-10-23 10:25:10 +02006393 msg->chunk_len -= channel_forward(res, msg->chunk_len);
Willy Tarreauc24715e2014-04-17 15:21:20 +02006394 msg->next = 0;
Willy Tarreau1234f4a2014-04-17 21:14:47 +02006395 msg->sov = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02006396 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006397 }
6398
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006399 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006400 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006401 * Similarly, with keep-alive on the client side, we don't want to forward a
6402 * close.
6403 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006404 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006405 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6406 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006407 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006408
Willy Tarreau5c620922011-05-11 19:56:11 +02006409 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006410 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006411 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006412 * modes are already handled by the stream sock layer. We must not do
6413 * this in content-length mode because it could present the MSG_MORE
6414 * flag with the last block of forwarded data, which would cause an
6415 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006416 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006417 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006418 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006419
Willy Tarreaud98cf932009-12-27 22:54:55 +01006420 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006421 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006422 return 0;
6423
Willy Tarreau40dba092010-03-04 18:14:51 +01006424 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006425 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006426 if (objt_server(s->target))
6427 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006428
6429 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006430 if (unlikely(compressing)) {
6431 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
6432 compressing = 0;
6433 }
6434
Willy Tarreaud98cf932009-12-27 22:54:55 +01006435 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006436 /* don't send any error message as we're in the body */
6437 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006438 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006439 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006440 if (objt_server(s->target))
6441 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006442
6443 if (!(s->flags & SN_ERR_MASK))
6444 s->flags |= SN_ERR_PRXCOND;
6445 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006446 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006447 return 0;
6448
6449 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006450 if (unlikely(compressing)) {
6451 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6452 compressing = 0;
6453 }
6454
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006455 txn->rsp.msg_state = HTTP_MSG_ERROR;
6456 /* don't send any error message as we're in the body */
6457 stream_int_retnclose(res->cons, NULL);
6458 res->analysers = 0;
6459 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6460
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006461 s->fe->fe_counters.cli_aborts++;
6462 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006463 if (objt_server(s->target))
6464 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006465
6466 if (!(s->flags & SN_ERR_MASK))
6467 s->flags |= SN_ERR_CLICL;
6468 if (!(s->flags & SN_FINST_MASK))
6469 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006470 return 0;
6471}
6472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006473/* Iterate the same filter through all request headers.
6474 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006475 * Since it can manage the switch to another backend, it updates the per-proxy
6476 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006477 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006478int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006479{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006480 char term;
6481 char *cur_ptr, *cur_end, *cur_next;
6482 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006483 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006484 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006485 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006487 last_hdr = 0;
6488
Willy Tarreau9b28e032012-10-12 23:49:43 +02006489 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006490 old_idx = 0;
6491
6492 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006493 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006494 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006495 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006496 (exp->action == ACT_ALLOW ||
6497 exp->action == ACT_DENY ||
6498 exp->action == ACT_TARPIT))
6499 return 0;
6500
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006501 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006502 if (!cur_idx)
6503 break;
6504
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006505 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006506 cur_ptr = cur_next;
6507 cur_end = cur_ptr + cur_hdr->len;
6508 cur_next = cur_end + cur_hdr->cr + 1;
6509
6510 /* Now we have one header between cur_ptr and cur_end,
6511 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006512 */
6513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006514 /* The annoying part is that pattern matching needs
6515 * that we modify the contents to null-terminate all
6516 * strings before testing them.
6517 */
6518
6519 term = *cur_end;
6520 *cur_end = '\0';
6521
6522 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6523 switch (exp->action) {
6524 case ACT_SETBE:
6525 /* It is not possible to jump a second time.
6526 * FIXME: should we return an HTTP/500 here so that
6527 * the admin knows there's a problem ?
6528 */
6529 if (t->be != t->fe)
6530 break;
6531
6532 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006533 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006534 last_hdr = 1;
6535 break;
6536
6537 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006538 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006539 last_hdr = 1;
6540 break;
6541
6542 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006543 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006544 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006545 break;
6546
6547 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006548 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006549 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006550 break;
6551
6552 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006553 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6554 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006555 /* FIXME: if the user adds a newline in the replacement, the
6556 * index will not be recalculated for now, and the new line
6557 * will not be counted as a new header.
6558 */
6559
6560 cur_end += delta;
6561 cur_next += delta;
6562 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006563 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006564 break;
6565
6566 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006567 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006568 cur_next += delta;
6569
Willy Tarreaufa355d42009-11-29 18:12:29 +01006570 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006571 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6572 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006573 cur_hdr->len = 0;
6574 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006575 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006576 break;
6577
6578 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006579 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006580 if (cur_end)
6581 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006582
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006583 /* keep the link from this header to next one in case of later
6584 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006585 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006586 old_idx = cur_idx;
6587 }
6588 return 0;
6589}
6590
6591
6592/* Apply the filter to the request line.
6593 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6594 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006595 * Since it can manage the switch to another backend, it updates the per-proxy
6596 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006597 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006598int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006599{
6600 char term;
6601 char *cur_ptr, *cur_end;
6602 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006603 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006604 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006605
Willy Tarreau3d300592007-03-18 18:34:41 +01006606 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006607 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006608 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006609 (exp->action == ACT_ALLOW ||
6610 exp->action == ACT_DENY ||
6611 exp->action == ACT_TARPIT))
6612 return 0;
6613 else if (exp->action == ACT_REMOVE)
6614 return 0;
6615
6616 done = 0;
6617
Willy Tarreau9b28e032012-10-12 23:49:43 +02006618 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006619 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006620
6621 /* Now we have the request line between cur_ptr and cur_end */
6622
6623 /* The annoying part is that pattern matching needs
6624 * that we modify the contents to null-terminate all
6625 * strings before testing them.
6626 */
6627
6628 term = *cur_end;
6629 *cur_end = '\0';
6630
6631 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6632 switch (exp->action) {
6633 case ACT_SETBE:
6634 /* It is not possible to jump a second time.
6635 * FIXME: should we return an HTTP/500 here so that
6636 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006637 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006638 if (t->be != t->fe)
6639 break;
6640
6641 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006642 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006643 done = 1;
6644 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006645
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006646 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006647 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006648 done = 1;
6649 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006650
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006651 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006652 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006653 done = 1;
6654 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006656 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006657 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006658 done = 1;
6659 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006660
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006661 case ACT_REPLACE:
6662 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006663 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6664 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006665 /* FIXME: if the user adds a newline in the replacement, the
6666 * index will not be recalculated for now, and the new line
6667 * will not be counted as a new header.
6668 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006669
Willy Tarreaufa355d42009-11-29 18:12:29 +01006670 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006671 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006672 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006673 HTTP_MSG_RQMETH,
6674 cur_ptr, cur_end + 1,
6675 NULL, NULL);
6676 if (unlikely(!cur_end))
6677 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006678
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006679 /* we have a full request and we know that we have either a CR
6680 * or an LF at <ptr>.
6681 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006682 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6683 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006684 /* there is no point trying this regex on headers */
6685 return 1;
6686 }
6687 }
6688 *cur_end = term; /* restore the string terminator */
6689 return done;
6690}
Willy Tarreau97de6242006-12-27 17:18:38 +01006691
Willy Tarreau58f10d72006-12-04 02:26:12 +01006692
Willy Tarreau58f10d72006-12-04 02:26:12 +01006693
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006694/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006695 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006696 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006697 * unparsable request. Since it can manage the switch to another backend, it
6698 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006699 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006700int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006701{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006702 struct http_txn *txn = &s->txn;
6703 struct hdr_exp *exp;
6704
6705 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006706 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006707
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006708 /*
6709 * The interleaving of transformations and verdicts
6710 * makes it difficult to decide to continue or stop
6711 * the evaluation.
6712 */
6713
Willy Tarreau6c123b12010-01-28 20:22:06 +01006714 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6715 break;
6716
Willy Tarreau3d300592007-03-18 18:34:41 +01006717 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006718 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006719 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006720 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006721
6722 /* if this filter had a condition, evaluate it now and skip to
6723 * next filter if the condition does not match.
6724 */
6725 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006726 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006727 ret = acl_pass(ret);
6728 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6729 ret = !ret;
6730
6731 if (!ret)
6732 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006733 }
6734
6735 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006736 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006737 if (unlikely(ret < 0))
6738 return -1;
6739
6740 if (likely(ret == 0)) {
6741 /* The filter did not match the request, it can be
6742 * iterated through all headers.
6743 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006744 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006745 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006746 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006747 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006748}
6749
6750
Willy Tarreaua15645d2007-03-18 16:22:39 +01006751
Willy Tarreau58f10d72006-12-04 02:26:12 +01006752/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006753 * Try to retrieve the server associated to the appsession.
6754 * If the server is found, it's assigned to the session.
6755 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006756void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006757 struct http_txn *txn = &t->txn;
6758 appsess *asession = NULL;
6759 char *sessid_temp = NULL;
6760
Cyril Bontéb21570a2009-11-29 20:04:48 +01006761 if (len > t->be->appsession_len) {
6762 len = t->be->appsession_len;
6763 }
6764
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006765 if (t->be->options2 & PR_O2_AS_REQL) {
6766 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006767 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006768 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006769 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006770 }
6771
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006772 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006773 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6774 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6775 return;
6776 }
6777
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006778 memcpy(txn->sessid, buf, len);
6779 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006780 }
6781
6782 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6783 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6784 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6785 return;
6786 }
6787
Cyril Bontéb21570a2009-11-29 20:04:48 +01006788 memcpy(sessid_temp, buf, len);
6789 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006790
6791 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6792 /* free previously allocated memory */
6793 pool_free2(apools.sessid, sessid_temp);
6794
6795 if (asession != NULL) {
6796 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6797 if (!(t->be->options2 & PR_O2_AS_REQL))
6798 asession->request_count++;
6799
6800 if (asession->serverid != NULL) {
6801 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006802
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006803 while (srv) {
6804 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006805 if ((srv->state & SRV_RUNNING) ||
6806 (t->be->options & PR_O_PERSIST) ||
6807 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006808 /* we found the server and it's usable */
6809 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006810 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006811 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006812 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006813
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006814 break;
6815 } else {
6816 txn->flags &= ~TX_CK_MASK;
6817 txn->flags |= TX_CK_DOWN;
6818 }
6819 }
6820 srv = srv->next;
6821 }
6822 }
6823 }
6824}
6825
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006826/* Find the end of a cookie value contained between <s> and <e>. It works the
6827 * same way as with headers above except that the semi-colon also ends a token.
6828 * See RFC2965 for more information. Note that it requires a valid header to
6829 * return a valid result.
6830 */
6831char *find_cookie_value_end(char *s, const char *e)
6832{
6833 int quoted, qdpair;
6834
6835 quoted = qdpair = 0;
6836 for (; s < e; s++) {
6837 if (qdpair) qdpair = 0;
6838 else if (quoted) {
6839 if (*s == '\\') qdpair = 1;
6840 else if (*s == '"') quoted = 0;
6841 }
6842 else if (*s == '"') quoted = 1;
6843 else if (*s == ',' || *s == ';') return s;
6844 }
6845 return s;
6846}
6847
6848/* Delete a value in a header between delimiters <from> and <next> in buffer
6849 * <buf>. The number of characters displaced is returned, and the pointer to
6850 * the first delimiter is updated if required. The function tries as much as
6851 * possible to respect the following principles :
6852 * - replace <from> delimiter by the <next> one unless <from> points to a
6853 * colon, in which case <next> is simply removed
6854 * - set exactly one space character after the new first delimiter, unless
6855 * there are not enough characters in the block being moved to do so.
6856 * - remove unneeded spaces before the previous delimiter and after the new
6857 * one.
6858 *
6859 * It is the caller's responsibility to ensure that :
6860 * - <from> points to a valid delimiter or the colon ;
6861 * - <next> points to a valid delimiter or the final CR/LF ;
6862 * - there are non-space chars before <from> ;
6863 * - there is a CR/LF at or after <next>.
6864 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006865int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006866{
6867 char *prev = *from;
6868
6869 if (*prev == ':') {
6870 /* We're removing the first value, preserve the colon and add a
6871 * space if possible.
6872 */
6873 if (!http_is_crlf[(unsigned char)*next])
6874 next++;
6875 prev++;
6876 if (prev < next)
6877 *prev++ = ' ';
6878
6879 while (http_is_spht[(unsigned char)*next])
6880 next++;
6881 } else {
6882 /* Remove useless spaces before the old delimiter. */
6883 while (http_is_spht[(unsigned char)*(prev-1)])
6884 prev--;
6885 *from = prev;
6886
6887 /* copy the delimiter and if possible a space if we're
6888 * not at the end of the line.
6889 */
6890 if (!http_is_crlf[(unsigned char)*next]) {
6891 *prev++ = *next++;
6892 if (prev + 1 < next)
6893 *prev++ = ' ';
6894 while (http_is_spht[(unsigned char)*next])
6895 next++;
6896 }
6897 }
6898 return buffer_replace2(buf, prev, next, NULL, 0);
6899}
6900
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006901/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006902 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006903 * desirable to call it only when needed. This code is quite complex because
6904 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6905 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006906 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006907void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006908{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006909 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006910 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006911 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006912 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6913 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006914
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006915 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006916 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006917 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006918
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006919 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006920 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006921 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006922
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006923 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006924 hdr_beg = hdr_next;
6925 hdr_end = hdr_beg + cur_hdr->len;
6926 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006927
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006928 /* We have one full header between hdr_beg and hdr_end, and the
6929 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006930 * "Cookie:" headers.
6931 */
6932
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006933 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006934 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006935 old_idx = cur_idx;
6936 continue;
6937 }
6938
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006939 del_from = NULL; /* nothing to be deleted */
6940 preserve_hdr = 0; /* assume we may kill the whole header */
6941
Willy Tarreau58f10d72006-12-04 02:26:12 +01006942 /* Now look for cookies. Conforming to RFC2109, we have to support
6943 * attributes whose name begin with a '$', and associate them with
6944 * the right cookie, if we want to delete this cookie.
6945 * So there are 3 cases for each cookie read :
6946 * 1) it's a special attribute, beginning with a '$' : ignore it.
6947 * 2) it's a server id cookie that we *MAY* want to delete : save
6948 * some pointers on it (last semi-colon, beginning of cookie...)
6949 * 3) it's an application cookie : we *MAY* have to delete a previous
6950 * "special" cookie.
6951 * At the end of loop, if a "special" cookie remains, we may have to
6952 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006953 * *MUST* delete it.
6954 *
6955 * Note: RFC2965 is unclear about the processing of spaces around
6956 * the equal sign in the ATTR=VALUE form. A careful inspection of
6957 * the RFC explicitly allows spaces before it, and not within the
6958 * tokens (attrs or values). An inspection of RFC2109 allows that
6959 * too but section 10.1.3 lets one think that spaces may be allowed
6960 * after the equal sign too, resulting in some (rare) buggy
6961 * implementations trying to do that. So let's do what servers do.
6962 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6963 * allowed quoted strings in values, with any possible character
6964 * after a backslash, including control chars and delimitors, which
6965 * causes parsing to become ambiguous. Browsers also allow spaces
6966 * within values even without quotes.
6967 *
6968 * We have to keep multiple pointers in order to support cookie
6969 * removal at the beginning, middle or end of header without
6970 * corrupting the header. All of these headers are valid :
6971 *
6972 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6973 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6974 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6975 * | | | | | | | | |
6976 * | | | | | | | | hdr_end <--+
6977 * | | | | | | | +--> next
6978 * | | | | | | +----> val_end
6979 * | | | | | +-----------> val_beg
6980 * | | | | +--------------> equal
6981 * | | | +----------------> att_end
6982 * | | +---------------------> att_beg
6983 * | +--------------------------> prev
6984 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006985 */
6986
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006987 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6988 /* Iterate through all cookies on this line */
6989
6990 /* find att_beg */
6991 att_beg = prev + 1;
6992 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6993 att_beg++;
6994
6995 /* find att_end : this is the first character after the last non
6996 * space before the equal. It may be equal to hdr_end.
6997 */
6998 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006999
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007000 while (equal < hdr_end) {
7001 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007002 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007003 if (http_is_spht[(unsigned char)*equal++])
7004 continue;
7005 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007006 }
7007
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007008 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7009 * is between <att_beg> and <equal>, both may be identical.
7010 */
7011
7012 /* look for end of cookie if there is an equal sign */
7013 if (equal < hdr_end && *equal == '=') {
7014 /* look for the beginning of the value */
7015 val_beg = equal + 1;
7016 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7017 val_beg++;
7018
7019 /* find the end of the value, respecting quotes */
7020 next = find_cookie_value_end(val_beg, hdr_end);
7021
7022 /* make val_end point to the first white space or delimitor after the value */
7023 val_end = next;
7024 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7025 val_end--;
7026 } else {
7027 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007028 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007029
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007030 /* We have nothing to do with attributes beginning with '$'. However,
7031 * they will automatically be removed if a header before them is removed,
7032 * since they're supposed to be linked together.
7033 */
7034 if (*att_beg == '$')
7035 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007036
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007037 /* Ignore cookies with no equal sign */
7038 if (equal == next) {
7039 /* This is not our cookie, so we must preserve it. But if we already
7040 * scheduled another cookie for removal, we cannot remove the
7041 * complete header, but we can remove the previous block itself.
7042 */
7043 preserve_hdr = 1;
7044 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007045 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007046 val_end += delta;
7047 next += delta;
7048 hdr_end += delta;
7049 hdr_next += delta;
7050 cur_hdr->len += delta;
7051 http_msg_move_end(&txn->req, delta);
7052 prev = del_from;
7053 del_from = NULL;
7054 }
7055 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007056 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007057
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007058 /* if there are spaces around the equal sign, we need to
7059 * strip them otherwise we'll get trouble for cookie captures,
7060 * or even for rewrites. Since this happens extremely rarely,
7061 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007062 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007063 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7064 int stripped_before = 0;
7065 int stripped_after = 0;
7066
7067 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007068 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007069 equal += stripped_before;
7070 val_beg += stripped_before;
7071 }
7072
7073 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007074 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007075 val_beg += stripped_after;
7076 stripped_before += stripped_after;
7077 }
7078
7079 val_end += stripped_before;
7080 next += stripped_before;
7081 hdr_end += stripped_before;
7082 hdr_next += stripped_before;
7083 cur_hdr->len += stripped_before;
7084 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007085 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007086 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007087
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007088 /* First, let's see if we want to capture this cookie. We check
7089 * that we don't already have a client side cookie, because we
7090 * can only capture one. Also as an optimisation, we ignore
7091 * cookies shorter than the declared name.
7092 */
7093 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7094 (val_end - att_beg >= t->fe->capture_namelen) &&
7095 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7096 int log_len = val_end - att_beg;
7097
7098 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7099 Alert("HTTP logging : out of memory.\n");
7100 } else {
7101 if (log_len > t->fe->capture_len)
7102 log_len = t->fe->capture_len;
7103 memcpy(txn->cli_cookie, att_beg, log_len);
7104 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007105 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007106 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007107
Willy Tarreaubca99692010-10-06 19:25:55 +02007108 /* Persistence cookies in passive, rewrite or insert mode have the
7109 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007110 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007111 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007112 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007113 * For cookies in prefix mode, the form is :
7114 *
7115 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007116 */
7117 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7118 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
7119 struct server *srv = t->be->srv;
7120 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007121
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007122 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7123 * have the server ID between val_beg and delim, and the original cookie between
7124 * delim+1 and val_end. Otherwise, delim==val_end :
7125 *
7126 * Cookie: NAME=SRV; # in all but prefix modes
7127 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7128 * | || || | |+-> next
7129 * | || || | +--> val_end
7130 * | || || +---------> delim
7131 * | || |+------------> val_beg
7132 * | || +-------------> att_end = equal
7133 * | |+-----------------> att_beg
7134 * | +------------------> prev
7135 * +-------------------------> hdr_beg
7136 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007137
Willy Tarreau67402132012-05-31 20:40:20 +02007138 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007139 for (delim = val_beg; delim < val_end; delim++)
7140 if (*delim == COOKIE_DELIM)
7141 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007142 } else {
7143 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007144 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007145 /* Now check if the cookie contains a date field, which would
7146 * appear after a vertical bar ('|') just after the server name
7147 * and before the delimiter.
7148 */
7149 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7150 if (vbar1) {
7151 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007152 * right is the last seen date. It is a base64 encoded
7153 * 30-bit value representing the UNIX date since the
7154 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007155 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007156 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007157 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007158 if (val_end - vbar1 >= 5) {
7159 val = b64tos30(vbar1);
7160 if (val > 0)
7161 txn->cookie_last_date = val << 2;
7162 }
7163 /* look for a second vertical bar */
7164 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7165 if (vbar1 && (val_end - vbar1 > 5)) {
7166 val = b64tos30(vbar1 + 1);
7167 if (val > 0)
7168 txn->cookie_first_date = val << 2;
7169 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007170 }
7171 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007172
Willy Tarreauf64d1412010-10-07 20:06:11 +02007173 /* if the cookie has an expiration date and the proxy wants to check
7174 * it, then we do that now. We first check if the cookie is too old,
7175 * then only if it has expired. We detect strict overflow because the
7176 * time resolution here is not great (4 seconds). Cookies with dates
7177 * in the future are ignored if their offset is beyond one day. This
7178 * allows an admin to fix timezone issues without expiring everyone
7179 * and at the same time avoids keeping unwanted side effects for too
7180 * long.
7181 */
7182 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007183 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
7184 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007185 txn->flags &= ~TX_CK_MASK;
7186 txn->flags |= TX_CK_OLD;
7187 delim = val_beg; // let's pretend we have not found the cookie
7188 txn->cookie_first_date = 0;
7189 txn->cookie_last_date = 0;
7190 }
7191 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007192 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
7193 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007194 txn->flags &= ~TX_CK_MASK;
7195 txn->flags |= TX_CK_EXPIRED;
7196 delim = val_beg; // let's pretend we have not found the cookie
7197 txn->cookie_first_date = 0;
7198 txn->cookie_last_date = 0;
7199 }
7200
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007201 /* Here, we'll look for the first running server which supports the cookie.
7202 * This allows to share a same cookie between several servers, for example
7203 * to dedicate backup servers to specific servers only.
7204 * However, to prevent clients from sticking to cookie-less backup server
7205 * when they have incidentely learned an empty cookie, we simply ignore
7206 * empty cookies and mark them as invalid.
7207 * The same behaviour is applied when persistence must be ignored.
7208 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007209 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007210 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007211
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007212 while (srv) {
7213 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7214 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7215 if ((srv->state & SRV_RUNNING) ||
7216 (t->be->options & PR_O_PERSIST) ||
7217 (t->flags & SN_FORCE_PRST)) {
7218 /* we found the server and we can use it */
7219 txn->flags &= ~TX_CK_MASK;
7220 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7221 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007222 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007223 break;
7224 } else {
7225 /* we found a server, but it's down,
7226 * mark it as such and go on in case
7227 * another one is available.
7228 */
7229 txn->flags &= ~TX_CK_MASK;
7230 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007231 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007232 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007233 srv = srv->next;
7234 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007235
Willy Tarreauf64d1412010-10-07 20:06:11 +02007236 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007237 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007238 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007239 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7240 txn->flags |= TX_CK_UNUSED;
7241 else
7242 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007243 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007244
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007245 /* depending on the cookie mode, we may have to either :
7246 * - delete the complete cookie if we're in insert+indirect mode, so that
7247 * the server never sees it ;
7248 * - remove the server id from the cookie value, and tag the cookie as an
7249 * application cookie so that it does not get accidentely removed later,
7250 * if we're in cookie prefix mode
7251 */
Willy Tarreau67402132012-05-31 20:40:20 +02007252 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007253 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007254
Willy Tarreau9b28e032012-10-12 23:49:43 +02007255 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007256 val_end += delta;
7257 next += delta;
7258 hdr_end += delta;
7259 hdr_next += delta;
7260 cur_hdr->len += delta;
7261 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007262
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007263 del_from = NULL;
7264 preserve_hdr = 1; /* we want to keep this cookie */
7265 }
7266 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007267 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007268 del_from = prev;
7269 }
7270 } else {
7271 /* This is not our cookie, so we must preserve it. But if we already
7272 * scheduled another cookie for removal, we cannot remove the
7273 * complete header, but we can remove the previous block itself.
7274 */
7275 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007276
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007277 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007278 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007279 if (att_beg >= del_from)
7280 att_beg += delta;
7281 if (att_end >= del_from)
7282 att_end += delta;
7283 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007284 val_end += delta;
7285 next += delta;
7286 hdr_end += delta;
7287 hdr_next += delta;
7288 cur_hdr->len += delta;
7289 http_msg_move_end(&txn->req, delta);
7290 prev = del_from;
7291 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007292 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007293 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007294
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007295 /* Look for the appsession cookie unless persistence must be ignored */
7296 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7297 int cmp_len, value_len;
7298 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007299
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007300 if (t->be->options2 & PR_O2_AS_PFX) {
7301 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7302 value_begin = att_beg + t->be->appsession_name_len;
7303 value_len = val_end - att_beg - t->be->appsession_name_len;
7304 } else {
7305 cmp_len = att_end - att_beg;
7306 value_begin = val_beg;
7307 value_len = val_end - val_beg;
7308 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007309
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007310 /* let's see if the cookie is our appcookie */
7311 if (cmp_len == t->be->appsession_name_len &&
7312 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7313 manage_client_side_appsession(t, value_begin, value_len);
7314 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007315 }
7316
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007317 /* continue with next cookie on this header line */
7318 att_beg = next;
7319 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007320
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007321 /* There are no more cookies on this line.
7322 * We may still have one (or several) marked for deletion at the
7323 * end of the line. We must do this now in two ways :
7324 * - if some cookies must be preserved, we only delete from the
7325 * mark to the end of line ;
7326 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007327 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007328 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007329 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007330 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007331 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007332 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007333 cur_hdr->len += delta;
7334 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007335 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007336
7337 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007338 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7339 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007340 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007341 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007342 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007343 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007344 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007345 }
7346
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007347 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007348 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007349 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007350}
7351
7352
Willy Tarreaua15645d2007-03-18 16:22:39 +01007353/* Iterate the same filter through all response headers contained in <rtr>.
7354 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7355 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007356int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007357{
7358 char term;
7359 char *cur_ptr, *cur_end, *cur_next;
7360 int cur_idx, old_idx, last_hdr;
7361 struct http_txn *txn = &t->txn;
7362 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007363 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007364
7365 last_hdr = 0;
7366
Willy Tarreau9b28e032012-10-12 23:49:43 +02007367 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007368 old_idx = 0;
7369
7370 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007371 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007372 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007373 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007374 (exp->action == ACT_ALLOW ||
7375 exp->action == ACT_DENY))
7376 return 0;
7377
7378 cur_idx = txn->hdr_idx.v[old_idx].next;
7379 if (!cur_idx)
7380 break;
7381
7382 cur_hdr = &txn->hdr_idx.v[cur_idx];
7383 cur_ptr = cur_next;
7384 cur_end = cur_ptr + cur_hdr->len;
7385 cur_next = cur_end + cur_hdr->cr + 1;
7386
7387 /* Now we have one header between cur_ptr and cur_end,
7388 * and the next header starts at cur_next.
7389 */
7390
7391 /* The annoying part is that pattern matching needs
7392 * that we modify the contents to null-terminate all
7393 * strings before testing them.
7394 */
7395
7396 term = *cur_end;
7397 *cur_end = '\0';
7398
7399 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7400 switch (exp->action) {
7401 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007402 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007403 last_hdr = 1;
7404 break;
7405
7406 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007407 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007408 last_hdr = 1;
7409 break;
7410
7411 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007412 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7413 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007414 /* FIXME: if the user adds a newline in the replacement, the
7415 * index will not be recalculated for now, and the new line
7416 * will not be counted as a new header.
7417 */
7418
7419 cur_end += delta;
7420 cur_next += delta;
7421 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007422 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007423 break;
7424
7425 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007426 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007427 cur_next += delta;
7428
Willy Tarreaufa355d42009-11-29 18:12:29 +01007429 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007430 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7431 txn->hdr_idx.used--;
7432 cur_hdr->len = 0;
7433 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007434 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007435 break;
7436
7437 }
7438 }
7439 if (cur_end)
7440 *cur_end = term; /* restore the string terminator */
7441
7442 /* keep the link from this header to next one in case of later
7443 * removal of next header.
7444 */
7445 old_idx = cur_idx;
7446 }
7447 return 0;
7448}
7449
7450
7451/* Apply the filter to the status line in the response buffer <rtr>.
7452 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7453 * or -1 if a replacement resulted in an invalid status line.
7454 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007455int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007456{
7457 char term;
7458 char *cur_ptr, *cur_end;
7459 int done;
7460 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007461 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007462
7463
Willy Tarreau3d300592007-03-18 18:34:41 +01007464 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007465 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007466 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007467 (exp->action == ACT_ALLOW ||
7468 exp->action == ACT_DENY))
7469 return 0;
7470 else if (exp->action == ACT_REMOVE)
7471 return 0;
7472
7473 done = 0;
7474
Willy Tarreau9b28e032012-10-12 23:49:43 +02007475 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007476 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007477
7478 /* Now we have the status line between cur_ptr and cur_end */
7479
7480 /* The annoying part is that pattern matching needs
7481 * that we modify the contents to null-terminate all
7482 * strings before testing them.
7483 */
7484
7485 term = *cur_end;
7486 *cur_end = '\0';
7487
7488 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7489 switch (exp->action) {
7490 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007491 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007492 done = 1;
7493 break;
7494
7495 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007496 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007497 done = 1;
7498 break;
7499
7500 case ACT_REPLACE:
7501 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007502 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7503 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007504 /* FIXME: if the user adds a newline in the replacement, the
7505 * index will not be recalculated for now, and the new line
7506 * will not be counted as a new header.
7507 */
7508
Willy Tarreaufa355d42009-11-29 18:12:29 +01007509 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007510 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007511 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007512 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007513 cur_ptr, cur_end + 1,
7514 NULL, NULL);
7515 if (unlikely(!cur_end))
7516 return -1;
7517
7518 /* we have a full respnse and we know that we have either a CR
7519 * or an LF at <ptr>.
7520 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007521 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007522 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007523 /* there is no point trying this regex on headers */
7524 return 1;
7525 }
7526 }
7527 *cur_end = term; /* restore the string terminator */
7528 return done;
7529}
7530
7531
7532
7533/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007534 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007535 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7536 * unparsable response.
7537 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007538int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007539{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007540 struct http_txn *txn = &s->txn;
7541 struct hdr_exp *exp;
7542
7543 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007544 int ret;
7545
7546 /*
7547 * The interleaving of transformations and verdicts
7548 * makes it difficult to decide to continue or stop
7549 * the evaluation.
7550 */
7551
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007552 if (txn->flags & TX_SVDENY)
7553 break;
7554
Willy Tarreau3d300592007-03-18 18:34:41 +01007555 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007556 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7557 exp->action == ACT_PASS)) {
7558 exp = exp->next;
7559 continue;
7560 }
7561
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007562 /* if this filter had a condition, evaluate it now and skip to
7563 * next filter if the condition does not match.
7564 */
7565 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007566 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007567 ret = acl_pass(ret);
7568 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7569 ret = !ret;
7570 if (!ret)
7571 continue;
7572 }
7573
Willy Tarreaua15645d2007-03-18 16:22:39 +01007574 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007575 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007576 if (unlikely(ret < 0))
7577 return -1;
7578
7579 if (likely(ret == 0)) {
7580 /* The filter did not match the response, it can be
7581 * iterated through all headers.
7582 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007583 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007584 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007585 }
7586 return 0;
7587}
7588
7589
Willy Tarreaua15645d2007-03-18 16:22:39 +01007590/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007591 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007592 * desirable to call it only when needed. This function is also used when we
7593 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007594 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007595void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007596{
7597 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007598 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007599 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007600 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007601 char *hdr_beg, *hdr_end, *hdr_next;
7602 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007603
Willy Tarreaua15645d2007-03-18 16:22:39 +01007604 /* Iterate through the headers.
7605 * we start with the start line.
7606 */
7607 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007608 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007609
7610 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7611 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007612 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007613
7614 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007615 hdr_beg = hdr_next;
7616 hdr_end = hdr_beg + cur_hdr->len;
7617 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007618
Willy Tarreau24581ba2010-08-31 22:39:35 +02007619 /* We have one full header between hdr_beg and hdr_end, and the
7620 * next header starts at hdr_next. We're only interested in
7621 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007622 */
7623
Willy Tarreau24581ba2010-08-31 22:39:35 +02007624 is_cookie2 = 0;
7625 prev = hdr_beg + 10;
7626 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007627 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007628 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7629 if (!val) {
7630 old_idx = cur_idx;
7631 continue;
7632 }
7633 is_cookie2 = 1;
7634 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007635 }
7636
Willy Tarreau24581ba2010-08-31 22:39:35 +02007637 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7638 * <prev> points to the colon.
7639 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007640 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007641
Willy Tarreau24581ba2010-08-31 22:39:35 +02007642 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7643 * check-cache is enabled) and we are not interested in checking
7644 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007645 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007646 if (t->be->cookie_name == NULL &&
7647 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007648 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007649 return;
7650
Willy Tarreau24581ba2010-08-31 22:39:35 +02007651 /* OK so now we know we have to process this response cookie.
7652 * The format of the Set-Cookie header is slightly different
7653 * from the format of the Cookie header in that it does not
7654 * support the comma as a cookie delimiter (thus the header
7655 * cannot be folded) because the Expires attribute described in
7656 * the original Netscape's spec may contain an unquoted date
7657 * with a comma inside. We have to live with this because
7658 * many browsers don't support Max-Age and some browsers don't
7659 * support quoted strings. However the Set-Cookie2 header is
7660 * clean.
7661 *
7662 * We have to keep multiple pointers in order to support cookie
7663 * removal at the beginning, middle or end of header without
7664 * corrupting the header (in case of set-cookie2). A special
7665 * pointer, <scav> points to the beginning of the set-cookie-av
7666 * fields after the first semi-colon. The <next> pointer points
7667 * either to the end of line (set-cookie) or next unquoted comma
7668 * (set-cookie2). All of these headers are valid :
7669 *
7670 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7671 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7672 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7673 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7674 * | | | | | | | | | |
7675 * | | | | | | | | +-> next hdr_end <--+
7676 * | | | | | | | +------------> scav
7677 * | | | | | | +--------------> val_end
7678 * | | | | | +--------------------> val_beg
7679 * | | | | +----------------------> equal
7680 * | | | +------------------------> att_end
7681 * | | +----------------------------> att_beg
7682 * | +------------------------------> prev
7683 * +-----------------------------------------> hdr_beg
7684 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007685
Willy Tarreau24581ba2010-08-31 22:39:35 +02007686 for (; prev < hdr_end; prev = next) {
7687 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007688
Willy Tarreau24581ba2010-08-31 22:39:35 +02007689 /* find att_beg */
7690 att_beg = prev + 1;
7691 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7692 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007693
Willy Tarreau24581ba2010-08-31 22:39:35 +02007694 /* find att_end : this is the first character after the last non
7695 * space before the equal. It may be equal to hdr_end.
7696 */
7697 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007698
Willy Tarreau24581ba2010-08-31 22:39:35 +02007699 while (equal < hdr_end) {
7700 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7701 break;
7702 if (http_is_spht[(unsigned char)*equal++])
7703 continue;
7704 att_end = equal;
7705 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007706
Willy Tarreau24581ba2010-08-31 22:39:35 +02007707 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7708 * is between <att_beg> and <equal>, both may be identical.
7709 */
7710
7711 /* look for end of cookie if there is an equal sign */
7712 if (equal < hdr_end && *equal == '=') {
7713 /* look for the beginning of the value */
7714 val_beg = equal + 1;
7715 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7716 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007717
Willy Tarreau24581ba2010-08-31 22:39:35 +02007718 /* find the end of the value, respecting quotes */
7719 next = find_cookie_value_end(val_beg, hdr_end);
7720
7721 /* make val_end point to the first white space or delimitor after the value */
7722 val_end = next;
7723 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7724 val_end--;
7725 } else {
7726 /* <equal> points to next comma, semi-colon or EOL */
7727 val_beg = val_end = next = equal;
7728 }
7729
7730 if (next < hdr_end) {
7731 /* Set-Cookie2 supports multiple cookies, and <next> points to
7732 * a colon or semi-colon before the end. So skip all attr-value
7733 * pairs and look for the next comma. For Set-Cookie, since
7734 * commas are permitted in values, skip to the end.
7735 */
7736 if (is_cookie2)
7737 next = find_hdr_value_end(next, hdr_end);
7738 else
7739 next = hdr_end;
7740 }
7741
7742 /* Now everything is as on the diagram above */
7743
7744 /* Ignore cookies with no equal sign */
7745 if (equal == val_end)
7746 continue;
7747
7748 /* If there are spaces around the equal sign, we need to
7749 * strip them otherwise we'll get trouble for cookie captures,
7750 * or even for rewrites. Since this happens extremely rarely,
7751 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007752 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007753 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7754 int stripped_before = 0;
7755 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007756
Willy Tarreau24581ba2010-08-31 22:39:35 +02007757 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007758 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007759 equal += stripped_before;
7760 val_beg += stripped_before;
7761 }
7762
7763 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007764 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007765 val_beg += stripped_after;
7766 stripped_before += stripped_after;
7767 }
7768
7769 val_end += stripped_before;
7770 next += stripped_before;
7771 hdr_end += stripped_before;
7772 hdr_next += stripped_before;
7773 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007774 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007775 }
7776
7777 /* First, let's see if we want to capture this cookie. We check
7778 * that we don't already have a server side cookie, because we
7779 * can only capture one. Also as an optimisation, we ignore
7780 * cookies shorter than the declared name.
7781 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007782 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007783 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007784 (val_end - att_beg >= t->fe->capture_namelen) &&
7785 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7786 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007787 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007788 Alert("HTTP logging : out of memory.\n");
7789 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007790 else {
7791 if (log_len > t->fe->capture_len)
7792 log_len = t->fe->capture_len;
7793 memcpy(txn->srv_cookie, att_beg, log_len);
7794 txn->srv_cookie[log_len] = 0;
7795 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007796 }
7797
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007798 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007799 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007800 if (!(t->flags & SN_IGNORE_PRST) &&
7801 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7802 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007803 /* assume passive cookie by default */
7804 txn->flags &= ~TX_SCK_MASK;
7805 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007806
7807 /* If the cookie is in insert mode on a known server, we'll delete
7808 * this occurrence because we'll insert another one later.
7809 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007810 * a direct access.
7811 */
Willy Tarreau67402132012-05-31 20:40:20 +02007812 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007813 /* The "preserve" flag was set, we don't want to touch the
7814 * server's cookie.
7815 */
7816 }
Willy Tarreau67402132012-05-31 20:40:20 +02007817 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7818 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007819 /* this cookie must be deleted */
7820 if (*prev == ':' && next == hdr_end) {
7821 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007822 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007823 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7824 txn->hdr_idx.used--;
7825 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007826 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007827 hdr_next += delta;
7828 http_msg_move_end(&txn->rsp, delta);
7829 /* note: while both invalid now, <next> and <hdr_end>
7830 * are still equal, so the for() will stop as expected.
7831 */
7832 } else {
7833 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007834 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007835 next = prev;
7836 hdr_end += delta;
7837 hdr_next += delta;
7838 cur_hdr->len += delta;
7839 http_msg_move_end(&txn->rsp, delta);
7840 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007841 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007842 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007843 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007844 }
Willy Tarreau67402132012-05-31 20:40:20 +02007845 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007846 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007847 * with this server since we know it.
7848 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007849 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007850 next += delta;
7851 hdr_end += delta;
7852 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007853 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007854 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007855
Willy Tarreauf1348312010-10-07 15:54:11 +02007856 txn->flags &= ~TX_SCK_MASK;
7857 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007858 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007859 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007860 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007861 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007862 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007863 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007864 next += delta;
7865 hdr_end += delta;
7866 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007867 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007868 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007869
Willy Tarreau827aee92011-03-10 16:55:02 +01007870 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007871 txn->flags &= ~TX_SCK_MASK;
7872 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007873 }
7874 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007875 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7876 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007877 int cmp_len, value_len;
7878 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007879
Cyril Bontéb21570a2009-11-29 20:04:48 +01007880 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007881 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7882 value_begin = att_beg + t->be->appsession_name_len;
7883 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007884 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007885 cmp_len = att_end - att_beg;
7886 value_begin = val_beg;
7887 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007888 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007889
Cyril Bonté17530c32010-04-06 21:11:10 +02007890 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007891 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7892 /* free a possibly previously allocated memory */
7893 pool_free2(apools.sessid, txn->sessid);
7894
Cyril Bontéb21570a2009-11-29 20:04:48 +01007895 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007896 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007897 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7898 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7899 return;
7900 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007901 memcpy(txn->sessid, value_begin, value_len);
7902 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007903 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007904 }
7905 /* that's done for this cookie, check the next one on the same
7906 * line when next != hdr_end (only if is_cookie2).
7907 */
7908 }
7909 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007910 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007911 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007912
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007913 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007914 appsess *asession = NULL;
7915 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007916 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007917 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007918 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007919 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7920 Alert("Not enough Memory process_srv():asession:calloc().\n");
7921 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7922 return;
7923 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007924 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7925
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007926 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7927 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7928 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007929 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007930 return;
7931 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007932 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007933 asession->sessid[t->be->appsession_len] = 0;
7934
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007935 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007936 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007937 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007938 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007939 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007940 return;
7941 }
7942 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007943 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007944
7945 asession->request_count = 0;
7946 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7947 }
7948
7949 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7950 asession->request_count++;
7951 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007952}
7953
7954
Willy Tarreaua15645d2007-03-18 16:22:39 +01007955/*
7956 * Check if response is cacheable or not. Updates t->flags.
7957 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007958void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007959{
7960 struct http_txn *txn = &t->txn;
7961 char *p1, *p2;
7962
7963 char *cur_ptr, *cur_end, *cur_next;
7964 int cur_idx;
7965
Willy Tarreau5df51872007-11-25 16:20:08 +01007966 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007967 return;
7968
7969 /* Iterate through the headers.
7970 * we start with the start line.
7971 */
7972 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007973 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007974
7975 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7976 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007977 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007978
7979 cur_hdr = &txn->hdr_idx.v[cur_idx];
7980 cur_ptr = cur_next;
7981 cur_end = cur_ptr + cur_hdr->len;
7982 cur_next = cur_end + cur_hdr->cr + 1;
7983
7984 /* We have one full header between cur_ptr and cur_end, and the
7985 * next header starts at cur_next. We're only interested in
7986 * "Cookie:" headers.
7987 */
7988
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007989 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7990 if (val) {
7991 if ((cur_end - (cur_ptr + val) >= 8) &&
7992 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7993 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7994 return;
7995 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007996 }
7997
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007998 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7999 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008000 continue;
8001
8002 /* OK, right now we know we have a cache-control header at cur_ptr */
8003
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008004 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008005
8006 if (p1 >= cur_end) /* no more info */
8007 continue;
8008
8009 /* p1 is at the beginning of the value */
8010 p2 = p1;
8011
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008012 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008013 p2++;
8014
8015 /* we have a complete value between p1 and p2 */
8016 if (p2 < cur_end && *p2 == '=') {
8017 /* we have something of the form no-cache="set-cookie" */
8018 if ((cur_end - p1 >= 21) &&
8019 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8020 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008021 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008022 continue;
8023 }
8024
8025 /* OK, so we know that either p2 points to the end of string or to a comma */
8026 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008027 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8029 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8030 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008031 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008032 return;
8033 }
8034
8035 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008036 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008037 continue;
8038 }
8039 }
8040}
8041
8042
Willy Tarreau58f10d72006-12-04 02:26:12 +01008043/*
8044 * Try to retrieve a known appsession in the URI, then the associated server.
8045 * If the server is found, it's assigned to the session.
8046 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008047void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008048{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008049 char *end_params, *first_param, *cur_param, *next_param;
8050 char separator;
8051 int value_len;
8052
8053 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008054
Willy Tarreaue2e27a52007-04-01 00:01:37 +02008055 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02008056 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST && t->txn.meth != HTTP_METH_HEAD)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008057 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008058 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008059
Cyril Bontéb21570a2009-11-29 20:04:48 +01008060 first_param = NULL;
8061 switch (mode) {
8062 case PR_O2_AS_M_PP:
8063 first_param = memchr(begin, ';', len);
8064 break;
8065 case PR_O2_AS_M_QS:
8066 first_param = memchr(begin, '?', len);
8067 break;
8068 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008069
Cyril Bontéb21570a2009-11-29 20:04:48 +01008070 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008071 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008072 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008073
Cyril Bontéb21570a2009-11-29 20:04:48 +01008074 switch (mode) {
8075 case PR_O2_AS_M_PP:
8076 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8077 end_params = (char *) begin + len;
8078 }
8079 separator = ';';
8080 break;
8081 case PR_O2_AS_M_QS:
8082 end_params = (char *) begin + len;
8083 separator = '&';
8084 break;
8085 default:
8086 /* unknown mode, shouldn't happen */
8087 return;
8088 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008089
Cyril Bontéb21570a2009-11-29 20:04:48 +01008090 cur_param = next_param = end_params;
8091 while (cur_param > first_param) {
8092 cur_param--;
8093 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8094 /* let's see if this is the appsession parameter */
8095 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
8096 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
8097 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
8098 /* Cool... it's the right one */
8099 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8100 value_len = MIN(t->be->appsession_len, next_param - cur_param);
8101 if (value_len > 0) {
8102 manage_client_side_appsession(t, cur_param, value_len);
8103 }
8104 break;
8105 }
8106 next_param = cur_param;
8107 }
8108 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008109#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008110 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02008111 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008112#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008113}
8114
Willy Tarreaub2513902006-12-17 14:52:38 +01008115/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008116 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008117 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008118 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008119 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008120 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008121 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008122 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008123 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008124int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008125{
8126 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008127 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008128 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008129
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008130 if (!uri_auth)
8131 return 0;
8132
Cyril Bonté70be45d2010-10-12 00:14:35 +02008133 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008134 return 0;
8135
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008136 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008137 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008138 return 0;
8139
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008140 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008141 return 0;
8142
Willy Tarreaub2513902006-12-17 14:52:38 +01008143 return 1;
8144}
8145
Willy Tarreau4076a152009-04-02 15:18:36 +02008146/*
8147 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008148 * By default it tries to report the error position as msg->err_pos. However if
8149 * this one is not set, it will then report msg->next, which is the last known
8150 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008151 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008152 */
8153void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008154 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008155 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008156{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008157 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008158 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008159
Willy Tarreau9b28e032012-10-12 23:49:43 +02008160 es->len = MIN(chn->buf->i, sizeof(es->buf));
8161 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008162 len1 = MIN(len1, es->len);
8163 len2 = es->len - len1; /* remaining data if buffer wraps */
8164
Willy Tarreau9b28e032012-10-12 23:49:43 +02008165 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008166 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008167 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008168
Willy Tarreau4076a152009-04-02 15:18:36 +02008169 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008170 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008171 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008172 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008173
Willy Tarreau4076a152009-04-02 15:18:36 +02008174 es->when = date; // user-visible date
8175 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008176 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008177 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008178 if (objt_conn(s->req->prod->end))
8179 es->src = __objt_conn(s->req->prod->end)->addr.from;
8180 else
8181 memset(&es->src, 0, sizeof(es->src));
8182
Willy Tarreau078272e2010-12-12 12:46:33 +01008183 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008184 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008185 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008186 es->s_flags = s->flags;
8187 es->t_flags = s->txn.flags;
8188 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008189 es->b_out = chn->buf->o;
8190 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008191 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008192 es->m_clen = msg->chunk_len;
8193 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008194}
Willy Tarreaub2513902006-12-17 14:52:38 +01008195
Willy Tarreau294c4732011-12-16 21:35:50 +01008196/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8197 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8198 * performed over the whole headers. Otherwise it must contain a valid header
8199 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8200 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8201 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8202 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008203 * -1. The value fetch stops at commas, so this function is suited for use with
8204 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008205 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008206 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008207unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008208 struct hdr_idx *idx, int occ,
8209 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008210{
Willy Tarreau294c4732011-12-16 21:35:50 +01008211 struct hdr_ctx local_ctx;
8212 char *ptr_hist[MAX_HDR_HISTORY];
8213 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008214 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008215 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008216
Willy Tarreau294c4732011-12-16 21:35:50 +01008217 if (!ctx) {
8218 local_ctx.idx = 0;
8219 ctx = &local_ctx;
8220 }
8221
Willy Tarreaubce70882009-09-07 11:51:47 +02008222 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008223 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008224 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008225 occ--;
8226 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008227 *vptr = ctx->line + ctx->val;
8228 *vlen = ctx->vlen;
8229 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008230 }
8231 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008232 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008233 }
8234
8235 /* negative occurrence, we scan all the list then walk back */
8236 if (-occ > MAX_HDR_HISTORY)
8237 return 0;
8238
Willy Tarreau294c4732011-12-16 21:35:50 +01008239 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008240 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008241 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8242 len_hist[hist_ptr] = ctx->vlen;
8243 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008244 hist_ptr = 0;
8245 found++;
8246 }
8247 if (-occ > found)
8248 return 0;
8249 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008250 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8251 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8252 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008253 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008254 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008255 if (hist_ptr >= MAX_HDR_HISTORY)
8256 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008257 *vptr = ptr_hist[hist_ptr];
8258 *vlen = len_hist[hist_ptr];
8259 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008260}
8261
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008262/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8263 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8264 * performed over the whole headers. Otherwise it must contain a valid header
8265 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8266 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8267 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8268 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8269 * -1. This function differs from http_get_hdr() in that it only returns full
8270 * line header values and does not stop at commas.
8271 * The return value is 0 if nothing was found, or non-zero otherwise.
8272 */
8273unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8274 struct hdr_idx *idx, int occ,
8275 struct hdr_ctx *ctx, char **vptr, int *vlen)
8276{
8277 struct hdr_ctx local_ctx;
8278 char *ptr_hist[MAX_HDR_HISTORY];
8279 int len_hist[MAX_HDR_HISTORY];
8280 unsigned int hist_ptr;
8281 int found;
8282
8283 if (!ctx) {
8284 local_ctx.idx = 0;
8285 ctx = &local_ctx;
8286 }
8287
8288 if (occ >= 0) {
8289 /* search from the beginning */
8290 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8291 occ--;
8292 if (occ <= 0) {
8293 *vptr = ctx->line + ctx->val;
8294 *vlen = ctx->vlen;
8295 return 1;
8296 }
8297 }
8298 return 0;
8299 }
8300
8301 /* negative occurrence, we scan all the list then walk back */
8302 if (-occ > MAX_HDR_HISTORY)
8303 return 0;
8304
8305 found = hist_ptr = 0;
8306 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8307 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8308 len_hist[hist_ptr] = ctx->vlen;
8309 if (++hist_ptr >= MAX_HDR_HISTORY)
8310 hist_ptr = 0;
8311 found++;
8312 }
8313 if (-occ > found)
8314 return 0;
8315 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8316 * find occurrence -occ, so we have to check [hist_ptr+occ].
8317 */
8318 hist_ptr += occ;
8319 if (hist_ptr >= MAX_HDR_HISTORY)
8320 hist_ptr -= MAX_HDR_HISTORY;
8321 *vptr = ptr_hist[hist_ptr];
8322 *vlen = len_hist[hist_ptr];
8323 return 1;
8324}
8325
Willy Tarreaubaaee002006-06-26 02:48:02 +02008326/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008327 * Print a debug line with a header. Always stop at the first CR or LF char,
8328 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8329 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008330 */
8331void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8332{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008333 int max;
8334 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008335 dir,
8336 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
8337 objt_conn(t->req->cons->end) ? (unsigned short)objt_conn(t->req->cons->end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008338
8339 for (max = 0; start + max < end; max++)
8340 if (start[max] == '\r' || start[max] == '\n')
8341 break;
8342
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008343 UBOUND(max, trash.size - trash.len - 3);
8344 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8345 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008346 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008347}
8348
Willy Tarreau0937bc42009-12-22 15:03:09 +01008349/*
8350 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8351 * the required fields are properly allocated and that we only need to (re)init
8352 * them. This should be used before processing any new request.
8353 */
8354void http_init_txn(struct session *s)
8355{
8356 struct http_txn *txn = &s->txn;
8357 struct proxy *fe = s->fe;
8358
8359 txn->flags = 0;
8360 txn->status = -1;
8361
Willy Tarreauf64d1412010-10-07 20:06:11 +02008362 txn->cookie_first_date = 0;
8363 txn->cookie_last_date = 0;
8364
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008365 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008366 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008367 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008368 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008369 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008370 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008371 txn->req.chunk_len = 0LL;
8372 txn->req.body_len = 0LL;
8373 txn->rsp.chunk_len = 0LL;
8374 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008375 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8376 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008377 txn->req.chn = s->req;
8378 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008379
8380 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008381
8382 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8383 if (fe->options2 & PR_O2_REQBUG_OK)
8384 txn->req.err_pos = -1; /* let buggy requests pass */
8385
Willy Tarreau46023632010-01-07 22:51:47 +01008386 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008387 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8388
Willy Tarreau46023632010-01-07 22:51:47 +01008389 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008390 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8391
8392 if (txn->hdr_idx.v)
8393 hdr_idx_init(&txn->hdr_idx);
8394}
8395
8396/* to be used at the end of a transaction */
8397void http_end_txn(struct session *s)
8398{
8399 struct http_txn *txn = &s->txn;
8400
Willy Tarreau75195602014-03-11 15:48:55 +01008401 /* release any possible compression context */
8402 if (s->flags & SN_COMP_READY)
8403 s->comp_algo->end(&s->comp_ctx);
8404 s->comp_algo = NULL;
8405 s->flags &= ~SN_COMP_READY;
8406
Willy Tarreau0937bc42009-12-22 15:03:09 +01008407 /* these ones will have been dynamically allocated */
8408 pool_free2(pool2_requri, txn->uri);
8409 pool_free2(pool2_capture, txn->cli_cookie);
8410 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008411 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008412 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008413
William Lallemanda73203e2012-03-12 12:48:57 +01008414 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008415 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008416 txn->uri = NULL;
8417 txn->srv_cookie = NULL;
8418 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008419
8420 if (txn->req.cap) {
8421 struct cap_hdr *h;
8422 for (h = s->fe->req_cap; h; h = h->next)
8423 pool_free2(h->pool, txn->req.cap[h->index]);
8424 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8425 }
8426
8427 if (txn->rsp.cap) {
8428 struct cap_hdr *h;
8429 for (h = s->fe->rsp_cap; h; h = h->next)
8430 pool_free2(h->pool, txn->rsp.cap[h->index]);
8431 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8432 }
8433
Willy Tarreau0937bc42009-12-22 15:03:09 +01008434}
8435
8436/* to be used at the end of a transaction to prepare a new one */
8437void http_reset_txn(struct session *s)
8438{
8439 http_end_txn(s);
8440 http_init_txn(s);
8441
8442 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008443 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008444 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008445 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008446 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008447 /* re-init store persistence */
8448 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008449 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008450
Willy Tarreau0937bc42009-12-22 15:03:09 +01008451 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008452
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008453 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008454
Willy Tarreau739cfba2010-01-25 23:11:14 +01008455 /* We must trim any excess data from the response buffer, because we
8456 * may have blocked an invalid response from a server that we don't
8457 * want to accidentely forward once we disable the analysers, nor do
8458 * we want those data to come along with next response. A typical
8459 * example of such data would be from a buggy server responding to
8460 * a HEAD with some data, or sending more than the advertised
8461 * content-length.
8462 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008463 if (unlikely(s->rep->buf->i))
8464 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008465
Willy Tarreau0937bc42009-12-22 15:03:09 +01008466 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008467 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008468
Willy Tarreaud04e8582010-05-31 12:31:35 +02008469 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008470 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008471
8472 s->req->rex = TICK_ETERNITY;
8473 s->req->wex = TICK_ETERNITY;
8474 s->req->analyse_exp = TICK_ETERNITY;
8475 s->rep->rex = TICK_ETERNITY;
8476 s->rep->wex = TICK_ETERNITY;
8477 s->rep->analyse_exp = TICK_ETERNITY;
8478}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008479
Willy Tarreauff011f22011-01-06 17:51:27 +01008480void free_http_req_rules(struct list *r) {
8481 struct http_req_rule *tr, *pr;
8482
8483 list_for_each_entry_safe(pr, tr, r, list) {
8484 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008485 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008486 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008487
8488 free(pr);
8489 }
8490}
8491
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008492/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008493struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8494{
8495 struct http_req_rule *rule;
8496 int cur_arg;
8497
8498 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8499 if (!rule) {
8500 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008501 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008502 }
8503
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008504 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008505 rule->action = HTTP_REQ_ACT_ALLOW;
8506 cur_arg = 1;
8507 } else if (!strcmp(args[0], "deny")) {
8508 rule->action = HTTP_REQ_ACT_DENY;
8509 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008510 } else if (!strcmp(args[0], "tarpit")) {
8511 rule->action = HTTP_REQ_ACT_TARPIT;
8512 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008513 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008514 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008515 cur_arg = 1;
8516
8517 while(*args[cur_arg]) {
8518 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008519 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008520 cur_arg+=2;
8521 continue;
8522 } else
8523 break;
8524 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008525 } else if (!strcmp(args[0], "set-nice")) {
8526 rule->action = HTTP_REQ_ACT_SET_NICE;
8527 cur_arg = 1;
8528
8529 if (!*args[cur_arg] ||
8530 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8531 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8532 file, linenum, args[0]);
8533 goto out_err;
8534 }
8535 rule->arg.nice = atoi(args[cur_arg]);
8536 if (rule->arg.nice < -1024)
8537 rule->arg.nice = -1024;
8538 else if (rule->arg.nice > 1024)
8539 rule->arg.nice = 1024;
8540 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008541 } else if (!strcmp(args[0], "set-tos")) {
8542#ifdef IP_TOS
8543 char *err;
8544 rule->action = HTTP_REQ_ACT_SET_TOS;
8545 cur_arg = 1;
8546
8547 if (!*args[cur_arg] ||
8548 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8549 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8550 file, linenum, args[0]);
8551 goto out_err;
8552 }
8553
8554 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8555 if (err && *err != '\0') {
8556 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8557 file, linenum, err, args[0]);
8558 goto out_err;
8559 }
8560 cur_arg++;
8561#else
8562 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8563 goto out_err;
8564#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008565 } else if (!strcmp(args[0], "set-mark")) {
8566#ifdef SO_MARK
8567 char *err;
8568 rule->action = HTTP_REQ_ACT_SET_MARK;
8569 cur_arg = 1;
8570
8571 if (!*args[cur_arg] ||
8572 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8573 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8574 file, linenum, args[0]);
8575 goto out_err;
8576 }
8577
8578 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8579 if (err && *err != '\0') {
8580 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8581 file, linenum, err, args[0]);
8582 goto out_err;
8583 }
8584 cur_arg++;
8585 global.last_checks |= LSTCHK_NETADM;
8586#else
8587 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8588 goto out_err;
8589#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008590 } else if (!strcmp(args[0], "set-log-level")) {
8591 rule->action = HTTP_REQ_ACT_SET_LOGL;
8592 cur_arg = 1;
8593
8594 if (!*args[cur_arg] ||
8595 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8596 bad_log_level:
8597 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8598 file, linenum, args[0]);
8599 goto out_err;
8600 }
8601 if (strcmp(args[cur_arg], "silent") == 0)
8602 rule->arg.loglevel = -1;
8603 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8604 goto bad_log_level;
8605 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008606 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8607 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8608 cur_arg = 1;
8609
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008610 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8611 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008612 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8613 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008614 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008615 }
8616
8617 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8618 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8619 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008620
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008621 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008622 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008623 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8624 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008625 free(proxy->conf.lfs_file);
8626 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8627 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008628 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008629 } else if (strcmp(args[0], "del-header") == 0) {
8630 rule->action = HTTP_REQ_ACT_DEL_HDR;
8631 cur_arg = 1;
8632
8633 if (!*args[cur_arg] ||
8634 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8635 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8636 file, linenum, args[0]);
8637 goto out_err;
8638 }
8639
8640 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8641 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8642
8643 proxy->conf.args.ctx = ARGC_HRQ;
8644 free(proxy->conf.lfs_file);
8645 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8646 proxy->conf.lfs_line = proxy->conf.args.line;
8647 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008648 } else if (strcmp(args[0], "redirect") == 0) {
8649 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008650 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008651
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008652 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008653 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8654 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8655 goto out_err;
8656 }
8657
8658 /* this redirect rule might already contain a parsed condition which
8659 * we'll pass to the http-request rule.
8660 */
8661 rule->action = HTTP_REQ_ACT_REDIR;
8662 rule->arg.redir = redir;
8663 rule->cond = redir->cond;
8664 redir->cond = NULL;
8665 cur_arg = 2;
8666 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008667 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008668 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008669 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008670 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008671 }
8672
8673 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8674 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008675 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008676
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008677 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8678 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8679 file, linenum, args[0], errmsg);
8680 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008681 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008682 }
8683 rule->cond = cond;
8684 }
8685 else if (*args[cur_arg]) {
8686 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8687 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8688 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008689 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008690 }
8691
8692 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008693 out_err:
8694 free(rule);
8695 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008696}
8697
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008698/* parse an "http-respose" rule */
8699struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8700{
8701 struct http_res_rule *rule;
8702 int cur_arg;
8703
8704 rule = calloc(1, sizeof(*rule));
8705 if (!rule) {
8706 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8707 goto out_err;
8708 }
8709
8710 if (!strcmp(args[0], "allow")) {
8711 rule->action = HTTP_RES_ACT_ALLOW;
8712 cur_arg = 1;
8713 } else if (!strcmp(args[0], "deny")) {
8714 rule->action = HTTP_RES_ACT_DENY;
8715 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008716 } else if (!strcmp(args[0], "set-nice")) {
8717 rule->action = HTTP_RES_ACT_SET_NICE;
8718 cur_arg = 1;
8719
8720 if (!*args[cur_arg] ||
8721 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8722 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8723 file, linenum, args[0]);
8724 goto out_err;
8725 }
8726 rule->arg.nice = atoi(args[cur_arg]);
8727 if (rule->arg.nice < -1024)
8728 rule->arg.nice = -1024;
8729 else if (rule->arg.nice > 1024)
8730 rule->arg.nice = 1024;
8731 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008732 } else if (!strcmp(args[0], "set-tos")) {
8733#ifdef IP_TOS
8734 char *err;
8735 rule->action = HTTP_RES_ACT_SET_TOS;
8736 cur_arg = 1;
8737
8738 if (!*args[cur_arg] ||
8739 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8740 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8741 file, linenum, args[0]);
8742 goto out_err;
8743 }
8744
8745 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8746 if (err && *err != '\0') {
8747 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8748 file, linenum, err, args[0]);
8749 goto out_err;
8750 }
8751 cur_arg++;
8752#else
8753 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8754 goto out_err;
8755#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008756 } else if (!strcmp(args[0], "set-mark")) {
8757#ifdef SO_MARK
8758 char *err;
8759 rule->action = HTTP_RES_ACT_SET_MARK;
8760 cur_arg = 1;
8761
8762 if (!*args[cur_arg] ||
8763 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8764 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8765 file, linenum, args[0]);
8766 goto out_err;
8767 }
8768
8769 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8770 if (err && *err != '\0') {
8771 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8772 file, linenum, err, args[0]);
8773 goto out_err;
8774 }
8775 cur_arg++;
8776 global.last_checks |= LSTCHK_NETADM;
8777#else
8778 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8779 goto out_err;
8780#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008781 } else if (!strcmp(args[0], "set-log-level")) {
8782 rule->action = HTTP_RES_ACT_SET_LOGL;
8783 cur_arg = 1;
8784
8785 if (!*args[cur_arg] ||
8786 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8787 bad_log_level:
8788 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8789 file, linenum, args[0]);
8790 goto out_err;
8791 }
8792 if (strcmp(args[cur_arg], "silent") == 0)
8793 rule->arg.loglevel = -1;
8794 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8795 goto bad_log_level;
8796 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008797 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8798 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8799 cur_arg = 1;
8800
8801 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8802 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8803 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8804 file, linenum, args[0]);
8805 goto out_err;
8806 }
8807
8808 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8809 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8810 LIST_INIT(&rule->arg.hdr_add.fmt);
8811
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008812 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008813 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008814 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
8815 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008816 free(proxy->conf.lfs_file);
8817 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8818 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008819 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008820 } else if (strcmp(args[0], "del-header") == 0) {
8821 rule->action = HTTP_RES_ACT_DEL_HDR;
8822 cur_arg = 1;
8823
8824 if (!*args[cur_arg] ||
8825 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8826 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8827 file, linenum, args[0]);
8828 goto out_err;
8829 }
8830
8831 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8832 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8833
8834 proxy->conf.args.ctx = ARGC_HRS;
8835 free(proxy->conf.lfs_file);
8836 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8837 proxy->conf.lfs_line = proxy->conf.args.line;
8838 cur_arg += 1;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008839 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008840 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'set-header', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', but got '%s'%s.\n",
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008841 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8842 goto out_err;
8843 }
8844
8845 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8846 struct acl_cond *cond;
8847 char *errmsg = NULL;
8848
8849 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8850 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8851 file, linenum, args[0], errmsg);
8852 free(errmsg);
8853 goto out_err;
8854 }
8855 rule->cond = cond;
8856 }
8857 else if (*args[cur_arg]) {
8858 Alert("parsing [%s:%d]: 'http-response %s' expects"
8859 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8860 file, linenum, args[0], args[cur_arg]);
8861 goto out_err;
8862 }
8863
8864 return rule;
8865 out_err:
8866 free(rule);
8867 return NULL;
8868}
8869
Willy Tarreau4baae242012-12-27 12:00:31 +01008870/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008871 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8872 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008873 */
8874struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008875 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008876{
8877 struct redirect_rule *rule;
8878 int cur_arg;
8879 int type = REDIRECT_TYPE_NONE;
8880 int code = 302;
8881 const char *destination = NULL;
8882 const char *cookie = NULL;
8883 int cookie_set = 0;
8884 unsigned int flags = REDIRECT_FLAG_NONE;
8885 struct acl_cond *cond = NULL;
8886
8887 cur_arg = 0;
8888 while (*(args[cur_arg])) {
8889 if (strcmp(args[cur_arg], "location") == 0) {
8890 if (!*args[cur_arg + 1])
8891 goto missing_arg;
8892
8893 type = REDIRECT_TYPE_LOCATION;
8894 cur_arg++;
8895 destination = args[cur_arg];
8896 }
8897 else if (strcmp(args[cur_arg], "prefix") == 0) {
8898 if (!*args[cur_arg + 1])
8899 goto missing_arg;
8900
8901 type = REDIRECT_TYPE_PREFIX;
8902 cur_arg++;
8903 destination = args[cur_arg];
8904 }
8905 else if (strcmp(args[cur_arg], "scheme") == 0) {
8906 if (!*args[cur_arg + 1])
8907 goto missing_arg;
8908
8909 type = REDIRECT_TYPE_SCHEME;
8910 cur_arg++;
8911 destination = args[cur_arg];
8912 }
8913 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8914 if (!*args[cur_arg + 1])
8915 goto missing_arg;
8916
8917 cur_arg++;
8918 cookie = args[cur_arg];
8919 cookie_set = 1;
8920 }
8921 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8922 if (!*args[cur_arg + 1])
8923 goto missing_arg;
8924
8925 cur_arg++;
8926 cookie = args[cur_arg];
8927 cookie_set = 0;
8928 }
8929 else if (strcmp(args[cur_arg], "code") == 0) {
8930 if (!*args[cur_arg + 1])
8931 goto missing_arg;
8932
8933 cur_arg++;
8934 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008935 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008936 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008937 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008938 args[cur_arg - 1], args[cur_arg]);
8939 return NULL;
8940 }
8941 }
8942 else if (!strcmp(args[cur_arg],"drop-query")) {
8943 flags |= REDIRECT_FLAG_DROP_QS;
8944 }
8945 else if (!strcmp(args[cur_arg],"append-slash")) {
8946 flags |= REDIRECT_FLAG_APPEND_SLASH;
8947 }
8948 else if (strcmp(args[cur_arg], "if") == 0 ||
8949 strcmp(args[cur_arg], "unless") == 0) {
8950 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8951 if (!cond) {
8952 memprintf(errmsg, "error in condition: %s", *errmsg);
8953 return NULL;
8954 }
8955 break;
8956 }
8957 else {
8958 memprintf(errmsg,
8959 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8960 args[cur_arg]);
8961 return NULL;
8962 }
8963 cur_arg++;
8964 }
8965
8966 if (type == REDIRECT_TYPE_NONE) {
8967 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8968 return NULL;
8969 }
8970
8971 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8972 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008973 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008974
8975 if (!use_fmt) {
8976 /* old-style static redirect rule */
8977 rule->rdr_str = strdup(destination);
8978 rule->rdr_len = strlen(destination);
8979 }
8980 else {
8981 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008982
8983 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8984 * if prefix == "/", we don't want to add anything, otherwise it
8985 * makes it hard for the user to configure a self-redirection.
8986 */
8987 proxy->conf.args.ctx = ARGC_RDR;
8988 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008989 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008990 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8991 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008992 free(curproxy->conf.lfs_file);
8993 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8994 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008995 }
8996 }
8997
Willy Tarreau4baae242012-12-27 12:00:31 +01008998 if (cookie) {
8999 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9000 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9001 */
9002 rule->cookie_len = strlen(cookie);
9003 if (cookie_set) {
9004 rule->cookie_str = malloc(rule->cookie_len + 10);
9005 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9006 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9007 rule->cookie_len += 9;
9008 } else {
9009 rule->cookie_str = malloc(rule->cookie_len + 21);
9010 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9011 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9012 rule->cookie_len += 20;
9013 }
9014 }
9015 rule->type = type;
9016 rule->code = code;
9017 rule->flags = flags;
9018 LIST_INIT(&rule->list);
9019 return rule;
9020
9021 missing_arg:
9022 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9023 return NULL;
9024}
9025
Willy Tarreau8797c062007-05-07 00:55:35 +02009026/************************************************************************/
9027/* The code below is dedicated to ACL parsing and matching */
9028/************************************************************************/
9029
9030
Willy Tarreau14174bc2012-04-16 14:34:04 +02009031/* This function ensures that the prerequisites for an L7 fetch are ready,
9032 * which means that a request or response is ready. If some data is missing,
9033 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009034 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9035 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009036 *
9037 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009038 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9039 * decide whether or not an HTTP message is present ;
9040 * 0 if the requested data cannot be fetched or if it is certain that
9041 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009042 * 1 if an HTTP message is ready
9043 */
9044static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009045smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009046 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009047{
9048 struct http_txn *txn = l7;
9049 struct http_msg *msg = &txn->req;
9050
9051 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9052 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9053 */
9054
9055 if (unlikely(!s || !txn))
9056 return 0;
9057
9058 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009059 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009060
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009061 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009062 if (unlikely(!s->req))
9063 return 0;
9064
Willy Tarreauaae75e32013-03-29 12:31:49 +01009065 /* If the buffer does not leave enough free space at the end,
9066 * we must first realign it.
9067 */
9068 if (s->req->buf->p > s->req->buf->data &&
9069 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9070 buffer_slow_realign(s->req->buf);
9071
Willy Tarreau14174bc2012-04-16 14:34:04 +02009072 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009073 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009074 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009075
9076 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009077 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009078 http_msg_analyzer(msg, &txn->hdr_idx);
9079
9080 /* Still no valid request ? */
9081 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009082 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009083 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009084 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009085 }
9086 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009087 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009088 return 0;
9089 }
9090
9091 /* OK we just got a valid HTTP request. We have some minor
9092 * preparation to perform so that further checks can rely
9093 * on HTTP tests.
9094 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009095
9096 /* If the request was parsed but was too large, we must absolutely
9097 * return an error so that it is not processed. At the moment this
9098 * cannot happen, but if the parsers are to change in the future,
9099 * we want this check to be maintained.
9100 */
9101 if (unlikely(s->req->buf->i + s->req->buf->p >
9102 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9103 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009104 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009105 return 1;
9106 }
9107
Willy Tarreau9b28e032012-10-12 23:49:43 +02009108 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009109 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9110 s->flags |= SN_REDIRECTABLE;
9111
Willy Tarreau506d0502013-07-06 13:29:24 +02009112 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9113 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009114 }
9115
Willy Tarreau506d0502013-07-06 13:29:24 +02009116 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009117 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009118 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009119
9120 /* otherwise everything's ready for the request */
9121 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009122 else {
9123 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009124 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9125 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009126 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009127 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009128 }
9129
9130 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009131 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009132 return 1;
9133}
Willy Tarreau8797c062007-05-07 00:55:35 +02009134
Willy Tarreauc0239e02012-04-16 14:42:55 +02009135#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009136 do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +02009137
Willy Tarreau24e32d82012-04-23 23:55:44 +02009138#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009139 do { int r = smp_prefetch_http(px, l4, l7, opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +02009140
Willy Tarreau8797c062007-05-07 00:55:35 +02009141
9142/* 1. Check on METHOD
9143 * We use the pre-parsed method if it is known, and store its number as an
9144 * integer. If it is unknown, we use the pointer and the length.
9145 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009146static int pat_parse_meth(const char *text, struct pattern *pattern, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009147{
9148 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009149 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02009150
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009151 len = strlen(text);
9152 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009153
9154 pattern->val.i = meth;
9155 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009156 trash = get_trash_chunk();
9157 if (trash->size < len) {
9158 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
9159 len, trash->size);
9160 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009161 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009162 pattern->ptr.str = trash->str;
Willy Tarreau8797c062007-05-07 00:55:35 +02009163 pattern->len = len;
9164 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009165 else {
9166 pattern->ptr.str = NULL;
9167 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009168 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009169 return 1;
9170}
9171
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009172/* This function fetches the method of current HTTP request and stores
9173 * it in the global pattern struct as a chunk. There are two possibilities :
9174 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9175 * in <len> and <ptr> is NULL ;
9176 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9177 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009178 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009179 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009180static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009181smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009182 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009183{
9184 int meth;
9185 struct http_txn *txn = l7;
9186
Willy Tarreau24e32d82012-04-23 23:55:44 +02009187 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009188
Willy Tarreau8797c062007-05-07 00:55:35 +02009189 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009190 smp->type = SMP_T_METH;
9191 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009192 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009193 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9194 /* ensure the indexes are not affected */
9195 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009196 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009197 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9198 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009199 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009200 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009201 return 1;
9202}
9203
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009204/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009205static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009206{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009207 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009208 struct pattern_list *lst;
9209 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009210
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009211 list_for_each_entry(lst, &expr->patterns, list) {
9212 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009213
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009214 /* well-known method */
9215 if (pattern->val.i != HTTP_METH_OTHER) {
9216 if (smp->data.meth.meth == pattern->val.i)
9217 return pattern;
9218 else
9219 continue;
9220 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009221
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009222 /* Other method, we must compare the strings */
9223 if (pattern->len != smp->data.meth.str.len)
9224 continue;
9225
9226 icase = pattern->flags & PAT_F_IGNORE_CASE;
9227 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) ||
9228 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0))
9229 return pattern;
9230 }
9231 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009232}
9233
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009234static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009235smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009236 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009237{
9238 struct http_txn *txn = l7;
9239 char *ptr;
9240 int len;
9241
Willy Tarreauc0239e02012-04-16 14:42:55 +02009242 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009243
Willy Tarreau8797c062007-05-07 00:55:35 +02009244 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009245 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009246
9247 while ((len-- > 0) && (*ptr++ != '/'));
9248 if (len <= 0)
9249 return 0;
9250
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009251 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009252 smp->data.str.str = ptr;
9253 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009254
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009255 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009256 return 1;
9257}
9258
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009259static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009260smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009261 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009262{
9263 struct http_txn *txn = l7;
9264 char *ptr;
9265 int len;
9266
Willy Tarreauc0239e02012-04-16 14:42:55 +02009267 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009268
Willy Tarreauf26b2522012-12-14 08:33:14 +01009269 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9270 return 0;
9271
Willy Tarreau8797c062007-05-07 00:55:35 +02009272 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009273 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009274
9275 while ((len-- > 0) && (*ptr++ != '/'));
9276 if (len <= 0)
9277 return 0;
9278
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009279 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009280 smp->data.str.str = ptr;
9281 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009282
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009283 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009284 return 1;
9285}
9286
9287/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009288static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009289smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009290 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009291{
9292 struct http_txn *txn = l7;
9293 char *ptr;
9294 int len;
9295
Willy Tarreauc0239e02012-04-16 14:42:55 +02009296 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009297
Willy Tarreauf26b2522012-12-14 08:33:14 +01009298 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9299 return 0;
9300
Willy Tarreau8797c062007-05-07 00:55:35 +02009301 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009302 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009303
Willy Tarreauf853c462012-04-23 18:53:56 +02009304 smp->type = SMP_T_UINT;
9305 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009306 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009307 return 1;
9308}
9309
9310/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009311static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009312smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009313 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009314{
9315 struct http_txn *txn = l7;
9316
Willy Tarreauc0239e02012-04-16 14:42:55 +02009317 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009318
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009319 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009320 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009321 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009322 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009323 return 1;
9324}
9325
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009326static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009327smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009328 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009329{
9330 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009331 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009332
Willy Tarreauc0239e02012-04-16 14:42:55 +02009333 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009334
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009335 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009336 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009337 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009338
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009339 smp->type = SMP_T_IPV4;
9340 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009341 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009342 return 1;
9343}
9344
9345static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009346smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009347 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009348{
9349 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009350 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009351
Willy Tarreauc0239e02012-04-16 14:42:55 +02009352 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009353
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009354 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009355 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9356 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009357
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009358 smp->type = SMP_T_UINT;
9359 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009360 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009361 return 1;
9362}
9363
Willy Tarreau185b5c42012-04-26 15:11:51 +02009364/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9365 * Accepts an optional argument of type string containing the header field name,
9366 * and an optional argument of type signed or unsigned integer to request an
9367 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009368 * headers are considered from the first one. It does not stop on commas and
9369 * returns full lines instead (useful for User-Agent or Date for example).
9370 */
9371static int
9372smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009373 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009374{
9375 struct http_txn *txn = l7;
9376 struct hdr_idx *idx = &txn->hdr_idx;
9377 struct hdr_ctx *ctx = smp->ctx.a[0];
9378 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9379 int occ = 0;
9380 const char *name_str = NULL;
9381 int name_len = 0;
9382
9383 if (!ctx) {
9384 /* first call */
9385 ctx = &static_hdr_ctx;
9386 ctx->idx = 0;
9387 smp->ctx.a[0] = ctx;
9388 }
9389
9390 if (args) {
9391 if (args[0].type != ARGT_STR)
9392 return 0;
9393 name_str = args[0].data.str.str;
9394 name_len = args[0].data.str.len;
9395
9396 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9397 occ = args[1].data.uint;
9398 }
9399
9400 CHECK_HTTP_MESSAGE_FIRST();
9401
9402 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9403 /* search for header from the beginning */
9404 ctx->idx = 0;
9405
9406 if (!occ && !(opt & SMP_OPT_ITERATE))
9407 /* no explicit occurrence and single fetch => last header by default */
9408 occ = -1;
9409
9410 if (!occ)
9411 /* prepare to report multiple occurrences for ACL fetches */
9412 smp->flags |= SMP_F_NOT_LAST;
9413
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009414 smp->type = SMP_T_STR;
9415 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009416 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9417 return 1;
9418
9419 smp->flags &= ~SMP_F_NOT_LAST;
9420 return 0;
9421}
9422
9423/* 6. Check on HTTP header count. The number of occurrences is returned.
9424 * Accepts exactly 1 argument of type string. It does not stop on commas and
9425 * returns full lines instead (useful for User-Agent or Date for example).
9426 */
9427static int
9428smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009429 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009430{
9431 struct http_txn *txn = l7;
9432 struct hdr_idx *idx = &txn->hdr_idx;
9433 struct hdr_ctx ctx;
9434 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9435 int cnt;
9436
9437 if (!args || args->type != ARGT_STR)
9438 return 0;
9439
9440 CHECK_HTTP_MESSAGE_FIRST();
9441
9442 ctx.idx = 0;
9443 cnt = 0;
9444 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9445 cnt++;
9446
9447 smp->type = SMP_T_UINT;
9448 smp->data.uint = cnt;
9449 smp->flags = SMP_F_VOL_HDR;
9450 return 1;
9451}
9452
9453/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9454 * Accepts an optional argument of type string containing the header field name,
9455 * and an optional argument of type signed or unsigned integer to request an
9456 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009457 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009458 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009459static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009460smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009461 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009462{
9463 struct http_txn *txn = l7;
9464 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009465 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009466 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009467 int occ = 0;
9468 const char *name_str = NULL;
9469 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009470
Willy Tarreaua890d072013-04-02 12:01:06 +02009471 if (!ctx) {
9472 /* first call */
9473 ctx = &static_hdr_ctx;
9474 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009475 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009476 }
9477
Willy Tarreau185b5c42012-04-26 15:11:51 +02009478 if (args) {
9479 if (args[0].type != ARGT_STR)
9480 return 0;
9481 name_str = args[0].data.str.str;
9482 name_len = args[0].data.str.len;
9483
9484 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9485 occ = args[1].data.uint;
9486 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009487
Willy Tarreaue333ec92012-04-16 16:26:40 +02009488 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009489
Willy Tarreau185b5c42012-04-26 15:11:51 +02009490 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009491 /* search for header from the beginning */
9492 ctx->idx = 0;
9493
Willy Tarreau185b5c42012-04-26 15:11:51 +02009494 if (!occ && !(opt & SMP_OPT_ITERATE))
9495 /* no explicit occurrence and single fetch => last header by default */
9496 occ = -1;
9497
9498 if (!occ)
9499 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009500 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009501
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009502 smp->type = SMP_T_STR;
9503 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009504 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009505 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009506
Willy Tarreau37406352012-04-23 16:16:37 +02009507 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009508 return 0;
9509}
9510
Willy Tarreauc11416f2007-06-17 16:58:38 +02009511/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009512 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009513 */
9514static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009515smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009516 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009517{
9518 struct http_txn *txn = l7;
9519 struct hdr_idx *idx = &txn->hdr_idx;
9520 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009521 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009522 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009523
Willy Tarreau24e32d82012-04-23 23:55:44 +02009524 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009525 return 0;
9526
Willy Tarreaue333ec92012-04-16 16:26:40 +02009527 CHECK_HTTP_MESSAGE_FIRST();
9528
Willy Tarreau33a7e692007-06-10 19:45:56 +02009529 ctx.idx = 0;
9530 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009531 while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009532 cnt++;
9533
Willy Tarreauf853c462012-04-23 18:53:56 +02009534 smp->type = SMP_T_UINT;
9535 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009536 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009537 return 1;
9538}
9539
Willy Tarreau185b5c42012-04-26 15:11:51 +02009540/* Fetch an HTTP header's integer value. The integer value is returned. It
9541 * takes a mandatory argument of type string and an optional one of type int
9542 * to designate a specific occurrence. It returns an unsigned integer, which
9543 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009544 */
9545static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009546smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009547 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009548{
Willy Tarreauef38c392013-07-22 16:29:32 +02009549 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009550
Willy Tarreauf853c462012-04-23 18:53:56 +02009551 if (ret > 0) {
9552 smp->type = SMP_T_UINT;
9553 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9554 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009555
Willy Tarreaud53e2422012-04-16 17:21:11 +02009556 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009557}
9558
Cyril Bonté69fa9922012-10-25 00:01:06 +02009559/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9560 * and an optional one of type int to designate a specific occurrence.
9561 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009562 */
9563static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009564smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009565 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009566{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009567 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009568
Willy Tarreauef38c392013-07-22 16:29:32 +02009569 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009570 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9571 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009572 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009573 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009574 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009575 if (smp->data.str.len < temp->size - 1) {
9576 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9577 temp->str[smp->data.str.len] = '\0';
9578 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9579 smp->type = SMP_T_IPV6;
9580 break;
9581 }
9582 }
9583 }
9584
Willy Tarreaud53e2422012-04-16 17:21:11 +02009585 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009586 if (!(smp->flags & SMP_F_NOT_LAST))
9587 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009588 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009589 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009590}
9591
Willy Tarreau737b0c12007-06-10 21:28:46 +02009592/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9593 * the first '/' after the possible hostname, and ends before the possible '?'.
9594 */
9595static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009596smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009597 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009598{
9599 struct http_txn *txn = l7;
9600 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009601
Willy Tarreauc0239e02012-04-16 14:42:55 +02009602 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009603
Willy Tarreau9b28e032012-10-12 23:49:43 +02009604 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009605 ptr = http_get_path(txn);
9606 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009607 return 0;
9608
9609 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009610 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009611 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009612
9613 while (ptr < end && *ptr != '?')
9614 ptr++;
9615
Willy Tarreauf853c462012-04-23 18:53:56 +02009616 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009617 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009618 return 1;
9619}
9620
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009621/* This produces a concatenation of the first occurrence of the Host header
9622 * followed by the path component if it begins with a slash ('/'). This means
9623 * that '*' will not be added, resulting in exactly the first Host entry.
9624 * If no Host header is found, then the path is returned as-is. The returned
9625 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009626 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009627 */
9628static int
9629smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009630 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009631{
9632 struct http_txn *txn = l7;
9633 char *ptr, *end, *beg;
9634 struct hdr_ctx ctx;
9635
9636 CHECK_HTTP_MESSAGE_FIRST();
9637
9638 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02009639 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009640 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009641
9642 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009643 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009644 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009645 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009646 smp->data.str.len = ctx.vlen;
9647
9648 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +02009649 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009650 beg = http_get_path(txn);
9651 if (!beg)
9652 beg = end;
9653
9654 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9655
9656 if (beg < ptr && *beg == '/') {
9657 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9658 smp->data.str.len += ptr - beg;
9659 }
9660
9661 smp->flags = SMP_F_VOL_1ST;
9662 return 1;
9663}
9664
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009665/* This produces a 32-bit hash of the concatenation of the first occurrence of
9666 * the Host header followed by the path component if it begins with a slash ('/').
9667 * This means that '*' will not be added, resulting in exactly the first Host
9668 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009669 * is hashed using the path hash followed by a full avalanche hash and provides a
9670 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009671 * high-traffic sites without having to store whole paths.
9672 */
9673static int
9674smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009675 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009676{
9677 struct http_txn *txn = l7;
9678 struct hdr_ctx ctx;
9679 unsigned int hash = 0;
9680 char *ptr, *beg, *end;
9681 int len;
9682
9683 CHECK_HTTP_MESSAGE_FIRST();
9684
9685 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02009686 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009687 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9688 ptr = ctx.line + ctx.val;
9689 len = ctx.vlen;
9690 while (len--)
9691 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9692 }
9693
9694 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +02009695 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009696 beg = http_get_path(txn);
9697 if (!beg)
9698 beg = end;
9699
9700 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9701
9702 if (beg < ptr && *beg == '/') {
9703 while (beg < ptr)
9704 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9705 }
9706 hash = full_hash(hash);
9707
9708 smp->type = SMP_T_UINT;
9709 smp->data.uint = hash;
9710 smp->flags = SMP_F_VOL_1ST;
9711 return 1;
9712}
9713
Willy Tarreau4a550602012-12-09 14:53:32 +01009714/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009715 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9716 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9717 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009718 * that in environments where IPv6 is insignificant, truncating the output to
9719 * 8 bytes would still work.
9720 */
9721static int
9722smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009723 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009724{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009725 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009726 struct connection *cli_conn = objt_conn(l4->si[0].end);
9727
9728 if (!cli_conn)
9729 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009730
Willy Tarreauef38c392013-07-22 16:29:32 +02009731 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009732 return 0;
9733
Willy Tarreau47ca5452012-12-23 20:22:19 +01009734 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009735 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9736 temp->len += sizeof(smp->data.uint);
9737
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009738 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009739 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009740 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009741 temp->len += 4;
9742 break;
9743 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009744 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009745 temp->len += 16;
9746 break;
9747 default:
9748 return 0;
9749 }
9750
9751 smp->data.str = *temp;
9752 smp->type = SMP_T_BIN;
9753 return 1;
9754}
9755
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009756static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009757smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009758 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009759{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009760 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9761 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9762 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009763
Willy Tarreau24e32d82012-04-23 23:55:44 +02009764 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009765
Willy Tarreauf853c462012-04-23 18:53:56 +02009766 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009767 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009768 return 1;
9769}
9770
Willy Tarreau7f18e522010-10-22 20:04:13 +02009771/* return a valid test if the current request is the first one on the connection */
9772static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009773smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009774 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009775{
9776 if (!s)
9777 return 0;
9778
Willy Tarreauf853c462012-04-23 18:53:56 +02009779 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009780 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009781 return 1;
9782}
9783
Willy Tarreau34db1082012-04-19 17:16:54 +02009784/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009785static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009786smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009787 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009788{
9789
Willy Tarreau24e32d82012-04-23 23:55:44 +02009790 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009791 return 0;
9792
Willy Tarreauc0239e02012-04-16 14:42:55 +02009793 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009794
Willy Tarreauc0239e02012-04-16 14:42:55 +02009795 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009796 return 0;
9797
Willy Tarreauf853c462012-04-23 18:53:56 +02009798 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009799 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009800 return 1;
9801}
Willy Tarreau8797c062007-05-07 00:55:35 +02009802
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009803/* Accepts exactly 1 argument of type userlist */
9804static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009805smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009806 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009807{
9808
9809 if (!args || args->type != ARGT_USR)
9810 return 0;
9811
9812 CHECK_HTTP_MESSAGE_FIRST();
9813
9814 if (!get_http_auth(l4))
9815 return 0;
9816
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009817 /* if the user does not belong to the userlist or has a wrong password,
9818 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009819 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009820 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009821 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
9822 return 0;
9823
9824 /* pat_match_auth() will need the user list */
9825 smp->ctx.a[0] = args->data.usr;
9826
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009827 smp->type = SMP_T_STR;
9828 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009829 smp->data.str.str = l4->txn.auth.user;
9830 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009831
9832 return 1;
9833}
9834
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009835/* Try to find the next occurrence of a cookie name in a cookie header value.
9836 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9837 * the cookie value is returned into *value and *value_l, and the function
9838 * returns a pointer to the next pointer to search from if the value was found.
9839 * Otherwise if the cookie was not found, NULL is returned and neither value
9840 * nor value_l are touched. The input <hdr> string should first point to the
9841 * header's value, and the <hdr_end> pointer must point to the first character
9842 * not part of the value. <list> must be non-zero if value may represent a list
9843 * of values (cookie headers). This makes it faster to abort parsing when no
9844 * list is expected.
9845 */
9846static char *
9847extract_cookie_value(char *hdr, const char *hdr_end,
9848 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009849 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009850{
9851 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9852 char *next;
9853
9854 /* we search at least a cookie name followed by an equal, and more
9855 * generally something like this :
9856 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9857 */
9858 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9859 /* Iterate through all cookies on this line */
9860
9861 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9862 att_beg++;
9863
9864 /* find att_end : this is the first character after the last non
9865 * space before the equal. It may be equal to hdr_end.
9866 */
9867 equal = att_end = att_beg;
9868
9869 while (equal < hdr_end) {
9870 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9871 break;
9872 if (http_is_spht[(unsigned char)*equal++])
9873 continue;
9874 att_end = equal;
9875 }
9876
9877 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9878 * is between <att_beg> and <equal>, both may be identical.
9879 */
9880
9881 /* look for end of cookie if there is an equal sign */
9882 if (equal < hdr_end && *equal == '=') {
9883 /* look for the beginning of the value */
9884 val_beg = equal + 1;
9885 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9886 val_beg++;
9887
9888 /* find the end of the value, respecting quotes */
9889 next = find_cookie_value_end(val_beg, hdr_end);
9890
9891 /* make val_end point to the first white space or delimitor after the value */
9892 val_end = next;
9893 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9894 val_end--;
9895 } else {
9896 val_beg = val_end = next = equal;
9897 }
9898
9899 /* We have nothing to do with attributes beginning with '$'. However,
9900 * they will automatically be removed if a header before them is removed,
9901 * since they're supposed to be linked together.
9902 */
9903 if (*att_beg == '$')
9904 continue;
9905
9906 /* Ignore cookies with no equal sign */
9907 if (equal == next)
9908 continue;
9909
9910 /* Now we have the cookie name between att_beg and att_end, and
9911 * its value between val_beg and val_end.
9912 */
9913
9914 if (att_end - att_beg == cookie_name_l &&
9915 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9916 /* let's return this value and indicate where to go on from */
9917 *value = val_beg;
9918 *value_l = val_end - val_beg;
9919 return next + 1;
9920 }
9921
9922 /* Set-Cookie headers only have the name in the first attr=value part */
9923 if (!list)
9924 break;
9925 }
9926
9927 return NULL;
9928}
9929
William Lallemanda43ba4e2014-01-28 18:14:25 +01009930/* Fetch a captured HTTP request header. The index is the position of
9931 * the "capture" option in the configuration file
9932 */
9933static int
9934smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9935 const struct arg *args, struct sample *smp, const char *kw)
9936{
9937 struct proxy *fe = l4->fe;
9938 struct http_txn *txn = l7;
9939 int idx;
9940
9941 if (!args || args->type != ARGT_UINT)
9942 return 0;
9943
9944 idx = args->data.uint;
9945
9946 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
9947 return 0;
9948
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009949 smp->type = SMP_T_STR;
9950 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009951 smp->data.str.str = txn->req.cap[idx];
9952 smp->data.str.len = strlen(txn->req.cap[idx]);
9953
9954 return 1;
9955}
9956
9957/* Fetch a captured HTTP response header. The index is the position of
9958 * the "capture" option in the configuration file
9959 */
9960static int
9961smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9962 const struct arg *args, struct sample *smp, const char *kw)
9963{
9964 struct proxy *fe = l4->fe;
9965 struct http_txn *txn = l7;
9966 int idx;
9967
9968 if (!args || args->type != ARGT_UINT)
9969 return 0;
9970
9971 idx = args->data.uint;
9972
9973 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
9974 return 0;
9975
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009976 smp->type = SMP_T_STR;
9977 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009978 smp->data.str.str = txn->rsp.cap[idx];
9979 smp->data.str.len = strlen(txn->rsp.cap[idx]);
9980
9981 return 1;
9982}
9983
William Lallemand65ad6e12014-01-31 15:08:02 +01009984/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
9985static int
9986smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9987 const struct arg *args, struct sample *smp, const char *kw)
9988{
9989 struct chunk *temp;
9990 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +01009991 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009992
9993 if (!txn->uri)
9994 return 0;
9995
William Lallemand96a77852014-02-05 00:30:02 +01009996 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009997
William Lallemand96a77852014-02-05 00:30:02 +01009998 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9999 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010000
William Lallemand96a77852014-02-05 00:30:02 +010010001 temp = get_trash_chunk();
10002 temp->str = txn->uri;
10003 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010004 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010005 smp->type = SMP_T_STR;
10006 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010007
10008 return 1;
10009
10010}
10011
10012/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10013static int
10014smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10015 const struct arg *args, struct sample *smp, const char *kw)
10016{
10017 struct chunk *temp;
10018 struct http_txn *txn = l7;
10019 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010020
10021 if (!txn->uri)
10022 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010023
William Lallemand65ad6e12014-01-31 15:08:02 +010010024 ptr = txn->uri;
10025
10026 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10027 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010028
William Lallemand65ad6e12014-01-31 15:08:02 +010010029 if (!*ptr)
10030 return 0;
10031
10032 ptr++; /* skip the space */
10033
10034 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010035 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010036 if (!ptr)
10037 return 0;
10038 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10039 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010040
10041 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010042 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010043 smp->type = SMP_T_STR;
10044 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010045
10046 return 1;
10047}
10048
10049
Willy Tarreaue333ec92012-04-16 16:26:40 +020010050/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010051 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010052 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010053 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010054 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010055 * Accepts exactly 1 argument of type string. If the input options indicate
10056 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010057 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010058 */
10059static int
Willy Tarreau51539362012-05-08 12:46:28 +020010060smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010061 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010062{
10063 struct http_txn *txn = l7;
10064 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010065 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010066 const struct http_msg *msg;
10067 const char *hdr_name;
10068 int hdr_name_len;
10069 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010070 int occ = 0;
10071 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010072
Willy Tarreau24e32d82012-04-23 23:55:44 +020010073 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010074 return 0;
10075
Willy Tarreaua890d072013-04-02 12:01:06 +020010076 if (!ctx) {
10077 /* first call */
10078 ctx = &static_hdr_ctx;
10079 ctx->idx = 0;
10080 smp->ctx.a[2] = ctx;
10081 }
10082
Willy Tarreaue333ec92012-04-16 16:26:40 +020010083 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010084
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010085 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010086 msg = &txn->req;
10087 hdr_name = "Cookie";
10088 hdr_name_len = 6;
10089 } else {
10090 msg = &txn->rsp;
10091 hdr_name = "Set-Cookie";
10092 hdr_name_len = 10;
10093 }
10094
Willy Tarreau28376d62012-04-26 21:26:10 +020010095 if (!occ && !(opt & SMP_OPT_ITERATE))
10096 /* no explicit occurrence and single fetch => last cookie by default */
10097 occ = -1;
10098
10099 /* OK so basically here, either we want only one value and it's the
10100 * last one, or we want to iterate over all of them and we fetch the
10101 * next one.
10102 */
10103
Willy Tarreau9b28e032012-10-12 23:49:43 +020010104 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010105 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010106 /* search for the header from the beginning, we must first initialize
10107 * the search parameters.
10108 */
Willy Tarreau37406352012-04-23 16:16:37 +020010109 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010110 ctx->idx = 0;
10111 }
10112
Willy Tarreau28376d62012-04-26 21:26:10 +020010113 smp->flags |= SMP_F_VOL_HDR;
10114
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010115 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010116 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10117 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010118 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10119 goto out;
10120
Willy Tarreau24e32d82012-04-23 23:55:44 +020010121 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010122 continue;
10123
Willy Tarreau37406352012-04-23 16:16:37 +020010124 smp->ctx.a[0] = ctx->line + ctx->val;
10125 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010126 }
10127
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010128 smp->type = SMP_T_STR;
10129 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010130 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010131 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010132 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010133 &smp->data.str.str,
10134 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010135 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010136 found = 1;
10137 if (occ >= 0) {
10138 /* one value was returned into smp->data.str.{str,len} */
10139 smp->flags |= SMP_F_NOT_LAST;
10140 return 1;
10141 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010142 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010143 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010144 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010145 /* all cookie headers and values were scanned. If we're looking for the
10146 * last occurrence, we may return it now.
10147 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010148 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010149 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010150 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010151}
10152
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010153/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010154 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010155 * multiple cookies may be parsed on the same line. The returned sample is of
10156 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010157 */
10158static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010159smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010160 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010161{
10162 struct http_txn *txn = l7;
10163 struct hdr_idx *idx = &txn->hdr_idx;
10164 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010165 const struct http_msg *msg;
10166 const char *hdr_name;
10167 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010168 int cnt;
10169 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010170 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010171
Willy Tarreau24e32d82012-04-23 23:55:44 +020010172 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010173 return 0;
10174
Willy Tarreaue333ec92012-04-16 16:26:40 +020010175 CHECK_HTTP_MESSAGE_FIRST();
10176
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010177 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010178 msg = &txn->req;
10179 hdr_name = "Cookie";
10180 hdr_name_len = 6;
10181 } else {
10182 msg = &txn->rsp;
10183 hdr_name = "Set-Cookie";
10184 hdr_name_len = 10;
10185 }
10186
Willy Tarreau9b28e032012-10-12 23:49:43 +020010187 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010188 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010189 ctx.idx = 0;
10190 cnt = 0;
10191
10192 while (1) {
10193 /* Note: val_beg == NULL every time we need to fetch a new header */
10194 if (!val_beg) {
10195 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10196 break;
10197
Willy Tarreau24e32d82012-04-23 23:55:44 +020010198 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010199 continue;
10200
10201 val_beg = ctx.line + ctx.val;
10202 val_end = val_beg + ctx.vlen;
10203 }
10204
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010205 smp->type = SMP_T_STR;
10206 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010207 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010208 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010209 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010210 &smp->data.str.str,
10211 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010212 cnt++;
10213 }
10214 }
10215
Willy Tarreaub169eba2013-12-16 15:14:43 +010010216 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010217 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010218 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010219 return 1;
10220}
10221
Willy Tarreau51539362012-05-08 12:46:28 +020010222/* Fetch an cookie's integer value. The integer value is returned. It
10223 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10224 */
10225static int
10226smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010227 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010228{
Willy Tarreauef38c392013-07-22 16:29:32 +020010229 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010230
10231 if (ret > 0) {
10232 smp->type = SMP_T_UINT;
10233 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10234 }
10235
10236 return ret;
10237}
10238
Willy Tarreau8797c062007-05-07 00:55:35 +020010239/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010240/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010241/************************************************************************/
10242
David Cournapeau16023ee2010-12-23 20:55:41 +090010243/*
10244 * Given a path string and its length, find the position of beginning of the
10245 * query string. Returns NULL if no query string is found in the path.
10246 *
10247 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10248 *
10249 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10250 */
bedis4c75cca2012-10-05 08:38:24 +020010251static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010252{
10253 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010254
bedis4c75cca2012-10-05 08:38:24 +020010255 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010256 return p ? p + 1 : NULL;
10257}
10258
bedis4c75cca2012-10-05 08:38:24 +020010259static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010260{
bedis4c75cca2012-10-05 08:38:24 +020010261 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010262}
10263
10264/*
10265 * Given a url parameter, find the starting position of the first occurence,
10266 * or NULL if the parameter is not found.
10267 *
10268 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10269 * the function will return query_string+8.
10270 */
10271static char*
10272find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010273 char* url_param_name, size_t url_param_name_l,
10274 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010275{
10276 char *pos, *last;
10277
10278 pos = query_string;
10279 last = query_string + query_string_l - url_param_name_l - 1;
10280
10281 while (pos <= last) {
10282 if (pos[url_param_name_l] == '=') {
10283 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10284 return pos;
10285 pos += url_param_name_l + 1;
10286 }
bedis4c75cca2012-10-05 08:38:24 +020010287 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010288 pos++;
10289 pos++;
10290 }
10291 return NULL;
10292}
10293
10294/*
10295 * Given a url parameter name, returns its value and size into *value and
10296 * *value_l respectively, and returns non-zero. If the parameter is not found,
10297 * zero is returned and value/value_l are not touched.
10298 */
10299static int
10300find_url_param_value(char* path, size_t path_l,
10301 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010302 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010303{
10304 char *query_string, *qs_end;
10305 char *arg_start;
10306 char *value_start, *value_end;
10307
bedis4c75cca2012-10-05 08:38:24 +020010308 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010309 if (!query_string)
10310 return 0;
10311
10312 qs_end = path + path_l;
10313 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010314 url_param_name, url_param_name_l,
10315 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010316 if (!arg_start)
10317 return 0;
10318
10319 value_start = arg_start + url_param_name_l + 1;
10320 value_end = value_start;
10321
bedis4c75cca2012-10-05 08:38:24 +020010322 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010323 value_end++;
10324
10325 *value = value_start;
10326 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010327 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010328}
10329
10330static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010331smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010332 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010333{
bedis4c75cca2012-10-05 08:38:24 +020010334 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010335 struct http_txn *txn = l7;
10336 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010337
bedis4c75cca2012-10-05 08:38:24 +020010338 if (!args || args[0].type != ARGT_STR ||
10339 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010340 return 0;
10341
10342 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010343
bedis4c75cca2012-10-05 08:38:24 +020010344 if (args[1].type)
10345 delim = *args[1].data.str.str;
10346
Willy Tarreau9b28e032012-10-12 23:49:43 +020010347 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010348 args->data.str.str, args->data.str.len,
10349 &smp->data.str.str, &smp->data.str.len,
10350 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010351 return 0;
10352
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010353 smp->type = SMP_T_STR;
10354 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010355 return 1;
10356}
10357
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010358/* Return the signed integer value for the specified url parameter (see url_param
10359 * above).
10360 */
10361static int
10362smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010363 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010364{
Willy Tarreauef38c392013-07-22 16:29:32 +020010365 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010366
10367 if (ret > 0) {
10368 smp->type = SMP_T_UINT;
10369 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10370 }
10371
10372 return ret;
10373}
10374
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010375/* This produces a 32-bit hash of the concatenation of the first occurrence of
10376 * the Host header followed by the path component if it begins with a slash ('/').
10377 * This means that '*' will not be added, resulting in exactly the first Host
10378 * entry. If no Host header is found, then the path is used. The resulting value
10379 * is hashed using the url hash followed by a full avalanche hash and provides a
10380 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10381 * high-traffic sites without having to store whole paths.
10382 * this differs from the base32 functions in that it includes the url parameters
10383 * as well as the path
10384 */
10385static int
10386smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010387 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010388{
10389 struct http_txn *txn = l7;
10390 struct hdr_ctx ctx;
10391 unsigned int hash = 0;
10392 char *ptr, *beg, *end;
10393 int len;
10394
10395 CHECK_HTTP_MESSAGE_FIRST();
10396
10397 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010398 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010399 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10400 ptr = ctx.line + ctx.val;
10401 len = ctx.vlen;
10402 while (len--)
10403 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10404 }
10405
10406 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010407 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010408 beg = http_get_path(txn);
10409 if (!beg)
10410 beg = end;
10411
10412 for (ptr = beg; ptr < end ; ptr++);
10413
10414 if (beg < ptr && *beg == '/') {
10415 while (beg < ptr)
10416 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10417 }
10418 hash = full_hash(hash);
10419
10420 smp->type = SMP_T_UINT;
10421 smp->data.uint = hash;
10422 smp->flags = SMP_F_VOL_1ST;
10423 return 1;
10424}
10425
10426/* This concatenates the source address with the 32-bit hash of the Host and
10427 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10428 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10429 * on the source address length. The URL hash is stored before the address so
10430 * that in environments where IPv6 is insignificant, truncating the output to
10431 * 8 bytes would still work.
10432 */
10433static int
10434smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010435 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010436{
10437 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010438 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010439
Willy Tarreaue155ec22013-11-18 18:33:22 +010010440 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010441 return 0;
10442
10443 temp = get_trash_chunk();
10444 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10445 temp->len += sizeof(smp->data.uint);
10446
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010447 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010448 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010449 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010450 temp->len += 4;
10451 break;
10452 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010453 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010454 temp->len += 16;
10455 break;
10456 default:
10457 return 0;
10458 }
10459
10460 smp->data.str = *temp;
10461 smp->type = SMP_T_BIN;
10462 return 1;
10463}
10464
Willy Tarreau185b5c42012-04-26 15:11:51 +020010465/* This function is used to validate the arguments passed to any "hdr" fetch
10466 * keyword. These keywords support an optional positive or negative occurrence
10467 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10468 * is assumed that the types are already the correct ones. Returns 0 on error,
10469 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10470 * error message in case of error, that the caller is responsible for freeing.
10471 * The initial location must either be freeable or NULL.
10472 */
10473static int val_hdr(struct arg *arg, char **err_msg)
10474{
10475 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010476 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010477 return 0;
10478 }
10479 return 1;
10480}
10481
Willy Tarreau276fae92013-07-25 14:36:01 +020010482/* takes an UINT value on input supposed to represent the time since EPOCH,
10483 * adds an optional offset found in args[0] and emits a string representing
10484 * the date in RFC-1123/5322 format.
10485 */
10486static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10487{
10488 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10489 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10490 struct chunk *temp;
10491 struct tm *tm;
10492 time_t curr_date = smp->data.uint;
10493
10494 /* add offset */
10495 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10496 curr_date += args[0].data.sint;
10497
10498 tm = gmtime(&curr_date);
10499
10500 temp = get_trash_chunk();
10501 temp->len = snprintf(temp->str, temp->size - temp->len,
10502 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10503 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10504 tm->tm_hour, tm->tm_min, tm->tm_sec);
10505
10506 smp->data.str = *temp;
10507 smp->type = SMP_T_STR;
10508 return 1;
10509}
10510
Thierry FOURNIERad903512014-04-11 17:51:01 +020010511/* Match language range with language tag. RFC2616 14.4:
10512 *
10513 * A language-range matches a language-tag if it exactly equals
10514 * the tag, or if it exactly equals a prefix of the tag such
10515 * that the first tag character following the prefix is "-".
10516 *
10517 * Return 1 if the strings match, else return 0.
10518 */
10519static inline int language_range_match(const char *range, int range_len,
10520 const char *tag, int tag_len)
10521{
10522 const char *end = range + range_len;
10523 const char *tend = tag + tag_len;
10524 while (range < end) {
10525 if (*range == '-' && tag == tend)
10526 return 1;
10527 if (*range != *tag || tag == tend)
10528 return 0;
10529 range++;
10530 tag++;
10531 }
10532 /* Return true only if the last char of the tag is matched. */
10533 return tag == tend;
10534}
10535
10536/* Arguments: The list of expected value, the number of parts returned and the separator */
10537static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
10538{
10539 const char *al = smp->data.str.str;
10540 const char *end = al + smp->data.str.len;
10541 const char *token;
10542 int toklen;
10543 int qvalue;
10544 const char *str;
10545 const char *w;
10546 int best_q = 0;
10547
10548 /* Set the constant to the sample, because the output of the
10549 * function will be peek in the constant configuration string.
10550 */
10551 smp->flags |= SMP_F_CONST;
10552 smp->data.str.size = 0;
10553 smp->data.str.str = "";
10554 smp->data.str.len = 0;
10555
10556 /* Parse the accept language */
10557 while (1) {
10558
10559 /* Jump spaces, quit if the end is detected. */
10560 while (al < end && isspace(*al))
10561 al++;
10562 if (al >= end)
10563 break;
10564
10565 /* Start of the fisrt word. */
10566 token = al;
10567
10568 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
10569 while (al < end && *al != ';' && *al != ',' && !isspace(*al))
10570 al++;
10571 if (al == token)
10572 goto expect_comma;
10573
10574 /* Length of the token. */
10575 toklen = al - token;
10576 qvalue = 1000;
10577
10578 /* Check if the token exists in the list. If the token not exists,
10579 * jump to the next token.
10580 */
10581 str = args[0].data.str.str;
10582 w = str;
10583 while (1) {
10584 if (*str == ';' || *str == '\0') {
10585 if (language_range_match(token, toklen, w, str-w))
10586 goto look_for_q;
10587 if (*str == '\0')
10588 goto expect_comma;
10589 w = str + 1;
10590 }
10591 str++;
10592 }
10593 goto expect_comma;
10594
10595look_for_q:
10596
10597 /* Jump spaces, quit if the end is detected. */
10598 while (al < end && isspace(*al))
10599 al++;
10600 if (al >= end)
10601 goto process_value;
10602
10603 /* If ',' is found, process the result */
10604 if (*al == ',')
10605 goto process_value;
10606
10607 /* If the character is different from ';', look
10608 * for the end of the header part in best effort.
10609 */
10610 if (*al != ';')
10611 goto expect_comma;
10612
10613 /* Assumes that the char is ';', now expect "q=". */
10614 al++;
10615
10616 /* Jump spaces, process value if the end is detected. */
10617 while (al < end && isspace(*al))
10618 al++;
10619 if (al >= end)
10620 goto process_value;
10621
10622 /* Expect 'q'. If no 'q', continue in best effort */
10623 if (*al != 'q')
10624 goto process_value;
10625 al++;
10626
10627 /* Jump spaces, process value if the end is detected. */
10628 while (al < end && isspace(*al))
10629 al++;
10630 if (al >= end)
10631 goto process_value;
10632
10633 /* Expect '='. If no '=', continue in best effort */
10634 if (*al != '=')
10635 goto process_value;
10636 al++;
10637
10638 /* Jump spaces, process value if the end is detected. */
10639 while (al < end && isspace(*al))
10640 al++;
10641 if (al >= end)
10642 goto process_value;
10643
10644 /* Parse the q value. */
10645 qvalue = parse_qvalue(al, &al);
10646
10647process_value:
10648
10649 /* If the new q value is the best q value, then store the associated
10650 * language in the response. If qvalue is the biggest value (1000),
10651 * break the process.
10652 */
10653 if (qvalue > best_q) {
10654 smp->data.str.str = (char *)w;
10655 smp->data.str.len = str - w;
10656 if (qvalue >= 1000)
10657 break;
10658 best_q = qvalue;
10659 }
10660
10661expect_comma:
10662
10663 /* Expect comma or end. If the end is detected, quit the loop. */
10664 while (al < end && *al != ',')
10665 al++;
10666 if (al >= end)
10667 break;
10668
10669 /* Comma is found, jump it and restart the analyzer. */
10670 al++;
10671 }
10672
10673 /* Set default value if required. */
10674 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
10675 smp->data.str.str = args[1].data.str.str;
10676 smp->data.str.len = args[1].data.str.len;
10677 }
10678
10679 /* Return true only if a matching language was found. */
10680 return smp->data.str.len != 0;
10681}
10682
Willy Tarreau4a568972010-05-12 08:08:50 +020010683/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010684/* All supported ACL keywords must be declared here. */
10685/************************************************************************/
10686
10687/* Note: must not be declared <const> as its list will be overwritten.
10688 * Please take care of keeping this list alphabetically sorted.
10689 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010690static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010691 { "base", "base", PAT_MATCH_STR },
10692 { "base_beg", "base", PAT_MATCH_BEG },
10693 { "base_dir", "base", PAT_MATCH_DIR },
10694 { "base_dom", "base", PAT_MATCH_DOM },
10695 { "base_end", "base", PAT_MATCH_END },
10696 { "base_len", "base", PAT_MATCH_LEN },
10697 { "base_reg", "base", PAT_MATCH_REG },
10698 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010699
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010700 { "cook", "req.cook", PAT_MATCH_STR },
10701 { "cook_beg", "req.cook", PAT_MATCH_BEG },
10702 { "cook_dir", "req.cook", PAT_MATCH_DIR },
10703 { "cook_dom", "req.cook", PAT_MATCH_DOM },
10704 { "cook_end", "req.cook", PAT_MATCH_END },
10705 { "cook_len", "req.cook", PAT_MATCH_LEN },
10706 { "cook_reg", "req.cook", PAT_MATCH_REG },
10707 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010708
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010709 { "hdr", "req.hdr", PAT_MATCH_STR },
10710 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
10711 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
10712 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
10713 { "hdr_end", "req.hdr", PAT_MATCH_END },
10714 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
10715 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
10716 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010717
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010718 /* these two declarations uses strings with list storage (in place
10719 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
10720 * and delete functions are relative to the list management. The parse
10721 * and match method are related to the corresponding fetch methods. This
10722 * is very particular ACL declaration mode.
10723 */
10724 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
10725 { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010726
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010727 { "path", "path", PAT_MATCH_STR },
10728 { "path_beg", "path", PAT_MATCH_BEG },
10729 { "path_dir", "path", PAT_MATCH_DIR },
10730 { "path_dom", "path", PAT_MATCH_DOM },
10731 { "path_end", "path", PAT_MATCH_END },
10732 { "path_len", "path", PAT_MATCH_LEN },
10733 { "path_reg", "path", PAT_MATCH_REG },
10734 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010735
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010736 { "req_ver", "req.ver", PAT_MATCH_STR },
10737 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010738
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010739 { "scook", "res.cook", PAT_MATCH_STR },
10740 { "scook_beg", "res.cook", PAT_MATCH_BEG },
10741 { "scook_dir", "res.cook", PAT_MATCH_DIR },
10742 { "scook_dom", "res.cook", PAT_MATCH_DOM },
10743 { "scook_end", "res.cook", PAT_MATCH_END },
10744 { "scook_len", "res.cook", PAT_MATCH_LEN },
10745 { "scook_reg", "res.cook", PAT_MATCH_REG },
10746 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010747
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010748 { "shdr", "res.hdr", PAT_MATCH_STR },
10749 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
10750 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
10751 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
10752 { "shdr_end", "res.hdr", PAT_MATCH_END },
10753 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
10754 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
10755 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010756
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010757 { "url", "url", PAT_MATCH_STR },
10758 { "url_beg", "url", PAT_MATCH_BEG },
10759 { "url_dir", "url", PAT_MATCH_DIR },
10760 { "url_dom", "url", PAT_MATCH_DOM },
10761 { "url_end", "url", PAT_MATCH_END },
10762 { "url_len", "url", PAT_MATCH_LEN },
10763 { "url_reg", "url", PAT_MATCH_REG },
10764 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010765
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010766 { "urlp", "urlp", PAT_MATCH_STR },
10767 { "urlp_beg", "urlp", PAT_MATCH_BEG },
10768 { "urlp_dir", "urlp", PAT_MATCH_DIR },
10769 { "urlp_dom", "urlp", PAT_MATCH_DOM },
10770 { "urlp_end", "urlp", PAT_MATCH_END },
10771 { "urlp_len", "urlp", PAT_MATCH_LEN },
10772 { "urlp_reg", "urlp", PAT_MATCH_REG },
10773 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010774
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010775 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010776}};
10777
10778/************************************************************************/
10779/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010780/************************************************************************/
10781/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010782static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010783 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010784 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10785 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10786
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010787 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
10788 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemand65ad6e12014-01-31 15:08:02 +010010789
William Lallemanda43ba4e2014-01-28 18:14:25 +010010790 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010791 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
10792 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010010793
Willy Tarreau409bcde2013-01-08 00:31:00 +010010794 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10795 * are only here to match the ACL's name, are request-only and are used
10796 * for ACL compatibility only.
10797 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010798 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10799 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010800 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10801 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10802
10803 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10804 * only here to match the ACL's name, are request-only and are used for
10805 * ACL compatibility only.
10806 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010807 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010808 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10809 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10810 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10811
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010812 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010813 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010814 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010815 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010816 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010817
10818 /* HTTP protocol on the request path */
10819 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010820 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010821
10822 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010823 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
10824 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010825
10826 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010827 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
10828 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010829
Willy Tarreau18ed2562013-01-14 15:56:36 +010010830 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010831 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010832 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10833 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10834
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010835 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010836 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010837 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010838 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10839 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10840 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10841
10842 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010843 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010844 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10845 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10846
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010847 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010848 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010849 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010850 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10851 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10852 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10853
Willy Tarreau409bcde2013-01-08 00:31:00 +010010854 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010855 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010856 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10857 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010858 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010010859
10860 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010861 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010862 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10863 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10864 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10865
10866 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010867 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010868 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10869 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010870 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10871 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010872 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10873 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010874 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10875 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010876}};
10877
Willy Tarreau8797c062007-05-07 00:55:35 +020010878
Willy Tarreau276fae92013-07-25 14:36:01 +020010879/* Note: must not be declared <const> as its list will be overwritten */
10880static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020010881 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
10882 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020010883 { NULL, NULL, 0, 0, 0 },
10884}};
10885
Willy Tarreau8797c062007-05-07 00:55:35 +020010886__attribute__((constructor))
10887static void __http_protocol_init(void)
10888{
10889 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010890 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010891 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010892}
10893
10894
Willy Tarreau58f10d72006-12-04 02:26:12 +010010895/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010896 * Local variables:
10897 * c-indent-level: 8
10898 * c-basic-offset: 8
10899 * End:
10900 */