blob: 071af6453540a826ea158de39a770633445b92a3 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreau655dce92009-11-08 13:10:58 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010044#include <proto/checks.h>
Maik Broemme2850cb42009-04-17 18:53:21 +020045#include <proto/client.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/fd.h>
48#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010049#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020050#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010052#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010054#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010056#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020057#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/task.h>
59
Willy Tarreau522d6c02009-12-06 18:49:18 +010060const char HTTP_100[] =
61 "HTTP/1.1 100 Continue\r\n\r\n";
62
63const struct chunk http_100_chunk = {
64 .str = (char *)&HTTP_100,
65 .len = sizeof(HTTP_100)-1
66};
67
Willy Tarreau1c47f852006-07-09 08:22:27 +020068/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010069const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020070 "HTTP/1.0 200 OK\r\n"
71 "Cache-Control: no-cache\r\n"
72 "Connection: close\r\n"
73 "Content-Type: text/html\r\n"
74 "\r\n"
75 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
76
Willy Tarreau0f772532006-12-23 20:51:41 +010077const struct chunk http_200_chunk = {
78 .str = (char *)&HTTP_200,
79 .len = sizeof(HTTP_200)-1
80};
81
Willy Tarreaub463dfb2008-06-07 23:08:56 +020082const char *HTTP_301 =
83 "HTTP/1.0 301 Moved Permantenly\r\n"
84 "Cache-Control: no-cache\r\n"
85 "Connection: close\r\n"
86 "Location: "; /* not terminated since it will be concatenated with the URL */
87
Willy Tarreau0f772532006-12-23 20:51:41 +010088const char *HTTP_302 =
89 "HTTP/1.0 302 Found\r\n"
90 "Cache-Control: no-cache\r\n"
91 "Connection: close\r\n"
92 "Location: "; /* not terminated since it will be concatenated with the URL */
93
94/* same as 302 except that the browser MUST retry with the GET method */
95const char *HTTP_303 =
96 "HTTP/1.0 303 See Other\r\n"
97 "Cache-Control: no-cache\r\n"
98 "Connection: close\r\n"
99 "Location: "; /* not terminated since it will be concatenated with the URL */
100
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
102const char *HTTP_401_fmt =
103 "HTTP/1.0 401 Unauthorized\r\n"
104 "Cache-Control: no-cache\r\n"
105 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200106 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
108 "\r\n"
109 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
110
Willy Tarreau0f772532006-12-23 20:51:41 +0100111
112const int http_err_codes[HTTP_ERR_SIZE] = {
113 [HTTP_ERR_400] = 400,
114 [HTTP_ERR_403] = 403,
115 [HTTP_ERR_408] = 408,
116 [HTTP_ERR_500] = 500,
117 [HTTP_ERR_502] = 502,
118 [HTTP_ERR_503] = 503,
119 [HTTP_ERR_504] = 504,
120};
121
Willy Tarreau80587432006-12-24 17:47:20 +0100122static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100123 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100124 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100125 "Cache-Control: no-cache\r\n"
126 "Connection: close\r\n"
127 "Content-Type: text/html\r\n"
128 "\r\n"
129 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
130
131 [HTTP_ERR_403] =
132 "HTTP/1.0 403 Forbidden\r\n"
133 "Cache-Control: no-cache\r\n"
134 "Connection: close\r\n"
135 "Content-Type: text/html\r\n"
136 "\r\n"
137 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
138
139 [HTTP_ERR_408] =
140 "HTTP/1.0 408 Request Time-out\r\n"
141 "Cache-Control: no-cache\r\n"
142 "Connection: close\r\n"
143 "Content-Type: text/html\r\n"
144 "\r\n"
145 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
146
147 [HTTP_ERR_500] =
148 "HTTP/1.0 500 Server Error\r\n"
149 "Cache-Control: no-cache\r\n"
150 "Connection: close\r\n"
151 "Content-Type: text/html\r\n"
152 "\r\n"
153 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
154
155 [HTTP_ERR_502] =
156 "HTTP/1.0 502 Bad Gateway\r\n"
157 "Cache-Control: no-cache\r\n"
158 "Connection: close\r\n"
159 "Content-Type: text/html\r\n"
160 "\r\n"
161 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
162
163 [HTTP_ERR_503] =
164 "HTTP/1.0 503 Service Unavailable\r\n"
165 "Cache-Control: no-cache\r\n"
166 "Connection: close\r\n"
167 "Content-Type: text/html\r\n"
168 "\r\n"
169 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
170
171 [HTTP_ERR_504] =
172 "HTTP/1.0 504 Gateway Time-out\r\n"
173 "Cache-Control: no-cache\r\n"
174 "Connection: close\r\n"
175 "Content-Type: text/html\r\n"
176 "\r\n"
177 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
178
179};
180
Willy Tarreau80587432006-12-24 17:47:20 +0100181/* We must put the messages here since GCC cannot initialize consts depending
182 * on strlen().
183 */
184struct chunk http_err_chunks[HTTP_ERR_SIZE];
185
Willy Tarreau42250582007-04-01 01:30:43 +0200186#define FD_SETS_ARE_BITFIELDS
187#ifdef FD_SETS_ARE_BITFIELDS
188/*
189 * This map is used with all the FD_* macros to check whether a particular bit
190 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
191 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
192 * byte should be encoded. Be careful to always pass bytes from 0 to 255
193 * exclusively to the macros.
194 */
195fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
196fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
197
198#else
199#error "Check if your OS uses bitfields for fd_sets"
200#endif
201
Willy Tarreau80587432006-12-24 17:47:20 +0100202void init_proto_http()
203{
Willy Tarreau42250582007-04-01 01:30:43 +0200204 int i;
205 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100206 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200207
Willy Tarreau80587432006-12-24 17:47:20 +0100208 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
209 if (!http_err_msgs[msg]) {
210 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
211 abort();
212 }
213
214 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
215 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
216 }
Willy Tarreau42250582007-04-01 01:30:43 +0200217
218 /* initialize the log header encoding map : '{|}"#' should be encoded with
219 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
220 * URL encoding only requires '"', '#' to be encoded as well as non-
221 * printable characters above.
222 */
223 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
224 memset(url_encode_map, 0, sizeof(url_encode_map));
225 for (i = 0; i < 32; i++) {
226 FD_SET(i, hdr_encode_map);
227 FD_SET(i, url_encode_map);
228 }
229 for (i = 127; i < 256; i++) {
230 FD_SET(i, hdr_encode_map);
231 FD_SET(i, url_encode_map);
232 }
233
234 tmp = "\"#{|}";
235 while (*tmp) {
236 FD_SET(*tmp, hdr_encode_map);
237 tmp++;
238 }
239
240 tmp = "\"#";
241 while (*tmp) {
242 FD_SET(*tmp, url_encode_map);
243 tmp++;
244 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200245
246 /* memory allocations */
247 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200248 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100249}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200250
Willy Tarreau53b6c742006-12-17 13:37:46 +0100251/*
252 * We have 26 list of methods (1 per first letter), each of which can have
253 * up to 3 entries (2 valid, 1 null).
254 */
255struct http_method_desc {
256 http_meth_t meth;
257 int len;
258 const char text[8];
259};
260
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100261const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100262 ['C' - 'A'] = {
263 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
264 },
265 ['D' - 'A'] = {
266 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
267 },
268 ['G' - 'A'] = {
269 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
270 },
271 ['H' - 'A'] = {
272 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
273 },
274 ['P' - 'A'] = {
275 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
276 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
277 },
278 ['T' - 'A'] = {
279 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
280 },
281 /* rest is empty like this :
282 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
283 */
284};
285
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100286/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200287 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100288 * RFC2616 for those chars.
289 */
290
291const char http_is_spht[256] = {
292 [' '] = 1, ['\t'] = 1,
293};
294
295const char http_is_crlf[256] = {
296 ['\r'] = 1, ['\n'] = 1,
297};
298
299const char http_is_lws[256] = {
300 [' '] = 1, ['\t'] = 1,
301 ['\r'] = 1, ['\n'] = 1,
302};
303
304const char http_is_sep[256] = {
305 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
306 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
307 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
308 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
309 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
310};
311
312const char http_is_ctl[256] = {
313 [0 ... 31] = 1,
314 [127] = 1,
315};
316
317/*
318 * A token is any ASCII char that is neither a separator nor a CTL char.
319 * Do not overwrite values in assignment since gcc-2.95 will not handle
320 * them correctly. Instead, define every non-CTL char's status.
321 */
322const char http_is_token[256] = {
323 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
324 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
325 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
326 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
327 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
328 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
329 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
330 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
331 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
332 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
333 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
334 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
335 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
336 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
337 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
338 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
339 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
340 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
341 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
342 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
343 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
344 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
345 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
346 ['|'] = 1, ['}'] = 0, ['~'] = 1,
347};
348
349
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100350/*
351 * An http ver_token is any ASCII which can be found in an HTTP version,
352 * which includes 'H', 'T', 'P', '/', '.' and any digit.
353 */
354const char http_is_ver_token[256] = {
355 ['.'] = 1, ['/'] = 1,
356 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
357 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
358 ['H'] = 1, ['P'] = 1, ['T'] = 1,
359};
360
361
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100362/*
363 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
364 * CRLF. Text length is measured first, so it cannot be NULL.
365 * The header is also automatically added to the index <hdr_idx>, and the end
366 * of headers is automatically adjusted. The number of bytes added is returned
367 * on success, otherwise <0 is returned indicating an error.
368 */
369int http_header_add_tail(struct buffer *b, struct http_msg *msg,
370 struct hdr_idx *hdr_idx, const char *text)
371{
372 int bytes, len;
373
374 len = strlen(text);
375 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
376 if (!bytes)
377 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100378 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100379 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
380}
381
382/*
383 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
384 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
385 * the buffer is only opened and the space reserved, but nothing is copied.
386 * The header is also automatically added to the index <hdr_idx>, and the end
387 * of headers is automatically adjusted. The number of bytes added is returned
388 * on success, otherwise <0 is returned indicating an error.
389 */
390int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
391 struct hdr_idx *hdr_idx, const char *text, int len)
392{
393 int bytes;
394
395 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
396 if (!bytes)
397 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100398 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100399 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
400}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200401
402/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100403 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
404 * If so, returns the position of the first non-space character relative to
405 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
406 * to return a pointer to the place after the first space. Returns 0 if the
407 * header name does not match. Checks are case-insensitive.
408 */
409int http_header_match2(const char *hdr, const char *end,
410 const char *name, int len)
411{
412 const char *val;
413
414 if (hdr + len >= end)
415 return 0;
416 if (hdr[len] != ':')
417 return 0;
418 if (strncasecmp(hdr, name, len) != 0)
419 return 0;
420 val = hdr + len + 1;
421 while (val < end && HTTP_IS_SPHT(*val))
422 val++;
423 if ((val >= end) && (len + 2 <= end - hdr))
424 return len + 2; /* we may replace starting from second space */
425 return val - hdr;
426}
427
Willy Tarreau33a7e692007-06-10 19:45:56 +0200428/* Find the end of the header value contained between <s> and <e>.
429 * See RFC2616, par 2.2 for more information. Note that it requires
430 * a valid header to return a valid result.
431 */
432const char *find_hdr_value_end(const char *s, const char *e)
433{
434 int quoted, qdpair;
435
436 quoted = qdpair = 0;
437 for (; s < e; s++) {
438 if (qdpair) qdpair = 0;
439 else if (quoted && *s == '\\') qdpair = 1;
440 else if (quoted && *s == '"') quoted = 0;
441 else if (*s == '"') quoted = 1;
442 else if (*s == ',') return s;
443 }
444 return s;
445}
446
447/* Find the first or next occurrence of header <name> in message buffer <sol>
448 * using headers index <idx>, and return it in the <ctx> structure. This
449 * structure holds everything necessary to use the header and find next
450 * occurrence. If its <idx> member is 0, the header is searched from the
451 * beginning. Otherwise, the next occurrence is returned. The function returns
452 * 1 when it finds a value, and 0 when there is no more.
453 */
454int http_find_header2(const char *name, int len,
455 const char *sol, struct hdr_idx *idx,
456 struct hdr_ctx *ctx)
457{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200458 const char *eol, *sov;
459 int cur_idx;
460
461 if (ctx->idx) {
462 /* We have previously returned a value, let's search
463 * another one on the same line.
464 */
465 cur_idx = ctx->idx;
466 sol = ctx->line;
467 sov = sol + ctx->val + ctx->vlen;
468 eol = sol + idx->v[cur_idx].len;
469
470 if (sov >= eol)
471 /* no more values in this header */
472 goto next_hdr;
473
474 /* values remaining for this header, skip the comma */
475 sov++;
476 while (sov < eol && http_is_lws[(unsigned char)*sov])
477 sov++;
478
479 goto return_hdr;
480 }
481
482 /* first request for this header */
483 sol += hdr_idx_first_pos(idx);
484 cur_idx = hdr_idx_first_idx(idx);
485
486 while (cur_idx) {
487 eol = sol + idx->v[cur_idx].len;
488
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200489 if (len == 0) {
490 /* No argument was passed, we want any header.
491 * To achieve this, we simply build a fake request. */
492 while (sol + len < eol && sol[len] != ':')
493 len++;
494 name = sol;
495 }
496
Willy Tarreau33a7e692007-06-10 19:45:56 +0200497 if ((len < eol - sol) &&
498 (sol[len] == ':') &&
499 (strncasecmp(sol, name, len) == 0)) {
500
501 sov = sol + len + 1;
502 while (sov < eol && http_is_lws[(unsigned char)*sov])
503 sov++;
504 return_hdr:
505 ctx->line = sol;
506 ctx->idx = cur_idx;
507 ctx->val = sov - sol;
508
509 eol = find_hdr_value_end(sov, eol);
510 ctx->vlen = eol - sov;
511 return 1;
512 }
513 next_hdr:
514 sol = eol + idx->v[cur_idx].cr + 1;
515 cur_idx = idx->v[cur_idx].next;
516 }
517 return 0;
518}
519
520int http_find_header(const char *name,
521 const char *sol, struct hdr_idx *idx,
522 struct hdr_ctx *ctx)
523{
524 return http_find_header2(name, strlen(name), sol, idx, ctx);
525}
526
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100527/* This function handles a server error at the stream interface level. The
528 * stream interface is assumed to be already in a closed state. An optional
529 * message is copied into the input buffer, and an HTTP status code stored.
530 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100531 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200532 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100533static void http_server_error(struct session *t, struct stream_interface *si,
534 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200535{
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100536 buffer_erase(si->ob);
537 buffer_erase(si->ib);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200538 buffer_auto_close(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100539 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100540 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100541 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200542 }
543 if (!(t->flags & SN_ERR_MASK))
544 t->flags |= err;
545 if (!(t->flags & SN_FINST_MASK))
546 t->flags |= finst;
547}
548
Willy Tarreau80587432006-12-24 17:47:20 +0100549/* This function returns the appropriate error location for the given session
550 * and message.
551 */
552
553struct chunk *error_message(struct session *s, int msgnum)
554{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200555 if (s->be->errmsg[msgnum].str)
556 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100557 else if (s->fe->errmsg[msgnum].str)
558 return &s->fe->errmsg[msgnum];
559 else
560 return &http_err_chunks[msgnum];
561}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200562
Willy Tarreau53b6c742006-12-17 13:37:46 +0100563/*
564 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
565 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
566 */
567static http_meth_t find_http_meth(const char *str, const int len)
568{
569 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100570 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100571
572 m = ((unsigned)*str - 'A');
573
574 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100575 for (h = http_methods[m]; h->len > 0; h++) {
576 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100577 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100578 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100579 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100580 };
581 return HTTP_METH_OTHER;
582 }
583 return HTTP_METH_NONE;
584
585}
586
Willy Tarreau21d2af32008-02-14 20:25:24 +0100587/* Parse the URI from the given transaction (which is assumed to be in request
588 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
589 * It is returned otherwise.
590 */
591static char *
592http_get_path(struct http_txn *txn)
593{
594 char *ptr, *end;
595
596 ptr = txn->req.sol + txn->req.sl.rq.u;
597 end = ptr + txn->req.sl.rq.u_l;
598
599 if (ptr >= end)
600 return NULL;
601
602 /* RFC2616, par. 5.1.2 :
603 * Request-URI = "*" | absuri | abspath | authority
604 */
605
606 if (*ptr == '*')
607 return NULL;
608
609 if (isalpha((unsigned char)*ptr)) {
610 /* this is a scheme as described by RFC3986, par. 3.1 */
611 ptr++;
612 while (ptr < end &&
613 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
614 ptr++;
615 /* skip '://' */
616 if (ptr == end || *ptr++ != ':')
617 return NULL;
618 if (ptr == end || *ptr++ != '/')
619 return NULL;
620 if (ptr == end || *ptr++ != '/')
621 return NULL;
622 }
623 /* skip [user[:passwd]@]host[:[port]] */
624
625 while (ptr < end && *ptr != '/')
626 ptr++;
627
628 if (ptr == end)
629 return NULL;
630
631 /* OK, we got the '/' ! */
632 return ptr;
633}
634
Willy Tarreauefb453c2008-10-26 20:49:47 +0100635/* Returns a 302 for a redirectable request. This may only be called just after
636 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
637 * left unchanged and will follow normal proxy processing.
638 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100639void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100640{
641 struct http_txn *txn;
642 struct chunk rdr;
643 char *path;
644 int len;
645
646 /* 1: create the response header */
647 rdr.len = strlen(HTTP_302);
648 rdr.str = trash;
649 memcpy(rdr.str, HTTP_302, rdr.len);
650
651 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200652 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100653 return;
654
655 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
656 rdr.len += s->srv->rdr_len;
657
658 /* 3: add the request URI */
659 txn = &s->txn;
660 path = http_get_path(txn);
661 if (!path)
662 return;
663
664 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200665 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100666 return;
667
668 memcpy(rdr.str + rdr.len, path, len);
669 rdr.len += len;
670 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
671 rdr.len += 4;
672
673 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100674 si->shutr(si);
675 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100676 si->err_type = SI_ET_NONE;
677 si->err_loc = NULL;
678 si->state = SI_ST_CLO;
679
680 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100681 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100682
683 /* FIXME: we should increase a counter of redirects per server and per backend. */
684 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100685 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100686}
687
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100688/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100689 * that the server side is closed. Note that err_type is actually a
690 * bitmask, where almost only aborts may be cumulated with other
691 * values. We consider that aborted operations are more important
692 * than timeouts or errors due to the fact that nobody else in the
693 * logs might explain incomplete retries. All others should avoid
694 * being cumulated. It should normally not be possible to have multiple
695 * aborts at once, but just in case, the first one in sequence is reported.
696 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100697void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100698{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100699 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100700
701 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100702 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
703 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100704 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100705 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
706 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100707 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100708 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
709 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100710 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100711 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
712 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100713 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100714 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
715 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100716 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100717 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
718 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100719 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100720 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
721 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100722}
723
Willy Tarreau42250582007-04-01 01:30:43 +0200724extern const char sess_term_cond[8];
725extern const char sess_fin_state[8];
726extern const char *monthname[12];
727const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
728const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
729 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
730 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200731struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200732struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100733
Emeric Brun3a058f32009-06-30 18:26:00 +0200734void http_sess_clflog(struct session *s)
735{
736 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
737 struct proxy *fe = s->fe;
738 struct proxy *be = s->be;
739 struct proxy *prx_log;
740 struct http_txn *txn = &s->txn;
741 int tolog, level, err;
742 char *uri, *h;
743 char *svid;
744 struct tm tm;
745 static char tmpline[MAX_SYSLOG_LEN];
746 int hdr;
747 size_t w;
748 int t_request;
749
750 prx_log = fe;
751 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
752 (s->conn_retries != be->conn_retries) ||
753 txn->status >= 500;
754
755 if (s->cli_addr.ss_family == AF_INET)
756 inet_ntop(AF_INET,
757 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
758 pn, sizeof(pn));
759 else
760 inet_ntop(AF_INET6,
761 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
762 pn, sizeof(pn));
763
764 get_gmtime(s->logs.accept_date.tv_sec, &tm);
765
766 /* FIXME: let's limit ourselves to frontend logging for now. */
767 tolog = fe->to_log;
768
769 h = tmpline;
770
771 w = snprintf(h, sizeof(tmpline),
772 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
773 pn,
774 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
775 tm.tm_hour, tm.tm_min, tm.tm_sec);
776 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
777 goto trunc;
778 h += w;
779
780 if (h >= tmpline + sizeof(tmpline) - 4)
781 goto trunc;
782
783 *(h++) = ' ';
784 *(h++) = '\"';
785 uri = txn->uri ? txn->uri : "<BADREQ>";
786 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
787 '#', url_encode_map, uri);
788 *(h++) = '\"';
789
790 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
791 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
792 goto trunc;
793 h += w;
794
795 if (h >= tmpline + sizeof(tmpline) - 9)
796 goto trunc;
797 memcpy(h, " \"-\" \"-\"", 8);
798 h += 8;
799
800 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
801 " %d %03d",
802 (s->cli_addr.ss_family == AF_INET) ?
803 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
804 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
805 (int)s->logs.accept_date.tv_usec/1000);
806 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
807 goto trunc;
808 h += w;
809
810 w = strlen(fe->id);
811 if (h >= tmpline + sizeof(tmpline) - 4 - w)
812 goto trunc;
813 *(h++) = ' ';
814 *(h++) = '\"';
815 memcpy(h, fe->id, w);
816 h += w;
817 *(h++) = '\"';
818
819 w = strlen(be->id);
820 if (h >= tmpline + sizeof(tmpline) - 4 - w)
821 goto trunc;
822 *(h++) = ' ';
823 *(h++) = '\"';
824 memcpy(h, be->id, w);
825 h += w;
826 *(h++) = '\"';
827
828 svid = (tolog & LW_SVID) ?
829 (s->data_source != DATA_SRC_STATS) ?
830 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
831
832 w = strlen(svid);
833 if (h >= tmpline + sizeof(tmpline) - 4 - w)
834 goto trunc;
835 *(h++) = ' ';
836 *(h++) = '\"';
837 memcpy(h, svid, w);
838 h += w;
839 *(h++) = '\"';
840
841 t_request = -1;
842 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
843 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
844 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
845 " %d %ld %ld %ld %ld",
846 t_request,
847 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
848 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
849 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
850 s->logs.t_close);
851 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
852 goto trunc;
853 h += w;
854
855 if (h >= tmpline + sizeof(tmpline) - 8)
856 goto trunc;
857 *(h++) = ' ';
858 *(h++) = '\"';
859 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
860 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
861 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
862 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
863 *(h++) = '\"';
864
865 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
866 " %d %d %d %d %d %ld %ld",
867 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
868 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
869 s->logs.srv_queue_size, s->logs.prx_queue_size);
870
871 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
872 goto trunc;
873 h += w;
874
875 if (txn->cli_cookie) {
876 w = strlen(txn->cli_cookie);
877 if (h >= tmpline + sizeof(tmpline) - 4 - w)
878 goto trunc;
879 *(h++) = ' ';
880 *(h++) = '\"';
881 memcpy(h, txn->cli_cookie, w);
882 h += w;
883 *(h++) = '\"';
884 } else {
885 if (h >= tmpline + sizeof(tmpline) - 5)
886 goto trunc;
887 memcpy(h, " \"-\"", 4);
888 h += 4;
889 }
890
891 if (txn->srv_cookie) {
892 w = strlen(txn->srv_cookie);
893 if (h >= tmpline + sizeof(tmpline) - 4 - w)
894 goto trunc;
895 *(h++) = ' ';
896 *(h++) = '\"';
897 memcpy(h, txn->srv_cookie, w);
898 h += w;
899 *(h++) = '\"';
900 } else {
901 if (h >= tmpline + sizeof(tmpline) - 5)
902 goto trunc;
903 memcpy(h, " \"-\"", 4);
904 h += 4;
905 }
906
907 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
908 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
909 if (h >= sizeof (tmpline) + tmpline - 4)
910 goto trunc;
911 *(h++) = ' ';
912 *(h++) = '\"';
913 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
914 '#', hdr_encode_map, txn->req.cap[hdr]);
915 *(h++) = '\"';
916 }
917 }
918
919 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
920 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
921 if (h >= sizeof (tmpline) + tmpline - 4)
922 goto trunc;
923 *(h++) = ' ';
924 *(h++) = '\"';
925 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
926 '#', hdr_encode_map, txn->rsp.cap[hdr]);
927 *(h++) = '\"';
928 }
929 }
930
931trunc:
932 *h = '\0';
933
934 level = LOG_INFO;
935 if (err && (fe->options2 & PR_O2_LOGERRORS))
936 level = LOG_ERR;
937
938 send_log(prx_log, level, "%s\n", tmpline);
939
940 s->logs.logwait = 0;
941}
942
Willy Tarreau42250582007-04-01 01:30:43 +0200943/*
944 * send a log for the session when we have enough info about it.
945 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100946 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100947void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200948{
949 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
950 struct proxy *fe = s->fe;
951 struct proxy *be = s->be;
952 struct proxy *prx_log;
953 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200954 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200955 char *uri, *h;
956 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200957 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200958 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200959 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200960 int hdr;
961
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200962 /* if we don't want to log normal traffic, return now */
963 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
964 (s->conn_retries != be->conn_retries) ||
965 txn->status >= 500;
966 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
967 return;
968
Willy Tarreau42250582007-04-01 01:30:43 +0200969 if (fe->logfac1 < 0 && fe->logfac2 < 0)
970 return;
971 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100972
Emeric Brun3a058f32009-06-30 18:26:00 +0200973 if (prx_log->options2 & PR_O2_CLFLOG)
974 return http_sess_clflog(s);
975
Willy Tarreau42250582007-04-01 01:30:43 +0200976 if (s->cli_addr.ss_family == AF_INET)
977 inet_ntop(AF_INET,
978 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
979 pn, sizeof(pn));
980 else
981 inet_ntop(AF_INET6,
982 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
983 pn, sizeof(pn));
984
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200985 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200986
987 /* FIXME: let's limit ourselves to frontend logging for now. */
988 tolog = fe->to_log;
989
990 h = tmpline;
991 if (fe->to_log & LW_REQHDR &&
992 txn->req.cap &&
993 (h < tmpline + sizeof(tmpline) - 10)) {
994 *(h++) = ' ';
995 *(h++) = '{';
996 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
997 if (hdr)
998 *(h++) = '|';
999 if (txn->req.cap[hdr] != NULL)
1000 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1001 '#', hdr_encode_map, txn->req.cap[hdr]);
1002 }
1003 *(h++) = '}';
1004 }
1005
1006 if (fe->to_log & LW_RSPHDR &&
1007 txn->rsp.cap &&
1008 (h < tmpline + sizeof(tmpline) - 7)) {
1009 *(h++) = ' ';
1010 *(h++) = '{';
1011 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1012 if (hdr)
1013 *(h++) = '|';
1014 if (txn->rsp.cap[hdr] != NULL)
1015 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1016 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1017 }
1018 *(h++) = '}';
1019 }
1020
1021 if (h < tmpline + sizeof(tmpline) - 4) {
1022 *(h++) = ' ';
1023 *(h++) = '"';
1024 uri = txn->uri ? txn->uri : "<BADREQ>";
1025 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1026 '#', url_encode_map, uri);
1027 *(h++) = '"';
1028 }
1029 *h = '\0';
1030
1031 svid = (tolog & LW_SVID) ?
1032 (s->data_source != DATA_SRC_STATS) ?
1033 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1034
Willy Tarreau70089872008-06-13 21:12:51 +02001035 t_request = -1;
1036 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1037 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1038
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001039 level = LOG_INFO;
1040 if (err && (fe->options2 & PR_O2_LOGERRORS))
1041 level = LOG_ERR;
1042
1043 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001044 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001045 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1046 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001047 pn,
1048 (s->cli_addr.ss_family == AF_INET) ?
1049 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1050 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001051 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001052 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001053 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001054 t_request,
1055 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001056 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1057 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1058 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1059 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001060 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001061 txn->cli_cookie ? txn->cli_cookie : "-",
1062 txn->srv_cookie ? txn->srv_cookie : "-",
1063 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1064 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1065 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1066 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1067 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001068 (s->flags & SN_REDISP)?"+":"",
1069 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001070 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1071
1072 s->logs.logwait = 0;
1073}
1074
Willy Tarreau117f59e2007-03-04 18:17:17 +01001075
1076/*
1077 * Capture headers from message starting at <som> according to header list
1078 * <cap_hdr>, and fill the <idx> structure appropriately.
1079 */
1080void capture_headers(char *som, struct hdr_idx *idx,
1081 char **cap, struct cap_hdr *cap_hdr)
1082{
1083 char *eol, *sol, *col, *sov;
1084 int cur_idx;
1085 struct cap_hdr *h;
1086 int len;
1087
1088 sol = som + hdr_idx_first_pos(idx);
1089 cur_idx = hdr_idx_first_idx(idx);
1090
1091 while (cur_idx) {
1092 eol = sol + idx->v[cur_idx].len;
1093
1094 col = sol;
1095 while (col < eol && *col != ':')
1096 col++;
1097
1098 sov = col + 1;
1099 while (sov < eol && http_is_lws[(unsigned char)*sov])
1100 sov++;
1101
1102 for (h = cap_hdr; h; h = h->next) {
1103 if ((h->namelen == col - sol) &&
1104 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1105 if (cap[h->index] == NULL)
1106 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001107 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001108
1109 if (cap[h->index] == NULL) {
1110 Alert("HTTP capture : out of memory.\n");
1111 continue;
1112 }
1113
1114 len = eol - sov;
1115 if (len > h->len)
1116 len = h->len;
1117
1118 memcpy(cap[h->index], sov, len);
1119 cap[h->index][len]=0;
1120 }
1121 }
1122 sol = eol + idx->v[cur_idx].cr + 1;
1123 cur_idx = idx->v[cur_idx].next;
1124 }
1125}
1126
1127
Willy Tarreau42250582007-04-01 01:30:43 +02001128/* either we find an LF at <ptr> or we jump to <bad>.
1129 */
1130#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1131
1132/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1133 * otherwise to <http_msg_ood> with <state> set to <st>.
1134 */
1135#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1136 ptr++; \
1137 if (likely(ptr < end)) \
1138 goto good; \
1139 else { \
1140 state = (st); \
1141 goto http_msg_ood; \
1142 } \
1143 } while (0)
1144
1145
Willy Tarreaubaaee002006-06-26 02:48:02 +02001146/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001147 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001148 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1149 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1150 * will give undefined results.
1151 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1152 * and that msg->sol points to the beginning of the response.
1153 * If a complete line is found (which implies that at least one CR or LF is
1154 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1155 * returned indicating an incomplete line (which does not mean that parts have
1156 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1157 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1158 * upon next call.
1159 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001160 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001161 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1162 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001163 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001164 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001165const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1166 unsigned int state, const char *ptr, const char *end,
1167 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001168{
Willy Tarreau8973c702007-01-21 23:58:29 +01001169 switch (state) {
1170 http_msg_rpver:
1171 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001172 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001173 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1174
1175 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001176 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001177 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1178 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001179 state = HTTP_MSG_ERROR;
1180 break;
1181
Willy Tarreau8973c702007-01-21 23:58:29 +01001182 http_msg_rpver_sp:
1183 case HTTP_MSG_RPVER_SP:
1184 if (likely(!HTTP_IS_LWS(*ptr))) {
1185 msg->sl.st.c = ptr - msg_buf;
1186 goto http_msg_rpcode;
1187 }
1188 if (likely(HTTP_IS_SPHT(*ptr)))
1189 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1190 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001191 state = HTTP_MSG_ERROR;
1192 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001193
1194 http_msg_rpcode:
1195 case HTTP_MSG_RPCODE:
1196 if (likely(!HTTP_IS_LWS(*ptr)))
1197 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1198
1199 if (likely(HTTP_IS_SPHT(*ptr))) {
1200 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1201 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1202 }
1203
1204 /* so it's a CR/LF, so there is no reason phrase */
1205 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1206 http_msg_rsp_reason:
1207 /* FIXME: should we support HTTP responses without any reason phrase ? */
1208 msg->sl.st.r = ptr - msg_buf;
1209 msg->sl.st.r_l = 0;
1210 goto http_msg_rpline_eol;
1211
1212 http_msg_rpcode_sp:
1213 case HTTP_MSG_RPCODE_SP:
1214 if (likely(!HTTP_IS_LWS(*ptr))) {
1215 msg->sl.st.r = ptr - msg_buf;
1216 goto http_msg_rpreason;
1217 }
1218 if (likely(HTTP_IS_SPHT(*ptr)))
1219 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1220 /* so it's a CR/LF, so there is no reason phrase */
1221 goto http_msg_rsp_reason;
1222
1223 http_msg_rpreason:
1224 case HTTP_MSG_RPREASON:
1225 if (likely(!HTTP_IS_CRLF(*ptr)))
1226 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1227 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1228 http_msg_rpline_eol:
1229 /* We have seen the end of line. Note that we do not
1230 * necessarily have the \n yet, but at least we know that we
1231 * have EITHER \r OR \n, otherwise the response would not be
1232 * complete. We can then record the response length and return
1233 * to the caller which will be able to register it.
1234 */
1235 msg->sl.st.l = ptr - msg->sol;
1236 return ptr;
1237
1238#ifdef DEBUG_FULL
1239 default:
1240 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1241 exit(1);
1242#endif
1243 }
1244
1245 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001246 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001247 if (ret_state)
1248 *ret_state = state;
1249 if (ret_ptr)
1250 *ret_ptr = (char *)ptr;
1251 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001252}
1253
1254
1255/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001256 * This function parses a request line between <ptr> and <end>, starting with
1257 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1258 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1259 * will give undefined results.
1260 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1261 * and that msg->sol points to the beginning of the request.
1262 * If a complete line is found (which implies that at least one CR or LF is
1263 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1264 * returned indicating an incomplete line (which does not mean that parts have
1265 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1266 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1267 * upon next call.
1268 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001269 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001270 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1271 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001272 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001273 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001274const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1275 unsigned int state, const char *ptr, const char *end,
1276 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001277{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 switch (state) {
1279 http_msg_rqmeth:
1280 case HTTP_MSG_RQMETH:
1281 if (likely(HTTP_IS_TOKEN(*ptr)))
1282 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001285 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001286 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1287 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 if (likely(HTTP_IS_CRLF(*ptr))) {
1290 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001291 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001292 http_msg_req09_uri:
1293 msg->sl.rq.u = ptr - msg_buf;
1294 http_msg_req09_uri_e:
1295 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1296 http_msg_req09_ver:
1297 msg->sl.rq.v = ptr - msg_buf;
1298 msg->sl.rq.v_l = 0;
1299 goto http_msg_rqline_eol;
1300 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001301 state = HTTP_MSG_ERROR;
1302 break;
1303
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001304 http_msg_rqmeth_sp:
1305 case HTTP_MSG_RQMETH_SP:
1306 if (likely(!HTTP_IS_LWS(*ptr))) {
1307 msg->sl.rq.u = ptr - msg_buf;
1308 goto http_msg_rquri;
1309 }
1310 if (likely(HTTP_IS_SPHT(*ptr)))
1311 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1312 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1313 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001314
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001315 http_msg_rquri:
1316 case HTTP_MSG_RQURI:
1317 if (likely(!HTTP_IS_LWS(*ptr)))
1318 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 if (likely(HTTP_IS_SPHT(*ptr))) {
1321 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1322 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1323 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001324
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1326 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 http_msg_rquri_sp:
1329 case HTTP_MSG_RQURI_SP:
1330 if (likely(!HTTP_IS_LWS(*ptr))) {
1331 msg->sl.rq.v = ptr - msg_buf;
1332 goto http_msg_rqver;
1333 }
1334 if (likely(HTTP_IS_SPHT(*ptr)))
1335 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1336 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1337 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001338
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 http_msg_rqver:
1340 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001341 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001343
1344 if (likely(HTTP_IS_CRLF(*ptr))) {
1345 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1346 http_msg_rqline_eol:
1347 /* We have seen the end of line. Note that we do not
1348 * necessarily have the \n yet, but at least we know that we
1349 * have EITHER \r OR \n, otherwise the request would not be
1350 * complete. We can then record the request length and return
1351 * to the caller which will be able to register it.
1352 */
1353 msg->sl.rq.l = ptr - msg->sol;
1354 return ptr;
1355 }
1356
1357 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001358 state = HTTP_MSG_ERROR;
1359 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001360
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001361#ifdef DEBUG_FULL
1362 default:
1363 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1364 exit(1);
1365#endif
1366 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001367
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001368 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001369 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 if (ret_state)
1371 *ret_state = state;
1372 if (ret_ptr)
1373 *ret_ptr = (char *)ptr;
1374 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001375}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001376
1377
Willy Tarreau8973c702007-01-21 23:58:29 +01001378/*
1379 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001380 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001381 * when data are missing and recalled at the exact same location with no
1382 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001383 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1384 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001385 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1387{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001388 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001390
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001391 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 ptr = buf->lr;
1393 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 if (unlikely(ptr >= end))
1396 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001399 /*
1400 * First, states that are specific to the response only.
1401 * We check them first so that request and headers are
1402 * closer to each other (accessed more often).
1403 */
1404 http_msg_rpbefore:
1405 case HTTP_MSG_RPBEFORE:
1406 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001407 if (likely(ptr != buf->data + msg->som)) {
1408 /* Remove empty leading lines, as recommended by RFC2616. */
1409 buffer_ignore(buf, ptr - buf->data - msg->som);
1410 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001411 }
Willy Tarreau816b9792009-09-15 21:25:21 +02001412 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001413 hdr_idx_init(idx);
1414 state = HTTP_MSG_RPVER;
1415 goto http_msg_rpver;
1416 }
1417
1418 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1419 goto http_msg_invalid;
1420
1421 if (unlikely(*ptr == '\n'))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1423 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1424 /* stop here */
1425
1426 http_msg_rpbefore_cr:
1427 case HTTP_MSG_RPBEFORE_CR:
1428 EXPECT_LF_HERE(ptr, http_msg_invalid);
1429 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1430 /* stop here */
1431
1432 http_msg_rpver:
1433 case HTTP_MSG_RPVER:
1434 case HTTP_MSG_RPVER_SP:
1435 case HTTP_MSG_RPCODE:
1436 case HTTP_MSG_RPCODE_SP:
1437 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001438 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001439 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001440 if (unlikely(!ptr))
1441 return;
1442
1443 /* we have a full response and we know that we have either a CR
1444 * or an LF at <ptr>.
1445 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001446 //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 +01001447 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1448
1449 msg->sol = ptr;
1450 if (likely(*ptr == '\r'))
1451 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1452 goto http_msg_rpline_end;
1453
1454 http_msg_rpline_end:
1455 case HTTP_MSG_RPLINE_END:
1456 /* msg->sol must point to the first of CR or LF. */
1457 EXPECT_LF_HERE(ptr, http_msg_invalid);
1458 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1459 /* stop here */
1460
1461 /*
1462 * Second, states that are specific to the request only
1463 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 http_msg_rqbefore:
1465 case HTTP_MSG_RQBEFORE:
1466 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001467 if (likely(ptr != buf->data + msg->som)) {
1468 /* Remove empty leading lines, as recommended by RFC2616. */
1469 buffer_ignore(buf, ptr - buf->data - msg->som);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001470 msg->som = ptr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 }
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001472 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001473 /* we will need this when keep-alive will be supported
1474 hdr_idx_init(idx);
1475 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001476 state = HTTP_MSG_RQMETH;
1477 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001479
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1481 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 if (unlikely(*ptr == '\n'))
1484 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1485 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001486 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 http_msg_rqbefore_cr:
1489 case HTTP_MSG_RQBEFORE_CR:
1490 EXPECT_LF_HERE(ptr, http_msg_invalid);
1491 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001492 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001493
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 http_msg_rqmeth:
1495 case HTTP_MSG_RQMETH:
1496 case HTTP_MSG_RQMETH_SP:
1497 case HTTP_MSG_RQURI:
1498 case HTTP_MSG_RQURI_SP:
1499 case HTTP_MSG_RQVER:
1500 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001501 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 if (unlikely(!ptr))
1503 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001504
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 /* we have a full request and we know that we have either a CR
1506 * or an LF at <ptr>.
1507 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001508 //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 +01001509 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001510
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 msg->sol = ptr;
1512 if (likely(*ptr == '\r'))
1513 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001515
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 http_msg_rqline_end:
1517 case HTTP_MSG_RQLINE_END:
1518 /* check for HTTP/0.9 request : no version information available.
1519 * msg->sol must point to the first of CR or LF.
1520 */
1521 if (unlikely(msg->sl.rq.v_l == 0))
1522 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 EXPECT_LF_HERE(ptr, http_msg_invalid);
1525 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001526 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001527
Willy Tarreau8973c702007-01-21 23:58:29 +01001528 /*
1529 * Common states below
1530 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 http_msg_hdr_first:
1532 case HTTP_MSG_HDR_FIRST:
1533 msg->sol = ptr;
1534 if (likely(!HTTP_IS_CRLF(*ptr))) {
1535 goto http_msg_hdr_name;
1536 }
1537
1538 if (likely(*ptr == '\r'))
1539 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1540 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001541
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 http_msg_hdr_name:
1543 case HTTP_MSG_HDR_NAME:
1544 /* assumes msg->sol points to the first char */
1545 if (likely(HTTP_IS_TOKEN(*ptr)))
1546 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001547
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 if (likely(*ptr == ':')) {
1549 msg->col = ptr - buf->data;
1550 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1551 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001552
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001553 if (likely(msg->err_pos < -1) || *ptr == '\n')
1554 goto http_msg_invalid;
1555
1556 if (msg->err_pos == -1) /* capture error pointer */
1557 msg->err_pos = ptr - buf->data; /* >= 0 now */
1558
1559 /* and we still accept this non-token character */
1560 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 http_msg_hdr_l1_sp:
1563 case HTTP_MSG_HDR_L1_SP:
1564 /* assumes msg->sol points to the first char and msg->col to the colon */
1565 if (likely(HTTP_IS_SPHT(*ptr)))
1566 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 /* header value can be basically anything except CR/LF */
1569 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 if (likely(!HTTP_IS_CRLF(*ptr))) {
1572 goto http_msg_hdr_val;
1573 }
1574
1575 if (likely(*ptr == '\r'))
1576 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1577 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001578
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 http_msg_hdr_l1_lf:
1580 case HTTP_MSG_HDR_L1_LF:
1581 EXPECT_LF_HERE(ptr, http_msg_invalid);
1582 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001583
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 http_msg_hdr_l1_lws:
1585 case HTTP_MSG_HDR_L1_LWS:
1586 if (likely(HTTP_IS_SPHT(*ptr))) {
1587 /* replace HT,CR,LF with spaces */
1588 for (; buf->data+msg->sov < ptr; msg->sov++)
1589 buf->data[msg->sov] = ' ';
1590 goto http_msg_hdr_l1_sp;
1591 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001592 /* we had a header consisting only in spaces ! */
1593 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 goto http_msg_complete_header;
1595
1596 http_msg_hdr_val:
1597 case HTTP_MSG_HDR_VAL:
1598 /* assumes msg->sol points to the first char, msg->col to the
1599 * colon, and msg->sov points to the first character of the
1600 * value.
1601 */
1602 if (likely(!HTTP_IS_CRLF(*ptr)))
1603 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001604
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 msg->eol = ptr;
1606 /* Note: we could also copy eol into ->eoh so that we have the
1607 * real header end in case it ends with lots of LWS, but is this
1608 * really needed ?
1609 */
1610 if (likely(*ptr == '\r'))
1611 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1612 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 http_msg_hdr_l2_lf:
1615 case HTTP_MSG_HDR_L2_LF:
1616 EXPECT_LF_HERE(ptr, http_msg_invalid);
1617 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619 http_msg_hdr_l2_lws:
1620 case HTTP_MSG_HDR_L2_LWS:
1621 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1622 /* LWS: replace HT,CR,LF with spaces */
1623 for (; msg->eol < ptr; msg->eol++)
1624 *msg->eol = ' ';
1625 goto http_msg_hdr_val;
1626 }
1627 http_msg_complete_header:
1628 /*
1629 * It was a new header, so the last one is finished.
1630 * Assumes msg->sol points to the first char, msg->col to the
1631 * colon, msg->sov points to the first character of the value
1632 * and msg->eol to the first CR or LF so we know how the line
1633 * ends. We insert last header into the index.
1634 */
1635 /*
1636 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1637 write(2, msg->sol, msg->eol-msg->sol);
1638 fprintf(stderr,"\n");
1639 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001640
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001641 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1642 idx, idx->tail) < 0))
1643 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 msg->sol = ptr;
1646 if (likely(!HTTP_IS_CRLF(*ptr))) {
1647 goto http_msg_hdr_name;
1648 }
1649
1650 if (likely(*ptr == '\r'))
1651 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1652 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 http_msg_last_lf:
1655 case HTTP_MSG_LAST_LF:
1656 /* Assumes msg->sol points to the first of either CR or LF */
1657 EXPECT_LF_HERE(ptr, http_msg_invalid);
1658 ptr++;
1659 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001660 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001661 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001662 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 return;
1664#ifdef DEBUG_FULL
1665 default:
1666 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1667 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001668#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 }
1670 http_msg_ood:
1671 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001672 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001673 buf->lr = ptr;
1674 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001675
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 http_msg_invalid:
1677 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001678 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001679 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001680 return;
1681}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001682
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001683/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1684 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1685 * nothing is done and 1 is returned.
1686 */
1687static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1688{
1689 int delta;
1690 char *cur_end;
1691
1692 if (msg->sl.rq.v_l != 0)
1693 return 1;
1694
1695 msg->sol = req->data + msg->som;
1696 cur_end = msg->sol + msg->sl.rq.l;
1697 delta = 0;
1698
1699 if (msg->sl.rq.u_l == 0) {
1700 /* if no URI was set, add "/" */
1701 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1702 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001703 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001704 }
1705 /* add HTTP version */
1706 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001707 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001708 cur_end += delta;
1709 cur_end = (char *)http_parse_reqline(msg, req->data,
1710 HTTP_MSG_RQMETH,
1711 msg->sol, cur_end + 1,
1712 NULL, NULL);
1713 if (unlikely(!cur_end))
1714 return 0;
1715
1716 /* we have a full HTTP/1.0 request now and we know that
1717 * we have either a CR or an LF at <ptr>.
1718 */
1719 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1720 return 1;
1721}
1722
Willy Tarreau5b154472009-12-21 20:11:07 +01001723/* Parse the Connection: headaer of an HTTP request, and set the transaction
1724 * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag
1725 * is also set so that we don't parse a second time. If some dangerous values
1726 * are encountered, we leave the status to indicate that the request might be
1727 * interpreted as keep-alive, but we also set the connection flags to indicate
1728 * that we WANT it to be a close, so that the header will be fixed. This
1729 * function should only be called when we know we're interested in checking
1730 * the request (not a CONNECT, and FE or BE mangles the header).
1731 */
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001732void http_req_parse_connection_header(struct http_txn *txn)
Willy Tarreau5b154472009-12-21 20:11:07 +01001733{
1734 struct http_msg *msg = &txn->req;
1735 struct hdr_ctx ctx;
1736 int conn_cl, conn_ka;
1737
1738 if (txn->flags & TX_CON_HDR_PARS)
1739 return;
1740
1741 conn_cl = 0;
1742 conn_ka = 0;
1743 ctx.idx = 0;
1744
1745 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
1746 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
1747 conn_cl = 1;
1748 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
1749 conn_ka = 1;
1750 }
1751
1752 /* Determine if the client wishes keep-alive or close.
1753 * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections
1754 * are persistent unless "Connection: close" is explicitly specified.
1755 * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections.
1756 * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless
1757 * they explicitly specify "Connection: Keep-Alive", regardless of any
1758 * optional "Keep-Alive" header.
1759 * Note that if we find a request with both "Connection: close" and
1760 * "Connection: Keep-Alive", we indicate we want a close but don't have
1761 * it, so that it can be enforced later.
1762 */
1763
1764 if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */
1765 if (conn_cl) {
1766 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1767 if (!conn_ka)
1768 txn->flags |= TX_REQ_CONN_CLO;
1769 }
1770 } else { /* HTTP/1.0 */
1771 if (!conn_ka)
1772 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO | TX_REQ_CONN_CLO;
1773 else if (conn_cl)
1774 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1775 }
1776 txn->flags |= TX_CON_HDR_PARS;
1777}
1778
Willy Tarreaud98cf932009-12-27 22:54:55 +01001779/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1780 * first byte of body, and increments msg->sov by the number of bytes parsed,
1781 * so that we know we can forward between ->som and ->sov. Note that due to
1782 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1783 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001784 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001785 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001786 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001787int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001788{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001789 char *ptr = buf->lr;
1790 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001791 unsigned int chunk = 0;
1792
1793 /* The chunk size is in the following form, though we are only
1794 * interested in the size and CRLF :
1795 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1796 */
1797 while (1) {
1798 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001799 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001800 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001801 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001802 if (c < 0) /* not a hex digit anymore */
1803 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001804 if (++ptr >= end)
1805 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001806 if (chunk & 0xF000000) /* overflow will occur */
1807 return -1;
1808 chunk = (chunk << 4) + c;
1809 }
1810
Willy Tarreaud98cf932009-12-27 22:54:55 +01001811 /* empty size not allowed */
1812 if (ptr == buf->lr)
1813 return -1;
1814
1815 while (http_is_spht[(unsigned char)*ptr]) {
1816 if (++ptr >= end)
1817 ptr = buf->data;
1818 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001819 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001820 }
1821
Willy Tarreaud98cf932009-12-27 22:54:55 +01001822 /* Up to there, we know that at least one byte is present at *ptr. Check
1823 * for the end of chunk size.
1824 */
1825 while (1) {
1826 if (likely(HTTP_IS_CRLF(*ptr))) {
1827 /* we now have a CR or an LF at ptr */
1828 if (likely(*ptr == '\r')) {
1829 if (++ptr >= end)
1830 ptr = buf->data;
1831 if (ptr == buf->r)
1832 return 0;
1833 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001834
Willy Tarreaud98cf932009-12-27 22:54:55 +01001835 if (*ptr != '\n')
1836 return -1;
1837 if (++ptr >= end)
1838 ptr = buf->data;
1839 /* done */
1840 break;
1841 }
1842 else if (*ptr == ';') {
1843 /* chunk extension, ends at next CRLF */
1844 if (++ptr >= end)
1845 ptr = buf->data;
1846 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001847 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001848
1849 while (!HTTP_IS_CRLF(*ptr)) {
1850 if (++ptr >= end)
1851 ptr = buf->data;
1852 if (ptr == buf->r)
1853 return 0;
1854 }
1855 /* we have a CRLF now, loop above */
1856 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001857 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858 else
Willy Tarreau115acb92009-12-26 13:56:06 +01001859 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001860 }
1861
Willy Tarreaud98cf932009-12-27 22:54:55 +01001862 /* OK we found our CRLF and now <ptr> points to the next byte,
1863 * which may or may not be present. We save that into ->lr and
1864 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001865 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001866 msg->sov += ptr - buf->lr;
1867 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01001868 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001869 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001870 return 1;
1871}
1872
Willy Tarreaud98cf932009-12-27 22:54:55 +01001873/* This function skips trailers in the buffer <buf> associated with HTTP
1874 * message <msg>. The first visited position is buf->lr. If the end of
1875 * the trailers is found, it is automatically scheduled to be forwarded,
1876 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1877 * If not enough data are available, the function does not change anything
1878 * except maybe buf->lr if it could parse some lines, and returns zero. If
1879 * a parse error is encountered, the function returns < 0 and does not
1880 * change anything except maybe buf->lr. Note that the message must already
1881 * be in HTTP_MSG_TRAILERS state before calling this function, which implies
1882 * that all non-trailers data have already been scheduled for forwarding. It
1883 * is also important to note that this function is designed to be able to
1884 * parse wrapped headers at end of buffer.
1885 */
1886int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1887{
1888 /* we have buf->lr which points to next line. Look for CRLF. */
1889 while (1) {
1890 char *p1 = NULL, *p2 = NULL;
1891 char *ptr = buf->lr;
1892
1893 /* scan current line and stop at LF or CRLF */
1894 while (1) {
1895 if (ptr == buf->r)
1896 return 0;
1897
1898 if (*ptr == '\n') {
1899 if (!p1)
1900 p1 = ptr;
1901 p2 = ptr;
1902 break;
1903 }
1904
1905 if (*ptr == '\r') {
1906 if (p1)
1907 return -1;
1908 p1 = ptr;
1909 }
1910
1911 ptr++;
1912 if (ptr >= buf->data + buf->size)
1913 ptr = buf->data;
1914 }
1915
1916 /* after LF; point to beginning of next line */
1917 p2++;
1918 if (p2 >= buf->data + buf->size)
1919 p2 = buf->data;
1920
1921 if (p1 == buf->lr) {
1922 /* LF/CRLF at beginning of line => end of trailers at p2 */
1923 int bytes;
1924 buf->lr = p2;
1925
1926 bytes = buf->lr - buf->data;
1927 /* forward last remaining trailers */
1928 buffer_forward(buf, bytes);
1929 msg->som = msg->sov = bytes;
1930 msg->msg_state = HTTP_MSG_DONE;
1931 return 1;
1932 }
1933 /* OK, next line then */
1934 buf->lr = p2;
1935 }
1936}
1937
1938/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1939 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
1940 * ->som, buf->lr in order to include this part into the next forwarding phase.
1941 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1942 * not enough data are available, the function does not change anything and
1943 * returns zero. If a parse error is encountered, the function returns < 0 and
1944 * does not change anything. Note: this function is designed to parse wrapped
1945 * CRLF at the end of the buffer.
1946 */
1947int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1948{
1949 char *ptr;
1950 int bytes;
1951
1952 /* NB: we'll check data availabilty at the end. It's not a
1953 * problem because whatever we match first will be checked
1954 * against the correct length.
1955 */
1956 bytes = 1;
1957 ptr = buf->lr;
1958 if (*ptr == '\r') {
1959 bytes++;
1960 ptr++;
1961 if (ptr >= buf->data + buf->size)
1962 ptr = buf->data;
1963 }
1964
1965 if (buf->l < bytes)
1966 return 0;
1967
1968 if (*ptr != '\n')
1969 return -1;
1970
1971 ptr++;
1972 if (ptr >= buf->data + buf->size)
1973 ptr = buf->data;
1974 buf->lr = ptr;
1975 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
1976 msg->sov = ptr - buf->data;
1977 msg->som = msg->sov - bytes;
1978 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1979 return 1;
1980}
Willy Tarreau5b154472009-12-21 20:11:07 +01001981
Willy Tarreau83e3af02009-12-28 17:39:57 +01001982void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
1983{
1984 char *end = buf->data + buf->size;
1985 int off = buf->data + buf->size - buf->w;
1986
1987 /* two possible cases :
1988 * - the buffer is in one contiguous block, we move it in-place
1989 * - the buffer is in two blocks, we move it via the trash
1990 */
1991 if (buf->l) {
1992 int block1 = buf->l;
1993 int block2 = 0;
1994 if (buf->r <= buf->w) {
1995 /* non-contiguous block */
1996 block1 = buf->data + buf->size - buf->w;
1997 block2 = buf->r - buf->data;
1998 }
1999 if (block2)
2000 memcpy(trash, buf->data, block2);
2001 memmove(buf->data, buf->w, block1);
2002 if (block2)
2003 memcpy(buf->data + block1, trash, block2);
2004 }
2005
2006 /* adjust all known pointers */
2007 buf->w = buf->data;
2008 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2009 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2010 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2011 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2012
2013 /* adjust relative pointers */
2014 msg->som = 0;
2015 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2016 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2017 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2018
2019 msg->sl.rq.u += off; if (msg->sl.rq.u >= buf->size) msg->sl.rq.u -= buf->size;
2020 msg->sl.rq.v += off; if (msg->sl.rq.v >= buf->size) msg->sl.rq.v -= buf->size;
2021
2022 if (msg->err_pos >= 0) {
2023 msg->err_pos += off;
2024 if (msg->err_pos >= buf->size)
2025 msg->err_pos -= buf->size;
2026 }
2027
2028 buf->flags &= ~BF_FULL;
2029 if (buf->l >= buffer_max_len(buf))
2030 buf->flags |= BF_FULL;
2031}
2032
Willy Tarreaud787e662009-07-07 10:14:51 +02002033/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2034 * processing can continue on next analysers, or zero if it either needs more
2035 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2036 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2037 * when it has nothing left to do, and may remove any analyser when it wants to
2038 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002039 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002040int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002041{
Willy Tarreau59234e92008-11-30 23:51:27 +01002042 /*
2043 * We will parse the partial (or complete) lines.
2044 * We will check the request syntax, and also join multi-line
2045 * headers. An index of all the lines will be elaborated while
2046 * parsing.
2047 *
2048 * For the parsing, we use a 28 states FSM.
2049 *
2050 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002051 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002052 * req->data + msg->eoh = end of processed headers / start of current one
2053 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002054 * req->lr = first non-visited byte
2055 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002056 *
2057 * At end of parsing, we may perform a capture of the error (if any), and
2058 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2059 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2060 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002061 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002062
Willy Tarreau59234e92008-11-30 23:51:27 +01002063 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002064 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002065 struct http_txn *txn = &s->txn;
2066 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002067 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002068
Willy Tarreau6bf17362009-02-24 10:48:35 +01002069 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2070 now_ms, __FUNCTION__,
2071 s,
2072 req,
2073 req->rex, req->wex,
2074 req->flags,
2075 req->l,
2076 req->analysers);
2077
Willy Tarreau52a0c602009-08-16 22:45:38 +02002078 /* we're speaking HTTP here, so let's speak HTTP to the client */
2079 s->srv_error = http_return_srv_error;
2080
Willy Tarreau83e3af02009-12-28 17:39:57 +01002081 /* There's a protected area at the end of the buffer for rewriting
2082 * purposes. We don't want to start to parse the request if the
2083 * protected area is affected, because we may have to move processed
2084 * data later, which is much more complicated.
2085 */
2086 if (req->l &&
2087 (req->r <= req->lr || req->r > req->data + req->size - global.tune.maxrewrite)) {
2088 if (req->send_max) {
2089 /* some data has still not left the buffer, wake us once that's done */
2090 buffer_dont_connect(req);
2091 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2092 return 0;
2093 }
2094
2095 http_buffer_heavy_realign(req, msg);
2096 }
2097
Willy Tarreau59234e92008-11-30 23:51:27 +01002098 if (likely(req->lr < req->r))
2099 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002100
Willy Tarreau59234e92008-11-30 23:51:27 +01002101 /* 1: we might have to print this header in debug mode */
2102 if (unlikely((global.mode & MODE_DEBUG) &&
2103 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002104 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002105 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002106
Willy Tarreau59234e92008-11-30 23:51:27 +01002107 sol = req->data + msg->som;
2108 eol = sol + msg->sl.rq.l;
2109 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002110
Willy Tarreau59234e92008-11-30 23:51:27 +01002111 sol += hdr_idx_first_pos(&txn->hdr_idx);
2112 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002113
Willy Tarreau59234e92008-11-30 23:51:27 +01002114 while (cur_idx) {
2115 eol = sol + txn->hdr_idx.v[cur_idx].len;
2116 debug_hdr("clihdr", s, sol, eol);
2117 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2118 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002119 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002120 }
2121
Willy Tarreau58f10d72006-12-04 02:26:12 +01002122
Willy Tarreau59234e92008-11-30 23:51:27 +01002123 /*
2124 * Now we quickly check if we have found a full valid request.
2125 * If not so, we check the FD and buffer states before leaving.
2126 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002127 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01002128 * requests are checked first.
2129 *
2130 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002131
Willy Tarreau655dce92009-11-08 13:10:58 +01002132 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002133 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002134 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002135 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002136 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2137 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002138
Willy Tarreau59234e92008-11-30 23:51:27 +01002139 /* 1: Since we are in header mode, if there's no space
2140 * left for headers, we won't be able to free more
2141 * later, so the session will never terminate. We
2142 * must terminate it now.
2143 */
2144 if (unlikely(req->flags & BF_FULL)) {
2145 /* FIXME: check if URI is set and return Status
2146 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002147 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002148 goto return_bad_req;
2149 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002150
Willy Tarreau59234e92008-11-30 23:51:27 +01002151 /* 2: have we encountered a read error ? */
2152 else if (req->flags & BF_READ_ERROR) {
2153 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002154 if (msg->err_pos >= 0)
2155 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002156 msg->msg_state = HTTP_MSG_ERROR;
2157 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002158
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002159 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002160 if (s->listener->counters)
2161 s->listener->counters->failed_req++;
2162
Willy Tarreau59234e92008-11-30 23:51:27 +01002163 if (!(s->flags & SN_ERR_MASK))
2164 s->flags |= SN_ERR_CLICL;
2165 if (!(s->flags & SN_FINST_MASK))
2166 s->flags |= SN_FINST_R;
2167 return 0;
2168 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002169
Willy Tarreau59234e92008-11-30 23:51:27 +01002170 /* 3: has the read timeout expired ? */
2171 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
2172 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002173 if (msg->err_pos >= 0)
2174 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002175 txn->status = 408;
2176 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2177 msg->msg_state = HTTP_MSG_ERROR;
2178 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002179
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002180 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002181 if (s->listener->counters)
2182 s->listener->counters->failed_req++;
2183
Willy Tarreau59234e92008-11-30 23:51:27 +01002184 if (!(s->flags & SN_ERR_MASK))
2185 s->flags |= SN_ERR_CLITO;
2186 if (!(s->flags & SN_FINST_MASK))
2187 s->flags |= SN_FINST_R;
2188 return 0;
2189 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002190
Willy Tarreau59234e92008-11-30 23:51:27 +01002191 /* 4: have we encountered a close ? */
2192 else if (req->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002193 if (msg->err_pos >= 0)
2194 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002195 txn->status = 400;
2196 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2197 msg->msg_state = HTTP_MSG_ERROR;
2198 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002199
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002200 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002201 if (s->listener->counters)
2202 s->listener->counters->failed_req++;
2203
Willy Tarreau59234e92008-11-30 23:51:27 +01002204 if (!(s->flags & SN_ERR_MASK))
2205 s->flags |= SN_ERR_CLICL;
2206 if (!(s->flags & SN_FINST_MASK))
2207 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002208 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002209 }
2210
Willy Tarreau520d95e2009-09-19 21:04:57 +02002211 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002212 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2213
Willy Tarreau59234e92008-11-30 23:51:27 +01002214 /* just set the request timeout once at the beginning of the request */
2215 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002216 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002217
Willy Tarreau59234e92008-11-30 23:51:27 +01002218 /* we're not ready yet */
2219 return 0;
2220 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002221
Willy Tarreaud787e662009-07-07 10:14:51 +02002222 /* OK now we have a complete HTTP request with indexed headers. Let's
2223 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002224 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2225 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2226 * points to the CRLF of the request line. req->lr points to the first
2227 * byte after the last LF. msg->col and msg->sov point to the first
2228 * byte of data. msg->eol cannot be trusted because it may have been
2229 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002230 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002231
Willy Tarreaud787e662009-07-07 10:14:51 +02002232 /* Maybe we found in invalid header name while we were configured not
2233 * to block on that, so we have to capture it now.
2234 */
2235 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002236 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2237
Willy Tarreau59234e92008-11-30 23:51:27 +01002238 /* ensure we keep this pointer to the beginning of the message */
2239 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002240
Willy Tarreau59234e92008-11-30 23:51:27 +01002241 /*
2242 * 1: identify the method
2243 */
2244 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
2245
2246 /* we can make use of server redirect on GET and HEAD */
2247 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2248 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002249
Willy Tarreau59234e92008-11-30 23:51:27 +01002250 /*
2251 * 2: check if the URI matches the monitor_uri.
2252 * We have to do this for every request which gets in, because
2253 * the monitor-uri is defined by the frontend.
2254 */
2255 if (unlikely((s->fe->monitor_uri_len != 0) &&
2256 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2257 !memcmp(&req->data[msg->sl.rq.u],
2258 s->fe->monitor_uri,
2259 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002260 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002261 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002262 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002263 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002264
Willy Tarreau59234e92008-11-30 23:51:27 +01002265 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002266
Willy Tarreau59234e92008-11-30 23:51:27 +01002267 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002268 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2269 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002270
Willy Tarreau59234e92008-11-30 23:51:27 +01002271 ret = acl_pass(ret);
2272 if (cond->pol == ACL_COND_UNLESS)
2273 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002274
Willy Tarreau59234e92008-11-30 23:51:27 +01002275 if (ret) {
2276 /* we fail this request, let's return 503 service unavail */
2277 txn->status = 503;
2278 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2279 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002280 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002281 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002282
Willy Tarreau59234e92008-11-30 23:51:27 +01002283 /* nothing to fail, let's reply normaly */
2284 txn->status = 200;
2285 stream_int_retnclose(req->prod, &http_200_chunk);
2286 goto return_prx_cond;
2287 }
2288
2289 /*
2290 * 3: Maybe we have to copy the original REQURI for the logs ?
2291 * Note: we cannot log anymore if the request has been
2292 * classified as invalid.
2293 */
2294 if (unlikely(s->logs.logwait & LW_REQ)) {
2295 /* we have a complete HTTP request that we must log */
2296 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2297 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002298
Willy Tarreau59234e92008-11-30 23:51:27 +01002299 if (urilen >= REQURI_LEN)
2300 urilen = REQURI_LEN - 1;
2301 memcpy(txn->uri, &req->data[msg->som], urilen);
2302 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002303
Willy Tarreau59234e92008-11-30 23:51:27 +01002304 if (!(s->logs.logwait &= ~LW_REQ))
2305 s->do_log(s);
2306 } else {
2307 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002308 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002309 }
Willy Tarreau06619262006-12-17 08:37:22 +01002310
Willy Tarreau59234e92008-11-30 23:51:27 +01002311 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002312 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2313 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002314
Willy Tarreau5b154472009-12-21 20:11:07 +01002315 /* ... and check if the request is HTTP/1.1 or above */
2316 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002317 ((req->data[msg->sl.rq.v + 5] > '1') ||
2318 ((req->data[msg->sl.rq.v + 5] == '1') &&
2319 (req->data[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002320 txn->flags |= TX_REQ_VER_11;
2321
2322 /* "connection" has not been parsed yet */
2323 txn->flags &= ~TX_CON_HDR_PARS;
2324
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002325 /* transfer length unknown*/
2326 txn->flags &= ~TX_REQ_XFER_LEN;
2327
Willy Tarreau59234e92008-11-30 23:51:27 +01002328 /* 5: we may need to capture headers */
2329 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
2330 capture_headers(req->data + msg->som, &txn->hdr_idx,
2331 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002332
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002333 /* 6: determine the transfer-length.
2334 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2335 * the presence of a message-body in a REQUEST and its transfer length
2336 * must be determined that way (in order of precedence) :
2337 * 1. The presence of a message-body in a request is signaled by the
2338 * inclusion of a Content-Length or Transfer-Encoding header field
2339 * in the request's header fields. When a request message contains
2340 * both a message-body of non-zero length and a method that does
2341 * not define any semantics for that request message-body, then an
2342 * origin server SHOULD either ignore the message-body or respond
2343 * with an appropriate error message (e.g., 413). A proxy or
2344 * gateway, when presented the same request, SHOULD either forward
2345 * the request inbound with the message- body or ignore the
2346 * message-body when determining a response.
2347 *
2348 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2349 * and the "chunked" transfer-coding (Section 6.2) is used, the
2350 * transfer-length is defined by the use of this transfer-coding.
2351 * If a Transfer-Encoding header field is present and the "chunked"
2352 * transfer-coding is not present, the transfer-length is defined
2353 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002354 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002355 * 3. If a Content-Length header field is present, its decimal value in
2356 * OCTETs represents both the entity-length and the transfer-length.
2357 * If a message is received with both a Transfer-Encoding header
2358 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002359 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002360 * 4. By the server closing the connection. (Closing the connection
2361 * cannot be used to indicate the end of a request body, since that
2362 * would leave no possibility for the server to send back a response.)
2363 *
2364 * Whenever a transfer-coding is applied to a message-body, the set of
2365 * transfer-codings MUST include "chunked", unless the message indicates
2366 * it is terminated by closing the connection. When the "chunked"
2367 * transfer-coding is used, it MUST be the last transfer-coding applied
2368 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002369 */
2370
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002371 /* CONNECT sets a tunnel and ignores everything else */
2372 if (txn->meth == HTTP_METH_CONNECT)
2373 goto skip_xfer_len;
2374
2375 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002376 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002377 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002378 while ((txn->flags & TX_REQ_VER_11) &&
2379 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002380 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2381 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2382 else if (txn->flags & TX_REQ_TE_CHNK) {
2383 /* bad transfer-encoding (chunked followed by something else) */
2384 use_close_only = 1;
2385 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2386 break;
2387 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002388 }
2389
Willy Tarreau32b47f42009-10-18 20:55:02 +02002390 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002391 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002392 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2393 signed long long cl;
2394
2395 if (!ctx.vlen)
2396 goto return_bad_req;
2397
2398 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2399 goto return_bad_req; /* parse failure */
2400
2401 if (cl < 0)
2402 goto return_bad_req;
2403
2404 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2405 goto return_bad_req; /* already specified, was different */
2406
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002407 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002408 msg->hdr_content_len = cl;
2409 }
2410
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002411 /* bodyless requests have a known length */
2412 if (!use_close_only)
2413 txn->flags |= TX_REQ_XFER_LEN;
2414
2415 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002416 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002417 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002418 req->analyse_exp = TICK_ETERNITY;
2419 return 1;
2420
2421 return_bad_req:
2422 /* We centralize bad requests processing here */
2423 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2424 /* we detected a parsing error. We want to archive this request
2425 * in the dedicated proxy area for later troubleshooting.
2426 */
2427 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2428 }
2429
2430 txn->req.msg_state = HTTP_MSG_ERROR;
2431 txn->status = 400;
2432 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002433
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002434 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002435 if (s->listener->counters)
2436 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002437
2438 return_prx_cond:
2439 if (!(s->flags & SN_ERR_MASK))
2440 s->flags |= SN_ERR_PRXCOND;
2441 if (!(s->flags & SN_FINST_MASK))
2442 s->flags |= SN_FINST_R;
2443
2444 req->analysers = 0;
2445 req->analyse_exp = TICK_ETERNITY;
2446 return 0;
2447}
2448
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002449/* This stream analyser runs all HTTP request processing which is common to
2450 * frontends and backends, which means blocking ACLs, filters, connection-close,
2451 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002452 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002453 * either needs more data or wants to immediately abort the request (eg: deny,
2454 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002455 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002456int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002457{
Willy Tarreaud787e662009-07-07 10:14:51 +02002458 struct http_txn *txn = &s->txn;
2459 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002460 struct acl_cond *cond;
2461 struct redirect_rule *rule;
2462 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002463
Willy Tarreau655dce92009-11-08 13:10:58 +01002464 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002465 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002466 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002467 return 0;
2468 }
2469
Willy Tarreau3a816292009-07-07 10:55:49 +02002470 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002471 req->analyse_exp = TICK_ETERNITY;
2472
2473 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2474 now_ms, __FUNCTION__,
2475 s,
2476 req,
2477 req->rex, req->wex,
2478 req->flags,
2479 req->l,
2480 req->analysers);
2481
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002482 /* first check whether we have some ACLs set to block this request */
2483 list_for_each_entry(cond, &px->block_cond, list) {
2484 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002485
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002486 ret = acl_pass(ret);
2487 if (cond->pol == ACL_COND_UNLESS)
2488 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002489
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002490 if (ret) {
2491 txn->status = 403;
2492 /* let's log the request time */
2493 s->logs.tv_request = now;
2494 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2495 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002496 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002497 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002498
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002499 /* try headers filters */
2500 if (px->req_exp != NULL) {
2501 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2502 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002503
Willy Tarreau59234e92008-11-30 23:51:27 +01002504 /* has the request been denied ? */
2505 if (txn->flags & TX_CLDENY) {
2506 /* no need to go further */
2507 txn->status = 403;
2508 /* let's log the request time */
2509 s->logs.tv_request = now;
2510 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2511 goto return_prx_cond;
2512 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002513
2514 /* When a connection is tarpitted, we use the tarpit timeout,
2515 * which may be the same as the connect timeout if unspecified.
2516 * If unset, then set it to zero because we really want it to
2517 * eventually expire. We build the tarpit as an analyser.
2518 */
2519 if (txn->flags & TX_CLTARPIT) {
2520 buffer_erase(s->req);
2521 /* wipe the request out so that we can drop the connection early
2522 * if the client closes first.
2523 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002524 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002525 req->analysers = 0; /* remove switching rules etc... */
2526 req->analysers |= AN_REQ_HTTP_TARPIT;
2527 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2528 if (!req->analyse_exp)
2529 req->analyse_exp = tick_add(now_ms, 0);
2530 return 1;
2531 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002532 }
Willy Tarreau06619262006-12-17 08:37:22 +01002533
Willy Tarreau5b154472009-12-21 20:11:07 +01002534 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2535 * only change if both the request and the config reference something else.
Willy Tarreau42736642009-10-18 21:04:35 +02002536 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002537
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002538 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreau5b154472009-12-21 20:11:07 +01002539 ((s->fe->options|s->be->options) & (PR_O_KEEPALIVE|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2540 int tmp = TX_CON_WANT_TUN;
2541 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2542 tmp = TX_CON_WANT_KAL;
2543 /* FIXME: for now, we don't support server-close mode */
2544 if ((s->fe->options|s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))
2545 tmp = TX_CON_WANT_CLO;
2546
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002547 if (!(txn->flags & TX_REQ_XFER_LEN))
2548 tmp = TX_CON_WANT_CLO;
2549
Willy Tarreau5b154472009-12-21 20:11:07 +01002550 if (!(txn->flags & TX_CON_HDR_PARS))
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002551 http_req_parse_connection_header(txn);
Willy Tarreau5b154472009-12-21 20:11:07 +01002552
2553 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2554 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2555 }
2556
2557 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2558 * once we know the backend, because the tunnel mode can be implied by the
2559 * lack of any close/keepalive options in both the FE and the BE. Since
2560 * this information can evolve with time, we proceed by trying to make the
2561 * header status match the desired status. For this, we'll have to adjust
2562 * the "Connection" header. The test for persistent connections has already
2563 * been performed, so we only enter here if there is a risk the connection
2564 * is considered as persistent and we want it to be closed on the server
2565 * side. It would be nice if we could enter this place only when a
2566 * Connection header exists. Note that a CONNECT method will not enter
2567 * here.
2568 */
2569 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002570 char *cur_ptr, *cur_end, *cur_next;
2571 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002572 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002573 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002574
Willy Tarreau5b154472009-12-21 20:11:07 +01002575 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002576 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002577
Willy Tarreau5b154472009-12-21 20:11:07 +01002578 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002579 cur_hdr = &txn->hdr_idx.v[cur_idx];
2580 cur_ptr = cur_next;
2581 cur_end = cur_ptr + cur_hdr->len;
2582 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002583
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002584 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002585 if (!val)
2586 continue;
2587
2588 /* 3 possibilities :
2589 * - we have already set "Connection: close" or we're in
2590 * HTTP/1.0, so we remove this line.
2591 * - we have not yet set "Connection: close", but this line
2592 * indicates close. We leave it untouched and set the flag.
2593 * - we have not yet set "Connection: close", and this line
2594 * indicates non-close. We replace it and set the flag.
2595 */
2596 if (must_delete) {
2597 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2598 http_msg_move_end(&txn->req, delta);
2599 cur_next += delta;
2600 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2601 txn->hdr_idx.used--;
2602 cur_hdr->len = 0;
2603 txn->flags |= TX_REQ_CONN_CLO;
2604 } else {
2605 if (cur_end - cur_ptr - val != 5 ||
2606 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2607 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2608 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002609 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002610 cur_hdr->len += delta;
2611 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002612 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002613 txn->flags |= TX_REQ_CONN_CLO;
2614 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002615 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002616 } /* for loop */
2617 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002618
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002619 /* add request headers from the rule sets in the same order */
2620 for (cur_idx = 0; cur_idx < px->nb_reqadd; cur_idx++) {
2621 if (unlikely(http_header_add_tail(req,
2622 &txn->req,
2623 &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002624 px->req_add[cur_idx]) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002625 goto return_bad_req;
2626 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002627
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002628 /* check if stats URI was requested, and if an auth is needed */
2629 if (px->uri_auth != NULL &&
2630 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2631 /* we have to check the URI and auth for this request.
2632 * FIXME!!! that one is rather dangerous, we want to
2633 * make it follow standard rules (eg: clear req->analysers).
2634 */
2635 if (stats_check_uri_auth(s, px)) {
2636 req->analysers = 0;
2637 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002639 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002640
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002641 /* check whether we have some ACLs set to redirect this request */
2642 list_for_each_entry(rule, &px->redirect_rules, list) {
2643 int ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreau06b917c2009-07-06 16:34:52 +02002644
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002645 ret = acl_pass(ret);
2646 if (rule->cond->pol == ACL_COND_UNLESS)
2647 ret = !ret;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002648
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002649 if (ret) {
2650 struct chunk rdr = { trash, 0 };
2651 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002652
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002653 /* build redirect message */
2654 switch(rule->code) {
2655 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002656 msg_fmt = HTTP_303;
2657 break;
2658 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002659 msg_fmt = HTTP_301;
2660 break;
2661 case 302:
2662 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002663 msg_fmt = HTTP_302;
2664 break;
2665 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002666
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002667 if (unlikely(chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002668 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002669
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002670 switch(rule->type) {
2671 case REDIRECT_TYPE_PREFIX: {
2672 const char *path;
2673 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002674
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002675 path = http_get_path(txn);
2676 /* build message using path */
2677 if (path) {
2678 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2679 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2680 int qs = 0;
2681 while (qs < pathlen) {
2682 if (path[qs] == '?') {
2683 pathlen = qs;
2684 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002685 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002686 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002687 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002688 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002689 } else {
2690 path = "/";
2691 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002692 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002693
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002694 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002695 goto return_bad_req;
2696
2697 /* add prefix. Note that if prefix == "/", we don't want to
2698 * add anything, otherwise it makes it hard for the user to
2699 * configure a self-redirection.
2700 */
2701 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002702 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2703 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002704 }
2705
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002706 /* add path */
2707 memcpy(rdr.str + rdr.len, path, pathlen);
2708 rdr.len += pathlen;
2709 break;
2710 }
2711 case REDIRECT_TYPE_LOCATION:
2712 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002713 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002714 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002715
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002716 /* add location */
2717 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2718 rdr.len += rule->rdr_len;
2719 break;
2720 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002721
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002722 if (rule->cookie_len) {
2723 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2724 rdr.len += 14;
2725 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2726 rdr.len += rule->cookie_len;
2727 memcpy(rdr.str + rdr.len, "\r\n", 2);
2728 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002729 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002730
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002731 /* add end of headers */
2732 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2733 rdr.len += 4;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002734
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002735 txn->status = rule->code;
2736 /* let's log the request time */
2737 s->logs.tv_request = now;
2738 stream_int_retnclose(req->prod, &rdr);
2739 goto return_prx_cond;
2740 }
2741 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002742
Willy Tarreaud98cf932009-12-27 22:54:55 +01002743 /* We can shut read side if we know how we won't transfer any more data && !abort_on_close */
2744 if ((txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau5b154472009-12-21 20:11:07 +01002745 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
Willy Tarreaud98cf932009-12-27 22:54:55 +01002746 (req->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)))
Willy Tarreau349a0f62009-10-18 21:17:42 +02002747 req->flags |= BF_DONT_READ;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +02002748
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002749 /* that's OK for us now, let's move on to next analysers */
2750 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002751
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002752 return_bad_req:
2753 /* We centralize bad requests processing here */
2754 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2755 /* we detected a parsing error. We want to archive this request
2756 * in the dedicated proxy area for later troubleshooting.
2757 */
2758 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2759 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002760
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002761 txn->req.msg_state = HTTP_MSG_ERROR;
2762 txn->status = 400;
2763 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002764
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002765 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002766 if (s->listener->counters)
2767 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002768
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002769 return_prx_cond:
2770 if (!(s->flags & SN_ERR_MASK))
2771 s->flags |= SN_ERR_PRXCOND;
2772 if (!(s->flags & SN_FINST_MASK))
2773 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002774
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002775 req->analysers = 0;
2776 req->analyse_exp = TICK_ETERNITY;
2777 return 0;
2778}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002779
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002780/* This function performs all the processing enabled for the current request.
2781 * It returns 1 if the processing can continue on next analysers, or zero if it
2782 * needs more data, encounters an error, or wants to immediately abort the
2783 * request. It relies on buffers flags, and updates s->req->analysers.
2784 */
2785int http_process_request(struct session *s, struct buffer *req, int an_bit)
2786{
2787 struct http_txn *txn = &s->txn;
2788 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002789
Willy Tarreau655dce92009-11-08 13:10:58 +01002790 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002791 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002792 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002793 return 0;
2794 }
2795
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002796 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2797 now_ms, __FUNCTION__,
2798 s,
2799 req,
2800 req->rex, req->wex,
2801 req->flags,
2802 req->l,
2803 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002804
Willy Tarreau59234e92008-11-30 23:51:27 +01002805 /*
2806 * Right now, we know that we have processed the entire headers
2807 * and that unwanted requests have been filtered out. We can do
2808 * whatever we want with the remaining request. Also, now we
2809 * may have separate values for ->fe, ->be.
2810 */
Willy Tarreau06619262006-12-17 08:37:22 +01002811
Willy Tarreau59234e92008-11-30 23:51:27 +01002812 /*
2813 * If HTTP PROXY is set we simply get remote server address
2814 * parsing incoming request.
2815 */
2816 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2817 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2818 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002819
Willy Tarreau59234e92008-11-30 23:51:27 +01002820 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002821 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002822 * Note that doing so might move headers in the request, but
2823 * the fields will stay coherent and the URI will not move.
2824 * This should only be performed in the backend.
2825 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002826 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002827 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2828 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002829
Willy Tarreau59234e92008-11-30 23:51:27 +01002830 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002831 * 8: the appsession cookie was looked up very early in 1.2,
2832 * so let's do the same now.
2833 */
2834
2835 /* It needs to look into the URI */
2836 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002837 get_srv_from_appsession(s, &req->data[msg->sl.rq.u], msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002838 }
2839
2840 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002841 * 9: add X-Forwarded-For if either the frontend or the backend
2842 * asks for it.
2843 */
2844 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2845 if (s->cli_addr.ss_family == AF_INET) {
2846 /* Add an X-Forwarded-For header unless the source IP is
2847 * in the 'except' network range.
2848 */
2849 if ((!s->fe->except_mask.s_addr ||
2850 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2851 != s->fe->except_net.s_addr) &&
2852 (!s->be->except_mask.s_addr ||
2853 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2854 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002855 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002856 unsigned char *pn;
2857 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002858
2859 /* Note: we rely on the backend to get the header name to be used for
2860 * x-forwarded-for, because the header is really meant for the backends.
2861 * However, if the backend did not specify any option, we have to rely
2862 * on the frontend's header name.
2863 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002864 if (s->be->fwdfor_hdr_len) {
2865 len = s->be->fwdfor_hdr_len;
2866 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002867 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002868 len = s->fe->fwdfor_hdr_len;
2869 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002870 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002871 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002872
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002873 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002874 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01002875 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002876 }
2877 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002878 else if (s->cli_addr.ss_family == AF_INET6) {
2879 /* FIXME: for the sake of completeness, we should also support
2880 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002881 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002882 int len;
2883 char pn[INET6_ADDRSTRLEN];
2884 inet_ntop(AF_INET6,
2885 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2886 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002887
Willy Tarreau59234e92008-11-30 23:51:27 +01002888 /* Note: we rely on the backend to get the header name to be used for
2889 * x-forwarded-for, because the header is really meant for the backends.
2890 * However, if the backend did not specify any option, we have to rely
2891 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002892 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002893 if (s->be->fwdfor_hdr_len) {
2894 len = s->be->fwdfor_hdr_len;
2895 memcpy(trash, s->be->fwdfor_hdr_name, len);
2896 } else {
2897 len = s->fe->fwdfor_hdr_len;
2898 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002899 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002900 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002901
Willy Tarreau59234e92008-11-30 23:51:27 +01002902 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002903 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01002904 goto return_bad_req;
2905 }
2906 }
2907
2908 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002909 * 10: add X-Original-To if either the frontend or the backend
2910 * asks for it.
2911 */
2912 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
2913
2914 /* FIXME: don't know if IPv6 can handle that case too. */
2915 if (s->cli_addr.ss_family == AF_INET) {
2916 /* Add an X-Original-To header unless the destination IP is
2917 * in the 'except' network range.
2918 */
2919 if (!(s->flags & SN_FRT_ADDR_SET))
2920 get_frt_addr(s);
2921
2922 if ((!s->fe->except_mask_to.s_addr ||
2923 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
2924 != s->fe->except_to.s_addr) &&
2925 (!s->be->except_mask_to.s_addr ||
2926 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
2927 != s->be->except_to.s_addr)) {
2928 int len;
2929 unsigned char *pn;
2930 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
2931
2932 /* Note: we rely on the backend to get the header name to be used for
2933 * x-original-to, because the header is really meant for the backends.
2934 * However, if the backend did not specify any option, we have to rely
2935 * on the frontend's header name.
2936 */
2937 if (s->be->orgto_hdr_len) {
2938 len = s->be->orgto_hdr_len;
2939 memcpy(trash, s->be->orgto_hdr_name, len);
2940 } else {
2941 len = s->fe->orgto_hdr_len;
2942 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002943 }
Maik Broemme2850cb42009-04-17 18:53:21 +02002944 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
2945
2946 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002947 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02002948 goto return_bad_req;
2949 }
2950 }
2951 }
2952
Willy Tarreau78599912009-10-17 20:12:21 +02002953 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau5b154472009-12-21 20:11:07 +01002954 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau78599912009-10-17 20:12:21 +02002955 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002956 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01002957 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01002958 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01002959 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01002960
2961 /* If we have no server assigned yet and we're balancing on url_param
2962 * with a POST request, we may be interested in checking the body for
2963 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01002964 */
2965 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2966 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01002967 s->be->url_param_post_limit != 0 &&
2968 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002969 memchr(req->data + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01002970 buffer_dont_connect(req);
2971 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01002972 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002973
Willy Tarreaud98cf932009-12-27 22:54:55 +01002974 if (txn->flags & TX_REQ_XFER_LEN)
2975 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01002976
Willy Tarreau59234e92008-11-30 23:51:27 +01002977 /*************************************************************
2978 * OK, that's finished for the headers. We have done what we *
2979 * could. Let's switch to the DATA state. *
2980 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01002981 req->analyse_exp = TICK_ETERNITY;
2982 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002983
Willy Tarreau59234e92008-11-30 23:51:27 +01002984 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01002985 /* OK let's go on with the BODY now */
2986 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002987
Willy Tarreau59234e92008-11-30 23:51:27 +01002988 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02002989 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002990 /* we detected a parsing error. We want to archive this request
2991 * in the dedicated proxy area for later troubleshooting.
2992 */
Willy Tarreau4076a152009-04-02 15:18:36 +02002993 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01002994 }
Willy Tarreau4076a152009-04-02 15:18:36 +02002995
Willy Tarreau59234e92008-11-30 23:51:27 +01002996 txn->req.msg_state = HTTP_MSG_ERROR;
2997 txn->status = 400;
2998 req->analysers = 0;
2999 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003000
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003001 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003002 if (s->listener->counters)
3003 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003004
Willy Tarreau59234e92008-11-30 23:51:27 +01003005 if (!(s->flags & SN_ERR_MASK))
3006 s->flags |= SN_ERR_PRXCOND;
3007 if (!(s->flags & SN_FINST_MASK))
3008 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003009 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003010}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003011
Willy Tarreau60b85b02008-11-30 23:28:40 +01003012/* This function is an analyser which processes the HTTP tarpit. It always
3013 * returns zero, at the beginning because it prevents any other processing
3014 * from occurring, and at the end because it terminates the request.
3015 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003016int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003017{
3018 struct http_txn *txn = &s->txn;
3019
3020 /* This connection is being tarpitted. The CLIENT side has
3021 * already set the connect expiration date to the right
3022 * timeout. We just have to check that the client is still
3023 * there and that the timeout has not expired.
3024 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003025 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003026 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3027 !tick_is_expired(req->analyse_exp, now_ms))
3028 return 0;
3029
3030 /* We will set the queue timer to the time spent, just for
3031 * logging purposes. We fake a 500 server error, so that the
3032 * attacker will not suspect his connection has been tarpitted.
3033 * It will not cause trouble to the logs because we can exclude
3034 * the tarpitted connections by filtering on the 'PT' status flags.
3035 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003036 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3037
3038 txn->status = 500;
3039 if (req->flags != BF_READ_ERROR)
3040 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3041
3042 req->analysers = 0;
3043 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003044
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003045 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003046 if (s->listener->counters)
3047 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003048
Willy Tarreau60b85b02008-11-30 23:28:40 +01003049 if (!(s->flags & SN_ERR_MASK))
3050 s->flags |= SN_ERR_PRXCOND;
3051 if (!(s->flags & SN_FINST_MASK))
3052 s->flags |= SN_FINST_T;
3053 return 0;
3054}
3055
Willy Tarreaud34af782008-11-30 23:36:37 +01003056/* This function is an analyser which processes the HTTP request body. It looks
3057 * for parameters to be used for the load balancing algorithm (url_param). It
3058 * must only be called after the standard HTTP request processing has occurred,
3059 * because it expects the request to be parsed. It returns zero if it needs to
3060 * read more data, or 1 once it has completed its analysis.
3061 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003062int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003063{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003064 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003065 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003066 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003067
3068 /* We have to parse the HTTP request body to find any required data.
3069 * "balance url_param check_post" should have been the only way to get
3070 * into this. We were brought here after HTTP header analysis, so all
3071 * related structures are ready.
3072 */
3073
Willy Tarreau522d6c02009-12-06 18:49:18 +01003074 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3075 goto missing_data;
3076
3077 if (msg->msg_state < HTTP_MSG_100_SENT) {
3078 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3079 * send an HTTP/1.1 100 Continue intermediate response.
3080 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003081 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003082 struct hdr_ctx ctx;
3083 ctx.idx = 0;
3084 /* Expect is allowed in 1.1, look for it */
3085 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3086 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3087 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3088 }
3089 }
3090 msg->msg_state = HTTP_MSG_100_SENT;
3091 }
3092
3093 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003094 /* we have msg->col and msg->sov which both point to the first
3095 * byte of message body. msg->som still points to the beginning
3096 * of the message. We must save the body in req->lr because it
3097 * survives buffer re-alignments.
3098 */
3099 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003100 if (txn->flags & TX_REQ_TE_CHNK)
3101 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3102 else
3103 msg->msg_state = HTTP_MSG_DATA;
3104 }
3105
3106 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003107 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003108 * set ->sov and ->lr to point to the body and switch to DATA or
3109 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003110 */
3111 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003112
Willy Tarreau115acb92009-12-26 13:56:06 +01003113 if (!ret)
3114 goto missing_data;
3115 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003116 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003117 }
3118
Willy Tarreaud98cf932009-12-27 22:54:55 +01003119 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003120 * We have the first non-header byte in msg->col, which is either the
3121 * beginning of the chunk size or of the data. The first data byte is in
3122 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3123 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003124 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003125
3126 if (msg->hdr_content_len < limit)
3127 limit = msg->hdr_content_len;
3128
Willy Tarreau7c96f672009-12-27 22:47:25 +01003129 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003130 goto http_end;
3131
3132 missing_data:
3133 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003134 if (req->flags & BF_FULL)
3135 goto return_bad_req;
3136
Willy Tarreau522d6c02009-12-06 18:49:18 +01003137 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3138 txn->status = 408;
3139 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3140 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003141 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003142
3143 /* we get here if we need to wait for more data */
3144 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003145 /* Not enough data. We'll re-use the http-request
3146 * timeout here. Ideally, we should set the timeout
3147 * relative to the accept() date. We just set the
3148 * request timeout once at the beginning of the
3149 * request.
3150 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003151 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003152 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003153 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003154 return 0;
3155 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003156
3157 http_end:
3158 /* The situation will not evolve, so let's give up on the analysis. */
3159 s->logs.tv_request = now; /* update the request timer to reflect full request */
3160 req->analysers &= ~an_bit;
3161 req->analyse_exp = TICK_ETERNITY;
3162 return 1;
3163
3164 return_bad_req: /* let's centralize all bad requests */
3165 txn->req.msg_state = HTTP_MSG_ERROR;
3166 txn->status = 400;
3167 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3168
3169 return_err_msg:
3170 req->analysers = 0;
3171 s->fe->counters.failed_req++;
3172 if (s->listener->counters)
3173 s->listener->counters->failed_req++;
3174
3175 if (!(s->flags & SN_ERR_MASK))
3176 s->flags |= SN_ERR_PRXCOND;
3177 if (!(s->flags & SN_FINST_MASK))
3178 s->flags |= SN_FINST_R;
3179 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003180}
3181
Willy Tarreaud98cf932009-12-27 22:54:55 +01003182/* This function is an analyser which forwards request body (including chunk
3183 * sizes if any). It is called as soon as we must forward, even if we forward
3184 * zero byte. The only situation where it must not be called is when we're in
3185 * tunnel mode and we want to forward till the close. It's used both to forward
3186 * remaining data and to resync after end of body. It expects the msg_state to
3187 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3188 * read more data, or 1 once we can go on with next request or end the session.
3189 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3190 * bytes of pending data + the headers if not already done (between som and sov).
3191 * It eventually adjusts som to match sov after the data in between have been sent.
3192 */
3193int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3194{
3195 struct http_txn *txn = &s->txn;
3196 struct http_msg *msg = &s->txn.req;
3197
3198 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3199 return 0;
3200
3201 /* Note that we don't have to send 100-continue back because we don't
3202 * need the data to complete our job, and it's up to the server to
3203 * decide whether to return 100, 417 or anything else in return of
3204 * an "Expect: 100-continue" header.
3205 */
3206
3207 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3208 /* we have msg->col and msg->sov which both point to the first
3209 * byte of message body. msg->som still points to the beginning
3210 * of the message. We must save the body in req->lr because it
3211 * survives buffer re-alignments.
3212 */
3213 req->lr = req->data + msg->sov;
3214 if (txn->flags & TX_REQ_TE_CHNK)
3215 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3216 else {
3217 msg->msg_state = HTTP_MSG_DATA;
3218 }
3219 }
3220
3221 /* we may already have some data pending */
3222 if (msg->hdr_content_len || msg->som != msg->sov) {
3223 buffer_forward(req, msg->sov - msg->som + msg->hdr_content_len);
3224 msg->hdr_content_len = 0; /* don't forward that again */
3225 msg->som = msg->sov;
3226 }
3227
3228 while (1) {
3229 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
3230 /* read the chunk size and assign it to ->hdr_content_len, then
3231 * set ->sov and ->lr to point to the body and switch to DATA or
3232 * TRAILERS state.
3233 */
3234 int ret = http_parse_chunk_size(req, msg);
3235
3236 if (!ret)
3237 goto missing_data;
3238 else if (ret < 0)
3239 goto return_bad_req;
3240 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
3241
3242 /* forward the chunk size as well as any pending data */
3243 if (msg->hdr_content_len || msg->som != msg->sov) {
3244 buffer_forward(req, msg->sov - msg->som + msg->hdr_content_len);
3245 msg->hdr_content_len = 0; /* don't forward that again */
3246 msg->som = msg->sov;
3247 }
3248 }
3249 else if (msg->msg_state == HTTP_MSG_DATA) {
3250 /* must still forward */
3251 if (req->to_forward)
3252 return 0;
3253
3254 /* we're sending the last bits of request, the server's response
3255 * is expected in a short time. Most often the first read is enough
3256 * to bring all the headers, so we're preparing the response buffer
3257 * to read the response now. Note that we should probably move that
3258 * to a more appropriate place.
3259 */
3260 s->rep->flags |= BF_READ_DONTWAIT;
3261
3262 /* nothing left to forward */
3263 if (txn->flags & TX_REQ_TE_CHNK)
3264 msg->msg_state = HTTP_MSG_DATA_CRLF;
3265 else {
3266 msg->msg_state = HTTP_MSG_DONE;
3267 }
3268 }
3269 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3270 /* we want the CRLF after the data */
3271 int ret;
3272
3273 if (!(req->flags & BF_OUT_EMPTY))
3274 return 0;
3275 /* The output pointer does not move anymore, next unsent data
3276 * are available at ->w. Let's save that.
3277 */
3278 req->lr = req->w;
3279 ret = http_skip_chunk_crlf(req, msg);
3280
3281 if (ret == 0)
3282 goto missing_data;
3283 else if (ret < 0)
3284 goto return_bad_req;
3285 /* we're in MSG_CHUNK_SIZE now */
3286 }
3287 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3288 int ret = http_forward_trailers(req, msg);
3289
3290 if (ret == 0)
3291 goto missing_data;
3292 else if (ret < 0)
3293 goto return_bad_req;
3294 /* we're in HTTP_MSG_DONE now */
3295 }
3296 else if (msg->msg_state == HTTP_MSG_DONE) {
3297 /* No need to read anymore, the request is complete */
3298 req->flags |= BF_DONT_READ;
3299
3300 if (!(s->req->flags & BF_OUT_EMPTY) || s->req->to_forward)
3301 return 0;
3302
3303 if (!(s->rep->flags & BF_OUT_EMPTY) || s->rep->to_forward)
3304 return 0;
3305
3306 if (txn->rsp.msg_state != HTTP_MSG_DONE && txn->rsp.msg_state != HTTP_MSG_ERROR) {
3307 /* OK the request was completely sent, re-enable
3308 * reading on server side but return immediately.
3309 * FIXME: we should probably move that somewhere else !
3310 */
3311 s->rep->flags &= ~BF_DONT_READ;
3312 return 0;
3313 }
3314
3315 /* when we support keep-alive or server-close modes, we'll have
3316 * to reset the transaction here.
3317 */
3318 req->flags &= ~BF_DONT_READ; /* re-enable reading on client side */
3319 break;
3320 }
3321 }
3322
3323 /* OK we're done with the data phase */
3324 req->analysers &= ~an_bit;
3325 return 1;
3326
3327 missing_data:
3328 /* forward the chunk size as well as any pending data */
3329 if (msg->hdr_content_len || msg->som != msg->sov) {
3330 buffer_forward(req, msg->sov - msg->som + msg->hdr_content_len);
3331 msg->hdr_content_len = 0; /* don't forward that again */
3332 msg->som = msg->sov;
3333 }
3334
3335 if (req->flags & BF_FULL)
3336 goto return_bad_req;
3337 /* the session handler will take care of timeouts and errors */
3338 return 0;
3339
3340 return_bad_req: /* let's centralize all bad requests */
3341 txn->req.msg_state = HTTP_MSG_ERROR;
3342 txn->status = 400;
3343 /* Note: we don't send any error if some data were already sent */
3344 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3345
3346 req->analysers = 0;
3347 s->fe->counters.failed_req++;
3348 if (s->listener->counters)
3349 s->listener->counters->failed_req++;
3350
3351 if (!(s->flags & SN_ERR_MASK))
3352 s->flags |= SN_ERR_PRXCOND;
3353 if (!(s->flags & SN_FINST_MASK))
3354 s->flags |= SN_FINST_R;
3355 return 0;
3356}
3357
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003358/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3359 * processing can continue on next analysers, or zero if it either needs more
3360 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3361 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3362 * when it has nothing left to do, and may remove any analyser when it wants to
3363 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003364 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003365int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003366{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003367 struct http_txn *txn = &s->txn;
3368 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003369 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003370 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003371 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003372 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003373
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003374 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 +02003375 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003376 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003377 rep,
3378 rep->rex, rep->wex,
3379 rep->flags,
3380 rep->l,
3381 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003382
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003383 /*
3384 * Now parse the partial (or complete) lines.
3385 * We will check the response syntax, and also join multi-line
3386 * headers. An index of all the lines will be elaborated while
3387 * parsing.
3388 *
3389 * For the parsing, we use a 28 states FSM.
3390 *
3391 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003392 * rep->data + msg->som = beginning of response
3393 * rep->data + msg->eoh = end of processed headers / start of current one
3394 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003395 * rep->lr = first non-visited byte
3396 * rep->r = end of data
3397 */
3398
Willy Tarreau83e3af02009-12-28 17:39:57 +01003399 /* There's a protected area at the end of the buffer for rewriting
3400 * purposes. We don't want to start to parse the request if the
3401 * protected area is affected, because we may have to move processed
3402 * data later, which is much more complicated.
3403 */
3404 if (rep->l &&
3405 (rep->r <= rep->lr || rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3406 if (rep->send_max) {
3407 /* some data has still not left the buffer, wake us once that's done */
3408 buffer_dont_close(rep);
3409 return 0;
3410 }
3411
3412 http_buffer_heavy_realign(rep, msg);
3413 }
3414
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003415 if (likely(rep->lr < rep->r))
3416 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau7f875f62008-08-11 17:35:01 +02003417
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003418 /* 1: we might have to print this header in debug mode */
3419 if (unlikely((global.mode & MODE_DEBUG) &&
3420 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003421 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003422 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003423
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003424 sol = rep->data + msg->som;
3425 eol = sol + msg->sl.rq.l;
3426 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003427
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003428 sol += hdr_idx_first_pos(&txn->hdr_idx);
3429 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003430
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003431 while (cur_idx) {
3432 eol = sol + txn->hdr_idx.v[cur_idx].len;
3433 debug_hdr("srvhdr", s, sol, eol);
3434 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3435 cur_idx = txn->hdr_idx.v[cur_idx].next;
3436 }
3437 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003438
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003439 /*
3440 * Now we quickly check if we have found a full valid response.
3441 * If not so, we check the FD and buffer states before leaving.
3442 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01003443 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003444 * responses are checked first.
3445 *
3446 * Depending on whether the client is still there or not, we
3447 * may send an error response back or not. Note that normally
3448 * we should only check for HTTP status there, and check I/O
3449 * errors somewhere else.
3450 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003451
Willy Tarreau655dce92009-11-08 13:10:58 +01003452 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003453 /* Invalid response */
3454 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
3455 /* we detected a parsing error. We want to archive this response
3456 * in the dedicated proxy area for later troubleshooting.
3457 */
3458 hdr_response_bad:
3459 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
3460 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3461
3462 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003463 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003464 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003465 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
3466 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003467
3468 rep->analysers = 0;
3469 txn->status = 502;
3470 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
3471
3472 if (!(s->flags & SN_ERR_MASK))
3473 s->flags |= SN_ERR_PRXCOND;
3474 if (!(s->flags & SN_FINST_MASK))
3475 s->flags |= SN_FINST_H;
3476
3477 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003478 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003479
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003480 /* too large response does not fit in buffer. */
3481 else if (rep->flags & BF_FULL) {
3482 goto hdr_response_bad;
3483 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003484
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003485 /* read error */
3486 else if (rep->flags & BF_READ_ERROR) {
3487 if (msg->err_pos >= 0)
3488 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02003489
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003490 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003491 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003492 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003493 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
3494 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003495
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003496 rep->analysers = 0;
3497 txn->status = 502;
3498 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02003499
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003500 if (!(s->flags & SN_ERR_MASK))
3501 s->flags |= SN_ERR_SRVCL;
3502 if (!(s->flags & SN_FINST_MASK))
3503 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003504 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003505 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003506
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003507 /* read timeout : return a 504 to the client. */
3508 else if (rep->flags & BF_READ_TIMEOUT) {
3509 if (msg->err_pos >= 0)
3510 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003511
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003512 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003513 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003514 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003515 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
3516 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003517
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003518 rep->analysers = 0;
3519 txn->status = 504;
3520 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02003521
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003522 if (!(s->flags & SN_ERR_MASK))
3523 s->flags |= SN_ERR_SRVTO;
3524 if (!(s->flags & SN_FINST_MASK))
3525 s->flags |= SN_FINST_H;
3526 return 0;
3527 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02003528
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003529 /* close from server */
3530 else if (rep->flags & BF_SHUTR) {
3531 if (msg->err_pos >= 0)
3532 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003533
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003534 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003535 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003536 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003537 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
3538 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003539
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003540 rep->analysers = 0;
3541 txn->status = 502;
3542 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01003543
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003544 if (!(s->flags & SN_ERR_MASK))
3545 s->flags |= SN_ERR_SRVCL;
3546 if (!(s->flags & SN_FINST_MASK))
3547 s->flags |= SN_FINST_H;
3548 return 0;
3549 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003550
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003551 /* write error to client (we don't send any message then) */
3552 else if (rep->flags & BF_WRITE_ERROR) {
3553 if (msg->err_pos >= 0)
3554 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003555
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003556 s->be->counters.failed_resp++;
3557 rep->analysers = 0;
3558
3559 if (!(s->flags & SN_ERR_MASK))
3560 s->flags |= SN_ERR_CLICL;
3561 if (!(s->flags & SN_FINST_MASK))
3562 s->flags |= SN_FINST_H;
3563
3564 /* process_session() will take care of the error */
3565 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003566 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003567
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003568 buffer_dont_close(rep);
3569 return 0;
3570 }
3571
3572 /* More interesting part now : we know that we have a complete
3573 * response which at least looks like HTTP. We have an indicator
3574 * of each header's length, so we can parse them quickly.
3575 */
3576
3577 if (unlikely(msg->err_pos >= 0))
3578 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3579
3580 /* ensure we keep this pointer to the beginning of the message */
3581 msg->sol = rep->data + msg->som;
3582
3583 /*
3584 * 1: get the status code
3585 */
3586 n = rep->data[msg->sl.st.c] - '0';
3587 if (n < 1 || n > 5)
3588 n = 0;
3589 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003590
Willy Tarreau5b154472009-12-21 20:11:07 +01003591 /* check if the response is HTTP/1.1 or above */
3592 if ((msg->sl.st.v_l == 8) &&
3593 ((rep->data[msg->som + 5] > '1') ||
3594 ((rep->data[msg->som + 5] == '1') &&
3595 (rep->data[msg->som + 7] >= '1'))))
3596 txn->flags |= TX_RES_VER_11;
3597
3598 /* "connection" has not been parsed yet */
3599 txn->flags &= ~TX_CON_HDR_PARS;
3600
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003601 /* transfer length unknown*/
3602 txn->flags &= ~TX_RES_XFER_LEN;
3603
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003604 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
3605
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003606 if (txn->status >= 100 && txn->status < 500)
3607 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
3608 else
3609 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
3610
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003611 /*
3612 * 2: check for cacheability.
3613 */
3614
3615 switch (txn->status) {
3616 case 200:
3617 case 203:
3618 case 206:
3619 case 300:
3620 case 301:
3621 case 410:
3622 /* RFC2616 @13.4:
3623 * "A response received with a status code of
3624 * 200, 203, 206, 300, 301 or 410 MAY be stored
3625 * by a cache (...) unless a cache-control
3626 * directive prohibits caching."
3627 *
3628 * RFC2616 @9.5: POST method :
3629 * "Responses to this method are not cacheable,
3630 * unless the response includes appropriate
3631 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003632 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003633 if (likely(txn->meth != HTTP_METH_POST) &&
3634 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3635 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3636 break;
3637 default:
3638 break;
3639 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003640
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003641 /*
3642 * 3: we may need to capture headers
3643 */
3644 s->logs.logwait &= ~LW_RESP;
3645 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
3646 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3647 txn->rsp.cap, s->fe->rsp_cap);
3648
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003649 /* 4: determine the transfer-length.
3650 * According to RFC2616 #4.4, amended by the HTTPbis working group,
3651 * the presence of a message-body in a RESPONSE and its transfer length
3652 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003653 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003654 * All responses to the HEAD request method MUST NOT include a
3655 * message-body, even though the presence of entity-header fields
3656 * might lead one to believe they do. All 1xx (informational), 204
3657 * (No Content), and 304 (Not Modified) responses MUST NOT include a
3658 * message-body. All other responses do include a message-body,
3659 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003660 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003661 * 1. Any response which "MUST NOT" include a message-body (such as the
3662 * 1xx, 204 and 304 responses and any response to a HEAD request) is
3663 * always terminated by the first empty line after the header fields,
3664 * regardless of the entity-header fields present in the message.
3665 *
3666 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
3667 * the "chunked" transfer-coding (Section 6.2) is used, the
3668 * transfer-length is defined by the use of this transfer-coding.
3669 * If a Transfer-Encoding header field is present and the "chunked"
3670 * transfer-coding is not present, the transfer-length is defined by
3671 * the sender closing the connection.
3672 *
3673 * 3. If a Content-Length header field is present, its decimal value in
3674 * OCTETs represents both the entity-length and the transfer-length.
3675 * If a message is received with both a Transfer-Encoding header
3676 * field and a Content-Length header field, the latter MUST be ignored.
3677 *
3678 * 4. If the message uses the media type "multipart/byteranges", and
3679 * the transfer-length is not otherwise specified, then this self-
3680 * delimiting media type defines the transfer-length. This media
3681 * type MUST NOT be used unless the sender knows that the recipient
3682 * can parse it; the presence in a request of a Range header with
3683 * multiple byte-range specifiers from a 1.1 client implies that the
3684 * client can parse multipart/byteranges responses.
3685 *
3686 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003687 */
3688
3689 /* Skip parsing if no content length is possible. The response flags
3690 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003691 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003692 * FIXME: should we parse anyway and return an error on chunked encoding ?
3693 */
3694 if (txn->meth == HTTP_METH_HEAD ||
3695 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003696 txn->status == 204 || txn->status == 304) {
3697 txn->flags |= TX_RES_XFER_LEN;
3698 goto skip_content_length;
3699 }
3700
3701 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003702 goto skip_content_length;
3703
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003704 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003705 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003706 while ((txn->flags & TX_RES_VER_11) &&
3707 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003708 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
3709 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3710 else if (txn->flags & TX_RES_TE_CHNK) {
3711 /* bad transfer-encoding (chunked followed by something else) */
3712 use_close_only = 1;
3713 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3714 break;
3715 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003716 }
3717
3718 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
3719 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003720 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003721 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
3722 signed long long cl;
3723
3724 if (!ctx.vlen)
3725 goto hdr_response_bad;
3726
3727 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
3728 goto hdr_response_bad; /* parse failure */
3729
3730 if (cl < 0)
3731 goto hdr_response_bad;
3732
3733 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
3734 goto hdr_response_bad; /* already specified, was different */
3735
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003736 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003737 msg->hdr_content_len = cl;
3738 }
3739
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003740 /* FIXME: we should also implement the multipart/byterange method.
3741 * For now on, we resort to close mode in this case (unknown length).
3742 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003743skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003744
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003745 /* end of job, return OK */
3746 rep->analysers &= ~an_bit;
3747 rep->analyse_exp = TICK_ETERNITY;
3748 return 1;
3749}
3750
3751/* This function performs all the processing enabled for the current response.
3752 * It normally returns zero, but may return 1 if it absolutely needs to be
3753 * called again after other functions. It relies on buffers flags, and updates
3754 * t->rep->analysers. It might make sense to explode it into several other
3755 * functions. It works like process_request (see indications above).
3756 */
3757int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
3758{
3759 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003760 struct http_msg *msg = &txn->rsp;
3761 struct proxy *cur_proxy;
3762 int cur_idx;
Willy Tarreau5b154472009-12-21 20:11:07 +01003763 int conn_ka = 0, conn_cl = 0;
3764 int must_close = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003765
3766 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3767 now_ms, __FUNCTION__,
3768 t,
3769 rep,
3770 rep->rex, rep->wex,
3771 rep->flags,
3772 rep->l,
3773 rep->analysers);
3774
Willy Tarreau655dce92009-11-08 13:10:58 +01003775 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003776 return 0;
3777
3778 rep->analysers &= ~an_bit;
3779 rep->analyse_exp = TICK_ETERNITY;
3780
Willy Tarreau5b154472009-12-21 20:11:07 +01003781 /* Now we have to check if we need to modify the Connection header.
3782 * This is more difficult on the response than it is on the request,
3783 * because we can have two different HTTP versions and we don't know
3784 * how the client will interprete a response. For instance, let's say
3785 * that the client sends a keep-alive request in HTTP/1.0 and gets an
3786 * HTTP/1.1 response without any header. Maybe it will bound itself to
3787 * HTTP/1.0 because it only knows about it, and will consider the lack
3788 * of header as a close, or maybe it knows HTTP/1.1 and can consider
3789 * the lack of header as a keep-alive. Thus we will use two flags
3790 * indicating how a request MAY be understood by the client. In case
3791 * of multiple possibilities, we'll fix the header to be explicit. If
3792 * ambiguous cases such as both close and keepalive are seen, then we
3793 * will fall back to explicit close. Note that we won't take risks with
3794 * HTTP/1.0 clients which may not necessarily understand keep-alive.
3795 */
3796
3797 if ((txn->meth != HTTP_METH_CONNECT) &&
3798 (txn->status >= 200) &&
3799 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN &&
3800 !(txn->flags & TX_CON_HDR_PARS)) {
3801 int may_keep = 0, may_close = 0; /* how it may be understood */
3802 struct hdr_ctx ctx;
3803
3804 ctx.idx = 0;
3805 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
3806 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
3807 conn_cl = 1;
3808 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
3809 conn_ka = 1;
3810 }
3811
3812 if (conn_cl) {
3813 /* close header present */
3814 may_close = 1;
3815 if (conn_ka) /* we have both close and keep-alive */
3816 may_keep = 1;
3817 }
3818 else if (conn_ka) {
3819 /* keep-alive alone */
3820 may_keep = 1;
3821 }
3822 else {
3823 /* no close nor keep-alive header */
3824 if (txn->flags & TX_RES_VER_11)
3825 may_keep = 1;
3826 else
3827 may_close = 1;
3828
3829 if (txn->flags & TX_REQ_VER_11)
3830 may_keep = 1;
3831 else
3832 may_close = 1;
3833 }
3834
3835 /* let's update the transaction status to reflect any close.
3836 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003837 * handled. We also explicitly state that we will close in
3838 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01003839 */
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003840 if (may_close || !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01003841 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3842
3843 /* Now we must adjust the response header :
3844 * - set "close" if may_keep and WANT_CLO
3845 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
3846 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
3847 *
3848 * Until we support the server-close mode, we'll only support the set "close".
3849 */
3850 if (may_keep && (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO)
3851 must_close = 1;
3852
3853 txn->flags |= TX_CON_HDR_PARS;
3854 }
3855
3856 /* We might have to check for "Connection:" if the server
3857 * returns a connection status that is not compatible with
3858 * the client's or with the config.
3859 */
3860 if ((txn->status >= 200) && must_close && (conn_cl|conn_ka)) {
3861 char *cur_ptr, *cur_end, *cur_next;
3862 int cur_idx, old_idx, delta, val;
3863 int must_delete;
3864 struct hdr_idx_elem *cur_hdr;
3865
3866 /* we just have to remove the headers if both sides are 1.0 */
3867 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
3868 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3869
3870 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
3871 cur_hdr = &txn->hdr_idx.v[cur_idx];
3872 cur_ptr = cur_next;
3873 cur_end = cur_ptr + cur_hdr->len;
3874 cur_next = cur_end + cur_hdr->cr + 1;
3875
3876 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3877 if (!val)
3878 continue;
3879
3880 /* 3 possibilities :
3881 * - we have already set "Connection: close" or we're in
3882 * HTTP/1.0, so we remove this line.
3883 * - we have not yet set "Connection: close", but this line
3884 * indicates close. We leave it untouched and set the flag.
3885 * - we have not yet set "Connection: close", and this line
3886 * indicates non-close. We replace it and set the flag.
3887 */
3888 if (must_delete) {
3889 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3890 http_msg_move_end(&txn->rsp, delta);
3891 cur_next += delta;
3892 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3893 txn->hdr_idx.used--;
3894 cur_hdr->len = 0;
3895 must_close = 0;
3896 } else {
3897 if (cur_end - cur_ptr - val != 5 ||
3898 strncasecmp(cur_ptr + val, "close", 5) != 0) {
3899 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3900 "close", 5);
3901 cur_next += delta;
3902 cur_hdr->len += delta;
3903 http_msg_move_end(&txn->rsp, delta);
3904 }
3905 must_delete = 1;
3906 must_close = 0;
3907 }
3908 } /* for loop */
3909 } /* if must close keep-alive */
3910
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003911 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003912 /*
3913 * 3: we will have to evaluate the filters.
3914 * As opposed to version 1.2, now they will be evaluated in the
3915 * filters order and not in the header order. This means that
3916 * each filter has to be validated among all headers.
3917 *
3918 * Filters are tried with ->be first, then with ->fe if it is
3919 * different from ->be.
3920 */
3921
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003922 cur_proxy = t->be;
3923 while (1) {
3924 struct proxy *rule_set = cur_proxy;
3925
3926 /* try headers filters */
3927 if (rule_set->rsp_exp != NULL) {
3928 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3929 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003930 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003931 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003932 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
3933 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003934 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003935 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02003936 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003937 txn->status = 502;
Willy Tarreau8e89b842009-10-18 23:56:35 +02003938 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003939 if (!(t->flags & SN_ERR_MASK))
3940 t->flags |= SN_ERR_PRXCOND;
3941 if (!(t->flags & SN_FINST_MASK))
3942 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02003943 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003944 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003945 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003946
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003947 /* has the response been denied ? */
3948 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01003949 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003950 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003951
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003952 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003953 if (t->listener->counters)
3954 t->listener->counters->denied_resp++;
3955
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003956 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003957 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003958
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003959 /* add response headers from the rule sets in the same order */
3960 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau816b9792009-09-15 21:25:21 +02003961 if (txn->status < 200)
3962 break;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003963 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003964 rule_set->rsp_add[cur_idx]) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003965 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003966 }
3967
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003968 /* check whether we're already working on the frontend */
3969 if (cur_proxy == t->fe)
3970 break;
3971 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003972 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003973
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003974 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01003975 * We may be facing a 1xx response (100 continue, 101 switching protocols),
3976 * in which case this is not the right response, and we're waiting for the
3977 * next one. Let's allow this response to go to the client and wait for the
3978 * next one.
3979 */
3980 if (txn->status < 200) {
3981 hdr_idx_init(&txn->hdr_idx);
3982 buffer_forward(rep, rep->lr - (rep->data + msg->som));
3983 msg->msg_state = HTTP_MSG_RPBEFORE;
3984 txn->status = 0;
3985 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
3986 return 1;
3987 }
3988
3989 /* we don't have any 1xx status code now */
3990
3991 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003992 * 4: check for server cookie.
3993 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01003994 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
3995 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003996 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003997
Willy Tarreaubaaee002006-06-26 02:48:02 +02003998
Willy Tarreaua15645d2007-03-18 16:22:39 +01003999 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004000 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004001 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004002 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004003 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004004
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004005 /*
4006 * 6: add server cookie in the response if needed
4007 */
4008 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004009 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004010 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004011
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004012 /* the server is known, it's not the one the client requested, we have to
4013 * insert a set-cookie here, except if we want to insert only on POST
4014 * requests and this one isn't. Note that servers which don't have cookies
4015 * (eg: some backup servers) will return a full cookie removal request.
4016 */
4017 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4018 t->be->cookie_name,
4019 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004020
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004021 if (t->be->cookie_domain)
4022 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004023
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004024 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004025 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004026 goto return_bad_resp;
4027 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004028
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004029 /* Here, we will tell an eventual cache on the client side that we don't
4030 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4031 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4032 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4033 */
4034 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004035
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004036 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4037
4038 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004039 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004040 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004041 }
4042 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004043
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004044 /*
4045 * 7: check if result will be cacheable with a cookie.
4046 * We'll block the response if security checks have caught
4047 * nasty things such as a cacheable cookie.
4048 */
4049 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4050 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004051 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004052
4053 /* we're in presence of a cacheable response containing
4054 * a set-cookie header. We'll block it as requested by
4055 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004056 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004057 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004058 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004059
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004060 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004061 if (t->listener->counters)
4062 t->listener->counters->denied_resp++;
4063
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004064 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4065 t->be->id, t->srv?t->srv->id:"<dispatch>");
4066 send_log(t->be, LOG_ALERT,
4067 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4068 t->be->id, t->srv?t->srv->id:"<dispatch>");
4069 goto return_srv_prx_502;
4070 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004071
4072 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004073 * 8: add "Connection: close" if needed and not yet set. This is
4074 * only needed for 1.1 responses since we know there is no other
4075 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004076 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004077 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004078 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004079 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004080 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004081 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004082 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004083
Willy Tarreaud98cf932009-12-27 22:54:55 +01004084 if (txn->flags & TX_RES_XFER_LEN)
4085 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004086
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004087 /*************************************************************
4088 * OK, that's finished for the headers. We have done what we *
4089 * could. Let's switch to the DATA state. *
4090 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004091
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004092 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004093
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004094 /* if the user wants to log as soon as possible, without counting
4095 * bytes from the server, then this is the right moment. We have
4096 * to temporarily assign bytes_out to log what we currently have.
4097 */
4098 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4099 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4100 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004101 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004102 t->logs.bytes_out = 0;
4103 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004104
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004105 /* Note: we must not try to cheat by jumping directly to DATA,
4106 * otherwise we would not let the client side wake up.
4107 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004108
Willy Tarreaudafde432008-08-17 01:00:46 +02004109 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004110 }
4111 return 0;
4112}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004113
Willy Tarreaud98cf932009-12-27 22:54:55 +01004114/* This function is an analyser which forwards response body (including chunk
4115 * sizes if any). It is called as soon as we must forward, even if we forward
4116 * zero byte. The only situation where it must not be called is when we're in
4117 * tunnel mode and we want to forward till the close. It's used both to forward
4118 * remaining data and to resync after end of body. It expects the msg_state to
4119 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4120 * read more data, or 1 once we can go on with next request or end the session.
4121 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4122 * bytes of pending data + the headers if not already done (between som and sov).
4123 * It eventually adjusts som to match sov after the data in between have been sent.
4124 */
4125int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4126{
4127 struct http_txn *txn = &s->txn;
4128 struct http_msg *msg = &s->txn.rsp;
4129
4130 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4131 return 0;
4132
4133 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4134 /* we have msg->col and msg->sov which both point to the first
4135 * byte of message body. msg->som still points to the beginning
4136 * of the message. We must save the body in req->lr because it
4137 * survives buffer re-alignments.
4138 */
4139 res->lr = res->data + msg->sov;
4140 if (txn->flags & TX_RES_TE_CHNK)
4141 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4142 else {
4143 msg->msg_state = HTTP_MSG_DATA;
4144 }
4145 }
4146
4147 /* we may already have some data pending */
4148 if (msg->hdr_content_len || msg->som != msg->sov) {
4149 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4150 msg->hdr_content_len = 0; /* don't forward that again */
4151 msg->som = msg->sov;
4152 }
4153
4154 while (1) {
4155 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
4156 /* read the chunk size and assign it to ->hdr_content_len, then
4157 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4158 */
4159 int ret = http_parse_chunk_size(res, msg);
4160
4161 if (!ret)
4162 goto missing_data;
4163 else if (ret < 0)
4164 goto return_bad_res;
4165 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
4166
4167 /* forward the chunk size as well as any pending data */
4168 if (msg->hdr_content_len || msg->som != msg->sov) {
4169 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4170 msg->hdr_content_len = 0; /* don't forward that again */
4171 msg->som = msg->sov;
4172 }
4173 }
4174 else if (msg->msg_state == HTTP_MSG_DATA) {
4175 /* must still forward */
4176 if (res->to_forward)
4177 return 0;
4178
4179 /* nothing left to forward */
4180 if (txn->flags & TX_RES_TE_CHNK)
4181 msg->msg_state = HTTP_MSG_DATA_CRLF;
4182 else {
4183 msg->msg_state = HTTP_MSG_DONE;
4184 }
4185 }
4186 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4187 /* we want the CRLF after the data */
4188 int ret;
4189
4190 if (!(res->flags & BF_OUT_EMPTY))
4191 return 0;
4192 /* The output pointer does not move anymore, next unsent data
4193 * are available at ->w. Let's save that.
4194 */
4195 res->lr = res->w;
4196 ret = http_skip_chunk_crlf(res, msg);
4197
4198 if (!ret)
4199 goto missing_data;
4200 else if (ret < 0)
4201 goto return_bad_res;
4202 /* we're in MSG_CHUNK_SIZE now */
4203 }
4204 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4205 int ret = http_forward_trailers(res, msg);
4206 if (ret == 0)
4207 goto missing_data;
4208 else if (ret < 0)
4209 goto return_bad_res;
4210 /* we're in HTTP_MSG_DONE now */
4211 }
4212 else if (msg->msg_state == HTTP_MSG_DONE) {
4213 if (!(s->req->flags & BF_OUT_EMPTY) || !(s->rep->flags & BF_OUT_EMPTY) ||
4214 s->req->to_forward || s->rep->to_forward)
4215 return 0;
4216
4217 if (txn->req.msg_state != HTTP_MSG_DONE && txn->req.msg_state != HTTP_MSG_ERROR)
4218 return 0;
4219
4220 /* when we support keep-alive or server-close modes, we'll have
4221 * to reset the transaction here.
4222 */
4223 s->req->flags &= ~BF_DONT_READ; /* re-enable reading on client side */
4224 break;
4225 }
4226 }
4227
4228 res->analysers &= ~an_bit;
4229 return 1;
4230
4231 missing_data:
4232 /* forward the chunk size as well as any pending data */
4233 if (msg->hdr_content_len || msg->som != msg->sov) {
4234 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4235 msg->hdr_content_len = 0; /* don't forward that again */
4236 msg->som = msg->sov;
4237 }
4238
4239 if (res->flags & BF_FULL)
4240 goto return_bad_res;
4241 /* the session handler will take care of timeouts and errors */
4242 return 0;
4243
4244 return_bad_res: /* let's centralize all bad resuests */
4245 txn->rsp.msg_state = HTTP_MSG_ERROR;
4246 txn->status = 502;
4247 stream_int_cond_close(res->cons, NULL);
4248
4249 res->analysers = 0;
4250 s->be->counters.failed_resp++;
4251 if (s->srv) {
4252 s->srv->counters.failed_resp++;
4253 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4254 }
4255
4256 if (!(s->flags & SN_ERR_MASK))
4257 s->flags |= SN_ERR_PRXCOND;
4258 if (!(s->flags & SN_FINST_MASK))
4259 s->flags |= SN_FINST_R;
4260 return 0;
4261}
4262
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004263/* Iterate the same filter through all request headers.
4264 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004265 * Since it can manage the switch to another backend, it updates the per-proxy
4266 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004267 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004268int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004269{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004270 char term;
4271 char *cur_ptr, *cur_end, *cur_next;
4272 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004273 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004274 struct hdr_idx_elem *cur_hdr;
4275 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004277 last_hdr = 0;
4278
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004279 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004280 old_idx = 0;
4281
4282 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004283 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004284 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004285 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004286 (exp->action == ACT_ALLOW ||
4287 exp->action == ACT_DENY ||
4288 exp->action == ACT_TARPIT))
4289 return 0;
4290
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004291 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004292 if (!cur_idx)
4293 break;
4294
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004295 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004296 cur_ptr = cur_next;
4297 cur_end = cur_ptr + cur_hdr->len;
4298 cur_next = cur_end + cur_hdr->cr + 1;
4299
4300 /* Now we have one header between cur_ptr and cur_end,
4301 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004302 */
4303
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004304 /* The annoying part is that pattern matching needs
4305 * that we modify the contents to null-terminate all
4306 * strings before testing them.
4307 */
4308
4309 term = *cur_end;
4310 *cur_end = '\0';
4311
4312 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4313 switch (exp->action) {
4314 case ACT_SETBE:
4315 /* It is not possible to jump a second time.
4316 * FIXME: should we return an HTTP/500 here so that
4317 * the admin knows there's a problem ?
4318 */
4319 if (t->be != t->fe)
4320 break;
4321
4322 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004323 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004324 last_hdr = 1;
4325 break;
4326
4327 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004328 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004329 last_hdr = 1;
4330 break;
4331
4332 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004333 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004334 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004335
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004336 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004337 if (t->listener->counters)
4338 t->listener->counters->denied_resp++;
4339
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004340 break;
4341
4342 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004343 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004344 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004345
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004346 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004347 if (t->listener->counters)
4348 t->listener->counters->denied_resp++;
4349
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004350 break;
4351
4352 case ACT_REPLACE:
4353 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4354 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4355 /* FIXME: if the user adds a newline in the replacement, the
4356 * index will not be recalculated for now, and the new line
4357 * will not be counted as a new header.
4358 */
4359
4360 cur_end += delta;
4361 cur_next += delta;
4362 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004363 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004364 break;
4365
4366 case ACT_REMOVE:
4367 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4368 cur_next += delta;
4369
Willy Tarreaufa355d42009-11-29 18:12:29 +01004370 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004371 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4372 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004373 cur_hdr->len = 0;
4374 cur_end = NULL; /* null-term has been rewritten */
4375 break;
4376
4377 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004378 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004379 if (cur_end)
4380 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004382 /* keep the link from this header to next one in case of later
4383 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004384 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004385 old_idx = cur_idx;
4386 }
4387 return 0;
4388}
4389
4390
4391/* Apply the filter to the request line.
4392 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4393 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004394 * Since it can manage the switch to another backend, it updates the per-proxy
4395 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004396 */
4397int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4398{
4399 char term;
4400 char *cur_ptr, *cur_end;
4401 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004402 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004403 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004404
Willy Tarreau58f10d72006-12-04 02:26:12 +01004405
Willy Tarreau3d300592007-03-18 18:34:41 +01004406 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004407 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004408 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004409 (exp->action == ACT_ALLOW ||
4410 exp->action == ACT_DENY ||
4411 exp->action == ACT_TARPIT))
4412 return 0;
4413 else if (exp->action == ACT_REMOVE)
4414 return 0;
4415
4416 done = 0;
4417
Willy Tarreau9cdde232007-05-02 20:58:19 +02004418 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004419 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004420
4421 /* Now we have the request line between cur_ptr and cur_end */
4422
4423 /* The annoying part is that pattern matching needs
4424 * that we modify the contents to null-terminate all
4425 * strings before testing them.
4426 */
4427
4428 term = *cur_end;
4429 *cur_end = '\0';
4430
4431 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4432 switch (exp->action) {
4433 case ACT_SETBE:
4434 /* It is not possible to jump a second time.
4435 * FIXME: should we return an HTTP/500 here so that
4436 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004437 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004438 if (t->be != t->fe)
4439 break;
4440
4441 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004442 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004443 done = 1;
4444 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004445
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004446 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004447 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004448 done = 1;
4449 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004451 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004452 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004453
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004454 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004455 if (t->listener->counters)
4456 t->listener->counters->denied_resp++;
4457
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004458 done = 1;
4459 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004461 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004462 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004463
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004464 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004465 if (t->listener->counters)
4466 t->listener->counters->denied_resp++;
4467
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004468 done = 1;
4469 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004471 case ACT_REPLACE:
4472 *cur_end = term; /* restore the string terminator */
4473 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4474 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4475 /* FIXME: if the user adds a newline in the replacement, the
4476 * index will not be recalculated for now, and the new line
4477 * will not be counted as a new header.
4478 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004479
Willy Tarreaufa355d42009-11-29 18:12:29 +01004480 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004481 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004482
Willy Tarreau9cdde232007-05-02 20:58:19 +02004483 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004484 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004485 HTTP_MSG_RQMETH,
4486 cur_ptr, cur_end + 1,
4487 NULL, NULL);
4488 if (unlikely(!cur_end))
4489 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004491 /* we have a full request and we know that we have either a CR
4492 * or an LF at <ptr>.
4493 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004494 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4495 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004496 /* there is no point trying this regex on headers */
4497 return 1;
4498 }
4499 }
4500 *cur_end = term; /* restore the string terminator */
4501 return done;
4502}
Willy Tarreau97de6242006-12-27 17:18:38 +01004503
Willy Tarreau58f10d72006-12-04 02:26:12 +01004504
Willy Tarreau58f10d72006-12-04 02:26:12 +01004505
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004506/*
4507 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4508 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004509 * unparsable request. Since it can manage the switch to another backend, it
4510 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004511 */
4512int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4513{
Willy Tarreau3d300592007-03-18 18:34:41 +01004514 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004515 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004516 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004517 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004518
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004519 /*
4520 * The interleaving of transformations and verdicts
4521 * makes it difficult to decide to continue or stop
4522 * the evaluation.
4523 */
4524
Willy Tarreau3d300592007-03-18 18:34:41 +01004525 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004526 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4527 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4528 exp = exp->next;
4529 continue;
4530 }
4531
4532 /* Apply the filter to the request line. */
4533 ret = apply_filter_to_req_line(t, req, exp);
4534 if (unlikely(ret < 0))
4535 return -1;
4536
4537 if (likely(ret == 0)) {
4538 /* The filter did not match the request, it can be
4539 * iterated through all headers.
4540 */
4541 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004542 }
4543 exp = exp->next;
4544 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004545 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004546}
4547
4548
Willy Tarreaua15645d2007-03-18 16:22:39 +01004549
Willy Tarreau58f10d72006-12-04 02:26:12 +01004550/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004551 * Try to retrieve the server associated to the appsession.
4552 * If the server is found, it's assigned to the session.
4553 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01004554void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004555 struct http_txn *txn = &t->txn;
4556 appsess *asession = NULL;
4557 char *sessid_temp = NULL;
4558
Cyril Bontéb21570a2009-11-29 20:04:48 +01004559 if (len > t->be->appsession_len) {
4560 len = t->be->appsession_len;
4561 }
4562
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004563 if (t->be->options2 & PR_O2_AS_REQL) {
4564 /* request-learn option is enabled : store the sessid in the session for future use */
4565 if (t->sessid != NULL) {
4566 /* free previously allocated memory as we don't need the session id found in the URL anymore */
4567 pool_free2(apools.sessid, t->sessid);
4568 }
4569
4570 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
4571 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4572 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4573 return;
4574 }
4575
Cyril Bontéb21570a2009-11-29 20:04:48 +01004576 memcpy(t->sessid, buf, len);
4577 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004578 }
4579
4580 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
4581 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4582 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4583 return;
4584 }
4585
Cyril Bontéb21570a2009-11-29 20:04:48 +01004586 memcpy(sessid_temp, buf, len);
4587 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004588
4589 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
4590 /* free previously allocated memory */
4591 pool_free2(apools.sessid, sessid_temp);
4592
4593 if (asession != NULL) {
4594 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
4595 if (!(t->be->options2 & PR_O2_AS_REQL))
4596 asession->request_count++;
4597
4598 if (asession->serverid != NULL) {
4599 struct server *srv = t->be->srv;
4600 while (srv) {
4601 if (strcmp(srv->id, asession->serverid) == 0) {
4602 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
4603 /* we found the server and it's usable */
4604 txn->flags &= ~TX_CK_MASK;
4605 txn->flags |= TX_CK_VALID;
4606 t->flags |= SN_DIRECT | SN_ASSIGNED;
4607 t->srv = srv;
4608 break;
4609 } else {
4610 txn->flags &= ~TX_CK_MASK;
4611 txn->flags |= TX_CK_DOWN;
4612 }
4613 }
4614 srv = srv->next;
4615 }
4616 }
4617 }
4618}
4619
4620/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004621 * Manage client-side cookie. It can impact performance by about 2% so it is
4622 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004623 */
4624void manage_client_side_cookies(struct session *t, struct buffer *req)
4625{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004626 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004627 char *p1, *p2, *p3, *p4;
4628 char *del_colon, *del_cookie, *colon;
4629 int app_cookies;
4630
Willy Tarreau58f10d72006-12-04 02:26:12 +01004631 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004632 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004633
Willy Tarreau2a324282006-12-05 00:05:46 +01004634 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004635 * we start with the start line.
4636 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004637 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004638 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004639
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004640 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004641 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004642 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004643
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004644 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004645 cur_ptr = cur_next;
4646 cur_end = cur_ptr + cur_hdr->len;
4647 cur_next = cur_end + cur_hdr->cr + 1;
4648
4649 /* We have one full header between cur_ptr and cur_end, and the
4650 * next header starts at cur_next. We're only interested in
4651 * "Cookie:" headers.
4652 */
4653
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004654 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4655 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004656 old_idx = cur_idx;
4657 continue;
4658 }
4659
4660 /* Now look for cookies. Conforming to RFC2109, we have to support
4661 * attributes whose name begin with a '$', and associate them with
4662 * the right cookie, if we want to delete this cookie.
4663 * So there are 3 cases for each cookie read :
4664 * 1) it's a special attribute, beginning with a '$' : ignore it.
4665 * 2) it's a server id cookie that we *MAY* want to delete : save
4666 * some pointers on it (last semi-colon, beginning of cookie...)
4667 * 3) it's an application cookie : we *MAY* have to delete a previous
4668 * "special" cookie.
4669 * At the end of loop, if a "special" cookie remains, we may have to
4670 * remove it. If no application cookie persists in the header, we
4671 * *MUST* delete it
4672 */
4673
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004674 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004675
Willy Tarreau58f10d72006-12-04 02:26:12 +01004676 /* del_cookie == NULL => nothing to be deleted */
4677 del_colon = del_cookie = NULL;
4678 app_cookies = 0;
4679
4680 while (p1 < cur_end) {
4681 /* skip spaces and colons, but keep an eye on these ones */
4682 while (p1 < cur_end) {
4683 if (*p1 == ';' || *p1 == ',')
4684 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004685 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004686 break;
4687 p1++;
4688 }
4689
4690 if (p1 == cur_end)
4691 break;
4692
4693 /* p1 is at the beginning of the cookie name */
4694 p2 = p1;
4695 while (p2 < cur_end && *p2 != '=')
4696 p2++;
4697
4698 if (p2 == cur_end)
4699 break;
4700
4701 p3 = p2 + 1; /* skips the '=' sign */
4702 if (p3 == cur_end)
4703 break;
4704
4705 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004706 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004707 p4++;
4708
4709 /* here, we have the cookie name between p1 and p2,
4710 * and its value between p3 and p4.
4711 * we can process it :
4712 *
4713 * Cookie: NAME=VALUE;
4714 * | || || |
4715 * | || || +--> p4
4716 * | || |+-------> p3
4717 * | || +--------> p2
4718 * | |+------------> p1
4719 * | +-------------> colon
4720 * +--------------------> cur_ptr
4721 */
4722
4723 if (*p1 == '$') {
4724 /* skip this one */
4725 }
4726 else {
4727 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004728 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004729 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004730 (p4 - p1 >= t->fe->capture_namelen) &&
4731 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004732 int log_len = p4 - p1;
4733
Willy Tarreau086b3b42007-05-13 21:45:51 +02004734 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004735 Alert("HTTP logging : out of memory.\n");
4736 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004737 if (log_len > t->fe->capture_len)
4738 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004739 memcpy(txn->cli_cookie, p1, log_len);
4740 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004741 }
4742 }
4743
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004744 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4745 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004746 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004747 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004748 char *delim;
4749
4750 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4751 * have the server ID betweek p3 and delim, and the original cookie between
4752 * delim+1 and p4. Otherwise, delim==p4 :
4753 *
4754 * Cookie: NAME=SRV~VALUE;
4755 * | || || | |
4756 * | || || | +--> p4
4757 * | || || +--------> delim
4758 * | || |+-----------> p3
4759 * | || +------------> p2
4760 * | |+----------------> p1
4761 * | +-----------------> colon
4762 * +------------------------> cur_ptr
4763 */
4764
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004765 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004766 for (delim = p3; delim < p4; delim++)
4767 if (*delim == COOKIE_DELIM)
4768 break;
4769 }
4770 else
4771 delim = p4;
4772
4773
4774 /* Here, we'll look for the first running server which supports the cookie.
4775 * This allows to share a same cookie between several servers, for example
4776 * to dedicate backup servers to specific servers only.
4777 * However, to prevent clients from sticking to cookie-less backup server
4778 * when they have incidentely learned an empty cookie, we simply ignore
4779 * empty cookies and mark them as invalid.
4780 */
4781 if (delim == p3)
4782 srv = NULL;
4783
4784 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004785 if (srv->cookie && (srv->cklen == delim - p3) &&
4786 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004787 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004788 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004789 txn->flags &= ~TX_CK_MASK;
4790 txn->flags |= TX_CK_VALID;
4791 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004792 t->srv = srv;
4793 break;
4794 } else {
4795 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004796 txn->flags &= ~TX_CK_MASK;
4797 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004798 }
4799 }
4800 srv = srv->next;
4801 }
4802
Willy Tarreau3d300592007-03-18 18:34:41 +01004803 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004804 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004805 txn->flags &= ~TX_CK_MASK;
4806 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004807 }
4808
4809 /* depending on the cookie mode, we may have to either :
4810 * - delete the complete cookie if we're in insert+indirect mode, so that
4811 * the server never sees it ;
4812 * - remove the server id from the cookie value, and tag the cookie as an
4813 * application cookie so that it does not get accidentely removed later,
4814 * if we're in cookie prefix mode
4815 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004816 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004817 int delta; /* negative */
4818
4819 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4820 p4 += delta;
4821 cur_end += delta;
4822 cur_next += delta;
4823 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004824 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004825
4826 del_cookie = del_colon = NULL;
4827 app_cookies++; /* protect the header from deletion */
4828 }
4829 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004830 (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 +01004831 del_cookie = p1;
4832 del_colon = colon;
4833 }
4834 } else {
4835 /* now we know that we must keep this cookie since it's
4836 * not ours. But if we wanted to delete our cookie
4837 * earlier, we cannot remove the complete header, but we
4838 * can remove the previous block itself.
4839 */
4840 app_cookies++;
4841
4842 if (del_cookie != NULL) {
4843 int delta; /* negative */
4844
4845 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4846 p4 += delta;
4847 cur_end += delta;
4848 cur_next += delta;
4849 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004850 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004851 del_cookie = del_colon = NULL;
4852 }
4853 }
4854
Cyril Bontéb21570a2009-11-29 20:04:48 +01004855 if (t->be->appsession_name != NULL) {
4856 int cmp_len, value_len;
4857 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004858
Cyril Bontéb21570a2009-11-29 20:04:48 +01004859 if (t->be->options2 & PR_O2_AS_PFX) {
4860 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
4861 value_begin = p1 + t->be->appsession_name_len;
4862 value_len = p4 - p1 - t->be->appsession_name_len;
4863 } else {
4864 cmp_len = p2 - p1;
4865 value_begin = p3;
4866 value_len = p4 - p3;
4867 }
4868
4869 /* let's see if the cookie is our appcookie */
4870 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
4871 /* Cool... it's the right one */
4872 manage_client_side_appsession(t, value_begin, value_len);
4873 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004874#if defined(DEBUG_HASH)
4875 Alert("manage_client_side_cookies\n");
4876 appsession_hash_dump(&(t->be->htbl_proxy));
4877#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004878 }/* end if ((t->proxy->appsession_name != NULL) ... */
4879 }
4880
4881 /* we'll have to look for another cookie ... */
4882 p1 = p4;
4883 } /* while (p1 < cur_end) */
4884
4885 /* There's no more cookie on this line.
4886 * We may have marked the last one(s) for deletion.
4887 * We must do this now in two ways :
4888 * - if there is no app cookie, we simply delete the header ;
4889 * - if there are app cookies, we must delete the end of the
4890 * string properly, including the colon/semi-colon before
4891 * the cookie name.
4892 */
4893 if (del_cookie != NULL) {
4894 int delta;
4895 if (app_cookies) {
4896 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4897 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004898 cur_hdr->len += delta;
4899 } else {
4900 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004901
4902 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004903 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4904 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004905 cur_hdr->len = 0;
4906 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004907 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004908 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004909 }
4910
4911 /* keep the link from this header to next one */
4912 old_idx = cur_idx;
4913 } /* end of cookie processing on this header */
4914}
4915
4916
Willy Tarreaua15645d2007-03-18 16:22:39 +01004917/* Iterate the same filter through all response headers contained in <rtr>.
4918 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4919 */
4920int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4921{
4922 char term;
4923 char *cur_ptr, *cur_end, *cur_next;
4924 int cur_idx, old_idx, last_hdr;
4925 struct http_txn *txn = &t->txn;
4926 struct hdr_idx_elem *cur_hdr;
4927 int len, delta;
4928
4929 last_hdr = 0;
4930
4931 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4932 old_idx = 0;
4933
4934 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004935 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004936 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004937 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004938 (exp->action == ACT_ALLOW ||
4939 exp->action == ACT_DENY))
4940 return 0;
4941
4942 cur_idx = txn->hdr_idx.v[old_idx].next;
4943 if (!cur_idx)
4944 break;
4945
4946 cur_hdr = &txn->hdr_idx.v[cur_idx];
4947 cur_ptr = cur_next;
4948 cur_end = cur_ptr + cur_hdr->len;
4949 cur_next = cur_end + cur_hdr->cr + 1;
4950
4951 /* Now we have one header between cur_ptr and cur_end,
4952 * and the next header starts at cur_next.
4953 */
4954
4955 /* The annoying part is that pattern matching needs
4956 * that we modify the contents to null-terminate all
4957 * strings before testing them.
4958 */
4959
4960 term = *cur_end;
4961 *cur_end = '\0';
4962
4963 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4964 switch (exp->action) {
4965 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004966 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004967 last_hdr = 1;
4968 break;
4969
4970 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004971 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004972 last_hdr = 1;
4973 break;
4974
4975 case ACT_REPLACE:
4976 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4977 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4978 /* FIXME: if the user adds a newline in the replacement, the
4979 * index will not be recalculated for now, and the new line
4980 * will not be counted as a new header.
4981 */
4982
4983 cur_end += delta;
4984 cur_next += delta;
4985 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004986 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004987 break;
4988
4989 case ACT_REMOVE:
4990 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4991 cur_next += delta;
4992
Willy Tarreaufa355d42009-11-29 18:12:29 +01004993 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004994 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4995 txn->hdr_idx.used--;
4996 cur_hdr->len = 0;
4997 cur_end = NULL; /* null-term has been rewritten */
4998 break;
4999
5000 }
5001 }
5002 if (cur_end)
5003 *cur_end = term; /* restore the string terminator */
5004
5005 /* keep the link from this header to next one in case of later
5006 * removal of next header.
5007 */
5008 old_idx = cur_idx;
5009 }
5010 return 0;
5011}
5012
5013
5014/* Apply the filter to the status line in the response buffer <rtr>.
5015 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5016 * or -1 if a replacement resulted in an invalid status line.
5017 */
5018int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5019{
5020 char term;
5021 char *cur_ptr, *cur_end;
5022 int done;
5023 struct http_txn *txn = &t->txn;
5024 int len, delta;
5025
5026
Willy Tarreau3d300592007-03-18 18:34:41 +01005027 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005028 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005029 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005030 (exp->action == ACT_ALLOW ||
5031 exp->action == ACT_DENY))
5032 return 0;
5033 else if (exp->action == ACT_REMOVE)
5034 return 0;
5035
5036 done = 0;
5037
Willy Tarreau9cdde232007-05-02 20:58:19 +02005038 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005039 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5040
5041 /* Now we have the status line between cur_ptr and cur_end */
5042
5043 /* The annoying part is that pattern matching needs
5044 * that we modify the contents to null-terminate all
5045 * strings before testing them.
5046 */
5047
5048 term = *cur_end;
5049 *cur_end = '\0';
5050
5051 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5052 switch (exp->action) {
5053 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005054 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005055 done = 1;
5056 break;
5057
5058 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005059 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005060 done = 1;
5061 break;
5062
5063 case ACT_REPLACE:
5064 *cur_end = term; /* restore the string terminator */
5065 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5066 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5067 /* FIXME: if the user adds a newline in the replacement, the
5068 * index will not be recalculated for now, and the new line
5069 * will not be counted as a new header.
5070 */
5071
Willy Tarreaufa355d42009-11-29 18:12:29 +01005072 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005073 cur_end += delta;
5074
Willy Tarreau9cdde232007-05-02 20:58:19 +02005075 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005076 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005077 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005078 cur_ptr, cur_end + 1,
5079 NULL, NULL);
5080 if (unlikely(!cur_end))
5081 return -1;
5082
5083 /* we have a full respnse and we know that we have either a CR
5084 * or an LF at <ptr>.
5085 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005086 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005087 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5088 /* there is no point trying this regex on headers */
5089 return 1;
5090 }
5091 }
5092 *cur_end = term; /* restore the string terminator */
5093 return done;
5094}
5095
5096
5097
5098/*
5099 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5100 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5101 * unparsable response.
5102 */
5103int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5104{
Willy Tarreau3d300592007-03-18 18:34:41 +01005105 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005106 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005107 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005108 int ret;
5109
5110 /*
5111 * The interleaving of transformations and verdicts
5112 * makes it difficult to decide to continue or stop
5113 * the evaluation.
5114 */
5115
Willy Tarreau3d300592007-03-18 18:34:41 +01005116 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005117 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5118 exp->action == ACT_PASS)) {
5119 exp = exp->next;
5120 continue;
5121 }
5122
5123 /* Apply the filter to the status line. */
5124 ret = apply_filter_to_sts_line(t, rtr, exp);
5125 if (unlikely(ret < 0))
5126 return -1;
5127
5128 if (likely(ret == 0)) {
5129 /* The filter did not match the response, it can be
5130 * iterated through all headers.
5131 */
5132 apply_filter_to_resp_headers(t, rtr, exp);
5133 }
5134 exp = exp->next;
5135 }
5136 return 0;
5137}
5138
5139
5140
5141/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005142 * Manage server-side cookies. It can impact performance by about 2% so it is
5143 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005144 */
5145void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5146{
5147 struct http_txn *txn = &t->txn;
5148 char *p1, *p2, *p3, *p4;
5149
Willy Tarreaua15645d2007-03-18 16:22:39 +01005150 char *cur_ptr, *cur_end, *cur_next;
5151 int cur_idx, old_idx, delta;
5152
Willy Tarreaua15645d2007-03-18 16:22:39 +01005153 /* Iterate through the headers.
5154 * we start with the start line.
5155 */
5156 old_idx = 0;
5157 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5158
5159 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5160 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005161 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005162
5163 cur_hdr = &txn->hdr_idx.v[cur_idx];
5164 cur_ptr = cur_next;
5165 cur_end = cur_ptr + cur_hdr->len;
5166 cur_next = cur_end + cur_hdr->cr + 1;
5167
5168 /* We have one full header between cur_ptr and cur_end, and the
5169 * next header starts at cur_next. We're only interested in
5170 * "Cookie:" headers.
5171 */
5172
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005173 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5174 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005175 old_idx = cur_idx;
5176 continue;
5177 }
5178
5179 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005180 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005181
5182
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005183 /* maybe we only wanted to see if there was a set-cookie. Note that
5184 * the cookie capture is declared in the fronend.
5185 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005186 if (t->be->cookie_name == NULL &&
5187 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005188 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005189 return;
5190
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005191 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005192
5193 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005194 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5195 break;
5196
5197 /* p1 is at the beginning of the cookie name */
5198 p2 = p1;
5199
5200 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5201 p2++;
5202
5203 if (p2 == cur_end || *p2 == ';') /* next cookie */
5204 break;
5205
5206 p3 = p2 + 1; /* skip the '=' sign */
5207 if (p3 == cur_end)
5208 break;
5209
5210 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005211 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005212 p4++;
5213
5214 /* here, we have the cookie name between p1 and p2,
5215 * and its value between p3 and p4.
5216 * we can process it.
5217 */
5218
5219 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005220 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005221 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005222 (p4 - p1 >= t->fe->capture_namelen) &&
5223 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005224 int log_len = p4 - p1;
5225
Willy Tarreau086b3b42007-05-13 21:45:51 +02005226 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005227 Alert("HTTP logging : out of memory.\n");
5228 }
5229
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005230 if (log_len > t->fe->capture_len)
5231 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005232 memcpy(txn->srv_cookie, p1, log_len);
5233 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005234 }
5235
5236 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005237 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5238 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005239 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005240 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005241
5242 /* If the cookie is in insert mode on a known server, we'll delete
5243 * this occurrence because we'll insert another one later.
5244 * We'll delete it too if the "indirect" option is set and we're in
5245 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005246 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5247 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005248 /* this header must be deleted */
5249 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5250 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5251 txn->hdr_idx.used--;
5252 cur_hdr->len = 0;
5253 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005254 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005255
Willy Tarreau3d300592007-03-18 18:34:41 +01005256 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005257 }
5258 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005259 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005260 /* replace bytes p3->p4 with the cookie name associated
5261 * with this server since we know it.
5262 */
5263 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5264 cur_hdr->len += delta;
5265 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005266 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005267
Willy Tarreau3d300592007-03-18 18:34:41 +01005268 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005269 }
5270 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005271 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005272 /* insert the cookie name associated with this server
5273 * before existing cookie, and insert a delimitor between them..
5274 */
5275 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5276 cur_hdr->len += delta;
5277 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005278 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005279
5280 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005281 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005282 }
5283 }
5284 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005285 else if (t->be->appsession_name != NULL) {
5286 int cmp_len, value_len;
5287 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005288
Cyril Bontéb21570a2009-11-29 20:04:48 +01005289 if (t->be->options2 & PR_O2_AS_PFX) {
5290 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5291 value_begin = p1 + t->be->appsession_name_len;
5292 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5293 } else {
5294 cmp_len = p2 - p1;
5295 value_begin = p3;
5296 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005297 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005298
5299 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5300 /* Cool... it's the right one */
5301 if (t->sessid != NULL) {
5302 /* free previously allocated memory as we don't need it anymore */
5303 pool_free2(apools.sessid, t->sessid);
5304 }
5305 /* Store the sessid in the session for future use */
5306 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5307 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5308 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5309 return;
5310 }
5311 memcpy(t->sessid, value_begin, value_len);
5312 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005313 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005314 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005315 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5316 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005317 /* keep the link from this header to next one */
5318 old_idx = cur_idx;
5319 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005320
5321 if (t->sessid != NULL) {
5322 appsess *asession = NULL;
5323 /* only do insert, if lookup fails */
5324 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5325 if (asession == NULL) {
5326 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5327 Alert("Not enough Memory process_srv():asession:calloc().\n");
5328 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5329 return;
5330 }
5331 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5332 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5333 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5334 return;
5335 }
5336 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5337 asession->sessid[t->be->appsession_len] = 0;
5338
5339 size_t server_id_len = strlen(t->srv->id) + 1;
5340 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5341 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5342 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5343 return;
5344 }
5345 asession->serverid[0] = '\0';
5346 memcpy(asession->serverid, t->srv->id, server_id_len);
5347
5348 asession->request_count = 0;
5349 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5350 }
5351
5352 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5353 asession->request_count++;
5354 }
5355
5356#if defined(DEBUG_HASH)
5357 Alert("manage_server_side_cookies\n");
5358 appsession_hash_dump(&(t->be->htbl_proxy));
5359#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005360}
5361
5362
5363
5364/*
5365 * Check if response is cacheable or not. Updates t->flags.
5366 */
5367void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5368{
5369 struct http_txn *txn = &t->txn;
5370 char *p1, *p2;
5371
5372 char *cur_ptr, *cur_end, *cur_next;
5373 int cur_idx;
5374
Willy Tarreau5df51872007-11-25 16:20:08 +01005375 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005376 return;
5377
5378 /* Iterate through the headers.
5379 * we start with the start line.
5380 */
5381 cur_idx = 0;
5382 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5383
5384 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5385 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005386 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005387
5388 cur_hdr = &txn->hdr_idx.v[cur_idx];
5389 cur_ptr = cur_next;
5390 cur_end = cur_ptr + cur_hdr->len;
5391 cur_next = cur_end + cur_hdr->cr + 1;
5392
5393 /* We have one full header between cur_ptr and cur_end, and the
5394 * next header starts at cur_next. We're only interested in
5395 * "Cookie:" headers.
5396 */
5397
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005398 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5399 if (val) {
5400 if ((cur_end - (cur_ptr + val) >= 8) &&
5401 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5402 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5403 return;
5404 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005405 }
5406
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005407 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5408 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005409 continue;
5410
5411 /* OK, right now we know we have a cache-control header at cur_ptr */
5412
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005413 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005414
5415 if (p1 >= cur_end) /* no more info */
5416 continue;
5417
5418 /* p1 is at the beginning of the value */
5419 p2 = p1;
5420
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005421 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005422 p2++;
5423
5424 /* we have a complete value between p1 and p2 */
5425 if (p2 < cur_end && *p2 == '=') {
5426 /* we have something of the form no-cache="set-cookie" */
5427 if ((cur_end - p1 >= 21) &&
5428 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5429 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005430 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005431 continue;
5432 }
5433
5434 /* OK, so we know that either p2 points to the end of string or to a comma */
5435 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5436 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5437 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5438 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005439 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005440 return;
5441 }
5442
5443 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005444 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005445 continue;
5446 }
5447 }
5448}
5449
5450
Willy Tarreau58f10d72006-12-04 02:26:12 +01005451/*
5452 * Try to retrieve a known appsession in the URI, then the associated server.
5453 * If the server is found, it's assigned to the session.
5454 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005455void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005456{
Cyril Bontéb21570a2009-11-29 20:04:48 +01005457 char *end_params, *first_param, *cur_param, *next_param;
5458 char separator;
5459 int value_len;
5460
5461 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005462
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005463 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01005464 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005465 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005466 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005467
Cyril Bontéb21570a2009-11-29 20:04:48 +01005468 first_param = NULL;
5469 switch (mode) {
5470 case PR_O2_AS_M_PP:
5471 first_param = memchr(begin, ';', len);
5472 break;
5473 case PR_O2_AS_M_QS:
5474 first_param = memchr(begin, '?', len);
5475 break;
5476 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005477
Cyril Bontéb21570a2009-11-29 20:04:48 +01005478 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005479 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005480 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005481
Cyril Bontéb21570a2009-11-29 20:04:48 +01005482 switch (mode) {
5483 case PR_O2_AS_M_PP:
5484 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
5485 end_params = (char *) begin + len;
5486 }
5487 separator = ';';
5488 break;
5489 case PR_O2_AS_M_QS:
5490 end_params = (char *) begin + len;
5491 separator = '&';
5492 break;
5493 default:
5494 /* unknown mode, shouldn't happen */
5495 return;
5496 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005497
Cyril Bontéb21570a2009-11-29 20:04:48 +01005498 cur_param = next_param = end_params;
5499 while (cur_param > first_param) {
5500 cur_param--;
5501 if ((cur_param[0] == separator) || (cur_param == first_param)) {
5502 /* let's see if this is the appsession parameter */
5503 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
5504 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
5505 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
5506 /* Cool... it's the right one */
5507 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
5508 value_len = MIN(t->be->appsession_len, next_param - cur_param);
5509 if (value_len > 0) {
5510 manage_client_side_appsession(t, cur_param, value_len);
5511 }
5512 break;
5513 }
5514 next_param = cur_param;
5515 }
5516 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005517#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005518 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005519 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005520#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005521}
5522
5523
Willy Tarreaub2513902006-12-17 14:52:38 +01005524/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005525 * In a GET or HEAD request, check if the requested URI matches the stats uri
5526 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005527 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005528 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005529 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005530 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005531 *
5532 * Returns 1 if the session's state changes, otherwise 0.
5533 */
5534int stats_check_uri_auth(struct session *t, struct proxy *backend)
5535{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005536 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005537 struct uri_auth *uri_auth = backend->uri_auth;
5538 struct user_auth *user;
5539 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005540 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005541
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005542 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5543
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005544 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005545 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005546 return 0;
5547
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005548 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005549
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005550 /* the URI is in h */
5551 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005552 return 0;
5553
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005554 h += uri_auth->uri_len;
5555 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5556 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005557 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005558 break;
5559 }
5560 h++;
5561 }
5562
5563 if (uri_auth->refresh) {
5564 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5565 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5566 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005567 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005568 break;
5569 }
5570 h++;
5571 }
5572 }
5573
Willy Tarreau55bb8452007-10-17 18:44:57 +02005574 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5575 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5576 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005577 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005578 break;
5579 }
5580 h++;
5581 }
5582
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005583 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5584
Willy Tarreaub2513902006-12-17 14:52:38 +01005585 /* we are in front of a interceptable URI. Let's check
5586 * if there's an authentication and if it's valid.
5587 */
5588 user = uri_auth->users;
5589 if (!user) {
5590 /* no user auth required, it's OK */
5591 authenticated = 1;
5592 } else {
5593 authenticated = 0;
5594
5595 /* a user list is defined, we have to check.
5596 * skip 21 chars for "Authorization: Basic ".
5597 */
5598
5599 /* FIXME: this should move to an earlier place */
5600 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005601 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5602 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5603 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005604 if (len > 14 &&
5605 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02005606 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01005607 break;
5608 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005609 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005610 }
5611
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005612 if (txn->auth_hdr.len < 21 ||
5613 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005614 user = NULL;
5615
5616 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005617 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5618 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005619 user->user_pwd, user->user_len)) {
5620 authenticated = 1;
5621 break;
5622 }
5623 user = user->next;
5624 }
5625 }
5626
5627 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005628 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005629
5630 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005631 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
5632 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005633 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01005634 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005635 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005636 if (!(t->flags & SN_ERR_MASK))
5637 t->flags |= SN_ERR_PRXCOND;
5638 if (!(t->flags & SN_FINST_MASK))
5639 t->flags |= SN_FINST_R;
5640 return 1;
5641 }
5642
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005643 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005644 * data.
5645 */
Willy Tarreau70089872008-06-13 21:12:51 +02005646 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005647 t->data_source = DATA_SRC_STATS;
5648 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005649 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005650 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
5651 t->rep->prod->private = t;
5652 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005653 return 1;
5654}
5655
Willy Tarreau4076a152009-04-02 15:18:36 +02005656/*
5657 * Capture a bad request or response and archive it in the proxy's structure.
5658 */
5659void http_capture_bad_message(struct error_snapshot *es, struct session *s,
5660 struct buffer *buf, struct http_msg *msg,
5661 struct proxy *other_end)
5662{
Willy Tarreau2df8d712009-05-01 11:33:17 +02005663 es->len = buf->r - (buf->data + msg->som);
5664 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02005665 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02005666 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02005667 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02005668 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02005669 es->when = date; // user-visible date
5670 es->sid = s->uniq_id;
5671 es->srv = s->srv;
5672 es->oe = other_end;
5673 es->src = s->cli_addr;
5674}
Willy Tarreaub2513902006-12-17 14:52:38 +01005675
Willy Tarreaubaaee002006-06-26 02:48:02 +02005676/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005677 * Print a debug line with a header
5678 */
5679void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5680{
5681 int len, max;
5682 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005683 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005684 max = end - start;
5685 UBOUND(max, sizeof(trash) - len - 1);
5686 len += strlcpy2(trash + len, start, max + 1);
5687 trash[len++] = '\n';
5688 write(1, trash, len);
5689}
5690
Willy Tarreau0937bc42009-12-22 15:03:09 +01005691/*
5692 * Initialize a new HTTP transaction for session <s>. It is assumed that all
5693 * the required fields are properly allocated and that we only need to (re)init
5694 * them. This should be used before processing any new request.
5695 */
5696void http_init_txn(struct session *s)
5697{
5698 struct http_txn *txn = &s->txn;
5699 struct proxy *fe = s->fe;
5700
5701 txn->flags = 0;
5702 txn->status = -1;
5703
5704 txn->req.sol = txn->req.eol = NULL;
5705 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
5706 txn->rsp.sol = txn->rsp.eol = NULL;
5707 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
5708 txn->req.hdr_content_len = 0LL;
5709 txn->rsp.hdr_content_len = 0LL;
5710 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5711 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5712 chunk_reset(&txn->auth_hdr);
5713
5714 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
5715 if (fe->options2 & PR_O2_REQBUG_OK)
5716 txn->req.err_pos = -1; /* let buggy requests pass */
5717
5718 if (txn->req.cap)
5719 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
5720
5721 if (txn->rsp.cap)
5722 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
5723
5724 if (txn->hdr_idx.v)
5725 hdr_idx_init(&txn->hdr_idx);
5726}
5727
5728/* to be used at the end of a transaction */
5729void http_end_txn(struct session *s)
5730{
5731 struct http_txn *txn = &s->txn;
5732
5733 /* these ones will have been dynamically allocated */
5734 pool_free2(pool2_requri, txn->uri);
5735 pool_free2(pool2_capture, txn->cli_cookie);
5736 pool_free2(pool2_capture, txn->srv_cookie);
5737 txn->uri = NULL;
5738 txn->srv_cookie = NULL;
5739 txn->cli_cookie = NULL;
5740}
5741
5742/* to be used at the end of a transaction to prepare a new one */
5743void http_reset_txn(struct session *s)
5744{
5745 http_end_txn(s);
5746 http_init_txn(s);
5747
5748 s->be = s->fe;
5749 s->req->analysers = s->listener->analysers;
5750 s->logs.logwait = s->fe->to_log;
5751 s->srv = s->prev_srv = s->srv_conn = NULL;
5752 s->pend_pos = NULL;
5753 s->conn_retries = s->be->conn_retries;
5754
5755 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
5756
5757 s->req->rto = s->fe->timeout.client;
5758 s->req->wto = s->be->timeout.server;
5759 s->req->cto = s->be->timeout.connect;
5760
5761 s->rep->rto = s->be->timeout.server;
5762 s->rep->wto = s->fe->timeout.client;
5763 s->rep->cto = TICK_ETERNITY;
5764
5765 s->req->rex = TICK_ETERNITY;
5766 s->req->wex = TICK_ETERNITY;
5767 s->req->analyse_exp = TICK_ETERNITY;
5768 s->rep->rex = TICK_ETERNITY;
5769 s->rep->wex = TICK_ETERNITY;
5770 s->rep->analyse_exp = TICK_ETERNITY;
5771}
Willy Tarreau58f10d72006-12-04 02:26:12 +01005772
Willy Tarreau8797c062007-05-07 00:55:35 +02005773/************************************************************************/
5774/* The code below is dedicated to ACL parsing and matching */
5775/************************************************************************/
5776
5777
5778
5779
5780/* 1. Check on METHOD
5781 * We use the pre-parsed method if it is known, and store its number as an
5782 * integer. If it is unknown, we use the pointer and the length.
5783 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005784static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005785{
5786 int len, meth;
5787
Willy Tarreauae8b7962007-06-09 23:10:04 +02005788 len = strlen(*text);
5789 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005790
5791 pattern->val.i = meth;
5792 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005793 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005794 if (!pattern->ptr.str)
5795 return 0;
5796 pattern->len = len;
5797 }
5798 return 1;
5799}
5800
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005801static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005802acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5803 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005804{
5805 int meth;
5806 struct http_txn *txn = l7;
5807
Willy Tarreaub6866442008-07-14 23:54:42 +02005808 if (!txn)
5809 return 0;
5810
Willy Tarreau655dce92009-11-08 13:10:58 +01005811 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005812 return 0;
5813
Willy Tarreau8797c062007-05-07 00:55:35 +02005814 meth = txn->meth;
5815 test->i = meth;
5816 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005817 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5818 /* ensure the indexes are not affected */
5819 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005820 test->len = txn->req.sl.rq.m_l;
5821 test->ptr = txn->req.sol;
5822 }
5823 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5824 return 1;
5825}
5826
5827static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5828{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005829 int icase;
5830
Willy Tarreau8797c062007-05-07 00:55:35 +02005831 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005832 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005833
5834 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005835 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005836
5837 /* Other method, we must compare the strings */
5838 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005839 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005840
5841 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5842 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5843 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005844 return ACL_PAT_FAIL;
5845 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005846}
5847
5848/* 2. Check on Request/Status Version
5849 * We simply compare strings here.
5850 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005851static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005852{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005853 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005854 if (!pattern->ptr.str)
5855 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005856 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005857 return 1;
5858}
5859
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005860static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005861acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5862 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005863{
5864 struct http_txn *txn = l7;
5865 char *ptr;
5866 int len;
5867
Willy Tarreaub6866442008-07-14 23:54:42 +02005868 if (!txn)
5869 return 0;
5870
Willy Tarreau655dce92009-11-08 13:10:58 +01005871 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005872 return 0;
5873
Willy Tarreau8797c062007-05-07 00:55:35 +02005874 len = txn->req.sl.rq.v_l;
5875 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5876
5877 while ((len-- > 0) && (*ptr++ != '/'));
5878 if (len <= 0)
5879 return 0;
5880
5881 test->ptr = ptr;
5882 test->len = len;
5883
5884 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5885 return 1;
5886}
5887
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005888static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005889acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5890 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005891{
5892 struct http_txn *txn = l7;
5893 char *ptr;
5894 int len;
5895
Willy Tarreaub6866442008-07-14 23:54:42 +02005896 if (!txn)
5897 return 0;
5898
Willy Tarreau655dce92009-11-08 13:10:58 +01005899 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005900 return 0;
5901
Willy Tarreau8797c062007-05-07 00:55:35 +02005902 len = txn->rsp.sl.st.v_l;
5903 ptr = txn->rsp.sol;
5904
5905 while ((len-- > 0) && (*ptr++ != '/'));
5906 if (len <= 0)
5907 return 0;
5908
5909 test->ptr = ptr;
5910 test->len = len;
5911
5912 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5913 return 1;
5914}
5915
5916/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005917static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005918acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5919 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005920{
5921 struct http_txn *txn = l7;
5922 char *ptr;
5923 int len;
5924
Willy Tarreaub6866442008-07-14 23:54:42 +02005925 if (!txn)
5926 return 0;
5927
Willy Tarreau655dce92009-11-08 13:10:58 +01005928 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005929 return 0;
5930
Willy Tarreau8797c062007-05-07 00:55:35 +02005931 len = txn->rsp.sl.st.c_l;
5932 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5933
5934 test->i = __strl2ui(ptr, len);
5935 test->flags = ACL_TEST_F_VOL_1ST;
5936 return 1;
5937}
5938
5939/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005940static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005941acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5942 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005943{
5944 struct http_txn *txn = l7;
5945
Willy Tarreaub6866442008-07-14 23:54:42 +02005946 if (!txn)
5947 return 0;
5948
Willy Tarreau655dce92009-11-08 13:10:58 +01005949 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005950 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005951
Willy Tarreauc11416f2007-06-17 16:58:38 +02005952 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5953 /* ensure the indexes are not affected */
5954 return 0;
5955
Willy Tarreau8797c062007-05-07 00:55:35 +02005956 test->len = txn->req.sl.rq.u_l;
5957 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5958
Willy Tarreauf3d25982007-05-08 22:45:09 +02005959 /* we do not need to set READ_ONLY because the data is in a buffer */
5960 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005961 return 1;
5962}
5963
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005964static int
5965acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5966 struct acl_expr *expr, struct acl_test *test)
5967{
5968 struct http_txn *txn = l7;
5969
Willy Tarreaub6866442008-07-14 23:54:42 +02005970 if (!txn)
5971 return 0;
5972
Willy Tarreau655dce92009-11-08 13:10:58 +01005973 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005974 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005975
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005976 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5977 /* ensure the indexes are not affected */
5978 return 0;
5979
5980 /* Parse HTTP request */
5981 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5982 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5983 test->i = AF_INET;
5984
5985 /*
5986 * If we are parsing url in frontend space, we prepare backend stage
5987 * to not parse again the same url ! optimization lazyness...
5988 */
5989 if (px->options & PR_O_HTTP_PROXY)
5990 l4->flags |= SN_ADDR_SET;
5991
5992 test->flags = ACL_TEST_F_READ_ONLY;
5993 return 1;
5994}
5995
5996static int
5997acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5998 struct acl_expr *expr, struct acl_test *test)
5999{
6000 struct http_txn *txn = l7;
6001
Willy Tarreaub6866442008-07-14 23:54:42 +02006002 if (!txn)
6003 return 0;
6004
Willy Tarreau655dce92009-11-08 13:10:58 +01006005 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006006 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006007
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006008 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6009 /* ensure the indexes are not affected */
6010 return 0;
6011
6012 /* Same optimization as url_ip */
6013 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6014 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6015
6016 if (px->options & PR_O_HTTP_PROXY)
6017 l4->flags |= SN_ADDR_SET;
6018
6019 test->flags = ACL_TEST_F_READ_ONLY;
6020 return 1;
6021}
6022
Willy Tarreauc11416f2007-06-17 16:58:38 +02006023/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6024 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6025 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006026static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006027acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006028 struct acl_expr *expr, struct acl_test *test)
6029{
6030 struct http_txn *txn = l7;
6031 struct hdr_idx *idx = &txn->hdr_idx;
6032 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006033
Willy Tarreaub6866442008-07-14 23:54:42 +02006034 if (!txn)
6035 return 0;
6036
Willy Tarreau33a7e692007-06-10 19:45:56 +02006037 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6038 /* search for header from the beginning */
6039 ctx->idx = 0;
6040
Willy Tarreau33a7e692007-06-10 19:45:56 +02006041 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6042 test->flags |= ACL_TEST_F_FETCH_MORE;
6043 test->flags |= ACL_TEST_F_VOL_HDR;
6044 test->len = ctx->vlen;
6045 test->ptr = (char *)ctx->line + ctx->val;
6046 return 1;
6047 }
6048
6049 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6050 test->flags |= ACL_TEST_F_VOL_HDR;
6051 return 0;
6052}
6053
Willy Tarreau33a7e692007-06-10 19:45:56 +02006054static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006055acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6056 struct acl_expr *expr, struct acl_test *test)
6057{
6058 struct http_txn *txn = l7;
6059
Willy Tarreaub6866442008-07-14 23:54:42 +02006060 if (!txn)
6061 return 0;
6062
Willy Tarreau655dce92009-11-08 13:10:58 +01006063 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006064 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006065
Willy Tarreauc11416f2007-06-17 16:58:38 +02006066 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6067 /* ensure the indexes are not affected */
6068 return 0;
6069
6070 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6071}
6072
6073static int
6074acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6075 struct acl_expr *expr, struct acl_test *test)
6076{
6077 struct http_txn *txn = l7;
6078
Willy Tarreaub6866442008-07-14 23:54:42 +02006079 if (!txn)
6080 return 0;
6081
Willy Tarreau655dce92009-11-08 13:10:58 +01006082 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006083 return 0;
6084
6085 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6086}
6087
6088/* 6. Check on HTTP header count. The number of occurrences is returned.
6089 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6090 */
6091static int
6092acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006093 struct acl_expr *expr, struct acl_test *test)
6094{
6095 struct http_txn *txn = l7;
6096 struct hdr_idx *idx = &txn->hdr_idx;
6097 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006098 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006099
Willy Tarreaub6866442008-07-14 23:54:42 +02006100 if (!txn)
6101 return 0;
6102
Willy Tarreau33a7e692007-06-10 19:45:56 +02006103 ctx.idx = 0;
6104 cnt = 0;
6105 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6106 cnt++;
6107
6108 test->i = cnt;
6109 test->flags = ACL_TEST_F_VOL_HDR;
6110 return 1;
6111}
6112
Willy Tarreauc11416f2007-06-17 16:58:38 +02006113static int
6114acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6115 struct acl_expr *expr, struct acl_test *test)
6116{
6117 struct http_txn *txn = l7;
6118
Willy Tarreaub6866442008-07-14 23:54:42 +02006119 if (!txn)
6120 return 0;
6121
Willy Tarreau655dce92009-11-08 13:10:58 +01006122 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006123 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006124
Willy Tarreauc11416f2007-06-17 16:58:38 +02006125 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6126 /* ensure the indexes are not affected */
6127 return 0;
6128
6129 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6130}
6131
6132static int
6133acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6134 struct acl_expr *expr, struct acl_test *test)
6135{
6136 struct http_txn *txn = l7;
6137
Willy Tarreaub6866442008-07-14 23:54:42 +02006138 if (!txn)
6139 return 0;
6140
Willy Tarreau655dce92009-11-08 13:10:58 +01006141 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006142 return 0;
6143
6144 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6145}
6146
Willy Tarreau33a7e692007-06-10 19:45:56 +02006147/* 7. Check on HTTP header's integer value. The integer value is returned.
6148 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006149 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006150 */
6151static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006152acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006153 struct acl_expr *expr, struct acl_test *test)
6154{
6155 struct http_txn *txn = l7;
6156 struct hdr_idx *idx = &txn->hdr_idx;
6157 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006158
Willy Tarreaub6866442008-07-14 23:54:42 +02006159 if (!txn)
6160 return 0;
6161
Willy Tarreau33a7e692007-06-10 19:45:56 +02006162 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6163 /* search for header from the beginning */
6164 ctx->idx = 0;
6165
Willy Tarreau33a7e692007-06-10 19:45:56 +02006166 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6167 test->flags |= ACL_TEST_F_FETCH_MORE;
6168 test->flags |= ACL_TEST_F_VOL_HDR;
6169 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6170 return 1;
6171 }
6172
6173 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6174 test->flags |= ACL_TEST_F_VOL_HDR;
6175 return 0;
6176}
6177
Willy Tarreauc11416f2007-06-17 16:58:38 +02006178static int
6179acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6180 struct acl_expr *expr, struct acl_test *test)
6181{
6182 struct http_txn *txn = l7;
6183
Willy Tarreaub6866442008-07-14 23:54:42 +02006184 if (!txn)
6185 return 0;
6186
Willy Tarreau655dce92009-11-08 13:10:58 +01006187 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006188 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006189
Willy Tarreauc11416f2007-06-17 16:58:38 +02006190 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6191 /* ensure the indexes are not affected */
6192 return 0;
6193
6194 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6195}
6196
6197static int
6198acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6199 struct acl_expr *expr, struct acl_test *test)
6200{
6201 struct http_txn *txn = l7;
6202
Willy Tarreaub6866442008-07-14 23:54:42 +02006203 if (!txn)
6204 return 0;
6205
Willy Tarreau655dce92009-11-08 13:10:58 +01006206 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006207 return 0;
6208
6209 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6210}
6211
Willy Tarreau106f9792009-09-19 07:54:16 +02006212/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6213 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6214 */
6215static int
6216acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6217 struct acl_expr *expr, struct acl_test *test)
6218{
6219 struct http_txn *txn = l7;
6220 struct hdr_idx *idx = &txn->hdr_idx;
6221 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6222
6223 if (!txn)
6224 return 0;
6225
6226 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6227 /* search for header from the beginning */
6228 ctx->idx = 0;
6229
6230 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6231 test->flags |= ACL_TEST_F_FETCH_MORE;
6232 test->flags |= ACL_TEST_F_VOL_HDR;
6233 /* Same optimization as url_ip */
6234 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6235 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6236 test->ptr = (void *)&l4->srv_addr.sin_addr;
6237 test->i = AF_INET;
6238 return 1;
6239 }
6240
6241 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6242 test->flags |= ACL_TEST_F_VOL_HDR;
6243 return 0;
6244}
6245
6246static int
6247acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6248 struct acl_expr *expr, struct acl_test *test)
6249{
6250 struct http_txn *txn = l7;
6251
6252 if (!txn)
6253 return 0;
6254
Willy Tarreau655dce92009-11-08 13:10:58 +01006255 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006256 return 0;
6257
6258 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6259 /* ensure the indexes are not affected */
6260 return 0;
6261
6262 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6263}
6264
6265static int
6266acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6267 struct acl_expr *expr, struct acl_test *test)
6268{
6269 struct http_txn *txn = l7;
6270
6271 if (!txn)
6272 return 0;
6273
Willy Tarreau655dce92009-11-08 13:10:58 +01006274 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006275 return 0;
6276
6277 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6278}
6279
Willy Tarreau737b0c12007-06-10 21:28:46 +02006280/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6281 * the first '/' after the possible hostname, and ends before the possible '?'.
6282 */
6283static int
6284acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6285 struct acl_expr *expr, struct acl_test *test)
6286{
6287 struct http_txn *txn = l7;
6288 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006289
Willy Tarreaub6866442008-07-14 23:54:42 +02006290 if (!txn)
6291 return 0;
6292
Willy Tarreau655dce92009-11-08 13:10:58 +01006293 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006294 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006295
Willy Tarreauc11416f2007-06-17 16:58:38 +02006296 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6297 /* ensure the indexes are not affected */
6298 return 0;
6299
Willy Tarreau21d2af32008-02-14 20:25:24 +01006300 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
6301 ptr = http_get_path(txn);
6302 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006303 return 0;
6304
6305 /* OK, we got the '/' ! */
6306 test->ptr = ptr;
6307
6308 while (ptr < end && *ptr != '?')
6309 ptr++;
6310
6311 test->len = ptr - test->ptr;
6312
6313 /* we do not need to set READ_ONLY because the data is in a buffer */
6314 test->flags = ACL_TEST_F_VOL_1ST;
6315 return 1;
6316}
6317
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006318static int
6319acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6320 struct acl_expr *expr, struct acl_test *test)
6321{
6322 struct buffer *req = s->req;
6323 struct http_txn *txn = &s->txn;
6324 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006325
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006326 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6327 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6328 */
6329
6330 if (!s || !req)
6331 return 0;
6332
Willy Tarreau655dce92009-11-08 13:10:58 +01006333 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006334 /* Already decoded as OK */
6335 test->flags |= ACL_TEST_F_SET_RES_PASS;
6336 return 1;
6337 }
6338
6339 /* Try to decode HTTP request */
6340 if (likely(req->lr < req->r))
6341 http_msg_analyzer(req, msg, &txn->hdr_idx);
6342
Willy Tarreau655dce92009-11-08 13:10:58 +01006343 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006344 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6345 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6346 return 1;
6347 }
6348 /* wait for final state */
6349 test->flags |= ACL_TEST_F_MAY_CHANGE;
6350 return 0;
6351 }
6352
6353 /* OK we got a valid HTTP request. We have some minor preparation to
6354 * perform so that further checks can rely on HTTP tests.
6355 */
6356 msg->sol = req->data + msg->som;
6357 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
6358 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6359 s->flags |= SN_REDIRECTABLE;
6360
6361 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
6362 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6363 return 1;
6364 }
6365
6366 test->flags |= ACL_TEST_F_SET_RES_PASS;
6367 return 1;
6368}
6369
Willy Tarreau8797c062007-05-07 00:55:35 +02006370
6371/************************************************************************/
6372/* All supported keywords must be declared here. */
6373/************************************************************************/
6374
6375/* Note: must not be declared <const> as its list will be overwritten */
6376static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006377 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
6378
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006379 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6380 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6381 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6382 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006383
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006384 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6385 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6386 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6387 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6388 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6389 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6390 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6391 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
6392 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02006393
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006394 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
6395 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6396 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6397 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6398 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6399 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6400 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6401 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6402 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
6403 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006404 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02006405
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006406 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6407 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
6408 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
6409 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
6410 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
6411 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
6412 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
6413 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
6414 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006415 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006416
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006417 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6418 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6419 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6420 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6421 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6422 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6423 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006424
Willy Tarreauf3d25982007-05-08 22:45:09 +02006425 { NULL, NULL, NULL, NULL },
6426
6427#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02006428 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
6429 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
6430 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
6431 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
6432 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
6433 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
6434 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
6435
Willy Tarreau8797c062007-05-07 00:55:35 +02006436 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
6437 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
6438 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
6439 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
6440 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
6441 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
6442 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
6443 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
6444
6445 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
6446 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
6447 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
6448 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
6449 { NULL, NULL, NULL, NULL },
6450#endif
6451}};
6452
6453
6454__attribute__((constructor))
6455static void __http_protocol_init(void)
6456{
6457 acl_register_keywords(&acl_kws);
6458}
6459
6460
Willy Tarreau58f10d72006-12-04 02:26:12 +01006461/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006462 * Local variables:
6463 * c-indent-level: 8
6464 * c-basic-offset: 8
6465 * End:
6466 */