blob: 6fd84f421648a28fa8a32aaf88f70f7faf119ee1 [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{
468 __label__ return_hdr, next_hdr;
469 const char *eol, *sov;
470 int cur_idx;
471
472 if (ctx->idx) {
473 /* We have previously returned a value, let's search
474 * another one on the same line.
475 */
476 cur_idx = ctx->idx;
477 sol = ctx->line;
478 sov = sol + ctx->val + ctx->vlen;
479 eol = sol + idx->v[cur_idx].len;
480
481 if (sov >= eol)
482 /* no more values in this header */
483 goto next_hdr;
484
485 /* values remaining for this header, skip the comma */
486 sov++;
487 while (sov < eol && http_is_lws[(unsigned char)*sov])
488 sov++;
489
490 goto return_hdr;
491 }
492
493 /* first request for this header */
494 sol += hdr_idx_first_pos(idx);
495 cur_idx = hdr_idx_first_idx(idx);
496
497 while (cur_idx) {
498 eol = sol + idx->v[cur_idx].len;
499
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200500 if (len == 0) {
501 /* No argument was passed, we want any header.
502 * To achieve this, we simply build a fake request. */
503 while (sol + len < eol && sol[len] != ':')
504 len++;
505 name = sol;
506 }
507
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 if ((len < eol - sol) &&
509 (sol[len] == ':') &&
510 (strncasecmp(sol, name, len) == 0)) {
511
512 sov = sol + len + 1;
513 while (sov < eol && http_is_lws[(unsigned char)*sov])
514 sov++;
515 return_hdr:
516 ctx->line = sol;
517 ctx->idx = cur_idx;
518 ctx->val = sov - sol;
519
520 eol = find_hdr_value_end(sov, eol);
521 ctx->vlen = eol - sov;
522 return 1;
523 }
524 next_hdr:
525 sol = eol + idx->v[cur_idx].cr + 1;
526 cur_idx = idx->v[cur_idx].next;
527 }
528 return 0;
529}
530
531int http_find_header(const char *name,
532 const char *sol, struct hdr_idx *idx,
533 struct hdr_ctx *ctx)
534{
535 return http_find_header2(name, strlen(name), sol, idx, ctx);
536}
537
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100538/* This function handles a server error at the stream interface level. The
539 * stream interface is assumed to be already in a closed state. An optional
540 * message is copied into the input buffer, and an HTTP status code stored.
541 * The error flags are set to the values in arguments. Any pending request
542 * is flushed.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100544static void http_server_error(struct session *t, struct stream_interface *si,
545 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546{
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100547 buffer_flush(si->ob);
548 buffer_flush(si->ib);
549 buffer_write_ena(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100550 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100551 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100552 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200553 }
554 if (!(t->flags & SN_ERR_MASK))
555 t->flags |= err;
556 if (!(t->flags & SN_FINST_MASK))
557 t->flags |= finst;
558}
559
Willy Tarreau80587432006-12-24 17:47:20 +0100560/* This function returns the appropriate error location for the given session
561 * and message.
562 */
563
564struct chunk *error_message(struct session *s, int msgnum)
565{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200566 if (s->be->errmsg[msgnum].str)
567 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100568 else if (s->fe->errmsg[msgnum].str)
569 return &s->fe->errmsg[msgnum];
570 else
571 return &http_err_chunks[msgnum];
572}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573
Willy Tarreau53b6c742006-12-17 13:37:46 +0100574/*
575 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
576 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
577 */
578static http_meth_t find_http_meth(const char *str, const int len)
579{
580 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100581 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100582
583 m = ((unsigned)*str - 'A');
584
585 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100586 for (h = http_methods[m]; h->len > 0; h++) {
587 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100588 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100589 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100590 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100591 };
592 return HTTP_METH_OTHER;
593 }
594 return HTTP_METH_NONE;
595
596}
597
Willy Tarreau21d2af32008-02-14 20:25:24 +0100598/* Parse the URI from the given transaction (which is assumed to be in request
599 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
600 * It is returned otherwise.
601 */
602static char *
603http_get_path(struct http_txn *txn)
604{
605 char *ptr, *end;
606
607 ptr = txn->req.sol + txn->req.sl.rq.u;
608 end = ptr + txn->req.sl.rq.u_l;
609
610 if (ptr >= end)
611 return NULL;
612
613 /* RFC2616, par. 5.1.2 :
614 * Request-URI = "*" | absuri | abspath | authority
615 */
616
617 if (*ptr == '*')
618 return NULL;
619
620 if (isalpha((unsigned char)*ptr)) {
621 /* this is a scheme as described by RFC3986, par. 3.1 */
622 ptr++;
623 while (ptr < end &&
624 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
625 ptr++;
626 /* skip '://' */
627 if (ptr == end || *ptr++ != ':')
628 return NULL;
629 if (ptr == end || *ptr++ != '/')
630 return NULL;
631 if (ptr == end || *ptr++ != '/')
632 return NULL;
633 }
634 /* skip [user[:passwd]@]host[:[port]] */
635
636 while (ptr < end && *ptr != '/')
637 ptr++;
638
639 if (ptr == end)
640 return NULL;
641
642 /* OK, we got the '/' ! */
643 return ptr;
644}
645
Willy Tarreauefb453c2008-10-26 20:49:47 +0100646/* Returns a 302 for a redirectable request. This may only be called just after
647 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
648 * left unchanged and will follow normal proxy processing.
649 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100650void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100651{
652 struct http_txn *txn;
653 struct chunk rdr;
654 char *path;
655 int len;
656
657 /* 1: create the response header */
658 rdr.len = strlen(HTTP_302);
659 rdr.str = trash;
660 memcpy(rdr.str, HTTP_302, rdr.len);
661
662 /* 2: add the server's prefix */
663 if (rdr.len + s->srv->rdr_len > sizeof(trash))
664 return;
665
666 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
667 rdr.len += s->srv->rdr_len;
668
669 /* 3: add the request URI */
670 txn = &s->txn;
671 path = http_get_path(txn);
672 if (!path)
673 return;
674
675 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
676 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
677 return;
678
679 memcpy(rdr.str + rdr.len, path, len);
680 rdr.len += len;
681 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
682 rdr.len += 4;
683
684 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100685 si->shutr(si);
686 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100687 si->err_type = SI_ET_NONE;
688 si->err_loc = NULL;
689 si->state = SI_ST_CLO;
690
691 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100692 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100693
694 /* FIXME: we should increase a counter of redirects per server and per backend. */
695 if (s->srv)
696 s->srv->cum_sess++;
697}
698
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100699/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100700 * that the server side is closed. Note that err_type is actually a
701 * bitmask, where almost only aborts may be cumulated with other
702 * values. We consider that aborted operations are more important
703 * than timeouts or errors due to the fact that nobody else in the
704 * logs might explain incomplete retries. All others should avoid
705 * being cumulated. It should normally not be possible to have multiple
706 * aborts at once, but just in case, the first one in sequence is reported.
707 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100708void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100709{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100710 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100711
712 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100713 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
714 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100715 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100716 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
717 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100718 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100719 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
720 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100721 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100722 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
723 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100724 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100725 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
726 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100727 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100728 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
729 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100730 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100731 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
732 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100733}
734
Willy Tarreau42250582007-04-01 01:30:43 +0200735extern const char sess_term_cond[8];
736extern const char sess_fin_state[8];
737extern const char *monthname[12];
738const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
739const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
740 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
741 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200742struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200743struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100744
Willy Tarreau42250582007-04-01 01:30:43 +0200745/*
746 * send a log for the session when we have enough info about it.
747 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100748 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100749void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200750{
751 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
752 struct proxy *fe = s->fe;
753 struct proxy *be = s->be;
754 struct proxy *prx_log;
755 struct http_txn *txn = &s->txn;
756 int tolog;
757 char *uri, *h;
758 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200759 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200760 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200761 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200762 int hdr;
763
764 if (fe->logfac1 < 0 && fe->logfac2 < 0)
765 return;
766 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100767
Willy Tarreau42250582007-04-01 01:30:43 +0200768 if (s->cli_addr.ss_family == AF_INET)
769 inet_ntop(AF_INET,
770 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
771 pn, sizeof(pn));
772 else
773 inet_ntop(AF_INET6,
774 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
775 pn, sizeof(pn));
776
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200777 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200778
779 /* FIXME: let's limit ourselves to frontend logging for now. */
780 tolog = fe->to_log;
781
782 h = tmpline;
783 if (fe->to_log & LW_REQHDR &&
784 txn->req.cap &&
785 (h < tmpline + sizeof(tmpline) - 10)) {
786 *(h++) = ' ';
787 *(h++) = '{';
788 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
789 if (hdr)
790 *(h++) = '|';
791 if (txn->req.cap[hdr] != NULL)
792 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
793 '#', hdr_encode_map, txn->req.cap[hdr]);
794 }
795 *(h++) = '}';
796 }
797
798 if (fe->to_log & LW_RSPHDR &&
799 txn->rsp.cap &&
800 (h < tmpline + sizeof(tmpline) - 7)) {
801 *(h++) = ' ';
802 *(h++) = '{';
803 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
804 if (hdr)
805 *(h++) = '|';
806 if (txn->rsp.cap[hdr] != NULL)
807 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
808 '#', hdr_encode_map, txn->rsp.cap[hdr]);
809 }
810 *(h++) = '}';
811 }
812
813 if (h < tmpline + sizeof(tmpline) - 4) {
814 *(h++) = ' ';
815 *(h++) = '"';
816 uri = txn->uri ? txn->uri : "<BADREQ>";
817 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
818 '#', url_encode_map, uri);
819 *(h++) = '"';
820 }
821 *h = '\0';
822
823 svid = (tolog & LW_SVID) ?
824 (s->data_source != DATA_SRC_STATS) ?
825 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
826
Willy Tarreau70089872008-06-13 21:12:51 +0200827 t_request = -1;
828 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
829 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
830
Willy Tarreau42250582007-04-01 01:30:43 +0200831 send_log(prx_log, LOG_INFO,
832 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
833 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100834 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200835 pn,
836 (s->cli_addr.ss_family == AF_INET) ?
837 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
838 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200839 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200840 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200841 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200842 t_request,
843 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200844 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
845 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
846 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
847 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100848 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200849 txn->cli_cookie ? txn->cli_cookie : "-",
850 txn->srv_cookie ? txn->srv_cookie : "-",
851 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
852 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
853 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
854 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
855 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100856 (s->flags & SN_REDISP)?"+":"",
857 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200858 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
859
860 s->logs.logwait = 0;
861}
862
Willy Tarreau117f59e2007-03-04 18:17:17 +0100863
864/*
865 * Capture headers from message starting at <som> according to header list
866 * <cap_hdr>, and fill the <idx> structure appropriately.
867 */
868void capture_headers(char *som, struct hdr_idx *idx,
869 char **cap, struct cap_hdr *cap_hdr)
870{
871 char *eol, *sol, *col, *sov;
872 int cur_idx;
873 struct cap_hdr *h;
874 int len;
875
876 sol = som + hdr_idx_first_pos(idx);
877 cur_idx = hdr_idx_first_idx(idx);
878
879 while (cur_idx) {
880 eol = sol + idx->v[cur_idx].len;
881
882 col = sol;
883 while (col < eol && *col != ':')
884 col++;
885
886 sov = col + 1;
887 while (sov < eol && http_is_lws[(unsigned char)*sov])
888 sov++;
889
890 for (h = cap_hdr; h; h = h->next) {
891 if ((h->namelen == col - sol) &&
892 (strncasecmp(sol, h->name, h->namelen) == 0)) {
893 if (cap[h->index] == NULL)
894 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200895 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100896
897 if (cap[h->index] == NULL) {
898 Alert("HTTP capture : out of memory.\n");
899 continue;
900 }
901
902 len = eol - sov;
903 if (len > h->len)
904 len = h->len;
905
906 memcpy(cap[h->index], sov, len);
907 cap[h->index][len]=0;
908 }
909 }
910 sol = eol + idx->v[cur_idx].cr + 1;
911 cur_idx = idx->v[cur_idx].next;
912 }
913}
914
915
Willy Tarreau42250582007-04-01 01:30:43 +0200916/* either we find an LF at <ptr> or we jump to <bad>.
917 */
918#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
919
920/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
921 * otherwise to <http_msg_ood> with <state> set to <st>.
922 */
923#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
924 ptr++; \
925 if (likely(ptr < end)) \
926 goto good; \
927 else { \
928 state = (st); \
929 goto http_msg_ood; \
930 } \
931 } while (0)
932
933
Willy Tarreaubaaee002006-06-26 02:48:02 +0200934/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100935 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100936 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
937 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
938 * will give undefined results.
939 * Note that it is upon the caller's responsibility to ensure that ptr < end,
940 * and that msg->sol points to the beginning of the response.
941 * If a complete line is found (which implies that at least one CR or LF is
942 * found before <end>, the updated <ptr> is returned, otherwise NULL is
943 * returned indicating an incomplete line (which does not mean that parts have
944 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
945 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
946 * upon next call.
947 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200948 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100949 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
950 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200951 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100952 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100953const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
954 unsigned int state, const char *ptr, const char *end,
955 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100956{
957 __label__
958 http_msg_rpver,
959 http_msg_rpver_sp,
960 http_msg_rpcode,
961 http_msg_rpcode_sp,
962 http_msg_rpreason,
963 http_msg_rpline_eol,
964 http_msg_ood, /* out of data */
965 http_msg_invalid;
966
967 switch (state) {
968 http_msg_rpver:
969 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100970 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100971 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
972
973 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100974 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100975 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
976 }
977 goto http_msg_invalid;
978
979 http_msg_rpver_sp:
980 case HTTP_MSG_RPVER_SP:
981 if (likely(!HTTP_IS_LWS(*ptr))) {
982 msg->sl.st.c = ptr - msg_buf;
983 goto http_msg_rpcode;
984 }
985 if (likely(HTTP_IS_SPHT(*ptr)))
986 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
987 /* so it's a CR/LF, this is invalid */
988 goto http_msg_invalid;
989
990 http_msg_rpcode:
991 case HTTP_MSG_RPCODE:
992 if (likely(!HTTP_IS_LWS(*ptr)))
993 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
994
995 if (likely(HTTP_IS_SPHT(*ptr))) {
996 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
997 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
998 }
999
1000 /* so it's a CR/LF, so there is no reason phrase */
1001 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1002 http_msg_rsp_reason:
1003 /* FIXME: should we support HTTP responses without any reason phrase ? */
1004 msg->sl.st.r = ptr - msg_buf;
1005 msg->sl.st.r_l = 0;
1006 goto http_msg_rpline_eol;
1007
1008 http_msg_rpcode_sp:
1009 case HTTP_MSG_RPCODE_SP:
1010 if (likely(!HTTP_IS_LWS(*ptr))) {
1011 msg->sl.st.r = ptr - msg_buf;
1012 goto http_msg_rpreason;
1013 }
1014 if (likely(HTTP_IS_SPHT(*ptr)))
1015 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1016 /* so it's a CR/LF, so there is no reason phrase */
1017 goto http_msg_rsp_reason;
1018
1019 http_msg_rpreason:
1020 case HTTP_MSG_RPREASON:
1021 if (likely(!HTTP_IS_CRLF(*ptr)))
1022 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1023 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1024 http_msg_rpline_eol:
1025 /* We have seen the end of line. Note that we do not
1026 * necessarily have the \n yet, but at least we know that we
1027 * have EITHER \r OR \n, otherwise the response would not be
1028 * complete. We can then record the response length and return
1029 * to the caller which will be able to register it.
1030 */
1031 msg->sl.st.l = ptr - msg->sol;
1032 return ptr;
1033
1034#ifdef DEBUG_FULL
1035 default:
1036 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1037 exit(1);
1038#endif
1039 }
1040
1041 http_msg_ood:
1042 /* out of data */
1043 if (ret_state)
1044 *ret_state = state;
1045 if (ret_ptr)
1046 *ret_ptr = (char *)ptr;
1047 return NULL;
1048
1049 http_msg_invalid:
1050 /* invalid message */
1051 if (ret_state)
1052 *ret_state = HTTP_MSG_ERROR;
1053 return NULL;
1054}
1055
1056
1057/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001058 * This function parses a request line between <ptr> and <end>, starting with
1059 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1060 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1061 * will give undefined results.
1062 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1063 * and that msg->sol points to the beginning of the request.
1064 * If a complete line is found (which implies that at least one CR or LF is
1065 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1066 * returned indicating an incomplete line (which does not mean that parts have
1067 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1068 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1069 * upon next call.
1070 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001071 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1073 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001074 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001075 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001076const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1077 unsigned int state, const char *ptr, const char *end,
1078 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001079{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001080 __label__
1081 http_msg_rqmeth,
1082 http_msg_rqmeth_sp,
1083 http_msg_rquri,
1084 http_msg_rquri_sp,
1085 http_msg_rqver,
1086 http_msg_rqline_eol,
1087 http_msg_ood, /* out of data */
1088 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001089
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001090 switch (state) {
1091 http_msg_rqmeth:
1092 case HTTP_MSG_RQMETH:
1093 if (likely(HTTP_IS_TOKEN(*ptr)))
1094 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001095
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001096 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001097 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1099 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001100
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001101 if (likely(HTTP_IS_CRLF(*ptr))) {
1102 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001103 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001104 http_msg_req09_uri:
1105 msg->sl.rq.u = ptr - msg_buf;
1106 http_msg_req09_uri_e:
1107 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1108 http_msg_req09_ver:
1109 msg->sl.rq.v = ptr - msg_buf;
1110 msg->sl.rq.v_l = 0;
1111 goto http_msg_rqline_eol;
1112 }
1113 goto http_msg_invalid;
1114
1115 http_msg_rqmeth_sp:
1116 case HTTP_MSG_RQMETH_SP:
1117 if (likely(!HTTP_IS_LWS(*ptr))) {
1118 msg->sl.rq.u = ptr - msg_buf;
1119 goto http_msg_rquri;
1120 }
1121 if (likely(HTTP_IS_SPHT(*ptr)))
1122 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1123 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1124 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001125
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001126 http_msg_rquri:
1127 case HTTP_MSG_RQURI:
1128 if (likely(!HTTP_IS_LWS(*ptr)))
1129 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001130
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001131 if (likely(HTTP_IS_SPHT(*ptr))) {
1132 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1133 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1134 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001135
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001136 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1137 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001138
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001139 http_msg_rquri_sp:
1140 case HTTP_MSG_RQURI_SP:
1141 if (likely(!HTTP_IS_LWS(*ptr))) {
1142 msg->sl.rq.v = ptr - msg_buf;
1143 goto http_msg_rqver;
1144 }
1145 if (likely(HTTP_IS_SPHT(*ptr)))
1146 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1147 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1148 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001149
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001150 http_msg_rqver:
1151 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001152 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001153 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001154
1155 if (likely(HTTP_IS_CRLF(*ptr))) {
1156 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1157 http_msg_rqline_eol:
1158 /* We have seen the end of line. Note that we do not
1159 * necessarily have the \n yet, but at least we know that we
1160 * have EITHER \r OR \n, otherwise the request would not be
1161 * complete. We can then record the request length and return
1162 * to the caller which will be able to register it.
1163 */
1164 msg->sl.rq.l = ptr - msg->sol;
1165 return ptr;
1166 }
1167
1168 /* neither an HTTP_VER token nor a CRLF */
1169 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001170
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001171#ifdef DEBUG_FULL
1172 default:
1173 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1174 exit(1);
1175#endif
1176 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001177
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001178 http_msg_ood:
1179 /* out of data */
1180 if (ret_state)
1181 *ret_state = state;
1182 if (ret_ptr)
1183 *ret_ptr = (char *)ptr;
1184 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001185
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001186 http_msg_invalid:
1187 /* invalid message */
1188 if (ret_state)
1189 *ret_state = HTTP_MSG_ERROR;
1190 return NULL;
1191}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001192
1193
Willy Tarreau8973c702007-01-21 23:58:29 +01001194/*
1195 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001196 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001197 * when data are missing and recalled at the exact same location with no
1198 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001199 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1200 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001201 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001202void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1203{
1204 __label__
1205 http_msg_rqbefore,
1206 http_msg_rqbefore_cr,
1207 http_msg_rqmeth,
1208 http_msg_rqline_end,
1209 http_msg_hdr_first,
1210 http_msg_hdr_name,
1211 http_msg_hdr_l1_sp,
1212 http_msg_hdr_l1_lf,
1213 http_msg_hdr_l1_lws,
1214 http_msg_hdr_val,
1215 http_msg_hdr_l2_lf,
1216 http_msg_hdr_l2_lws,
1217 http_msg_complete_header,
1218 http_msg_last_lf,
1219 http_msg_ood, /* out of data */
1220 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001221
Willy Tarreaue69eada2008-01-27 00:34:10 +01001222 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001223 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001224
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001225 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 ptr = buf->lr;
1227 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001228
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001229 if (unlikely(ptr >= end))
1230 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001231
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001232 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001233 /*
1234 * First, states that are specific to the response only.
1235 * We check them first so that request and headers are
1236 * closer to each other (accessed more often).
1237 */
1238 http_msg_rpbefore:
1239 case HTTP_MSG_RPBEFORE:
1240 if (likely(HTTP_IS_TOKEN(*ptr))) {
1241 if (likely(ptr == buf->data)) {
1242 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001243 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001244 } else {
1245#if PARSE_PRESERVE_EMPTY_LINES
1246 /* only skip empty leading lines, don't remove them */
1247 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001248 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001249#else
1250 /* Remove empty leading lines, as recommended by
1251 * RFC2616. This takes a lot of time because we
1252 * must move all the buffer backwards, but this
1253 * is rarely needed. The method above will be
1254 * cleaner when we'll be able to start sending
1255 * the request from any place in the buffer.
1256 */
1257 buf->lr = ptr;
1258 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001259 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001260 msg->sol = buf->data;
1261 ptr = buf->data;
1262 end = buf->r;
1263#endif
1264 }
1265 hdr_idx_init(idx);
1266 state = HTTP_MSG_RPVER;
1267 goto http_msg_rpver;
1268 }
1269
1270 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1271 goto http_msg_invalid;
1272
1273 if (unlikely(*ptr == '\n'))
1274 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1275 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1276 /* stop here */
1277
1278 http_msg_rpbefore_cr:
1279 case HTTP_MSG_RPBEFORE_CR:
1280 EXPECT_LF_HERE(ptr, http_msg_invalid);
1281 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1282 /* stop here */
1283
1284 http_msg_rpver:
1285 case HTTP_MSG_RPVER:
1286 case HTTP_MSG_RPVER_SP:
1287 case HTTP_MSG_RPCODE:
1288 case HTTP_MSG_RPCODE_SP:
1289 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001290 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001291 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001292 if (unlikely(!ptr))
1293 return;
1294
1295 /* we have a full response and we know that we have either a CR
1296 * or an LF at <ptr>.
1297 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001298 //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 +01001299 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1300
1301 msg->sol = ptr;
1302 if (likely(*ptr == '\r'))
1303 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1304 goto http_msg_rpline_end;
1305
1306 http_msg_rpline_end:
1307 case HTTP_MSG_RPLINE_END:
1308 /* msg->sol must point to the first of CR or LF. */
1309 EXPECT_LF_HERE(ptr, http_msg_invalid);
1310 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1311 /* stop here */
1312
1313 /*
1314 * Second, states that are specific to the request only
1315 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 http_msg_rqbefore:
1317 case HTTP_MSG_RQBEFORE:
1318 if (likely(HTTP_IS_TOKEN(*ptr))) {
1319 if (likely(ptr == buf->data)) {
1320 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001321 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001322 } else {
1323#if PARSE_PRESERVE_EMPTY_LINES
1324 /* only skip empty leading lines, don't remove them */
1325 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001326 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001327#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 /* Remove empty leading lines, as recommended by
1329 * RFC2616. This takes a lot of time because we
1330 * must move all the buffer backwards, but this
1331 * is rarely needed. The method above will be
1332 * cleaner when we'll be able to start sending
1333 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001334 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001335 buf->lr = ptr;
1336 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001337 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001338 msg->sol = buf->data;
1339 ptr = buf->data;
1340 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001341#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001343 /* we will need this when keep-alive will be supported
1344 hdr_idx_init(idx);
1345 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001346 state = HTTP_MSG_RQMETH;
1347 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001348 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001349
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1351 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001352
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001353 if (unlikely(*ptr == '\n'))
1354 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1355 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001356 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001357
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001358 http_msg_rqbefore_cr:
1359 case HTTP_MSG_RQBEFORE_CR:
1360 EXPECT_LF_HERE(ptr, http_msg_invalid);
1361 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001363
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 http_msg_rqmeth:
1365 case HTTP_MSG_RQMETH:
1366 case HTTP_MSG_RQMETH_SP:
1367 case HTTP_MSG_RQURI:
1368 case HTTP_MSG_RQURI_SP:
1369 case HTTP_MSG_RQVER:
1370 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001371 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 if (unlikely(!ptr))
1373 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001374
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001375 /* we have a full request and we know that we have either a CR
1376 * or an LF at <ptr>.
1377 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001378 //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 +01001379 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 msg->sol = ptr;
1382 if (likely(*ptr == '\r'))
1383 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001385
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 http_msg_rqline_end:
1387 case HTTP_MSG_RQLINE_END:
1388 /* check for HTTP/0.9 request : no version information available.
1389 * msg->sol must point to the first of CR or LF.
1390 */
1391 if (unlikely(msg->sl.rq.v_l == 0))
1392 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001393
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 EXPECT_LF_HERE(ptr, http_msg_invalid);
1395 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001396 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001397
Willy Tarreau8973c702007-01-21 23:58:29 +01001398 /*
1399 * Common states below
1400 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001401 http_msg_hdr_first:
1402 case HTTP_MSG_HDR_FIRST:
1403 msg->sol = ptr;
1404 if (likely(!HTTP_IS_CRLF(*ptr))) {
1405 goto http_msg_hdr_name;
1406 }
1407
1408 if (likely(*ptr == '\r'))
1409 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1410 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 http_msg_hdr_name:
1413 case HTTP_MSG_HDR_NAME:
1414 /* assumes msg->sol points to the first char */
1415 if (likely(HTTP_IS_TOKEN(*ptr)))
1416 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001417
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 if (likely(*ptr == ':')) {
1419 msg->col = ptr - buf->data;
1420 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1421 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 http_msg_hdr_l1_sp:
1426 case HTTP_MSG_HDR_L1_SP:
1427 /* assumes msg->sol points to the first char and msg->col to the colon */
1428 if (likely(HTTP_IS_SPHT(*ptr)))
1429 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001430
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 /* header value can be basically anything except CR/LF */
1432 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 if (likely(!HTTP_IS_CRLF(*ptr))) {
1435 goto http_msg_hdr_val;
1436 }
1437
1438 if (likely(*ptr == '\r'))
1439 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1440 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001441
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 http_msg_hdr_l1_lf:
1443 case HTTP_MSG_HDR_L1_LF:
1444 EXPECT_LF_HERE(ptr, http_msg_invalid);
1445 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 http_msg_hdr_l1_lws:
1448 case HTTP_MSG_HDR_L1_LWS:
1449 if (likely(HTTP_IS_SPHT(*ptr))) {
1450 /* replace HT,CR,LF with spaces */
1451 for (; buf->data+msg->sov < ptr; msg->sov++)
1452 buf->data[msg->sov] = ' ';
1453 goto http_msg_hdr_l1_sp;
1454 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001455 /* we had a header consisting only in spaces ! */
1456 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 goto http_msg_complete_header;
1458
1459 http_msg_hdr_val:
1460 case HTTP_MSG_HDR_VAL:
1461 /* assumes msg->sol points to the first char, msg->col to the
1462 * colon, and msg->sov points to the first character of the
1463 * value.
1464 */
1465 if (likely(!HTTP_IS_CRLF(*ptr)))
1466 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001467
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 msg->eol = ptr;
1469 /* Note: we could also copy eol into ->eoh so that we have the
1470 * real header end in case it ends with lots of LWS, but is this
1471 * really needed ?
1472 */
1473 if (likely(*ptr == '\r'))
1474 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1475 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001476
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 http_msg_hdr_l2_lf:
1478 case HTTP_MSG_HDR_L2_LF:
1479 EXPECT_LF_HERE(ptr, http_msg_invalid);
1480 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001481
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482 http_msg_hdr_l2_lws:
1483 case HTTP_MSG_HDR_L2_LWS:
1484 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1485 /* LWS: replace HT,CR,LF with spaces */
1486 for (; msg->eol < ptr; msg->eol++)
1487 *msg->eol = ' ';
1488 goto http_msg_hdr_val;
1489 }
1490 http_msg_complete_header:
1491 /*
1492 * It was a new header, so the last one is finished.
1493 * Assumes msg->sol points to the first char, msg->col to the
1494 * colon, msg->sov points to the first character of the value
1495 * and msg->eol to the first CR or LF so we know how the line
1496 * ends. We insert last header into the index.
1497 */
1498 /*
1499 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1500 write(2, msg->sol, msg->eol-msg->sol);
1501 fprintf(stderr,"\n");
1502 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001503
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1505 idx, idx->tail) < 0))
1506 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001507
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 msg->sol = ptr;
1509 if (likely(!HTTP_IS_CRLF(*ptr))) {
1510 goto http_msg_hdr_name;
1511 }
1512
1513 if (likely(*ptr == '\r'))
1514 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1515 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 http_msg_last_lf:
1518 case HTTP_MSG_LAST_LF:
1519 /* Assumes msg->sol points to the first of either CR or LF */
1520 EXPECT_LF_HERE(ptr, http_msg_invalid);
1521 ptr++;
1522 buf->lr = ptr;
1523 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001524 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 return;
1526#ifdef DEBUG_FULL
1527 default:
1528 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1529 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001530#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 }
1532 http_msg_ood:
1533 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001534 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 buf->lr = ptr;
1536 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001537
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 http_msg_invalid:
1539 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001540 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 return;
1542}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001543
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001544/* This function performs all the processing enabled for the current request.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001545 * It returns 1 if the processing can continue on next analysers, or zero if it
1546 * needs more data, encounters an error, or wants to immediately abort the
1547 * request. It relies on buffers flags, and updates s->req->analysers. Its
1548 * behaviour is rather simple:
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001549 * - all enabled analysers are called in turn from the lower to the higher
1550 * bit.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001551 * - the analyser must check for errors and timeouts, and react as expected.
1552 * It does not have to close anything upon error, the caller will.
1553 * - if the analyser does not have enough data, it must return 0without calling
1554 * other ones. It should also probably do a buffer_write_dis() to ensure
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001555 * that unprocessed data will not be forwarded. But that probably depends on
Willy Tarreauedcf6682008-11-30 23:15:34 +01001556 * the protocol.
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001557 * - if an analyser has enough data, it just has to pass on to the next
Willy Tarreauedcf6682008-11-30 23:15:34 +01001558 * analyser without using buffer_write_dis() (enabled by default).
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001559 * - if an analyser thinks it has no added value anymore staying here, it must
1560 * reset its bit from the analysers flags in order not to be called anymore.
1561 *
1562 * In the future, analysers should be able to indicate that they want to be
1563 * called after XXX bytes have been received (or transfered), and the min of
1564 * all's wishes will be used to ring back (unless a special condition occurs).
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001566int http_process_request(struct session *s, struct buffer *req)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567{
Willy Tarreau59234e92008-11-30 23:51:27 +01001568 /*
1569 * We will parse the partial (or complete) lines.
1570 * We will check the request syntax, and also join multi-line
1571 * headers. An index of all the lines will be elaborated while
1572 * parsing.
1573 *
1574 * For the parsing, we use a 28 states FSM.
1575 *
1576 * Here is the information we currently have :
1577 * req->data + req->som = beginning of request
1578 * req->data + req->eoh = end of processed headers / start of current one
1579 * req->data + req->eol = end of current header or line (LF or CRLF)
1580 * req->lr = first non-visited byte
1581 * req->r = end of data
1582 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001583
Willy Tarreau59234e92008-11-30 23:51:27 +01001584 int cur_idx;
1585 struct http_txn *txn = &s->txn;
1586 struct http_msg *msg = &txn->req;
1587 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001588
Willy Tarreau7b92db42009-02-24 10:48:35 +01001589 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1590 now_ms, __FUNCTION__,
1591 s,
1592 req,
1593 req->rex, req->wex,
1594 req->flags,
1595 req->l,
1596 req->analysers);
1597
Willy Tarreau59234e92008-11-30 23:51:27 +01001598 if (likely(req->lr < req->r))
1599 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001600
Willy Tarreau59234e92008-11-30 23:51:27 +01001601 /* 1: we might have to print this header in debug mode */
1602 if (unlikely((global.mode & MODE_DEBUG) &&
1603 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
1604 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
1605 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001606
Willy Tarreau59234e92008-11-30 23:51:27 +01001607 sol = req->data + msg->som;
1608 eol = sol + msg->sl.rq.l;
1609 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001610
Willy Tarreau59234e92008-11-30 23:51:27 +01001611 sol += hdr_idx_first_pos(&txn->hdr_idx);
1612 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001613
Willy Tarreau59234e92008-11-30 23:51:27 +01001614 while (cur_idx) {
1615 eol = sol + txn->hdr_idx.v[cur_idx].len;
1616 debug_hdr("clihdr", s, sol, eol);
1617 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1618 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001620 }
1621
Willy Tarreau58f10d72006-12-04 02:26:12 +01001622
Willy Tarreau59234e92008-11-30 23:51:27 +01001623 /*
1624 * Now we quickly check if we have found a full valid request.
1625 * If not so, we check the FD and buffer states before leaving.
1626 * A full request is indicated by the fact that we have seen
1627 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1628 * requests are checked first.
1629 *
1630 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001631
Willy Tarreau59234e92008-11-30 23:51:27 +01001632 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001633 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001634 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001635 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001636 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
1637 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001638
Willy Tarreau59234e92008-11-30 23:51:27 +01001639 /* 1: Since we are in header mode, if there's no space
1640 * left for headers, we won't be able to free more
1641 * later, so the session will never terminate. We
1642 * must terminate it now.
1643 */
1644 if (unlikely(req->flags & BF_FULL)) {
1645 /* FIXME: check if URI is set and return Status
1646 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001647 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001648 goto return_bad_req;
1649 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001650
Willy Tarreau59234e92008-11-30 23:51:27 +01001651 /* 2: have we encountered a read error ? */
1652 else if (req->flags & BF_READ_ERROR) {
1653 /* we cannot return any message on error */
1654 msg->msg_state = HTTP_MSG_ERROR;
1655 req->analysers = 0;
1656 s->fe->failed_req++;
1657 if (!(s->flags & SN_ERR_MASK))
1658 s->flags |= SN_ERR_CLICL;
1659 if (!(s->flags & SN_FINST_MASK))
1660 s->flags |= SN_FINST_R;
1661 return 0;
1662 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001663
Willy Tarreau59234e92008-11-30 23:51:27 +01001664 /* 3: has the read timeout expired ? */
1665 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
1666 /* read timeout : give up with an error message. */
1667 txn->status = 408;
1668 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
1669 msg->msg_state = HTTP_MSG_ERROR;
1670 req->analysers = 0;
1671 s->fe->failed_req++;
1672 if (!(s->flags & SN_ERR_MASK))
1673 s->flags |= SN_ERR_CLITO;
1674 if (!(s->flags & SN_FINST_MASK))
1675 s->flags |= SN_FINST_R;
1676 return 0;
1677 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001678
Willy Tarreau59234e92008-11-30 23:51:27 +01001679 /* 4: have we encountered a close ? */
1680 else if (req->flags & BF_SHUTR) {
1681 txn->status = 400;
1682 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1683 msg->msg_state = HTTP_MSG_ERROR;
1684 req->analysers = 0;
1685 s->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001686
Willy Tarreau59234e92008-11-30 23:51:27 +01001687 if (!(s->flags & SN_ERR_MASK))
1688 s->flags |= SN_ERR_CLICL;
1689 if (!(s->flags & SN_FINST_MASK))
1690 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001691 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001692 }
1693
Willy Tarreau59234e92008-11-30 23:51:27 +01001694 buffer_write_dis(req);
1695 /* just set the request timeout once at the beginning of the request */
1696 if (!tick_isset(req->analyse_exp))
1697 req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001698
Willy Tarreau59234e92008-11-30 23:51:27 +01001699 /* we're not ready yet */
1700 return 0;
1701 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001702
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001703
Willy Tarreau59234e92008-11-30 23:51:27 +01001704 /****************************************************************
1705 * More interesting part now : we know that we have a complete *
1706 * request which at least looks like HTTP. We have an indicator *
1707 * of each header's length, so we can parse them quickly. *
1708 ****************************************************************/
Willy Tarreau9cdde232007-05-02 20:58:19 +02001709
Willy Tarreau59234e92008-11-30 23:51:27 +01001710 req->analysers &= ~AN_REQ_HTTP_HDR;
1711 req->analyse_exp = TICK_ETERNITY;
1712
1713 /* ensure we keep this pointer to the beginning of the message */
1714 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001715
Willy Tarreau59234e92008-11-30 23:51:27 +01001716 /*
1717 * 1: identify the method
1718 */
1719 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
1720
1721 /* we can make use of server redirect on GET and HEAD */
1722 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1723 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001724
Willy Tarreau59234e92008-11-30 23:51:27 +01001725 /*
1726 * 2: check if the URI matches the monitor_uri.
1727 * We have to do this for every request which gets in, because
1728 * the monitor-uri is defined by the frontend.
1729 */
1730 if (unlikely((s->fe->monitor_uri_len != 0) &&
1731 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1732 !memcmp(&req->data[msg->sl.rq.u],
1733 s->fe->monitor_uri,
1734 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001735 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001736 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001737 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001738 struct acl_cond *cond;
1739 cur_proxy = s->fe;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001740
Willy Tarreau59234e92008-11-30 23:51:27 +01001741 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001742
Willy Tarreau59234e92008-11-30 23:51:27 +01001743 /* Check if we want to fail this monitor request or not */
1744 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1745 int ret = acl_exec_cond(cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001746
Willy Tarreau59234e92008-11-30 23:51:27 +01001747 ret = acl_pass(ret);
1748 if (cond->pol == ACL_COND_UNLESS)
1749 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001750
Willy Tarreau59234e92008-11-30 23:51:27 +01001751 if (ret) {
1752 /* we fail this request, let's return 503 service unavail */
1753 txn->status = 503;
1754 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
1755 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001756 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001758
Willy Tarreau59234e92008-11-30 23:51:27 +01001759 /* nothing to fail, let's reply normaly */
1760 txn->status = 200;
1761 stream_int_retnclose(req->prod, &http_200_chunk);
1762 goto return_prx_cond;
1763 }
1764
1765 /*
1766 * 3: Maybe we have to copy the original REQURI for the logs ?
1767 * Note: we cannot log anymore if the request has been
1768 * classified as invalid.
1769 */
1770 if (unlikely(s->logs.logwait & LW_REQ)) {
1771 /* we have a complete HTTP request that we must log */
1772 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1773 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001774
Willy Tarreau59234e92008-11-30 23:51:27 +01001775 if (urilen >= REQURI_LEN)
1776 urilen = REQURI_LEN - 1;
1777 memcpy(txn->uri, &req->data[msg->som], urilen);
1778 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001779
Willy Tarreau59234e92008-11-30 23:51:27 +01001780 if (!(s->logs.logwait &= ~LW_REQ))
1781 s->do_log(s);
1782 } else {
1783 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001785 }
Willy Tarreau06619262006-12-17 08:37:22 +01001786
Willy Tarreau59234e92008-11-30 23:51:27 +01001787 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1788 if (unlikely(msg->sl.rq.v_l == 0)) {
1789 int delta;
1790 char *cur_end;
1791 msg->sol = req->data + msg->som;
1792 cur_end = msg->sol + msg->sl.rq.l;
1793 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001794
Willy Tarreau59234e92008-11-30 23:51:27 +01001795 if (msg->sl.rq.u_l == 0) {
1796 /* if no URI was set, add "/" */
1797 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001798 cur_end += delta;
Willy Tarreau59234e92008-11-30 23:51:27 +01001799 msg->eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001800 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001801 /* add HTTP version */
1802 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1803 msg->eoh += delta;
1804 cur_end += delta;
1805 cur_end = (char *)http_parse_reqline(msg, req->data,
1806 HTTP_MSG_RQMETH,
1807 msg->sol, cur_end + 1,
1808 NULL, NULL);
1809 if (unlikely(!cur_end))
1810 goto return_bad_req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811
Willy Tarreau59234e92008-11-30 23:51:27 +01001812 /* we have a full HTTP/1.0 request now and we know that
1813 * we have either a CR or an LF at <ptr>.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001814 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001815 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1816 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001817
1818
Willy Tarreau59234e92008-11-30 23:51:27 +01001819 /* 5: we may need to capture headers */
1820 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
1821 capture_headers(req->data + msg->som, &txn->hdr_idx,
1822 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001823
Willy Tarreau59234e92008-11-30 23:51:27 +01001824 /*
1825 * 6: we will have to evaluate the filters.
1826 * As opposed to version 1.2, now they will be evaluated in the
1827 * filters order and not in the header order. This means that
1828 * each filter has to be validated among all headers.
1829 *
1830 * We can now check whether we want to switch to another
1831 * backend, in which case we will re-check the backend's
1832 * filters and various options. In order to support 3-level
1833 * switching, here's how we should proceed :
1834 *
1835 * a) run be.
1836 * if (switch) then switch ->be to the new backend.
1837 * b) run be if (be != fe).
1838 * There cannot be any switch from there, so ->be cannot be
1839 * changed anymore.
1840 *
1841 * => filters always apply to ->be, then ->be may change.
1842 *
1843 * The response path will be able to apply either ->be, or
1844 * ->be then ->fe filters in order to match the reverse of
1845 * the forward sequence.
1846 */
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001847
Willy Tarreau59234e92008-11-30 23:51:27 +01001848 do {
1849 struct acl_cond *cond;
1850 struct redirect_rule *rule;
1851 struct proxy *rule_set = s->be;
1852 cur_proxy = s->be;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001853
Willy Tarreau59234e92008-11-30 23:51:27 +01001854 /* first check whether we have some ACLs set to redirect this request */
1855 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
1856 int ret = acl_exec_cond(rule->cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001857
Willy Tarreau59234e92008-11-30 23:51:27 +01001858 ret = acl_pass(ret);
1859 if (rule->cond->pol == ACL_COND_UNLESS)
1860 ret = !ret;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001861
Willy Tarreau59234e92008-11-30 23:51:27 +01001862 if (ret) {
1863 struct chunk rdr = { trash, 0 };
1864 const char *msg_fmt;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001865
Willy Tarreau59234e92008-11-30 23:51:27 +01001866 /* build redirect message */
1867 switch(rule->code) {
1868 case 303:
1869 rdr.len = strlen(HTTP_303);
1870 msg_fmt = HTTP_303;
1871 break;
1872 case 301:
1873 rdr.len = strlen(HTTP_301);
1874 msg_fmt = HTTP_301;
1875 break;
1876 case 302:
1877 default:
1878 rdr.len = strlen(HTTP_302);
1879 msg_fmt = HTTP_302;
1880 break;
1881 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001882
Willy Tarreau59234e92008-11-30 23:51:27 +01001883 if (unlikely(rdr.len > sizeof(trash)))
1884 goto return_bad_req;
1885 memcpy(rdr.str, msg_fmt, rdr.len);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001886
Willy Tarreau59234e92008-11-30 23:51:27 +01001887 switch(rule->type) {
1888 case REDIRECT_TYPE_PREFIX: {
1889 const char *path;
1890 int pathlen;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001891
Willy Tarreau59234e92008-11-30 23:51:27 +01001892 path = http_get_path(txn);
1893 /* build message using path */
1894 if (path) {
1895 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
Willy Tarreau79da4692008-11-19 20:03:04 +01001896 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
1897 int qs = 0;
1898 while (qs < pathlen) {
1899 if (path[qs] == '?') {
1900 pathlen = qs;
1901 break;
1902 }
1903 qs++;
1904 }
1905 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001906 } else {
1907 path = "/";
1908 pathlen = 1;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001909 }
1910
Willy Tarreau59234e92008-11-30 23:51:27 +01001911 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
1912 goto return_bad_req;
1913
Willy Tarreaufe651a52008-11-19 21:15:17 +01001914 /* add prefix. Note that if prefix == "/", we don't want to
1915 * add anything, otherwise it makes it hard for the user to
1916 * configure a self-redirection.
1917 */
1918 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
1919 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1920 rdr.len += rule->rdr_len;
1921 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001922
Willy Tarreau59234e92008-11-30 23:51:27 +01001923 /* add path */
1924 memcpy(rdr.str + rdr.len, path, pathlen);
1925 rdr.len += pathlen;
1926 break;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001927 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001928 case REDIRECT_TYPE_LOCATION:
1929 default:
1930 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
1931 goto return_bad_req;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001932
Willy Tarreau59234e92008-11-30 23:51:27 +01001933 /* add location */
1934 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1935 rdr.len += rule->rdr_len;
1936 break;
1937 }
Willy Tarreau11382812008-07-09 16:18:21 +02001938
Willy Tarreau0140f252008-11-19 21:07:09 +01001939 if (rule->cookie_len) {
1940 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
1941 rdr.len += 14;
1942 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
1943 rdr.len += rule->cookie_len;
1944 memcpy(rdr.str + rdr.len, "\r\n", 2);
1945 rdr.len += 2;
1946 }
1947
Willy Tarreau59234e92008-11-30 23:51:27 +01001948 /* add end of headers */
1949 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
1950 rdr.len += 4;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001951
Willy Tarreau59234e92008-11-30 23:51:27 +01001952 txn->status = rule->code;
1953 /* let's log the request time */
1954 s->logs.tv_request = now;
1955 stream_int_retnclose(req->prod, &rdr);
1956 goto return_prx_cond;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001957 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001958 }
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001959
Willy Tarreau59234e92008-11-30 23:51:27 +01001960 /* first check whether we have some ACLs set to block this request */
1961 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
1962 int ret = acl_exec_cond(cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau53b6c742006-12-17 13:37:46 +01001963
Willy Tarreau59234e92008-11-30 23:51:27 +01001964 ret = acl_pass(ret);
1965 if (cond->pol == ACL_COND_UNLESS)
1966 ret = !ret;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001967
Willy Tarreau59234e92008-11-30 23:51:27 +01001968 if (ret) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001969 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001970 /* let's log the request time */
Willy Tarreau59234e92008-11-30 23:51:27 +01001971 s->logs.tv_request = now;
1972 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001973 goto return_prx_cond;
1974 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001975 }
1976
1977 /* try headers filters */
1978 if (rule_set->req_exp != NULL) {
1979 if (apply_filters_to_request(s, req, rule_set->req_exp) < 0)
1980 goto return_bad_req;
1981 }
1982
1983 if (!(s->flags & SN_BE_ASSIGNED) && (s->be != cur_proxy)) {
1984 /* to ensure correct connection accounting on
1985 * the backend, we count the connection for the
1986 * one managing the queue.
1987 */
1988 s->be->beconn++;
1989 if (s->be->beconn > s->be->beconn_max)
1990 s->be->beconn_max = s->be->beconn;
1991 s->be->cum_beconn++;
1992 s->flags |= SN_BE_ASSIGNED;
1993 }
Willy Tarreau06619262006-12-17 08:37:22 +01001994
Willy Tarreau59234e92008-11-30 23:51:27 +01001995 /* has the request been denied ? */
1996 if (txn->flags & TX_CLDENY) {
1997 /* no need to go further */
1998 txn->status = 403;
1999 /* let's log the request time */
2000 s->logs.tv_request = now;
2001 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2002 goto return_prx_cond;
2003 }
Willy Tarreau06619262006-12-17 08:37:22 +01002004
Willy Tarreau59234e92008-11-30 23:51:27 +01002005 /* We might have to check for "Connection:" */
2006 if (((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2007 !(s->flags & SN_CONN_CLOSED)) {
2008 char *cur_ptr, *cur_end, *cur_next;
2009 int cur_idx, old_idx, delta, val;
2010 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002011
Willy Tarreau59234e92008-11-30 23:51:27 +01002012 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
2013 old_idx = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002014
Willy Tarreau59234e92008-11-30 23:51:27 +01002015 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2016 cur_hdr = &txn->hdr_idx.v[cur_idx];
2017 cur_ptr = cur_next;
2018 cur_end = cur_ptr + cur_hdr->len;
2019 cur_next = cur_end + cur_hdr->cr + 1;
2020
2021 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2022 if (val) {
2023 /* 3 possibilities :
2024 * - we have already set Connection: close,
2025 * so we remove this line.
2026 * - we have not yet set Connection: close,
2027 * but this line indicates close. We leave
2028 * it untouched and set the flag.
2029 * - we have not yet set Connection: close,
2030 * and this line indicates non-close. We
2031 * replace it.
2032 */
2033 if (s->flags & SN_CONN_CLOSED) {
2034 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2035 txn->req.eoh += delta;
2036 cur_next += delta;
2037 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2038 txn->hdr_idx.used--;
2039 cur_hdr->len = 0;
2040 } else {
2041 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2042 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2043 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002044 cur_next += delta;
Willy Tarreau59234e92008-11-30 23:51:27 +01002045 cur_hdr->len += delta;
2046 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002047 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002048 s->flags |= SN_CONN_CLOSED;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002049 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002050 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002051 old_idx = cur_idx;
Willy Tarreau06619262006-12-17 08:37:22 +01002052 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002053 }
2054 /* add request headers from the rule sets in the same order */
2055 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2056 if (unlikely(http_header_add_tail(req,
2057 &txn->req,
2058 &txn->hdr_idx,
2059 rule_set->req_add[cur_idx])) < 0)
2060 goto return_bad_req;
2061 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002062
Willy Tarreau59234e92008-11-30 23:51:27 +01002063 /* check if stats URI was requested, and if an auth is needed */
2064 if (rule_set->uri_auth != NULL &&
2065 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2066 /* we have to check the URI and auth for this request.
2067 * FIXME!!! that one is rather dangerous, we want to
2068 * make it follow standard rules (eg: clear req->analysers).
2069 */
2070 if (stats_check_uri_auth(s, rule_set)) {
2071 req->analysers = 0;
2072 return 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01002073 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002074 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002075
Willy Tarreau59234e92008-11-30 23:51:27 +01002076 /* now check whether we have some switching rules for this request */
2077 if (!(s->flags & SN_BE_ASSIGNED)) {
2078 struct switching_rule *rule;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002079
Willy Tarreau59234e92008-11-30 23:51:27 +01002080 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2081 int ret;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002082
Willy Tarreau59234e92008-11-30 23:51:27 +01002083 ret = acl_exec_cond(rule->cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002084
Willy Tarreau59234e92008-11-30 23:51:27 +01002085 ret = acl_pass(ret);
2086 if (rule->cond->pol == ACL_COND_UNLESS)
2087 ret = !ret;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002088
Willy Tarreau59234e92008-11-30 23:51:27 +01002089 if (ret) {
2090 s->be = rule->be.backend;
2091 s->be->beconn++;
2092 if (s->be->beconn > s->be->beconn_max)
2093 s->be->beconn_max = s->be->beconn;
2094 s->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002095
Willy Tarreau59234e92008-11-30 23:51:27 +01002096 /* assign new parameters to the session from the new backend */
2097 s->rep->rto = s->req->wto = s->be->timeout.server;
2098 s->req->cto = s->be->timeout.connect;
2099 s->conn_retries = s->be->conn_retries;
2100 s->flags |= SN_BE_ASSIGNED;
2101 break;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002102 }
2103 }
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002104 }
2105
Willy Tarreau59234e92008-11-30 23:51:27 +01002106 if (!(s->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2107 /* No backend was set, but there was a default
2108 * backend set in the frontend, so we use it and
2109 * loop again.
2110 */
2111 s->be = cur_proxy->defbe.be;
2112 s->be->beconn++;
2113 if (s->be->beconn > s->be->beconn_max)
2114 s->be->beconn_max = s->be->beconn;
2115 s->be->cum_beconn++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002116
Willy Tarreau59234e92008-11-30 23:51:27 +01002117 /* assign new parameters to the session from the new backend */
2118 s->rep->rto = s->req->wto = s->be->timeout.server;
2119 s->req->cto = s->be->timeout.connect;
2120 s->conn_retries = s->be->conn_retries;
2121 s->flags |= SN_BE_ASSIGNED;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002122 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002123 } while (s->be != cur_proxy); /* we loop only if s->be has changed */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002124
Willy Tarreau59234e92008-11-30 23:51:27 +01002125 if (!(s->flags & SN_BE_ASSIGNED)) {
2126 /* To ensure correct connection accounting on
2127 * the backend, we count the connection for the
2128 * one managing the queue.
Willy Tarreau06619262006-12-17 08:37:22 +01002129 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002130 s->be->beconn++;
2131 if (s->be->beconn > s->be->beconn_max)
2132 s->be->beconn_max = s->be->beconn;
2133 s->be->cum_beconn++;
2134 s->flags |= SN_BE_ASSIGNED;
2135 }
Willy Tarreau06619262006-12-17 08:37:22 +01002136
Willy Tarreau59234e92008-11-30 23:51:27 +01002137 /*
2138 * Right now, we know that we have processed the entire headers
2139 * and that unwanted requests have been filtered out. We can do
2140 * whatever we want with the remaining request. Also, now we
2141 * may have separate values for ->fe, ->be.
2142 */
Willy Tarreau06619262006-12-17 08:37:22 +01002143
Willy Tarreau59234e92008-11-30 23:51:27 +01002144 /*
2145 * If HTTP PROXY is set we simply get remote server address
2146 * parsing incoming request.
2147 */
2148 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2149 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2150 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002151
Willy Tarreau59234e92008-11-30 23:51:27 +01002152 /*
2153 * 7: the appsession cookie was looked up very early in 1.2,
2154 * so let's do the same now.
2155 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002156
Willy Tarreau59234e92008-11-30 23:51:27 +01002157 /* It needs to look into the URI */
2158 if (s->be->appsession_name) {
2159 get_srv_from_appsession(s, &req->data[msg->som], msg->sl.rq.l);
2160 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01002161
Willy Tarreau59234e92008-11-30 23:51:27 +01002162 /*
2163 * 8: Now we can work with the cookies.
2164 * Note that doing so might move headers in the request, but
2165 * the fields will stay coherent and the URI will not move.
2166 * This should only be performed in the backend.
2167 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002168 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002169 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2170 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002171
Willy Tarreau59234e92008-11-30 23:51:27 +01002172 /*
2173 * 9: add X-Forwarded-For if either the frontend or the backend
2174 * asks for it.
2175 */
2176 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2177 if (s->cli_addr.ss_family == AF_INET) {
2178 /* Add an X-Forwarded-For header unless the source IP is
2179 * in the 'except' network range.
2180 */
2181 if ((!s->fe->except_mask.s_addr ||
2182 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2183 != s->fe->except_net.s_addr) &&
2184 (!s->be->except_mask.s_addr ||
2185 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2186 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002187 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002188 unsigned char *pn;
2189 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002190
2191 /* Note: we rely on the backend to get the header name to be used for
2192 * x-forwarded-for, because the header is really meant for the backends.
2193 * However, if the backend did not specify any option, we have to rely
2194 * on the frontend's header name.
2195 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002196 if (s->be->fwdfor_hdr_len) {
2197 len = s->be->fwdfor_hdr_len;
2198 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002199 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002200 len = s->fe->fwdfor_hdr_len;
2201 memcpy(trash, s->fe->fwdfor_hdr_name, len);
2202 }
2203 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002204
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002205 if (unlikely(http_header_add_tail2(req, &txn->req,
2206 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002207 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002208 }
2209 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002210 else if (s->cli_addr.ss_family == AF_INET6) {
2211 /* FIXME: for the sake of completeness, we should also support
2212 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002213 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002214 int len;
2215 char pn[INET6_ADDRSTRLEN];
2216 inet_ntop(AF_INET6,
2217 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2218 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002219
Willy Tarreau59234e92008-11-30 23:51:27 +01002220 /* Note: we rely on the backend to get the header name to be used for
2221 * x-forwarded-for, because the header is really meant for the backends.
2222 * However, if the backend did not specify any option, we have to rely
2223 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002224 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002225 if (s->be->fwdfor_hdr_len) {
2226 len = s->be->fwdfor_hdr_len;
2227 memcpy(trash, s->be->fwdfor_hdr_name, len);
2228 } else {
2229 len = s->fe->fwdfor_hdr_len;
2230 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002231 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002232 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002233
Willy Tarreau59234e92008-11-30 23:51:27 +01002234 if (unlikely(http_header_add_tail2(req, &txn->req,
2235 &txn->hdr_idx, trash, len)) < 0)
2236 goto return_bad_req;
2237 }
2238 }
2239
2240 /*
2241 * 10: add "Connection: close" if needed and not yet set.
2242 * Note that we do not need to add it in case of HTTP/1.0.
2243 */
2244 if (!(s->flags & SN_CONN_CLOSED) &&
2245 ((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2246 if ((unlikely(msg->sl.rq.v_l != 8) ||
2247 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2248 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
2249 "Connection: close", 17)) < 0)
2250 goto return_bad_req;
2251 s->flags |= SN_CONN_CLOSED;
2252 }
2253 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2254 * If not assigned, perhaps we are balancing on url_param, but this is a
2255 * POST; and the parameters are in the body, maybe scan there to find our server.
2256 * (unless headers overflowed the buffer?)
2257 */
2258 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2259 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
2260 s->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
2261 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2262 /* are there enough bytes here? total == l || r || rlim ?
2263 * len is unsigned, but eoh is int,
2264 * how many bytes of body have we received?
2265 * eoh is the first empty line of the header
2266 */
2267 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
2268 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
2269
2270 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2271 * We can't assume responsibility for the server's decision,
2272 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2273 * We also can't change our mind later, about which server to choose, so round robin.
2274 */
2275 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2276 struct hdr_ctx ctx;
2277 ctx.idx = 0;
2278 /* Expect is allowed in 1.1, look for it */
2279 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2280 if (ctx.idx != 0 &&
2281 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
2282 /* We can't reliablly stall and wait for data, because of
2283 * .NET clients that don't conform to rfc2616; so, no need for
2284 * the next block to check length expectations.
2285 * We could send 100 status back to the client, but then we need to
2286 * re-write headers, and send the message. And this isn't the right
2287 * place for that action.
2288 * TODO: support Expect elsewhere and delete this block.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002289 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002290 goto end_check_maybe_wait_for_body;
2291 }
2292
2293 if (likely(len > s->be->url_param_post_limit)) {
2294 /* nothing to do, we got enough */
2295 } else {
2296 /* limit implies we are supposed to need this many bytes
2297 * to find the parameter. Let's see how many bytes we can wait for.
2298 */
2299 long long hint = len;
2300 struct hdr_ctx ctx;
2301 ctx.idx = 0;
2302 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
2303 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2304 buffer_write_dis(req);
2305 req->analysers |= AN_REQ_HTTP_BODY;
2306 }
2307 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002308 ctx.idx = 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002309 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2310 /* now if we have a length, we'll take the hint */
2311 if (ctx.idx) {
2312 /* We have Content-Length */
2313 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
2314 hint = 0; /* parse failure, untrusted client */
2315 else {
2316 if (hint > 0)
2317 msg->hdr_content_len = hint;
2318 else
2319 hint = 0; /* bad client, sent negative length */
2320 }
2321 }
2322 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
2323 if (s->be->url_param_post_limit < hint)
2324 hint = s->be->url_param_post_limit;
2325 /* now do we really need to buffer more data? */
2326 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002327 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002328 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002329 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002330 /* else... There are no body bytes to wait for */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002331 }
2332 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002333 }
2334 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002335
Willy Tarreau59234e92008-11-30 23:51:27 +01002336 /*************************************************************
2337 * OK, that's finished for the headers. We have done what we *
2338 * could. Let's switch to the DATA state. *
2339 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002340
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
2342 s->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002343
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 /* When a connection is tarpitted, we use the tarpit timeout,
2345 * which may be the same as the connect timeout if unspecified.
2346 * If unset, then set it to zero because we really want it to
2347 * eventually expire. We build the tarpit as an analyser.
2348 */
2349 if (txn->flags & TX_CLTARPIT) {
2350 buffer_flush(s->req);
2351 /* flush the request so that we can drop the connection early
2352 * if the client closes first.
Willy Tarreau2a324282006-12-05 00:05:46 +01002353 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002354 buffer_write_dis(req);
2355 req->analysers |= AN_REQ_HTTP_TARPIT;
2356 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2357 if (!req->analyse_exp)
2358 req->analyse_exp = now_ms;
2359 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002360
Willy Tarreau59234e92008-11-30 23:51:27 +01002361 /* OK let's go on with the BODY now */
2362 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002363
Willy Tarreau59234e92008-11-30 23:51:27 +01002364 return_bad_req: /* let's centralize all bad requests */
2365 txn->req.msg_state = HTTP_MSG_ERROR;
2366 txn->status = 400;
2367 req->analysers = 0;
2368 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2369 s->fe->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002370
Willy Tarreau59234e92008-11-30 23:51:27 +01002371 return_prx_cond:
2372 if (!(s->flags & SN_ERR_MASK))
2373 s->flags |= SN_ERR_PRXCOND;
2374 if (!(s->flags & SN_FINST_MASK))
2375 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002376 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002377}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002378
Willy Tarreau60b85b02008-11-30 23:28:40 +01002379/* This function is an analyser which processes the HTTP tarpit. It always
2380 * returns zero, at the beginning because it prevents any other processing
2381 * from occurring, and at the end because it terminates the request.
2382 */
2383int http_process_tarpit(struct session *s, struct buffer *req)
2384{
2385 struct http_txn *txn = &s->txn;
2386
2387 /* This connection is being tarpitted. The CLIENT side has
2388 * already set the connect expiration date to the right
2389 * timeout. We just have to check that the client is still
2390 * there and that the timeout has not expired.
2391 */
2392 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
2393 !tick_is_expired(req->analyse_exp, now_ms))
2394 return 0;
2395
2396 /* We will set the queue timer to the time spent, just for
2397 * logging purposes. We fake a 500 server error, so that the
2398 * attacker will not suspect his connection has been tarpitted.
2399 * It will not cause trouble to the logs because we can exclude
2400 * the tarpitted connections by filtering on the 'PT' status flags.
2401 */
2402 trace_term(s, TT_HTTP_SRV_2);
2403 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2404
2405 txn->status = 500;
2406 if (req->flags != BF_READ_ERROR)
2407 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
2408
2409 req->analysers = 0;
2410 req->analyse_exp = TICK_ETERNITY;
2411
2412 s->fe->failed_req++;
2413 if (!(s->flags & SN_ERR_MASK))
2414 s->flags |= SN_ERR_PRXCOND;
2415 if (!(s->flags & SN_FINST_MASK))
2416 s->flags |= SN_FINST_T;
2417 return 0;
2418}
2419
Willy Tarreaud34af782008-11-30 23:36:37 +01002420/* This function is an analyser which processes the HTTP request body. It looks
2421 * for parameters to be used for the load balancing algorithm (url_param). It
2422 * must only be called after the standard HTTP request processing has occurred,
2423 * because it expects the request to be parsed. It returns zero if it needs to
2424 * read more data, or 1 once it has completed its analysis.
2425 */
2426int http_process_request_body(struct session *s, struct buffer *req)
2427{
2428 struct http_msg *msg = &s->txn.req;
2429 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2430 long long limit = s->be->url_param_post_limit;
2431 struct hdr_ctx ctx;
2432
2433 /* We have to parse the HTTP request body to find any required data.
2434 * "balance url_param check_post" should have been the only way to get
2435 * into this. We were brought here after HTTP header analysis, so all
2436 * related structures are ready.
2437 */
2438
2439 ctx.idx = 0;
2440
2441 /* now if we have a length, we'll take the hint */
2442 http_find_header2("Transfer-Encoding", 17, msg->sol, &s->txn.hdr_idx, &ctx);
2443 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2444 unsigned int chunk = 0;
2445 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2446 char c = msg->sol[body];
2447 if (ishex(c)) {
2448 unsigned int hex = toupper(c) - '0';
2449 if (hex > 9)
2450 hex -= 'A' - '9' - 1;
2451 chunk = (chunk << 4) | hex;
2452 } else
2453 break;
2454 body++;
2455 }
2456 if (body + 2 >= req->l) /* we want CRLF too */
2457 goto http_body_end; /* end of buffer? data missing! */
2458
2459 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
2460 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
2461
2462 body += 2; // skip CRLF
2463
2464 /* if we support more then one chunk here, we have to do it again when assigning server
2465 * 1. how much entity data do we have? new var
2466 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2467 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2468 */
2469
2470 if (chunk < limit)
2471 limit = chunk; /* only reading one chunk */
2472 } else {
2473 if (msg->hdr_content_len < limit)
2474 limit = msg->hdr_content_len;
2475 }
2476
2477 http_body_end:
2478 /* we leave once we know we have nothing left to do. This means that we have
2479 * enough bytes, or that we know we'll not get any more (buffer full, read
2480 * buffer closed).
2481 */
2482 if (req->l - body >= limit || /* enough bytes! */
2483 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
2484 tick_is_expired(req->analyse_exp, now_ms)) {
2485 /* The situation will not evolve, so let's give up on the analysis. */
2486 s->logs.tv_request = now; /* update the request timer to reflect full request */
2487 req->analysers &= ~AN_REQ_HTTP_BODY;
2488 req->analyse_exp = TICK_ETERNITY;
2489 return 1;
2490 }
2491 else {
2492 /* Not enough data. We'll re-use the http-request
2493 * timeout here. Ideally, we should set the timeout
2494 * relative to the accept() date. We just set the
2495 * request timeout once at the beginning of the
2496 * request.
2497 */
2498 buffer_write_dis(req);
2499 if (!tick_isset(req->analyse_exp))
2500 req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq);
2501 return 0;
2502 }
2503}
2504
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002505/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002506 * It normally returns zero, but may return 1 if it absolutely needs to be
2507 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002508 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002509 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002510 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002511int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002512{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002513 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002514 struct buffer *req = t->req;
2515 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002516
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002517 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 +02002518 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002519 t,
2520 rep,
2521 rep->rex, rep->wex,
2522 rep->flags,
2523 rep->l,
2524 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002525
Willy Tarreau2df28e82008-08-17 15:20:19 +02002526 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002527 /*
2528 * Now parse the partial (or complete) lines.
2529 * We will check the response syntax, and also join multi-line
2530 * headers. An index of all the lines will be elaborated while
2531 * parsing.
2532 *
2533 * For the parsing, we use a 28 states FSM.
2534 *
2535 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002536 * rep->data + rep->som = beginning of response
2537 * rep->data + rep->eoh = end of processed headers / start of current one
2538 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002539 * rep->lr = first non-visited byte
2540 * rep->r = end of data
2541 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002542
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002543 int cur_idx;
2544 struct http_msg *msg = &txn->rsp;
2545 struct proxy *cur_proxy;
2546
2547 if (likely(rep->lr < rep->r))
2548 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2549
2550 /* 1: we might have to print this header in debug mode */
2551 if (unlikely((global.mode & MODE_DEBUG) &&
2552 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2553 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2554 char *eol, *sol;
2555
2556 sol = rep->data + msg->som;
2557 eol = sol + msg->sl.rq.l;
2558 debug_hdr("srvrep", t, sol, eol);
2559
2560 sol += hdr_idx_first_pos(&txn->hdr_idx);
2561 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2562
2563 while (cur_idx) {
2564 eol = sol + txn->hdr_idx.v[cur_idx].len;
2565 debug_hdr("srvhdr", t, sol, eol);
2566 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2567 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002568 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002569 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002570
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002571 /*
2572 * Now we quickly check if we have found a full valid response.
2573 * If not so, we check the FD and buffer states before leaving.
2574 * A full response is indicated by the fact that we have seen
2575 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2576 * responses are checked first.
2577 *
2578 * Depending on whether the client is still there or not, we
2579 * may send an error response back or not. Note that normally
2580 * we should only check for HTTP status there, and check I/O
2581 * errors somewhere else.
2582 */
2583
2584 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002585 /* Invalid response */
2586 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2587 hdr_response_bad:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002588 //buffer_shutr(rep);
2589 //buffer_shutw(req);
2590 //fd_delete(req->cons->fd);
2591 //req->cons->state = SI_ST_CLO;
2592 buffer_shutr_now(rep);
2593 buffer_shutw_now(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002594 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002595 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002596 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002597 //sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002598 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002599 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002600 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002601 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002602 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002603 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002604 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002605 if (!(t->flags & SN_FINST_MASK))
2606 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002607
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002608 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2609 // process_srv_queue(t->srv);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002610
Willy Tarreaudafde432008-08-17 01:00:46 +02002611 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002612 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002613 /* too large response does not fit in buffer. */
2614 else if (rep->flags & BF_FULL) {
2615 goto hdr_response_bad;
2616 }
2617 /* read error */
2618 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002619 buffer_shutr_now(rep);
2620 buffer_shutw_now(req);
2621 //fd_delete(req->cons->fd);
2622 //req->cons->state = SI_ST_CLO;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002623 //if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002624 //t->srv->cur_sess--;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002625 //t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002626 //sess_change_server(t, NULL);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002627 //}
2628 //t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002629 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002630 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002631 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002632 if (!(t->flags & SN_ERR_MASK))
2633 t->flags |= SN_ERR_SRVCL;
2634 if (!(t->flags & SN_FINST_MASK))
2635 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002636
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002637 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2638 // process_srv_queue(t->srv);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002639
Willy Tarreaudafde432008-08-17 01:00:46 +02002640 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002641 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002642 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002643 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002644 buffer_shutr_now(rep);
2645 buffer_shutw_now(req);
2646 //fd_delete(req->cons->fd);
2647 //req->cons->state = SI_ST_CLO;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002648 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002649 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002650 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002651 //sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002652 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002653 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002654 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002655 txn->status = 504;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002656 stream_int_return(rep->cons, error_message(t, HTTP_ERR_504));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002657 if (!(t->flags & SN_ERR_MASK))
2658 t->flags |= SN_ERR_SRVTO;
2659 if (!(t->flags & SN_FINST_MASK))
2660 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002661
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002662 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2663 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002664 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002665 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002666 /* write error to client, or close from server */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002667 else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002668 buffer_shutr_now(rep);
2669 buffer_shutw_now(req);
2670 //fd_delete(req->cons->fd);
2671 //req->cons->state = SI_ST_CLO;
2672 if (t->srv) {
2673 //t->srv->cur_sess--;
2674 t->srv->failed_resp++;
2675 //sess_change_server(t, NULL);
2676 }
2677 t->be->failed_resp++;
2678 rep->analysers = 0;
2679 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002680 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002681 if (!(t->flags & SN_ERR_MASK))
2682 t->flags |= SN_ERR_SRVCL;
2683 if (!(t->flags & SN_FINST_MASK))
2684 t->flags |= SN_FINST_H;
2685
2686 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2687 // process_srv_queue(t->srv);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002688
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002689 return 0;
2690 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02002691 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002692 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002693 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002694
Willy Tarreau21d2af32008-02-14 20:25:24 +01002695
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002696 /*****************************************************************
2697 * More interesting part now : we know that we have a complete *
2698 * response which at least looks like HTTP. We have an indicator *
2699 * of each header's length, so we can parse them quickly. *
2700 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002701
Willy Tarreau2df28e82008-08-17 15:20:19 +02002702 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002703
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002704 /* ensure we keep this pointer to the beginning of the message */
2705 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002706
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002707 /*
2708 * 1: get the status code and check for cacheability.
2709 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002710
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002711 t->logs.logwait &= ~LW_RESP;
2712 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002713
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002714 switch (txn->status) {
2715 case 200:
2716 case 203:
2717 case 206:
2718 case 300:
2719 case 301:
2720 case 410:
2721 /* RFC2616 @13.4:
2722 * "A response received with a status code of
2723 * 200, 203, 206, 300, 301 or 410 MAY be stored
2724 * by a cache (...) unless a cache-control
2725 * directive prohibits caching."
2726 *
2727 * RFC2616 @9.5: POST method :
2728 * "Responses to this method are not cacheable,
2729 * unless the response includes appropriate
2730 * Cache-Control or Expires header fields."
2731 */
2732 if (likely(txn->meth != HTTP_METH_POST) &&
2733 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2734 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2735 break;
2736 default:
2737 break;
2738 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002739
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002740 /*
2741 * 2: we may need to capture headers
2742 */
2743 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2744 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2745 txn->rsp.cap, t->fe->rsp_cap);
2746
2747 /*
2748 * 3: we will have to evaluate the filters.
2749 * As opposed to version 1.2, now they will be evaluated in the
2750 * filters order and not in the header order. This means that
2751 * each filter has to be validated among all headers.
2752 *
2753 * Filters are tried with ->be first, then with ->fe if it is
2754 * different from ->be.
2755 */
2756
2757 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2758
2759 cur_proxy = t->be;
2760 while (1) {
2761 struct proxy *rule_set = cur_proxy;
2762
2763 /* try headers filters */
2764 if (rule_set->rsp_exp != NULL) {
2765 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2766 return_bad_resp:
2767 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002768 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002769 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002770 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002771 }
2772 cur_proxy->failed_resp++;
2773 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002774 buffer_shutr_now(rep);
2775 buffer_shutw_now(req);
2776 //fd_delete(req->cons->fd);
2777 //req->cons->state = SI_ST_CLO;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002778 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002779 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002780 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002781 if (!(t->flags & SN_ERR_MASK))
2782 t->flags |= SN_ERR_PRXCOND;
2783 if (!(t->flags & SN_FINST_MASK))
2784 t->flags |= SN_FINST_H;
2785 /* We used to have a free connection slot. Since we'll never use it,
2786 * we have to inform the server that it may be used by another session.
2787 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002788 //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
2789 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002790 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002791 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002792 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002793
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002794 /* has the response been denied ? */
2795 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01002796 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002797 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002798 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002799 //sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002800 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002801 cur_proxy->denied_resp++;
2802 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002803 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002804
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002805 /* We might have to check for "Connection:" */
2806 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2807 !(t->flags & SN_CONN_CLOSED)) {
2808 char *cur_ptr, *cur_end, *cur_next;
2809 int cur_idx, old_idx, delta, val;
2810 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002811
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002812 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2813 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002814
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002815 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2816 cur_hdr = &txn->hdr_idx.v[cur_idx];
2817 cur_ptr = cur_next;
2818 cur_end = cur_ptr + cur_hdr->len;
2819 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002820
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002821 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2822 if (val) {
2823 /* 3 possibilities :
2824 * - we have already set Connection: close,
2825 * so we remove this line.
2826 * - we have not yet set Connection: close,
2827 * but this line indicates close. We leave
2828 * it untouched and set the flag.
2829 * - we have not yet set Connection: close,
2830 * and this line indicates non-close. We
2831 * replace it.
2832 */
2833 if (t->flags & SN_CONN_CLOSED) {
2834 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2835 txn->rsp.eoh += delta;
2836 cur_next += delta;
2837 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2838 txn->hdr_idx.used--;
2839 cur_hdr->len = 0;
2840 } else {
2841 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2842 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2843 "close", 5);
2844 cur_next += delta;
2845 cur_hdr->len += delta;
2846 txn->rsp.eoh += delta;
2847 }
2848 t->flags |= SN_CONN_CLOSED;
2849 }
2850 }
2851 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002852 }
2853 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002854
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002855 /* add response headers from the rule sets in the same order */
2856 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2857 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2858 rule_set->rsp_add[cur_idx])) < 0)
2859 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002860 }
2861
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002862 /* check whether we're already working on the frontend */
2863 if (cur_proxy == t->fe)
2864 break;
2865 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002866 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002867
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002868 /*
2869 * 4: check for server cookie.
2870 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002871 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002872 || (t->be->options & PR_O_CHK_CACHE))
2873 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002874
Willy Tarreaubaaee002006-06-26 02:48:02 +02002875
Willy Tarreaua15645d2007-03-18 16:22:39 +01002876 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002877 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002878 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002879 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2880 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002881
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002882 /*
2883 * 6: add server cookie in the response if needed
2884 */
2885 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2886 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2887 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002888
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002889 /* the server is known, it's not the one the client requested, we have to
2890 * insert a set-cookie here, except if we want to insert only on POST
2891 * requests and this one isn't. Note that servers which don't have cookies
2892 * (eg: some backup servers) will return a full cookie removal request.
2893 */
2894 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2895 t->be->cookie_name,
2896 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002897
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002898 if (t->be->cookie_domain)
2899 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002900
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002901 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2902 trash, len)) < 0)
2903 goto return_bad_resp;
2904 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002905
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002906 /* Here, we will tell an eventual cache on the client side that we don't
2907 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2908 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2909 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2910 */
2911 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002912
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002913 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2914
2915 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2916 "Cache-control: private", 22)) < 0)
2917 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002918 }
2919 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002920
Willy Tarreaubaaee002006-06-26 02:48:02 +02002921
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002922 /*
2923 * 7: check if result will be cacheable with a cookie.
2924 * We'll block the response if security checks have caught
2925 * nasty things such as a cacheable cookie.
2926 */
2927 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2928 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2929 (t->be->options & PR_O_CHK_CACHE)) {
2930
2931 /* we're in presence of a cacheable response containing
2932 * a set-cookie header. We'll block it as requested by
2933 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002934 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002935 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002936 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002937 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002938 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002939 }
2940 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002941
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002942 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2943 t->be->id, t->srv?t->srv->id:"<dispatch>");
2944 send_log(t->be, LOG_ALERT,
2945 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2946 t->be->id, t->srv?t->srv->id:"<dispatch>");
2947 goto return_srv_prx_502;
2948 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002949
2950 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002951 * 8: add "Connection: close" if needed and not yet set.
2952 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002953 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002954 if (!(t->flags & SN_CONN_CLOSED) &&
2955 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2956 if ((unlikely(msg->sl.st.v_l != 8) ||
2957 unlikely(req->data[msg->som + 7] != '0')) &&
2958 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2959 "Connection: close", 17)) < 0)
2960 goto return_bad_resp;
2961 t->flags |= SN_CONN_CLOSED;
2962 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002963
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002964 /*************************************************************
2965 * OK, that's finished for the headers. We have done what we *
2966 * could. Let's switch to the DATA state. *
2967 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002968
Willy Tarreaue393fe22008-08-16 22:18:07 +02002969 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002970 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002971
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002972#ifdef CONFIG_HAP_TCPSPLICE
2973 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
2974 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002975 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002976 }
2977#endif
2978 /* if the user wants to log as soon as possible, without counting
2979 * bytes from the server, then this is the right moment. We have
2980 * to temporarily assign bytes_out to log what we currently have.
2981 */
2982 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2983 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
2984 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002985 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002986 t->logs.bytes_out = 0;
2987 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002988
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002989 /* Note: we must not try to cheat by jumping directly to DATA,
2990 * otherwise we would not let the client side wake up.
2991 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002992
Willy Tarreaudafde432008-08-17 01:00:46 +02002993 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002994 }
Willy Tarreaudafde432008-08-17 01:00:46 +02002995
Willy Tarreau2df28e82008-08-17 15:20:19 +02002996 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2997 * probably reduce one day's debugging session.
2998 */
2999#ifdef DEBUG_DEV
3000 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3001 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3002 __FILE__, __LINE__, rep->analysers);
3003 ABORT_NOW();
3004 }
3005#endif
3006 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003007 return 0;
3008}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003009
Willy Tarreaubaaee002006-06-26 02:48:02 +02003010/*
3011 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003012 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02003013 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
3014 * buffer, which it uses to keep on being called when there is free space in
Willy Tarreau01bf8672008-12-07 18:03:29 +01003015 * the buffer, or simply by letting an empty buffer upon return.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003016 */
Willy Tarreau01bf8672008-12-07 18:03:29 +01003017void produce_content(struct session *s, struct buffer *rep)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003018{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003019 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau01bf8672008-12-07 18:03:29 +01003020 buffer_stop_hijack(rep);
3021 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003022 }
3023 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003024 /* dump server statistics */
Willy Tarreau0a464892008-12-07 18:30:00 +01003025 int ret = stats_dump_http(s, rep, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003026 if (ret >= 0)
Willy Tarreau01bf8672008-12-07 18:03:29 +01003027 return;
Willy Tarreau91861262007-10-17 17:06:05 +02003028 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003029 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003030
Willy Tarreau91861262007-10-17 17:06:05 +02003031 /* unknown data source or internal error */
3032 s->txn.status = 500;
Willy Tarreau01bf8672008-12-07 18:03:29 +01003033 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003034 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02003035 if (!(s->flags & SN_ERR_MASK))
3036 s->flags |= SN_ERR_PRXCOND;
3037 if (!(s->flags & SN_FINST_MASK))
3038 s->flags |= SN_FINST_R;
Willy Tarreau01bf8672008-12-07 18:03:29 +01003039 buffer_stop_hijack(rep);
3040 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003041}
3042
3043
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003044/* Iterate the same filter through all request headers.
3045 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003046 * Since it can manage the switch to another backend, it updates the per-proxy
3047 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003048 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003049int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003050{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003051 char term;
3052 char *cur_ptr, *cur_end, *cur_next;
3053 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003054 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003055 struct hdr_idx_elem *cur_hdr;
3056 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003057
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003058 last_hdr = 0;
3059
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003060 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003061 old_idx = 0;
3062
3063 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003064 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003065 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003066 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003067 (exp->action == ACT_ALLOW ||
3068 exp->action == ACT_DENY ||
3069 exp->action == ACT_TARPIT))
3070 return 0;
3071
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003072 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003073 if (!cur_idx)
3074 break;
3075
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003076 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003077 cur_ptr = cur_next;
3078 cur_end = cur_ptr + cur_hdr->len;
3079 cur_next = cur_end + cur_hdr->cr + 1;
3080
3081 /* Now we have one header between cur_ptr and cur_end,
3082 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003083 */
3084
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003085 /* The annoying part is that pattern matching needs
3086 * that we modify the contents to null-terminate all
3087 * strings before testing them.
3088 */
3089
3090 term = *cur_end;
3091 *cur_end = '\0';
3092
3093 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3094 switch (exp->action) {
3095 case ACT_SETBE:
3096 /* It is not possible to jump a second time.
3097 * FIXME: should we return an HTTP/500 here so that
3098 * the admin knows there's a problem ?
3099 */
3100 if (t->be != t->fe)
3101 break;
3102
3103 /* Swithing Proxy */
3104 t->be = (struct proxy *) exp->replace;
3105
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003106 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003107 * because we have associated req_cap and rsp_cap to the
3108 * frontend, and the beconn will be updated later.
3109 */
3110
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003111 t->rep->rto = t->req->wto = t->be->timeout.server;
3112 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003113 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003114 last_hdr = 1;
3115 break;
3116
3117 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003118 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003119 last_hdr = 1;
3120 break;
3121
3122 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003123 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003124 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003125 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003126 break;
3127
3128 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003129 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003130 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003131 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003132 break;
3133
3134 case ACT_REPLACE:
3135 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3136 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3137 /* FIXME: if the user adds a newline in the replacement, the
3138 * index will not be recalculated for now, and the new line
3139 * will not be counted as a new header.
3140 */
3141
3142 cur_end += delta;
3143 cur_next += delta;
3144 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003145 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003146 break;
3147
3148 case ACT_REMOVE:
3149 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3150 cur_next += delta;
3151
3152 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003153 txn->req.eoh += delta;
3154 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3155 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003156 cur_hdr->len = 0;
3157 cur_end = NULL; /* null-term has been rewritten */
3158 break;
3159
3160 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003161 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003162 if (cur_end)
3163 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003164
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003165 /* keep the link from this header to next one in case of later
3166 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003167 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003168 old_idx = cur_idx;
3169 }
3170 return 0;
3171}
3172
3173
3174/* Apply the filter to the request line.
3175 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3176 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003177 * Since it can manage the switch to another backend, it updates the per-proxy
3178 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003179 */
3180int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3181{
3182 char term;
3183 char *cur_ptr, *cur_end;
3184 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003185 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003186 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003187
Willy Tarreau58f10d72006-12-04 02:26:12 +01003188
Willy Tarreau3d300592007-03-18 18:34:41 +01003189 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003190 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003191 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003192 (exp->action == ACT_ALLOW ||
3193 exp->action == ACT_DENY ||
3194 exp->action == ACT_TARPIT))
3195 return 0;
3196 else if (exp->action == ACT_REMOVE)
3197 return 0;
3198
3199 done = 0;
3200
Willy Tarreau9cdde232007-05-02 20:58:19 +02003201 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003202 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003203
3204 /* Now we have the request line between cur_ptr and cur_end */
3205
3206 /* The annoying part is that pattern matching needs
3207 * that we modify the contents to null-terminate all
3208 * strings before testing them.
3209 */
3210
3211 term = *cur_end;
3212 *cur_end = '\0';
3213
3214 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3215 switch (exp->action) {
3216 case ACT_SETBE:
3217 /* It is not possible to jump a second time.
3218 * FIXME: should we return an HTTP/500 here so that
3219 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003220 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003221 if (t->be != t->fe)
3222 break;
3223
3224 /* Swithing Proxy */
3225 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003226
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003227 /* right now, the backend switch is not too much complicated
3228 * because we have associated req_cap and rsp_cap to the
3229 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003230 */
3231
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003232 t->rep->rto = t->req->wto = t->be->timeout.server;
3233 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003234 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003235 done = 1;
3236 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003237
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003238 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003239 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003240 done = 1;
3241 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003242
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003243 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003244 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003245 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003246 done = 1;
3247 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003248
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003249 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003250 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003251 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003252 done = 1;
3253 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003254
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003255 case ACT_REPLACE:
3256 *cur_end = term; /* restore the string terminator */
3257 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3258 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3259 /* FIXME: if the user adds a newline in the replacement, the
3260 * index will not be recalculated for now, and the new line
3261 * will not be counted as a new header.
3262 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003263
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003264 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003265 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003266
Willy Tarreau9cdde232007-05-02 20:58:19 +02003267 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003268 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003269 HTTP_MSG_RQMETH,
3270 cur_ptr, cur_end + 1,
3271 NULL, NULL);
3272 if (unlikely(!cur_end))
3273 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003274
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003275 /* we have a full request and we know that we have either a CR
3276 * or an LF at <ptr>.
3277 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003278 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3279 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003280 /* there is no point trying this regex on headers */
3281 return 1;
3282 }
3283 }
3284 *cur_end = term; /* restore the string terminator */
3285 return done;
3286}
Willy Tarreau97de6242006-12-27 17:18:38 +01003287
Willy Tarreau58f10d72006-12-04 02:26:12 +01003288
Willy Tarreau58f10d72006-12-04 02:26:12 +01003289
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003290/*
3291 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3292 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003293 * unparsable request. Since it can manage the switch to another backend, it
3294 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003295 */
3296int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3297{
Willy Tarreau3d300592007-03-18 18:34:41 +01003298 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003299 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003300 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003301 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003302
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003303 /*
3304 * The interleaving of transformations and verdicts
3305 * makes it difficult to decide to continue or stop
3306 * the evaluation.
3307 */
3308
Willy Tarreau3d300592007-03-18 18:34:41 +01003309 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003310 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3311 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3312 exp = exp->next;
3313 continue;
3314 }
3315
3316 /* Apply the filter to the request line. */
3317 ret = apply_filter_to_req_line(t, req, exp);
3318 if (unlikely(ret < 0))
3319 return -1;
3320
3321 if (likely(ret == 0)) {
3322 /* The filter did not match the request, it can be
3323 * iterated through all headers.
3324 */
3325 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003326 }
3327 exp = exp->next;
3328 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003329 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003330}
3331
3332
Willy Tarreaua15645d2007-03-18 16:22:39 +01003333
Willy Tarreau58f10d72006-12-04 02:26:12 +01003334/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003335 * Manage client-side cookie. It can impact performance by about 2% so it is
3336 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003337 */
3338void manage_client_side_cookies(struct session *t, struct buffer *req)
3339{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003340 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003341 char *p1, *p2, *p3, *p4;
3342 char *del_colon, *del_cookie, *colon;
3343 int app_cookies;
3344
3345 appsess *asession_temp = NULL;
3346 appsess local_asession;
3347
3348 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003349 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003350
Willy Tarreau2a324282006-12-05 00:05:46 +01003351 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003352 * we start with the start line.
3353 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003354 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003355 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003356
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003357 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003358 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003359 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003360
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003361 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003362 cur_ptr = cur_next;
3363 cur_end = cur_ptr + cur_hdr->len;
3364 cur_next = cur_end + cur_hdr->cr + 1;
3365
3366 /* We have one full header between cur_ptr and cur_end, and the
3367 * next header starts at cur_next. We're only interested in
3368 * "Cookie:" headers.
3369 */
3370
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003371 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3372 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003373 old_idx = cur_idx;
3374 continue;
3375 }
3376
3377 /* Now look for cookies. Conforming to RFC2109, we have to support
3378 * attributes whose name begin with a '$', and associate them with
3379 * the right cookie, if we want to delete this cookie.
3380 * So there are 3 cases for each cookie read :
3381 * 1) it's a special attribute, beginning with a '$' : ignore it.
3382 * 2) it's a server id cookie that we *MAY* want to delete : save
3383 * some pointers on it (last semi-colon, beginning of cookie...)
3384 * 3) it's an application cookie : we *MAY* have to delete a previous
3385 * "special" cookie.
3386 * At the end of loop, if a "special" cookie remains, we may have to
3387 * remove it. If no application cookie persists in the header, we
3388 * *MUST* delete it
3389 */
3390
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003391 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003392
Willy Tarreau58f10d72006-12-04 02:26:12 +01003393 /* del_cookie == NULL => nothing to be deleted */
3394 del_colon = del_cookie = NULL;
3395 app_cookies = 0;
3396
3397 while (p1 < cur_end) {
3398 /* skip spaces and colons, but keep an eye on these ones */
3399 while (p1 < cur_end) {
3400 if (*p1 == ';' || *p1 == ',')
3401 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003402 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003403 break;
3404 p1++;
3405 }
3406
3407 if (p1 == cur_end)
3408 break;
3409
3410 /* p1 is at the beginning of the cookie name */
3411 p2 = p1;
3412 while (p2 < cur_end && *p2 != '=')
3413 p2++;
3414
3415 if (p2 == cur_end)
3416 break;
3417
3418 p3 = p2 + 1; /* skips the '=' sign */
3419 if (p3 == cur_end)
3420 break;
3421
3422 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003423 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003424 p4++;
3425
3426 /* here, we have the cookie name between p1 and p2,
3427 * and its value between p3 and p4.
3428 * we can process it :
3429 *
3430 * Cookie: NAME=VALUE;
3431 * | || || |
3432 * | || || +--> p4
3433 * | || |+-------> p3
3434 * | || +--------> p2
3435 * | |+------------> p1
3436 * | +-------------> colon
3437 * +--------------------> cur_ptr
3438 */
3439
3440 if (*p1 == '$') {
3441 /* skip this one */
3442 }
3443 else {
3444 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003445 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003446 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003447 (p4 - p1 >= t->fe->capture_namelen) &&
3448 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003449 int log_len = p4 - p1;
3450
Willy Tarreau086b3b42007-05-13 21:45:51 +02003451 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003452 Alert("HTTP logging : out of memory.\n");
3453 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003454 if (log_len > t->fe->capture_len)
3455 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003456 memcpy(txn->cli_cookie, p1, log_len);
3457 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003458 }
3459 }
3460
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003461 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3462 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003463 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003464 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003465 char *delim;
3466
3467 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3468 * have the server ID betweek p3 and delim, and the original cookie between
3469 * delim+1 and p4. Otherwise, delim==p4 :
3470 *
3471 * Cookie: NAME=SRV~VALUE;
3472 * | || || | |
3473 * | || || | +--> p4
3474 * | || || +--------> delim
3475 * | || |+-----------> p3
3476 * | || +------------> p2
3477 * | |+----------------> p1
3478 * | +-----------------> colon
3479 * +------------------------> cur_ptr
3480 */
3481
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003482 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003483 for (delim = p3; delim < p4; delim++)
3484 if (*delim == COOKIE_DELIM)
3485 break;
3486 }
3487 else
3488 delim = p4;
3489
3490
3491 /* Here, we'll look for the first running server which supports the cookie.
3492 * This allows to share a same cookie between several servers, for example
3493 * to dedicate backup servers to specific servers only.
3494 * However, to prevent clients from sticking to cookie-less backup server
3495 * when they have incidentely learned an empty cookie, we simply ignore
3496 * empty cookies and mark them as invalid.
3497 */
3498 if (delim == p3)
3499 srv = NULL;
3500
3501 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003502 if (srv->cookie && (srv->cklen == delim - p3) &&
3503 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003504 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003505 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003506 txn->flags &= ~TX_CK_MASK;
3507 txn->flags |= TX_CK_VALID;
3508 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003509 t->srv = srv;
3510 break;
3511 } else {
3512 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003513 txn->flags &= ~TX_CK_MASK;
3514 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003515 }
3516 }
3517 srv = srv->next;
3518 }
3519
Willy Tarreau3d300592007-03-18 18:34:41 +01003520 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003521 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003522 txn->flags &= ~TX_CK_MASK;
3523 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003524 }
3525
3526 /* depending on the cookie mode, we may have to either :
3527 * - delete the complete cookie if we're in insert+indirect mode, so that
3528 * the server never sees it ;
3529 * - remove the server id from the cookie value, and tag the cookie as an
3530 * application cookie so that it does not get accidentely removed later,
3531 * if we're in cookie prefix mode
3532 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003533 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003534 int delta; /* negative */
3535
3536 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3537 p4 += delta;
3538 cur_end += delta;
3539 cur_next += delta;
3540 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003541 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003542
3543 del_cookie = del_colon = NULL;
3544 app_cookies++; /* protect the header from deletion */
3545 }
3546 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003547 (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 +01003548 del_cookie = p1;
3549 del_colon = colon;
3550 }
3551 } else {
3552 /* now we know that we must keep this cookie since it's
3553 * not ours. But if we wanted to delete our cookie
3554 * earlier, we cannot remove the complete header, but we
3555 * can remove the previous block itself.
3556 */
3557 app_cookies++;
3558
3559 if (del_cookie != NULL) {
3560 int delta; /* negative */
3561
3562 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3563 p4 += delta;
3564 cur_end += delta;
3565 cur_next += delta;
3566 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003567 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003568 del_cookie = del_colon = NULL;
3569 }
3570 }
3571
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003572 if ((t->be->appsession_name != NULL) &&
3573 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003574 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003575
Willy Tarreau58f10d72006-12-04 02:26:12 +01003576 /* Cool... it's the right one */
3577
3578 asession_temp = &local_asession;
3579
Willy Tarreau63963c62007-05-13 21:29:55 +02003580 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003581 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3582 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3583 return;
3584 }
3585
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003586 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3587 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003588 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003589
Willy Tarreau58f10d72006-12-04 02:26:12 +01003590 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003591 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3592 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003593 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003594 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003595 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003596 Alert("Not enough memory process_cli():asession:calloc().\n");
3597 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3598 return;
3599 }
3600
3601 asession_temp->sessid = local_asession.sessid;
3602 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003603 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02003604 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003605 } else {
3606 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003607 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003608 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003609 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003610 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003611 Alert("Found Application Session without matching server.\n");
3612 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003613 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003614 while (srv) {
3615 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003616 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003617 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003618 txn->flags &= ~TX_CK_MASK;
3619 txn->flags |= TX_CK_VALID;
3620 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003621 t->srv = srv;
3622 break;
3623 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01003624 txn->flags &= ~TX_CK_MASK;
3625 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003626 }
3627 }
3628 srv = srv->next;
3629 }/* end while(srv) */
3630 }/* end else if server == NULL */
3631
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003632 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003633 asession_temp->request_count++;
3634#if defined(DEBUG_HASH)
3635 Alert("manage_client_side_cookies\n");
3636 appsession_hash_dump(&(t->be->htbl_proxy));
3637#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01003638 }/* end if ((t->proxy->appsession_name != NULL) ... */
3639 }
3640
3641 /* we'll have to look for another cookie ... */
3642 p1 = p4;
3643 } /* while (p1 < cur_end) */
3644
3645 /* There's no more cookie on this line.
3646 * We may have marked the last one(s) for deletion.
3647 * We must do this now in two ways :
3648 * - if there is no app cookie, we simply delete the header ;
3649 * - if there are app cookies, we must delete the end of the
3650 * string properly, including the colon/semi-colon before
3651 * the cookie name.
3652 */
3653 if (del_cookie != NULL) {
3654 int delta;
3655 if (app_cookies) {
3656 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
3657 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003658 cur_hdr->len += delta;
3659 } else {
3660 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003661
3662 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003663 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3664 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003665 cur_hdr->len = 0;
3666 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01003667 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003668 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003669 }
3670
3671 /* keep the link from this header to next one */
3672 old_idx = cur_idx;
3673 } /* end of cookie processing on this header */
3674}
3675
3676
Willy Tarreaua15645d2007-03-18 16:22:39 +01003677/* Iterate the same filter through all response headers contained in <rtr>.
3678 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3679 */
3680int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3681{
3682 char term;
3683 char *cur_ptr, *cur_end, *cur_next;
3684 int cur_idx, old_idx, last_hdr;
3685 struct http_txn *txn = &t->txn;
3686 struct hdr_idx_elem *cur_hdr;
3687 int len, delta;
3688
3689 last_hdr = 0;
3690
3691 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3692 old_idx = 0;
3693
3694 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003695 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003696 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003697 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003698 (exp->action == ACT_ALLOW ||
3699 exp->action == ACT_DENY))
3700 return 0;
3701
3702 cur_idx = txn->hdr_idx.v[old_idx].next;
3703 if (!cur_idx)
3704 break;
3705
3706 cur_hdr = &txn->hdr_idx.v[cur_idx];
3707 cur_ptr = cur_next;
3708 cur_end = cur_ptr + cur_hdr->len;
3709 cur_next = cur_end + cur_hdr->cr + 1;
3710
3711 /* Now we have one header between cur_ptr and cur_end,
3712 * and the next header starts at cur_next.
3713 */
3714
3715 /* The annoying part is that pattern matching needs
3716 * that we modify the contents to null-terminate all
3717 * strings before testing them.
3718 */
3719
3720 term = *cur_end;
3721 *cur_end = '\0';
3722
3723 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3724 switch (exp->action) {
3725 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003726 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003727 last_hdr = 1;
3728 break;
3729
3730 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003731 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003732 last_hdr = 1;
3733 break;
3734
3735 case ACT_REPLACE:
3736 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3737 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3738 /* FIXME: if the user adds a newline in the replacement, the
3739 * index will not be recalculated for now, and the new line
3740 * will not be counted as a new header.
3741 */
3742
3743 cur_end += delta;
3744 cur_next += delta;
3745 cur_hdr->len += delta;
3746 txn->rsp.eoh += delta;
3747 break;
3748
3749 case ACT_REMOVE:
3750 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3751 cur_next += delta;
3752
3753 /* FIXME: this should be a separate function */
3754 txn->rsp.eoh += delta;
3755 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3756 txn->hdr_idx.used--;
3757 cur_hdr->len = 0;
3758 cur_end = NULL; /* null-term has been rewritten */
3759 break;
3760
3761 }
3762 }
3763 if (cur_end)
3764 *cur_end = term; /* restore the string terminator */
3765
3766 /* keep the link from this header to next one in case of later
3767 * removal of next header.
3768 */
3769 old_idx = cur_idx;
3770 }
3771 return 0;
3772}
3773
3774
3775/* Apply the filter to the status line in the response buffer <rtr>.
3776 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3777 * or -1 if a replacement resulted in an invalid status line.
3778 */
3779int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3780{
3781 char term;
3782 char *cur_ptr, *cur_end;
3783 int done;
3784 struct http_txn *txn = &t->txn;
3785 int len, delta;
3786
3787
Willy Tarreau3d300592007-03-18 18:34:41 +01003788 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003789 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003790 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003791 (exp->action == ACT_ALLOW ||
3792 exp->action == ACT_DENY))
3793 return 0;
3794 else if (exp->action == ACT_REMOVE)
3795 return 0;
3796
3797 done = 0;
3798
Willy Tarreau9cdde232007-05-02 20:58:19 +02003799 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003800 cur_end = cur_ptr + txn->rsp.sl.rq.l;
3801
3802 /* Now we have the status line between cur_ptr and cur_end */
3803
3804 /* The annoying part is that pattern matching needs
3805 * that we modify the contents to null-terminate all
3806 * strings before testing them.
3807 */
3808
3809 term = *cur_end;
3810 *cur_end = '\0';
3811
3812 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3813 switch (exp->action) {
3814 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003815 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003816 done = 1;
3817 break;
3818
3819 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003820 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003821 done = 1;
3822 break;
3823
3824 case ACT_REPLACE:
3825 *cur_end = term; /* restore the string terminator */
3826 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3827 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3828 /* FIXME: if the user adds a newline in the replacement, the
3829 * index will not be recalculated for now, and the new line
3830 * will not be counted as a new header.
3831 */
3832
3833 txn->rsp.eoh += delta;
3834 cur_end += delta;
3835
Willy Tarreau9cdde232007-05-02 20:58:19 +02003836 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003837 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02003838 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003839 cur_ptr, cur_end + 1,
3840 NULL, NULL);
3841 if (unlikely(!cur_end))
3842 return -1;
3843
3844 /* we have a full respnse and we know that we have either a CR
3845 * or an LF at <ptr>.
3846 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003847 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003848 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
3849 /* there is no point trying this regex on headers */
3850 return 1;
3851 }
3852 }
3853 *cur_end = term; /* restore the string terminator */
3854 return done;
3855}
3856
3857
3858
3859/*
3860 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
3861 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3862 * unparsable response.
3863 */
3864int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3865{
Willy Tarreau3d300592007-03-18 18:34:41 +01003866 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003867 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003868 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003869 int ret;
3870
3871 /*
3872 * The interleaving of transformations and verdicts
3873 * makes it difficult to decide to continue or stop
3874 * the evaluation.
3875 */
3876
Willy Tarreau3d300592007-03-18 18:34:41 +01003877 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003878 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3879 exp->action == ACT_PASS)) {
3880 exp = exp->next;
3881 continue;
3882 }
3883
3884 /* Apply the filter to the status line. */
3885 ret = apply_filter_to_sts_line(t, rtr, exp);
3886 if (unlikely(ret < 0))
3887 return -1;
3888
3889 if (likely(ret == 0)) {
3890 /* The filter did not match the response, it can be
3891 * iterated through all headers.
3892 */
3893 apply_filter_to_resp_headers(t, rtr, exp);
3894 }
3895 exp = exp->next;
3896 }
3897 return 0;
3898}
3899
3900
3901
3902/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003903 * Manage server-side cookies. It can impact performance by about 2% so it is
3904 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003905 */
3906void manage_server_side_cookies(struct session *t, struct buffer *rtr)
3907{
3908 struct http_txn *txn = &t->txn;
3909 char *p1, *p2, *p3, *p4;
3910
3911 appsess *asession_temp = NULL;
3912 appsess local_asession;
3913
3914 char *cur_ptr, *cur_end, *cur_next;
3915 int cur_idx, old_idx, delta;
3916
Willy Tarreaua15645d2007-03-18 16:22:39 +01003917 /* Iterate through the headers.
3918 * we start with the start line.
3919 */
3920 old_idx = 0;
3921 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3922
3923 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3924 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003925 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003926
3927 cur_hdr = &txn->hdr_idx.v[cur_idx];
3928 cur_ptr = cur_next;
3929 cur_end = cur_ptr + cur_hdr->len;
3930 cur_next = cur_end + cur_hdr->cr + 1;
3931
3932 /* We have one full header between cur_ptr and cur_end, and the
3933 * next header starts at cur_next. We're only interested in
3934 * "Cookie:" headers.
3935 */
3936
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003937 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
3938 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003939 old_idx = cur_idx;
3940 continue;
3941 }
3942
3943 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01003944 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003945
3946
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003947 /* maybe we only wanted to see if there was a set-cookie. Note that
3948 * the cookie capture is declared in the fronend.
3949 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003950 if (t->be->cookie_name == NULL &&
3951 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003952 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003953 return;
3954
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003955 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003956
3957 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003958 if (p1 == cur_end || *p1 == ';') /* end of cookie */
3959 break;
3960
3961 /* p1 is at the beginning of the cookie name */
3962 p2 = p1;
3963
3964 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
3965 p2++;
3966
3967 if (p2 == cur_end || *p2 == ';') /* next cookie */
3968 break;
3969
3970 p3 = p2 + 1; /* skip the '=' sign */
3971 if (p3 == cur_end)
3972 break;
3973
3974 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003975 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01003976 p4++;
3977
3978 /* here, we have the cookie name between p1 and p2,
3979 * and its value between p3 and p4.
3980 * we can process it.
3981 */
3982
3983 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003984 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003985 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003986 (p4 - p1 >= t->fe->capture_namelen) &&
3987 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003988 int log_len = p4 - p1;
3989
Willy Tarreau086b3b42007-05-13 21:45:51 +02003990 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003991 Alert("HTTP logging : out of memory.\n");
3992 }
3993
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003994 if (log_len > t->fe->capture_len)
3995 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003996 memcpy(txn->srv_cookie, p1, log_len);
3997 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003998 }
3999
4000 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004001 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4002 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004003 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004004 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004005
4006 /* If the cookie is in insert mode on a known server, we'll delete
4007 * this occurrence because we'll insert another one later.
4008 * We'll delete it too if the "indirect" option is set and we're in
4009 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004010 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4011 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004012 /* this header must be deleted */
4013 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4014 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4015 txn->hdr_idx.used--;
4016 cur_hdr->len = 0;
4017 cur_next += delta;
4018 txn->rsp.eoh += delta;
4019
Willy Tarreau3d300592007-03-18 18:34:41 +01004020 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004021 }
4022 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004023 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004024 /* replace bytes p3->p4 with the cookie name associated
4025 * with this server since we know it.
4026 */
4027 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4028 cur_hdr->len += delta;
4029 cur_next += delta;
4030 txn->rsp.eoh += delta;
4031
Willy Tarreau3d300592007-03-18 18:34:41 +01004032 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004033 }
4034 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004035 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004036 /* insert the cookie name associated with this server
4037 * before existing cookie, and insert a delimitor between them..
4038 */
4039 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4040 cur_hdr->len += delta;
4041 cur_next += delta;
4042 txn->rsp.eoh += delta;
4043
4044 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004045 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004046 }
4047 }
4048 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004049 else if ((t->be->appsession_name != NULL) &&
4050 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004051
4052 /* Cool... it's the right one */
4053
4054 size_t server_id_len = strlen(t->srv->id) + 1;
4055 asession_temp = &local_asession;
4056
Willy Tarreau63963c62007-05-13 21:29:55 +02004057 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004058 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4059 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4060 return;
4061 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004062 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4063 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004064 asession_temp->serverid = NULL;
4065
4066 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004067 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4068 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004069 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004070 Alert("Not enough Memory process_srv():asession:calloc().\n");
4071 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4072 return;
4073 }
4074 asession_temp->sessid = local_asession.sessid;
4075 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004076 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004077 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4078 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004079 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004080 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004081 }
4082
Willy Tarreaua15645d2007-03-18 16:22:39 +01004083 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004084 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004085 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4086 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4087 return;
4088 }
4089 asession_temp->serverid[0] = '\0';
4090 }
4091
4092 if (asession_temp->serverid[0] == '\0')
4093 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4094
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004095 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004096 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004097#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004098 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004099 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004100#endif
4101 }/* end if ((t->proxy->appsession_name != NULL) ... */
4102 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4103 } /* we're now at the end of the cookie value */
4104
4105 /* keep the link from this header to next one */
4106 old_idx = cur_idx;
4107 } /* end of cookie processing on this header */
4108}
4109
4110
4111
4112/*
4113 * Check if response is cacheable or not. Updates t->flags.
4114 */
4115void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4116{
4117 struct http_txn *txn = &t->txn;
4118 char *p1, *p2;
4119
4120 char *cur_ptr, *cur_end, *cur_next;
4121 int cur_idx;
4122
Willy Tarreau5df51872007-11-25 16:20:08 +01004123 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004124 return;
4125
4126 /* Iterate through the headers.
4127 * we start with the start line.
4128 */
4129 cur_idx = 0;
4130 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4131
4132 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4133 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004134 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004135
4136 cur_hdr = &txn->hdr_idx.v[cur_idx];
4137 cur_ptr = cur_next;
4138 cur_end = cur_ptr + cur_hdr->len;
4139 cur_next = cur_end + cur_hdr->cr + 1;
4140
4141 /* We have one full header between cur_ptr and cur_end, and the
4142 * next header starts at cur_next. We're only interested in
4143 * "Cookie:" headers.
4144 */
4145
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004146 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4147 if (val) {
4148 if ((cur_end - (cur_ptr + val) >= 8) &&
4149 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4150 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4151 return;
4152 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004153 }
4154
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004155 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4156 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004157 continue;
4158
4159 /* OK, right now we know we have a cache-control header at cur_ptr */
4160
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004161 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004162
4163 if (p1 >= cur_end) /* no more info */
4164 continue;
4165
4166 /* p1 is at the beginning of the value */
4167 p2 = p1;
4168
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004169 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004170 p2++;
4171
4172 /* we have a complete value between p1 and p2 */
4173 if (p2 < cur_end && *p2 == '=') {
4174 /* we have something of the form no-cache="set-cookie" */
4175 if ((cur_end - p1 >= 21) &&
4176 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4177 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004178 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004179 continue;
4180 }
4181
4182 /* OK, so we know that either p2 points to the end of string or to a comma */
4183 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4184 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4185 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4186 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004187 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004188 return;
4189 }
4190
4191 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004192 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004193 continue;
4194 }
4195 }
4196}
4197
4198
Willy Tarreau58f10d72006-12-04 02:26:12 +01004199/*
4200 * Try to retrieve a known appsession in the URI, then the associated server.
4201 * If the server is found, it's assigned to the session.
4202 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004203void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004204{
Willy Tarreau3d300592007-03-18 18:34:41 +01004205 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004206 appsess *asession_temp = NULL;
4207 appsess local_asession;
4208 char *request_line;
4209
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004210 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004211 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004212 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004213 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004214 return;
4215
4216 /* skip ';' */
4217 request_line++;
4218
4219 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004220 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004221 return;
4222
4223 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004224 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004225
4226 /* First try if we already have an appsession */
4227 asession_temp = &local_asession;
4228
Willy Tarreau63963c62007-05-13 21:29:55 +02004229 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004230 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4231 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4232 return;
4233 }
4234
4235 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004236 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4237 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004238 asession_temp->serverid = NULL;
4239
4240 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004241 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4242 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004243 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004244 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004245 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004246 Alert("Not enough memory process_cli():asession:calloc().\n");
4247 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4248 return;
4249 }
4250 asession_temp->sessid = local_asession.sessid;
4251 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004252 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004253 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004254 }
4255 else {
4256 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004257 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004258 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004259
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004260 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004261 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004262
Willy Tarreau58f10d72006-12-04 02:26:12 +01004263#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004264 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004265 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004266#endif
4267 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004268 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004269 Alert("Found Application Session without matching server.\n");
4270 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004271 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004272 while (srv) {
4273 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004274 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004275 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004276 txn->flags &= ~TX_CK_MASK;
4277 txn->flags |= TX_CK_VALID;
4278 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004279 t->srv = srv;
4280 break;
4281 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004282 txn->flags &= ~TX_CK_MASK;
4283 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004284 }
4285 }
4286 srv = srv->next;
4287 }
4288 }
4289}
4290
4291
Willy Tarreaub2513902006-12-17 14:52:38 +01004292/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004293 * In a GET or HEAD request, check if the requested URI matches the stats uri
4294 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004295 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004296 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004297 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004298 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004299 *
4300 * Returns 1 if the session's state changes, otherwise 0.
4301 */
4302int stats_check_uri_auth(struct session *t, struct proxy *backend)
4303{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004304 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004305 struct uri_auth *uri_auth = backend->uri_auth;
4306 struct user_auth *user;
4307 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004308 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004309
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004310 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
4311
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004312 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004313 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004314 return 0;
4315
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004316 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004317
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004318 /* the URI is in h */
4319 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004320 return 0;
4321
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004322 h += uri_auth->uri_len;
4323 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4324 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004325 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004326 break;
4327 }
4328 h++;
4329 }
4330
4331 if (uri_auth->refresh) {
4332 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4333 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4334 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004335 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004336 break;
4337 }
4338 h++;
4339 }
4340 }
4341
Willy Tarreau55bb8452007-10-17 18:44:57 +02004342 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4343 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4344 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004345 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02004346 break;
4347 }
4348 h++;
4349 }
4350
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004351 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
4352
Willy Tarreaub2513902006-12-17 14:52:38 +01004353 /* we are in front of a interceptable URI. Let's check
4354 * if there's an authentication and if it's valid.
4355 */
4356 user = uri_auth->users;
4357 if (!user) {
4358 /* no user auth required, it's OK */
4359 authenticated = 1;
4360 } else {
4361 authenticated = 0;
4362
4363 /* a user list is defined, we have to check.
4364 * skip 21 chars for "Authorization: Basic ".
4365 */
4366
4367 /* FIXME: this should move to an earlier place */
4368 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004369 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4370 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4371 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004372 if (len > 14 &&
4373 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004374 txn->auth_hdr.str = h;
4375 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004376 break;
4377 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004378 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004379 }
4380
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004381 if (txn->auth_hdr.len < 21 ||
4382 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004383 user = NULL;
4384
4385 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004386 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4387 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004388 user->user_pwd, user->user_len)) {
4389 authenticated = 1;
4390 break;
4391 }
4392 user = user->next;
4393 }
4394 }
4395
4396 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004397 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004398
4399 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004400 msg.str = trash;
4401 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004402 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01004403 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02004404 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02004405 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004406 if (!(t->flags & SN_ERR_MASK))
4407 t->flags |= SN_ERR_PRXCOND;
4408 if (!(t->flags & SN_FINST_MASK))
4409 t->flags |= SN_FINST_R;
4410 return 1;
4411 }
4412
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004413 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01004414 * data.
4415 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01004416 buffer_write_dis(t->req);
Willy Tarreau72b179a2008-08-28 16:01:32 +02004417 buffer_shutw_now(t->req);
4418 buffer_shutr_now(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02004419 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01004420 t->data_source = DATA_SRC_STATS;
4421 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02004422 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau01bf8672008-12-07 18:03:29 +01004423 buffer_install_hijacker(t, t->rep, produce_content);
Willy Tarreaub2513902006-12-17 14:52:38 +01004424 return 1;
4425}
4426
4427
Willy Tarreaubaaee002006-06-26 02:48:02 +02004428/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004429 * Print a debug line with a header
4430 */
4431void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4432{
4433 int len, max;
4434 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004435 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004436 max = end - start;
4437 UBOUND(max, sizeof(trash) - len - 1);
4438 len += strlcpy2(trash + len, start, max + 1);
4439 trash[len++] = '\n';
4440 write(1, trash, len);
4441}
4442
4443
Willy Tarreau8797c062007-05-07 00:55:35 +02004444/************************************************************************/
4445/* The code below is dedicated to ACL parsing and matching */
4446/************************************************************************/
4447
4448
4449
4450
4451/* 1. Check on METHOD
4452 * We use the pre-parsed method if it is known, and store its number as an
4453 * integer. If it is unknown, we use the pointer and the length.
4454 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004455static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004456{
4457 int len, meth;
4458
Willy Tarreauae8b7962007-06-09 23:10:04 +02004459 len = strlen(*text);
4460 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004461
4462 pattern->val.i = meth;
4463 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004464 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004465 if (!pattern->ptr.str)
4466 return 0;
4467 pattern->len = len;
4468 }
4469 return 1;
4470}
4471
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004472static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004473acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4474 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004475{
4476 int meth;
4477 struct http_txn *txn = l7;
4478
Willy Tarreaub6866442008-07-14 23:54:42 +02004479 if (!txn)
4480 return 0;
4481
Willy Tarreauc11416f2007-06-17 16:58:38 +02004482 if (txn->req.msg_state != HTTP_MSG_BODY)
4483 return 0;
4484
Willy Tarreau8797c062007-05-07 00:55:35 +02004485 meth = txn->meth;
4486 test->i = meth;
4487 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004488 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4489 /* ensure the indexes are not affected */
4490 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004491 test->len = txn->req.sl.rq.m_l;
4492 test->ptr = txn->req.sol;
4493 }
4494 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4495 return 1;
4496}
4497
4498static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4499{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004500 int icase;
4501
Willy Tarreau8797c062007-05-07 00:55:35 +02004502 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02004503 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02004504
4505 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02004506 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004507
4508 /* Other method, we must compare the strings */
4509 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02004510 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004511
4512 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4513 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4514 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02004515 return ACL_PAT_FAIL;
4516 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004517}
4518
4519/* 2. Check on Request/Status Version
4520 * We simply compare strings here.
4521 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004522static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004523{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004524 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004525 if (!pattern->ptr.str)
4526 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004527 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004528 return 1;
4529}
4530
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004531static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004532acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4533 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004534{
4535 struct http_txn *txn = l7;
4536 char *ptr;
4537 int len;
4538
Willy Tarreaub6866442008-07-14 23:54:42 +02004539 if (!txn)
4540 return 0;
4541
Willy Tarreauc11416f2007-06-17 16:58:38 +02004542 if (txn->req.msg_state != HTTP_MSG_BODY)
4543 return 0;
4544
Willy Tarreau8797c062007-05-07 00:55:35 +02004545 len = txn->req.sl.rq.v_l;
4546 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4547
4548 while ((len-- > 0) && (*ptr++ != '/'));
4549 if (len <= 0)
4550 return 0;
4551
4552 test->ptr = ptr;
4553 test->len = len;
4554
4555 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4556 return 1;
4557}
4558
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004559static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004560acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4561 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004562{
4563 struct http_txn *txn = l7;
4564 char *ptr;
4565 int len;
4566
Willy Tarreaub6866442008-07-14 23:54:42 +02004567 if (!txn)
4568 return 0;
4569
Willy Tarreauc11416f2007-06-17 16:58:38 +02004570 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4571 return 0;
4572
Willy Tarreau8797c062007-05-07 00:55:35 +02004573 len = txn->rsp.sl.st.v_l;
4574 ptr = txn->rsp.sol;
4575
4576 while ((len-- > 0) && (*ptr++ != '/'));
4577 if (len <= 0)
4578 return 0;
4579
4580 test->ptr = ptr;
4581 test->len = len;
4582
4583 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4584 return 1;
4585}
4586
4587/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004588static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004589acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4590 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004591{
4592 struct http_txn *txn = l7;
4593 char *ptr;
4594 int len;
4595
Willy Tarreaub6866442008-07-14 23:54:42 +02004596 if (!txn)
4597 return 0;
4598
Willy Tarreauc11416f2007-06-17 16:58:38 +02004599 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4600 return 0;
4601
Willy Tarreau8797c062007-05-07 00:55:35 +02004602 len = txn->rsp.sl.st.c_l;
4603 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4604
4605 test->i = __strl2ui(ptr, len);
4606 test->flags = ACL_TEST_F_VOL_1ST;
4607 return 1;
4608}
4609
4610/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004611static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004612acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4613 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004614{
4615 struct http_txn *txn = l7;
4616
Willy Tarreaub6866442008-07-14 23:54:42 +02004617 if (!txn)
4618 return 0;
4619
Willy Tarreauc11416f2007-06-17 16:58:38 +02004620 if (txn->req.msg_state != HTTP_MSG_BODY)
4621 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004622
Willy Tarreauc11416f2007-06-17 16:58:38 +02004623 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4624 /* ensure the indexes are not affected */
4625 return 0;
4626
Willy Tarreau8797c062007-05-07 00:55:35 +02004627 test->len = txn->req.sl.rq.u_l;
4628 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4629
Willy Tarreauf3d25982007-05-08 22:45:09 +02004630 /* we do not need to set READ_ONLY because the data is in a buffer */
4631 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004632 return 1;
4633}
4634
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004635static int
4636acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
4637 struct acl_expr *expr, struct acl_test *test)
4638{
4639 struct http_txn *txn = l7;
4640
Willy Tarreaub6866442008-07-14 23:54:42 +02004641 if (!txn)
4642 return 0;
4643
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004644 if (txn->req.msg_state != HTTP_MSG_BODY)
4645 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004646
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004647 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4648 /* ensure the indexes are not affected */
4649 return 0;
4650
4651 /* Parse HTTP request */
4652 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4653 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
4654 test->i = AF_INET;
4655
4656 /*
4657 * If we are parsing url in frontend space, we prepare backend stage
4658 * to not parse again the same url ! optimization lazyness...
4659 */
4660 if (px->options & PR_O_HTTP_PROXY)
4661 l4->flags |= SN_ADDR_SET;
4662
4663 test->flags = ACL_TEST_F_READ_ONLY;
4664 return 1;
4665}
4666
4667static int
4668acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
4669 struct acl_expr *expr, struct acl_test *test)
4670{
4671 struct http_txn *txn = l7;
4672
Willy Tarreaub6866442008-07-14 23:54:42 +02004673 if (!txn)
4674 return 0;
4675
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004676 if (txn->req.msg_state != HTTP_MSG_BODY)
4677 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004678
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004679 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4680 /* ensure the indexes are not affected */
4681 return 0;
4682
4683 /* Same optimization as url_ip */
4684 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4685 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
4686
4687 if (px->options & PR_O_HTTP_PROXY)
4688 l4->flags |= SN_ADDR_SET;
4689
4690 test->flags = ACL_TEST_F_READ_ONLY;
4691 return 1;
4692}
4693
Willy Tarreauc11416f2007-06-17 16:58:38 +02004694/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
4695 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
4696 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02004697static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004698acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004699 struct acl_expr *expr, struct acl_test *test)
4700{
4701 struct http_txn *txn = l7;
4702 struct hdr_idx *idx = &txn->hdr_idx;
4703 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004704
Willy Tarreaub6866442008-07-14 23:54:42 +02004705 if (!txn)
4706 return 0;
4707
Willy Tarreau33a7e692007-06-10 19:45:56 +02004708 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4709 /* search for header from the beginning */
4710 ctx->idx = 0;
4711
Willy Tarreau33a7e692007-06-10 19:45:56 +02004712 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4713 test->flags |= ACL_TEST_F_FETCH_MORE;
4714 test->flags |= ACL_TEST_F_VOL_HDR;
4715 test->len = ctx->vlen;
4716 test->ptr = (char *)ctx->line + ctx->val;
4717 return 1;
4718 }
4719
4720 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4721 test->flags |= ACL_TEST_F_VOL_HDR;
4722 return 0;
4723}
4724
Willy Tarreau33a7e692007-06-10 19:45:56 +02004725static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004726acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
4727 struct acl_expr *expr, struct acl_test *test)
4728{
4729 struct http_txn *txn = l7;
4730
Willy Tarreaub6866442008-07-14 23:54:42 +02004731 if (!txn)
4732 return 0;
4733
Willy Tarreauc11416f2007-06-17 16:58:38 +02004734 if (txn->req.msg_state != HTTP_MSG_BODY)
4735 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004736
Willy Tarreauc11416f2007-06-17 16:58:38 +02004737 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4738 /* ensure the indexes are not affected */
4739 return 0;
4740
4741 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
4742}
4743
4744static int
4745acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
4746 struct acl_expr *expr, struct acl_test *test)
4747{
4748 struct http_txn *txn = l7;
4749
Willy Tarreaub6866442008-07-14 23:54:42 +02004750 if (!txn)
4751 return 0;
4752
Willy Tarreauc11416f2007-06-17 16:58:38 +02004753 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4754 return 0;
4755
4756 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
4757}
4758
4759/* 6. Check on HTTP header count. The number of occurrences is returned.
4760 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
4761 */
4762static int
4763acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004764 struct acl_expr *expr, struct acl_test *test)
4765{
4766 struct http_txn *txn = l7;
4767 struct hdr_idx *idx = &txn->hdr_idx;
4768 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004769 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02004770
Willy Tarreaub6866442008-07-14 23:54:42 +02004771 if (!txn)
4772 return 0;
4773
Willy Tarreau33a7e692007-06-10 19:45:56 +02004774 ctx.idx = 0;
4775 cnt = 0;
4776 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
4777 cnt++;
4778
4779 test->i = cnt;
4780 test->flags = ACL_TEST_F_VOL_HDR;
4781 return 1;
4782}
4783
Willy Tarreauc11416f2007-06-17 16:58:38 +02004784static int
4785acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4786 struct acl_expr *expr, struct acl_test *test)
4787{
4788 struct http_txn *txn = l7;
4789
Willy Tarreaub6866442008-07-14 23:54:42 +02004790 if (!txn)
4791 return 0;
4792
Willy Tarreauc11416f2007-06-17 16:58:38 +02004793 if (txn->req.msg_state != HTTP_MSG_BODY)
4794 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004795
Willy Tarreauc11416f2007-06-17 16:58:38 +02004796 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4797 /* ensure the indexes are not affected */
4798 return 0;
4799
4800 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
4801}
4802
4803static int
4804acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4805 struct acl_expr *expr, struct acl_test *test)
4806{
4807 struct http_txn *txn = l7;
4808
Willy Tarreaub6866442008-07-14 23:54:42 +02004809 if (!txn)
4810 return 0;
4811
Willy Tarreauc11416f2007-06-17 16:58:38 +02004812 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4813 return 0;
4814
4815 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
4816}
4817
Willy Tarreau33a7e692007-06-10 19:45:56 +02004818/* 7. Check on HTTP header's integer value. The integer value is returned.
4819 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02004820 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02004821 */
4822static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004823acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004824 struct acl_expr *expr, struct acl_test *test)
4825{
4826 struct http_txn *txn = l7;
4827 struct hdr_idx *idx = &txn->hdr_idx;
4828 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004829
Willy Tarreaub6866442008-07-14 23:54:42 +02004830 if (!txn)
4831 return 0;
4832
Willy Tarreau33a7e692007-06-10 19:45:56 +02004833 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4834 /* search for header from the beginning */
4835 ctx->idx = 0;
4836
Willy Tarreau33a7e692007-06-10 19:45:56 +02004837 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4838 test->flags |= ACL_TEST_F_FETCH_MORE;
4839 test->flags |= ACL_TEST_F_VOL_HDR;
4840 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
4841 return 1;
4842 }
4843
4844 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4845 test->flags |= ACL_TEST_F_VOL_HDR;
4846 return 0;
4847}
4848
Willy Tarreauc11416f2007-06-17 16:58:38 +02004849static int
4850acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4851 struct acl_expr *expr, struct acl_test *test)
4852{
4853 struct http_txn *txn = l7;
4854
Willy Tarreaub6866442008-07-14 23:54:42 +02004855 if (!txn)
4856 return 0;
4857
Willy Tarreauc11416f2007-06-17 16:58:38 +02004858 if (txn->req.msg_state != HTTP_MSG_BODY)
4859 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004860
Willy Tarreauc11416f2007-06-17 16:58:38 +02004861 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4862 /* ensure the indexes are not affected */
4863 return 0;
4864
4865 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
4866}
4867
4868static int
4869acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4870 struct acl_expr *expr, struct acl_test *test)
4871{
4872 struct http_txn *txn = l7;
4873
Willy Tarreaub6866442008-07-14 23:54:42 +02004874 if (!txn)
4875 return 0;
4876
Willy Tarreauc11416f2007-06-17 16:58:38 +02004877 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4878 return 0;
4879
4880 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
4881}
4882
Willy Tarreau737b0c12007-06-10 21:28:46 +02004883/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
4884 * the first '/' after the possible hostname, and ends before the possible '?'.
4885 */
4886static int
4887acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
4888 struct acl_expr *expr, struct acl_test *test)
4889{
4890 struct http_txn *txn = l7;
4891 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004892
Willy Tarreaub6866442008-07-14 23:54:42 +02004893 if (!txn)
4894 return 0;
4895
Willy Tarreauc11416f2007-06-17 16:58:38 +02004896 if (txn->req.msg_state != HTTP_MSG_BODY)
4897 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004898
Willy Tarreauc11416f2007-06-17 16:58:38 +02004899 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4900 /* ensure the indexes are not affected */
4901 return 0;
4902
Willy Tarreau21d2af32008-02-14 20:25:24 +01004903 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4904 ptr = http_get_path(txn);
4905 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02004906 return 0;
4907
4908 /* OK, we got the '/' ! */
4909 test->ptr = ptr;
4910
4911 while (ptr < end && *ptr != '?')
4912 ptr++;
4913
4914 test->len = ptr - test->ptr;
4915
4916 /* we do not need to set READ_ONLY because the data is in a buffer */
4917 test->flags = ACL_TEST_F_VOL_1ST;
4918 return 1;
4919}
4920
4921
Willy Tarreau8797c062007-05-07 00:55:35 +02004922
4923/************************************************************************/
4924/* All supported keywords must be declared here. */
4925/************************************************************************/
4926
4927/* Note: must not be declared <const> as its list will be overwritten */
4928static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004929 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
4930 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4931 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
4932 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02004933
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004934 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4935 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4936 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4937 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4938 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4939 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
4940 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4941 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
4942 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02004943
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004944 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
4945 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4946 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4947 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4948 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4949 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4950 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4951 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
4952 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
4953 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02004954
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004955 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
4956 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
4957 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
4958 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
4959 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
4960 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
4961 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
4962 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
4963 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02004964
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004965 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4966 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4967 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4968 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4969 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4970 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4971 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02004972
Willy Tarreauf3d25982007-05-08 22:45:09 +02004973 { NULL, NULL, NULL, NULL },
4974
4975#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02004976 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
4977 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
4978 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
4979 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
4980 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
4981 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
4982 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
4983
Willy Tarreau8797c062007-05-07 00:55:35 +02004984 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
4985 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
4986 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
4987 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
4988 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
4989 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
4990 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
4991 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
4992
4993 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
4994 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
4995 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
4996 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
4997 { NULL, NULL, NULL, NULL },
4998#endif
4999}};
5000
5001
5002__attribute__((constructor))
5003static void __http_protocol_init(void)
5004{
5005 acl_register_keywords(&acl_kws);
5006}
5007
5008
Willy Tarreau58f10d72006-12-04 02:26:12 +01005009/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005010 * Local variables:
5011 * c-indent-level: 8
5012 * c-basic-offset: 8
5013 * End:
5014 */