blob: a5de8804d1141973ec4aaa9d93814fbd024d0bfd [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
Willy Tarreau82eeaf22009-12-29 12:09:05 +01003320 if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) {
3321 /* option forceclose is set, let's enforce it now that the transfer is complete. */
3322 buffer_abort(req);
3323 }
3324
Willy Tarreau5523b322009-12-29 12:05:52 +01003325 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3326 if (req->flags & BF_OUT_EMPTY)
3327 msg->msg_state = HTTP_MSG_CLOSED;
3328 else
3329 msg->msg_state = HTTP_MSG_CLOSING;
3330 }
3331 else {
3332 /* for other modes, we let further requests pass for now */
3333 req->flags &= ~BF_DONT_READ;
3334 /* FIXME: we're still forced to do that here */
3335 s->rep->flags &= ~BF_DONT_READ;
3336 break;
3337 }
3338 }
3339 else if (msg->msg_state == HTTP_MSG_CLOSING) {
3340 /* nothing else to forward, just waiting for the buffer to be empty */
3341 if (!(req->flags & BF_OUT_EMPTY))
3342 return 0;
3343 msg->msg_state = HTTP_MSG_CLOSED;
3344 }
3345 else if (msg->msg_state == HTTP_MSG_CLOSED) {
3346 req->flags &= ~BF_DONT_READ;
3347 /* FIXME: we're still forced to do that here */
3348 s->rep->flags &= ~BF_DONT_READ;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003349 break;
3350 }
3351 }
3352
3353 /* OK we're done with the data phase */
3354 req->analysers &= ~an_bit;
3355 return 1;
3356
3357 missing_data:
3358 /* forward the chunk size as well as any pending data */
3359 if (msg->hdr_content_len || msg->som != msg->sov) {
3360 buffer_forward(req, msg->sov - msg->som + msg->hdr_content_len);
3361 msg->hdr_content_len = 0; /* don't forward that again */
3362 msg->som = msg->sov;
3363 }
3364
3365 if (req->flags & BF_FULL)
3366 goto return_bad_req;
3367 /* the session handler will take care of timeouts and errors */
3368 return 0;
3369
3370 return_bad_req: /* let's centralize all bad requests */
3371 txn->req.msg_state = HTTP_MSG_ERROR;
3372 txn->status = 400;
3373 /* Note: we don't send any error if some data were already sent */
3374 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3375
3376 req->analysers = 0;
3377 s->fe->counters.failed_req++;
3378 if (s->listener->counters)
3379 s->listener->counters->failed_req++;
3380
3381 if (!(s->flags & SN_ERR_MASK))
3382 s->flags |= SN_ERR_PRXCOND;
3383 if (!(s->flags & SN_FINST_MASK))
3384 s->flags |= SN_FINST_R;
3385 return 0;
3386}
3387
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003388/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3389 * processing can continue on next analysers, or zero if it either needs more
3390 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3391 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3392 * when it has nothing left to do, and may remove any analyser when it wants to
3393 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003394 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003395int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003396{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003397 struct http_txn *txn = &s->txn;
3398 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003399 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003400 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003401 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003402 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003403
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003404 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 +02003405 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003406 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003407 rep,
3408 rep->rex, rep->wex,
3409 rep->flags,
3410 rep->l,
3411 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003412
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003413 /*
3414 * Now parse the partial (or complete) lines.
3415 * We will check the response syntax, and also join multi-line
3416 * headers. An index of all the lines will be elaborated while
3417 * parsing.
3418 *
3419 * For the parsing, we use a 28 states FSM.
3420 *
3421 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003422 * rep->data + msg->som = beginning of response
3423 * rep->data + msg->eoh = end of processed headers / start of current one
3424 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003425 * rep->lr = first non-visited byte
3426 * rep->r = end of data
3427 */
3428
Willy Tarreau83e3af02009-12-28 17:39:57 +01003429 /* There's a protected area at the end of the buffer for rewriting
3430 * purposes. We don't want to start to parse the request if the
3431 * protected area is affected, because we may have to move processed
3432 * data later, which is much more complicated.
3433 */
3434 if (rep->l &&
3435 (rep->r <= rep->lr || rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3436 if (rep->send_max) {
3437 /* some data has still not left the buffer, wake us once that's done */
3438 buffer_dont_close(rep);
3439 return 0;
3440 }
3441
3442 http_buffer_heavy_realign(rep, msg);
3443 }
3444
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003445 if (likely(rep->lr < rep->r))
3446 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau7f875f62008-08-11 17:35:01 +02003447
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003448 /* 1: we might have to print this header in debug mode */
3449 if (unlikely((global.mode & MODE_DEBUG) &&
3450 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003451 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003452 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003453
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003454 sol = rep->data + msg->som;
3455 eol = sol + msg->sl.rq.l;
3456 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003457
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003458 sol += hdr_idx_first_pos(&txn->hdr_idx);
3459 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003460
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003461 while (cur_idx) {
3462 eol = sol + txn->hdr_idx.v[cur_idx].len;
3463 debug_hdr("srvhdr", s, sol, eol);
3464 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3465 cur_idx = txn->hdr_idx.v[cur_idx].next;
3466 }
3467 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003468
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003469 /*
3470 * Now we quickly check if we have found a full valid response.
3471 * If not so, we check the FD and buffer states before leaving.
3472 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01003473 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003474 * responses are checked first.
3475 *
3476 * Depending on whether the client is still there or not, we
3477 * may send an error response back or not. Note that normally
3478 * we should only check for HTTP status there, and check I/O
3479 * errors somewhere else.
3480 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003481
Willy Tarreau655dce92009-11-08 13:10:58 +01003482 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003483 /* Invalid response */
3484 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
3485 /* we detected a parsing error. We want to archive this response
3486 * in the dedicated proxy area for later troubleshooting.
3487 */
3488 hdr_response_bad:
3489 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
3490 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3491
3492 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003493 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003494 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003495 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
3496 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003497
3498 rep->analysers = 0;
3499 txn->status = 502;
3500 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
3501
3502 if (!(s->flags & SN_ERR_MASK))
3503 s->flags |= SN_ERR_PRXCOND;
3504 if (!(s->flags & SN_FINST_MASK))
3505 s->flags |= SN_FINST_H;
3506
3507 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003508 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003509
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003510 /* too large response does not fit in buffer. */
3511 else if (rep->flags & BF_FULL) {
3512 goto hdr_response_bad;
3513 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003514
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003515 /* read error */
3516 else if (rep->flags & BF_READ_ERROR) {
3517 if (msg->err_pos >= 0)
3518 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02003519
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003520 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003521 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003522 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003523 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
3524 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003525
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003526 rep->analysers = 0;
3527 txn->status = 502;
3528 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02003529
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003530 if (!(s->flags & SN_ERR_MASK))
3531 s->flags |= SN_ERR_SRVCL;
3532 if (!(s->flags & SN_FINST_MASK))
3533 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003534 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003535 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003536
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003537 /* read timeout : return a 504 to the client. */
3538 else if (rep->flags & BF_READ_TIMEOUT) {
3539 if (msg->err_pos >= 0)
3540 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003541
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003542 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003543 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003544 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003545 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
3546 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003547
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003548 rep->analysers = 0;
3549 txn->status = 504;
3550 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02003551
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003552 if (!(s->flags & SN_ERR_MASK))
3553 s->flags |= SN_ERR_SRVTO;
3554 if (!(s->flags & SN_FINST_MASK))
3555 s->flags |= SN_FINST_H;
3556 return 0;
3557 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02003558
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003559 /* close from server */
3560 else if (rep->flags & BF_SHUTR) {
3561 if (msg->err_pos >= 0)
3562 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003563
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003564 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003565 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003566 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003567 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
3568 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003569
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003570 rep->analysers = 0;
3571 txn->status = 502;
3572 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01003573
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003574 if (!(s->flags & SN_ERR_MASK))
3575 s->flags |= SN_ERR_SRVCL;
3576 if (!(s->flags & SN_FINST_MASK))
3577 s->flags |= SN_FINST_H;
3578 return 0;
3579 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003580
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003581 /* write error to client (we don't send any message then) */
3582 else if (rep->flags & BF_WRITE_ERROR) {
3583 if (msg->err_pos >= 0)
3584 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003585
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003586 s->be->counters.failed_resp++;
3587 rep->analysers = 0;
3588
3589 if (!(s->flags & SN_ERR_MASK))
3590 s->flags |= SN_ERR_CLICL;
3591 if (!(s->flags & SN_FINST_MASK))
3592 s->flags |= SN_FINST_H;
3593
3594 /* process_session() will take care of the error */
3595 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003596 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003597
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003598 buffer_dont_close(rep);
3599 return 0;
3600 }
3601
3602 /* More interesting part now : we know that we have a complete
3603 * response which at least looks like HTTP. We have an indicator
3604 * of each header's length, so we can parse them quickly.
3605 */
3606
3607 if (unlikely(msg->err_pos >= 0))
3608 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3609
3610 /* ensure we keep this pointer to the beginning of the message */
3611 msg->sol = rep->data + msg->som;
3612
3613 /*
3614 * 1: get the status code
3615 */
3616 n = rep->data[msg->sl.st.c] - '0';
3617 if (n < 1 || n > 5)
3618 n = 0;
3619 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003620
Willy Tarreau5b154472009-12-21 20:11:07 +01003621 /* check if the response is HTTP/1.1 or above */
3622 if ((msg->sl.st.v_l == 8) &&
3623 ((rep->data[msg->som + 5] > '1') ||
3624 ((rep->data[msg->som + 5] == '1') &&
3625 (rep->data[msg->som + 7] >= '1'))))
3626 txn->flags |= TX_RES_VER_11;
3627
3628 /* "connection" has not been parsed yet */
3629 txn->flags &= ~TX_CON_HDR_PARS;
3630
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003631 /* transfer length unknown*/
3632 txn->flags &= ~TX_RES_XFER_LEN;
3633
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003634 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
3635
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003636 if (txn->status >= 100 && txn->status < 500)
3637 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
3638 else
3639 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
3640
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003641 /*
3642 * 2: check for cacheability.
3643 */
3644
3645 switch (txn->status) {
3646 case 200:
3647 case 203:
3648 case 206:
3649 case 300:
3650 case 301:
3651 case 410:
3652 /* RFC2616 @13.4:
3653 * "A response received with a status code of
3654 * 200, 203, 206, 300, 301 or 410 MAY be stored
3655 * by a cache (...) unless a cache-control
3656 * directive prohibits caching."
3657 *
3658 * RFC2616 @9.5: POST method :
3659 * "Responses to this method are not cacheable,
3660 * unless the response includes appropriate
3661 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003662 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003663 if (likely(txn->meth != HTTP_METH_POST) &&
3664 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3665 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3666 break;
3667 default:
3668 break;
3669 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003670
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003671 /*
3672 * 3: we may need to capture headers
3673 */
3674 s->logs.logwait &= ~LW_RESP;
3675 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
3676 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3677 txn->rsp.cap, s->fe->rsp_cap);
3678
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003679 /* 4: determine the transfer-length.
3680 * According to RFC2616 #4.4, amended by the HTTPbis working group,
3681 * the presence of a message-body in a RESPONSE and its transfer length
3682 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003683 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003684 * All responses to the HEAD request method MUST NOT include a
3685 * message-body, even though the presence of entity-header fields
3686 * might lead one to believe they do. All 1xx (informational), 204
3687 * (No Content), and 304 (Not Modified) responses MUST NOT include a
3688 * message-body. All other responses do include a message-body,
3689 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003690 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003691 * 1. Any response which "MUST NOT" include a message-body (such as the
3692 * 1xx, 204 and 304 responses and any response to a HEAD request) is
3693 * always terminated by the first empty line after the header fields,
3694 * regardless of the entity-header fields present in the message.
3695 *
3696 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
3697 * the "chunked" transfer-coding (Section 6.2) is used, the
3698 * transfer-length is defined by the use of this transfer-coding.
3699 * If a Transfer-Encoding header field is present and the "chunked"
3700 * transfer-coding is not present, the transfer-length is defined by
3701 * the sender closing the connection.
3702 *
3703 * 3. If a Content-Length header field is present, its decimal value in
3704 * OCTETs represents both the entity-length and the transfer-length.
3705 * If a message is received with both a Transfer-Encoding header
3706 * field and a Content-Length header field, the latter MUST be ignored.
3707 *
3708 * 4. If the message uses the media type "multipart/byteranges", and
3709 * the transfer-length is not otherwise specified, then this self-
3710 * delimiting media type defines the transfer-length. This media
3711 * type MUST NOT be used unless the sender knows that the recipient
3712 * can parse it; the presence in a request of a Range header with
3713 * multiple byte-range specifiers from a 1.1 client implies that the
3714 * client can parse multipart/byteranges responses.
3715 *
3716 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003717 */
3718
3719 /* Skip parsing if no content length is possible. The response flags
3720 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003721 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003722 * FIXME: should we parse anyway and return an error on chunked encoding ?
3723 */
3724 if (txn->meth == HTTP_METH_HEAD ||
3725 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003726 txn->status == 204 || txn->status == 304) {
3727 txn->flags |= TX_RES_XFER_LEN;
3728 goto skip_content_length;
3729 }
3730
3731 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003732 goto skip_content_length;
3733
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003734 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003735 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003736 while ((txn->flags & TX_RES_VER_11) &&
3737 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003738 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
3739 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3740 else if (txn->flags & TX_RES_TE_CHNK) {
3741 /* bad transfer-encoding (chunked followed by something else) */
3742 use_close_only = 1;
3743 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3744 break;
3745 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003746 }
3747
3748 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
3749 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003750 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003751 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
3752 signed long long cl;
3753
3754 if (!ctx.vlen)
3755 goto hdr_response_bad;
3756
3757 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
3758 goto hdr_response_bad; /* parse failure */
3759
3760 if (cl < 0)
3761 goto hdr_response_bad;
3762
3763 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
3764 goto hdr_response_bad; /* already specified, was different */
3765
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003766 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003767 msg->hdr_content_len = cl;
3768 }
3769
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003770 /* FIXME: we should also implement the multipart/byterange method.
3771 * For now on, we resort to close mode in this case (unknown length).
3772 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003773skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003774
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003775 /* end of job, return OK */
3776 rep->analysers &= ~an_bit;
3777 rep->analyse_exp = TICK_ETERNITY;
3778 return 1;
3779}
3780
3781/* This function performs all the processing enabled for the current response.
3782 * It normally returns zero, but may return 1 if it absolutely needs to be
3783 * called again after other functions. It relies on buffers flags, and updates
3784 * t->rep->analysers. It might make sense to explode it into several other
3785 * functions. It works like process_request (see indications above).
3786 */
3787int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
3788{
3789 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003790 struct http_msg *msg = &txn->rsp;
3791 struct proxy *cur_proxy;
3792 int cur_idx;
Willy Tarreau5b154472009-12-21 20:11:07 +01003793 int conn_ka = 0, conn_cl = 0;
3794 int must_close = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003795
3796 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3797 now_ms, __FUNCTION__,
3798 t,
3799 rep,
3800 rep->rex, rep->wex,
3801 rep->flags,
3802 rep->l,
3803 rep->analysers);
3804
Willy Tarreau655dce92009-11-08 13:10:58 +01003805 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003806 return 0;
3807
3808 rep->analysers &= ~an_bit;
3809 rep->analyse_exp = TICK_ETERNITY;
3810
Willy Tarreau5b154472009-12-21 20:11:07 +01003811 /* Now we have to check if we need to modify the Connection header.
3812 * This is more difficult on the response than it is on the request,
3813 * because we can have two different HTTP versions and we don't know
3814 * how the client will interprete a response. For instance, let's say
3815 * that the client sends a keep-alive request in HTTP/1.0 and gets an
3816 * HTTP/1.1 response without any header. Maybe it will bound itself to
3817 * HTTP/1.0 because it only knows about it, and will consider the lack
3818 * of header as a close, or maybe it knows HTTP/1.1 and can consider
3819 * the lack of header as a keep-alive. Thus we will use two flags
3820 * indicating how a request MAY be understood by the client. In case
3821 * of multiple possibilities, we'll fix the header to be explicit. If
3822 * ambiguous cases such as both close and keepalive are seen, then we
3823 * will fall back to explicit close. Note that we won't take risks with
3824 * HTTP/1.0 clients which may not necessarily understand keep-alive.
3825 */
3826
3827 if ((txn->meth != HTTP_METH_CONNECT) &&
3828 (txn->status >= 200) &&
3829 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN &&
3830 !(txn->flags & TX_CON_HDR_PARS)) {
3831 int may_keep = 0, may_close = 0; /* how it may be understood */
3832 struct hdr_ctx ctx;
3833
3834 ctx.idx = 0;
3835 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
3836 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
3837 conn_cl = 1;
3838 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
3839 conn_ka = 1;
3840 }
3841
3842 if (conn_cl) {
3843 /* close header present */
3844 may_close = 1;
3845 if (conn_ka) /* we have both close and keep-alive */
3846 may_keep = 1;
3847 }
3848 else if (conn_ka) {
3849 /* keep-alive alone */
3850 may_keep = 1;
3851 }
3852 else {
3853 /* no close nor keep-alive header */
3854 if (txn->flags & TX_RES_VER_11)
3855 may_keep = 1;
3856 else
3857 may_close = 1;
3858
3859 if (txn->flags & TX_REQ_VER_11)
3860 may_keep = 1;
3861 else
3862 may_close = 1;
3863 }
3864
3865 /* let's update the transaction status to reflect any close.
3866 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003867 * handled. We also explicitly state that we will close in
3868 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01003869 */
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003870 if (may_close || !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01003871 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3872
3873 /* Now we must adjust the response header :
3874 * - set "close" if may_keep and WANT_CLO
3875 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
3876 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
3877 *
3878 * Until we support the server-close mode, we'll only support the set "close".
3879 */
3880 if (may_keep && (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO)
3881 must_close = 1;
3882
3883 txn->flags |= TX_CON_HDR_PARS;
3884 }
3885
3886 /* We might have to check for "Connection:" if the server
3887 * returns a connection status that is not compatible with
3888 * the client's or with the config.
3889 */
3890 if ((txn->status >= 200) && must_close && (conn_cl|conn_ka)) {
3891 char *cur_ptr, *cur_end, *cur_next;
3892 int cur_idx, old_idx, delta, val;
3893 int must_delete;
3894 struct hdr_idx_elem *cur_hdr;
3895
3896 /* we just have to remove the headers if both sides are 1.0 */
3897 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
3898 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3899
3900 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
3901 cur_hdr = &txn->hdr_idx.v[cur_idx];
3902 cur_ptr = cur_next;
3903 cur_end = cur_ptr + cur_hdr->len;
3904 cur_next = cur_end + cur_hdr->cr + 1;
3905
3906 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3907 if (!val)
3908 continue;
3909
3910 /* 3 possibilities :
3911 * - we have already set "Connection: close" or we're in
3912 * HTTP/1.0, so we remove this line.
3913 * - we have not yet set "Connection: close", but this line
3914 * indicates close. We leave it untouched and set the flag.
3915 * - we have not yet set "Connection: close", and this line
3916 * indicates non-close. We replace it and set the flag.
3917 */
3918 if (must_delete) {
3919 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3920 http_msg_move_end(&txn->rsp, delta);
3921 cur_next += delta;
3922 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3923 txn->hdr_idx.used--;
3924 cur_hdr->len = 0;
3925 must_close = 0;
3926 } else {
3927 if (cur_end - cur_ptr - val != 5 ||
3928 strncasecmp(cur_ptr + val, "close", 5) != 0) {
3929 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3930 "close", 5);
3931 cur_next += delta;
3932 cur_hdr->len += delta;
3933 http_msg_move_end(&txn->rsp, delta);
3934 }
3935 must_delete = 1;
3936 must_close = 0;
3937 }
3938 } /* for loop */
3939 } /* if must close keep-alive */
3940
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003941 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003942 /*
3943 * 3: we will have to evaluate the filters.
3944 * As opposed to version 1.2, now they will be evaluated in the
3945 * filters order and not in the header order. This means that
3946 * each filter has to be validated among all headers.
3947 *
3948 * Filters are tried with ->be first, then with ->fe if it is
3949 * different from ->be.
3950 */
3951
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003952 cur_proxy = t->be;
3953 while (1) {
3954 struct proxy *rule_set = cur_proxy;
3955
3956 /* try headers filters */
3957 if (rule_set->rsp_exp != NULL) {
3958 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3959 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003960 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003961 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003962 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
3963 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003964 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003965 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02003966 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003967 txn->status = 502;
Willy Tarreau8e89b842009-10-18 23:56:35 +02003968 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003969 if (!(t->flags & SN_ERR_MASK))
3970 t->flags |= SN_ERR_PRXCOND;
3971 if (!(t->flags & SN_FINST_MASK))
3972 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02003973 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003974 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003975 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003976
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003977 /* has the response been denied ? */
3978 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01003979 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003980 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003981
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003982 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003983 if (t->listener->counters)
3984 t->listener->counters->denied_resp++;
3985
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003986 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003987 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003988
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003989 /* add response headers from the rule sets in the same order */
3990 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau816b9792009-09-15 21:25:21 +02003991 if (txn->status < 200)
3992 break;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003993 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003994 rule_set->rsp_add[cur_idx]) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003995 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003996 }
3997
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003998 /* check whether we're already working on the frontend */
3999 if (cur_proxy == t->fe)
4000 break;
4001 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004002 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004003
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004004 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004005 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4006 * in which case this is not the right response, and we're waiting for the
4007 * next one. Let's allow this response to go to the client and wait for the
4008 * next one.
4009 */
4010 if (txn->status < 200) {
4011 hdr_idx_init(&txn->hdr_idx);
4012 buffer_forward(rep, rep->lr - (rep->data + msg->som));
4013 msg->msg_state = HTTP_MSG_RPBEFORE;
4014 txn->status = 0;
4015 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4016 return 1;
4017 }
4018
4019 /* we don't have any 1xx status code now */
4020
4021 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004022 * 4: check for server cookie.
4023 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004024 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4025 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004026 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004027
Willy Tarreaubaaee002006-06-26 02:48:02 +02004028
Willy Tarreaua15645d2007-03-18 16:22:39 +01004029 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004030 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004031 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004032 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004033 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004034
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004035 /*
4036 * 6: add server cookie in the response if needed
4037 */
4038 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004039 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004040 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004041
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004042 /* the server is known, it's not the one the client requested, we have to
4043 * insert a set-cookie here, except if we want to insert only on POST
4044 * requests and this one isn't. Note that servers which don't have cookies
4045 * (eg: some backup servers) will return a full cookie removal request.
4046 */
4047 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4048 t->be->cookie_name,
4049 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004050
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004051 if (t->be->cookie_domain)
4052 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004053
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004054 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004055 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004056 goto return_bad_resp;
4057 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004058
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004059 /* Here, we will tell an eventual cache on the client side that we don't
4060 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4061 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4062 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4063 */
4064 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004065
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004066 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4067
4068 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004069 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004070 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004071 }
4072 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004073
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004074 /*
4075 * 7: check if result will be cacheable with a cookie.
4076 * We'll block the response if security checks have caught
4077 * nasty things such as a cacheable cookie.
4078 */
4079 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4080 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004081 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004082
4083 /* we're in presence of a cacheable response containing
4084 * a set-cookie header. We'll block it as requested by
4085 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004086 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004087 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004088 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004089
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004090 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004091 if (t->listener->counters)
4092 t->listener->counters->denied_resp++;
4093
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004094 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4095 t->be->id, t->srv?t->srv->id:"<dispatch>");
4096 send_log(t->be, LOG_ALERT,
4097 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4098 t->be->id, t->srv?t->srv->id:"<dispatch>");
4099 goto return_srv_prx_502;
4100 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004101
4102 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004103 * 8: add "Connection: close" if needed and not yet set. This is
4104 * only needed for 1.1 responses since we know there is no other
4105 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004106 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004107 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004108 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004109 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004110 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004111 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004112 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004113
Willy Tarreaud98cf932009-12-27 22:54:55 +01004114 if (txn->flags & TX_RES_XFER_LEN)
4115 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004116
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004117 /*************************************************************
4118 * OK, that's finished for the headers. We have done what we *
4119 * could. Let's switch to the DATA state. *
4120 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004121
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004122 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004123
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004124 /* if the user wants to log as soon as possible, without counting
4125 * bytes from the server, then this is the right moment. We have
4126 * to temporarily assign bytes_out to log what we currently have.
4127 */
4128 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4129 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4130 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004131 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004132 t->logs.bytes_out = 0;
4133 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004134
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004135 /* Note: we must not try to cheat by jumping directly to DATA,
4136 * otherwise we would not let the client side wake up.
4137 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004138
Willy Tarreaudafde432008-08-17 01:00:46 +02004139 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004140 }
4141 return 0;
4142}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004143
Willy Tarreaud98cf932009-12-27 22:54:55 +01004144/* This function is an analyser which forwards response body (including chunk
4145 * sizes if any). It is called as soon as we must forward, even if we forward
4146 * zero byte. The only situation where it must not be called is when we're in
4147 * tunnel mode and we want to forward till the close. It's used both to forward
4148 * remaining data and to resync after end of body. It expects the msg_state to
4149 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4150 * read more data, or 1 once we can go on with next request or end the session.
4151 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4152 * bytes of pending data + the headers if not already done (between som and sov).
4153 * It eventually adjusts som to match sov after the data in between have been sent.
4154 */
4155int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4156{
4157 struct http_txn *txn = &s->txn;
4158 struct http_msg *msg = &s->txn.rsp;
4159
4160 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4161 return 0;
4162
4163 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4164 /* we have msg->col and msg->sov which both point to the first
4165 * byte of message body. msg->som still points to the beginning
4166 * of the message. We must save the body in req->lr because it
4167 * survives buffer re-alignments.
4168 */
4169 res->lr = res->data + msg->sov;
4170 if (txn->flags & TX_RES_TE_CHNK)
4171 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4172 else {
4173 msg->msg_state = HTTP_MSG_DATA;
4174 }
4175 }
4176
4177 /* we may already have some data pending */
4178 if (msg->hdr_content_len || msg->som != msg->sov) {
4179 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4180 msg->hdr_content_len = 0; /* don't forward that again */
4181 msg->som = msg->sov;
4182 }
4183
4184 while (1) {
Willy Tarreau5523b322009-12-29 12:05:52 +01004185 // printf("res1: stq=%d, str=%d, l=%d, send_max=%d, fl=%08x, txf=%08x\n",
4186 // 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 +01004187 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
4188 /* read the chunk size and assign it to ->hdr_content_len, then
4189 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4190 */
4191 int ret = http_parse_chunk_size(res, msg);
4192
4193 if (!ret)
4194 goto missing_data;
4195 else if (ret < 0)
4196 goto return_bad_res;
4197 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
4198
4199 /* forward the chunk size as well as any pending data */
4200 if (msg->hdr_content_len || msg->som != msg->sov) {
4201 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4202 msg->hdr_content_len = 0; /* don't forward that again */
4203 msg->som = msg->sov;
4204 }
4205 }
4206 else if (msg->msg_state == HTTP_MSG_DATA) {
4207 /* must still forward */
4208 if (res->to_forward)
4209 return 0;
4210
4211 /* nothing left to forward */
4212 if (txn->flags & TX_RES_TE_CHNK)
4213 msg->msg_state = HTTP_MSG_DATA_CRLF;
4214 else {
4215 msg->msg_state = HTTP_MSG_DONE;
4216 }
4217 }
4218 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4219 /* we want the CRLF after the data */
4220 int ret;
4221
4222 if (!(res->flags & BF_OUT_EMPTY))
4223 return 0;
4224 /* The output pointer does not move anymore, next unsent data
4225 * are available at ->w. Let's save that.
4226 */
4227 res->lr = res->w;
4228 ret = http_skip_chunk_crlf(res, msg);
4229
4230 if (!ret)
4231 goto missing_data;
4232 else if (ret < 0)
4233 goto return_bad_res;
4234 /* we're in MSG_CHUNK_SIZE now */
4235 }
4236 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4237 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004238
Willy Tarreaud98cf932009-12-27 22:54:55 +01004239 if (ret == 0)
4240 goto missing_data;
4241 else if (ret < 0)
4242 goto return_bad_res;
4243 /* we're in HTTP_MSG_DONE now */
4244 }
4245 else if (msg->msg_state == HTTP_MSG_DONE) {
Willy Tarreau5523b322009-12-29 12:05:52 +01004246 /* In theory, we don't need to read anymore, but we must
4247 * still monitor the server connection for a possible close,
4248 * so we don't set the BF_DONT_READ flag here.
4249 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004250
Willy Tarreau5523b322009-12-29 12:05:52 +01004251 if (txn->req.msg_state < HTTP_MSG_DONE && txn->req.msg_state != HTTP_MSG_ERROR) {
4252 /* The client seems to still be sending data, probably
4253 * because we got an error response during an upload.
4254 * We have the choice of either breaking the connection
4255 * or letting it pass through. Let's do the later.
4256 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004257 return 0;
Willy Tarreau5523b322009-12-29 12:05:52 +01004258 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004259
4260 /* when we support keep-alive or server-close modes, we'll have
4261 * to reset the transaction here.
4262 */
Willy Tarreau5523b322009-12-29 12:05:52 +01004263
Willy Tarreau82eeaf22009-12-29 12:09:05 +01004264 if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) {
4265 /* option forceclose is set, let's enforce it now that the transfer is complete. */
4266 buffer_abort(res);
4267 }
4268
Willy Tarreau5523b322009-12-29 12:05:52 +01004269 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4270 if (res->flags & BF_OUT_EMPTY)
4271 msg->msg_state = HTTP_MSG_CLOSED;
4272 else
4273 msg->msg_state = HTTP_MSG_CLOSING;
4274 }
4275 else {
4276 /* for other modes, we let further responses pass for now */
4277 res->flags &= ~BF_DONT_READ;
4278 /* FIXME: we're still forced to do that here */
4279 s->req->flags &= ~BF_DONT_READ;
4280 break;
4281 }
4282 }
4283 else if (msg->msg_state == HTTP_MSG_CLOSING) {
4284 /* nothing else to forward, just waiting for the buffer to be empty */
4285 if (!(res->flags & BF_OUT_EMPTY))
4286 return 0;
4287 msg->msg_state = HTTP_MSG_CLOSED;
4288 }
4289 else if (msg->msg_state == HTTP_MSG_CLOSED) {
4290 res->flags &= ~BF_DONT_READ;
4291 /* FIXME: we're still forced to do that here */
4292 s->req->flags &= ~BF_DONT_READ;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004293 break;
4294 }
4295 }
4296
4297 res->analysers &= ~an_bit;
4298 return 1;
4299
4300 missing_data:
4301 /* forward the chunk size as well as any pending data */
4302 if (msg->hdr_content_len || msg->som != msg->sov) {
4303 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4304 msg->hdr_content_len = 0; /* don't forward that again */
4305 msg->som = msg->sov;
4306 }
4307
4308 if (res->flags & BF_FULL)
4309 goto return_bad_res;
4310 /* the session handler will take care of timeouts and errors */
4311 return 0;
4312
4313 return_bad_res: /* let's centralize all bad resuests */
4314 txn->rsp.msg_state = HTTP_MSG_ERROR;
4315 txn->status = 502;
4316 stream_int_cond_close(res->cons, NULL);
4317
4318 res->analysers = 0;
4319 s->be->counters.failed_resp++;
4320 if (s->srv) {
4321 s->srv->counters.failed_resp++;
4322 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4323 }
4324
4325 if (!(s->flags & SN_ERR_MASK))
4326 s->flags |= SN_ERR_PRXCOND;
4327 if (!(s->flags & SN_FINST_MASK))
4328 s->flags |= SN_FINST_R;
4329 return 0;
4330}
4331
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004332/* Iterate the same filter through all request headers.
4333 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004334 * Since it can manage the switch to another backend, it updates the per-proxy
4335 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004336 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004337int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004338{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004339 char term;
4340 char *cur_ptr, *cur_end, *cur_next;
4341 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004342 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004343 struct hdr_idx_elem *cur_hdr;
4344 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004345
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004346 last_hdr = 0;
4347
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004348 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004349 old_idx = 0;
4350
4351 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004352 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004353 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004354 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004355 (exp->action == ACT_ALLOW ||
4356 exp->action == ACT_DENY ||
4357 exp->action == ACT_TARPIT))
4358 return 0;
4359
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004360 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004361 if (!cur_idx)
4362 break;
4363
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004364 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004365 cur_ptr = cur_next;
4366 cur_end = cur_ptr + cur_hdr->len;
4367 cur_next = cur_end + cur_hdr->cr + 1;
4368
4369 /* Now we have one header between cur_ptr and cur_end,
4370 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004371 */
4372
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004373 /* The annoying part is that pattern matching needs
4374 * that we modify the contents to null-terminate all
4375 * strings before testing them.
4376 */
4377
4378 term = *cur_end;
4379 *cur_end = '\0';
4380
4381 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4382 switch (exp->action) {
4383 case ACT_SETBE:
4384 /* It is not possible to jump a second time.
4385 * FIXME: should we return an HTTP/500 here so that
4386 * the admin knows there's a problem ?
4387 */
4388 if (t->be != t->fe)
4389 break;
4390
4391 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004392 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004393 last_hdr = 1;
4394 break;
4395
4396 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004397 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004398 last_hdr = 1;
4399 break;
4400
4401 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004402 txn->flags |= TX_CLDENY;
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_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004412 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004413 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004414
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004415 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004416 if (t->listener->counters)
4417 t->listener->counters->denied_resp++;
4418
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004419 break;
4420
4421 case ACT_REPLACE:
4422 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4423 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4424 /* FIXME: if the user adds a newline in the replacement, the
4425 * index will not be recalculated for now, and the new line
4426 * will not be counted as a new header.
4427 */
4428
4429 cur_end += delta;
4430 cur_next += delta;
4431 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004432 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004433 break;
4434
4435 case ACT_REMOVE:
4436 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4437 cur_next += delta;
4438
Willy Tarreaufa355d42009-11-29 18:12:29 +01004439 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004440 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4441 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004442 cur_hdr->len = 0;
4443 cur_end = NULL; /* null-term has been rewritten */
4444 break;
4445
4446 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004447 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004448 if (cur_end)
4449 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004451 /* keep the link from this header to next one in case of later
4452 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004453 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004454 old_idx = cur_idx;
4455 }
4456 return 0;
4457}
4458
4459
4460/* Apply the filter to the request line.
4461 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4462 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004463 * Since it can manage the switch to another backend, it updates the per-proxy
4464 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004465 */
4466int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4467{
4468 char term;
4469 char *cur_ptr, *cur_end;
4470 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004471 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004472 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004473
Willy Tarreau58f10d72006-12-04 02:26:12 +01004474
Willy Tarreau3d300592007-03-18 18:34:41 +01004475 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004476 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004477 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004478 (exp->action == ACT_ALLOW ||
4479 exp->action == ACT_DENY ||
4480 exp->action == ACT_TARPIT))
4481 return 0;
4482 else if (exp->action == ACT_REMOVE)
4483 return 0;
4484
4485 done = 0;
4486
Willy Tarreau9cdde232007-05-02 20:58:19 +02004487 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004488 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004489
4490 /* Now we have the request line between cur_ptr and cur_end */
4491
4492 /* The annoying part is that pattern matching needs
4493 * that we modify the contents to null-terminate all
4494 * strings before testing them.
4495 */
4496
4497 term = *cur_end;
4498 *cur_end = '\0';
4499
4500 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4501 switch (exp->action) {
4502 case ACT_SETBE:
4503 /* It is not possible to jump a second time.
4504 * FIXME: should we return an HTTP/500 here so that
4505 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004506 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004507 if (t->be != t->fe)
4508 break;
4509
4510 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004511 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004512 done = 1;
4513 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004515 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004516 txn->flags |= TX_CLALLOW;
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_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004521 txn->flags |= TX_CLDENY;
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_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004531 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004532
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004533 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004534 if (t->listener->counters)
4535 t->listener->counters->denied_resp++;
4536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004537 done = 1;
4538 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004540 case ACT_REPLACE:
4541 *cur_end = term; /* restore the string terminator */
4542 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4543 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4544 /* FIXME: if the user adds a newline in the replacement, the
4545 * index will not be recalculated for now, and the new line
4546 * will not be counted as a new header.
4547 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004548
Willy Tarreaufa355d42009-11-29 18:12:29 +01004549 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004550 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004551
Willy Tarreau9cdde232007-05-02 20:58:19 +02004552 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004553 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004554 HTTP_MSG_RQMETH,
4555 cur_ptr, cur_end + 1,
4556 NULL, NULL);
4557 if (unlikely(!cur_end))
4558 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004560 /* we have a full request and we know that we have either a CR
4561 * or an LF at <ptr>.
4562 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004563 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4564 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004565 /* there is no point trying this regex on headers */
4566 return 1;
4567 }
4568 }
4569 *cur_end = term; /* restore the string terminator */
4570 return done;
4571}
Willy Tarreau97de6242006-12-27 17:18:38 +01004572
Willy Tarreau58f10d72006-12-04 02:26:12 +01004573
Willy Tarreau58f10d72006-12-04 02:26:12 +01004574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004575/*
4576 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4577 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004578 * unparsable request. Since it can manage the switch to another backend, it
4579 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004580 */
4581int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4582{
Willy Tarreau3d300592007-03-18 18:34:41 +01004583 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004584 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004585 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004586 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004587
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004588 /*
4589 * The interleaving of transformations and verdicts
4590 * makes it difficult to decide to continue or stop
4591 * the evaluation.
4592 */
4593
Willy Tarreau3d300592007-03-18 18:34:41 +01004594 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004595 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4596 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4597 exp = exp->next;
4598 continue;
4599 }
4600
4601 /* Apply the filter to the request line. */
4602 ret = apply_filter_to_req_line(t, req, exp);
4603 if (unlikely(ret < 0))
4604 return -1;
4605
4606 if (likely(ret == 0)) {
4607 /* The filter did not match the request, it can be
4608 * iterated through all headers.
4609 */
4610 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004611 }
4612 exp = exp->next;
4613 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004614 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004615}
4616
4617
Willy Tarreaua15645d2007-03-18 16:22:39 +01004618
Willy Tarreau58f10d72006-12-04 02:26:12 +01004619/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004620 * Try to retrieve the server associated to the appsession.
4621 * If the server is found, it's assigned to the session.
4622 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01004623void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004624 struct http_txn *txn = &t->txn;
4625 appsess *asession = NULL;
4626 char *sessid_temp = NULL;
4627
Cyril Bontéb21570a2009-11-29 20:04:48 +01004628 if (len > t->be->appsession_len) {
4629 len = t->be->appsession_len;
4630 }
4631
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004632 if (t->be->options2 & PR_O2_AS_REQL) {
4633 /* request-learn option is enabled : store the sessid in the session for future use */
4634 if (t->sessid != NULL) {
4635 /* free previously allocated memory as we don't need the session id found in the URL anymore */
4636 pool_free2(apools.sessid, t->sessid);
4637 }
4638
4639 if ((t->sessid = 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(t->sessid, buf, len);
4646 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004647 }
4648
4649 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
4650 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4651 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4652 return;
4653 }
4654
Cyril Bontéb21570a2009-11-29 20:04:48 +01004655 memcpy(sessid_temp, buf, len);
4656 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004657
4658 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
4659 /* free previously allocated memory */
4660 pool_free2(apools.sessid, sessid_temp);
4661
4662 if (asession != NULL) {
4663 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
4664 if (!(t->be->options2 & PR_O2_AS_REQL))
4665 asession->request_count++;
4666
4667 if (asession->serverid != NULL) {
4668 struct server *srv = t->be->srv;
4669 while (srv) {
4670 if (strcmp(srv->id, asession->serverid) == 0) {
4671 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
4672 /* we found the server and it's usable */
4673 txn->flags &= ~TX_CK_MASK;
4674 txn->flags |= TX_CK_VALID;
4675 t->flags |= SN_DIRECT | SN_ASSIGNED;
4676 t->srv = srv;
4677 break;
4678 } else {
4679 txn->flags &= ~TX_CK_MASK;
4680 txn->flags |= TX_CK_DOWN;
4681 }
4682 }
4683 srv = srv->next;
4684 }
4685 }
4686 }
4687}
4688
4689/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004690 * Manage client-side cookie. It can impact performance by about 2% so it is
4691 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004692 */
4693void manage_client_side_cookies(struct session *t, struct buffer *req)
4694{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004695 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004696 char *p1, *p2, *p3, *p4;
4697 char *del_colon, *del_cookie, *colon;
4698 int app_cookies;
4699
Willy Tarreau58f10d72006-12-04 02:26:12 +01004700 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004701 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004702
Willy Tarreau2a324282006-12-05 00:05:46 +01004703 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004704 * we start with the start line.
4705 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004706 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004707 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004708
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004709 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004710 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004711 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004712
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004713 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004714 cur_ptr = cur_next;
4715 cur_end = cur_ptr + cur_hdr->len;
4716 cur_next = cur_end + cur_hdr->cr + 1;
4717
4718 /* We have one full header between cur_ptr and cur_end, and the
4719 * next header starts at cur_next. We're only interested in
4720 * "Cookie:" headers.
4721 */
4722
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004723 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4724 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004725 old_idx = cur_idx;
4726 continue;
4727 }
4728
4729 /* Now look for cookies. Conforming to RFC2109, we have to support
4730 * attributes whose name begin with a '$', and associate them with
4731 * the right cookie, if we want to delete this cookie.
4732 * So there are 3 cases for each cookie read :
4733 * 1) it's a special attribute, beginning with a '$' : ignore it.
4734 * 2) it's a server id cookie that we *MAY* want to delete : save
4735 * some pointers on it (last semi-colon, beginning of cookie...)
4736 * 3) it's an application cookie : we *MAY* have to delete a previous
4737 * "special" cookie.
4738 * At the end of loop, if a "special" cookie remains, we may have to
4739 * remove it. If no application cookie persists in the header, we
4740 * *MUST* delete it
4741 */
4742
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004743 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004744
Willy Tarreau58f10d72006-12-04 02:26:12 +01004745 /* del_cookie == NULL => nothing to be deleted */
4746 del_colon = del_cookie = NULL;
4747 app_cookies = 0;
4748
4749 while (p1 < cur_end) {
4750 /* skip spaces and colons, but keep an eye on these ones */
4751 while (p1 < cur_end) {
4752 if (*p1 == ';' || *p1 == ',')
4753 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004754 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004755 break;
4756 p1++;
4757 }
4758
4759 if (p1 == cur_end)
4760 break;
4761
4762 /* p1 is at the beginning of the cookie name */
4763 p2 = p1;
4764 while (p2 < cur_end && *p2 != '=')
4765 p2++;
4766
4767 if (p2 == cur_end)
4768 break;
4769
4770 p3 = p2 + 1; /* skips the '=' sign */
4771 if (p3 == cur_end)
4772 break;
4773
4774 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004775 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004776 p4++;
4777
4778 /* here, we have the cookie name between p1 and p2,
4779 * and its value between p3 and p4.
4780 * we can process it :
4781 *
4782 * Cookie: NAME=VALUE;
4783 * | || || |
4784 * | || || +--> p4
4785 * | || |+-------> p3
4786 * | || +--------> p2
4787 * | |+------------> p1
4788 * | +-------------> colon
4789 * +--------------------> cur_ptr
4790 */
4791
4792 if (*p1 == '$') {
4793 /* skip this one */
4794 }
4795 else {
4796 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004797 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004798 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004799 (p4 - p1 >= t->fe->capture_namelen) &&
4800 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004801 int log_len = p4 - p1;
4802
Willy Tarreau086b3b42007-05-13 21:45:51 +02004803 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004804 Alert("HTTP logging : out of memory.\n");
4805 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004806 if (log_len > t->fe->capture_len)
4807 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004808 memcpy(txn->cli_cookie, p1, log_len);
4809 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004810 }
4811 }
4812
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004813 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4814 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004815 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004816 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004817 char *delim;
4818
4819 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4820 * have the server ID betweek p3 and delim, and the original cookie between
4821 * delim+1 and p4. Otherwise, delim==p4 :
4822 *
4823 * Cookie: NAME=SRV~VALUE;
4824 * | || || | |
4825 * | || || | +--> p4
4826 * | || || +--------> delim
4827 * | || |+-----------> p3
4828 * | || +------------> p2
4829 * | |+----------------> p1
4830 * | +-----------------> colon
4831 * +------------------------> cur_ptr
4832 */
4833
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004834 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004835 for (delim = p3; delim < p4; delim++)
4836 if (*delim == COOKIE_DELIM)
4837 break;
4838 }
4839 else
4840 delim = p4;
4841
4842
4843 /* Here, we'll look for the first running server which supports the cookie.
4844 * This allows to share a same cookie between several servers, for example
4845 * to dedicate backup servers to specific servers only.
4846 * However, to prevent clients from sticking to cookie-less backup server
4847 * when they have incidentely learned an empty cookie, we simply ignore
4848 * empty cookies and mark them as invalid.
4849 */
4850 if (delim == p3)
4851 srv = NULL;
4852
4853 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004854 if (srv->cookie && (srv->cklen == delim - p3) &&
4855 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004856 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004857 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004858 txn->flags &= ~TX_CK_MASK;
4859 txn->flags |= TX_CK_VALID;
4860 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004861 t->srv = srv;
4862 break;
4863 } else {
4864 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004865 txn->flags &= ~TX_CK_MASK;
4866 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004867 }
4868 }
4869 srv = srv->next;
4870 }
4871
Willy Tarreau3d300592007-03-18 18:34:41 +01004872 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004873 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004874 txn->flags &= ~TX_CK_MASK;
4875 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004876 }
4877
4878 /* depending on the cookie mode, we may have to either :
4879 * - delete the complete cookie if we're in insert+indirect mode, so that
4880 * the server never sees it ;
4881 * - remove the server id from the cookie value, and tag the cookie as an
4882 * application cookie so that it does not get accidentely removed later,
4883 * if we're in cookie prefix mode
4884 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004885 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004886 int delta; /* negative */
4887
4888 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4889 p4 += delta;
4890 cur_end += delta;
4891 cur_next += delta;
4892 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004893 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004894
4895 del_cookie = del_colon = NULL;
4896 app_cookies++; /* protect the header from deletion */
4897 }
4898 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004899 (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 +01004900 del_cookie = p1;
4901 del_colon = colon;
4902 }
4903 } else {
4904 /* now we know that we must keep this cookie since it's
4905 * not ours. But if we wanted to delete our cookie
4906 * earlier, we cannot remove the complete header, but we
4907 * can remove the previous block itself.
4908 */
4909 app_cookies++;
4910
4911 if (del_cookie != NULL) {
4912 int delta; /* negative */
4913
4914 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4915 p4 += delta;
4916 cur_end += delta;
4917 cur_next += delta;
4918 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004919 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004920 del_cookie = del_colon = NULL;
4921 }
4922 }
4923
Cyril Bontéb21570a2009-11-29 20:04:48 +01004924 if (t->be->appsession_name != NULL) {
4925 int cmp_len, value_len;
4926 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004927
Cyril Bontéb21570a2009-11-29 20:04:48 +01004928 if (t->be->options2 & PR_O2_AS_PFX) {
4929 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
4930 value_begin = p1 + t->be->appsession_name_len;
4931 value_len = p4 - p1 - t->be->appsession_name_len;
4932 } else {
4933 cmp_len = p2 - p1;
4934 value_begin = p3;
4935 value_len = p4 - p3;
4936 }
4937
4938 /* let's see if the cookie is our appcookie */
4939 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
4940 /* Cool... it's the right one */
4941 manage_client_side_appsession(t, value_begin, value_len);
4942 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004943#if defined(DEBUG_HASH)
4944 Alert("manage_client_side_cookies\n");
4945 appsession_hash_dump(&(t->be->htbl_proxy));
4946#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004947 }/* end if ((t->proxy->appsession_name != NULL) ... */
4948 }
4949
4950 /* we'll have to look for another cookie ... */
4951 p1 = p4;
4952 } /* while (p1 < cur_end) */
4953
4954 /* There's no more cookie on this line.
4955 * We may have marked the last one(s) for deletion.
4956 * We must do this now in two ways :
4957 * - if there is no app cookie, we simply delete the header ;
4958 * - if there are app cookies, we must delete the end of the
4959 * string properly, including the colon/semi-colon before
4960 * the cookie name.
4961 */
4962 if (del_cookie != NULL) {
4963 int delta;
4964 if (app_cookies) {
4965 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4966 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004967 cur_hdr->len += delta;
4968 } else {
4969 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004970
4971 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004972 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4973 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004974 cur_hdr->len = 0;
4975 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004976 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004977 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004978 }
4979
4980 /* keep the link from this header to next one */
4981 old_idx = cur_idx;
4982 } /* end of cookie processing on this header */
4983}
4984
4985
Willy Tarreaua15645d2007-03-18 16:22:39 +01004986/* Iterate the same filter through all response headers contained in <rtr>.
4987 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4988 */
4989int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4990{
4991 char term;
4992 char *cur_ptr, *cur_end, *cur_next;
4993 int cur_idx, old_idx, last_hdr;
4994 struct http_txn *txn = &t->txn;
4995 struct hdr_idx_elem *cur_hdr;
4996 int len, delta;
4997
4998 last_hdr = 0;
4999
5000 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5001 old_idx = 0;
5002
5003 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005004 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005005 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005006 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005007 (exp->action == ACT_ALLOW ||
5008 exp->action == ACT_DENY))
5009 return 0;
5010
5011 cur_idx = txn->hdr_idx.v[old_idx].next;
5012 if (!cur_idx)
5013 break;
5014
5015 cur_hdr = &txn->hdr_idx.v[cur_idx];
5016 cur_ptr = cur_next;
5017 cur_end = cur_ptr + cur_hdr->len;
5018 cur_next = cur_end + cur_hdr->cr + 1;
5019
5020 /* Now we have one header between cur_ptr and cur_end,
5021 * and the next header starts at cur_next.
5022 */
5023
5024 /* The annoying part is that pattern matching needs
5025 * that we modify the contents to null-terminate all
5026 * strings before testing them.
5027 */
5028
5029 term = *cur_end;
5030 *cur_end = '\0';
5031
5032 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5033 switch (exp->action) {
5034 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005035 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005036 last_hdr = 1;
5037 break;
5038
5039 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005040 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005041 last_hdr = 1;
5042 break;
5043
5044 case ACT_REPLACE:
5045 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5046 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5047 /* FIXME: if the user adds a newline in the replacement, the
5048 * index will not be recalculated for now, and the new line
5049 * will not be counted as a new header.
5050 */
5051
5052 cur_end += delta;
5053 cur_next += delta;
5054 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005055 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005056 break;
5057
5058 case ACT_REMOVE:
5059 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5060 cur_next += delta;
5061
Willy Tarreaufa355d42009-11-29 18:12:29 +01005062 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005063 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5064 txn->hdr_idx.used--;
5065 cur_hdr->len = 0;
5066 cur_end = NULL; /* null-term has been rewritten */
5067 break;
5068
5069 }
5070 }
5071 if (cur_end)
5072 *cur_end = term; /* restore the string terminator */
5073
5074 /* keep the link from this header to next one in case of later
5075 * removal of next header.
5076 */
5077 old_idx = cur_idx;
5078 }
5079 return 0;
5080}
5081
5082
5083/* Apply the filter to the status line in the response buffer <rtr>.
5084 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5085 * or -1 if a replacement resulted in an invalid status line.
5086 */
5087int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5088{
5089 char term;
5090 char *cur_ptr, *cur_end;
5091 int done;
5092 struct http_txn *txn = &t->txn;
5093 int len, delta;
5094
5095
Willy Tarreau3d300592007-03-18 18:34:41 +01005096 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005097 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005098 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005099 (exp->action == ACT_ALLOW ||
5100 exp->action == ACT_DENY))
5101 return 0;
5102 else if (exp->action == ACT_REMOVE)
5103 return 0;
5104
5105 done = 0;
5106
Willy Tarreau9cdde232007-05-02 20:58:19 +02005107 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005108 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5109
5110 /* Now we have the status line between cur_ptr and cur_end */
5111
5112 /* The annoying part is that pattern matching needs
5113 * that we modify the contents to null-terminate all
5114 * strings before testing them.
5115 */
5116
5117 term = *cur_end;
5118 *cur_end = '\0';
5119
5120 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5121 switch (exp->action) {
5122 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005123 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005124 done = 1;
5125 break;
5126
5127 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005128 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005129 done = 1;
5130 break;
5131
5132 case ACT_REPLACE:
5133 *cur_end = term; /* restore the string terminator */
5134 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5135 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5136 /* FIXME: if the user adds a newline in the replacement, the
5137 * index will not be recalculated for now, and the new line
5138 * will not be counted as a new header.
5139 */
5140
Willy Tarreaufa355d42009-11-29 18:12:29 +01005141 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005142 cur_end += delta;
5143
Willy Tarreau9cdde232007-05-02 20:58:19 +02005144 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005145 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005146 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005147 cur_ptr, cur_end + 1,
5148 NULL, NULL);
5149 if (unlikely(!cur_end))
5150 return -1;
5151
5152 /* we have a full respnse and we know that we have either a CR
5153 * or an LF at <ptr>.
5154 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005155 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005156 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5157 /* there is no point trying this regex on headers */
5158 return 1;
5159 }
5160 }
5161 *cur_end = term; /* restore the string terminator */
5162 return done;
5163}
5164
5165
5166
5167/*
5168 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5169 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5170 * unparsable response.
5171 */
5172int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5173{
Willy Tarreau3d300592007-03-18 18:34:41 +01005174 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005175 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005176 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005177 int ret;
5178
5179 /*
5180 * The interleaving of transformations and verdicts
5181 * makes it difficult to decide to continue or stop
5182 * the evaluation.
5183 */
5184
Willy Tarreau3d300592007-03-18 18:34:41 +01005185 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005186 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5187 exp->action == ACT_PASS)) {
5188 exp = exp->next;
5189 continue;
5190 }
5191
5192 /* Apply the filter to the status line. */
5193 ret = apply_filter_to_sts_line(t, rtr, exp);
5194 if (unlikely(ret < 0))
5195 return -1;
5196
5197 if (likely(ret == 0)) {
5198 /* The filter did not match the response, it can be
5199 * iterated through all headers.
5200 */
5201 apply_filter_to_resp_headers(t, rtr, exp);
5202 }
5203 exp = exp->next;
5204 }
5205 return 0;
5206}
5207
5208
5209
5210/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005211 * Manage server-side cookies. It can impact performance by about 2% so it is
5212 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005213 */
5214void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5215{
5216 struct http_txn *txn = &t->txn;
5217 char *p1, *p2, *p3, *p4;
5218
Willy Tarreaua15645d2007-03-18 16:22:39 +01005219 char *cur_ptr, *cur_end, *cur_next;
5220 int cur_idx, old_idx, delta;
5221
Willy Tarreaua15645d2007-03-18 16:22:39 +01005222 /* Iterate through the headers.
5223 * we start with the start line.
5224 */
5225 old_idx = 0;
5226 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5227
5228 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5229 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005230 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005231
5232 cur_hdr = &txn->hdr_idx.v[cur_idx];
5233 cur_ptr = cur_next;
5234 cur_end = cur_ptr + cur_hdr->len;
5235 cur_next = cur_end + cur_hdr->cr + 1;
5236
5237 /* We have one full header between cur_ptr and cur_end, and the
5238 * next header starts at cur_next. We're only interested in
5239 * "Cookie:" headers.
5240 */
5241
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005242 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5243 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005244 old_idx = cur_idx;
5245 continue;
5246 }
5247
5248 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005249 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005250
5251
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005252 /* maybe we only wanted to see if there was a set-cookie. Note that
5253 * the cookie capture is declared in the fronend.
5254 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005255 if (t->be->cookie_name == NULL &&
5256 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005257 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005258 return;
5259
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005260 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005261
5262 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005263 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5264 break;
5265
5266 /* p1 is at the beginning of the cookie name */
5267 p2 = p1;
5268
5269 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5270 p2++;
5271
5272 if (p2 == cur_end || *p2 == ';') /* next cookie */
5273 break;
5274
5275 p3 = p2 + 1; /* skip the '=' sign */
5276 if (p3 == cur_end)
5277 break;
5278
5279 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005280 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005281 p4++;
5282
5283 /* here, we have the cookie name between p1 and p2,
5284 * and its value between p3 and p4.
5285 * we can process it.
5286 */
5287
5288 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005289 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005290 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005291 (p4 - p1 >= t->fe->capture_namelen) &&
5292 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005293 int log_len = p4 - p1;
5294
Willy Tarreau086b3b42007-05-13 21:45:51 +02005295 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005296 Alert("HTTP logging : out of memory.\n");
5297 }
5298
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005299 if (log_len > t->fe->capture_len)
5300 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005301 memcpy(txn->srv_cookie, p1, log_len);
5302 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005303 }
5304
5305 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005306 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5307 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005308 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005309 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005310
5311 /* If the cookie is in insert mode on a known server, we'll delete
5312 * this occurrence because we'll insert another one later.
5313 * We'll delete it too if the "indirect" option is set and we're in
5314 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005315 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5316 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005317 /* this header must be deleted */
5318 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5319 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5320 txn->hdr_idx.used--;
5321 cur_hdr->len = 0;
5322 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005323 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005324
Willy Tarreau3d300592007-03-18 18:34:41 +01005325 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005326 }
5327 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005328 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005329 /* replace bytes p3->p4 with the cookie name associated
5330 * with this server since we know it.
5331 */
5332 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5333 cur_hdr->len += delta;
5334 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005335 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005336
Willy Tarreau3d300592007-03-18 18:34:41 +01005337 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005338 }
5339 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005340 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005341 /* insert the cookie name associated with this server
5342 * before existing cookie, and insert a delimitor between them..
5343 */
5344 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5345 cur_hdr->len += delta;
5346 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005347 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005348
5349 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005350 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005351 }
5352 }
5353 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005354 else if (t->be->appsession_name != NULL) {
5355 int cmp_len, value_len;
5356 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005357
Cyril Bontéb21570a2009-11-29 20:04:48 +01005358 if (t->be->options2 & PR_O2_AS_PFX) {
5359 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5360 value_begin = p1 + t->be->appsession_name_len;
5361 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5362 } else {
5363 cmp_len = p2 - p1;
5364 value_begin = p3;
5365 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005366 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005367
5368 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5369 /* Cool... it's the right one */
5370 if (t->sessid != NULL) {
5371 /* free previously allocated memory as we don't need it anymore */
5372 pool_free2(apools.sessid, t->sessid);
5373 }
5374 /* Store the sessid in the session for future use */
5375 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5376 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5377 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5378 return;
5379 }
5380 memcpy(t->sessid, value_begin, value_len);
5381 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005382 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005383 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005384 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5385 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005386 /* keep the link from this header to next one */
5387 old_idx = cur_idx;
5388 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005389
5390 if (t->sessid != NULL) {
5391 appsess *asession = NULL;
5392 /* only do insert, if lookup fails */
5393 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5394 if (asession == NULL) {
5395 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5396 Alert("Not enough Memory process_srv():asession:calloc().\n");
5397 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5398 return;
5399 }
5400 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5401 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5402 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5403 return;
5404 }
5405 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5406 asession->sessid[t->be->appsession_len] = 0;
5407
5408 size_t server_id_len = strlen(t->srv->id) + 1;
5409 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5410 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5411 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5412 return;
5413 }
5414 asession->serverid[0] = '\0';
5415 memcpy(asession->serverid, t->srv->id, server_id_len);
5416
5417 asession->request_count = 0;
5418 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5419 }
5420
5421 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5422 asession->request_count++;
5423 }
5424
5425#if defined(DEBUG_HASH)
5426 Alert("manage_server_side_cookies\n");
5427 appsession_hash_dump(&(t->be->htbl_proxy));
5428#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005429}
5430
5431
5432
5433/*
5434 * Check if response is cacheable or not. Updates t->flags.
5435 */
5436void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5437{
5438 struct http_txn *txn = &t->txn;
5439 char *p1, *p2;
5440
5441 char *cur_ptr, *cur_end, *cur_next;
5442 int cur_idx;
5443
Willy Tarreau5df51872007-11-25 16:20:08 +01005444 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005445 return;
5446
5447 /* Iterate through the headers.
5448 * we start with the start line.
5449 */
5450 cur_idx = 0;
5451 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5452
5453 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5454 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005455 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005456
5457 cur_hdr = &txn->hdr_idx.v[cur_idx];
5458 cur_ptr = cur_next;
5459 cur_end = cur_ptr + cur_hdr->len;
5460 cur_next = cur_end + cur_hdr->cr + 1;
5461
5462 /* We have one full header between cur_ptr and cur_end, and the
5463 * next header starts at cur_next. We're only interested in
5464 * "Cookie:" headers.
5465 */
5466
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005467 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5468 if (val) {
5469 if ((cur_end - (cur_ptr + val) >= 8) &&
5470 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5471 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5472 return;
5473 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005474 }
5475
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005476 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5477 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005478 continue;
5479
5480 /* OK, right now we know we have a cache-control header at cur_ptr */
5481
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005482 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005483
5484 if (p1 >= cur_end) /* no more info */
5485 continue;
5486
5487 /* p1 is at the beginning of the value */
5488 p2 = p1;
5489
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005490 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005491 p2++;
5492
5493 /* we have a complete value between p1 and p2 */
5494 if (p2 < cur_end && *p2 == '=') {
5495 /* we have something of the form no-cache="set-cookie" */
5496 if ((cur_end - p1 >= 21) &&
5497 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5498 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005499 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005500 continue;
5501 }
5502
5503 /* OK, so we know that either p2 points to the end of string or to a comma */
5504 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5505 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5506 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5507 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005508 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005509 return;
5510 }
5511
5512 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005513 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005514 continue;
5515 }
5516 }
5517}
5518
5519
Willy Tarreau58f10d72006-12-04 02:26:12 +01005520/*
5521 * Try to retrieve a known appsession in the URI, then the associated server.
5522 * If the server is found, it's assigned to the session.
5523 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005524void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005525{
Cyril Bontéb21570a2009-11-29 20:04:48 +01005526 char *end_params, *first_param, *cur_param, *next_param;
5527 char separator;
5528 int value_len;
5529
5530 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005531
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005532 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01005533 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005534 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005535 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005536
Cyril Bontéb21570a2009-11-29 20:04:48 +01005537 first_param = NULL;
5538 switch (mode) {
5539 case PR_O2_AS_M_PP:
5540 first_param = memchr(begin, ';', len);
5541 break;
5542 case PR_O2_AS_M_QS:
5543 first_param = memchr(begin, '?', len);
5544 break;
5545 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005546
Cyril Bontéb21570a2009-11-29 20:04:48 +01005547 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005548 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005549 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005550
Cyril Bontéb21570a2009-11-29 20:04:48 +01005551 switch (mode) {
5552 case PR_O2_AS_M_PP:
5553 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
5554 end_params = (char *) begin + len;
5555 }
5556 separator = ';';
5557 break;
5558 case PR_O2_AS_M_QS:
5559 end_params = (char *) begin + len;
5560 separator = '&';
5561 break;
5562 default:
5563 /* unknown mode, shouldn't happen */
5564 return;
5565 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005566
Cyril Bontéb21570a2009-11-29 20:04:48 +01005567 cur_param = next_param = end_params;
5568 while (cur_param > first_param) {
5569 cur_param--;
5570 if ((cur_param[0] == separator) || (cur_param == first_param)) {
5571 /* let's see if this is the appsession parameter */
5572 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
5573 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
5574 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
5575 /* Cool... it's the right one */
5576 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
5577 value_len = MIN(t->be->appsession_len, next_param - cur_param);
5578 if (value_len > 0) {
5579 manage_client_side_appsession(t, cur_param, value_len);
5580 }
5581 break;
5582 }
5583 next_param = cur_param;
5584 }
5585 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005586#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005587 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005588 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005589#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005590}
5591
5592
Willy Tarreaub2513902006-12-17 14:52:38 +01005593/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005594 * In a GET or HEAD request, check if the requested URI matches the stats uri
5595 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005596 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005597 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005598 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005599 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005600 *
5601 * Returns 1 if the session's state changes, otherwise 0.
5602 */
5603int stats_check_uri_auth(struct session *t, struct proxy *backend)
5604{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005605 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005606 struct uri_auth *uri_auth = backend->uri_auth;
5607 struct user_auth *user;
5608 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005609 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005610
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005611 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5612
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005613 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005614 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005615 return 0;
5616
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005617 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005618
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005619 /* the URI is in h */
5620 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005621 return 0;
5622
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005623 h += uri_auth->uri_len;
5624 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5625 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005626 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005627 break;
5628 }
5629 h++;
5630 }
5631
5632 if (uri_auth->refresh) {
5633 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 - 10) {
5635 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005636 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005637 break;
5638 }
5639 h++;
5640 }
5641 }
5642
Willy Tarreau55bb8452007-10-17 18:44:57 +02005643 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5644 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5645 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005646 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005647 break;
5648 }
5649 h++;
5650 }
5651
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005652 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5653
Willy Tarreaub2513902006-12-17 14:52:38 +01005654 /* we are in front of a interceptable URI. Let's check
5655 * if there's an authentication and if it's valid.
5656 */
5657 user = uri_auth->users;
5658 if (!user) {
5659 /* no user auth required, it's OK */
5660 authenticated = 1;
5661 } else {
5662 authenticated = 0;
5663
5664 /* a user list is defined, we have to check.
5665 * skip 21 chars for "Authorization: Basic ".
5666 */
5667
5668 /* FIXME: this should move to an earlier place */
5669 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005670 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5671 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5672 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005673 if (len > 14 &&
5674 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02005675 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01005676 break;
5677 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005678 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005679 }
5680
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005681 if (txn->auth_hdr.len < 21 ||
5682 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005683 user = NULL;
5684
5685 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005686 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5687 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005688 user->user_pwd, user->user_len)) {
5689 authenticated = 1;
5690 break;
5691 }
5692 user = user->next;
5693 }
5694 }
5695
5696 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005697 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005698
5699 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005700 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
5701 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005702 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01005703 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005704 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005705 if (!(t->flags & SN_ERR_MASK))
5706 t->flags |= SN_ERR_PRXCOND;
5707 if (!(t->flags & SN_FINST_MASK))
5708 t->flags |= SN_FINST_R;
5709 return 1;
5710 }
5711
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005712 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005713 * data.
5714 */
Willy Tarreau70089872008-06-13 21:12:51 +02005715 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005716 t->data_source = DATA_SRC_STATS;
5717 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005718 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005719 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
5720 t->rep->prod->private = t;
5721 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005722 return 1;
5723}
5724
Willy Tarreau4076a152009-04-02 15:18:36 +02005725/*
5726 * Capture a bad request or response and archive it in the proxy's structure.
5727 */
5728void http_capture_bad_message(struct error_snapshot *es, struct session *s,
5729 struct buffer *buf, struct http_msg *msg,
5730 struct proxy *other_end)
5731{
Willy Tarreau2df8d712009-05-01 11:33:17 +02005732 es->len = buf->r - (buf->data + msg->som);
5733 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02005734 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02005735 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02005736 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02005737 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02005738 es->when = date; // user-visible date
5739 es->sid = s->uniq_id;
5740 es->srv = s->srv;
5741 es->oe = other_end;
5742 es->src = s->cli_addr;
5743}
Willy Tarreaub2513902006-12-17 14:52:38 +01005744
Willy Tarreaubaaee002006-06-26 02:48:02 +02005745/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005746 * Print a debug line with a header
5747 */
5748void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5749{
5750 int len, max;
5751 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005752 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005753 max = end - start;
5754 UBOUND(max, sizeof(trash) - len - 1);
5755 len += strlcpy2(trash + len, start, max + 1);
5756 trash[len++] = '\n';
5757 write(1, trash, len);
5758}
5759
Willy Tarreau0937bc42009-12-22 15:03:09 +01005760/*
5761 * Initialize a new HTTP transaction for session <s>. It is assumed that all
5762 * the required fields are properly allocated and that we only need to (re)init
5763 * them. This should be used before processing any new request.
5764 */
5765void http_init_txn(struct session *s)
5766{
5767 struct http_txn *txn = &s->txn;
5768 struct proxy *fe = s->fe;
5769
5770 txn->flags = 0;
5771 txn->status = -1;
5772
5773 txn->req.sol = txn->req.eol = NULL;
5774 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
5775 txn->rsp.sol = txn->rsp.eol = NULL;
5776 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
5777 txn->req.hdr_content_len = 0LL;
5778 txn->rsp.hdr_content_len = 0LL;
5779 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5780 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5781 chunk_reset(&txn->auth_hdr);
5782
5783 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
5784 if (fe->options2 & PR_O2_REQBUG_OK)
5785 txn->req.err_pos = -1; /* let buggy requests pass */
5786
5787 if (txn->req.cap)
5788 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
5789
5790 if (txn->rsp.cap)
5791 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
5792
5793 if (txn->hdr_idx.v)
5794 hdr_idx_init(&txn->hdr_idx);
5795}
5796
5797/* to be used at the end of a transaction */
5798void http_end_txn(struct session *s)
5799{
5800 struct http_txn *txn = &s->txn;
5801
5802 /* these ones will have been dynamically allocated */
5803 pool_free2(pool2_requri, txn->uri);
5804 pool_free2(pool2_capture, txn->cli_cookie);
5805 pool_free2(pool2_capture, txn->srv_cookie);
5806 txn->uri = NULL;
5807 txn->srv_cookie = NULL;
5808 txn->cli_cookie = NULL;
5809}
5810
5811/* to be used at the end of a transaction to prepare a new one */
5812void http_reset_txn(struct session *s)
5813{
5814 http_end_txn(s);
5815 http_init_txn(s);
5816
5817 s->be = s->fe;
5818 s->req->analysers = s->listener->analysers;
5819 s->logs.logwait = s->fe->to_log;
5820 s->srv = s->prev_srv = s->srv_conn = NULL;
5821 s->pend_pos = NULL;
5822 s->conn_retries = s->be->conn_retries;
5823
5824 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
5825
5826 s->req->rto = s->fe->timeout.client;
5827 s->req->wto = s->be->timeout.server;
5828 s->req->cto = s->be->timeout.connect;
5829
5830 s->rep->rto = s->be->timeout.server;
5831 s->rep->wto = s->fe->timeout.client;
5832 s->rep->cto = TICK_ETERNITY;
5833
5834 s->req->rex = TICK_ETERNITY;
5835 s->req->wex = TICK_ETERNITY;
5836 s->req->analyse_exp = TICK_ETERNITY;
5837 s->rep->rex = TICK_ETERNITY;
5838 s->rep->wex = TICK_ETERNITY;
5839 s->rep->analyse_exp = TICK_ETERNITY;
5840}
Willy Tarreau58f10d72006-12-04 02:26:12 +01005841
Willy Tarreau8797c062007-05-07 00:55:35 +02005842/************************************************************************/
5843/* The code below is dedicated to ACL parsing and matching */
5844/************************************************************************/
5845
5846
5847
5848
5849/* 1. Check on METHOD
5850 * We use the pre-parsed method if it is known, and store its number as an
5851 * integer. If it is unknown, we use the pointer and the length.
5852 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005853static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005854{
5855 int len, meth;
5856
Willy Tarreauae8b7962007-06-09 23:10:04 +02005857 len = strlen(*text);
5858 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005859
5860 pattern->val.i = meth;
5861 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005862 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005863 if (!pattern->ptr.str)
5864 return 0;
5865 pattern->len = len;
5866 }
5867 return 1;
5868}
5869
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005870static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005871acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5872 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005873{
5874 int meth;
5875 struct http_txn *txn = l7;
5876
Willy Tarreaub6866442008-07-14 23:54:42 +02005877 if (!txn)
5878 return 0;
5879
Willy Tarreau655dce92009-11-08 13:10:58 +01005880 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005881 return 0;
5882
Willy Tarreau8797c062007-05-07 00:55:35 +02005883 meth = txn->meth;
5884 test->i = meth;
5885 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005886 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5887 /* ensure the indexes are not affected */
5888 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005889 test->len = txn->req.sl.rq.m_l;
5890 test->ptr = txn->req.sol;
5891 }
5892 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5893 return 1;
5894}
5895
5896static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5897{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005898 int icase;
5899
Willy Tarreau8797c062007-05-07 00:55:35 +02005900 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005901 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005902
5903 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005904 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005905
5906 /* Other method, we must compare the strings */
5907 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005908 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005909
5910 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5911 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5912 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005913 return ACL_PAT_FAIL;
5914 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005915}
5916
5917/* 2. Check on Request/Status Version
5918 * We simply compare strings here.
5919 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005920static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005921{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005922 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005923 if (!pattern->ptr.str)
5924 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005925 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005926 return 1;
5927}
5928
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005929static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005930acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5931 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005932{
5933 struct http_txn *txn = l7;
5934 char *ptr;
5935 int len;
5936
Willy Tarreaub6866442008-07-14 23:54:42 +02005937 if (!txn)
5938 return 0;
5939
Willy Tarreau655dce92009-11-08 13:10:58 +01005940 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005941 return 0;
5942
Willy Tarreau8797c062007-05-07 00:55:35 +02005943 len = txn->req.sl.rq.v_l;
5944 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5945
5946 while ((len-- > 0) && (*ptr++ != '/'));
5947 if (len <= 0)
5948 return 0;
5949
5950 test->ptr = ptr;
5951 test->len = len;
5952
5953 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5954 return 1;
5955}
5956
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005957static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005958acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5959 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005960{
5961 struct http_txn *txn = l7;
5962 char *ptr;
5963 int len;
5964
Willy Tarreaub6866442008-07-14 23:54:42 +02005965 if (!txn)
5966 return 0;
5967
Willy Tarreau655dce92009-11-08 13:10:58 +01005968 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005969 return 0;
5970
Willy Tarreau8797c062007-05-07 00:55:35 +02005971 len = txn->rsp.sl.st.v_l;
5972 ptr = txn->rsp.sol;
5973
5974 while ((len-- > 0) && (*ptr++ != '/'));
5975 if (len <= 0)
5976 return 0;
5977
5978 test->ptr = ptr;
5979 test->len = len;
5980
5981 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5982 return 1;
5983}
5984
5985/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005986static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005987acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5988 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005989{
5990 struct http_txn *txn = l7;
5991 char *ptr;
5992 int len;
5993
Willy Tarreaub6866442008-07-14 23:54:42 +02005994 if (!txn)
5995 return 0;
5996
Willy Tarreau655dce92009-11-08 13:10:58 +01005997 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005998 return 0;
5999
Willy Tarreau8797c062007-05-07 00:55:35 +02006000 len = txn->rsp.sl.st.c_l;
6001 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6002
6003 test->i = __strl2ui(ptr, len);
6004 test->flags = ACL_TEST_F_VOL_1ST;
6005 return 1;
6006}
6007
6008/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006009static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006010acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6011 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006012{
6013 struct http_txn *txn = l7;
6014
Willy Tarreaub6866442008-07-14 23:54:42 +02006015 if (!txn)
6016 return 0;
6017
Willy Tarreau655dce92009-11-08 13:10:58 +01006018 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006019 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006020
Willy Tarreauc11416f2007-06-17 16:58:38 +02006021 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6022 /* ensure the indexes are not affected */
6023 return 0;
6024
Willy Tarreau8797c062007-05-07 00:55:35 +02006025 test->len = txn->req.sl.rq.u_l;
6026 test->ptr = txn->req.sol + txn->req.sl.rq.u;
6027
Willy Tarreauf3d25982007-05-08 22:45:09 +02006028 /* we do not need to set READ_ONLY because the data is in a buffer */
6029 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006030 return 1;
6031}
6032
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006033static int
6034acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6035 struct acl_expr *expr, struct acl_test *test)
6036{
6037 struct http_txn *txn = l7;
6038
Willy Tarreaub6866442008-07-14 23:54:42 +02006039 if (!txn)
6040 return 0;
6041
Willy Tarreau655dce92009-11-08 13:10:58 +01006042 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006043 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006044
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006045 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6046 /* ensure the indexes are not affected */
6047 return 0;
6048
6049 /* Parse HTTP request */
6050 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6051 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6052 test->i = AF_INET;
6053
6054 /*
6055 * If we are parsing url in frontend space, we prepare backend stage
6056 * to not parse again the same url ! optimization lazyness...
6057 */
6058 if (px->options & PR_O_HTTP_PROXY)
6059 l4->flags |= SN_ADDR_SET;
6060
6061 test->flags = ACL_TEST_F_READ_ONLY;
6062 return 1;
6063}
6064
6065static int
6066acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6067 struct acl_expr *expr, struct acl_test *test)
6068{
6069 struct http_txn *txn = l7;
6070
Willy Tarreaub6866442008-07-14 23:54:42 +02006071 if (!txn)
6072 return 0;
6073
Willy Tarreau655dce92009-11-08 13:10:58 +01006074 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006075 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006076
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006077 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6078 /* ensure the indexes are not affected */
6079 return 0;
6080
6081 /* Same optimization as url_ip */
6082 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
6083 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6084
6085 if (px->options & PR_O_HTTP_PROXY)
6086 l4->flags |= SN_ADDR_SET;
6087
6088 test->flags = ACL_TEST_F_READ_ONLY;
6089 return 1;
6090}
6091
Willy Tarreauc11416f2007-06-17 16:58:38 +02006092/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6093 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6094 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006095static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006096acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006097 struct acl_expr *expr, struct acl_test *test)
6098{
6099 struct http_txn *txn = l7;
6100 struct hdr_idx *idx = &txn->hdr_idx;
6101 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006102
Willy Tarreaub6866442008-07-14 23:54:42 +02006103 if (!txn)
6104 return 0;
6105
Willy Tarreau33a7e692007-06-10 19:45:56 +02006106 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6107 /* search for header from the beginning */
6108 ctx->idx = 0;
6109
Willy Tarreau33a7e692007-06-10 19:45:56 +02006110 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6111 test->flags |= ACL_TEST_F_FETCH_MORE;
6112 test->flags |= ACL_TEST_F_VOL_HDR;
6113 test->len = ctx->vlen;
6114 test->ptr = (char *)ctx->line + ctx->val;
6115 return 1;
6116 }
6117
6118 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6119 test->flags |= ACL_TEST_F_VOL_HDR;
6120 return 0;
6121}
6122
Willy Tarreau33a7e692007-06-10 19:45:56 +02006123static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006124acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6125 struct acl_expr *expr, struct acl_test *test)
6126{
6127 struct http_txn *txn = l7;
6128
Willy Tarreaub6866442008-07-14 23:54:42 +02006129 if (!txn)
6130 return 0;
6131
Willy Tarreau655dce92009-11-08 13:10:58 +01006132 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006133 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006134
Willy Tarreauc11416f2007-06-17 16:58:38 +02006135 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6136 /* ensure the indexes are not affected */
6137 return 0;
6138
6139 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6140}
6141
6142static int
6143acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6144 struct acl_expr *expr, struct acl_test *test)
6145{
6146 struct http_txn *txn = l7;
6147
Willy Tarreaub6866442008-07-14 23:54:42 +02006148 if (!txn)
6149 return 0;
6150
Willy Tarreau655dce92009-11-08 13:10:58 +01006151 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006152 return 0;
6153
6154 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6155}
6156
6157/* 6. Check on HTTP header count. The number of occurrences is returned.
6158 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6159 */
6160static int
6161acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006162 struct acl_expr *expr, struct acl_test *test)
6163{
6164 struct http_txn *txn = l7;
6165 struct hdr_idx *idx = &txn->hdr_idx;
6166 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006167 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006168
Willy Tarreaub6866442008-07-14 23:54:42 +02006169 if (!txn)
6170 return 0;
6171
Willy Tarreau33a7e692007-06-10 19:45:56 +02006172 ctx.idx = 0;
6173 cnt = 0;
6174 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6175 cnt++;
6176
6177 test->i = cnt;
6178 test->flags = ACL_TEST_F_VOL_HDR;
6179 return 1;
6180}
6181
Willy Tarreauc11416f2007-06-17 16:58:38 +02006182static int
6183acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6184 struct acl_expr *expr, struct acl_test *test)
6185{
6186 struct http_txn *txn = l7;
6187
Willy Tarreaub6866442008-07-14 23:54:42 +02006188 if (!txn)
6189 return 0;
6190
Willy Tarreau655dce92009-11-08 13:10:58 +01006191 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006192 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006193
Willy Tarreauc11416f2007-06-17 16:58:38 +02006194 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6195 /* ensure the indexes are not affected */
6196 return 0;
6197
6198 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6199}
6200
6201static int
6202acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6203 struct acl_expr *expr, struct acl_test *test)
6204{
6205 struct http_txn *txn = l7;
6206
Willy Tarreaub6866442008-07-14 23:54:42 +02006207 if (!txn)
6208 return 0;
6209
Willy Tarreau655dce92009-11-08 13:10:58 +01006210 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006211 return 0;
6212
6213 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6214}
6215
Willy Tarreau33a7e692007-06-10 19:45:56 +02006216/* 7. Check on HTTP header's integer value. The integer value is returned.
6217 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006218 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006219 */
6220static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006221acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006222 struct acl_expr *expr, struct acl_test *test)
6223{
6224 struct http_txn *txn = l7;
6225 struct hdr_idx *idx = &txn->hdr_idx;
6226 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006227
Willy Tarreaub6866442008-07-14 23:54:42 +02006228 if (!txn)
6229 return 0;
6230
Willy Tarreau33a7e692007-06-10 19:45:56 +02006231 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6232 /* search for header from the beginning */
6233 ctx->idx = 0;
6234
Willy Tarreau33a7e692007-06-10 19:45:56 +02006235 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6236 test->flags |= ACL_TEST_F_FETCH_MORE;
6237 test->flags |= ACL_TEST_F_VOL_HDR;
6238 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6239 return 1;
6240 }
6241
6242 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6243 test->flags |= ACL_TEST_F_VOL_HDR;
6244 return 0;
6245}
6246
Willy Tarreauc11416f2007-06-17 16:58:38 +02006247static int
6248acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6249 struct acl_expr *expr, struct acl_test *test)
6250{
6251 struct http_txn *txn = l7;
6252
Willy Tarreaub6866442008-07-14 23:54:42 +02006253 if (!txn)
6254 return 0;
6255
Willy Tarreau655dce92009-11-08 13:10:58 +01006256 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006257 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006258
Willy Tarreauc11416f2007-06-17 16:58:38 +02006259 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6260 /* ensure the indexes are not affected */
6261 return 0;
6262
6263 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6264}
6265
6266static int
6267acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6268 struct acl_expr *expr, struct acl_test *test)
6269{
6270 struct http_txn *txn = l7;
6271
Willy Tarreaub6866442008-07-14 23:54:42 +02006272 if (!txn)
6273 return 0;
6274
Willy Tarreau655dce92009-11-08 13:10:58 +01006275 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006276 return 0;
6277
6278 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6279}
6280
Willy Tarreau106f9792009-09-19 07:54:16 +02006281/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6282 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6283 */
6284static int
6285acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6286 struct acl_expr *expr, struct acl_test *test)
6287{
6288 struct http_txn *txn = l7;
6289 struct hdr_idx *idx = &txn->hdr_idx;
6290 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6291
6292 if (!txn)
6293 return 0;
6294
6295 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6296 /* search for header from the beginning */
6297 ctx->idx = 0;
6298
6299 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6300 test->flags |= ACL_TEST_F_FETCH_MORE;
6301 test->flags |= ACL_TEST_F_VOL_HDR;
6302 /* Same optimization as url_ip */
6303 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6304 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6305 test->ptr = (void *)&l4->srv_addr.sin_addr;
6306 test->i = AF_INET;
6307 return 1;
6308 }
6309
6310 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6311 test->flags |= ACL_TEST_F_VOL_HDR;
6312 return 0;
6313}
6314
6315static int
6316acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6317 struct acl_expr *expr, struct acl_test *test)
6318{
6319 struct http_txn *txn = l7;
6320
6321 if (!txn)
6322 return 0;
6323
Willy Tarreau655dce92009-11-08 13:10:58 +01006324 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006325 return 0;
6326
6327 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6328 /* ensure the indexes are not affected */
6329 return 0;
6330
6331 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6332}
6333
6334static int
6335acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6336 struct acl_expr *expr, struct acl_test *test)
6337{
6338 struct http_txn *txn = l7;
6339
6340 if (!txn)
6341 return 0;
6342
Willy Tarreau655dce92009-11-08 13:10:58 +01006343 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006344 return 0;
6345
6346 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6347}
6348
Willy Tarreau737b0c12007-06-10 21:28:46 +02006349/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6350 * the first '/' after the possible hostname, and ends before the possible '?'.
6351 */
6352static int
6353acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6354 struct acl_expr *expr, struct acl_test *test)
6355{
6356 struct http_txn *txn = l7;
6357 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006358
Willy Tarreaub6866442008-07-14 23:54:42 +02006359 if (!txn)
6360 return 0;
6361
Willy Tarreau655dce92009-11-08 13:10:58 +01006362 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006363 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006364
Willy Tarreauc11416f2007-06-17 16:58:38 +02006365 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6366 /* ensure the indexes are not affected */
6367 return 0;
6368
Willy Tarreau21d2af32008-02-14 20:25:24 +01006369 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
6370 ptr = http_get_path(txn);
6371 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006372 return 0;
6373
6374 /* OK, we got the '/' ! */
6375 test->ptr = ptr;
6376
6377 while (ptr < end && *ptr != '?')
6378 ptr++;
6379
6380 test->len = ptr - test->ptr;
6381
6382 /* we do not need to set READ_ONLY because the data is in a buffer */
6383 test->flags = ACL_TEST_F_VOL_1ST;
6384 return 1;
6385}
6386
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006387static int
6388acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6389 struct acl_expr *expr, struct acl_test *test)
6390{
6391 struct buffer *req = s->req;
6392 struct http_txn *txn = &s->txn;
6393 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006394
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006395 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6396 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6397 */
6398
6399 if (!s || !req)
6400 return 0;
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 /* Already decoded as OK */
6404 test->flags |= ACL_TEST_F_SET_RES_PASS;
6405 return 1;
6406 }
6407
6408 /* Try to decode HTTP request */
6409 if (likely(req->lr < req->r))
6410 http_msg_analyzer(req, msg, &txn->hdr_idx);
6411
Willy Tarreau655dce92009-11-08 13:10:58 +01006412 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006413 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6414 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6415 return 1;
6416 }
6417 /* wait for final state */
6418 test->flags |= ACL_TEST_F_MAY_CHANGE;
6419 return 0;
6420 }
6421
6422 /* OK we got a valid HTTP request. We have some minor preparation to
6423 * perform so that further checks can rely on HTTP tests.
6424 */
6425 msg->sol = req->data + msg->som;
6426 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
6427 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6428 s->flags |= SN_REDIRECTABLE;
6429
6430 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
6431 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6432 return 1;
6433 }
6434
6435 test->flags |= ACL_TEST_F_SET_RES_PASS;
6436 return 1;
6437}
6438
Willy Tarreau8797c062007-05-07 00:55:35 +02006439
6440/************************************************************************/
6441/* All supported keywords must be declared here. */
6442/************************************************************************/
6443
6444/* Note: must not be declared <const> as its list will be overwritten */
6445static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006446 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
6447
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006448 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6449 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6450 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6451 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006452
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006453 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6454 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6455 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6456 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6457 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6458 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6459 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6460 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
6461 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02006462
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006463 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
6464 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6465 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6466 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6467 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6468 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6469 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6470 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6471 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
6472 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006473 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02006474
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006475 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6476 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
6477 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
6478 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
6479 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
6480 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
6481 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
6482 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
6483 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006484 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006485
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006486 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6487 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6488 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6489 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6490 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6491 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6492 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006493
Willy Tarreauf3d25982007-05-08 22:45:09 +02006494 { NULL, NULL, NULL, NULL },
6495
6496#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02006497 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
6498 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
6499 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
6500 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
6501 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
6502 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
6503 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
6504
Willy Tarreau8797c062007-05-07 00:55:35 +02006505 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
6506 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
6507 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
6508 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
6509 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
6510 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
6511 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
6512 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
6513
6514 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
6515 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
6516 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
6517 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
6518 { NULL, NULL, NULL, NULL },
6519#endif
6520}};
6521
6522
6523__attribute__((constructor))
6524static void __http_protocol_init(void)
6525{
6526 acl_register_keywords(&acl_kws);
6527}
6528
6529
Willy Tarreau58f10d72006-12-04 02:26:12 +01006530/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006531 * Local variables:
6532 * c-indent-level: 8
6533 * c-basic-offset: 8
6534 * End:
6535 */