blob: 7fd0b445d90bdf6c82a7511c16efc6d97c914172 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004 * Copyright 2000-2010 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 Tarreaua9679ac2010-01-03 17:32:57 +010082/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020083const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
Willy Tarreau0f772532006-12-23 20:51:41 +010089const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
95/* same as 302 except that the browser MUST retry with the GET method */
96const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010097 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010098 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010099 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
103const char *HTTP_401_fmt =
104 "HTTP/1.0 401 Unauthorized\r\n"
105 "Cache-Control: no-cache\r\n"
106 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200107 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
109 "\r\n"
110 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
111
Willy Tarreau0f772532006-12-23 20:51:41 +0100112
113const int http_err_codes[HTTP_ERR_SIZE] = {
114 [HTTP_ERR_400] = 400,
115 [HTTP_ERR_403] = 403,
116 [HTTP_ERR_408] = 408,
117 [HTTP_ERR_500] = 500,
118 [HTTP_ERR_502] = 502,
119 [HTTP_ERR_503] = 503,
120 [HTTP_ERR_504] = 504,
121};
122
Willy Tarreau80587432006-12-24 17:47:20 +0100123static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100124 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100125 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100126 "Cache-Control: no-cache\r\n"
127 "Connection: close\r\n"
128 "Content-Type: text/html\r\n"
129 "\r\n"
130 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
131
132 [HTTP_ERR_403] =
133 "HTTP/1.0 403 Forbidden\r\n"
134 "Cache-Control: no-cache\r\n"
135 "Connection: close\r\n"
136 "Content-Type: text/html\r\n"
137 "\r\n"
138 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
139
140 [HTTP_ERR_408] =
141 "HTTP/1.0 408 Request Time-out\r\n"
142 "Cache-Control: no-cache\r\n"
143 "Connection: close\r\n"
144 "Content-Type: text/html\r\n"
145 "\r\n"
146 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
147
148 [HTTP_ERR_500] =
149 "HTTP/1.0 500 Server Error\r\n"
150 "Cache-Control: no-cache\r\n"
151 "Connection: close\r\n"
152 "Content-Type: text/html\r\n"
153 "\r\n"
154 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
155
156 [HTTP_ERR_502] =
157 "HTTP/1.0 502 Bad Gateway\r\n"
158 "Cache-Control: no-cache\r\n"
159 "Connection: close\r\n"
160 "Content-Type: text/html\r\n"
161 "\r\n"
162 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
163
164 [HTTP_ERR_503] =
165 "HTTP/1.0 503 Service Unavailable\r\n"
166 "Cache-Control: no-cache\r\n"
167 "Connection: close\r\n"
168 "Content-Type: text/html\r\n"
169 "\r\n"
170 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
171
172 [HTTP_ERR_504] =
173 "HTTP/1.0 504 Gateway Time-out\r\n"
174 "Cache-Control: no-cache\r\n"
175 "Connection: close\r\n"
176 "Content-Type: text/html\r\n"
177 "\r\n"
178 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
179
180};
181
Willy Tarreau80587432006-12-24 17:47:20 +0100182/* We must put the messages here since GCC cannot initialize consts depending
183 * on strlen().
184 */
185struct chunk http_err_chunks[HTTP_ERR_SIZE];
186
Willy Tarreau42250582007-04-01 01:30:43 +0200187#define FD_SETS_ARE_BITFIELDS
188#ifdef FD_SETS_ARE_BITFIELDS
189/*
190 * This map is used with all the FD_* macros to check whether a particular bit
191 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
192 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
193 * byte should be encoded. Be careful to always pass bytes from 0 to 255
194 * exclusively to the macros.
195 */
196fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
197fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
198
199#else
200#error "Check if your OS uses bitfields for fd_sets"
201#endif
202
Willy Tarreau80587432006-12-24 17:47:20 +0100203void init_proto_http()
204{
Willy Tarreau42250582007-04-01 01:30:43 +0200205 int i;
206 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100207 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200208
Willy Tarreau80587432006-12-24 17:47:20 +0100209 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
210 if (!http_err_msgs[msg]) {
211 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
212 abort();
213 }
214
215 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
216 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
217 }
Willy Tarreau42250582007-04-01 01:30:43 +0200218
219 /* initialize the log header encoding map : '{|}"#' should be encoded with
220 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
221 * URL encoding only requires '"', '#' to be encoded as well as non-
222 * printable characters above.
223 */
224 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
225 memset(url_encode_map, 0, sizeof(url_encode_map));
226 for (i = 0; i < 32; i++) {
227 FD_SET(i, hdr_encode_map);
228 FD_SET(i, url_encode_map);
229 }
230 for (i = 127; i < 256; i++) {
231 FD_SET(i, hdr_encode_map);
232 FD_SET(i, url_encode_map);
233 }
234
235 tmp = "\"#{|}";
236 while (*tmp) {
237 FD_SET(*tmp, hdr_encode_map);
238 tmp++;
239 }
240
241 tmp = "\"#";
242 while (*tmp) {
243 FD_SET(*tmp, url_encode_map);
244 tmp++;
245 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200246
247 /* memory allocations */
248 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200249 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100250}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251
Willy Tarreau53b6c742006-12-17 13:37:46 +0100252/*
253 * We have 26 list of methods (1 per first letter), each of which can have
254 * up to 3 entries (2 valid, 1 null).
255 */
256struct http_method_desc {
257 http_meth_t meth;
258 int len;
259 const char text[8];
260};
261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100262const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100263 ['C' - 'A'] = {
264 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
265 },
266 ['D' - 'A'] = {
267 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
268 },
269 ['G' - 'A'] = {
270 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
271 },
272 ['H' - 'A'] = {
273 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
274 },
275 ['P' - 'A'] = {
276 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
277 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
278 },
279 ['T' - 'A'] = {
280 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
281 },
282 /* rest is empty like this :
283 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
284 */
285};
286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100287/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200288 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100289 * RFC2616 for those chars.
290 */
291
292const char http_is_spht[256] = {
293 [' '] = 1, ['\t'] = 1,
294};
295
296const char http_is_crlf[256] = {
297 ['\r'] = 1, ['\n'] = 1,
298};
299
300const char http_is_lws[256] = {
301 [' '] = 1, ['\t'] = 1,
302 ['\r'] = 1, ['\n'] = 1,
303};
304
305const char http_is_sep[256] = {
306 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
307 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
308 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
309 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
310 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
311};
312
313const char http_is_ctl[256] = {
314 [0 ... 31] = 1,
315 [127] = 1,
316};
317
318/*
319 * A token is any ASCII char that is neither a separator nor a CTL char.
320 * Do not overwrite values in assignment since gcc-2.95 will not handle
321 * them correctly. Instead, define every non-CTL char's status.
322 */
323const char http_is_token[256] = {
324 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
325 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
326 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
327 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
328 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
329 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
330 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
331 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
332 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
333 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
334 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
335 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
336 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
337 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
338 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
339 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
340 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
341 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
342 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
343 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
344 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
345 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
346 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
347 ['|'] = 1, ['}'] = 0, ['~'] = 1,
348};
349
350
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100351/*
352 * An http ver_token is any ASCII which can be found in an HTTP version,
353 * which includes 'H', 'T', 'P', '/', '.' and any digit.
354 */
355const char http_is_ver_token[256] = {
356 ['.'] = 1, ['/'] = 1,
357 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
358 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
359 ['H'] = 1, ['P'] = 1, ['T'] = 1,
360};
361
362
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100363/*
364 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
365 * CRLF. Text length is measured first, so it cannot be NULL.
366 * The header is also automatically added to the index <hdr_idx>, and the end
367 * of headers is automatically adjusted. The number of bytes added is returned
368 * on success, otherwise <0 is returned indicating an error.
369 */
370int http_header_add_tail(struct buffer *b, struct http_msg *msg,
371 struct hdr_idx *hdr_idx, const char *text)
372{
373 int bytes, len;
374
375 len = strlen(text);
376 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
377 if (!bytes)
378 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100379 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100380 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
381}
382
383/*
384 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
385 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
386 * the buffer is only opened and the space reserved, but nothing is copied.
387 * The header is also automatically added to the index <hdr_idx>, and the end
388 * of headers is automatically adjusted. The number of bytes added is returned
389 * on success, otherwise <0 is returned indicating an error.
390 */
391int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
392 struct hdr_idx *hdr_idx, const char *text, int len)
393{
394 int bytes;
395
396 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
397 if (!bytes)
398 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100399 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100400 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
401}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200402
403/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100404 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
405 * If so, returns the position of the first non-space character relative to
406 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
407 * to return a pointer to the place after the first space. Returns 0 if the
408 * header name does not match. Checks are case-insensitive.
409 */
410int http_header_match2(const char *hdr, const char *end,
411 const char *name, int len)
412{
413 const char *val;
414
415 if (hdr + len >= end)
416 return 0;
417 if (hdr[len] != ':')
418 return 0;
419 if (strncasecmp(hdr, name, len) != 0)
420 return 0;
421 val = hdr + len + 1;
422 while (val < end && HTTP_IS_SPHT(*val))
423 val++;
424 if ((val >= end) && (len + 2 <= end - hdr))
425 return len + 2; /* we may replace starting from second space */
426 return val - hdr;
427}
428
Willy Tarreau33a7e692007-06-10 19:45:56 +0200429/* Find the end of the header value contained between <s> and <e>.
430 * See RFC2616, par 2.2 for more information. Note that it requires
431 * a valid header to return a valid result.
432 */
433const char *find_hdr_value_end(const char *s, const char *e)
434{
435 int quoted, qdpair;
436
437 quoted = qdpair = 0;
438 for (; s < e; s++) {
439 if (qdpair) qdpair = 0;
440 else if (quoted && *s == '\\') qdpair = 1;
441 else if (quoted && *s == '"') quoted = 0;
442 else if (*s == '"') quoted = 1;
443 else if (*s == ',') return s;
444 }
445 return s;
446}
447
448/* Find the first or next occurrence of header <name> in message buffer <sol>
449 * using headers index <idx>, and return it in the <ctx> structure. This
450 * structure holds everything necessary to use the header and find next
451 * occurrence. If its <idx> member is 0, the header is searched from the
452 * beginning. Otherwise, the next occurrence is returned. The function returns
453 * 1 when it finds a value, and 0 when there is no more.
454 */
455int http_find_header2(const char *name, int len,
456 const char *sol, struct hdr_idx *idx,
457 struct hdr_ctx *ctx)
458{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200459 const char *eol, *sov;
460 int cur_idx;
461
462 if (ctx->idx) {
463 /* We have previously returned a value, let's search
464 * another one on the same line.
465 */
466 cur_idx = ctx->idx;
467 sol = ctx->line;
468 sov = sol + ctx->val + ctx->vlen;
469 eol = sol + idx->v[cur_idx].len;
470
471 if (sov >= eol)
472 /* no more values in this header */
473 goto next_hdr;
474
475 /* values remaining for this header, skip the comma */
476 sov++;
477 while (sov < eol && http_is_lws[(unsigned char)*sov])
478 sov++;
479
480 goto return_hdr;
481 }
482
483 /* first request for this header */
484 sol += hdr_idx_first_pos(idx);
485 cur_idx = hdr_idx_first_idx(idx);
486
487 while (cur_idx) {
488 eol = sol + idx->v[cur_idx].len;
489
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200490 if (len == 0) {
491 /* No argument was passed, we want any header.
492 * To achieve this, we simply build a fake request. */
493 while (sol + len < eol && sol[len] != ':')
494 len++;
495 name = sol;
496 }
497
Willy Tarreau33a7e692007-06-10 19:45:56 +0200498 if ((len < eol - sol) &&
499 (sol[len] == ':') &&
500 (strncasecmp(sol, name, len) == 0)) {
501
502 sov = sol + len + 1;
503 while (sov < eol && http_is_lws[(unsigned char)*sov])
504 sov++;
505 return_hdr:
506 ctx->line = sol;
507 ctx->idx = cur_idx;
508 ctx->val = sov - sol;
509
510 eol = find_hdr_value_end(sov, eol);
511 ctx->vlen = eol - sov;
512 return 1;
513 }
514 next_hdr:
515 sol = eol + idx->v[cur_idx].cr + 1;
516 cur_idx = idx->v[cur_idx].next;
517 }
518 return 0;
519}
520
521int http_find_header(const char *name,
522 const char *sol, struct hdr_idx *idx,
523 struct hdr_ctx *ctx)
524{
525 return http_find_header2(name, strlen(name), sol, idx, ctx);
526}
527
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100528/* This function handles a server error at the stream interface level. The
529 * stream interface is assumed to be already in a closed state. An optional
530 * message is copied into the input buffer, and an HTTP status code stored.
531 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100532 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200533 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100534static void http_server_error(struct session *t, struct stream_interface *si,
535 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200536{
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100537 buffer_erase(si->ob);
538 buffer_erase(si->ib);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200539 buffer_auto_close(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100540 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100541 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100542 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543 }
544 if (!(t->flags & SN_ERR_MASK))
545 t->flags |= err;
546 if (!(t->flags & SN_FINST_MASK))
547 t->flags |= finst;
548}
549
Willy Tarreau80587432006-12-24 17:47:20 +0100550/* This function returns the appropriate error location for the given session
551 * and message.
552 */
553
554struct chunk *error_message(struct session *s, int msgnum)
555{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200556 if (s->be->errmsg[msgnum].str)
557 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100558 else if (s->fe->errmsg[msgnum].str)
559 return &s->fe->errmsg[msgnum];
560 else
561 return &http_err_chunks[msgnum];
562}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200563
Willy Tarreau53b6c742006-12-17 13:37:46 +0100564/*
565 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
566 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
567 */
568static http_meth_t find_http_meth(const char *str, const int len)
569{
570 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100571 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100572
573 m = ((unsigned)*str - 'A');
574
575 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100576 for (h = http_methods[m]; h->len > 0; h++) {
577 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100578 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100579 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100580 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100581 };
582 return HTTP_METH_OTHER;
583 }
584 return HTTP_METH_NONE;
585
586}
587
Willy Tarreau21d2af32008-02-14 20:25:24 +0100588/* Parse the URI from the given transaction (which is assumed to be in request
589 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
590 * It is returned otherwise.
591 */
592static char *
593http_get_path(struct http_txn *txn)
594{
595 char *ptr, *end;
596
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100597 ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100598 end = ptr + txn->req.sl.rq.u_l;
599
600 if (ptr >= end)
601 return NULL;
602
603 /* RFC2616, par. 5.1.2 :
604 * Request-URI = "*" | absuri | abspath | authority
605 */
606
607 if (*ptr == '*')
608 return NULL;
609
610 if (isalpha((unsigned char)*ptr)) {
611 /* this is a scheme as described by RFC3986, par. 3.1 */
612 ptr++;
613 while (ptr < end &&
614 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
615 ptr++;
616 /* skip '://' */
617 if (ptr == end || *ptr++ != ':')
618 return NULL;
619 if (ptr == end || *ptr++ != '/')
620 return NULL;
621 if (ptr == end || *ptr++ != '/')
622 return NULL;
623 }
624 /* skip [user[:passwd]@]host[:[port]] */
625
626 while (ptr < end && *ptr != '/')
627 ptr++;
628
629 if (ptr == end)
630 return NULL;
631
632 /* OK, we got the '/' ! */
633 return ptr;
634}
635
Willy Tarreauefb453c2008-10-26 20:49:47 +0100636/* Returns a 302 for a redirectable request. This may only be called just after
637 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
638 * left unchanged and will follow normal proxy processing.
639 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100640void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100641{
642 struct http_txn *txn;
643 struct chunk rdr;
644 char *path;
645 int len;
646
647 /* 1: create the response header */
648 rdr.len = strlen(HTTP_302);
649 rdr.str = trash;
650 memcpy(rdr.str, HTTP_302, rdr.len);
651
652 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200653 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100654 return;
655
656 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
657 rdr.len += s->srv->rdr_len;
658
659 /* 3: add the request URI */
660 txn = &s->txn;
661 path = http_get_path(txn);
662 if (!path)
663 return;
664
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100665 len = txn->req.sl.rq.u_l + (txn->req.sol-txn->req.som+txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200666 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100667 return;
668
669 memcpy(rdr.str + rdr.len, path, len);
670 rdr.len += len;
Willy Tarreaua9679ac2010-01-03 17:32:57 +0100671 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
672 rdr.len += 23;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100673
674 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100675 si->shutr(si);
676 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100677 si->err_type = SI_ET_NONE;
678 si->err_loc = NULL;
679 si->state = SI_ST_CLO;
680
681 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100682 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100683
684 /* FIXME: we should increase a counter of redirects per server and per backend. */
685 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100686 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100687}
688
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100689/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100690 * that the server side is closed. Note that err_type is actually a
691 * bitmask, where almost only aborts may be cumulated with other
692 * values. We consider that aborted operations are more important
693 * than timeouts or errors due to the fact that nobody else in the
694 * logs might explain incomplete retries. All others should avoid
695 * being cumulated. It should normally not be possible to have multiple
696 * aborts at once, but just in case, the first one in sequence is reported.
697 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100698void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100699{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100700 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100701
702 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100703 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
704 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100705 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100706 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
707 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100708 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100709 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
710 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100711 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100712 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
713 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100714 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100715 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
716 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100717 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100718 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
719 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100720 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100721 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
722 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100723}
724
Willy Tarreau42250582007-04-01 01:30:43 +0200725extern const char sess_term_cond[8];
726extern const char sess_fin_state[8];
727extern const char *monthname[12];
728const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
729const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
730 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
731 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200732struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200733struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100734
Emeric Brun3a058f32009-06-30 18:26:00 +0200735void http_sess_clflog(struct session *s)
736{
737 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
738 struct proxy *fe = s->fe;
739 struct proxy *be = s->be;
740 struct proxy *prx_log;
741 struct http_txn *txn = &s->txn;
742 int tolog, level, err;
743 char *uri, *h;
744 char *svid;
745 struct tm tm;
746 static char tmpline[MAX_SYSLOG_LEN];
747 int hdr;
748 size_t w;
749 int t_request;
750
751 prx_log = fe;
752 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
753 (s->conn_retries != be->conn_retries) ||
754 txn->status >= 500;
755
756 if (s->cli_addr.ss_family == AF_INET)
757 inet_ntop(AF_INET,
758 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
759 pn, sizeof(pn));
760 else
761 inet_ntop(AF_INET6,
762 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
763 pn, sizeof(pn));
764
765 get_gmtime(s->logs.accept_date.tv_sec, &tm);
766
767 /* FIXME: let's limit ourselves to frontend logging for now. */
768 tolog = fe->to_log;
769
770 h = tmpline;
771
772 w = snprintf(h, sizeof(tmpline),
773 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
774 pn,
775 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
776 tm.tm_hour, tm.tm_min, tm.tm_sec);
777 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
778 goto trunc;
779 h += w;
780
781 if (h >= tmpline + sizeof(tmpline) - 4)
782 goto trunc;
783
784 *(h++) = ' ';
785 *(h++) = '\"';
786 uri = txn->uri ? txn->uri : "<BADREQ>";
787 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
788 '#', url_encode_map, uri);
789 *(h++) = '\"';
790
791 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
792 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
793 goto trunc;
794 h += w;
795
796 if (h >= tmpline + sizeof(tmpline) - 9)
797 goto trunc;
798 memcpy(h, " \"-\" \"-\"", 8);
799 h += 8;
800
801 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
802 " %d %03d",
803 (s->cli_addr.ss_family == AF_INET) ?
804 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
805 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
806 (int)s->logs.accept_date.tv_usec/1000);
807 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
808 goto trunc;
809 h += w;
810
811 w = strlen(fe->id);
812 if (h >= tmpline + sizeof(tmpline) - 4 - w)
813 goto trunc;
814 *(h++) = ' ';
815 *(h++) = '\"';
816 memcpy(h, fe->id, w);
817 h += w;
818 *(h++) = '\"';
819
820 w = strlen(be->id);
821 if (h >= tmpline + sizeof(tmpline) - 4 - w)
822 goto trunc;
823 *(h++) = ' ';
824 *(h++) = '\"';
825 memcpy(h, be->id, w);
826 h += w;
827 *(h++) = '\"';
828
829 svid = (tolog & LW_SVID) ?
830 (s->data_source != DATA_SRC_STATS) ?
831 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
832
833 w = strlen(svid);
834 if (h >= tmpline + sizeof(tmpline) - 4 - w)
835 goto trunc;
836 *(h++) = ' ';
837 *(h++) = '\"';
838 memcpy(h, svid, w);
839 h += w;
840 *(h++) = '\"';
841
842 t_request = -1;
843 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
844 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
845 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
846 " %d %ld %ld %ld %ld",
847 t_request,
848 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
849 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
850 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
851 s->logs.t_close);
852 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
853 goto trunc;
854 h += w;
855
856 if (h >= tmpline + sizeof(tmpline) - 8)
857 goto trunc;
858 *(h++) = ' ';
859 *(h++) = '\"';
860 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
861 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
862 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
863 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
864 *(h++) = '\"';
865
866 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
867 " %d %d %d %d %d %ld %ld",
868 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
869 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
870 s->logs.srv_queue_size, s->logs.prx_queue_size);
871
872 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
873 goto trunc;
874 h += w;
875
876 if (txn->cli_cookie) {
877 w = strlen(txn->cli_cookie);
878 if (h >= tmpline + sizeof(tmpline) - 4 - w)
879 goto trunc;
880 *(h++) = ' ';
881 *(h++) = '\"';
882 memcpy(h, txn->cli_cookie, w);
883 h += w;
884 *(h++) = '\"';
885 } else {
886 if (h >= tmpline + sizeof(tmpline) - 5)
887 goto trunc;
888 memcpy(h, " \"-\"", 4);
889 h += 4;
890 }
891
892 if (txn->srv_cookie) {
893 w = strlen(txn->srv_cookie);
894 if (h >= tmpline + sizeof(tmpline) - 4 - w)
895 goto trunc;
896 *(h++) = ' ';
897 *(h++) = '\"';
898 memcpy(h, txn->srv_cookie, w);
899 h += w;
900 *(h++) = '\"';
901 } else {
902 if (h >= tmpline + sizeof(tmpline) - 5)
903 goto trunc;
904 memcpy(h, " \"-\"", 4);
905 h += 4;
906 }
907
908 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
909 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
910 if (h >= sizeof (tmpline) + tmpline - 4)
911 goto trunc;
912 *(h++) = ' ';
913 *(h++) = '\"';
914 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
915 '#', hdr_encode_map, txn->req.cap[hdr]);
916 *(h++) = '\"';
917 }
918 }
919
920 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
921 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
922 if (h >= sizeof (tmpline) + tmpline - 4)
923 goto trunc;
924 *(h++) = ' ';
925 *(h++) = '\"';
926 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
927 '#', hdr_encode_map, txn->rsp.cap[hdr]);
928 *(h++) = '\"';
929 }
930 }
931
932trunc:
933 *h = '\0';
934
935 level = LOG_INFO;
936 if (err && (fe->options2 & PR_O2_LOGERRORS))
937 level = LOG_ERR;
938
939 send_log(prx_log, level, "%s\n", tmpline);
940
941 s->logs.logwait = 0;
942}
943
Willy Tarreau42250582007-04-01 01:30:43 +0200944/*
945 * send a log for the session when we have enough info about it.
946 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100947 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100948void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200949{
950 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
951 struct proxy *fe = s->fe;
952 struct proxy *be = s->be;
953 struct proxy *prx_log;
954 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200955 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200956 char *uri, *h;
957 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200958 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200959 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200960 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200961 int hdr;
962
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200963 /* if we don't want to log normal traffic, return now */
964 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
965 (s->conn_retries != be->conn_retries) ||
966 txn->status >= 500;
967 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
968 return;
969
Willy Tarreau42250582007-04-01 01:30:43 +0200970 if (fe->logfac1 < 0 && fe->logfac2 < 0)
971 return;
972 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100973
Emeric Brun3a058f32009-06-30 18:26:00 +0200974 if (prx_log->options2 & PR_O2_CLFLOG)
975 return http_sess_clflog(s);
976
Willy Tarreau42250582007-04-01 01:30:43 +0200977 if (s->cli_addr.ss_family == AF_INET)
978 inet_ntop(AF_INET,
979 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
980 pn, sizeof(pn));
981 else
982 inet_ntop(AF_INET6,
983 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
984 pn, sizeof(pn));
985
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200986 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200987
988 /* FIXME: let's limit ourselves to frontend logging for now. */
989 tolog = fe->to_log;
990
991 h = tmpline;
992 if (fe->to_log & LW_REQHDR &&
993 txn->req.cap &&
994 (h < tmpline + sizeof(tmpline) - 10)) {
995 *(h++) = ' ';
996 *(h++) = '{';
997 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
998 if (hdr)
999 *(h++) = '|';
1000 if (txn->req.cap[hdr] != NULL)
1001 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1002 '#', hdr_encode_map, txn->req.cap[hdr]);
1003 }
1004 *(h++) = '}';
1005 }
1006
1007 if (fe->to_log & LW_RSPHDR &&
1008 txn->rsp.cap &&
1009 (h < tmpline + sizeof(tmpline) - 7)) {
1010 *(h++) = ' ';
1011 *(h++) = '{';
1012 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1013 if (hdr)
1014 *(h++) = '|';
1015 if (txn->rsp.cap[hdr] != NULL)
1016 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1017 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1018 }
1019 *(h++) = '}';
1020 }
1021
1022 if (h < tmpline + sizeof(tmpline) - 4) {
1023 *(h++) = ' ';
1024 *(h++) = '"';
1025 uri = txn->uri ? txn->uri : "<BADREQ>";
1026 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1027 '#', url_encode_map, uri);
1028 *(h++) = '"';
1029 }
1030 *h = '\0';
1031
1032 svid = (tolog & LW_SVID) ?
1033 (s->data_source != DATA_SRC_STATS) ?
1034 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1035
Willy Tarreau70089872008-06-13 21:12:51 +02001036 t_request = -1;
1037 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1038 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1039
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001040 level = LOG_INFO;
1041 if (err && (fe->options2 & PR_O2_LOGERRORS))
1042 level = LOG_ERR;
1043
1044 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001045 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001046 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1047 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001048 pn,
1049 (s->cli_addr.ss_family == AF_INET) ?
1050 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1051 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001052 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001053 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001054 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001055 t_request,
1056 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001057 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1058 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1059 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1060 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001061 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001062 txn->cli_cookie ? txn->cli_cookie : "-",
1063 txn->srv_cookie ? txn->srv_cookie : "-",
1064 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1065 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1066 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1067 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1068 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001069 (s->flags & SN_REDISP)?"+":"",
1070 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001071 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1072
1073 s->logs.logwait = 0;
1074}
1075
Willy Tarreau117f59e2007-03-04 18:17:17 +01001076
1077/*
1078 * Capture headers from message starting at <som> according to header list
1079 * <cap_hdr>, and fill the <idx> structure appropriately.
1080 */
1081void capture_headers(char *som, struct hdr_idx *idx,
1082 char **cap, struct cap_hdr *cap_hdr)
1083{
1084 char *eol, *sol, *col, *sov;
1085 int cur_idx;
1086 struct cap_hdr *h;
1087 int len;
1088
1089 sol = som + hdr_idx_first_pos(idx);
1090 cur_idx = hdr_idx_first_idx(idx);
1091
1092 while (cur_idx) {
1093 eol = sol + idx->v[cur_idx].len;
1094
1095 col = sol;
1096 while (col < eol && *col != ':')
1097 col++;
1098
1099 sov = col + 1;
1100 while (sov < eol && http_is_lws[(unsigned char)*sov])
1101 sov++;
1102
1103 for (h = cap_hdr; h; h = h->next) {
1104 if ((h->namelen == col - sol) &&
1105 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1106 if (cap[h->index] == NULL)
1107 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001108 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001109
1110 if (cap[h->index] == NULL) {
1111 Alert("HTTP capture : out of memory.\n");
1112 continue;
1113 }
1114
1115 len = eol - sov;
1116 if (len > h->len)
1117 len = h->len;
1118
1119 memcpy(cap[h->index], sov, len);
1120 cap[h->index][len]=0;
1121 }
1122 }
1123 sol = eol + idx->v[cur_idx].cr + 1;
1124 cur_idx = idx->v[cur_idx].next;
1125 }
1126}
1127
1128
Willy Tarreau42250582007-04-01 01:30:43 +02001129/* either we find an LF at <ptr> or we jump to <bad>.
1130 */
1131#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1132
1133/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1134 * otherwise to <http_msg_ood> with <state> set to <st>.
1135 */
1136#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1137 ptr++; \
1138 if (likely(ptr < end)) \
1139 goto good; \
1140 else { \
1141 state = (st); \
1142 goto http_msg_ood; \
1143 } \
1144 } while (0)
1145
1146
Willy Tarreaubaaee002006-06-26 02:48:02 +02001147/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001148 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001149 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1150 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1151 * will give undefined results.
1152 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1153 * and that msg->sol points to the beginning of the response.
1154 * If a complete line is found (which implies that at least one CR or LF is
1155 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1156 * returned indicating an incomplete line (which does not mean that parts have
1157 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1158 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1159 * upon next call.
1160 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001161 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001162 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1163 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001164 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001165 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001166const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1167 unsigned int state, const char *ptr, const char *end,
1168 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001169{
Willy Tarreau8973c702007-01-21 23:58:29 +01001170 switch (state) {
1171 http_msg_rpver:
1172 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001173 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001174 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1175
1176 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001177 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001178 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1179 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001180 state = HTTP_MSG_ERROR;
1181 break;
1182
Willy Tarreau8973c702007-01-21 23:58:29 +01001183 http_msg_rpver_sp:
1184 case HTTP_MSG_RPVER_SP:
1185 if (likely(!HTTP_IS_LWS(*ptr))) {
1186 msg->sl.st.c = ptr - msg_buf;
1187 goto http_msg_rpcode;
1188 }
1189 if (likely(HTTP_IS_SPHT(*ptr)))
1190 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1191 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001192 state = HTTP_MSG_ERROR;
1193 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001194
1195 http_msg_rpcode:
1196 case HTTP_MSG_RPCODE:
1197 if (likely(!HTTP_IS_LWS(*ptr)))
1198 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1199
1200 if (likely(HTTP_IS_SPHT(*ptr))) {
1201 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1202 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1203 }
1204
1205 /* so it's a CR/LF, so there is no reason phrase */
1206 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1207 http_msg_rsp_reason:
1208 /* FIXME: should we support HTTP responses without any reason phrase ? */
1209 msg->sl.st.r = ptr - msg_buf;
1210 msg->sl.st.r_l = 0;
1211 goto http_msg_rpline_eol;
1212
1213 http_msg_rpcode_sp:
1214 case HTTP_MSG_RPCODE_SP:
1215 if (likely(!HTTP_IS_LWS(*ptr))) {
1216 msg->sl.st.r = ptr - msg_buf;
1217 goto http_msg_rpreason;
1218 }
1219 if (likely(HTTP_IS_SPHT(*ptr)))
1220 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1221 /* so it's a CR/LF, so there is no reason phrase */
1222 goto http_msg_rsp_reason;
1223
1224 http_msg_rpreason:
1225 case HTTP_MSG_RPREASON:
1226 if (likely(!HTTP_IS_CRLF(*ptr)))
1227 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1228 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1229 http_msg_rpline_eol:
1230 /* We have seen the end of line. Note that we do not
1231 * necessarily have the \n yet, but at least we know that we
1232 * have EITHER \r OR \n, otherwise the response would not be
1233 * complete. We can then record the response length and return
1234 * to the caller which will be able to register it.
1235 */
1236 msg->sl.st.l = ptr - msg->sol;
1237 return ptr;
1238
1239#ifdef DEBUG_FULL
1240 default:
1241 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1242 exit(1);
1243#endif
1244 }
1245
1246 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001247 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001248 if (ret_state)
1249 *ret_state = state;
1250 if (ret_ptr)
1251 *ret_ptr = (char *)ptr;
1252 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001253}
1254
1255
1256/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001257 * This function parses a request line between <ptr> and <end>, starting with
1258 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1259 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1260 * will give undefined results.
1261 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1262 * and that msg->sol points to the beginning of the request.
1263 * If a complete line is found (which implies that at least one CR or LF is
1264 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1265 * returned indicating an incomplete line (which does not mean that parts have
1266 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1267 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1268 * upon next call.
1269 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001270 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001271 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1272 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001273 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001274 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001275const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1276 unsigned int state, const char *ptr, const char *end,
1277 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001278{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001279 switch (state) {
1280 http_msg_rqmeth:
1281 case HTTP_MSG_RQMETH:
1282 if (likely(HTTP_IS_TOKEN(*ptr)))
1283 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001284
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001285 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001286 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1288 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001289
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 if (likely(HTTP_IS_CRLF(*ptr))) {
1291 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001292 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001293 http_msg_req09_uri:
1294 msg->sl.rq.u = ptr - msg_buf;
1295 http_msg_req09_uri_e:
1296 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1297 http_msg_req09_ver:
1298 msg->sl.rq.v = ptr - msg_buf;
1299 msg->sl.rq.v_l = 0;
1300 goto http_msg_rqline_eol;
1301 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001302 state = HTTP_MSG_ERROR;
1303 break;
1304
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001305 http_msg_rqmeth_sp:
1306 case HTTP_MSG_RQMETH_SP:
1307 if (likely(!HTTP_IS_LWS(*ptr))) {
1308 msg->sl.rq.u = ptr - msg_buf;
1309 goto http_msg_rquri;
1310 }
1311 if (likely(HTTP_IS_SPHT(*ptr)))
1312 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1313 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1314 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001315
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 http_msg_rquri:
1317 case HTTP_MSG_RQURI:
1318 if (likely(!HTTP_IS_LWS(*ptr)))
1319 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001320
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 if (likely(HTTP_IS_SPHT(*ptr))) {
1322 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1323 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1324 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1327 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001328
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001329 http_msg_rquri_sp:
1330 case HTTP_MSG_RQURI_SP:
1331 if (likely(!HTTP_IS_LWS(*ptr))) {
1332 msg->sl.rq.v = ptr - msg_buf;
1333 goto http_msg_rqver;
1334 }
1335 if (likely(HTTP_IS_SPHT(*ptr)))
1336 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1337 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1338 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001339
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 http_msg_rqver:
1341 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001342 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001343 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001344
1345 if (likely(HTTP_IS_CRLF(*ptr))) {
1346 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1347 http_msg_rqline_eol:
1348 /* We have seen the end of line. Note that we do not
1349 * necessarily have the \n yet, but at least we know that we
1350 * have EITHER \r OR \n, otherwise the request would not be
1351 * complete. We can then record the request length and return
1352 * to the caller which will be able to register it.
1353 */
1354 msg->sl.rq.l = ptr - msg->sol;
1355 return ptr;
1356 }
1357
1358 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001359 state = HTTP_MSG_ERROR;
1360 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001361
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001362#ifdef DEBUG_FULL
1363 default:
1364 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1365 exit(1);
1366#endif
1367 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001368
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001370 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 if (ret_state)
1372 *ret_state = state;
1373 if (ret_ptr)
1374 *ret_ptr = (char *)ptr;
1375 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001377
1378
Willy Tarreau8973c702007-01-21 23:58:29 +01001379/*
1380 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001381 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001382 * when data are missing and recalled at the exact same location with no
1383 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001384 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001385 * fields. Note that msg->som and msg->sol will be initialized after completing
1386 * the first state, so that none of the msg pointers has to be initialized
1387 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001388 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1390{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001391 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001393
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001394 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 ptr = buf->lr;
1396 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 if (unlikely(ptr >= end))
1399 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001400
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001402 /*
1403 * First, states that are specific to the response only.
1404 * We check them first so that request and headers are
1405 * closer to each other (accessed more often).
1406 */
1407 http_msg_rpbefore:
1408 case HTTP_MSG_RPBEFORE:
1409 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001410 /* we have a start of message, but we have to check
1411 * first if we need to remove some CRLF. We can only
1412 * do this when send_max=0.
1413 */
1414 char *beg = buf->w + buf->send_max;
1415 if (beg >= buf->data + buf->size)
1416 beg -= buf->size;
1417 if (unlikely(ptr != beg)) {
1418 if (buf->send_max)
1419 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001420 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001421 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001422 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001423 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001424 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001425 hdr_idx_init(idx);
1426 state = HTTP_MSG_RPVER;
1427 goto http_msg_rpver;
1428 }
1429
1430 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1431 goto http_msg_invalid;
1432
1433 if (unlikely(*ptr == '\n'))
1434 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1435 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1436 /* stop here */
1437
1438 http_msg_rpbefore_cr:
1439 case HTTP_MSG_RPBEFORE_CR:
1440 EXPECT_LF_HERE(ptr, http_msg_invalid);
1441 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1442 /* stop here */
1443
1444 http_msg_rpver:
1445 case HTTP_MSG_RPVER:
1446 case HTTP_MSG_RPVER_SP:
1447 case HTTP_MSG_RPCODE:
1448 case HTTP_MSG_RPCODE_SP:
1449 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001450 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001451 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001452 if (unlikely(!ptr))
1453 return;
1454
1455 /* we have a full response and we know that we have either a CR
1456 * or an LF at <ptr>.
1457 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001458 //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 +01001459 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1460
1461 msg->sol = ptr;
1462 if (likely(*ptr == '\r'))
1463 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1464 goto http_msg_rpline_end;
1465
1466 http_msg_rpline_end:
1467 case HTTP_MSG_RPLINE_END:
1468 /* msg->sol must point to the first of CR or LF. */
1469 EXPECT_LF_HERE(ptr, http_msg_invalid);
1470 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1471 /* stop here */
1472
1473 /*
1474 * Second, states that are specific to the request only
1475 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 http_msg_rqbefore:
1477 case HTTP_MSG_RQBEFORE:
1478 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001479 /* we have a start of message, but we have to check
1480 * first if we need to remove some CRLF. We can only
1481 * do this when send_max=0.
1482 */
1483 char *beg = buf->w + buf->send_max;
1484 if (beg >= buf->data + buf->size)
1485 beg -= buf->size;
1486 if (likely(ptr != beg)) {
1487 if (buf->send_max)
1488 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001489 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001490 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001492 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001493 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001494 /* we will need this when keep-alive will be supported
1495 hdr_idx_init(idx);
1496 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001497 state = HTTP_MSG_RQMETH;
1498 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001500
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1502 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001503
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 if (unlikely(*ptr == '\n'))
1505 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1506 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001507 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 http_msg_rqbefore_cr:
1510 case HTTP_MSG_RQBEFORE_CR:
1511 EXPECT_LF_HERE(ptr, http_msg_invalid);
1512 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001513 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 http_msg_rqmeth:
1516 case HTTP_MSG_RQMETH:
1517 case HTTP_MSG_RQMETH_SP:
1518 case HTTP_MSG_RQURI:
1519 case HTTP_MSG_RQURI_SP:
1520 case HTTP_MSG_RQVER:
1521 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001522 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 if (unlikely(!ptr))
1524 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 /* we have a full request and we know that we have either a CR
1527 * or an LF at <ptr>.
1528 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001529 //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 +01001530 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001531
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 msg->sol = ptr;
1533 if (likely(*ptr == '\r'))
1534 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 http_msg_rqline_end:
1538 case HTTP_MSG_RQLINE_END:
1539 /* check for HTTP/0.9 request : no version information available.
1540 * msg->sol must point to the first of CR or LF.
1541 */
1542 if (unlikely(msg->sl.rq.v_l == 0))
1543 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001544
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001545 EXPECT_LF_HERE(ptr, http_msg_invalid);
1546 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001547 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001548
Willy Tarreau8973c702007-01-21 23:58:29 +01001549 /*
1550 * Common states below
1551 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 http_msg_hdr_first:
1553 case HTTP_MSG_HDR_FIRST:
1554 msg->sol = ptr;
1555 if (likely(!HTTP_IS_CRLF(*ptr))) {
1556 goto http_msg_hdr_name;
1557 }
1558
1559 if (likely(*ptr == '\r'))
1560 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1561 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001562
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 http_msg_hdr_name:
1564 case HTTP_MSG_HDR_NAME:
1565 /* assumes msg->sol points to the first char */
1566 if (likely(HTTP_IS_TOKEN(*ptr)))
1567 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 if (likely(*ptr == ':')) {
1570 msg->col = ptr - buf->data;
1571 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1572 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001573
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001574 if (likely(msg->err_pos < -1) || *ptr == '\n')
1575 goto http_msg_invalid;
1576
1577 if (msg->err_pos == -1) /* capture error pointer */
1578 msg->err_pos = ptr - buf->data; /* >= 0 now */
1579
1580 /* and we still accept this non-token character */
1581 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001582
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 http_msg_hdr_l1_sp:
1584 case HTTP_MSG_HDR_L1_SP:
1585 /* assumes msg->sol points to the first char and msg->col to the colon */
1586 if (likely(HTTP_IS_SPHT(*ptr)))
1587 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001588
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 /* header value can be basically anything except CR/LF */
1590 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 if (likely(!HTTP_IS_CRLF(*ptr))) {
1593 goto http_msg_hdr_val;
1594 }
1595
1596 if (likely(*ptr == '\r'))
1597 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1598 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 http_msg_hdr_l1_lf:
1601 case HTTP_MSG_HDR_L1_LF:
1602 EXPECT_LF_HERE(ptr, http_msg_invalid);
1603 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001604
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 http_msg_hdr_l1_lws:
1606 case HTTP_MSG_HDR_L1_LWS:
1607 if (likely(HTTP_IS_SPHT(*ptr))) {
1608 /* replace HT,CR,LF with spaces */
1609 for (; buf->data+msg->sov < ptr; msg->sov++)
1610 buf->data[msg->sov] = ' ';
1611 goto http_msg_hdr_l1_sp;
1612 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001613 /* we had a header consisting only in spaces ! */
1614 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 goto http_msg_complete_header;
1616
1617 http_msg_hdr_val:
1618 case HTTP_MSG_HDR_VAL:
1619 /* assumes msg->sol points to the first char, msg->col to the
1620 * colon, and msg->sov points to the first character of the
1621 * value.
1622 */
1623 if (likely(!HTTP_IS_CRLF(*ptr)))
1624 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001625
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 msg->eol = ptr;
1627 /* Note: we could also copy eol into ->eoh so that we have the
1628 * real header end in case it ends with lots of LWS, but is this
1629 * really needed ?
1630 */
1631 if (likely(*ptr == '\r'))
1632 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1633 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001634
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 http_msg_hdr_l2_lf:
1636 case HTTP_MSG_HDR_L2_LF:
1637 EXPECT_LF_HERE(ptr, http_msg_invalid);
1638 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 http_msg_hdr_l2_lws:
1641 case HTTP_MSG_HDR_L2_LWS:
1642 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1643 /* LWS: replace HT,CR,LF with spaces */
1644 for (; msg->eol < ptr; msg->eol++)
1645 *msg->eol = ' ';
1646 goto http_msg_hdr_val;
1647 }
1648 http_msg_complete_header:
1649 /*
1650 * It was a new header, so the last one is finished.
1651 * Assumes msg->sol points to the first char, msg->col to the
1652 * colon, msg->sov points to the first character of the value
1653 * and msg->eol to the first CR or LF so we know how the line
1654 * ends. We insert last header into the index.
1655 */
1656 /*
1657 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1658 write(2, msg->sol, msg->eol-msg->sol);
1659 fprintf(stderr,"\n");
1660 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001661
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001662 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1663 idx, idx->tail) < 0))
1664 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 msg->sol = ptr;
1667 if (likely(!HTTP_IS_CRLF(*ptr))) {
1668 goto http_msg_hdr_name;
1669 }
1670
1671 if (likely(*ptr == '\r'))
1672 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1673 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001674
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 http_msg_last_lf:
1676 case HTTP_MSG_LAST_LF:
1677 /* Assumes msg->sol points to the first of either CR or LF */
1678 EXPECT_LF_HERE(ptr, http_msg_invalid);
1679 ptr++;
1680 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001681 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001682 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001683 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 return;
1685#ifdef DEBUG_FULL
1686 default:
1687 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1688 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001689#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 }
1691 http_msg_ood:
1692 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001693 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 buf->lr = ptr;
1695 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001696
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 http_msg_invalid:
1698 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001699 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001700 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001701 return;
1702}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001703
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001704/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1705 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1706 * nothing is done and 1 is returned.
1707 */
1708static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1709{
1710 int delta;
1711 char *cur_end;
1712
1713 if (msg->sl.rq.v_l != 0)
1714 return 1;
1715
1716 msg->sol = req->data + msg->som;
1717 cur_end = msg->sol + msg->sl.rq.l;
1718 delta = 0;
1719
1720 if (msg->sl.rq.u_l == 0) {
1721 /* if no URI was set, add "/" */
1722 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1723 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001724 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001725 }
1726 /* add HTTP version */
1727 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001728 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001729 cur_end += delta;
1730 cur_end = (char *)http_parse_reqline(msg, req->data,
1731 HTTP_MSG_RQMETH,
1732 msg->sol, cur_end + 1,
1733 NULL, NULL);
1734 if (unlikely(!cur_end))
1735 return 0;
1736
1737 /* we have a full HTTP/1.0 request now and we know that
1738 * we have either a CR or an LF at <ptr>.
1739 */
1740 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1741 return 1;
1742}
1743
Willy Tarreau5b154472009-12-21 20:11:07 +01001744/* Parse the Connection: headaer of an HTTP request, and set the transaction
1745 * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag
1746 * is also set so that we don't parse a second time. If some dangerous values
1747 * are encountered, we leave the status to indicate that the request might be
1748 * interpreted as keep-alive, but we also set the connection flags to indicate
1749 * that we WANT it to be a close, so that the header will be fixed. This
1750 * function should only be called when we know we're interested in checking
1751 * the request (not a CONNECT, and FE or BE mangles the header).
1752 */
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001753void http_req_parse_connection_header(struct http_txn *txn)
Willy Tarreau5b154472009-12-21 20:11:07 +01001754{
1755 struct http_msg *msg = &txn->req;
1756 struct hdr_ctx ctx;
1757 int conn_cl, conn_ka;
1758
1759 if (txn->flags & TX_CON_HDR_PARS)
1760 return;
1761
1762 conn_cl = 0;
1763 conn_ka = 0;
1764 ctx.idx = 0;
1765
1766 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
1767 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
1768 conn_cl = 1;
1769 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
1770 conn_ka = 1;
1771 }
1772
1773 /* Determine if the client wishes keep-alive or close.
1774 * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections
1775 * are persistent unless "Connection: close" is explicitly specified.
1776 * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections.
1777 * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless
1778 * they explicitly specify "Connection: Keep-Alive", regardless of any
1779 * optional "Keep-Alive" header.
1780 * Note that if we find a request with both "Connection: close" and
1781 * "Connection: Keep-Alive", we indicate we want a close but don't have
1782 * it, so that it can be enforced later.
1783 */
1784
1785 if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */
1786 if (conn_cl) {
1787 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1788 if (!conn_ka)
1789 txn->flags |= TX_REQ_CONN_CLO;
1790 }
1791 } else { /* HTTP/1.0 */
1792 if (!conn_ka)
1793 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO | TX_REQ_CONN_CLO;
1794 else if (conn_cl)
1795 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1796 }
1797 txn->flags |= TX_CON_HDR_PARS;
1798}
1799
Willy Tarreaud98cf932009-12-27 22:54:55 +01001800/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1801 * first byte of body, and increments msg->sov by the number of bytes parsed,
1802 * so that we know we can forward between ->som and ->sov. Note that due to
1803 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1804 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001805 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001806 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001807 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001808int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001809{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001810 char *ptr = buf->lr;
1811 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001812 unsigned int chunk = 0;
1813
1814 /* The chunk size is in the following form, though we are only
1815 * interested in the size and CRLF :
1816 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1817 */
1818 while (1) {
1819 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001820 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001821 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001822 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001823 if (c < 0) /* not a hex digit anymore */
1824 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001825 if (++ptr >= end)
1826 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001827 if (chunk & 0xF000000) /* overflow will occur */
1828 return -1;
1829 chunk = (chunk << 4) + c;
1830 }
1831
Willy Tarreaud98cf932009-12-27 22:54:55 +01001832 /* empty size not allowed */
1833 if (ptr == buf->lr)
1834 return -1;
1835
1836 while (http_is_spht[(unsigned char)*ptr]) {
1837 if (++ptr >= end)
1838 ptr = buf->data;
1839 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001840 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001841 }
1842
Willy Tarreaud98cf932009-12-27 22:54:55 +01001843 /* Up to there, we know that at least one byte is present at *ptr. Check
1844 * for the end of chunk size.
1845 */
1846 while (1) {
1847 if (likely(HTTP_IS_CRLF(*ptr))) {
1848 /* we now have a CR or an LF at ptr */
1849 if (likely(*ptr == '\r')) {
1850 if (++ptr >= end)
1851 ptr = buf->data;
1852 if (ptr == buf->r)
1853 return 0;
1854 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001855
Willy Tarreaud98cf932009-12-27 22:54:55 +01001856 if (*ptr != '\n')
1857 return -1;
1858 if (++ptr >= end)
1859 ptr = buf->data;
1860 /* done */
1861 break;
1862 }
1863 else if (*ptr == ';') {
1864 /* chunk extension, ends at next CRLF */
1865 if (++ptr >= end)
1866 ptr = buf->data;
1867 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001868 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001869
1870 while (!HTTP_IS_CRLF(*ptr)) {
1871 if (++ptr >= end)
1872 ptr = buf->data;
1873 if (ptr == buf->r)
1874 return 0;
1875 }
1876 /* we have a CRLF now, loop above */
1877 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001878 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001879 else
Willy Tarreau115acb92009-12-26 13:56:06 +01001880 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001881 }
1882
Willy Tarreaud98cf932009-12-27 22:54:55 +01001883 /* OK we found our CRLF and now <ptr> points to the next byte,
1884 * which may or may not be present. We save that into ->lr and
1885 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001886 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001887 msg->sov += ptr - buf->lr;
1888 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01001889 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001890 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001891 return 1;
1892}
1893
Willy Tarreaud98cf932009-12-27 22:54:55 +01001894/* This function skips trailers in the buffer <buf> associated with HTTP
1895 * message <msg>. The first visited position is buf->lr. If the end of
1896 * the trailers is found, it is automatically scheduled to be forwarded,
1897 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1898 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001899 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1900 * zero. If a parse error is encountered, the function returns < 0 and does not
1901 * change anything except maybe buf->lr and msg->sov. Note that the message
1902 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1903 * which implies that all non-trailers data have already been scheduled for
1904 * forwarding, and that the difference between msg->som and msg->sov exactly
1905 * matches the length of trailers already parsed and not forwarded. It is also
1906 * important to note that this function is designed to be able to parse wrapped
1907 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001908 */
1909int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1910{
1911 /* we have buf->lr which points to next line. Look for CRLF. */
1912 while (1) {
1913 char *p1 = NULL, *p2 = NULL;
1914 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001915 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001916
1917 /* scan current line and stop at LF or CRLF */
1918 while (1) {
1919 if (ptr == buf->r)
1920 return 0;
1921
1922 if (*ptr == '\n') {
1923 if (!p1)
1924 p1 = ptr;
1925 p2 = ptr;
1926 break;
1927 }
1928
1929 if (*ptr == '\r') {
1930 if (p1)
1931 return -1;
1932 p1 = ptr;
1933 }
1934
1935 ptr++;
1936 if (ptr >= buf->data + buf->size)
1937 ptr = buf->data;
1938 }
1939
1940 /* after LF; point to beginning of next line */
1941 p2++;
1942 if (p2 >= buf->data + buf->size)
1943 p2 = buf->data;
1944
Willy Tarreau638cd022010-01-03 07:42:04 +01001945 bytes = p2 - buf->lr;
1946 if (bytes < 0)
1947 bytes += buf->size;
1948
1949 /* schedule this line for forwarding */
1950 msg->sov += bytes;
1951 if (msg->sov >= buf->size)
1952 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953
Willy Tarreau638cd022010-01-03 07:42:04 +01001954 if (p1 == buf->lr) {
1955 /* LF/CRLF at beginning of line => end of trailers at p2.
1956 * Everything was scheduled for forwarding, there's nothing
1957 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001958 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001959 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001960 msg->msg_state = HTTP_MSG_DONE;
1961 return 1;
1962 }
1963 /* OK, next line then */
1964 buf->lr = p2;
1965 }
1966}
1967
1968/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1969 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
1970 * ->som, buf->lr in order to include this part into the next forwarding phase.
1971 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1972 * not enough data are available, the function does not change anything and
1973 * returns zero. If a parse error is encountered, the function returns < 0 and
1974 * does not change anything. Note: this function is designed to parse wrapped
1975 * CRLF at the end of the buffer.
1976 */
1977int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1978{
1979 char *ptr;
1980 int bytes;
1981
1982 /* NB: we'll check data availabilty at the end. It's not a
1983 * problem because whatever we match first will be checked
1984 * against the correct length.
1985 */
1986 bytes = 1;
1987 ptr = buf->lr;
1988 if (*ptr == '\r') {
1989 bytes++;
1990 ptr++;
1991 if (ptr >= buf->data + buf->size)
1992 ptr = buf->data;
1993 }
1994
1995 if (buf->l < bytes)
1996 return 0;
1997
1998 if (*ptr != '\n')
1999 return -1;
2000
2001 ptr++;
2002 if (ptr >= buf->data + buf->size)
2003 ptr = buf->data;
2004 buf->lr = ptr;
2005 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2006 msg->sov = ptr - buf->data;
2007 msg->som = msg->sov - bytes;
2008 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2009 return 1;
2010}
Willy Tarreau5b154472009-12-21 20:11:07 +01002011
Willy Tarreau83e3af02009-12-28 17:39:57 +01002012void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2013{
2014 char *end = buf->data + buf->size;
2015 int off = buf->data + buf->size - buf->w;
2016
2017 /* two possible cases :
2018 * - the buffer is in one contiguous block, we move it in-place
2019 * - the buffer is in two blocks, we move it via the trash
2020 */
2021 if (buf->l) {
2022 int block1 = buf->l;
2023 int block2 = 0;
2024 if (buf->r <= buf->w) {
2025 /* non-contiguous block */
2026 block1 = buf->data + buf->size - buf->w;
2027 block2 = buf->r - buf->data;
2028 }
2029 if (block2)
2030 memcpy(trash, buf->data, block2);
2031 memmove(buf->data, buf->w, block1);
2032 if (block2)
2033 memcpy(buf->data + block1, trash, block2);
2034 }
2035
2036 /* adjust all known pointers */
2037 buf->w = buf->data;
2038 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2039 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2040 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2041 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2042
2043 /* adjust relative pointers */
2044 msg->som = 0;
2045 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2046 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2047 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2048
2049 msg->sl.rq.u += off; if (msg->sl.rq.u >= buf->size) msg->sl.rq.u -= buf->size;
2050 msg->sl.rq.v += off; if (msg->sl.rq.v >= buf->size) msg->sl.rq.v -= buf->size;
2051
2052 if (msg->err_pos >= 0) {
2053 msg->err_pos += off;
2054 if (msg->err_pos >= buf->size)
2055 msg->err_pos -= buf->size;
2056 }
2057
2058 buf->flags &= ~BF_FULL;
2059 if (buf->l >= buffer_max_len(buf))
2060 buf->flags |= BF_FULL;
2061}
2062
Willy Tarreaud787e662009-07-07 10:14:51 +02002063/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2064 * processing can continue on next analysers, or zero if it either needs more
2065 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2066 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2067 * when it has nothing left to do, and may remove any analyser when it wants to
2068 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002069 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002070int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002071{
Willy Tarreau59234e92008-11-30 23:51:27 +01002072 /*
2073 * We will parse the partial (or complete) lines.
2074 * We will check the request syntax, and also join multi-line
2075 * headers. An index of all the lines will be elaborated while
2076 * parsing.
2077 *
2078 * For the parsing, we use a 28 states FSM.
2079 *
2080 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002081 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002082 * req->data + msg->eoh = end of processed headers / start of current one
2083 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002084 * req->lr = first non-visited byte
2085 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002086 *
2087 * At end of parsing, we may perform a capture of the error (if any), and
2088 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2089 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2090 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002091 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002092
Willy Tarreau59234e92008-11-30 23:51:27 +01002093 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002094 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002095 struct http_txn *txn = &s->txn;
2096 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002097 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002098
Willy Tarreau6bf17362009-02-24 10:48:35 +01002099 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2100 now_ms, __FUNCTION__,
2101 s,
2102 req,
2103 req->rex, req->wex,
2104 req->flags,
2105 req->l,
2106 req->analysers);
2107
Willy Tarreau52a0c602009-08-16 22:45:38 +02002108 /* we're speaking HTTP here, so let's speak HTTP to the client */
2109 s->srv_error = http_return_srv_error;
2110
Willy Tarreau83e3af02009-12-28 17:39:57 +01002111 /* There's a protected area at the end of the buffer for rewriting
2112 * purposes. We don't want to start to parse the request if the
2113 * protected area is affected, because we may have to move processed
2114 * data later, which is much more complicated.
2115 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002116 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
2117 if (unlikely((req->flags & BF_FULL) ||
2118 req->r < req->lr ||
2119 req->r > req->data + req->size - global.tune.maxrewrite)) {
2120 if (req->send_max) {
2121 /* some data has still not left the buffer, wake us once that's done */
2122 buffer_dont_connect(req);
2123 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2124 return 0;
2125 }
2126 if (req->l <= req->size - global.tune.maxrewrite)
2127 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002128 }
2129
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002130 if (likely(req->lr < req->r))
2131 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002132 }
2133
Willy Tarreau59234e92008-11-30 23:51:27 +01002134 /* 1: we might have to print this header in debug mode */
2135 if (unlikely((global.mode & MODE_DEBUG) &&
2136 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002137 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002138 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002139
Willy Tarreau59234e92008-11-30 23:51:27 +01002140 sol = req->data + msg->som;
2141 eol = sol + msg->sl.rq.l;
2142 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002143
Willy Tarreau59234e92008-11-30 23:51:27 +01002144 sol += hdr_idx_first_pos(&txn->hdr_idx);
2145 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002146
Willy Tarreau59234e92008-11-30 23:51:27 +01002147 while (cur_idx) {
2148 eol = sol + txn->hdr_idx.v[cur_idx].len;
2149 debug_hdr("clihdr", s, sol, eol);
2150 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2151 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002152 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002153 }
2154
Willy Tarreau58f10d72006-12-04 02:26:12 +01002155
Willy Tarreau59234e92008-11-30 23:51:27 +01002156 /*
2157 * Now we quickly check if we have found a full valid request.
2158 * If not so, we check the FD and buffer states before leaving.
2159 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002160 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01002161 * requests are checked first.
2162 *
2163 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002164
Willy Tarreau655dce92009-11-08 13:10:58 +01002165 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002166 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002167 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002168 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002169 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2170 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002171
Willy Tarreau59234e92008-11-30 23:51:27 +01002172 /* 1: Since we are in header mode, if there's no space
2173 * left for headers, we won't be able to free more
2174 * later, so the session will never terminate. We
2175 * must terminate it now.
2176 */
2177 if (unlikely(req->flags & BF_FULL)) {
2178 /* FIXME: check if URI is set and return Status
2179 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002180 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002181 goto return_bad_req;
2182 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002183
Willy Tarreau59234e92008-11-30 23:51:27 +01002184 /* 2: have we encountered a read error ? */
2185 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002186 if (txn->flags & TX_NOT_FIRST)
2187 goto failed_keep_alive;
2188
Willy Tarreau59234e92008-11-30 23:51:27 +01002189 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002190 if (msg->err_pos >= 0)
2191 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002192 msg->msg_state = HTTP_MSG_ERROR;
2193 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002194
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002195 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002196 if (s->listener->counters)
2197 s->listener->counters->failed_req++;
2198
Willy Tarreau59234e92008-11-30 23:51:27 +01002199 if (!(s->flags & SN_ERR_MASK))
2200 s->flags |= SN_ERR_CLICL;
2201 if (!(s->flags & SN_FINST_MASK))
2202 s->flags |= SN_FINST_R;
2203 return 0;
2204 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002205
Willy Tarreau59234e92008-11-30 23:51:27 +01002206 /* 3: has the read timeout expired ? */
2207 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002208 if (txn->flags & TX_NOT_FIRST)
2209 goto failed_keep_alive;
2210
Willy Tarreau59234e92008-11-30 23:51:27 +01002211 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002212 if (msg->err_pos >= 0)
2213 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002214 txn->status = 408;
2215 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2216 msg->msg_state = HTTP_MSG_ERROR;
2217 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002218
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002219 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002220 if (s->listener->counters)
2221 s->listener->counters->failed_req++;
2222
Willy Tarreau59234e92008-11-30 23:51:27 +01002223 if (!(s->flags & SN_ERR_MASK))
2224 s->flags |= SN_ERR_CLITO;
2225 if (!(s->flags & SN_FINST_MASK))
2226 s->flags |= SN_FINST_R;
2227 return 0;
2228 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002229
Willy Tarreau59234e92008-11-30 23:51:27 +01002230 /* 4: have we encountered a close ? */
2231 else if (req->flags & BF_SHUTR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002232 if (txn->flags & TX_NOT_FIRST)
2233 goto failed_keep_alive;
2234
Willy Tarreau4076a152009-04-02 15:18:36 +02002235 if (msg->err_pos >= 0)
2236 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002237 txn->status = 400;
2238 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2239 msg->msg_state = HTTP_MSG_ERROR;
2240 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002241
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002242 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002243 if (s->listener->counters)
2244 s->listener->counters->failed_req++;
2245
Willy Tarreau59234e92008-11-30 23:51:27 +01002246 if (!(s->flags & SN_ERR_MASK))
2247 s->flags |= SN_ERR_CLICL;
2248 if (!(s->flags & SN_FINST_MASK))
2249 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002250 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002251 }
2252
Willy Tarreau520d95e2009-09-19 21:04:57 +02002253 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002254 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau5e8949c2010-01-03 14:38:03 +01002255 req->flags &= ~BF_DONT_READ;
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002256
Willy Tarreau59234e92008-11-30 23:51:27 +01002257 /* just set the request timeout once at the beginning of the request */
2258 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002259 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002260
Willy Tarreau59234e92008-11-30 23:51:27 +01002261 /* we're not ready yet */
2262 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002263
2264 failed_keep_alive:
2265 /* Here we process low-level errors for keep-alive requests. In
2266 * short, if the request is not the first one and it experiences
2267 * a timeout, read error or shutdown, we just silently close so
2268 * that the client can try again.
2269 */
2270 txn->status = 0;
2271 msg->msg_state = HTTP_MSG_RQBEFORE;
2272 req->analysers = 0;
2273 s->logs.logwait = 0;
2274 stream_int_cond_close(req->prod, NULL);
2275 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002276 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002277
Willy Tarreaud787e662009-07-07 10:14:51 +02002278 /* OK now we have a complete HTTP request with indexed headers. Let's
2279 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002280 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2281 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2282 * points to the CRLF of the request line. req->lr points to the first
2283 * byte after the last LF. msg->col and msg->sov point to the first
2284 * byte of data. msg->eol cannot be trusted because it may have been
2285 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002286 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002287
Willy Tarreaud787e662009-07-07 10:14:51 +02002288 /* Maybe we found in invalid header name while we were configured not
2289 * to block on that, so we have to capture it now.
2290 */
2291 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002292 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2293
Willy Tarreau59234e92008-11-30 23:51:27 +01002294 /* ensure we keep this pointer to the beginning of the message */
2295 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002296
Willy Tarreau59234e92008-11-30 23:51:27 +01002297 /*
2298 * 1: identify the method
2299 */
2300 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
2301
2302 /* we can make use of server redirect on GET and HEAD */
2303 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2304 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002305
Willy Tarreau59234e92008-11-30 23:51:27 +01002306 /*
2307 * 2: check if the URI matches the monitor_uri.
2308 * We have to do this for every request which gets in, because
2309 * the monitor-uri is defined by the frontend.
2310 */
2311 if (unlikely((s->fe->monitor_uri_len != 0) &&
2312 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2313 !memcmp(&req->data[msg->sl.rq.u],
2314 s->fe->monitor_uri,
2315 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002316 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002317 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002318 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002319 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002320
Willy Tarreau59234e92008-11-30 23:51:27 +01002321 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002322
Willy Tarreau59234e92008-11-30 23:51:27 +01002323 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002324 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2325 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002326
Willy Tarreau59234e92008-11-30 23:51:27 +01002327 ret = acl_pass(ret);
2328 if (cond->pol == ACL_COND_UNLESS)
2329 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002330
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 if (ret) {
2332 /* we fail this request, let's return 503 service unavail */
2333 txn->status = 503;
2334 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2335 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002336 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002337 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002338
Willy Tarreau59234e92008-11-30 23:51:27 +01002339 /* nothing to fail, let's reply normaly */
2340 txn->status = 200;
2341 stream_int_retnclose(req->prod, &http_200_chunk);
2342 goto return_prx_cond;
2343 }
2344
2345 /*
2346 * 3: Maybe we have to copy the original REQURI for the logs ?
2347 * Note: we cannot log anymore if the request has been
2348 * classified as invalid.
2349 */
2350 if (unlikely(s->logs.logwait & LW_REQ)) {
2351 /* we have a complete HTTP request that we must log */
2352 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2353 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002354
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 if (urilen >= REQURI_LEN)
2356 urilen = REQURI_LEN - 1;
2357 memcpy(txn->uri, &req->data[msg->som], urilen);
2358 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002359
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 if (!(s->logs.logwait &= ~LW_REQ))
2361 s->do_log(s);
2362 } else {
2363 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002364 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002365 }
Willy Tarreau06619262006-12-17 08:37:22 +01002366
Willy Tarreau59234e92008-11-30 23:51:27 +01002367 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002368 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2369 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002370
Willy Tarreau5b154472009-12-21 20:11:07 +01002371 /* ... and check if the request is HTTP/1.1 or above */
2372 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002373 ((req->data[msg->sl.rq.v + 5] > '1') ||
2374 ((req->data[msg->sl.rq.v + 5] == '1') &&
2375 (req->data[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002376 txn->flags |= TX_REQ_VER_11;
2377
2378 /* "connection" has not been parsed yet */
2379 txn->flags &= ~TX_CON_HDR_PARS;
2380
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002381 /* transfer length unknown*/
2382 txn->flags &= ~TX_REQ_XFER_LEN;
2383
Willy Tarreau59234e92008-11-30 23:51:27 +01002384 /* 5: we may need to capture headers */
2385 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
2386 capture_headers(req->data + msg->som, &txn->hdr_idx,
2387 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002388
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002389 /* 6: determine the transfer-length.
2390 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2391 * the presence of a message-body in a REQUEST and its transfer length
2392 * must be determined that way (in order of precedence) :
2393 * 1. The presence of a message-body in a request is signaled by the
2394 * inclusion of a Content-Length or Transfer-Encoding header field
2395 * in the request's header fields. When a request message contains
2396 * both a message-body of non-zero length and a method that does
2397 * not define any semantics for that request message-body, then an
2398 * origin server SHOULD either ignore the message-body or respond
2399 * with an appropriate error message (e.g., 413). A proxy or
2400 * gateway, when presented the same request, SHOULD either forward
2401 * the request inbound with the message- body or ignore the
2402 * message-body when determining a response.
2403 *
2404 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2405 * and the "chunked" transfer-coding (Section 6.2) is used, the
2406 * transfer-length is defined by the use of this transfer-coding.
2407 * If a Transfer-Encoding header field is present and the "chunked"
2408 * transfer-coding is not present, the transfer-length is defined
2409 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002410 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002411 * 3. If a Content-Length header field is present, its decimal value in
2412 * OCTETs represents both the entity-length and the transfer-length.
2413 * If a message is received with both a Transfer-Encoding header
2414 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002415 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002416 * 4. By the server closing the connection. (Closing the connection
2417 * cannot be used to indicate the end of a request body, since that
2418 * would leave no possibility for the server to send back a response.)
2419 *
2420 * Whenever a transfer-coding is applied to a message-body, the set of
2421 * transfer-codings MUST include "chunked", unless the message indicates
2422 * it is terminated by closing the connection. When the "chunked"
2423 * transfer-coding is used, it MUST be the last transfer-coding applied
2424 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002425 */
2426
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002427 /* CONNECT sets a tunnel and ignores everything else */
2428 if (txn->meth == HTTP_METH_CONNECT)
2429 goto skip_xfer_len;
2430
2431 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002432 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002433 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002434 while ((txn->flags & TX_REQ_VER_11) &&
2435 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002436 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2437 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2438 else if (txn->flags & TX_REQ_TE_CHNK) {
2439 /* bad transfer-encoding (chunked followed by something else) */
2440 use_close_only = 1;
2441 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2442 break;
2443 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002444 }
2445
Willy Tarreau32b47f42009-10-18 20:55:02 +02002446 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002447 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002448 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2449 signed long long cl;
2450
2451 if (!ctx.vlen)
2452 goto return_bad_req;
2453
2454 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2455 goto return_bad_req; /* parse failure */
2456
2457 if (cl < 0)
2458 goto return_bad_req;
2459
2460 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2461 goto return_bad_req; /* already specified, was different */
2462
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002463 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002464 msg->hdr_content_len = cl;
2465 }
2466
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002467 /* bodyless requests have a known length */
2468 if (!use_close_only)
2469 txn->flags |= TX_REQ_XFER_LEN;
2470
2471 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002472 /* end of job, return OK */
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 return 1;
2476
2477 return_bad_req:
2478 /* We centralize bad requests processing here */
2479 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2480 /* we detected a parsing error. We want to archive this request
2481 * in the dedicated proxy area for later troubleshooting.
2482 */
2483 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2484 }
2485
2486 txn->req.msg_state = HTTP_MSG_ERROR;
2487 txn->status = 400;
2488 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002489
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002490 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002491 if (s->listener->counters)
2492 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002493
2494 return_prx_cond:
2495 if (!(s->flags & SN_ERR_MASK))
2496 s->flags |= SN_ERR_PRXCOND;
2497 if (!(s->flags & SN_FINST_MASK))
2498 s->flags |= SN_FINST_R;
2499
2500 req->analysers = 0;
2501 req->analyse_exp = TICK_ETERNITY;
2502 return 0;
2503}
2504
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002505/* This stream analyser runs all HTTP request processing which is common to
2506 * frontends and backends, which means blocking ACLs, filters, connection-close,
2507 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002508 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002509 * either needs more data or wants to immediately abort the request (eg: deny,
2510 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002511 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002512int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002513{
Willy Tarreaud787e662009-07-07 10:14:51 +02002514 struct http_txn *txn = &s->txn;
2515 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002516 struct acl_cond *cond;
2517 struct redirect_rule *rule;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002518 struct wordlist *wl;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002519 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002520
Willy Tarreau655dce92009-11-08 13:10:58 +01002521 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002522 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002523 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002524 return 0;
2525 }
2526
Willy Tarreau3a816292009-07-07 10:55:49 +02002527 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002528 req->analyse_exp = TICK_ETERNITY;
2529
2530 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2531 now_ms, __FUNCTION__,
2532 s,
2533 req,
2534 req->rex, req->wex,
2535 req->flags,
2536 req->l,
2537 req->analysers);
2538
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002539 /* first check whether we have some ACLs set to block this request */
2540 list_for_each_entry(cond, &px->block_cond, list) {
2541 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002542
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002543 ret = acl_pass(ret);
2544 if (cond->pol == ACL_COND_UNLESS)
2545 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002546
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002547 if (ret) {
2548 txn->status = 403;
2549 /* let's log the request time */
2550 s->logs.tv_request = now;
2551 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2552 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002553 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002554 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002555
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002556 /* try headers filters */
2557 if (px->req_exp != NULL) {
2558 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2559 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002560
Willy Tarreau59234e92008-11-30 23:51:27 +01002561 /* has the request been denied ? */
2562 if (txn->flags & TX_CLDENY) {
2563 /* no need to go further */
2564 txn->status = 403;
2565 /* let's log the request time */
2566 s->logs.tv_request = now;
2567 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2568 goto return_prx_cond;
2569 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002570
2571 /* When a connection is tarpitted, we use the tarpit timeout,
2572 * which may be the same as the connect timeout if unspecified.
2573 * If unset, then set it to zero because we really want it to
2574 * eventually expire. We build the tarpit as an analyser.
2575 */
2576 if (txn->flags & TX_CLTARPIT) {
2577 buffer_erase(s->req);
2578 /* wipe the request out so that we can drop the connection early
2579 * if the client closes first.
2580 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002581 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002582 req->analysers = 0; /* remove switching rules etc... */
2583 req->analysers |= AN_REQ_HTTP_TARPIT;
2584 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2585 if (!req->analyse_exp)
2586 req->analyse_exp = tick_add(now_ms, 0);
2587 return 1;
2588 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002589 }
Willy Tarreau06619262006-12-17 08:37:22 +01002590
Willy Tarreau5b154472009-12-21 20:11:07 +01002591 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2592 * only change if both the request and the config reference something else.
Willy Tarreau42736642009-10-18 21:04:35 +02002593 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002594
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002595 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01002596 ((s->fe->options|s->be->options) & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01002597 int tmp = TX_CON_WANT_TUN;
2598 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2599 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002600 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2601 tmp = TX_CON_WANT_SCL;
Willy Tarreau5b154472009-12-21 20:11:07 +01002602 if ((s->fe->options|s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))
2603 tmp = TX_CON_WANT_CLO;
2604
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002605 if (!(txn->flags & TX_REQ_XFER_LEN))
2606 tmp = TX_CON_WANT_CLO;
2607
Willy Tarreau5b154472009-12-21 20:11:07 +01002608 if (!(txn->flags & TX_CON_HDR_PARS))
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002609 http_req_parse_connection_header(txn);
Willy Tarreau5b154472009-12-21 20:11:07 +01002610
2611 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2612 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2613 }
2614
2615 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2616 * once we know the backend, because the tunnel mode can be implied by the
2617 * lack of any close/keepalive options in both the FE and the BE. Since
2618 * this information can evolve with time, we proceed by trying to make the
2619 * header status match the desired status. For this, we'll have to adjust
2620 * the "Connection" header. The test for persistent connections has already
2621 * been performed, so we only enter here if there is a risk the connection
2622 * is considered as persistent and we want it to be closed on the server
2623 * side. It would be nice if we could enter this place only when a
2624 * Connection header exists. Note that a CONNECT method will not enter
2625 * here.
2626 */
2627 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002628 char *cur_ptr, *cur_end, *cur_next;
2629 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002630 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002631 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002632
Willy Tarreau5b154472009-12-21 20:11:07 +01002633 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002634 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002635
Willy Tarreau5b154472009-12-21 20:11:07 +01002636 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002637 cur_hdr = &txn->hdr_idx.v[cur_idx];
2638 cur_ptr = cur_next;
2639 cur_end = cur_ptr + cur_hdr->len;
2640 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002641
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002642 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002643 if (!val)
2644 continue;
2645
2646 /* 3 possibilities :
2647 * - we have already set "Connection: close" or we're in
2648 * HTTP/1.0, so we remove this line.
2649 * - we have not yet set "Connection: close", but this line
2650 * indicates close. We leave it untouched and set the flag.
2651 * - we have not yet set "Connection: close", and this line
2652 * indicates non-close. We replace it and set the flag.
2653 */
2654 if (must_delete) {
2655 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2656 http_msg_move_end(&txn->req, delta);
2657 cur_next += delta;
2658 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2659 txn->hdr_idx.used--;
2660 cur_hdr->len = 0;
2661 txn->flags |= TX_REQ_CONN_CLO;
2662 } else {
2663 if (cur_end - cur_ptr - val != 5 ||
2664 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2665 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2666 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002667 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002668 cur_hdr->len += delta;
2669 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002670 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002671 txn->flags |= TX_REQ_CONN_CLO;
2672 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002673 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002674 } /* for loop */
2675 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002676
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002677 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002678 list_for_each_entry(wl, &px->req_add, list) {
2679 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002680 goto return_bad_req;
2681 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002682
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002683 /* check if stats URI was requested, and if an auth is needed */
2684 if (px->uri_auth != NULL &&
2685 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2686 /* we have to check the URI and auth for this request.
2687 * FIXME!!! that one is rather dangerous, we want to
2688 * make it follow standard rules (eg: clear req->analysers).
2689 */
2690 if (stats_check_uri_auth(s, px)) {
2691 req->analysers = 0;
2692 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002694 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002695
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002696 /* check whether we have some ACLs set to redirect this request */
2697 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002698 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002699
Willy Tarreauf285f542010-01-03 20:03:03 +01002700 if (rule->cond) {
2701 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
2702 ret = acl_pass(ret);
2703 if (rule->cond->pol == ACL_COND_UNLESS)
2704 ret = !ret;
2705 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002706
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002707 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002708 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002709 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002710
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002711 /* build redirect message */
2712 switch(rule->code) {
2713 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002714 msg_fmt = HTTP_303;
2715 break;
2716 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002717 msg_fmt = HTTP_301;
2718 break;
2719 case 302:
2720 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002721 msg_fmt = HTTP_302;
2722 break;
2723 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002724
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002725 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002726 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002727
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002728 switch(rule->type) {
2729 case REDIRECT_TYPE_PREFIX: {
2730 const char *path;
2731 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002732
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002733 path = http_get_path(txn);
2734 /* build message using path */
2735 if (path) {
Willy Tarreaua95a1f42010-01-03 13:04:35 +01002736 pathlen = txn->req.sl.rq.u_l + (txn->req.sol-txn->req.som+txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002737 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2738 int qs = 0;
2739 while (qs < pathlen) {
2740 if (path[qs] == '?') {
2741 pathlen = qs;
2742 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002743 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002744 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002745 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002746 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002747 } else {
2748 path = "/";
2749 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002750 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002751
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002752 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002753 goto return_bad_req;
2754
2755 /* add prefix. Note that if prefix == "/", we don't want to
2756 * add anything, otherwise it makes it hard for the user to
2757 * configure a self-redirection.
2758 */
2759 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002760 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2761 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002762 }
2763
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002764 /* add path */
2765 memcpy(rdr.str + rdr.len, path, pathlen);
2766 rdr.len += pathlen;
2767 break;
2768 }
2769 case REDIRECT_TYPE_LOCATION:
2770 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002771 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002772 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002773
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002774 /* add location */
2775 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2776 rdr.len += rule->rdr_len;
2777 break;
2778 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002779
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002780 if (rule->cookie_len) {
2781 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2782 rdr.len += 14;
2783 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2784 rdr.len += rule->cookie_len;
2785 memcpy(rdr.str + rdr.len, "\r\n", 2);
2786 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002787 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002788
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002789 /* add end of headers and the keep-alive/close status.
2790 * We may choose to set keep-alive if the Location begins
2791 * with a slash, because the client will come back to the
2792 * same server.
2793 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002794 txn->status = rule->code;
2795 /* let's log the request time */
2796 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01002797
2798 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
2799 (txn->flags & TX_REQ_XFER_LEN) &&
2800 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
2801 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2802 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2803 /* keep-alive possible */
2804 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive\r\n\r\n", 28);
2805 rdr.len += 28;
2806 buffer_write(req->prod->ob, rdr.str, rdr.len);
2807 /* "eat" the request */
2808 buffer_ignore(req, msg->sov - msg->som);
2809 msg->som = msg->sov;
2810 req->analysers = AN_REQ_HTTP_XFER_BODY;
2811 txn->req.msg_state = HTTP_MSG_DONE;
2812 txn->rsp.msg_state = HTTP_MSG_CLOSED;
2813 break;
2814 } else {
2815 /* keep-alive not possible */
2816 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
2817 rdr.len += 23;
2818 stream_int_cond_close(req->prod, &rdr);
2819 goto return_prx_cond;
2820 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002821 }
2822 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002823
Willy Tarreaud98cf932009-12-27 22:54:55 +01002824 /* We can shut read side if we know how we won't transfer any more data && !abort_on_close */
2825 if ((txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau5b154472009-12-21 20:11:07 +01002826 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
Willy Tarreaud98cf932009-12-27 22:54:55 +01002827 (req->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)))
Willy Tarreau349a0f62009-10-18 21:17:42 +02002828 req->flags |= BF_DONT_READ;
Willy Tarreau5e8949c2010-01-03 14:38:03 +01002829 else
2830 req->flags &= ~BF_DONT_READ;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +02002831
Willy Tarreau2be39392010-01-03 17:24:51 +01002832 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2833 * If this happens, then the data will not come immediately, so we must
2834 * send all what we have without waiting. Note that due to the small gain
2835 * in waiting for the body of the request, it's easier to simply put the
2836 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
2837 * itself once used.
2838 */
2839 req->flags |= BF_SEND_DONTWAIT;
2840
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002841 /* that's OK for us now, let's move on to next analysers */
2842 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002843
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002844 return_bad_req:
2845 /* We centralize bad requests processing here */
2846 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2847 /* we detected a parsing error. We want to archive this request
2848 * in the dedicated proxy area for later troubleshooting.
2849 */
2850 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2851 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002852
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002853 txn->req.msg_state = HTTP_MSG_ERROR;
2854 txn->status = 400;
2855 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002856
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002857 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002858 if (s->listener->counters)
2859 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002860
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002861 return_prx_cond:
2862 if (!(s->flags & SN_ERR_MASK))
2863 s->flags |= SN_ERR_PRXCOND;
2864 if (!(s->flags & SN_FINST_MASK))
2865 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002866
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002867 req->analysers = 0;
2868 req->analyse_exp = TICK_ETERNITY;
2869 return 0;
2870}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002871
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002872/* This function performs all the processing enabled for the current request.
2873 * It returns 1 if the processing can continue on next analysers, or zero if it
2874 * needs more data, encounters an error, or wants to immediately abort the
2875 * request. It relies on buffers flags, and updates s->req->analysers.
2876 */
2877int http_process_request(struct session *s, struct buffer *req, int an_bit)
2878{
2879 struct http_txn *txn = &s->txn;
2880 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002881
Willy Tarreau655dce92009-11-08 13:10:58 +01002882 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002883 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002884 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002885 return 0;
2886 }
2887
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002888 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2889 now_ms, __FUNCTION__,
2890 s,
2891 req,
2892 req->rex, req->wex,
2893 req->flags,
2894 req->l,
2895 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002896
Willy Tarreau59234e92008-11-30 23:51:27 +01002897 /*
2898 * Right now, we know that we have processed the entire headers
2899 * and that unwanted requests have been filtered out. We can do
2900 * whatever we want with the remaining request. Also, now we
2901 * may have separate values for ->fe, ->be.
2902 */
Willy Tarreau06619262006-12-17 08:37:22 +01002903
Willy Tarreau59234e92008-11-30 23:51:27 +01002904 /*
2905 * If HTTP PROXY is set we simply get remote server address
2906 * parsing incoming request.
2907 */
2908 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2909 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2910 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002911
Willy Tarreau59234e92008-11-30 23:51:27 +01002912 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002913 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002914 * Note that doing so might move headers in the request, but
2915 * the fields will stay coherent and the URI will not move.
2916 * This should only be performed in the backend.
2917 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002918 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002919 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2920 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002921
Willy Tarreau59234e92008-11-30 23:51:27 +01002922 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002923 * 8: the appsession cookie was looked up very early in 1.2,
2924 * so let's do the same now.
2925 */
2926
2927 /* It needs to look into the URI */
2928 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002929 get_srv_from_appsession(s, &req->data[msg->sl.rq.u], msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002930 }
2931
2932 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002933 * 9: add X-Forwarded-For if either the frontend or the backend
2934 * asks for it.
2935 */
2936 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2937 if (s->cli_addr.ss_family == AF_INET) {
2938 /* Add an X-Forwarded-For header unless the source IP is
2939 * in the 'except' network range.
2940 */
2941 if ((!s->fe->except_mask.s_addr ||
2942 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2943 != s->fe->except_net.s_addr) &&
2944 (!s->be->except_mask.s_addr ||
2945 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2946 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002947 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002948 unsigned char *pn;
2949 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002950
2951 /* Note: we rely on the backend to get the header name to be used for
2952 * x-forwarded-for, because the header is really meant for the backends.
2953 * However, if the backend did not specify any option, we have to rely
2954 * on the frontend's header name.
2955 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002956 if (s->be->fwdfor_hdr_len) {
2957 len = s->be->fwdfor_hdr_len;
2958 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002959 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002960 len = s->fe->fwdfor_hdr_len;
2961 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002962 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002963 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002964
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002965 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002966 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01002967 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002968 }
2969 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002970 else if (s->cli_addr.ss_family == AF_INET6) {
2971 /* FIXME: for the sake of completeness, we should also support
2972 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002973 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002974 int len;
2975 char pn[INET6_ADDRSTRLEN];
2976 inet_ntop(AF_INET6,
2977 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2978 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002979
Willy Tarreau59234e92008-11-30 23:51:27 +01002980 /* Note: we rely on the backend to get the header name to be used for
2981 * x-forwarded-for, because the header is really meant for the backends.
2982 * However, if the backend did not specify any option, we have to rely
2983 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002984 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002985 if (s->be->fwdfor_hdr_len) {
2986 len = s->be->fwdfor_hdr_len;
2987 memcpy(trash, s->be->fwdfor_hdr_name, len);
2988 } else {
2989 len = s->fe->fwdfor_hdr_len;
2990 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002991 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002992 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002993
Willy Tarreau59234e92008-11-30 23:51:27 +01002994 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002995 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01002996 goto return_bad_req;
2997 }
2998 }
2999
3000 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003001 * 10: add X-Original-To if either the frontend or the backend
3002 * asks for it.
3003 */
3004 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3005
3006 /* FIXME: don't know if IPv6 can handle that case too. */
3007 if (s->cli_addr.ss_family == AF_INET) {
3008 /* Add an X-Original-To header unless the destination IP is
3009 * in the 'except' network range.
3010 */
3011 if (!(s->flags & SN_FRT_ADDR_SET))
3012 get_frt_addr(s);
3013
3014 if ((!s->fe->except_mask_to.s_addr ||
3015 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3016 != s->fe->except_to.s_addr) &&
3017 (!s->be->except_mask_to.s_addr ||
3018 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3019 != s->be->except_to.s_addr)) {
3020 int len;
3021 unsigned char *pn;
3022 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3023
3024 /* Note: we rely on the backend to get the header name to be used for
3025 * x-original-to, because the header is really meant for the backends.
3026 * However, if the backend did not specify any option, we have to rely
3027 * on the frontend's header name.
3028 */
3029 if (s->be->orgto_hdr_len) {
3030 len = s->be->orgto_hdr_len;
3031 memcpy(trash, s->be->orgto_hdr_name, len);
3032 } else {
3033 len = s->fe->orgto_hdr_len;
3034 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003035 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003036 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3037
3038 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003039 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003040 goto return_bad_req;
3041 }
3042 }
3043 }
3044
Willy Tarreau78599912009-10-17 20:12:21 +02003045 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau5b154472009-12-21 20:11:07 +01003046 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau78599912009-10-17 20:12:21 +02003047 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003048 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003049 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01003050 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01003051 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003052
3053 /* If we have no server assigned yet and we're balancing on url_param
3054 * with a POST request, we may be interested in checking the body for
3055 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003056 */
3057 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3058 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003059 s->be->url_param_post_limit != 0 &&
3060 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01003061 memchr(req->data + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003062 buffer_dont_connect(req);
3063 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003064 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003065
Willy Tarreaud98cf932009-12-27 22:54:55 +01003066 if (txn->flags & TX_REQ_XFER_LEN)
3067 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003068
Willy Tarreau59234e92008-11-30 23:51:27 +01003069 /*************************************************************
3070 * OK, that's finished for the headers. We have done what we *
3071 * could. Let's switch to the DATA state. *
3072 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003073 req->analyse_exp = TICK_ETERNITY;
3074 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003075
Willy Tarreau59234e92008-11-30 23:51:27 +01003076 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003077 /* OK let's go on with the BODY now */
3078 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003079
Willy Tarreau59234e92008-11-30 23:51:27 +01003080 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003081 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003082 /* we detected a parsing error. We want to archive this request
3083 * in the dedicated proxy area for later troubleshooting.
3084 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003085 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003086 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003087
Willy Tarreau59234e92008-11-30 23:51:27 +01003088 txn->req.msg_state = HTTP_MSG_ERROR;
3089 txn->status = 400;
3090 req->analysers = 0;
3091 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003092
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003093 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003094 if (s->listener->counters)
3095 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003096
Willy Tarreau59234e92008-11-30 23:51:27 +01003097 if (!(s->flags & SN_ERR_MASK))
3098 s->flags |= SN_ERR_PRXCOND;
3099 if (!(s->flags & SN_FINST_MASK))
3100 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003101 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003102}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003103
Willy Tarreau60b85b02008-11-30 23:28:40 +01003104/* This function is an analyser which processes the HTTP tarpit. It always
3105 * returns zero, at the beginning because it prevents any other processing
3106 * from occurring, and at the end because it terminates the request.
3107 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003108int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003109{
3110 struct http_txn *txn = &s->txn;
3111
3112 /* This connection is being tarpitted. The CLIENT side has
3113 * already set the connect expiration date to the right
3114 * timeout. We just have to check that the client is still
3115 * there and that the timeout has not expired.
3116 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003117 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003118 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3119 !tick_is_expired(req->analyse_exp, now_ms))
3120 return 0;
3121
3122 /* We will set the queue timer to the time spent, just for
3123 * logging purposes. We fake a 500 server error, so that the
3124 * attacker will not suspect his connection has been tarpitted.
3125 * It will not cause trouble to the logs because we can exclude
3126 * the tarpitted connections by filtering on the 'PT' status flags.
3127 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003128 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3129
3130 txn->status = 500;
3131 if (req->flags != BF_READ_ERROR)
3132 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3133
3134 req->analysers = 0;
3135 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003136
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003137 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003138 if (s->listener->counters)
3139 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003140
Willy Tarreau60b85b02008-11-30 23:28:40 +01003141 if (!(s->flags & SN_ERR_MASK))
3142 s->flags |= SN_ERR_PRXCOND;
3143 if (!(s->flags & SN_FINST_MASK))
3144 s->flags |= SN_FINST_T;
3145 return 0;
3146}
3147
Willy Tarreaud34af782008-11-30 23:36:37 +01003148/* This function is an analyser which processes the HTTP request body. It looks
3149 * for parameters to be used for the load balancing algorithm (url_param). It
3150 * must only be called after the standard HTTP request processing has occurred,
3151 * because it expects the request to be parsed. It returns zero if it needs to
3152 * read more data, or 1 once it has completed its analysis.
3153 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003154int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003155{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003156 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003157 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003158 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003159
3160 /* We have to parse the HTTP request body to find any required data.
3161 * "balance url_param check_post" should have been the only way to get
3162 * into this. We were brought here after HTTP header analysis, so all
3163 * related structures are ready.
3164 */
3165
Willy Tarreau522d6c02009-12-06 18:49:18 +01003166 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3167 goto missing_data;
3168
3169 if (msg->msg_state < HTTP_MSG_100_SENT) {
3170 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3171 * send an HTTP/1.1 100 Continue intermediate response.
3172 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003173 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003174 struct hdr_ctx ctx;
3175 ctx.idx = 0;
3176 /* Expect is allowed in 1.1, look for it */
3177 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3178 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3179 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3180 }
3181 }
3182 msg->msg_state = HTTP_MSG_100_SENT;
3183 }
3184
3185 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003186 /* we have msg->col and msg->sov which both point to the first
3187 * byte of message body. msg->som still points to the beginning
3188 * of the message. We must save the body in req->lr because it
3189 * survives buffer re-alignments.
3190 */
3191 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003192 if (txn->flags & TX_REQ_TE_CHNK)
3193 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3194 else
3195 msg->msg_state = HTTP_MSG_DATA;
3196 }
3197
3198 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003199 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003200 * set ->sov and ->lr to point to the body and switch to DATA or
3201 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003202 */
3203 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003204
Willy Tarreau115acb92009-12-26 13:56:06 +01003205 if (!ret)
3206 goto missing_data;
3207 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003208 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003209 }
3210
Willy Tarreaud98cf932009-12-27 22:54:55 +01003211 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003212 * We have the first non-header byte in msg->col, which is either the
3213 * beginning of the chunk size or of the data. The first data byte is in
3214 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3215 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003216 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003217
3218 if (msg->hdr_content_len < limit)
3219 limit = msg->hdr_content_len;
3220
Willy Tarreau7c96f672009-12-27 22:47:25 +01003221 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003222 goto http_end;
3223
3224 missing_data:
3225 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003226 if (req->flags & BF_FULL)
3227 goto return_bad_req;
3228
Willy Tarreau522d6c02009-12-06 18:49:18 +01003229 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3230 txn->status = 408;
3231 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3232 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003233 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003234
3235 /* we get here if we need to wait for more data */
3236 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003237 /* Not enough data. We'll re-use the http-request
3238 * timeout here. Ideally, we should set the timeout
3239 * relative to the accept() date. We just set the
3240 * request timeout once at the beginning of the
3241 * request.
3242 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003243 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003244 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003245 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003246 return 0;
3247 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003248
3249 http_end:
3250 /* The situation will not evolve, so let's give up on the analysis. */
3251 s->logs.tv_request = now; /* update the request timer to reflect full request */
3252 req->analysers &= ~an_bit;
3253 req->analyse_exp = TICK_ETERNITY;
3254 return 1;
3255
3256 return_bad_req: /* let's centralize all bad requests */
3257 txn->req.msg_state = HTTP_MSG_ERROR;
3258 txn->status = 400;
3259 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3260
3261 return_err_msg:
3262 req->analysers = 0;
3263 s->fe->counters.failed_req++;
3264 if (s->listener->counters)
3265 s->listener->counters->failed_req++;
3266
3267 if (!(s->flags & SN_ERR_MASK))
3268 s->flags |= SN_ERR_PRXCOND;
3269 if (!(s->flags & SN_FINST_MASK))
3270 s->flags |= SN_FINST_R;
3271 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003272}
3273
Willy Tarreaud98cf932009-12-27 22:54:55 +01003274/* This function is an analyser which forwards request body (including chunk
3275 * sizes if any). It is called as soon as we must forward, even if we forward
3276 * zero byte. The only situation where it must not be called is when we're in
3277 * tunnel mode and we want to forward till the close. It's used both to forward
3278 * remaining data and to resync after end of body. It expects the msg_state to
3279 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3280 * read more data, or 1 once we can go on with next request or end the session.
3281 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3282 * bytes of pending data + the headers if not already done (between som and sov).
3283 * It eventually adjusts som to match sov after the data in between have been sent.
3284 */
3285int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3286{
3287 struct http_txn *txn = &s->txn;
3288 struct http_msg *msg = &s->txn.req;
3289
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003290 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3291 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3292 /* Output closed while we were sending data. We must abort. */
3293 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003294 req->analysers &= ~an_bit;
3295 return 1;
3296 }
3297
Willy Tarreaud98cf932009-12-27 22:54:55 +01003298 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3299 return 0;
3300
3301 /* Note that we don't have to send 100-continue back because we don't
3302 * need the data to complete our job, and it's up to the server to
3303 * decide whether to return 100, 417 or anything else in return of
3304 * an "Expect: 100-continue" header.
3305 */
3306
3307 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3308 /* we have msg->col and msg->sov which both point to the first
3309 * byte of message body. msg->som still points to the beginning
3310 * of the message. We must save the body in req->lr because it
3311 * survives buffer re-alignments.
3312 */
3313 req->lr = req->data + msg->sov;
3314 if (txn->flags & TX_REQ_TE_CHNK)
3315 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3316 else {
3317 msg->msg_state = HTTP_MSG_DATA;
3318 }
3319 }
3320
Willy Tarreaud98cf932009-12-27 22:54:55 +01003321 while (1) {
Willy Tarreau638cd022010-01-03 07:42:04 +01003322 /* we may have some data pending */
3323 if (msg->hdr_content_len || msg->som != msg->sov) {
3324 int bytes = msg->sov - msg->som;
3325 if (bytes < 0) /* sov may have wrapped at the end */
3326 bytes += req->size;
3327 buffer_forward(req, bytes + msg->hdr_content_len);
3328 msg->hdr_content_len = 0; /* don't forward that again */
3329 msg->som = msg->sov;
3330 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003331
Willy Tarreaucaabe412010-01-03 23:08:28 +01003332 if (msg->msg_state == HTTP_MSG_DATA) {
3333 /* must still forward */
3334 if (req->to_forward)
3335 return 0;
3336
3337 /* nothing left to forward */
3338 if (txn->flags & TX_REQ_TE_CHNK)
3339 msg->msg_state = HTTP_MSG_DATA_CRLF;
3340 else {
3341 msg->msg_state = HTTP_MSG_DONE;
3342 goto http_msg_done;
3343 }
3344 }
3345 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003346 /* read the chunk size and assign it to ->hdr_content_len, then
3347 * set ->sov and ->lr to point to the body and switch to DATA or
3348 * TRAILERS state.
3349 */
3350 int ret = http_parse_chunk_size(req, msg);
3351
3352 if (!ret)
3353 goto missing_data;
3354 else if (ret < 0)
3355 goto return_bad_req;
3356 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003357 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003358 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3359 /* we want the CRLF after the data */
3360 int ret;
3361
3362 if (!(req->flags & BF_OUT_EMPTY))
3363 return 0;
3364 /* The output pointer does not move anymore, next unsent data
3365 * are available at ->w. Let's save that.
3366 */
3367 req->lr = req->w;
3368 ret = http_skip_chunk_crlf(req, msg);
3369
3370 if (ret == 0)
3371 goto missing_data;
3372 else if (ret < 0)
3373 goto return_bad_req;
3374 /* we're in MSG_CHUNK_SIZE now */
3375 }
3376 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3377 int ret = http_forward_trailers(req, msg);
3378
3379 if (ret == 0)
3380 goto missing_data;
3381 else if (ret < 0)
3382 goto return_bad_req;
3383 /* we're in HTTP_MSG_DONE now */
3384 }
3385 else if (msg->msg_state == HTTP_MSG_DONE) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01003386 http_msg_done:
Willy Tarreau5523b322009-12-29 12:05:52 +01003387 /* No need to read anymore, the request was completely parsed */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003388 req->flags |= BF_DONT_READ;
3389
Willy Tarreau5523b322009-12-29 12:05:52 +01003390 if (txn->rsp.msg_state < HTTP_MSG_DONE && txn->rsp.msg_state != HTTP_MSG_ERROR) {
3391 /* The server has not finished to respond, so we
3392 * don't want to move in order not to upset it.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003393 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003394 return 0;
3395 }
3396
3397 /* when we support keep-alive or server-close modes, we'll have
3398 * to reset the transaction here.
3399 */
Willy Tarreau5523b322009-12-29 12:05:52 +01003400
Willy Tarreaub608feb2010-01-02 22:47:18 +01003401 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3402 /* initiate a connection close to the server */
3403 req->cons->flags |= SI_FL_NOLINGER;
3404 buffer_shutw_now(req);
3405 }
3406 else if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) {
Willy Tarreau9438c712009-12-29 14:39:48 +01003407 /* Option forceclose is set, let's enforce it now
3408 * that the transfer is complete. We can safely speed
3409 * up the close because we know the server has received
3410 * everything we wanted it to receive.
3411 */
3412 req->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau82eeaf22009-12-29 12:09:05 +01003413 buffer_abort(req);
3414 }
3415
Willy Tarreau5523b322009-12-29 12:05:52 +01003416 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01003417 if (req->flags & BF_OUT_EMPTY) {
Willy Tarreau5523b322009-12-29 12:05:52 +01003418 msg->msg_state = HTTP_MSG_CLOSED;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003419 goto http_msg_closed;
3420 }
3421 else {
Willy Tarreau5523b322009-12-29 12:05:52 +01003422 msg->msg_state = HTTP_MSG_CLOSING;
Willy Tarreaucaabe412010-01-03 23:08:28 +01003423 goto http_msg_closing;
3424 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003425 }
3426 else {
3427 /* for other modes, we let further requests pass for now */
3428 req->flags &= ~BF_DONT_READ;
3429 /* FIXME: we're still forced to do that here */
3430 s->rep->flags &= ~BF_DONT_READ;
3431 break;
3432 }
3433 }
3434 else if (msg->msg_state == HTTP_MSG_CLOSING) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01003435 http_msg_closing:
Willy Tarreau5523b322009-12-29 12:05:52 +01003436 /* nothing else to forward, just waiting for the buffer to be empty */
3437 if (!(req->flags & BF_OUT_EMPTY))
3438 return 0;
3439 msg->msg_state = HTTP_MSG_CLOSED;
3440 }
3441 else if (msg->msg_state == HTTP_MSG_CLOSED) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01003442 http_msg_closed:
Willy Tarreau5523b322009-12-29 12:05:52 +01003443 req->flags &= ~BF_DONT_READ;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003444
3445 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3446
3447 /* FIXME : this part is 1) awful, 2) tricky, 3) duplicated
3448 * ... but it works.
3449 * We need a better way to force a connection close without
3450 * any risk of propagation to the other side. We need a more
3451 * portable way of releasing a backend's and a server's
3452 * connections. We need a safer way to reinitialize buffer
3453 * flags. We also need a more accurate method for computing
3454 * per-request data.
3455 */
3456 s->req->cons->flags |= SI_FL_NOLINGER;
3457 s->req->cons->shutr(s->req->cons);
3458 s->req->cons->shutw(s->req->cons);
3459
3460 if (s->flags & SN_BE_ASSIGNED)
3461 s->be->beconn--;
3462
3463 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3464 session_process_counters(s);
3465
3466 if (s->txn.status) {
3467 int n;
3468
3469 n = s->txn.status / 100;
3470 if (n < 1 || n > 5)
3471 n = 0;
3472
3473 if (s->fe->mode == PR_MODE_HTTP)
3474 s->fe->counters.p.http.rsp[n]++;
3475
3476 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3477 (s->be->mode == PR_MODE_HTTP))
3478 s->be->counters.p.http.rsp[n]++;
3479 }
3480
3481 /* don't count other requests' data */
3482 s->logs.bytes_in -= s->req->l - s->req->send_max;
3483 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3484
3485 /* let's do a final log if we need it */
3486 if (s->logs.logwait &&
3487 !(s->flags & SN_MONITOR) &&
3488 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3489 s->do_log(s);
3490 }
3491
3492 s->logs.accept_date = date; /* user-visible date for logging */
3493 s->logs.tv_accept = now; /* corrected date for internal use */
3494 tv_zero(&s->logs.tv_request);
3495 s->logs.t_queue = -1;
3496 s->logs.t_connect = -1;
3497 s->logs.t_data = -1;
3498 s->logs.t_close = 0;
3499 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3500 s->logs.srv_queue_size = 0; /* we will get this number soon */
3501
3502 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3503 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3504
3505 if (s->pend_pos)
3506 pendconn_free(s->pend_pos);
3507
3508 if (s->srv) {
3509 if (s->flags & SN_CURR_SESS) {
3510 s->flags &= ~SN_CURR_SESS;
3511 s->srv->cur_sess--;
3512 }
3513 if (may_dequeue_tasks(s->srv, s->be))
3514 process_srv_queue(s->srv);
3515 }
3516
3517 if (unlikely(s->srv_conn))
3518 sess_change_server(s, NULL);
3519 s->srv = NULL;
3520
3521 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3522 s->req->cons->fd = -1; /* just to help with debugging */
3523 s->req->cons->err_type = SI_ET_NONE;
3524 s->req->cons->err_loc = NULL;
3525 s->req->cons->exp = TICK_ETERNITY;
3526 s->req->cons->flags = SI_FL_NONE;
3527 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE);
3528 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE|BF_WRITE_PARTIAL);
3529 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3530 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3531 s->txn.meth = 0;
3532 http_reset_txn(s);
3533 txn->flags |= TX_NOT_FIRST;
3534 if (s->be->options2 & PR_O2_INDEPSTR)
3535 s->req->cons->flags |= SI_FL_INDEP_STR;
3536
Willy Tarreauface8392010-01-03 11:37:54 +01003537 /* if the request buffer is not empty, it means we're
3538 * about to process another request, so send pending
3539 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau5e8949c2010-01-03 14:38:03 +01003540 * Also, let's not start reading a small request packet,
3541 * we may prefer to read a larger one later.
Willy Tarreauface8392010-01-03 11:37:54 +01003542 */
Willy Tarreau5e8949c2010-01-03 14:38:03 +01003543 if (s->req->l > s->req->send_max) {
Willy Tarreauface8392010-01-03 11:37:54 +01003544 s->rep->flags |= BF_EXPECT_MORE;
Willy Tarreau5e8949c2010-01-03 14:38:03 +01003545 s->req->flags |= BF_DONT_READ;
3546 }
Willy Tarreauface8392010-01-03 11:37:54 +01003547
Willy Tarreaub608feb2010-01-02 22:47:18 +01003548 /* make ->lr point to the first non-forwarded byte */
3549 s->req->lr = s->req->w + s->req->send_max;
3550 if (s->req->lr >= s->req->data + s->req->size)
3551 s->req->lr -= s->req->size;
3552 s->rep->lr = s->rep->w + s->rep->send_max;
3553 if (s->rep->lr >= s->rep->data + s->rep->size)
3554 s->rep->lr -= s->req->size;
3555
3556 s->req->analysers |= s->fe->fe_req_ana;
3557 s->rep->analysers = 0;
3558 }
3559
Willy Tarreau5523b322009-12-29 12:05:52 +01003560 /* FIXME: we're still forced to do that here */
3561 s->rep->flags &= ~BF_DONT_READ;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003562 break;
3563 }
3564 }
3565
3566 /* OK we're done with the data phase */
3567 req->analysers &= ~an_bit;
3568 return 1;
3569
3570 missing_data:
3571 /* forward the chunk size as well as any pending data */
3572 if (msg->hdr_content_len || msg->som != msg->sov) {
3573 buffer_forward(req, msg->sov - msg->som + msg->hdr_content_len);
3574 msg->hdr_content_len = 0; /* don't forward that again */
3575 msg->som = msg->sov;
3576 }
3577
3578 if (req->flags & BF_FULL)
3579 goto return_bad_req;
3580 /* the session handler will take care of timeouts and errors */
3581 return 0;
3582
3583 return_bad_req: /* let's centralize all bad requests */
3584 txn->req.msg_state = HTTP_MSG_ERROR;
3585 txn->status = 400;
3586 /* Note: we don't send any error if some data were already sent */
3587 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3588
3589 req->analysers = 0;
3590 s->fe->counters.failed_req++;
3591 if (s->listener->counters)
3592 s->listener->counters->failed_req++;
3593
3594 if (!(s->flags & SN_ERR_MASK))
3595 s->flags |= SN_ERR_PRXCOND;
3596 if (!(s->flags & SN_FINST_MASK))
3597 s->flags |= SN_FINST_R;
3598 return 0;
3599}
3600
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003601/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3602 * processing can continue on next analysers, or zero if it either needs more
3603 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3604 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3605 * when it has nothing left to do, and may remove any analyser when it wants to
3606 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003607 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003608int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003609{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003610 struct http_txn *txn = &s->txn;
3611 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003612 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003613 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003614 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003615 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003616
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003617 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 +02003618 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003619 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003620 rep,
3621 rep->rex, rep->wex,
3622 rep->flags,
3623 rep->l,
3624 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003625
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003626 /*
3627 * Now parse the partial (or complete) lines.
3628 * We will check the response syntax, and also join multi-line
3629 * headers. An index of all the lines will be elaborated while
3630 * parsing.
3631 *
3632 * For the parsing, we use a 28 states FSM.
3633 *
3634 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003635 * rep->data + msg->som = beginning of response
3636 * rep->data + msg->eoh = end of processed headers / start of current one
3637 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003638 * rep->lr = first non-visited byte
3639 * rep->r = end of data
3640 */
3641
Willy Tarreau83e3af02009-12-28 17:39:57 +01003642 /* There's a protected area at the end of the buffer for rewriting
3643 * purposes. We don't want to start to parse the request if the
3644 * protected area is affected, because we may have to move processed
3645 * data later, which is much more complicated.
3646 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003647 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3648 if (unlikely((rep->flags & BF_FULL) ||
3649 rep->r < rep->lr ||
3650 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3651 if (rep->send_max) {
3652 /* some data has still not left the buffer, wake us once that's done */
3653 buffer_dont_close(rep);
3654 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
3655 return 0;
3656 }
3657 if (rep->l <= rep->size - global.tune.maxrewrite)
3658 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003659 }
3660
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003661 if (likely(rep->lr < rep->r))
3662 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003663 }
3664
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003665 /* 1: we might have to print this header in debug mode */
3666 if (unlikely((global.mode & MODE_DEBUG) &&
3667 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003668 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003669 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003670
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003671 sol = rep->data + msg->som;
3672 eol = sol + msg->sl.rq.l;
3673 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003674
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003675 sol += hdr_idx_first_pos(&txn->hdr_idx);
3676 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003677
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003678 while (cur_idx) {
3679 eol = sol + txn->hdr_idx.v[cur_idx].len;
3680 debug_hdr("srvhdr", s, sol, eol);
3681 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3682 cur_idx = txn->hdr_idx.v[cur_idx].next;
3683 }
3684 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003685
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003686 /*
3687 * Now we quickly check if we have found a full valid response.
3688 * If not so, we check the FD and buffer states before leaving.
3689 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01003690 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003691 * responses are checked first.
3692 *
3693 * Depending on whether the client is still there or not, we
3694 * may send an error response back or not. Note that normally
3695 * we should only check for HTTP status there, and check I/O
3696 * errors somewhere else.
3697 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003698
Willy Tarreau655dce92009-11-08 13:10:58 +01003699 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003700 /* Invalid response */
3701 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
3702 /* we detected a parsing error. We want to archive this response
3703 * in the dedicated proxy area for later troubleshooting.
3704 */
3705 hdr_response_bad:
3706 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
3707 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3708
3709 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003710 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003711 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003712 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
3713 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003714
3715 rep->analysers = 0;
3716 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003717 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003718 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
3719
3720 if (!(s->flags & SN_ERR_MASK))
3721 s->flags |= SN_ERR_PRXCOND;
3722 if (!(s->flags & SN_FINST_MASK))
3723 s->flags |= SN_FINST_H;
3724
3725 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003726 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003727
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003728 /* too large response does not fit in buffer. */
3729 else if (rep->flags & BF_FULL) {
3730 goto hdr_response_bad;
3731 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003732
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003733 /* read error */
3734 else if (rep->flags & BF_READ_ERROR) {
3735 if (msg->err_pos >= 0)
3736 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02003737
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003738 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003739 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003740 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003741 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
3742 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003743
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003744 rep->analysers = 0;
3745 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003746 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003747 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02003748
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003749 if (!(s->flags & SN_ERR_MASK))
3750 s->flags |= SN_ERR_SRVCL;
3751 if (!(s->flags & SN_FINST_MASK))
3752 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003753 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003754 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003755
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003756 /* read timeout : return a 504 to the client. */
3757 else if (rep->flags & BF_READ_TIMEOUT) {
3758 if (msg->err_pos >= 0)
3759 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003760
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003761 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003762 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003763 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003764 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
3765 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003766
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003767 rep->analysers = 0;
3768 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003769 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003770 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02003771
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003772 if (!(s->flags & SN_ERR_MASK))
3773 s->flags |= SN_ERR_SRVTO;
3774 if (!(s->flags & SN_FINST_MASK))
3775 s->flags |= SN_FINST_H;
3776 return 0;
3777 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02003778
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003779 /* close from server */
3780 else if (rep->flags & BF_SHUTR) {
3781 if (msg->err_pos >= 0)
3782 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003783
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003784 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003785 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003786 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003787 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
3788 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003789
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003790 rep->analysers = 0;
3791 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003792 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003793 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01003794
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003795 if (!(s->flags & SN_ERR_MASK))
3796 s->flags |= SN_ERR_SRVCL;
3797 if (!(s->flags & SN_FINST_MASK))
3798 s->flags |= SN_FINST_H;
3799 return 0;
3800 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003801
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003802 /* write error to client (we don't send any message then) */
3803 else if (rep->flags & BF_WRITE_ERROR) {
3804 if (msg->err_pos >= 0)
3805 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003806
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003807 s->be->counters.failed_resp++;
3808 rep->analysers = 0;
3809
3810 if (!(s->flags & SN_ERR_MASK))
3811 s->flags |= SN_ERR_CLICL;
3812 if (!(s->flags & SN_FINST_MASK))
3813 s->flags |= SN_FINST_H;
3814
3815 /* process_session() will take care of the error */
3816 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003817 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003818
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003819 buffer_dont_close(rep);
3820 return 0;
3821 }
3822
3823 /* More interesting part now : we know that we have a complete
3824 * response which at least looks like HTTP. We have an indicator
3825 * of each header's length, so we can parse them quickly.
3826 */
3827
3828 if (unlikely(msg->err_pos >= 0))
3829 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3830
3831 /* ensure we keep this pointer to the beginning of the message */
3832 msg->sol = rep->data + msg->som;
3833
3834 /*
3835 * 1: get the status code
3836 */
3837 n = rep->data[msg->sl.st.c] - '0';
3838 if (n < 1 || n > 5)
3839 n = 0;
3840 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003841
Willy Tarreau5b154472009-12-21 20:11:07 +01003842 /* check if the response is HTTP/1.1 or above */
3843 if ((msg->sl.st.v_l == 8) &&
3844 ((rep->data[msg->som + 5] > '1') ||
3845 ((rep->data[msg->som + 5] == '1') &&
3846 (rep->data[msg->som + 7] >= '1'))))
3847 txn->flags |= TX_RES_VER_11;
3848
3849 /* "connection" has not been parsed yet */
3850 txn->flags &= ~TX_CON_HDR_PARS;
3851
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003852 /* transfer length unknown*/
3853 txn->flags &= ~TX_RES_XFER_LEN;
3854
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003855 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
3856
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003857 if (txn->status >= 100 && txn->status < 500)
3858 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
3859 else
3860 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
3861
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003862 /*
3863 * 2: check for cacheability.
3864 */
3865
3866 switch (txn->status) {
3867 case 200:
3868 case 203:
3869 case 206:
3870 case 300:
3871 case 301:
3872 case 410:
3873 /* RFC2616 @13.4:
3874 * "A response received with a status code of
3875 * 200, 203, 206, 300, 301 or 410 MAY be stored
3876 * by a cache (...) unless a cache-control
3877 * directive prohibits caching."
3878 *
3879 * RFC2616 @9.5: POST method :
3880 * "Responses to this method are not cacheable,
3881 * unless the response includes appropriate
3882 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003883 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003884 if (likely(txn->meth != HTTP_METH_POST) &&
3885 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3886 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3887 break;
3888 default:
3889 break;
3890 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003891
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003892 /*
3893 * 3: we may need to capture headers
3894 */
3895 s->logs.logwait &= ~LW_RESP;
3896 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
3897 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3898 txn->rsp.cap, s->fe->rsp_cap);
3899
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003900 /* 4: determine the transfer-length.
3901 * According to RFC2616 #4.4, amended by the HTTPbis working group,
3902 * the presence of a message-body in a RESPONSE and its transfer length
3903 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003904 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003905 * All responses to the HEAD request method MUST NOT include a
3906 * message-body, even though the presence of entity-header fields
3907 * might lead one to believe they do. All 1xx (informational), 204
3908 * (No Content), and 304 (Not Modified) responses MUST NOT include a
3909 * message-body. All other responses do include a message-body,
3910 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003911 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003912 * 1. Any response which "MUST NOT" include a message-body (such as the
3913 * 1xx, 204 and 304 responses and any response to a HEAD request) is
3914 * always terminated by the first empty line after the header fields,
3915 * regardless of the entity-header fields present in the message.
3916 *
3917 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
3918 * the "chunked" transfer-coding (Section 6.2) is used, the
3919 * transfer-length is defined by the use of this transfer-coding.
3920 * If a Transfer-Encoding header field is present and the "chunked"
3921 * transfer-coding is not present, the transfer-length is defined by
3922 * the sender closing the connection.
3923 *
3924 * 3. If a Content-Length header field is present, its decimal value in
3925 * OCTETs represents both the entity-length and the transfer-length.
3926 * If a message is received with both a Transfer-Encoding header
3927 * field and a Content-Length header field, the latter MUST be ignored.
3928 *
3929 * 4. If the message uses the media type "multipart/byteranges", and
3930 * the transfer-length is not otherwise specified, then this self-
3931 * delimiting media type defines the transfer-length. This media
3932 * type MUST NOT be used unless the sender knows that the recipient
3933 * can parse it; the presence in a request of a Range header with
3934 * multiple byte-range specifiers from a 1.1 client implies that the
3935 * client can parse multipart/byteranges responses.
3936 *
3937 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003938 */
3939
3940 /* Skip parsing if no content length is possible. The response flags
3941 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003942 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003943 * FIXME: should we parse anyway and return an error on chunked encoding ?
3944 */
3945 if (txn->meth == HTTP_METH_HEAD ||
3946 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003947 txn->status == 204 || txn->status == 304) {
3948 txn->flags |= TX_RES_XFER_LEN;
3949 goto skip_content_length;
3950 }
3951
3952 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003953 goto skip_content_length;
3954
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003955 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003956 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003957 while ((txn->flags & TX_RES_VER_11) &&
3958 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003959 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
3960 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3961 else if (txn->flags & TX_RES_TE_CHNK) {
3962 /* bad transfer-encoding (chunked followed by something else) */
3963 use_close_only = 1;
3964 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3965 break;
3966 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003967 }
3968
3969 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
3970 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003971 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003972 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
3973 signed long long cl;
3974
3975 if (!ctx.vlen)
3976 goto hdr_response_bad;
3977
3978 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
3979 goto hdr_response_bad; /* parse failure */
3980
3981 if (cl < 0)
3982 goto hdr_response_bad;
3983
3984 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
3985 goto hdr_response_bad; /* already specified, was different */
3986
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003987 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003988 msg->hdr_content_len = cl;
3989 }
3990
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003991 /* FIXME: we should also implement the multipart/byterange method.
3992 * For now on, we resort to close mode in this case (unknown length).
3993 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003994skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003995
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003996 /* end of job, return OK */
3997 rep->analysers &= ~an_bit;
3998 rep->analyse_exp = TICK_ETERNITY;
3999 return 1;
4000}
4001
4002/* This function performs all the processing enabled for the current response.
4003 * It normally returns zero, but may return 1 if it absolutely needs to be
4004 * called again after other functions. It relies on buffers flags, and updates
4005 * t->rep->analysers. It might make sense to explode it into several other
4006 * functions. It works like process_request (see indications above).
4007 */
4008int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4009{
4010 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004011 struct http_msg *msg = &txn->rsp;
4012 struct proxy *cur_proxy;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004013 struct wordlist *wl;
Willy Tarreau5b154472009-12-21 20:11:07 +01004014 int conn_ka = 0, conn_cl = 0;
4015 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004016 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004017
4018 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4019 now_ms, __FUNCTION__,
4020 t,
4021 rep,
4022 rep->rex, rep->wex,
4023 rep->flags,
4024 rep->l,
4025 rep->analysers);
4026
Willy Tarreau655dce92009-11-08 13:10:58 +01004027 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004028 return 0;
4029
4030 rep->analysers &= ~an_bit;
4031 rep->analyse_exp = TICK_ETERNITY;
4032
Willy Tarreau5b154472009-12-21 20:11:07 +01004033 /* Now we have to check if we need to modify the Connection header.
4034 * This is more difficult on the response than it is on the request,
4035 * because we can have two different HTTP versions and we don't know
4036 * how the client will interprete a response. For instance, let's say
4037 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4038 * HTTP/1.1 response without any header. Maybe it will bound itself to
4039 * HTTP/1.0 because it only knows about it, and will consider the lack
4040 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4041 * the lack of header as a keep-alive. Thus we will use two flags
4042 * indicating how a request MAY be understood by the client. In case
4043 * of multiple possibilities, we'll fix the header to be explicit. If
4044 * ambiguous cases such as both close and keepalive are seen, then we
4045 * will fall back to explicit close. Note that we won't take risks with
4046 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4047 */
4048
4049 if ((txn->meth != HTTP_METH_CONNECT) &&
4050 (txn->status >= 200) &&
4051 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN &&
4052 !(txn->flags & TX_CON_HDR_PARS)) {
4053 int may_keep = 0, may_close = 0; /* how it may be understood */
4054 struct hdr_ctx ctx;
4055
4056 ctx.idx = 0;
4057 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4058 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4059 conn_cl = 1;
4060 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4061 conn_ka = 1;
4062 }
4063
4064 if (conn_cl) {
4065 /* close header present */
4066 may_close = 1;
4067 if (conn_ka) /* we have both close and keep-alive */
4068 may_keep = 1;
4069 }
4070 else if (conn_ka) {
4071 /* keep-alive alone */
4072 may_keep = 1;
4073 }
4074 else {
4075 /* no close nor keep-alive header */
4076 if (txn->flags & TX_RES_VER_11)
4077 may_keep = 1;
4078 else
4079 may_close = 1;
4080
4081 if (txn->flags & TX_REQ_VER_11)
4082 may_keep = 1;
4083 else
4084 may_close = 1;
4085 }
4086
4087 /* let's update the transaction status to reflect any close.
4088 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004089 * handled. We also explicitly state that we will close in
4090 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004091 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004092 if ((may_close &&
4093 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4094 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004095 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4096
4097 /* Now we must adjust the response header :
4098 * - set "close" if may_keep and WANT_CLO
4099 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4100 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
4101 *
4102 * Until we support the server-close mode, we'll only support the set "close".
4103 */
4104 if (may_keep && (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO)
4105 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004106 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4107 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4108 must_del_close = 1;
4109 if (!(txn->flags & TX_REQ_VER_11))
4110 must_keep = 1;
4111 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004112
4113 txn->flags |= TX_CON_HDR_PARS;
4114 }
4115
4116 /* We might have to check for "Connection:" if the server
4117 * returns a connection status that is not compatible with
4118 * the client's or with the config.
4119 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004120 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004121 char *cur_ptr, *cur_end, *cur_next;
4122 int cur_idx, old_idx, delta, val;
4123 int must_delete;
4124 struct hdr_idx_elem *cur_hdr;
4125
4126 /* we just have to remove the headers if both sides are 1.0 */
4127 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004128
4129 /* same if we want to re-enable keep-alive on 1.1 */
4130 must_delete |= must_del_close;
4131
Willy Tarreau5b154472009-12-21 20:11:07 +01004132 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4133
4134 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4135 cur_hdr = &txn->hdr_idx.v[cur_idx];
4136 cur_ptr = cur_next;
4137 cur_end = cur_ptr + cur_hdr->len;
4138 cur_next = cur_end + cur_hdr->cr + 1;
4139
4140 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4141 if (!val)
4142 continue;
4143
4144 /* 3 possibilities :
4145 * - we have already set "Connection: close" or we're in
4146 * HTTP/1.0, so we remove this line.
4147 * - we have not yet set "Connection: close", but this line
4148 * indicates close. We leave it untouched and set the flag.
4149 * - we have not yet set "Connection: close", and this line
4150 * indicates non-close. We replace it and set the flag.
4151 */
4152 if (must_delete) {
4153 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4154 http_msg_move_end(&txn->rsp, delta);
4155 cur_next += delta;
4156 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4157 txn->hdr_idx.used--;
4158 cur_hdr->len = 0;
4159 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004160 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004161 } else {
4162 if (cur_end - cur_ptr - val != 5 ||
4163 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4164 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4165 "close", 5);
4166 cur_next += delta;
4167 cur_hdr->len += delta;
4168 http_msg_move_end(&txn->rsp, delta);
4169 }
4170 must_delete = 1;
4171 must_close = 0;
4172 }
4173 } /* for loop */
4174 } /* if must close keep-alive */
4175
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004176 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004177 /*
4178 * 3: we will have to evaluate the filters.
4179 * As opposed to version 1.2, now they will be evaluated in the
4180 * filters order and not in the header order. This means that
4181 * each filter has to be validated among all headers.
4182 *
4183 * Filters are tried with ->be first, then with ->fe if it is
4184 * different from ->be.
4185 */
4186
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004187 cur_proxy = t->be;
4188 while (1) {
4189 struct proxy *rule_set = cur_proxy;
4190
4191 /* try headers filters */
4192 if (rule_set->rsp_exp != NULL) {
4193 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4194 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004195 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004196 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004197 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4198 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004199 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004200 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004201 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004202 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004203 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004204 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004205 if (!(t->flags & SN_ERR_MASK))
4206 t->flags |= SN_ERR_PRXCOND;
4207 if (!(t->flags & SN_FINST_MASK))
4208 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004209 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004210 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004211 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004212
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004213 /* has the response been denied ? */
4214 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004215 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004216 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004217
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004218 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004219 if (t->listener->counters)
4220 t->listener->counters->denied_resp++;
4221
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004222 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004223 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004224
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004225 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004226 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004227 if (txn->status < 200)
4228 break;
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004229 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004230 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004231 }
4232
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004233 /* check whether we're already working on the frontend */
4234 if (cur_proxy == t->fe)
4235 break;
4236 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004237 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004238
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004239 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004240 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4241 * in which case this is not the right response, and we're waiting for the
4242 * next one. Let's allow this response to go to the client and wait for the
4243 * next one.
4244 */
4245 if (txn->status < 200) {
4246 hdr_idx_init(&txn->hdr_idx);
4247 buffer_forward(rep, rep->lr - (rep->data + msg->som));
4248 msg->msg_state = HTTP_MSG_RPBEFORE;
4249 txn->status = 0;
4250 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4251 return 1;
4252 }
4253
4254 /* we don't have any 1xx status code now */
4255
4256 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004257 * 4: check for server cookie.
4258 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004259 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4260 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004261 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004262
Willy Tarreaubaaee002006-06-26 02:48:02 +02004263
Willy Tarreaua15645d2007-03-18 16:22:39 +01004264 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004265 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004266 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004267 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004268 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004269
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004270 /*
4271 * 6: add server cookie in the response if needed
4272 */
4273 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004274 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004275 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004276
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004277 /* the server is known, it's not the one the client requested, we have to
4278 * insert a set-cookie here, except if we want to insert only on POST
4279 * requests and this one isn't. Note that servers which don't have cookies
4280 * (eg: some backup servers) will return a full cookie removal request.
4281 */
4282 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4283 t->be->cookie_name,
4284 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004285
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004286 if (t->be->cookie_domain)
4287 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004288
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004289 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004290 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004291 goto return_bad_resp;
4292 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004293
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004294 /* Here, we will tell an eventual cache on the client side that we don't
4295 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4296 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4297 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4298 */
4299 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004300
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004301 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4302
4303 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004304 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004305 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004306 }
4307 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004308
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004309 /*
4310 * 7: check if result will be cacheable with a cookie.
4311 * We'll block the response if security checks have caught
4312 * nasty things such as a cacheable cookie.
4313 */
4314 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4315 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004316 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004317
4318 /* we're in presence of a cacheable response containing
4319 * a set-cookie header. We'll block it as requested by
4320 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004321 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004322 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004323 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004324
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004325 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004326 if (t->listener->counters)
4327 t->listener->counters->denied_resp++;
4328
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004329 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4330 t->be->id, t->srv?t->srv->id:"<dispatch>");
4331 send_log(t->be, LOG_ALERT,
4332 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4333 t->be->id, t->srv?t->srv->id:"<dispatch>");
4334 goto return_srv_prx_502;
4335 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004336
4337 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004338 * 8: add "Connection: close" if needed and not yet set. This is
4339 * only needed for 1.1 responses since we know there is no other
4340 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004341 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004342 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004343 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004344 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004345 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004346 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004347 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004348 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4349 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4350 "Connection: keep-alive", 22) < 0))
4351 goto return_bad_resp;
4352 must_keep = 0;
4353 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004354
Willy Tarreaud98cf932009-12-27 22:54:55 +01004355 if (txn->flags & TX_RES_XFER_LEN)
4356 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004357
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004358 /*************************************************************
4359 * OK, that's finished for the headers. We have done what we *
4360 * could. Let's switch to the DATA state. *
4361 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004362
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004363 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004364
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004365 /* if the user wants to log as soon as possible, without counting
4366 * bytes from the server, then this is the right moment. We have
4367 * to temporarily assign bytes_out to log what we currently have.
4368 */
4369 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4370 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4371 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004372 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004373 t->logs.bytes_out = 0;
4374 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004375
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004376 /* Note: we must not try to cheat by jumping directly to DATA,
4377 * otherwise we would not let the client side wake up.
4378 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004379
Willy Tarreaudafde432008-08-17 01:00:46 +02004380 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004381 }
4382 return 0;
4383}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004384
Willy Tarreaud98cf932009-12-27 22:54:55 +01004385/* This function is an analyser which forwards response body (including chunk
4386 * sizes if any). It is called as soon as we must forward, even if we forward
4387 * zero byte. The only situation where it must not be called is when we're in
4388 * tunnel mode and we want to forward till the close. It's used both to forward
4389 * remaining data and to resync after end of body. It expects the msg_state to
4390 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4391 * read more data, or 1 once we can go on with next request or end the session.
4392 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4393 * bytes of pending data + the headers if not already done (between som and sov).
4394 * It eventually adjusts som to match sov after the data in between have been sent.
4395 */
4396int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4397{
4398 struct http_txn *txn = &s->txn;
4399 struct http_msg *msg = &s->txn.rsp;
4400
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004401 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4402 !s->req->analysers) {
4403 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4404 buffer_ignore(res, res->l - res->send_max);
4405 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004406 res->analysers &= ~an_bit;
4407 return 1;
4408 }
4409
Willy Tarreaud98cf932009-12-27 22:54:55 +01004410 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4411 return 0;
4412
Willy Tarreaub608feb2010-01-02 22:47:18 +01004413 /* note: in server-close mode, we don't want to automatically close the
4414 * output when the input is closed.
4415 */
4416 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4417 buffer_dont_close(res);
4418
Willy Tarreaud98cf932009-12-27 22:54:55 +01004419 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4420 /* we have msg->col and msg->sov which both point to the first
4421 * byte of message body. msg->som still points to the beginning
4422 * of the message. We must save the body in req->lr because it
4423 * survives buffer re-alignments.
4424 */
4425 res->lr = res->data + msg->sov;
4426 if (txn->flags & TX_RES_TE_CHNK)
4427 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4428 else {
4429 msg->msg_state = HTTP_MSG_DATA;
4430 }
4431 }
4432
Willy Tarreaud98cf932009-12-27 22:54:55 +01004433 while (1) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004434 /* we may have some data pending */
4435 if (msg->hdr_content_len || msg->som != msg->sov) {
4436 int bytes = msg->sov - msg->som;
4437 if (bytes < 0) /* sov may have wrapped at the end */
4438 bytes += res->size;
4439 buffer_forward(res, bytes + msg->hdr_content_len);
4440 msg->hdr_content_len = 0; /* don't forward that again */
4441 msg->som = msg->sov;
4442 }
4443
Willy Tarreaucaabe412010-01-03 23:08:28 +01004444 if (msg->msg_state == HTTP_MSG_DATA) {
4445 /* must still forward */
4446 if (res->to_forward)
4447 return 0;
4448
4449 /* nothing left to forward */
4450 if (txn->flags & TX_RES_TE_CHNK)
4451 msg->msg_state = HTTP_MSG_DATA_CRLF;
4452 else
4453 msg->msg_state = HTTP_MSG_DONE;
4454 }
4455 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004456 /* read the chunk size and assign it to ->hdr_content_len, then
4457 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4458 */
4459 int ret = http_parse_chunk_size(res, msg);
4460
4461 if (!ret)
4462 goto missing_data;
4463 else if (ret < 0)
4464 goto return_bad_res;
4465 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004466 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004467 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4468 /* we want the CRLF after the data */
4469 int ret;
4470
4471 if (!(res->flags & BF_OUT_EMPTY))
4472 return 0;
4473 /* The output pointer does not move anymore, next unsent data
4474 * are available at ->w. Let's save that.
4475 */
4476 res->lr = res->w;
4477 ret = http_skip_chunk_crlf(res, msg);
4478
4479 if (!ret)
4480 goto missing_data;
4481 else if (ret < 0)
4482 goto return_bad_res;
4483 /* we're in MSG_CHUNK_SIZE now */
4484 }
4485 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4486 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004487
Willy Tarreaud98cf932009-12-27 22:54:55 +01004488 if (ret == 0)
4489 goto missing_data;
4490 else if (ret < 0)
4491 goto return_bad_res;
4492 /* we're in HTTP_MSG_DONE now */
4493 }
4494 else if (msg->msg_state == HTTP_MSG_DONE) {
Willy Tarreau5523b322009-12-29 12:05:52 +01004495 /* In theory, we don't need to read anymore, but we must
4496 * still monitor the server connection for a possible close,
4497 * so we don't set the BF_DONT_READ flag here.
4498 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004499
Willy Tarreau5523b322009-12-29 12:05:52 +01004500 if (txn->req.msg_state < HTTP_MSG_DONE && txn->req.msg_state != HTTP_MSG_ERROR) {
4501 /* The client seems to still be sending data, probably
4502 * because we got an error response during an upload.
4503 * We have the choice of either breaking the connection
4504 * or letting it pass through. Let's do the later.
4505 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004506 return 0;
Willy Tarreau5523b322009-12-29 12:05:52 +01004507 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004508
4509 /* when we support keep-alive or server-close modes, we'll have
4510 * to reset the transaction here.
4511 */
Willy Tarreau5523b322009-12-29 12:05:52 +01004512
Willy Tarreau82eeaf22009-12-29 12:09:05 +01004513 if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) {
4514 /* option forceclose is set, let's enforce it now that the transfer is complete. */
4515 buffer_abort(res);
4516 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004517 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4518 /* server close is handled entirely on the req analyser */
4519 s->req->cons->flags |= SI_FL_NOLINGER;
4520 buffer_shutw_now(s->req);
4521 }
Willy Tarreau82eeaf22009-12-29 12:09:05 +01004522
Willy Tarreau5523b322009-12-29 12:05:52 +01004523 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01004524 if (res->flags & BF_OUT_EMPTY) {
Willy Tarreau5523b322009-12-29 12:05:52 +01004525 msg->msg_state = HTTP_MSG_CLOSED;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004526 goto http_msg_closed;
4527 }
4528 else {
Willy Tarreau5523b322009-12-29 12:05:52 +01004529 msg->msg_state = HTTP_MSG_CLOSING;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004530 goto http_msg_closing;
4531 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004532 }
4533 else {
4534 /* for other modes, we let further responses pass for now */
4535 res->flags &= ~BF_DONT_READ;
4536 /* FIXME: we're still forced to do that here */
4537 s->req->flags &= ~BF_DONT_READ;
4538 break;
4539 }
4540 }
4541 else if (msg->msg_state == HTTP_MSG_CLOSING) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01004542 http_msg_closing:
Willy Tarreau5523b322009-12-29 12:05:52 +01004543 /* nothing else to forward, just waiting for the buffer to be empty */
4544 if (!(res->flags & BF_OUT_EMPTY))
4545 return 0;
4546 msg->msg_state = HTTP_MSG_CLOSED;
4547 }
4548 else if (msg->msg_state == HTTP_MSG_CLOSED) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01004549 http_msg_closed:
Willy Tarreau5523b322009-12-29 12:05:52 +01004550 res->flags &= ~BF_DONT_READ;
4551 /* FIXME: we're still forced to do that here */
4552 s->req->flags &= ~BF_DONT_READ;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004553 break;
4554 }
4555 }
4556
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004557 buffer_ignore(res, res->l - res->send_max);
4558 buffer_auto_close(res);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004559 res->analysers &= ~an_bit;
4560 return 1;
4561
4562 missing_data:
4563 /* forward the chunk size as well as any pending data */
4564 if (msg->hdr_content_len || msg->som != msg->sov) {
4565 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4566 msg->hdr_content_len = 0; /* don't forward that again */
4567 msg->som = msg->sov;
4568 }
4569
4570 if (res->flags & BF_FULL)
4571 goto return_bad_res;
4572 /* the session handler will take care of timeouts and errors */
4573 return 0;
4574
4575 return_bad_res: /* let's centralize all bad resuests */
4576 txn->rsp.msg_state = HTTP_MSG_ERROR;
4577 txn->status = 502;
4578 stream_int_cond_close(res->cons, NULL);
4579
4580 res->analysers = 0;
4581 s->be->counters.failed_resp++;
4582 if (s->srv) {
4583 s->srv->counters.failed_resp++;
4584 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4585 }
4586
4587 if (!(s->flags & SN_ERR_MASK))
4588 s->flags |= SN_ERR_PRXCOND;
4589 if (!(s->flags & SN_FINST_MASK))
4590 s->flags |= SN_FINST_R;
4591 return 0;
4592}
4593
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004594/* Iterate the same filter through all request headers.
4595 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004596 * Since it can manage the switch to another backend, it updates the per-proxy
4597 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004598 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004599int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004600{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004601 char term;
4602 char *cur_ptr, *cur_end, *cur_next;
4603 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004604 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004605 struct hdr_idx_elem *cur_hdr;
4606 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004607
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004608 last_hdr = 0;
4609
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004610 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004611 old_idx = 0;
4612
4613 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004614 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004615 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004616 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004617 (exp->action == ACT_ALLOW ||
4618 exp->action == ACT_DENY ||
4619 exp->action == ACT_TARPIT))
4620 return 0;
4621
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004622 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004623 if (!cur_idx)
4624 break;
4625
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004626 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004627 cur_ptr = cur_next;
4628 cur_end = cur_ptr + cur_hdr->len;
4629 cur_next = cur_end + cur_hdr->cr + 1;
4630
4631 /* Now we have one header between cur_ptr and cur_end,
4632 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004633 */
4634
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004635 /* The annoying part is that pattern matching needs
4636 * that we modify the contents to null-terminate all
4637 * strings before testing them.
4638 */
4639
4640 term = *cur_end;
4641 *cur_end = '\0';
4642
4643 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4644 switch (exp->action) {
4645 case ACT_SETBE:
4646 /* It is not possible to jump a second time.
4647 * FIXME: should we return an HTTP/500 here so that
4648 * the admin knows there's a problem ?
4649 */
4650 if (t->be != t->fe)
4651 break;
4652
4653 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004654 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004655 last_hdr = 1;
4656 break;
4657
4658 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004659 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004660 last_hdr = 1;
4661 break;
4662
4663 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004664 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004665 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004666
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004667 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004668 if (t->listener->counters)
4669 t->listener->counters->denied_resp++;
4670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004671 break;
4672
4673 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004674 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004675 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004676
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004677 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004678 if (t->listener->counters)
4679 t->listener->counters->denied_resp++;
4680
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004681 break;
4682
4683 case ACT_REPLACE:
4684 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4685 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4686 /* FIXME: if the user adds a newline in the replacement, the
4687 * index will not be recalculated for now, and the new line
4688 * will not be counted as a new header.
4689 */
4690
4691 cur_end += delta;
4692 cur_next += delta;
4693 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004694 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004695 break;
4696
4697 case ACT_REMOVE:
4698 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4699 cur_next += delta;
4700
Willy Tarreaufa355d42009-11-29 18:12:29 +01004701 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004702 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4703 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004704 cur_hdr->len = 0;
4705 cur_end = NULL; /* null-term has been rewritten */
4706 break;
4707
4708 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004709 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004710 if (cur_end)
4711 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004712
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004713 /* keep the link from this header to next one in case of later
4714 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004715 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004716 old_idx = cur_idx;
4717 }
4718 return 0;
4719}
4720
4721
4722/* Apply the filter to the request line.
4723 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4724 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004725 * Since it can manage the switch to another backend, it updates the per-proxy
4726 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004727 */
4728int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4729{
4730 char term;
4731 char *cur_ptr, *cur_end;
4732 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004733 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004734 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004735
Willy Tarreau58f10d72006-12-04 02:26:12 +01004736
Willy Tarreau3d300592007-03-18 18:34:41 +01004737 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004738 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004739 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004740 (exp->action == ACT_ALLOW ||
4741 exp->action == ACT_DENY ||
4742 exp->action == ACT_TARPIT))
4743 return 0;
4744 else if (exp->action == ACT_REMOVE)
4745 return 0;
4746
4747 done = 0;
4748
Willy Tarreau9cdde232007-05-02 20:58:19 +02004749 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004750 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004751
4752 /* Now we have the request line between cur_ptr and cur_end */
4753
4754 /* The annoying part is that pattern matching needs
4755 * that we modify the contents to null-terminate all
4756 * strings before testing them.
4757 */
4758
4759 term = *cur_end;
4760 *cur_end = '\0';
4761
4762 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4763 switch (exp->action) {
4764 case ACT_SETBE:
4765 /* It is not possible to jump a second time.
4766 * FIXME: should we return an HTTP/500 here so that
4767 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004768 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004769 if (t->be != t->fe)
4770 break;
4771
4772 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004773 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004774 done = 1;
4775 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004776
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004777 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004778 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004779 done = 1;
4780 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004781
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004782 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004783 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004784
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004785 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004786 if (t->listener->counters)
4787 t->listener->counters->denied_resp++;
4788
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004789 done = 1;
4790 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004791
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004792 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004793 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004794
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004795 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004796 if (t->listener->counters)
4797 t->listener->counters->denied_resp++;
4798
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004799 done = 1;
4800 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004801
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004802 case ACT_REPLACE:
4803 *cur_end = term; /* restore the string terminator */
4804 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4805 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4806 /* FIXME: if the user adds a newline in the replacement, the
4807 * index will not be recalculated for now, and the new line
4808 * will not be counted as a new header.
4809 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004810
Willy Tarreaufa355d42009-11-29 18:12:29 +01004811 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004812 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004813
Willy Tarreau9cdde232007-05-02 20:58:19 +02004814 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004815 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004816 HTTP_MSG_RQMETH,
4817 cur_ptr, cur_end + 1,
4818 NULL, NULL);
4819 if (unlikely(!cur_end))
4820 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004821
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004822 /* we have a full request and we know that we have either a CR
4823 * or an LF at <ptr>.
4824 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004825 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4826 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004827 /* there is no point trying this regex on headers */
4828 return 1;
4829 }
4830 }
4831 *cur_end = term; /* restore the string terminator */
4832 return done;
4833}
Willy Tarreau97de6242006-12-27 17:18:38 +01004834
Willy Tarreau58f10d72006-12-04 02:26:12 +01004835
Willy Tarreau58f10d72006-12-04 02:26:12 +01004836
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004837/*
4838 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4839 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004840 * unparsable request. Since it can manage the switch to another backend, it
4841 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004842 */
4843int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4844{
Willy Tarreau3d300592007-03-18 18:34:41 +01004845 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004846 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004847 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004848 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004849
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004850 /*
4851 * The interleaving of transformations and verdicts
4852 * makes it difficult to decide to continue or stop
4853 * the evaluation.
4854 */
4855
Willy Tarreau3d300592007-03-18 18:34:41 +01004856 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004857 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4858 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4859 exp = exp->next;
4860 continue;
4861 }
4862
4863 /* Apply the filter to the request line. */
4864 ret = apply_filter_to_req_line(t, req, exp);
4865 if (unlikely(ret < 0))
4866 return -1;
4867
4868 if (likely(ret == 0)) {
4869 /* The filter did not match the request, it can be
4870 * iterated through all headers.
4871 */
4872 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004873 }
4874 exp = exp->next;
4875 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004876 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004877}
4878
4879
Willy Tarreaua15645d2007-03-18 16:22:39 +01004880
Willy Tarreau58f10d72006-12-04 02:26:12 +01004881/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004882 * Try to retrieve the server associated to the appsession.
4883 * If the server is found, it's assigned to the session.
4884 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01004885void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004886 struct http_txn *txn = &t->txn;
4887 appsess *asession = NULL;
4888 char *sessid_temp = NULL;
4889
Cyril Bontéb21570a2009-11-29 20:04:48 +01004890 if (len > t->be->appsession_len) {
4891 len = t->be->appsession_len;
4892 }
4893
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004894 if (t->be->options2 & PR_O2_AS_REQL) {
4895 /* request-learn option is enabled : store the sessid in the session for future use */
4896 if (t->sessid != NULL) {
4897 /* free previously allocated memory as we don't need the session id found in the URL anymore */
4898 pool_free2(apools.sessid, t->sessid);
4899 }
4900
4901 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
4902 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4903 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4904 return;
4905 }
4906
Cyril Bontéb21570a2009-11-29 20:04:48 +01004907 memcpy(t->sessid, buf, len);
4908 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004909 }
4910
4911 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
4912 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4913 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4914 return;
4915 }
4916
Cyril Bontéb21570a2009-11-29 20:04:48 +01004917 memcpy(sessid_temp, buf, len);
4918 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004919
4920 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
4921 /* free previously allocated memory */
4922 pool_free2(apools.sessid, sessid_temp);
4923
4924 if (asession != NULL) {
4925 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
4926 if (!(t->be->options2 & PR_O2_AS_REQL))
4927 asession->request_count++;
4928
4929 if (asession->serverid != NULL) {
4930 struct server *srv = t->be->srv;
4931 while (srv) {
4932 if (strcmp(srv->id, asession->serverid) == 0) {
4933 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
4934 /* we found the server and it's usable */
4935 txn->flags &= ~TX_CK_MASK;
4936 txn->flags |= TX_CK_VALID;
4937 t->flags |= SN_DIRECT | SN_ASSIGNED;
4938 t->srv = srv;
4939 break;
4940 } else {
4941 txn->flags &= ~TX_CK_MASK;
4942 txn->flags |= TX_CK_DOWN;
4943 }
4944 }
4945 srv = srv->next;
4946 }
4947 }
4948 }
4949}
4950
4951/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004952 * Manage client-side cookie. It can impact performance by about 2% so it is
4953 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004954 */
4955void manage_client_side_cookies(struct session *t, struct buffer *req)
4956{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004957 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01004958 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004959 char *del_colon, *del_cookie, *colon;
4960 int app_cookies;
4961
Willy Tarreau58f10d72006-12-04 02:26:12 +01004962 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004963 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004964
Willy Tarreau2a324282006-12-05 00:05:46 +01004965 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004966 * we start with the start line.
4967 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004968 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004969 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004970
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004971 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004972 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004973 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004974
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004975 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004976 cur_ptr = cur_next;
4977 cur_end = cur_ptr + cur_hdr->len;
4978 cur_next = cur_end + cur_hdr->cr + 1;
4979
4980 /* We have one full header between cur_ptr and cur_end, and the
4981 * next header starts at cur_next. We're only interested in
4982 * "Cookie:" headers.
4983 */
4984
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004985 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4986 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004987 old_idx = cur_idx;
4988 continue;
4989 }
4990
4991 /* Now look for cookies. Conforming to RFC2109, we have to support
4992 * attributes whose name begin with a '$', and associate them with
4993 * the right cookie, if we want to delete this cookie.
4994 * So there are 3 cases for each cookie read :
4995 * 1) it's a special attribute, beginning with a '$' : ignore it.
4996 * 2) it's a server id cookie that we *MAY* want to delete : save
4997 * some pointers on it (last semi-colon, beginning of cookie...)
4998 * 3) it's an application cookie : we *MAY* have to delete a previous
4999 * "special" cookie.
5000 * At the end of loop, if a "special" cookie remains, we may have to
5001 * remove it. If no application cookie persists in the header, we
5002 * *MUST* delete it
5003 */
5004
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005005 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005006
Willy Tarreau58f10d72006-12-04 02:26:12 +01005007 /* del_cookie == NULL => nothing to be deleted */
5008 del_colon = del_cookie = NULL;
5009 app_cookies = 0;
5010
5011 while (p1 < cur_end) {
5012 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005013 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005014 while (p1 < cur_end) {
5015 if (*p1 == ';' || *p1 == ',')
5016 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005017 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005018 break;
5019 p1++;
5020 }
5021
5022 if (p1 == cur_end)
5023 break;
5024
5025 /* p1 is at the beginning of the cookie name */
5026 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005027 while (p2 < cur_end && *p2 != '=') {
5028 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5029 /* oops, the cookie name was truncated, resync */
5030 p1 = p2;
5031 goto resync_name;
5032 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005033 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005034 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005035
5036 if (p2 == cur_end)
5037 break;
5038
5039 p3 = p2 + 1; /* skips the '=' sign */
5040 if (p3 == cur_end)
5041 break;
5042
Willy Tarreau305ae852010-01-03 19:45:54 +01005043 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5044 p5 = p4 = p3;
5045 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5046 if (!isspace((unsigned char)*p5))
5047 p4 = p5 + 1;
5048 p5++;
5049 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005050
5051 /* here, we have the cookie name between p1 and p2,
5052 * and its value between p3 and p4.
5053 * we can process it :
5054 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005055 * Cookie: NAME=VALUE ;
5056 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005057 * | || || +--> p4
5058 * | || |+-------> p3
5059 * | || +--------> p2
5060 * | |+------------> p1
5061 * | +-------------> colon
5062 * +--------------------> cur_ptr
5063 */
5064
5065 if (*p1 == '$') {
5066 /* skip this one */
5067 }
5068 else {
5069 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005070 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005071 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005072 (p4 - p1 >= t->fe->capture_namelen) &&
5073 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005074 int log_len = p4 - p1;
5075
Willy Tarreau086b3b42007-05-13 21:45:51 +02005076 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005077 Alert("HTTP logging : out of memory.\n");
5078 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005079 if (log_len > t->fe->capture_len)
5080 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005081 memcpy(txn->cli_cookie, p1, log_len);
5082 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005083 }
5084 }
5085
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005086 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5087 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005088 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005089 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005090 char *delim;
5091
5092 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5093 * have the server ID betweek p3 and delim, and the original cookie between
5094 * delim+1 and p4. Otherwise, delim==p4 :
5095 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005096 * Cookie: NAME=SRV~VALUE ;
5097 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005098 * | || || | +--> p4
5099 * | || || +--------> delim
5100 * | || |+-----------> p3
5101 * | || +------------> p2
5102 * | |+----------------> p1
5103 * | +-----------------> colon
5104 * +------------------------> cur_ptr
5105 */
5106
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005107 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005108 for (delim = p3; delim < p4; delim++)
5109 if (*delim == COOKIE_DELIM)
5110 break;
5111 }
5112 else
5113 delim = p4;
5114
5115
5116 /* Here, we'll look for the first running server which supports the cookie.
5117 * This allows to share a same cookie between several servers, for example
5118 * to dedicate backup servers to specific servers only.
5119 * However, to prevent clients from sticking to cookie-less backup server
5120 * when they have incidentely learned an empty cookie, we simply ignore
5121 * empty cookies and mark them as invalid.
5122 */
5123 if (delim == p3)
5124 srv = NULL;
5125
5126 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005127 if (srv->cookie && (srv->cklen == delim - p3) &&
5128 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005129 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005130 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005131 txn->flags &= ~TX_CK_MASK;
5132 txn->flags |= TX_CK_VALID;
5133 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005134 t->srv = srv;
5135 break;
5136 } else {
5137 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005138 txn->flags &= ~TX_CK_MASK;
5139 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005140 }
5141 }
5142 srv = srv->next;
5143 }
5144
Willy Tarreau3d300592007-03-18 18:34:41 +01005145 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005146 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005147 txn->flags &= ~TX_CK_MASK;
5148 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005149 }
5150
5151 /* depending on the cookie mode, we may have to either :
5152 * - delete the complete cookie if we're in insert+indirect mode, so that
5153 * the server never sees it ;
5154 * - remove the server id from the cookie value, and tag the cookie as an
5155 * application cookie so that it does not get accidentely removed later,
5156 * if we're in cookie prefix mode
5157 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005158 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005159 int delta; /* negative */
5160
5161 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5162 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005163 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005164 cur_end += delta;
5165 cur_next += delta;
5166 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005167 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005168
5169 del_cookie = del_colon = NULL;
5170 app_cookies++; /* protect the header from deletion */
5171 }
5172 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005173 (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 +01005174 del_cookie = p1;
5175 del_colon = colon;
5176 }
5177 } else {
5178 /* now we know that we must keep this cookie since it's
5179 * not ours. But if we wanted to delete our cookie
5180 * earlier, we cannot remove the complete header, but we
5181 * can remove the previous block itself.
5182 */
5183 app_cookies++;
5184
5185 if (del_cookie != NULL) {
5186 int delta; /* negative */
5187
5188 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5189 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005190 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005191 cur_end += delta;
5192 cur_next += delta;
5193 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005194 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005195 del_cookie = del_colon = NULL;
5196 }
5197 }
5198
Cyril Bontéb21570a2009-11-29 20:04:48 +01005199 if (t->be->appsession_name != NULL) {
5200 int cmp_len, value_len;
5201 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005202
Cyril Bontéb21570a2009-11-29 20:04:48 +01005203 if (t->be->options2 & PR_O2_AS_PFX) {
5204 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5205 value_begin = p1 + t->be->appsession_name_len;
5206 value_len = p4 - p1 - t->be->appsession_name_len;
5207 } else {
5208 cmp_len = p2 - p1;
5209 value_begin = p3;
5210 value_len = p4 - p3;
5211 }
5212
5213 /* let's see if the cookie is our appcookie */
5214 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5215 /* Cool... it's the right one */
5216 manage_client_side_appsession(t, value_begin, value_len);
5217 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005218#if defined(DEBUG_HASH)
5219 Alert("manage_client_side_cookies\n");
5220 appsession_hash_dump(&(t->be->htbl_proxy));
5221#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005222 }/* end if ((t->proxy->appsession_name != NULL) ... */
5223 }
5224
5225 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005226 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005227 } /* while (p1 < cur_end) */
5228
5229 /* There's no more cookie on this line.
5230 * We may have marked the last one(s) for deletion.
5231 * We must do this now in two ways :
5232 * - if there is no app cookie, we simply delete the header ;
5233 * - if there are app cookies, we must delete the end of the
5234 * string properly, including the colon/semi-colon before
5235 * the cookie name.
5236 */
5237 if (del_cookie != NULL) {
5238 int delta;
5239 if (app_cookies) {
5240 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5241 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005242 cur_hdr->len += delta;
5243 } else {
5244 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005245
5246 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005247 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5248 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005249 cur_hdr->len = 0;
5250 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005251 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005252 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005253 }
5254
5255 /* keep the link from this header to next one */
5256 old_idx = cur_idx;
5257 } /* end of cookie processing on this header */
5258}
5259
5260
Willy Tarreaua15645d2007-03-18 16:22:39 +01005261/* Iterate the same filter through all response headers contained in <rtr>.
5262 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5263 */
5264int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5265{
5266 char term;
5267 char *cur_ptr, *cur_end, *cur_next;
5268 int cur_idx, old_idx, last_hdr;
5269 struct http_txn *txn = &t->txn;
5270 struct hdr_idx_elem *cur_hdr;
5271 int len, delta;
5272
5273 last_hdr = 0;
5274
5275 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5276 old_idx = 0;
5277
5278 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005279 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005280 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005281 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005282 (exp->action == ACT_ALLOW ||
5283 exp->action == ACT_DENY))
5284 return 0;
5285
5286 cur_idx = txn->hdr_idx.v[old_idx].next;
5287 if (!cur_idx)
5288 break;
5289
5290 cur_hdr = &txn->hdr_idx.v[cur_idx];
5291 cur_ptr = cur_next;
5292 cur_end = cur_ptr + cur_hdr->len;
5293 cur_next = cur_end + cur_hdr->cr + 1;
5294
5295 /* Now we have one header between cur_ptr and cur_end,
5296 * and the next header starts at cur_next.
5297 */
5298
5299 /* The annoying part is that pattern matching needs
5300 * that we modify the contents to null-terminate all
5301 * strings before testing them.
5302 */
5303
5304 term = *cur_end;
5305 *cur_end = '\0';
5306
5307 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5308 switch (exp->action) {
5309 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005310 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005311 last_hdr = 1;
5312 break;
5313
5314 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005315 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005316 last_hdr = 1;
5317 break;
5318
5319 case ACT_REPLACE:
5320 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5321 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5322 /* FIXME: if the user adds a newline in the replacement, the
5323 * index will not be recalculated for now, and the new line
5324 * will not be counted as a new header.
5325 */
5326
5327 cur_end += delta;
5328 cur_next += delta;
5329 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005330 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005331 break;
5332
5333 case ACT_REMOVE:
5334 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5335 cur_next += delta;
5336
Willy Tarreaufa355d42009-11-29 18:12:29 +01005337 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005338 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5339 txn->hdr_idx.used--;
5340 cur_hdr->len = 0;
5341 cur_end = NULL; /* null-term has been rewritten */
5342 break;
5343
5344 }
5345 }
5346 if (cur_end)
5347 *cur_end = term; /* restore the string terminator */
5348
5349 /* keep the link from this header to next one in case of later
5350 * removal of next header.
5351 */
5352 old_idx = cur_idx;
5353 }
5354 return 0;
5355}
5356
5357
5358/* Apply the filter to the status line in the response buffer <rtr>.
5359 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5360 * or -1 if a replacement resulted in an invalid status line.
5361 */
5362int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5363{
5364 char term;
5365 char *cur_ptr, *cur_end;
5366 int done;
5367 struct http_txn *txn = &t->txn;
5368 int len, delta;
5369
5370
Willy Tarreau3d300592007-03-18 18:34:41 +01005371 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005372 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005373 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005374 (exp->action == ACT_ALLOW ||
5375 exp->action == ACT_DENY))
5376 return 0;
5377 else if (exp->action == ACT_REMOVE)
5378 return 0;
5379
5380 done = 0;
5381
Willy Tarreau9cdde232007-05-02 20:58:19 +02005382 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005383 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5384
5385 /* Now we have the status line between cur_ptr and cur_end */
5386
5387 /* The annoying part is that pattern matching needs
5388 * that we modify the contents to null-terminate all
5389 * strings before testing them.
5390 */
5391
5392 term = *cur_end;
5393 *cur_end = '\0';
5394
5395 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5396 switch (exp->action) {
5397 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005398 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005399 done = 1;
5400 break;
5401
5402 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005403 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005404 done = 1;
5405 break;
5406
5407 case ACT_REPLACE:
5408 *cur_end = term; /* restore the string terminator */
5409 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5410 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5411 /* FIXME: if the user adds a newline in the replacement, the
5412 * index will not be recalculated for now, and the new line
5413 * will not be counted as a new header.
5414 */
5415
Willy Tarreaufa355d42009-11-29 18:12:29 +01005416 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005417 cur_end += delta;
5418
Willy Tarreau9cdde232007-05-02 20:58:19 +02005419 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005420 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005421 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005422 cur_ptr, cur_end + 1,
5423 NULL, NULL);
5424 if (unlikely(!cur_end))
5425 return -1;
5426
5427 /* we have a full respnse and we know that we have either a CR
5428 * or an LF at <ptr>.
5429 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005430 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005431 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5432 /* there is no point trying this regex on headers */
5433 return 1;
5434 }
5435 }
5436 *cur_end = term; /* restore the string terminator */
5437 return done;
5438}
5439
5440
5441
5442/*
5443 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5444 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5445 * unparsable response.
5446 */
5447int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5448{
Willy Tarreau3d300592007-03-18 18:34:41 +01005449 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005450 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005451 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005452 int ret;
5453
5454 /*
5455 * The interleaving of transformations and verdicts
5456 * makes it difficult to decide to continue or stop
5457 * the evaluation.
5458 */
5459
Willy Tarreau3d300592007-03-18 18:34:41 +01005460 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005461 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5462 exp->action == ACT_PASS)) {
5463 exp = exp->next;
5464 continue;
5465 }
5466
5467 /* Apply the filter to the status line. */
5468 ret = apply_filter_to_sts_line(t, rtr, exp);
5469 if (unlikely(ret < 0))
5470 return -1;
5471
5472 if (likely(ret == 0)) {
5473 /* The filter did not match the response, it can be
5474 * iterated through all headers.
5475 */
5476 apply_filter_to_resp_headers(t, rtr, exp);
5477 }
5478 exp = exp->next;
5479 }
5480 return 0;
5481}
5482
5483
5484
5485/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005486 * Manage server-side cookies. It can impact performance by about 2% so it is
5487 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005488 */
5489void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5490{
5491 struct http_txn *txn = &t->txn;
5492 char *p1, *p2, *p3, *p4;
5493
Willy Tarreaua15645d2007-03-18 16:22:39 +01005494 char *cur_ptr, *cur_end, *cur_next;
5495 int cur_idx, old_idx, delta;
5496
Willy Tarreaua15645d2007-03-18 16:22:39 +01005497 /* Iterate through the headers.
5498 * we start with the start line.
5499 */
5500 old_idx = 0;
5501 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5502
5503 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5504 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005505 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005506
5507 cur_hdr = &txn->hdr_idx.v[cur_idx];
5508 cur_ptr = cur_next;
5509 cur_end = cur_ptr + cur_hdr->len;
5510 cur_next = cur_end + cur_hdr->cr + 1;
5511
5512 /* We have one full header between cur_ptr and cur_end, and the
5513 * next header starts at cur_next. We're only interested in
5514 * "Cookie:" headers.
5515 */
5516
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005517 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5518 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005519 old_idx = cur_idx;
5520 continue;
5521 }
5522
5523 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005524 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005525
5526
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005527 /* maybe we only wanted to see if there was a set-cookie. Note that
5528 * the cookie capture is declared in the fronend.
5529 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005530 if (t->be->cookie_name == NULL &&
5531 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005532 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005533 return;
5534
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005535 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005536
5537 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005538 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5539 break;
5540
5541 /* p1 is at the beginning of the cookie name */
5542 p2 = p1;
5543
5544 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5545 p2++;
5546
5547 if (p2 == cur_end || *p2 == ';') /* next cookie */
5548 break;
5549
5550 p3 = p2 + 1; /* skip the '=' sign */
5551 if (p3 == cur_end)
5552 break;
5553
5554 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005555 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005556 p4++;
5557
5558 /* here, we have the cookie name between p1 and p2,
5559 * and its value between p3 and p4.
5560 * we can process it.
5561 */
5562
5563 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005564 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005565 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005566 (p4 - p1 >= t->fe->capture_namelen) &&
5567 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005568 int log_len = p4 - p1;
5569
Willy Tarreau086b3b42007-05-13 21:45:51 +02005570 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005571 Alert("HTTP logging : out of memory.\n");
5572 }
5573
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005574 if (log_len > t->fe->capture_len)
5575 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005576 memcpy(txn->srv_cookie, p1, log_len);
5577 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005578 }
5579
5580 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005581 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5582 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005583 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005584 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005585
5586 /* If the cookie is in insert mode on a known server, we'll delete
5587 * this occurrence because we'll insert another one later.
5588 * We'll delete it too if the "indirect" option is set and we're in
5589 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005590 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5591 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005592 /* this header must be deleted */
5593 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5594 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5595 txn->hdr_idx.used--;
5596 cur_hdr->len = 0;
5597 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005598 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005599
Willy Tarreau3d300592007-03-18 18:34:41 +01005600 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005601 }
5602 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005603 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005604 /* replace bytes p3->p4 with the cookie name associated
5605 * with this server since we know it.
5606 */
5607 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5608 cur_hdr->len += delta;
5609 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005610 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005611
Willy Tarreau3d300592007-03-18 18:34:41 +01005612 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005613 }
5614 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005615 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005616 /* insert the cookie name associated with this server
5617 * before existing cookie, and insert a delimitor between them..
5618 */
5619 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5620 cur_hdr->len += delta;
5621 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005622 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005623
5624 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005625 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005626 }
5627 }
5628 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005629 else if (t->be->appsession_name != NULL) {
5630 int cmp_len, value_len;
5631 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005632
Cyril Bontéb21570a2009-11-29 20:04:48 +01005633 if (t->be->options2 & PR_O2_AS_PFX) {
5634 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5635 value_begin = p1 + t->be->appsession_name_len;
5636 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5637 } else {
5638 cmp_len = p2 - p1;
5639 value_begin = p3;
5640 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005641 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005642
5643 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5644 /* Cool... it's the right one */
5645 if (t->sessid != NULL) {
5646 /* free previously allocated memory as we don't need it anymore */
5647 pool_free2(apools.sessid, t->sessid);
5648 }
5649 /* Store the sessid in the session for future use */
5650 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5651 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5652 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5653 return;
5654 }
5655 memcpy(t->sessid, value_begin, value_len);
5656 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005657 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005658 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005659 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5660 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005661 /* keep the link from this header to next one */
5662 old_idx = cur_idx;
5663 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005664
5665 if (t->sessid != NULL) {
5666 appsess *asession = NULL;
5667 /* only do insert, if lookup fails */
5668 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5669 if (asession == NULL) {
5670 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5671 Alert("Not enough Memory process_srv():asession:calloc().\n");
5672 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5673 return;
5674 }
5675 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5676 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5677 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5678 return;
5679 }
5680 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5681 asession->sessid[t->be->appsession_len] = 0;
5682
5683 size_t server_id_len = strlen(t->srv->id) + 1;
5684 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5685 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5686 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5687 return;
5688 }
5689 asession->serverid[0] = '\0';
5690 memcpy(asession->serverid, t->srv->id, server_id_len);
5691
5692 asession->request_count = 0;
5693 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5694 }
5695
5696 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5697 asession->request_count++;
5698 }
5699
5700#if defined(DEBUG_HASH)
5701 Alert("manage_server_side_cookies\n");
5702 appsession_hash_dump(&(t->be->htbl_proxy));
5703#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005704}
5705
5706
5707
5708/*
5709 * Check if response is cacheable or not. Updates t->flags.
5710 */
5711void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5712{
5713 struct http_txn *txn = &t->txn;
5714 char *p1, *p2;
5715
5716 char *cur_ptr, *cur_end, *cur_next;
5717 int cur_idx;
5718
Willy Tarreau5df51872007-11-25 16:20:08 +01005719 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005720 return;
5721
5722 /* Iterate through the headers.
5723 * we start with the start line.
5724 */
5725 cur_idx = 0;
5726 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5727
5728 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5729 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005730 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005731
5732 cur_hdr = &txn->hdr_idx.v[cur_idx];
5733 cur_ptr = cur_next;
5734 cur_end = cur_ptr + cur_hdr->len;
5735 cur_next = cur_end + cur_hdr->cr + 1;
5736
5737 /* We have one full header between cur_ptr and cur_end, and the
5738 * next header starts at cur_next. We're only interested in
5739 * "Cookie:" headers.
5740 */
5741
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005742 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5743 if (val) {
5744 if ((cur_end - (cur_ptr + val) >= 8) &&
5745 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5746 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5747 return;
5748 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005749 }
5750
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005751 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5752 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005753 continue;
5754
5755 /* OK, right now we know we have a cache-control header at cur_ptr */
5756
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005757 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005758
5759 if (p1 >= cur_end) /* no more info */
5760 continue;
5761
5762 /* p1 is at the beginning of the value */
5763 p2 = p1;
5764
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005765 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005766 p2++;
5767
5768 /* we have a complete value between p1 and p2 */
5769 if (p2 < cur_end && *p2 == '=') {
5770 /* we have something of the form no-cache="set-cookie" */
5771 if ((cur_end - p1 >= 21) &&
5772 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5773 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005774 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005775 continue;
5776 }
5777
5778 /* OK, so we know that either p2 points to the end of string or to a comma */
5779 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5780 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5781 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5782 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005783 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005784 return;
5785 }
5786
5787 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005788 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005789 continue;
5790 }
5791 }
5792}
5793
5794
Willy Tarreau58f10d72006-12-04 02:26:12 +01005795/*
5796 * Try to retrieve a known appsession in the URI, then the associated server.
5797 * If the server is found, it's assigned to the session.
5798 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005799void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005800{
Cyril Bontéb21570a2009-11-29 20:04:48 +01005801 char *end_params, *first_param, *cur_param, *next_param;
5802 char separator;
5803 int value_len;
5804
5805 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005806
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005807 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01005808 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005809 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005810 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005811
Cyril Bontéb21570a2009-11-29 20:04:48 +01005812 first_param = NULL;
5813 switch (mode) {
5814 case PR_O2_AS_M_PP:
5815 first_param = memchr(begin, ';', len);
5816 break;
5817 case PR_O2_AS_M_QS:
5818 first_param = memchr(begin, '?', len);
5819 break;
5820 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005821
Cyril Bontéb21570a2009-11-29 20:04:48 +01005822 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005823 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005824 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005825
Cyril Bontéb21570a2009-11-29 20:04:48 +01005826 switch (mode) {
5827 case PR_O2_AS_M_PP:
5828 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
5829 end_params = (char *) begin + len;
5830 }
5831 separator = ';';
5832 break;
5833 case PR_O2_AS_M_QS:
5834 end_params = (char *) begin + len;
5835 separator = '&';
5836 break;
5837 default:
5838 /* unknown mode, shouldn't happen */
5839 return;
5840 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005841
Cyril Bontéb21570a2009-11-29 20:04:48 +01005842 cur_param = next_param = end_params;
5843 while (cur_param > first_param) {
5844 cur_param--;
5845 if ((cur_param[0] == separator) || (cur_param == first_param)) {
5846 /* let's see if this is the appsession parameter */
5847 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
5848 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
5849 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
5850 /* Cool... it's the right one */
5851 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
5852 value_len = MIN(t->be->appsession_len, next_param - cur_param);
5853 if (value_len > 0) {
5854 manage_client_side_appsession(t, cur_param, value_len);
5855 }
5856 break;
5857 }
5858 next_param = cur_param;
5859 }
5860 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005861#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005862 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005863 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005864#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005865}
5866
5867
Willy Tarreaub2513902006-12-17 14:52:38 +01005868/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005869 * In a GET or HEAD request, check if the requested URI matches the stats uri
5870 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005871 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005872 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005873 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005874 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005875 *
5876 * Returns 1 if the session's state changes, otherwise 0.
5877 */
5878int stats_check_uri_auth(struct session *t, struct proxy *backend)
5879{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005880 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005881 struct uri_auth *uri_auth = backend->uri_auth;
5882 struct user_auth *user;
5883 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005884 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005885
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005886 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5887
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005888 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005889 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005890 return 0;
5891
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005892 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005893
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005894 /* the URI is in h */
5895 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005896 return 0;
5897
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005898 h += uri_auth->uri_len;
5899 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5900 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005901 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005902 break;
5903 }
5904 h++;
5905 }
5906
5907 if (uri_auth->refresh) {
5908 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5909 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5910 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005911 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005912 break;
5913 }
5914 h++;
5915 }
5916 }
5917
Willy Tarreau55bb8452007-10-17 18:44:57 +02005918 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5919 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5920 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005921 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005922 break;
5923 }
5924 h++;
5925 }
5926
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005927 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5928
Willy Tarreaub2513902006-12-17 14:52:38 +01005929 /* we are in front of a interceptable URI. Let's check
5930 * if there's an authentication and if it's valid.
5931 */
5932 user = uri_auth->users;
5933 if (!user) {
5934 /* no user auth required, it's OK */
5935 authenticated = 1;
5936 } else {
5937 authenticated = 0;
5938
5939 /* a user list is defined, we have to check.
5940 * skip 21 chars for "Authorization: Basic ".
5941 */
5942
5943 /* FIXME: this should move to an earlier place */
5944 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005945 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5946 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5947 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005948 if (len > 14 &&
5949 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02005950 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01005951 break;
5952 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005953 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005954 }
5955
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005956 if (txn->auth_hdr.len < 21 ||
5957 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005958 user = NULL;
5959
5960 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005961 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5962 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005963 user->user_pwd, user->user_len)) {
5964 authenticated = 1;
5965 break;
5966 }
5967 user = user->next;
5968 }
5969 }
5970
5971 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005972 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005973
5974 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005975 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
5976 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005977 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01005978 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005979 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005980 if (!(t->flags & SN_ERR_MASK))
5981 t->flags |= SN_ERR_PRXCOND;
5982 if (!(t->flags & SN_FINST_MASK))
5983 t->flags |= SN_FINST_R;
5984 return 1;
5985 }
5986
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005987 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005988 * data.
5989 */
Willy Tarreau70089872008-06-13 21:12:51 +02005990 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005991 t->data_source = DATA_SRC_STATS;
5992 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005993 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005994 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
5995 t->rep->prod->private = t;
5996 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005997 return 1;
5998}
5999
Willy Tarreau4076a152009-04-02 15:18:36 +02006000/*
6001 * Capture a bad request or response and archive it in the proxy's structure.
6002 */
6003void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6004 struct buffer *buf, struct http_msg *msg,
6005 struct proxy *other_end)
6006{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006007 es->len = buf->r - (buf->data + msg->som);
6008 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006009 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006010 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006011 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006012 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006013 es->when = date; // user-visible date
6014 es->sid = s->uniq_id;
6015 es->srv = s->srv;
6016 es->oe = other_end;
6017 es->src = s->cli_addr;
6018}
Willy Tarreaub2513902006-12-17 14:52:38 +01006019
Willy Tarreaubaaee002006-06-26 02:48:02 +02006020/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006021 * Print a debug line with a header
6022 */
6023void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6024{
6025 int len, max;
6026 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006027 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006028 max = end - start;
6029 UBOUND(max, sizeof(trash) - len - 1);
6030 len += strlcpy2(trash + len, start, max + 1);
6031 trash[len++] = '\n';
6032 write(1, trash, len);
6033}
6034
Willy Tarreau0937bc42009-12-22 15:03:09 +01006035/*
6036 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6037 * the required fields are properly allocated and that we only need to (re)init
6038 * them. This should be used before processing any new request.
6039 */
6040void http_init_txn(struct session *s)
6041{
6042 struct http_txn *txn = &s->txn;
6043 struct proxy *fe = s->fe;
6044
6045 txn->flags = 0;
6046 txn->status = -1;
6047
6048 txn->req.sol = txn->req.eol = NULL;
6049 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6050 txn->rsp.sol = txn->rsp.eol = NULL;
6051 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6052 txn->req.hdr_content_len = 0LL;
6053 txn->rsp.hdr_content_len = 0LL;
6054 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6055 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6056 chunk_reset(&txn->auth_hdr);
6057
6058 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6059 if (fe->options2 & PR_O2_REQBUG_OK)
6060 txn->req.err_pos = -1; /* let buggy requests pass */
6061
6062 if (txn->req.cap)
6063 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6064
6065 if (txn->rsp.cap)
6066 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6067
6068 if (txn->hdr_idx.v)
6069 hdr_idx_init(&txn->hdr_idx);
6070}
6071
6072/* to be used at the end of a transaction */
6073void http_end_txn(struct session *s)
6074{
6075 struct http_txn *txn = &s->txn;
6076
6077 /* these ones will have been dynamically allocated */
6078 pool_free2(pool2_requri, txn->uri);
6079 pool_free2(pool2_capture, txn->cli_cookie);
6080 pool_free2(pool2_capture, txn->srv_cookie);
6081 txn->uri = NULL;
6082 txn->srv_cookie = NULL;
6083 txn->cli_cookie = NULL;
6084}
6085
6086/* to be used at the end of a transaction to prepare a new one */
6087void http_reset_txn(struct session *s)
6088{
6089 http_end_txn(s);
6090 http_init_txn(s);
6091
6092 s->be = s->fe;
6093 s->req->analysers = s->listener->analysers;
6094 s->logs.logwait = s->fe->to_log;
6095 s->srv = s->prev_srv = s->srv_conn = NULL;
6096 s->pend_pos = NULL;
6097 s->conn_retries = s->be->conn_retries;
6098
6099 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6100
6101 s->req->rto = s->fe->timeout.client;
6102 s->req->wto = s->be->timeout.server;
6103 s->req->cto = s->be->timeout.connect;
6104
6105 s->rep->rto = s->be->timeout.server;
6106 s->rep->wto = s->fe->timeout.client;
6107 s->rep->cto = TICK_ETERNITY;
6108
6109 s->req->rex = TICK_ETERNITY;
6110 s->req->wex = TICK_ETERNITY;
6111 s->req->analyse_exp = TICK_ETERNITY;
6112 s->rep->rex = TICK_ETERNITY;
6113 s->rep->wex = TICK_ETERNITY;
6114 s->rep->analyse_exp = TICK_ETERNITY;
6115}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006116
Willy Tarreau8797c062007-05-07 00:55:35 +02006117/************************************************************************/
6118/* The code below is dedicated to ACL parsing and matching */
6119/************************************************************************/
6120
6121
6122
6123
6124/* 1. Check on METHOD
6125 * We use the pre-parsed method if it is known, and store its number as an
6126 * integer. If it is unknown, we use the pointer and the length.
6127 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006128static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006129{
6130 int len, meth;
6131
Willy Tarreauae8b7962007-06-09 23:10:04 +02006132 len = strlen(*text);
6133 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006134
6135 pattern->val.i = meth;
6136 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006137 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006138 if (!pattern->ptr.str)
6139 return 0;
6140 pattern->len = len;
6141 }
6142 return 1;
6143}
6144
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006145static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006146acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6147 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006148{
6149 int meth;
6150 struct http_txn *txn = l7;
6151
Willy Tarreaub6866442008-07-14 23:54:42 +02006152 if (!txn)
6153 return 0;
6154
Willy Tarreau655dce92009-11-08 13:10:58 +01006155 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006156 return 0;
6157
Willy Tarreau8797c062007-05-07 00:55:35 +02006158 meth = txn->meth;
6159 test->i = meth;
6160 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006161 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6162 /* ensure the indexes are not affected */
6163 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006164 test->len = txn->req.sl.rq.m_l;
6165 test->ptr = txn->req.sol;
6166 }
6167 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6168 return 1;
6169}
6170
6171static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6172{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006173 int icase;
6174
Willy Tarreau8797c062007-05-07 00:55:35 +02006175 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006176 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006177
6178 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006179 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006180
6181 /* Other method, we must compare the strings */
6182 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006183 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006184
6185 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6186 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6187 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006188 return ACL_PAT_FAIL;
6189 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006190}
6191
6192/* 2. Check on Request/Status Version
6193 * We simply compare strings here.
6194 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006195static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006196{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006197 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006198 if (!pattern->ptr.str)
6199 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006200 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006201 return 1;
6202}
6203
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006204static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006205acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6206 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006207{
6208 struct http_txn *txn = l7;
6209 char *ptr;
6210 int len;
6211
Willy Tarreaub6866442008-07-14 23:54:42 +02006212 if (!txn)
6213 return 0;
6214
Willy Tarreau655dce92009-11-08 13:10:58 +01006215 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006216 return 0;
6217
Willy Tarreau8797c062007-05-07 00:55:35 +02006218 len = txn->req.sl.rq.v_l;
6219 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
6220
6221 while ((len-- > 0) && (*ptr++ != '/'));
6222 if (len <= 0)
6223 return 0;
6224
6225 test->ptr = ptr;
6226 test->len = len;
6227
6228 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6229 return 1;
6230}
6231
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006232static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006233acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6234 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006235{
6236 struct http_txn *txn = l7;
6237 char *ptr;
6238 int len;
6239
Willy Tarreaub6866442008-07-14 23:54:42 +02006240 if (!txn)
6241 return 0;
6242
Willy Tarreau655dce92009-11-08 13:10:58 +01006243 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006244 return 0;
6245
Willy Tarreau8797c062007-05-07 00:55:35 +02006246 len = txn->rsp.sl.st.v_l;
6247 ptr = txn->rsp.sol;
6248
6249 while ((len-- > 0) && (*ptr++ != '/'));
6250 if (len <= 0)
6251 return 0;
6252
6253 test->ptr = ptr;
6254 test->len = len;
6255
6256 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6257 return 1;
6258}
6259
6260/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006261static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006262acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6263 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006264{
6265 struct http_txn *txn = l7;
6266 char *ptr;
6267 int len;
6268
Willy Tarreaub6866442008-07-14 23:54:42 +02006269 if (!txn)
6270 return 0;
6271
Willy Tarreau655dce92009-11-08 13:10:58 +01006272 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006273 return 0;
6274
Willy Tarreau8797c062007-05-07 00:55:35 +02006275 len = txn->rsp.sl.st.c_l;
6276 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6277
6278 test->i = __strl2ui(ptr, len);
6279 test->flags = ACL_TEST_F_VOL_1ST;
6280 return 1;
6281}
6282
6283/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006284static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006285acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6286 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006287{
6288 struct http_txn *txn = l7;
6289
Willy Tarreaub6866442008-07-14 23:54:42 +02006290 if (!txn)
6291 return 0;
6292
Willy Tarreau655dce92009-11-08 13:10:58 +01006293 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006294 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006295
Willy Tarreauc11416f2007-06-17 16:58:38 +02006296 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6297 /* ensure the indexes are not affected */
6298 return 0;
6299
Willy Tarreau8797c062007-05-07 00:55:35 +02006300 test->len = txn->req.sl.rq.u_l;
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006301 test->ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006302
Willy Tarreauf3d25982007-05-08 22:45:09 +02006303 /* we do not need to set READ_ONLY because the data is in a buffer */
6304 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006305 return 1;
6306}
6307
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006308static int
6309acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6310 struct acl_expr *expr, struct acl_test *test)
6311{
6312 struct http_txn *txn = l7;
6313
Willy Tarreaub6866442008-07-14 23:54:42 +02006314 if (!txn)
6315 return 0;
6316
Willy Tarreau655dce92009-11-08 13:10:58 +01006317 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006318 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006319
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006320 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6321 /* ensure the indexes are not affected */
6322 return 0;
6323
6324 /* Parse HTTP request */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006325 url2sa(txn->req.sol - txn->req.som + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006326 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6327 test->i = AF_INET;
6328
6329 /*
6330 * If we are parsing url in frontend space, we prepare backend stage
6331 * to not parse again the same url ! optimization lazyness...
6332 */
6333 if (px->options & PR_O_HTTP_PROXY)
6334 l4->flags |= SN_ADDR_SET;
6335
6336 test->flags = ACL_TEST_F_READ_ONLY;
6337 return 1;
6338}
6339
6340static int
6341acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6342 struct acl_expr *expr, struct acl_test *test)
6343{
6344 struct http_txn *txn = l7;
6345
Willy Tarreaub6866442008-07-14 23:54:42 +02006346 if (!txn)
6347 return 0;
6348
Willy Tarreau655dce92009-11-08 13:10:58 +01006349 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006350 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006351
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006352 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6353 /* ensure the indexes are not affected */
6354 return 0;
6355
6356 /* Same optimization as url_ip */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006357 url2sa(txn->req.sol - txn->req.som + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006358 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6359
6360 if (px->options & PR_O_HTTP_PROXY)
6361 l4->flags |= SN_ADDR_SET;
6362
6363 test->flags = ACL_TEST_F_READ_ONLY;
6364 return 1;
6365}
6366
Willy Tarreauc11416f2007-06-17 16:58:38 +02006367/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6368 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6369 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006370static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006371acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006372 struct acl_expr *expr, struct acl_test *test)
6373{
6374 struct http_txn *txn = l7;
6375 struct hdr_idx *idx = &txn->hdr_idx;
6376 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006377
Willy Tarreaub6866442008-07-14 23:54:42 +02006378 if (!txn)
6379 return 0;
6380
Willy Tarreau33a7e692007-06-10 19:45:56 +02006381 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6382 /* search for header from the beginning */
6383 ctx->idx = 0;
6384
Willy Tarreau33a7e692007-06-10 19:45:56 +02006385 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6386 test->flags |= ACL_TEST_F_FETCH_MORE;
6387 test->flags |= ACL_TEST_F_VOL_HDR;
6388 test->len = ctx->vlen;
6389 test->ptr = (char *)ctx->line + ctx->val;
6390 return 1;
6391 }
6392
6393 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6394 test->flags |= ACL_TEST_F_VOL_HDR;
6395 return 0;
6396}
6397
Willy Tarreau33a7e692007-06-10 19:45:56 +02006398static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006399acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6400 struct acl_expr *expr, struct acl_test *test)
6401{
6402 struct http_txn *txn = l7;
6403
Willy Tarreaub6866442008-07-14 23:54:42 +02006404 if (!txn)
6405 return 0;
6406
Willy Tarreau655dce92009-11-08 13:10:58 +01006407 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006408 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006409
Willy Tarreauc11416f2007-06-17 16:58:38 +02006410 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6411 /* ensure the indexes are not affected */
6412 return 0;
6413
6414 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6415}
6416
6417static int
6418acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6419 struct acl_expr *expr, struct acl_test *test)
6420{
6421 struct http_txn *txn = l7;
6422
Willy Tarreaub6866442008-07-14 23:54:42 +02006423 if (!txn)
6424 return 0;
6425
Willy Tarreau655dce92009-11-08 13:10:58 +01006426 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006427 return 0;
6428
6429 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6430}
6431
6432/* 6. Check on HTTP header count. The number of occurrences is returned.
6433 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6434 */
6435static int
6436acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006437 struct acl_expr *expr, struct acl_test *test)
6438{
6439 struct http_txn *txn = l7;
6440 struct hdr_idx *idx = &txn->hdr_idx;
6441 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006442 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006443
Willy Tarreaub6866442008-07-14 23:54:42 +02006444 if (!txn)
6445 return 0;
6446
Willy Tarreau33a7e692007-06-10 19:45:56 +02006447 ctx.idx = 0;
6448 cnt = 0;
6449 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6450 cnt++;
6451
6452 test->i = cnt;
6453 test->flags = ACL_TEST_F_VOL_HDR;
6454 return 1;
6455}
6456
Willy Tarreauc11416f2007-06-17 16:58:38 +02006457static int
6458acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6459 struct acl_expr *expr, struct acl_test *test)
6460{
6461 struct http_txn *txn = l7;
6462
Willy Tarreaub6866442008-07-14 23:54:42 +02006463 if (!txn)
6464 return 0;
6465
Willy Tarreau655dce92009-11-08 13:10:58 +01006466 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006467 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006468
Willy Tarreauc11416f2007-06-17 16:58:38 +02006469 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6470 /* ensure the indexes are not affected */
6471 return 0;
6472
6473 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6474}
6475
6476static int
6477acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6478 struct acl_expr *expr, struct acl_test *test)
6479{
6480 struct http_txn *txn = l7;
6481
Willy Tarreaub6866442008-07-14 23:54:42 +02006482 if (!txn)
6483 return 0;
6484
Willy Tarreau655dce92009-11-08 13:10:58 +01006485 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006486 return 0;
6487
6488 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6489}
6490
Willy Tarreau33a7e692007-06-10 19:45:56 +02006491/* 7. Check on HTTP header's integer value. The integer value is returned.
6492 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006493 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006494 */
6495static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006496acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006497 struct acl_expr *expr, struct acl_test *test)
6498{
6499 struct http_txn *txn = l7;
6500 struct hdr_idx *idx = &txn->hdr_idx;
6501 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006502
Willy Tarreaub6866442008-07-14 23:54:42 +02006503 if (!txn)
6504 return 0;
6505
Willy Tarreau33a7e692007-06-10 19:45:56 +02006506 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6507 /* search for header from the beginning */
6508 ctx->idx = 0;
6509
Willy Tarreau33a7e692007-06-10 19:45:56 +02006510 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6511 test->flags |= ACL_TEST_F_FETCH_MORE;
6512 test->flags |= ACL_TEST_F_VOL_HDR;
6513 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6514 return 1;
6515 }
6516
6517 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6518 test->flags |= ACL_TEST_F_VOL_HDR;
6519 return 0;
6520}
6521
Willy Tarreauc11416f2007-06-17 16:58:38 +02006522static int
6523acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6524 struct acl_expr *expr, struct acl_test *test)
6525{
6526 struct http_txn *txn = l7;
6527
Willy Tarreaub6866442008-07-14 23:54:42 +02006528 if (!txn)
6529 return 0;
6530
Willy Tarreau655dce92009-11-08 13:10:58 +01006531 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006532 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006533
Willy Tarreauc11416f2007-06-17 16:58:38 +02006534 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6535 /* ensure the indexes are not affected */
6536 return 0;
6537
6538 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6539}
6540
6541static int
6542acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6543 struct acl_expr *expr, struct acl_test *test)
6544{
6545 struct http_txn *txn = l7;
6546
Willy Tarreaub6866442008-07-14 23:54:42 +02006547 if (!txn)
6548 return 0;
6549
Willy Tarreau655dce92009-11-08 13:10:58 +01006550 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006551 return 0;
6552
6553 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6554}
6555
Willy Tarreau106f9792009-09-19 07:54:16 +02006556/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6557 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6558 */
6559static int
6560acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6561 struct acl_expr *expr, struct acl_test *test)
6562{
6563 struct http_txn *txn = l7;
6564 struct hdr_idx *idx = &txn->hdr_idx;
6565 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6566
6567 if (!txn)
6568 return 0;
6569
6570 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6571 /* search for header from the beginning */
6572 ctx->idx = 0;
6573
6574 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6575 test->flags |= ACL_TEST_F_FETCH_MORE;
6576 test->flags |= ACL_TEST_F_VOL_HDR;
6577 /* Same optimization as url_ip */
6578 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6579 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6580 test->ptr = (void *)&l4->srv_addr.sin_addr;
6581 test->i = AF_INET;
6582 return 1;
6583 }
6584
6585 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6586 test->flags |= ACL_TEST_F_VOL_HDR;
6587 return 0;
6588}
6589
6590static int
6591acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6592 struct acl_expr *expr, struct acl_test *test)
6593{
6594 struct http_txn *txn = l7;
6595
6596 if (!txn)
6597 return 0;
6598
Willy Tarreau655dce92009-11-08 13:10:58 +01006599 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006600 return 0;
6601
6602 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6603 /* ensure the indexes are not affected */
6604 return 0;
6605
6606 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6607}
6608
6609static int
6610acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6611 struct acl_expr *expr, struct acl_test *test)
6612{
6613 struct http_txn *txn = l7;
6614
6615 if (!txn)
6616 return 0;
6617
Willy Tarreau655dce92009-11-08 13:10:58 +01006618 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006619 return 0;
6620
6621 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6622}
6623
Willy Tarreau737b0c12007-06-10 21:28:46 +02006624/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6625 * the first '/' after the possible hostname, and ends before the possible '?'.
6626 */
6627static int
6628acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6629 struct acl_expr *expr, struct acl_test *test)
6630{
6631 struct http_txn *txn = l7;
6632 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006633
Willy Tarreaub6866442008-07-14 23:54:42 +02006634 if (!txn)
6635 return 0;
6636
Willy Tarreau655dce92009-11-08 13:10:58 +01006637 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006638 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006639
Willy Tarreauc11416f2007-06-17 16:58:38 +02006640 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6641 /* ensure the indexes are not affected */
6642 return 0;
6643
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006644 end = txn->req.sol - txn->req.som + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006645 ptr = http_get_path(txn);
6646 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006647 return 0;
6648
6649 /* OK, we got the '/' ! */
6650 test->ptr = ptr;
6651
6652 while (ptr < end && *ptr != '?')
6653 ptr++;
6654
6655 test->len = ptr - test->ptr;
6656
6657 /* we do not need to set READ_ONLY because the data is in a buffer */
6658 test->flags = ACL_TEST_F_VOL_1ST;
6659 return 1;
6660}
6661
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006662static int
6663acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6664 struct acl_expr *expr, struct acl_test *test)
6665{
6666 struct buffer *req = s->req;
6667 struct http_txn *txn = &s->txn;
6668 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006669
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006670 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6671 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6672 */
6673
6674 if (!s || !req)
6675 return 0;
6676
Willy Tarreau655dce92009-11-08 13:10:58 +01006677 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006678 /* Already decoded as OK */
6679 test->flags |= ACL_TEST_F_SET_RES_PASS;
6680 return 1;
6681 }
6682
6683 /* Try to decode HTTP request */
6684 if (likely(req->lr < req->r))
6685 http_msg_analyzer(req, msg, &txn->hdr_idx);
6686
Willy Tarreau655dce92009-11-08 13:10:58 +01006687 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006688 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6689 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6690 return 1;
6691 }
6692 /* wait for final state */
6693 test->flags |= ACL_TEST_F_MAY_CHANGE;
6694 return 0;
6695 }
6696
6697 /* OK we got a valid HTTP request. We have some minor preparation to
6698 * perform so that further checks can rely on HTTP tests.
6699 */
6700 msg->sol = req->data + msg->som;
6701 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
6702 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6703 s->flags |= SN_REDIRECTABLE;
6704
6705 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
6706 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6707 return 1;
6708 }
6709
6710 test->flags |= ACL_TEST_F_SET_RES_PASS;
6711 return 1;
6712}
6713
Willy Tarreau8797c062007-05-07 00:55:35 +02006714
6715/************************************************************************/
6716/* All supported keywords must be declared here. */
6717/************************************************************************/
6718
6719/* Note: must not be declared <const> as its list will be overwritten */
6720static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006721 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
6722
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006723 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6724 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6725 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6726 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006727
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006728 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6729 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6730 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6731 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6732 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6733 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6734 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6735 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
6736 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02006737
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006738 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
6739 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6740 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6741 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6742 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6743 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6744 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6745 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6746 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
6747 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006748 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02006749
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006750 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6751 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
6752 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
6753 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
6754 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
6755 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
6756 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
6757 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
6758 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006759 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006760
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006761 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6762 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6763 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6764 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6765 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6766 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6767 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006768
Willy Tarreauf3d25982007-05-08 22:45:09 +02006769 { NULL, NULL, NULL, NULL },
6770
6771#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02006772 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
6773 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
6774 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
6775 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
6776 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
6777 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
6778 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
6779
Willy Tarreau8797c062007-05-07 00:55:35 +02006780 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
6781 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
6782 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
6783 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
6784 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
6785 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
6786 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
6787 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
6788
6789 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
6790 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
6791 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
6792 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
6793 { NULL, NULL, NULL, NULL },
6794#endif
6795}};
6796
6797
6798__attribute__((constructor))
6799static void __http_protocol_init(void)
6800{
6801 acl_register_keywords(&acl_kws);
6802}
6803
6804
Willy Tarreau58f10d72006-12-04 02:26:12 +01006805/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006806 * Local variables:
6807 * c-indent-level: 8
6808 * c-basic-offset: 8
6809 * End:
6810 */