blob: 6acb0b7b284ad4ca8cd5273c4a8861e183ef3e26 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02004 * Copyright 2000-2008 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>
Willy Tarreau91861262007-10-17 17:06:05 +020044#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/fd.h>
46#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010047#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020048#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/proto_http.h>
50#include <proto/queue.h>
51#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010052#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020053#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/task.h>
55
Willy Tarreau6d1a9882007-01-07 02:03:04 +010056#ifdef CONFIG_HAP_TCPSPLICE
57#include <libtcpsplice.h>
58#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
Willy Tarreau58f10d72006-12-04 02:26:12 +010060#define DEBUG_PARSE_NO_SPEEDUP
61#undef DEBUG_PARSE_NO_SPEEDUP
62
Willy Tarreau976f1ee2006-12-17 10:06:03 +010063/* This is used to perform a quick jump as an alternative to a break/continue
64 * instruction. The first argument is the label for normal operation, and the
65 * second one is the break/continue instruction in the no_speedup mode.
66 */
67
68#ifdef DEBUG_PARSE_NO_SPEEDUP
69#define QUICK_JUMP(x,y) y
70#else
71#define QUICK_JUMP(x,y) goto x
72#endif
73
Willy Tarreau1c47f852006-07-09 08:22:27 +020074/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010075const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020076 "HTTP/1.0 200 OK\r\n"
77 "Cache-Control: no-cache\r\n"
78 "Connection: close\r\n"
79 "Content-Type: text/html\r\n"
80 "\r\n"
81 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const struct chunk http_200_chunk = {
84 .str = (char *)&HTTP_200,
85 .len = sizeof(HTTP_200)-1
86};
87
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088const char *HTTP_301 =
89 "HTTP/1.0 301 Moved Permantenly\r\n"
90 "Cache-Control: no-cache\r\n"
91 "Connection: close\r\n"
92 "Location: "; /* not terminated since it will be concatenated with the URL */
93
Willy Tarreau0f772532006-12-23 20:51:41 +010094const char *HTTP_302 =
95 "HTTP/1.0 302 Found\r\n"
96 "Cache-Control: no-cache\r\n"
97 "Connection: close\r\n"
98 "Location: "; /* not terminated since it will be concatenated with the URL */
99
100/* same as 302 except that the browser MUST retry with the GET method */
101const char *HTTP_303 =
102 "HTTP/1.0 303 See Other\r\n"
103 "Cache-Control: no-cache\r\n"
104 "Connection: close\r\n"
105 "Location: "; /* not terminated since it will be concatenated with the URL */
106
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
108const char *HTTP_401_fmt =
109 "HTTP/1.0 401 Unauthorized\r\n"
110 "Cache-Control: no-cache\r\n"
111 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200112 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
114 "\r\n"
115 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
116
Willy Tarreau0f772532006-12-23 20:51:41 +0100117
118const int http_err_codes[HTTP_ERR_SIZE] = {
119 [HTTP_ERR_400] = 400,
120 [HTTP_ERR_403] = 403,
121 [HTTP_ERR_408] = 408,
122 [HTTP_ERR_500] = 500,
123 [HTTP_ERR_502] = 502,
124 [HTTP_ERR_503] = 503,
125 [HTTP_ERR_504] = 504,
126};
127
Willy Tarreau80587432006-12-24 17:47:20 +0100128static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100129 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100130 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 "Cache-Control: no-cache\r\n"
132 "Connection: close\r\n"
133 "Content-Type: text/html\r\n"
134 "\r\n"
135 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
136
137 [HTTP_ERR_403] =
138 "HTTP/1.0 403 Forbidden\r\n"
139 "Cache-Control: no-cache\r\n"
140 "Connection: close\r\n"
141 "Content-Type: text/html\r\n"
142 "\r\n"
143 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
144
145 [HTTP_ERR_408] =
146 "HTTP/1.0 408 Request Time-out\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
152
153 [HTTP_ERR_500] =
154 "HTTP/1.0 500 Server Error\r\n"
155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
160
161 [HTTP_ERR_502] =
162 "HTTP/1.0 502 Bad Gateway\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
168
169 [HTTP_ERR_503] =
170 "HTTP/1.0 503 Service Unavailable\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
176
177 [HTTP_ERR_504] =
178 "HTTP/1.0 504 Gateway Time-out\r\n"
179 "Cache-Control: no-cache\r\n"
180 "Connection: close\r\n"
181 "Content-Type: text/html\r\n"
182 "\r\n"
183 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
184
185};
186
Willy Tarreau80587432006-12-24 17:47:20 +0100187/* We must put the messages here since GCC cannot initialize consts depending
188 * on strlen().
189 */
190struct chunk http_err_chunks[HTTP_ERR_SIZE];
191
Willy Tarreau42250582007-04-01 01:30:43 +0200192#define FD_SETS_ARE_BITFIELDS
193#ifdef FD_SETS_ARE_BITFIELDS
194/*
195 * This map is used with all the FD_* macros to check whether a particular bit
196 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
197 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
198 * byte should be encoded. Be careful to always pass bytes from 0 to 255
199 * exclusively to the macros.
200 */
201fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
202fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
203
204#else
205#error "Check if your OS uses bitfields for fd_sets"
206#endif
207
Willy Tarreau80587432006-12-24 17:47:20 +0100208void init_proto_http()
209{
Willy Tarreau42250582007-04-01 01:30:43 +0200210 int i;
211 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100212 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200213
Willy Tarreau80587432006-12-24 17:47:20 +0100214 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
215 if (!http_err_msgs[msg]) {
216 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
217 abort();
218 }
219
220 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
221 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
222 }
Willy Tarreau42250582007-04-01 01:30:43 +0200223
224 /* initialize the log header encoding map : '{|}"#' should be encoded with
225 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
226 * URL encoding only requires '"', '#' to be encoded as well as non-
227 * printable characters above.
228 */
229 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
230 memset(url_encode_map, 0, sizeof(url_encode_map));
231 for (i = 0; i < 32; i++) {
232 FD_SET(i, hdr_encode_map);
233 FD_SET(i, url_encode_map);
234 }
235 for (i = 127; i < 256; i++) {
236 FD_SET(i, hdr_encode_map);
237 FD_SET(i, url_encode_map);
238 }
239
240 tmp = "\"#{|}";
241 while (*tmp) {
242 FD_SET(*tmp, hdr_encode_map);
243 tmp++;
244 }
245
246 tmp = "\"#";
247 while (*tmp) {
248 FD_SET(*tmp, url_encode_map);
249 tmp++;
250 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200251
252 /* memory allocations */
253 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200254 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100255}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200256
Willy Tarreau53b6c742006-12-17 13:37:46 +0100257/*
258 * We have 26 list of methods (1 per first letter), each of which can have
259 * up to 3 entries (2 valid, 1 null).
260 */
261struct http_method_desc {
262 http_meth_t meth;
263 int len;
264 const char text[8];
265};
266
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100267const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100268 ['C' - 'A'] = {
269 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
270 },
271 ['D' - 'A'] = {
272 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
273 },
274 ['G' - 'A'] = {
275 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
276 },
277 ['H' - 'A'] = {
278 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
279 },
280 ['P' - 'A'] = {
281 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
282 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
283 },
284 ['T' - 'A'] = {
285 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
286 },
287 /* rest is empty like this :
288 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
289 */
290};
291
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100292/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200293 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100294 * RFC2616 for those chars.
295 */
296
297const char http_is_spht[256] = {
298 [' '] = 1, ['\t'] = 1,
299};
300
301const char http_is_crlf[256] = {
302 ['\r'] = 1, ['\n'] = 1,
303};
304
305const char http_is_lws[256] = {
306 [' '] = 1, ['\t'] = 1,
307 ['\r'] = 1, ['\n'] = 1,
308};
309
310const char http_is_sep[256] = {
311 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
312 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
313 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
314 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
315 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
316};
317
318const char http_is_ctl[256] = {
319 [0 ... 31] = 1,
320 [127] = 1,
321};
322
323/*
324 * A token is any ASCII char that is neither a separator nor a CTL char.
325 * Do not overwrite values in assignment since gcc-2.95 will not handle
326 * them correctly. Instead, define every non-CTL char's status.
327 */
328const char http_is_token[256] = {
329 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
330 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
331 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
332 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
333 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
334 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
335 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
336 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
337 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
338 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
339 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
340 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
341 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
342 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
343 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
344 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
345 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
346 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
347 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
348 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
349 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
350 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
351 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
352 ['|'] = 1, ['}'] = 0, ['~'] = 1,
353};
354
355
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100356/*
357 * An http ver_token is any ASCII which can be found in an HTTP version,
358 * which includes 'H', 'T', 'P', '/', '.' and any digit.
359 */
360const char http_is_ver_token[256] = {
361 ['.'] = 1, ['/'] = 1,
362 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
363 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
364 ['H'] = 1, ['P'] = 1, ['T'] = 1,
365};
366
367
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368#ifdef DEBUG_FULL
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200369static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370#endif
371
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100372/*
373 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
374 * CRLF. Text length is measured first, so it cannot be NULL.
375 * The header is also automatically added to the index <hdr_idx>, and the end
376 * of headers is automatically adjusted. The number of bytes added is returned
377 * on success, otherwise <0 is returned indicating an error.
378 */
379int http_header_add_tail(struct buffer *b, struct http_msg *msg,
380 struct hdr_idx *hdr_idx, const char *text)
381{
382 int bytes, len;
383
384 len = strlen(text);
385 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
386 if (!bytes)
387 return -1;
388 msg->eoh += bytes;
389 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
390}
391
392/*
393 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
394 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
395 * the buffer is only opened and the space reserved, but nothing is copied.
396 * The header is also automatically added to the index <hdr_idx>, and the end
397 * of headers is automatically adjusted. The number of bytes added is returned
398 * on success, otherwise <0 is returned indicating an error.
399 */
400int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
401 struct hdr_idx *hdr_idx, const char *text, int len)
402{
403 int bytes;
404
405 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
406 if (!bytes)
407 return -1;
408 msg->eoh += bytes;
409 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
410}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411
412/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100413 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
414 * If so, returns the position of the first non-space character relative to
415 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
416 * to return a pointer to the place after the first space. Returns 0 if the
417 * header name does not match. Checks are case-insensitive.
418 */
419int http_header_match2(const char *hdr, const char *end,
420 const char *name, int len)
421{
422 const char *val;
423
424 if (hdr + len >= end)
425 return 0;
426 if (hdr[len] != ':')
427 return 0;
428 if (strncasecmp(hdr, name, len) != 0)
429 return 0;
430 val = hdr + len + 1;
431 while (val < end && HTTP_IS_SPHT(*val))
432 val++;
433 if ((val >= end) && (len + 2 <= end - hdr))
434 return len + 2; /* we may replace starting from second space */
435 return val - hdr;
436}
437
Willy Tarreau33a7e692007-06-10 19:45:56 +0200438/* Find the end of the header value contained between <s> and <e>.
439 * See RFC2616, par 2.2 for more information. Note that it requires
440 * a valid header to return a valid result.
441 */
442const char *find_hdr_value_end(const char *s, const char *e)
443{
444 int quoted, qdpair;
445
446 quoted = qdpair = 0;
447 for (; s < e; s++) {
448 if (qdpair) qdpair = 0;
449 else if (quoted && *s == '\\') qdpair = 1;
450 else if (quoted && *s == '"') quoted = 0;
451 else if (*s == '"') quoted = 1;
452 else if (*s == ',') return s;
453 }
454 return s;
455}
456
457/* Find the first or next occurrence of header <name> in message buffer <sol>
458 * using headers index <idx>, and return it in the <ctx> structure. This
459 * structure holds everything necessary to use the header and find next
460 * occurrence. If its <idx> member is 0, the header is searched from the
461 * beginning. Otherwise, the next occurrence is returned. The function returns
462 * 1 when it finds a value, and 0 when there is no more.
463 */
464int http_find_header2(const char *name, int len,
465 const char *sol, struct hdr_idx *idx,
466 struct hdr_ctx *ctx)
467{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200468 const char *eol, *sov;
469 int cur_idx;
470
471 if (ctx->idx) {
472 /* We have previously returned a value, let's search
473 * another one on the same line.
474 */
475 cur_idx = ctx->idx;
476 sol = ctx->line;
477 sov = sol + ctx->val + ctx->vlen;
478 eol = sol + idx->v[cur_idx].len;
479
480 if (sov >= eol)
481 /* no more values in this header */
482 goto next_hdr;
483
484 /* values remaining for this header, skip the comma */
485 sov++;
486 while (sov < eol && http_is_lws[(unsigned char)*sov])
487 sov++;
488
489 goto return_hdr;
490 }
491
492 /* first request for this header */
493 sol += hdr_idx_first_pos(idx);
494 cur_idx = hdr_idx_first_idx(idx);
495
496 while (cur_idx) {
497 eol = sol + idx->v[cur_idx].len;
498
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200499 if (len == 0) {
500 /* No argument was passed, we want any header.
501 * To achieve this, we simply build a fake request. */
502 while (sol + len < eol && sol[len] != ':')
503 len++;
504 name = sol;
505 }
506
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 if ((len < eol - sol) &&
508 (sol[len] == ':') &&
509 (strncasecmp(sol, name, len) == 0)) {
510
511 sov = sol + len + 1;
512 while (sov < eol && http_is_lws[(unsigned char)*sov])
513 sov++;
514 return_hdr:
515 ctx->line = sol;
516 ctx->idx = cur_idx;
517 ctx->val = sov - sol;
518
519 eol = find_hdr_value_end(sov, eol);
520 ctx->vlen = eol - sov;
521 return 1;
522 }
523 next_hdr:
524 sol = eol + idx->v[cur_idx].cr + 1;
525 cur_idx = idx->v[cur_idx].next;
526 }
527 return 0;
528}
529
530int http_find_header(const char *name,
531 const char *sol, struct hdr_idx *idx,
532 struct hdr_ctx *ctx)
533{
534 return http_find_header2(name, strlen(name), sol, idx, ctx);
535}
536
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100537/* This function handles a server error at the stream interface level. The
538 * stream interface is assumed to be already in a closed state. An optional
539 * message is copied into the input buffer, and an HTTP status code stored.
540 * The error flags are set to the values in arguments. Any pending request
541 * is flushed.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200542 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100543static void http_server_error(struct session *t, struct stream_interface *si,
544 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545{
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100546 buffer_flush(si->ob);
547 buffer_flush(si->ib);
548 buffer_write_ena(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100549 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100550 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100551 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200552 }
553 if (!(t->flags & SN_ERR_MASK))
554 t->flags |= err;
555 if (!(t->flags & SN_FINST_MASK))
556 t->flags |= finst;
557}
558
Willy Tarreau80587432006-12-24 17:47:20 +0100559/* This function returns the appropriate error location for the given session
560 * and message.
561 */
562
563struct chunk *error_message(struct session *s, int msgnum)
564{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200565 if (s->be->errmsg[msgnum].str)
566 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100567 else if (s->fe->errmsg[msgnum].str)
568 return &s->fe->errmsg[msgnum];
569 else
570 return &http_err_chunks[msgnum];
571}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572
Willy Tarreau53b6c742006-12-17 13:37:46 +0100573/*
574 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
575 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
576 */
577static http_meth_t find_http_meth(const char *str, const int len)
578{
579 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100580 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100581
582 m = ((unsigned)*str - 'A');
583
584 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100585 for (h = http_methods[m]; h->len > 0; h++) {
586 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100587 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100588 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100589 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100590 };
591 return HTTP_METH_OTHER;
592 }
593 return HTTP_METH_NONE;
594
595}
596
Willy Tarreau21d2af32008-02-14 20:25:24 +0100597/* Parse the URI from the given transaction (which is assumed to be in request
598 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
599 * It is returned otherwise.
600 */
601static char *
602http_get_path(struct http_txn *txn)
603{
604 char *ptr, *end;
605
606 ptr = txn->req.sol + txn->req.sl.rq.u;
607 end = ptr + txn->req.sl.rq.u_l;
608
609 if (ptr >= end)
610 return NULL;
611
612 /* RFC2616, par. 5.1.2 :
613 * Request-URI = "*" | absuri | abspath | authority
614 */
615
616 if (*ptr == '*')
617 return NULL;
618
619 if (isalpha((unsigned char)*ptr)) {
620 /* this is a scheme as described by RFC3986, par. 3.1 */
621 ptr++;
622 while (ptr < end &&
623 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
624 ptr++;
625 /* skip '://' */
626 if (ptr == end || *ptr++ != ':')
627 return NULL;
628 if (ptr == end || *ptr++ != '/')
629 return NULL;
630 if (ptr == end || *ptr++ != '/')
631 return NULL;
632 }
633 /* skip [user[:passwd]@]host[:[port]] */
634
635 while (ptr < end && *ptr != '/')
636 ptr++;
637
638 if (ptr == end)
639 return NULL;
640
641 /* OK, we got the '/' ! */
642 return ptr;
643}
644
Willy Tarreauefb453c2008-10-26 20:49:47 +0100645/* Returns a 302 for a redirectable request. This may only be called just after
646 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
647 * left unchanged and will follow normal proxy processing.
648 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100649void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100650{
651 struct http_txn *txn;
652 struct chunk rdr;
653 char *path;
654 int len;
655
656 /* 1: create the response header */
657 rdr.len = strlen(HTTP_302);
658 rdr.str = trash;
659 memcpy(rdr.str, HTTP_302, rdr.len);
660
661 /* 2: add the server's prefix */
662 if (rdr.len + s->srv->rdr_len > sizeof(trash))
663 return;
664
665 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
666 rdr.len += s->srv->rdr_len;
667
668 /* 3: add the request URI */
669 txn = &s->txn;
670 path = http_get_path(txn);
671 if (!path)
672 return;
673
674 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
675 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
676 return;
677
678 memcpy(rdr.str + rdr.len, path, len);
679 rdr.len += len;
680 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
681 rdr.len += 4;
682
683 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100684 si->shutr(si);
685 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100686 si->err_type = SI_ET_NONE;
687 si->err_loc = NULL;
688 si->state = SI_ST_CLO;
689
690 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100691 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100692
693 /* FIXME: we should increase a counter of redirects per server and per backend. */
694 if (s->srv)
695 s->srv->cum_sess++;
696}
697
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100698/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100699 * that the server side is closed. Note that err_type is actually a
700 * bitmask, where almost only aborts may be cumulated with other
701 * values. We consider that aborted operations are more important
702 * than timeouts or errors due to the fact that nobody else in the
703 * logs might explain incomplete retries. All others should avoid
704 * being cumulated. It should normally not be possible to have multiple
705 * aborts at once, but just in case, the first one in sequence is reported.
706 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100707void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100708{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100709 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100710
711 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100712 http_server_error(s, si, SN_ERR_CLICL, 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_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100715 http_server_error(s, si, SN_ERR_CLICL, 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_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100718 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
719 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100720 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100721 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
722 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100723 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100724 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
725 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100726 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100727 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
728 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100729 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100730 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
731 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100732}
733
Willy Tarreau42250582007-04-01 01:30:43 +0200734extern const char sess_term_cond[8];
735extern const char sess_fin_state[8];
736extern const char *monthname[12];
737const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
738const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
739 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
740 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200741struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200742struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100743
Willy Tarreau42250582007-04-01 01:30:43 +0200744/*
745 * send a log for the session when we have enough info about it.
746 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100747 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100748void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200749{
750 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
751 struct proxy *fe = s->fe;
752 struct proxy *be = s->be;
753 struct proxy *prx_log;
754 struct http_txn *txn = &s->txn;
755 int tolog;
756 char *uri, *h;
757 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200758 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200759 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200760 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200761 int hdr;
762
763 if (fe->logfac1 < 0 && fe->logfac2 < 0)
764 return;
765 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100766
Willy Tarreau42250582007-04-01 01:30:43 +0200767 if (s->cli_addr.ss_family == AF_INET)
768 inet_ntop(AF_INET,
769 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
770 pn, sizeof(pn));
771 else
772 inet_ntop(AF_INET6,
773 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
774 pn, sizeof(pn));
775
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200776 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200777
778 /* FIXME: let's limit ourselves to frontend logging for now. */
779 tolog = fe->to_log;
780
781 h = tmpline;
782 if (fe->to_log & LW_REQHDR &&
783 txn->req.cap &&
784 (h < tmpline + sizeof(tmpline) - 10)) {
785 *(h++) = ' ';
786 *(h++) = '{';
787 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
788 if (hdr)
789 *(h++) = '|';
790 if (txn->req.cap[hdr] != NULL)
791 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
792 '#', hdr_encode_map, txn->req.cap[hdr]);
793 }
794 *(h++) = '}';
795 }
796
797 if (fe->to_log & LW_RSPHDR &&
798 txn->rsp.cap &&
799 (h < tmpline + sizeof(tmpline) - 7)) {
800 *(h++) = ' ';
801 *(h++) = '{';
802 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
803 if (hdr)
804 *(h++) = '|';
805 if (txn->rsp.cap[hdr] != NULL)
806 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
807 '#', hdr_encode_map, txn->rsp.cap[hdr]);
808 }
809 *(h++) = '}';
810 }
811
812 if (h < tmpline + sizeof(tmpline) - 4) {
813 *(h++) = ' ';
814 *(h++) = '"';
815 uri = txn->uri ? txn->uri : "<BADREQ>";
816 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
817 '#', url_encode_map, uri);
818 *(h++) = '"';
819 }
820 *h = '\0';
821
822 svid = (tolog & LW_SVID) ?
823 (s->data_source != DATA_SRC_STATS) ?
824 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
825
Willy Tarreau70089872008-06-13 21:12:51 +0200826 t_request = -1;
827 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
828 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
829
Willy Tarreau42250582007-04-01 01:30:43 +0200830 send_log(prx_log, LOG_INFO,
831 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
832 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100833 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200834 pn,
835 (s->cli_addr.ss_family == AF_INET) ?
836 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
837 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200838 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200839 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200840 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200841 t_request,
842 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200843 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
844 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
845 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
846 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100847 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200848 txn->cli_cookie ? txn->cli_cookie : "-",
849 txn->srv_cookie ? txn->srv_cookie : "-",
850 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
851 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
852 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
853 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
854 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100855 (s->flags & SN_REDISP)?"+":"",
856 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200857 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
858
859 s->logs.logwait = 0;
860}
861
Willy Tarreau117f59e2007-03-04 18:17:17 +0100862
863/*
864 * Capture headers from message starting at <som> according to header list
865 * <cap_hdr>, and fill the <idx> structure appropriately.
866 */
867void capture_headers(char *som, struct hdr_idx *idx,
868 char **cap, struct cap_hdr *cap_hdr)
869{
870 char *eol, *sol, *col, *sov;
871 int cur_idx;
872 struct cap_hdr *h;
873 int len;
874
875 sol = som + hdr_idx_first_pos(idx);
876 cur_idx = hdr_idx_first_idx(idx);
877
878 while (cur_idx) {
879 eol = sol + idx->v[cur_idx].len;
880
881 col = sol;
882 while (col < eol && *col != ':')
883 col++;
884
885 sov = col + 1;
886 while (sov < eol && http_is_lws[(unsigned char)*sov])
887 sov++;
888
889 for (h = cap_hdr; h; h = h->next) {
890 if ((h->namelen == col - sol) &&
891 (strncasecmp(sol, h->name, h->namelen) == 0)) {
892 if (cap[h->index] == NULL)
893 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200894 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100895
896 if (cap[h->index] == NULL) {
897 Alert("HTTP capture : out of memory.\n");
898 continue;
899 }
900
901 len = eol - sov;
902 if (len > h->len)
903 len = h->len;
904
905 memcpy(cap[h->index], sov, len);
906 cap[h->index][len]=0;
907 }
908 }
909 sol = eol + idx->v[cur_idx].cr + 1;
910 cur_idx = idx->v[cur_idx].next;
911 }
912}
913
914
Willy Tarreau42250582007-04-01 01:30:43 +0200915/* either we find an LF at <ptr> or we jump to <bad>.
916 */
917#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
918
919/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
920 * otherwise to <http_msg_ood> with <state> set to <st>.
921 */
922#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
923 ptr++; \
924 if (likely(ptr < end)) \
925 goto good; \
926 else { \
927 state = (st); \
928 goto http_msg_ood; \
929 } \
930 } while (0)
931
932
Willy Tarreaubaaee002006-06-26 02:48:02 +0200933/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100934 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100935 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
936 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
937 * will give undefined results.
938 * Note that it is upon the caller's responsibility to ensure that ptr < end,
939 * and that msg->sol points to the beginning of the response.
940 * If a complete line is found (which implies that at least one CR or LF is
941 * found before <end>, the updated <ptr> is returned, otherwise NULL is
942 * returned indicating an incomplete line (which does not mean that parts have
943 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
944 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
945 * upon next call.
946 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200947 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100948 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
949 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200950 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100951 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100952const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
953 unsigned int state, const char *ptr, const char *end,
954 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100955{
Willy Tarreau8973c702007-01-21 23:58:29 +0100956 switch (state) {
957 http_msg_rpver:
958 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100959 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100960 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
961
962 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100963 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100964 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
965 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100966 state = HTTP_MSG_ERROR;
967 break;
968
Willy Tarreau8973c702007-01-21 23:58:29 +0100969 http_msg_rpver_sp:
970 case HTTP_MSG_RPVER_SP:
971 if (likely(!HTTP_IS_LWS(*ptr))) {
972 msg->sl.st.c = ptr - msg_buf;
973 goto http_msg_rpcode;
974 }
975 if (likely(HTTP_IS_SPHT(*ptr)))
976 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
977 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100978 state = HTTP_MSG_ERROR;
979 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100980
981 http_msg_rpcode:
982 case HTTP_MSG_RPCODE:
983 if (likely(!HTTP_IS_LWS(*ptr)))
984 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
985
986 if (likely(HTTP_IS_SPHT(*ptr))) {
987 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
988 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
989 }
990
991 /* so it's a CR/LF, so there is no reason phrase */
992 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
993 http_msg_rsp_reason:
994 /* FIXME: should we support HTTP responses without any reason phrase ? */
995 msg->sl.st.r = ptr - msg_buf;
996 msg->sl.st.r_l = 0;
997 goto http_msg_rpline_eol;
998
999 http_msg_rpcode_sp:
1000 case HTTP_MSG_RPCODE_SP:
1001 if (likely(!HTTP_IS_LWS(*ptr))) {
1002 msg->sl.st.r = ptr - msg_buf;
1003 goto http_msg_rpreason;
1004 }
1005 if (likely(HTTP_IS_SPHT(*ptr)))
1006 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1007 /* so it's a CR/LF, so there is no reason phrase */
1008 goto http_msg_rsp_reason;
1009
1010 http_msg_rpreason:
1011 case HTTP_MSG_RPREASON:
1012 if (likely(!HTTP_IS_CRLF(*ptr)))
1013 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1014 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1015 http_msg_rpline_eol:
1016 /* We have seen the end of line. Note that we do not
1017 * necessarily have the \n yet, but at least we know that we
1018 * have EITHER \r OR \n, otherwise the response would not be
1019 * complete. We can then record the response length and return
1020 * to the caller which will be able to register it.
1021 */
1022 msg->sl.st.l = ptr - msg->sol;
1023 return ptr;
1024
1025#ifdef DEBUG_FULL
1026 default:
1027 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1028 exit(1);
1029#endif
1030 }
1031
1032 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001033 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001034 if (ret_state)
1035 *ret_state = state;
1036 if (ret_ptr)
1037 *ret_ptr = (char *)ptr;
1038 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001039}
1040
1041
1042/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001043 * This function parses a request line between <ptr> and <end>, starting with
1044 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1045 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1046 * will give undefined results.
1047 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1048 * and that msg->sol points to the beginning of the request.
1049 * If a complete line is found (which implies that at least one CR or LF is
1050 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1051 * returned indicating an incomplete line (which does not mean that parts have
1052 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1053 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1054 * upon next call.
1055 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001056 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001057 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1058 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001059 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001060 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001061const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1062 unsigned int state, const char *ptr, const char *end,
1063 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001064{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001065 switch (state) {
1066 http_msg_rqmeth:
1067 case HTTP_MSG_RQMETH:
1068 if (likely(HTTP_IS_TOKEN(*ptr)))
1069 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001071 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001072 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1074 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001075
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001076 if (likely(HTTP_IS_CRLF(*ptr))) {
1077 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001078 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 http_msg_req09_uri:
1080 msg->sl.rq.u = ptr - msg_buf;
1081 http_msg_req09_uri_e:
1082 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1083 http_msg_req09_ver:
1084 msg->sl.rq.v = ptr - msg_buf;
1085 msg->sl.rq.v_l = 0;
1086 goto http_msg_rqline_eol;
1087 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001088 state = HTTP_MSG_ERROR;
1089 break;
1090
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 http_msg_rqmeth_sp:
1092 case HTTP_MSG_RQMETH_SP:
1093 if (likely(!HTTP_IS_LWS(*ptr))) {
1094 msg->sl.rq.u = ptr - msg_buf;
1095 goto http_msg_rquri;
1096 }
1097 if (likely(HTTP_IS_SPHT(*ptr)))
1098 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1099 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1100 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001101
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001102 http_msg_rquri:
1103 case HTTP_MSG_RQURI:
1104 if (likely(!HTTP_IS_LWS(*ptr)))
1105 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001106
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 if (likely(HTTP_IS_SPHT(*ptr))) {
1108 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1109 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1110 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001111
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001112 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1113 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001114
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001115 http_msg_rquri_sp:
1116 case HTTP_MSG_RQURI_SP:
1117 if (likely(!HTTP_IS_LWS(*ptr))) {
1118 msg->sl.rq.v = ptr - msg_buf;
1119 goto http_msg_rqver;
1120 }
1121 if (likely(HTTP_IS_SPHT(*ptr)))
1122 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1123 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1124 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001125
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001126 http_msg_rqver:
1127 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001128 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001129 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001130
1131 if (likely(HTTP_IS_CRLF(*ptr))) {
1132 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1133 http_msg_rqline_eol:
1134 /* We have seen the end of line. Note that we do not
1135 * necessarily have the \n yet, but at least we know that we
1136 * have EITHER \r OR \n, otherwise the request would not be
1137 * complete. We can then record the request length and return
1138 * to the caller which will be able to register it.
1139 */
1140 msg->sl.rq.l = ptr - msg->sol;
1141 return ptr;
1142 }
1143
1144 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001145 state = HTTP_MSG_ERROR;
1146 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001147
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001148#ifdef DEBUG_FULL
1149 default:
1150 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1151 exit(1);
1152#endif
1153 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001154
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001155 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001156 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001157 if (ret_state)
1158 *ret_state = state;
1159 if (ret_ptr)
1160 *ret_ptr = (char *)ptr;
1161 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001162}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001163
1164
Willy Tarreau8973c702007-01-21 23:58:29 +01001165/*
1166 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001167 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 * when data are missing and recalled at the exact same location with no
1169 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001170 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1171 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001172 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001173void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1174{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001175 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001176 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001177
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001178 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001179 ptr = buf->lr;
1180 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001181
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001182 if (unlikely(ptr >= end))
1183 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001184
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001185 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001186 /*
1187 * First, states that are specific to the response only.
1188 * We check them first so that request and headers are
1189 * closer to each other (accessed more often).
1190 */
1191 http_msg_rpbefore:
1192 case HTTP_MSG_RPBEFORE:
1193 if (likely(HTTP_IS_TOKEN(*ptr))) {
1194 if (likely(ptr == buf->data)) {
1195 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001196 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001197 } else {
1198#if PARSE_PRESERVE_EMPTY_LINES
1199 /* only skip empty leading lines, don't remove them */
1200 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001201 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001202#else
1203 /* Remove empty leading lines, as recommended by
1204 * RFC2616. This takes a lot of time because we
1205 * must move all the buffer backwards, but this
1206 * is rarely needed. The method above will be
1207 * cleaner when we'll be able to start sending
1208 * the request from any place in the buffer.
1209 */
1210 buf->lr = ptr;
1211 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001212 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001213 msg->sol = buf->data;
1214 ptr = buf->data;
1215 end = buf->r;
1216#endif
1217 }
1218 hdr_idx_init(idx);
1219 state = HTTP_MSG_RPVER;
1220 goto http_msg_rpver;
1221 }
1222
1223 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1224 goto http_msg_invalid;
1225
1226 if (unlikely(*ptr == '\n'))
1227 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1228 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1229 /* stop here */
1230
1231 http_msg_rpbefore_cr:
1232 case HTTP_MSG_RPBEFORE_CR:
1233 EXPECT_LF_HERE(ptr, http_msg_invalid);
1234 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1235 /* stop here */
1236
1237 http_msg_rpver:
1238 case HTTP_MSG_RPVER:
1239 case HTTP_MSG_RPVER_SP:
1240 case HTTP_MSG_RPCODE:
1241 case HTTP_MSG_RPCODE_SP:
1242 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001243 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001244 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001245 if (unlikely(!ptr))
1246 return;
1247
1248 /* we have a full response and we know that we have either a CR
1249 * or an LF at <ptr>.
1250 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001251 //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 +01001252 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1253
1254 msg->sol = ptr;
1255 if (likely(*ptr == '\r'))
1256 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1257 goto http_msg_rpline_end;
1258
1259 http_msg_rpline_end:
1260 case HTTP_MSG_RPLINE_END:
1261 /* msg->sol must point to the first of CR or LF. */
1262 EXPECT_LF_HERE(ptr, http_msg_invalid);
1263 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1264 /* stop here */
1265
1266 /*
1267 * Second, states that are specific to the request only
1268 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001269 http_msg_rqbefore:
1270 case HTTP_MSG_RQBEFORE:
1271 if (likely(HTTP_IS_TOKEN(*ptr))) {
1272 if (likely(ptr == buf->data)) {
1273 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001274 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 } else {
1276#if PARSE_PRESERVE_EMPTY_LINES
1277 /* only skip empty leading lines, don't remove them */
1278 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001279 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001280#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281 /* Remove empty leading lines, as recommended by
1282 * RFC2616. This takes a lot of time because we
1283 * must move all the buffer backwards, but this
1284 * is rarely needed. The method above will be
1285 * cleaner when we'll be able to start sending
1286 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001287 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001288 buf->lr = ptr;
1289 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001290 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001291 msg->sol = buf->data;
1292 ptr = buf->data;
1293 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001294#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001296 /* we will need this when keep-alive will be supported
1297 hdr_idx_init(idx);
1298 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001299 state = HTTP_MSG_RQMETH;
1300 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001301 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001302
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1304 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 if (unlikely(*ptr == '\n'))
1307 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1308 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 http_msg_rqbefore_cr:
1312 case HTTP_MSG_RQBEFORE_CR:
1313 EXPECT_LF_HERE(ptr, http_msg_invalid);
1314 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001315 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 http_msg_rqmeth:
1318 case HTTP_MSG_RQMETH:
1319 case HTTP_MSG_RQMETH_SP:
1320 case HTTP_MSG_RQURI:
1321 case HTTP_MSG_RQURI_SP:
1322 case HTTP_MSG_RQVER:
1323 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001324 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 if (unlikely(!ptr))
1326 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 /* we have a full request and we know that we have either a CR
1329 * or an LF at <ptr>.
1330 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001331 //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 +01001332 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001333
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001334 msg->sol = ptr;
1335 if (likely(*ptr == '\r'))
1336 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001338
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 http_msg_rqline_end:
1340 case HTTP_MSG_RQLINE_END:
1341 /* check for HTTP/0.9 request : no version information available.
1342 * msg->sol must point to the first of CR or LF.
1343 */
1344 if (unlikely(msg->sl.rq.v_l == 0))
1345 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 EXPECT_LF_HERE(ptr, http_msg_invalid);
1348 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001350
Willy Tarreau8973c702007-01-21 23:58:29 +01001351 /*
1352 * Common states below
1353 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354 http_msg_hdr_first:
1355 case HTTP_MSG_HDR_FIRST:
1356 msg->sol = ptr;
1357 if (likely(!HTTP_IS_CRLF(*ptr))) {
1358 goto http_msg_hdr_name;
1359 }
1360
1361 if (likely(*ptr == '\r'))
1362 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1363 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001364
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 http_msg_hdr_name:
1366 case HTTP_MSG_HDR_NAME:
1367 /* assumes msg->sol points to the first char */
1368 if (likely(HTTP_IS_TOKEN(*ptr)))
1369 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001370
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 if (likely(*ptr == ':')) {
1372 msg->col = ptr - buf->data;
1373 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1374 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001375
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001377
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001378 http_msg_hdr_l1_sp:
1379 case HTTP_MSG_HDR_L1_SP:
1380 /* assumes msg->sol points to the first char and msg->col to the colon */
1381 if (likely(HTTP_IS_SPHT(*ptr)))
1382 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001383
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 /* header value can be basically anything except CR/LF */
1385 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 if (likely(!HTTP_IS_CRLF(*ptr))) {
1388 goto http_msg_hdr_val;
1389 }
1390
1391 if (likely(*ptr == '\r'))
1392 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1393 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 http_msg_hdr_l1_lf:
1396 case HTTP_MSG_HDR_L1_LF:
1397 EXPECT_LF_HERE(ptr, http_msg_invalid);
1398 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001399
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 http_msg_hdr_l1_lws:
1401 case HTTP_MSG_HDR_L1_LWS:
1402 if (likely(HTTP_IS_SPHT(*ptr))) {
1403 /* replace HT,CR,LF with spaces */
1404 for (; buf->data+msg->sov < ptr; msg->sov++)
1405 buf->data[msg->sov] = ' ';
1406 goto http_msg_hdr_l1_sp;
1407 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001408 /* we had a header consisting only in spaces ! */
1409 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 goto http_msg_complete_header;
1411
1412 http_msg_hdr_val:
1413 case HTTP_MSG_HDR_VAL:
1414 /* assumes msg->sol points to the first char, msg->col to the
1415 * colon, and msg->sov points to the first character of the
1416 * value.
1417 */
1418 if (likely(!HTTP_IS_CRLF(*ptr)))
1419 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001420
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 msg->eol = ptr;
1422 /* Note: we could also copy eol into ->eoh so that we have the
1423 * real header end in case it ends with lots of LWS, but is this
1424 * really needed ?
1425 */
1426 if (likely(*ptr == '\r'))
1427 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1428 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 http_msg_hdr_l2_lf:
1431 case HTTP_MSG_HDR_L2_LF:
1432 EXPECT_LF_HERE(ptr, http_msg_invalid);
1433 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001434
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 http_msg_hdr_l2_lws:
1436 case HTTP_MSG_HDR_L2_LWS:
1437 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1438 /* LWS: replace HT,CR,LF with spaces */
1439 for (; msg->eol < ptr; msg->eol++)
1440 *msg->eol = ' ';
1441 goto http_msg_hdr_val;
1442 }
1443 http_msg_complete_header:
1444 /*
1445 * It was a new header, so the last one is finished.
1446 * Assumes msg->sol points to the first char, msg->col to the
1447 * colon, msg->sov points to the first character of the value
1448 * and msg->eol to the first CR or LF so we know how the line
1449 * ends. We insert last header into the index.
1450 */
1451 /*
1452 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1453 write(2, msg->sol, msg->eol-msg->sol);
1454 fprintf(stderr,"\n");
1455 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001456
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1458 idx, idx->tail) < 0))
1459 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 msg->sol = ptr;
1462 if (likely(!HTTP_IS_CRLF(*ptr))) {
1463 goto http_msg_hdr_name;
1464 }
1465
1466 if (likely(*ptr == '\r'))
1467 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1468 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001469
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 http_msg_last_lf:
1471 case HTTP_MSG_LAST_LF:
1472 /* Assumes msg->sol points to the first of either CR or LF */
1473 EXPECT_LF_HERE(ptr, http_msg_invalid);
1474 ptr++;
1475 buf->lr = ptr;
1476 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001477 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 return;
1479#ifdef DEBUG_FULL
1480 default:
1481 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1482 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001483#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 }
1485 http_msg_ood:
1486 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001487 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 buf->lr = ptr;
1489 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 http_msg_invalid:
1492 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001493 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001494 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495 return;
1496}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001497
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001498/* This function performs all the processing enabled for the current request.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001499 * It returns 1 if the processing can continue on next analysers, or zero if it
1500 * needs more data, encounters an error, or wants to immediately abort the
1501 * request. It relies on buffers flags, and updates s->req->analysers. Its
1502 * behaviour is rather simple:
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001503 * - all enabled analysers are called in turn from the lower to the higher
1504 * bit.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001505 * - the analyser must check for errors and timeouts, and react as expected.
1506 * It does not have to close anything upon error, the caller will.
1507 * - if the analyser does not have enough data, it must return 0without calling
1508 * other ones. It should also probably do a buffer_write_dis() to ensure
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001509 * that unprocessed data will not be forwarded. But that probably depends on
Willy Tarreauedcf6682008-11-30 23:15:34 +01001510 * the protocol.
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001511 * - if an analyser has enough data, it just has to pass on to the next
Willy Tarreauedcf6682008-11-30 23:15:34 +01001512 * analyser without using buffer_write_dis() (enabled by default).
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001513 * - if an analyser thinks it has no added value anymore staying here, it must
1514 * reset its bit from the analysers flags in order not to be called anymore.
1515 *
1516 * In the future, analysers should be able to indicate that they want to be
1517 * called after XXX bytes have been received (or transfered), and the min of
1518 * all's wishes will be used to ring back (unless a special condition occurs).
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001520int http_process_request(struct session *s, struct buffer *req)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521{
Willy Tarreau7552c032009-03-01 11:10:40 +01001522
1523 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1524 now_ms, __FUNCTION__,
1525 s,
1526 req,
1527 req->rex, req->wex,
1528 req->flags,
1529 req->l,
1530 req->analysers);
1531
Willy Tarreau59234e92008-11-30 23:51:27 +01001532 /*
1533 * We will parse the partial (or complete) lines.
1534 * We will check the request syntax, and also join multi-line
1535 * headers. An index of all the lines will be elaborated while
1536 * parsing.
1537 *
1538 * For the parsing, we use a 28 states FSM.
1539 *
1540 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01001541 * req->data + msg->som = beginning of request
Willy Tarreau59234e92008-11-30 23:51:27 +01001542 * req->data + req->eoh = end of processed headers / start of current one
1543 * req->data + req->eol = end of current header or line (LF or CRLF)
1544 * req->lr = first non-visited byte
1545 * req->r = end of data
1546 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001547
Willy Tarreau59234e92008-11-30 23:51:27 +01001548 int cur_idx;
1549 struct http_txn *txn = &s->txn;
1550 struct http_msg *msg = &txn->req;
1551 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001552
Willy Tarreau59234e92008-11-30 23:51:27 +01001553 if (likely(req->lr < req->r))
1554 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001555
Willy Tarreau59234e92008-11-30 23:51:27 +01001556 /* 1: we might have to print this header in debug mode */
1557 if (unlikely((global.mode & MODE_DEBUG) &&
1558 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
1559 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
1560 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001561
Willy Tarreau59234e92008-11-30 23:51:27 +01001562 sol = req->data + msg->som;
1563 eol = sol + msg->sl.rq.l;
1564 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001565
Willy Tarreau59234e92008-11-30 23:51:27 +01001566 sol += hdr_idx_first_pos(&txn->hdr_idx);
1567 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001568
Willy Tarreau59234e92008-11-30 23:51:27 +01001569 while (cur_idx) {
1570 eol = sol + txn->hdr_idx.v[cur_idx].len;
1571 debug_hdr("clihdr", s, sol, eol);
1572 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1573 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001575 }
1576
Willy Tarreau58f10d72006-12-04 02:26:12 +01001577
Willy Tarreau59234e92008-11-30 23:51:27 +01001578 /*
1579 * Now we quickly check if we have found a full valid request.
1580 * If not so, we check the FD and buffer states before leaving.
1581 * A full request is indicated by the fact that we have seen
1582 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1583 * requests are checked first.
1584 *
1585 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001586
Willy Tarreau59234e92008-11-30 23:51:27 +01001587 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001588 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001589 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001590 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001591 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
1592 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001593
Willy Tarreau59234e92008-11-30 23:51:27 +01001594 /* 1: Since we are in header mode, if there's no space
1595 * left for headers, we won't be able to free more
1596 * later, so the session will never terminate. We
1597 * must terminate it now.
1598 */
1599 if (unlikely(req->flags & BF_FULL)) {
1600 /* FIXME: check if URI is set and return Status
1601 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001602 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001603 goto return_bad_req;
1604 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001605
Willy Tarreau59234e92008-11-30 23:51:27 +01001606 /* 2: have we encountered a read error ? */
1607 else if (req->flags & BF_READ_ERROR) {
1608 /* we cannot return any message on error */
1609 msg->msg_state = HTTP_MSG_ERROR;
1610 req->analysers = 0;
1611 s->fe->failed_req++;
1612 if (!(s->flags & SN_ERR_MASK))
1613 s->flags |= SN_ERR_CLICL;
1614 if (!(s->flags & SN_FINST_MASK))
1615 s->flags |= SN_FINST_R;
1616 return 0;
1617 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001618
Willy Tarreau59234e92008-11-30 23:51:27 +01001619 /* 3: has the read timeout expired ? */
1620 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
1621 /* read timeout : give up with an error message. */
1622 txn->status = 408;
1623 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
1624 msg->msg_state = HTTP_MSG_ERROR;
1625 req->analysers = 0;
1626 s->fe->failed_req++;
1627 if (!(s->flags & SN_ERR_MASK))
1628 s->flags |= SN_ERR_CLITO;
1629 if (!(s->flags & SN_FINST_MASK))
1630 s->flags |= SN_FINST_R;
1631 return 0;
1632 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001633
Willy Tarreau59234e92008-11-30 23:51:27 +01001634 /* 4: have we encountered a close ? */
1635 else if (req->flags & BF_SHUTR) {
1636 txn->status = 400;
1637 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1638 msg->msg_state = HTTP_MSG_ERROR;
1639 req->analysers = 0;
1640 s->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001641
Willy Tarreau59234e92008-11-30 23:51:27 +01001642 if (!(s->flags & SN_ERR_MASK))
1643 s->flags |= SN_ERR_CLICL;
1644 if (!(s->flags & SN_FINST_MASK))
1645 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001646 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001647 }
1648
Willy Tarreau59234e92008-11-30 23:51:27 +01001649 buffer_write_dis(req);
1650 /* just set the request timeout once at the beginning of the request */
1651 if (!tick_isset(req->analyse_exp))
1652 req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001653
Willy Tarreau59234e92008-11-30 23:51:27 +01001654 /* we're not ready yet */
1655 return 0;
1656 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001657
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001658
Willy Tarreau59234e92008-11-30 23:51:27 +01001659 /****************************************************************
1660 * More interesting part now : we know that we have a complete *
1661 * request which at least looks like HTTP. We have an indicator *
1662 * of each header's length, so we can parse them quickly. *
1663 ****************************************************************/
Willy Tarreau9cdde232007-05-02 20:58:19 +02001664
Willy Tarreau59234e92008-11-30 23:51:27 +01001665 req->analysers &= ~AN_REQ_HTTP_HDR;
1666 req->analyse_exp = TICK_ETERNITY;
1667
1668 /* ensure we keep this pointer to the beginning of the message */
1669 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001670
Willy Tarreau59234e92008-11-30 23:51:27 +01001671 /*
1672 * 1: identify the method
1673 */
1674 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
1675
1676 /* we can make use of server redirect on GET and HEAD */
1677 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1678 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001679
Willy Tarreau59234e92008-11-30 23:51:27 +01001680 /*
1681 * 2: check if the URI matches the monitor_uri.
1682 * We have to do this for every request which gets in, because
1683 * the monitor-uri is defined by the frontend.
1684 */
1685 if (unlikely((s->fe->monitor_uri_len != 0) &&
1686 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1687 !memcmp(&req->data[msg->sl.rq.u],
1688 s->fe->monitor_uri,
1689 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001690 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001691 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001692 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001693 struct acl_cond *cond;
1694 cur_proxy = s->fe;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001695
Willy Tarreau59234e92008-11-30 23:51:27 +01001696 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001697
Willy Tarreau59234e92008-11-30 23:51:27 +01001698 /* Check if we want to fail this monitor request or not */
1699 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1700 int ret = acl_exec_cond(cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001701
Willy Tarreau59234e92008-11-30 23:51:27 +01001702 ret = acl_pass(ret);
1703 if (cond->pol == ACL_COND_UNLESS)
1704 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001705
Willy Tarreau59234e92008-11-30 23:51:27 +01001706 if (ret) {
1707 /* we fail this request, let's return 503 service unavail */
1708 txn->status = 503;
1709 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
1710 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001711 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001712 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001713
Willy Tarreau59234e92008-11-30 23:51:27 +01001714 /* nothing to fail, let's reply normaly */
1715 txn->status = 200;
1716 stream_int_retnclose(req->prod, &http_200_chunk);
1717 goto return_prx_cond;
1718 }
1719
1720 /*
1721 * 3: Maybe we have to copy the original REQURI for the logs ?
1722 * Note: we cannot log anymore if the request has been
1723 * classified as invalid.
1724 */
1725 if (unlikely(s->logs.logwait & LW_REQ)) {
1726 /* we have a complete HTTP request that we must log */
1727 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1728 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729
Willy Tarreau59234e92008-11-30 23:51:27 +01001730 if (urilen >= REQURI_LEN)
1731 urilen = REQURI_LEN - 1;
1732 memcpy(txn->uri, &req->data[msg->som], urilen);
1733 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734
Willy Tarreau59234e92008-11-30 23:51:27 +01001735 if (!(s->logs.logwait &= ~LW_REQ))
1736 s->do_log(s);
1737 } else {
1738 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001740 }
Willy Tarreau06619262006-12-17 08:37:22 +01001741
Willy Tarreau59234e92008-11-30 23:51:27 +01001742 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1743 if (unlikely(msg->sl.rq.v_l == 0)) {
1744 int delta;
1745 char *cur_end;
1746 msg->sol = req->data + msg->som;
1747 cur_end = msg->sol + msg->sl.rq.l;
1748 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001749
Willy Tarreau59234e92008-11-30 23:51:27 +01001750 if (msg->sl.rq.u_l == 0) {
1751 /* if no URI was set, add "/" */
1752 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 cur_end += delta;
Willy Tarreau59234e92008-11-30 23:51:27 +01001754 msg->eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001755 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001756 /* add HTTP version */
1757 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1758 msg->eoh += delta;
1759 cur_end += delta;
1760 cur_end = (char *)http_parse_reqline(msg, req->data,
1761 HTTP_MSG_RQMETH,
1762 msg->sol, cur_end + 1,
1763 NULL, NULL);
1764 if (unlikely(!cur_end))
1765 goto return_bad_req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001766
Willy Tarreau59234e92008-11-30 23:51:27 +01001767 /* we have a full HTTP/1.0 request now and we know that
1768 * we have either a CR or an LF at <ptr>.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001769 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001770 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1771 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001772
1773
Willy Tarreau59234e92008-11-30 23:51:27 +01001774 /* 5: we may need to capture headers */
1775 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
1776 capture_headers(req->data + msg->som, &txn->hdr_idx,
1777 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001778
Willy Tarreau59234e92008-11-30 23:51:27 +01001779 /*
1780 * 6: we will have to evaluate the filters.
1781 * As opposed to version 1.2, now they will be evaluated in the
1782 * filters order and not in the header order. This means that
1783 * each filter has to be validated among all headers.
1784 *
1785 * We can now check whether we want to switch to another
1786 * backend, in which case we will re-check the backend's
1787 * filters and various options. In order to support 3-level
1788 * switching, here's how we should proceed :
1789 *
1790 * a) run be.
1791 * if (switch) then switch ->be to the new backend.
1792 * b) run be if (be != fe).
1793 * There cannot be any switch from there, so ->be cannot be
1794 * changed anymore.
1795 *
1796 * => filters always apply to ->be, then ->be may change.
1797 *
1798 * The response path will be able to apply either ->be, or
1799 * ->be then ->fe filters in order to match the reverse of
1800 * the forward sequence.
1801 */
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001802
Willy Tarreau59234e92008-11-30 23:51:27 +01001803 do {
1804 struct acl_cond *cond;
1805 struct redirect_rule *rule;
1806 struct proxy *rule_set = s->be;
1807 cur_proxy = s->be;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001808
Willy Tarreau59234e92008-11-30 23:51:27 +01001809 /* first check whether we have some ACLs set to redirect this request */
1810 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
1811 int ret = acl_exec_cond(rule->cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001812
Willy Tarreau59234e92008-11-30 23:51:27 +01001813 ret = acl_pass(ret);
1814 if (rule->cond->pol == ACL_COND_UNLESS)
1815 ret = !ret;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001816
Willy Tarreau59234e92008-11-30 23:51:27 +01001817 if (ret) {
1818 struct chunk rdr = { trash, 0 };
1819 const char *msg_fmt;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001820
Willy Tarreau59234e92008-11-30 23:51:27 +01001821 /* build redirect message */
1822 switch(rule->code) {
1823 case 303:
1824 rdr.len = strlen(HTTP_303);
1825 msg_fmt = HTTP_303;
1826 break;
1827 case 301:
1828 rdr.len = strlen(HTTP_301);
1829 msg_fmt = HTTP_301;
1830 break;
1831 case 302:
1832 default:
1833 rdr.len = strlen(HTTP_302);
1834 msg_fmt = HTTP_302;
1835 break;
1836 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001837
Willy Tarreau59234e92008-11-30 23:51:27 +01001838 if (unlikely(rdr.len > sizeof(trash)))
1839 goto return_bad_req;
1840 memcpy(rdr.str, msg_fmt, rdr.len);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001841
Willy Tarreau59234e92008-11-30 23:51:27 +01001842 switch(rule->type) {
1843 case REDIRECT_TYPE_PREFIX: {
1844 const char *path;
1845 int pathlen;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001846
Willy Tarreau59234e92008-11-30 23:51:27 +01001847 path = http_get_path(txn);
1848 /* build message using path */
1849 if (path) {
1850 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
Willy Tarreau79da4692008-11-19 20:03:04 +01001851 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
1852 int qs = 0;
1853 while (qs < pathlen) {
1854 if (path[qs] == '?') {
1855 pathlen = qs;
1856 break;
1857 }
1858 qs++;
1859 }
1860 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001861 } else {
1862 path = "/";
1863 pathlen = 1;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001864 }
1865
Willy Tarreau59234e92008-11-30 23:51:27 +01001866 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
1867 goto return_bad_req;
1868
Willy Tarreaufe651a52008-11-19 21:15:17 +01001869 /* add prefix. Note that if prefix == "/", we don't want to
1870 * add anything, otherwise it makes it hard for the user to
1871 * configure a self-redirection.
1872 */
1873 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
1874 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1875 rdr.len += rule->rdr_len;
1876 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001877
Willy Tarreau59234e92008-11-30 23:51:27 +01001878 /* add path */
1879 memcpy(rdr.str + rdr.len, path, pathlen);
1880 rdr.len += pathlen;
1881 break;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001882 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001883 case REDIRECT_TYPE_LOCATION:
1884 default:
1885 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
1886 goto return_bad_req;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001887
Willy Tarreau59234e92008-11-30 23:51:27 +01001888 /* add location */
1889 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1890 rdr.len += rule->rdr_len;
1891 break;
1892 }
Willy Tarreau11382812008-07-09 16:18:21 +02001893
Willy Tarreau0140f252008-11-19 21:07:09 +01001894 if (rule->cookie_len) {
1895 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
1896 rdr.len += 14;
1897 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
1898 rdr.len += rule->cookie_len;
1899 memcpy(rdr.str + rdr.len, "\r\n", 2);
1900 rdr.len += 2;
1901 }
1902
Willy Tarreau59234e92008-11-30 23:51:27 +01001903 /* add end of headers */
1904 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
1905 rdr.len += 4;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001906
Willy Tarreau59234e92008-11-30 23:51:27 +01001907 txn->status = rule->code;
1908 /* let's log the request time */
1909 s->logs.tv_request = now;
1910 stream_int_retnclose(req->prod, &rdr);
1911 goto return_prx_cond;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001912 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001913 }
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001914
Willy Tarreau59234e92008-11-30 23:51:27 +01001915 /* first check whether we have some ACLs set to block this request */
1916 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
1917 int ret = acl_exec_cond(cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau53b6c742006-12-17 13:37:46 +01001918
Willy Tarreau59234e92008-11-30 23:51:27 +01001919 ret = acl_pass(ret);
1920 if (cond->pol == ACL_COND_UNLESS)
1921 ret = !ret;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001922
Willy Tarreau59234e92008-11-30 23:51:27 +01001923 if (ret) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001924 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001925 /* let's log the request time */
Willy Tarreau59234e92008-11-30 23:51:27 +01001926 s->logs.tv_request = now;
1927 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001928 goto return_prx_cond;
1929 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001930 }
1931
1932 /* try headers filters */
1933 if (rule_set->req_exp != NULL) {
1934 if (apply_filters_to_request(s, req, rule_set->req_exp) < 0)
1935 goto return_bad_req;
1936 }
1937
1938 if (!(s->flags & SN_BE_ASSIGNED) && (s->be != cur_proxy)) {
1939 /* to ensure correct connection accounting on
1940 * the backend, we count the connection for the
1941 * one managing the queue.
1942 */
1943 s->be->beconn++;
1944 if (s->be->beconn > s->be->beconn_max)
1945 s->be->beconn_max = s->be->beconn;
1946 s->be->cum_beconn++;
1947 s->flags |= SN_BE_ASSIGNED;
1948 }
Willy Tarreau06619262006-12-17 08:37:22 +01001949
Willy Tarreau59234e92008-11-30 23:51:27 +01001950 /* has the request been denied ? */
1951 if (txn->flags & TX_CLDENY) {
1952 /* no need to go further */
1953 txn->status = 403;
1954 /* let's log the request time */
1955 s->logs.tv_request = now;
1956 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
1957 goto return_prx_cond;
1958 }
Willy Tarreau06619262006-12-17 08:37:22 +01001959
Willy Tarreau59234e92008-11-30 23:51:27 +01001960 /* We might have to check for "Connection:" */
1961 if (((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
1962 !(s->flags & SN_CONN_CLOSED)) {
1963 char *cur_ptr, *cur_end, *cur_next;
1964 int cur_idx, old_idx, delta, val;
1965 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001966
Willy Tarreau59234e92008-11-30 23:51:27 +01001967 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
1968 old_idx = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001969
Willy Tarreau59234e92008-11-30 23:51:27 +01001970 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1971 cur_hdr = &txn->hdr_idx.v[cur_idx];
1972 cur_ptr = cur_next;
1973 cur_end = cur_ptr + cur_hdr->len;
1974 cur_next = cur_end + cur_hdr->cr + 1;
1975
1976 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1977 if (val) {
1978 /* 3 possibilities :
1979 * - we have already set Connection: close,
1980 * so we remove this line.
1981 * - we have not yet set Connection: close,
1982 * but this line indicates close. We leave
1983 * it untouched and set the flag.
1984 * - we have not yet set Connection: close,
1985 * and this line indicates non-close. We
1986 * replace it.
1987 */
1988 if (s->flags & SN_CONN_CLOSED) {
1989 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
1990 txn->req.eoh += delta;
1991 cur_next += delta;
1992 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1993 txn->hdr_idx.used--;
1994 cur_hdr->len = 0;
1995 } else {
1996 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1997 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1998 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001999 cur_next += delta;
Willy Tarreau59234e92008-11-30 23:51:27 +01002000 cur_hdr->len += delta;
2001 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002002 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002003 s->flags |= SN_CONN_CLOSED;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002004 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002005 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002006 old_idx = cur_idx;
Willy Tarreau06619262006-12-17 08:37:22 +01002007 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002008 }
2009 /* add request headers from the rule sets in the same order */
2010 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2011 if (unlikely(http_header_add_tail(req,
2012 &txn->req,
2013 &txn->hdr_idx,
2014 rule_set->req_add[cur_idx])) < 0)
2015 goto return_bad_req;
2016 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002017
Willy Tarreau59234e92008-11-30 23:51:27 +01002018 /* check if stats URI was requested, and if an auth is needed */
2019 if (rule_set->uri_auth != NULL &&
2020 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2021 /* we have to check the URI and auth for this request.
2022 * FIXME!!! that one is rather dangerous, we want to
2023 * make it follow standard rules (eg: clear req->analysers).
2024 */
2025 if (stats_check_uri_auth(s, rule_set)) {
2026 req->analysers = 0;
2027 return 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01002028 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002029 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002030
Willy Tarreau59234e92008-11-30 23:51:27 +01002031 /* now check whether we have some switching rules for this request */
2032 if (!(s->flags & SN_BE_ASSIGNED)) {
2033 struct switching_rule *rule;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002034
Willy Tarreau59234e92008-11-30 23:51:27 +01002035 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2036 int ret;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002037
Willy Tarreau59234e92008-11-30 23:51:27 +01002038 ret = acl_exec_cond(rule->cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002039
Willy Tarreau59234e92008-11-30 23:51:27 +01002040 ret = acl_pass(ret);
2041 if (rule->cond->pol == ACL_COND_UNLESS)
2042 ret = !ret;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002043
Willy Tarreau59234e92008-11-30 23:51:27 +01002044 if (ret) {
2045 s->be = rule->be.backend;
2046 s->be->beconn++;
2047 if (s->be->beconn > s->be->beconn_max)
2048 s->be->beconn_max = s->be->beconn;
2049 s->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002050
Willy Tarreau59234e92008-11-30 23:51:27 +01002051 /* assign new parameters to the session from the new backend */
2052 s->rep->rto = s->req->wto = s->be->timeout.server;
2053 s->req->cto = s->be->timeout.connect;
2054 s->conn_retries = s->be->conn_retries;
2055 s->flags |= SN_BE_ASSIGNED;
2056 break;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002057 }
2058 }
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002059 }
2060
Willy Tarreau59234e92008-11-30 23:51:27 +01002061 if (!(s->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2062 /* No backend was set, but there was a default
2063 * backend set in the frontend, so we use it and
2064 * loop again.
2065 */
2066 s->be = cur_proxy->defbe.be;
2067 s->be->beconn++;
2068 if (s->be->beconn > s->be->beconn_max)
2069 s->be->beconn_max = s->be->beconn;
2070 s->be->cum_beconn++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002071
Willy Tarreau59234e92008-11-30 23:51:27 +01002072 /* assign new parameters to the session from the new backend */
2073 s->rep->rto = s->req->wto = s->be->timeout.server;
2074 s->req->cto = s->be->timeout.connect;
2075 s->conn_retries = s->be->conn_retries;
2076 s->flags |= SN_BE_ASSIGNED;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002077 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002078 } while (s->be != cur_proxy); /* we loop only if s->be has changed */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002079
Willy Tarreau59234e92008-11-30 23:51:27 +01002080 if (!(s->flags & SN_BE_ASSIGNED)) {
2081 /* To ensure correct connection accounting on
2082 * the backend, we count the connection for the
2083 * one managing the queue.
Willy Tarreau06619262006-12-17 08:37:22 +01002084 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002085 s->be->beconn++;
2086 if (s->be->beconn > s->be->beconn_max)
2087 s->be->beconn_max = s->be->beconn;
2088 s->be->cum_beconn++;
2089 s->flags |= SN_BE_ASSIGNED;
2090 }
Willy Tarreau06619262006-12-17 08:37:22 +01002091
Willy Tarreau59234e92008-11-30 23:51:27 +01002092 /*
2093 * Right now, we know that we have processed the entire headers
2094 * and that unwanted requests have been filtered out. We can do
2095 * whatever we want with the remaining request. Also, now we
2096 * may have separate values for ->fe, ->be.
2097 */
Willy Tarreau06619262006-12-17 08:37:22 +01002098
Willy Tarreau59234e92008-11-30 23:51:27 +01002099 /*
2100 * If HTTP PROXY is set we simply get remote server address
2101 * parsing incoming request.
2102 */
2103 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2104 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2105 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002106
Willy Tarreau59234e92008-11-30 23:51:27 +01002107 /*
2108 * 7: the appsession cookie was looked up very early in 1.2,
2109 * so let's do the same now.
2110 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002111
Willy Tarreau59234e92008-11-30 23:51:27 +01002112 /* It needs to look into the URI */
2113 if (s->be->appsession_name) {
2114 get_srv_from_appsession(s, &req->data[msg->som], msg->sl.rq.l);
2115 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01002116
Willy Tarreau59234e92008-11-30 23:51:27 +01002117 /*
2118 * 8: Now we can work with the cookies.
2119 * Note that doing so might move headers in the request, but
2120 * the fields will stay coherent and the URI will not move.
2121 * This should only be performed in the backend.
2122 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002123 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002124 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2125 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002126
Willy Tarreau59234e92008-11-30 23:51:27 +01002127 /*
2128 * 9: add X-Forwarded-For if either the frontend or the backend
2129 * asks for it.
2130 */
2131 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2132 if (s->cli_addr.ss_family == AF_INET) {
2133 /* Add an X-Forwarded-For header unless the source IP is
2134 * in the 'except' network range.
2135 */
2136 if ((!s->fe->except_mask.s_addr ||
2137 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2138 != s->fe->except_net.s_addr) &&
2139 (!s->be->except_mask.s_addr ||
2140 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2141 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002142 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002143 unsigned char *pn;
2144 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002145
2146 /* Note: we rely on the backend to get the header name to be used for
2147 * x-forwarded-for, because the header is really meant for the backends.
2148 * However, if the backend did not specify any option, we have to rely
2149 * on the frontend's header name.
2150 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002151 if (s->be->fwdfor_hdr_len) {
2152 len = s->be->fwdfor_hdr_len;
2153 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002154 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002155 len = s->fe->fwdfor_hdr_len;
2156 memcpy(trash, s->fe->fwdfor_hdr_name, len);
2157 }
2158 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002159
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002160 if (unlikely(http_header_add_tail2(req, &txn->req,
2161 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002162 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002163 }
2164 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002165 else if (s->cli_addr.ss_family == AF_INET6) {
2166 /* FIXME: for the sake of completeness, we should also support
2167 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002168 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002169 int len;
2170 char pn[INET6_ADDRSTRLEN];
2171 inet_ntop(AF_INET6,
2172 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2173 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002174
Willy Tarreau59234e92008-11-30 23:51:27 +01002175 /* Note: we rely on the backend to get the header name to be used for
2176 * x-forwarded-for, because the header is really meant for the backends.
2177 * However, if the backend did not specify any option, we have to rely
2178 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002179 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002180 if (s->be->fwdfor_hdr_len) {
2181 len = s->be->fwdfor_hdr_len;
2182 memcpy(trash, s->be->fwdfor_hdr_name, len);
2183 } else {
2184 len = s->fe->fwdfor_hdr_len;
2185 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002186 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002187 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002188
Willy Tarreau59234e92008-11-30 23:51:27 +01002189 if (unlikely(http_header_add_tail2(req, &txn->req,
2190 &txn->hdr_idx, trash, len)) < 0)
2191 goto return_bad_req;
2192 }
2193 }
2194
2195 /*
2196 * 10: add "Connection: close" if needed and not yet set.
2197 * Note that we do not need to add it in case of HTTP/1.0.
2198 */
2199 if (!(s->flags & SN_CONN_CLOSED) &&
2200 ((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2201 if ((unlikely(msg->sl.rq.v_l != 8) ||
2202 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2203 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
2204 "Connection: close", 17)) < 0)
2205 goto return_bad_req;
2206 s->flags |= SN_CONN_CLOSED;
2207 }
2208 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2209 * If not assigned, perhaps we are balancing on url_param, but this is a
2210 * POST; and the parameters are in the body, maybe scan there to find our server.
2211 * (unless headers overflowed the buffer?)
2212 */
2213 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2214 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
2215 s->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
2216 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2217 /* are there enough bytes here? total == l || r || rlim ?
2218 * len is unsigned, but eoh is int,
2219 * how many bytes of body have we received?
2220 * eoh is the first empty line of the header
2221 */
2222 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
2223 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
2224
2225 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2226 * We can't assume responsibility for the server's decision,
2227 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2228 * We also can't change our mind later, about which server to choose, so round robin.
2229 */
2230 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2231 struct hdr_ctx ctx;
2232 ctx.idx = 0;
2233 /* Expect is allowed in 1.1, look for it */
2234 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2235 if (ctx.idx != 0 &&
2236 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
2237 /* We can't reliablly stall and wait for data, because of
2238 * .NET clients that don't conform to rfc2616; so, no need for
2239 * the next block to check length expectations.
2240 * We could send 100 status back to the client, but then we need to
2241 * re-write headers, and send the message. And this isn't the right
2242 * place for that action.
2243 * TODO: support Expect elsewhere and delete this block.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002244 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002245 goto end_check_maybe_wait_for_body;
2246 }
2247
2248 if (likely(len > s->be->url_param_post_limit)) {
2249 /* nothing to do, we got enough */
2250 } else {
2251 /* limit implies we are supposed to need this many bytes
2252 * to find the parameter. Let's see how many bytes we can wait for.
2253 */
2254 long long hint = len;
2255 struct hdr_ctx ctx;
2256 ctx.idx = 0;
2257 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
2258 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2259 buffer_write_dis(req);
2260 req->analysers |= AN_REQ_HTTP_BODY;
2261 }
2262 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002263 ctx.idx = 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002264 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2265 /* now if we have a length, we'll take the hint */
2266 if (ctx.idx) {
2267 /* We have Content-Length */
2268 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
2269 hint = 0; /* parse failure, untrusted client */
2270 else {
2271 if (hint > 0)
2272 msg->hdr_content_len = hint;
2273 else
2274 hint = 0; /* bad client, sent negative length */
2275 }
2276 }
2277 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
2278 if (s->be->url_param_post_limit < hint)
2279 hint = s->be->url_param_post_limit;
2280 /* now do we really need to buffer more data? */
2281 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002282 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002283 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002284 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002285 /* else... There are no body bytes to wait for */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002286 }
2287 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002288 }
2289 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002290
Willy Tarreau59234e92008-11-30 23:51:27 +01002291 /*************************************************************
2292 * OK, that's finished for the headers. We have done what we *
2293 * could. Let's switch to the DATA state. *
2294 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002295
Willy Tarreau59234e92008-11-30 23:51:27 +01002296 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
2297 s->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002298
Willy Tarreau59234e92008-11-30 23:51:27 +01002299 /* When a connection is tarpitted, we use the tarpit timeout,
2300 * which may be the same as the connect timeout if unspecified.
2301 * If unset, then set it to zero because we really want it to
2302 * eventually expire. We build the tarpit as an analyser.
2303 */
2304 if (txn->flags & TX_CLTARPIT) {
2305 buffer_flush(s->req);
2306 /* flush the request so that we can drop the connection early
2307 * if the client closes first.
Willy Tarreau2a324282006-12-05 00:05:46 +01002308 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002309 buffer_write_dis(req);
2310 req->analysers |= AN_REQ_HTTP_TARPIT;
2311 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2312 if (!req->analyse_exp)
2313 req->analyse_exp = now_ms;
2314 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002315
Willy Tarreau59234e92008-11-30 23:51:27 +01002316 /* OK let's go on with the BODY now */
2317 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002318
Willy Tarreau59234e92008-11-30 23:51:27 +01002319 return_bad_req: /* let's centralize all bad requests */
Willy Tarreauf073a832009-03-01 23:21:47 +01002320 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2321 /* we detected a parsing error. We want to archive this request
2322 * in the dedicated proxy area for later troubleshooting.
2323 */
2324 struct error_snapshot *es = &s->fe->invalid_req;
2325 int maxlen = MIN(req->r - req->data + msg->som, sizeof(es->buf));
2326 memcpy(es->buf, req->data + msg->som, maxlen);
2327 es->pos = req->lr - req->data + msg->som;
2328 es->len = req->r - req->data + msg->som;
Willy Tarreaudefc52d2009-03-04 20:53:44 +01002329 es->when = date; // user-visible date
Willy Tarreauf073a832009-03-01 23:21:47 +01002330 es->sid = s->uniq_id;
2331 es->srv = s->srv;
2332 es->oe = s->be;
2333 es->src = s->cli_addr;
2334 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002335 txn->req.msg_state = HTTP_MSG_ERROR;
2336 txn->status = 400;
2337 req->analysers = 0;
2338 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2339 s->fe->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002340
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 return_prx_cond:
2342 if (!(s->flags & SN_ERR_MASK))
2343 s->flags |= SN_ERR_PRXCOND;
2344 if (!(s->flags & SN_FINST_MASK))
2345 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002346 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002347}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002348
Willy Tarreau60b85b02008-11-30 23:28:40 +01002349/* This function is an analyser which processes the HTTP tarpit. It always
2350 * returns zero, at the beginning because it prevents any other processing
2351 * from occurring, and at the end because it terminates the request.
2352 */
2353int http_process_tarpit(struct session *s, struct buffer *req)
2354{
2355 struct http_txn *txn = &s->txn;
2356
2357 /* This connection is being tarpitted. The CLIENT side has
2358 * already set the connect expiration date to the right
2359 * timeout. We just have to check that the client is still
2360 * there and that the timeout has not expired.
2361 */
2362 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
2363 !tick_is_expired(req->analyse_exp, now_ms))
2364 return 0;
2365
2366 /* We will set the queue timer to the time spent, just for
2367 * logging purposes. We fake a 500 server error, so that the
2368 * attacker will not suspect his connection has been tarpitted.
2369 * It will not cause trouble to the logs because we can exclude
2370 * the tarpitted connections by filtering on the 'PT' status flags.
2371 */
2372 trace_term(s, TT_HTTP_SRV_2);
2373 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2374
2375 txn->status = 500;
2376 if (req->flags != BF_READ_ERROR)
2377 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
2378
2379 req->analysers = 0;
2380 req->analyse_exp = TICK_ETERNITY;
2381
2382 s->fe->failed_req++;
2383 if (!(s->flags & SN_ERR_MASK))
2384 s->flags |= SN_ERR_PRXCOND;
2385 if (!(s->flags & SN_FINST_MASK))
2386 s->flags |= SN_FINST_T;
2387 return 0;
2388}
2389
Willy Tarreaud34af782008-11-30 23:36:37 +01002390/* This function is an analyser which processes the HTTP request body. It looks
2391 * for parameters to be used for the load balancing algorithm (url_param). It
2392 * must only be called after the standard HTTP request processing has occurred,
2393 * because it expects the request to be parsed. It returns zero if it needs to
2394 * read more data, or 1 once it has completed its analysis.
2395 */
2396int http_process_request_body(struct session *s, struct buffer *req)
2397{
2398 struct http_msg *msg = &s->txn.req;
2399 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2400 long long limit = s->be->url_param_post_limit;
2401 struct hdr_ctx ctx;
2402
2403 /* We have to parse the HTTP request body to find any required data.
2404 * "balance url_param check_post" should have been the only way to get
2405 * into this. We were brought here after HTTP header analysis, so all
2406 * related structures are ready.
2407 */
2408
2409 ctx.idx = 0;
2410
2411 /* now if we have a length, we'll take the hint */
2412 http_find_header2("Transfer-Encoding", 17, msg->sol, &s->txn.hdr_idx, &ctx);
2413 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2414 unsigned int chunk = 0;
2415 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2416 char c = msg->sol[body];
2417 if (ishex(c)) {
2418 unsigned int hex = toupper(c) - '0';
2419 if (hex > 9)
2420 hex -= 'A' - '9' - 1;
2421 chunk = (chunk << 4) | hex;
2422 } else
2423 break;
2424 body++;
2425 }
2426 if (body + 2 >= req->l) /* we want CRLF too */
2427 goto http_body_end; /* end of buffer? data missing! */
2428
2429 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
2430 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
2431
2432 body += 2; // skip CRLF
2433
2434 /* if we support more then one chunk here, we have to do it again when assigning server
2435 * 1. how much entity data do we have? new var
2436 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2437 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2438 */
2439
2440 if (chunk < limit)
2441 limit = chunk; /* only reading one chunk */
2442 } else {
2443 if (msg->hdr_content_len < limit)
2444 limit = msg->hdr_content_len;
2445 }
2446
2447 http_body_end:
2448 /* we leave once we know we have nothing left to do. This means that we have
2449 * enough bytes, or that we know we'll not get any more (buffer full, read
2450 * buffer closed).
2451 */
2452 if (req->l - body >= limit || /* enough bytes! */
2453 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
2454 tick_is_expired(req->analyse_exp, now_ms)) {
2455 /* The situation will not evolve, so let's give up on the analysis. */
2456 s->logs.tv_request = now; /* update the request timer to reflect full request */
2457 req->analysers &= ~AN_REQ_HTTP_BODY;
2458 req->analyse_exp = TICK_ETERNITY;
2459 return 1;
2460 }
2461 else {
2462 /* Not enough data. We'll re-use the http-request
2463 * timeout here. Ideally, we should set the timeout
2464 * relative to the accept() date. We just set the
2465 * request timeout once at the beginning of the
2466 * request.
2467 */
2468 buffer_write_dis(req);
2469 if (!tick_isset(req->analyse_exp))
2470 req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq);
2471 return 0;
2472 }
2473}
2474
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002475/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002476 * It normally returns zero, but may return 1 if it absolutely needs to be
2477 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002478 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002479 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002480 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002481int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002482{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002483 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002484 struct buffer *req = t->req;
2485 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002486
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002487 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 +02002488 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002489 t,
2490 rep,
2491 rep->rex, rep->wex,
2492 rep->flags,
2493 rep->l,
2494 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002495
Willy Tarreau2df28e82008-08-17 15:20:19 +02002496 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002497 /*
2498 * Now parse the partial (or complete) lines.
2499 * We will check the response syntax, and also join multi-line
2500 * headers. An index of all the lines will be elaborated while
2501 * parsing.
2502 *
2503 * For the parsing, we use a 28 states FSM.
2504 *
2505 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002506 * rep->data + rep->som = beginning of response
2507 * rep->data + rep->eoh = end of processed headers / start of current one
2508 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002509 * rep->lr = first non-visited byte
2510 * rep->r = end of data
2511 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002512
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002513 int cur_idx;
2514 struct http_msg *msg = &txn->rsp;
2515 struct proxy *cur_proxy;
2516
2517 if (likely(rep->lr < rep->r))
2518 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2519
2520 /* 1: we might have to print this header in debug mode */
2521 if (unlikely((global.mode & MODE_DEBUG) &&
2522 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2523 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2524 char *eol, *sol;
2525
2526 sol = rep->data + msg->som;
2527 eol = sol + msg->sl.rq.l;
2528 debug_hdr("srvrep", t, sol, eol);
2529
2530 sol += hdr_idx_first_pos(&txn->hdr_idx);
2531 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2532
2533 while (cur_idx) {
2534 eol = sol + txn->hdr_idx.v[cur_idx].len;
2535 debug_hdr("srvhdr", t, sol, eol);
2536 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2537 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002538 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002539 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002540
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002541 /*
2542 * Now we quickly check if we have found a full valid response.
2543 * If not so, we check the FD and buffer states before leaving.
2544 * A full response is indicated by the fact that we have seen
2545 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2546 * responses are checked first.
2547 *
2548 * Depending on whether the client is still there or not, we
2549 * may send an error response back or not. Note that normally
2550 * we should only check for HTTP status there, and check I/O
2551 * errors somewhere else.
2552 */
2553
2554 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002555 /* Invalid response */
2556 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002557 /* we detected a parsing error. We want to archive this response
2558 * in the dedicated proxy area for later troubleshooting.
2559 */
2560 struct error_snapshot *es = &t->be->invalid_rep;
2561 int maxlen = MIN(rep->r - rep->data + msg->som, sizeof(es->buf));
2562 memcpy(es->buf, rep->data + msg->som, maxlen);
2563 es->pos = rep->lr - rep->data + msg->som;
2564 es->len = rep->r - rep->data + msg->som;
Willy Tarreaudefc52d2009-03-04 20:53:44 +01002565 es->when = date; // user-visible date
Willy Tarreauf073a832009-03-01 23:21:47 +01002566 es->sid = t->uniq_id;
2567 es->srv = t->srv;
2568 es->oe = t->fe;
2569 es->src = t->cli_addr;
2570
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002571 hdr_response_bad:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002572 //buffer_shutr(rep);
2573 //buffer_shutw(req);
2574 //fd_delete(req->cons->fd);
2575 //req->cons->state = SI_ST_CLO;
2576 buffer_shutr_now(rep);
2577 buffer_shutw_now(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002578 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002579 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002580 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002581 //sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002582 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002583 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002584 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002585 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002586 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002587 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002588 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002589 if (!(t->flags & SN_FINST_MASK))
2590 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002591
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002592 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2593 // process_srv_queue(t->srv);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002594
Willy Tarreaudafde432008-08-17 01:00:46 +02002595 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002596 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002597 /* too large response does not fit in buffer. */
2598 else if (rep->flags & BF_FULL) {
2599 goto hdr_response_bad;
2600 }
2601 /* read error */
2602 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002603 buffer_shutr_now(rep);
2604 buffer_shutw_now(req);
2605 //fd_delete(req->cons->fd);
2606 //req->cons->state = SI_ST_CLO;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002607 //if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002608 //t->srv->cur_sess--;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002609 //t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002610 //sess_change_server(t, NULL);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002611 //}
2612 //t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002613 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002614 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002615 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002616 if (!(t->flags & SN_ERR_MASK))
2617 t->flags |= SN_ERR_SRVCL;
2618 if (!(t->flags & SN_FINST_MASK))
2619 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002620
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002621 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2622 // process_srv_queue(t->srv);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002623
Willy Tarreaudafde432008-08-17 01:00:46 +02002624 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002625 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002626 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002627 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002628 buffer_shutr_now(rep);
2629 buffer_shutw_now(req);
2630 //fd_delete(req->cons->fd);
2631 //req->cons->state = SI_ST_CLO;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002632 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002633 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002634 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002635 //sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002636 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002637 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002638 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002639 txn->status = 504;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002640 stream_int_return(rep->cons, error_message(t, HTTP_ERR_504));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002641 if (!(t->flags & SN_ERR_MASK))
2642 t->flags |= SN_ERR_SRVTO;
2643 if (!(t->flags & SN_FINST_MASK))
2644 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002645
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002646 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2647 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002648 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002649 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002650 /* write error to client, or close from server */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002651 else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002652 buffer_shutr_now(rep);
2653 buffer_shutw_now(req);
2654 //fd_delete(req->cons->fd);
2655 //req->cons->state = SI_ST_CLO;
2656 if (t->srv) {
2657 //t->srv->cur_sess--;
2658 t->srv->failed_resp++;
2659 //sess_change_server(t, NULL);
2660 }
2661 t->be->failed_resp++;
2662 rep->analysers = 0;
2663 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002664 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002665 if (!(t->flags & SN_ERR_MASK))
2666 t->flags |= SN_ERR_SRVCL;
2667 if (!(t->flags & SN_FINST_MASK))
2668 t->flags |= SN_FINST_H;
2669
2670 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2671 // process_srv_queue(t->srv);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002672
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002673 return 0;
2674 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02002675 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002676 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002677 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002678
Willy Tarreau21d2af32008-02-14 20:25:24 +01002679
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002680 /*****************************************************************
2681 * More interesting part now : we know that we have a complete *
2682 * response which at least looks like HTTP. We have an indicator *
2683 * of each header's length, so we can parse them quickly. *
2684 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002685
Willy Tarreau2df28e82008-08-17 15:20:19 +02002686 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002687
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002688 /* ensure we keep this pointer to the beginning of the message */
2689 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002690
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002691 /*
2692 * 1: get the status code and check for cacheability.
2693 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002694
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002695 t->logs.logwait &= ~LW_RESP;
2696 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002697
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002698 switch (txn->status) {
2699 case 200:
2700 case 203:
2701 case 206:
2702 case 300:
2703 case 301:
2704 case 410:
2705 /* RFC2616 @13.4:
2706 * "A response received with a status code of
2707 * 200, 203, 206, 300, 301 or 410 MAY be stored
2708 * by a cache (...) unless a cache-control
2709 * directive prohibits caching."
2710 *
2711 * RFC2616 @9.5: POST method :
2712 * "Responses to this method are not cacheable,
2713 * unless the response includes appropriate
2714 * Cache-Control or Expires header fields."
2715 */
2716 if (likely(txn->meth != HTTP_METH_POST) &&
2717 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2718 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2719 break;
2720 default:
2721 break;
2722 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002723
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002724 /*
2725 * 2: we may need to capture headers
2726 */
2727 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2728 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2729 txn->rsp.cap, t->fe->rsp_cap);
2730
2731 /*
2732 * 3: we will have to evaluate the filters.
2733 * As opposed to version 1.2, now they will be evaluated in the
2734 * filters order and not in the header order. This means that
2735 * each filter has to be validated among all headers.
2736 *
2737 * Filters are tried with ->be first, then with ->fe if it is
2738 * different from ->be.
2739 */
2740
2741 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2742
2743 cur_proxy = t->be;
2744 while (1) {
2745 struct proxy *rule_set = cur_proxy;
2746
2747 /* try headers filters */
2748 if (rule_set->rsp_exp != NULL) {
2749 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2750 return_bad_resp:
2751 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002752 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002753 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002754 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002755 }
2756 cur_proxy->failed_resp++;
2757 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002758 buffer_shutr_now(rep);
2759 buffer_shutw_now(req);
2760 //fd_delete(req->cons->fd);
2761 //req->cons->state = SI_ST_CLO;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002762 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002763 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002764 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002765 if (!(t->flags & SN_ERR_MASK))
2766 t->flags |= SN_ERR_PRXCOND;
2767 if (!(t->flags & SN_FINST_MASK))
2768 t->flags |= SN_FINST_H;
2769 /* We used to have a free connection slot. Since we'll never use it,
2770 * we have to inform the server that it may be used by another session.
2771 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002772 //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
2773 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002774 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002775 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002776 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002777
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002778 /* has the response been denied ? */
2779 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01002780 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002781 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002782 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002783 //sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002784 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002785 cur_proxy->denied_resp++;
2786 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002787 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002788
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002789 /* We might have to check for "Connection:" */
2790 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2791 !(t->flags & SN_CONN_CLOSED)) {
2792 char *cur_ptr, *cur_end, *cur_next;
2793 int cur_idx, old_idx, delta, val;
2794 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002795
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002796 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2797 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002798
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002799 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2800 cur_hdr = &txn->hdr_idx.v[cur_idx];
2801 cur_ptr = cur_next;
2802 cur_end = cur_ptr + cur_hdr->len;
2803 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002804
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002805 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2806 if (val) {
2807 /* 3 possibilities :
2808 * - we have already set Connection: close,
2809 * so we remove this line.
2810 * - we have not yet set Connection: close,
2811 * but this line indicates close. We leave
2812 * it untouched and set the flag.
2813 * - we have not yet set Connection: close,
2814 * and this line indicates non-close. We
2815 * replace it.
2816 */
2817 if (t->flags & SN_CONN_CLOSED) {
2818 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2819 txn->rsp.eoh += delta;
2820 cur_next += delta;
2821 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2822 txn->hdr_idx.used--;
2823 cur_hdr->len = 0;
2824 } else {
2825 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2826 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2827 "close", 5);
2828 cur_next += delta;
2829 cur_hdr->len += delta;
2830 txn->rsp.eoh += delta;
2831 }
2832 t->flags |= SN_CONN_CLOSED;
2833 }
2834 }
2835 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002836 }
2837 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002838
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002839 /* add response headers from the rule sets in the same order */
2840 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2841 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2842 rule_set->rsp_add[cur_idx])) < 0)
2843 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002844 }
2845
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002846 /* check whether we're already working on the frontend */
2847 if (cur_proxy == t->fe)
2848 break;
2849 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002850 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002851
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002852 /*
2853 * 4: check for server cookie.
2854 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002855 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002856 || (t->be->options & PR_O_CHK_CACHE))
2857 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002858
Willy Tarreaubaaee002006-06-26 02:48:02 +02002859
Willy Tarreaua15645d2007-03-18 16:22:39 +01002860 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002861 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002862 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002863 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2864 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002865
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002866 /*
2867 * 6: add server cookie in the response if needed
2868 */
2869 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2870 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2871 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002872
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002873 /* the server is known, it's not the one the client requested, we have to
2874 * insert a set-cookie here, except if we want to insert only on POST
2875 * requests and this one isn't. Note that servers which don't have cookies
2876 * (eg: some backup servers) will return a full cookie removal request.
2877 */
2878 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2879 t->be->cookie_name,
2880 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002881
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002882 if (t->be->cookie_domain)
2883 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002884
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002885 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2886 trash, len)) < 0)
2887 goto return_bad_resp;
2888 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002889
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002890 /* Here, we will tell an eventual cache on the client side that we don't
2891 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2892 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2893 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2894 */
2895 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002896
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002897 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2898
2899 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2900 "Cache-control: private", 22)) < 0)
2901 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002902 }
2903 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002904
Willy Tarreaubaaee002006-06-26 02:48:02 +02002905
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002906 /*
2907 * 7: check if result will be cacheable with a cookie.
2908 * We'll block the response if security checks have caught
2909 * nasty things such as a cacheable cookie.
2910 */
2911 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2912 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2913 (t->be->options & PR_O_CHK_CACHE)) {
2914
2915 /* we're in presence of a cacheable response containing
2916 * a set-cookie header. We'll block it as requested by
2917 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002918 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002919 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002920 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002921 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002922 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002923 }
2924 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002925
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002926 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2927 t->be->id, t->srv?t->srv->id:"<dispatch>");
2928 send_log(t->be, LOG_ALERT,
2929 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2930 t->be->id, t->srv?t->srv->id:"<dispatch>");
2931 goto return_srv_prx_502;
2932 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002933
2934 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002935 * 8: add "Connection: close" if needed and not yet set.
2936 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002937 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002938 if (!(t->flags & SN_CONN_CLOSED) &&
2939 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2940 if ((unlikely(msg->sl.st.v_l != 8) ||
2941 unlikely(req->data[msg->som + 7] != '0')) &&
2942 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2943 "Connection: close", 17)) < 0)
2944 goto return_bad_resp;
2945 t->flags |= SN_CONN_CLOSED;
2946 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002947
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002948 /*************************************************************
2949 * OK, that's finished for the headers. We have done what we *
2950 * could. Let's switch to the DATA state. *
2951 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002952
Willy Tarreaue393fe22008-08-16 22:18:07 +02002953 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002954 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002955
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002956#ifdef CONFIG_HAP_TCPSPLICE
2957 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
2958 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002959 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002960 }
2961#endif
2962 /* if the user wants to log as soon as possible, without counting
2963 * bytes from the server, then this is the right moment. We have
2964 * to temporarily assign bytes_out to log what we currently have.
2965 */
2966 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2967 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
2968 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002969 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002970 t->logs.bytes_out = 0;
2971 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002972
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002973 /* Note: we must not try to cheat by jumping directly to DATA,
2974 * otherwise we would not let the client side wake up.
2975 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002976
Willy Tarreaudafde432008-08-17 01:00:46 +02002977 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002978 }
Willy Tarreaudafde432008-08-17 01:00:46 +02002979
Willy Tarreau2df28e82008-08-17 15:20:19 +02002980 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2981 * probably reduce one day's debugging session.
2982 */
2983#ifdef DEBUG_DEV
2984 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
2985 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2986 __FILE__, __LINE__, rep->analysers);
2987 ABORT_NOW();
2988 }
2989#endif
2990 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002991 return 0;
2992}
Willy Tarreaua15645d2007-03-18 16:22:39 +01002993
Willy Tarreaubaaee002006-06-26 02:48:02 +02002994/*
2995 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02002996 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02002997 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
2998 * buffer, which it uses to keep on being called when there is free space in
Willy Tarreau01bf8672008-12-07 18:03:29 +01002999 * the buffer, or simply by letting an empty buffer upon return.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003000 */
Willy Tarreau01bf8672008-12-07 18:03:29 +01003001void produce_content(struct session *s, struct buffer *rep)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003002{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003003 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau01bf8672008-12-07 18:03:29 +01003004 buffer_stop_hijack(rep);
3005 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003006 }
3007 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003008 /* dump server statistics */
Willy Tarreau0a464892008-12-07 18:30:00 +01003009 int ret = stats_dump_http(s, rep, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003010 if (ret >= 0)
Willy Tarreau01bf8672008-12-07 18:03:29 +01003011 return;
Willy Tarreau91861262007-10-17 17:06:05 +02003012 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003013 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003014
Willy Tarreau91861262007-10-17 17:06:05 +02003015 /* unknown data source or internal error */
3016 s->txn.status = 500;
Willy Tarreau01bf8672008-12-07 18:03:29 +01003017 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003018 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02003019 if (!(s->flags & SN_ERR_MASK))
3020 s->flags |= SN_ERR_PRXCOND;
3021 if (!(s->flags & SN_FINST_MASK))
3022 s->flags |= SN_FINST_R;
Willy Tarreau01bf8672008-12-07 18:03:29 +01003023 buffer_stop_hijack(rep);
3024 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003025}
3026
3027
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003028/* Iterate the same filter through all request headers.
3029 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003030 * Since it can manage the switch to another backend, it updates the per-proxy
3031 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003032 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003033int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003034{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003035 char term;
3036 char *cur_ptr, *cur_end, *cur_next;
3037 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003038 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003039 struct hdr_idx_elem *cur_hdr;
3040 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003041
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003042 last_hdr = 0;
3043
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003044 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003045 old_idx = 0;
3046
3047 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003048 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003049 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003050 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003051 (exp->action == ACT_ALLOW ||
3052 exp->action == ACT_DENY ||
3053 exp->action == ACT_TARPIT))
3054 return 0;
3055
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003056 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003057 if (!cur_idx)
3058 break;
3059
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003060 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003061 cur_ptr = cur_next;
3062 cur_end = cur_ptr + cur_hdr->len;
3063 cur_next = cur_end + cur_hdr->cr + 1;
3064
3065 /* Now we have one header between cur_ptr and cur_end,
3066 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003067 */
3068
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003069 /* The annoying part is that pattern matching needs
3070 * that we modify the contents to null-terminate all
3071 * strings before testing them.
3072 */
3073
3074 term = *cur_end;
3075 *cur_end = '\0';
3076
3077 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3078 switch (exp->action) {
3079 case ACT_SETBE:
3080 /* It is not possible to jump a second time.
3081 * FIXME: should we return an HTTP/500 here so that
3082 * the admin knows there's a problem ?
3083 */
3084 if (t->be != t->fe)
3085 break;
3086
3087 /* Swithing Proxy */
3088 t->be = (struct proxy *) exp->replace;
3089
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003090 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003091 * because we have associated req_cap and rsp_cap to the
3092 * frontend, and the beconn will be updated later.
3093 */
3094
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003095 t->rep->rto = t->req->wto = t->be->timeout.server;
3096 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003097 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003098 last_hdr = 1;
3099 break;
3100
3101 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003102 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003103 last_hdr = 1;
3104 break;
3105
3106 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003107 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003108 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003109 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003110 break;
3111
3112 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003113 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003114 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003115 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003116 break;
3117
3118 case ACT_REPLACE:
3119 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3120 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3121 /* FIXME: if the user adds a newline in the replacement, the
3122 * index will not be recalculated for now, and the new line
3123 * will not be counted as a new header.
3124 */
3125
3126 cur_end += delta;
3127 cur_next += delta;
3128 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003129 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003130 break;
3131
3132 case ACT_REMOVE:
3133 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3134 cur_next += delta;
3135
3136 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003137 txn->req.eoh += delta;
3138 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3139 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003140 cur_hdr->len = 0;
3141 cur_end = NULL; /* null-term has been rewritten */
3142 break;
3143
3144 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003145 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003146 if (cur_end)
3147 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003148
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003149 /* keep the link from this header to next one in case of later
3150 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003151 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003152 old_idx = cur_idx;
3153 }
3154 return 0;
3155}
3156
3157
3158/* Apply the filter to the request line.
3159 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3160 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003161 * Since it can manage the switch to another backend, it updates the per-proxy
3162 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003163 */
3164int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3165{
3166 char term;
3167 char *cur_ptr, *cur_end;
3168 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003169 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003170 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003171
Willy Tarreau58f10d72006-12-04 02:26:12 +01003172
Willy Tarreau3d300592007-03-18 18:34:41 +01003173 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003174 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003175 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003176 (exp->action == ACT_ALLOW ||
3177 exp->action == ACT_DENY ||
3178 exp->action == ACT_TARPIT))
3179 return 0;
3180 else if (exp->action == ACT_REMOVE)
3181 return 0;
3182
3183 done = 0;
3184
Willy Tarreau9cdde232007-05-02 20:58:19 +02003185 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003186 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003187
3188 /* Now we have the request line between cur_ptr and cur_end */
3189
3190 /* The annoying part is that pattern matching needs
3191 * that we modify the contents to null-terminate all
3192 * strings before testing them.
3193 */
3194
3195 term = *cur_end;
3196 *cur_end = '\0';
3197
3198 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3199 switch (exp->action) {
3200 case ACT_SETBE:
3201 /* It is not possible to jump a second time.
3202 * FIXME: should we return an HTTP/500 here so that
3203 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003204 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003205 if (t->be != t->fe)
3206 break;
3207
3208 /* Swithing Proxy */
3209 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003210
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003211 /* right now, the backend switch is not too much complicated
3212 * because we have associated req_cap and rsp_cap to the
3213 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003214 */
3215
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003216 t->rep->rto = t->req->wto = t->be->timeout.server;
3217 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003218 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003219 done = 1;
3220 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003221
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003222 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003223 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003224 done = 1;
3225 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003226
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003227 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003228 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003229 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003230 done = 1;
3231 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003232
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003233 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003234 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003235 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003236 done = 1;
3237 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003238
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003239 case ACT_REPLACE:
3240 *cur_end = term; /* restore the string terminator */
3241 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3242 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3243 /* FIXME: if the user adds a newline in the replacement, the
3244 * index will not be recalculated for now, and the new line
3245 * will not be counted as a new header.
3246 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003247
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003248 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003249 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003250
Willy Tarreau9cdde232007-05-02 20:58:19 +02003251 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003252 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003253 HTTP_MSG_RQMETH,
3254 cur_ptr, cur_end + 1,
3255 NULL, NULL);
3256 if (unlikely(!cur_end))
3257 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003258
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003259 /* we have a full request and we know that we have either a CR
3260 * or an LF at <ptr>.
3261 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003262 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3263 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003264 /* there is no point trying this regex on headers */
3265 return 1;
3266 }
3267 }
3268 *cur_end = term; /* restore the string terminator */
3269 return done;
3270}
Willy Tarreau97de6242006-12-27 17:18:38 +01003271
Willy Tarreau58f10d72006-12-04 02:26:12 +01003272
Willy Tarreau58f10d72006-12-04 02:26:12 +01003273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003274/*
3275 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3276 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003277 * unparsable request. Since it can manage the switch to another backend, it
3278 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003279 */
3280int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3281{
Willy Tarreau3d300592007-03-18 18:34:41 +01003282 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003283 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003284 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003285 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003287 /*
3288 * The interleaving of transformations and verdicts
3289 * makes it difficult to decide to continue or stop
3290 * the evaluation.
3291 */
3292
Willy Tarreau3d300592007-03-18 18:34:41 +01003293 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003294 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3295 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3296 exp = exp->next;
3297 continue;
3298 }
3299
3300 /* Apply the filter to the request line. */
3301 ret = apply_filter_to_req_line(t, req, exp);
3302 if (unlikely(ret < 0))
3303 return -1;
3304
3305 if (likely(ret == 0)) {
3306 /* The filter did not match the request, it can be
3307 * iterated through all headers.
3308 */
3309 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003310 }
3311 exp = exp->next;
3312 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003313 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003314}
3315
3316
Willy Tarreaua15645d2007-03-18 16:22:39 +01003317
Willy Tarreau58f10d72006-12-04 02:26:12 +01003318/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003319 * Manage client-side cookie. It can impact performance by about 2% so it is
3320 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003321 */
3322void manage_client_side_cookies(struct session *t, struct buffer *req)
3323{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003324 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003325 char *p1, *p2, *p3, *p4;
3326 char *del_colon, *del_cookie, *colon;
3327 int app_cookies;
3328
3329 appsess *asession_temp = NULL;
3330 appsess local_asession;
3331
3332 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003333 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003334
Willy Tarreau2a324282006-12-05 00:05:46 +01003335 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003336 * we start with the start line.
3337 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003338 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003339 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003340
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003341 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003342 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003343 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003344
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003345 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003346 cur_ptr = cur_next;
3347 cur_end = cur_ptr + cur_hdr->len;
3348 cur_next = cur_end + cur_hdr->cr + 1;
3349
3350 /* We have one full header between cur_ptr and cur_end, and the
3351 * next header starts at cur_next. We're only interested in
3352 * "Cookie:" headers.
3353 */
3354
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003355 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3356 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003357 old_idx = cur_idx;
3358 continue;
3359 }
3360
3361 /* Now look for cookies. Conforming to RFC2109, we have to support
3362 * attributes whose name begin with a '$', and associate them with
3363 * the right cookie, if we want to delete this cookie.
3364 * So there are 3 cases for each cookie read :
3365 * 1) it's a special attribute, beginning with a '$' : ignore it.
3366 * 2) it's a server id cookie that we *MAY* want to delete : save
3367 * some pointers on it (last semi-colon, beginning of cookie...)
3368 * 3) it's an application cookie : we *MAY* have to delete a previous
3369 * "special" cookie.
3370 * At the end of loop, if a "special" cookie remains, we may have to
3371 * remove it. If no application cookie persists in the header, we
3372 * *MUST* delete it
3373 */
3374
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003375 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003376
Willy Tarreau58f10d72006-12-04 02:26:12 +01003377 /* del_cookie == NULL => nothing to be deleted */
3378 del_colon = del_cookie = NULL;
3379 app_cookies = 0;
3380
3381 while (p1 < cur_end) {
3382 /* skip spaces and colons, but keep an eye on these ones */
3383 while (p1 < cur_end) {
3384 if (*p1 == ';' || *p1 == ',')
3385 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003386 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003387 break;
3388 p1++;
3389 }
3390
3391 if (p1 == cur_end)
3392 break;
3393
3394 /* p1 is at the beginning of the cookie name */
3395 p2 = p1;
3396 while (p2 < cur_end && *p2 != '=')
3397 p2++;
3398
3399 if (p2 == cur_end)
3400 break;
3401
3402 p3 = p2 + 1; /* skips the '=' sign */
3403 if (p3 == cur_end)
3404 break;
3405
3406 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003407 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003408 p4++;
3409
3410 /* here, we have the cookie name between p1 and p2,
3411 * and its value between p3 and p4.
3412 * we can process it :
3413 *
3414 * Cookie: NAME=VALUE;
3415 * | || || |
3416 * | || || +--> p4
3417 * | || |+-------> p3
3418 * | || +--------> p2
3419 * | |+------------> p1
3420 * | +-------------> colon
3421 * +--------------------> cur_ptr
3422 */
3423
3424 if (*p1 == '$') {
3425 /* skip this one */
3426 }
3427 else {
3428 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003429 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003430 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003431 (p4 - p1 >= t->fe->capture_namelen) &&
3432 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003433 int log_len = p4 - p1;
3434
Willy Tarreau086b3b42007-05-13 21:45:51 +02003435 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003436 Alert("HTTP logging : out of memory.\n");
3437 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003438 if (log_len > t->fe->capture_len)
3439 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003440 memcpy(txn->cli_cookie, p1, log_len);
3441 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003442 }
3443 }
3444
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003445 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3446 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003447 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003448 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003449 char *delim;
3450
3451 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3452 * have the server ID betweek p3 and delim, and the original cookie between
3453 * delim+1 and p4. Otherwise, delim==p4 :
3454 *
3455 * Cookie: NAME=SRV~VALUE;
3456 * | || || | |
3457 * | || || | +--> p4
3458 * | || || +--------> delim
3459 * | || |+-----------> p3
3460 * | || +------------> p2
3461 * | |+----------------> p1
3462 * | +-----------------> colon
3463 * +------------------------> cur_ptr
3464 */
3465
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003466 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003467 for (delim = p3; delim < p4; delim++)
3468 if (*delim == COOKIE_DELIM)
3469 break;
3470 }
3471 else
3472 delim = p4;
3473
3474
3475 /* Here, we'll look for the first running server which supports the cookie.
3476 * This allows to share a same cookie between several servers, for example
3477 * to dedicate backup servers to specific servers only.
3478 * However, to prevent clients from sticking to cookie-less backup server
3479 * when they have incidentely learned an empty cookie, we simply ignore
3480 * empty cookies and mark them as invalid.
3481 */
3482 if (delim == p3)
3483 srv = NULL;
3484
3485 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003486 if (srv->cookie && (srv->cklen == delim - p3) &&
3487 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003488 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003489 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003490 txn->flags &= ~TX_CK_MASK;
3491 txn->flags |= TX_CK_VALID;
3492 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003493 t->srv = srv;
3494 break;
3495 } else {
3496 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003497 txn->flags &= ~TX_CK_MASK;
3498 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003499 }
3500 }
3501 srv = srv->next;
3502 }
3503
Willy Tarreau3d300592007-03-18 18:34:41 +01003504 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003505 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003506 txn->flags &= ~TX_CK_MASK;
3507 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003508 }
3509
3510 /* depending on the cookie mode, we may have to either :
3511 * - delete the complete cookie if we're in insert+indirect mode, so that
3512 * the server never sees it ;
3513 * - remove the server id from the cookie value, and tag the cookie as an
3514 * application cookie so that it does not get accidentely removed later,
3515 * if we're in cookie prefix mode
3516 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003517 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003518 int delta; /* negative */
3519
3520 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3521 p4 += delta;
3522 cur_end += delta;
3523 cur_next += delta;
3524 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003525 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003526
3527 del_cookie = del_colon = NULL;
3528 app_cookies++; /* protect the header from deletion */
3529 }
3530 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003531 (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 +01003532 del_cookie = p1;
3533 del_colon = colon;
3534 }
3535 } else {
3536 /* now we know that we must keep this cookie since it's
3537 * not ours. But if we wanted to delete our cookie
3538 * earlier, we cannot remove the complete header, but we
3539 * can remove the previous block itself.
3540 */
3541 app_cookies++;
3542
3543 if (del_cookie != NULL) {
3544 int delta; /* negative */
3545
3546 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3547 p4 += delta;
3548 cur_end += delta;
3549 cur_next += delta;
3550 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003551 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003552 del_cookie = del_colon = NULL;
3553 }
3554 }
3555
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003556 if ((t->be->appsession_name != NULL) &&
3557 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003558 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003559
Willy Tarreau58f10d72006-12-04 02:26:12 +01003560 /* Cool... it's the right one */
3561
3562 asession_temp = &local_asession;
3563
Willy Tarreau63963c62007-05-13 21:29:55 +02003564 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003565 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3566 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3567 return;
3568 }
3569
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003570 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3571 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003572 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003573
Willy Tarreau58f10d72006-12-04 02:26:12 +01003574 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003575 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3576 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003577 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003578 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003579 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003580 Alert("Not enough memory process_cli():asession:calloc().\n");
3581 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3582 return;
3583 }
3584
3585 asession_temp->sessid = local_asession.sessid;
3586 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003587 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02003588 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003589 } else {
3590 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003591 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003592 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003593 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003594 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003595 Alert("Found Application Session without matching server.\n");
3596 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003597 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003598 while (srv) {
3599 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003600 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003601 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003602 txn->flags &= ~TX_CK_MASK;
3603 txn->flags |= TX_CK_VALID;
3604 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003605 t->srv = srv;
3606 break;
3607 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01003608 txn->flags &= ~TX_CK_MASK;
3609 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003610 }
3611 }
3612 srv = srv->next;
3613 }/* end while(srv) */
3614 }/* end else if server == NULL */
3615
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003616 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003617 asession_temp->request_count++;
3618#if defined(DEBUG_HASH)
3619 Alert("manage_client_side_cookies\n");
3620 appsession_hash_dump(&(t->be->htbl_proxy));
3621#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01003622 }/* end if ((t->proxy->appsession_name != NULL) ... */
3623 }
3624
3625 /* we'll have to look for another cookie ... */
3626 p1 = p4;
3627 } /* while (p1 < cur_end) */
3628
3629 /* There's no more cookie on this line.
3630 * We may have marked the last one(s) for deletion.
3631 * We must do this now in two ways :
3632 * - if there is no app cookie, we simply delete the header ;
3633 * - if there are app cookies, we must delete the end of the
3634 * string properly, including the colon/semi-colon before
3635 * the cookie name.
3636 */
3637 if (del_cookie != NULL) {
3638 int delta;
3639 if (app_cookies) {
3640 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
3641 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003642 cur_hdr->len += delta;
3643 } else {
3644 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003645
3646 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003647 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3648 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003649 cur_hdr->len = 0;
3650 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01003651 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003652 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003653 }
3654
3655 /* keep the link from this header to next one */
3656 old_idx = cur_idx;
3657 } /* end of cookie processing on this header */
3658}
3659
3660
Willy Tarreaua15645d2007-03-18 16:22:39 +01003661/* Iterate the same filter through all response headers contained in <rtr>.
3662 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3663 */
3664int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3665{
3666 char term;
3667 char *cur_ptr, *cur_end, *cur_next;
3668 int cur_idx, old_idx, last_hdr;
3669 struct http_txn *txn = &t->txn;
3670 struct hdr_idx_elem *cur_hdr;
3671 int len, delta;
3672
3673 last_hdr = 0;
3674
3675 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3676 old_idx = 0;
3677
3678 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003679 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003680 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003681 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003682 (exp->action == ACT_ALLOW ||
3683 exp->action == ACT_DENY))
3684 return 0;
3685
3686 cur_idx = txn->hdr_idx.v[old_idx].next;
3687 if (!cur_idx)
3688 break;
3689
3690 cur_hdr = &txn->hdr_idx.v[cur_idx];
3691 cur_ptr = cur_next;
3692 cur_end = cur_ptr + cur_hdr->len;
3693 cur_next = cur_end + cur_hdr->cr + 1;
3694
3695 /* Now we have one header between cur_ptr and cur_end,
3696 * and the next header starts at cur_next.
3697 */
3698
3699 /* The annoying part is that pattern matching needs
3700 * that we modify the contents to null-terminate all
3701 * strings before testing them.
3702 */
3703
3704 term = *cur_end;
3705 *cur_end = '\0';
3706
3707 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3708 switch (exp->action) {
3709 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003710 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003711 last_hdr = 1;
3712 break;
3713
3714 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003715 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003716 last_hdr = 1;
3717 break;
3718
3719 case ACT_REPLACE:
3720 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3721 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3722 /* FIXME: if the user adds a newline in the replacement, the
3723 * index will not be recalculated for now, and the new line
3724 * will not be counted as a new header.
3725 */
3726
3727 cur_end += delta;
3728 cur_next += delta;
3729 cur_hdr->len += delta;
3730 txn->rsp.eoh += delta;
3731 break;
3732
3733 case ACT_REMOVE:
3734 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3735 cur_next += delta;
3736
3737 /* FIXME: this should be a separate function */
3738 txn->rsp.eoh += delta;
3739 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3740 txn->hdr_idx.used--;
3741 cur_hdr->len = 0;
3742 cur_end = NULL; /* null-term has been rewritten */
3743 break;
3744
3745 }
3746 }
3747 if (cur_end)
3748 *cur_end = term; /* restore the string terminator */
3749
3750 /* keep the link from this header to next one in case of later
3751 * removal of next header.
3752 */
3753 old_idx = cur_idx;
3754 }
3755 return 0;
3756}
3757
3758
3759/* Apply the filter to the status line in the response buffer <rtr>.
3760 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3761 * or -1 if a replacement resulted in an invalid status line.
3762 */
3763int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3764{
3765 char term;
3766 char *cur_ptr, *cur_end;
3767 int done;
3768 struct http_txn *txn = &t->txn;
3769 int len, delta;
3770
3771
Willy Tarreau3d300592007-03-18 18:34:41 +01003772 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003773 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003774 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003775 (exp->action == ACT_ALLOW ||
3776 exp->action == ACT_DENY))
3777 return 0;
3778 else if (exp->action == ACT_REMOVE)
3779 return 0;
3780
3781 done = 0;
3782
Willy Tarreau9cdde232007-05-02 20:58:19 +02003783 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003784 cur_end = cur_ptr + txn->rsp.sl.rq.l;
3785
3786 /* Now we have the status line between cur_ptr and cur_end */
3787
3788 /* The annoying part is that pattern matching needs
3789 * that we modify the contents to null-terminate all
3790 * strings before testing them.
3791 */
3792
3793 term = *cur_end;
3794 *cur_end = '\0';
3795
3796 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3797 switch (exp->action) {
3798 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003799 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003800 done = 1;
3801 break;
3802
3803 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003804 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003805 done = 1;
3806 break;
3807
3808 case ACT_REPLACE:
3809 *cur_end = term; /* restore the string terminator */
3810 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3811 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3812 /* FIXME: if the user adds a newline in the replacement, the
3813 * index will not be recalculated for now, and the new line
3814 * will not be counted as a new header.
3815 */
3816
3817 txn->rsp.eoh += delta;
3818 cur_end += delta;
3819
Willy Tarreau9cdde232007-05-02 20:58:19 +02003820 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003821 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02003822 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003823 cur_ptr, cur_end + 1,
3824 NULL, NULL);
3825 if (unlikely(!cur_end))
3826 return -1;
3827
3828 /* we have a full respnse and we know that we have either a CR
3829 * or an LF at <ptr>.
3830 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003831 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003832 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
3833 /* there is no point trying this regex on headers */
3834 return 1;
3835 }
3836 }
3837 *cur_end = term; /* restore the string terminator */
3838 return done;
3839}
3840
3841
3842
3843/*
3844 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
3845 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3846 * unparsable response.
3847 */
3848int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3849{
Willy Tarreau3d300592007-03-18 18:34:41 +01003850 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003851 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003852 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003853 int ret;
3854
3855 /*
3856 * The interleaving of transformations and verdicts
3857 * makes it difficult to decide to continue or stop
3858 * the evaluation.
3859 */
3860
Willy Tarreau3d300592007-03-18 18:34:41 +01003861 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003862 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3863 exp->action == ACT_PASS)) {
3864 exp = exp->next;
3865 continue;
3866 }
3867
3868 /* Apply the filter to the status line. */
3869 ret = apply_filter_to_sts_line(t, rtr, exp);
3870 if (unlikely(ret < 0))
3871 return -1;
3872
3873 if (likely(ret == 0)) {
3874 /* The filter did not match the response, it can be
3875 * iterated through all headers.
3876 */
3877 apply_filter_to_resp_headers(t, rtr, exp);
3878 }
3879 exp = exp->next;
3880 }
3881 return 0;
3882}
3883
3884
3885
3886/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003887 * Manage server-side cookies. It can impact performance by about 2% so it is
3888 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003889 */
3890void manage_server_side_cookies(struct session *t, struct buffer *rtr)
3891{
3892 struct http_txn *txn = &t->txn;
3893 char *p1, *p2, *p3, *p4;
3894
3895 appsess *asession_temp = NULL;
3896 appsess local_asession;
3897
3898 char *cur_ptr, *cur_end, *cur_next;
3899 int cur_idx, old_idx, delta;
3900
Willy Tarreaua15645d2007-03-18 16:22:39 +01003901 /* Iterate through the headers.
3902 * we start with the start line.
3903 */
3904 old_idx = 0;
3905 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3906
3907 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3908 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003909 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003910
3911 cur_hdr = &txn->hdr_idx.v[cur_idx];
3912 cur_ptr = cur_next;
3913 cur_end = cur_ptr + cur_hdr->len;
3914 cur_next = cur_end + cur_hdr->cr + 1;
3915
3916 /* We have one full header between cur_ptr and cur_end, and the
3917 * next header starts at cur_next. We're only interested in
3918 * "Cookie:" headers.
3919 */
3920
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003921 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
3922 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003923 old_idx = cur_idx;
3924 continue;
3925 }
3926
3927 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01003928 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003929
3930
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003931 /* maybe we only wanted to see if there was a set-cookie. Note that
3932 * the cookie capture is declared in the fronend.
3933 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003934 if (t->be->cookie_name == NULL &&
3935 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003936 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003937 return;
3938
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003939 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003940
3941 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003942 if (p1 == cur_end || *p1 == ';') /* end of cookie */
3943 break;
3944
3945 /* p1 is at the beginning of the cookie name */
3946 p2 = p1;
3947
3948 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
3949 p2++;
3950
3951 if (p2 == cur_end || *p2 == ';') /* next cookie */
3952 break;
3953
3954 p3 = p2 + 1; /* skip the '=' sign */
3955 if (p3 == cur_end)
3956 break;
3957
3958 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003959 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01003960 p4++;
3961
3962 /* here, we have the cookie name between p1 and p2,
3963 * and its value between p3 and p4.
3964 * we can process it.
3965 */
3966
3967 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003968 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003969 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003970 (p4 - p1 >= t->fe->capture_namelen) &&
3971 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003972 int log_len = p4 - p1;
3973
Willy Tarreau086b3b42007-05-13 21:45:51 +02003974 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003975 Alert("HTTP logging : out of memory.\n");
3976 }
3977
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003978 if (log_len > t->fe->capture_len)
3979 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003980 memcpy(txn->srv_cookie, p1, log_len);
3981 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003982 }
3983
3984 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003985 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3986 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003987 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01003988 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003989
3990 /* If the cookie is in insert mode on a known server, we'll delete
3991 * this occurrence because we'll insert another one later.
3992 * We'll delete it too if the "indirect" option is set and we're in
3993 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003994 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
3995 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003996 /* this header must be deleted */
3997 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3998 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3999 txn->hdr_idx.used--;
4000 cur_hdr->len = 0;
4001 cur_next += delta;
4002 txn->rsp.eoh += delta;
4003
Willy Tarreau3d300592007-03-18 18:34:41 +01004004 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004005 }
4006 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004007 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004008 /* replace bytes p3->p4 with the cookie name associated
4009 * with this server since we know it.
4010 */
4011 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4012 cur_hdr->len += delta;
4013 cur_next += delta;
4014 txn->rsp.eoh += delta;
4015
Willy Tarreau3d300592007-03-18 18:34:41 +01004016 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004017 }
4018 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004019 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004020 /* insert the cookie name associated with this server
4021 * before existing cookie, and insert a delimitor between them..
4022 */
4023 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4024 cur_hdr->len += delta;
4025 cur_next += delta;
4026 txn->rsp.eoh += delta;
4027
4028 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004029 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004030 }
4031 }
4032 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004033 else if ((t->be->appsession_name != NULL) &&
4034 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004035
4036 /* Cool... it's the right one */
4037
4038 size_t server_id_len = strlen(t->srv->id) + 1;
4039 asession_temp = &local_asession;
4040
Willy Tarreau63963c62007-05-13 21:29:55 +02004041 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004042 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4043 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4044 return;
4045 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004046 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4047 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004048 asession_temp->serverid = NULL;
4049
4050 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004051 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4052 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004053 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004054 Alert("Not enough Memory process_srv():asession:calloc().\n");
4055 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4056 return;
4057 }
4058 asession_temp->sessid = local_asession.sessid;
4059 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004060 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004061 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4062 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004063 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004064 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004065 }
4066
Willy Tarreaua15645d2007-03-18 16:22:39 +01004067 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004068 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004069 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4070 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4071 return;
4072 }
4073 asession_temp->serverid[0] = '\0';
4074 }
4075
4076 if (asession_temp->serverid[0] == '\0')
4077 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4078
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004079 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004080 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004081#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004082 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004083 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004084#endif
4085 }/* end if ((t->proxy->appsession_name != NULL) ... */
4086 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4087 } /* we're now at the end of the cookie value */
4088
4089 /* keep the link from this header to next one */
4090 old_idx = cur_idx;
4091 } /* end of cookie processing on this header */
4092}
4093
4094
4095
4096/*
4097 * Check if response is cacheable or not. Updates t->flags.
4098 */
4099void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4100{
4101 struct http_txn *txn = &t->txn;
4102 char *p1, *p2;
4103
4104 char *cur_ptr, *cur_end, *cur_next;
4105 int cur_idx;
4106
Willy Tarreau5df51872007-11-25 16:20:08 +01004107 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004108 return;
4109
4110 /* Iterate through the headers.
4111 * we start with the start line.
4112 */
4113 cur_idx = 0;
4114 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4115
4116 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4117 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004118 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004119
4120 cur_hdr = &txn->hdr_idx.v[cur_idx];
4121 cur_ptr = cur_next;
4122 cur_end = cur_ptr + cur_hdr->len;
4123 cur_next = cur_end + cur_hdr->cr + 1;
4124
4125 /* We have one full header between cur_ptr and cur_end, and the
4126 * next header starts at cur_next. We're only interested in
4127 * "Cookie:" headers.
4128 */
4129
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004130 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4131 if (val) {
4132 if ((cur_end - (cur_ptr + val) >= 8) &&
4133 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4134 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4135 return;
4136 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004137 }
4138
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004139 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4140 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004141 continue;
4142
4143 /* OK, right now we know we have a cache-control header at cur_ptr */
4144
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004145 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004146
4147 if (p1 >= cur_end) /* no more info */
4148 continue;
4149
4150 /* p1 is at the beginning of the value */
4151 p2 = p1;
4152
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004153 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004154 p2++;
4155
4156 /* we have a complete value between p1 and p2 */
4157 if (p2 < cur_end && *p2 == '=') {
4158 /* we have something of the form no-cache="set-cookie" */
4159 if ((cur_end - p1 >= 21) &&
4160 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4161 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004162 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004163 continue;
4164 }
4165
4166 /* OK, so we know that either p2 points to the end of string or to a comma */
4167 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4168 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4169 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4170 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004171 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004172 return;
4173 }
4174
4175 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004176 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004177 continue;
4178 }
4179 }
4180}
4181
4182
Willy Tarreau58f10d72006-12-04 02:26:12 +01004183/*
4184 * Try to retrieve a known appsession in the URI, then the associated server.
4185 * If the server is found, it's assigned to the session.
4186 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004187void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004188{
Willy Tarreau3d300592007-03-18 18:34:41 +01004189 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004190 appsess *asession_temp = NULL;
4191 appsess local_asession;
4192 char *request_line;
4193
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004194 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004195 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004196 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004197 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004198 return;
4199
4200 /* skip ';' */
4201 request_line++;
4202
4203 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004204 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004205 return;
4206
4207 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004208 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004209
4210 /* First try if we already have an appsession */
4211 asession_temp = &local_asession;
4212
Willy Tarreau63963c62007-05-13 21:29:55 +02004213 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004214 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4215 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4216 return;
4217 }
4218
4219 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004220 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4221 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004222 asession_temp->serverid = NULL;
4223
4224 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004225 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4226 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004227 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004228 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004229 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004230 Alert("Not enough memory process_cli():asession:calloc().\n");
4231 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4232 return;
4233 }
4234 asession_temp->sessid = local_asession.sessid;
4235 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004236 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004237 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004238 }
4239 else {
4240 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004241 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004242 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004243
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004244 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004245 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004246
Willy Tarreau58f10d72006-12-04 02:26:12 +01004247#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004248 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004249 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004250#endif
4251 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004252 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004253 Alert("Found Application Session without matching server.\n");
4254 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004255 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004256 while (srv) {
4257 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004258 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004259 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004260 txn->flags &= ~TX_CK_MASK;
4261 txn->flags |= TX_CK_VALID;
4262 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004263 t->srv = srv;
4264 break;
4265 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004266 txn->flags &= ~TX_CK_MASK;
4267 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004268 }
4269 }
4270 srv = srv->next;
4271 }
4272 }
4273}
4274
4275
Willy Tarreaub2513902006-12-17 14:52:38 +01004276/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004277 * In a GET or HEAD request, check if the requested URI matches the stats uri
4278 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004279 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004280 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004281 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004282 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004283 *
4284 * Returns 1 if the session's state changes, otherwise 0.
4285 */
4286int stats_check_uri_auth(struct session *t, struct proxy *backend)
4287{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004288 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004289 struct uri_auth *uri_auth = backend->uri_auth;
4290 struct user_auth *user;
4291 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004292 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004293
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004294 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
4295
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004296 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004297 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004298 return 0;
4299
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004300 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004301
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004302 /* the URI is in h */
4303 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004304 return 0;
4305
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004306 h += uri_auth->uri_len;
4307 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4308 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004309 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004310 break;
4311 }
4312 h++;
4313 }
4314
4315 if (uri_auth->refresh) {
4316 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4317 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4318 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004319 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004320 break;
4321 }
4322 h++;
4323 }
4324 }
4325
Willy Tarreau55bb8452007-10-17 18:44:57 +02004326 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4327 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4328 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004329 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02004330 break;
4331 }
4332 h++;
4333 }
4334
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004335 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
4336
Willy Tarreaub2513902006-12-17 14:52:38 +01004337 /* we are in front of a interceptable URI. Let's check
4338 * if there's an authentication and if it's valid.
4339 */
4340 user = uri_auth->users;
4341 if (!user) {
4342 /* no user auth required, it's OK */
4343 authenticated = 1;
4344 } else {
4345 authenticated = 0;
4346
4347 /* a user list is defined, we have to check.
4348 * skip 21 chars for "Authorization: Basic ".
4349 */
4350
4351 /* FIXME: this should move to an earlier place */
4352 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004353 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4354 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4355 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004356 if (len > 14 &&
4357 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004358 txn->auth_hdr.str = h;
4359 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004360 break;
4361 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004362 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004363 }
4364
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004365 if (txn->auth_hdr.len < 21 ||
4366 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004367 user = NULL;
4368
4369 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004370 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4371 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004372 user->user_pwd, user->user_len)) {
4373 authenticated = 1;
4374 break;
4375 }
4376 user = user->next;
4377 }
4378 }
4379
4380 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004381 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004382
4383 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004384 msg.str = trash;
4385 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004386 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01004387 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02004388 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02004389 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004390 if (!(t->flags & SN_ERR_MASK))
4391 t->flags |= SN_ERR_PRXCOND;
4392 if (!(t->flags & SN_FINST_MASK))
4393 t->flags |= SN_FINST_R;
4394 return 1;
4395 }
4396
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004397 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01004398 * data.
4399 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01004400 buffer_write_dis(t->req);
Willy Tarreau72b179a2008-08-28 16:01:32 +02004401 buffer_shutw_now(t->req);
4402 buffer_shutr_now(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02004403 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01004404 t->data_source = DATA_SRC_STATS;
4405 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02004406 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau01bf8672008-12-07 18:03:29 +01004407 buffer_install_hijacker(t, t->rep, produce_content);
Willy Tarreaub2513902006-12-17 14:52:38 +01004408 return 1;
4409}
4410
4411
Willy Tarreaubaaee002006-06-26 02:48:02 +02004412/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004413 * Print a debug line with a header
4414 */
4415void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4416{
4417 int len, max;
4418 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004419 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004420 max = end - start;
4421 UBOUND(max, sizeof(trash) - len - 1);
4422 len += strlcpy2(trash + len, start, max + 1);
4423 trash[len++] = '\n';
4424 write(1, trash, len);
4425}
4426
4427
Willy Tarreau8797c062007-05-07 00:55:35 +02004428/************************************************************************/
4429/* The code below is dedicated to ACL parsing and matching */
4430/************************************************************************/
4431
4432
4433
4434
4435/* 1. Check on METHOD
4436 * We use the pre-parsed method if it is known, and store its number as an
4437 * integer. If it is unknown, we use the pointer and the length.
4438 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004439static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004440{
4441 int len, meth;
4442
Willy Tarreauae8b7962007-06-09 23:10:04 +02004443 len = strlen(*text);
4444 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004445
4446 pattern->val.i = meth;
4447 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004448 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004449 if (!pattern->ptr.str)
4450 return 0;
4451 pattern->len = len;
4452 }
4453 return 1;
4454}
4455
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004456static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004457acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4458 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004459{
4460 int meth;
4461 struct http_txn *txn = l7;
4462
Willy Tarreaub6866442008-07-14 23:54:42 +02004463 if (!txn)
4464 return 0;
4465
Willy Tarreauc11416f2007-06-17 16:58:38 +02004466 if (txn->req.msg_state != HTTP_MSG_BODY)
4467 return 0;
4468
Willy Tarreau8797c062007-05-07 00:55:35 +02004469 meth = txn->meth;
4470 test->i = meth;
4471 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004472 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4473 /* ensure the indexes are not affected */
4474 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004475 test->len = txn->req.sl.rq.m_l;
4476 test->ptr = txn->req.sol;
4477 }
4478 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4479 return 1;
4480}
4481
4482static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4483{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004484 int icase;
4485
Willy Tarreau8797c062007-05-07 00:55:35 +02004486 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02004487 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02004488
4489 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02004490 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004491
4492 /* Other method, we must compare the strings */
4493 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02004494 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004495
4496 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4497 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4498 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02004499 return ACL_PAT_FAIL;
4500 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004501}
4502
4503/* 2. Check on Request/Status Version
4504 * We simply compare strings here.
4505 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004506static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004507{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004508 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004509 if (!pattern->ptr.str)
4510 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004511 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004512 return 1;
4513}
4514
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004515static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004516acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4517 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004518{
4519 struct http_txn *txn = l7;
4520 char *ptr;
4521 int len;
4522
Willy Tarreaub6866442008-07-14 23:54:42 +02004523 if (!txn)
4524 return 0;
4525
Willy Tarreauc11416f2007-06-17 16:58:38 +02004526 if (txn->req.msg_state != HTTP_MSG_BODY)
4527 return 0;
4528
Willy Tarreau8797c062007-05-07 00:55:35 +02004529 len = txn->req.sl.rq.v_l;
4530 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4531
4532 while ((len-- > 0) && (*ptr++ != '/'));
4533 if (len <= 0)
4534 return 0;
4535
4536 test->ptr = ptr;
4537 test->len = len;
4538
4539 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4540 return 1;
4541}
4542
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004543static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004544acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4545 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004546{
4547 struct http_txn *txn = l7;
4548 char *ptr;
4549 int len;
4550
Willy Tarreaub6866442008-07-14 23:54:42 +02004551 if (!txn)
4552 return 0;
4553
Willy Tarreauc11416f2007-06-17 16:58:38 +02004554 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4555 return 0;
4556
Willy Tarreau8797c062007-05-07 00:55:35 +02004557 len = txn->rsp.sl.st.v_l;
4558 ptr = txn->rsp.sol;
4559
4560 while ((len-- > 0) && (*ptr++ != '/'));
4561 if (len <= 0)
4562 return 0;
4563
4564 test->ptr = ptr;
4565 test->len = len;
4566
4567 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4568 return 1;
4569}
4570
4571/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004572static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004573acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4574 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004575{
4576 struct http_txn *txn = l7;
4577 char *ptr;
4578 int len;
4579
Willy Tarreaub6866442008-07-14 23:54:42 +02004580 if (!txn)
4581 return 0;
4582
Willy Tarreauc11416f2007-06-17 16:58:38 +02004583 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4584 return 0;
4585
Willy Tarreau8797c062007-05-07 00:55:35 +02004586 len = txn->rsp.sl.st.c_l;
4587 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4588
4589 test->i = __strl2ui(ptr, len);
4590 test->flags = ACL_TEST_F_VOL_1ST;
4591 return 1;
4592}
4593
4594/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004595static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004596acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4597 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004598{
4599 struct http_txn *txn = l7;
4600
Willy Tarreaub6866442008-07-14 23:54:42 +02004601 if (!txn)
4602 return 0;
4603
Willy Tarreauc11416f2007-06-17 16:58:38 +02004604 if (txn->req.msg_state != HTTP_MSG_BODY)
4605 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004606
Willy Tarreauc11416f2007-06-17 16:58:38 +02004607 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4608 /* ensure the indexes are not affected */
4609 return 0;
4610
Willy Tarreau8797c062007-05-07 00:55:35 +02004611 test->len = txn->req.sl.rq.u_l;
4612 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4613
Willy Tarreauf3d25982007-05-08 22:45:09 +02004614 /* we do not need to set READ_ONLY because the data is in a buffer */
4615 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004616 return 1;
4617}
4618
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004619static int
4620acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
4621 struct acl_expr *expr, struct acl_test *test)
4622{
4623 struct http_txn *txn = l7;
4624
Willy Tarreaub6866442008-07-14 23:54:42 +02004625 if (!txn)
4626 return 0;
4627
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004628 if (txn->req.msg_state != HTTP_MSG_BODY)
4629 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004630
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004631 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4632 /* ensure the indexes are not affected */
4633 return 0;
4634
4635 /* Parse HTTP request */
4636 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4637 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
4638 test->i = AF_INET;
4639
4640 /*
4641 * If we are parsing url in frontend space, we prepare backend stage
4642 * to not parse again the same url ! optimization lazyness...
4643 */
4644 if (px->options & PR_O_HTTP_PROXY)
4645 l4->flags |= SN_ADDR_SET;
4646
4647 test->flags = ACL_TEST_F_READ_ONLY;
4648 return 1;
4649}
4650
4651static int
4652acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
4653 struct acl_expr *expr, struct acl_test *test)
4654{
4655 struct http_txn *txn = l7;
4656
Willy Tarreaub6866442008-07-14 23:54:42 +02004657 if (!txn)
4658 return 0;
4659
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004660 if (txn->req.msg_state != HTTP_MSG_BODY)
4661 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004662
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004663 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4664 /* ensure the indexes are not affected */
4665 return 0;
4666
4667 /* Same optimization as url_ip */
4668 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4669 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
4670
4671 if (px->options & PR_O_HTTP_PROXY)
4672 l4->flags |= SN_ADDR_SET;
4673
4674 test->flags = ACL_TEST_F_READ_ONLY;
4675 return 1;
4676}
4677
Willy Tarreauc11416f2007-06-17 16:58:38 +02004678/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
4679 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
4680 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02004681static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004682acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004683 struct acl_expr *expr, struct acl_test *test)
4684{
4685 struct http_txn *txn = l7;
4686 struct hdr_idx *idx = &txn->hdr_idx;
4687 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004688
Willy Tarreaub6866442008-07-14 23:54:42 +02004689 if (!txn)
4690 return 0;
4691
Willy Tarreau33a7e692007-06-10 19:45:56 +02004692 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4693 /* search for header from the beginning */
4694 ctx->idx = 0;
4695
Willy Tarreau33a7e692007-06-10 19:45:56 +02004696 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4697 test->flags |= ACL_TEST_F_FETCH_MORE;
4698 test->flags |= ACL_TEST_F_VOL_HDR;
4699 test->len = ctx->vlen;
4700 test->ptr = (char *)ctx->line + ctx->val;
4701 return 1;
4702 }
4703
4704 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4705 test->flags |= ACL_TEST_F_VOL_HDR;
4706 return 0;
4707}
4708
Willy Tarreau33a7e692007-06-10 19:45:56 +02004709static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004710acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
4711 struct acl_expr *expr, struct acl_test *test)
4712{
4713 struct http_txn *txn = l7;
4714
Willy Tarreaub6866442008-07-14 23:54:42 +02004715 if (!txn)
4716 return 0;
4717
Willy Tarreauc11416f2007-06-17 16:58:38 +02004718 if (txn->req.msg_state != HTTP_MSG_BODY)
4719 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004720
Willy Tarreauc11416f2007-06-17 16:58:38 +02004721 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4722 /* ensure the indexes are not affected */
4723 return 0;
4724
4725 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
4726}
4727
4728static int
4729acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
4730 struct acl_expr *expr, struct acl_test *test)
4731{
4732 struct http_txn *txn = l7;
4733
Willy Tarreaub6866442008-07-14 23:54:42 +02004734 if (!txn)
4735 return 0;
4736
Willy Tarreauc11416f2007-06-17 16:58:38 +02004737 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4738 return 0;
4739
4740 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
4741}
4742
4743/* 6. Check on HTTP header count. The number of occurrences is returned.
4744 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
4745 */
4746static int
4747acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004748 struct acl_expr *expr, struct acl_test *test)
4749{
4750 struct http_txn *txn = l7;
4751 struct hdr_idx *idx = &txn->hdr_idx;
4752 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004753 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02004754
Willy Tarreaub6866442008-07-14 23:54:42 +02004755 if (!txn)
4756 return 0;
4757
Willy Tarreau33a7e692007-06-10 19:45:56 +02004758 ctx.idx = 0;
4759 cnt = 0;
4760 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
4761 cnt++;
4762
4763 test->i = cnt;
4764 test->flags = ACL_TEST_F_VOL_HDR;
4765 return 1;
4766}
4767
Willy Tarreauc11416f2007-06-17 16:58:38 +02004768static int
4769acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4770 struct acl_expr *expr, struct acl_test *test)
4771{
4772 struct http_txn *txn = l7;
4773
Willy Tarreaub6866442008-07-14 23:54:42 +02004774 if (!txn)
4775 return 0;
4776
Willy Tarreauc11416f2007-06-17 16:58:38 +02004777 if (txn->req.msg_state != HTTP_MSG_BODY)
4778 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004779
Willy Tarreauc11416f2007-06-17 16:58:38 +02004780 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4781 /* ensure the indexes are not affected */
4782 return 0;
4783
4784 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
4785}
4786
4787static int
4788acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4789 struct acl_expr *expr, struct acl_test *test)
4790{
4791 struct http_txn *txn = l7;
4792
Willy Tarreaub6866442008-07-14 23:54:42 +02004793 if (!txn)
4794 return 0;
4795
Willy Tarreauc11416f2007-06-17 16:58:38 +02004796 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4797 return 0;
4798
4799 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
4800}
4801
Willy Tarreau33a7e692007-06-10 19:45:56 +02004802/* 7. Check on HTTP header's integer value. The integer value is returned.
4803 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02004804 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02004805 */
4806static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004807acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004808 struct acl_expr *expr, struct acl_test *test)
4809{
4810 struct http_txn *txn = l7;
4811 struct hdr_idx *idx = &txn->hdr_idx;
4812 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004813
Willy Tarreaub6866442008-07-14 23:54:42 +02004814 if (!txn)
4815 return 0;
4816
Willy Tarreau33a7e692007-06-10 19:45:56 +02004817 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4818 /* search for header from the beginning */
4819 ctx->idx = 0;
4820
Willy Tarreau33a7e692007-06-10 19:45:56 +02004821 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4822 test->flags |= ACL_TEST_F_FETCH_MORE;
4823 test->flags |= ACL_TEST_F_VOL_HDR;
4824 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
4825 return 1;
4826 }
4827
4828 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4829 test->flags |= ACL_TEST_F_VOL_HDR;
4830 return 0;
4831}
4832
Willy Tarreauc11416f2007-06-17 16:58:38 +02004833static int
4834acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4835 struct acl_expr *expr, struct acl_test *test)
4836{
4837 struct http_txn *txn = l7;
4838
Willy Tarreaub6866442008-07-14 23:54:42 +02004839 if (!txn)
4840 return 0;
4841
Willy Tarreauc11416f2007-06-17 16:58:38 +02004842 if (txn->req.msg_state != HTTP_MSG_BODY)
4843 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004844
Willy Tarreauc11416f2007-06-17 16:58:38 +02004845 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4846 /* ensure the indexes are not affected */
4847 return 0;
4848
4849 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
4850}
4851
4852static int
4853acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4854 struct acl_expr *expr, struct acl_test *test)
4855{
4856 struct http_txn *txn = l7;
4857
Willy Tarreaub6866442008-07-14 23:54:42 +02004858 if (!txn)
4859 return 0;
4860
Willy Tarreauc11416f2007-06-17 16:58:38 +02004861 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4862 return 0;
4863
4864 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
4865}
4866
Willy Tarreau737b0c12007-06-10 21:28:46 +02004867/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
4868 * the first '/' after the possible hostname, and ends before the possible '?'.
4869 */
4870static int
4871acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
4872 struct acl_expr *expr, struct acl_test *test)
4873{
4874 struct http_txn *txn = l7;
4875 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004876
Willy Tarreaub6866442008-07-14 23:54:42 +02004877 if (!txn)
4878 return 0;
4879
Willy Tarreauc11416f2007-06-17 16:58:38 +02004880 if (txn->req.msg_state != HTTP_MSG_BODY)
4881 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004882
Willy Tarreauc11416f2007-06-17 16:58:38 +02004883 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4884 /* ensure the indexes are not affected */
4885 return 0;
4886
Willy Tarreau21d2af32008-02-14 20:25:24 +01004887 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4888 ptr = http_get_path(txn);
4889 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02004890 return 0;
4891
4892 /* OK, we got the '/' ! */
4893 test->ptr = ptr;
4894
4895 while (ptr < end && *ptr != '?')
4896 ptr++;
4897
4898 test->len = ptr - test->ptr;
4899
4900 /* we do not need to set READ_ONLY because the data is in a buffer */
4901 test->flags = ACL_TEST_F_VOL_1ST;
4902 return 1;
4903}
4904
4905
Willy Tarreau8797c062007-05-07 00:55:35 +02004906
4907/************************************************************************/
4908/* All supported keywords must be declared here. */
4909/************************************************************************/
4910
4911/* Note: must not be declared <const> as its list will be overwritten */
4912static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004913 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
4914 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4915 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
4916 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02004917
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004918 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4919 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4920 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4921 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4922 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4923 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
4924 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4925 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
4926 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02004927
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004928 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
4929 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4930 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4931 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4932 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4933 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4934 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4935 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
4936 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
4937 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02004938
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004939 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
4940 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
4941 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
4942 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
4943 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
4944 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
4945 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
4946 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
4947 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02004948
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004949 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4950 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4951 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4952 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4953 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4954 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4955 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02004956
Willy Tarreauf3d25982007-05-08 22:45:09 +02004957 { NULL, NULL, NULL, NULL },
4958
4959#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02004960 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
4961 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
4962 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
4963 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
4964 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
4965 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
4966 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
4967
Willy Tarreau8797c062007-05-07 00:55:35 +02004968 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
4969 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
4970 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
4971 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
4972 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
4973 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
4974 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
4975 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
4976
4977 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
4978 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
4979 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
4980 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
4981 { NULL, NULL, NULL, NULL },
4982#endif
4983}};
4984
4985
4986__attribute__((constructor))
4987static void __http_protocol_init(void)
4988{
4989 acl_register_keywords(&acl_kws);
4990}
4991
4992
Willy Tarreau58f10d72006-12-04 02:26:12 +01004993/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004994 * Local variables:
4995 * c-indent-level: 8
4996 * c-basic-offset: 8
4997 * End:
4998 */