blob: 60a3177b69dc88fb9aa723cd2f49aee937aff89c [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;
Willy Tarreau5523b322009-12-29 12:05:52 +01001927 /* Forward last remaining trailers. Note that since all the
1928 * bytes are included in the buffer and we've found the end,
1929 * we won't leave anything in to_forward.
1930 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001931 buffer_forward(buf, bytes);
1932 msg->som = msg->sov = bytes;
1933 msg->msg_state = HTTP_MSG_DONE;
1934 return 1;
1935 }
1936 /* OK, next line then */
1937 buf->lr = p2;
1938 }
1939}
1940
1941/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1942 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
1943 * ->som, buf->lr in order to include this part into the next forwarding phase.
1944 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1945 * not enough data are available, the function does not change anything and
1946 * returns zero. If a parse error is encountered, the function returns < 0 and
1947 * does not change anything. Note: this function is designed to parse wrapped
1948 * CRLF at the end of the buffer.
1949 */
1950int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1951{
1952 char *ptr;
1953 int bytes;
1954
1955 /* NB: we'll check data availabilty at the end. It's not a
1956 * problem because whatever we match first will be checked
1957 * against the correct length.
1958 */
1959 bytes = 1;
1960 ptr = buf->lr;
1961 if (*ptr == '\r') {
1962 bytes++;
1963 ptr++;
1964 if (ptr >= buf->data + buf->size)
1965 ptr = buf->data;
1966 }
1967
1968 if (buf->l < bytes)
1969 return 0;
1970
1971 if (*ptr != '\n')
1972 return -1;
1973
1974 ptr++;
1975 if (ptr >= buf->data + buf->size)
1976 ptr = buf->data;
1977 buf->lr = ptr;
1978 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
1979 msg->sov = ptr - buf->data;
1980 msg->som = msg->sov - bytes;
1981 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1982 return 1;
1983}
Willy Tarreau5b154472009-12-21 20:11:07 +01001984
Willy Tarreau83e3af02009-12-28 17:39:57 +01001985void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
1986{
1987 char *end = buf->data + buf->size;
1988 int off = buf->data + buf->size - buf->w;
1989
1990 /* two possible cases :
1991 * - the buffer is in one contiguous block, we move it in-place
1992 * - the buffer is in two blocks, we move it via the trash
1993 */
1994 if (buf->l) {
1995 int block1 = buf->l;
1996 int block2 = 0;
1997 if (buf->r <= buf->w) {
1998 /* non-contiguous block */
1999 block1 = buf->data + buf->size - buf->w;
2000 block2 = buf->r - buf->data;
2001 }
2002 if (block2)
2003 memcpy(trash, buf->data, block2);
2004 memmove(buf->data, buf->w, block1);
2005 if (block2)
2006 memcpy(buf->data + block1, trash, block2);
2007 }
2008
2009 /* adjust all known pointers */
2010 buf->w = buf->data;
2011 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2012 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2013 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2014 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2015
2016 /* adjust relative pointers */
2017 msg->som = 0;
2018 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2019 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2020 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2021
2022 msg->sl.rq.u += off; if (msg->sl.rq.u >= buf->size) msg->sl.rq.u -= buf->size;
2023 msg->sl.rq.v += off; if (msg->sl.rq.v >= buf->size) msg->sl.rq.v -= buf->size;
2024
2025 if (msg->err_pos >= 0) {
2026 msg->err_pos += off;
2027 if (msg->err_pos >= buf->size)
2028 msg->err_pos -= buf->size;
2029 }
2030
2031 buf->flags &= ~BF_FULL;
2032 if (buf->l >= buffer_max_len(buf))
2033 buf->flags |= BF_FULL;
2034}
2035
Willy Tarreaud787e662009-07-07 10:14:51 +02002036/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2037 * processing can continue on next analysers, or zero if it either needs more
2038 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2039 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2040 * when it has nothing left to do, and may remove any analyser when it wants to
2041 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002042 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002043int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002044{
Willy Tarreau59234e92008-11-30 23:51:27 +01002045 /*
2046 * We will parse the partial (or complete) lines.
2047 * We will check the request syntax, and also join multi-line
2048 * headers. An index of all the lines will be elaborated while
2049 * parsing.
2050 *
2051 * For the parsing, we use a 28 states FSM.
2052 *
2053 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002054 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002055 * req->data + msg->eoh = end of processed headers / start of current one
2056 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002057 * req->lr = first non-visited byte
2058 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002059 *
2060 * At end of parsing, we may perform a capture of the error (if any), and
2061 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2062 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2063 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002064 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002065
Willy Tarreau59234e92008-11-30 23:51:27 +01002066 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002067 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002068 struct http_txn *txn = &s->txn;
2069 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002070 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002071
Willy Tarreau6bf17362009-02-24 10:48:35 +01002072 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2073 now_ms, __FUNCTION__,
2074 s,
2075 req,
2076 req->rex, req->wex,
2077 req->flags,
2078 req->l,
2079 req->analysers);
2080
Willy Tarreau52a0c602009-08-16 22:45:38 +02002081 /* we're speaking HTTP here, so let's speak HTTP to the client */
2082 s->srv_error = http_return_srv_error;
2083
Willy Tarreau83e3af02009-12-28 17:39:57 +01002084 /* There's a protected area at the end of the buffer for rewriting
2085 * purposes. We don't want to start to parse the request if the
2086 * protected area is affected, because we may have to move processed
2087 * data later, which is much more complicated.
2088 */
2089 if (req->l &&
2090 (req->r <= req->lr || req->r > req->data + req->size - global.tune.maxrewrite)) {
2091 if (req->send_max) {
2092 /* some data has still not left the buffer, wake us once that's done */
2093 buffer_dont_connect(req);
2094 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2095 return 0;
2096 }
2097
2098 http_buffer_heavy_realign(req, msg);
2099 }
2100
Willy Tarreau59234e92008-11-30 23:51:27 +01002101 if (likely(req->lr < req->r))
2102 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002103
Willy Tarreau59234e92008-11-30 23:51:27 +01002104 /* 1: we might have to print this header in debug mode */
2105 if (unlikely((global.mode & MODE_DEBUG) &&
2106 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002107 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002108 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002109
Willy Tarreau59234e92008-11-30 23:51:27 +01002110 sol = req->data + msg->som;
2111 eol = sol + msg->sl.rq.l;
2112 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002113
Willy Tarreau59234e92008-11-30 23:51:27 +01002114 sol += hdr_idx_first_pos(&txn->hdr_idx);
2115 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002116
Willy Tarreau59234e92008-11-30 23:51:27 +01002117 while (cur_idx) {
2118 eol = sol + txn->hdr_idx.v[cur_idx].len;
2119 debug_hdr("clihdr", s, sol, eol);
2120 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2121 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002122 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002123 }
2124
Willy Tarreau58f10d72006-12-04 02:26:12 +01002125
Willy Tarreau59234e92008-11-30 23:51:27 +01002126 /*
2127 * Now we quickly check if we have found a full valid request.
2128 * If not so, we check the FD and buffer states before leaving.
2129 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002130 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01002131 * requests are checked first.
2132 *
2133 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002134
Willy Tarreau655dce92009-11-08 13:10:58 +01002135 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002136 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002137 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002138 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002139 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2140 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002141
Willy Tarreau59234e92008-11-30 23:51:27 +01002142 /* 1: Since we are in header mode, if there's no space
2143 * left for headers, we won't be able to free more
2144 * later, so the session will never terminate. We
2145 * must terminate it now.
2146 */
2147 if (unlikely(req->flags & BF_FULL)) {
2148 /* FIXME: check if URI is set and return Status
2149 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002150 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002151 goto return_bad_req;
2152 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002153
Willy Tarreau59234e92008-11-30 23:51:27 +01002154 /* 2: have we encountered a read error ? */
2155 else if (req->flags & BF_READ_ERROR) {
2156 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002157 if (msg->err_pos >= 0)
2158 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002159 msg->msg_state = HTTP_MSG_ERROR;
2160 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002161
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002162 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002163 if (s->listener->counters)
2164 s->listener->counters->failed_req++;
2165
Willy Tarreau59234e92008-11-30 23:51:27 +01002166 if (!(s->flags & SN_ERR_MASK))
2167 s->flags |= SN_ERR_CLICL;
2168 if (!(s->flags & SN_FINST_MASK))
2169 s->flags |= SN_FINST_R;
2170 return 0;
2171 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002172
Willy Tarreau59234e92008-11-30 23:51:27 +01002173 /* 3: has the read timeout expired ? */
2174 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
2175 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002176 if (msg->err_pos >= 0)
2177 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002178 txn->status = 408;
2179 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2180 msg->msg_state = HTTP_MSG_ERROR;
2181 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002182
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002183 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002184 if (s->listener->counters)
2185 s->listener->counters->failed_req++;
2186
Willy Tarreau59234e92008-11-30 23:51:27 +01002187 if (!(s->flags & SN_ERR_MASK))
2188 s->flags |= SN_ERR_CLITO;
2189 if (!(s->flags & SN_FINST_MASK))
2190 s->flags |= SN_FINST_R;
2191 return 0;
2192 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002193
Willy Tarreau59234e92008-11-30 23:51:27 +01002194 /* 4: have we encountered a close ? */
2195 else if (req->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002196 if (msg->err_pos >= 0)
2197 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002198 txn->status = 400;
2199 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2200 msg->msg_state = HTTP_MSG_ERROR;
2201 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002202
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002203 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002204 if (s->listener->counters)
2205 s->listener->counters->failed_req++;
2206
Willy Tarreau59234e92008-11-30 23:51:27 +01002207 if (!(s->flags & SN_ERR_MASK))
2208 s->flags |= SN_ERR_CLICL;
2209 if (!(s->flags & SN_FINST_MASK))
2210 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002211 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002212 }
2213
Willy Tarreau520d95e2009-09-19 21:04:57 +02002214 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002215 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2216
Willy Tarreau59234e92008-11-30 23:51:27 +01002217 /* just set the request timeout once at the beginning of the request */
2218 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002219 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002220
Willy Tarreau59234e92008-11-30 23:51:27 +01002221 /* we're not ready yet */
2222 return 0;
2223 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002224
Willy Tarreaud787e662009-07-07 10:14:51 +02002225 /* OK now we have a complete HTTP request with indexed headers. Let's
2226 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002227 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2228 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2229 * points to the CRLF of the request line. req->lr points to the first
2230 * byte after the last LF. msg->col and msg->sov point to the first
2231 * byte of data. msg->eol cannot be trusted because it may have been
2232 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002233 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002234
Willy Tarreaud787e662009-07-07 10:14:51 +02002235 /* Maybe we found in invalid header name while we were configured not
2236 * to block on that, so we have to capture it now.
2237 */
2238 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002239 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2240
Willy Tarreau59234e92008-11-30 23:51:27 +01002241 /* ensure we keep this pointer to the beginning of the message */
2242 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002243
Willy Tarreau59234e92008-11-30 23:51:27 +01002244 /*
2245 * 1: identify the method
2246 */
2247 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
2248
2249 /* we can make use of server redirect on GET and HEAD */
2250 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2251 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002252
Willy Tarreau59234e92008-11-30 23:51:27 +01002253 /*
2254 * 2: check if the URI matches the monitor_uri.
2255 * We have to do this for every request which gets in, because
2256 * the monitor-uri is defined by the frontend.
2257 */
2258 if (unlikely((s->fe->monitor_uri_len != 0) &&
2259 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2260 !memcmp(&req->data[msg->sl.rq.u],
2261 s->fe->monitor_uri,
2262 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002263 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002264 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002265 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002266 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002267
Willy Tarreau59234e92008-11-30 23:51:27 +01002268 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002269
Willy Tarreau59234e92008-11-30 23:51:27 +01002270 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002271 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2272 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002273
Willy Tarreau59234e92008-11-30 23:51:27 +01002274 ret = acl_pass(ret);
2275 if (cond->pol == ACL_COND_UNLESS)
2276 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002277
Willy Tarreau59234e92008-11-30 23:51:27 +01002278 if (ret) {
2279 /* we fail this request, let's return 503 service unavail */
2280 txn->status = 503;
2281 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2282 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002283 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002284 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002285
Willy Tarreau59234e92008-11-30 23:51:27 +01002286 /* nothing to fail, let's reply normaly */
2287 txn->status = 200;
2288 stream_int_retnclose(req->prod, &http_200_chunk);
2289 goto return_prx_cond;
2290 }
2291
2292 /*
2293 * 3: Maybe we have to copy the original REQURI for the logs ?
2294 * Note: we cannot log anymore if the request has been
2295 * classified as invalid.
2296 */
2297 if (unlikely(s->logs.logwait & LW_REQ)) {
2298 /* we have a complete HTTP request that we must log */
2299 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2300 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002301
Willy Tarreau59234e92008-11-30 23:51:27 +01002302 if (urilen >= REQURI_LEN)
2303 urilen = REQURI_LEN - 1;
2304 memcpy(txn->uri, &req->data[msg->som], urilen);
2305 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002306
Willy Tarreau59234e92008-11-30 23:51:27 +01002307 if (!(s->logs.logwait &= ~LW_REQ))
2308 s->do_log(s);
2309 } else {
2310 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002311 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002312 }
Willy Tarreau06619262006-12-17 08:37:22 +01002313
Willy Tarreau59234e92008-11-30 23:51:27 +01002314 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002315 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2316 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002317
Willy Tarreau5b154472009-12-21 20:11:07 +01002318 /* ... and check if the request is HTTP/1.1 or above */
2319 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002320 ((req->data[msg->sl.rq.v + 5] > '1') ||
2321 ((req->data[msg->sl.rq.v + 5] == '1') &&
2322 (req->data[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002323 txn->flags |= TX_REQ_VER_11;
2324
2325 /* "connection" has not been parsed yet */
2326 txn->flags &= ~TX_CON_HDR_PARS;
2327
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002328 /* transfer length unknown*/
2329 txn->flags &= ~TX_REQ_XFER_LEN;
2330
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 /* 5: we may need to capture headers */
2332 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
2333 capture_headers(req->data + msg->som, &txn->hdr_idx,
2334 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002335
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002336 /* 6: determine the transfer-length.
2337 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2338 * the presence of a message-body in a REQUEST and its transfer length
2339 * must be determined that way (in order of precedence) :
2340 * 1. The presence of a message-body in a request is signaled by the
2341 * inclusion of a Content-Length or Transfer-Encoding header field
2342 * in the request's header fields. When a request message contains
2343 * both a message-body of non-zero length and a method that does
2344 * not define any semantics for that request message-body, then an
2345 * origin server SHOULD either ignore the message-body or respond
2346 * with an appropriate error message (e.g., 413). A proxy or
2347 * gateway, when presented the same request, SHOULD either forward
2348 * the request inbound with the message- body or ignore the
2349 * message-body when determining a response.
2350 *
2351 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2352 * and the "chunked" transfer-coding (Section 6.2) is used, the
2353 * transfer-length is defined by the use of this transfer-coding.
2354 * If a Transfer-Encoding header field is present and the "chunked"
2355 * transfer-coding is not present, the transfer-length is defined
2356 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002357 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002358 * 3. If a Content-Length header field is present, its decimal value in
2359 * OCTETs represents both the entity-length and the transfer-length.
2360 * If a message is received with both a Transfer-Encoding header
2361 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002362 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002363 * 4. By the server closing the connection. (Closing the connection
2364 * cannot be used to indicate the end of a request body, since that
2365 * would leave no possibility for the server to send back a response.)
2366 *
2367 * Whenever a transfer-coding is applied to a message-body, the set of
2368 * transfer-codings MUST include "chunked", unless the message indicates
2369 * it is terminated by closing the connection. When the "chunked"
2370 * transfer-coding is used, it MUST be the last transfer-coding applied
2371 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002372 */
2373
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002374 /* CONNECT sets a tunnel and ignores everything else */
2375 if (txn->meth == HTTP_METH_CONNECT)
2376 goto skip_xfer_len;
2377
2378 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002379 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002380 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002381 while ((txn->flags & TX_REQ_VER_11) &&
2382 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002383 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2384 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2385 else if (txn->flags & TX_REQ_TE_CHNK) {
2386 /* bad transfer-encoding (chunked followed by something else) */
2387 use_close_only = 1;
2388 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2389 break;
2390 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002391 }
2392
Willy Tarreau32b47f42009-10-18 20:55:02 +02002393 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002394 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002395 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2396 signed long long cl;
2397
2398 if (!ctx.vlen)
2399 goto return_bad_req;
2400
2401 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2402 goto return_bad_req; /* parse failure */
2403
2404 if (cl < 0)
2405 goto return_bad_req;
2406
2407 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2408 goto return_bad_req; /* already specified, was different */
2409
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002410 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002411 msg->hdr_content_len = cl;
2412 }
2413
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002414 /* bodyless requests have a known length */
2415 if (!use_close_only)
2416 txn->flags |= TX_REQ_XFER_LEN;
2417
2418 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002419 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002420 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002421 req->analyse_exp = TICK_ETERNITY;
2422 return 1;
2423
2424 return_bad_req:
2425 /* We centralize bad requests processing here */
2426 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2427 /* we detected a parsing error. We want to archive this request
2428 * in the dedicated proxy area for later troubleshooting.
2429 */
2430 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2431 }
2432
2433 txn->req.msg_state = HTTP_MSG_ERROR;
2434 txn->status = 400;
2435 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002436
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002437 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002438 if (s->listener->counters)
2439 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002440
2441 return_prx_cond:
2442 if (!(s->flags & SN_ERR_MASK))
2443 s->flags |= SN_ERR_PRXCOND;
2444 if (!(s->flags & SN_FINST_MASK))
2445 s->flags |= SN_FINST_R;
2446
2447 req->analysers = 0;
2448 req->analyse_exp = TICK_ETERNITY;
2449 return 0;
2450}
2451
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002452/* This stream analyser runs all HTTP request processing which is common to
2453 * frontends and backends, which means blocking ACLs, filters, connection-close,
2454 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002455 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002456 * either needs more data or wants to immediately abort the request (eg: deny,
2457 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002458 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002459int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002460{
Willy Tarreaud787e662009-07-07 10:14:51 +02002461 struct http_txn *txn = &s->txn;
2462 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002463 struct acl_cond *cond;
2464 struct redirect_rule *rule;
2465 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002466
Willy Tarreau655dce92009-11-08 13:10:58 +01002467 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002468 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002469 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002470 return 0;
2471 }
2472
Willy Tarreau3a816292009-07-07 10:55:49 +02002473 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002474 req->analyse_exp = TICK_ETERNITY;
2475
2476 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2477 now_ms, __FUNCTION__,
2478 s,
2479 req,
2480 req->rex, req->wex,
2481 req->flags,
2482 req->l,
2483 req->analysers);
2484
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002485 /* first check whether we have some ACLs set to block this request */
2486 list_for_each_entry(cond, &px->block_cond, list) {
2487 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002488
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002489 ret = acl_pass(ret);
2490 if (cond->pol == ACL_COND_UNLESS)
2491 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002492
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002493 if (ret) {
2494 txn->status = 403;
2495 /* let's log the request time */
2496 s->logs.tv_request = now;
2497 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2498 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002499 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002500 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002501
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002502 /* try headers filters */
2503 if (px->req_exp != NULL) {
2504 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2505 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002506
Willy Tarreau59234e92008-11-30 23:51:27 +01002507 /* has the request been denied ? */
2508 if (txn->flags & TX_CLDENY) {
2509 /* no need to go further */
2510 txn->status = 403;
2511 /* let's log the request time */
2512 s->logs.tv_request = now;
2513 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2514 goto return_prx_cond;
2515 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002516
2517 /* When a connection is tarpitted, we use the tarpit timeout,
2518 * which may be the same as the connect timeout if unspecified.
2519 * If unset, then set it to zero because we really want it to
2520 * eventually expire. We build the tarpit as an analyser.
2521 */
2522 if (txn->flags & TX_CLTARPIT) {
2523 buffer_erase(s->req);
2524 /* wipe the request out so that we can drop the connection early
2525 * if the client closes first.
2526 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002527 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002528 req->analysers = 0; /* remove switching rules etc... */
2529 req->analysers |= AN_REQ_HTTP_TARPIT;
2530 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2531 if (!req->analyse_exp)
2532 req->analyse_exp = tick_add(now_ms, 0);
2533 return 1;
2534 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002535 }
Willy Tarreau06619262006-12-17 08:37:22 +01002536
Willy Tarreau5b154472009-12-21 20:11:07 +01002537 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2538 * only change if both the request and the config reference something else.
Willy Tarreau42736642009-10-18 21:04:35 +02002539 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002540
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002541 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreau5b154472009-12-21 20:11:07 +01002542 ((s->fe->options|s->be->options) & (PR_O_KEEPALIVE|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2543 int tmp = TX_CON_WANT_TUN;
2544 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2545 tmp = TX_CON_WANT_KAL;
2546 /* FIXME: for now, we don't support server-close mode */
2547 if ((s->fe->options|s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))
2548 tmp = TX_CON_WANT_CLO;
2549
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002550 if (!(txn->flags & TX_REQ_XFER_LEN))
2551 tmp = TX_CON_WANT_CLO;
2552
Willy Tarreau5b154472009-12-21 20:11:07 +01002553 if (!(txn->flags & TX_CON_HDR_PARS))
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002554 http_req_parse_connection_header(txn);
Willy Tarreau5b154472009-12-21 20:11:07 +01002555
2556 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2557 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2558 }
2559
2560 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2561 * once we know the backend, because the tunnel mode can be implied by the
2562 * lack of any close/keepalive options in both the FE and the BE. Since
2563 * this information can evolve with time, we proceed by trying to make the
2564 * header status match the desired status. For this, we'll have to adjust
2565 * the "Connection" header. The test for persistent connections has already
2566 * been performed, so we only enter here if there is a risk the connection
2567 * is considered as persistent and we want it to be closed on the server
2568 * side. It would be nice if we could enter this place only when a
2569 * Connection header exists. Note that a CONNECT method will not enter
2570 * here.
2571 */
2572 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002573 char *cur_ptr, *cur_end, *cur_next;
2574 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002575 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002576 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002577
Willy Tarreau5b154472009-12-21 20:11:07 +01002578 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002579 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002580
Willy Tarreau5b154472009-12-21 20:11:07 +01002581 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002582 cur_hdr = &txn->hdr_idx.v[cur_idx];
2583 cur_ptr = cur_next;
2584 cur_end = cur_ptr + cur_hdr->len;
2585 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002586
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002587 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002588 if (!val)
2589 continue;
2590
2591 /* 3 possibilities :
2592 * - we have already set "Connection: close" or we're in
2593 * HTTP/1.0, so we remove this line.
2594 * - we have not yet set "Connection: close", but this line
2595 * indicates close. We leave it untouched and set the flag.
2596 * - we have not yet set "Connection: close", and this line
2597 * indicates non-close. We replace it and set the flag.
2598 */
2599 if (must_delete) {
2600 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2601 http_msg_move_end(&txn->req, delta);
2602 cur_next += delta;
2603 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2604 txn->hdr_idx.used--;
2605 cur_hdr->len = 0;
2606 txn->flags |= TX_REQ_CONN_CLO;
2607 } else {
2608 if (cur_end - cur_ptr - val != 5 ||
2609 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2610 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2611 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002612 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002613 cur_hdr->len += delta;
2614 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002615 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002616 txn->flags |= TX_REQ_CONN_CLO;
2617 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002618 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002619 } /* for loop */
2620 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002621
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002622 /* add request headers from the rule sets in the same order */
2623 for (cur_idx = 0; cur_idx < px->nb_reqadd; cur_idx++) {
2624 if (unlikely(http_header_add_tail(req,
2625 &txn->req,
2626 &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002627 px->req_add[cur_idx]) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002628 goto return_bad_req;
2629 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002630
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002631 /* check if stats URI was requested, and if an auth is needed */
2632 if (px->uri_auth != NULL &&
2633 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2634 /* we have to check the URI and auth for this request.
2635 * FIXME!!! that one is rather dangerous, we want to
2636 * make it follow standard rules (eg: clear req->analysers).
2637 */
2638 if (stats_check_uri_auth(s, px)) {
2639 req->analysers = 0;
2640 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002641 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002642 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002643
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002644 /* check whether we have some ACLs set to redirect this request */
2645 list_for_each_entry(rule, &px->redirect_rules, list) {
2646 int ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreau06b917c2009-07-06 16:34:52 +02002647
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002648 ret = acl_pass(ret);
2649 if (rule->cond->pol == ACL_COND_UNLESS)
2650 ret = !ret;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002651
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002652 if (ret) {
2653 struct chunk rdr = { trash, 0 };
2654 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002655
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002656 /* build redirect message */
2657 switch(rule->code) {
2658 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002659 msg_fmt = HTTP_303;
2660 break;
2661 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002662 msg_fmt = HTTP_301;
2663 break;
2664 case 302:
2665 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002666 msg_fmt = HTTP_302;
2667 break;
2668 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002669
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002670 if (unlikely(chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002671 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002672
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002673 switch(rule->type) {
2674 case REDIRECT_TYPE_PREFIX: {
2675 const char *path;
2676 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002677
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002678 path = http_get_path(txn);
2679 /* build message using path */
2680 if (path) {
2681 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2682 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2683 int qs = 0;
2684 while (qs < pathlen) {
2685 if (path[qs] == '?') {
2686 pathlen = qs;
2687 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002688 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002689 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002690 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002691 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002692 } else {
2693 path = "/";
2694 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002695 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002696
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002697 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002698 goto return_bad_req;
2699
2700 /* add prefix. Note that if prefix == "/", we don't want to
2701 * add anything, otherwise it makes it hard for the user to
2702 * configure a self-redirection.
2703 */
2704 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002705 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2706 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002707 }
2708
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002709 /* add path */
2710 memcpy(rdr.str + rdr.len, path, pathlen);
2711 rdr.len += pathlen;
2712 break;
2713 }
2714 case REDIRECT_TYPE_LOCATION:
2715 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002716 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002717 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002718
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002719 /* add location */
2720 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2721 rdr.len += rule->rdr_len;
2722 break;
2723 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002724
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002725 if (rule->cookie_len) {
2726 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2727 rdr.len += 14;
2728 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2729 rdr.len += rule->cookie_len;
2730 memcpy(rdr.str + rdr.len, "\r\n", 2);
2731 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002732 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002733
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002734 /* add end of headers */
2735 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2736 rdr.len += 4;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002737
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002738 txn->status = rule->code;
2739 /* let's log the request time */
2740 s->logs.tv_request = now;
2741 stream_int_retnclose(req->prod, &rdr);
2742 goto return_prx_cond;
2743 }
2744 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002745
Willy Tarreaud98cf932009-12-27 22:54:55 +01002746 /* We can shut read side if we know how we won't transfer any more data && !abort_on_close */
2747 if ((txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau5b154472009-12-21 20:11:07 +01002748 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
Willy Tarreaud98cf932009-12-27 22:54:55 +01002749 (req->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)))
Willy Tarreau349a0f62009-10-18 21:17:42 +02002750 req->flags |= BF_DONT_READ;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +02002751
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002752 /* that's OK for us now, let's move on to next analysers */
2753 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002754
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002755 return_bad_req:
2756 /* We centralize bad requests processing here */
2757 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2758 /* we detected a parsing error. We want to archive this request
2759 * in the dedicated proxy area for later troubleshooting.
2760 */
2761 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2762 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002763
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002764 txn->req.msg_state = HTTP_MSG_ERROR;
2765 txn->status = 400;
2766 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002767
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002768 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002769 if (s->listener->counters)
2770 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002771
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002772 return_prx_cond:
2773 if (!(s->flags & SN_ERR_MASK))
2774 s->flags |= SN_ERR_PRXCOND;
2775 if (!(s->flags & SN_FINST_MASK))
2776 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002777
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002778 req->analysers = 0;
2779 req->analyse_exp = TICK_ETERNITY;
2780 return 0;
2781}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002782
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002783/* This function performs all the processing enabled for the current request.
2784 * It returns 1 if the processing can continue on next analysers, or zero if it
2785 * needs more data, encounters an error, or wants to immediately abort the
2786 * request. It relies on buffers flags, and updates s->req->analysers.
2787 */
2788int http_process_request(struct session *s, struct buffer *req, int an_bit)
2789{
2790 struct http_txn *txn = &s->txn;
2791 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002792
Willy Tarreau655dce92009-11-08 13:10:58 +01002793 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002794 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002795 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002796 return 0;
2797 }
2798
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002799 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2800 now_ms, __FUNCTION__,
2801 s,
2802 req,
2803 req->rex, req->wex,
2804 req->flags,
2805 req->l,
2806 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002807
Willy Tarreau59234e92008-11-30 23:51:27 +01002808 /*
2809 * Right now, we know that we have processed the entire headers
2810 * and that unwanted requests have been filtered out. We can do
2811 * whatever we want with the remaining request. Also, now we
2812 * may have separate values for ->fe, ->be.
2813 */
Willy Tarreau06619262006-12-17 08:37:22 +01002814
Willy Tarreau59234e92008-11-30 23:51:27 +01002815 /*
2816 * If HTTP PROXY is set we simply get remote server address
2817 * parsing incoming request.
2818 */
2819 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2820 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2821 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002822
Willy Tarreau59234e92008-11-30 23:51:27 +01002823 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002824 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002825 * Note that doing so might move headers in the request, but
2826 * the fields will stay coherent and the URI will not move.
2827 * This should only be performed in the backend.
2828 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002829 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002830 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2831 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002832
Willy Tarreau59234e92008-11-30 23:51:27 +01002833 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002834 * 8: the appsession cookie was looked up very early in 1.2,
2835 * so let's do the same now.
2836 */
2837
2838 /* It needs to look into the URI */
2839 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002840 get_srv_from_appsession(s, &req->data[msg->sl.rq.u], msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002841 }
2842
2843 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002844 * 9: add X-Forwarded-For if either the frontend or the backend
2845 * asks for it.
2846 */
2847 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2848 if (s->cli_addr.ss_family == AF_INET) {
2849 /* Add an X-Forwarded-For header unless the source IP is
2850 * in the 'except' network range.
2851 */
2852 if ((!s->fe->except_mask.s_addr ||
2853 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2854 != s->fe->except_net.s_addr) &&
2855 (!s->be->except_mask.s_addr ||
2856 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2857 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002858 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002859 unsigned char *pn;
2860 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002861
2862 /* Note: we rely on the backend to get the header name to be used for
2863 * x-forwarded-for, because the header is really meant for the backends.
2864 * However, if the backend did not specify any option, we have to rely
2865 * on the frontend's header name.
2866 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002867 if (s->be->fwdfor_hdr_len) {
2868 len = s->be->fwdfor_hdr_len;
2869 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002870 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002871 len = s->fe->fwdfor_hdr_len;
2872 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002873 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002874 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002875
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002876 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002877 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01002878 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002879 }
2880 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002881 else if (s->cli_addr.ss_family == AF_INET6) {
2882 /* FIXME: for the sake of completeness, we should also support
2883 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002884 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002885 int len;
2886 char pn[INET6_ADDRSTRLEN];
2887 inet_ntop(AF_INET6,
2888 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2889 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002890
Willy Tarreau59234e92008-11-30 23:51:27 +01002891 /* Note: we rely on the backend to get the header name to be used for
2892 * x-forwarded-for, because the header is really meant for the backends.
2893 * However, if the backend did not specify any option, we have to rely
2894 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002895 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002896 if (s->be->fwdfor_hdr_len) {
2897 len = s->be->fwdfor_hdr_len;
2898 memcpy(trash, s->be->fwdfor_hdr_name, len);
2899 } else {
2900 len = s->fe->fwdfor_hdr_len;
2901 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002902 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002903 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002904
Willy Tarreau59234e92008-11-30 23:51:27 +01002905 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002906 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01002907 goto return_bad_req;
2908 }
2909 }
2910
2911 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002912 * 10: add X-Original-To if either the frontend or the backend
2913 * asks for it.
2914 */
2915 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
2916
2917 /* FIXME: don't know if IPv6 can handle that case too. */
2918 if (s->cli_addr.ss_family == AF_INET) {
2919 /* Add an X-Original-To header unless the destination IP is
2920 * in the 'except' network range.
2921 */
2922 if (!(s->flags & SN_FRT_ADDR_SET))
2923 get_frt_addr(s);
2924
2925 if ((!s->fe->except_mask_to.s_addr ||
2926 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
2927 != s->fe->except_to.s_addr) &&
2928 (!s->be->except_mask_to.s_addr ||
2929 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
2930 != s->be->except_to.s_addr)) {
2931 int len;
2932 unsigned char *pn;
2933 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
2934
2935 /* Note: we rely on the backend to get the header name to be used for
2936 * x-original-to, because the header is really meant for the backends.
2937 * However, if the backend did not specify any option, we have to rely
2938 * on the frontend's header name.
2939 */
2940 if (s->be->orgto_hdr_len) {
2941 len = s->be->orgto_hdr_len;
2942 memcpy(trash, s->be->orgto_hdr_name, len);
2943 } else {
2944 len = s->fe->orgto_hdr_len;
2945 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002946 }
Maik Broemme2850cb42009-04-17 18:53:21 +02002947 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
2948
2949 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002950 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02002951 goto return_bad_req;
2952 }
2953 }
2954 }
2955
Willy Tarreau78599912009-10-17 20:12:21 +02002956 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau5b154472009-12-21 20:11:07 +01002957 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau78599912009-10-17 20:12:21 +02002958 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002959 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01002960 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01002961 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01002962 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01002963
2964 /* If we have no server assigned yet and we're balancing on url_param
2965 * with a POST request, we may be interested in checking the body for
2966 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01002967 */
2968 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2969 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01002970 s->be->url_param_post_limit != 0 &&
2971 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002972 memchr(req->data + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01002973 buffer_dont_connect(req);
2974 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01002975 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002976
Willy Tarreaud98cf932009-12-27 22:54:55 +01002977 if (txn->flags & TX_REQ_XFER_LEN)
2978 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01002979
Willy Tarreau59234e92008-11-30 23:51:27 +01002980 /*************************************************************
2981 * OK, that's finished for the headers. We have done what we *
2982 * could. Let's switch to the DATA state. *
2983 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01002984 req->analyse_exp = TICK_ETERNITY;
2985 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002986
Willy Tarreau59234e92008-11-30 23:51:27 +01002987 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01002988 /* OK let's go on with the BODY now */
2989 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002990
Willy Tarreau59234e92008-11-30 23:51:27 +01002991 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02002992 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002993 /* we detected a parsing error. We want to archive this request
2994 * in the dedicated proxy area for later troubleshooting.
2995 */
Willy Tarreau4076a152009-04-02 15:18:36 +02002996 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01002997 }
Willy Tarreau4076a152009-04-02 15:18:36 +02002998
Willy Tarreau59234e92008-11-30 23:51:27 +01002999 txn->req.msg_state = HTTP_MSG_ERROR;
3000 txn->status = 400;
3001 req->analysers = 0;
3002 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003003
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003004 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003005 if (s->listener->counters)
3006 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003007
Willy Tarreau59234e92008-11-30 23:51:27 +01003008 if (!(s->flags & SN_ERR_MASK))
3009 s->flags |= SN_ERR_PRXCOND;
3010 if (!(s->flags & SN_FINST_MASK))
3011 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003012 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003013}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003014
Willy Tarreau60b85b02008-11-30 23:28:40 +01003015/* This function is an analyser which processes the HTTP tarpit. It always
3016 * returns zero, at the beginning because it prevents any other processing
3017 * from occurring, and at the end because it terminates the request.
3018 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003019int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003020{
3021 struct http_txn *txn = &s->txn;
3022
3023 /* This connection is being tarpitted. The CLIENT side has
3024 * already set the connect expiration date to the right
3025 * timeout. We just have to check that the client is still
3026 * there and that the timeout has not expired.
3027 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003028 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003029 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3030 !tick_is_expired(req->analyse_exp, now_ms))
3031 return 0;
3032
3033 /* We will set the queue timer to the time spent, just for
3034 * logging purposes. We fake a 500 server error, so that the
3035 * attacker will not suspect his connection has been tarpitted.
3036 * It will not cause trouble to the logs because we can exclude
3037 * the tarpitted connections by filtering on the 'PT' status flags.
3038 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003039 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3040
3041 txn->status = 500;
3042 if (req->flags != BF_READ_ERROR)
3043 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3044
3045 req->analysers = 0;
3046 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003047
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003048 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003049 if (s->listener->counters)
3050 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003051
Willy Tarreau60b85b02008-11-30 23:28:40 +01003052 if (!(s->flags & SN_ERR_MASK))
3053 s->flags |= SN_ERR_PRXCOND;
3054 if (!(s->flags & SN_FINST_MASK))
3055 s->flags |= SN_FINST_T;
3056 return 0;
3057}
3058
Willy Tarreaud34af782008-11-30 23:36:37 +01003059/* This function is an analyser which processes the HTTP request body. It looks
3060 * for parameters to be used for the load balancing algorithm (url_param). It
3061 * must only be called after the standard HTTP request processing has occurred,
3062 * because it expects the request to be parsed. It returns zero if it needs to
3063 * read more data, or 1 once it has completed its analysis.
3064 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003065int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003066{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003067 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003068 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003069 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003070
3071 /* We have to parse the HTTP request body to find any required data.
3072 * "balance url_param check_post" should have been the only way to get
3073 * into this. We were brought here after HTTP header analysis, so all
3074 * related structures are ready.
3075 */
3076
Willy Tarreau522d6c02009-12-06 18:49:18 +01003077 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3078 goto missing_data;
3079
3080 if (msg->msg_state < HTTP_MSG_100_SENT) {
3081 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3082 * send an HTTP/1.1 100 Continue intermediate response.
3083 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003084 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003085 struct hdr_ctx ctx;
3086 ctx.idx = 0;
3087 /* Expect is allowed in 1.1, look for it */
3088 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3089 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3090 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3091 }
3092 }
3093 msg->msg_state = HTTP_MSG_100_SENT;
3094 }
3095
3096 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003097 /* we have msg->col and msg->sov which both point to the first
3098 * byte of message body. msg->som still points to the beginning
3099 * of the message. We must save the body in req->lr because it
3100 * survives buffer re-alignments.
3101 */
3102 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003103 if (txn->flags & TX_REQ_TE_CHNK)
3104 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3105 else
3106 msg->msg_state = HTTP_MSG_DATA;
3107 }
3108
3109 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003110 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003111 * set ->sov and ->lr to point to the body and switch to DATA or
3112 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003113 */
3114 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003115
Willy Tarreau115acb92009-12-26 13:56:06 +01003116 if (!ret)
3117 goto missing_data;
3118 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003119 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003120 }
3121
Willy Tarreaud98cf932009-12-27 22:54:55 +01003122 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003123 * We have the first non-header byte in msg->col, which is either the
3124 * beginning of the chunk size or of the data. The first data byte is in
3125 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3126 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003127 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003128
3129 if (msg->hdr_content_len < limit)
3130 limit = msg->hdr_content_len;
3131
Willy Tarreau7c96f672009-12-27 22:47:25 +01003132 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003133 goto http_end;
3134
3135 missing_data:
3136 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003137 if (req->flags & BF_FULL)
3138 goto return_bad_req;
3139
Willy Tarreau522d6c02009-12-06 18:49:18 +01003140 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3141 txn->status = 408;
3142 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3143 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003144 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003145
3146 /* we get here if we need to wait for more data */
3147 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003148 /* Not enough data. We'll re-use the http-request
3149 * timeout here. Ideally, we should set the timeout
3150 * relative to the accept() date. We just set the
3151 * request timeout once at the beginning of the
3152 * request.
3153 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003154 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003155 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003156 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003157 return 0;
3158 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003159
3160 http_end:
3161 /* The situation will not evolve, so let's give up on the analysis. */
3162 s->logs.tv_request = now; /* update the request timer to reflect full request */
3163 req->analysers &= ~an_bit;
3164 req->analyse_exp = TICK_ETERNITY;
3165 return 1;
3166
3167 return_bad_req: /* let's centralize all bad requests */
3168 txn->req.msg_state = HTTP_MSG_ERROR;
3169 txn->status = 400;
3170 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3171
3172 return_err_msg:
3173 req->analysers = 0;
3174 s->fe->counters.failed_req++;
3175 if (s->listener->counters)
3176 s->listener->counters->failed_req++;
3177
3178 if (!(s->flags & SN_ERR_MASK))
3179 s->flags |= SN_ERR_PRXCOND;
3180 if (!(s->flags & SN_FINST_MASK))
3181 s->flags |= SN_FINST_R;
3182 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003183}
3184
Willy Tarreaud98cf932009-12-27 22:54:55 +01003185/* This function is an analyser which forwards request body (including chunk
3186 * sizes if any). It is called as soon as we must forward, even if we forward
3187 * zero byte. The only situation where it must not be called is when we're in
3188 * tunnel mode and we want to forward till the close. It's used both to forward
3189 * remaining data and to resync after end of body. It expects the msg_state to
3190 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3191 * read more data, or 1 once we can go on with next request or end the session.
3192 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3193 * bytes of pending data + the headers if not already done (between som and sov).
3194 * It eventually adjusts som to match sov after the data in between have been sent.
3195 */
3196int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3197{
3198 struct http_txn *txn = &s->txn;
3199 struct http_msg *msg = &s->txn.req;
3200
3201 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3202 return 0;
3203
3204 /* Note that we don't have to send 100-continue back because we don't
3205 * need the data to complete our job, and it's up to the server to
3206 * decide whether to return 100, 417 or anything else in return of
3207 * an "Expect: 100-continue" header.
3208 */
3209
3210 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3211 /* we have msg->col and msg->sov which both point to the first
3212 * byte of message body. msg->som still points to the beginning
3213 * of the message. We must save the body in req->lr because it
3214 * survives buffer re-alignments.
3215 */
3216 req->lr = req->data + msg->sov;
3217 if (txn->flags & TX_REQ_TE_CHNK)
3218 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3219 else {
3220 msg->msg_state = HTTP_MSG_DATA;
3221 }
3222 }
3223
3224 /* we may already have some data pending */
3225 if (msg->hdr_content_len || msg->som != msg->sov) {
3226 buffer_forward(req, msg->sov - msg->som + msg->hdr_content_len);
3227 msg->hdr_content_len = 0; /* don't forward that again */
3228 msg->som = msg->sov;
3229 }
3230
3231 while (1) {
Willy Tarreau5523b322009-12-29 12:05:52 +01003232 // printf("req1: stq=%d, str=%d, l=%d, send_max=%d, fl=%08x, txf=%08x\n",
3233 // msg->msg_state, txn->rsp.msg_state, s->rep->l, s->rep->send_max, s->rep->flags, txn->flags);
3234
Willy Tarreaud98cf932009-12-27 22:54:55 +01003235 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
3236 /* read the chunk size and assign it to ->hdr_content_len, then
3237 * set ->sov and ->lr to point to the body and switch to DATA or
3238 * TRAILERS state.
3239 */
3240 int ret = http_parse_chunk_size(req, msg);
3241
3242 if (!ret)
3243 goto missing_data;
3244 else if (ret < 0)
3245 goto return_bad_req;
3246 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
3247
3248 /* forward the chunk size as well as any pending data */
3249 if (msg->hdr_content_len || msg->som != msg->sov) {
3250 buffer_forward(req, msg->sov - msg->som + msg->hdr_content_len);
3251 msg->hdr_content_len = 0; /* don't forward that again */
3252 msg->som = msg->sov;
3253 }
3254 }
3255 else if (msg->msg_state == HTTP_MSG_DATA) {
3256 /* must still forward */
3257 if (req->to_forward)
3258 return 0;
3259
3260 /* we're sending the last bits of request, the server's response
3261 * is expected in a short time. Most often the first read is enough
3262 * to bring all the headers, so we're preparing the response buffer
3263 * to read the response now. Note that we should probably move that
3264 * to a more appropriate place.
3265 */
Willy Tarreau5523b322009-12-29 12:05:52 +01003266 if (txn->rsp.msg_state == HTTP_MSG_RPBEFORE) {
3267 s->rep->flags &= ~BF_DONT_READ;
3268 s->rep->flags |= BF_READ_DONTWAIT;
3269 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003270
3271 /* nothing left to forward */
3272 if (txn->flags & TX_REQ_TE_CHNK)
3273 msg->msg_state = HTTP_MSG_DATA_CRLF;
3274 else {
3275 msg->msg_state = HTTP_MSG_DONE;
3276 }
3277 }
3278 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3279 /* we want the CRLF after the data */
3280 int ret;
3281
3282 if (!(req->flags & BF_OUT_EMPTY))
3283 return 0;
3284 /* The output pointer does not move anymore, next unsent data
3285 * are available at ->w. Let's save that.
3286 */
3287 req->lr = req->w;
3288 ret = http_skip_chunk_crlf(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 MSG_CHUNK_SIZE now */
3295 }
3296 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3297 int ret = http_forward_trailers(req, msg);
3298
3299 if (ret == 0)
3300 goto missing_data;
3301 else if (ret < 0)
3302 goto return_bad_req;
3303 /* we're in HTTP_MSG_DONE now */
3304 }
3305 else if (msg->msg_state == HTTP_MSG_DONE) {
Willy Tarreau5523b322009-12-29 12:05:52 +01003306 /* No need to read anymore, the request was completely parsed */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003307 req->flags |= BF_DONT_READ;
3308
Willy Tarreau5523b322009-12-29 12:05:52 +01003309 if (txn->rsp.msg_state < HTTP_MSG_DONE && txn->rsp.msg_state != HTTP_MSG_ERROR) {
3310 /* The server has not finished to respond, so we
3311 * don't want to move in order not to upset it.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003312 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003313 return 0;
3314 }
3315
3316 /* when we support keep-alive or server-close modes, we'll have
3317 * to reset the transaction here.
3318 */
Willy Tarreau5523b322009-12-29 12:05:52 +01003319
3320 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3321 if (req->flags & BF_OUT_EMPTY)
3322 msg->msg_state = HTTP_MSG_CLOSED;
3323 else
3324 msg->msg_state = HTTP_MSG_CLOSING;
3325 }
3326 else {
3327 /* for other modes, we let further requests pass for now */
3328 req->flags &= ~BF_DONT_READ;
3329 /* FIXME: we're still forced to do that here */
3330 s->rep->flags &= ~BF_DONT_READ;
3331 break;
3332 }
3333 }
3334 else if (msg->msg_state == HTTP_MSG_CLOSING) {
3335 /* nothing else to forward, just waiting for the buffer to be empty */
3336 if (!(req->flags & BF_OUT_EMPTY))
3337 return 0;
3338 msg->msg_state = HTTP_MSG_CLOSED;
3339 }
3340 else if (msg->msg_state == HTTP_MSG_CLOSED) {
3341 req->flags &= ~BF_DONT_READ;
3342 /* FIXME: we're still forced to do that here */
3343 s->rep->flags &= ~BF_DONT_READ;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003344 break;
3345 }
3346 }
3347
3348 /* OK we're done with the data phase */
3349 req->analysers &= ~an_bit;
3350 return 1;
3351
3352 missing_data:
3353 /* forward the chunk size as well as any pending data */
3354 if (msg->hdr_content_len || msg->som != msg->sov) {
3355 buffer_forward(req, msg->sov - msg->som + msg->hdr_content_len);
3356 msg->hdr_content_len = 0; /* don't forward that again */
3357 msg->som = msg->sov;
3358 }
3359
3360 if (req->flags & BF_FULL)
3361 goto return_bad_req;
3362 /* the session handler will take care of timeouts and errors */
3363 return 0;
3364
3365 return_bad_req: /* let's centralize all bad requests */
3366 txn->req.msg_state = HTTP_MSG_ERROR;
3367 txn->status = 400;
3368 /* Note: we don't send any error if some data were already sent */
3369 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3370
3371 req->analysers = 0;
3372 s->fe->counters.failed_req++;
3373 if (s->listener->counters)
3374 s->listener->counters->failed_req++;
3375
3376 if (!(s->flags & SN_ERR_MASK))
3377 s->flags |= SN_ERR_PRXCOND;
3378 if (!(s->flags & SN_FINST_MASK))
3379 s->flags |= SN_FINST_R;
3380 return 0;
3381}
3382
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003383/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3384 * processing can continue on next analysers, or zero if it either needs more
3385 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3386 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3387 * when it has nothing left to do, and may remove any analyser when it wants to
3388 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003389 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003390int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003391{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003392 struct http_txn *txn = &s->txn;
3393 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003394 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003395 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003396 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003397 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003398
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003399 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 +02003400 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003401 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003402 rep,
3403 rep->rex, rep->wex,
3404 rep->flags,
3405 rep->l,
3406 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003407
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003408 /*
3409 * Now parse the partial (or complete) lines.
3410 * We will check the response syntax, and also join multi-line
3411 * headers. An index of all the lines will be elaborated while
3412 * parsing.
3413 *
3414 * For the parsing, we use a 28 states FSM.
3415 *
3416 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003417 * rep->data + msg->som = beginning of response
3418 * rep->data + msg->eoh = end of processed headers / start of current one
3419 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003420 * rep->lr = first non-visited byte
3421 * rep->r = end of data
3422 */
3423
Willy Tarreau83e3af02009-12-28 17:39:57 +01003424 /* There's a protected area at the end of the buffer for rewriting
3425 * purposes. We don't want to start to parse the request if the
3426 * protected area is affected, because we may have to move processed
3427 * data later, which is much more complicated.
3428 */
3429 if (rep->l &&
3430 (rep->r <= rep->lr || rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3431 if (rep->send_max) {
3432 /* some data has still not left the buffer, wake us once that's done */
3433 buffer_dont_close(rep);
3434 return 0;
3435 }
3436
3437 http_buffer_heavy_realign(rep, msg);
3438 }
3439
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003440 if (likely(rep->lr < rep->r))
3441 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau7f875f62008-08-11 17:35:01 +02003442
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003443 /* 1: we might have to print this header in debug mode */
3444 if (unlikely((global.mode & MODE_DEBUG) &&
3445 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003446 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003447 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003448
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003449 sol = rep->data + msg->som;
3450 eol = sol + msg->sl.rq.l;
3451 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003452
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003453 sol += hdr_idx_first_pos(&txn->hdr_idx);
3454 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003455
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003456 while (cur_idx) {
3457 eol = sol + txn->hdr_idx.v[cur_idx].len;
3458 debug_hdr("srvhdr", s, sol, eol);
3459 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3460 cur_idx = txn->hdr_idx.v[cur_idx].next;
3461 }
3462 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003463
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003464 /*
3465 * Now we quickly check if we have found a full valid response.
3466 * If not so, we check the FD and buffer states before leaving.
3467 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01003468 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003469 * responses are checked first.
3470 *
3471 * Depending on whether the client is still there or not, we
3472 * may send an error response back or not. Note that normally
3473 * we should only check for HTTP status there, and check I/O
3474 * errors somewhere else.
3475 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003476
Willy Tarreau655dce92009-11-08 13:10:58 +01003477 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003478 /* Invalid response */
3479 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
3480 /* we detected a parsing error. We want to archive this response
3481 * in the dedicated proxy area for later troubleshooting.
3482 */
3483 hdr_response_bad:
3484 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
3485 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3486
3487 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003488 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003489 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003490 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
3491 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003492
3493 rep->analysers = 0;
3494 txn->status = 502;
3495 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
3496
3497 if (!(s->flags & SN_ERR_MASK))
3498 s->flags |= SN_ERR_PRXCOND;
3499 if (!(s->flags & SN_FINST_MASK))
3500 s->flags |= SN_FINST_H;
3501
3502 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003503 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003504
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003505 /* too large response does not fit in buffer. */
3506 else if (rep->flags & BF_FULL) {
3507 goto hdr_response_bad;
3508 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003509
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003510 /* read error */
3511 else if (rep->flags & BF_READ_ERROR) {
3512 if (msg->err_pos >= 0)
3513 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02003514
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003515 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003516 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003517 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003518 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
3519 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003520
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003521 rep->analysers = 0;
3522 txn->status = 502;
3523 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02003524
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003525 if (!(s->flags & SN_ERR_MASK))
3526 s->flags |= SN_ERR_SRVCL;
3527 if (!(s->flags & SN_FINST_MASK))
3528 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003529 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003530 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003531
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003532 /* read timeout : return a 504 to the client. */
3533 else if (rep->flags & BF_READ_TIMEOUT) {
3534 if (msg->err_pos >= 0)
3535 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003536
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003537 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003538 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003539 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003540 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
3541 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003542
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003543 rep->analysers = 0;
3544 txn->status = 504;
3545 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02003546
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003547 if (!(s->flags & SN_ERR_MASK))
3548 s->flags |= SN_ERR_SRVTO;
3549 if (!(s->flags & SN_FINST_MASK))
3550 s->flags |= SN_FINST_H;
3551 return 0;
3552 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02003553
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003554 /* close from server */
3555 else if (rep->flags & BF_SHUTR) {
3556 if (msg->err_pos >= 0)
3557 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003558
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003559 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003560 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003561 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003562 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
3563 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003564
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003565 rep->analysers = 0;
3566 txn->status = 502;
3567 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01003568
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003569 if (!(s->flags & SN_ERR_MASK))
3570 s->flags |= SN_ERR_SRVCL;
3571 if (!(s->flags & SN_FINST_MASK))
3572 s->flags |= SN_FINST_H;
3573 return 0;
3574 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003575
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003576 /* write error to client (we don't send any message then) */
3577 else if (rep->flags & BF_WRITE_ERROR) {
3578 if (msg->err_pos >= 0)
3579 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003580
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003581 s->be->counters.failed_resp++;
3582 rep->analysers = 0;
3583
3584 if (!(s->flags & SN_ERR_MASK))
3585 s->flags |= SN_ERR_CLICL;
3586 if (!(s->flags & SN_FINST_MASK))
3587 s->flags |= SN_FINST_H;
3588
3589 /* process_session() will take care of the error */
3590 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003591 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003592
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003593 buffer_dont_close(rep);
3594 return 0;
3595 }
3596
3597 /* More interesting part now : we know that we have a complete
3598 * response which at least looks like HTTP. We have an indicator
3599 * of each header's length, so we can parse them quickly.
3600 */
3601
3602 if (unlikely(msg->err_pos >= 0))
3603 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3604
3605 /* ensure we keep this pointer to the beginning of the message */
3606 msg->sol = rep->data + msg->som;
3607
3608 /*
3609 * 1: get the status code
3610 */
3611 n = rep->data[msg->sl.st.c] - '0';
3612 if (n < 1 || n > 5)
3613 n = 0;
3614 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003615
Willy Tarreau5b154472009-12-21 20:11:07 +01003616 /* check if the response is HTTP/1.1 or above */
3617 if ((msg->sl.st.v_l == 8) &&
3618 ((rep->data[msg->som + 5] > '1') ||
3619 ((rep->data[msg->som + 5] == '1') &&
3620 (rep->data[msg->som + 7] >= '1'))))
3621 txn->flags |= TX_RES_VER_11;
3622
3623 /* "connection" has not been parsed yet */
3624 txn->flags &= ~TX_CON_HDR_PARS;
3625
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003626 /* transfer length unknown*/
3627 txn->flags &= ~TX_RES_XFER_LEN;
3628
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003629 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
3630
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003631 if (txn->status >= 100 && txn->status < 500)
3632 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
3633 else
3634 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
3635
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003636 /*
3637 * 2: check for cacheability.
3638 */
3639
3640 switch (txn->status) {
3641 case 200:
3642 case 203:
3643 case 206:
3644 case 300:
3645 case 301:
3646 case 410:
3647 /* RFC2616 @13.4:
3648 * "A response received with a status code of
3649 * 200, 203, 206, 300, 301 or 410 MAY be stored
3650 * by a cache (...) unless a cache-control
3651 * directive prohibits caching."
3652 *
3653 * RFC2616 @9.5: POST method :
3654 * "Responses to this method are not cacheable,
3655 * unless the response includes appropriate
3656 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003657 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003658 if (likely(txn->meth != HTTP_METH_POST) &&
3659 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3660 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3661 break;
3662 default:
3663 break;
3664 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003665
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003666 /*
3667 * 3: we may need to capture headers
3668 */
3669 s->logs.logwait &= ~LW_RESP;
3670 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
3671 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3672 txn->rsp.cap, s->fe->rsp_cap);
3673
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003674 /* 4: determine the transfer-length.
3675 * According to RFC2616 #4.4, amended by the HTTPbis working group,
3676 * the presence of a message-body in a RESPONSE and its transfer length
3677 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003678 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003679 * All responses to the HEAD request method MUST NOT include a
3680 * message-body, even though the presence of entity-header fields
3681 * might lead one to believe they do. All 1xx (informational), 204
3682 * (No Content), and 304 (Not Modified) responses MUST NOT include a
3683 * message-body. All other responses do include a message-body,
3684 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003685 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003686 * 1. Any response which "MUST NOT" include a message-body (such as the
3687 * 1xx, 204 and 304 responses and any response to a HEAD request) is
3688 * always terminated by the first empty line after the header fields,
3689 * regardless of the entity-header fields present in the message.
3690 *
3691 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
3692 * the "chunked" transfer-coding (Section 6.2) is used, the
3693 * transfer-length is defined by the use of this transfer-coding.
3694 * If a Transfer-Encoding header field is present and the "chunked"
3695 * transfer-coding is not present, the transfer-length is defined by
3696 * the sender closing the connection.
3697 *
3698 * 3. If a Content-Length header field is present, its decimal value in
3699 * OCTETs represents both the entity-length and the transfer-length.
3700 * If a message is received with both a Transfer-Encoding header
3701 * field and a Content-Length header field, the latter MUST be ignored.
3702 *
3703 * 4. If the message uses the media type "multipart/byteranges", and
3704 * the transfer-length is not otherwise specified, then this self-
3705 * delimiting media type defines the transfer-length. This media
3706 * type MUST NOT be used unless the sender knows that the recipient
3707 * can parse it; the presence in a request of a Range header with
3708 * multiple byte-range specifiers from a 1.1 client implies that the
3709 * client can parse multipart/byteranges responses.
3710 *
3711 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003712 */
3713
3714 /* Skip parsing if no content length is possible. The response flags
3715 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003716 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003717 * FIXME: should we parse anyway and return an error on chunked encoding ?
3718 */
3719 if (txn->meth == HTTP_METH_HEAD ||
3720 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003721 txn->status == 204 || txn->status == 304) {
3722 txn->flags |= TX_RES_XFER_LEN;
3723 goto skip_content_length;
3724 }
3725
3726 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003727 goto skip_content_length;
3728
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003729 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003730 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003731 while ((txn->flags & TX_RES_VER_11) &&
3732 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003733 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
3734 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3735 else if (txn->flags & TX_RES_TE_CHNK) {
3736 /* bad transfer-encoding (chunked followed by something else) */
3737 use_close_only = 1;
3738 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3739 break;
3740 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003741 }
3742
3743 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
3744 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003745 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003746 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
3747 signed long long cl;
3748
3749 if (!ctx.vlen)
3750 goto hdr_response_bad;
3751
3752 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
3753 goto hdr_response_bad; /* parse failure */
3754
3755 if (cl < 0)
3756 goto hdr_response_bad;
3757
3758 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
3759 goto hdr_response_bad; /* already specified, was different */
3760
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003761 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003762 msg->hdr_content_len = cl;
3763 }
3764
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003765 /* FIXME: we should also implement the multipart/byterange method.
3766 * For now on, we resort to close mode in this case (unknown length).
3767 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003768skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003769
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003770 /* end of job, return OK */
3771 rep->analysers &= ~an_bit;
3772 rep->analyse_exp = TICK_ETERNITY;
3773 return 1;
3774}
3775
3776/* This function performs all the processing enabled for the current response.
3777 * It normally returns zero, but may return 1 if it absolutely needs to be
3778 * called again after other functions. It relies on buffers flags, and updates
3779 * t->rep->analysers. It might make sense to explode it into several other
3780 * functions. It works like process_request (see indications above).
3781 */
3782int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
3783{
3784 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003785 struct http_msg *msg = &txn->rsp;
3786 struct proxy *cur_proxy;
3787 int cur_idx;
Willy Tarreau5b154472009-12-21 20:11:07 +01003788 int conn_ka = 0, conn_cl = 0;
3789 int must_close = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003790
3791 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3792 now_ms, __FUNCTION__,
3793 t,
3794 rep,
3795 rep->rex, rep->wex,
3796 rep->flags,
3797 rep->l,
3798 rep->analysers);
3799
Willy Tarreau655dce92009-11-08 13:10:58 +01003800 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003801 return 0;
3802
3803 rep->analysers &= ~an_bit;
3804 rep->analyse_exp = TICK_ETERNITY;
3805
Willy Tarreau5b154472009-12-21 20:11:07 +01003806 /* Now we have to check if we need to modify the Connection header.
3807 * This is more difficult on the response than it is on the request,
3808 * because we can have two different HTTP versions and we don't know
3809 * how the client will interprete a response. For instance, let's say
3810 * that the client sends a keep-alive request in HTTP/1.0 and gets an
3811 * HTTP/1.1 response without any header. Maybe it will bound itself to
3812 * HTTP/1.0 because it only knows about it, and will consider the lack
3813 * of header as a close, or maybe it knows HTTP/1.1 and can consider
3814 * the lack of header as a keep-alive. Thus we will use two flags
3815 * indicating how a request MAY be understood by the client. In case
3816 * of multiple possibilities, we'll fix the header to be explicit. If
3817 * ambiguous cases such as both close and keepalive are seen, then we
3818 * will fall back to explicit close. Note that we won't take risks with
3819 * HTTP/1.0 clients which may not necessarily understand keep-alive.
3820 */
3821
3822 if ((txn->meth != HTTP_METH_CONNECT) &&
3823 (txn->status >= 200) &&
3824 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN &&
3825 !(txn->flags & TX_CON_HDR_PARS)) {
3826 int may_keep = 0, may_close = 0; /* how it may be understood */
3827 struct hdr_ctx ctx;
3828
3829 ctx.idx = 0;
3830 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
3831 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
3832 conn_cl = 1;
3833 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
3834 conn_ka = 1;
3835 }
3836
3837 if (conn_cl) {
3838 /* close header present */
3839 may_close = 1;
3840 if (conn_ka) /* we have both close and keep-alive */
3841 may_keep = 1;
3842 }
3843 else if (conn_ka) {
3844 /* keep-alive alone */
3845 may_keep = 1;
3846 }
3847 else {
3848 /* no close nor keep-alive header */
3849 if (txn->flags & TX_RES_VER_11)
3850 may_keep = 1;
3851 else
3852 may_close = 1;
3853
3854 if (txn->flags & TX_REQ_VER_11)
3855 may_keep = 1;
3856 else
3857 may_close = 1;
3858 }
3859
3860 /* let's update the transaction status to reflect any close.
3861 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003862 * handled. We also explicitly state that we will close in
3863 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01003864 */
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003865 if (may_close || !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01003866 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3867
3868 /* Now we must adjust the response header :
3869 * - set "close" if may_keep and WANT_CLO
3870 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
3871 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
3872 *
3873 * Until we support the server-close mode, we'll only support the set "close".
3874 */
3875 if (may_keep && (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO)
3876 must_close = 1;
3877
3878 txn->flags |= TX_CON_HDR_PARS;
3879 }
3880
3881 /* We might have to check for "Connection:" if the server
3882 * returns a connection status that is not compatible with
3883 * the client's or with the config.
3884 */
3885 if ((txn->status >= 200) && must_close && (conn_cl|conn_ka)) {
3886 char *cur_ptr, *cur_end, *cur_next;
3887 int cur_idx, old_idx, delta, val;
3888 int must_delete;
3889 struct hdr_idx_elem *cur_hdr;
3890
3891 /* we just have to remove the headers if both sides are 1.0 */
3892 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
3893 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3894
3895 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
3896 cur_hdr = &txn->hdr_idx.v[cur_idx];
3897 cur_ptr = cur_next;
3898 cur_end = cur_ptr + cur_hdr->len;
3899 cur_next = cur_end + cur_hdr->cr + 1;
3900
3901 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3902 if (!val)
3903 continue;
3904
3905 /* 3 possibilities :
3906 * - we have already set "Connection: close" or we're in
3907 * HTTP/1.0, so we remove this line.
3908 * - we have not yet set "Connection: close", but this line
3909 * indicates close. We leave it untouched and set the flag.
3910 * - we have not yet set "Connection: close", and this line
3911 * indicates non-close. We replace it and set the flag.
3912 */
3913 if (must_delete) {
3914 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3915 http_msg_move_end(&txn->rsp, delta);
3916 cur_next += delta;
3917 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3918 txn->hdr_idx.used--;
3919 cur_hdr->len = 0;
3920 must_close = 0;
3921 } else {
3922 if (cur_end - cur_ptr - val != 5 ||
3923 strncasecmp(cur_ptr + val, "close", 5) != 0) {
3924 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3925 "close", 5);
3926 cur_next += delta;
3927 cur_hdr->len += delta;
3928 http_msg_move_end(&txn->rsp, delta);
3929 }
3930 must_delete = 1;
3931 must_close = 0;
3932 }
3933 } /* for loop */
3934 } /* if must close keep-alive */
3935
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003936 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003937 /*
3938 * 3: we will have to evaluate the filters.
3939 * As opposed to version 1.2, now they will be evaluated in the
3940 * filters order and not in the header order. This means that
3941 * each filter has to be validated among all headers.
3942 *
3943 * Filters are tried with ->be first, then with ->fe if it is
3944 * different from ->be.
3945 */
3946
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003947 cur_proxy = t->be;
3948 while (1) {
3949 struct proxy *rule_set = cur_proxy;
3950
3951 /* try headers filters */
3952 if (rule_set->rsp_exp != NULL) {
3953 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3954 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003955 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003956 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003957 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
3958 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003959 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003960 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02003961 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003962 txn->status = 502;
Willy Tarreau8e89b842009-10-18 23:56:35 +02003963 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003964 if (!(t->flags & SN_ERR_MASK))
3965 t->flags |= SN_ERR_PRXCOND;
3966 if (!(t->flags & SN_FINST_MASK))
3967 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02003968 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003969 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003970 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003971
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003972 /* has the response been denied ? */
3973 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01003974 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003975 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003976
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003977 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003978 if (t->listener->counters)
3979 t->listener->counters->denied_resp++;
3980
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003981 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003982 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003983
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003984 /* add response headers from the rule sets in the same order */
3985 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau816b9792009-09-15 21:25:21 +02003986 if (txn->status < 200)
3987 break;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003988 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003989 rule_set->rsp_add[cur_idx]) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003990 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003991 }
3992
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003993 /* check whether we're already working on the frontend */
3994 if (cur_proxy == t->fe)
3995 break;
3996 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003997 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003998
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003999 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004000 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4001 * in which case this is not the right response, and we're waiting for the
4002 * next one. Let's allow this response to go to the client and wait for the
4003 * next one.
4004 */
4005 if (txn->status < 200) {
4006 hdr_idx_init(&txn->hdr_idx);
4007 buffer_forward(rep, rep->lr - (rep->data + msg->som));
4008 msg->msg_state = HTTP_MSG_RPBEFORE;
4009 txn->status = 0;
4010 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4011 return 1;
4012 }
4013
4014 /* we don't have any 1xx status code now */
4015
4016 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004017 * 4: check for server cookie.
4018 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004019 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4020 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004021 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004022
Willy Tarreaubaaee002006-06-26 02:48:02 +02004023
Willy Tarreaua15645d2007-03-18 16:22:39 +01004024 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004025 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004026 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004027 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004028 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004029
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004030 /*
4031 * 6: add server cookie in the response if needed
4032 */
4033 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004034 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004035 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004036
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004037 /* the server is known, it's not the one the client requested, we have to
4038 * insert a set-cookie here, except if we want to insert only on POST
4039 * requests and this one isn't. Note that servers which don't have cookies
4040 * (eg: some backup servers) will return a full cookie removal request.
4041 */
4042 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4043 t->be->cookie_name,
4044 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004045
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004046 if (t->be->cookie_domain)
4047 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004048
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004049 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004050 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004051 goto return_bad_resp;
4052 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004053
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004054 /* Here, we will tell an eventual cache on the client side that we don't
4055 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4056 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4057 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4058 */
4059 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004060
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004061 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4062
4063 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004064 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004065 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004066 }
4067 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004068
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004069 /*
4070 * 7: check if result will be cacheable with a cookie.
4071 * We'll block the response if security checks have caught
4072 * nasty things such as a cacheable cookie.
4073 */
4074 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4075 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004076 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004077
4078 /* we're in presence of a cacheable response containing
4079 * a set-cookie header. We'll block it as requested by
4080 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004081 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004082 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004083 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004084
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004085 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004086 if (t->listener->counters)
4087 t->listener->counters->denied_resp++;
4088
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004089 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4090 t->be->id, t->srv?t->srv->id:"<dispatch>");
4091 send_log(t->be, LOG_ALERT,
4092 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4093 t->be->id, t->srv?t->srv->id:"<dispatch>");
4094 goto return_srv_prx_502;
4095 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004096
4097 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004098 * 8: add "Connection: close" if needed and not yet set. This is
4099 * only needed for 1.1 responses since we know there is no other
4100 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004101 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004102 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004103 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004104 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004105 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004106 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004107 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004108
Willy Tarreaud98cf932009-12-27 22:54:55 +01004109 if (txn->flags & TX_RES_XFER_LEN)
4110 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004111
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004112 /*************************************************************
4113 * OK, that's finished for the headers. We have done what we *
4114 * could. Let's switch to the DATA state. *
4115 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004116
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004117 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004118
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004119 /* if the user wants to log as soon as possible, without counting
4120 * bytes from the server, then this is the right moment. We have
4121 * to temporarily assign bytes_out to log what we currently have.
4122 */
4123 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4124 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4125 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004126 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004127 t->logs.bytes_out = 0;
4128 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004129
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004130 /* Note: we must not try to cheat by jumping directly to DATA,
4131 * otherwise we would not let the client side wake up.
4132 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004133
Willy Tarreaudafde432008-08-17 01:00:46 +02004134 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004135 }
4136 return 0;
4137}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004138
Willy Tarreaud98cf932009-12-27 22:54:55 +01004139/* This function is an analyser which forwards response body (including chunk
4140 * sizes if any). It is called as soon as we must forward, even if we forward
4141 * zero byte. The only situation where it must not be called is when we're in
4142 * tunnel mode and we want to forward till the close. It's used both to forward
4143 * remaining data and to resync after end of body. It expects the msg_state to
4144 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4145 * read more data, or 1 once we can go on with next request or end the session.
4146 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4147 * bytes of pending data + the headers if not already done (between som and sov).
4148 * It eventually adjusts som to match sov after the data in between have been sent.
4149 */
4150int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4151{
4152 struct http_txn *txn = &s->txn;
4153 struct http_msg *msg = &s->txn.rsp;
4154
4155 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4156 return 0;
4157
4158 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4159 /* we have msg->col and msg->sov which both point to the first
4160 * byte of message body. msg->som still points to the beginning
4161 * of the message. We must save the body in req->lr because it
4162 * survives buffer re-alignments.
4163 */
4164 res->lr = res->data + msg->sov;
4165 if (txn->flags & TX_RES_TE_CHNK)
4166 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4167 else {
4168 msg->msg_state = HTTP_MSG_DATA;
4169 }
4170 }
4171
4172 /* we may already have some data pending */
4173 if (msg->hdr_content_len || msg->som != msg->sov) {
4174 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4175 msg->hdr_content_len = 0; /* don't forward that again */
4176 msg->som = msg->sov;
4177 }
4178
4179 while (1) {
Willy Tarreau5523b322009-12-29 12:05:52 +01004180 // printf("res1: stq=%d, str=%d, l=%d, send_max=%d, fl=%08x, txf=%08x\n",
4181 // txn->req.msg_state, msg->msg_state, s->rep->l, s->rep->send_max, s->rep->flags, txn->flags);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004182 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
4183 /* read the chunk size and assign it to ->hdr_content_len, then
4184 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4185 */
4186 int ret = http_parse_chunk_size(res, msg);
4187
4188 if (!ret)
4189 goto missing_data;
4190 else if (ret < 0)
4191 goto return_bad_res;
4192 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
4193
4194 /* forward the chunk size as well as any pending data */
4195 if (msg->hdr_content_len || msg->som != msg->sov) {
4196 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4197 msg->hdr_content_len = 0; /* don't forward that again */
4198 msg->som = msg->sov;
4199 }
4200 }
4201 else if (msg->msg_state == HTTP_MSG_DATA) {
4202 /* must still forward */
4203 if (res->to_forward)
4204 return 0;
4205
4206 /* nothing left to forward */
4207 if (txn->flags & TX_RES_TE_CHNK)
4208 msg->msg_state = HTTP_MSG_DATA_CRLF;
4209 else {
4210 msg->msg_state = HTTP_MSG_DONE;
4211 }
4212 }
4213 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4214 /* we want the CRLF after the data */
4215 int ret;
4216
4217 if (!(res->flags & BF_OUT_EMPTY))
4218 return 0;
4219 /* The output pointer does not move anymore, next unsent data
4220 * are available at ->w. Let's save that.
4221 */
4222 res->lr = res->w;
4223 ret = http_skip_chunk_crlf(res, msg);
4224
4225 if (!ret)
4226 goto missing_data;
4227 else if (ret < 0)
4228 goto return_bad_res;
4229 /* we're in MSG_CHUNK_SIZE now */
4230 }
4231 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4232 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004233
Willy Tarreaud98cf932009-12-27 22:54:55 +01004234 if (ret == 0)
4235 goto missing_data;
4236 else if (ret < 0)
4237 goto return_bad_res;
4238 /* we're in HTTP_MSG_DONE now */
4239 }
4240 else if (msg->msg_state == HTTP_MSG_DONE) {
Willy Tarreau5523b322009-12-29 12:05:52 +01004241 /* In theory, we don't need to read anymore, but we must
4242 * still monitor the server connection for a possible close,
4243 * so we don't set the BF_DONT_READ flag here.
4244 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004245
Willy Tarreau5523b322009-12-29 12:05:52 +01004246 if (txn->req.msg_state < HTTP_MSG_DONE && txn->req.msg_state != HTTP_MSG_ERROR) {
4247 /* The client seems to still be sending data, probably
4248 * because we got an error response during an upload.
4249 * We have the choice of either breaking the connection
4250 * or letting it pass through. Let's do the later.
4251 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004252 return 0;
Willy Tarreau5523b322009-12-29 12:05:52 +01004253 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004254
4255 /* when we support keep-alive or server-close modes, we'll have
4256 * to reset the transaction here.
4257 */
Willy Tarreau5523b322009-12-29 12:05:52 +01004258
4259 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4260 if (res->flags & BF_OUT_EMPTY)
4261 msg->msg_state = HTTP_MSG_CLOSED;
4262 else
4263 msg->msg_state = HTTP_MSG_CLOSING;
4264 }
4265 else {
4266 /* for other modes, we let further responses pass for now */
4267 res->flags &= ~BF_DONT_READ;
4268 /* FIXME: we're still forced to do that here */
4269 s->req->flags &= ~BF_DONT_READ;
4270 break;
4271 }
4272 }
4273 else if (msg->msg_state == HTTP_MSG_CLOSING) {
4274 /* nothing else to forward, just waiting for the buffer to be empty */
4275 if (!(res->flags & BF_OUT_EMPTY))
4276 return 0;
4277 msg->msg_state = HTTP_MSG_CLOSED;
4278 }
4279 else if (msg->msg_state == HTTP_MSG_CLOSED) {
4280 res->flags &= ~BF_DONT_READ;
4281 /* FIXME: we're still forced to do that here */
4282 s->req->flags &= ~BF_DONT_READ;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004283 break;
4284 }
4285 }
4286
4287 res->analysers &= ~an_bit;
4288 return 1;
4289
4290 missing_data:
4291 /* forward the chunk size as well as any pending data */
4292 if (msg->hdr_content_len || msg->som != msg->sov) {
4293 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4294 msg->hdr_content_len = 0; /* don't forward that again */
4295 msg->som = msg->sov;
4296 }
4297
4298 if (res->flags & BF_FULL)
4299 goto return_bad_res;
4300 /* the session handler will take care of timeouts and errors */
4301 return 0;
4302
4303 return_bad_res: /* let's centralize all bad resuests */
4304 txn->rsp.msg_state = HTTP_MSG_ERROR;
4305 txn->status = 502;
4306 stream_int_cond_close(res->cons, NULL);
4307
4308 res->analysers = 0;
4309 s->be->counters.failed_resp++;
4310 if (s->srv) {
4311 s->srv->counters.failed_resp++;
4312 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4313 }
4314
4315 if (!(s->flags & SN_ERR_MASK))
4316 s->flags |= SN_ERR_PRXCOND;
4317 if (!(s->flags & SN_FINST_MASK))
4318 s->flags |= SN_FINST_R;
4319 return 0;
4320}
4321
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004322/* Iterate the same filter through all request headers.
4323 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004324 * Since it can manage the switch to another backend, it updates the per-proxy
4325 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004326 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004327int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004328{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004329 char term;
4330 char *cur_ptr, *cur_end, *cur_next;
4331 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004332 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004333 struct hdr_idx_elem *cur_hdr;
4334 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004335
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004336 last_hdr = 0;
4337
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004338 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004339 old_idx = 0;
4340
4341 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004342 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004343 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004344 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004345 (exp->action == ACT_ALLOW ||
4346 exp->action == ACT_DENY ||
4347 exp->action == ACT_TARPIT))
4348 return 0;
4349
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004350 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004351 if (!cur_idx)
4352 break;
4353
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004354 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004355 cur_ptr = cur_next;
4356 cur_end = cur_ptr + cur_hdr->len;
4357 cur_next = cur_end + cur_hdr->cr + 1;
4358
4359 /* Now we have one header between cur_ptr and cur_end,
4360 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004361 */
4362
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004363 /* The annoying part is that pattern matching needs
4364 * that we modify the contents to null-terminate all
4365 * strings before testing them.
4366 */
4367
4368 term = *cur_end;
4369 *cur_end = '\0';
4370
4371 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4372 switch (exp->action) {
4373 case ACT_SETBE:
4374 /* It is not possible to jump a second time.
4375 * FIXME: should we return an HTTP/500 here so that
4376 * the admin knows there's a problem ?
4377 */
4378 if (t->be != t->fe)
4379 break;
4380
4381 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004382 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004383 last_hdr = 1;
4384 break;
4385
4386 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004387 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004388 last_hdr = 1;
4389 break;
4390
4391 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004392 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004393 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004394
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004395 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004396 if (t->listener->counters)
4397 t->listener->counters->denied_resp++;
4398
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004399 break;
4400
4401 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004402 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004403 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004404
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004405 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004406 if (t->listener->counters)
4407 t->listener->counters->denied_resp++;
4408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004409 break;
4410
4411 case ACT_REPLACE:
4412 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4413 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4414 /* FIXME: if the user adds a newline in the replacement, the
4415 * index will not be recalculated for now, and the new line
4416 * will not be counted as a new header.
4417 */
4418
4419 cur_end += delta;
4420 cur_next += delta;
4421 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004422 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004423 break;
4424
4425 case ACT_REMOVE:
4426 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4427 cur_next += delta;
4428
Willy Tarreaufa355d42009-11-29 18:12:29 +01004429 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004430 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4431 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004432 cur_hdr->len = 0;
4433 cur_end = NULL; /* null-term has been rewritten */
4434 break;
4435
4436 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004437 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004438 if (cur_end)
4439 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004440
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004441 /* keep the link from this header to next one in case of later
4442 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004443 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004444 old_idx = cur_idx;
4445 }
4446 return 0;
4447}
4448
4449
4450/* Apply the filter to the request line.
4451 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4452 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004453 * Since it can manage the switch to another backend, it updates the per-proxy
4454 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004455 */
4456int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4457{
4458 char term;
4459 char *cur_ptr, *cur_end;
4460 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004461 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004462 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004463
Willy Tarreau58f10d72006-12-04 02:26:12 +01004464
Willy Tarreau3d300592007-03-18 18:34:41 +01004465 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004466 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004467 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004468 (exp->action == ACT_ALLOW ||
4469 exp->action == ACT_DENY ||
4470 exp->action == ACT_TARPIT))
4471 return 0;
4472 else if (exp->action == ACT_REMOVE)
4473 return 0;
4474
4475 done = 0;
4476
Willy Tarreau9cdde232007-05-02 20:58:19 +02004477 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004478 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004479
4480 /* Now we have the request line between cur_ptr and cur_end */
4481
4482 /* The annoying part is that pattern matching needs
4483 * that we modify the contents to null-terminate all
4484 * strings before testing them.
4485 */
4486
4487 term = *cur_end;
4488 *cur_end = '\0';
4489
4490 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4491 switch (exp->action) {
4492 case ACT_SETBE:
4493 /* It is not possible to jump a second time.
4494 * FIXME: should we return an HTTP/500 here so that
4495 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004496 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004497 if (t->be != t->fe)
4498 break;
4499
4500 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004501 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004502 done = 1;
4503 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004504
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004505 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004506 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004507 done = 1;
4508 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004509
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004510 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004511 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004512
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004513 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004514 if (t->listener->counters)
4515 t->listener->counters->denied_resp++;
4516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004517 done = 1;
4518 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004520 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004521 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004522
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004523 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004524 if (t->listener->counters)
4525 t->listener->counters->denied_resp++;
4526
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004527 done = 1;
4528 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004529
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004530 case ACT_REPLACE:
4531 *cur_end = term; /* restore the string terminator */
4532 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4533 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4534 /* FIXME: if the user adds a newline in the replacement, the
4535 * index will not be recalculated for now, and the new line
4536 * will not be counted as a new header.
4537 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004538
Willy Tarreaufa355d42009-11-29 18:12:29 +01004539 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004540 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004541
Willy Tarreau9cdde232007-05-02 20:58:19 +02004542 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004543 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004544 HTTP_MSG_RQMETH,
4545 cur_ptr, cur_end + 1,
4546 NULL, NULL);
4547 if (unlikely(!cur_end))
4548 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004549
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004550 /* we have a full request and we know that we have either a CR
4551 * or an LF at <ptr>.
4552 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004553 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4554 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004555 /* there is no point trying this regex on headers */
4556 return 1;
4557 }
4558 }
4559 *cur_end = term; /* restore the string terminator */
4560 return done;
4561}
Willy Tarreau97de6242006-12-27 17:18:38 +01004562
Willy Tarreau58f10d72006-12-04 02:26:12 +01004563
Willy Tarreau58f10d72006-12-04 02:26:12 +01004564
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004565/*
4566 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4567 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004568 * unparsable request. Since it can manage the switch to another backend, it
4569 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004570 */
4571int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4572{
Willy Tarreau3d300592007-03-18 18:34:41 +01004573 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004574 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004575 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004576 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004577
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004578 /*
4579 * The interleaving of transformations and verdicts
4580 * makes it difficult to decide to continue or stop
4581 * the evaluation.
4582 */
4583
Willy Tarreau3d300592007-03-18 18:34:41 +01004584 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004585 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4586 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4587 exp = exp->next;
4588 continue;
4589 }
4590
4591 /* Apply the filter to the request line. */
4592 ret = apply_filter_to_req_line(t, req, exp);
4593 if (unlikely(ret < 0))
4594 return -1;
4595
4596 if (likely(ret == 0)) {
4597 /* The filter did not match the request, it can be
4598 * iterated through all headers.
4599 */
4600 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004601 }
4602 exp = exp->next;
4603 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004604 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004605}
4606
4607
Willy Tarreaua15645d2007-03-18 16:22:39 +01004608
Willy Tarreau58f10d72006-12-04 02:26:12 +01004609/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004610 * Try to retrieve the server associated to the appsession.
4611 * If the server is found, it's assigned to the session.
4612 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01004613void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004614 struct http_txn *txn = &t->txn;
4615 appsess *asession = NULL;
4616 char *sessid_temp = NULL;
4617
Cyril Bontéb21570a2009-11-29 20:04:48 +01004618 if (len > t->be->appsession_len) {
4619 len = t->be->appsession_len;
4620 }
4621
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004622 if (t->be->options2 & PR_O2_AS_REQL) {
4623 /* request-learn option is enabled : store the sessid in the session for future use */
4624 if (t->sessid != NULL) {
4625 /* free previously allocated memory as we don't need the session id found in the URL anymore */
4626 pool_free2(apools.sessid, t->sessid);
4627 }
4628
4629 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
4630 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4631 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4632 return;
4633 }
4634
Cyril Bontéb21570a2009-11-29 20:04:48 +01004635 memcpy(t->sessid, buf, len);
4636 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004637 }
4638
4639 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
4640 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4641 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4642 return;
4643 }
4644
Cyril Bontéb21570a2009-11-29 20:04:48 +01004645 memcpy(sessid_temp, buf, len);
4646 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004647
4648 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
4649 /* free previously allocated memory */
4650 pool_free2(apools.sessid, sessid_temp);
4651
4652 if (asession != NULL) {
4653 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
4654 if (!(t->be->options2 & PR_O2_AS_REQL))
4655 asession->request_count++;
4656
4657 if (asession->serverid != NULL) {
4658 struct server *srv = t->be->srv;
4659 while (srv) {
4660 if (strcmp(srv->id, asession->serverid) == 0) {
4661 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
4662 /* we found the server and it's usable */
4663 txn->flags &= ~TX_CK_MASK;
4664 txn->flags |= TX_CK_VALID;
4665 t->flags |= SN_DIRECT | SN_ASSIGNED;
4666 t->srv = srv;
4667 break;
4668 } else {
4669 txn->flags &= ~TX_CK_MASK;
4670 txn->flags |= TX_CK_DOWN;
4671 }
4672 }
4673 srv = srv->next;
4674 }
4675 }
4676 }
4677}
4678
4679/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004680 * Manage client-side cookie. It can impact performance by about 2% so it is
4681 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004682 */
4683void manage_client_side_cookies(struct session *t, struct buffer *req)
4684{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004685 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004686 char *p1, *p2, *p3, *p4;
4687 char *del_colon, *del_cookie, *colon;
4688 int app_cookies;
4689
Willy Tarreau58f10d72006-12-04 02:26:12 +01004690 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004691 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004692
Willy Tarreau2a324282006-12-05 00:05:46 +01004693 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004694 * we start with the start line.
4695 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004696 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004697 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004698
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004699 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004700 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004701 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004702
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004703 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004704 cur_ptr = cur_next;
4705 cur_end = cur_ptr + cur_hdr->len;
4706 cur_next = cur_end + cur_hdr->cr + 1;
4707
4708 /* We have one full header between cur_ptr and cur_end, and the
4709 * next header starts at cur_next. We're only interested in
4710 * "Cookie:" headers.
4711 */
4712
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004713 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4714 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004715 old_idx = cur_idx;
4716 continue;
4717 }
4718
4719 /* Now look for cookies. Conforming to RFC2109, we have to support
4720 * attributes whose name begin with a '$', and associate them with
4721 * the right cookie, if we want to delete this cookie.
4722 * So there are 3 cases for each cookie read :
4723 * 1) it's a special attribute, beginning with a '$' : ignore it.
4724 * 2) it's a server id cookie that we *MAY* want to delete : save
4725 * some pointers on it (last semi-colon, beginning of cookie...)
4726 * 3) it's an application cookie : we *MAY* have to delete a previous
4727 * "special" cookie.
4728 * At the end of loop, if a "special" cookie remains, we may have to
4729 * remove it. If no application cookie persists in the header, we
4730 * *MUST* delete it
4731 */
4732
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004733 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004734
Willy Tarreau58f10d72006-12-04 02:26:12 +01004735 /* del_cookie == NULL => nothing to be deleted */
4736 del_colon = del_cookie = NULL;
4737 app_cookies = 0;
4738
4739 while (p1 < cur_end) {
4740 /* skip spaces and colons, but keep an eye on these ones */
4741 while (p1 < cur_end) {
4742 if (*p1 == ';' || *p1 == ',')
4743 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004744 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004745 break;
4746 p1++;
4747 }
4748
4749 if (p1 == cur_end)
4750 break;
4751
4752 /* p1 is at the beginning of the cookie name */
4753 p2 = p1;
4754 while (p2 < cur_end && *p2 != '=')
4755 p2++;
4756
4757 if (p2 == cur_end)
4758 break;
4759
4760 p3 = p2 + 1; /* skips the '=' sign */
4761 if (p3 == cur_end)
4762 break;
4763
4764 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004765 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004766 p4++;
4767
4768 /* here, we have the cookie name between p1 and p2,
4769 * and its value between p3 and p4.
4770 * we can process it :
4771 *
4772 * Cookie: NAME=VALUE;
4773 * | || || |
4774 * | || || +--> p4
4775 * | || |+-------> p3
4776 * | || +--------> p2
4777 * | |+------------> p1
4778 * | +-------------> colon
4779 * +--------------------> cur_ptr
4780 */
4781
4782 if (*p1 == '$') {
4783 /* skip this one */
4784 }
4785 else {
4786 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004787 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004788 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004789 (p4 - p1 >= t->fe->capture_namelen) &&
4790 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004791 int log_len = p4 - p1;
4792
Willy Tarreau086b3b42007-05-13 21:45:51 +02004793 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004794 Alert("HTTP logging : out of memory.\n");
4795 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004796 if (log_len > t->fe->capture_len)
4797 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004798 memcpy(txn->cli_cookie, p1, log_len);
4799 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004800 }
4801 }
4802
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004803 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4804 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004805 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004806 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004807 char *delim;
4808
4809 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4810 * have the server ID betweek p3 and delim, and the original cookie between
4811 * delim+1 and p4. Otherwise, delim==p4 :
4812 *
4813 * Cookie: NAME=SRV~VALUE;
4814 * | || || | |
4815 * | || || | +--> p4
4816 * | || || +--------> delim
4817 * | || |+-----------> p3
4818 * | || +------------> p2
4819 * | |+----------------> p1
4820 * | +-----------------> colon
4821 * +------------------------> cur_ptr
4822 */
4823
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004824 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004825 for (delim = p3; delim < p4; delim++)
4826 if (*delim == COOKIE_DELIM)
4827 break;
4828 }
4829 else
4830 delim = p4;
4831
4832
4833 /* Here, we'll look for the first running server which supports the cookie.
4834 * This allows to share a same cookie between several servers, for example
4835 * to dedicate backup servers to specific servers only.
4836 * However, to prevent clients from sticking to cookie-less backup server
4837 * when they have incidentely learned an empty cookie, we simply ignore
4838 * empty cookies and mark them as invalid.
4839 */
4840 if (delim == p3)
4841 srv = NULL;
4842
4843 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004844 if (srv->cookie && (srv->cklen == delim - p3) &&
4845 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004846 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004847 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004848 txn->flags &= ~TX_CK_MASK;
4849 txn->flags |= TX_CK_VALID;
4850 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004851 t->srv = srv;
4852 break;
4853 } else {
4854 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004855 txn->flags &= ~TX_CK_MASK;
4856 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004857 }
4858 }
4859 srv = srv->next;
4860 }
4861
Willy Tarreau3d300592007-03-18 18:34:41 +01004862 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004863 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004864 txn->flags &= ~TX_CK_MASK;
4865 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004866 }
4867
4868 /* depending on the cookie mode, we may have to either :
4869 * - delete the complete cookie if we're in insert+indirect mode, so that
4870 * the server never sees it ;
4871 * - remove the server id from the cookie value, and tag the cookie as an
4872 * application cookie so that it does not get accidentely removed later,
4873 * if we're in cookie prefix mode
4874 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004875 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004876 int delta; /* negative */
4877
4878 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4879 p4 += delta;
4880 cur_end += delta;
4881 cur_next += delta;
4882 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004883 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004884
4885 del_cookie = del_colon = NULL;
4886 app_cookies++; /* protect the header from deletion */
4887 }
4888 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004889 (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 +01004890 del_cookie = p1;
4891 del_colon = colon;
4892 }
4893 } else {
4894 /* now we know that we must keep this cookie since it's
4895 * not ours. But if we wanted to delete our cookie
4896 * earlier, we cannot remove the complete header, but we
4897 * can remove the previous block itself.
4898 */
4899 app_cookies++;
4900
4901 if (del_cookie != NULL) {
4902 int delta; /* negative */
4903
4904 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4905 p4 += delta;
4906 cur_end += delta;
4907 cur_next += delta;
4908 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004909 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004910 del_cookie = del_colon = NULL;
4911 }
4912 }
4913
Cyril Bontéb21570a2009-11-29 20:04:48 +01004914 if (t->be->appsession_name != NULL) {
4915 int cmp_len, value_len;
4916 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004917
Cyril Bontéb21570a2009-11-29 20:04:48 +01004918 if (t->be->options2 & PR_O2_AS_PFX) {
4919 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
4920 value_begin = p1 + t->be->appsession_name_len;
4921 value_len = p4 - p1 - t->be->appsession_name_len;
4922 } else {
4923 cmp_len = p2 - p1;
4924 value_begin = p3;
4925 value_len = p4 - p3;
4926 }
4927
4928 /* let's see if the cookie is our appcookie */
4929 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
4930 /* Cool... it's the right one */
4931 manage_client_side_appsession(t, value_begin, value_len);
4932 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004933#if defined(DEBUG_HASH)
4934 Alert("manage_client_side_cookies\n");
4935 appsession_hash_dump(&(t->be->htbl_proxy));
4936#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004937 }/* end if ((t->proxy->appsession_name != NULL) ... */
4938 }
4939
4940 /* we'll have to look for another cookie ... */
4941 p1 = p4;
4942 } /* while (p1 < cur_end) */
4943
4944 /* There's no more cookie on this line.
4945 * We may have marked the last one(s) for deletion.
4946 * We must do this now in two ways :
4947 * - if there is no app cookie, we simply delete the header ;
4948 * - if there are app cookies, we must delete the end of the
4949 * string properly, including the colon/semi-colon before
4950 * the cookie name.
4951 */
4952 if (del_cookie != NULL) {
4953 int delta;
4954 if (app_cookies) {
4955 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4956 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004957 cur_hdr->len += delta;
4958 } else {
4959 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004960
4961 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004962 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4963 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004964 cur_hdr->len = 0;
4965 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004966 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004967 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004968 }
4969
4970 /* keep the link from this header to next one */
4971 old_idx = cur_idx;
4972 } /* end of cookie processing on this header */
4973}
4974
4975
Willy Tarreaua15645d2007-03-18 16:22:39 +01004976/* Iterate the same filter through all response headers contained in <rtr>.
4977 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4978 */
4979int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4980{
4981 char term;
4982 char *cur_ptr, *cur_end, *cur_next;
4983 int cur_idx, old_idx, last_hdr;
4984 struct http_txn *txn = &t->txn;
4985 struct hdr_idx_elem *cur_hdr;
4986 int len, delta;
4987
4988 last_hdr = 0;
4989
4990 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4991 old_idx = 0;
4992
4993 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004994 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004995 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004996 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004997 (exp->action == ACT_ALLOW ||
4998 exp->action == ACT_DENY))
4999 return 0;
5000
5001 cur_idx = txn->hdr_idx.v[old_idx].next;
5002 if (!cur_idx)
5003 break;
5004
5005 cur_hdr = &txn->hdr_idx.v[cur_idx];
5006 cur_ptr = cur_next;
5007 cur_end = cur_ptr + cur_hdr->len;
5008 cur_next = cur_end + cur_hdr->cr + 1;
5009
5010 /* Now we have one header between cur_ptr and cur_end,
5011 * and the next header starts at cur_next.
5012 */
5013
5014 /* The annoying part is that pattern matching needs
5015 * that we modify the contents to null-terminate all
5016 * strings before testing them.
5017 */
5018
5019 term = *cur_end;
5020 *cur_end = '\0';
5021
5022 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5023 switch (exp->action) {
5024 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005025 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005026 last_hdr = 1;
5027 break;
5028
5029 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005030 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005031 last_hdr = 1;
5032 break;
5033
5034 case ACT_REPLACE:
5035 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5036 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5037 /* FIXME: if the user adds a newline in the replacement, the
5038 * index will not be recalculated for now, and the new line
5039 * will not be counted as a new header.
5040 */
5041
5042 cur_end += delta;
5043 cur_next += delta;
5044 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005045 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005046 break;
5047
5048 case ACT_REMOVE:
5049 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5050 cur_next += delta;
5051
Willy Tarreaufa355d42009-11-29 18:12:29 +01005052 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005053 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5054 txn->hdr_idx.used--;
5055 cur_hdr->len = 0;
5056 cur_end = NULL; /* null-term has been rewritten */
5057 break;
5058
5059 }
5060 }
5061 if (cur_end)
5062 *cur_end = term; /* restore the string terminator */
5063
5064 /* keep the link from this header to next one in case of later
5065 * removal of next header.
5066 */
5067 old_idx = cur_idx;
5068 }
5069 return 0;
5070}
5071
5072
5073/* Apply the filter to the status line in the response buffer <rtr>.
5074 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5075 * or -1 if a replacement resulted in an invalid status line.
5076 */
5077int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5078{
5079 char term;
5080 char *cur_ptr, *cur_end;
5081 int done;
5082 struct http_txn *txn = &t->txn;
5083 int len, delta;
5084
5085
Willy Tarreau3d300592007-03-18 18:34:41 +01005086 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005087 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005088 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005089 (exp->action == ACT_ALLOW ||
5090 exp->action == ACT_DENY))
5091 return 0;
5092 else if (exp->action == ACT_REMOVE)
5093 return 0;
5094
5095 done = 0;
5096
Willy Tarreau9cdde232007-05-02 20:58:19 +02005097 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005098 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5099
5100 /* Now we have the status line between cur_ptr and cur_end */
5101
5102 /* The annoying part is that pattern matching needs
5103 * that we modify the contents to null-terminate all
5104 * strings before testing them.
5105 */
5106
5107 term = *cur_end;
5108 *cur_end = '\0';
5109
5110 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5111 switch (exp->action) {
5112 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005113 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005114 done = 1;
5115 break;
5116
5117 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005118 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005119 done = 1;
5120 break;
5121
5122 case ACT_REPLACE:
5123 *cur_end = term; /* restore the string terminator */
5124 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5125 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5126 /* FIXME: if the user adds a newline in the replacement, the
5127 * index will not be recalculated for now, and the new line
5128 * will not be counted as a new header.
5129 */
5130
Willy Tarreaufa355d42009-11-29 18:12:29 +01005131 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005132 cur_end += delta;
5133
Willy Tarreau9cdde232007-05-02 20:58:19 +02005134 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005135 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005136 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005137 cur_ptr, cur_end + 1,
5138 NULL, NULL);
5139 if (unlikely(!cur_end))
5140 return -1;
5141
5142 /* we have a full respnse and we know that we have either a CR
5143 * or an LF at <ptr>.
5144 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005145 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005146 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5147 /* there is no point trying this regex on headers */
5148 return 1;
5149 }
5150 }
5151 *cur_end = term; /* restore the string terminator */
5152 return done;
5153}
5154
5155
5156
5157/*
5158 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5159 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5160 * unparsable response.
5161 */
5162int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5163{
Willy Tarreau3d300592007-03-18 18:34:41 +01005164 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005165 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005166 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005167 int ret;
5168
5169 /*
5170 * The interleaving of transformations and verdicts
5171 * makes it difficult to decide to continue or stop
5172 * the evaluation.
5173 */
5174
Willy Tarreau3d300592007-03-18 18:34:41 +01005175 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005176 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5177 exp->action == ACT_PASS)) {
5178 exp = exp->next;
5179 continue;
5180 }
5181
5182 /* Apply the filter to the status line. */
5183 ret = apply_filter_to_sts_line(t, rtr, exp);
5184 if (unlikely(ret < 0))
5185 return -1;
5186
5187 if (likely(ret == 0)) {
5188 /* The filter did not match the response, it can be
5189 * iterated through all headers.
5190 */
5191 apply_filter_to_resp_headers(t, rtr, exp);
5192 }
5193 exp = exp->next;
5194 }
5195 return 0;
5196}
5197
5198
5199
5200/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005201 * Manage server-side cookies. It can impact performance by about 2% so it is
5202 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005203 */
5204void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5205{
5206 struct http_txn *txn = &t->txn;
5207 char *p1, *p2, *p3, *p4;
5208
Willy Tarreaua15645d2007-03-18 16:22:39 +01005209 char *cur_ptr, *cur_end, *cur_next;
5210 int cur_idx, old_idx, delta;
5211
Willy Tarreaua15645d2007-03-18 16:22:39 +01005212 /* Iterate through the headers.
5213 * we start with the start line.
5214 */
5215 old_idx = 0;
5216 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5217
5218 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5219 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005220 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005221
5222 cur_hdr = &txn->hdr_idx.v[cur_idx];
5223 cur_ptr = cur_next;
5224 cur_end = cur_ptr + cur_hdr->len;
5225 cur_next = cur_end + cur_hdr->cr + 1;
5226
5227 /* We have one full header between cur_ptr and cur_end, and the
5228 * next header starts at cur_next. We're only interested in
5229 * "Cookie:" headers.
5230 */
5231
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005232 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5233 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005234 old_idx = cur_idx;
5235 continue;
5236 }
5237
5238 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005239 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005240
5241
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005242 /* maybe we only wanted to see if there was a set-cookie. Note that
5243 * the cookie capture is declared in the fronend.
5244 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005245 if (t->be->cookie_name == NULL &&
5246 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005247 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005248 return;
5249
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005250 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005251
5252 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005253 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5254 break;
5255
5256 /* p1 is at the beginning of the cookie name */
5257 p2 = p1;
5258
5259 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5260 p2++;
5261
5262 if (p2 == cur_end || *p2 == ';') /* next cookie */
5263 break;
5264
5265 p3 = p2 + 1; /* skip the '=' sign */
5266 if (p3 == cur_end)
5267 break;
5268
5269 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005270 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005271 p4++;
5272
5273 /* here, we have the cookie name between p1 and p2,
5274 * and its value between p3 and p4.
5275 * we can process it.
5276 */
5277
5278 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005279 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005280 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005281 (p4 - p1 >= t->fe->capture_namelen) &&
5282 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005283 int log_len = p4 - p1;
5284
Willy Tarreau086b3b42007-05-13 21:45:51 +02005285 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005286 Alert("HTTP logging : out of memory.\n");
5287 }
5288
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005289 if (log_len > t->fe->capture_len)
5290 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005291 memcpy(txn->srv_cookie, p1, log_len);
5292 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005293 }
5294
5295 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005296 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5297 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005298 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005299 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005300
5301 /* If the cookie is in insert mode on a known server, we'll delete
5302 * this occurrence because we'll insert another one later.
5303 * We'll delete it too if the "indirect" option is set and we're in
5304 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005305 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5306 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005307 /* this header must be deleted */
5308 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5309 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5310 txn->hdr_idx.used--;
5311 cur_hdr->len = 0;
5312 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005313 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005314
Willy Tarreau3d300592007-03-18 18:34:41 +01005315 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005316 }
5317 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005318 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005319 /* replace bytes p3->p4 with the cookie name associated
5320 * with this server since we know it.
5321 */
5322 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5323 cur_hdr->len += delta;
5324 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005325 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005326
Willy Tarreau3d300592007-03-18 18:34:41 +01005327 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005328 }
5329 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005330 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005331 /* insert the cookie name associated with this server
5332 * before existing cookie, and insert a delimitor between them..
5333 */
5334 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5335 cur_hdr->len += delta;
5336 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005337 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005338
5339 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005340 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005341 }
5342 }
5343 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005344 else if (t->be->appsession_name != NULL) {
5345 int cmp_len, value_len;
5346 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005347
Cyril Bontéb21570a2009-11-29 20:04:48 +01005348 if (t->be->options2 & PR_O2_AS_PFX) {
5349 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5350 value_begin = p1 + t->be->appsession_name_len;
5351 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5352 } else {
5353 cmp_len = p2 - p1;
5354 value_begin = p3;
5355 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005356 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005357
5358 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5359 /* Cool... it's the right one */
5360 if (t->sessid != NULL) {
5361 /* free previously allocated memory as we don't need it anymore */
5362 pool_free2(apools.sessid, t->sessid);
5363 }
5364 /* Store the sessid in the session for future use */
5365 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5366 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5367 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5368 return;
5369 }
5370 memcpy(t->sessid, value_begin, value_len);
5371 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005372 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005373 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005374 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5375 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005376 /* keep the link from this header to next one */
5377 old_idx = cur_idx;
5378 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005379
5380 if (t->sessid != NULL) {
5381 appsess *asession = NULL;
5382 /* only do insert, if lookup fails */
5383 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5384 if (asession == NULL) {
5385 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5386 Alert("Not enough Memory process_srv():asession:calloc().\n");
5387 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5388 return;
5389 }
5390 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5391 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5392 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5393 return;
5394 }
5395 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5396 asession->sessid[t->be->appsession_len] = 0;
5397
5398 size_t server_id_len = strlen(t->srv->id) + 1;
5399 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5400 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5401 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5402 return;
5403 }
5404 asession->serverid[0] = '\0';
5405 memcpy(asession->serverid, t->srv->id, server_id_len);
5406
5407 asession->request_count = 0;
5408 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5409 }
5410
5411 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5412 asession->request_count++;
5413 }
5414
5415#if defined(DEBUG_HASH)
5416 Alert("manage_server_side_cookies\n");
5417 appsession_hash_dump(&(t->be->htbl_proxy));
5418#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005419}
5420
5421
5422
5423/*
5424 * Check if response is cacheable or not. Updates t->flags.
5425 */
5426void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5427{
5428 struct http_txn *txn = &t->txn;
5429 char *p1, *p2;
5430
5431 char *cur_ptr, *cur_end, *cur_next;
5432 int cur_idx;
5433
Willy Tarreau5df51872007-11-25 16:20:08 +01005434 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005435 return;
5436
5437 /* Iterate through the headers.
5438 * we start with the start line.
5439 */
5440 cur_idx = 0;
5441 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5442
5443 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5444 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005445 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005446
5447 cur_hdr = &txn->hdr_idx.v[cur_idx];
5448 cur_ptr = cur_next;
5449 cur_end = cur_ptr + cur_hdr->len;
5450 cur_next = cur_end + cur_hdr->cr + 1;
5451
5452 /* We have one full header between cur_ptr and cur_end, and the
5453 * next header starts at cur_next. We're only interested in
5454 * "Cookie:" headers.
5455 */
5456
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005457 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5458 if (val) {
5459 if ((cur_end - (cur_ptr + val) >= 8) &&
5460 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5461 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5462 return;
5463 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005464 }
5465
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005466 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5467 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005468 continue;
5469
5470 /* OK, right now we know we have a cache-control header at cur_ptr */
5471
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005472 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005473
5474 if (p1 >= cur_end) /* no more info */
5475 continue;
5476
5477 /* p1 is at the beginning of the value */
5478 p2 = p1;
5479
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005480 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005481 p2++;
5482
5483 /* we have a complete value between p1 and p2 */
5484 if (p2 < cur_end && *p2 == '=') {
5485 /* we have something of the form no-cache="set-cookie" */
5486 if ((cur_end - p1 >= 21) &&
5487 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5488 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005489 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005490 continue;
5491 }
5492
5493 /* OK, so we know that either p2 points to the end of string or to a comma */
5494 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5495 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5496 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5497 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005498 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005499 return;
5500 }
5501
5502 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005503 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005504 continue;
5505 }
5506 }
5507}
5508
5509
Willy Tarreau58f10d72006-12-04 02:26:12 +01005510/*
5511 * Try to retrieve a known appsession in the URI, then the associated server.
5512 * If the server is found, it's assigned to the session.
5513 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005514void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005515{
Cyril Bontéb21570a2009-11-29 20:04:48 +01005516 char *end_params, *first_param, *cur_param, *next_param;
5517 char separator;
5518 int value_len;
5519
5520 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005521
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005522 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01005523 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005524 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005525 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005526
Cyril Bontéb21570a2009-11-29 20:04:48 +01005527 first_param = NULL;
5528 switch (mode) {
5529 case PR_O2_AS_M_PP:
5530 first_param = memchr(begin, ';', len);
5531 break;
5532 case PR_O2_AS_M_QS:
5533 first_param = memchr(begin, '?', len);
5534 break;
5535 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005536
Cyril Bontéb21570a2009-11-29 20:04:48 +01005537 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005538 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005539 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005540
Cyril Bontéb21570a2009-11-29 20:04:48 +01005541 switch (mode) {
5542 case PR_O2_AS_M_PP:
5543 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
5544 end_params = (char *) begin + len;
5545 }
5546 separator = ';';
5547 break;
5548 case PR_O2_AS_M_QS:
5549 end_params = (char *) begin + len;
5550 separator = '&';
5551 break;
5552 default:
5553 /* unknown mode, shouldn't happen */
5554 return;
5555 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005556
Cyril Bontéb21570a2009-11-29 20:04:48 +01005557 cur_param = next_param = end_params;
5558 while (cur_param > first_param) {
5559 cur_param--;
5560 if ((cur_param[0] == separator) || (cur_param == first_param)) {
5561 /* let's see if this is the appsession parameter */
5562 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
5563 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
5564 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
5565 /* Cool... it's the right one */
5566 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
5567 value_len = MIN(t->be->appsession_len, next_param - cur_param);
5568 if (value_len > 0) {
5569 manage_client_side_appsession(t, cur_param, value_len);
5570 }
5571 break;
5572 }
5573 next_param = cur_param;
5574 }
5575 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005576#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005577 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005578 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005579#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005580}
5581
5582
Willy Tarreaub2513902006-12-17 14:52:38 +01005583/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005584 * In a GET or HEAD request, check if the requested URI matches the stats uri
5585 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005586 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005587 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005588 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005589 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005590 *
5591 * Returns 1 if the session's state changes, otherwise 0.
5592 */
5593int stats_check_uri_auth(struct session *t, struct proxy *backend)
5594{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005595 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005596 struct uri_auth *uri_auth = backend->uri_auth;
5597 struct user_auth *user;
5598 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005599 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005600
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005601 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005603 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005604 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005605 return 0;
5606
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005607 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005608
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005609 /* the URI is in h */
5610 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005611 return 0;
5612
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005613 h += uri_auth->uri_len;
5614 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5615 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005616 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005617 break;
5618 }
5619 h++;
5620 }
5621
5622 if (uri_auth->refresh) {
5623 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5624 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5625 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005626 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005627 break;
5628 }
5629 h++;
5630 }
5631 }
5632
Willy Tarreau55bb8452007-10-17 18:44:57 +02005633 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5634 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5635 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005636 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005637 break;
5638 }
5639 h++;
5640 }
5641
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005642 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5643
Willy Tarreaub2513902006-12-17 14:52:38 +01005644 /* we are in front of a interceptable URI. Let's check
5645 * if there's an authentication and if it's valid.
5646 */
5647 user = uri_auth->users;
5648 if (!user) {
5649 /* no user auth required, it's OK */
5650 authenticated = 1;
5651 } else {
5652 authenticated = 0;
5653
5654 /* a user list is defined, we have to check.
5655 * skip 21 chars for "Authorization: Basic ".
5656 */
5657
5658 /* FIXME: this should move to an earlier place */
5659 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005660 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5661 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5662 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005663 if (len > 14 &&
5664 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02005665 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01005666 break;
5667 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005668 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005669 }
5670
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005671 if (txn->auth_hdr.len < 21 ||
5672 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005673 user = NULL;
5674
5675 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005676 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5677 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005678 user->user_pwd, user->user_len)) {
5679 authenticated = 1;
5680 break;
5681 }
5682 user = user->next;
5683 }
5684 }
5685
5686 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005687 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005688
5689 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005690 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
5691 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005692 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01005693 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005694 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005695 if (!(t->flags & SN_ERR_MASK))
5696 t->flags |= SN_ERR_PRXCOND;
5697 if (!(t->flags & SN_FINST_MASK))
5698 t->flags |= SN_FINST_R;
5699 return 1;
5700 }
5701
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005702 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005703 * data.
5704 */
Willy Tarreau70089872008-06-13 21:12:51 +02005705 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005706 t->data_source = DATA_SRC_STATS;
5707 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005708 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005709 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
5710 t->rep->prod->private = t;
5711 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005712 return 1;
5713}
5714
Willy Tarreau4076a152009-04-02 15:18:36 +02005715/*
5716 * Capture a bad request or response and archive it in the proxy's structure.
5717 */
5718void http_capture_bad_message(struct error_snapshot *es, struct session *s,
5719 struct buffer *buf, struct http_msg *msg,
5720 struct proxy *other_end)
5721{
Willy Tarreau2df8d712009-05-01 11:33:17 +02005722 es->len = buf->r - (buf->data + msg->som);
5723 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02005724 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02005725 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02005726 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02005727 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02005728 es->when = date; // user-visible date
5729 es->sid = s->uniq_id;
5730 es->srv = s->srv;
5731 es->oe = other_end;
5732 es->src = s->cli_addr;
5733}
Willy Tarreaub2513902006-12-17 14:52:38 +01005734
Willy Tarreaubaaee002006-06-26 02:48:02 +02005735/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005736 * Print a debug line with a header
5737 */
5738void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5739{
5740 int len, max;
5741 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005742 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005743 max = end - start;
5744 UBOUND(max, sizeof(trash) - len - 1);
5745 len += strlcpy2(trash + len, start, max + 1);
5746 trash[len++] = '\n';
5747 write(1, trash, len);
5748}
5749
Willy Tarreau0937bc42009-12-22 15:03:09 +01005750/*
5751 * Initialize a new HTTP transaction for session <s>. It is assumed that all
5752 * the required fields are properly allocated and that we only need to (re)init
5753 * them. This should be used before processing any new request.
5754 */
5755void http_init_txn(struct session *s)
5756{
5757 struct http_txn *txn = &s->txn;
5758 struct proxy *fe = s->fe;
5759
5760 txn->flags = 0;
5761 txn->status = -1;
5762
5763 txn->req.sol = txn->req.eol = NULL;
5764 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
5765 txn->rsp.sol = txn->rsp.eol = NULL;
5766 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
5767 txn->req.hdr_content_len = 0LL;
5768 txn->rsp.hdr_content_len = 0LL;
5769 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5770 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5771 chunk_reset(&txn->auth_hdr);
5772
5773 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
5774 if (fe->options2 & PR_O2_REQBUG_OK)
5775 txn->req.err_pos = -1; /* let buggy requests pass */
5776
5777 if (txn->req.cap)
5778 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
5779
5780 if (txn->rsp.cap)
5781 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
5782
5783 if (txn->hdr_idx.v)
5784 hdr_idx_init(&txn->hdr_idx);
5785}
5786
5787/* to be used at the end of a transaction */
5788void http_end_txn(struct session *s)
5789{
5790 struct http_txn *txn = &s->txn;
5791
5792 /* these ones will have been dynamically allocated */
5793 pool_free2(pool2_requri, txn->uri);
5794 pool_free2(pool2_capture, txn->cli_cookie);
5795 pool_free2(pool2_capture, txn->srv_cookie);
5796 txn->uri = NULL;
5797 txn->srv_cookie = NULL;
5798 txn->cli_cookie = NULL;
5799}
5800
5801/* to be used at the end of a transaction to prepare a new one */
5802void http_reset_txn(struct session *s)
5803{
5804 http_end_txn(s);
5805 http_init_txn(s);
5806
5807 s->be = s->fe;
5808 s->req->analysers = s->listener->analysers;
5809 s->logs.logwait = s->fe->to_log;
5810 s->srv = s->prev_srv = s->srv_conn = NULL;
5811 s->pend_pos = NULL;
5812 s->conn_retries = s->be->conn_retries;
5813
5814 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
5815
5816 s->req->rto = s->fe->timeout.client;
5817 s->req->wto = s->be->timeout.server;
5818 s->req->cto = s->be->timeout.connect;
5819
5820 s->rep->rto = s->be->timeout.server;
5821 s->rep->wto = s->fe->timeout.client;
5822 s->rep->cto = TICK_ETERNITY;
5823
5824 s->req->rex = TICK_ETERNITY;
5825 s->req->wex = TICK_ETERNITY;
5826 s->req->analyse_exp = TICK_ETERNITY;
5827 s->rep->rex = TICK_ETERNITY;
5828 s->rep->wex = TICK_ETERNITY;
5829 s->rep->analyse_exp = TICK_ETERNITY;
5830}
Willy Tarreau58f10d72006-12-04 02:26:12 +01005831
Willy Tarreau8797c062007-05-07 00:55:35 +02005832/************************************************************************/
5833/* The code below is dedicated to ACL parsing and matching */
5834/************************************************************************/
5835
5836
5837
5838
5839/* 1. Check on METHOD
5840 * We use the pre-parsed method if it is known, and store its number as an
5841 * integer. If it is unknown, we use the pointer and the length.
5842 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005843static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005844{
5845 int len, meth;
5846
Willy Tarreauae8b7962007-06-09 23:10:04 +02005847 len = strlen(*text);
5848 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005849
5850 pattern->val.i = meth;
5851 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005852 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005853 if (!pattern->ptr.str)
5854 return 0;
5855 pattern->len = len;
5856 }
5857 return 1;
5858}
5859
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005860static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005861acl_fetch_meth(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 int meth;
5865 struct http_txn *txn = l7;
5866
Willy Tarreaub6866442008-07-14 23:54:42 +02005867 if (!txn)
5868 return 0;
5869
Willy Tarreau655dce92009-11-08 13:10:58 +01005870 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005871 return 0;
5872
Willy Tarreau8797c062007-05-07 00:55:35 +02005873 meth = txn->meth;
5874 test->i = meth;
5875 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005876 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5877 /* ensure the indexes are not affected */
5878 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005879 test->len = txn->req.sl.rq.m_l;
5880 test->ptr = txn->req.sol;
5881 }
5882 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5883 return 1;
5884}
5885
5886static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5887{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005888 int icase;
5889
Willy Tarreau8797c062007-05-07 00:55:35 +02005890 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005891 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005892
5893 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005894 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005895
5896 /* Other method, we must compare the strings */
5897 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005898 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005899
5900 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5901 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5902 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005903 return ACL_PAT_FAIL;
5904 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005905}
5906
5907/* 2. Check on Request/Status Version
5908 * We simply compare strings here.
5909 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005910static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005911{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005912 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005913 if (!pattern->ptr.str)
5914 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005915 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005916 return 1;
5917}
5918
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005919static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005920acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5921 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005922{
5923 struct http_txn *txn = l7;
5924 char *ptr;
5925 int len;
5926
Willy Tarreaub6866442008-07-14 23:54:42 +02005927 if (!txn)
5928 return 0;
5929
Willy Tarreau655dce92009-11-08 13:10:58 +01005930 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005931 return 0;
5932
Willy Tarreau8797c062007-05-07 00:55:35 +02005933 len = txn->req.sl.rq.v_l;
5934 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5935
5936 while ((len-- > 0) && (*ptr++ != '/'));
5937 if (len <= 0)
5938 return 0;
5939
5940 test->ptr = ptr;
5941 test->len = len;
5942
5943 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5944 return 1;
5945}
5946
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005947static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005948acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5949 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005950{
5951 struct http_txn *txn = l7;
5952 char *ptr;
5953 int len;
5954
Willy Tarreaub6866442008-07-14 23:54:42 +02005955 if (!txn)
5956 return 0;
5957
Willy Tarreau655dce92009-11-08 13:10:58 +01005958 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005959 return 0;
5960
Willy Tarreau8797c062007-05-07 00:55:35 +02005961 len = txn->rsp.sl.st.v_l;
5962 ptr = txn->rsp.sol;
5963
5964 while ((len-- > 0) && (*ptr++ != '/'));
5965 if (len <= 0)
5966 return 0;
5967
5968 test->ptr = ptr;
5969 test->len = len;
5970
5971 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5972 return 1;
5973}
5974
5975/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005976static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005977acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5978 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005979{
5980 struct http_txn *txn = l7;
5981 char *ptr;
5982 int len;
5983
Willy Tarreaub6866442008-07-14 23:54:42 +02005984 if (!txn)
5985 return 0;
5986
Willy Tarreau655dce92009-11-08 13:10:58 +01005987 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005988 return 0;
5989
Willy Tarreau8797c062007-05-07 00:55:35 +02005990 len = txn->rsp.sl.st.c_l;
5991 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5992
5993 test->i = __strl2ui(ptr, len);
5994 test->flags = ACL_TEST_F_VOL_1ST;
5995 return 1;
5996}
5997
5998/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005999static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006000acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6001 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006002{
6003 struct http_txn *txn = l7;
6004
Willy Tarreaub6866442008-07-14 23:54:42 +02006005 if (!txn)
6006 return 0;
6007
Willy Tarreau655dce92009-11-08 13:10:58 +01006008 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006009 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006010
Willy Tarreauc11416f2007-06-17 16:58:38 +02006011 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6012 /* ensure the indexes are not affected */
6013 return 0;
6014
Willy Tarreau8797c062007-05-07 00:55:35 +02006015 test->len = txn->req.sl.rq.u_l;
6016 test->ptr = txn->req.sol + txn->req.sl.rq.u;
6017
Willy Tarreauf3d25982007-05-08 22:45:09 +02006018 /* we do not need to set READ_ONLY because the data is in a buffer */
6019 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006020 return 1;
6021}
6022
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006023static int
6024acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6025 struct acl_expr *expr, struct acl_test *test)
6026{
6027 struct http_txn *txn = l7;
6028
Willy Tarreaub6866442008-07-14 23:54:42 +02006029 if (!txn)
6030 return 0;
6031
Willy Tarreau655dce92009-11-08 13:10:58 +01006032 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006033 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006034
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006035 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6036 /* ensure the indexes are not affected */
6037 return 0;
6038
6039 /* Parse HTTP request */
6040 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6041 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6042 test->i = AF_INET;
6043
6044 /*
6045 * If we are parsing url in frontend space, we prepare backend stage
6046 * to not parse again the same url ! optimization lazyness...
6047 */
6048 if (px->options & PR_O_HTTP_PROXY)
6049 l4->flags |= SN_ADDR_SET;
6050
6051 test->flags = ACL_TEST_F_READ_ONLY;
6052 return 1;
6053}
6054
6055static int
6056acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6057 struct acl_expr *expr, struct acl_test *test)
6058{
6059 struct http_txn *txn = l7;
6060
Willy Tarreaub6866442008-07-14 23:54:42 +02006061 if (!txn)
6062 return 0;
6063
Willy Tarreau655dce92009-11-08 13:10:58 +01006064 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006065 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006066
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006067 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6068 /* ensure the indexes are not affected */
6069 return 0;
6070
6071 /* Same optimization as url_ip */
6072 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6073 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6074
6075 if (px->options & PR_O_HTTP_PROXY)
6076 l4->flags |= SN_ADDR_SET;
6077
6078 test->flags = ACL_TEST_F_READ_ONLY;
6079 return 1;
6080}
6081
Willy Tarreauc11416f2007-06-17 16:58:38 +02006082/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6083 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6084 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006085static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006086acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006087 struct acl_expr *expr, struct acl_test *test)
6088{
6089 struct http_txn *txn = l7;
6090 struct hdr_idx *idx = &txn->hdr_idx;
6091 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006092
Willy Tarreaub6866442008-07-14 23:54:42 +02006093 if (!txn)
6094 return 0;
6095
Willy Tarreau33a7e692007-06-10 19:45:56 +02006096 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6097 /* search for header from the beginning */
6098 ctx->idx = 0;
6099
Willy Tarreau33a7e692007-06-10 19:45:56 +02006100 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6101 test->flags |= ACL_TEST_F_FETCH_MORE;
6102 test->flags |= ACL_TEST_F_VOL_HDR;
6103 test->len = ctx->vlen;
6104 test->ptr = (char *)ctx->line + ctx->val;
6105 return 1;
6106 }
6107
6108 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6109 test->flags |= ACL_TEST_F_VOL_HDR;
6110 return 0;
6111}
6112
Willy Tarreau33a7e692007-06-10 19:45:56 +02006113static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006114acl_fetch_chdr(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(px, l4, txn, txn->req.sol, expr, test);
6130}
6131
6132static int
6133acl_fetch_shdr(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(px, l4, txn, txn->rsp.sol, expr, test);
6145}
6146
6147/* 6. Check on HTTP header count. The number of occurrences is returned.
6148 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6149 */
6150static int
6151acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006152 struct acl_expr *expr, struct acl_test *test)
6153{
6154 struct http_txn *txn = l7;
6155 struct hdr_idx *idx = &txn->hdr_idx;
6156 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006157 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006158
Willy Tarreaub6866442008-07-14 23:54:42 +02006159 if (!txn)
6160 return 0;
6161
Willy Tarreau33a7e692007-06-10 19:45:56 +02006162 ctx.idx = 0;
6163 cnt = 0;
6164 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6165 cnt++;
6166
6167 test->i = cnt;
6168 test->flags = ACL_TEST_F_VOL_HDR;
6169 return 1;
6170}
6171
Willy Tarreauc11416f2007-06-17 16:58:38 +02006172static int
6173acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6174 struct acl_expr *expr, struct acl_test *test)
6175{
6176 struct http_txn *txn = l7;
6177
Willy Tarreaub6866442008-07-14 23:54:42 +02006178 if (!txn)
6179 return 0;
6180
Willy Tarreau655dce92009-11-08 13:10:58 +01006181 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006182 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006183
Willy Tarreauc11416f2007-06-17 16:58:38 +02006184 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6185 /* ensure the indexes are not affected */
6186 return 0;
6187
6188 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6189}
6190
6191static int
6192acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6193 struct acl_expr *expr, struct acl_test *test)
6194{
6195 struct http_txn *txn = l7;
6196
Willy Tarreaub6866442008-07-14 23:54:42 +02006197 if (!txn)
6198 return 0;
6199
Willy Tarreau655dce92009-11-08 13:10:58 +01006200 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006201 return 0;
6202
6203 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6204}
6205
Willy Tarreau33a7e692007-06-10 19:45:56 +02006206/* 7. Check on HTTP header's integer value. The integer value is returned.
6207 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006208 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006209 */
6210static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006211acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006212 struct acl_expr *expr, struct acl_test *test)
6213{
6214 struct http_txn *txn = l7;
6215 struct hdr_idx *idx = &txn->hdr_idx;
6216 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006217
Willy Tarreaub6866442008-07-14 23:54:42 +02006218 if (!txn)
6219 return 0;
6220
Willy Tarreau33a7e692007-06-10 19:45:56 +02006221 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6222 /* search for header from the beginning */
6223 ctx->idx = 0;
6224
Willy Tarreau33a7e692007-06-10 19:45:56 +02006225 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6226 test->flags |= ACL_TEST_F_FETCH_MORE;
6227 test->flags |= ACL_TEST_F_VOL_HDR;
6228 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6229 return 1;
6230 }
6231
6232 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6233 test->flags |= ACL_TEST_F_VOL_HDR;
6234 return 0;
6235}
6236
Willy Tarreauc11416f2007-06-17 16:58:38 +02006237static int
6238acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6239 struct acl_expr *expr, struct acl_test *test)
6240{
6241 struct http_txn *txn = l7;
6242
Willy Tarreaub6866442008-07-14 23:54:42 +02006243 if (!txn)
6244 return 0;
6245
Willy Tarreau655dce92009-11-08 13:10:58 +01006246 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006247 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006248
Willy Tarreauc11416f2007-06-17 16:58:38 +02006249 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6250 /* ensure the indexes are not affected */
6251 return 0;
6252
6253 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6254}
6255
6256static int
6257acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6258 struct acl_expr *expr, struct acl_test *test)
6259{
6260 struct http_txn *txn = l7;
6261
Willy Tarreaub6866442008-07-14 23:54:42 +02006262 if (!txn)
6263 return 0;
6264
Willy Tarreau655dce92009-11-08 13:10:58 +01006265 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006266 return 0;
6267
6268 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6269}
6270
Willy Tarreau106f9792009-09-19 07:54:16 +02006271/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6272 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6273 */
6274static int
6275acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6276 struct acl_expr *expr, struct acl_test *test)
6277{
6278 struct http_txn *txn = l7;
6279 struct hdr_idx *idx = &txn->hdr_idx;
6280 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6281
6282 if (!txn)
6283 return 0;
6284
6285 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6286 /* search for header from the beginning */
6287 ctx->idx = 0;
6288
6289 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6290 test->flags |= ACL_TEST_F_FETCH_MORE;
6291 test->flags |= ACL_TEST_F_VOL_HDR;
6292 /* Same optimization as url_ip */
6293 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6294 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6295 test->ptr = (void *)&l4->srv_addr.sin_addr;
6296 test->i = AF_INET;
6297 return 1;
6298 }
6299
6300 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6301 test->flags |= ACL_TEST_F_VOL_HDR;
6302 return 0;
6303}
6304
6305static int
6306acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6307 struct acl_expr *expr, struct acl_test *test)
6308{
6309 struct http_txn *txn = l7;
6310
6311 if (!txn)
6312 return 0;
6313
Willy Tarreau655dce92009-11-08 13:10:58 +01006314 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006315 return 0;
6316
6317 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6318 /* ensure the indexes are not affected */
6319 return 0;
6320
6321 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6322}
6323
6324static int
6325acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6326 struct acl_expr *expr, struct acl_test *test)
6327{
6328 struct http_txn *txn = l7;
6329
6330 if (!txn)
6331 return 0;
6332
Willy Tarreau655dce92009-11-08 13:10:58 +01006333 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006334 return 0;
6335
6336 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6337}
6338
Willy Tarreau737b0c12007-06-10 21:28:46 +02006339/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6340 * the first '/' after the possible hostname, and ends before the possible '?'.
6341 */
6342static int
6343acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6344 struct acl_expr *expr, struct acl_test *test)
6345{
6346 struct http_txn *txn = l7;
6347 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006348
Willy Tarreaub6866442008-07-14 23:54:42 +02006349 if (!txn)
6350 return 0;
6351
Willy Tarreau655dce92009-11-08 13:10:58 +01006352 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006353 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006354
Willy Tarreauc11416f2007-06-17 16:58:38 +02006355 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6356 /* ensure the indexes are not affected */
6357 return 0;
6358
Willy Tarreau21d2af32008-02-14 20:25:24 +01006359 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
6360 ptr = http_get_path(txn);
6361 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006362 return 0;
6363
6364 /* OK, we got the '/' ! */
6365 test->ptr = ptr;
6366
6367 while (ptr < end && *ptr != '?')
6368 ptr++;
6369
6370 test->len = ptr - test->ptr;
6371
6372 /* we do not need to set READ_ONLY because the data is in a buffer */
6373 test->flags = ACL_TEST_F_VOL_1ST;
6374 return 1;
6375}
6376
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006377static int
6378acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6379 struct acl_expr *expr, struct acl_test *test)
6380{
6381 struct buffer *req = s->req;
6382 struct http_txn *txn = &s->txn;
6383 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006384
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006385 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6386 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6387 */
6388
6389 if (!s || !req)
6390 return 0;
6391
Willy Tarreau655dce92009-11-08 13:10:58 +01006392 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006393 /* Already decoded as OK */
6394 test->flags |= ACL_TEST_F_SET_RES_PASS;
6395 return 1;
6396 }
6397
6398 /* Try to decode HTTP request */
6399 if (likely(req->lr < req->r))
6400 http_msg_analyzer(req, msg, &txn->hdr_idx);
6401
Willy Tarreau655dce92009-11-08 13:10:58 +01006402 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006403 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6404 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6405 return 1;
6406 }
6407 /* wait for final state */
6408 test->flags |= ACL_TEST_F_MAY_CHANGE;
6409 return 0;
6410 }
6411
6412 /* OK we got a valid HTTP request. We have some minor preparation to
6413 * perform so that further checks can rely on HTTP tests.
6414 */
6415 msg->sol = req->data + msg->som;
6416 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
6417 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6418 s->flags |= SN_REDIRECTABLE;
6419
6420 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
6421 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6422 return 1;
6423 }
6424
6425 test->flags |= ACL_TEST_F_SET_RES_PASS;
6426 return 1;
6427}
6428
Willy Tarreau8797c062007-05-07 00:55:35 +02006429
6430/************************************************************************/
6431/* All supported keywords must be declared here. */
6432/************************************************************************/
6433
6434/* Note: must not be declared <const> as its list will be overwritten */
6435static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006436 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
6437
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006438 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6439 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6440 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6441 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006442
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006443 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6444 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6445 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6446 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6447 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6448 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6449 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6450 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
6451 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02006452
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006453 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
6454 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6455 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6456 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6457 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6458 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6459 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6460 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6461 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
6462 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006463 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02006464
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006465 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6466 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
6467 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
6468 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
6469 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
6470 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
6471 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
6472 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
6473 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006474 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006475
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006476 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6477 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6478 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6479 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6480 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6481 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6482 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006483
Willy Tarreauf3d25982007-05-08 22:45:09 +02006484 { NULL, NULL, NULL, NULL },
6485
6486#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02006487 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
6488 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
6489 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
6490 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
6491 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
6492 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
6493 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
6494
Willy Tarreau8797c062007-05-07 00:55:35 +02006495 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
6496 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
6497 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
6498 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
6499 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
6500 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
6501 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
6502 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
6503
6504 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
6505 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
6506 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
6507 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
6508 { NULL, NULL, NULL, NULL },
6509#endif
6510}};
6511
6512
6513__attribute__((constructor))
6514static void __http_protocol_init(void)
6515{
6516 acl_register_keywords(&acl_kws);
6517}
6518
6519
Willy Tarreau58f10d72006-12-04 02:26:12 +01006520/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006521 * Local variables:
6522 * c-indent-level: 8
6523 * c-basic-offset: 8
6524 * End:
6525 */