blob: f61bfe6b2ad4be67ca8492841e509dcc96260ce8 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreau655dce92009-11-08 13:10:58 +01004 * Copyright 2000-2009 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 Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010044#include <proto/checks.h>
Maik Broemme2850cb42009-04-17 18:53:21 +020045#include <proto/client.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/fd.h>
48#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010049#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020050#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010052#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010054#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010056#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020057#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/task.h>
59
Willy Tarreau522d6c02009-12-06 18:49:18 +010060const char HTTP_100[] =
61 "HTTP/1.1 100 Continue\r\n\r\n";
62
63const struct chunk http_100_chunk = {
64 .str = (char *)&HTTP_100,
65 .len = sizeof(HTTP_100)-1
66};
67
Willy Tarreau1c47f852006-07-09 08:22:27 +020068/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010069const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020070 "HTTP/1.0 200 OK\r\n"
71 "Cache-Control: no-cache\r\n"
72 "Connection: close\r\n"
73 "Content-Type: text/html\r\n"
74 "\r\n"
75 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
76
Willy Tarreau0f772532006-12-23 20:51:41 +010077const struct chunk http_200_chunk = {
78 .str = (char *)&HTTP_200,
79 .len = sizeof(HTTP_200)-1
80};
81
Willy Tarreaub463dfb2008-06-07 23:08:56 +020082const char *HTTP_301 =
83 "HTTP/1.0 301 Moved Permantenly\r\n"
84 "Cache-Control: no-cache\r\n"
85 "Connection: close\r\n"
86 "Location: "; /* not terminated since it will be concatenated with the URL */
87
Willy Tarreau0f772532006-12-23 20:51:41 +010088const char *HTTP_302 =
89 "HTTP/1.0 302 Found\r\n"
90 "Cache-Control: no-cache\r\n"
91 "Connection: close\r\n"
92 "Location: "; /* not terminated since it will be concatenated with the URL */
93
94/* same as 302 except that the browser MUST retry with the GET method */
95const char *HTTP_303 =
96 "HTTP/1.0 303 See Other\r\n"
97 "Cache-Control: no-cache\r\n"
98 "Connection: close\r\n"
99 "Location: "; /* not terminated since it will be concatenated with the URL */
100
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
102const char *HTTP_401_fmt =
103 "HTTP/1.0 401 Unauthorized\r\n"
104 "Cache-Control: no-cache\r\n"
105 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200106 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
108 "\r\n"
109 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
110
Willy Tarreau0f772532006-12-23 20:51:41 +0100111
112const int http_err_codes[HTTP_ERR_SIZE] = {
113 [HTTP_ERR_400] = 400,
114 [HTTP_ERR_403] = 403,
115 [HTTP_ERR_408] = 408,
116 [HTTP_ERR_500] = 500,
117 [HTTP_ERR_502] = 502,
118 [HTTP_ERR_503] = 503,
119 [HTTP_ERR_504] = 504,
120};
121
Willy Tarreau80587432006-12-24 17:47:20 +0100122static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100123 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100124 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100125 "Cache-Control: no-cache\r\n"
126 "Connection: close\r\n"
127 "Content-Type: text/html\r\n"
128 "\r\n"
129 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
130
131 [HTTP_ERR_403] =
132 "HTTP/1.0 403 Forbidden\r\n"
133 "Cache-Control: no-cache\r\n"
134 "Connection: close\r\n"
135 "Content-Type: text/html\r\n"
136 "\r\n"
137 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
138
139 [HTTP_ERR_408] =
140 "HTTP/1.0 408 Request Time-out\r\n"
141 "Cache-Control: no-cache\r\n"
142 "Connection: close\r\n"
143 "Content-Type: text/html\r\n"
144 "\r\n"
145 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
146
147 [HTTP_ERR_500] =
148 "HTTP/1.0 500 Server Error\r\n"
149 "Cache-Control: no-cache\r\n"
150 "Connection: close\r\n"
151 "Content-Type: text/html\r\n"
152 "\r\n"
153 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
154
155 [HTTP_ERR_502] =
156 "HTTP/1.0 502 Bad Gateway\r\n"
157 "Cache-Control: no-cache\r\n"
158 "Connection: close\r\n"
159 "Content-Type: text/html\r\n"
160 "\r\n"
161 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
162
163 [HTTP_ERR_503] =
164 "HTTP/1.0 503 Service Unavailable\r\n"
165 "Cache-Control: no-cache\r\n"
166 "Connection: close\r\n"
167 "Content-Type: text/html\r\n"
168 "\r\n"
169 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
170
171 [HTTP_ERR_504] =
172 "HTTP/1.0 504 Gateway Time-out\r\n"
173 "Cache-Control: no-cache\r\n"
174 "Connection: close\r\n"
175 "Content-Type: text/html\r\n"
176 "\r\n"
177 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
178
179};
180
Willy Tarreau80587432006-12-24 17:47:20 +0100181/* We must put the messages here since GCC cannot initialize consts depending
182 * on strlen().
183 */
184struct chunk http_err_chunks[HTTP_ERR_SIZE];
185
Willy Tarreau42250582007-04-01 01:30:43 +0200186#define FD_SETS_ARE_BITFIELDS
187#ifdef FD_SETS_ARE_BITFIELDS
188/*
189 * This map is used with all the FD_* macros to check whether a particular bit
190 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
191 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
192 * byte should be encoded. Be careful to always pass bytes from 0 to 255
193 * exclusively to the macros.
194 */
195fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
196fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
197
198#else
199#error "Check if your OS uses bitfields for fd_sets"
200#endif
201
Willy Tarreau80587432006-12-24 17:47:20 +0100202void init_proto_http()
203{
Willy Tarreau42250582007-04-01 01:30:43 +0200204 int i;
205 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100206 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200207
Willy Tarreau80587432006-12-24 17:47:20 +0100208 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
209 if (!http_err_msgs[msg]) {
210 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
211 abort();
212 }
213
214 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
215 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
216 }
Willy Tarreau42250582007-04-01 01:30:43 +0200217
218 /* initialize the log header encoding map : '{|}"#' should be encoded with
219 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
220 * URL encoding only requires '"', '#' to be encoded as well as non-
221 * printable characters above.
222 */
223 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
224 memset(url_encode_map, 0, sizeof(url_encode_map));
225 for (i = 0; i < 32; i++) {
226 FD_SET(i, hdr_encode_map);
227 FD_SET(i, url_encode_map);
228 }
229 for (i = 127; i < 256; i++) {
230 FD_SET(i, hdr_encode_map);
231 FD_SET(i, url_encode_map);
232 }
233
234 tmp = "\"#{|}";
235 while (*tmp) {
236 FD_SET(*tmp, hdr_encode_map);
237 tmp++;
238 }
239
240 tmp = "\"#";
241 while (*tmp) {
242 FD_SET(*tmp, url_encode_map);
243 tmp++;
244 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200245
246 /* memory allocations */
247 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200248 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100249}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200250
Willy Tarreau53b6c742006-12-17 13:37:46 +0100251/*
252 * We have 26 list of methods (1 per first letter), each of which can have
253 * up to 3 entries (2 valid, 1 null).
254 */
255struct http_method_desc {
256 http_meth_t meth;
257 int len;
258 const char text[8];
259};
260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100261const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100262 ['C' - 'A'] = {
263 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
264 },
265 ['D' - 'A'] = {
266 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
267 },
268 ['G' - 'A'] = {
269 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
270 },
271 ['H' - 'A'] = {
272 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
273 },
274 ['P' - 'A'] = {
275 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
276 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
277 },
278 ['T' - 'A'] = {
279 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
280 },
281 /* rest is empty like this :
282 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
283 */
284};
285
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100286/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200287 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100288 * RFC2616 for those chars.
289 */
290
291const char http_is_spht[256] = {
292 [' '] = 1, ['\t'] = 1,
293};
294
295const char http_is_crlf[256] = {
296 ['\r'] = 1, ['\n'] = 1,
297};
298
299const char http_is_lws[256] = {
300 [' '] = 1, ['\t'] = 1,
301 ['\r'] = 1, ['\n'] = 1,
302};
303
304const char http_is_sep[256] = {
305 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
306 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
307 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
308 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
309 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
310};
311
312const char http_is_ctl[256] = {
313 [0 ... 31] = 1,
314 [127] = 1,
315};
316
317/*
318 * A token is any ASCII char that is neither a separator nor a CTL char.
319 * Do not overwrite values in assignment since gcc-2.95 will not handle
320 * them correctly. Instead, define every non-CTL char's status.
321 */
322const char http_is_token[256] = {
323 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
324 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
325 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
326 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
327 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
328 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
329 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
330 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
331 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
332 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
333 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
334 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
335 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
336 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
337 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
338 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
339 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
340 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
341 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
342 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
343 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
344 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
345 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
346 ['|'] = 1, ['}'] = 0, ['~'] = 1,
347};
348
349
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100350/*
351 * An http ver_token is any ASCII which can be found in an HTTP version,
352 * which includes 'H', 'T', 'P', '/', '.' and any digit.
353 */
354const char http_is_ver_token[256] = {
355 ['.'] = 1, ['/'] = 1,
356 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
357 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
358 ['H'] = 1, ['P'] = 1, ['T'] = 1,
359};
360
361
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100362/*
363 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
364 * CRLF. Text length is measured first, so it cannot be NULL.
365 * The header is also automatically added to the index <hdr_idx>, and the end
366 * of headers is automatically adjusted. The number of bytes added is returned
367 * on success, otherwise <0 is returned indicating an error.
368 */
369int http_header_add_tail(struct buffer *b, struct http_msg *msg,
370 struct hdr_idx *hdr_idx, const char *text)
371{
372 int bytes, len;
373
374 len = strlen(text);
375 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
376 if (!bytes)
377 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100378 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100379 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
380}
381
382/*
383 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
384 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
385 * the buffer is only opened and the space reserved, but nothing is copied.
386 * The header is also automatically added to the index <hdr_idx>, and the end
387 * of headers is automatically adjusted. The number of bytes added is returned
388 * on success, otherwise <0 is returned indicating an error.
389 */
390int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
391 struct hdr_idx *hdr_idx, const char *text, int len)
392{
393 int bytes;
394
395 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
396 if (!bytes)
397 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100398 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100399 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
400}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200401
402/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100403 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
404 * If so, returns the position of the first non-space character relative to
405 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
406 * to return a pointer to the place after the first space. Returns 0 if the
407 * header name does not match. Checks are case-insensitive.
408 */
409int http_header_match2(const char *hdr, const char *end,
410 const char *name, int len)
411{
412 const char *val;
413
414 if (hdr + len >= end)
415 return 0;
416 if (hdr[len] != ':')
417 return 0;
418 if (strncasecmp(hdr, name, len) != 0)
419 return 0;
420 val = hdr + len + 1;
421 while (val < end && HTTP_IS_SPHT(*val))
422 val++;
423 if ((val >= end) && (len + 2 <= end - hdr))
424 return len + 2; /* we may replace starting from second space */
425 return val - hdr;
426}
427
Willy Tarreau33a7e692007-06-10 19:45:56 +0200428/* Find the end of the header value contained between <s> and <e>.
429 * See RFC2616, par 2.2 for more information. Note that it requires
430 * a valid header to return a valid result.
431 */
432const char *find_hdr_value_end(const char *s, const char *e)
433{
434 int quoted, qdpair;
435
436 quoted = qdpair = 0;
437 for (; s < e; s++) {
438 if (qdpair) qdpair = 0;
439 else if (quoted && *s == '\\') qdpair = 1;
440 else if (quoted && *s == '"') quoted = 0;
441 else if (*s == '"') quoted = 1;
442 else if (*s == ',') return s;
443 }
444 return s;
445}
446
447/* Find the first or next occurrence of header <name> in message buffer <sol>
448 * using headers index <idx>, and return it in the <ctx> structure. This
449 * structure holds everything necessary to use the header and find next
450 * occurrence. If its <idx> member is 0, the header is searched from the
451 * beginning. Otherwise, the next occurrence is returned. The function returns
452 * 1 when it finds a value, and 0 when there is no more.
453 */
454int http_find_header2(const char *name, int len,
455 const char *sol, struct hdr_idx *idx,
456 struct hdr_ctx *ctx)
457{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200458 const char *eol, *sov;
459 int cur_idx;
460
461 if (ctx->idx) {
462 /* We have previously returned a value, let's search
463 * another one on the same line.
464 */
465 cur_idx = ctx->idx;
466 sol = ctx->line;
467 sov = sol + ctx->val + ctx->vlen;
468 eol = sol + idx->v[cur_idx].len;
469
470 if (sov >= eol)
471 /* no more values in this header */
472 goto next_hdr;
473
474 /* values remaining for this header, skip the comma */
475 sov++;
476 while (sov < eol && http_is_lws[(unsigned char)*sov])
477 sov++;
478
479 goto return_hdr;
480 }
481
482 /* first request for this header */
483 sol += hdr_idx_first_pos(idx);
484 cur_idx = hdr_idx_first_idx(idx);
485
486 while (cur_idx) {
487 eol = sol + idx->v[cur_idx].len;
488
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200489 if (len == 0) {
490 /* No argument was passed, we want any header.
491 * To achieve this, we simply build a fake request. */
492 while (sol + len < eol && sol[len] != ':')
493 len++;
494 name = sol;
495 }
496
Willy Tarreau33a7e692007-06-10 19:45:56 +0200497 if ((len < eol - sol) &&
498 (sol[len] == ':') &&
499 (strncasecmp(sol, name, len) == 0)) {
500
501 sov = sol + len + 1;
502 while (sov < eol && http_is_lws[(unsigned char)*sov])
503 sov++;
504 return_hdr:
505 ctx->line = sol;
506 ctx->idx = cur_idx;
507 ctx->val = sov - sol;
508
509 eol = find_hdr_value_end(sov, eol);
510 ctx->vlen = eol - sov;
511 return 1;
512 }
513 next_hdr:
514 sol = eol + idx->v[cur_idx].cr + 1;
515 cur_idx = idx->v[cur_idx].next;
516 }
517 return 0;
518}
519
520int http_find_header(const char *name,
521 const char *sol, struct hdr_idx *idx,
522 struct hdr_ctx *ctx)
523{
524 return http_find_header2(name, strlen(name), sol, idx, ctx);
525}
526
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100527/* This function handles a server error at the stream interface level. The
528 * stream interface is assumed to be already in a closed state. An optional
529 * message is copied into the input buffer, and an HTTP status code stored.
530 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100531 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200532 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100533static void http_server_error(struct session *t, struct stream_interface *si,
534 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200535{
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100536 buffer_erase(si->ob);
537 buffer_erase(si->ib);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200538 buffer_auto_close(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100539 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100540 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100541 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200542 }
543 if (!(t->flags & SN_ERR_MASK))
544 t->flags |= err;
545 if (!(t->flags & SN_FINST_MASK))
546 t->flags |= finst;
547}
548
Willy Tarreau80587432006-12-24 17:47:20 +0100549/* This function returns the appropriate error location for the given session
550 * and message.
551 */
552
553struct chunk *error_message(struct session *s, int msgnum)
554{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200555 if (s->be->errmsg[msgnum].str)
556 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100557 else if (s->fe->errmsg[msgnum].str)
558 return &s->fe->errmsg[msgnum];
559 else
560 return &http_err_chunks[msgnum];
561}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200562
Willy Tarreau53b6c742006-12-17 13:37:46 +0100563/*
564 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
565 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
566 */
567static http_meth_t find_http_meth(const char *str, const int len)
568{
569 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100570 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100571
572 m = ((unsigned)*str - 'A');
573
574 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100575 for (h = http_methods[m]; h->len > 0; h++) {
576 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100577 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100578 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100579 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100580 };
581 return HTTP_METH_OTHER;
582 }
583 return HTTP_METH_NONE;
584
585}
586
Willy Tarreau21d2af32008-02-14 20:25:24 +0100587/* Parse the URI from the given transaction (which is assumed to be in request
588 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
589 * It is returned otherwise.
590 */
591static char *
592http_get_path(struct http_txn *txn)
593{
594 char *ptr, *end;
595
596 ptr = txn->req.sol + txn->req.sl.rq.u;
597 end = ptr + txn->req.sl.rq.u_l;
598
599 if (ptr >= end)
600 return NULL;
601
602 /* RFC2616, par. 5.1.2 :
603 * Request-URI = "*" | absuri | abspath | authority
604 */
605
606 if (*ptr == '*')
607 return NULL;
608
609 if (isalpha((unsigned char)*ptr)) {
610 /* this is a scheme as described by RFC3986, par. 3.1 */
611 ptr++;
612 while (ptr < end &&
613 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
614 ptr++;
615 /* skip '://' */
616 if (ptr == end || *ptr++ != ':')
617 return NULL;
618 if (ptr == end || *ptr++ != '/')
619 return NULL;
620 if (ptr == end || *ptr++ != '/')
621 return NULL;
622 }
623 /* skip [user[:passwd]@]host[:[port]] */
624
625 while (ptr < end && *ptr != '/')
626 ptr++;
627
628 if (ptr == end)
629 return NULL;
630
631 /* OK, we got the '/' ! */
632 return ptr;
633}
634
Willy Tarreauefb453c2008-10-26 20:49:47 +0100635/* Returns a 302 for a redirectable request. This may only be called just after
636 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
637 * left unchanged and will follow normal proxy processing.
638 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100639void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100640{
641 struct http_txn *txn;
642 struct chunk rdr;
643 char *path;
644 int len;
645
646 /* 1: create the response header */
647 rdr.len = strlen(HTTP_302);
648 rdr.str = trash;
649 memcpy(rdr.str, HTTP_302, rdr.len);
650
651 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200652 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100653 return;
654
655 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
656 rdr.len += s->srv->rdr_len;
657
658 /* 3: add the request URI */
659 txn = &s->txn;
660 path = http_get_path(txn);
661 if (!path)
662 return;
663
664 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200665 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100666 return;
667
668 memcpy(rdr.str + rdr.len, path, len);
669 rdr.len += len;
670 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
671 rdr.len += 4;
672
673 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100674 si->shutr(si);
675 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100676 si->err_type = SI_ET_NONE;
677 si->err_loc = NULL;
678 si->state = SI_ST_CLO;
679
680 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100681 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100682
683 /* FIXME: we should increase a counter of redirects per server and per backend. */
684 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100685 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100686}
687
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100688/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100689 * that the server side is closed. Note that err_type is actually a
690 * bitmask, where almost only aborts may be cumulated with other
691 * values. We consider that aborted operations are more important
692 * than timeouts or errors due to the fact that nobody else in the
693 * logs might explain incomplete retries. All others should avoid
694 * being cumulated. It should normally not be possible to have multiple
695 * aborts at once, but just in case, the first one in sequence is reported.
696 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100697void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100698{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100699 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100700
701 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100702 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
703 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100704 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100705 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
706 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100707 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100708 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
709 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100710 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100711 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
712 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100713 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100714 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
715 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100716 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100717 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
718 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100719 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100720 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
721 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100722}
723
Willy Tarreau42250582007-04-01 01:30:43 +0200724extern const char sess_term_cond[8];
725extern const char sess_fin_state[8];
726extern const char *monthname[12];
727const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
728const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
729 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
730 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200731struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200732struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100733
Emeric Brun3a058f32009-06-30 18:26:00 +0200734void http_sess_clflog(struct session *s)
735{
736 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
737 struct proxy *fe = s->fe;
738 struct proxy *be = s->be;
739 struct proxy *prx_log;
740 struct http_txn *txn = &s->txn;
741 int tolog, level, err;
742 char *uri, *h;
743 char *svid;
744 struct tm tm;
745 static char tmpline[MAX_SYSLOG_LEN];
746 int hdr;
747 size_t w;
748 int t_request;
749
750 prx_log = fe;
751 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
752 (s->conn_retries != be->conn_retries) ||
753 txn->status >= 500;
754
755 if (s->cli_addr.ss_family == AF_INET)
756 inet_ntop(AF_INET,
757 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
758 pn, sizeof(pn));
759 else
760 inet_ntop(AF_INET6,
761 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
762 pn, sizeof(pn));
763
764 get_gmtime(s->logs.accept_date.tv_sec, &tm);
765
766 /* FIXME: let's limit ourselves to frontend logging for now. */
767 tolog = fe->to_log;
768
769 h = tmpline;
770
771 w = snprintf(h, sizeof(tmpline),
772 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
773 pn,
774 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
775 tm.tm_hour, tm.tm_min, tm.tm_sec);
776 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
777 goto trunc;
778 h += w;
779
780 if (h >= tmpline + sizeof(tmpline) - 4)
781 goto trunc;
782
783 *(h++) = ' ';
784 *(h++) = '\"';
785 uri = txn->uri ? txn->uri : "<BADREQ>";
786 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
787 '#', url_encode_map, uri);
788 *(h++) = '\"';
789
790 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
791 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
792 goto trunc;
793 h += w;
794
795 if (h >= tmpline + sizeof(tmpline) - 9)
796 goto trunc;
797 memcpy(h, " \"-\" \"-\"", 8);
798 h += 8;
799
800 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
801 " %d %03d",
802 (s->cli_addr.ss_family == AF_INET) ?
803 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
804 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
805 (int)s->logs.accept_date.tv_usec/1000);
806 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
807 goto trunc;
808 h += w;
809
810 w = strlen(fe->id);
811 if (h >= tmpline + sizeof(tmpline) - 4 - w)
812 goto trunc;
813 *(h++) = ' ';
814 *(h++) = '\"';
815 memcpy(h, fe->id, w);
816 h += w;
817 *(h++) = '\"';
818
819 w = strlen(be->id);
820 if (h >= tmpline + sizeof(tmpline) - 4 - w)
821 goto trunc;
822 *(h++) = ' ';
823 *(h++) = '\"';
824 memcpy(h, be->id, w);
825 h += w;
826 *(h++) = '\"';
827
828 svid = (tolog & LW_SVID) ?
829 (s->data_source != DATA_SRC_STATS) ?
830 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
831
832 w = strlen(svid);
833 if (h >= tmpline + sizeof(tmpline) - 4 - w)
834 goto trunc;
835 *(h++) = ' ';
836 *(h++) = '\"';
837 memcpy(h, svid, w);
838 h += w;
839 *(h++) = '\"';
840
841 t_request = -1;
842 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
843 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
844 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
845 " %d %ld %ld %ld %ld",
846 t_request,
847 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
848 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
849 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
850 s->logs.t_close);
851 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
852 goto trunc;
853 h += w;
854
855 if (h >= tmpline + sizeof(tmpline) - 8)
856 goto trunc;
857 *(h++) = ' ';
858 *(h++) = '\"';
859 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
860 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
861 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
862 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
863 *(h++) = '\"';
864
865 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
866 " %d %d %d %d %d %ld %ld",
867 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
868 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
869 s->logs.srv_queue_size, s->logs.prx_queue_size);
870
871 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
872 goto trunc;
873 h += w;
874
875 if (txn->cli_cookie) {
876 w = strlen(txn->cli_cookie);
877 if (h >= tmpline + sizeof(tmpline) - 4 - w)
878 goto trunc;
879 *(h++) = ' ';
880 *(h++) = '\"';
881 memcpy(h, txn->cli_cookie, w);
882 h += w;
883 *(h++) = '\"';
884 } else {
885 if (h >= tmpline + sizeof(tmpline) - 5)
886 goto trunc;
887 memcpy(h, " \"-\"", 4);
888 h += 4;
889 }
890
891 if (txn->srv_cookie) {
892 w = strlen(txn->srv_cookie);
893 if (h >= tmpline + sizeof(tmpline) - 4 - w)
894 goto trunc;
895 *(h++) = ' ';
896 *(h++) = '\"';
897 memcpy(h, txn->srv_cookie, w);
898 h += w;
899 *(h++) = '\"';
900 } else {
901 if (h >= tmpline + sizeof(tmpline) - 5)
902 goto trunc;
903 memcpy(h, " \"-\"", 4);
904 h += 4;
905 }
906
907 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
908 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
909 if (h >= sizeof (tmpline) + tmpline - 4)
910 goto trunc;
911 *(h++) = ' ';
912 *(h++) = '\"';
913 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
914 '#', hdr_encode_map, txn->req.cap[hdr]);
915 *(h++) = '\"';
916 }
917 }
918
919 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
920 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
921 if (h >= sizeof (tmpline) + tmpline - 4)
922 goto trunc;
923 *(h++) = ' ';
924 *(h++) = '\"';
925 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
926 '#', hdr_encode_map, txn->rsp.cap[hdr]);
927 *(h++) = '\"';
928 }
929 }
930
931trunc:
932 *h = '\0';
933
934 level = LOG_INFO;
935 if (err && (fe->options2 & PR_O2_LOGERRORS))
936 level = LOG_ERR;
937
938 send_log(prx_log, level, "%s\n", tmpline);
939
940 s->logs.logwait = 0;
941}
942
Willy Tarreau42250582007-04-01 01:30:43 +0200943/*
944 * send a log for the session when we have enough info about it.
945 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100946 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100947void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200948{
949 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
950 struct proxy *fe = s->fe;
951 struct proxy *be = s->be;
952 struct proxy *prx_log;
953 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200954 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200955 char *uri, *h;
956 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200957 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200958 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200959 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200960 int hdr;
961
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200962 /* if we don't want to log normal traffic, return now */
963 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
964 (s->conn_retries != be->conn_retries) ||
965 txn->status >= 500;
966 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
967 return;
968
Willy Tarreau42250582007-04-01 01:30:43 +0200969 if (fe->logfac1 < 0 && fe->logfac2 < 0)
970 return;
971 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100972
Emeric Brun3a058f32009-06-30 18:26:00 +0200973 if (prx_log->options2 & PR_O2_CLFLOG)
974 return http_sess_clflog(s);
975
Willy Tarreau42250582007-04-01 01:30:43 +0200976 if (s->cli_addr.ss_family == AF_INET)
977 inet_ntop(AF_INET,
978 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
979 pn, sizeof(pn));
980 else
981 inet_ntop(AF_INET6,
982 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
983 pn, sizeof(pn));
984
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200985 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200986
987 /* FIXME: let's limit ourselves to frontend logging for now. */
988 tolog = fe->to_log;
989
990 h = tmpline;
991 if (fe->to_log & LW_REQHDR &&
992 txn->req.cap &&
993 (h < tmpline + sizeof(tmpline) - 10)) {
994 *(h++) = ' ';
995 *(h++) = '{';
996 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
997 if (hdr)
998 *(h++) = '|';
999 if (txn->req.cap[hdr] != NULL)
1000 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1001 '#', hdr_encode_map, txn->req.cap[hdr]);
1002 }
1003 *(h++) = '}';
1004 }
1005
1006 if (fe->to_log & LW_RSPHDR &&
1007 txn->rsp.cap &&
1008 (h < tmpline + sizeof(tmpline) - 7)) {
1009 *(h++) = ' ';
1010 *(h++) = '{';
1011 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1012 if (hdr)
1013 *(h++) = '|';
1014 if (txn->rsp.cap[hdr] != NULL)
1015 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1016 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1017 }
1018 *(h++) = '}';
1019 }
1020
1021 if (h < tmpline + sizeof(tmpline) - 4) {
1022 *(h++) = ' ';
1023 *(h++) = '"';
1024 uri = txn->uri ? txn->uri : "<BADREQ>";
1025 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1026 '#', url_encode_map, uri);
1027 *(h++) = '"';
1028 }
1029 *h = '\0';
1030
1031 svid = (tolog & LW_SVID) ?
1032 (s->data_source != DATA_SRC_STATS) ?
1033 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1034
Willy Tarreau70089872008-06-13 21:12:51 +02001035 t_request = -1;
1036 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1037 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1038
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001039 level = LOG_INFO;
1040 if (err && (fe->options2 & PR_O2_LOGERRORS))
1041 level = LOG_ERR;
1042
1043 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001044 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001045 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1046 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001047 pn,
1048 (s->cli_addr.ss_family == AF_INET) ?
1049 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1050 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001051 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001052 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001053 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001054 t_request,
1055 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001056 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1057 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1058 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1059 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001060 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001061 txn->cli_cookie ? txn->cli_cookie : "-",
1062 txn->srv_cookie ? txn->srv_cookie : "-",
1063 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1064 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1065 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1066 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1067 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001068 (s->flags & SN_REDISP)?"+":"",
1069 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001070 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1071
1072 s->logs.logwait = 0;
1073}
1074
Willy Tarreau117f59e2007-03-04 18:17:17 +01001075
1076/*
1077 * Capture headers from message starting at <som> according to header list
1078 * <cap_hdr>, and fill the <idx> structure appropriately.
1079 */
1080void capture_headers(char *som, struct hdr_idx *idx,
1081 char **cap, struct cap_hdr *cap_hdr)
1082{
1083 char *eol, *sol, *col, *sov;
1084 int cur_idx;
1085 struct cap_hdr *h;
1086 int len;
1087
1088 sol = som + hdr_idx_first_pos(idx);
1089 cur_idx = hdr_idx_first_idx(idx);
1090
1091 while (cur_idx) {
1092 eol = sol + idx->v[cur_idx].len;
1093
1094 col = sol;
1095 while (col < eol && *col != ':')
1096 col++;
1097
1098 sov = col + 1;
1099 while (sov < eol && http_is_lws[(unsigned char)*sov])
1100 sov++;
1101
1102 for (h = cap_hdr; h; h = h->next) {
1103 if ((h->namelen == col - sol) &&
1104 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1105 if (cap[h->index] == NULL)
1106 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001107 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001108
1109 if (cap[h->index] == NULL) {
1110 Alert("HTTP capture : out of memory.\n");
1111 continue;
1112 }
1113
1114 len = eol - sov;
1115 if (len > h->len)
1116 len = h->len;
1117
1118 memcpy(cap[h->index], sov, len);
1119 cap[h->index][len]=0;
1120 }
1121 }
1122 sol = eol + idx->v[cur_idx].cr + 1;
1123 cur_idx = idx->v[cur_idx].next;
1124 }
1125}
1126
1127
Willy Tarreau42250582007-04-01 01:30:43 +02001128/* either we find an LF at <ptr> or we jump to <bad>.
1129 */
1130#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1131
1132/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1133 * otherwise to <http_msg_ood> with <state> set to <st>.
1134 */
1135#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1136 ptr++; \
1137 if (likely(ptr < end)) \
1138 goto good; \
1139 else { \
1140 state = (st); \
1141 goto http_msg_ood; \
1142 } \
1143 } while (0)
1144
1145
Willy Tarreaubaaee002006-06-26 02:48:02 +02001146/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001147 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001148 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1149 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1150 * will give undefined results.
1151 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1152 * and that msg->sol points to the beginning of the response.
1153 * If a complete line is found (which implies that at least one CR or LF is
1154 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1155 * returned indicating an incomplete line (which does not mean that parts have
1156 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1157 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1158 * upon next call.
1159 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001160 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001161 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1162 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001163 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001164 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001165const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1166 unsigned int state, const char *ptr, const char *end,
1167 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001168{
Willy Tarreau8973c702007-01-21 23:58:29 +01001169 switch (state) {
1170 http_msg_rpver:
1171 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001172 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001173 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1174
1175 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001176 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001177 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1178 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001179 state = HTTP_MSG_ERROR;
1180 break;
1181
Willy Tarreau8973c702007-01-21 23:58:29 +01001182 http_msg_rpver_sp:
1183 case HTTP_MSG_RPVER_SP:
1184 if (likely(!HTTP_IS_LWS(*ptr))) {
1185 msg->sl.st.c = ptr - msg_buf;
1186 goto http_msg_rpcode;
1187 }
1188 if (likely(HTTP_IS_SPHT(*ptr)))
1189 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1190 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001191 state = HTTP_MSG_ERROR;
1192 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001193
1194 http_msg_rpcode:
1195 case HTTP_MSG_RPCODE:
1196 if (likely(!HTTP_IS_LWS(*ptr)))
1197 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1198
1199 if (likely(HTTP_IS_SPHT(*ptr))) {
1200 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1201 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1202 }
1203
1204 /* so it's a CR/LF, so there is no reason phrase */
1205 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1206 http_msg_rsp_reason:
1207 /* FIXME: should we support HTTP responses without any reason phrase ? */
1208 msg->sl.st.r = ptr - msg_buf;
1209 msg->sl.st.r_l = 0;
1210 goto http_msg_rpline_eol;
1211
1212 http_msg_rpcode_sp:
1213 case HTTP_MSG_RPCODE_SP:
1214 if (likely(!HTTP_IS_LWS(*ptr))) {
1215 msg->sl.st.r = ptr - msg_buf;
1216 goto http_msg_rpreason;
1217 }
1218 if (likely(HTTP_IS_SPHT(*ptr)))
1219 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1220 /* so it's a CR/LF, so there is no reason phrase */
1221 goto http_msg_rsp_reason;
1222
1223 http_msg_rpreason:
1224 case HTTP_MSG_RPREASON:
1225 if (likely(!HTTP_IS_CRLF(*ptr)))
1226 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1227 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1228 http_msg_rpline_eol:
1229 /* We have seen the end of line. Note that we do not
1230 * necessarily have the \n yet, but at least we know that we
1231 * have EITHER \r OR \n, otherwise the response would not be
1232 * complete. We can then record the response length and return
1233 * to the caller which will be able to register it.
1234 */
1235 msg->sl.st.l = ptr - msg->sol;
1236 return ptr;
1237
1238#ifdef DEBUG_FULL
1239 default:
1240 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1241 exit(1);
1242#endif
1243 }
1244
1245 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001246 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001247 if (ret_state)
1248 *ret_state = state;
1249 if (ret_ptr)
1250 *ret_ptr = (char *)ptr;
1251 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001252}
1253
1254
1255/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001256 * This function parses a request line between <ptr> and <end>, starting with
1257 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1258 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1259 * will give undefined results.
1260 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1261 * and that msg->sol points to the beginning of the request.
1262 * If a complete line is found (which implies that at least one CR or LF is
1263 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1264 * returned indicating an incomplete line (which does not mean that parts have
1265 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1266 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1267 * upon next call.
1268 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001269 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001270 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1271 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001272 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001273 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001274const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1275 unsigned int state, const char *ptr, const char *end,
1276 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001277{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 switch (state) {
1279 http_msg_rqmeth:
1280 case HTTP_MSG_RQMETH:
1281 if (likely(HTTP_IS_TOKEN(*ptr)))
1282 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001285 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001286 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1287 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 if (likely(HTTP_IS_CRLF(*ptr))) {
1290 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001291 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001292 http_msg_req09_uri:
1293 msg->sl.rq.u = ptr - msg_buf;
1294 http_msg_req09_uri_e:
1295 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1296 http_msg_req09_ver:
1297 msg->sl.rq.v = ptr - msg_buf;
1298 msg->sl.rq.v_l = 0;
1299 goto http_msg_rqline_eol;
1300 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001301 state = HTTP_MSG_ERROR;
1302 break;
1303
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001304 http_msg_rqmeth_sp:
1305 case HTTP_MSG_RQMETH_SP:
1306 if (likely(!HTTP_IS_LWS(*ptr))) {
1307 msg->sl.rq.u = ptr - msg_buf;
1308 goto http_msg_rquri;
1309 }
1310 if (likely(HTTP_IS_SPHT(*ptr)))
1311 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1312 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1313 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001314
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001315 http_msg_rquri:
1316 case HTTP_MSG_RQURI:
1317 if (likely(!HTTP_IS_LWS(*ptr)))
1318 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 if (likely(HTTP_IS_SPHT(*ptr))) {
1321 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1322 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1323 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001324
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1326 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 http_msg_rquri_sp:
1329 case HTTP_MSG_RQURI_SP:
1330 if (likely(!HTTP_IS_LWS(*ptr))) {
1331 msg->sl.rq.v = ptr - msg_buf;
1332 goto http_msg_rqver;
1333 }
1334 if (likely(HTTP_IS_SPHT(*ptr)))
1335 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1336 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1337 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001338
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 http_msg_rqver:
1340 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001341 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001343
1344 if (likely(HTTP_IS_CRLF(*ptr))) {
1345 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1346 http_msg_rqline_eol:
1347 /* We have seen the end of line. Note that we do not
1348 * necessarily have the \n yet, but at least we know that we
1349 * have EITHER \r OR \n, otherwise the request would not be
1350 * complete. We can then record the request length and return
1351 * to the caller which will be able to register it.
1352 */
1353 msg->sl.rq.l = ptr - msg->sol;
1354 return ptr;
1355 }
1356
1357 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001358 state = HTTP_MSG_ERROR;
1359 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001360
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001361#ifdef DEBUG_FULL
1362 default:
1363 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1364 exit(1);
1365#endif
1366 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001367
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001368 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001369 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 if (ret_state)
1371 *ret_state = state;
1372 if (ret_ptr)
1373 *ret_ptr = (char *)ptr;
1374 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001375}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001376
1377
Willy Tarreau8973c702007-01-21 23:58:29 +01001378/*
1379 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001380 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001381 * when data are missing and recalled at the exact same location with no
1382 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001383 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1384 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001385 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1387{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001388 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001390
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001391 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 ptr = buf->lr;
1393 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 if (unlikely(ptr >= end))
1396 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001399 /*
1400 * First, states that are specific to the response only.
1401 * We check them first so that request and headers are
1402 * closer to each other (accessed more often).
1403 */
1404 http_msg_rpbefore:
1405 case HTTP_MSG_RPBEFORE:
1406 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau816b9792009-09-15 21:25:21 +02001407#if !defined(PARSE_PRESERVE_EMPTY_LINES)
1408 if (likely(ptr != buf->data)) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001409 /* Remove empty leading lines, as recommended by
1410 * RFC2616. This takes a lot of time because we
1411 * must move all the buffer backwards, but this
1412 * is rarely needed. The method above will be
1413 * cleaner when we'll be able to start sending
1414 * the request from any place in the buffer.
1415 */
Willy Tarreau816b9792009-09-15 21:25:21 +02001416 ptr += buffer_replace2(buf, buf->lr, ptr, NULL, 0);
Willy Tarreau8973c702007-01-21 23:58:29 +01001417 end = buf->r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001418 }
Willy Tarreau816b9792009-09-15 21:25:21 +02001419#endif
1420 msg->sol = ptr;
1421 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001422 hdr_idx_init(idx);
1423 state = HTTP_MSG_RPVER;
1424 goto http_msg_rpver;
1425 }
1426
1427 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1428 goto http_msg_invalid;
1429
1430 if (unlikely(*ptr == '\n'))
1431 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1432 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1433 /* stop here */
1434
1435 http_msg_rpbefore_cr:
1436 case HTTP_MSG_RPBEFORE_CR:
1437 EXPECT_LF_HERE(ptr, http_msg_invalid);
1438 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1439 /* stop here */
1440
1441 http_msg_rpver:
1442 case HTTP_MSG_RPVER:
1443 case HTTP_MSG_RPVER_SP:
1444 case HTTP_MSG_RPCODE:
1445 case HTTP_MSG_RPCODE_SP:
1446 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001447 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001448 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001449 if (unlikely(!ptr))
1450 return;
1451
1452 /* we have a full response and we know that we have either a CR
1453 * or an LF at <ptr>.
1454 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001455 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001456 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1457
1458 msg->sol = ptr;
1459 if (likely(*ptr == '\r'))
1460 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1461 goto http_msg_rpline_end;
1462
1463 http_msg_rpline_end:
1464 case HTTP_MSG_RPLINE_END:
1465 /* msg->sol must point to the first of CR or LF. */
1466 EXPECT_LF_HERE(ptr, http_msg_invalid);
1467 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1468 /* stop here */
1469
1470 /*
1471 * Second, states that are specific to the request only
1472 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 http_msg_rqbefore:
1474 case HTTP_MSG_RQBEFORE:
1475 if (likely(HTTP_IS_TOKEN(*ptr))) {
1476 if (likely(ptr == buf->data)) {
1477 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001478 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001479 } else {
1480#if PARSE_PRESERVE_EMPTY_LINES
1481 /* only skip empty leading lines, don't remove them */
1482 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001483 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001484#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 /* Remove empty leading lines, as recommended by
1486 * RFC2616. This takes a lot of time because we
1487 * must move all the buffer backwards, but this
1488 * is rarely needed. The method above will be
1489 * cleaner when we'll be able to start sending
1490 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001491 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 buf->lr = ptr;
1493 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001494 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495 msg->sol = buf->data;
1496 ptr = buf->data;
1497 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001498#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001500 /* we will need this when keep-alive will be supported
1501 hdr_idx_init(idx);
1502 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001503 state = HTTP_MSG_RQMETH;
1504 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001506
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1508 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001509
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 if (unlikely(*ptr == '\n'))
1511 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1512 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001513 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 http_msg_rqbefore_cr:
1516 case HTTP_MSG_RQBEFORE_CR:
1517 EXPECT_LF_HERE(ptr, http_msg_invalid);
1518 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001519 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001520
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 http_msg_rqmeth:
1522 case HTTP_MSG_RQMETH:
1523 case HTTP_MSG_RQMETH_SP:
1524 case HTTP_MSG_RQURI:
1525 case HTTP_MSG_RQURI_SP:
1526 case HTTP_MSG_RQVER:
1527 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001528 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 if (unlikely(!ptr))
1530 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001531
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 /* we have a full request and we know that we have either a CR
1533 * or an LF at <ptr>.
1534 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001535 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001537
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 msg->sol = ptr;
1539 if (likely(*ptr == '\r'))
1540 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001542
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 http_msg_rqline_end:
1544 case HTTP_MSG_RQLINE_END:
1545 /* check for HTTP/0.9 request : no version information available.
1546 * msg->sol must point to the first of CR or LF.
1547 */
1548 if (unlikely(msg->sl.rq.v_l == 0))
1549 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001550
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 EXPECT_LF_HERE(ptr, http_msg_invalid);
1552 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001553 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001554
Willy Tarreau8973c702007-01-21 23:58:29 +01001555 /*
1556 * Common states below
1557 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001558 http_msg_hdr_first:
1559 case HTTP_MSG_HDR_FIRST:
1560 msg->sol = ptr;
1561 if (likely(!HTTP_IS_CRLF(*ptr))) {
1562 goto http_msg_hdr_name;
1563 }
1564
1565 if (likely(*ptr == '\r'))
1566 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1567 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 http_msg_hdr_name:
1570 case HTTP_MSG_HDR_NAME:
1571 /* assumes msg->sol points to the first char */
1572 if (likely(HTTP_IS_TOKEN(*ptr)))
1573 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 if (likely(*ptr == ':')) {
1576 msg->col = ptr - buf->data;
1577 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1578 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001579
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001580 if (likely(msg->err_pos < -1) || *ptr == '\n')
1581 goto http_msg_invalid;
1582
1583 if (msg->err_pos == -1) /* capture error pointer */
1584 msg->err_pos = ptr - buf->data; /* >= 0 now */
1585
1586 /* and we still accept this non-token character */
1587 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001588
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 http_msg_hdr_l1_sp:
1590 case HTTP_MSG_HDR_L1_SP:
1591 /* assumes msg->sol points to the first char and msg->col to the colon */
1592 if (likely(HTTP_IS_SPHT(*ptr)))
1593 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001594
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001595 /* header value can be basically anything except CR/LF */
1596 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001597
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001598 if (likely(!HTTP_IS_CRLF(*ptr))) {
1599 goto http_msg_hdr_val;
1600 }
1601
1602 if (likely(*ptr == '\r'))
1603 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1604 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001605
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 http_msg_hdr_l1_lf:
1607 case HTTP_MSG_HDR_L1_LF:
1608 EXPECT_LF_HERE(ptr, http_msg_invalid);
1609 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001610
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 http_msg_hdr_l1_lws:
1612 case HTTP_MSG_HDR_L1_LWS:
1613 if (likely(HTTP_IS_SPHT(*ptr))) {
1614 /* replace HT,CR,LF with spaces */
1615 for (; buf->data+msg->sov < ptr; msg->sov++)
1616 buf->data[msg->sov] = ' ';
1617 goto http_msg_hdr_l1_sp;
1618 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001619 /* we had a header consisting only in spaces ! */
1620 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 goto http_msg_complete_header;
1622
1623 http_msg_hdr_val:
1624 case HTTP_MSG_HDR_VAL:
1625 /* assumes msg->sol points to the first char, msg->col to the
1626 * colon, and msg->sov points to the first character of the
1627 * value.
1628 */
1629 if (likely(!HTTP_IS_CRLF(*ptr)))
1630 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 msg->eol = ptr;
1633 /* Note: we could also copy eol into ->eoh so that we have the
1634 * real header end in case it ends with lots of LWS, but is this
1635 * really needed ?
1636 */
1637 if (likely(*ptr == '\r'))
1638 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1639 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001640
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 http_msg_hdr_l2_lf:
1642 case HTTP_MSG_HDR_L2_LF:
1643 EXPECT_LF_HERE(ptr, http_msg_invalid);
1644 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001645
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001646 http_msg_hdr_l2_lws:
1647 case HTTP_MSG_HDR_L2_LWS:
1648 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1649 /* LWS: replace HT,CR,LF with spaces */
1650 for (; msg->eol < ptr; msg->eol++)
1651 *msg->eol = ' ';
1652 goto http_msg_hdr_val;
1653 }
1654 http_msg_complete_header:
1655 /*
1656 * It was a new header, so the last one is finished.
1657 * Assumes msg->sol points to the first char, msg->col to the
1658 * colon, msg->sov points to the first character of the value
1659 * and msg->eol to the first CR or LF so we know how the line
1660 * ends. We insert last header into the index.
1661 */
1662 /*
1663 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1664 write(2, msg->sol, msg->eol-msg->sol);
1665 fprintf(stderr,"\n");
1666 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001667
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1669 idx, idx->tail) < 0))
1670 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001671
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001672 msg->sol = ptr;
1673 if (likely(!HTTP_IS_CRLF(*ptr))) {
1674 goto http_msg_hdr_name;
1675 }
1676
1677 if (likely(*ptr == '\r'))
1678 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1679 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001680
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 http_msg_last_lf:
1682 case HTTP_MSG_LAST_LF:
1683 /* Assumes msg->sol points to the first of either CR or LF */
1684 EXPECT_LF_HERE(ptr, http_msg_invalid);
1685 ptr++;
1686 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001687 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001689 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 return;
1691#ifdef DEBUG_FULL
1692 default:
1693 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1694 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001695#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001696 }
1697 http_msg_ood:
1698 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001699 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 buf->lr = ptr;
1701 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001702
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 http_msg_invalid:
1704 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001705 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001706 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 return;
1708}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001709
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001710/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1711 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1712 * nothing is done and 1 is returned.
1713 */
1714static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1715{
1716 int delta;
1717 char *cur_end;
1718
1719 if (msg->sl.rq.v_l != 0)
1720 return 1;
1721
1722 msg->sol = req->data + msg->som;
1723 cur_end = msg->sol + msg->sl.rq.l;
1724 delta = 0;
1725
1726 if (msg->sl.rq.u_l == 0) {
1727 /* if no URI was set, add "/" */
1728 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1729 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001730 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001731 }
1732 /* add HTTP version */
1733 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001734 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001735 cur_end += delta;
1736 cur_end = (char *)http_parse_reqline(msg, req->data,
1737 HTTP_MSG_RQMETH,
1738 msg->sol, cur_end + 1,
1739 NULL, NULL);
1740 if (unlikely(!cur_end))
1741 return 0;
1742
1743 /* we have a full HTTP/1.0 request now and we know that
1744 * we have either a CR or an LF at <ptr>.
1745 */
1746 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1747 return 1;
1748}
1749
Willy Tarreau5b154472009-12-21 20:11:07 +01001750/* Parse the Connection: headaer of an HTTP request, and set the transaction
1751 * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag
1752 * is also set so that we don't parse a second time. If some dangerous values
1753 * are encountered, we leave the status to indicate that the request might be
1754 * interpreted as keep-alive, but we also set the connection flags to indicate
1755 * that we WANT it to be a close, so that the header will be fixed. This
1756 * function should only be called when we know we're interested in checking
1757 * the request (not a CONNECT, and FE or BE mangles the header).
1758 */
1759void http_parse_connection_header(struct http_txn *txn)
1760{
1761 struct http_msg *msg = &txn->req;
1762 struct hdr_ctx ctx;
1763 int conn_cl, conn_ka;
1764
1765 if (txn->flags & TX_CON_HDR_PARS)
1766 return;
1767
1768 conn_cl = 0;
1769 conn_ka = 0;
1770 ctx.idx = 0;
1771
1772 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
1773 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
1774 conn_cl = 1;
1775 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
1776 conn_ka = 1;
1777 }
1778
1779 /* Determine if the client wishes keep-alive or close.
1780 * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections
1781 * are persistent unless "Connection: close" is explicitly specified.
1782 * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections.
1783 * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless
1784 * they explicitly specify "Connection: Keep-Alive", regardless of any
1785 * optional "Keep-Alive" header.
1786 * Note that if we find a request with both "Connection: close" and
1787 * "Connection: Keep-Alive", we indicate we want a close but don't have
1788 * it, so that it can be enforced later.
1789 */
1790
1791 if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */
1792 if (conn_cl) {
1793 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1794 if (!conn_ka)
1795 txn->flags |= TX_REQ_CONN_CLO;
1796 }
1797 } else { /* HTTP/1.0 */
1798 if (!conn_ka)
1799 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO | TX_REQ_CONN_CLO;
1800 else if (conn_cl)
1801 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1802 }
1803 txn->flags |= TX_CON_HDR_PARS;
1804}
1805
1806
Willy Tarreaud787e662009-07-07 10:14:51 +02001807/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1808 * processing can continue on next analysers, or zero if it either needs more
1809 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1810 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1811 * when it has nothing left to do, and may remove any analyser when it wants to
1812 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001813 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001814int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815{
Willy Tarreau59234e92008-11-30 23:51:27 +01001816 /*
1817 * We will parse the partial (or complete) lines.
1818 * We will check the request syntax, and also join multi-line
1819 * headers. An index of all the lines will be elaborated while
1820 * parsing.
1821 *
1822 * For the parsing, we use a 28 states FSM.
1823 *
1824 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01001825 * req->data + msg->som = beginning of request
Willy Tarreau59234e92008-11-30 23:51:27 +01001826 * req->data + req->eoh = end of processed headers / start of current one
1827 * req->data + req->eol = end of current header or line (LF or CRLF)
1828 * req->lr = first non-visited byte
1829 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02001830 *
1831 * At end of parsing, we may perform a capture of the error (if any), and
1832 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
1833 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1834 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001835 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001836
Willy Tarreau59234e92008-11-30 23:51:27 +01001837 int cur_idx;
1838 struct http_txn *txn = &s->txn;
1839 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001840 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001841
Willy Tarreau6bf17362009-02-24 10:48:35 +01001842 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1843 now_ms, __FUNCTION__,
1844 s,
1845 req,
1846 req->rex, req->wex,
1847 req->flags,
1848 req->l,
1849 req->analysers);
1850
Willy Tarreau52a0c602009-08-16 22:45:38 +02001851 /* we're speaking HTTP here, so let's speak HTTP to the client */
1852 s->srv_error = http_return_srv_error;
1853
Willy Tarreau59234e92008-11-30 23:51:27 +01001854 if (likely(req->lr < req->r))
1855 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001856
Willy Tarreau59234e92008-11-30 23:51:27 +01001857 /* 1: we might have to print this header in debug mode */
1858 if (unlikely((global.mode & MODE_DEBUG) &&
1859 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01001860 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001861 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001862
Willy Tarreau59234e92008-11-30 23:51:27 +01001863 sol = req->data + msg->som;
1864 eol = sol + msg->sl.rq.l;
1865 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001866
Willy Tarreau59234e92008-11-30 23:51:27 +01001867 sol += hdr_idx_first_pos(&txn->hdr_idx);
1868 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001869
Willy Tarreau59234e92008-11-30 23:51:27 +01001870 while (cur_idx) {
1871 eol = sol + txn->hdr_idx.v[cur_idx].len;
1872 debug_hdr("clihdr", s, sol, eol);
1873 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1874 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001875 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001876 }
1877
Willy Tarreau58f10d72006-12-04 02:26:12 +01001878
Willy Tarreau59234e92008-11-30 23:51:27 +01001879 /*
1880 * Now we quickly check if we have found a full valid request.
1881 * If not so, we check the FD and buffer states before leaving.
1882 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001883 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01001884 * requests are checked first.
1885 *
1886 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001887
Willy Tarreau655dce92009-11-08 13:10:58 +01001888 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001889 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001890 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001891 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001892 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
1893 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001894
Willy Tarreau59234e92008-11-30 23:51:27 +01001895 /* 1: Since we are in header mode, if there's no space
1896 * left for headers, we won't be able to free more
1897 * later, so the session will never terminate. We
1898 * must terminate it now.
1899 */
1900 if (unlikely(req->flags & BF_FULL)) {
1901 /* FIXME: check if URI is set and return Status
1902 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001903 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001904 goto return_bad_req;
1905 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001906
Willy Tarreau59234e92008-11-30 23:51:27 +01001907 /* 2: have we encountered a read error ? */
1908 else if (req->flags & BF_READ_ERROR) {
1909 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02001910 if (msg->err_pos >= 0)
1911 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001912 msg->msg_state = HTTP_MSG_ERROR;
1913 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001914
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001915 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001916 if (s->listener->counters)
1917 s->listener->counters->failed_req++;
1918
Willy Tarreau59234e92008-11-30 23:51:27 +01001919 if (!(s->flags & SN_ERR_MASK))
1920 s->flags |= SN_ERR_CLICL;
1921 if (!(s->flags & SN_FINST_MASK))
1922 s->flags |= SN_FINST_R;
1923 return 0;
1924 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001925
Willy Tarreau59234e92008-11-30 23:51:27 +01001926 /* 3: has the read timeout expired ? */
1927 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
1928 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02001929 if (msg->err_pos >= 0)
1930 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001931 txn->status = 408;
1932 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
1933 msg->msg_state = HTTP_MSG_ERROR;
1934 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001935
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001936 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001937 if (s->listener->counters)
1938 s->listener->counters->failed_req++;
1939
Willy Tarreau59234e92008-11-30 23:51:27 +01001940 if (!(s->flags & SN_ERR_MASK))
1941 s->flags |= SN_ERR_CLITO;
1942 if (!(s->flags & SN_FINST_MASK))
1943 s->flags |= SN_FINST_R;
1944 return 0;
1945 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001946
Willy Tarreau59234e92008-11-30 23:51:27 +01001947 /* 4: have we encountered a close ? */
1948 else if (req->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02001949 if (msg->err_pos >= 0)
1950 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001951 txn->status = 400;
1952 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1953 msg->msg_state = HTTP_MSG_ERROR;
1954 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001955
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001956 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001957 if (s->listener->counters)
1958 s->listener->counters->failed_req++;
1959
Willy Tarreau59234e92008-11-30 23:51:27 +01001960 if (!(s->flags & SN_ERR_MASK))
1961 s->flags |= SN_ERR_CLICL;
1962 if (!(s->flags & SN_FINST_MASK))
1963 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001964 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001965 }
1966
Willy Tarreau520d95e2009-09-19 21:04:57 +02001967 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001968 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
1969
Willy Tarreau59234e92008-11-30 23:51:27 +01001970 /* just set the request timeout once at the beginning of the request */
1971 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02001972 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001973
Willy Tarreau59234e92008-11-30 23:51:27 +01001974 /* we're not ready yet */
1975 return 0;
1976 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001977
Willy Tarreaud787e662009-07-07 10:14:51 +02001978 /* OK now we have a complete HTTP request with indexed headers. Let's
1979 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01001980 * later. At this point, we have the last CRLF at req->data + msg->eoh.
1981 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
1982 * points to the CRLF of the request line. req->lr points to the first
1983 * byte after the last LF. msg->col and msg->sov point to the first
1984 * byte of data. msg->eol cannot be trusted because it may have been
1985 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001986 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001987
Willy Tarreaud787e662009-07-07 10:14:51 +02001988 /* Maybe we found in invalid header name while we were configured not
1989 * to block on that, so we have to capture it now.
1990 */
1991 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02001992 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
1993
Willy Tarreau59234e92008-11-30 23:51:27 +01001994 /* ensure we keep this pointer to the beginning of the message */
1995 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001996
Willy Tarreau59234e92008-11-30 23:51:27 +01001997 /*
1998 * 1: identify the method
1999 */
2000 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
2001
2002 /* we can make use of server redirect on GET and HEAD */
2003 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2004 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002005
Willy Tarreau59234e92008-11-30 23:51:27 +01002006 /*
2007 * 2: check if the URI matches the monitor_uri.
2008 * We have to do this for every request which gets in, because
2009 * the monitor-uri is defined by the frontend.
2010 */
2011 if (unlikely((s->fe->monitor_uri_len != 0) &&
2012 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2013 !memcmp(&req->data[msg->sl.rq.u],
2014 s->fe->monitor_uri,
2015 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002016 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002017 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002018 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002019 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002020
Willy Tarreau59234e92008-11-30 23:51:27 +01002021 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002022
Willy Tarreau59234e92008-11-30 23:51:27 +01002023 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002024 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2025 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002026
Willy Tarreau59234e92008-11-30 23:51:27 +01002027 ret = acl_pass(ret);
2028 if (cond->pol == ACL_COND_UNLESS)
2029 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002030
Willy Tarreau59234e92008-11-30 23:51:27 +01002031 if (ret) {
2032 /* we fail this request, let's return 503 service unavail */
2033 txn->status = 503;
2034 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2035 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002036 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002037 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002038
Willy Tarreau59234e92008-11-30 23:51:27 +01002039 /* nothing to fail, let's reply normaly */
2040 txn->status = 200;
2041 stream_int_retnclose(req->prod, &http_200_chunk);
2042 goto return_prx_cond;
2043 }
2044
2045 /*
2046 * 3: Maybe we have to copy the original REQURI for the logs ?
2047 * Note: we cannot log anymore if the request has been
2048 * classified as invalid.
2049 */
2050 if (unlikely(s->logs.logwait & LW_REQ)) {
2051 /* we have a complete HTTP request that we must log */
2052 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2053 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002054
Willy Tarreau59234e92008-11-30 23:51:27 +01002055 if (urilen >= REQURI_LEN)
2056 urilen = REQURI_LEN - 1;
2057 memcpy(txn->uri, &req->data[msg->som], urilen);
2058 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002059
Willy Tarreau59234e92008-11-30 23:51:27 +01002060 if (!(s->logs.logwait &= ~LW_REQ))
2061 s->do_log(s);
2062 } else {
2063 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002064 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002065 }
Willy Tarreau06619262006-12-17 08:37:22 +01002066
Willy Tarreau59234e92008-11-30 23:51:27 +01002067 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002068 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2069 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002070
Willy Tarreau5b154472009-12-21 20:11:07 +01002071 /* ... and check if the request is HTTP/1.1 or above */
2072 if ((msg->sl.rq.v_l == 8) &&
2073 ((req->data[msg->som + msg->sl.rq.v + 5] > '1') ||
2074 ((req->data[msg->som + msg->sl.rq.v + 5] == '1') &&
2075 (req->data[msg->som + msg->sl.rq.v + 7] >= '1'))))
2076 txn->flags |= TX_REQ_VER_11;
2077
2078 /* "connection" has not been parsed yet */
2079 txn->flags &= ~TX_CON_HDR_PARS;
2080
Willy Tarreau59234e92008-11-30 23:51:27 +01002081 /* 5: we may need to capture headers */
2082 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
2083 capture_headers(req->data + msg->som, &txn->hdr_idx,
2084 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002085
Willy Tarreau32b47f42009-10-18 20:55:02 +02002086 /* 6: determine if we have a transfer-encoding or content-length.
2087 * RFC2616 #4.4 states that :
2088 * - If a Transfer-Encoding header field is present and has any value
2089 * other than "identity", then the transfer-length is defined by use
2090 * of the "chunked" transfer-coding ;
2091 *
2092 * - If a Content-Length header field is present, its decimal value in
2093 * OCTETs represents both the entity-length and the transfer-length.
2094 * If a message is received with both a Transfer-Encoding header
2095 * field and a Content-Length header field, the latter MUST be ignored.
2096 *
2097 * - If a request contains a message-body and a Content-Length is not
2098 * given, the server SHOULD respond with 400 (bad request) if it
2099 * cannot determine the length of the message, or with 411 (length
2100 * required) if it wishes to insist on receiving a valid Content-Length.
2101 */
2102
Willy Tarreau32b47f42009-10-18 20:55:02 +02002103 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002104 while ((txn->flags & TX_REQ_VER_11) &&
2105 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002106 if (ctx.vlen == 8 && strncasecmp(ctx.line + ctx.val, "identity", 8) == 0)
2107 continue;
2108 txn->flags |= TX_REQ_TE_CHNK;
2109 break;
2110 }
2111
2112 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
2113 ctx.idx = 0;
2114 while (!(txn->flags & TX_REQ_TE_CHNK) &&
2115 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2116 signed long long cl;
2117
2118 if (!ctx.vlen)
2119 goto return_bad_req;
2120
2121 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2122 goto return_bad_req; /* parse failure */
2123
2124 if (cl < 0)
2125 goto return_bad_req;
2126
2127 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2128 goto return_bad_req; /* already specified, was different */
2129
2130 txn->flags |= TX_REQ_CNT_LEN;
2131 msg->hdr_content_len = cl;
2132 }
2133
Willy Tarreaud787e662009-07-07 10:14:51 +02002134 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002135 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002136 req->analyse_exp = TICK_ETERNITY;
2137 return 1;
2138
2139 return_bad_req:
2140 /* We centralize bad requests processing here */
2141 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2142 /* we detected a parsing error. We want to archive this request
2143 * in the dedicated proxy area for later troubleshooting.
2144 */
2145 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2146 }
2147
2148 txn->req.msg_state = HTTP_MSG_ERROR;
2149 txn->status = 400;
2150 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002151
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002152 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002153 if (s->listener->counters)
2154 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002155
2156 return_prx_cond:
2157 if (!(s->flags & SN_ERR_MASK))
2158 s->flags |= SN_ERR_PRXCOND;
2159 if (!(s->flags & SN_FINST_MASK))
2160 s->flags |= SN_FINST_R;
2161
2162 req->analysers = 0;
2163 req->analyse_exp = TICK_ETERNITY;
2164 return 0;
2165}
2166
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002167/* This stream analyser runs all HTTP request processing which is common to
2168 * frontends and backends, which means blocking ACLs, filters, connection-close,
2169 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002170 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002171 * either needs more data or wants to immediately abort the request (eg: deny,
2172 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002173 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002174int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002175{
Willy Tarreaud787e662009-07-07 10:14:51 +02002176 struct http_txn *txn = &s->txn;
2177 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002178 struct acl_cond *cond;
2179 struct redirect_rule *rule;
2180 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002181
Willy Tarreau655dce92009-11-08 13:10:58 +01002182 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002183 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002184 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002185 return 0;
2186 }
2187
Willy Tarreau3a816292009-07-07 10:55:49 +02002188 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002189 req->analyse_exp = TICK_ETERNITY;
2190
2191 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2192 now_ms, __FUNCTION__,
2193 s,
2194 req,
2195 req->rex, req->wex,
2196 req->flags,
2197 req->l,
2198 req->analysers);
2199
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002200 /* first check whether we have some ACLs set to block this request */
2201 list_for_each_entry(cond, &px->block_cond, list) {
2202 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002203
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002204 ret = acl_pass(ret);
2205 if (cond->pol == ACL_COND_UNLESS)
2206 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002207
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002208 if (ret) {
2209 txn->status = 403;
2210 /* let's log the request time */
2211 s->logs.tv_request = now;
2212 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2213 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002214 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002215 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002216
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002217 /* try headers filters */
2218 if (px->req_exp != NULL) {
2219 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2220 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002221
Willy Tarreau59234e92008-11-30 23:51:27 +01002222 /* has the request been denied ? */
2223 if (txn->flags & TX_CLDENY) {
2224 /* no need to go further */
2225 txn->status = 403;
2226 /* let's log the request time */
2227 s->logs.tv_request = now;
2228 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2229 goto return_prx_cond;
2230 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002231
2232 /* When a connection is tarpitted, we use the tarpit timeout,
2233 * which may be the same as the connect timeout if unspecified.
2234 * If unset, then set it to zero because we really want it to
2235 * eventually expire. We build the tarpit as an analyser.
2236 */
2237 if (txn->flags & TX_CLTARPIT) {
2238 buffer_erase(s->req);
2239 /* wipe the request out so that we can drop the connection early
2240 * if the client closes first.
2241 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002242 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002243 req->analysers = 0; /* remove switching rules etc... */
2244 req->analysers |= AN_REQ_HTTP_TARPIT;
2245 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2246 if (!req->analyse_exp)
2247 req->analyse_exp = tick_add(now_ms, 0);
2248 return 1;
2249 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002250 }
Willy Tarreau06619262006-12-17 08:37:22 +01002251
Willy Tarreau5b154472009-12-21 20:11:07 +01002252 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2253 * only change if both the request and the config reference something else.
Willy Tarreau42736642009-10-18 21:04:35 +02002254 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002255
2256 if ((s->txn.meth != HTTP_METH_CONNECT) &&
2257 ((s->fe->options|s->be->options) & (PR_O_KEEPALIVE|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2258 int tmp = TX_CON_WANT_TUN;
2259 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2260 tmp = TX_CON_WANT_KAL;
2261 /* FIXME: for now, we don't support server-close mode */
2262 if ((s->fe->options|s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))
2263 tmp = TX_CON_WANT_CLO;
2264
2265 if (!(txn->flags & TX_CON_HDR_PARS))
2266 http_parse_connection_header(txn);
2267
2268 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2269 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2270 }
2271
2272 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2273 * once we know the backend, because the tunnel mode can be implied by the
2274 * lack of any close/keepalive options in both the FE and the BE. Since
2275 * this information can evolve with time, we proceed by trying to make the
2276 * header status match the desired status. For this, we'll have to adjust
2277 * the "Connection" header. The test for persistent connections has already
2278 * been performed, so we only enter here if there is a risk the connection
2279 * is considered as persistent and we want it to be closed on the server
2280 * side. It would be nice if we could enter this place only when a
2281 * Connection header exists. Note that a CONNECT method will not enter
2282 * here.
2283 */
2284 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002285 char *cur_ptr, *cur_end, *cur_next;
2286 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002287 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002288 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002289
Willy Tarreau5b154472009-12-21 20:11:07 +01002290 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002291 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002292
Willy Tarreau5b154472009-12-21 20:11:07 +01002293 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002294 cur_hdr = &txn->hdr_idx.v[cur_idx];
2295 cur_ptr = cur_next;
2296 cur_end = cur_ptr + cur_hdr->len;
2297 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002298
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002299 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002300 if (!val)
2301 continue;
2302
2303 /* 3 possibilities :
2304 * - we have already set "Connection: close" or we're in
2305 * HTTP/1.0, so we remove this line.
2306 * - we have not yet set "Connection: close", but this line
2307 * indicates close. We leave it untouched and set the flag.
2308 * - we have not yet set "Connection: close", and this line
2309 * indicates non-close. We replace it and set the flag.
2310 */
2311 if (must_delete) {
2312 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2313 http_msg_move_end(&txn->req, delta);
2314 cur_next += delta;
2315 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2316 txn->hdr_idx.used--;
2317 cur_hdr->len = 0;
2318 txn->flags |= TX_REQ_CONN_CLO;
2319 } else {
2320 if (cur_end - cur_ptr - val != 5 ||
2321 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2322 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2323 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002324 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002325 cur_hdr->len += delta;
2326 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002327 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002328 txn->flags |= TX_REQ_CONN_CLO;
2329 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002330 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002331 } /* for loop */
2332 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002333
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002334 /* add request headers from the rule sets in the same order */
2335 for (cur_idx = 0; cur_idx < px->nb_reqadd; cur_idx++) {
2336 if (unlikely(http_header_add_tail(req,
2337 &txn->req,
2338 &txn->hdr_idx,
2339 px->req_add[cur_idx])) < 0)
2340 goto return_bad_req;
2341 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002342
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002343 /* check if stats URI was requested, and if an auth is needed */
2344 if (px->uri_auth != NULL &&
2345 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2346 /* we have to check the URI and auth for this request.
2347 * FIXME!!! that one is rather dangerous, we want to
2348 * make it follow standard rules (eg: clear req->analysers).
2349 */
2350 if (stats_check_uri_auth(s, px)) {
2351 req->analysers = 0;
2352 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002354 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002355
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002356 /* check whether we have some ACLs set to redirect this request */
2357 list_for_each_entry(rule, &px->redirect_rules, list) {
2358 int ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreau06b917c2009-07-06 16:34:52 +02002359
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002360 ret = acl_pass(ret);
2361 if (rule->cond->pol == ACL_COND_UNLESS)
2362 ret = !ret;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002363
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002364 if (ret) {
2365 struct chunk rdr = { trash, 0 };
2366 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002367
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002368 /* build redirect message */
2369 switch(rule->code) {
2370 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002371 msg_fmt = HTTP_303;
2372 break;
2373 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002374 msg_fmt = HTTP_301;
2375 break;
2376 case 302:
2377 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002378 msg_fmt = HTTP_302;
2379 break;
2380 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002381
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002382 if (unlikely(chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002383 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002384
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002385 switch(rule->type) {
2386 case REDIRECT_TYPE_PREFIX: {
2387 const char *path;
2388 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002389
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002390 path = http_get_path(txn);
2391 /* build message using path */
2392 if (path) {
2393 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2394 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2395 int qs = 0;
2396 while (qs < pathlen) {
2397 if (path[qs] == '?') {
2398 pathlen = qs;
2399 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002400 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002401 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002402 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002403 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002404 } else {
2405 path = "/";
2406 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002407 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002408
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002409 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002410 goto return_bad_req;
2411
2412 /* add prefix. Note that if prefix == "/", we don't want to
2413 * add anything, otherwise it makes it hard for the user to
2414 * configure a self-redirection.
2415 */
2416 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002417 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2418 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002419 }
2420
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002421 /* add path */
2422 memcpy(rdr.str + rdr.len, path, pathlen);
2423 rdr.len += pathlen;
2424 break;
2425 }
2426 case REDIRECT_TYPE_LOCATION:
2427 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002428 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002429 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002430
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002431 /* add location */
2432 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2433 rdr.len += rule->rdr_len;
2434 break;
2435 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002436
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002437 if (rule->cookie_len) {
2438 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2439 rdr.len += 14;
2440 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2441 rdr.len += rule->cookie_len;
2442 memcpy(rdr.str + rdr.len, "\r\n", 2);
2443 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002444 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002445
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002446 /* add end of headers */
2447 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2448 rdr.len += 4;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002449
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002450 txn->status = rule->code;
2451 /* let's log the request time */
2452 s->logs.tv_request = now;
2453 stream_int_retnclose(req->prod, &rdr);
2454 goto return_prx_cond;
2455 }
2456 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002457
Willy Tarreau5b154472009-12-21 20:11:07 +01002458 /* We can shut read side if "connection: close" && no-data && !tunnel && !abort_on_close */
2459 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO &&
2460 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2461 (txn->meth != HTTP_METH_CONNECT) &&
2462 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau349a0f62009-10-18 21:17:42 +02002463 req->flags |= BF_DONT_READ;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +02002464
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002465 /* that's OK for us now, let's move on to next analysers */
2466 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002467
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002468 return_bad_req:
2469 /* We centralize bad requests processing here */
2470 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2471 /* we detected a parsing error. We want to archive this request
2472 * in the dedicated proxy area for later troubleshooting.
2473 */
2474 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2475 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002476
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002477 txn->req.msg_state = HTTP_MSG_ERROR;
2478 txn->status = 400;
2479 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002480
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002481 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002482 if (s->listener->counters)
2483 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002484
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002485 return_prx_cond:
2486 if (!(s->flags & SN_ERR_MASK))
2487 s->flags |= SN_ERR_PRXCOND;
2488 if (!(s->flags & SN_FINST_MASK))
2489 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002490
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002491 req->analysers = 0;
2492 req->analyse_exp = TICK_ETERNITY;
2493 return 0;
2494}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002495
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002496/* This function performs all the processing enabled for the current request.
2497 * It returns 1 if the processing can continue on next analysers, or zero if it
2498 * needs more data, encounters an error, or wants to immediately abort the
2499 * request. It relies on buffers flags, and updates s->req->analysers.
2500 */
2501int http_process_request(struct session *s, struct buffer *req, int an_bit)
2502{
2503 struct http_txn *txn = &s->txn;
2504 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002505
Willy Tarreau655dce92009-11-08 13:10:58 +01002506 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002507 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002508 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002509 return 0;
2510 }
2511
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002512 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2513 now_ms, __FUNCTION__,
2514 s,
2515 req,
2516 req->rex, req->wex,
2517 req->flags,
2518 req->l,
2519 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002520
Willy Tarreau59234e92008-11-30 23:51:27 +01002521 /*
2522 * Right now, we know that we have processed the entire headers
2523 * and that unwanted requests have been filtered out. We can do
2524 * whatever we want with the remaining request. Also, now we
2525 * may have separate values for ->fe, ->be.
2526 */
Willy Tarreau06619262006-12-17 08:37:22 +01002527
Willy Tarreau59234e92008-11-30 23:51:27 +01002528 /*
2529 * If HTTP PROXY is set we simply get remote server address
2530 * parsing incoming request.
2531 */
2532 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2533 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2534 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002535
Willy Tarreau59234e92008-11-30 23:51:27 +01002536 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002537 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002538 * Note that doing so might move headers in the request, but
2539 * the fields will stay coherent and the URI will not move.
2540 * This should only be performed in the backend.
2541 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002542 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002543 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2544 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002545
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002547 * 8: the appsession cookie was looked up very early in 1.2,
2548 * so let's do the same now.
2549 */
2550
2551 /* It needs to look into the URI */
2552 if ((s->sessid == NULL) && s->be->appsession_name) {
2553 get_srv_from_appsession(s, &req->data[msg->som + msg->sl.rq.u], msg->sl.rq.u_l);
2554 }
2555
2556 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 * 9: add X-Forwarded-For if either the frontend or the backend
2558 * asks for it.
2559 */
2560 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2561 if (s->cli_addr.ss_family == AF_INET) {
2562 /* Add an X-Forwarded-For header unless the source IP is
2563 * in the 'except' network range.
2564 */
2565 if ((!s->fe->except_mask.s_addr ||
2566 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2567 != s->fe->except_net.s_addr) &&
2568 (!s->be->except_mask.s_addr ||
2569 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2570 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002571 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 unsigned char *pn;
2573 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002574
2575 /* Note: we rely on the backend to get the header name to be used for
2576 * x-forwarded-for, because the header is really meant for the backends.
2577 * However, if the backend did not specify any option, we have to rely
2578 * on the frontend's header name.
2579 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002580 if (s->be->fwdfor_hdr_len) {
2581 len = s->be->fwdfor_hdr_len;
2582 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002583 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002584 len = s->fe->fwdfor_hdr_len;
2585 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002586 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002587 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002588
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002589 if (unlikely(http_header_add_tail2(req, &txn->req,
2590 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002591 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002592 }
2593 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002594 else if (s->cli_addr.ss_family == AF_INET6) {
2595 /* FIXME: for the sake of completeness, we should also support
2596 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002597 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002598 int len;
2599 char pn[INET6_ADDRSTRLEN];
2600 inet_ntop(AF_INET6,
2601 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2602 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002603
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 /* Note: we rely on the backend to get the header name to be used for
2605 * x-forwarded-for, because the header is really meant for the backends.
2606 * However, if the backend did not specify any option, we have to rely
2607 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002608 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002609 if (s->be->fwdfor_hdr_len) {
2610 len = s->be->fwdfor_hdr_len;
2611 memcpy(trash, s->be->fwdfor_hdr_name, len);
2612 } else {
2613 len = s->fe->fwdfor_hdr_len;
2614 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002615 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002616 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002617
Willy Tarreau59234e92008-11-30 23:51:27 +01002618 if (unlikely(http_header_add_tail2(req, &txn->req,
2619 &txn->hdr_idx, trash, len)) < 0)
2620 goto return_bad_req;
2621 }
2622 }
2623
2624 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002625 * 10: add X-Original-To if either the frontend or the backend
2626 * asks for it.
2627 */
2628 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
2629
2630 /* FIXME: don't know if IPv6 can handle that case too. */
2631 if (s->cli_addr.ss_family == AF_INET) {
2632 /* Add an X-Original-To header unless the destination IP is
2633 * in the 'except' network range.
2634 */
2635 if (!(s->flags & SN_FRT_ADDR_SET))
2636 get_frt_addr(s);
2637
2638 if ((!s->fe->except_mask_to.s_addr ||
2639 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
2640 != s->fe->except_to.s_addr) &&
2641 (!s->be->except_mask_to.s_addr ||
2642 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
2643 != s->be->except_to.s_addr)) {
2644 int len;
2645 unsigned char *pn;
2646 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
2647
2648 /* Note: we rely on the backend to get the header name to be used for
2649 * x-original-to, because the header is really meant for the backends.
2650 * However, if the backend did not specify any option, we have to rely
2651 * on the frontend's header name.
2652 */
2653 if (s->be->orgto_hdr_len) {
2654 len = s->be->orgto_hdr_len;
2655 memcpy(trash, s->be->orgto_hdr_name, len);
2656 } else {
2657 len = s->fe->orgto_hdr_len;
2658 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002659 }
Maik Broemme2850cb42009-04-17 18:53:21 +02002660 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
2661
2662 if (unlikely(http_header_add_tail2(req, &txn->req,
2663 &txn->hdr_idx, trash, len)) < 0)
2664 goto return_bad_req;
2665 }
2666 }
2667 }
2668
Willy Tarreau78599912009-10-17 20:12:21 +02002669 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau5b154472009-12-21 20:11:07 +01002670 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau78599912009-10-17 20:12:21 +02002671 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 "Connection: close", 17)) < 0)
2673 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01002674 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01002675 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01002676
2677 /* If we have no server assigned yet and we're balancing on url_param
2678 * with a POST request, we may be interested in checking the body for
2679 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 */
2681 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2682 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01002683 s->be->url_param_post_limit != 0 &&
2684 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01002686 buffer_dont_connect(req);
2687 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01002688 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002689
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 /*************************************************************
2691 * OK, that's finished for the headers. We have done what we *
2692 * could. Let's switch to the DATA state. *
2693 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01002694 req->analyse_exp = TICK_ETERNITY;
2695 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002696
Willy Tarreau59234e92008-11-30 23:51:27 +01002697 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01002698 /* OK let's go on with the BODY now */
2699 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002700
Willy Tarreau59234e92008-11-30 23:51:27 +01002701 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02002702 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002703 /* we detected a parsing error. We want to archive this request
2704 * in the dedicated proxy area for later troubleshooting.
2705 */
Willy Tarreau4076a152009-04-02 15:18:36 +02002706 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01002707 }
Willy Tarreau4076a152009-04-02 15:18:36 +02002708
Willy Tarreau59234e92008-11-30 23:51:27 +01002709 txn->req.msg_state = HTTP_MSG_ERROR;
2710 txn->status = 400;
2711 req->analysers = 0;
2712 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002713
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002714 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002715 if (s->listener->counters)
2716 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002717
Willy Tarreau59234e92008-11-30 23:51:27 +01002718 if (!(s->flags & SN_ERR_MASK))
2719 s->flags |= SN_ERR_PRXCOND;
2720 if (!(s->flags & SN_FINST_MASK))
2721 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002722 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002723}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002724
Willy Tarreau60b85b02008-11-30 23:28:40 +01002725/* This function is an analyser which processes the HTTP tarpit. It always
2726 * returns zero, at the beginning because it prevents any other processing
2727 * from occurring, and at the end because it terminates the request.
2728 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002729int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01002730{
2731 struct http_txn *txn = &s->txn;
2732
2733 /* This connection is being tarpitted. The CLIENT side has
2734 * already set the connect expiration date to the right
2735 * timeout. We just have to check that the client is still
2736 * there and that the timeout has not expired.
2737 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002738 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01002739 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
2740 !tick_is_expired(req->analyse_exp, now_ms))
2741 return 0;
2742
2743 /* We will set the queue timer to the time spent, just for
2744 * logging purposes. We fake a 500 server error, so that the
2745 * attacker will not suspect his connection has been tarpitted.
2746 * It will not cause trouble to the logs because we can exclude
2747 * the tarpitted connections by filtering on the 'PT' status flags.
2748 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01002749 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2750
2751 txn->status = 500;
2752 if (req->flags != BF_READ_ERROR)
2753 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
2754
2755 req->analysers = 0;
2756 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002757
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002758 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002759 if (s->listener->counters)
2760 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01002761
Willy Tarreau60b85b02008-11-30 23:28:40 +01002762 if (!(s->flags & SN_ERR_MASK))
2763 s->flags |= SN_ERR_PRXCOND;
2764 if (!(s->flags & SN_FINST_MASK))
2765 s->flags |= SN_FINST_T;
2766 return 0;
2767}
2768
Willy Tarreaud34af782008-11-30 23:36:37 +01002769/* This function is an analyser which processes the HTTP request body. It looks
2770 * for parameters to be used for the load balancing algorithm (url_param). It
2771 * must only be called after the standard HTTP request processing has occurred,
2772 * because it expects the request to be parsed. It returns zero if it needs to
2773 * read more data, or 1 once it has completed its analysis.
2774 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002775int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01002776{
Willy Tarreau522d6c02009-12-06 18:49:18 +01002777 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01002778 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01002779 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01002780
2781 /* We have to parse the HTTP request body to find any required data.
2782 * "balance url_param check_post" should have been the only way to get
2783 * into this. We were brought here after HTTP header analysis, so all
2784 * related structures are ready.
2785 */
2786
Willy Tarreau522d6c02009-12-06 18:49:18 +01002787 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
2788 goto missing_data;
2789
2790 if (msg->msg_state < HTTP_MSG_100_SENT) {
2791 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2792 * send an HTTP/1.1 100 Continue intermediate response.
2793 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002794 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01002795 struct hdr_ctx ctx;
2796 ctx.idx = 0;
2797 /* Expect is allowed in 1.1, look for it */
2798 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
2799 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2800 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
2801 }
2802 }
2803 msg->msg_state = HTTP_MSG_100_SENT;
2804 }
2805
2806 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
2807 if (txn->flags & TX_REQ_TE_CHNK)
2808 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2809 else
2810 msg->msg_state = HTTP_MSG_DATA;
2811 }
2812
2813 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
2814 unsigned long body = msg->col;
Willy Tarreaud34af782008-11-30 23:36:37 +01002815 unsigned int chunk = 0;
Willy Tarreau522d6c02009-12-06 18:49:18 +01002816
Willy Tarreaud34af782008-11-30 23:36:37 +01002817 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01002818 int c = hex2i(msg->sol[body]);
2819 if (c < 0)
2820 goto return_bad_req;
2821 chunk = (chunk << 4) + c;
Willy Tarreaud34af782008-11-30 23:36:37 +01002822 body++;
2823 }
Willy Tarreaud34af782008-11-30 23:36:37 +01002824
Willy Tarreau522d6c02009-12-06 18:49:18 +01002825 /* now we want CRLF */
2826 if (body + 2 >= req->l) /* we want CRLF too */
2827 goto missing_data; /* end of buffer? data missing! */
Willy Tarreaud34af782008-11-30 23:36:37 +01002828
Willy Tarreau522d6c02009-12-06 18:49:18 +01002829 if ((msg->sol[body++] != '\r') || (msg->sol[body++] != '\n'))
2830 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01002831
Willy Tarreau522d6c02009-12-06 18:49:18 +01002832 msg->sov = body;
2833 txn->req.hdr_content_len = chunk;
2834 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud34af782008-11-30 23:36:37 +01002835 }
2836
Willy Tarreau522d6c02009-12-06 18:49:18 +01002837 /* Now we're in HTTP_MSG_DATA state.
2838 * We have the first non-header byte in msg->col, which is either the
2839 * beginning of the chunk size or of the data. The first data byte is in
2840 * msg->sov, which is equal to msg->col when not using transfer-encoding.
2841 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01002842 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01002843
2844 if (msg->hdr_content_len < limit)
2845 limit = msg->hdr_content_len;
2846
2847 if (req->l - msg->sov >= limit) /* we have enough bytes now */
2848 goto http_end;
2849
2850 missing_data:
2851 /* we get here if we need to wait for more data */
2852 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
2853 txn->status = 408;
2854 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2855 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01002856 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01002857
2858 /* we get here if we need to wait for more data */
2859 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01002860 /* Not enough data. We'll re-use the http-request
2861 * timeout here. Ideally, we should set the timeout
2862 * relative to the accept() date. We just set the
2863 * request timeout once at the beginning of the
2864 * request.
2865 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002866 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01002867 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002868 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01002869 return 0;
2870 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01002871
2872 http_end:
2873 /* The situation will not evolve, so let's give up on the analysis. */
2874 s->logs.tv_request = now; /* update the request timer to reflect full request */
2875 req->analysers &= ~an_bit;
2876 req->analyse_exp = TICK_ETERNITY;
2877 return 1;
2878
2879 return_bad_req: /* let's centralize all bad requests */
2880 txn->req.msg_state = HTTP_MSG_ERROR;
2881 txn->status = 400;
2882 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2883
2884 return_err_msg:
2885 req->analysers = 0;
2886 s->fe->counters.failed_req++;
2887 if (s->listener->counters)
2888 s->listener->counters->failed_req++;
2889
2890 if (!(s->flags & SN_ERR_MASK))
2891 s->flags |= SN_ERR_PRXCOND;
2892 if (!(s->flags & SN_FINST_MASK))
2893 s->flags |= SN_FINST_R;
2894 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01002895}
2896
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002897/* This stream analyser waits for a complete HTTP response. It returns 1 if the
2898 * processing can continue on next analysers, or zero if it either needs more
2899 * data or wants to immediately abort the response (eg: timeout, error, ...). It
2900 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
2901 * when it has nothing left to do, and may remove any analyser when it wants to
2902 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002903 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002904int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002905{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002906 struct http_txn *txn = &s->txn;
2907 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02002908 struct hdr_ctx ctx;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002909 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002910 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002911
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002912 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002913 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002914 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002915 rep,
2916 rep->rex, rep->wex,
2917 rep->flags,
2918 rep->l,
2919 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002920
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002921 /*
2922 * Now parse the partial (or complete) lines.
2923 * We will check the response syntax, and also join multi-line
2924 * headers. An index of all the lines will be elaborated while
2925 * parsing.
2926 *
2927 * For the parsing, we use a 28 states FSM.
2928 *
2929 * Here is the information we currently have :
2930 * rep->data + rep->som = beginning of response
2931 * rep->data + rep->eoh = end of processed headers / start of current one
2932 * rep->data + rep->eol = end of current header or line (LF or CRLF)
2933 * rep->lr = first non-visited byte
2934 * rep->r = end of data
2935 */
2936
2937 if (likely(rep->lr < rep->r))
2938 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau7f875f62008-08-11 17:35:01 +02002939
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002940 /* 1: we might have to print this header in debug mode */
2941 if (unlikely((global.mode & MODE_DEBUG) &&
2942 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002943 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002944 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002945
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002946 sol = rep->data + msg->som;
2947 eol = sol + msg->sl.rq.l;
2948 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002949
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002950 sol += hdr_idx_first_pos(&txn->hdr_idx);
2951 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002952
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002953 while (cur_idx) {
2954 eol = sol + txn->hdr_idx.v[cur_idx].len;
2955 debug_hdr("srvhdr", s, sol, eol);
2956 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2957 cur_idx = txn->hdr_idx.v[cur_idx].next;
2958 }
2959 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002960
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002961 /*
2962 * Now we quickly check if we have found a full valid response.
2963 * If not so, we check the FD and buffer states before leaving.
2964 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002965 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002966 * responses are checked first.
2967 *
2968 * Depending on whether the client is still there or not, we
2969 * may send an error response back or not. Note that normally
2970 * we should only check for HTTP status there, and check I/O
2971 * errors somewhere else.
2972 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002973
Willy Tarreau655dce92009-11-08 13:10:58 +01002974 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002975 /* Invalid response */
2976 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2977 /* we detected a parsing error. We want to archive this response
2978 * in the dedicated proxy area for later troubleshooting.
2979 */
2980 hdr_response_bad:
2981 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
2982 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
2983
2984 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002985 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002986 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002987 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
2988 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002989
2990 rep->analysers = 0;
2991 txn->status = 502;
2992 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
2993
2994 if (!(s->flags & SN_ERR_MASK))
2995 s->flags |= SN_ERR_PRXCOND;
2996 if (!(s->flags & SN_FINST_MASK))
2997 s->flags |= SN_FINST_H;
2998
2999 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003000 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003001
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003002 /* too large response does not fit in buffer. */
3003 else if (rep->flags & BF_FULL) {
3004 goto hdr_response_bad;
3005 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003006
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003007 /* read error */
3008 else if (rep->flags & BF_READ_ERROR) {
3009 if (msg->err_pos >= 0)
3010 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02003011
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003012 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003013 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003014 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003015 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
3016 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003017
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003018 rep->analysers = 0;
3019 txn->status = 502;
3020 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02003021
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003022 if (!(s->flags & SN_ERR_MASK))
3023 s->flags |= SN_ERR_SRVCL;
3024 if (!(s->flags & SN_FINST_MASK))
3025 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003026 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003027 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003028
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003029 /* read timeout : return a 504 to the client. */
3030 else if (rep->flags & BF_READ_TIMEOUT) {
3031 if (msg->err_pos >= 0)
3032 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003033
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003034 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003035 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003036 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003037 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
3038 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003039
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003040 rep->analysers = 0;
3041 txn->status = 504;
3042 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02003043
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003044 if (!(s->flags & SN_ERR_MASK))
3045 s->flags |= SN_ERR_SRVTO;
3046 if (!(s->flags & SN_FINST_MASK))
3047 s->flags |= SN_FINST_H;
3048 return 0;
3049 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02003050
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003051 /* close from server */
3052 else if (rep->flags & BF_SHUTR) {
3053 if (msg->err_pos >= 0)
3054 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003055
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003056 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003057 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003058 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003059 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
3060 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003061
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003062 rep->analysers = 0;
3063 txn->status = 502;
3064 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01003065
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003066 if (!(s->flags & SN_ERR_MASK))
3067 s->flags |= SN_ERR_SRVCL;
3068 if (!(s->flags & SN_FINST_MASK))
3069 s->flags |= SN_FINST_H;
3070 return 0;
3071 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003072
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003073 /* write error to client (we don't send any message then) */
3074 else if (rep->flags & BF_WRITE_ERROR) {
3075 if (msg->err_pos >= 0)
3076 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003077
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003078 s->be->counters.failed_resp++;
3079 rep->analysers = 0;
3080
3081 if (!(s->flags & SN_ERR_MASK))
3082 s->flags |= SN_ERR_CLICL;
3083 if (!(s->flags & SN_FINST_MASK))
3084 s->flags |= SN_FINST_H;
3085
3086 /* process_session() will take care of the error */
3087 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003088 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003089
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003090 buffer_dont_close(rep);
3091 return 0;
3092 }
3093
3094 /* More interesting part now : we know that we have a complete
3095 * response which at least looks like HTTP. We have an indicator
3096 * of each header's length, so we can parse them quickly.
3097 */
3098
3099 if (unlikely(msg->err_pos >= 0))
3100 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3101
3102 /* ensure we keep this pointer to the beginning of the message */
3103 msg->sol = rep->data + msg->som;
3104
3105 /*
3106 * 1: get the status code
3107 */
3108 n = rep->data[msg->sl.st.c] - '0';
3109 if (n < 1 || n > 5)
3110 n = 0;
3111 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003112
Willy Tarreau5b154472009-12-21 20:11:07 +01003113 /* check if the response is HTTP/1.1 or above */
3114 if ((msg->sl.st.v_l == 8) &&
3115 ((rep->data[msg->som + 5] > '1') ||
3116 ((rep->data[msg->som + 5] == '1') &&
3117 (rep->data[msg->som + 7] >= '1'))))
3118 txn->flags |= TX_RES_VER_11;
3119
3120 /* "connection" has not been parsed yet */
3121 txn->flags &= ~TX_CON_HDR_PARS;
3122
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003123 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
3124
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003125 if (txn->status >= 100 && txn->status < 500)
3126 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
3127 else
3128 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
3129
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003130 /*
3131 * 2: check for cacheability.
3132 */
3133
3134 switch (txn->status) {
3135 case 200:
3136 case 203:
3137 case 206:
3138 case 300:
3139 case 301:
3140 case 410:
3141 /* RFC2616 @13.4:
3142 * "A response received with a status code of
3143 * 200, 203, 206, 300, 301 or 410 MAY be stored
3144 * by a cache (...) unless a cache-control
3145 * directive prohibits caching."
3146 *
3147 * RFC2616 @9.5: POST method :
3148 * "Responses to this method are not cacheable,
3149 * unless the response includes appropriate
3150 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003151 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003152 if (likely(txn->meth != HTTP_METH_POST) &&
3153 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3154 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3155 break;
3156 default:
3157 break;
3158 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003159
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003160 /*
3161 * 3: we may need to capture headers
3162 */
3163 s->logs.logwait &= ~LW_RESP;
3164 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
3165 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3166 txn->rsp.cap, s->fe->rsp_cap);
3167
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003168 /* 4: determine if we have a transfer-encoding or content-length.
3169 * RFC2616 #4.4 states that :
3170 * - Any response which "MUST NOT" include a message-body (such as the
3171 * 1xx, 204 and 304 responses and any response to a HEAD request) is
3172 * always terminated by the first empty line after the header fields,
3173 * regardless of the entity-header fields present in the message.
3174 *
3175 * - If a Transfer-Encoding header field is present and has any value
3176 * other than "identity", then the transfer-length is defined by use
3177 * of the "chunked" transfer-coding ;
3178 *
3179 * - If a Content-Length header field is present, its decimal value in
3180 * OCTETs represents both the entity-length and the transfer-length.
3181 * If a message is received with both a Transfer-Encoding header
3182 * field and a Content-Length header field, the latter MUST be ignored.
3183 */
3184
3185 /* Skip parsing if no content length is possible. The response flags
3186 * remain 0 as well as the hdr_content_len, which may or may not mirror
3187 * the real header value.
3188 * FIXME: should we parse anyway and return an error on chunked encoding ?
3189 */
3190 if (txn->meth == HTTP_METH_HEAD ||
3191 (txn->status >= 100 && txn->status < 200) ||
3192 txn->status == 204 || txn->status == 304)
3193 goto skip_content_length;
3194
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003195 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003196 while ((txn->flags & TX_RES_VER_11) &&
3197 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003198 if (ctx.vlen == 8 && strncasecmp(ctx.line + ctx.val, "identity", 8) == 0)
3199 continue;
3200 txn->flags |= TX_RES_TE_CHNK;
3201 break;
3202 }
3203
3204 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
3205 ctx.idx = 0;
3206 while (!(txn->flags & TX_RES_TE_CHNK) &&
3207 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
3208 signed long long cl;
3209
3210 if (!ctx.vlen)
3211 goto hdr_response_bad;
3212
3213 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
3214 goto hdr_response_bad; /* parse failure */
3215
3216 if (cl < 0)
3217 goto hdr_response_bad;
3218
3219 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
3220 goto hdr_response_bad; /* already specified, was different */
3221
3222 txn->flags |= TX_RES_CNT_LEN;
3223 msg->hdr_content_len = cl;
3224 }
3225
3226skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003227
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003228 /* end of job, return OK */
3229 rep->analysers &= ~an_bit;
3230 rep->analyse_exp = TICK_ETERNITY;
3231 return 1;
3232}
3233
3234/* This function performs all the processing enabled for the current response.
3235 * It normally returns zero, but may return 1 if it absolutely needs to be
3236 * called again after other functions. It relies on buffers flags, and updates
3237 * t->rep->analysers. It might make sense to explode it into several other
3238 * functions. It works like process_request (see indications above).
3239 */
3240int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
3241{
3242 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003243 struct http_msg *msg = &txn->rsp;
3244 struct proxy *cur_proxy;
3245 int cur_idx;
Willy Tarreau5b154472009-12-21 20:11:07 +01003246 int conn_ka = 0, conn_cl = 0;
3247 int must_close = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003248
3249 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3250 now_ms, __FUNCTION__,
3251 t,
3252 rep,
3253 rep->rex, rep->wex,
3254 rep->flags,
3255 rep->l,
3256 rep->analysers);
3257
Willy Tarreau655dce92009-11-08 13:10:58 +01003258 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003259 return 0;
3260
3261 rep->analysers &= ~an_bit;
3262 rep->analyse_exp = TICK_ETERNITY;
3263
Willy Tarreau5b154472009-12-21 20:11:07 +01003264 /* Now we have to check if we need to modify the Connection header.
3265 * This is more difficult on the response than it is on the request,
3266 * because we can have two different HTTP versions and we don't know
3267 * how the client will interprete a response. For instance, let's say
3268 * that the client sends a keep-alive request in HTTP/1.0 and gets an
3269 * HTTP/1.1 response without any header. Maybe it will bound itself to
3270 * HTTP/1.0 because it only knows about it, and will consider the lack
3271 * of header as a close, or maybe it knows HTTP/1.1 and can consider
3272 * the lack of header as a keep-alive. Thus we will use two flags
3273 * indicating how a request MAY be understood by the client. In case
3274 * of multiple possibilities, we'll fix the header to be explicit. If
3275 * ambiguous cases such as both close and keepalive are seen, then we
3276 * will fall back to explicit close. Note that we won't take risks with
3277 * HTTP/1.0 clients which may not necessarily understand keep-alive.
3278 */
3279
3280 if ((txn->meth != HTTP_METH_CONNECT) &&
3281 (txn->status >= 200) &&
3282 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN &&
3283 !(txn->flags & TX_CON_HDR_PARS)) {
3284 int may_keep = 0, may_close = 0; /* how it may be understood */
3285 struct hdr_ctx ctx;
3286
3287 ctx.idx = 0;
3288 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
3289 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
3290 conn_cl = 1;
3291 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
3292 conn_ka = 1;
3293 }
3294
3295 if (conn_cl) {
3296 /* close header present */
3297 may_close = 1;
3298 if (conn_ka) /* we have both close and keep-alive */
3299 may_keep = 1;
3300 }
3301 else if (conn_ka) {
3302 /* keep-alive alone */
3303 may_keep = 1;
3304 }
3305 else {
3306 /* no close nor keep-alive header */
3307 if (txn->flags & TX_RES_VER_11)
3308 may_keep = 1;
3309 else
3310 may_close = 1;
3311
3312 if (txn->flags & TX_REQ_VER_11)
3313 may_keep = 1;
3314 else
3315 may_close = 1;
3316 }
3317
3318 /* let's update the transaction status to reflect any close.
3319 * Note that ambiguous cases with keep & close will also be
3320 * handled.
3321 */
3322 if (may_close)
3323 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3324
3325 /* Now we must adjust the response header :
3326 * - set "close" if may_keep and WANT_CLO
3327 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
3328 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
3329 *
3330 * Until we support the server-close mode, we'll only support the set "close".
3331 */
3332 if (may_keep && (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO)
3333 must_close = 1;
3334
3335 txn->flags |= TX_CON_HDR_PARS;
3336 }
3337
3338 /* We might have to check for "Connection:" if the server
3339 * returns a connection status that is not compatible with
3340 * the client's or with the config.
3341 */
3342 if ((txn->status >= 200) && must_close && (conn_cl|conn_ka)) {
3343 char *cur_ptr, *cur_end, *cur_next;
3344 int cur_idx, old_idx, delta, val;
3345 int must_delete;
3346 struct hdr_idx_elem *cur_hdr;
3347
3348 /* we just have to remove the headers if both sides are 1.0 */
3349 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
3350 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3351
3352 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
3353 cur_hdr = &txn->hdr_idx.v[cur_idx];
3354 cur_ptr = cur_next;
3355 cur_end = cur_ptr + cur_hdr->len;
3356 cur_next = cur_end + cur_hdr->cr + 1;
3357
3358 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3359 if (!val)
3360 continue;
3361
3362 /* 3 possibilities :
3363 * - we have already set "Connection: close" or we're in
3364 * HTTP/1.0, so we remove this line.
3365 * - we have not yet set "Connection: close", but this line
3366 * indicates close. We leave it untouched and set the flag.
3367 * - we have not yet set "Connection: close", and this line
3368 * indicates non-close. We replace it and set the flag.
3369 */
3370 if (must_delete) {
3371 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3372 http_msg_move_end(&txn->rsp, delta);
3373 cur_next += delta;
3374 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3375 txn->hdr_idx.used--;
3376 cur_hdr->len = 0;
3377 must_close = 0;
3378 } else {
3379 if (cur_end - cur_ptr - val != 5 ||
3380 strncasecmp(cur_ptr + val, "close", 5) != 0) {
3381 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3382 "close", 5);
3383 cur_next += delta;
3384 cur_hdr->len += delta;
3385 http_msg_move_end(&txn->rsp, delta);
3386 }
3387 must_delete = 1;
3388 must_close = 0;
3389 }
3390 } /* for loop */
3391 } /* if must close keep-alive */
3392
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003393 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003394 /*
3395 * 3: we will have to evaluate the filters.
3396 * As opposed to version 1.2, now they will be evaluated in the
3397 * filters order and not in the header order. This means that
3398 * each filter has to be validated among all headers.
3399 *
3400 * Filters are tried with ->be first, then with ->fe if it is
3401 * different from ->be.
3402 */
3403
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003404 cur_proxy = t->be;
3405 while (1) {
3406 struct proxy *rule_set = cur_proxy;
3407
3408 /* try headers filters */
3409 if (rule_set->rsp_exp != NULL) {
3410 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3411 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003412 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003413 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003414 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
3415 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003416 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003417 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02003418 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003419 txn->status = 502;
Willy Tarreau8e89b842009-10-18 23:56:35 +02003420 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003421 if (!(t->flags & SN_ERR_MASK))
3422 t->flags |= SN_ERR_PRXCOND;
3423 if (!(t->flags & SN_FINST_MASK))
3424 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02003425 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003426 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003427 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003428
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003429 /* has the response been denied ? */
3430 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01003431 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003432 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003433
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003434 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003435 if (t->listener->counters)
3436 t->listener->counters->denied_resp++;
3437
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003438 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003439 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003440
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003441 /* add response headers from the rule sets in the same order */
3442 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau816b9792009-09-15 21:25:21 +02003443 if (txn->status < 200)
3444 break;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003445 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3446 rule_set->rsp_add[cur_idx])) < 0)
3447 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003448 }
3449
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003450 /* check whether we're already working on the frontend */
3451 if (cur_proxy == t->fe)
3452 break;
3453 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003454 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003455
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003456 /*
3457 * 4: check for server cookie.
3458 */
Willy Tarreau816b9792009-09-15 21:25:21 +02003459 if ((t->be->cookie_name || t->be->appsession_name || t->fe->capture_name
3460 || (t->be->options & PR_O_CHK_CACHE)) && txn->status >= 200)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003461 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003462
Willy Tarreaubaaee002006-06-26 02:48:02 +02003463
Willy Tarreaua15645d2007-03-18 16:22:39 +01003464 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003465 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003466 */
Willy Tarreau816b9792009-09-15 21:25:21 +02003467 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0 && txn->status >= 200)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003468 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003469
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003470 /*
3471 * 6: add server cookie in the response if needed
3472 */
3473 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau816b9792009-09-15 21:25:21 +02003474 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
3475 txn->status >= 200) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003476 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003477
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003478 /* the server is known, it's not the one the client requested, we have to
3479 * insert a set-cookie here, except if we want to insert only on POST
3480 * requests and this one isn't. Note that servers which don't have cookies
3481 * (eg: some backup servers) will return a full cookie removal request.
3482 */
3483 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
3484 t->be->cookie_name,
3485 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003486
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003487 if (t->be->cookie_domain)
3488 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003489
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003490 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3491 trash, len)) < 0)
3492 goto return_bad_resp;
3493 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003494
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003495 /* Here, we will tell an eventual cache on the client side that we don't
3496 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3497 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3498 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3499 */
3500 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003501
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003502 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3503
3504 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3505 "Cache-control: private", 22)) < 0)
3506 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003507 }
3508 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003509
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003510 /*
3511 * 7: check if result will be cacheable with a cookie.
3512 * We'll block the response if security checks have caught
3513 * nasty things such as a cacheable cookie.
3514 */
3515 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3516 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau816b9792009-09-15 21:25:21 +02003517 (t->be->options & PR_O_CHK_CACHE) &&
3518 txn->status >= 200) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003519
3520 /* we're in presence of a cacheable response containing
3521 * a set-cookie header. We'll block it as requested by
3522 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003523 */
Willy Tarreau8365f932009-03-15 23:11:49 +01003524 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003525 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003526
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003527 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003528 if (t->listener->counters)
3529 t->listener->counters->denied_resp++;
3530
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003531 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3532 t->be->id, t->srv?t->srv->id:"<dispatch>");
3533 send_log(t->be, LOG_ALERT,
3534 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3535 t->be->id, t->srv?t->srv->id:"<dispatch>");
3536 goto return_srv_prx_502;
3537 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003538
3539 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01003540 * 8: add "Connection: close" if needed and not yet set. This is
3541 * only needed for 1.1 responses since we know there is no other
3542 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003543 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003544 if (txn->status >= 200 && must_close && (txn->flags & TX_RES_VER_11)) {
3545 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003546 "Connection: close", 17)) < 0)
3547 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01003548 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003549 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003550
Willy Tarreau816b9792009-09-15 21:25:21 +02003551 /*
3552 * 9: we may be facing a 1xx response (100 continue, 101 switching protocols),
3553 * in which case this is not the right response, and we're waiting for the
3554 * next one. Let's allow this response to go to the client and wait for the
3555 * next one.
3556 */
3557 if (txn->status < 200) {
3558 hdr_idx_init(&txn->hdr_idx);
3559 buffer_forward(rep, rep->lr - (rep->data + msg->som));
3560 msg->msg_state = HTTP_MSG_RPBEFORE;
3561 txn->status = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003562 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
3563 return 1;
Willy Tarreau816b9792009-09-15 21:25:21 +02003564 }
3565
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003566 /*************************************************************
3567 * OK, that's finished for the headers. We have done what we *
3568 * could. Let's switch to the DATA state. *
3569 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003570
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003571 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003572
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003573 /* if the user wants to log as soon as possible, without counting
3574 * bytes from the server, then this is the right moment. We have
3575 * to temporarily assign bytes_out to log what we currently have.
3576 */
3577 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3578 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3579 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01003580 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003581 t->logs.bytes_out = 0;
3582 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003583
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003584 /* Note: we must not try to cheat by jumping directly to DATA,
3585 * otherwise we would not let the client side wake up.
3586 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003587
Willy Tarreaudafde432008-08-17 01:00:46 +02003588 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003589 }
3590 return 0;
3591}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003592
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003593/* Iterate the same filter through all request headers.
3594 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003595 * Since it can manage the switch to another backend, it updates the per-proxy
3596 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003597 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003598int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003599{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003600 char term;
3601 char *cur_ptr, *cur_end, *cur_next;
3602 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003603 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003604 struct hdr_idx_elem *cur_hdr;
3605 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003606
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003607 last_hdr = 0;
3608
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003609 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003610 old_idx = 0;
3611
3612 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003613 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003614 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003615 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003616 (exp->action == ACT_ALLOW ||
3617 exp->action == ACT_DENY ||
3618 exp->action == ACT_TARPIT))
3619 return 0;
3620
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003621 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003622 if (!cur_idx)
3623 break;
3624
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003625 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003626 cur_ptr = cur_next;
3627 cur_end = cur_ptr + cur_hdr->len;
3628 cur_next = cur_end + cur_hdr->cr + 1;
3629
3630 /* Now we have one header between cur_ptr and cur_end,
3631 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003632 */
3633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003634 /* The annoying part is that pattern matching needs
3635 * that we modify the contents to null-terminate all
3636 * strings before testing them.
3637 */
3638
3639 term = *cur_end;
3640 *cur_end = '\0';
3641
3642 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3643 switch (exp->action) {
3644 case ACT_SETBE:
3645 /* It is not possible to jump a second time.
3646 * FIXME: should we return an HTTP/500 here so that
3647 * the admin knows there's a problem ?
3648 */
3649 if (t->be != t->fe)
3650 break;
3651
3652 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003653 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003654 last_hdr = 1;
3655 break;
3656
3657 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003658 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003659 last_hdr = 1;
3660 break;
3661
3662 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003663 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003664 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003665
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003666 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003667 if (t->listener->counters)
3668 t->listener->counters->denied_resp++;
3669
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003670 break;
3671
3672 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003673 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003674 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003675
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003676 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003677 if (t->listener->counters)
3678 t->listener->counters->denied_resp++;
3679
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003680 break;
3681
3682 case ACT_REPLACE:
3683 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3684 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3685 /* FIXME: if the user adds a newline in the replacement, the
3686 * index will not be recalculated for now, and the new line
3687 * will not be counted as a new header.
3688 */
3689
3690 cur_end += delta;
3691 cur_next += delta;
3692 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01003693 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003694 break;
3695
3696 case ACT_REMOVE:
3697 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3698 cur_next += delta;
3699
Willy Tarreaufa355d42009-11-29 18:12:29 +01003700 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003701 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3702 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003703 cur_hdr->len = 0;
3704 cur_end = NULL; /* null-term has been rewritten */
3705 break;
3706
3707 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003708 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003709 if (cur_end)
3710 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003711
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003712 /* keep the link from this header to next one in case of later
3713 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003714 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003715 old_idx = cur_idx;
3716 }
3717 return 0;
3718}
3719
3720
3721/* Apply the filter to the request line.
3722 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3723 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003724 * Since it can manage the switch to another backend, it updates the per-proxy
3725 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003726 */
3727int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3728{
3729 char term;
3730 char *cur_ptr, *cur_end;
3731 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003732 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003733 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003734
Willy Tarreau58f10d72006-12-04 02:26:12 +01003735
Willy Tarreau3d300592007-03-18 18:34:41 +01003736 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003737 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003738 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003739 (exp->action == ACT_ALLOW ||
3740 exp->action == ACT_DENY ||
3741 exp->action == ACT_TARPIT))
3742 return 0;
3743 else if (exp->action == ACT_REMOVE)
3744 return 0;
3745
3746 done = 0;
3747
Willy Tarreau9cdde232007-05-02 20:58:19 +02003748 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003749 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003750
3751 /* Now we have the request line between cur_ptr and cur_end */
3752
3753 /* The annoying part is that pattern matching needs
3754 * that we modify the contents to null-terminate all
3755 * strings before testing them.
3756 */
3757
3758 term = *cur_end;
3759 *cur_end = '\0';
3760
3761 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3762 switch (exp->action) {
3763 case ACT_SETBE:
3764 /* It is not possible to jump a second time.
3765 * FIXME: should we return an HTTP/500 here so that
3766 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003767 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003768 if (t->be != t->fe)
3769 break;
3770
3771 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003772 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003773 done = 1;
3774 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003775
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003776 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003777 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003778 done = 1;
3779 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003780
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003781 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003782 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003783
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003784 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003785 if (t->listener->counters)
3786 t->listener->counters->denied_resp++;
3787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003788 done = 1;
3789 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003790
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003791 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003792 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003793
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003794 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003795 if (t->listener->counters)
3796 t->listener->counters->denied_resp++;
3797
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003798 done = 1;
3799 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003800
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003801 case ACT_REPLACE:
3802 *cur_end = term; /* restore the string terminator */
3803 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3804 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3805 /* FIXME: if the user adds a newline in the replacement, the
3806 * index will not be recalculated for now, and the new line
3807 * will not be counted as a new header.
3808 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003809
Willy Tarreaufa355d42009-11-29 18:12:29 +01003810 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003811 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003812
Willy Tarreau9cdde232007-05-02 20:58:19 +02003813 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003814 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003815 HTTP_MSG_RQMETH,
3816 cur_ptr, cur_end + 1,
3817 NULL, NULL);
3818 if (unlikely(!cur_end))
3819 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003820
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003821 /* we have a full request and we know that we have either a CR
3822 * or an LF at <ptr>.
3823 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003824 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3825 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003826 /* there is no point trying this regex on headers */
3827 return 1;
3828 }
3829 }
3830 *cur_end = term; /* restore the string terminator */
3831 return done;
3832}
Willy Tarreau97de6242006-12-27 17:18:38 +01003833
Willy Tarreau58f10d72006-12-04 02:26:12 +01003834
Willy Tarreau58f10d72006-12-04 02:26:12 +01003835
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003836/*
3837 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3838 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003839 * unparsable request. Since it can manage the switch to another backend, it
3840 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003841 */
3842int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3843{
Willy Tarreau3d300592007-03-18 18:34:41 +01003844 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003845 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003846 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003847 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003848
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003849 /*
3850 * The interleaving of transformations and verdicts
3851 * makes it difficult to decide to continue or stop
3852 * the evaluation.
3853 */
3854
Willy Tarreau3d300592007-03-18 18:34:41 +01003855 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003856 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3857 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3858 exp = exp->next;
3859 continue;
3860 }
3861
3862 /* Apply the filter to the request line. */
3863 ret = apply_filter_to_req_line(t, req, exp);
3864 if (unlikely(ret < 0))
3865 return -1;
3866
3867 if (likely(ret == 0)) {
3868 /* The filter did not match the request, it can be
3869 * iterated through all headers.
3870 */
3871 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003872 }
3873 exp = exp->next;
3874 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003875 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003876}
3877
3878
Willy Tarreaua15645d2007-03-18 16:22:39 +01003879
Willy Tarreau58f10d72006-12-04 02:26:12 +01003880/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02003881 * Try to retrieve the server associated to the appsession.
3882 * If the server is found, it's assigned to the session.
3883 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01003884void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02003885 struct http_txn *txn = &t->txn;
3886 appsess *asession = NULL;
3887 char *sessid_temp = NULL;
3888
Cyril Bontéb21570a2009-11-29 20:04:48 +01003889 if (len > t->be->appsession_len) {
3890 len = t->be->appsession_len;
3891 }
3892
Cyril Bontébf47aeb2009-10-15 00:15:40 +02003893 if (t->be->options2 & PR_O2_AS_REQL) {
3894 /* request-learn option is enabled : store the sessid in the session for future use */
3895 if (t->sessid != NULL) {
3896 /* free previously allocated memory as we don't need the session id found in the URL anymore */
3897 pool_free2(apools.sessid, t->sessid);
3898 }
3899
3900 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
3901 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3902 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3903 return;
3904 }
3905
Cyril Bontéb21570a2009-11-29 20:04:48 +01003906 memcpy(t->sessid, buf, len);
3907 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02003908 }
3909
3910 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
3911 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3912 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3913 return;
3914 }
3915
Cyril Bontéb21570a2009-11-29 20:04:48 +01003916 memcpy(sessid_temp, buf, len);
3917 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02003918
3919 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
3920 /* free previously allocated memory */
3921 pool_free2(apools.sessid, sessid_temp);
3922
3923 if (asession != NULL) {
3924 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
3925 if (!(t->be->options2 & PR_O2_AS_REQL))
3926 asession->request_count++;
3927
3928 if (asession->serverid != NULL) {
3929 struct server *srv = t->be->srv;
3930 while (srv) {
3931 if (strcmp(srv->id, asession->serverid) == 0) {
3932 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
3933 /* we found the server and it's usable */
3934 txn->flags &= ~TX_CK_MASK;
3935 txn->flags |= TX_CK_VALID;
3936 t->flags |= SN_DIRECT | SN_ASSIGNED;
3937 t->srv = srv;
3938 break;
3939 } else {
3940 txn->flags &= ~TX_CK_MASK;
3941 txn->flags |= TX_CK_DOWN;
3942 }
3943 }
3944 srv = srv->next;
3945 }
3946 }
3947 }
3948}
3949
3950/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003951 * Manage client-side cookie. It can impact performance by about 2% so it is
3952 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003953 */
3954void manage_client_side_cookies(struct session *t, struct buffer *req)
3955{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003956 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003957 char *p1, *p2, *p3, *p4;
3958 char *del_colon, *del_cookie, *colon;
3959 int app_cookies;
3960
Willy Tarreau58f10d72006-12-04 02:26:12 +01003961 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003962 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003963
Willy Tarreau2a324282006-12-05 00:05:46 +01003964 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003965 * we start with the start line.
3966 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003967 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003968 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003969
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003970 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003971 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003972 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003973
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003974 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003975 cur_ptr = cur_next;
3976 cur_end = cur_ptr + cur_hdr->len;
3977 cur_next = cur_end + cur_hdr->cr + 1;
3978
3979 /* We have one full header between cur_ptr and cur_end, and the
3980 * next header starts at cur_next. We're only interested in
3981 * "Cookie:" headers.
3982 */
3983
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003984 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3985 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003986 old_idx = cur_idx;
3987 continue;
3988 }
3989
3990 /* Now look for cookies. Conforming to RFC2109, we have to support
3991 * attributes whose name begin with a '$', and associate them with
3992 * the right cookie, if we want to delete this cookie.
3993 * So there are 3 cases for each cookie read :
3994 * 1) it's a special attribute, beginning with a '$' : ignore it.
3995 * 2) it's a server id cookie that we *MAY* want to delete : save
3996 * some pointers on it (last semi-colon, beginning of cookie...)
3997 * 3) it's an application cookie : we *MAY* have to delete a previous
3998 * "special" cookie.
3999 * At the end of loop, if a "special" cookie remains, we may have to
4000 * remove it. If no application cookie persists in the header, we
4001 * *MUST* delete it
4002 */
4003
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004004 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004005
Willy Tarreau58f10d72006-12-04 02:26:12 +01004006 /* del_cookie == NULL => nothing to be deleted */
4007 del_colon = del_cookie = NULL;
4008 app_cookies = 0;
4009
4010 while (p1 < cur_end) {
4011 /* skip spaces and colons, but keep an eye on these ones */
4012 while (p1 < cur_end) {
4013 if (*p1 == ';' || *p1 == ',')
4014 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004015 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004016 break;
4017 p1++;
4018 }
4019
4020 if (p1 == cur_end)
4021 break;
4022
4023 /* p1 is at the beginning of the cookie name */
4024 p2 = p1;
4025 while (p2 < cur_end && *p2 != '=')
4026 p2++;
4027
4028 if (p2 == cur_end)
4029 break;
4030
4031 p3 = p2 + 1; /* skips the '=' sign */
4032 if (p3 == cur_end)
4033 break;
4034
4035 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004036 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004037 p4++;
4038
4039 /* here, we have the cookie name between p1 and p2,
4040 * and its value between p3 and p4.
4041 * we can process it :
4042 *
4043 * Cookie: NAME=VALUE;
4044 * | || || |
4045 * | || || +--> p4
4046 * | || |+-------> p3
4047 * | || +--------> p2
4048 * | |+------------> p1
4049 * | +-------------> colon
4050 * +--------------------> cur_ptr
4051 */
4052
4053 if (*p1 == '$') {
4054 /* skip this one */
4055 }
4056 else {
4057 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004058 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004059 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004060 (p4 - p1 >= t->fe->capture_namelen) &&
4061 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004062 int log_len = p4 - p1;
4063
Willy Tarreau086b3b42007-05-13 21:45:51 +02004064 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004065 Alert("HTTP logging : out of memory.\n");
4066 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004067 if (log_len > t->fe->capture_len)
4068 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004069 memcpy(txn->cli_cookie, p1, log_len);
4070 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004071 }
4072 }
4073
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004074 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4075 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004076 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004077 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004078 char *delim;
4079
4080 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4081 * have the server ID betweek p3 and delim, and the original cookie between
4082 * delim+1 and p4. Otherwise, delim==p4 :
4083 *
4084 * Cookie: NAME=SRV~VALUE;
4085 * | || || | |
4086 * | || || | +--> p4
4087 * | || || +--------> delim
4088 * | || |+-----------> p3
4089 * | || +------------> p2
4090 * | |+----------------> p1
4091 * | +-----------------> colon
4092 * +------------------------> cur_ptr
4093 */
4094
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004095 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004096 for (delim = p3; delim < p4; delim++)
4097 if (*delim == COOKIE_DELIM)
4098 break;
4099 }
4100 else
4101 delim = p4;
4102
4103
4104 /* Here, we'll look for the first running server which supports the cookie.
4105 * This allows to share a same cookie between several servers, for example
4106 * to dedicate backup servers to specific servers only.
4107 * However, to prevent clients from sticking to cookie-less backup server
4108 * when they have incidentely learned an empty cookie, we simply ignore
4109 * empty cookies and mark them as invalid.
4110 */
4111 if (delim == p3)
4112 srv = NULL;
4113
4114 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004115 if (srv->cookie && (srv->cklen == delim - p3) &&
4116 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004117 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004118 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004119 txn->flags &= ~TX_CK_MASK;
4120 txn->flags |= TX_CK_VALID;
4121 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004122 t->srv = srv;
4123 break;
4124 } else {
4125 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004126 txn->flags &= ~TX_CK_MASK;
4127 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004128 }
4129 }
4130 srv = srv->next;
4131 }
4132
Willy Tarreau3d300592007-03-18 18:34:41 +01004133 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004134 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004135 txn->flags &= ~TX_CK_MASK;
4136 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004137 }
4138
4139 /* depending on the cookie mode, we may have to either :
4140 * - delete the complete cookie if we're in insert+indirect mode, so that
4141 * the server never sees it ;
4142 * - remove the server id from the cookie value, and tag the cookie as an
4143 * application cookie so that it does not get accidentely removed later,
4144 * if we're in cookie prefix mode
4145 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004146 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004147 int delta; /* negative */
4148
4149 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4150 p4 += delta;
4151 cur_end += delta;
4152 cur_next += delta;
4153 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004154 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004155
4156 del_cookie = del_colon = NULL;
4157 app_cookies++; /* protect the header from deletion */
4158 }
4159 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004160 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004161 del_cookie = p1;
4162 del_colon = colon;
4163 }
4164 } else {
4165 /* now we know that we must keep this cookie since it's
4166 * not ours. But if we wanted to delete our cookie
4167 * earlier, we cannot remove the complete header, but we
4168 * can remove the previous block itself.
4169 */
4170 app_cookies++;
4171
4172 if (del_cookie != NULL) {
4173 int delta; /* negative */
4174
4175 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4176 p4 += delta;
4177 cur_end += delta;
4178 cur_next += delta;
4179 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004180 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004181 del_cookie = del_colon = NULL;
4182 }
4183 }
4184
Cyril Bontéb21570a2009-11-29 20:04:48 +01004185 if (t->be->appsession_name != NULL) {
4186 int cmp_len, value_len;
4187 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004188
Cyril Bontéb21570a2009-11-29 20:04:48 +01004189 if (t->be->options2 & PR_O2_AS_PFX) {
4190 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
4191 value_begin = p1 + t->be->appsession_name_len;
4192 value_len = p4 - p1 - t->be->appsession_name_len;
4193 } else {
4194 cmp_len = p2 - p1;
4195 value_begin = p3;
4196 value_len = p4 - p3;
4197 }
4198
4199 /* let's see if the cookie is our appcookie */
4200 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
4201 /* Cool... it's the right one */
4202 manage_client_side_appsession(t, value_begin, value_len);
4203 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004204#if defined(DEBUG_HASH)
4205 Alert("manage_client_side_cookies\n");
4206 appsession_hash_dump(&(t->be->htbl_proxy));
4207#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004208 }/* end if ((t->proxy->appsession_name != NULL) ... */
4209 }
4210
4211 /* we'll have to look for another cookie ... */
4212 p1 = p4;
4213 } /* while (p1 < cur_end) */
4214
4215 /* There's no more cookie on this line.
4216 * We may have marked the last one(s) for deletion.
4217 * We must do this now in two ways :
4218 * - if there is no app cookie, we simply delete the header ;
4219 * - if there are app cookies, we must delete the end of the
4220 * string properly, including the colon/semi-colon before
4221 * the cookie name.
4222 */
4223 if (del_cookie != NULL) {
4224 int delta;
4225 if (app_cookies) {
4226 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4227 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004228 cur_hdr->len += delta;
4229 } else {
4230 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004231
4232 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004233 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4234 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004235 cur_hdr->len = 0;
4236 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004237 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004238 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004239 }
4240
4241 /* keep the link from this header to next one */
4242 old_idx = cur_idx;
4243 } /* end of cookie processing on this header */
4244}
4245
4246
Willy Tarreaua15645d2007-03-18 16:22:39 +01004247/* Iterate the same filter through all response headers contained in <rtr>.
4248 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4249 */
4250int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4251{
4252 char term;
4253 char *cur_ptr, *cur_end, *cur_next;
4254 int cur_idx, old_idx, last_hdr;
4255 struct http_txn *txn = &t->txn;
4256 struct hdr_idx_elem *cur_hdr;
4257 int len, delta;
4258
4259 last_hdr = 0;
4260
4261 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4262 old_idx = 0;
4263
4264 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004265 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004266 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004267 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004268 (exp->action == ACT_ALLOW ||
4269 exp->action == ACT_DENY))
4270 return 0;
4271
4272 cur_idx = txn->hdr_idx.v[old_idx].next;
4273 if (!cur_idx)
4274 break;
4275
4276 cur_hdr = &txn->hdr_idx.v[cur_idx];
4277 cur_ptr = cur_next;
4278 cur_end = cur_ptr + cur_hdr->len;
4279 cur_next = cur_end + cur_hdr->cr + 1;
4280
4281 /* Now we have one header between cur_ptr and cur_end,
4282 * and the next header starts at cur_next.
4283 */
4284
4285 /* The annoying part is that pattern matching needs
4286 * that we modify the contents to null-terminate all
4287 * strings before testing them.
4288 */
4289
4290 term = *cur_end;
4291 *cur_end = '\0';
4292
4293 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4294 switch (exp->action) {
4295 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004296 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004297 last_hdr = 1;
4298 break;
4299
4300 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004301 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004302 last_hdr = 1;
4303 break;
4304
4305 case ACT_REPLACE:
4306 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4307 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4308 /* FIXME: if the user adds a newline in the replacement, the
4309 * index will not be recalculated for now, and the new line
4310 * will not be counted as a new header.
4311 */
4312
4313 cur_end += delta;
4314 cur_next += delta;
4315 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004316 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004317 break;
4318
4319 case ACT_REMOVE:
4320 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4321 cur_next += delta;
4322
Willy Tarreaufa355d42009-11-29 18:12:29 +01004323 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004324 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4325 txn->hdr_idx.used--;
4326 cur_hdr->len = 0;
4327 cur_end = NULL; /* null-term has been rewritten */
4328 break;
4329
4330 }
4331 }
4332 if (cur_end)
4333 *cur_end = term; /* restore the string terminator */
4334
4335 /* keep the link from this header to next one in case of later
4336 * removal of next header.
4337 */
4338 old_idx = cur_idx;
4339 }
4340 return 0;
4341}
4342
4343
4344/* Apply the filter to the status line in the response buffer <rtr>.
4345 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4346 * or -1 if a replacement resulted in an invalid status line.
4347 */
4348int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4349{
4350 char term;
4351 char *cur_ptr, *cur_end;
4352 int done;
4353 struct http_txn *txn = &t->txn;
4354 int len, delta;
4355
4356
Willy Tarreau3d300592007-03-18 18:34:41 +01004357 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004358 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004359 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004360 (exp->action == ACT_ALLOW ||
4361 exp->action == ACT_DENY))
4362 return 0;
4363 else if (exp->action == ACT_REMOVE)
4364 return 0;
4365
4366 done = 0;
4367
Willy Tarreau9cdde232007-05-02 20:58:19 +02004368 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004369 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4370
4371 /* Now we have the status line between cur_ptr and cur_end */
4372
4373 /* The annoying part is that pattern matching needs
4374 * that we modify the contents to null-terminate all
4375 * strings before testing them.
4376 */
4377
4378 term = *cur_end;
4379 *cur_end = '\0';
4380
4381 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4382 switch (exp->action) {
4383 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004384 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004385 done = 1;
4386 break;
4387
4388 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004389 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004390 done = 1;
4391 break;
4392
4393 case ACT_REPLACE:
4394 *cur_end = term; /* restore the string terminator */
4395 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4396 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4397 /* FIXME: if the user adds a newline in the replacement, the
4398 * index will not be recalculated for now, and the new line
4399 * will not be counted as a new header.
4400 */
4401
Willy Tarreaufa355d42009-11-29 18:12:29 +01004402 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004403 cur_end += delta;
4404
Willy Tarreau9cdde232007-05-02 20:58:19 +02004405 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004406 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004407 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004408 cur_ptr, cur_end + 1,
4409 NULL, NULL);
4410 if (unlikely(!cur_end))
4411 return -1;
4412
4413 /* we have a full respnse and we know that we have either a CR
4414 * or an LF at <ptr>.
4415 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004416 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004417 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4418 /* there is no point trying this regex on headers */
4419 return 1;
4420 }
4421 }
4422 *cur_end = term; /* restore the string terminator */
4423 return done;
4424}
4425
4426
4427
4428/*
4429 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4430 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4431 * unparsable response.
4432 */
4433int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4434{
Willy Tarreau3d300592007-03-18 18:34:41 +01004435 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004436 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004437 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004438 int ret;
4439
4440 /*
4441 * The interleaving of transformations and verdicts
4442 * makes it difficult to decide to continue or stop
4443 * the evaluation.
4444 */
4445
Willy Tarreau3d300592007-03-18 18:34:41 +01004446 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004447 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4448 exp->action == ACT_PASS)) {
4449 exp = exp->next;
4450 continue;
4451 }
4452
4453 /* Apply the filter to the status line. */
4454 ret = apply_filter_to_sts_line(t, rtr, exp);
4455 if (unlikely(ret < 0))
4456 return -1;
4457
4458 if (likely(ret == 0)) {
4459 /* The filter did not match the response, it can be
4460 * iterated through all headers.
4461 */
4462 apply_filter_to_resp_headers(t, rtr, exp);
4463 }
4464 exp = exp->next;
4465 }
4466 return 0;
4467}
4468
4469
4470
4471/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004472 * Manage server-side cookies. It can impact performance by about 2% so it is
4473 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004474 */
4475void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4476{
4477 struct http_txn *txn = &t->txn;
4478 char *p1, *p2, *p3, *p4;
4479
Willy Tarreaua15645d2007-03-18 16:22:39 +01004480 char *cur_ptr, *cur_end, *cur_next;
4481 int cur_idx, old_idx, delta;
4482
Willy Tarreaua15645d2007-03-18 16:22:39 +01004483 /* Iterate through the headers.
4484 * we start with the start line.
4485 */
4486 old_idx = 0;
4487 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4488
4489 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4490 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004491 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004492
4493 cur_hdr = &txn->hdr_idx.v[cur_idx];
4494 cur_ptr = cur_next;
4495 cur_end = cur_ptr + cur_hdr->len;
4496 cur_next = cur_end + cur_hdr->cr + 1;
4497
4498 /* We have one full header between cur_ptr and cur_end, and the
4499 * next header starts at cur_next. We're only interested in
4500 * "Cookie:" headers.
4501 */
4502
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004503 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4504 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004505 old_idx = cur_idx;
4506 continue;
4507 }
4508
4509 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004510 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004511
4512
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004513 /* maybe we only wanted to see if there was a set-cookie. Note that
4514 * the cookie capture is declared in the fronend.
4515 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004516 if (t->be->cookie_name == NULL &&
4517 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004518 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004519 return;
4520
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004521 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004522
4523 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004524 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4525 break;
4526
4527 /* p1 is at the beginning of the cookie name */
4528 p2 = p1;
4529
4530 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4531 p2++;
4532
4533 if (p2 == cur_end || *p2 == ';') /* next cookie */
4534 break;
4535
4536 p3 = p2 + 1; /* skip the '=' sign */
4537 if (p3 == cur_end)
4538 break;
4539
4540 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004541 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004542 p4++;
4543
4544 /* here, we have the cookie name between p1 and p2,
4545 * and its value between p3 and p4.
4546 * we can process it.
4547 */
4548
4549 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004550 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004551 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004552 (p4 - p1 >= t->fe->capture_namelen) &&
4553 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004554 int log_len = p4 - p1;
4555
Willy Tarreau086b3b42007-05-13 21:45:51 +02004556 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004557 Alert("HTTP logging : out of memory.\n");
4558 }
4559
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004560 if (log_len > t->fe->capture_len)
4561 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004562 memcpy(txn->srv_cookie, p1, log_len);
4563 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004564 }
4565
4566 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004567 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4568 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004569 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004570 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004571
4572 /* If the cookie is in insert mode on a known server, we'll delete
4573 * this occurrence because we'll insert another one later.
4574 * We'll delete it too if the "indirect" option is set and we're in
4575 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004576 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4577 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004578 /* this header must be deleted */
4579 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4580 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4581 txn->hdr_idx.used--;
4582 cur_hdr->len = 0;
4583 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004584 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004585
Willy Tarreau3d300592007-03-18 18:34:41 +01004586 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004587 }
4588 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004589 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004590 /* replace bytes p3->p4 with the cookie name associated
4591 * with this server since we know it.
4592 */
4593 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4594 cur_hdr->len += delta;
4595 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004596 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004597
Willy Tarreau3d300592007-03-18 18:34:41 +01004598 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004599 }
4600 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004601 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004602 /* insert the cookie name associated with this server
4603 * before existing cookie, and insert a delimitor between them..
4604 */
4605 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4606 cur_hdr->len += delta;
4607 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004608 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004609
4610 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004611 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004612 }
4613 }
4614 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01004615 else if (t->be->appsession_name != NULL) {
4616 int cmp_len, value_len;
4617 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004618
Cyril Bontéb21570a2009-11-29 20:04:48 +01004619 if (t->be->options2 & PR_O2_AS_PFX) {
4620 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
4621 value_begin = p1 + t->be->appsession_name_len;
4622 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
4623 } else {
4624 cmp_len = p2 - p1;
4625 value_begin = p3;
4626 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004627 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01004628
4629 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
4630 /* Cool... it's the right one */
4631 if (t->sessid != NULL) {
4632 /* free previously allocated memory as we don't need it anymore */
4633 pool_free2(apools.sessid, t->sessid);
4634 }
4635 /* Store the sessid in the session for future use */
4636 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
4637 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4638 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4639 return;
4640 }
4641 memcpy(t->sessid, value_begin, value_len);
4642 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004643 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01004644 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004645 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4646 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004647 /* keep the link from this header to next one */
4648 old_idx = cur_idx;
4649 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004650
4651 if (t->sessid != NULL) {
4652 appsess *asession = NULL;
4653 /* only do insert, if lookup fails */
4654 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
4655 if (asession == NULL) {
4656 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
4657 Alert("Not enough Memory process_srv():asession:calloc().\n");
4658 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4659 return;
4660 }
4661 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
4662 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4663 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4664 return;
4665 }
4666 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
4667 asession->sessid[t->be->appsession_len] = 0;
4668
4669 size_t server_id_len = strlen(t->srv->id) + 1;
4670 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
4671 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4672 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4673 return;
4674 }
4675 asession->serverid[0] = '\0';
4676 memcpy(asession->serverid, t->srv->id, server_id_len);
4677
4678 asession->request_count = 0;
4679 appsession_hash_insert(&(t->be->htbl_proxy), asession);
4680 }
4681
4682 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
4683 asession->request_count++;
4684 }
4685
4686#if defined(DEBUG_HASH)
4687 Alert("manage_server_side_cookies\n");
4688 appsession_hash_dump(&(t->be->htbl_proxy));
4689#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01004690}
4691
4692
4693
4694/*
4695 * Check if response is cacheable or not. Updates t->flags.
4696 */
4697void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4698{
4699 struct http_txn *txn = &t->txn;
4700 char *p1, *p2;
4701
4702 char *cur_ptr, *cur_end, *cur_next;
4703 int cur_idx;
4704
Willy Tarreau5df51872007-11-25 16:20:08 +01004705 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004706 return;
4707
4708 /* Iterate through the headers.
4709 * we start with the start line.
4710 */
4711 cur_idx = 0;
4712 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4713
4714 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4715 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004716 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004717
4718 cur_hdr = &txn->hdr_idx.v[cur_idx];
4719 cur_ptr = cur_next;
4720 cur_end = cur_ptr + cur_hdr->len;
4721 cur_next = cur_end + cur_hdr->cr + 1;
4722
4723 /* We have one full header between cur_ptr and cur_end, and the
4724 * next header starts at cur_next. We're only interested in
4725 * "Cookie:" headers.
4726 */
4727
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004728 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4729 if (val) {
4730 if ((cur_end - (cur_ptr + val) >= 8) &&
4731 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4732 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4733 return;
4734 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004735 }
4736
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004737 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4738 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004739 continue;
4740
4741 /* OK, right now we know we have a cache-control header at cur_ptr */
4742
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004743 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004744
4745 if (p1 >= cur_end) /* no more info */
4746 continue;
4747
4748 /* p1 is at the beginning of the value */
4749 p2 = p1;
4750
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004751 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004752 p2++;
4753
4754 /* we have a complete value between p1 and p2 */
4755 if (p2 < cur_end && *p2 == '=') {
4756 /* we have something of the form no-cache="set-cookie" */
4757 if ((cur_end - p1 >= 21) &&
4758 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4759 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004760 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004761 continue;
4762 }
4763
4764 /* OK, so we know that either p2 points to the end of string or to a comma */
4765 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4766 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4767 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4768 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004769 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004770 return;
4771 }
4772
4773 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004774 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004775 continue;
4776 }
4777 }
4778}
4779
4780
Willy Tarreau58f10d72006-12-04 02:26:12 +01004781/*
4782 * Try to retrieve a known appsession in the URI, then the associated server.
4783 * If the server is found, it's assigned to the session.
4784 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004785void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004786{
Cyril Bontéb21570a2009-11-29 20:04:48 +01004787 char *end_params, *first_param, *cur_param, *next_param;
4788 char separator;
4789 int value_len;
4790
4791 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004792
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004793 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01004794 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004795 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01004796 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004797
Cyril Bontéb21570a2009-11-29 20:04:48 +01004798 first_param = NULL;
4799 switch (mode) {
4800 case PR_O2_AS_M_PP:
4801 first_param = memchr(begin, ';', len);
4802 break;
4803 case PR_O2_AS_M_QS:
4804 first_param = memchr(begin, '?', len);
4805 break;
4806 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004807
Cyril Bontéb21570a2009-11-29 20:04:48 +01004808 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004809 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01004810 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004811
Cyril Bontéb21570a2009-11-29 20:04:48 +01004812 switch (mode) {
4813 case PR_O2_AS_M_PP:
4814 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
4815 end_params = (char *) begin + len;
4816 }
4817 separator = ';';
4818 break;
4819 case PR_O2_AS_M_QS:
4820 end_params = (char *) begin + len;
4821 separator = '&';
4822 break;
4823 default:
4824 /* unknown mode, shouldn't happen */
4825 return;
4826 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004827
Cyril Bontéb21570a2009-11-29 20:04:48 +01004828 cur_param = next_param = end_params;
4829 while (cur_param > first_param) {
4830 cur_param--;
4831 if ((cur_param[0] == separator) || (cur_param == first_param)) {
4832 /* let's see if this is the appsession parameter */
4833 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
4834 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
4835 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
4836 /* Cool... it's the right one */
4837 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
4838 value_len = MIN(t->be->appsession_len, next_param - cur_param);
4839 if (value_len > 0) {
4840 manage_client_side_appsession(t, cur_param, value_len);
4841 }
4842 break;
4843 }
4844 next_param = cur_param;
4845 }
4846 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004847#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004848 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004849 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004850#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004851}
4852
4853
Willy Tarreaub2513902006-12-17 14:52:38 +01004854/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004855 * In a GET or HEAD request, check if the requested URI matches the stats uri
4856 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004857 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004858 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004859 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02004860 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004861 *
4862 * Returns 1 if the session's state changes, otherwise 0.
4863 */
4864int stats_check_uri_auth(struct session *t, struct proxy *backend)
4865{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004866 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004867 struct uri_auth *uri_auth = backend->uri_auth;
4868 struct user_auth *user;
4869 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004870 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004871
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004872 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
4873
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004874 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004875 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004876 return 0;
4877
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004878 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004879
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004880 /* the URI is in h */
4881 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004882 return 0;
4883
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004884 h += uri_auth->uri_len;
4885 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4886 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004887 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004888 break;
4889 }
4890 h++;
4891 }
4892
4893 if (uri_auth->refresh) {
4894 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4895 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4896 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004897 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004898 break;
4899 }
4900 h++;
4901 }
4902 }
4903
Willy Tarreau55bb8452007-10-17 18:44:57 +02004904 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4905 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4906 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004907 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02004908 break;
4909 }
4910 h++;
4911 }
4912
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004913 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
4914
Willy Tarreaub2513902006-12-17 14:52:38 +01004915 /* we are in front of a interceptable URI. Let's check
4916 * if there's an authentication and if it's valid.
4917 */
4918 user = uri_auth->users;
4919 if (!user) {
4920 /* no user auth required, it's OK */
4921 authenticated = 1;
4922 } else {
4923 authenticated = 0;
4924
4925 /* a user list is defined, we have to check.
4926 * skip 21 chars for "Authorization: Basic ".
4927 */
4928
4929 /* FIXME: this should move to an earlier place */
4930 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004931 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4932 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4933 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004934 if (len > 14 &&
4935 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02004936 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01004937 break;
4938 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004939 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004940 }
4941
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004942 if (txn->auth_hdr.len < 21 ||
4943 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004944 user = NULL;
4945
4946 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004947 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4948 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004949 user->user_pwd, user->user_len)) {
4950 authenticated = 1;
4951 break;
4952 }
4953 user = user->next;
4954 }
4955 }
4956
4957 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004958 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004959
4960 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004961 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
4962 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004963 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01004964 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02004965 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004966 if (!(t->flags & SN_ERR_MASK))
4967 t->flags |= SN_ERR_PRXCOND;
4968 if (!(t->flags & SN_FINST_MASK))
4969 t->flags |= SN_FINST_R;
4970 return 1;
4971 }
4972
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004973 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01004974 * data.
4975 */
Willy Tarreau70089872008-06-13 21:12:51 +02004976 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01004977 t->data_source = DATA_SRC_STATS;
4978 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02004979 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02004980 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
4981 t->rep->prod->private = t;
4982 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004983 return 1;
4984}
4985
Willy Tarreau4076a152009-04-02 15:18:36 +02004986/*
4987 * Capture a bad request or response and archive it in the proxy's structure.
4988 */
4989void http_capture_bad_message(struct error_snapshot *es, struct session *s,
4990 struct buffer *buf, struct http_msg *msg,
4991 struct proxy *other_end)
4992{
Willy Tarreau2df8d712009-05-01 11:33:17 +02004993 es->len = buf->r - (buf->data + msg->som);
4994 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02004995 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02004996 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02004997 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02004998 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02004999 es->when = date; // user-visible date
5000 es->sid = s->uniq_id;
5001 es->srv = s->srv;
5002 es->oe = other_end;
5003 es->src = s->cli_addr;
5004}
Willy Tarreaub2513902006-12-17 14:52:38 +01005005
Willy Tarreaubaaee002006-06-26 02:48:02 +02005006/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005007 * Print a debug line with a header
5008 */
5009void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5010{
5011 int len, max;
5012 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005013 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005014 max = end - start;
5015 UBOUND(max, sizeof(trash) - len - 1);
5016 len += strlcpy2(trash + len, start, max + 1);
5017 trash[len++] = '\n';
5018 write(1, trash, len);
5019}
5020
5021
Willy Tarreau8797c062007-05-07 00:55:35 +02005022/************************************************************************/
5023/* The code below is dedicated to ACL parsing and matching */
5024/************************************************************************/
5025
5026
5027
5028
5029/* 1. Check on METHOD
5030 * We use the pre-parsed method if it is known, and store its number as an
5031 * integer. If it is unknown, we use the pointer and the length.
5032 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005033static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005034{
5035 int len, meth;
5036
Willy Tarreauae8b7962007-06-09 23:10:04 +02005037 len = strlen(*text);
5038 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005039
5040 pattern->val.i = meth;
5041 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005042 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005043 if (!pattern->ptr.str)
5044 return 0;
5045 pattern->len = len;
5046 }
5047 return 1;
5048}
5049
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005050static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005051acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5052 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005053{
5054 int meth;
5055 struct http_txn *txn = l7;
5056
Willy Tarreaub6866442008-07-14 23:54:42 +02005057 if (!txn)
5058 return 0;
5059
Willy Tarreau655dce92009-11-08 13:10:58 +01005060 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005061 return 0;
5062
Willy Tarreau8797c062007-05-07 00:55:35 +02005063 meth = txn->meth;
5064 test->i = meth;
5065 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005066 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5067 /* ensure the indexes are not affected */
5068 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005069 test->len = txn->req.sl.rq.m_l;
5070 test->ptr = txn->req.sol;
5071 }
5072 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5073 return 1;
5074}
5075
5076static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5077{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005078 int icase;
5079
Willy Tarreau8797c062007-05-07 00:55:35 +02005080 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005081 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005082
5083 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005084 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005085
5086 /* Other method, we must compare the strings */
5087 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005088 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005089
5090 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5091 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5092 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005093 return ACL_PAT_FAIL;
5094 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005095}
5096
5097/* 2. Check on Request/Status Version
5098 * We simply compare strings here.
5099 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005100static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005101{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005102 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005103 if (!pattern->ptr.str)
5104 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005105 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005106 return 1;
5107}
5108
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005109static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005110acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5111 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005112{
5113 struct http_txn *txn = l7;
5114 char *ptr;
5115 int len;
5116
Willy Tarreaub6866442008-07-14 23:54:42 +02005117 if (!txn)
5118 return 0;
5119
Willy Tarreau655dce92009-11-08 13:10:58 +01005120 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005121 return 0;
5122
Willy Tarreau8797c062007-05-07 00:55:35 +02005123 len = txn->req.sl.rq.v_l;
5124 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5125
5126 while ((len-- > 0) && (*ptr++ != '/'));
5127 if (len <= 0)
5128 return 0;
5129
5130 test->ptr = ptr;
5131 test->len = len;
5132
5133 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5134 return 1;
5135}
5136
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005137static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005138acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5139 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005140{
5141 struct http_txn *txn = l7;
5142 char *ptr;
5143 int len;
5144
Willy Tarreaub6866442008-07-14 23:54:42 +02005145 if (!txn)
5146 return 0;
5147
Willy Tarreau655dce92009-11-08 13:10:58 +01005148 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005149 return 0;
5150
Willy Tarreau8797c062007-05-07 00:55:35 +02005151 len = txn->rsp.sl.st.v_l;
5152 ptr = txn->rsp.sol;
5153
5154 while ((len-- > 0) && (*ptr++ != '/'));
5155 if (len <= 0)
5156 return 0;
5157
5158 test->ptr = ptr;
5159 test->len = len;
5160
5161 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5162 return 1;
5163}
5164
5165/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005166static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005167acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5168 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005169{
5170 struct http_txn *txn = l7;
5171 char *ptr;
5172 int len;
5173
Willy Tarreaub6866442008-07-14 23:54:42 +02005174 if (!txn)
5175 return 0;
5176
Willy Tarreau655dce92009-11-08 13:10:58 +01005177 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005178 return 0;
5179
Willy Tarreau8797c062007-05-07 00:55:35 +02005180 len = txn->rsp.sl.st.c_l;
5181 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5182
5183 test->i = __strl2ui(ptr, len);
5184 test->flags = ACL_TEST_F_VOL_1ST;
5185 return 1;
5186}
5187
5188/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005189static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005190acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5191 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005192{
5193 struct http_txn *txn = l7;
5194
Willy Tarreaub6866442008-07-14 23:54:42 +02005195 if (!txn)
5196 return 0;
5197
Willy Tarreau655dce92009-11-08 13:10:58 +01005198 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005199 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005200
Willy Tarreauc11416f2007-06-17 16:58:38 +02005201 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5202 /* ensure the indexes are not affected */
5203 return 0;
5204
Willy Tarreau8797c062007-05-07 00:55:35 +02005205 test->len = txn->req.sl.rq.u_l;
5206 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5207
Willy Tarreauf3d25982007-05-08 22:45:09 +02005208 /* we do not need to set READ_ONLY because the data is in a buffer */
5209 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005210 return 1;
5211}
5212
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005213static int
5214acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5215 struct acl_expr *expr, struct acl_test *test)
5216{
5217 struct http_txn *txn = l7;
5218
Willy Tarreaub6866442008-07-14 23:54:42 +02005219 if (!txn)
5220 return 0;
5221
Willy Tarreau655dce92009-11-08 13:10:58 +01005222 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005223 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005224
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005225 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5226 /* ensure the indexes are not affected */
5227 return 0;
5228
5229 /* Parse HTTP request */
5230 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5231 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5232 test->i = AF_INET;
5233
5234 /*
5235 * If we are parsing url in frontend space, we prepare backend stage
5236 * to not parse again the same url ! optimization lazyness...
5237 */
5238 if (px->options & PR_O_HTTP_PROXY)
5239 l4->flags |= SN_ADDR_SET;
5240
5241 test->flags = ACL_TEST_F_READ_ONLY;
5242 return 1;
5243}
5244
5245static int
5246acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5247 struct acl_expr *expr, struct acl_test *test)
5248{
5249 struct http_txn *txn = l7;
5250
Willy Tarreaub6866442008-07-14 23:54:42 +02005251 if (!txn)
5252 return 0;
5253
Willy Tarreau655dce92009-11-08 13:10:58 +01005254 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005255 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005256
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005257 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5258 /* ensure the indexes are not affected */
5259 return 0;
5260
5261 /* Same optimization as url_ip */
5262 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5263 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5264
5265 if (px->options & PR_O_HTTP_PROXY)
5266 l4->flags |= SN_ADDR_SET;
5267
5268 test->flags = ACL_TEST_F_READ_ONLY;
5269 return 1;
5270}
5271
Willy Tarreauc11416f2007-06-17 16:58:38 +02005272/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5273 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5274 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005275static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005276acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005277 struct acl_expr *expr, struct acl_test *test)
5278{
5279 struct http_txn *txn = l7;
5280 struct hdr_idx *idx = &txn->hdr_idx;
5281 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005282
Willy Tarreaub6866442008-07-14 23:54:42 +02005283 if (!txn)
5284 return 0;
5285
Willy Tarreau33a7e692007-06-10 19:45:56 +02005286 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5287 /* search for header from the beginning */
5288 ctx->idx = 0;
5289
Willy Tarreau33a7e692007-06-10 19:45:56 +02005290 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5291 test->flags |= ACL_TEST_F_FETCH_MORE;
5292 test->flags |= ACL_TEST_F_VOL_HDR;
5293 test->len = ctx->vlen;
5294 test->ptr = (char *)ctx->line + ctx->val;
5295 return 1;
5296 }
5297
5298 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5299 test->flags |= ACL_TEST_F_VOL_HDR;
5300 return 0;
5301}
5302
Willy Tarreau33a7e692007-06-10 19:45:56 +02005303static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005304acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5305 struct acl_expr *expr, struct acl_test *test)
5306{
5307 struct http_txn *txn = l7;
5308
Willy Tarreaub6866442008-07-14 23:54:42 +02005309 if (!txn)
5310 return 0;
5311
Willy Tarreau655dce92009-11-08 13:10:58 +01005312 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005313 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005314
Willy Tarreauc11416f2007-06-17 16:58:38 +02005315 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5316 /* ensure the indexes are not affected */
5317 return 0;
5318
5319 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5320}
5321
5322static int
5323acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5324 struct acl_expr *expr, struct acl_test *test)
5325{
5326 struct http_txn *txn = l7;
5327
Willy Tarreaub6866442008-07-14 23:54:42 +02005328 if (!txn)
5329 return 0;
5330
Willy Tarreau655dce92009-11-08 13:10:58 +01005331 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005332 return 0;
5333
5334 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5335}
5336
5337/* 6. Check on HTTP header count. The number of occurrences is returned.
5338 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5339 */
5340static int
5341acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005342 struct acl_expr *expr, struct acl_test *test)
5343{
5344 struct http_txn *txn = l7;
5345 struct hdr_idx *idx = &txn->hdr_idx;
5346 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005347 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005348
Willy Tarreaub6866442008-07-14 23:54:42 +02005349 if (!txn)
5350 return 0;
5351
Willy Tarreau33a7e692007-06-10 19:45:56 +02005352 ctx.idx = 0;
5353 cnt = 0;
5354 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5355 cnt++;
5356
5357 test->i = cnt;
5358 test->flags = ACL_TEST_F_VOL_HDR;
5359 return 1;
5360}
5361
Willy Tarreauc11416f2007-06-17 16:58:38 +02005362static int
5363acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5364 struct acl_expr *expr, struct acl_test *test)
5365{
5366 struct http_txn *txn = l7;
5367
Willy Tarreaub6866442008-07-14 23:54:42 +02005368 if (!txn)
5369 return 0;
5370
Willy Tarreau655dce92009-11-08 13:10:58 +01005371 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005372 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005373
Willy Tarreauc11416f2007-06-17 16:58:38 +02005374 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5375 /* ensure the indexes are not affected */
5376 return 0;
5377
5378 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5379}
5380
5381static int
5382acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5383 struct acl_expr *expr, struct acl_test *test)
5384{
5385 struct http_txn *txn = l7;
5386
Willy Tarreaub6866442008-07-14 23:54:42 +02005387 if (!txn)
5388 return 0;
5389
Willy Tarreau655dce92009-11-08 13:10:58 +01005390 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005391 return 0;
5392
5393 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5394}
5395
Willy Tarreau33a7e692007-06-10 19:45:56 +02005396/* 7. Check on HTTP header's integer value. The integer value is returned.
5397 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005398 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005399 */
5400static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005401acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005402 struct acl_expr *expr, struct acl_test *test)
5403{
5404 struct http_txn *txn = l7;
5405 struct hdr_idx *idx = &txn->hdr_idx;
5406 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005407
Willy Tarreaub6866442008-07-14 23:54:42 +02005408 if (!txn)
5409 return 0;
5410
Willy Tarreau33a7e692007-06-10 19:45:56 +02005411 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5412 /* search for header from the beginning */
5413 ctx->idx = 0;
5414
Willy Tarreau33a7e692007-06-10 19:45:56 +02005415 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5416 test->flags |= ACL_TEST_F_FETCH_MORE;
5417 test->flags |= ACL_TEST_F_VOL_HDR;
5418 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5419 return 1;
5420 }
5421
5422 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5423 test->flags |= ACL_TEST_F_VOL_HDR;
5424 return 0;
5425}
5426
Willy Tarreauc11416f2007-06-17 16:58:38 +02005427static int
5428acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5429 struct acl_expr *expr, struct acl_test *test)
5430{
5431 struct http_txn *txn = l7;
5432
Willy Tarreaub6866442008-07-14 23:54:42 +02005433 if (!txn)
5434 return 0;
5435
Willy Tarreau655dce92009-11-08 13:10:58 +01005436 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005437 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005438
Willy Tarreauc11416f2007-06-17 16:58:38 +02005439 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5440 /* ensure the indexes are not affected */
5441 return 0;
5442
5443 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5444}
5445
5446static int
5447acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5448 struct acl_expr *expr, struct acl_test *test)
5449{
5450 struct http_txn *txn = l7;
5451
Willy Tarreaub6866442008-07-14 23:54:42 +02005452 if (!txn)
5453 return 0;
5454
Willy Tarreau655dce92009-11-08 13:10:58 +01005455 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005456 return 0;
5457
5458 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5459}
5460
Willy Tarreau106f9792009-09-19 07:54:16 +02005461/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
5462 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5463 */
5464static int
5465acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
5466 struct acl_expr *expr, struct acl_test *test)
5467{
5468 struct http_txn *txn = l7;
5469 struct hdr_idx *idx = &txn->hdr_idx;
5470 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
5471
5472 if (!txn)
5473 return 0;
5474
5475 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5476 /* search for header from the beginning */
5477 ctx->idx = 0;
5478
5479 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5480 test->flags |= ACL_TEST_F_FETCH_MORE;
5481 test->flags |= ACL_TEST_F_VOL_HDR;
5482 /* Same optimization as url_ip */
5483 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
5484 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
5485 test->ptr = (void *)&l4->srv_addr.sin_addr;
5486 test->i = AF_INET;
5487 return 1;
5488 }
5489
5490 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5491 test->flags |= ACL_TEST_F_VOL_HDR;
5492 return 0;
5493}
5494
5495static int
5496acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5497 struct acl_expr *expr, struct acl_test *test)
5498{
5499 struct http_txn *txn = l7;
5500
5501 if (!txn)
5502 return 0;
5503
Willy Tarreau655dce92009-11-08 13:10:58 +01005504 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02005505 return 0;
5506
5507 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5508 /* ensure the indexes are not affected */
5509 return 0;
5510
5511 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
5512}
5513
5514static int
5515acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5516 struct acl_expr *expr, struct acl_test *test)
5517{
5518 struct http_txn *txn = l7;
5519
5520 if (!txn)
5521 return 0;
5522
Willy Tarreau655dce92009-11-08 13:10:58 +01005523 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02005524 return 0;
5525
5526 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
5527}
5528
Willy Tarreau737b0c12007-06-10 21:28:46 +02005529/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5530 * the first '/' after the possible hostname, and ends before the possible '?'.
5531 */
5532static int
5533acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5534 struct acl_expr *expr, struct acl_test *test)
5535{
5536 struct http_txn *txn = l7;
5537 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005538
Willy Tarreaub6866442008-07-14 23:54:42 +02005539 if (!txn)
5540 return 0;
5541
Willy Tarreau655dce92009-11-08 13:10:58 +01005542 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005543 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005544
Willy Tarreauc11416f2007-06-17 16:58:38 +02005545 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5546 /* ensure the indexes are not affected */
5547 return 0;
5548
Willy Tarreau21d2af32008-02-14 20:25:24 +01005549 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5550 ptr = http_get_path(txn);
5551 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005552 return 0;
5553
5554 /* OK, we got the '/' ! */
5555 test->ptr = ptr;
5556
5557 while (ptr < end && *ptr != '?')
5558 ptr++;
5559
5560 test->len = ptr - test->ptr;
5561
5562 /* we do not need to set READ_ONLY because the data is in a buffer */
5563 test->flags = ACL_TEST_F_VOL_1ST;
5564 return 1;
5565}
5566
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005567static int
5568acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
5569 struct acl_expr *expr, struct acl_test *test)
5570{
5571 struct buffer *req = s->req;
5572 struct http_txn *txn = &s->txn;
5573 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02005574
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005575 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
5576 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
5577 */
5578
5579 if (!s || !req)
5580 return 0;
5581
Willy Tarreau655dce92009-11-08 13:10:58 +01005582 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005583 /* Already decoded as OK */
5584 test->flags |= ACL_TEST_F_SET_RES_PASS;
5585 return 1;
5586 }
5587
5588 /* Try to decode HTTP request */
5589 if (likely(req->lr < req->r))
5590 http_msg_analyzer(req, msg, &txn->hdr_idx);
5591
Willy Tarreau655dce92009-11-08 13:10:58 +01005592 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005593 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
5594 test->flags |= ACL_TEST_F_SET_RES_FAIL;
5595 return 1;
5596 }
5597 /* wait for final state */
5598 test->flags |= ACL_TEST_F_MAY_CHANGE;
5599 return 0;
5600 }
5601
5602 /* OK we got a valid HTTP request. We have some minor preparation to
5603 * perform so that further checks can rely on HTTP tests.
5604 */
5605 msg->sol = req->data + msg->som;
5606 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
5607 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
5608 s->flags |= SN_REDIRECTABLE;
5609
5610 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
5611 test->flags |= ACL_TEST_F_SET_RES_FAIL;
5612 return 1;
5613 }
5614
5615 test->flags |= ACL_TEST_F_SET_RES_PASS;
5616 return 1;
5617}
5618
Willy Tarreau8797c062007-05-07 00:55:35 +02005619
5620/************************************************************************/
5621/* All supported keywords must be declared here. */
5622/************************************************************************/
5623
5624/* Note: must not be declared <const> as its list will be overwritten */
5625static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005626 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
5627
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005628 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5629 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5630 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5631 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005632
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005633 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5634 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5635 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5636 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5637 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5638 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5639 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5640 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5641 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005642
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005643 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5644 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5645 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5646 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5647 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5648 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5649 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5650 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5651 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5652 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02005653 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005654
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005655 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5656 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5657 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5658 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5659 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5660 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5661 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5662 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5663 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02005664 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005665
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005666 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5667 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5668 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5669 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5670 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5671 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5672 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005673
Willy Tarreauf3d25982007-05-08 22:45:09 +02005674 { NULL, NULL, NULL, NULL },
5675
5676#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005677 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5678 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5679 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5680 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5681 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5682 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5683 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5684
Willy Tarreau8797c062007-05-07 00:55:35 +02005685 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5686 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5687 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5688 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5689 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5690 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5691 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5692 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5693
5694 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5695 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5696 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5697 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5698 { NULL, NULL, NULL, NULL },
5699#endif
5700}};
5701
5702
5703__attribute__((constructor))
5704static void __http_protocol_init(void)
5705{
5706 acl_register_keywords(&acl_kws);
5707}
5708
5709
Willy Tarreau58f10d72006-12-04 02:26:12 +01005710/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005711 * Local variables:
5712 * c-indent-level: 8
5713 * c-basic-offset: 8
5714 * End:
5715 */