blob: e1c4414b028a384928d67bdf2f66fa77e5abc989 [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
Willy Tarreau15de77e2010-01-02 21:59:16 +01001384 * fields. Note that msg->som and msg->sol will be initialized after completing
1385 * the first state, so that none of the msg pointers has to be initialized
1386 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001387 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1389{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001390 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001391 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001392
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001393 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 ptr = buf->lr;
1395 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 if (unlikely(ptr >= end))
1398 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001399
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001401 /*
1402 * First, states that are specific to the response only.
1403 * We check them first so that request and headers are
1404 * closer to each other (accessed more often).
1405 */
1406 http_msg_rpbefore:
1407 case HTTP_MSG_RPBEFORE:
1408 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001409 /* we have a start of message, but we have to check
1410 * first if we need to remove some CRLF. We can only
1411 * do this when send_max=0.
1412 */
1413 char *beg = buf->w + buf->send_max;
1414 if (beg >= buf->data + buf->size)
1415 beg -= buf->size;
1416 if (unlikely(ptr != beg)) {
1417 if (buf->send_max)
1418 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001419 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001420 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001421 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001422 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001423 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001424 hdr_idx_init(idx);
1425 state = HTTP_MSG_RPVER;
1426 goto http_msg_rpver;
1427 }
1428
1429 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1430 goto http_msg_invalid;
1431
1432 if (unlikely(*ptr == '\n'))
1433 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1434 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1435 /* stop here */
1436
1437 http_msg_rpbefore_cr:
1438 case HTTP_MSG_RPBEFORE_CR:
1439 EXPECT_LF_HERE(ptr, http_msg_invalid);
1440 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1441 /* stop here */
1442
1443 http_msg_rpver:
1444 case HTTP_MSG_RPVER:
1445 case HTTP_MSG_RPVER_SP:
1446 case HTTP_MSG_RPCODE:
1447 case HTTP_MSG_RPCODE_SP:
1448 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001449 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001450 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001451 if (unlikely(!ptr))
1452 return;
1453
1454 /* we have a full response and we know that we have either a CR
1455 * or an LF at <ptr>.
1456 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001457 //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 +01001458 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1459
1460 msg->sol = ptr;
1461 if (likely(*ptr == '\r'))
1462 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1463 goto http_msg_rpline_end;
1464
1465 http_msg_rpline_end:
1466 case HTTP_MSG_RPLINE_END:
1467 /* msg->sol must point to the first of CR or LF. */
1468 EXPECT_LF_HERE(ptr, http_msg_invalid);
1469 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1470 /* stop here */
1471
1472 /*
1473 * Second, states that are specific to the request only
1474 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 http_msg_rqbefore:
1476 case HTTP_MSG_RQBEFORE:
1477 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001478 /* we have a start of message, but we have to check
1479 * first if we need to remove some CRLF. We can only
1480 * do this when send_max=0.
1481 */
1482 char *beg = buf->w + buf->send_max;
1483 if (beg >= buf->data + buf->size)
1484 beg -= buf->size;
1485 if (likely(ptr != beg)) {
1486 if (buf->send_max)
1487 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001488 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001489 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001491 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001492 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001493 /* we will need this when keep-alive will be supported
1494 hdr_idx_init(idx);
1495 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001496 state = HTTP_MSG_RQMETH;
1497 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1501 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001502
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 if (unlikely(*ptr == '\n'))
1504 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1505 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001506 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001507
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 http_msg_rqbefore_cr:
1509 case HTTP_MSG_RQBEFORE_CR:
1510 EXPECT_LF_HERE(ptr, http_msg_invalid);
1511 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001512 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 http_msg_rqmeth:
1515 case HTTP_MSG_RQMETH:
1516 case HTTP_MSG_RQMETH_SP:
1517 case HTTP_MSG_RQURI:
1518 case HTTP_MSG_RQURI_SP:
1519 case HTTP_MSG_RQVER:
1520 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001521 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 if (unlikely(!ptr))
1523 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 /* we have a full request and we know that we have either a CR
1526 * or an LF at <ptr>.
1527 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001528 //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 +01001529 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 msg->sol = ptr;
1532 if (likely(*ptr == '\r'))
1533 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001535
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 http_msg_rqline_end:
1537 case HTTP_MSG_RQLINE_END:
1538 /* check for HTTP/0.9 request : no version information available.
1539 * msg->sol must point to the first of CR or LF.
1540 */
1541 if (unlikely(msg->sl.rq.v_l == 0))
1542 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001543
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001544 EXPECT_LF_HERE(ptr, http_msg_invalid);
1545 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001546 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001547
Willy Tarreau8973c702007-01-21 23:58:29 +01001548 /*
1549 * Common states below
1550 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 http_msg_hdr_first:
1552 case HTTP_MSG_HDR_FIRST:
1553 msg->sol = ptr;
1554 if (likely(!HTTP_IS_CRLF(*ptr))) {
1555 goto http_msg_hdr_name;
1556 }
1557
1558 if (likely(*ptr == '\r'))
1559 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1560 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 http_msg_hdr_name:
1563 case HTTP_MSG_HDR_NAME:
1564 /* assumes msg->sol points to the first char */
1565 if (likely(HTTP_IS_TOKEN(*ptr)))
1566 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 if (likely(*ptr == ':')) {
1569 msg->col = ptr - buf->data;
1570 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1571 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001572
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001573 if (likely(msg->err_pos < -1) || *ptr == '\n')
1574 goto http_msg_invalid;
1575
1576 if (msg->err_pos == -1) /* capture error pointer */
1577 msg->err_pos = ptr - buf->data; /* >= 0 now */
1578
1579 /* and we still accept this non-token character */
1580 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001581
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 http_msg_hdr_l1_sp:
1583 case HTTP_MSG_HDR_L1_SP:
1584 /* assumes msg->sol points to the first char and msg->col to the colon */
1585 if (likely(HTTP_IS_SPHT(*ptr)))
1586 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001587
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 /* header value can be basically anything except CR/LF */
1589 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001590
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 if (likely(!HTTP_IS_CRLF(*ptr))) {
1592 goto http_msg_hdr_val;
1593 }
1594
1595 if (likely(*ptr == '\r'))
1596 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1597 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001598
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 http_msg_hdr_l1_lf:
1600 case HTTP_MSG_HDR_L1_LF:
1601 EXPECT_LF_HERE(ptr, http_msg_invalid);
1602 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001603
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001604 http_msg_hdr_l1_lws:
1605 case HTTP_MSG_HDR_L1_LWS:
1606 if (likely(HTTP_IS_SPHT(*ptr))) {
1607 /* replace HT,CR,LF with spaces */
1608 for (; buf->data+msg->sov < ptr; msg->sov++)
1609 buf->data[msg->sov] = ' ';
1610 goto http_msg_hdr_l1_sp;
1611 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001612 /* we had a header consisting only in spaces ! */
1613 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 goto http_msg_complete_header;
1615
1616 http_msg_hdr_val:
1617 case HTTP_MSG_HDR_VAL:
1618 /* assumes msg->sol points to the first char, msg->col to the
1619 * colon, and msg->sov points to the first character of the
1620 * value.
1621 */
1622 if (likely(!HTTP_IS_CRLF(*ptr)))
1623 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001624
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 msg->eol = ptr;
1626 /* Note: we could also copy eol into ->eoh so that we have the
1627 * real header end in case it ends with lots of LWS, but is this
1628 * really needed ?
1629 */
1630 if (likely(*ptr == '\r'))
1631 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1632 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 http_msg_hdr_l2_lf:
1635 case HTTP_MSG_HDR_L2_LF:
1636 EXPECT_LF_HERE(ptr, http_msg_invalid);
1637 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001638
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 http_msg_hdr_l2_lws:
1640 case HTTP_MSG_HDR_L2_LWS:
1641 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1642 /* LWS: replace HT,CR,LF with spaces */
1643 for (; msg->eol < ptr; msg->eol++)
1644 *msg->eol = ' ';
1645 goto http_msg_hdr_val;
1646 }
1647 http_msg_complete_header:
1648 /*
1649 * It was a new header, so the last one is finished.
1650 * Assumes msg->sol points to the first char, msg->col to the
1651 * colon, msg->sov points to the first character of the value
1652 * and msg->eol to the first CR or LF so we know how the line
1653 * ends. We insert last header into the index.
1654 */
1655 /*
1656 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1657 write(2, msg->sol, msg->eol-msg->sol);
1658 fprintf(stderr,"\n");
1659 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001660
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001661 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1662 idx, idx->tail) < 0))
1663 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001664
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001665 msg->sol = ptr;
1666 if (likely(!HTTP_IS_CRLF(*ptr))) {
1667 goto http_msg_hdr_name;
1668 }
1669
1670 if (likely(*ptr == '\r'))
1671 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1672 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001673
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 http_msg_last_lf:
1675 case HTTP_MSG_LAST_LF:
1676 /* Assumes msg->sol points to the first of either CR or LF */
1677 EXPECT_LF_HERE(ptr, http_msg_invalid);
1678 ptr++;
1679 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001680 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001682 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001683 return;
1684#ifdef DEBUG_FULL
1685 default:
1686 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1687 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001688#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 }
1690 http_msg_ood:
1691 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001692 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 buf->lr = ptr;
1694 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001695
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001696 http_msg_invalid:
1697 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001698 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001699 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 return;
1701}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001702
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001703/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1704 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1705 * nothing is done and 1 is returned.
1706 */
1707static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1708{
1709 int delta;
1710 char *cur_end;
1711
1712 if (msg->sl.rq.v_l != 0)
1713 return 1;
1714
1715 msg->sol = req->data + msg->som;
1716 cur_end = msg->sol + msg->sl.rq.l;
1717 delta = 0;
1718
1719 if (msg->sl.rq.u_l == 0) {
1720 /* if no URI was set, add "/" */
1721 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1722 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001723 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001724 }
1725 /* add HTTP version */
1726 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001727 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001728 cur_end += delta;
1729 cur_end = (char *)http_parse_reqline(msg, req->data,
1730 HTTP_MSG_RQMETH,
1731 msg->sol, cur_end + 1,
1732 NULL, NULL);
1733 if (unlikely(!cur_end))
1734 return 0;
1735
1736 /* we have a full HTTP/1.0 request now and we know that
1737 * we have either a CR or an LF at <ptr>.
1738 */
1739 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1740 return 1;
1741}
1742
Willy Tarreau5b154472009-12-21 20:11:07 +01001743/* Parse the Connection: headaer of an HTTP request, and set the transaction
1744 * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag
1745 * is also set so that we don't parse a second time. If some dangerous values
1746 * are encountered, we leave the status to indicate that the request might be
1747 * interpreted as keep-alive, but we also set the connection flags to indicate
1748 * that we WANT it to be a close, so that the header will be fixed. This
1749 * function should only be called when we know we're interested in checking
1750 * the request (not a CONNECT, and FE or BE mangles the header).
1751 */
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001752void http_req_parse_connection_header(struct http_txn *txn)
Willy Tarreau5b154472009-12-21 20:11:07 +01001753{
1754 struct http_msg *msg = &txn->req;
1755 struct hdr_ctx ctx;
1756 int conn_cl, conn_ka;
1757
1758 if (txn->flags & TX_CON_HDR_PARS)
1759 return;
1760
1761 conn_cl = 0;
1762 conn_ka = 0;
1763 ctx.idx = 0;
1764
1765 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
1766 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
1767 conn_cl = 1;
1768 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
1769 conn_ka = 1;
1770 }
1771
1772 /* Determine if the client wishes keep-alive or close.
1773 * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections
1774 * are persistent unless "Connection: close" is explicitly specified.
1775 * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections.
1776 * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless
1777 * they explicitly specify "Connection: Keep-Alive", regardless of any
1778 * optional "Keep-Alive" header.
1779 * Note that if we find a request with both "Connection: close" and
1780 * "Connection: Keep-Alive", we indicate we want a close but don't have
1781 * it, so that it can be enforced later.
1782 */
1783
1784 if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */
1785 if (conn_cl) {
1786 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1787 if (!conn_ka)
1788 txn->flags |= TX_REQ_CONN_CLO;
1789 }
1790 } else { /* HTTP/1.0 */
1791 if (!conn_ka)
1792 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO | TX_REQ_CONN_CLO;
1793 else if (conn_cl)
1794 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1795 }
1796 txn->flags |= TX_CON_HDR_PARS;
1797}
1798
Willy Tarreaud98cf932009-12-27 22:54:55 +01001799/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1800 * first byte of body, and increments msg->sov by the number of bytes parsed,
1801 * so that we know we can forward between ->som and ->sov. Note that due to
1802 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1803 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001804 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001805 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001806 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001807int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001808{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001809 char *ptr = buf->lr;
1810 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001811 unsigned int chunk = 0;
1812
1813 /* The chunk size is in the following form, though we are only
1814 * interested in the size and CRLF :
1815 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1816 */
1817 while (1) {
1818 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001819 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001820 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001821 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001822 if (c < 0) /* not a hex digit anymore */
1823 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001824 if (++ptr >= end)
1825 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001826 if (chunk & 0xF000000) /* overflow will occur */
1827 return -1;
1828 chunk = (chunk << 4) + c;
1829 }
1830
Willy Tarreaud98cf932009-12-27 22:54:55 +01001831 /* empty size not allowed */
1832 if (ptr == buf->lr)
1833 return -1;
1834
1835 while (http_is_spht[(unsigned char)*ptr]) {
1836 if (++ptr >= end)
1837 ptr = buf->data;
1838 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001839 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001840 }
1841
Willy Tarreaud98cf932009-12-27 22:54:55 +01001842 /* Up to there, we know that at least one byte is present at *ptr. Check
1843 * for the end of chunk size.
1844 */
1845 while (1) {
1846 if (likely(HTTP_IS_CRLF(*ptr))) {
1847 /* we now have a CR or an LF at ptr */
1848 if (likely(*ptr == '\r')) {
1849 if (++ptr >= end)
1850 ptr = buf->data;
1851 if (ptr == buf->r)
1852 return 0;
1853 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001854
Willy Tarreaud98cf932009-12-27 22:54:55 +01001855 if (*ptr != '\n')
1856 return -1;
1857 if (++ptr >= end)
1858 ptr = buf->data;
1859 /* done */
1860 break;
1861 }
1862 else if (*ptr == ';') {
1863 /* chunk extension, ends at next CRLF */
1864 if (++ptr >= end)
1865 ptr = buf->data;
1866 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001867 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001868
1869 while (!HTTP_IS_CRLF(*ptr)) {
1870 if (++ptr >= end)
1871 ptr = buf->data;
1872 if (ptr == buf->r)
1873 return 0;
1874 }
1875 /* we have a CRLF now, loop above */
1876 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001877 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001878 else
Willy Tarreau115acb92009-12-26 13:56:06 +01001879 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001880 }
1881
Willy Tarreaud98cf932009-12-27 22:54:55 +01001882 /* OK we found our CRLF and now <ptr> points to the next byte,
1883 * which may or may not be present. We save that into ->lr and
1884 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001885 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001886 msg->sov += ptr - buf->lr;
1887 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01001888 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001889 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001890 return 1;
1891}
1892
Willy Tarreaud98cf932009-12-27 22:54:55 +01001893/* This function skips trailers in the buffer <buf> associated with HTTP
1894 * message <msg>. The first visited position is buf->lr. If the end of
1895 * the trailers is found, it is automatically scheduled to be forwarded,
1896 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1897 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001898 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1899 * zero. If a parse error is encountered, the function returns < 0 and does not
1900 * change anything except maybe buf->lr and msg->sov. Note that the message
1901 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1902 * which implies that all non-trailers data have already been scheduled for
1903 * forwarding, and that the difference between msg->som and msg->sov exactly
1904 * matches the length of trailers already parsed and not forwarded. It is also
1905 * important to note that this function is designed to be able to parse wrapped
1906 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001907 */
1908int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1909{
1910 /* we have buf->lr which points to next line. Look for CRLF. */
1911 while (1) {
1912 char *p1 = NULL, *p2 = NULL;
1913 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001914 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001915
1916 /* scan current line and stop at LF or CRLF */
1917 while (1) {
1918 if (ptr == buf->r)
1919 return 0;
1920
1921 if (*ptr == '\n') {
1922 if (!p1)
1923 p1 = ptr;
1924 p2 = ptr;
1925 break;
1926 }
1927
1928 if (*ptr == '\r') {
1929 if (p1)
1930 return -1;
1931 p1 = ptr;
1932 }
1933
1934 ptr++;
1935 if (ptr >= buf->data + buf->size)
1936 ptr = buf->data;
1937 }
1938
1939 /* after LF; point to beginning of next line */
1940 p2++;
1941 if (p2 >= buf->data + buf->size)
1942 p2 = buf->data;
1943
Willy Tarreau638cd022010-01-03 07:42:04 +01001944 bytes = p2 - buf->lr;
1945 if (bytes < 0)
1946 bytes += buf->size;
1947
1948 /* schedule this line for forwarding */
1949 msg->sov += bytes;
1950 if (msg->sov >= buf->size)
1951 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952
Willy Tarreau638cd022010-01-03 07:42:04 +01001953 if (p1 == buf->lr) {
1954 /* LF/CRLF at beginning of line => end of trailers at p2.
1955 * Everything was scheduled for forwarding, there's nothing
1956 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001957 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001958 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001959 msg->msg_state = HTTP_MSG_DONE;
1960 return 1;
1961 }
1962 /* OK, next line then */
1963 buf->lr = p2;
1964 }
1965}
1966
1967/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1968 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
1969 * ->som, buf->lr in order to include this part into the next forwarding phase.
1970 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1971 * not enough data are available, the function does not change anything and
1972 * returns zero. If a parse error is encountered, the function returns < 0 and
1973 * does not change anything. Note: this function is designed to parse wrapped
1974 * CRLF at the end of the buffer.
1975 */
1976int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1977{
1978 char *ptr;
1979 int bytes;
1980
1981 /* NB: we'll check data availabilty at the end. It's not a
1982 * problem because whatever we match first will be checked
1983 * against the correct length.
1984 */
1985 bytes = 1;
1986 ptr = buf->lr;
1987 if (*ptr == '\r') {
1988 bytes++;
1989 ptr++;
1990 if (ptr >= buf->data + buf->size)
1991 ptr = buf->data;
1992 }
1993
1994 if (buf->l < bytes)
1995 return 0;
1996
1997 if (*ptr != '\n')
1998 return -1;
1999
2000 ptr++;
2001 if (ptr >= buf->data + buf->size)
2002 ptr = buf->data;
2003 buf->lr = ptr;
2004 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2005 msg->sov = ptr - buf->data;
2006 msg->som = msg->sov - bytes;
2007 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2008 return 1;
2009}
Willy Tarreau5b154472009-12-21 20:11:07 +01002010
Willy Tarreau83e3af02009-12-28 17:39:57 +01002011void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2012{
2013 char *end = buf->data + buf->size;
2014 int off = buf->data + buf->size - buf->w;
2015
2016 /* two possible cases :
2017 * - the buffer is in one contiguous block, we move it in-place
2018 * - the buffer is in two blocks, we move it via the trash
2019 */
2020 if (buf->l) {
2021 int block1 = buf->l;
2022 int block2 = 0;
2023 if (buf->r <= buf->w) {
2024 /* non-contiguous block */
2025 block1 = buf->data + buf->size - buf->w;
2026 block2 = buf->r - buf->data;
2027 }
2028 if (block2)
2029 memcpy(trash, buf->data, block2);
2030 memmove(buf->data, buf->w, block1);
2031 if (block2)
2032 memcpy(buf->data + block1, trash, block2);
2033 }
2034
2035 /* adjust all known pointers */
2036 buf->w = buf->data;
2037 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2038 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2039 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2040 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2041
2042 /* adjust relative pointers */
2043 msg->som = 0;
2044 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2045 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2046 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2047
2048 msg->sl.rq.u += off; if (msg->sl.rq.u >= buf->size) msg->sl.rq.u -= buf->size;
2049 msg->sl.rq.v += off; if (msg->sl.rq.v >= buf->size) msg->sl.rq.v -= buf->size;
2050
2051 if (msg->err_pos >= 0) {
2052 msg->err_pos += off;
2053 if (msg->err_pos >= buf->size)
2054 msg->err_pos -= buf->size;
2055 }
2056
2057 buf->flags &= ~BF_FULL;
2058 if (buf->l >= buffer_max_len(buf))
2059 buf->flags |= BF_FULL;
2060}
2061
Willy Tarreaud787e662009-07-07 10:14:51 +02002062/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2063 * processing can continue on next analysers, or zero if it either needs more
2064 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2065 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2066 * when it has nothing left to do, and may remove any analyser when it wants to
2067 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002068 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002069int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002070{
Willy Tarreau59234e92008-11-30 23:51:27 +01002071 /*
2072 * We will parse the partial (or complete) lines.
2073 * We will check the request syntax, and also join multi-line
2074 * headers. An index of all the lines will be elaborated while
2075 * parsing.
2076 *
2077 * For the parsing, we use a 28 states FSM.
2078 *
2079 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002080 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002081 * req->data + msg->eoh = end of processed headers / start of current one
2082 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002083 * req->lr = first non-visited byte
2084 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002085 *
2086 * At end of parsing, we may perform a capture of the error (if any), and
2087 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2088 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2089 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002090 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002091
Willy Tarreau59234e92008-11-30 23:51:27 +01002092 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002093 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002094 struct http_txn *txn = &s->txn;
2095 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002096 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002097
Willy Tarreau6bf17362009-02-24 10:48:35 +01002098 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2099 now_ms, __FUNCTION__,
2100 s,
2101 req,
2102 req->rex, req->wex,
2103 req->flags,
2104 req->l,
2105 req->analysers);
2106
Willy Tarreau52a0c602009-08-16 22:45:38 +02002107 /* we're speaking HTTP here, so let's speak HTTP to the client */
2108 s->srv_error = http_return_srv_error;
2109
Willy Tarreau83e3af02009-12-28 17:39:57 +01002110 /* There's a protected area at the end of the buffer for rewriting
2111 * purposes. We don't want to start to parse the request if the
2112 * protected area is affected, because we may have to move processed
2113 * data later, which is much more complicated.
2114 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002115 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
2116 if (unlikely((req->flags & BF_FULL) ||
2117 req->r < req->lr ||
2118 req->r > req->data + req->size - global.tune.maxrewrite)) {
2119 if (req->send_max) {
2120 /* some data has still not left the buffer, wake us once that's done */
2121 buffer_dont_connect(req);
2122 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2123 return 0;
2124 }
2125 if (req->l <= req->size - global.tune.maxrewrite)
2126 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002127 }
2128
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002129 if (likely(req->lr < req->r))
2130 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002131 }
2132
Willy Tarreau59234e92008-11-30 23:51:27 +01002133 /* 1: we might have to print this header in debug mode */
2134 if (unlikely((global.mode & MODE_DEBUG) &&
2135 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002136 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002137 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002138
Willy Tarreau59234e92008-11-30 23:51:27 +01002139 sol = req->data + msg->som;
2140 eol = sol + msg->sl.rq.l;
2141 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002142
Willy Tarreau59234e92008-11-30 23:51:27 +01002143 sol += hdr_idx_first_pos(&txn->hdr_idx);
2144 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002145
Willy Tarreau59234e92008-11-30 23:51:27 +01002146 while (cur_idx) {
2147 eol = sol + txn->hdr_idx.v[cur_idx].len;
2148 debug_hdr("clihdr", s, sol, eol);
2149 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2150 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002151 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002152 }
2153
Willy Tarreau58f10d72006-12-04 02:26:12 +01002154
Willy Tarreau59234e92008-11-30 23:51:27 +01002155 /*
2156 * Now we quickly check if we have found a full valid request.
2157 * If not so, we check the FD and buffer states before leaving.
2158 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002159 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01002160 * requests are checked first.
2161 *
2162 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002163
Willy Tarreau655dce92009-11-08 13:10:58 +01002164 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002165 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002166 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002167 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002168 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2169 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002170
Willy Tarreau59234e92008-11-30 23:51:27 +01002171 /* 1: Since we are in header mode, if there's no space
2172 * left for headers, we won't be able to free more
2173 * later, so the session will never terminate. We
2174 * must terminate it now.
2175 */
2176 if (unlikely(req->flags & BF_FULL)) {
2177 /* FIXME: check if URI is set and return Status
2178 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002179 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002180 goto return_bad_req;
2181 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002182
Willy Tarreau59234e92008-11-30 23:51:27 +01002183 /* 2: have we encountered a read error ? */
2184 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002185 if (txn->flags & TX_NOT_FIRST)
2186 goto failed_keep_alive;
2187
Willy Tarreau59234e92008-11-30 23:51:27 +01002188 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002189 if (msg->err_pos >= 0)
2190 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002191 msg->msg_state = HTTP_MSG_ERROR;
2192 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002193
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002194 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002195 if (s->listener->counters)
2196 s->listener->counters->failed_req++;
2197
Willy Tarreau59234e92008-11-30 23:51:27 +01002198 if (!(s->flags & SN_ERR_MASK))
2199 s->flags |= SN_ERR_CLICL;
2200 if (!(s->flags & SN_FINST_MASK))
2201 s->flags |= SN_FINST_R;
2202 return 0;
2203 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002204
Willy Tarreau59234e92008-11-30 23:51:27 +01002205 /* 3: has the read timeout expired ? */
2206 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002207 if (txn->flags & TX_NOT_FIRST)
2208 goto failed_keep_alive;
2209
Willy Tarreau59234e92008-11-30 23:51:27 +01002210 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002211 if (msg->err_pos >= 0)
2212 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002213 txn->status = 408;
2214 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2215 msg->msg_state = HTTP_MSG_ERROR;
2216 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002217
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002218 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002219 if (s->listener->counters)
2220 s->listener->counters->failed_req++;
2221
Willy Tarreau59234e92008-11-30 23:51:27 +01002222 if (!(s->flags & SN_ERR_MASK))
2223 s->flags |= SN_ERR_CLITO;
2224 if (!(s->flags & SN_FINST_MASK))
2225 s->flags |= SN_FINST_R;
2226 return 0;
2227 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002228
Willy Tarreau59234e92008-11-30 23:51:27 +01002229 /* 4: have we encountered a close ? */
2230 else if (req->flags & BF_SHUTR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002231 if (txn->flags & TX_NOT_FIRST)
2232 goto failed_keep_alive;
2233
Willy Tarreau4076a152009-04-02 15:18:36 +02002234 if (msg->err_pos >= 0)
2235 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002236 txn->status = 400;
2237 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2238 msg->msg_state = HTTP_MSG_ERROR;
2239 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002240
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002241 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002242 if (s->listener->counters)
2243 s->listener->counters->failed_req++;
2244
Willy Tarreau59234e92008-11-30 23:51:27 +01002245 if (!(s->flags & SN_ERR_MASK))
2246 s->flags |= SN_ERR_CLICL;
2247 if (!(s->flags & SN_FINST_MASK))
2248 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002249 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002250 }
2251
Willy Tarreau520d95e2009-09-19 21:04:57 +02002252 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002253 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2254
Willy Tarreau59234e92008-11-30 23:51:27 +01002255 /* just set the request timeout once at the beginning of the request */
2256 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002257 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002258
Willy Tarreau59234e92008-11-30 23:51:27 +01002259 /* we're not ready yet */
2260 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002261
2262 failed_keep_alive:
2263 /* Here we process low-level errors for keep-alive requests. In
2264 * short, if the request is not the first one and it experiences
2265 * a timeout, read error or shutdown, we just silently close so
2266 * that the client can try again.
2267 */
2268 txn->status = 0;
2269 msg->msg_state = HTTP_MSG_RQBEFORE;
2270 req->analysers = 0;
2271 s->logs.logwait = 0;
2272 stream_int_cond_close(req->prod, NULL);
2273 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002274 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002275
Willy Tarreaud787e662009-07-07 10:14:51 +02002276 /* OK now we have a complete HTTP request with indexed headers. Let's
2277 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002278 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2279 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2280 * points to the CRLF of the request line. req->lr points to the first
2281 * byte after the last LF. msg->col and msg->sov point to the first
2282 * byte of data. msg->eol cannot be trusted because it may have been
2283 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002284 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002285
Willy Tarreaud787e662009-07-07 10:14:51 +02002286 /* Maybe we found in invalid header name while we were configured not
2287 * to block on that, so we have to capture it now.
2288 */
2289 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002290 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2291
Willy Tarreau59234e92008-11-30 23:51:27 +01002292 /* ensure we keep this pointer to the beginning of the message */
2293 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002294
Willy Tarreau59234e92008-11-30 23:51:27 +01002295 /*
2296 * 1: identify the method
2297 */
2298 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
2299
2300 /* we can make use of server redirect on GET and HEAD */
2301 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2302 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002303
Willy Tarreau59234e92008-11-30 23:51:27 +01002304 /*
2305 * 2: check if the URI matches the monitor_uri.
2306 * We have to do this for every request which gets in, because
2307 * the monitor-uri is defined by the frontend.
2308 */
2309 if (unlikely((s->fe->monitor_uri_len != 0) &&
2310 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2311 !memcmp(&req->data[msg->sl.rq.u],
2312 s->fe->monitor_uri,
2313 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002314 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002315 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002316 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002317 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002318
Willy Tarreau59234e92008-11-30 23:51:27 +01002319 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002320
Willy Tarreau59234e92008-11-30 23:51:27 +01002321 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002322 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2323 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002324
Willy Tarreau59234e92008-11-30 23:51:27 +01002325 ret = acl_pass(ret);
2326 if (cond->pol == ACL_COND_UNLESS)
2327 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002328
Willy Tarreau59234e92008-11-30 23:51:27 +01002329 if (ret) {
2330 /* we fail this request, let's return 503 service unavail */
2331 txn->status = 503;
2332 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2333 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002334 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002335 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002336
Willy Tarreau59234e92008-11-30 23:51:27 +01002337 /* nothing to fail, let's reply normaly */
2338 txn->status = 200;
2339 stream_int_retnclose(req->prod, &http_200_chunk);
2340 goto return_prx_cond;
2341 }
2342
2343 /*
2344 * 3: Maybe we have to copy the original REQURI for the logs ?
2345 * Note: we cannot log anymore if the request has been
2346 * classified as invalid.
2347 */
2348 if (unlikely(s->logs.logwait & LW_REQ)) {
2349 /* we have a complete HTTP request that we must log */
2350 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2351 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 if (urilen >= REQURI_LEN)
2354 urilen = REQURI_LEN - 1;
2355 memcpy(txn->uri, &req->data[msg->som], urilen);
2356 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002357
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 if (!(s->logs.logwait &= ~LW_REQ))
2359 s->do_log(s);
2360 } else {
2361 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002362 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 }
Willy Tarreau06619262006-12-17 08:37:22 +01002364
Willy Tarreau59234e92008-11-30 23:51:27 +01002365 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002366 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2367 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002368
Willy Tarreau5b154472009-12-21 20:11:07 +01002369 /* ... and check if the request is HTTP/1.1 or above */
2370 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002371 ((req->data[msg->sl.rq.v + 5] > '1') ||
2372 ((req->data[msg->sl.rq.v + 5] == '1') &&
2373 (req->data[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002374 txn->flags |= TX_REQ_VER_11;
2375
2376 /* "connection" has not been parsed yet */
2377 txn->flags &= ~TX_CON_HDR_PARS;
2378
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002379 /* transfer length unknown*/
2380 txn->flags &= ~TX_REQ_XFER_LEN;
2381
Willy Tarreau59234e92008-11-30 23:51:27 +01002382 /* 5: we may need to capture headers */
2383 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
2384 capture_headers(req->data + msg->som, &txn->hdr_idx,
2385 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002386
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002387 /* 6: determine the transfer-length.
2388 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2389 * the presence of a message-body in a REQUEST and its transfer length
2390 * must be determined that way (in order of precedence) :
2391 * 1. The presence of a message-body in a request is signaled by the
2392 * inclusion of a Content-Length or Transfer-Encoding header field
2393 * in the request's header fields. When a request message contains
2394 * both a message-body of non-zero length and a method that does
2395 * not define any semantics for that request message-body, then an
2396 * origin server SHOULD either ignore the message-body or respond
2397 * with an appropriate error message (e.g., 413). A proxy or
2398 * gateway, when presented the same request, SHOULD either forward
2399 * the request inbound with the message- body or ignore the
2400 * message-body when determining a response.
2401 *
2402 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2403 * and the "chunked" transfer-coding (Section 6.2) is used, the
2404 * transfer-length is defined by the use of this transfer-coding.
2405 * If a Transfer-Encoding header field is present and the "chunked"
2406 * transfer-coding is not present, the transfer-length is defined
2407 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002408 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002409 * 3. If a Content-Length header field is present, its decimal value in
2410 * OCTETs represents both the entity-length and the transfer-length.
2411 * If a message is received with both a Transfer-Encoding header
2412 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002413 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002414 * 4. By the server closing the connection. (Closing the connection
2415 * cannot be used to indicate the end of a request body, since that
2416 * would leave no possibility for the server to send back a response.)
2417 *
2418 * Whenever a transfer-coding is applied to a message-body, the set of
2419 * transfer-codings MUST include "chunked", unless the message indicates
2420 * it is terminated by closing the connection. When the "chunked"
2421 * transfer-coding is used, it MUST be the last transfer-coding applied
2422 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002423 */
2424
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002425 /* CONNECT sets a tunnel and ignores everything else */
2426 if (txn->meth == HTTP_METH_CONNECT)
2427 goto skip_xfer_len;
2428
2429 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002430 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002431 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002432 while ((txn->flags & TX_REQ_VER_11) &&
2433 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002434 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2435 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2436 else if (txn->flags & TX_REQ_TE_CHNK) {
2437 /* bad transfer-encoding (chunked followed by something else) */
2438 use_close_only = 1;
2439 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2440 break;
2441 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002442 }
2443
Willy Tarreau32b47f42009-10-18 20:55:02 +02002444 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002445 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002446 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2447 signed long long cl;
2448
2449 if (!ctx.vlen)
2450 goto return_bad_req;
2451
2452 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2453 goto return_bad_req; /* parse failure */
2454
2455 if (cl < 0)
2456 goto return_bad_req;
2457
2458 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2459 goto return_bad_req; /* already specified, was different */
2460
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002461 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002462 msg->hdr_content_len = cl;
2463 }
2464
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002465 /* bodyless requests have a known length */
2466 if (!use_close_only)
2467 txn->flags |= TX_REQ_XFER_LEN;
2468
2469 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002470 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002471 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002472 req->analyse_exp = TICK_ETERNITY;
2473 return 1;
2474
2475 return_bad_req:
2476 /* We centralize bad requests processing here */
2477 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2478 /* we detected a parsing error. We want to archive this request
2479 * in the dedicated proxy area for later troubleshooting.
2480 */
2481 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2482 }
2483
2484 txn->req.msg_state = HTTP_MSG_ERROR;
2485 txn->status = 400;
2486 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002487
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002488 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002489 if (s->listener->counters)
2490 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002491
2492 return_prx_cond:
2493 if (!(s->flags & SN_ERR_MASK))
2494 s->flags |= SN_ERR_PRXCOND;
2495 if (!(s->flags & SN_FINST_MASK))
2496 s->flags |= SN_FINST_R;
2497
2498 req->analysers = 0;
2499 req->analyse_exp = TICK_ETERNITY;
2500 return 0;
2501}
2502
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002503/* This stream analyser runs all HTTP request processing which is common to
2504 * frontends and backends, which means blocking ACLs, filters, connection-close,
2505 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002506 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002507 * either needs more data or wants to immediately abort the request (eg: deny,
2508 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002509 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002510int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002511{
Willy Tarreaud787e662009-07-07 10:14:51 +02002512 struct http_txn *txn = &s->txn;
2513 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002514 struct acl_cond *cond;
2515 struct redirect_rule *rule;
2516 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002517
Willy Tarreau655dce92009-11-08 13:10:58 +01002518 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002519 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002520 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002521 return 0;
2522 }
2523
Willy Tarreau3a816292009-07-07 10:55:49 +02002524 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002525 req->analyse_exp = TICK_ETERNITY;
2526
2527 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2528 now_ms, __FUNCTION__,
2529 s,
2530 req,
2531 req->rex, req->wex,
2532 req->flags,
2533 req->l,
2534 req->analysers);
2535
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002536 /* first check whether we have some ACLs set to block this request */
2537 list_for_each_entry(cond, &px->block_cond, list) {
2538 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002539
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002540 ret = acl_pass(ret);
2541 if (cond->pol == ACL_COND_UNLESS)
2542 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002543
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002544 if (ret) {
2545 txn->status = 403;
2546 /* let's log the request time */
2547 s->logs.tv_request = now;
2548 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2549 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002551 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002552
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002553 /* try headers filters */
2554 if (px->req_exp != NULL) {
2555 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2556 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002557
Willy Tarreau59234e92008-11-30 23:51:27 +01002558 /* has the request been denied ? */
2559 if (txn->flags & TX_CLDENY) {
2560 /* no need to go further */
2561 txn->status = 403;
2562 /* let's log the request time */
2563 s->logs.tv_request = now;
2564 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2565 goto return_prx_cond;
2566 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002567
2568 /* When a connection is tarpitted, we use the tarpit timeout,
2569 * which may be the same as the connect timeout if unspecified.
2570 * If unset, then set it to zero because we really want it to
2571 * eventually expire. We build the tarpit as an analyser.
2572 */
2573 if (txn->flags & TX_CLTARPIT) {
2574 buffer_erase(s->req);
2575 /* wipe the request out so that we can drop the connection early
2576 * if the client closes first.
2577 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002578 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002579 req->analysers = 0; /* remove switching rules etc... */
2580 req->analysers |= AN_REQ_HTTP_TARPIT;
2581 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2582 if (!req->analyse_exp)
2583 req->analyse_exp = tick_add(now_ms, 0);
2584 return 1;
2585 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002586 }
Willy Tarreau06619262006-12-17 08:37:22 +01002587
Willy Tarreau5b154472009-12-21 20:11:07 +01002588 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2589 * only change if both the request and the config reference something else.
Willy Tarreau42736642009-10-18 21:04:35 +02002590 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002591
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002592 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01002593 ((s->fe->options|s->be->options) & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01002594 int tmp = TX_CON_WANT_TUN;
2595 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2596 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002597 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2598 tmp = TX_CON_WANT_SCL;
Willy Tarreau5b154472009-12-21 20:11:07 +01002599 if ((s->fe->options|s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))
2600 tmp = TX_CON_WANT_CLO;
2601
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002602 if (!(txn->flags & TX_REQ_XFER_LEN))
2603 tmp = TX_CON_WANT_CLO;
2604
Willy Tarreau5b154472009-12-21 20:11:07 +01002605 if (!(txn->flags & TX_CON_HDR_PARS))
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002606 http_req_parse_connection_header(txn);
Willy Tarreau5b154472009-12-21 20:11:07 +01002607
2608 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2609 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2610 }
2611
2612 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2613 * once we know the backend, because the tunnel mode can be implied by the
2614 * lack of any close/keepalive options in both the FE and the BE. Since
2615 * this information can evolve with time, we proceed by trying to make the
2616 * header status match the desired status. For this, we'll have to adjust
2617 * the "Connection" header. The test for persistent connections has already
2618 * been performed, so we only enter here if there is a risk the connection
2619 * is considered as persistent and we want it to be closed on the server
2620 * side. It would be nice if we could enter this place only when a
2621 * Connection header exists. Note that a CONNECT method will not enter
2622 * here.
2623 */
2624 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002625 char *cur_ptr, *cur_end, *cur_next;
2626 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002627 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002628 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002629
Willy Tarreau5b154472009-12-21 20:11:07 +01002630 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002631 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002632
Willy Tarreau5b154472009-12-21 20:11:07 +01002633 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002634 cur_hdr = &txn->hdr_idx.v[cur_idx];
2635 cur_ptr = cur_next;
2636 cur_end = cur_ptr + cur_hdr->len;
2637 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002638
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002639 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002640 if (!val)
2641 continue;
2642
2643 /* 3 possibilities :
2644 * - we have already set "Connection: close" or we're in
2645 * HTTP/1.0, so we remove this line.
2646 * - we have not yet set "Connection: close", but this line
2647 * indicates close. We leave it untouched and set the flag.
2648 * - we have not yet set "Connection: close", and this line
2649 * indicates non-close. We replace it and set the flag.
2650 */
2651 if (must_delete) {
2652 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2653 http_msg_move_end(&txn->req, delta);
2654 cur_next += delta;
2655 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2656 txn->hdr_idx.used--;
2657 cur_hdr->len = 0;
2658 txn->flags |= TX_REQ_CONN_CLO;
2659 } else {
2660 if (cur_end - cur_ptr - val != 5 ||
2661 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2662 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2663 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002664 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002665 cur_hdr->len += delta;
2666 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002667 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002668 txn->flags |= TX_REQ_CONN_CLO;
2669 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002670 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002671 } /* for loop */
2672 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002673
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002674 /* add request headers from the rule sets in the same order */
2675 for (cur_idx = 0; cur_idx < px->nb_reqadd; cur_idx++) {
2676 if (unlikely(http_header_add_tail(req,
2677 &txn->req,
2678 &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002679 px->req_add[cur_idx]) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002680 goto return_bad_req;
2681 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002682
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002683 /* check if stats URI was requested, and if an auth is needed */
2684 if (px->uri_auth != NULL &&
2685 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2686 /* we have to check the URI and auth for this request.
2687 * FIXME!!! that one is rather dangerous, we want to
2688 * make it follow standard rules (eg: clear req->analysers).
2689 */
2690 if (stats_check_uri_auth(s, px)) {
2691 req->analysers = 0;
2692 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002694 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002695
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002696 /* check whether we have some ACLs set to redirect this request */
2697 list_for_each_entry(rule, &px->redirect_rules, list) {
2698 int ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreau06b917c2009-07-06 16:34:52 +02002699
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002700 ret = acl_pass(ret);
2701 if (rule->cond->pol == ACL_COND_UNLESS)
2702 ret = !ret;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002703
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002704 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002705 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002706 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002707
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002708 /* build redirect message */
2709 switch(rule->code) {
2710 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002711 msg_fmt = HTTP_303;
2712 break;
2713 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002714 msg_fmt = HTTP_301;
2715 break;
2716 case 302:
2717 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002718 msg_fmt = HTTP_302;
2719 break;
2720 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002721
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002722 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002723 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002724
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002725 switch(rule->type) {
2726 case REDIRECT_TYPE_PREFIX: {
2727 const char *path;
2728 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002729
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002730 path = http_get_path(txn);
2731 /* build message using path */
2732 if (path) {
2733 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2734 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2735 int qs = 0;
2736 while (qs < pathlen) {
2737 if (path[qs] == '?') {
2738 pathlen = qs;
2739 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002740 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002741 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002742 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002743 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002744 } else {
2745 path = "/";
2746 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002747 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002748
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002749 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002750 goto return_bad_req;
2751
2752 /* add prefix. Note that if prefix == "/", we don't want to
2753 * add anything, otherwise it makes it hard for the user to
2754 * configure a self-redirection.
2755 */
2756 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002757 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2758 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002759 }
2760
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002761 /* add path */
2762 memcpy(rdr.str + rdr.len, path, pathlen);
2763 rdr.len += pathlen;
2764 break;
2765 }
2766 case REDIRECT_TYPE_LOCATION:
2767 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002768 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002769 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002770
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002771 /* add location */
2772 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2773 rdr.len += rule->rdr_len;
2774 break;
2775 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002776
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002777 if (rule->cookie_len) {
2778 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2779 rdr.len += 14;
2780 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2781 rdr.len += rule->cookie_len;
2782 memcpy(rdr.str + rdr.len, "\r\n", 2);
2783 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002784 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002785
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002786 /* add end of headers */
2787 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2788 rdr.len += 4;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002789
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002790 txn->status = rule->code;
2791 /* let's log the request time */
2792 s->logs.tv_request = now;
2793 stream_int_retnclose(req->prod, &rdr);
2794 goto return_prx_cond;
2795 }
2796 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002797
Willy Tarreaud98cf932009-12-27 22:54:55 +01002798 /* We can shut read side if we know how we won't transfer any more data && !abort_on_close */
2799 if ((txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau5b154472009-12-21 20:11:07 +01002800 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
Willy Tarreaud98cf932009-12-27 22:54:55 +01002801 (req->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)))
Willy Tarreau349a0f62009-10-18 21:17:42 +02002802 req->flags |= BF_DONT_READ;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +02002803
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002804 /* that's OK for us now, let's move on to next analysers */
2805 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002806
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002807 return_bad_req:
2808 /* We centralize bad requests processing here */
2809 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2810 /* we detected a parsing error. We want to archive this request
2811 * in the dedicated proxy area for later troubleshooting.
2812 */
2813 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2814 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002815
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002816 txn->req.msg_state = HTTP_MSG_ERROR;
2817 txn->status = 400;
2818 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002819
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002820 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002821 if (s->listener->counters)
2822 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002823
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002824 return_prx_cond:
2825 if (!(s->flags & SN_ERR_MASK))
2826 s->flags |= SN_ERR_PRXCOND;
2827 if (!(s->flags & SN_FINST_MASK))
2828 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002829
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002830 req->analysers = 0;
2831 req->analyse_exp = TICK_ETERNITY;
2832 return 0;
2833}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002834
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002835/* This function performs all the processing enabled for the current request.
2836 * It returns 1 if the processing can continue on next analysers, or zero if it
2837 * needs more data, encounters an error, or wants to immediately abort the
2838 * request. It relies on buffers flags, and updates s->req->analysers.
2839 */
2840int http_process_request(struct session *s, struct buffer *req, int an_bit)
2841{
2842 struct http_txn *txn = &s->txn;
2843 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002844
Willy Tarreau655dce92009-11-08 13:10:58 +01002845 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002846 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002847 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002848 return 0;
2849 }
2850
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002851 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2852 now_ms, __FUNCTION__,
2853 s,
2854 req,
2855 req->rex, req->wex,
2856 req->flags,
2857 req->l,
2858 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002859
Willy Tarreau59234e92008-11-30 23:51:27 +01002860 /*
2861 * Right now, we know that we have processed the entire headers
2862 * and that unwanted requests have been filtered out. We can do
2863 * whatever we want with the remaining request. Also, now we
2864 * may have separate values for ->fe, ->be.
2865 */
Willy Tarreau06619262006-12-17 08:37:22 +01002866
Willy Tarreau59234e92008-11-30 23:51:27 +01002867 /*
2868 * If HTTP PROXY is set we simply get remote server address
2869 * parsing incoming request.
2870 */
2871 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2872 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2873 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002874
Willy Tarreau59234e92008-11-30 23:51:27 +01002875 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002876 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002877 * Note that doing so might move headers in the request, but
2878 * the fields will stay coherent and the URI will not move.
2879 * This should only be performed in the backend.
2880 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002881 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002882 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2883 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002884
Willy Tarreau59234e92008-11-30 23:51:27 +01002885 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002886 * 8: the appsession cookie was looked up very early in 1.2,
2887 * so let's do the same now.
2888 */
2889
2890 /* It needs to look into the URI */
2891 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002892 get_srv_from_appsession(s, &req->data[msg->sl.rq.u], msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002893 }
2894
2895 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002896 * 9: add X-Forwarded-For if either the frontend or the backend
2897 * asks for it.
2898 */
2899 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2900 if (s->cli_addr.ss_family == AF_INET) {
2901 /* Add an X-Forwarded-For header unless the source IP is
2902 * in the 'except' network range.
2903 */
2904 if ((!s->fe->except_mask.s_addr ||
2905 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2906 != s->fe->except_net.s_addr) &&
2907 (!s->be->except_mask.s_addr ||
2908 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2909 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002910 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002911 unsigned char *pn;
2912 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002913
2914 /* Note: we rely on the backend to get the header name to be used for
2915 * x-forwarded-for, because the header is really meant for the backends.
2916 * However, if the backend did not specify any option, we have to rely
2917 * on the frontend's header name.
2918 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002919 if (s->be->fwdfor_hdr_len) {
2920 len = s->be->fwdfor_hdr_len;
2921 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002922 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002923 len = s->fe->fwdfor_hdr_len;
2924 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002925 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002926 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002927
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002928 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002929 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01002930 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002931 }
2932 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002933 else if (s->cli_addr.ss_family == AF_INET6) {
2934 /* FIXME: for the sake of completeness, we should also support
2935 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002936 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002937 int len;
2938 char pn[INET6_ADDRSTRLEN];
2939 inet_ntop(AF_INET6,
2940 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2941 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002942
Willy Tarreau59234e92008-11-30 23:51:27 +01002943 /* Note: we rely on the backend to get the header name to be used for
2944 * x-forwarded-for, because the header is really meant for the backends.
2945 * However, if the backend did not specify any option, we have to rely
2946 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002947 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002948 if (s->be->fwdfor_hdr_len) {
2949 len = s->be->fwdfor_hdr_len;
2950 memcpy(trash, s->be->fwdfor_hdr_name, len);
2951 } else {
2952 len = s->fe->fwdfor_hdr_len;
2953 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002954 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002955 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002956
Willy Tarreau59234e92008-11-30 23:51:27 +01002957 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002958 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01002959 goto return_bad_req;
2960 }
2961 }
2962
2963 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002964 * 10: add X-Original-To if either the frontend or the backend
2965 * asks for it.
2966 */
2967 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
2968
2969 /* FIXME: don't know if IPv6 can handle that case too. */
2970 if (s->cli_addr.ss_family == AF_INET) {
2971 /* Add an X-Original-To header unless the destination IP is
2972 * in the 'except' network range.
2973 */
2974 if (!(s->flags & SN_FRT_ADDR_SET))
2975 get_frt_addr(s);
2976
2977 if ((!s->fe->except_mask_to.s_addr ||
2978 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
2979 != s->fe->except_to.s_addr) &&
2980 (!s->be->except_mask_to.s_addr ||
2981 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
2982 != s->be->except_to.s_addr)) {
2983 int len;
2984 unsigned char *pn;
2985 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
2986
2987 /* Note: we rely on the backend to get the header name to be used for
2988 * x-original-to, because the header is really meant for the backends.
2989 * However, if the backend did not specify any option, we have to rely
2990 * on the frontend's header name.
2991 */
2992 if (s->be->orgto_hdr_len) {
2993 len = s->be->orgto_hdr_len;
2994 memcpy(trash, s->be->orgto_hdr_name, len);
2995 } else {
2996 len = s->fe->orgto_hdr_len;
2997 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002998 }
Maik Broemme2850cb42009-04-17 18:53:21 +02002999 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3000
3001 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003002 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003003 goto return_bad_req;
3004 }
3005 }
3006 }
3007
Willy Tarreau78599912009-10-17 20:12:21 +02003008 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau5b154472009-12-21 20:11:07 +01003009 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau78599912009-10-17 20:12:21 +02003010 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003011 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003012 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01003013 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01003014 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003015
3016 /* If we have no server assigned yet and we're balancing on url_param
3017 * with a POST request, we may be interested in checking the body for
3018 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003019 */
3020 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3021 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003022 s->be->url_param_post_limit != 0 &&
3023 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01003024 memchr(req->data + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003025 buffer_dont_connect(req);
3026 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003027 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003028
Willy Tarreaud98cf932009-12-27 22:54:55 +01003029 if (txn->flags & TX_REQ_XFER_LEN)
3030 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003031
Willy Tarreau59234e92008-11-30 23:51:27 +01003032 /*************************************************************
3033 * OK, that's finished for the headers. We have done what we *
3034 * could. Let's switch to the DATA state. *
3035 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003036 req->analyse_exp = TICK_ETERNITY;
3037 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003038
Willy Tarreau59234e92008-11-30 23:51:27 +01003039 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003040 /* OK let's go on with the BODY now */
3041 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003042
Willy Tarreau59234e92008-11-30 23:51:27 +01003043 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003044 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003045 /* we detected a parsing error. We want to archive this request
3046 * in the dedicated proxy area for later troubleshooting.
3047 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003048 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003049 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003050
Willy Tarreau59234e92008-11-30 23:51:27 +01003051 txn->req.msg_state = HTTP_MSG_ERROR;
3052 txn->status = 400;
3053 req->analysers = 0;
3054 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003055
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003056 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003057 if (s->listener->counters)
3058 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003059
Willy Tarreau59234e92008-11-30 23:51:27 +01003060 if (!(s->flags & SN_ERR_MASK))
3061 s->flags |= SN_ERR_PRXCOND;
3062 if (!(s->flags & SN_FINST_MASK))
3063 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003064 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003065}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003066
Willy Tarreau60b85b02008-11-30 23:28:40 +01003067/* This function is an analyser which processes the HTTP tarpit. It always
3068 * returns zero, at the beginning because it prevents any other processing
3069 * from occurring, and at the end because it terminates the request.
3070 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003071int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003072{
3073 struct http_txn *txn = &s->txn;
3074
3075 /* This connection is being tarpitted. The CLIENT side has
3076 * already set the connect expiration date to the right
3077 * timeout. We just have to check that the client is still
3078 * there and that the timeout has not expired.
3079 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003080 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003081 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3082 !tick_is_expired(req->analyse_exp, now_ms))
3083 return 0;
3084
3085 /* We will set the queue timer to the time spent, just for
3086 * logging purposes. We fake a 500 server error, so that the
3087 * attacker will not suspect his connection has been tarpitted.
3088 * It will not cause trouble to the logs because we can exclude
3089 * the tarpitted connections by filtering on the 'PT' status flags.
3090 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003091 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3092
3093 txn->status = 500;
3094 if (req->flags != BF_READ_ERROR)
3095 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3096
3097 req->analysers = 0;
3098 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003099
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003100 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003101 if (s->listener->counters)
3102 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003103
Willy Tarreau60b85b02008-11-30 23:28:40 +01003104 if (!(s->flags & SN_ERR_MASK))
3105 s->flags |= SN_ERR_PRXCOND;
3106 if (!(s->flags & SN_FINST_MASK))
3107 s->flags |= SN_FINST_T;
3108 return 0;
3109}
3110
Willy Tarreaud34af782008-11-30 23:36:37 +01003111/* This function is an analyser which processes the HTTP request body. It looks
3112 * for parameters to be used for the load balancing algorithm (url_param). It
3113 * must only be called after the standard HTTP request processing has occurred,
3114 * because it expects the request to be parsed. It returns zero if it needs to
3115 * read more data, or 1 once it has completed its analysis.
3116 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003117int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003118{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003119 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003120 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003121 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003122
3123 /* We have to parse the HTTP request body to find any required data.
3124 * "balance url_param check_post" should have been the only way to get
3125 * into this. We were brought here after HTTP header analysis, so all
3126 * related structures are ready.
3127 */
3128
Willy Tarreau522d6c02009-12-06 18:49:18 +01003129 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3130 goto missing_data;
3131
3132 if (msg->msg_state < HTTP_MSG_100_SENT) {
3133 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3134 * send an HTTP/1.1 100 Continue intermediate response.
3135 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003136 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003137 struct hdr_ctx ctx;
3138 ctx.idx = 0;
3139 /* Expect is allowed in 1.1, look for it */
3140 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3141 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3142 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3143 }
3144 }
3145 msg->msg_state = HTTP_MSG_100_SENT;
3146 }
3147
3148 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003149 /* we have msg->col and msg->sov which both point to the first
3150 * byte of message body. msg->som still points to the beginning
3151 * of the message. We must save the body in req->lr because it
3152 * survives buffer re-alignments.
3153 */
3154 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003155 if (txn->flags & TX_REQ_TE_CHNK)
3156 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3157 else
3158 msg->msg_state = HTTP_MSG_DATA;
3159 }
3160
3161 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003162 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003163 * set ->sov and ->lr to point to the body and switch to DATA or
3164 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003165 */
3166 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003167
Willy Tarreau115acb92009-12-26 13:56:06 +01003168 if (!ret)
3169 goto missing_data;
3170 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003171 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003172 }
3173
Willy Tarreaud98cf932009-12-27 22:54:55 +01003174 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003175 * We have the first non-header byte in msg->col, which is either the
3176 * beginning of the chunk size or of the data. The first data byte is in
3177 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3178 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003179 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003180
3181 if (msg->hdr_content_len < limit)
3182 limit = msg->hdr_content_len;
3183
Willy Tarreau7c96f672009-12-27 22:47:25 +01003184 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003185 goto http_end;
3186
3187 missing_data:
3188 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003189 if (req->flags & BF_FULL)
3190 goto return_bad_req;
3191
Willy Tarreau522d6c02009-12-06 18:49:18 +01003192 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3193 txn->status = 408;
3194 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3195 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003196 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003197
3198 /* we get here if we need to wait for more data */
3199 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003200 /* Not enough data. We'll re-use the http-request
3201 * timeout here. Ideally, we should set the timeout
3202 * relative to the accept() date. We just set the
3203 * request timeout once at the beginning of the
3204 * request.
3205 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003206 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003207 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003208 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003209 return 0;
3210 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003211
3212 http_end:
3213 /* The situation will not evolve, so let's give up on the analysis. */
3214 s->logs.tv_request = now; /* update the request timer to reflect full request */
3215 req->analysers &= ~an_bit;
3216 req->analyse_exp = TICK_ETERNITY;
3217 return 1;
3218
3219 return_bad_req: /* let's centralize all bad requests */
3220 txn->req.msg_state = HTTP_MSG_ERROR;
3221 txn->status = 400;
3222 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3223
3224 return_err_msg:
3225 req->analysers = 0;
3226 s->fe->counters.failed_req++;
3227 if (s->listener->counters)
3228 s->listener->counters->failed_req++;
3229
3230 if (!(s->flags & SN_ERR_MASK))
3231 s->flags |= SN_ERR_PRXCOND;
3232 if (!(s->flags & SN_FINST_MASK))
3233 s->flags |= SN_FINST_R;
3234 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003235}
3236
Willy Tarreaud98cf932009-12-27 22:54:55 +01003237/* This function is an analyser which forwards request body (including chunk
3238 * sizes if any). It is called as soon as we must forward, even if we forward
3239 * zero byte. The only situation where it must not be called is when we're in
3240 * tunnel mode and we want to forward till the close. It's used both to forward
3241 * remaining data and to resync after end of body. It expects the msg_state to
3242 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3243 * read more data, or 1 once we can go on with next request or end the session.
3244 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3245 * bytes of pending data + the headers if not already done (between som and sov).
3246 * It eventually adjusts som to match sov after the data in between have been sent.
3247 */
3248int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3249{
3250 struct http_txn *txn = &s->txn;
3251 struct http_msg *msg = &s->txn.req;
3252
Willy Tarreau21c5e4d2010-01-03 00:19:31 +01003253 if (req->flags & (BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
Willy Tarreau082b01c2010-01-02 23:58:04 +01003254 req->analysers &= ~an_bit;
3255 return 1;
3256 }
3257
Willy Tarreaud98cf932009-12-27 22:54:55 +01003258 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3259 return 0;
3260
3261 /* Note that we don't have to send 100-continue back because we don't
3262 * need the data to complete our job, and it's up to the server to
3263 * decide whether to return 100, 417 or anything else in return of
3264 * an "Expect: 100-continue" header.
3265 */
3266
3267 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3268 /* we have msg->col and msg->sov which both point to the first
3269 * byte of message body. msg->som still points to the beginning
3270 * of the message. We must save the body in req->lr because it
3271 * survives buffer re-alignments.
3272 */
3273 req->lr = req->data + msg->sov;
3274 if (txn->flags & TX_REQ_TE_CHNK)
3275 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3276 else {
3277 msg->msg_state = HTTP_MSG_DATA;
3278 }
3279 }
3280
Willy Tarreaud98cf932009-12-27 22:54:55 +01003281 while (1) {
Willy Tarreau638cd022010-01-03 07:42:04 +01003282 /* we may have some data pending */
3283 if (msg->hdr_content_len || msg->som != msg->sov) {
3284 int bytes = msg->sov - msg->som;
3285 if (bytes < 0) /* sov may have wrapped at the end */
3286 bytes += req->size;
3287 buffer_forward(req, bytes + msg->hdr_content_len);
3288 msg->hdr_content_len = 0; /* don't forward that again */
3289 msg->som = msg->sov;
3290 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003291
Willy Tarreaud98cf932009-12-27 22:54:55 +01003292 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
3293 /* read the chunk size and assign it to ->hdr_content_len, then
3294 * set ->sov and ->lr to point to the body and switch to DATA or
3295 * TRAILERS state.
3296 */
3297 int ret = http_parse_chunk_size(req, msg);
3298
3299 if (!ret)
3300 goto missing_data;
3301 else if (ret < 0)
3302 goto return_bad_req;
3303 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003304 }
3305 else if (msg->msg_state == HTTP_MSG_DATA) {
3306 /* must still forward */
3307 if (req->to_forward)
3308 return 0;
3309
3310 /* we're sending the last bits of request, the server's response
3311 * is expected in a short time. Most often the first read is enough
3312 * to bring all the headers, so we're preparing the response buffer
3313 * to read the response now. Note that we should probably move that
3314 * to a more appropriate place.
3315 */
Willy Tarreau5523b322009-12-29 12:05:52 +01003316 if (txn->rsp.msg_state == HTTP_MSG_RPBEFORE) {
3317 s->rep->flags &= ~BF_DONT_READ;
3318 s->rep->flags |= BF_READ_DONTWAIT;
3319 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003320
3321 /* nothing left to forward */
3322 if (txn->flags & TX_REQ_TE_CHNK)
3323 msg->msg_state = HTTP_MSG_DATA_CRLF;
3324 else {
3325 msg->msg_state = HTTP_MSG_DONE;
3326 }
3327 }
3328 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3329 /* we want the CRLF after the data */
3330 int ret;
3331
3332 if (!(req->flags & BF_OUT_EMPTY))
3333 return 0;
3334 /* The output pointer does not move anymore, next unsent data
3335 * are available at ->w. Let's save that.
3336 */
3337 req->lr = req->w;
3338 ret = http_skip_chunk_crlf(req, msg);
3339
3340 if (ret == 0)
3341 goto missing_data;
3342 else if (ret < 0)
3343 goto return_bad_req;
3344 /* we're in MSG_CHUNK_SIZE now */
3345 }
3346 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3347 int ret = http_forward_trailers(req, msg);
3348
3349 if (ret == 0)
3350 goto missing_data;
3351 else if (ret < 0)
3352 goto return_bad_req;
3353 /* we're in HTTP_MSG_DONE now */
3354 }
3355 else if (msg->msg_state == HTTP_MSG_DONE) {
Willy Tarreau5523b322009-12-29 12:05:52 +01003356 /* No need to read anymore, the request was completely parsed */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003357 req->flags |= BF_DONT_READ;
3358
Willy Tarreau5523b322009-12-29 12:05:52 +01003359 if (txn->rsp.msg_state < HTTP_MSG_DONE && txn->rsp.msg_state != HTTP_MSG_ERROR) {
3360 /* The server has not finished to respond, so we
3361 * don't want to move in order not to upset it.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003362 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003363 return 0;
3364 }
3365
3366 /* when we support keep-alive or server-close modes, we'll have
3367 * to reset the transaction here.
3368 */
Willy Tarreau5523b322009-12-29 12:05:52 +01003369
Willy Tarreaub608feb2010-01-02 22:47:18 +01003370 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3371 /* initiate a connection close to the server */
3372 req->cons->flags |= SI_FL_NOLINGER;
3373 buffer_shutw_now(req);
3374 }
3375 else if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) {
Willy Tarreau9438c712009-12-29 14:39:48 +01003376 /* Option forceclose is set, let's enforce it now
3377 * that the transfer is complete. We can safely speed
3378 * up the close because we know the server has received
3379 * everything we wanted it to receive.
3380 */
3381 req->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau82eeaf22009-12-29 12:09:05 +01003382 buffer_abort(req);
3383 }
3384
Willy Tarreau5523b322009-12-29 12:05:52 +01003385 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3386 if (req->flags & BF_OUT_EMPTY)
3387 msg->msg_state = HTTP_MSG_CLOSED;
3388 else
3389 msg->msg_state = HTTP_MSG_CLOSING;
3390 }
3391 else {
3392 /* for other modes, we let further requests pass for now */
3393 req->flags &= ~BF_DONT_READ;
3394 /* FIXME: we're still forced to do that here */
3395 s->rep->flags &= ~BF_DONT_READ;
3396 break;
3397 }
3398 }
3399 else if (msg->msg_state == HTTP_MSG_CLOSING) {
3400 /* nothing else to forward, just waiting for the buffer to be empty */
3401 if (!(req->flags & BF_OUT_EMPTY))
3402 return 0;
3403 msg->msg_state = HTTP_MSG_CLOSED;
3404 }
3405 else if (msg->msg_state == HTTP_MSG_CLOSED) {
3406 req->flags &= ~BF_DONT_READ;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003407
3408 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3409
3410 /* FIXME : this part is 1) awful, 2) tricky, 3) duplicated
3411 * ... but it works.
3412 * We need a better way to force a connection close without
3413 * any risk of propagation to the other side. We need a more
3414 * portable way of releasing a backend's and a server's
3415 * connections. We need a safer way to reinitialize buffer
3416 * flags. We also need a more accurate method for computing
3417 * per-request data.
3418 */
3419 s->req->cons->flags |= SI_FL_NOLINGER;
3420 s->req->cons->shutr(s->req->cons);
3421 s->req->cons->shutw(s->req->cons);
3422
3423 if (s->flags & SN_BE_ASSIGNED)
3424 s->be->beconn--;
3425
3426 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3427 session_process_counters(s);
3428
3429 if (s->txn.status) {
3430 int n;
3431
3432 n = s->txn.status / 100;
3433 if (n < 1 || n > 5)
3434 n = 0;
3435
3436 if (s->fe->mode == PR_MODE_HTTP)
3437 s->fe->counters.p.http.rsp[n]++;
3438
3439 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3440 (s->be->mode == PR_MODE_HTTP))
3441 s->be->counters.p.http.rsp[n]++;
3442 }
3443
3444 /* don't count other requests' data */
3445 s->logs.bytes_in -= s->req->l - s->req->send_max;
3446 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3447
3448 /* let's do a final log if we need it */
3449 if (s->logs.logwait &&
3450 !(s->flags & SN_MONITOR) &&
3451 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3452 s->do_log(s);
3453 }
3454
3455 s->logs.accept_date = date; /* user-visible date for logging */
3456 s->logs.tv_accept = now; /* corrected date for internal use */
3457 tv_zero(&s->logs.tv_request);
3458 s->logs.t_queue = -1;
3459 s->logs.t_connect = -1;
3460 s->logs.t_data = -1;
3461 s->logs.t_close = 0;
3462 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3463 s->logs.srv_queue_size = 0; /* we will get this number soon */
3464
3465 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3466 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3467
3468 if (s->pend_pos)
3469 pendconn_free(s->pend_pos);
3470
3471 if (s->srv) {
3472 if (s->flags & SN_CURR_SESS) {
3473 s->flags &= ~SN_CURR_SESS;
3474 s->srv->cur_sess--;
3475 }
3476 if (may_dequeue_tasks(s->srv, s->be))
3477 process_srv_queue(s->srv);
3478 }
3479
3480 if (unlikely(s->srv_conn))
3481 sess_change_server(s, NULL);
3482 s->srv = NULL;
3483
3484 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3485 s->req->cons->fd = -1; /* just to help with debugging */
3486 s->req->cons->err_type = SI_ET_NONE;
3487 s->req->cons->err_loc = NULL;
3488 s->req->cons->exp = TICK_ETERNITY;
3489 s->req->cons->flags = SI_FL_NONE;
3490 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE);
3491 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE|BF_WRITE_PARTIAL);
3492 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3493 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3494 s->txn.meth = 0;
3495 http_reset_txn(s);
3496 txn->flags |= TX_NOT_FIRST;
3497 if (s->be->options2 & PR_O2_INDEPSTR)
3498 s->req->cons->flags |= SI_FL_INDEP_STR;
3499
Willy Tarreauface8392010-01-03 11:37:54 +01003500 /* if the request buffer is not empty, it means we're
3501 * about to process another request, so send pending
3502 * data with MSG_MORE to merge TCP packets when possible.
3503 */
3504 if (s->req->l)
3505 s->rep->flags |= BF_EXPECT_MORE;
3506
Willy Tarreaub608feb2010-01-02 22:47:18 +01003507 /* make ->lr point to the first non-forwarded byte */
3508 s->req->lr = s->req->w + s->req->send_max;
3509 if (s->req->lr >= s->req->data + s->req->size)
3510 s->req->lr -= s->req->size;
3511 s->rep->lr = s->rep->w + s->rep->send_max;
3512 if (s->rep->lr >= s->rep->data + s->rep->size)
3513 s->rep->lr -= s->req->size;
3514
3515 s->req->analysers |= s->fe->fe_req_ana;
3516 s->rep->analysers = 0;
3517 }
3518
Willy Tarreau5523b322009-12-29 12:05:52 +01003519 /* FIXME: we're still forced to do that here */
3520 s->rep->flags &= ~BF_DONT_READ;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003521 break;
3522 }
3523 }
3524
3525 /* OK we're done with the data phase */
3526 req->analysers &= ~an_bit;
3527 return 1;
3528
3529 missing_data:
3530 /* forward the chunk size as well as any pending data */
3531 if (msg->hdr_content_len || msg->som != msg->sov) {
3532 buffer_forward(req, msg->sov - msg->som + msg->hdr_content_len);
3533 msg->hdr_content_len = 0; /* don't forward that again */
3534 msg->som = msg->sov;
3535 }
3536
3537 if (req->flags & BF_FULL)
3538 goto return_bad_req;
3539 /* the session handler will take care of timeouts and errors */
3540 return 0;
3541
3542 return_bad_req: /* let's centralize all bad requests */
3543 txn->req.msg_state = HTTP_MSG_ERROR;
3544 txn->status = 400;
3545 /* Note: we don't send any error if some data were already sent */
3546 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3547
3548 req->analysers = 0;
3549 s->fe->counters.failed_req++;
3550 if (s->listener->counters)
3551 s->listener->counters->failed_req++;
3552
3553 if (!(s->flags & SN_ERR_MASK))
3554 s->flags |= SN_ERR_PRXCOND;
3555 if (!(s->flags & SN_FINST_MASK))
3556 s->flags |= SN_FINST_R;
3557 return 0;
3558}
3559
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003560/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3561 * processing can continue on next analysers, or zero if it either needs more
3562 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3563 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3564 * when it has nothing left to do, and may remove any analyser when it wants to
3565 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003566 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003567int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003568{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003569 struct http_txn *txn = &s->txn;
3570 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003571 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003572 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003573 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003574 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003575
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003576 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 +02003577 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003578 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003579 rep,
3580 rep->rex, rep->wex,
3581 rep->flags,
3582 rep->l,
3583 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003584
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003585 /*
3586 * Now parse the partial (or complete) lines.
3587 * We will check the response syntax, and also join multi-line
3588 * headers. An index of all the lines will be elaborated while
3589 * parsing.
3590 *
3591 * For the parsing, we use a 28 states FSM.
3592 *
3593 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003594 * rep->data + msg->som = beginning of response
3595 * rep->data + msg->eoh = end of processed headers / start of current one
3596 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003597 * rep->lr = first non-visited byte
3598 * rep->r = end of data
3599 */
3600
Willy Tarreau83e3af02009-12-28 17:39:57 +01003601 /* There's a protected area at the end of the buffer for rewriting
3602 * purposes. We don't want to start to parse the request if the
3603 * protected area is affected, because we may have to move processed
3604 * data later, which is much more complicated.
3605 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003606 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3607 if (unlikely((rep->flags & BF_FULL) ||
3608 rep->r < rep->lr ||
3609 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3610 if (rep->send_max) {
3611 /* some data has still not left the buffer, wake us once that's done */
3612 buffer_dont_close(rep);
3613 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
3614 return 0;
3615 }
3616 if (rep->l <= rep->size - global.tune.maxrewrite)
3617 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003618 }
3619
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003620 if (likely(rep->lr < rep->r))
3621 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003622 }
3623
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003624 /* 1: we might have to print this header in debug mode */
3625 if (unlikely((global.mode & MODE_DEBUG) &&
3626 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003627 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003628 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003629
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003630 sol = rep->data + msg->som;
3631 eol = sol + msg->sl.rq.l;
3632 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003633
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003634 sol += hdr_idx_first_pos(&txn->hdr_idx);
3635 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003636
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003637 while (cur_idx) {
3638 eol = sol + txn->hdr_idx.v[cur_idx].len;
3639 debug_hdr("srvhdr", s, sol, eol);
3640 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3641 cur_idx = txn->hdr_idx.v[cur_idx].next;
3642 }
3643 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003644
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003645 /*
3646 * Now we quickly check if we have found a full valid response.
3647 * If not so, we check the FD and buffer states before leaving.
3648 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01003649 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003650 * responses are checked first.
3651 *
3652 * Depending on whether the client is still there or not, we
3653 * may send an error response back or not. Note that normally
3654 * we should only check for HTTP status there, and check I/O
3655 * errors somewhere else.
3656 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003657
Willy Tarreau655dce92009-11-08 13:10:58 +01003658 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003659 /* Invalid response */
3660 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
3661 /* we detected a parsing error. We want to archive this response
3662 * in the dedicated proxy area for later troubleshooting.
3663 */
3664 hdr_response_bad:
3665 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
3666 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3667
3668 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003669 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003670 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003671 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
3672 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003673
3674 rep->analysers = 0;
3675 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003676 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003677 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
3678
3679 if (!(s->flags & SN_ERR_MASK))
3680 s->flags |= SN_ERR_PRXCOND;
3681 if (!(s->flags & SN_FINST_MASK))
3682 s->flags |= SN_FINST_H;
3683
3684 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003685 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003686
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003687 /* too large response does not fit in buffer. */
3688 else if (rep->flags & BF_FULL) {
3689 goto hdr_response_bad;
3690 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003691
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003692 /* read error */
3693 else if (rep->flags & BF_READ_ERROR) {
3694 if (msg->err_pos >= 0)
3695 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02003696
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003697 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003698 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003699 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003700 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
3701 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003702
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003703 rep->analysers = 0;
3704 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003705 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003706 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02003707
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003708 if (!(s->flags & SN_ERR_MASK))
3709 s->flags |= SN_ERR_SRVCL;
3710 if (!(s->flags & SN_FINST_MASK))
3711 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003712 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003713 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003714
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003715 /* read timeout : return a 504 to the client. */
3716 else if (rep->flags & BF_READ_TIMEOUT) {
3717 if (msg->err_pos >= 0)
3718 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003719
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003720 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003721 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003722 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003723 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
3724 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003725
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003726 rep->analysers = 0;
3727 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003728 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003729 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02003730
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003731 if (!(s->flags & SN_ERR_MASK))
3732 s->flags |= SN_ERR_SRVTO;
3733 if (!(s->flags & SN_FINST_MASK))
3734 s->flags |= SN_FINST_H;
3735 return 0;
3736 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02003737
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003738 /* close from server */
3739 else if (rep->flags & BF_SHUTR) {
3740 if (msg->err_pos >= 0)
3741 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003742
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003743 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003744 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003745 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003746 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
3747 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003748
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003749 rep->analysers = 0;
3750 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003751 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003752 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01003753
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003754 if (!(s->flags & SN_ERR_MASK))
3755 s->flags |= SN_ERR_SRVCL;
3756 if (!(s->flags & SN_FINST_MASK))
3757 s->flags |= SN_FINST_H;
3758 return 0;
3759 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003760
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003761 /* write error to client (we don't send any message then) */
3762 else if (rep->flags & BF_WRITE_ERROR) {
3763 if (msg->err_pos >= 0)
3764 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003765
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003766 s->be->counters.failed_resp++;
3767 rep->analysers = 0;
3768
3769 if (!(s->flags & SN_ERR_MASK))
3770 s->flags |= SN_ERR_CLICL;
3771 if (!(s->flags & SN_FINST_MASK))
3772 s->flags |= SN_FINST_H;
3773
3774 /* process_session() will take care of the error */
3775 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003776 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003777
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003778 buffer_dont_close(rep);
3779 return 0;
3780 }
3781
3782 /* More interesting part now : we know that we have a complete
3783 * response which at least looks like HTTP. We have an indicator
3784 * of each header's length, so we can parse them quickly.
3785 */
3786
3787 if (unlikely(msg->err_pos >= 0))
3788 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3789
3790 /* ensure we keep this pointer to the beginning of the message */
3791 msg->sol = rep->data + msg->som;
3792
3793 /*
3794 * 1: get the status code
3795 */
3796 n = rep->data[msg->sl.st.c] - '0';
3797 if (n < 1 || n > 5)
3798 n = 0;
3799 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003800
Willy Tarreau5b154472009-12-21 20:11:07 +01003801 /* check if the response is HTTP/1.1 or above */
3802 if ((msg->sl.st.v_l == 8) &&
3803 ((rep->data[msg->som + 5] > '1') ||
3804 ((rep->data[msg->som + 5] == '1') &&
3805 (rep->data[msg->som + 7] >= '1'))))
3806 txn->flags |= TX_RES_VER_11;
3807
3808 /* "connection" has not been parsed yet */
3809 txn->flags &= ~TX_CON_HDR_PARS;
3810
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003811 /* transfer length unknown*/
3812 txn->flags &= ~TX_RES_XFER_LEN;
3813
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003814 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
3815
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003816 if (txn->status >= 100 && txn->status < 500)
3817 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
3818 else
3819 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
3820
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003821 /*
3822 * 2: check for cacheability.
3823 */
3824
3825 switch (txn->status) {
3826 case 200:
3827 case 203:
3828 case 206:
3829 case 300:
3830 case 301:
3831 case 410:
3832 /* RFC2616 @13.4:
3833 * "A response received with a status code of
3834 * 200, 203, 206, 300, 301 or 410 MAY be stored
3835 * by a cache (...) unless a cache-control
3836 * directive prohibits caching."
3837 *
3838 * RFC2616 @9.5: POST method :
3839 * "Responses to this method are not cacheable,
3840 * unless the response includes appropriate
3841 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003842 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003843 if (likely(txn->meth != HTTP_METH_POST) &&
3844 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3845 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3846 break;
3847 default:
3848 break;
3849 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003850
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003851 /*
3852 * 3: we may need to capture headers
3853 */
3854 s->logs.logwait &= ~LW_RESP;
3855 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
3856 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3857 txn->rsp.cap, s->fe->rsp_cap);
3858
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003859 /* 4: determine the transfer-length.
3860 * According to RFC2616 #4.4, amended by the HTTPbis working group,
3861 * the presence of a message-body in a RESPONSE and its transfer length
3862 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003863 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003864 * All responses to the HEAD request method MUST NOT include a
3865 * message-body, even though the presence of entity-header fields
3866 * might lead one to believe they do. All 1xx (informational), 204
3867 * (No Content), and 304 (Not Modified) responses MUST NOT include a
3868 * message-body. All other responses do include a message-body,
3869 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003870 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003871 * 1. Any response which "MUST NOT" include a message-body (such as the
3872 * 1xx, 204 and 304 responses and any response to a HEAD request) is
3873 * always terminated by the first empty line after the header fields,
3874 * regardless of the entity-header fields present in the message.
3875 *
3876 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
3877 * the "chunked" transfer-coding (Section 6.2) is used, the
3878 * transfer-length is defined by the use of this transfer-coding.
3879 * If a Transfer-Encoding header field is present and the "chunked"
3880 * transfer-coding is not present, the transfer-length is defined by
3881 * the sender closing the connection.
3882 *
3883 * 3. If a Content-Length header field is present, its decimal value in
3884 * OCTETs represents both the entity-length and the transfer-length.
3885 * If a message is received with both a Transfer-Encoding header
3886 * field and a Content-Length header field, the latter MUST be ignored.
3887 *
3888 * 4. If the message uses the media type "multipart/byteranges", and
3889 * the transfer-length is not otherwise specified, then this self-
3890 * delimiting media type defines the transfer-length. This media
3891 * type MUST NOT be used unless the sender knows that the recipient
3892 * can parse it; the presence in a request of a Range header with
3893 * multiple byte-range specifiers from a 1.1 client implies that the
3894 * client can parse multipart/byteranges responses.
3895 *
3896 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003897 */
3898
3899 /* Skip parsing if no content length is possible. The response flags
3900 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003901 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003902 * FIXME: should we parse anyway and return an error on chunked encoding ?
3903 */
3904 if (txn->meth == HTTP_METH_HEAD ||
3905 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003906 txn->status == 204 || txn->status == 304) {
3907 txn->flags |= TX_RES_XFER_LEN;
3908 goto skip_content_length;
3909 }
3910
3911 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003912 goto skip_content_length;
3913
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003914 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003915 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003916 while ((txn->flags & TX_RES_VER_11) &&
3917 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003918 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
3919 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3920 else if (txn->flags & TX_RES_TE_CHNK) {
3921 /* bad transfer-encoding (chunked followed by something else) */
3922 use_close_only = 1;
3923 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3924 break;
3925 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003926 }
3927
3928 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
3929 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003930 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003931 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
3932 signed long long cl;
3933
3934 if (!ctx.vlen)
3935 goto hdr_response_bad;
3936
3937 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
3938 goto hdr_response_bad; /* parse failure */
3939
3940 if (cl < 0)
3941 goto hdr_response_bad;
3942
3943 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
3944 goto hdr_response_bad; /* already specified, was different */
3945
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003946 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003947 msg->hdr_content_len = cl;
3948 }
3949
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003950 /* FIXME: we should also implement the multipart/byterange method.
3951 * For now on, we resort to close mode in this case (unknown length).
3952 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003953skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003954
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003955 /* end of job, return OK */
3956 rep->analysers &= ~an_bit;
3957 rep->analyse_exp = TICK_ETERNITY;
3958 return 1;
3959}
3960
3961/* This function performs all the processing enabled for the current response.
3962 * It normally returns zero, but may return 1 if it absolutely needs to be
3963 * called again after other functions. It relies on buffers flags, and updates
3964 * t->rep->analysers. It might make sense to explode it into several other
3965 * functions. It works like process_request (see indications above).
3966 */
3967int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
3968{
3969 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003970 struct http_msg *msg = &txn->rsp;
3971 struct proxy *cur_proxy;
3972 int cur_idx;
Willy Tarreau5b154472009-12-21 20:11:07 +01003973 int conn_ka = 0, conn_cl = 0;
3974 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003975 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003976
3977 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3978 now_ms, __FUNCTION__,
3979 t,
3980 rep,
3981 rep->rex, rep->wex,
3982 rep->flags,
3983 rep->l,
3984 rep->analysers);
3985
Willy Tarreau655dce92009-11-08 13:10:58 +01003986 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003987 return 0;
3988
3989 rep->analysers &= ~an_bit;
3990 rep->analyse_exp = TICK_ETERNITY;
3991
Willy Tarreau5b154472009-12-21 20:11:07 +01003992 /* Now we have to check if we need to modify the Connection header.
3993 * This is more difficult on the response than it is on the request,
3994 * because we can have two different HTTP versions and we don't know
3995 * how the client will interprete a response. For instance, let's say
3996 * that the client sends a keep-alive request in HTTP/1.0 and gets an
3997 * HTTP/1.1 response without any header. Maybe it will bound itself to
3998 * HTTP/1.0 because it only knows about it, and will consider the lack
3999 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4000 * the lack of header as a keep-alive. Thus we will use two flags
4001 * indicating how a request MAY be understood by the client. In case
4002 * of multiple possibilities, we'll fix the header to be explicit. If
4003 * ambiguous cases such as both close and keepalive are seen, then we
4004 * will fall back to explicit close. Note that we won't take risks with
4005 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4006 */
4007
4008 if ((txn->meth != HTTP_METH_CONNECT) &&
4009 (txn->status >= 200) &&
4010 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN &&
4011 !(txn->flags & TX_CON_HDR_PARS)) {
4012 int may_keep = 0, may_close = 0; /* how it may be understood */
4013 struct hdr_ctx ctx;
4014
4015 ctx.idx = 0;
4016 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4017 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4018 conn_cl = 1;
4019 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4020 conn_ka = 1;
4021 }
4022
4023 if (conn_cl) {
4024 /* close header present */
4025 may_close = 1;
4026 if (conn_ka) /* we have both close and keep-alive */
4027 may_keep = 1;
4028 }
4029 else if (conn_ka) {
4030 /* keep-alive alone */
4031 may_keep = 1;
4032 }
4033 else {
4034 /* no close nor keep-alive header */
4035 if (txn->flags & TX_RES_VER_11)
4036 may_keep = 1;
4037 else
4038 may_close = 1;
4039
4040 if (txn->flags & TX_REQ_VER_11)
4041 may_keep = 1;
4042 else
4043 may_close = 1;
4044 }
4045
4046 /* let's update the transaction status to reflect any close.
4047 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004048 * handled. We also explicitly state that we will close in
4049 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004050 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004051 if ((may_close &&
4052 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4053 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004054 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4055
4056 /* Now we must adjust the response header :
4057 * - set "close" if may_keep and WANT_CLO
4058 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4059 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
4060 *
4061 * Until we support the server-close mode, we'll only support the set "close".
4062 */
4063 if (may_keep && (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO)
4064 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004065 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4066 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4067 must_del_close = 1;
4068 if (!(txn->flags & TX_REQ_VER_11))
4069 must_keep = 1;
4070 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004071
4072 txn->flags |= TX_CON_HDR_PARS;
4073 }
4074
4075 /* We might have to check for "Connection:" if the server
4076 * returns a connection status that is not compatible with
4077 * the client's or with the config.
4078 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004079 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004080 char *cur_ptr, *cur_end, *cur_next;
4081 int cur_idx, old_idx, delta, val;
4082 int must_delete;
4083 struct hdr_idx_elem *cur_hdr;
4084
4085 /* we just have to remove the headers if both sides are 1.0 */
4086 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004087
4088 /* same if we want to re-enable keep-alive on 1.1 */
4089 must_delete |= must_del_close;
4090
Willy Tarreau5b154472009-12-21 20:11:07 +01004091 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4092
4093 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4094 cur_hdr = &txn->hdr_idx.v[cur_idx];
4095 cur_ptr = cur_next;
4096 cur_end = cur_ptr + cur_hdr->len;
4097 cur_next = cur_end + cur_hdr->cr + 1;
4098
4099 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4100 if (!val)
4101 continue;
4102
4103 /* 3 possibilities :
4104 * - we have already set "Connection: close" or we're in
4105 * HTTP/1.0, so we remove this line.
4106 * - we have not yet set "Connection: close", but this line
4107 * indicates close. We leave it untouched and set the flag.
4108 * - we have not yet set "Connection: close", and this line
4109 * indicates non-close. We replace it and set the flag.
4110 */
4111 if (must_delete) {
4112 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4113 http_msg_move_end(&txn->rsp, delta);
4114 cur_next += delta;
4115 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4116 txn->hdr_idx.used--;
4117 cur_hdr->len = 0;
4118 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004119 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004120 } else {
4121 if (cur_end - cur_ptr - val != 5 ||
4122 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4123 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4124 "close", 5);
4125 cur_next += delta;
4126 cur_hdr->len += delta;
4127 http_msg_move_end(&txn->rsp, delta);
4128 }
4129 must_delete = 1;
4130 must_close = 0;
4131 }
4132 } /* for loop */
4133 } /* if must close keep-alive */
4134
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004135 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004136 /*
4137 * 3: we will have to evaluate the filters.
4138 * As opposed to version 1.2, now they will be evaluated in the
4139 * filters order and not in the header order. This means that
4140 * each filter has to be validated among all headers.
4141 *
4142 * Filters are tried with ->be first, then with ->fe if it is
4143 * different from ->be.
4144 */
4145
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004146 cur_proxy = t->be;
4147 while (1) {
4148 struct proxy *rule_set = cur_proxy;
4149
4150 /* try headers filters */
4151 if (rule_set->rsp_exp != NULL) {
4152 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4153 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004154 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004155 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004156 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4157 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004158 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004159 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004160 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004161 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004162 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004163 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004164 if (!(t->flags & SN_ERR_MASK))
4165 t->flags |= SN_ERR_PRXCOND;
4166 if (!(t->flags & SN_FINST_MASK))
4167 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004168 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004169 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004170 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004171
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004172 /* has the response been denied ? */
4173 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004174 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004175 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004176
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004177 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004178 if (t->listener->counters)
4179 t->listener->counters->denied_resp++;
4180
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004181 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004182 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004183
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004184 /* add response headers from the rule sets in the same order */
4185 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004186 if (txn->status < 200)
4187 break;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004188 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004189 rule_set->rsp_add[cur_idx]) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004190 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004191 }
4192
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004193 /* check whether we're already working on the frontend */
4194 if (cur_proxy == t->fe)
4195 break;
4196 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004197 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004198
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004199 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004200 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4201 * in which case this is not the right response, and we're waiting for the
4202 * next one. Let's allow this response to go to the client and wait for the
4203 * next one.
4204 */
4205 if (txn->status < 200) {
4206 hdr_idx_init(&txn->hdr_idx);
4207 buffer_forward(rep, rep->lr - (rep->data + msg->som));
4208 msg->msg_state = HTTP_MSG_RPBEFORE;
4209 txn->status = 0;
4210 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4211 return 1;
4212 }
4213
4214 /* we don't have any 1xx status code now */
4215
4216 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004217 * 4: check for server cookie.
4218 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004219 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4220 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004221 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004222
Willy Tarreaubaaee002006-06-26 02:48:02 +02004223
Willy Tarreaua15645d2007-03-18 16:22:39 +01004224 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004225 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004226 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004227 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004228 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004229
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004230 /*
4231 * 6: add server cookie in the response if needed
4232 */
4233 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004234 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004235 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004236
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004237 /* the server is known, it's not the one the client requested, we have to
4238 * insert a set-cookie here, except if we want to insert only on POST
4239 * requests and this one isn't. Note that servers which don't have cookies
4240 * (eg: some backup servers) will return a full cookie removal request.
4241 */
4242 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4243 t->be->cookie_name,
4244 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004245
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004246 if (t->be->cookie_domain)
4247 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004248
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004249 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004250 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004251 goto return_bad_resp;
4252 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004253
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004254 /* Here, we will tell an eventual cache on the client side that we don't
4255 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4256 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4257 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4258 */
4259 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004260
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004261 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4262
4263 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004264 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004265 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004266 }
4267 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004268
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004269 /*
4270 * 7: check if result will be cacheable with a cookie.
4271 * We'll block the response if security checks have caught
4272 * nasty things such as a cacheable cookie.
4273 */
4274 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4275 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004276 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004277
4278 /* we're in presence of a cacheable response containing
4279 * a set-cookie header. We'll block it as requested by
4280 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004281 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004282 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004283 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004284
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004285 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004286 if (t->listener->counters)
4287 t->listener->counters->denied_resp++;
4288
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004289 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4290 t->be->id, t->srv?t->srv->id:"<dispatch>");
4291 send_log(t->be, LOG_ALERT,
4292 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4293 t->be->id, t->srv?t->srv->id:"<dispatch>");
4294 goto return_srv_prx_502;
4295 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004296
4297 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004298 * 8: add "Connection: close" if needed and not yet set. This is
4299 * only needed for 1.1 responses since we know there is no other
4300 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004301 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004302 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004303 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004304 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004305 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004306 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004307 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004308 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4309 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4310 "Connection: keep-alive", 22) < 0))
4311 goto return_bad_resp;
4312 must_keep = 0;
4313 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004314
Willy Tarreaud98cf932009-12-27 22:54:55 +01004315 if (txn->flags & TX_RES_XFER_LEN)
4316 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004317
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004318 /*************************************************************
4319 * OK, that's finished for the headers. We have done what we *
4320 * could. Let's switch to the DATA state. *
4321 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004322
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004323 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004324
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004325 /* if the user wants to log as soon as possible, without counting
4326 * bytes from the server, then this is the right moment. We have
4327 * to temporarily assign bytes_out to log what we currently have.
4328 */
4329 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4330 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4331 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004332 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004333 t->logs.bytes_out = 0;
4334 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004335
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004336 /* Note: we must not try to cheat by jumping directly to DATA,
4337 * otherwise we would not let the client side wake up.
4338 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004339
Willy Tarreaudafde432008-08-17 01:00:46 +02004340 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004341 }
4342 return 0;
4343}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004344
Willy Tarreaud98cf932009-12-27 22:54:55 +01004345/* This function is an analyser which forwards response body (including chunk
4346 * sizes if any). It is called as soon as we must forward, even if we forward
4347 * zero byte. The only situation where it must not be called is when we're in
4348 * tunnel mode and we want to forward till the close. It's used both to forward
4349 * remaining data and to resync after end of body. It expects the msg_state to
4350 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4351 * read more data, or 1 once we can go on with next request or end the session.
4352 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4353 * bytes of pending data + the headers if not already done (between som and sov).
4354 * It eventually adjusts som to match sov after the data in between have been sent.
4355 */
4356int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4357{
4358 struct http_txn *txn = &s->txn;
4359 struct http_msg *msg = &s->txn.rsp;
4360
Willy Tarreau21c5e4d2010-01-03 00:19:31 +01004361 if (res->flags & (BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
Willy Tarreau082b01c2010-01-02 23:58:04 +01004362 res->analysers &= ~an_bit;
4363 return 1;
4364 }
4365
Willy Tarreaud98cf932009-12-27 22:54:55 +01004366 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4367 return 0;
4368
Willy Tarreaub608feb2010-01-02 22:47:18 +01004369 /* note: in server-close mode, we don't want to automatically close the
4370 * output when the input is closed.
4371 */
4372 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4373 buffer_dont_close(res);
4374
Willy Tarreaud98cf932009-12-27 22:54:55 +01004375 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4376 /* we have msg->col and msg->sov which both point to the first
4377 * byte of message body. msg->som still points to the beginning
4378 * of the message. We must save the body in req->lr because it
4379 * survives buffer re-alignments.
4380 */
4381 res->lr = res->data + msg->sov;
4382 if (txn->flags & TX_RES_TE_CHNK)
4383 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4384 else {
4385 msg->msg_state = HTTP_MSG_DATA;
4386 }
4387 }
4388
Willy Tarreaud98cf932009-12-27 22:54:55 +01004389 while (1) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004390 /* we may have some data pending */
4391 if (msg->hdr_content_len || msg->som != msg->sov) {
4392 int bytes = msg->sov - msg->som;
4393 if (bytes < 0) /* sov may have wrapped at the end */
4394 bytes += res->size;
4395 buffer_forward(res, bytes + msg->hdr_content_len);
4396 msg->hdr_content_len = 0; /* don't forward that again */
4397 msg->som = msg->sov;
4398 }
4399
Willy Tarreaud98cf932009-12-27 22:54:55 +01004400 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
4401 /* read the chunk size and assign it to ->hdr_content_len, then
4402 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4403 */
4404 int ret = http_parse_chunk_size(res, msg);
4405
4406 if (!ret)
4407 goto missing_data;
4408 else if (ret < 0)
4409 goto return_bad_res;
4410 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004411 }
4412 else if (msg->msg_state == HTTP_MSG_DATA) {
4413 /* must still forward */
4414 if (res->to_forward)
4415 return 0;
4416
4417 /* nothing left to forward */
4418 if (txn->flags & TX_RES_TE_CHNK)
4419 msg->msg_state = HTTP_MSG_DATA_CRLF;
4420 else {
4421 msg->msg_state = HTTP_MSG_DONE;
4422 }
4423 }
4424 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4425 /* we want the CRLF after the data */
4426 int ret;
4427
4428 if (!(res->flags & BF_OUT_EMPTY))
4429 return 0;
4430 /* The output pointer does not move anymore, next unsent data
4431 * are available at ->w. Let's save that.
4432 */
4433 res->lr = res->w;
4434 ret = http_skip_chunk_crlf(res, msg);
4435
4436 if (!ret)
4437 goto missing_data;
4438 else if (ret < 0)
4439 goto return_bad_res;
4440 /* we're in MSG_CHUNK_SIZE now */
4441 }
4442 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4443 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004444
Willy Tarreaud98cf932009-12-27 22:54:55 +01004445 if (ret == 0)
4446 goto missing_data;
4447 else if (ret < 0)
4448 goto return_bad_res;
4449 /* we're in HTTP_MSG_DONE now */
4450 }
4451 else if (msg->msg_state == HTTP_MSG_DONE) {
Willy Tarreau5523b322009-12-29 12:05:52 +01004452 /* In theory, we don't need to read anymore, but we must
4453 * still monitor the server connection for a possible close,
4454 * so we don't set the BF_DONT_READ flag here.
4455 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004456
Willy Tarreau5523b322009-12-29 12:05:52 +01004457 if (txn->req.msg_state < HTTP_MSG_DONE && txn->req.msg_state != HTTP_MSG_ERROR) {
4458 /* The client seems to still be sending data, probably
4459 * because we got an error response during an upload.
4460 * We have the choice of either breaking the connection
4461 * or letting it pass through. Let's do the later.
4462 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004463 return 0;
Willy Tarreau5523b322009-12-29 12:05:52 +01004464 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004465
4466 /* when we support keep-alive or server-close modes, we'll have
4467 * to reset the transaction here.
4468 */
Willy Tarreau5523b322009-12-29 12:05:52 +01004469
Willy Tarreau82eeaf22009-12-29 12:09:05 +01004470 if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) {
4471 /* option forceclose is set, let's enforce it now that the transfer is complete. */
4472 buffer_abort(res);
4473 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004474 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4475 /* server close is handled entirely on the req analyser */
4476 s->req->cons->flags |= SI_FL_NOLINGER;
4477 buffer_shutw_now(s->req);
4478 }
Willy Tarreau82eeaf22009-12-29 12:09:05 +01004479
Willy Tarreau5523b322009-12-29 12:05:52 +01004480 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4481 if (res->flags & BF_OUT_EMPTY)
4482 msg->msg_state = HTTP_MSG_CLOSED;
4483 else
4484 msg->msg_state = HTTP_MSG_CLOSING;
4485 }
4486 else {
4487 /* for other modes, we let further responses pass for now */
4488 res->flags &= ~BF_DONT_READ;
4489 /* FIXME: we're still forced to do that here */
4490 s->req->flags &= ~BF_DONT_READ;
4491 break;
4492 }
4493 }
4494 else if (msg->msg_state == HTTP_MSG_CLOSING) {
4495 /* nothing else to forward, just waiting for the buffer to be empty */
4496 if (!(res->flags & BF_OUT_EMPTY))
4497 return 0;
4498 msg->msg_state = HTTP_MSG_CLOSED;
4499 }
4500 else if (msg->msg_state == HTTP_MSG_CLOSED) {
4501 res->flags &= ~BF_DONT_READ;
4502 /* FIXME: we're still forced to do that here */
4503 s->req->flags &= ~BF_DONT_READ;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004504 break;
4505 }
4506 }
4507
4508 res->analysers &= ~an_bit;
4509 return 1;
4510
4511 missing_data:
4512 /* forward the chunk size as well as any pending data */
4513 if (msg->hdr_content_len || msg->som != msg->sov) {
4514 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4515 msg->hdr_content_len = 0; /* don't forward that again */
4516 msg->som = msg->sov;
4517 }
4518
4519 if (res->flags & BF_FULL)
4520 goto return_bad_res;
4521 /* the session handler will take care of timeouts and errors */
4522 return 0;
4523
4524 return_bad_res: /* let's centralize all bad resuests */
4525 txn->rsp.msg_state = HTTP_MSG_ERROR;
4526 txn->status = 502;
4527 stream_int_cond_close(res->cons, NULL);
4528
4529 res->analysers = 0;
4530 s->be->counters.failed_resp++;
4531 if (s->srv) {
4532 s->srv->counters.failed_resp++;
4533 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4534 }
4535
4536 if (!(s->flags & SN_ERR_MASK))
4537 s->flags |= SN_ERR_PRXCOND;
4538 if (!(s->flags & SN_FINST_MASK))
4539 s->flags |= SN_FINST_R;
4540 return 0;
4541}
4542
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004543/* Iterate the same filter through all request headers.
4544 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004545 * Since it can manage the switch to another backend, it updates the per-proxy
4546 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004547 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004548int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004549{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004550 char term;
4551 char *cur_ptr, *cur_end, *cur_next;
4552 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004553 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004554 struct hdr_idx_elem *cur_hdr;
4555 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004556
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004557 last_hdr = 0;
4558
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004559 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004560 old_idx = 0;
4561
4562 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004563 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004564 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004565 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004566 (exp->action == ACT_ALLOW ||
4567 exp->action == ACT_DENY ||
4568 exp->action == ACT_TARPIT))
4569 return 0;
4570
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004571 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004572 if (!cur_idx)
4573 break;
4574
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004575 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004576 cur_ptr = cur_next;
4577 cur_end = cur_ptr + cur_hdr->len;
4578 cur_next = cur_end + cur_hdr->cr + 1;
4579
4580 /* Now we have one header between cur_ptr and cur_end,
4581 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004582 */
4583
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004584 /* The annoying part is that pattern matching needs
4585 * that we modify the contents to null-terminate all
4586 * strings before testing them.
4587 */
4588
4589 term = *cur_end;
4590 *cur_end = '\0';
4591
4592 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4593 switch (exp->action) {
4594 case ACT_SETBE:
4595 /* It is not possible to jump a second time.
4596 * FIXME: should we return an HTTP/500 here so that
4597 * the admin knows there's a problem ?
4598 */
4599 if (t->be != t->fe)
4600 break;
4601
4602 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004603 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004604 last_hdr = 1;
4605 break;
4606
4607 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004608 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004609 last_hdr = 1;
4610 break;
4611
4612 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004613 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004614 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004615
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004616 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004617 if (t->listener->counters)
4618 t->listener->counters->denied_resp++;
4619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004620 break;
4621
4622 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004623 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004624 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004625
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004626 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004627 if (t->listener->counters)
4628 t->listener->counters->denied_resp++;
4629
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004630 break;
4631
4632 case ACT_REPLACE:
4633 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4634 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4635 /* FIXME: if the user adds a newline in the replacement, the
4636 * index will not be recalculated for now, and the new line
4637 * will not be counted as a new header.
4638 */
4639
4640 cur_end += delta;
4641 cur_next += delta;
4642 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004643 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004644 break;
4645
4646 case ACT_REMOVE:
4647 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4648 cur_next += delta;
4649
Willy Tarreaufa355d42009-11-29 18:12:29 +01004650 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004651 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4652 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004653 cur_hdr->len = 0;
4654 cur_end = NULL; /* null-term has been rewritten */
4655 break;
4656
4657 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004658 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004659 if (cur_end)
4660 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004661
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004662 /* keep the link from this header to next one in case of later
4663 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004664 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004665 old_idx = cur_idx;
4666 }
4667 return 0;
4668}
4669
4670
4671/* Apply the filter to the request line.
4672 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4673 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004674 * Since it can manage the switch to another backend, it updates the per-proxy
4675 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004676 */
4677int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4678{
4679 char term;
4680 char *cur_ptr, *cur_end;
4681 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004682 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004683 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004684
Willy Tarreau58f10d72006-12-04 02:26:12 +01004685
Willy Tarreau3d300592007-03-18 18:34:41 +01004686 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004687 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004688 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004689 (exp->action == ACT_ALLOW ||
4690 exp->action == ACT_DENY ||
4691 exp->action == ACT_TARPIT))
4692 return 0;
4693 else if (exp->action == ACT_REMOVE)
4694 return 0;
4695
4696 done = 0;
4697
Willy Tarreau9cdde232007-05-02 20:58:19 +02004698 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004699 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004700
4701 /* Now we have the request line between cur_ptr and cur_end */
4702
4703 /* The annoying part is that pattern matching needs
4704 * that we modify the contents to null-terminate all
4705 * strings before testing them.
4706 */
4707
4708 term = *cur_end;
4709 *cur_end = '\0';
4710
4711 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4712 switch (exp->action) {
4713 case ACT_SETBE:
4714 /* It is not possible to jump a second time.
4715 * FIXME: should we return an HTTP/500 here so that
4716 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004717 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004718 if (t->be != t->fe)
4719 break;
4720
4721 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004722 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004723 done = 1;
4724 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004725
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004726 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004727 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004728 done = 1;
4729 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004731 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004732 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004733
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004734 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004735 if (t->listener->counters)
4736 t->listener->counters->denied_resp++;
4737
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004738 done = 1;
4739 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004740
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004741 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004742 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004743
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004744 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004745 if (t->listener->counters)
4746 t->listener->counters->denied_resp++;
4747
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004748 done = 1;
4749 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004751 case ACT_REPLACE:
4752 *cur_end = term; /* restore the string terminator */
4753 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4754 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4755 /* FIXME: if the user adds a newline in the replacement, the
4756 * index will not be recalculated for now, and the new line
4757 * will not be counted as a new header.
4758 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004759
Willy Tarreaufa355d42009-11-29 18:12:29 +01004760 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004761 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004762
Willy Tarreau9cdde232007-05-02 20:58:19 +02004763 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004764 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004765 HTTP_MSG_RQMETH,
4766 cur_ptr, cur_end + 1,
4767 NULL, NULL);
4768 if (unlikely(!cur_end))
4769 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004770
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004771 /* we have a full request and we know that we have either a CR
4772 * or an LF at <ptr>.
4773 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004774 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4775 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004776 /* there is no point trying this regex on headers */
4777 return 1;
4778 }
4779 }
4780 *cur_end = term; /* restore the string terminator */
4781 return done;
4782}
Willy Tarreau97de6242006-12-27 17:18:38 +01004783
Willy Tarreau58f10d72006-12-04 02:26:12 +01004784
Willy Tarreau58f10d72006-12-04 02:26:12 +01004785
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004786/*
4787 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4788 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004789 * unparsable request. Since it can manage the switch to another backend, it
4790 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004791 */
4792int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4793{
Willy Tarreau3d300592007-03-18 18:34:41 +01004794 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004795 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004796 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004797 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004798
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004799 /*
4800 * The interleaving of transformations and verdicts
4801 * makes it difficult to decide to continue or stop
4802 * the evaluation.
4803 */
4804
Willy Tarreau3d300592007-03-18 18:34:41 +01004805 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004806 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4807 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4808 exp = exp->next;
4809 continue;
4810 }
4811
4812 /* Apply the filter to the request line. */
4813 ret = apply_filter_to_req_line(t, req, exp);
4814 if (unlikely(ret < 0))
4815 return -1;
4816
4817 if (likely(ret == 0)) {
4818 /* The filter did not match the request, it can be
4819 * iterated through all headers.
4820 */
4821 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004822 }
4823 exp = exp->next;
4824 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004825 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004826}
4827
4828
Willy Tarreaua15645d2007-03-18 16:22:39 +01004829
Willy Tarreau58f10d72006-12-04 02:26:12 +01004830/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004831 * Try to retrieve the server associated to the appsession.
4832 * If the server is found, it's assigned to the session.
4833 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01004834void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004835 struct http_txn *txn = &t->txn;
4836 appsess *asession = NULL;
4837 char *sessid_temp = NULL;
4838
Cyril Bontéb21570a2009-11-29 20:04:48 +01004839 if (len > t->be->appsession_len) {
4840 len = t->be->appsession_len;
4841 }
4842
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004843 if (t->be->options2 & PR_O2_AS_REQL) {
4844 /* request-learn option is enabled : store the sessid in the session for future use */
4845 if (t->sessid != NULL) {
4846 /* free previously allocated memory as we don't need the session id found in the URL anymore */
4847 pool_free2(apools.sessid, t->sessid);
4848 }
4849
4850 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
4851 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4852 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4853 return;
4854 }
4855
Cyril Bontéb21570a2009-11-29 20:04:48 +01004856 memcpy(t->sessid, buf, len);
4857 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004858 }
4859
4860 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
4861 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4862 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4863 return;
4864 }
4865
Cyril Bontéb21570a2009-11-29 20:04:48 +01004866 memcpy(sessid_temp, buf, len);
4867 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004868
4869 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
4870 /* free previously allocated memory */
4871 pool_free2(apools.sessid, sessid_temp);
4872
4873 if (asession != NULL) {
4874 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
4875 if (!(t->be->options2 & PR_O2_AS_REQL))
4876 asession->request_count++;
4877
4878 if (asession->serverid != NULL) {
4879 struct server *srv = t->be->srv;
4880 while (srv) {
4881 if (strcmp(srv->id, asession->serverid) == 0) {
4882 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
4883 /* we found the server and it's usable */
4884 txn->flags &= ~TX_CK_MASK;
4885 txn->flags |= TX_CK_VALID;
4886 t->flags |= SN_DIRECT | SN_ASSIGNED;
4887 t->srv = srv;
4888 break;
4889 } else {
4890 txn->flags &= ~TX_CK_MASK;
4891 txn->flags |= TX_CK_DOWN;
4892 }
4893 }
4894 srv = srv->next;
4895 }
4896 }
4897 }
4898}
4899
4900/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004901 * Manage client-side cookie. It can impact performance by about 2% so it is
4902 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004903 */
4904void manage_client_side_cookies(struct session *t, struct buffer *req)
4905{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004906 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004907 char *p1, *p2, *p3, *p4;
4908 char *del_colon, *del_cookie, *colon;
4909 int app_cookies;
4910
Willy Tarreau58f10d72006-12-04 02:26:12 +01004911 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004912 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004913
Willy Tarreau2a324282006-12-05 00:05:46 +01004914 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004915 * we start with the start line.
4916 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004917 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004918 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004919
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004920 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004921 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004922 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004923
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004924 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004925 cur_ptr = cur_next;
4926 cur_end = cur_ptr + cur_hdr->len;
4927 cur_next = cur_end + cur_hdr->cr + 1;
4928
4929 /* We have one full header between cur_ptr and cur_end, and the
4930 * next header starts at cur_next. We're only interested in
4931 * "Cookie:" headers.
4932 */
4933
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004934 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4935 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004936 old_idx = cur_idx;
4937 continue;
4938 }
4939
4940 /* Now look for cookies. Conforming to RFC2109, we have to support
4941 * attributes whose name begin with a '$', and associate them with
4942 * the right cookie, if we want to delete this cookie.
4943 * So there are 3 cases for each cookie read :
4944 * 1) it's a special attribute, beginning with a '$' : ignore it.
4945 * 2) it's a server id cookie that we *MAY* want to delete : save
4946 * some pointers on it (last semi-colon, beginning of cookie...)
4947 * 3) it's an application cookie : we *MAY* have to delete a previous
4948 * "special" cookie.
4949 * At the end of loop, if a "special" cookie remains, we may have to
4950 * remove it. If no application cookie persists in the header, we
4951 * *MUST* delete it
4952 */
4953
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004954 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004955
Willy Tarreau58f10d72006-12-04 02:26:12 +01004956 /* del_cookie == NULL => nothing to be deleted */
4957 del_colon = del_cookie = NULL;
4958 app_cookies = 0;
4959
4960 while (p1 < cur_end) {
4961 /* skip spaces and colons, but keep an eye on these ones */
4962 while (p1 < cur_end) {
4963 if (*p1 == ';' || *p1 == ',')
4964 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004965 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004966 break;
4967 p1++;
4968 }
4969
4970 if (p1 == cur_end)
4971 break;
4972
4973 /* p1 is at the beginning of the cookie name */
4974 p2 = p1;
4975 while (p2 < cur_end && *p2 != '=')
4976 p2++;
4977
4978 if (p2 == cur_end)
4979 break;
4980
4981 p3 = p2 + 1; /* skips the '=' sign */
4982 if (p3 == cur_end)
4983 break;
4984
4985 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004986 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004987 p4++;
4988
4989 /* here, we have the cookie name between p1 and p2,
4990 * and its value between p3 and p4.
4991 * we can process it :
4992 *
4993 * Cookie: NAME=VALUE;
4994 * | || || |
4995 * | || || +--> p4
4996 * | || |+-------> p3
4997 * | || +--------> p2
4998 * | |+------------> p1
4999 * | +-------------> colon
5000 * +--------------------> cur_ptr
5001 */
5002
5003 if (*p1 == '$') {
5004 /* skip this one */
5005 }
5006 else {
5007 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005008 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005009 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005010 (p4 - p1 >= t->fe->capture_namelen) &&
5011 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005012 int log_len = p4 - p1;
5013
Willy Tarreau086b3b42007-05-13 21:45:51 +02005014 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005015 Alert("HTTP logging : out of memory.\n");
5016 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005017 if (log_len > t->fe->capture_len)
5018 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005019 memcpy(txn->cli_cookie, p1, log_len);
5020 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005021 }
5022 }
5023
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005024 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5025 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005026 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005027 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005028 char *delim;
5029
5030 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5031 * have the server ID betweek p3 and delim, and the original cookie between
5032 * delim+1 and p4. Otherwise, delim==p4 :
5033 *
5034 * Cookie: NAME=SRV~VALUE;
5035 * | || || | |
5036 * | || || | +--> p4
5037 * | || || +--------> delim
5038 * | || |+-----------> p3
5039 * | || +------------> p2
5040 * | |+----------------> p1
5041 * | +-----------------> colon
5042 * +------------------------> cur_ptr
5043 */
5044
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005045 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005046 for (delim = p3; delim < p4; delim++)
5047 if (*delim == COOKIE_DELIM)
5048 break;
5049 }
5050 else
5051 delim = p4;
5052
5053
5054 /* Here, we'll look for the first running server which supports the cookie.
5055 * This allows to share a same cookie between several servers, for example
5056 * to dedicate backup servers to specific servers only.
5057 * However, to prevent clients from sticking to cookie-less backup server
5058 * when they have incidentely learned an empty cookie, we simply ignore
5059 * empty cookies and mark them as invalid.
5060 */
5061 if (delim == p3)
5062 srv = NULL;
5063
5064 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005065 if (srv->cookie && (srv->cklen == delim - p3) &&
5066 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005067 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005068 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005069 txn->flags &= ~TX_CK_MASK;
5070 txn->flags |= TX_CK_VALID;
5071 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005072 t->srv = srv;
5073 break;
5074 } else {
5075 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005076 txn->flags &= ~TX_CK_MASK;
5077 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005078 }
5079 }
5080 srv = srv->next;
5081 }
5082
Willy Tarreau3d300592007-03-18 18:34:41 +01005083 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005084 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005085 txn->flags &= ~TX_CK_MASK;
5086 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005087 }
5088
5089 /* depending on the cookie mode, we may have to either :
5090 * - delete the complete cookie if we're in insert+indirect mode, so that
5091 * the server never sees it ;
5092 * - remove the server id from the cookie value, and tag the cookie as an
5093 * application cookie so that it does not get accidentely removed later,
5094 * if we're in cookie prefix mode
5095 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005096 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005097 int delta; /* negative */
5098
5099 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5100 p4 += delta;
5101 cur_end += delta;
5102 cur_next += delta;
5103 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005104 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005105
5106 del_cookie = del_colon = NULL;
5107 app_cookies++; /* protect the header from deletion */
5108 }
5109 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005110 (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 +01005111 del_cookie = p1;
5112 del_colon = colon;
5113 }
5114 } else {
5115 /* now we know that we must keep this cookie since it's
5116 * not ours. But if we wanted to delete our cookie
5117 * earlier, we cannot remove the complete header, but we
5118 * can remove the previous block itself.
5119 */
5120 app_cookies++;
5121
5122 if (del_cookie != NULL) {
5123 int delta; /* negative */
5124
5125 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5126 p4 += delta;
5127 cur_end += delta;
5128 cur_next += delta;
5129 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005130 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005131 del_cookie = del_colon = NULL;
5132 }
5133 }
5134
Cyril Bontéb21570a2009-11-29 20:04:48 +01005135 if (t->be->appsession_name != NULL) {
5136 int cmp_len, value_len;
5137 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005138
Cyril Bontéb21570a2009-11-29 20:04:48 +01005139 if (t->be->options2 & PR_O2_AS_PFX) {
5140 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5141 value_begin = p1 + t->be->appsession_name_len;
5142 value_len = p4 - p1 - t->be->appsession_name_len;
5143 } else {
5144 cmp_len = p2 - p1;
5145 value_begin = p3;
5146 value_len = p4 - p3;
5147 }
5148
5149 /* let's see if the cookie is our appcookie */
5150 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5151 /* Cool... it's the right one */
5152 manage_client_side_appsession(t, value_begin, value_len);
5153 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005154#if defined(DEBUG_HASH)
5155 Alert("manage_client_side_cookies\n");
5156 appsession_hash_dump(&(t->be->htbl_proxy));
5157#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005158 }/* end if ((t->proxy->appsession_name != NULL) ... */
5159 }
5160
5161 /* we'll have to look for another cookie ... */
5162 p1 = p4;
5163 } /* while (p1 < cur_end) */
5164
5165 /* There's no more cookie on this line.
5166 * We may have marked the last one(s) for deletion.
5167 * We must do this now in two ways :
5168 * - if there is no app cookie, we simply delete the header ;
5169 * - if there are app cookies, we must delete the end of the
5170 * string properly, including the colon/semi-colon before
5171 * the cookie name.
5172 */
5173 if (del_cookie != NULL) {
5174 int delta;
5175 if (app_cookies) {
5176 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5177 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005178 cur_hdr->len += delta;
5179 } else {
5180 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005181
5182 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005183 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5184 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005185 cur_hdr->len = 0;
5186 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005187 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005188 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005189 }
5190
5191 /* keep the link from this header to next one */
5192 old_idx = cur_idx;
5193 } /* end of cookie processing on this header */
5194}
5195
5196
Willy Tarreaua15645d2007-03-18 16:22:39 +01005197/* Iterate the same filter through all response headers contained in <rtr>.
5198 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5199 */
5200int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5201{
5202 char term;
5203 char *cur_ptr, *cur_end, *cur_next;
5204 int cur_idx, old_idx, last_hdr;
5205 struct http_txn *txn = &t->txn;
5206 struct hdr_idx_elem *cur_hdr;
5207 int len, delta;
5208
5209 last_hdr = 0;
5210
5211 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5212 old_idx = 0;
5213
5214 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005215 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005216 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005217 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005218 (exp->action == ACT_ALLOW ||
5219 exp->action == ACT_DENY))
5220 return 0;
5221
5222 cur_idx = txn->hdr_idx.v[old_idx].next;
5223 if (!cur_idx)
5224 break;
5225
5226 cur_hdr = &txn->hdr_idx.v[cur_idx];
5227 cur_ptr = cur_next;
5228 cur_end = cur_ptr + cur_hdr->len;
5229 cur_next = cur_end + cur_hdr->cr + 1;
5230
5231 /* Now we have one header between cur_ptr and cur_end,
5232 * and the next header starts at cur_next.
5233 */
5234
5235 /* The annoying part is that pattern matching needs
5236 * that we modify the contents to null-terminate all
5237 * strings before testing them.
5238 */
5239
5240 term = *cur_end;
5241 *cur_end = '\0';
5242
5243 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5244 switch (exp->action) {
5245 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005246 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005247 last_hdr = 1;
5248 break;
5249
5250 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005251 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005252 last_hdr = 1;
5253 break;
5254
5255 case ACT_REPLACE:
5256 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5257 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5258 /* FIXME: if the user adds a newline in the replacement, the
5259 * index will not be recalculated for now, and the new line
5260 * will not be counted as a new header.
5261 */
5262
5263 cur_end += delta;
5264 cur_next += delta;
5265 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005266 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005267 break;
5268
5269 case ACT_REMOVE:
5270 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5271 cur_next += delta;
5272
Willy Tarreaufa355d42009-11-29 18:12:29 +01005273 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005274 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5275 txn->hdr_idx.used--;
5276 cur_hdr->len = 0;
5277 cur_end = NULL; /* null-term has been rewritten */
5278 break;
5279
5280 }
5281 }
5282 if (cur_end)
5283 *cur_end = term; /* restore the string terminator */
5284
5285 /* keep the link from this header to next one in case of later
5286 * removal of next header.
5287 */
5288 old_idx = cur_idx;
5289 }
5290 return 0;
5291}
5292
5293
5294/* Apply the filter to the status line in the response buffer <rtr>.
5295 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5296 * or -1 if a replacement resulted in an invalid status line.
5297 */
5298int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5299{
5300 char term;
5301 char *cur_ptr, *cur_end;
5302 int done;
5303 struct http_txn *txn = &t->txn;
5304 int len, delta;
5305
5306
Willy Tarreau3d300592007-03-18 18:34:41 +01005307 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005308 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005309 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005310 (exp->action == ACT_ALLOW ||
5311 exp->action == ACT_DENY))
5312 return 0;
5313 else if (exp->action == ACT_REMOVE)
5314 return 0;
5315
5316 done = 0;
5317
Willy Tarreau9cdde232007-05-02 20:58:19 +02005318 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005319 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5320
5321 /* Now we have the status line between cur_ptr and cur_end */
5322
5323 /* The annoying part is that pattern matching needs
5324 * that we modify the contents to null-terminate all
5325 * strings before testing them.
5326 */
5327
5328 term = *cur_end;
5329 *cur_end = '\0';
5330
5331 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5332 switch (exp->action) {
5333 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005334 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005335 done = 1;
5336 break;
5337
5338 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005339 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005340 done = 1;
5341 break;
5342
5343 case ACT_REPLACE:
5344 *cur_end = term; /* restore the string terminator */
5345 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5346 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5347 /* FIXME: if the user adds a newline in the replacement, the
5348 * index will not be recalculated for now, and the new line
5349 * will not be counted as a new header.
5350 */
5351
Willy Tarreaufa355d42009-11-29 18:12:29 +01005352 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005353 cur_end += delta;
5354
Willy Tarreau9cdde232007-05-02 20:58:19 +02005355 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005356 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005357 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005358 cur_ptr, cur_end + 1,
5359 NULL, NULL);
5360 if (unlikely(!cur_end))
5361 return -1;
5362
5363 /* we have a full respnse and we know that we have either a CR
5364 * or an LF at <ptr>.
5365 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005366 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005367 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5368 /* there is no point trying this regex on headers */
5369 return 1;
5370 }
5371 }
5372 *cur_end = term; /* restore the string terminator */
5373 return done;
5374}
5375
5376
5377
5378/*
5379 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5380 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5381 * unparsable response.
5382 */
5383int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5384{
Willy Tarreau3d300592007-03-18 18:34:41 +01005385 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005386 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005387 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005388 int ret;
5389
5390 /*
5391 * The interleaving of transformations and verdicts
5392 * makes it difficult to decide to continue or stop
5393 * the evaluation.
5394 */
5395
Willy Tarreau3d300592007-03-18 18:34:41 +01005396 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005397 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5398 exp->action == ACT_PASS)) {
5399 exp = exp->next;
5400 continue;
5401 }
5402
5403 /* Apply the filter to the status line. */
5404 ret = apply_filter_to_sts_line(t, rtr, exp);
5405 if (unlikely(ret < 0))
5406 return -1;
5407
5408 if (likely(ret == 0)) {
5409 /* The filter did not match the response, it can be
5410 * iterated through all headers.
5411 */
5412 apply_filter_to_resp_headers(t, rtr, exp);
5413 }
5414 exp = exp->next;
5415 }
5416 return 0;
5417}
5418
5419
5420
5421/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005422 * Manage server-side cookies. It can impact performance by about 2% so it is
5423 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005424 */
5425void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5426{
5427 struct http_txn *txn = &t->txn;
5428 char *p1, *p2, *p3, *p4;
5429
Willy Tarreaua15645d2007-03-18 16:22:39 +01005430 char *cur_ptr, *cur_end, *cur_next;
5431 int cur_idx, old_idx, delta;
5432
Willy Tarreaua15645d2007-03-18 16:22:39 +01005433 /* Iterate through the headers.
5434 * we start with the start line.
5435 */
5436 old_idx = 0;
5437 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5438
5439 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5440 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005441 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005442
5443 cur_hdr = &txn->hdr_idx.v[cur_idx];
5444 cur_ptr = cur_next;
5445 cur_end = cur_ptr + cur_hdr->len;
5446 cur_next = cur_end + cur_hdr->cr + 1;
5447
5448 /* We have one full header between cur_ptr and cur_end, and the
5449 * next header starts at cur_next. We're only interested in
5450 * "Cookie:" headers.
5451 */
5452
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005453 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5454 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005455 old_idx = cur_idx;
5456 continue;
5457 }
5458
5459 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005460 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005461
5462
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005463 /* maybe we only wanted to see if there was a set-cookie. Note that
5464 * the cookie capture is declared in the fronend.
5465 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005466 if (t->be->cookie_name == NULL &&
5467 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005468 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005469 return;
5470
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005471 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005472
5473 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005474 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5475 break;
5476
5477 /* p1 is at the beginning of the cookie name */
5478 p2 = p1;
5479
5480 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5481 p2++;
5482
5483 if (p2 == cur_end || *p2 == ';') /* next cookie */
5484 break;
5485
5486 p3 = p2 + 1; /* skip the '=' sign */
5487 if (p3 == cur_end)
5488 break;
5489
5490 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005491 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005492 p4++;
5493
5494 /* here, we have the cookie name between p1 and p2,
5495 * and its value between p3 and p4.
5496 * we can process it.
5497 */
5498
5499 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005500 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005501 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005502 (p4 - p1 >= t->fe->capture_namelen) &&
5503 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005504 int log_len = p4 - p1;
5505
Willy Tarreau086b3b42007-05-13 21:45:51 +02005506 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005507 Alert("HTTP logging : out of memory.\n");
5508 }
5509
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005510 if (log_len > t->fe->capture_len)
5511 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005512 memcpy(txn->srv_cookie, p1, log_len);
5513 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005514 }
5515
5516 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005517 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5518 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005519 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005520 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005521
5522 /* If the cookie is in insert mode on a known server, we'll delete
5523 * this occurrence because we'll insert another one later.
5524 * We'll delete it too if the "indirect" option is set and we're in
5525 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005526 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5527 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005528 /* this header must be deleted */
5529 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5530 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5531 txn->hdr_idx.used--;
5532 cur_hdr->len = 0;
5533 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005534 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005535
Willy Tarreau3d300592007-03-18 18:34:41 +01005536 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005537 }
5538 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005539 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005540 /* replace bytes p3->p4 with the cookie name associated
5541 * with this server since we know it.
5542 */
5543 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5544 cur_hdr->len += delta;
5545 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005546 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005547
Willy Tarreau3d300592007-03-18 18:34:41 +01005548 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005549 }
5550 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005551 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005552 /* insert the cookie name associated with this server
5553 * before existing cookie, and insert a delimitor between them..
5554 */
5555 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5556 cur_hdr->len += delta;
5557 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005558 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005559
5560 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005561 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005562 }
5563 }
5564 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005565 else if (t->be->appsession_name != NULL) {
5566 int cmp_len, value_len;
5567 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005568
Cyril Bontéb21570a2009-11-29 20:04:48 +01005569 if (t->be->options2 & PR_O2_AS_PFX) {
5570 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5571 value_begin = p1 + t->be->appsession_name_len;
5572 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5573 } else {
5574 cmp_len = p2 - p1;
5575 value_begin = p3;
5576 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005577 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005578
5579 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5580 /* Cool... it's the right one */
5581 if (t->sessid != NULL) {
5582 /* free previously allocated memory as we don't need it anymore */
5583 pool_free2(apools.sessid, t->sessid);
5584 }
5585 /* Store the sessid in the session for future use */
5586 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5587 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5588 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5589 return;
5590 }
5591 memcpy(t->sessid, value_begin, value_len);
5592 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005593 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005594 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005595 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5596 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005597 /* keep the link from this header to next one */
5598 old_idx = cur_idx;
5599 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005600
5601 if (t->sessid != NULL) {
5602 appsess *asession = NULL;
5603 /* only do insert, if lookup fails */
5604 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5605 if (asession == NULL) {
5606 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5607 Alert("Not enough Memory process_srv():asession:calloc().\n");
5608 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5609 return;
5610 }
5611 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5612 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5613 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5614 return;
5615 }
5616 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5617 asession->sessid[t->be->appsession_len] = 0;
5618
5619 size_t server_id_len = strlen(t->srv->id) + 1;
5620 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5621 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5622 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5623 return;
5624 }
5625 asession->serverid[0] = '\0';
5626 memcpy(asession->serverid, t->srv->id, server_id_len);
5627
5628 asession->request_count = 0;
5629 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5630 }
5631
5632 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5633 asession->request_count++;
5634 }
5635
5636#if defined(DEBUG_HASH)
5637 Alert("manage_server_side_cookies\n");
5638 appsession_hash_dump(&(t->be->htbl_proxy));
5639#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005640}
5641
5642
5643
5644/*
5645 * Check if response is cacheable or not. Updates t->flags.
5646 */
5647void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5648{
5649 struct http_txn *txn = &t->txn;
5650 char *p1, *p2;
5651
5652 char *cur_ptr, *cur_end, *cur_next;
5653 int cur_idx;
5654
Willy Tarreau5df51872007-11-25 16:20:08 +01005655 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005656 return;
5657
5658 /* Iterate through the headers.
5659 * we start with the start line.
5660 */
5661 cur_idx = 0;
5662 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5663
5664 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5665 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005666 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005667
5668 cur_hdr = &txn->hdr_idx.v[cur_idx];
5669 cur_ptr = cur_next;
5670 cur_end = cur_ptr + cur_hdr->len;
5671 cur_next = cur_end + cur_hdr->cr + 1;
5672
5673 /* We have one full header between cur_ptr and cur_end, and the
5674 * next header starts at cur_next. We're only interested in
5675 * "Cookie:" headers.
5676 */
5677
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005678 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5679 if (val) {
5680 if ((cur_end - (cur_ptr + val) >= 8) &&
5681 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5682 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5683 return;
5684 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005685 }
5686
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005687 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5688 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005689 continue;
5690
5691 /* OK, right now we know we have a cache-control header at cur_ptr */
5692
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005693 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005694
5695 if (p1 >= cur_end) /* no more info */
5696 continue;
5697
5698 /* p1 is at the beginning of the value */
5699 p2 = p1;
5700
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005701 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005702 p2++;
5703
5704 /* we have a complete value between p1 and p2 */
5705 if (p2 < cur_end && *p2 == '=') {
5706 /* we have something of the form no-cache="set-cookie" */
5707 if ((cur_end - p1 >= 21) &&
5708 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5709 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005710 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005711 continue;
5712 }
5713
5714 /* OK, so we know that either p2 points to the end of string or to a comma */
5715 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5716 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5717 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5718 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005719 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005720 return;
5721 }
5722
5723 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005724 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005725 continue;
5726 }
5727 }
5728}
5729
5730
Willy Tarreau58f10d72006-12-04 02:26:12 +01005731/*
5732 * Try to retrieve a known appsession in the URI, then the associated server.
5733 * If the server is found, it's assigned to the session.
5734 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005735void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005736{
Cyril Bontéb21570a2009-11-29 20:04:48 +01005737 char *end_params, *first_param, *cur_param, *next_param;
5738 char separator;
5739 int value_len;
5740
5741 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005742
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005743 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01005744 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005745 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005746 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005747
Cyril Bontéb21570a2009-11-29 20:04:48 +01005748 first_param = NULL;
5749 switch (mode) {
5750 case PR_O2_AS_M_PP:
5751 first_param = memchr(begin, ';', len);
5752 break;
5753 case PR_O2_AS_M_QS:
5754 first_param = memchr(begin, '?', len);
5755 break;
5756 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005757
Cyril Bontéb21570a2009-11-29 20:04:48 +01005758 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005759 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005760 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005761
Cyril Bontéb21570a2009-11-29 20:04:48 +01005762 switch (mode) {
5763 case PR_O2_AS_M_PP:
5764 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
5765 end_params = (char *) begin + len;
5766 }
5767 separator = ';';
5768 break;
5769 case PR_O2_AS_M_QS:
5770 end_params = (char *) begin + len;
5771 separator = '&';
5772 break;
5773 default:
5774 /* unknown mode, shouldn't happen */
5775 return;
5776 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005777
Cyril Bontéb21570a2009-11-29 20:04:48 +01005778 cur_param = next_param = end_params;
5779 while (cur_param > first_param) {
5780 cur_param--;
5781 if ((cur_param[0] == separator) || (cur_param == first_param)) {
5782 /* let's see if this is the appsession parameter */
5783 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
5784 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
5785 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
5786 /* Cool... it's the right one */
5787 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
5788 value_len = MIN(t->be->appsession_len, next_param - cur_param);
5789 if (value_len > 0) {
5790 manage_client_side_appsession(t, cur_param, value_len);
5791 }
5792 break;
5793 }
5794 next_param = cur_param;
5795 }
5796 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005797#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005798 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005799 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005800#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005801}
5802
5803
Willy Tarreaub2513902006-12-17 14:52:38 +01005804/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005805 * In a GET or HEAD request, check if the requested URI matches the stats uri
5806 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005807 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005808 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005809 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005810 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005811 *
5812 * Returns 1 if the session's state changes, otherwise 0.
5813 */
5814int stats_check_uri_auth(struct session *t, struct proxy *backend)
5815{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005816 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005817 struct uri_auth *uri_auth = backend->uri_auth;
5818 struct user_auth *user;
5819 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005820 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005821
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005822 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5823
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005824 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005825 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005826 return 0;
5827
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005828 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005829
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005830 /* the URI is in h */
5831 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005832 return 0;
5833
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005834 h += uri_auth->uri_len;
5835 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5836 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005837 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005838 break;
5839 }
5840 h++;
5841 }
5842
5843 if (uri_auth->refresh) {
5844 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5845 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5846 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005847 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005848 break;
5849 }
5850 h++;
5851 }
5852 }
5853
Willy Tarreau55bb8452007-10-17 18:44:57 +02005854 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5855 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5856 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005857 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005858 break;
5859 }
5860 h++;
5861 }
5862
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005863 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5864
Willy Tarreaub2513902006-12-17 14:52:38 +01005865 /* we are in front of a interceptable URI. Let's check
5866 * if there's an authentication and if it's valid.
5867 */
5868 user = uri_auth->users;
5869 if (!user) {
5870 /* no user auth required, it's OK */
5871 authenticated = 1;
5872 } else {
5873 authenticated = 0;
5874
5875 /* a user list is defined, we have to check.
5876 * skip 21 chars for "Authorization: Basic ".
5877 */
5878
5879 /* FIXME: this should move to an earlier place */
5880 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005881 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5882 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5883 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005884 if (len > 14 &&
5885 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02005886 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01005887 break;
5888 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005889 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005890 }
5891
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005892 if (txn->auth_hdr.len < 21 ||
5893 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005894 user = NULL;
5895
5896 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005897 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5898 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005899 user->user_pwd, user->user_len)) {
5900 authenticated = 1;
5901 break;
5902 }
5903 user = user->next;
5904 }
5905 }
5906
5907 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005908 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005909
5910 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005911 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
5912 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005913 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01005914 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005915 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005916 if (!(t->flags & SN_ERR_MASK))
5917 t->flags |= SN_ERR_PRXCOND;
5918 if (!(t->flags & SN_FINST_MASK))
5919 t->flags |= SN_FINST_R;
5920 return 1;
5921 }
5922
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005923 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005924 * data.
5925 */
Willy Tarreau70089872008-06-13 21:12:51 +02005926 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005927 t->data_source = DATA_SRC_STATS;
5928 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005929 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005930 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
5931 t->rep->prod->private = t;
5932 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005933 return 1;
5934}
5935
Willy Tarreau4076a152009-04-02 15:18:36 +02005936/*
5937 * Capture a bad request or response and archive it in the proxy's structure.
5938 */
5939void http_capture_bad_message(struct error_snapshot *es, struct session *s,
5940 struct buffer *buf, struct http_msg *msg,
5941 struct proxy *other_end)
5942{
Willy Tarreau2df8d712009-05-01 11:33:17 +02005943 es->len = buf->r - (buf->data + msg->som);
5944 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02005945 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02005946 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02005947 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02005948 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02005949 es->when = date; // user-visible date
5950 es->sid = s->uniq_id;
5951 es->srv = s->srv;
5952 es->oe = other_end;
5953 es->src = s->cli_addr;
5954}
Willy Tarreaub2513902006-12-17 14:52:38 +01005955
Willy Tarreaubaaee002006-06-26 02:48:02 +02005956/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005957 * Print a debug line with a header
5958 */
5959void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5960{
5961 int len, max;
5962 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005963 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005964 max = end - start;
5965 UBOUND(max, sizeof(trash) - len - 1);
5966 len += strlcpy2(trash + len, start, max + 1);
5967 trash[len++] = '\n';
5968 write(1, trash, len);
5969}
5970
Willy Tarreau0937bc42009-12-22 15:03:09 +01005971/*
5972 * Initialize a new HTTP transaction for session <s>. It is assumed that all
5973 * the required fields are properly allocated and that we only need to (re)init
5974 * them. This should be used before processing any new request.
5975 */
5976void http_init_txn(struct session *s)
5977{
5978 struct http_txn *txn = &s->txn;
5979 struct proxy *fe = s->fe;
5980
5981 txn->flags = 0;
5982 txn->status = -1;
5983
5984 txn->req.sol = txn->req.eol = NULL;
5985 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
5986 txn->rsp.sol = txn->rsp.eol = NULL;
5987 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
5988 txn->req.hdr_content_len = 0LL;
5989 txn->rsp.hdr_content_len = 0LL;
5990 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5991 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5992 chunk_reset(&txn->auth_hdr);
5993
5994 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
5995 if (fe->options2 & PR_O2_REQBUG_OK)
5996 txn->req.err_pos = -1; /* let buggy requests pass */
5997
5998 if (txn->req.cap)
5999 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6000
6001 if (txn->rsp.cap)
6002 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6003
6004 if (txn->hdr_idx.v)
6005 hdr_idx_init(&txn->hdr_idx);
6006}
6007
6008/* to be used at the end of a transaction */
6009void http_end_txn(struct session *s)
6010{
6011 struct http_txn *txn = &s->txn;
6012
6013 /* these ones will have been dynamically allocated */
6014 pool_free2(pool2_requri, txn->uri);
6015 pool_free2(pool2_capture, txn->cli_cookie);
6016 pool_free2(pool2_capture, txn->srv_cookie);
6017 txn->uri = NULL;
6018 txn->srv_cookie = NULL;
6019 txn->cli_cookie = NULL;
6020}
6021
6022/* to be used at the end of a transaction to prepare a new one */
6023void http_reset_txn(struct session *s)
6024{
6025 http_end_txn(s);
6026 http_init_txn(s);
6027
6028 s->be = s->fe;
6029 s->req->analysers = s->listener->analysers;
6030 s->logs.logwait = s->fe->to_log;
6031 s->srv = s->prev_srv = s->srv_conn = NULL;
6032 s->pend_pos = NULL;
6033 s->conn_retries = s->be->conn_retries;
6034
6035 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6036
6037 s->req->rto = s->fe->timeout.client;
6038 s->req->wto = s->be->timeout.server;
6039 s->req->cto = s->be->timeout.connect;
6040
6041 s->rep->rto = s->be->timeout.server;
6042 s->rep->wto = s->fe->timeout.client;
6043 s->rep->cto = TICK_ETERNITY;
6044
6045 s->req->rex = TICK_ETERNITY;
6046 s->req->wex = TICK_ETERNITY;
6047 s->req->analyse_exp = TICK_ETERNITY;
6048 s->rep->rex = TICK_ETERNITY;
6049 s->rep->wex = TICK_ETERNITY;
6050 s->rep->analyse_exp = TICK_ETERNITY;
6051}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006052
Willy Tarreau8797c062007-05-07 00:55:35 +02006053/************************************************************************/
6054/* The code below is dedicated to ACL parsing and matching */
6055/************************************************************************/
6056
6057
6058
6059
6060/* 1. Check on METHOD
6061 * We use the pre-parsed method if it is known, and store its number as an
6062 * integer. If it is unknown, we use the pointer and the length.
6063 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006064static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006065{
6066 int len, meth;
6067
Willy Tarreauae8b7962007-06-09 23:10:04 +02006068 len = strlen(*text);
6069 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006070
6071 pattern->val.i = meth;
6072 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006073 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006074 if (!pattern->ptr.str)
6075 return 0;
6076 pattern->len = len;
6077 }
6078 return 1;
6079}
6080
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006081static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006082acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6083 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006084{
6085 int meth;
6086 struct http_txn *txn = l7;
6087
Willy Tarreaub6866442008-07-14 23:54:42 +02006088 if (!txn)
6089 return 0;
6090
Willy Tarreau655dce92009-11-08 13:10:58 +01006091 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006092 return 0;
6093
Willy Tarreau8797c062007-05-07 00:55:35 +02006094 meth = txn->meth;
6095 test->i = meth;
6096 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006097 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6098 /* ensure the indexes are not affected */
6099 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006100 test->len = txn->req.sl.rq.m_l;
6101 test->ptr = txn->req.sol;
6102 }
6103 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6104 return 1;
6105}
6106
6107static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6108{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006109 int icase;
6110
Willy Tarreau8797c062007-05-07 00:55:35 +02006111 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006112 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006113
6114 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006115 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006116
6117 /* Other method, we must compare the strings */
6118 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006119 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006120
6121 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6122 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6123 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006124 return ACL_PAT_FAIL;
6125 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006126}
6127
6128/* 2. Check on Request/Status Version
6129 * We simply compare strings here.
6130 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006131static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006132{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006133 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006134 if (!pattern->ptr.str)
6135 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006136 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006137 return 1;
6138}
6139
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006140static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006141acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6142 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006143{
6144 struct http_txn *txn = l7;
6145 char *ptr;
6146 int len;
6147
Willy Tarreaub6866442008-07-14 23:54:42 +02006148 if (!txn)
6149 return 0;
6150
Willy Tarreau655dce92009-11-08 13:10:58 +01006151 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006152 return 0;
6153
Willy Tarreau8797c062007-05-07 00:55:35 +02006154 len = txn->req.sl.rq.v_l;
6155 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
6156
6157 while ((len-- > 0) && (*ptr++ != '/'));
6158 if (len <= 0)
6159 return 0;
6160
6161 test->ptr = ptr;
6162 test->len = len;
6163
6164 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6165 return 1;
6166}
6167
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006168static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006169acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6170 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006171{
6172 struct http_txn *txn = l7;
6173 char *ptr;
6174 int len;
6175
Willy Tarreaub6866442008-07-14 23:54:42 +02006176 if (!txn)
6177 return 0;
6178
Willy Tarreau655dce92009-11-08 13:10:58 +01006179 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006180 return 0;
6181
Willy Tarreau8797c062007-05-07 00:55:35 +02006182 len = txn->rsp.sl.st.v_l;
6183 ptr = txn->rsp.sol;
6184
6185 while ((len-- > 0) && (*ptr++ != '/'));
6186 if (len <= 0)
6187 return 0;
6188
6189 test->ptr = ptr;
6190 test->len = len;
6191
6192 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6193 return 1;
6194}
6195
6196/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006197static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006198acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6199 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006200{
6201 struct http_txn *txn = l7;
6202 char *ptr;
6203 int len;
6204
Willy Tarreaub6866442008-07-14 23:54:42 +02006205 if (!txn)
6206 return 0;
6207
Willy Tarreau655dce92009-11-08 13:10:58 +01006208 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006209 return 0;
6210
Willy Tarreau8797c062007-05-07 00:55:35 +02006211 len = txn->rsp.sl.st.c_l;
6212 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6213
6214 test->i = __strl2ui(ptr, len);
6215 test->flags = ACL_TEST_F_VOL_1ST;
6216 return 1;
6217}
6218
6219/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006220static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006221acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6222 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006223{
6224 struct http_txn *txn = l7;
6225
Willy Tarreaub6866442008-07-14 23:54:42 +02006226 if (!txn)
6227 return 0;
6228
Willy Tarreau655dce92009-11-08 13:10:58 +01006229 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006230 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006231
Willy Tarreauc11416f2007-06-17 16:58:38 +02006232 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6233 /* ensure the indexes are not affected */
6234 return 0;
6235
Willy Tarreau8797c062007-05-07 00:55:35 +02006236 test->len = txn->req.sl.rq.u_l;
6237 test->ptr = txn->req.sol + txn->req.sl.rq.u;
6238
Willy Tarreauf3d25982007-05-08 22:45:09 +02006239 /* we do not need to set READ_ONLY because the data is in a buffer */
6240 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006241 return 1;
6242}
6243
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006244static int
6245acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6246 struct acl_expr *expr, struct acl_test *test)
6247{
6248 struct http_txn *txn = l7;
6249
Willy Tarreaub6866442008-07-14 23:54:42 +02006250 if (!txn)
6251 return 0;
6252
Willy Tarreau655dce92009-11-08 13:10:58 +01006253 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006254 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006255
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006256 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6257 /* ensure the indexes are not affected */
6258 return 0;
6259
6260 /* Parse HTTP request */
6261 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6262 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6263 test->i = AF_INET;
6264
6265 /*
6266 * If we are parsing url in frontend space, we prepare backend stage
6267 * to not parse again the same url ! optimization lazyness...
6268 */
6269 if (px->options & PR_O_HTTP_PROXY)
6270 l4->flags |= SN_ADDR_SET;
6271
6272 test->flags = ACL_TEST_F_READ_ONLY;
6273 return 1;
6274}
6275
6276static int
6277acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6278 struct acl_expr *expr, struct acl_test *test)
6279{
6280 struct http_txn *txn = l7;
6281
Willy Tarreaub6866442008-07-14 23:54:42 +02006282 if (!txn)
6283 return 0;
6284
Willy Tarreau655dce92009-11-08 13:10:58 +01006285 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006286 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006287
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006288 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6289 /* ensure the indexes are not affected */
6290 return 0;
6291
6292 /* Same optimization as url_ip */
6293 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6294 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6295
6296 if (px->options & PR_O_HTTP_PROXY)
6297 l4->flags |= SN_ADDR_SET;
6298
6299 test->flags = ACL_TEST_F_READ_ONLY;
6300 return 1;
6301}
6302
Willy Tarreauc11416f2007-06-17 16:58:38 +02006303/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6304 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6305 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006306static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006307acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006308 struct acl_expr *expr, struct acl_test *test)
6309{
6310 struct http_txn *txn = l7;
6311 struct hdr_idx *idx = &txn->hdr_idx;
6312 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006313
Willy Tarreaub6866442008-07-14 23:54:42 +02006314 if (!txn)
6315 return 0;
6316
Willy Tarreau33a7e692007-06-10 19:45:56 +02006317 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6318 /* search for header from the beginning */
6319 ctx->idx = 0;
6320
Willy Tarreau33a7e692007-06-10 19:45:56 +02006321 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6322 test->flags |= ACL_TEST_F_FETCH_MORE;
6323 test->flags |= ACL_TEST_F_VOL_HDR;
6324 test->len = ctx->vlen;
6325 test->ptr = (char *)ctx->line + ctx->val;
6326 return 1;
6327 }
6328
6329 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6330 test->flags |= ACL_TEST_F_VOL_HDR;
6331 return 0;
6332}
6333
Willy Tarreau33a7e692007-06-10 19:45:56 +02006334static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006335acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6336 struct acl_expr *expr, struct acl_test *test)
6337{
6338 struct http_txn *txn = l7;
6339
Willy Tarreaub6866442008-07-14 23:54:42 +02006340 if (!txn)
6341 return 0;
6342
Willy Tarreau655dce92009-11-08 13:10:58 +01006343 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006344 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006345
Willy Tarreauc11416f2007-06-17 16:58:38 +02006346 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6347 /* ensure the indexes are not affected */
6348 return 0;
6349
6350 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6351}
6352
6353static int
6354acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6355 struct acl_expr *expr, struct acl_test *test)
6356{
6357 struct http_txn *txn = l7;
6358
Willy Tarreaub6866442008-07-14 23:54:42 +02006359 if (!txn)
6360 return 0;
6361
Willy Tarreau655dce92009-11-08 13:10:58 +01006362 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006363 return 0;
6364
6365 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6366}
6367
6368/* 6. Check on HTTP header count. The number of occurrences is returned.
6369 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6370 */
6371static int
6372acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006373 struct acl_expr *expr, struct acl_test *test)
6374{
6375 struct http_txn *txn = l7;
6376 struct hdr_idx *idx = &txn->hdr_idx;
6377 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006378 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006379
Willy Tarreaub6866442008-07-14 23:54:42 +02006380 if (!txn)
6381 return 0;
6382
Willy Tarreau33a7e692007-06-10 19:45:56 +02006383 ctx.idx = 0;
6384 cnt = 0;
6385 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6386 cnt++;
6387
6388 test->i = cnt;
6389 test->flags = ACL_TEST_F_VOL_HDR;
6390 return 1;
6391}
6392
Willy Tarreauc11416f2007-06-17 16:58:38 +02006393static int
6394acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6395 struct acl_expr *expr, struct acl_test *test)
6396{
6397 struct http_txn *txn = l7;
6398
Willy Tarreaub6866442008-07-14 23:54:42 +02006399 if (!txn)
6400 return 0;
6401
Willy Tarreau655dce92009-11-08 13:10:58 +01006402 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006403 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006404
Willy Tarreauc11416f2007-06-17 16:58:38 +02006405 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6406 /* ensure the indexes are not affected */
6407 return 0;
6408
6409 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6410}
6411
6412static int
6413acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6414 struct acl_expr *expr, struct acl_test *test)
6415{
6416 struct http_txn *txn = l7;
6417
Willy Tarreaub6866442008-07-14 23:54:42 +02006418 if (!txn)
6419 return 0;
6420
Willy Tarreau655dce92009-11-08 13:10:58 +01006421 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006422 return 0;
6423
6424 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6425}
6426
Willy Tarreau33a7e692007-06-10 19:45:56 +02006427/* 7. Check on HTTP header's integer value. The integer value is returned.
6428 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006429 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006430 */
6431static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006432acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006433 struct acl_expr *expr, struct acl_test *test)
6434{
6435 struct http_txn *txn = l7;
6436 struct hdr_idx *idx = &txn->hdr_idx;
6437 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006438
Willy Tarreaub6866442008-07-14 23:54:42 +02006439 if (!txn)
6440 return 0;
6441
Willy Tarreau33a7e692007-06-10 19:45:56 +02006442 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6443 /* search for header from the beginning */
6444 ctx->idx = 0;
6445
Willy Tarreau33a7e692007-06-10 19:45:56 +02006446 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6447 test->flags |= ACL_TEST_F_FETCH_MORE;
6448 test->flags |= ACL_TEST_F_VOL_HDR;
6449 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6450 return 1;
6451 }
6452
6453 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6454 test->flags |= ACL_TEST_F_VOL_HDR;
6455 return 0;
6456}
6457
Willy Tarreauc11416f2007-06-17 16:58:38 +02006458static int
6459acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6460 struct acl_expr *expr, struct acl_test *test)
6461{
6462 struct http_txn *txn = l7;
6463
Willy Tarreaub6866442008-07-14 23:54:42 +02006464 if (!txn)
6465 return 0;
6466
Willy Tarreau655dce92009-11-08 13:10:58 +01006467 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006468 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006469
Willy Tarreauc11416f2007-06-17 16:58:38 +02006470 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6471 /* ensure the indexes are not affected */
6472 return 0;
6473
6474 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6475}
6476
6477static int
6478acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6479 struct acl_expr *expr, struct acl_test *test)
6480{
6481 struct http_txn *txn = l7;
6482
Willy Tarreaub6866442008-07-14 23:54:42 +02006483 if (!txn)
6484 return 0;
6485
Willy Tarreau655dce92009-11-08 13:10:58 +01006486 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006487 return 0;
6488
6489 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6490}
6491
Willy Tarreau106f9792009-09-19 07:54:16 +02006492/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6493 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6494 */
6495static int
6496acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6497 struct acl_expr *expr, struct acl_test *test)
6498{
6499 struct http_txn *txn = l7;
6500 struct hdr_idx *idx = &txn->hdr_idx;
6501 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6502
6503 if (!txn)
6504 return 0;
6505
6506 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6507 /* search for header from the beginning */
6508 ctx->idx = 0;
6509
6510 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6511 test->flags |= ACL_TEST_F_FETCH_MORE;
6512 test->flags |= ACL_TEST_F_VOL_HDR;
6513 /* Same optimization as url_ip */
6514 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6515 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6516 test->ptr = (void *)&l4->srv_addr.sin_addr;
6517 test->i = AF_INET;
6518 return 1;
6519 }
6520
6521 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6522 test->flags |= ACL_TEST_F_VOL_HDR;
6523 return 0;
6524}
6525
6526static int
6527acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6528 struct acl_expr *expr, struct acl_test *test)
6529{
6530 struct http_txn *txn = l7;
6531
6532 if (!txn)
6533 return 0;
6534
Willy Tarreau655dce92009-11-08 13:10:58 +01006535 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006536 return 0;
6537
6538 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6539 /* ensure the indexes are not affected */
6540 return 0;
6541
6542 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6543}
6544
6545static int
6546acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6547 struct acl_expr *expr, struct acl_test *test)
6548{
6549 struct http_txn *txn = l7;
6550
6551 if (!txn)
6552 return 0;
6553
Willy Tarreau655dce92009-11-08 13:10:58 +01006554 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006555 return 0;
6556
6557 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6558}
6559
Willy Tarreau737b0c12007-06-10 21:28:46 +02006560/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6561 * the first '/' after the possible hostname, and ends before the possible '?'.
6562 */
6563static int
6564acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6565 struct acl_expr *expr, struct acl_test *test)
6566{
6567 struct http_txn *txn = l7;
6568 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006569
Willy Tarreaub6866442008-07-14 23:54:42 +02006570 if (!txn)
6571 return 0;
6572
Willy Tarreau655dce92009-11-08 13:10:58 +01006573 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006574 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006575
Willy Tarreauc11416f2007-06-17 16:58:38 +02006576 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6577 /* ensure the indexes are not affected */
6578 return 0;
6579
Willy Tarreau21d2af32008-02-14 20:25:24 +01006580 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
6581 ptr = http_get_path(txn);
6582 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006583 return 0;
6584
6585 /* OK, we got the '/' ! */
6586 test->ptr = ptr;
6587
6588 while (ptr < end && *ptr != '?')
6589 ptr++;
6590
6591 test->len = ptr - test->ptr;
6592
6593 /* we do not need to set READ_ONLY because the data is in a buffer */
6594 test->flags = ACL_TEST_F_VOL_1ST;
6595 return 1;
6596}
6597
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006598static int
6599acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6600 struct acl_expr *expr, struct acl_test *test)
6601{
6602 struct buffer *req = s->req;
6603 struct http_txn *txn = &s->txn;
6604 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006605
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006606 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6607 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6608 */
6609
6610 if (!s || !req)
6611 return 0;
6612
Willy Tarreau655dce92009-11-08 13:10:58 +01006613 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006614 /* Already decoded as OK */
6615 test->flags |= ACL_TEST_F_SET_RES_PASS;
6616 return 1;
6617 }
6618
6619 /* Try to decode HTTP request */
6620 if (likely(req->lr < req->r))
6621 http_msg_analyzer(req, msg, &txn->hdr_idx);
6622
Willy Tarreau655dce92009-11-08 13:10:58 +01006623 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006624 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6625 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6626 return 1;
6627 }
6628 /* wait for final state */
6629 test->flags |= ACL_TEST_F_MAY_CHANGE;
6630 return 0;
6631 }
6632
6633 /* OK we got a valid HTTP request. We have some minor preparation to
6634 * perform so that further checks can rely on HTTP tests.
6635 */
6636 msg->sol = req->data + msg->som;
6637 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
6638 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6639 s->flags |= SN_REDIRECTABLE;
6640
6641 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
6642 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6643 return 1;
6644 }
6645
6646 test->flags |= ACL_TEST_F_SET_RES_PASS;
6647 return 1;
6648}
6649
Willy Tarreau8797c062007-05-07 00:55:35 +02006650
6651/************************************************************************/
6652/* All supported keywords must be declared here. */
6653/************************************************************************/
6654
6655/* Note: must not be declared <const> as its list will be overwritten */
6656static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006657 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
6658
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006659 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6660 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6661 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6662 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006663
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006664 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6665 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6666 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6667 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6668 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6669 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6670 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6671 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
6672 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02006673
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006674 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
6675 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6676 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6677 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6678 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6679 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6680 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6681 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6682 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
6683 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006684 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02006685
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006686 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6687 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
6688 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
6689 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
6690 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
6691 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
6692 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
6693 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
6694 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006695 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006696
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006697 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6698 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6699 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6700 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6701 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6702 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6703 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006704
Willy Tarreauf3d25982007-05-08 22:45:09 +02006705 { NULL, NULL, NULL, NULL },
6706
6707#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02006708 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
6709 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
6710 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
6711 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
6712 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
6713 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
6714 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
6715
Willy Tarreau8797c062007-05-07 00:55:35 +02006716 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
6717 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
6718 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
6719 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
6720 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
6721 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
6722 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
6723 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
6724
6725 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
6726 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
6727 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
6728 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
6729 { NULL, NULL, NULL, NULL },
6730#endif
6731}};
6732
6733
6734__attribute__((constructor))
6735static void __http_protocol_init(void)
6736{
6737 acl_register_keywords(&acl_kws);
6738}
6739
6740
Willy Tarreau58f10d72006-12-04 02:26:12 +01006741/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006742 * Local variables:
6743 * c-indent-level: 8
6744 * c-basic-offset: 8
6745 * End:
6746 */