blob: 86b411ea81ed4211870c75e37dc4a648784880bd [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreau7c669d72008-06-20 15:04:11 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Willy Tarreau91861262007-10-17 17:06:05 +020044#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <proto/fd.h>
46#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010047#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020048#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/proto_http.h>
50#include <proto/queue.h>
51#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010052#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020053#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/task.h>
55
Willy Tarreau6d1a9882007-01-07 02:03:04 +010056#ifdef CONFIG_HAP_TCPSPLICE
57#include <libtcpsplice.h>
58#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
Willy Tarreau58f10d72006-12-04 02:26:12 +010060#define DEBUG_PARSE_NO_SPEEDUP
61#undef DEBUG_PARSE_NO_SPEEDUP
62
Willy Tarreau976f1ee2006-12-17 10:06:03 +010063/* This is used to perform a quick jump as an alternative to a break/continue
64 * instruction. The first argument is the label for normal operation, and the
65 * second one is the break/continue instruction in the no_speedup mode.
66 */
67
68#ifdef DEBUG_PARSE_NO_SPEEDUP
69#define QUICK_JUMP(x,y) y
70#else
71#define QUICK_JUMP(x,y) goto x
72#endif
73
Willy Tarreau1c47f852006-07-09 08:22:27 +020074/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010075const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020076 "HTTP/1.0 200 OK\r\n"
77 "Cache-Control: no-cache\r\n"
78 "Connection: close\r\n"
79 "Content-Type: text/html\r\n"
80 "\r\n"
81 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const struct chunk http_200_chunk = {
84 .str = (char *)&HTTP_200,
85 .len = sizeof(HTTP_200)-1
86};
87
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088const char *HTTP_301 =
89 "HTTP/1.0 301 Moved Permantenly\r\n"
90 "Cache-Control: no-cache\r\n"
91 "Connection: close\r\n"
92 "Location: "; /* not terminated since it will be concatenated with the URL */
93
Willy Tarreau0f772532006-12-23 20:51:41 +010094const char *HTTP_302 =
95 "HTTP/1.0 302 Found\r\n"
96 "Cache-Control: no-cache\r\n"
97 "Connection: close\r\n"
98 "Location: "; /* not terminated since it will be concatenated with the URL */
99
100/* same as 302 except that the browser MUST retry with the GET method */
101const char *HTTP_303 =
102 "HTTP/1.0 303 See Other\r\n"
103 "Cache-Control: no-cache\r\n"
104 "Connection: close\r\n"
105 "Location: "; /* not terminated since it will be concatenated with the URL */
106
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
108const char *HTTP_401_fmt =
109 "HTTP/1.0 401 Unauthorized\r\n"
110 "Cache-Control: no-cache\r\n"
111 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200112 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
114 "\r\n"
115 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
116
Willy Tarreau0f772532006-12-23 20:51:41 +0100117
118const int http_err_codes[HTTP_ERR_SIZE] = {
119 [HTTP_ERR_400] = 400,
120 [HTTP_ERR_403] = 403,
121 [HTTP_ERR_408] = 408,
122 [HTTP_ERR_500] = 500,
123 [HTTP_ERR_502] = 502,
124 [HTTP_ERR_503] = 503,
125 [HTTP_ERR_504] = 504,
126};
127
Willy Tarreau80587432006-12-24 17:47:20 +0100128static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100129 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100130 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 "Cache-Control: no-cache\r\n"
132 "Connection: close\r\n"
133 "Content-Type: text/html\r\n"
134 "\r\n"
135 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
136
137 [HTTP_ERR_403] =
138 "HTTP/1.0 403 Forbidden\r\n"
139 "Cache-Control: no-cache\r\n"
140 "Connection: close\r\n"
141 "Content-Type: text/html\r\n"
142 "\r\n"
143 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
144
145 [HTTP_ERR_408] =
146 "HTTP/1.0 408 Request Time-out\r\n"
147 "Cache-Control: no-cache\r\n"
148 "Connection: close\r\n"
149 "Content-Type: text/html\r\n"
150 "\r\n"
151 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
152
153 [HTTP_ERR_500] =
154 "HTTP/1.0 500 Server Error\r\n"
155 "Cache-Control: no-cache\r\n"
156 "Connection: close\r\n"
157 "Content-Type: text/html\r\n"
158 "\r\n"
159 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
160
161 [HTTP_ERR_502] =
162 "HTTP/1.0 502 Bad Gateway\r\n"
163 "Cache-Control: no-cache\r\n"
164 "Connection: close\r\n"
165 "Content-Type: text/html\r\n"
166 "\r\n"
167 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
168
169 [HTTP_ERR_503] =
170 "HTTP/1.0 503 Service Unavailable\r\n"
171 "Cache-Control: no-cache\r\n"
172 "Connection: close\r\n"
173 "Content-Type: text/html\r\n"
174 "\r\n"
175 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
176
177 [HTTP_ERR_504] =
178 "HTTP/1.0 504 Gateway Time-out\r\n"
179 "Cache-Control: no-cache\r\n"
180 "Connection: close\r\n"
181 "Content-Type: text/html\r\n"
182 "\r\n"
183 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
184
185};
186
Willy Tarreau80587432006-12-24 17:47:20 +0100187/* We must put the messages here since GCC cannot initialize consts depending
188 * on strlen().
189 */
190struct chunk http_err_chunks[HTTP_ERR_SIZE];
191
Willy Tarreau42250582007-04-01 01:30:43 +0200192#define FD_SETS_ARE_BITFIELDS
193#ifdef FD_SETS_ARE_BITFIELDS
194/*
195 * This map is used with all the FD_* macros to check whether a particular bit
196 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
197 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
198 * byte should be encoded. Be careful to always pass bytes from 0 to 255
199 * exclusively to the macros.
200 */
201fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
202fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
203
204#else
205#error "Check if your OS uses bitfields for fd_sets"
206#endif
207
Willy Tarreau80587432006-12-24 17:47:20 +0100208void init_proto_http()
209{
Willy Tarreau42250582007-04-01 01:30:43 +0200210 int i;
211 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100212 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200213
Willy Tarreau80587432006-12-24 17:47:20 +0100214 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
215 if (!http_err_msgs[msg]) {
216 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
217 abort();
218 }
219
220 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
221 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
222 }
Willy Tarreau42250582007-04-01 01:30:43 +0200223
224 /* initialize the log header encoding map : '{|}"#' should be encoded with
225 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
226 * URL encoding only requires '"', '#' to be encoded as well as non-
227 * printable characters above.
228 */
229 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
230 memset(url_encode_map, 0, sizeof(url_encode_map));
231 for (i = 0; i < 32; i++) {
232 FD_SET(i, hdr_encode_map);
233 FD_SET(i, url_encode_map);
234 }
235 for (i = 127; i < 256; i++) {
236 FD_SET(i, hdr_encode_map);
237 FD_SET(i, url_encode_map);
238 }
239
240 tmp = "\"#{|}";
241 while (*tmp) {
242 FD_SET(*tmp, hdr_encode_map);
243 tmp++;
244 }
245
246 tmp = "\"#";
247 while (*tmp) {
248 FD_SET(*tmp, url_encode_map);
249 tmp++;
250 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200251
252 /* memory allocations */
253 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200254 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100255}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200256
Willy Tarreau53b6c742006-12-17 13:37:46 +0100257/*
258 * We have 26 list of methods (1 per first letter), each of which can have
259 * up to 3 entries (2 valid, 1 null).
260 */
261struct http_method_desc {
262 http_meth_t meth;
263 int len;
264 const char text[8];
265};
266
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100267const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100268 ['C' - 'A'] = {
269 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
270 },
271 ['D' - 'A'] = {
272 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
273 },
274 ['G' - 'A'] = {
275 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
276 },
277 ['H' - 'A'] = {
278 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
279 },
280 ['P' - 'A'] = {
281 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
282 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
283 },
284 ['T' - 'A'] = {
285 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
286 },
287 /* rest is empty like this :
288 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
289 */
290};
291
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100292/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200293 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100294 * RFC2616 for those chars.
295 */
296
297const char http_is_spht[256] = {
298 [' '] = 1, ['\t'] = 1,
299};
300
301const char http_is_crlf[256] = {
302 ['\r'] = 1, ['\n'] = 1,
303};
304
305const char http_is_lws[256] = {
306 [' '] = 1, ['\t'] = 1,
307 ['\r'] = 1, ['\n'] = 1,
308};
309
310const char http_is_sep[256] = {
311 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
312 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
313 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
314 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
315 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
316};
317
318const char http_is_ctl[256] = {
319 [0 ... 31] = 1,
320 [127] = 1,
321};
322
323/*
324 * A token is any ASCII char that is neither a separator nor a CTL char.
325 * Do not overwrite values in assignment since gcc-2.95 will not handle
326 * them correctly. Instead, define every non-CTL char's status.
327 */
328const char http_is_token[256] = {
329 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
330 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
331 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
332 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
333 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
334 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
335 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
336 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
337 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
338 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
339 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
340 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
341 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
342 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
343 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
344 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
345 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
346 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
347 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
348 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
349 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
350 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
351 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
352 ['|'] = 1, ['}'] = 0, ['~'] = 1,
353};
354
355
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100356/*
357 * An http ver_token is any ASCII which can be found in an HTTP version,
358 * which includes 'H', 'T', 'P', '/', '.' and any digit.
359 */
360const char http_is_ver_token[256] = {
361 ['.'] = 1, ['/'] = 1,
362 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
363 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
364 ['H'] = 1, ['P'] = 1, ['T'] = 1,
365};
366
367
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368#ifdef DEBUG_FULL
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200369static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370#endif
371
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100372/*
373 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
374 * CRLF. Text length is measured first, so it cannot be NULL.
375 * The header is also automatically added to the index <hdr_idx>, and the end
376 * of headers is automatically adjusted. The number of bytes added is returned
377 * on success, otherwise <0 is returned indicating an error.
378 */
379int http_header_add_tail(struct buffer *b, struct http_msg *msg,
380 struct hdr_idx *hdr_idx, const char *text)
381{
382 int bytes, len;
383
384 len = strlen(text);
385 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
386 if (!bytes)
387 return -1;
388 msg->eoh += bytes;
389 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
390}
391
392/*
393 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
394 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
395 * the buffer is only opened and the space reserved, but nothing is copied.
396 * The header is also automatically added to the index <hdr_idx>, and the end
397 * of headers is automatically adjusted. The number of bytes added is returned
398 * on success, otherwise <0 is returned indicating an error.
399 */
400int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
401 struct hdr_idx *hdr_idx, const char *text, int len)
402{
403 int bytes;
404
405 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
406 if (!bytes)
407 return -1;
408 msg->eoh += bytes;
409 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
410}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200411
412/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100413 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
414 * If so, returns the position of the first non-space character relative to
415 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
416 * to return a pointer to the place after the first space. Returns 0 if the
417 * header name does not match. Checks are case-insensitive.
418 */
419int http_header_match2(const char *hdr, const char *end,
420 const char *name, int len)
421{
422 const char *val;
423
424 if (hdr + len >= end)
425 return 0;
426 if (hdr[len] != ':')
427 return 0;
428 if (strncasecmp(hdr, name, len) != 0)
429 return 0;
430 val = hdr + len + 1;
431 while (val < end && HTTP_IS_SPHT(*val))
432 val++;
433 if ((val >= end) && (len + 2 <= end - hdr))
434 return len + 2; /* we may replace starting from second space */
435 return val - hdr;
436}
437
Willy Tarreau33a7e692007-06-10 19:45:56 +0200438/* Find the end of the header value contained between <s> and <e>.
439 * See RFC2616, par 2.2 for more information. Note that it requires
440 * a valid header to return a valid result.
441 */
442const char *find_hdr_value_end(const char *s, const char *e)
443{
444 int quoted, qdpair;
445
446 quoted = qdpair = 0;
447 for (; s < e; s++) {
448 if (qdpair) qdpair = 0;
449 else if (quoted && *s == '\\') qdpair = 1;
450 else if (quoted && *s == '"') quoted = 0;
451 else if (*s == '"') quoted = 1;
452 else if (*s == ',') return s;
453 }
454 return s;
455}
456
457/* Find the first or next occurrence of header <name> in message buffer <sol>
458 * using headers index <idx>, and return it in the <ctx> structure. This
459 * structure holds everything necessary to use the header and find next
460 * occurrence. If its <idx> member is 0, the header is searched from the
461 * beginning. Otherwise, the next occurrence is returned. The function returns
462 * 1 when it finds a value, and 0 when there is no more.
463 */
464int http_find_header2(const char *name, int len,
465 const char *sol, struct hdr_idx *idx,
466 struct hdr_ctx *ctx)
467{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200468 const char *eol, *sov;
469 int cur_idx;
470
471 if (ctx->idx) {
472 /* We have previously returned a value, let's search
473 * another one on the same line.
474 */
475 cur_idx = ctx->idx;
476 sol = ctx->line;
477 sov = sol + ctx->val + ctx->vlen;
478 eol = sol + idx->v[cur_idx].len;
479
480 if (sov >= eol)
481 /* no more values in this header */
482 goto next_hdr;
483
484 /* values remaining for this header, skip the comma */
485 sov++;
486 while (sov < eol && http_is_lws[(unsigned char)*sov])
487 sov++;
488
489 goto return_hdr;
490 }
491
492 /* first request for this header */
493 sol += hdr_idx_first_pos(idx);
494 cur_idx = hdr_idx_first_idx(idx);
495
496 while (cur_idx) {
497 eol = sol + idx->v[cur_idx].len;
498
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200499 if (len == 0) {
500 /* No argument was passed, we want any header.
501 * To achieve this, we simply build a fake request. */
502 while (sol + len < eol && sol[len] != ':')
503 len++;
504 name = sol;
505 }
506
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 if ((len < eol - sol) &&
508 (sol[len] == ':') &&
509 (strncasecmp(sol, name, len) == 0)) {
510
511 sov = sol + len + 1;
512 while (sov < eol && http_is_lws[(unsigned char)*sov])
513 sov++;
514 return_hdr:
515 ctx->line = sol;
516 ctx->idx = cur_idx;
517 ctx->val = sov - sol;
518
519 eol = find_hdr_value_end(sov, eol);
520 ctx->vlen = eol - sov;
521 return 1;
522 }
523 next_hdr:
524 sol = eol + idx->v[cur_idx].cr + 1;
525 cur_idx = idx->v[cur_idx].next;
526 }
527 return 0;
528}
529
530int http_find_header(const char *name,
531 const char *sol, struct hdr_idx *idx,
532 struct hdr_ctx *ctx)
533{
534 return http_find_header2(name, strlen(name), sol, idx, ctx);
535}
536
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100537/* This function handles a server error at the stream interface level. The
538 * stream interface is assumed to be already in a closed state. An optional
539 * message is copied into the input buffer, and an HTTP status code stored.
540 * The error flags are set to the values in arguments. Any pending request
541 * is flushed.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200542 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100543static void http_server_error(struct session *t, struct stream_interface *si,
544 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545{
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100546 buffer_flush(si->ob);
547 buffer_flush(si->ib);
548 buffer_write_ena(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100549 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100550 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100551 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200552 }
553 if (!(t->flags & SN_ERR_MASK))
554 t->flags |= err;
555 if (!(t->flags & SN_FINST_MASK))
556 t->flags |= finst;
557}
558
Willy Tarreau80587432006-12-24 17:47:20 +0100559/* This function returns the appropriate error location for the given session
560 * and message.
561 */
562
563struct chunk *error_message(struct session *s, int msgnum)
564{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200565 if (s->be->errmsg[msgnum].str)
566 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100567 else if (s->fe->errmsg[msgnum].str)
568 return &s->fe->errmsg[msgnum];
569 else
570 return &http_err_chunks[msgnum];
571}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200572
Willy Tarreau53b6c742006-12-17 13:37:46 +0100573/*
574 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
575 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
576 */
577static http_meth_t find_http_meth(const char *str, const int len)
578{
579 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100580 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100581
582 m = ((unsigned)*str - 'A');
583
584 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100585 for (h = http_methods[m]; h->len > 0; h++) {
586 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100587 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100588 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100589 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100590 };
591 return HTTP_METH_OTHER;
592 }
593 return HTTP_METH_NONE;
594
595}
596
Willy Tarreau21d2af32008-02-14 20:25:24 +0100597/* Parse the URI from the given transaction (which is assumed to be in request
598 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
599 * It is returned otherwise.
600 */
601static char *
602http_get_path(struct http_txn *txn)
603{
604 char *ptr, *end;
605
606 ptr = txn->req.sol + txn->req.sl.rq.u;
607 end = ptr + txn->req.sl.rq.u_l;
608
609 if (ptr >= end)
610 return NULL;
611
612 /* RFC2616, par. 5.1.2 :
613 * Request-URI = "*" | absuri | abspath | authority
614 */
615
616 if (*ptr == '*')
617 return NULL;
618
619 if (isalpha((unsigned char)*ptr)) {
620 /* this is a scheme as described by RFC3986, par. 3.1 */
621 ptr++;
622 while (ptr < end &&
623 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
624 ptr++;
625 /* skip '://' */
626 if (ptr == end || *ptr++ != ':')
627 return NULL;
628 if (ptr == end || *ptr++ != '/')
629 return NULL;
630 if (ptr == end || *ptr++ != '/')
631 return NULL;
632 }
633 /* skip [user[:passwd]@]host[:[port]] */
634
635 while (ptr < end && *ptr != '/')
636 ptr++;
637
638 if (ptr == end)
639 return NULL;
640
641 /* OK, we got the '/' ! */
642 return ptr;
643}
644
Willy Tarreauefb453c2008-10-26 20:49:47 +0100645/* Returns a 302 for a redirectable request. This may only be called just after
646 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
647 * left unchanged and will follow normal proxy processing.
648 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100649void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100650{
651 struct http_txn *txn;
652 struct chunk rdr;
653 char *path;
654 int len;
655
656 /* 1: create the response header */
657 rdr.len = strlen(HTTP_302);
658 rdr.str = trash;
659 memcpy(rdr.str, HTTP_302, rdr.len);
660
661 /* 2: add the server's prefix */
662 if (rdr.len + s->srv->rdr_len > sizeof(trash))
663 return;
664
665 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
666 rdr.len += s->srv->rdr_len;
667
668 /* 3: add the request URI */
669 txn = &s->txn;
670 path = http_get_path(txn);
671 if (!path)
672 return;
673
674 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
675 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
676 return;
677
678 memcpy(rdr.str + rdr.len, path, len);
679 rdr.len += len;
680 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
681 rdr.len += 4;
682
683 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100684 si->shutr(si);
685 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100686 si->err_type = SI_ET_NONE;
687 si->err_loc = NULL;
688 si->state = SI_ST_CLO;
689
690 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100691 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100692
693 /* FIXME: we should increase a counter of redirects per server and per backend. */
694 if (s->srv)
695 s->srv->cum_sess++;
696}
697
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100698/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100699 * that the server side is closed. Note that err_type is actually a
700 * bitmask, where almost only aborts may be cumulated with other
701 * values. We consider that aborted operations are more important
702 * than timeouts or errors due to the fact that nobody else in the
703 * logs might explain incomplete retries. All others should avoid
704 * being cumulated. It should normally not be possible to have multiple
705 * aborts at once, but just in case, the first one in sequence is reported.
706 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100707void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100708{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100709 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100710
711 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100712 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
713 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100714 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100715 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
716 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100717 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100718 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
719 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100720 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100721 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
722 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100723 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100724 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
725 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100726 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100727 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
728 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100729 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100730 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
731 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100732}
733
Willy Tarreau42250582007-04-01 01:30:43 +0200734extern const char sess_term_cond[8];
735extern const char sess_fin_state[8];
736extern const char *monthname[12];
737const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
738const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
739 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
740 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200741struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200742struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100743
Willy Tarreau42250582007-04-01 01:30:43 +0200744/*
745 * send a log for the session when we have enough info about it.
746 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100747 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100748void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200749{
750 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
751 struct proxy *fe = s->fe;
752 struct proxy *be = s->be;
753 struct proxy *prx_log;
754 struct http_txn *txn = &s->txn;
755 int tolog;
756 char *uri, *h;
757 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200758 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200759 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200760 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200761 int hdr;
762
763 if (fe->logfac1 < 0 && fe->logfac2 < 0)
764 return;
765 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100766
Willy Tarreau42250582007-04-01 01:30:43 +0200767 if (s->cli_addr.ss_family == AF_INET)
768 inet_ntop(AF_INET,
769 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
770 pn, sizeof(pn));
771 else
772 inet_ntop(AF_INET6,
773 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
774 pn, sizeof(pn));
775
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200776 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200777
778 /* FIXME: let's limit ourselves to frontend logging for now. */
779 tolog = fe->to_log;
780
781 h = tmpline;
782 if (fe->to_log & LW_REQHDR &&
783 txn->req.cap &&
784 (h < tmpline + sizeof(tmpline) - 10)) {
785 *(h++) = ' ';
786 *(h++) = '{';
787 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
788 if (hdr)
789 *(h++) = '|';
790 if (txn->req.cap[hdr] != NULL)
791 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
792 '#', hdr_encode_map, txn->req.cap[hdr]);
793 }
794 *(h++) = '}';
795 }
796
797 if (fe->to_log & LW_RSPHDR &&
798 txn->rsp.cap &&
799 (h < tmpline + sizeof(tmpline) - 7)) {
800 *(h++) = ' ';
801 *(h++) = '{';
802 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
803 if (hdr)
804 *(h++) = '|';
805 if (txn->rsp.cap[hdr] != NULL)
806 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
807 '#', hdr_encode_map, txn->rsp.cap[hdr]);
808 }
809 *(h++) = '}';
810 }
811
812 if (h < tmpline + sizeof(tmpline) - 4) {
813 *(h++) = ' ';
814 *(h++) = '"';
815 uri = txn->uri ? txn->uri : "<BADREQ>";
816 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
817 '#', url_encode_map, uri);
818 *(h++) = '"';
819 }
820 *h = '\0';
821
822 svid = (tolog & LW_SVID) ?
823 (s->data_source != DATA_SRC_STATS) ?
824 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
825
Willy Tarreau70089872008-06-13 21:12:51 +0200826 t_request = -1;
827 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
828 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
829
Willy Tarreau42250582007-04-01 01:30:43 +0200830 send_log(prx_log, LOG_INFO,
831 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
832 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100833 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200834 pn,
835 (s->cli_addr.ss_family == AF_INET) ?
836 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
837 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200838 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200839 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200840 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200841 t_request,
842 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200843 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
844 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
845 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
846 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100847 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200848 txn->cli_cookie ? txn->cli_cookie : "-",
849 txn->srv_cookie ? txn->srv_cookie : "-",
850 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
851 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
852 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
853 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
854 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100855 (s->flags & SN_REDISP)?"+":"",
856 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200857 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
858
859 s->logs.logwait = 0;
860}
861
Willy Tarreau117f59e2007-03-04 18:17:17 +0100862
863/*
864 * Capture headers from message starting at <som> according to header list
865 * <cap_hdr>, and fill the <idx> structure appropriately.
866 */
867void capture_headers(char *som, struct hdr_idx *idx,
868 char **cap, struct cap_hdr *cap_hdr)
869{
870 char *eol, *sol, *col, *sov;
871 int cur_idx;
872 struct cap_hdr *h;
873 int len;
874
875 sol = som + hdr_idx_first_pos(idx);
876 cur_idx = hdr_idx_first_idx(idx);
877
878 while (cur_idx) {
879 eol = sol + idx->v[cur_idx].len;
880
881 col = sol;
882 while (col < eol && *col != ':')
883 col++;
884
885 sov = col + 1;
886 while (sov < eol && http_is_lws[(unsigned char)*sov])
887 sov++;
888
889 for (h = cap_hdr; h; h = h->next) {
890 if ((h->namelen == col - sol) &&
891 (strncasecmp(sol, h->name, h->namelen) == 0)) {
892 if (cap[h->index] == NULL)
893 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200894 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100895
896 if (cap[h->index] == NULL) {
897 Alert("HTTP capture : out of memory.\n");
898 continue;
899 }
900
901 len = eol - sov;
902 if (len > h->len)
903 len = h->len;
904
905 memcpy(cap[h->index], sov, len);
906 cap[h->index][len]=0;
907 }
908 }
909 sol = eol + idx->v[cur_idx].cr + 1;
910 cur_idx = idx->v[cur_idx].next;
911 }
912}
913
914
Willy Tarreau42250582007-04-01 01:30:43 +0200915/* either we find an LF at <ptr> or we jump to <bad>.
916 */
917#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
918
919/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
920 * otherwise to <http_msg_ood> with <state> set to <st>.
921 */
922#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
923 ptr++; \
924 if (likely(ptr < end)) \
925 goto good; \
926 else { \
927 state = (st); \
928 goto http_msg_ood; \
929 } \
930 } while (0)
931
932
Willy Tarreaubaaee002006-06-26 02:48:02 +0200933/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100934 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100935 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
936 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
937 * will give undefined results.
938 * Note that it is upon the caller's responsibility to ensure that ptr < end,
939 * and that msg->sol points to the beginning of the response.
940 * If a complete line is found (which implies that at least one CR or LF is
941 * found before <end>, the updated <ptr> is returned, otherwise NULL is
942 * returned indicating an incomplete line (which does not mean that parts have
943 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
944 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
945 * upon next call.
946 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200947 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100948 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
949 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200950 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100951 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100952const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
953 unsigned int state, const char *ptr, const char *end,
954 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100955{
Willy Tarreau8973c702007-01-21 23:58:29 +0100956 switch (state) {
957 http_msg_rpver:
958 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100959 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100960 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
961
962 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100963 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100964 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
965 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100966 state = HTTP_MSG_ERROR;
967 break;
968
Willy Tarreau8973c702007-01-21 23:58:29 +0100969 http_msg_rpver_sp:
970 case HTTP_MSG_RPVER_SP:
971 if (likely(!HTTP_IS_LWS(*ptr))) {
972 msg->sl.st.c = ptr - msg_buf;
973 goto http_msg_rpcode;
974 }
975 if (likely(HTTP_IS_SPHT(*ptr)))
976 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
977 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100978 state = HTTP_MSG_ERROR;
979 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100980
981 http_msg_rpcode:
982 case HTTP_MSG_RPCODE:
983 if (likely(!HTTP_IS_LWS(*ptr)))
984 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
985
986 if (likely(HTTP_IS_SPHT(*ptr))) {
987 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
988 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
989 }
990
991 /* so it's a CR/LF, so there is no reason phrase */
992 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
993 http_msg_rsp_reason:
994 /* FIXME: should we support HTTP responses without any reason phrase ? */
995 msg->sl.st.r = ptr - msg_buf;
996 msg->sl.st.r_l = 0;
997 goto http_msg_rpline_eol;
998
999 http_msg_rpcode_sp:
1000 case HTTP_MSG_RPCODE_SP:
1001 if (likely(!HTTP_IS_LWS(*ptr))) {
1002 msg->sl.st.r = ptr - msg_buf;
1003 goto http_msg_rpreason;
1004 }
1005 if (likely(HTTP_IS_SPHT(*ptr)))
1006 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1007 /* so it's a CR/LF, so there is no reason phrase */
1008 goto http_msg_rsp_reason;
1009
1010 http_msg_rpreason:
1011 case HTTP_MSG_RPREASON:
1012 if (likely(!HTTP_IS_CRLF(*ptr)))
1013 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1014 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1015 http_msg_rpline_eol:
1016 /* We have seen the end of line. Note that we do not
1017 * necessarily have the \n yet, but at least we know that we
1018 * have EITHER \r OR \n, otherwise the response would not be
1019 * complete. We can then record the response length and return
1020 * to the caller which will be able to register it.
1021 */
1022 msg->sl.st.l = ptr - msg->sol;
1023 return ptr;
1024
1025#ifdef DEBUG_FULL
1026 default:
1027 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1028 exit(1);
1029#endif
1030 }
1031
1032 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001033 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001034 if (ret_state)
1035 *ret_state = state;
1036 if (ret_ptr)
1037 *ret_ptr = (char *)ptr;
1038 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001039}
1040
1041
1042/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001043 * This function parses a request line between <ptr> and <end>, starting with
1044 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1045 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1046 * will give undefined results.
1047 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1048 * and that msg->sol points to the beginning of the request.
1049 * If a complete line is found (which implies that at least one CR or LF is
1050 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1051 * returned indicating an incomplete line (which does not mean that parts have
1052 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1053 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1054 * upon next call.
1055 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001056 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001057 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1058 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001059 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001060 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001061const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1062 unsigned int state, const char *ptr, const char *end,
1063 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001064{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001065 switch (state) {
1066 http_msg_rqmeth:
1067 case HTTP_MSG_RQMETH:
1068 if (likely(HTTP_IS_TOKEN(*ptr)))
1069 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001070
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001071 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001072 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1074 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001075
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001076 if (likely(HTTP_IS_CRLF(*ptr))) {
1077 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001078 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 http_msg_req09_uri:
1080 msg->sl.rq.u = ptr - msg_buf;
1081 http_msg_req09_uri_e:
1082 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1083 http_msg_req09_ver:
1084 msg->sl.rq.v = ptr - msg_buf;
1085 msg->sl.rq.v_l = 0;
1086 goto http_msg_rqline_eol;
1087 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001088 state = HTTP_MSG_ERROR;
1089 break;
1090
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 http_msg_rqmeth_sp:
1092 case HTTP_MSG_RQMETH_SP:
1093 if (likely(!HTTP_IS_LWS(*ptr))) {
1094 msg->sl.rq.u = ptr - msg_buf;
1095 goto http_msg_rquri;
1096 }
1097 if (likely(HTTP_IS_SPHT(*ptr)))
1098 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1099 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1100 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001101
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001102 http_msg_rquri:
1103 case HTTP_MSG_RQURI:
1104 if (likely(!HTTP_IS_LWS(*ptr)))
1105 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001106
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 if (likely(HTTP_IS_SPHT(*ptr))) {
1108 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1109 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1110 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001111
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001112 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1113 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001114
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001115 http_msg_rquri_sp:
1116 case HTTP_MSG_RQURI_SP:
1117 if (likely(!HTTP_IS_LWS(*ptr))) {
1118 msg->sl.rq.v = ptr - msg_buf;
1119 goto http_msg_rqver;
1120 }
1121 if (likely(HTTP_IS_SPHT(*ptr)))
1122 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1123 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1124 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001125
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001126 http_msg_rqver:
1127 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001128 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001129 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001130
1131 if (likely(HTTP_IS_CRLF(*ptr))) {
1132 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1133 http_msg_rqline_eol:
1134 /* We have seen the end of line. Note that we do not
1135 * necessarily have the \n yet, but at least we know that we
1136 * have EITHER \r OR \n, otherwise the request would not be
1137 * complete. We can then record the request length and return
1138 * to the caller which will be able to register it.
1139 */
1140 msg->sl.rq.l = ptr - msg->sol;
1141 return ptr;
1142 }
1143
1144 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001145 state = HTTP_MSG_ERROR;
1146 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001147
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001148#ifdef DEBUG_FULL
1149 default:
1150 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1151 exit(1);
1152#endif
1153 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001154
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001155 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001156 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001157 if (ret_state)
1158 *ret_state = state;
1159 if (ret_ptr)
1160 *ret_ptr = (char *)ptr;
1161 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001162}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001163
1164
Willy Tarreau8973c702007-01-21 23:58:29 +01001165/*
1166 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001167 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 * when data are missing and recalled at the exact same location with no
1169 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001170 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1171 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001172 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001173void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1174{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001175 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001176 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001177
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001178 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001179 ptr = buf->lr;
1180 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001181
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001182 if (unlikely(ptr >= end))
1183 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001184
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001185 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001186 /*
1187 * First, states that are specific to the response only.
1188 * We check them first so that request and headers are
1189 * closer to each other (accessed more often).
1190 */
1191 http_msg_rpbefore:
1192 case HTTP_MSG_RPBEFORE:
1193 if (likely(HTTP_IS_TOKEN(*ptr))) {
1194 if (likely(ptr == buf->data)) {
1195 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001196 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001197 } else {
1198#if PARSE_PRESERVE_EMPTY_LINES
1199 /* only skip empty leading lines, don't remove them */
1200 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001201 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001202#else
1203 /* Remove empty leading lines, as recommended by
1204 * RFC2616. This takes a lot of time because we
1205 * must move all the buffer backwards, but this
1206 * is rarely needed. The method above will be
1207 * cleaner when we'll be able to start sending
1208 * the request from any place in the buffer.
1209 */
1210 buf->lr = ptr;
1211 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001212 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001213 msg->sol = buf->data;
1214 ptr = buf->data;
1215 end = buf->r;
1216#endif
1217 }
1218 hdr_idx_init(idx);
1219 state = HTTP_MSG_RPVER;
1220 goto http_msg_rpver;
1221 }
1222
1223 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1224 goto http_msg_invalid;
1225
1226 if (unlikely(*ptr == '\n'))
1227 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1228 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1229 /* stop here */
1230
1231 http_msg_rpbefore_cr:
1232 case HTTP_MSG_RPBEFORE_CR:
1233 EXPECT_LF_HERE(ptr, http_msg_invalid);
1234 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1235 /* stop here */
1236
1237 http_msg_rpver:
1238 case HTTP_MSG_RPVER:
1239 case HTTP_MSG_RPVER_SP:
1240 case HTTP_MSG_RPCODE:
1241 case HTTP_MSG_RPCODE_SP:
1242 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001243 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001244 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001245 if (unlikely(!ptr))
1246 return;
1247
1248 /* we have a full response and we know that we have either a CR
1249 * or an LF at <ptr>.
1250 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001251 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001252 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1253
1254 msg->sol = ptr;
1255 if (likely(*ptr == '\r'))
1256 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1257 goto http_msg_rpline_end;
1258
1259 http_msg_rpline_end:
1260 case HTTP_MSG_RPLINE_END:
1261 /* msg->sol must point to the first of CR or LF. */
1262 EXPECT_LF_HERE(ptr, http_msg_invalid);
1263 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1264 /* stop here */
1265
1266 /*
1267 * Second, states that are specific to the request only
1268 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001269 http_msg_rqbefore:
1270 case HTTP_MSG_RQBEFORE:
1271 if (likely(HTTP_IS_TOKEN(*ptr))) {
1272 if (likely(ptr == buf->data)) {
1273 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001274 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 } else {
1276#if PARSE_PRESERVE_EMPTY_LINES
1277 /* only skip empty leading lines, don't remove them */
1278 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001279 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001280#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281 /* Remove empty leading lines, as recommended by
1282 * RFC2616. This takes a lot of time because we
1283 * must move all the buffer backwards, but this
1284 * is rarely needed. The method above will be
1285 * cleaner when we'll be able to start sending
1286 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001287 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001288 buf->lr = ptr;
1289 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001290 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001291 msg->sol = buf->data;
1292 ptr = buf->data;
1293 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001294#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001296 /* we will need this when keep-alive will be supported
1297 hdr_idx_init(idx);
1298 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001299 state = HTTP_MSG_RQMETH;
1300 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001301 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001302
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1304 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 if (unlikely(*ptr == '\n'))
1307 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1308 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 http_msg_rqbefore_cr:
1312 case HTTP_MSG_RQBEFORE_CR:
1313 EXPECT_LF_HERE(ptr, http_msg_invalid);
1314 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001315 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 http_msg_rqmeth:
1318 case HTTP_MSG_RQMETH:
1319 case HTTP_MSG_RQMETH_SP:
1320 case HTTP_MSG_RQURI:
1321 case HTTP_MSG_RQURI_SP:
1322 case HTTP_MSG_RQVER:
1323 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001324 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001325 if (unlikely(!ptr))
1326 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 /* we have a full request and we know that we have either a CR
1329 * or an LF at <ptr>.
1330 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001331 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001332 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001333
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001334 msg->sol = ptr;
1335 if (likely(*ptr == '\r'))
1336 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001338
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 http_msg_rqline_end:
1340 case HTTP_MSG_RQLINE_END:
1341 /* check for HTTP/0.9 request : no version information available.
1342 * msg->sol must point to the first of CR or LF.
1343 */
1344 if (unlikely(msg->sl.rq.v_l == 0))
1345 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 EXPECT_LF_HERE(ptr, http_msg_invalid);
1348 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001350
Willy Tarreau8973c702007-01-21 23:58:29 +01001351 /*
1352 * Common states below
1353 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354 http_msg_hdr_first:
1355 case HTTP_MSG_HDR_FIRST:
1356 msg->sol = ptr;
1357 if (likely(!HTTP_IS_CRLF(*ptr))) {
1358 goto http_msg_hdr_name;
1359 }
1360
1361 if (likely(*ptr == '\r'))
1362 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1363 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001364
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 http_msg_hdr_name:
1366 case HTTP_MSG_HDR_NAME:
1367 /* assumes msg->sol points to the first char */
1368 if (likely(HTTP_IS_TOKEN(*ptr)))
1369 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001370
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 if (likely(*ptr == ':')) {
1372 msg->col = ptr - buf->data;
1373 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1374 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001375
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001377
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001378 http_msg_hdr_l1_sp:
1379 case HTTP_MSG_HDR_L1_SP:
1380 /* assumes msg->sol points to the first char and msg->col to the colon */
1381 if (likely(HTTP_IS_SPHT(*ptr)))
1382 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001383
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 /* header value can be basically anything except CR/LF */
1385 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 if (likely(!HTTP_IS_CRLF(*ptr))) {
1388 goto http_msg_hdr_val;
1389 }
1390
1391 if (likely(*ptr == '\r'))
1392 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1393 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 http_msg_hdr_l1_lf:
1396 case HTTP_MSG_HDR_L1_LF:
1397 EXPECT_LF_HERE(ptr, http_msg_invalid);
1398 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001399
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 http_msg_hdr_l1_lws:
1401 case HTTP_MSG_HDR_L1_LWS:
1402 if (likely(HTTP_IS_SPHT(*ptr))) {
1403 /* replace HT,CR,LF with spaces */
1404 for (; buf->data+msg->sov < ptr; msg->sov++)
1405 buf->data[msg->sov] = ' ';
1406 goto http_msg_hdr_l1_sp;
1407 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001408 /* we had a header consisting only in spaces ! */
1409 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 goto http_msg_complete_header;
1411
1412 http_msg_hdr_val:
1413 case HTTP_MSG_HDR_VAL:
1414 /* assumes msg->sol points to the first char, msg->col to the
1415 * colon, and msg->sov points to the first character of the
1416 * value.
1417 */
1418 if (likely(!HTTP_IS_CRLF(*ptr)))
1419 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001420
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 msg->eol = ptr;
1422 /* Note: we could also copy eol into ->eoh so that we have the
1423 * real header end in case it ends with lots of LWS, but is this
1424 * really needed ?
1425 */
1426 if (likely(*ptr == '\r'))
1427 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1428 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 http_msg_hdr_l2_lf:
1431 case HTTP_MSG_HDR_L2_LF:
1432 EXPECT_LF_HERE(ptr, http_msg_invalid);
1433 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001434
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 http_msg_hdr_l2_lws:
1436 case HTTP_MSG_HDR_L2_LWS:
1437 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1438 /* LWS: replace HT,CR,LF with spaces */
1439 for (; msg->eol < ptr; msg->eol++)
1440 *msg->eol = ' ';
1441 goto http_msg_hdr_val;
1442 }
1443 http_msg_complete_header:
1444 /*
1445 * It was a new header, so the last one is finished.
1446 * Assumes msg->sol points to the first char, msg->col to the
1447 * colon, msg->sov points to the first character of the value
1448 * and msg->eol to the first CR or LF so we know how the line
1449 * ends. We insert last header into the index.
1450 */
1451 /*
1452 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1453 write(2, msg->sol, msg->eol-msg->sol);
1454 fprintf(stderr,"\n");
1455 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001456
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1458 idx, idx->tail) < 0))
1459 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 msg->sol = ptr;
1462 if (likely(!HTTP_IS_CRLF(*ptr))) {
1463 goto http_msg_hdr_name;
1464 }
1465
1466 if (likely(*ptr == '\r'))
1467 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1468 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001469
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 http_msg_last_lf:
1471 case HTTP_MSG_LAST_LF:
1472 /* Assumes msg->sol points to the first of either CR or LF */
1473 EXPECT_LF_HERE(ptr, http_msg_invalid);
1474 ptr++;
1475 buf->lr = ptr;
1476 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001477 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 return;
1479#ifdef DEBUG_FULL
1480 default:
1481 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1482 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001483#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 }
1485 http_msg_ood:
1486 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001487 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 buf->lr = ptr;
1489 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 http_msg_invalid:
1492 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001493 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001494 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495 return;
1496}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001497
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001498/* This function performs all the processing enabled for the current request.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001499 * It returns 1 if the processing can continue on next analysers, or zero if it
1500 * needs more data, encounters an error, or wants to immediately abort the
1501 * request. It relies on buffers flags, and updates s->req->analysers. Its
1502 * behaviour is rather simple:
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001503 * - all enabled analysers are called in turn from the lower to the higher
1504 * bit.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001505 * - the analyser must check for errors and timeouts, and react as expected.
1506 * It does not have to close anything upon error, the caller will.
1507 * - if the analyser does not have enough data, it must return 0without calling
1508 * other ones. It should also probably do a buffer_write_dis() to ensure
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001509 * that unprocessed data will not be forwarded. But that probably depends on
Willy Tarreauedcf6682008-11-30 23:15:34 +01001510 * the protocol.
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001511 * - if an analyser has enough data, it just has to pass on to the next
Willy Tarreauedcf6682008-11-30 23:15:34 +01001512 * analyser without using buffer_write_dis() (enabled by default).
Willy Tarreau41f40ed2008-08-21 10:05:00 +02001513 * - if an analyser thinks it has no added value anymore staying here, it must
1514 * reset its bit from the analysers flags in order not to be called anymore.
1515 *
1516 * In the future, analysers should be able to indicate that they want to be
1517 * called after XXX bytes have been received (or transfered), and the min of
1518 * all's wishes will be used to ring back (unless a special condition occurs).
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001520int http_process_request(struct session *s, struct buffer *req)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521{
Willy Tarreau7552c032009-03-01 11:10:40 +01001522
1523 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1524 now_ms, __FUNCTION__,
1525 s,
1526 req,
1527 req->rex, req->wex,
1528 req->flags,
1529 req->l,
1530 req->analysers);
1531
Willy Tarreau59234e92008-11-30 23:51:27 +01001532 /*
1533 * We will parse the partial (or complete) lines.
1534 * We will check the request syntax, and also join multi-line
1535 * headers. An index of all the lines will be elaborated while
1536 * parsing.
1537 *
1538 * For the parsing, we use a 28 states FSM.
1539 *
1540 * Here is the information we currently have :
1541 * req->data + req->som = beginning of request
1542 * req->data + req->eoh = end of processed headers / start of current one
1543 * req->data + req->eol = end of current header or line (LF or CRLF)
1544 * req->lr = first non-visited byte
1545 * req->r = end of data
1546 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001547
Willy Tarreau59234e92008-11-30 23:51:27 +01001548 int cur_idx;
1549 struct http_txn *txn = &s->txn;
1550 struct http_msg *msg = &txn->req;
1551 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001552
Willy Tarreau59234e92008-11-30 23:51:27 +01001553 if (likely(req->lr < req->r))
1554 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001555
Willy Tarreau59234e92008-11-30 23:51:27 +01001556 /* 1: we might have to print this header in debug mode */
1557 if (unlikely((global.mode & MODE_DEBUG) &&
1558 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
1559 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
1560 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001561
Willy Tarreau59234e92008-11-30 23:51:27 +01001562 sol = req->data + msg->som;
1563 eol = sol + msg->sl.rq.l;
1564 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001565
Willy Tarreau59234e92008-11-30 23:51:27 +01001566 sol += hdr_idx_first_pos(&txn->hdr_idx);
1567 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001568
Willy Tarreau59234e92008-11-30 23:51:27 +01001569 while (cur_idx) {
1570 eol = sol + txn->hdr_idx.v[cur_idx].len;
1571 debug_hdr("clihdr", s, sol, eol);
1572 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1573 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001575 }
1576
Willy Tarreau58f10d72006-12-04 02:26:12 +01001577
Willy Tarreau59234e92008-11-30 23:51:27 +01001578 /*
1579 * Now we quickly check if we have found a full valid request.
1580 * If not so, we check the FD and buffer states before leaving.
1581 * A full request is indicated by the fact that we have seen
1582 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1583 * requests are checked first.
1584 *
1585 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001586
Willy Tarreau59234e92008-11-30 23:51:27 +01001587 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001588 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001589 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001590 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001591 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
1592 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001593
Willy Tarreau59234e92008-11-30 23:51:27 +01001594 /* 1: Since we are in header mode, if there's no space
1595 * left for headers, we won't be able to free more
1596 * later, so the session will never terminate. We
1597 * must terminate it now.
1598 */
1599 if (unlikely(req->flags & BF_FULL)) {
1600 /* FIXME: check if URI is set and return Status
1601 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001602 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001603 goto return_bad_req;
1604 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001605
Willy Tarreau59234e92008-11-30 23:51:27 +01001606 /* 2: have we encountered a read error ? */
1607 else if (req->flags & BF_READ_ERROR) {
1608 /* we cannot return any message on error */
1609 msg->msg_state = HTTP_MSG_ERROR;
1610 req->analysers = 0;
1611 s->fe->failed_req++;
1612 if (!(s->flags & SN_ERR_MASK))
1613 s->flags |= SN_ERR_CLICL;
1614 if (!(s->flags & SN_FINST_MASK))
1615 s->flags |= SN_FINST_R;
1616 return 0;
1617 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001618
Willy Tarreau59234e92008-11-30 23:51:27 +01001619 /* 3: has the read timeout expired ? */
1620 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
1621 /* read timeout : give up with an error message. */
1622 txn->status = 408;
1623 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
1624 msg->msg_state = HTTP_MSG_ERROR;
1625 req->analysers = 0;
1626 s->fe->failed_req++;
1627 if (!(s->flags & SN_ERR_MASK))
1628 s->flags |= SN_ERR_CLITO;
1629 if (!(s->flags & SN_FINST_MASK))
1630 s->flags |= SN_FINST_R;
1631 return 0;
1632 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001633
Willy Tarreau59234e92008-11-30 23:51:27 +01001634 /* 4: have we encountered a close ? */
1635 else if (req->flags & BF_SHUTR) {
1636 txn->status = 400;
1637 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1638 msg->msg_state = HTTP_MSG_ERROR;
1639 req->analysers = 0;
1640 s->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001641
Willy Tarreau59234e92008-11-30 23:51:27 +01001642 if (!(s->flags & SN_ERR_MASK))
1643 s->flags |= SN_ERR_CLICL;
1644 if (!(s->flags & SN_FINST_MASK))
1645 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001646 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001647 }
1648
Willy Tarreau59234e92008-11-30 23:51:27 +01001649 buffer_write_dis(req);
1650 /* just set the request timeout once at the beginning of the request */
1651 if (!tick_isset(req->analyse_exp))
1652 req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001653
Willy Tarreau59234e92008-11-30 23:51:27 +01001654 /* we're not ready yet */
1655 return 0;
1656 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001657
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001658
Willy Tarreau59234e92008-11-30 23:51:27 +01001659 /****************************************************************
1660 * More interesting part now : we know that we have a complete *
1661 * request which at least looks like HTTP. We have an indicator *
1662 * of each header's length, so we can parse them quickly. *
1663 ****************************************************************/
Willy Tarreau9cdde232007-05-02 20:58:19 +02001664
Willy Tarreau59234e92008-11-30 23:51:27 +01001665 req->analysers &= ~AN_REQ_HTTP_HDR;
1666 req->analyse_exp = TICK_ETERNITY;
1667
1668 /* ensure we keep this pointer to the beginning of the message */
1669 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001670
Willy Tarreau59234e92008-11-30 23:51:27 +01001671 /*
1672 * 1: identify the method
1673 */
1674 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
1675
1676 /* we can make use of server redirect on GET and HEAD */
1677 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1678 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001679
Willy Tarreau59234e92008-11-30 23:51:27 +01001680 /*
1681 * 2: check if the URI matches the monitor_uri.
1682 * We have to do this for every request which gets in, because
1683 * the monitor-uri is defined by the frontend.
1684 */
1685 if (unlikely((s->fe->monitor_uri_len != 0) &&
1686 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1687 !memcmp(&req->data[msg->sl.rq.u],
1688 s->fe->monitor_uri,
1689 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001690 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001691 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001692 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001693 struct acl_cond *cond;
1694 cur_proxy = s->fe;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001695
Willy Tarreau59234e92008-11-30 23:51:27 +01001696 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001697
Willy Tarreau59234e92008-11-30 23:51:27 +01001698 /* Check if we want to fail this monitor request or not */
1699 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1700 int ret = acl_exec_cond(cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001701
Willy Tarreau59234e92008-11-30 23:51:27 +01001702 ret = acl_pass(ret);
1703 if (cond->pol == ACL_COND_UNLESS)
1704 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001705
Willy Tarreau59234e92008-11-30 23:51:27 +01001706 if (ret) {
1707 /* we fail this request, let's return 503 service unavail */
1708 txn->status = 503;
1709 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
1710 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001711 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001712 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001713
Willy Tarreau59234e92008-11-30 23:51:27 +01001714 /* nothing to fail, let's reply normaly */
1715 txn->status = 200;
1716 stream_int_retnclose(req->prod, &http_200_chunk);
1717 goto return_prx_cond;
1718 }
1719
1720 /*
1721 * 3: Maybe we have to copy the original REQURI for the logs ?
1722 * Note: we cannot log anymore if the request has been
1723 * classified as invalid.
1724 */
1725 if (unlikely(s->logs.logwait & LW_REQ)) {
1726 /* we have a complete HTTP request that we must log */
1727 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1728 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729
Willy Tarreau59234e92008-11-30 23:51:27 +01001730 if (urilen >= REQURI_LEN)
1731 urilen = REQURI_LEN - 1;
1732 memcpy(txn->uri, &req->data[msg->som], urilen);
1733 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734
Willy Tarreau59234e92008-11-30 23:51:27 +01001735 if (!(s->logs.logwait &= ~LW_REQ))
1736 s->do_log(s);
1737 } else {
1738 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001740 }
Willy Tarreau06619262006-12-17 08:37:22 +01001741
Willy Tarreau59234e92008-11-30 23:51:27 +01001742 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1743 if (unlikely(msg->sl.rq.v_l == 0)) {
1744 int delta;
1745 char *cur_end;
1746 msg->sol = req->data + msg->som;
1747 cur_end = msg->sol + msg->sl.rq.l;
1748 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001749
Willy Tarreau59234e92008-11-30 23:51:27 +01001750 if (msg->sl.rq.u_l == 0) {
1751 /* if no URI was set, add "/" */
1752 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 cur_end += delta;
Willy Tarreau59234e92008-11-30 23:51:27 +01001754 msg->eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001755 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001756 /* add HTTP version */
1757 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1758 msg->eoh += delta;
1759 cur_end += delta;
1760 cur_end = (char *)http_parse_reqline(msg, req->data,
1761 HTTP_MSG_RQMETH,
1762 msg->sol, cur_end + 1,
1763 NULL, NULL);
1764 if (unlikely(!cur_end))
1765 goto return_bad_req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001766
Willy Tarreau59234e92008-11-30 23:51:27 +01001767 /* we have a full HTTP/1.0 request now and we know that
1768 * we have either a CR or an LF at <ptr>.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001769 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001770 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1771 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001772
1773
Willy Tarreau59234e92008-11-30 23:51:27 +01001774 /* 5: we may need to capture headers */
1775 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
1776 capture_headers(req->data + msg->som, &txn->hdr_idx,
1777 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001778
Willy Tarreau59234e92008-11-30 23:51:27 +01001779 /*
1780 * 6: we will have to evaluate the filters.
1781 * As opposed to version 1.2, now they will be evaluated in the
1782 * filters order and not in the header order. This means that
1783 * each filter has to be validated among all headers.
1784 *
1785 * We can now check whether we want to switch to another
1786 * backend, in which case we will re-check the backend's
1787 * filters and various options. In order to support 3-level
1788 * switching, here's how we should proceed :
1789 *
1790 * a) run be.
1791 * if (switch) then switch ->be to the new backend.
1792 * b) run be if (be != fe).
1793 * There cannot be any switch from there, so ->be cannot be
1794 * changed anymore.
1795 *
1796 * => filters always apply to ->be, then ->be may change.
1797 *
1798 * The response path will be able to apply either ->be, or
1799 * ->be then ->fe filters in order to match the reverse of
1800 * the forward sequence.
1801 */
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001802
Willy Tarreau59234e92008-11-30 23:51:27 +01001803 do {
1804 struct acl_cond *cond;
1805 struct redirect_rule *rule;
1806 struct proxy *rule_set = s->be;
1807 cur_proxy = s->be;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001808
Willy Tarreau59234e92008-11-30 23:51:27 +01001809 /* first check whether we have some ACLs set to redirect this request */
1810 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
1811 int ret = acl_exec_cond(rule->cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001812
Willy Tarreau59234e92008-11-30 23:51:27 +01001813 ret = acl_pass(ret);
1814 if (rule->cond->pol == ACL_COND_UNLESS)
1815 ret = !ret;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001816
Willy Tarreau59234e92008-11-30 23:51:27 +01001817 if (ret) {
1818 struct chunk rdr = { trash, 0 };
1819 const char *msg_fmt;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001820
Willy Tarreau59234e92008-11-30 23:51:27 +01001821 /* build redirect message */
1822 switch(rule->code) {
1823 case 303:
1824 rdr.len = strlen(HTTP_303);
1825 msg_fmt = HTTP_303;
1826 break;
1827 case 301:
1828 rdr.len = strlen(HTTP_301);
1829 msg_fmt = HTTP_301;
1830 break;
1831 case 302:
1832 default:
1833 rdr.len = strlen(HTTP_302);
1834 msg_fmt = HTTP_302;
1835 break;
1836 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001837
Willy Tarreau59234e92008-11-30 23:51:27 +01001838 if (unlikely(rdr.len > sizeof(trash)))
1839 goto return_bad_req;
1840 memcpy(rdr.str, msg_fmt, rdr.len);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001841
Willy Tarreau59234e92008-11-30 23:51:27 +01001842 switch(rule->type) {
1843 case REDIRECT_TYPE_PREFIX: {
1844 const char *path;
1845 int pathlen;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001846
Willy Tarreau59234e92008-11-30 23:51:27 +01001847 path = http_get_path(txn);
1848 /* build message using path */
1849 if (path) {
1850 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
Willy Tarreau79da4692008-11-19 20:03:04 +01001851 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
1852 int qs = 0;
1853 while (qs < pathlen) {
1854 if (path[qs] == '?') {
1855 pathlen = qs;
1856 break;
1857 }
1858 qs++;
1859 }
1860 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001861 } else {
1862 path = "/";
1863 pathlen = 1;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001864 }
1865
Willy Tarreau59234e92008-11-30 23:51:27 +01001866 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
1867 goto return_bad_req;
1868
Willy Tarreaufe651a52008-11-19 21:15:17 +01001869 /* add prefix. Note that if prefix == "/", we don't want to
1870 * add anything, otherwise it makes it hard for the user to
1871 * configure a self-redirection.
1872 */
1873 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
1874 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1875 rdr.len += rule->rdr_len;
1876 }
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001877
Willy Tarreau59234e92008-11-30 23:51:27 +01001878 /* add path */
1879 memcpy(rdr.str + rdr.len, path, pathlen);
1880 rdr.len += pathlen;
1881 break;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001882 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001883 case REDIRECT_TYPE_LOCATION:
1884 default:
1885 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
1886 goto return_bad_req;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001887
Willy Tarreau59234e92008-11-30 23:51:27 +01001888 /* add location */
1889 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1890 rdr.len += rule->rdr_len;
1891 break;
1892 }
Willy Tarreau11382812008-07-09 16:18:21 +02001893
Willy Tarreau0140f252008-11-19 21:07:09 +01001894 if (rule->cookie_len) {
1895 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
1896 rdr.len += 14;
1897 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
1898 rdr.len += rule->cookie_len;
1899 memcpy(rdr.str + rdr.len, "\r\n", 2);
1900 rdr.len += 2;
1901 }
1902
Willy Tarreau59234e92008-11-30 23:51:27 +01001903 /* add end of headers */
1904 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
1905 rdr.len += 4;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001906
Willy Tarreau59234e92008-11-30 23:51:27 +01001907 txn->status = rule->code;
1908 /* let's log the request time */
1909 s->logs.tv_request = now;
1910 stream_int_retnclose(req->prod, &rdr);
1911 goto return_prx_cond;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001912 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001913 }
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001914
Willy Tarreau59234e92008-11-30 23:51:27 +01001915 /* first check whether we have some ACLs set to block this request */
1916 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
1917 int ret = acl_exec_cond(cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau53b6c742006-12-17 13:37:46 +01001918
Willy Tarreau59234e92008-11-30 23:51:27 +01001919 ret = acl_pass(ret);
1920 if (cond->pol == ACL_COND_UNLESS)
1921 ret = !ret;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01001922
Willy Tarreau59234e92008-11-30 23:51:27 +01001923 if (ret) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001924 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01001925 /* let's log the request time */
Willy Tarreau59234e92008-11-30 23:51:27 +01001926 s->logs.tv_request = now;
1927 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01001928 goto return_prx_cond;
1929 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001930 }
1931
1932 /* try headers filters */
1933 if (rule_set->req_exp != NULL) {
1934 if (apply_filters_to_request(s, req, rule_set->req_exp) < 0)
1935 goto return_bad_req;
1936 }
1937
1938 if (!(s->flags & SN_BE_ASSIGNED) && (s->be != cur_proxy)) {
1939 /* to ensure correct connection accounting on
1940 * the backend, we count the connection for the
1941 * one managing the queue.
1942 */
1943 s->be->beconn++;
1944 if (s->be->beconn > s->be->beconn_max)
1945 s->be->beconn_max = s->be->beconn;
1946 s->be->cum_beconn++;
1947 s->flags |= SN_BE_ASSIGNED;
1948 }
Willy Tarreau06619262006-12-17 08:37:22 +01001949
Willy Tarreau59234e92008-11-30 23:51:27 +01001950 /* has the request been denied ? */
1951 if (txn->flags & TX_CLDENY) {
1952 /* no need to go further */
1953 txn->status = 403;
1954 /* let's log the request time */
1955 s->logs.tv_request = now;
1956 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
1957 goto return_prx_cond;
1958 }
Willy Tarreau06619262006-12-17 08:37:22 +01001959
Willy Tarreau59234e92008-11-30 23:51:27 +01001960 /* We might have to check for "Connection:" */
1961 if (((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
1962 !(s->flags & SN_CONN_CLOSED)) {
1963 char *cur_ptr, *cur_end, *cur_next;
1964 int cur_idx, old_idx, delta, val;
1965 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001966
Willy Tarreau59234e92008-11-30 23:51:27 +01001967 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
1968 old_idx = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001969
Willy Tarreau59234e92008-11-30 23:51:27 +01001970 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1971 cur_hdr = &txn->hdr_idx.v[cur_idx];
1972 cur_ptr = cur_next;
1973 cur_end = cur_ptr + cur_hdr->len;
1974 cur_next = cur_end + cur_hdr->cr + 1;
1975
1976 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1977 if (val) {
1978 /* 3 possibilities :
1979 * - we have already set Connection: close,
1980 * so we remove this line.
1981 * - we have not yet set Connection: close,
1982 * but this line indicates close. We leave
1983 * it untouched and set the flag.
1984 * - we have not yet set Connection: close,
1985 * and this line indicates non-close. We
1986 * replace it.
1987 */
1988 if (s->flags & SN_CONN_CLOSED) {
1989 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
1990 txn->req.eoh += delta;
1991 cur_next += delta;
1992 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1993 txn->hdr_idx.used--;
1994 cur_hdr->len = 0;
1995 } else {
1996 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1997 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1998 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001999 cur_next += delta;
Willy Tarreau59234e92008-11-30 23:51:27 +01002000 cur_hdr->len += delta;
2001 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002002 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002003 s->flags |= SN_CONN_CLOSED;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002004 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002005 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002006 old_idx = cur_idx;
Willy Tarreau06619262006-12-17 08:37:22 +01002007 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002008 }
2009 /* add request headers from the rule sets in the same order */
2010 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2011 if (unlikely(http_header_add_tail(req,
2012 &txn->req,
2013 &txn->hdr_idx,
2014 rule_set->req_add[cur_idx])) < 0)
2015 goto return_bad_req;
2016 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002017
Willy Tarreau59234e92008-11-30 23:51:27 +01002018 /* check if stats URI was requested, and if an auth is needed */
2019 if (rule_set->uri_auth != NULL &&
2020 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2021 /* we have to check the URI and auth for this request.
2022 * FIXME!!! that one is rather dangerous, we want to
2023 * make it follow standard rules (eg: clear req->analysers).
2024 */
2025 if (stats_check_uri_auth(s, rule_set)) {
2026 req->analysers = 0;
2027 return 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01002028 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002029 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002030
Willy Tarreau59234e92008-11-30 23:51:27 +01002031 /* now check whether we have some switching rules for this request */
2032 if (!(s->flags & SN_BE_ASSIGNED)) {
2033 struct switching_rule *rule;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002034
Willy Tarreau59234e92008-11-30 23:51:27 +01002035 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2036 int ret;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002037
Willy Tarreau59234e92008-11-30 23:51:27 +01002038 ret = acl_exec_cond(rule->cond, cur_proxy, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002039
Willy Tarreau59234e92008-11-30 23:51:27 +01002040 ret = acl_pass(ret);
2041 if (rule->cond->pol == ACL_COND_UNLESS)
2042 ret = !ret;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002043
Willy Tarreau59234e92008-11-30 23:51:27 +01002044 if (ret) {
2045 s->be = rule->be.backend;
2046 s->be->beconn++;
2047 if (s->be->beconn > s->be->beconn_max)
2048 s->be->beconn_max = s->be->beconn;
2049 s->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002050
Willy Tarreau59234e92008-11-30 23:51:27 +01002051 /* assign new parameters to the session from the new backend */
2052 s->rep->rto = s->req->wto = s->be->timeout.server;
2053 s->req->cto = s->be->timeout.connect;
2054 s->conn_retries = s->be->conn_retries;
2055 s->flags |= SN_BE_ASSIGNED;
2056 break;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002057 }
2058 }
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002059 }
2060
Willy Tarreau59234e92008-11-30 23:51:27 +01002061 if (!(s->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2062 /* No backend was set, but there was a default
2063 * backend set in the frontend, so we use it and
2064 * loop again.
2065 */
2066 s->be = cur_proxy->defbe.be;
2067 s->be->beconn++;
2068 if (s->be->beconn > s->be->beconn_max)
2069 s->be->beconn_max = s->be->beconn;
2070 s->be->cum_beconn++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002071
Willy Tarreau59234e92008-11-30 23:51:27 +01002072 /* assign new parameters to the session from the new backend */
2073 s->rep->rto = s->req->wto = s->be->timeout.server;
2074 s->req->cto = s->be->timeout.connect;
2075 s->conn_retries = s->be->conn_retries;
2076 s->flags |= SN_BE_ASSIGNED;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002077 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002078 } while (s->be != cur_proxy); /* we loop only if s->be has changed */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002079
Willy Tarreau59234e92008-11-30 23:51:27 +01002080 if (!(s->flags & SN_BE_ASSIGNED)) {
2081 /* To ensure correct connection accounting on
2082 * the backend, we count the connection for the
2083 * one managing the queue.
Willy Tarreau06619262006-12-17 08:37:22 +01002084 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002085 s->be->beconn++;
2086 if (s->be->beconn > s->be->beconn_max)
2087 s->be->beconn_max = s->be->beconn;
2088 s->be->cum_beconn++;
2089 s->flags |= SN_BE_ASSIGNED;
2090 }
Willy Tarreau06619262006-12-17 08:37:22 +01002091
Willy Tarreau59234e92008-11-30 23:51:27 +01002092 /*
2093 * Right now, we know that we have processed the entire headers
2094 * and that unwanted requests have been filtered out. We can do
2095 * whatever we want with the remaining request. Also, now we
2096 * may have separate values for ->fe, ->be.
2097 */
Willy Tarreau06619262006-12-17 08:37:22 +01002098
Willy Tarreau59234e92008-11-30 23:51:27 +01002099 /*
2100 * If HTTP PROXY is set we simply get remote server address
2101 * parsing incoming request.
2102 */
2103 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2104 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2105 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002106
Willy Tarreau59234e92008-11-30 23:51:27 +01002107 /*
2108 * 7: the appsession cookie was looked up very early in 1.2,
2109 * so let's do the same now.
2110 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002111
Willy Tarreau59234e92008-11-30 23:51:27 +01002112 /* It needs to look into the URI */
2113 if (s->be->appsession_name) {
2114 get_srv_from_appsession(s, &req->data[msg->som], msg->sl.rq.l);
2115 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01002116
Willy Tarreau59234e92008-11-30 23:51:27 +01002117 /*
2118 * 8: Now we can work with the cookies.
2119 * Note that doing so might move headers in the request, but
2120 * the fields will stay coherent and the URI will not move.
2121 * This should only be performed in the backend.
2122 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002123 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002124 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2125 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002126
Willy Tarreau59234e92008-11-30 23:51:27 +01002127 /*
2128 * 9: add X-Forwarded-For if either the frontend or the backend
2129 * asks for it.
2130 */
2131 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2132 if (s->cli_addr.ss_family == AF_INET) {
2133 /* Add an X-Forwarded-For header unless the source IP is
2134 * in the 'except' network range.
2135 */
2136 if ((!s->fe->except_mask.s_addr ||
2137 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2138 != s->fe->except_net.s_addr) &&
2139 (!s->be->except_mask.s_addr ||
2140 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2141 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002142 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002143 unsigned char *pn;
2144 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002145
2146 /* Note: we rely on the backend to get the header name to be used for
2147 * x-forwarded-for, because the header is really meant for the backends.
2148 * However, if the backend did not specify any option, we have to rely
2149 * on the frontend's header name.
2150 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002151 if (s->be->fwdfor_hdr_len) {
2152 len = s->be->fwdfor_hdr_len;
2153 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002154 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002155 len = s->fe->fwdfor_hdr_len;
2156 memcpy(trash, s->fe->fwdfor_hdr_name, len);
2157 }
2158 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002159
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002160 if (unlikely(http_header_add_tail2(req, &txn->req,
2161 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002162 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002163 }
2164 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002165 else if (s->cli_addr.ss_family == AF_INET6) {
2166 /* FIXME: for the sake of completeness, we should also support
2167 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002168 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002169 int len;
2170 char pn[INET6_ADDRSTRLEN];
2171 inet_ntop(AF_INET6,
2172 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2173 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002174
Willy Tarreau59234e92008-11-30 23:51:27 +01002175 /* Note: we rely on the backend to get the header name to be used for
2176 * x-forwarded-for, because the header is really meant for the backends.
2177 * However, if the backend did not specify any option, we have to rely
2178 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002179 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002180 if (s->be->fwdfor_hdr_len) {
2181 len = s->be->fwdfor_hdr_len;
2182 memcpy(trash, s->be->fwdfor_hdr_name, len);
2183 } else {
2184 len = s->fe->fwdfor_hdr_len;
2185 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002186 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002187 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002188
Willy Tarreau59234e92008-11-30 23:51:27 +01002189 if (unlikely(http_header_add_tail2(req, &txn->req,
2190 &txn->hdr_idx, trash, len)) < 0)
2191 goto return_bad_req;
2192 }
2193 }
2194
2195 /*
2196 * 10: add "Connection: close" if needed and not yet set.
2197 * Note that we do not need to add it in case of HTTP/1.0.
2198 */
2199 if (!(s->flags & SN_CONN_CLOSED) &&
2200 ((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2201 if ((unlikely(msg->sl.rq.v_l != 8) ||
2202 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2203 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
2204 "Connection: close", 17)) < 0)
2205 goto return_bad_req;
2206 s->flags |= SN_CONN_CLOSED;
2207 }
2208 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2209 * If not assigned, perhaps we are balancing on url_param, but this is a
2210 * POST; and the parameters are in the body, maybe scan there to find our server.
2211 * (unless headers overflowed the buffer?)
2212 */
2213 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2214 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
2215 s->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
2216 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2217 /* are there enough bytes here? total == l || r || rlim ?
2218 * len is unsigned, but eoh is int,
2219 * how many bytes of body have we received?
2220 * eoh is the first empty line of the header
2221 */
2222 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
2223 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
2224
2225 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2226 * We can't assume responsibility for the server's decision,
2227 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2228 * We also can't change our mind later, about which server to choose, so round robin.
2229 */
2230 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2231 struct hdr_ctx ctx;
2232 ctx.idx = 0;
2233 /* Expect is allowed in 1.1, look for it */
2234 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2235 if (ctx.idx != 0 &&
2236 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
2237 /* We can't reliablly stall and wait for data, because of
2238 * .NET clients that don't conform to rfc2616; so, no need for
2239 * the next block to check length expectations.
2240 * We could send 100 status back to the client, but then we need to
2241 * re-write headers, and send the message. And this isn't the right
2242 * place for that action.
2243 * TODO: support Expect elsewhere and delete this block.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002244 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002245 goto end_check_maybe_wait_for_body;
2246 }
2247
2248 if (likely(len > s->be->url_param_post_limit)) {
2249 /* nothing to do, we got enough */
2250 } else {
2251 /* limit implies we are supposed to need this many bytes
2252 * to find the parameter. Let's see how many bytes we can wait for.
2253 */
2254 long long hint = len;
2255 struct hdr_ctx ctx;
2256 ctx.idx = 0;
2257 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
2258 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2259 buffer_write_dis(req);
2260 req->analysers |= AN_REQ_HTTP_BODY;
2261 }
2262 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002263 ctx.idx = 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002264 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2265 /* now if we have a length, we'll take the hint */
2266 if (ctx.idx) {
2267 /* We have Content-Length */
2268 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
2269 hint = 0; /* parse failure, untrusted client */
2270 else {
2271 if (hint > 0)
2272 msg->hdr_content_len = hint;
2273 else
2274 hint = 0; /* bad client, sent negative length */
2275 }
2276 }
2277 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
2278 if (s->be->url_param_post_limit < hint)
2279 hint = s->be->url_param_post_limit;
2280 /* now do we really need to buffer more data? */
2281 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002282 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002283 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002284 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002285 /* else... There are no body bytes to wait for */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002286 }
2287 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002288 }
2289 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002290
Willy Tarreau59234e92008-11-30 23:51:27 +01002291 /*************************************************************
2292 * OK, that's finished for the headers. We have done what we *
2293 * could. Let's switch to the DATA state. *
2294 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002295
Willy Tarreau59234e92008-11-30 23:51:27 +01002296 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
2297 s->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002298
Willy Tarreau59234e92008-11-30 23:51:27 +01002299 /* When a connection is tarpitted, we use the tarpit timeout,
2300 * which may be the same as the connect timeout if unspecified.
2301 * If unset, then set it to zero because we really want it to
2302 * eventually expire. We build the tarpit as an analyser.
2303 */
2304 if (txn->flags & TX_CLTARPIT) {
2305 buffer_flush(s->req);
2306 /* flush the request so that we can drop the connection early
2307 * if the client closes first.
Willy Tarreau2a324282006-12-05 00:05:46 +01002308 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002309 buffer_write_dis(req);
2310 req->analysers |= AN_REQ_HTTP_TARPIT;
2311 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2312 if (!req->analyse_exp)
2313 req->analyse_exp = now_ms;
2314 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002315
Willy Tarreau59234e92008-11-30 23:51:27 +01002316 /* OK let's go on with the BODY now */
2317 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002318
Willy Tarreau59234e92008-11-30 23:51:27 +01002319 return_bad_req: /* let's centralize all bad requests */
2320 txn->req.msg_state = HTTP_MSG_ERROR;
2321 txn->status = 400;
2322 req->analysers = 0;
2323 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2324 s->fe->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002325
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 return_prx_cond:
2327 if (!(s->flags & SN_ERR_MASK))
2328 s->flags |= SN_ERR_PRXCOND;
2329 if (!(s->flags & SN_FINST_MASK))
2330 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002331 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002332}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002333
Willy Tarreau60b85b02008-11-30 23:28:40 +01002334/* This function is an analyser which processes the HTTP tarpit. It always
2335 * returns zero, at the beginning because it prevents any other processing
2336 * from occurring, and at the end because it terminates the request.
2337 */
2338int http_process_tarpit(struct session *s, struct buffer *req)
2339{
2340 struct http_txn *txn = &s->txn;
2341
2342 /* This connection is being tarpitted. The CLIENT side has
2343 * already set the connect expiration date to the right
2344 * timeout. We just have to check that the client is still
2345 * there and that the timeout has not expired.
2346 */
2347 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
2348 !tick_is_expired(req->analyse_exp, now_ms))
2349 return 0;
2350
2351 /* We will set the queue timer to the time spent, just for
2352 * logging purposes. We fake a 500 server error, so that the
2353 * attacker will not suspect his connection has been tarpitted.
2354 * It will not cause trouble to the logs because we can exclude
2355 * the tarpitted connections by filtering on the 'PT' status flags.
2356 */
2357 trace_term(s, TT_HTTP_SRV_2);
2358 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2359
2360 txn->status = 500;
2361 if (req->flags != BF_READ_ERROR)
2362 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
2363
2364 req->analysers = 0;
2365 req->analyse_exp = TICK_ETERNITY;
2366
2367 s->fe->failed_req++;
2368 if (!(s->flags & SN_ERR_MASK))
2369 s->flags |= SN_ERR_PRXCOND;
2370 if (!(s->flags & SN_FINST_MASK))
2371 s->flags |= SN_FINST_T;
2372 return 0;
2373}
2374
Willy Tarreaud34af782008-11-30 23:36:37 +01002375/* This function is an analyser which processes the HTTP request body. It looks
2376 * for parameters to be used for the load balancing algorithm (url_param). It
2377 * must only be called after the standard HTTP request processing has occurred,
2378 * because it expects the request to be parsed. It returns zero if it needs to
2379 * read more data, or 1 once it has completed its analysis.
2380 */
2381int http_process_request_body(struct session *s, struct buffer *req)
2382{
2383 struct http_msg *msg = &s->txn.req;
2384 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2385 long long limit = s->be->url_param_post_limit;
2386 struct hdr_ctx ctx;
2387
2388 /* We have to parse the HTTP request body to find any required data.
2389 * "balance url_param check_post" should have been the only way to get
2390 * into this. We were brought here after HTTP header analysis, so all
2391 * related structures are ready.
2392 */
2393
2394 ctx.idx = 0;
2395
2396 /* now if we have a length, we'll take the hint */
2397 http_find_header2("Transfer-Encoding", 17, msg->sol, &s->txn.hdr_idx, &ctx);
2398 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2399 unsigned int chunk = 0;
2400 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2401 char c = msg->sol[body];
2402 if (ishex(c)) {
2403 unsigned int hex = toupper(c) - '0';
2404 if (hex > 9)
2405 hex -= 'A' - '9' - 1;
2406 chunk = (chunk << 4) | hex;
2407 } else
2408 break;
2409 body++;
2410 }
2411 if (body + 2 >= req->l) /* we want CRLF too */
2412 goto http_body_end; /* end of buffer? data missing! */
2413
2414 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
2415 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
2416
2417 body += 2; // skip CRLF
2418
2419 /* if we support more then one chunk here, we have to do it again when assigning server
2420 * 1. how much entity data do we have? new var
2421 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2422 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2423 */
2424
2425 if (chunk < limit)
2426 limit = chunk; /* only reading one chunk */
2427 } else {
2428 if (msg->hdr_content_len < limit)
2429 limit = msg->hdr_content_len;
2430 }
2431
2432 http_body_end:
2433 /* we leave once we know we have nothing left to do. This means that we have
2434 * enough bytes, or that we know we'll not get any more (buffer full, read
2435 * buffer closed).
2436 */
2437 if (req->l - body >= limit || /* enough bytes! */
2438 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
2439 tick_is_expired(req->analyse_exp, now_ms)) {
2440 /* The situation will not evolve, so let's give up on the analysis. */
2441 s->logs.tv_request = now; /* update the request timer to reflect full request */
2442 req->analysers &= ~AN_REQ_HTTP_BODY;
2443 req->analyse_exp = TICK_ETERNITY;
2444 return 1;
2445 }
2446 else {
2447 /* Not enough data. We'll re-use the http-request
2448 * timeout here. Ideally, we should set the timeout
2449 * relative to the accept() date. We just set the
2450 * request timeout once at the beginning of the
2451 * request.
2452 */
2453 buffer_write_dis(req);
2454 if (!tick_isset(req->analyse_exp))
2455 req->analyse_exp = tick_add_ifset(now_ms, s->fe->timeout.httpreq);
2456 return 0;
2457 }
2458}
2459
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002460/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002461 * It normally returns zero, but may return 1 if it absolutely needs to be
2462 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002463 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002464 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002465 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002466int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002467{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002468 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002469 struct buffer *req = t->req;
2470 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002471
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002472 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 +02002473 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002474 t,
2475 rep,
2476 rep->rex, rep->wex,
2477 rep->flags,
2478 rep->l,
2479 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002480
Willy Tarreau2df28e82008-08-17 15:20:19 +02002481 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002482 /*
2483 * Now parse the partial (or complete) lines.
2484 * We will check the response syntax, and also join multi-line
2485 * headers. An index of all the lines will be elaborated while
2486 * parsing.
2487 *
2488 * For the parsing, we use a 28 states FSM.
2489 *
2490 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002491 * rep->data + rep->som = beginning of response
2492 * rep->data + rep->eoh = end of processed headers / start of current one
2493 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002494 * rep->lr = first non-visited byte
2495 * rep->r = end of data
2496 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002497
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002498 int cur_idx;
2499 struct http_msg *msg = &txn->rsp;
2500 struct proxy *cur_proxy;
2501
2502 if (likely(rep->lr < rep->r))
2503 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2504
2505 /* 1: we might have to print this header in debug mode */
2506 if (unlikely((global.mode & MODE_DEBUG) &&
2507 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2508 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2509 char *eol, *sol;
2510
2511 sol = rep->data + msg->som;
2512 eol = sol + msg->sl.rq.l;
2513 debug_hdr("srvrep", t, sol, eol);
2514
2515 sol += hdr_idx_first_pos(&txn->hdr_idx);
2516 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2517
2518 while (cur_idx) {
2519 eol = sol + txn->hdr_idx.v[cur_idx].len;
2520 debug_hdr("srvhdr", t, sol, eol);
2521 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2522 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002523 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002524 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002525
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002526 /*
2527 * Now we quickly check if we have found a full valid response.
2528 * If not so, we check the FD and buffer states before leaving.
2529 * A full response is indicated by the fact that we have seen
2530 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2531 * responses are checked first.
2532 *
2533 * Depending on whether the client is still there or not, we
2534 * may send an error response back or not. Note that normally
2535 * we should only check for HTTP status there, and check I/O
2536 * errors somewhere else.
2537 */
2538
2539 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002540 /* Invalid response */
2541 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2542 hdr_response_bad:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002543 //buffer_shutr(rep);
2544 //buffer_shutw(req);
2545 //fd_delete(req->cons->fd);
2546 //req->cons->state = SI_ST_CLO;
2547 buffer_shutr_now(rep);
2548 buffer_shutw_now(req);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002549 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002550 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002551 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002552 //sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002553 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002554 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002555 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002556 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002557 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002558 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002559 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002560 if (!(t->flags & SN_FINST_MASK))
2561 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002562
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002563 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2564 // process_srv_queue(t->srv);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002565
Willy Tarreaudafde432008-08-17 01:00:46 +02002566 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002567 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002568 /* too large response does not fit in buffer. */
2569 else if (rep->flags & BF_FULL) {
2570 goto hdr_response_bad;
2571 }
2572 /* read error */
2573 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002574 buffer_shutr_now(rep);
2575 buffer_shutw_now(req);
2576 //fd_delete(req->cons->fd);
2577 //req->cons->state = SI_ST_CLO;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002578 //if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002579 //t->srv->cur_sess--;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002580 //t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002581 //sess_change_server(t, NULL);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002582 //}
2583 //t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002584 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002585 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002586 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002587 if (!(t->flags & SN_ERR_MASK))
2588 t->flags |= SN_ERR_SRVCL;
2589 if (!(t->flags & SN_FINST_MASK))
2590 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002591
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002592 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2593 // process_srv_queue(t->srv);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002594
Willy Tarreaudafde432008-08-17 01:00:46 +02002595 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002596 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002597 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002598 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002599 buffer_shutr_now(rep);
2600 buffer_shutw_now(req);
2601 //fd_delete(req->cons->fd);
2602 //req->cons->state = SI_ST_CLO;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002603 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002604 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002605 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002606 //sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002607 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002608 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002609 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002610 txn->status = 504;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002611 stream_int_return(rep->cons, error_message(t, HTTP_ERR_504));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002612 if (!(t->flags & SN_ERR_MASK))
2613 t->flags |= SN_ERR_SRVTO;
2614 if (!(t->flags & SN_FINST_MASK))
2615 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002616
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002617 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2618 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002619 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002620 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002621 /* write error to client, or close from server */
Willy Tarreaue5ed4062008-08-30 03:17:31 +02002622 else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) {
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002623 buffer_shutr_now(rep);
2624 buffer_shutw_now(req);
2625 //fd_delete(req->cons->fd);
2626 //req->cons->state = SI_ST_CLO;
2627 if (t->srv) {
2628 //t->srv->cur_sess--;
2629 t->srv->failed_resp++;
2630 //sess_change_server(t, NULL);
2631 }
2632 t->be->failed_resp++;
2633 rep->analysers = 0;
2634 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002635 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002636 if (!(t->flags & SN_ERR_MASK))
2637 t->flags |= SN_ERR_SRVCL;
2638 if (!(t->flags & SN_FINST_MASK))
2639 t->flags |= SN_FINST_H;
2640
2641 //if (t->srv && may_dequeue_tasks(t->srv, t->be))
2642 // process_srv_queue(t->srv);
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002643
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002644 return 0;
2645 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02002646 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002647 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002648 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002649
Willy Tarreau21d2af32008-02-14 20:25:24 +01002650
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002651 /*****************************************************************
2652 * More interesting part now : we know that we have a complete *
2653 * response which at least looks like HTTP. We have an indicator *
2654 * of each header's length, so we can parse them quickly. *
2655 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002656
Willy Tarreau2df28e82008-08-17 15:20:19 +02002657 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002658
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002659 /* ensure we keep this pointer to the beginning of the message */
2660 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002661
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002662 /*
2663 * 1: get the status code and check for cacheability.
2664 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002665
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002666 t->logs.logwait &= ~LW_RESP;
2667 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002668
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002669 switch (txn->status) {
2670 case 200:
2671 case 203:
2672 case 206:
2673 case 300:
2674 case 301:
2675 case 410:
2676 /* RFC2616 @13.4:
2677 * "A response received with a status code of
2678 * 200, 203, 206, 300, 301 or 410 MAY be stored
2679 * by a cache (...) unless a cache-control
2680 * directive prohibits caching."
2681 *
2682 * RFC2616 @9.5: POST method :
2683 * "Responses to this method are not cacheable,
2684 * unless the response includes appropriate
2685 * Cache-Control or Expires header fields."
2686 */
2687 if (likely(txn->meth != HTTP_METH_POST) &&
2688 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2689 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2690 break;
2691 default:
2692 break;
2693 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002694
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002695 /*
2696 * 2: we may need to capture headers
2697 */
2698 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2699 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2700 txn->rsp.cap, t->fe->rsp_cap);
2701
2702 /*
2703 * 3: we will have to evaluate the filters.
2704 * As opposed to version 1.2, now they will be evaluated in the
2705 * filters order and not in the header order. This means that
2706 * each filter has to be validated among all headers.
2707 *
2708 * Filters are tried with ->be first, then with ->fe if it is
2709 * different from ->be.
2710 */
2711
2712 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2713
2714 cur_proxy = t->be;
2715 while (1) {
2716 struct proxy *rule_set = cur_proxy;
2717
2718 /* try headers filters */
2719 if (rule_set->rsp_exp != NULL) {
2720 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2721 return_bad_resp:
2722 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002723 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002724 t->srv->failed_resp++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002725 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002726 }
2727 cur_proxy->failed_resp++;
2728 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002729 buffer_shutr_now(rep);
2730 buffer_shutw_now(req);
2731 //fd_delete(req->cons->fd);
2732 //req->cons->state = SI_ST_CLO;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002733 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002734 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002735 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002736 if (!(t->flags & SN_ERR_MASK))
2737 t->flags |= SN_ERR_PRXCOND;
2738 if (!(t->flags & SN_FINST_MASK))
2739 t->flags |= SN_FINST_H;
2740 /* We used to have a free connection slot. Since we'll never use it,
2741 * we have to inform the server that it may be used by another session.
2742 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002743 //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
2744 // process_srv_queue(t->srv);
Willy Tarreaudafde432008-08-17 01:00:46 +02002745 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002746 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002747 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002748
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002749 /* has the response been denied ? */
2750 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01002751 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002752 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002753 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002754 //sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002755 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002756 cur_proxy->denied_resp++;
2757 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002758 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002759
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002760 /* We might have to check for "Connection:" */
2761 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2762 !(t->flags & SN_CONN_CLOSED)) {
2763 char *cur_ptr, *cur_end, *cur_next;
2764 int cur_idx, old_idx, delta, val;
2765 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002766
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002767 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2768 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002769
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002770 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2771 cur_hdr = &txn->hdr_idx.v[cur_idx];
2772 cur_ptr = cur_next;
2773 cur_end = cur_ptr + cur_hdr->len;
2774 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002775
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002776 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2777 if (val) {
2778 /* 3 possibilities :
2779 * - we have already set Connection: close,
2780 * so we remove this line.
2781 * - we have not yet set Connection: close,
2782 * but this line indicates close. We leave
2783 * it untouched and set the flag.
2784 * - we have not yet set Connection: close,
2785 * and this line indicates non-close. We
2786 * replace it.
2787 */
2788 if (t->flags & SN_CONN_CLOSED) {
2789 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2790 txn->rsp.eoh += delta;
2791 cur_next += delta;
2792 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2793 txn->hdr_idx.used--;
2794 cur_hdr->len = 0;
2795 } else {
2796 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2797 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2798 "close", 5);
2799 cur_next += delta;
2800 cur_hdr->len += delta;
2801 txn->rsp.eoh += delta;
2802 }
2803 t->flags |= SN_CONN_CLOSED;
2804 }
2805 }
2806 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002807 }
2808 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002809
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002810 /* add response headers from the rule sets in the same order */
2811 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2812 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2813 rule_set->rsp_add[cur_idx])) < 0)
2814 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002815 }
2816
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002817 /* check whether we're already working on the frontend */
2818 if (cur_proxy == t->fe)
2819 break;
2820 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002821 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002822
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002823 /*
2824 * 4: check for server cookie.
2825 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002826 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002827 || (t->be->options & PR_O_CHK_CACHE))
2828 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002829
Willy Tarreaubaaee002006-06-26 02:48:02 +02002830
Willy Tarreaua15645d2007-03-18 16:22:39 +01002831 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002832 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002833 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002834 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2835 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002836
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002837 /*
2838 * 6: add server cookie in the response if needed
2839 */
2840 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2841 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2842 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002843
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002844 /* the server is known, it's not the one the client requested, we have to
2845 * insert a set-cookie here, except if we want to insert only on POST
2846 * requests and this one isn't. Note that servers which don't have cookies
2847 * (eg: some backup servers) will return a full cookie removal request.
2848 */
2849 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2850 t->be->cookie_name,
2851 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002852
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002853 if (t->be->cookie_domain)
2854 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002855
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002856 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2857 trash, len)) < 0)
2858 goto return_bad_resp;
2859 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002860
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002861 /* Here, we will tell an eventual cache on the client side that we don't
2862 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2863 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2864 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2865 */
2866 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002867
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002868 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2869
2870 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2871 "Cache-control: private", 22)) < 0)
2872 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002873 }
2874 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002875
Willy Tarreaubaaee002006-06-26 02:48:02 +02002876
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002877 /*
2878 * 7: check if result will be cacheable with a cookie.
2879 * We'll block the response if security checks have caught
2880 * nasty things such as a cacheable cookie.
2881 */
2882 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2883 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2884 (t->be->options & PR_O_CHK_CACHE)) {
2885
2886 /* we're in presence of a cacheable response containing
2887 * a set-cookie header. We'll block it as requested by
2888 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002889 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002890 if (t->srv) {
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002891 //t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002892 t->srv->failed_secu++;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002893 //sess_change_server(t, NULL);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002894 }
2895 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002896
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002897 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2898 t->be->id, t->srv?t->srv->id:"<dispatch>");
2899 send_log(t->be, LOG_ALERT,
2900 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2901 t->be->id, t->srv?t->srv->id:"<dispatch>");
2902 goto return_srv_prx_502;
2903 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002904
2905 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002906 * 8: add "Connection: close" if needed and not yet set.
2907 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002908 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002909 if (!(t->flags & SN_CONN_CLOSED) &&
2910 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2911 if ((unlikely(msg->sl.st.v_l != 8) ||
2912 unlikely(req->data[msg->som + 7] != '0')) &&
2913 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2914 "Connection: close", 17)) < 0)
2915 goto return_bad_resp;
2916 t->flags |= SN_CONN_CLOSED;
2917 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002918
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002919 /*************************************************************
2920 * OK, that's finished for the headers. We have done what we *
2921 * could. Let's switch to the DATA state. *
2922 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002923
Willy Tarreaue393fe22008-08-16 22:18:07 +02002924 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002925 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002926
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002927#ifdef CONFIG_HAP_TCPSPLICE
2928 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
2929 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002930 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002931 }
2932#endif
2933 /* if the user wants to log as soon as possible, without counting
2934 * bytes from the server, then this is the right moment. We have
2935 * to temporarily assign bytes_out to log what we currently have.
2936 */
2937 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2938 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
2939 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002940 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002941 t->logs.bytes_out = 0;
2942 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002943
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002944 /* Note: we must not try to cheat by jumping directly to DATA,
2945 * otherwise we would not let the client side wake up.
2946 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002947
Willy Tarreaudafde432008-08-17 01:00:46 +02002948 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002949 }
Willy Tarreaudafde432008-08-17 01:00:46 +02002950
Willy Tarreau2df28e82008-08-17 15:20:19 +02002951 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
2952 * probably reduce one day's debugging session.
2953 */
2954#ifdef DEBUG_DEV
2955 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
2956 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
2957 __FILE__, __LINE__, rep->analysers);
2958 ABORT_NOW();
2959 }
2960#endif
2961 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002962 return 0;
2963}
Willy Tarreaua15645d2007-03-18 16:22:39 +01002964
Willy Tarreaubaaee002006-06-26 02:48:02 +02002965/*
2966 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02002967 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02002968 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
2969 * buffer, which it uses to keep on being called when there is free space in
Willy Tarreau01bf8672008-12-07 18:03:29 +01002970 * the buffer, or simply by letting an empty buffer upon return.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002971 */
Willy Tarreau01bf8672008-12-07 18:03:29 +01002972void produce_content(struct session *s, struct buffer *rep)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002973{
Willy Tarreaubaaee002006-06-26 02:48:02 +02002974 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau01bf8672008-12-07 18:03:29 +01002975 buffer_stop_hijack(rep);
2976 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002977 }
2978 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002979 /* dump server statistics */
Willy Tarreau0a464892008-12-07 18:30:00 +01002980 int ret = stats_dump_http(s, rep, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02002981 if (ret >= 0)
Willy Tarreau01bf8672008-12-07 18:03:29 +01002982 return;
Willy Tarreau91861262007-10-17 17:06:05 +02002983 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002984 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002985
Willy Tarreau91861262007-10-17 17:06:05 +02002986 /* unknown data source or internal error */
2987 s->txn.status = 500;
Willy Tarreau01bf8672008-12-07 18:03:29 +01002988 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02002989 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02002990 if (!(s->flags & SN_ERR_MASK))
2991 s->flags |= SN_ERR_PRXCOND;
2992 if (!(s->flags & SN_FINST_MASK))
2993 s->flags |= SN_FINST_R;
Willy Tarreau01bf8672008-12-07 18:03:29 +01002994 buffer_stop_hijack(rep);
2995 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002996}
2997
2998
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002999/* Iterate the same filter through all request headers.
3000 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003001 * Since it can manage the switch to another backend, it updates the per-proxy
3002 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003003 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003004int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003005{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003006 char term;
3007 char *cur_ptr, *cur_end, *cur_next;
3008 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003009 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003010 struct hdr_idx_elem *cur_hdr;
3011 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003012
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003013 last_hdr = 0;
3014
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003015 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003016 old_idx = 0;
3017
3018 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003019 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003020 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003021 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003022 (exp->action == ACT_ALLOW ||
3023 exp->action == ACT_DENY ||
3024 exp->action == ACT_TARPIT))
3025 return 0;
3026
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003027 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003028 if (!cur_idx)
3029 break;
3030
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003031 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003032 cur_ptr = cur_next;
3033 cur_end = cur_ptr + cur_hdr->len;
3034 cur_next = cur_end + cur_hdr->cr + 1;
3035
3036 /* Now we have one header between cur_ptr and cur_end,
3037 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003038 */
3039
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003040 /* The annoying part is that pattern matching needs
3041 * that we modify the contents to null-terminate all
3042 * strings before testing them.
3043 */
3044
3045 term = *cur_end;
3046 *cur_end = '\0';
3047
3048 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3049 switch (exp->action) {
3050 case ACT_SETBE:
3051 /* It is not possible to jump a second time.
3052 * FIXME: should we return an HTTP/500 here so that
3053 * the admin knows there's a problem ?
3054 */
3055 if (t->be != t->fe)
3056 break;
3057
3058 /* Swithing Proxy */
3059 t->be = (struct proxy *) exp->replace;
3060
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003061 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003062 * because we have associated req_cap and rsp_cap to the
3063 * frontend, and the beconn will be updated later.
3064 */
3065
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003066 t->rep->rto = t->req->wto = t->be->timeout.server;
3067 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003068 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003069 last_hdr = 1;
3070 break;
3071
3072 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003073 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003074 last_hdr = 1;
3075 break;
3076
3077 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003078 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003079 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003080 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003081 break;
3082
3083 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003084 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003085 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003086 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003087 break;
3088
3089 case ACT_REPLACE:
3090 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3091 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3092 /* FIXME: if the user adds a newline in the replacement, the
3093 * index will not be recalculated for now, and the new line
3094 * will not be counted as a new header.
3095 */
3096
3097 cur_end += delta;
3098 cur_next += delta;
3099 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003100 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003101 break;
3102
3103 case ACT_REMOVE:
3104 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3105 cur_next += delta;
3106
3107 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003108 txn->req.eoh += delta;
3109 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3110 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003111 cur_hdr->len = 0;
3112 cur_end = NULL; /* null-term has been rewritten */
3113 break;
3114
3115 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003116 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003117 if (cur_end)
3118 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003119
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003120 /* keep the link from this header to next one in case of later
3121 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003122 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003123 old_idx = cur_idx;
3124 }
3125 return 0;
3126}
3127
3128
3129/* Apply the filter to the request line.
3130 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3131 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003132 * Since it can manage the switch to another backend, it updates the per-proxy
3133 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003134 */
3135int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3136{
3137 char term;
3138 char *cur_ptr, *cur_end;
3139 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003140 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003141 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003142
Willy Tarreau58f10d72006-12-04 02:26:12 +01003143
Willy Tarreau3d300592007-03-18 18:34:41 +01003144 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003145 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003146 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003147 (exp->action == ACT_ALLOW ||
3148 exp->action == ACT_DENY ||
3149 exp->action == ACT_TARPIT))
3150 return 0;
3151 else if (exp->action == ACT_REMOVE)
3152 return 0;
3153
3154 done = 0;
3155
Willy Tarreau9cdde232007-05-02 20:58:19 +02003156 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003157 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003158
3159 /* Now we have the request line between cur_ptr and cur_end */
3160
3161 /* The annoying part is that pattern matching needs
3162 * that we modify the contents to null-terminate all
3163 * strings before testing them.
3164 */
3165
3166 term = *cur_end;
3167 *cur_end = '\0';
3168
3169 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3170 switch (exp->action) {
3171 case ACT_SETBE:
3172 /* It is not possible to jump a second time.
3173 * FIXME: should we return an HTTP/500 here so that
3174 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003175 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003176 if (t->be != t->fe)
3177 break;
3178
3179 /* Swithing Proxy */
3180 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003181
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003182 /* right now, the backend switch is not too much complicated
3183 * because we have associated req_cap and rsp_cap to the
3184 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003185 */
3186
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003187 t->rep->rto = t->req->wto = t->be->timeout.server;
3188 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003189 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003190 done = 1;
3191 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003192
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003193 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003194 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003195 done = 1;
3196 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003197
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003198 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003199 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003200 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003201 done = 1;
3202 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003203
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003204 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003205 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003206 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003207 done = 1;
3208 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003209
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003210 case ACT_REPLACE:
3211 *cur_end = term; /* restore the string terminator */
3212 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3213 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3214 /* FIXME: if the user adds a newline in the replacement, the
3215 * index will not be recalculated for now, and the new line
3216 * will not be counted as a new header.
3217 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003218
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003219 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003220 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003221
Willy Tarreau9cdde232007-05-02 20:58:19 +02003222 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003223 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003224 HTTP_MSG_RQMETH,
3225 cur_ptr, cur_end + 1,
3226 NULL, NULL);
3227 if (unlikely(!cur_end))
3228 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003229
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003230 /* we have a full request and we know that we have either a CR
3231 * or an LF at <ptr>.
3232 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003233 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3234 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003235 /* there is no point trying this regex on headers */
3236 return 1;
3237 }
3238 }
3239 *cur_end = term; /* restore the string terminator */
3240 return done;
3241}
Willy Tarreau97de6242006-12-27 17:18:38 +01003242
Willy Tarreau58f10d72006-12-04 02:26:12 +01003243
Willy Tarreau58f10d72006-12-04 02:26:12 +01003244
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003245/*
3246 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3247 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003248 * unparsable request. Since it can manage the switch to another backend, it
3249 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003250 */
3251int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3252{
Willy Tarreau3d300592007-03-18 18:34:41 +01003253 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003254 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003255 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003256 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003258 /*
3259 * The interleaving of transformations and verdicts
3260 * makes it difficult to decide to continue or stop
3261 * the evaluation.
3262 */
3263
Willy Tarreau3d300592007-03-18 18:34:41 +01003264 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003265 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3266 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3267 exp = exp->next;
3268 continue;
3269 }
3270
3271 /* Apply the filter to the request line. */
3272 ret = apply_filter_to_req_line(t, req, exp);
3273 if (unlikely(ret < 0))
3274 return -1;
3275
3276 if (likely(ret == 0)) {
3277 /* The filter did not match the request, it can be
3278 * iterated through all headers.
3279 */
3280 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003281 }
3282 exp = exp->next;
3283 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003284 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003285}
3286
3287
Willy Tarreaua15645d2007-03-18 16:22:39 +01003288
Willy Tarreau58f10d72006-12-04 02:26:12 +01003289/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003290 * Manage client-side cookie. It can impact performance by about 2% so it is
3291 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003292 */
3293void manage_client_side_cookies(struct session *t, struct buffer *req)
3294{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003295 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003296 char *p1, *p2, *p3, *p4;
3297 char *del_colon, *del_cookie, *colon;
3298 int app_cookies;
3299
3300 appsess *asession_temp = NULL;
3301 appsess local_asession;
3302
3303 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003304 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003305
Willy Tarreau2a324282006-12-05 00:05:46 +01003306 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003307 * we start with the start line.
3308 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003309 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003310 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003311
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003312 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003313 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003314 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003315
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003316 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003317 cur_ptr = cur_next;
3318 cur_end = cur_ptr + cur_hdr->len;
3319 cur_next = cur_end + cur_hdr->cr + 1;
3320
3321 /* We have one full header between cur_ptr and cur_end, and the
3322 * next header starts at cur_next. We're only interested in
3323 * "Cookie:" headers.
3324 */
3325
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003326 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3327 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003328 old_idx = cur_idx;
3329 continue;
3330 }
3331
3332 /* Now look for cookies. Conforming to RFC2109, we have to support
3333 * attributes whose name begin with a '$', and associate them with
3334 * the right cookie, if we want to delete this cookie.
3335 * So there are 3 cases for each cookie read :
3336 * 1) it's a special attribute, beginning with a '$' : ignore it.
3337 * 2) it's a server id cookie that we *MAY* want to delete : save
3338 * some pointers on it (last semi-colon, beginning of cookie...)
3339 * 3) it's an application cookie : we *MAY* have to delete a previous
3340 * "special" cookie.
3341 * At the end of loop, if a "special" cookie remains, we may have to
3342 * remove it. If no application cookie persists in the header, we
3343 * *MUST* delete it
3344 */
3345
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003346 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003347
Willy Tarreau58f10d72006-12-04 02:26:12 +01003348 /* del_cookie == NULL => nothing to be deleted */
3349 del_colon = del_cookie = NULL;
3350 app_cookies = 0;
3351
3352 while (p1 < cur_end) {
3353 /* skip spaces and colons, but keep an eye on these ones */
3354 while (p1 < cur_end) {
3355 if (*p1 == ';' || *p1 == ',')
3356 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003357 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003358 break;
3359 p1++;
3360 }
3361
3362 if (p1 == cur_end)
3363 break;
3364
3365 /* p1 is at the beginning of the cookie name */
3366 p2 = p1;
3367 while (p2 < cur_end && *p2 != '=')
3368 p2++;
3369
3370 if (p2 == cur_end)
3371 break;
3372
3373 p3 = p2 + 1; /* skips the '=' sign */
3374 if (p3 == cur_end)
3375 break;
3376
3377 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003378 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003379 p4++;
3380
3381 /* here, we have the cookie name between p1 and p2,
3382 * and its value between p3 and p4.
3383 * we can process it :
3384 *
3385 * Cookie: NAME=VALUE;
3386 * | || || |
3387 * | || || +--> p4
3388 * | || |+-------> p3
3389 * | || +--------> p2
3390 * | |+------------> p1
3391 * | +-------------> colon
3392 * +--------------------> cur_ptr
3393 */
3394
3395 if (*p1 == '$') {
3396 /* skip this one */
3397 }
3398 else {
3399 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003400 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003401 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003402 (p4 - p1 >= t->fe->capture_namelen) &&
3403 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003404 int log_len = p4 - p1;
3405
Willy Tarreau086b3b42007-05-13 21:45:51 +02003406 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003407 Alert("HTTP logging : out of memory.\n");
3408 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003409 if (log_len > t->fe->capture_len)
3410 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003411 memcpy(txn->cli_cookie, p1, log_len);
3412 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003413 }
3414 }
3415
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003416 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3417 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003418 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003419 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003420 char *delim;
3421
3422 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3423 * have the server ID betweek p3 and delim, and the original cookie between
3424 * delim+1 and p4. Otherwise, delim==p4 :
3425 *
3426 * Cookie: NAME=SRV~VALUE;
3427 * | || || | |
3428 * | || || | +--> p4
3429 * | || || +--------> delim
3430 * | || |+-----------> p3
3431 * | || +------------> p2
3432 * | |+----------------> p1
3433 * | +-----------------> colon
3434 * +------------------------> cur_ptr
3435 */
3436
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003437 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003438 for (delim = p3; delim < p4; delim++)
3439 if (*delim == COOKIE_DELIM)
3440 break;
3441 }
3442 else
3443 delim = p4;
3444
3445
3446 /* Here, we'll look for the first running server which supports the cookie.
3447 * This allows to share a same cookie between several servers, for example
3448 * to dedicate backup servers to specific servers only.
3449 * However, to prevent clients from sticking to cookie-less backup server
3450 * when they have incidentely learned an empty cookie, we simply ignore
3451 * empty cookies and mark them as invalid.
3452 */
3453 if (delim == p3)
3454 srv = NULL;
3455
3456 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003457 if (srv->cookie && (srv->cklen == delim - p3) &&
3458 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003459 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003460 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003461 txn->flags &= ~TX_CK_MASK;
3462 txn->flags |= TX_CK_VALID;
3463 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003464 t->srv = srv;
3465 break;
3466 } else {
3467 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003468 txn->flags &= ~TX_CK_MASK;
3469 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003470 }
3471 }
3472 srv = srv->next;
3473 }
3474
Willy Tarreau3d300592007-03-18 18:34:41 +01003475 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003476 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003477 txn->flags &= ~TX_CK_MASK;
3478 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003479 }
3480
3481 /* depending on the cookie mode, we may have to either :
3482 * - delete the complete cookie if we're in insert+indirect mode, so that
3483 * the server never sees it ;
3484 * - remove the server id from the cookie value, and tag the cookie as an
3485 * application cookie so that it does not get accidentely removed later,
3486 * if we're in cookie prefix mode
3487 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003488 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003489 int delta; /* negative */
3490
3491 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3492 p4 += delta;
3493 cur_end += delta;
3494 cur_next += delta;
3495 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003496 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003497
3498 del_cookie = del_colon = NULL;
3499 app_cookies++; /* protect the header from deletion */
3500 }
3501 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003502 (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 +01003503 del_cookie = p1;
3504 del_colon = colon;
3505 }
3506 } else {
3507 /* now we know that we must keep this cookie since it's
3508 * not ours. But if we wanted to delete our cookie
3509 * earlier, we cannot remove the complete header, but we
3510 * can remove the previous block itself.
3511 */
3512 app_cookies++;
3513
3514 if (del_cookie != NULL) {
3515 int delta; /* negative */
3516
3517 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3518 p4 += delta;
3519 cur_end += delta;
3520 cur_next += delta;
3521 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003522 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003523 del_cookie = del_colon = NULL;
3524 }
3525 }
3526
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003527 if ((t->be->appsession_name != NULL) &&
3528 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003529 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003530
Willy Tarreau58f10d72006-12-04 02:26:12 +01003531 /* Cool... it's the right one */
3532
3533 asession_temp = &local_asession;
3534
Willy Tarreau63963c62007-05-13 21:29:55 +02003535 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003536 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3537 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3538 return;
3539 }
3540
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003541 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3542 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003543 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003544
Willy Tarreau58f10d72006-12-04 02:26:12 +01003545 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003546 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3547 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003548 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003549 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003550 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003551 Alert("Not enough memory process_cli():asession:calloc().\n");
3552 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3553 return;
3554 }
3555
3556 asession_temp->sessid = local_asession.sessid;
3557 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003558 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02003559 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003560 } else {
3561 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003562 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003563 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003564 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003565 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003566 Alert("Found Application Session without matching server.\n");
3567 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003568 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003569 while (srv) {
3570 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003571 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003572 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003573 txn->flags &= ~TX_CK_MASK;
3574 txn->flags |= TX_CK_VALID;
3575 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003576 t->srv = srv;
3577 break;
3578 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01003579 txn->flags &= ~TX_CK_MASK;
3580 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003581 }
3582 }
3583 srv = srv->next;
3584 }/* end while(srv) */
3585 }/* end else if server == NULL */
3586
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003587 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003588 asession_temp->request_count++;
3589#if defined(DEBUG_HASH)
3590 Alert("manage_client_side_cookies\n");
3591 appsession_hash_dump(&(t->be->htbl_proxy));
3592#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01003593 }/* end if ((t->proxy->appsession_name != NULL) ... */
3594 }
3595
3596 /* we'll have to look for another cookie ... */
3597 p1 = p4;
3598 } /* while (p1 < cur_end) */
3599
3600 /* There's no more cookie on this line.
3601 * We may have marked the last one(s) for deletion.
3602 * We must do this now in two ways :
3603 * - if there is no app cookie, we simply delete the header ;
3604 * - if there are app cookies, we must delete the end of the
3605 * string properly, including the colon/semi-colon before
3606 * the cookie name.
3607 */
3608 if (del_cookie != NULL) {
3609 int delta;
3610 if (app_cookies) {
3611 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
3612 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003613 cur_hdr->len += delta;
3614 } else {
3615 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003616
3617 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003618 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3619 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003620 cur_hdr->len = 0;
3621 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01003622 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003623 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003624 }
3625
3626 /* keep the link from this header to next one */
3627 old_idx = cur_idx;
3628 } /* end of cookie processing on this header */
3629}
3630
3631
Willy Tarreaua15645d2007-03-18 16:22:39 +01003632/* Iterate the same filter through all response headers contained in <rtr>.
3633 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3634 */
3635int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3636{
3637 char term;
3638 char *cur_ptr, *cur_end, *cur_next;
3639 int cur_idx, old_idx, last_hdr;
3640 struct http_txn *txn = &t->txn;
3641 struct hdr_idx_elem *cur_hdr;
3642 int len, delta;
3643
3644 last_hdr = 0;
3645
3646 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3647 old_idx = 0;
3648
3649 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003650 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003651 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003652 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003653 (exp->action == ACT_ALLOW ||
3654 exp->action == ACT_DENY))
3655 return 0;
3656
3657 cur_idx = txn->hdr_idx.v[old_idx].next;
3658 if (!cur_idx)
3659 break;
3660
3661 cur_hdr = &txn->hdr_idx.v[cur_idx];
3662 cur_ptr = cur_next;
3663 cur_end = cur_ptr + cur_hdr->len;
3664 cur_next = cur_end + cur_hdr->cr + 1;
3665
3666 /* Now we have one header between cur_ptr and cur_end,
3667 * and the next header starts at cur_next.
3668 */
3669
3670 /* The annoying part is that pattern matching needs
3671 * that we modify the contents to null-terminate all
3672 * strings before testing them.
3673 */
3674
3675 term = *cur_end;
3676 *cur_end = '\0';
3677
3678 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3679 switch (exp->action) {
3680 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003681 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003682 last_hdr = 1;
3683 break;
3684
3685 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003686 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003687 last_hdr = 1;
3688 break;
3689
3690 case ACT_REPLACE:
3691 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3692 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3693 /* FIXME: if the user adds a newline in the replacement, the
3694 * index will not be recalculated for now, and the new line
3695 * will not be counted as a new header.
3696 */
3697
3698 cur_end += delta;
3699 cur_next += delta;
3700 cur_hdr->len += delta;
3701 txn->rsp.eoh += delta;
3702 break;
3703
3704 case ACT_REMOVE:
3705 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3706 cur_next += delta;
3707
3708 /* FIXME: this should be a separate function */
3709 txn->rsp.eoh += delta;
3710 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3711 txn->hdr_idx.used--;
3712 cur_hdr->len = 0;
3713 cur_end = NULL; /* null-term has been rewritten */
3714 break;
3715
3716 }
3717 }
3718 if (cur_end)
3719 *cur_end = term; /* restore the string terminator */
3720
3721 /* keep the link from this header to next one in case of later
3722 * removal of next header.
3723 */
3724 old_idx = cur_idx;
3725 }
3726 return 0;
3727}
3728
3729
3730/* Apply the filter to the status line in the response buffer <rtr>.
3731 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3732 * or -1 if a replacement resulted in an invalid status line.
3733 */
3734int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3735{
3736 char term;
3737 char *cur_ptr, *cur_end;
3738 int done;
3739 struct http_txn *txn = &t->txn;
3740 int len, delta;
3741
3742
Willy Tarreau3d300592007-03-18 18:34:41 +01003743 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003744 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003745 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003746 (exp->action == ACT_ALLOW ||
3747 exp->action == ACT_DENY))
3748 return 0;
3749 else if (exp->action == ACT_REMOVE)
3750 return 0;
3751
3752 done = 0;
3753
Willy Tarreau9cdde232007-05-02 20:58:19 +02003754 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003755 cur_end = cur_ptr + txn->rsp.sl.rq.l;
3756
3757 /* Now we have the status line between cur_ptr and cur_end */
3758
3759 /* The annoying part is that pattern matching needs
3760 * that we modify the contents to null-terminate all
3761 * strings before testing them.
3762 */
3763
3764 term = *cur_end;
3765 *cur_end = '\0';
3766
3767 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3768 switch (exp->action) {
3769 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003770 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003771 done = 1;
3772 break;
3773
3774 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003775 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003776 done = 1;
3777 break;
3778
3779 case ACT_REPLACE:
3780 *cur_end = term; /* restore the string terminator */
3781 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3782 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3783 /* FIXME: if the user adds a newline in the replacement, the
3784 * index will not be recalculated for now, and the new line
3785 * will not be counted as a new header.
3786 */
3787
3788 txn->rsp.eoh += delta;
3789 cur_end += delta;
3790
Willy Tarreau9cdde232007-05-02 20:58:19 +02003791 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003792 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02003793 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003794 cur_ptr, cur_end + 1,
3795 NULL, NULL);
3796 if (unlikely(!cur_end))
3797 return -1;
3798
3799 /* we have a full respnse and we know that we have either a CR
3800 * or an LF at <ptr>.
3801 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003802 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003803 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
3804 /* there is no point trying this regex on headers */
3805 return 1;
3806 }
3807 }
3808 *cur_end = term; /* restore the string terminator */
3809 return done;
3810}
3811
3812
3813
3814/*
3815 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
3816 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3817 * unparsable response.
3818 */
3819int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3820{
Willy Tarreau3d300592007-03-18 18:34:41 +01003821 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003822 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003823 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003824 int ret;
3825
3826 /*
3827 * The interleaving of transformations and verdicts
3828 * makes it difficult to decide to continue or stop
3829 * the evaluation.
3830 */
3831
Willy Tarreau3d300592007-03-18 18:34:41 +01003832 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003833 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3834 exp->action == ACT_PASS)) {
3835 exp = exp->next;
3836 continue;
3837 }
3838
3839 /* Apply the filter to the status line. */
3840 ret = apply_filter_to_sts_line(t, rtr, exp);
3841 if (unlikely(ret < 0))
3842 return -1;
3843
3844 if (likely(ret == 0)) {
3845 /* The filter did not match the response, it can be
3846 * iterated through all headers.
3847 */
3848 apply_filter_to_resp_headers(t, rtr, exp);
3849 }
3850 exp = exp->next;
3851 }
3852 return 0;
3853}
3854
3855
3856
3857/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003858 * Manage server-side cookies. It can impact performance by about 2% so it is
3859 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003860 */
3861void manage_server_side_cookies(struct session *t, struct buffer *rtr)
3862{
3863 struct http_txn *txn = &t->txn;
3864 char *p1, *p2, *p3, *p4;
3865
3866 appsess *asession_temp = NULL;
3867 appsess local_asession;
3868
3869 char *cur_ptr, *cur_end, *cur_next;
3870 int cur_idx, old_idx, delta;
3871
Willy Tarreaua15645d2007-03-18 16:22:39 +01003872 /* Iterate through the headers.
3873 * we start with the start line.
3874 */
3875 old_idx = 0;
3876 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3877
3878 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3879 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003880 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003881
3882 cur_hdr = &txn->hdr_idx.v[cur_idx];
3883 cur_ptr = cur_next;
3884 cur_end = cur_ptr + cur_hdr->len;
3885 cur_next = cur_end + cur_hdr->cr + 1;
3886
3887 /* We have one full header between cur_ptr and cur_end, and the
3888 * next header starts at cur_next. We're only interested in
3889 * "Cookie:" headers.
3890 */
3891
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003892 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
3893 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003894 old_idx = cur_idx;
3895 continue;
3896 }
3897
3898 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01003899 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003900
3901
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003902 /* maybe we only wanted to see if there was a set-cookie. Note that
3903 * the cookie capture is declared in the fronend.
3904 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003905 if (t->be->cookie_name == NULL &&
3906 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003907 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003908 return;
3909
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003910 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003911
3912 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003913 if (p1 == cur_end || *p1 == ';') /* end of cookie */
3914 break;
3915
3916 /* p1 is at the beginning of the cookie name */
3917 p2 = p1;
3918
3919 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
3920 p2++;
3921
3922 if (p2 == cur_end || *p2 == ';') /* next cookie */
3923 break;
3924
3925 p3 = p2 + 1; /* skip the '=' sign */
3926 if (p3 == cur_end)
3927 break;
3928
3929 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003930 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01003931 p4++;
3932
3933 /* here, we have the cookie name between p1 and p2,
3934 * and its value between p3 and p4.
3935 * we can process it.
3936 */
3937
3938 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003939 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003940 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003941 (p4 - p1 >= t->fe->capture_namelen) &&
3942 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003943 int log_len = p4 - p1;
3944
Willy Tarreau086b3b42007-05-13 21:45:51 +02003945 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003946 Alert("HTTP logging : out of memory.\n");
3947 }
3948
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003949 if (log_len > t->fe->capture_len)
3950 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003951 memcpy(txn->srv_cookie, p1, log_len);
3952 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003953 }
3954
3955 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003956 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3957 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003958 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01003959 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003960
3961 /* If the cookie is in insert mode on a known server, we'll delete
3962 * this occurrence because we'll insert another one later.
3963 * We'll delete it too if the "indirect" option is set and we're in
3964 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003965 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
3966 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003967 /* this header must be deleted */
3968 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3969 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3970 txn->hdr_idx.used--;
3971 cur_hdr->len = 0;
3972 cur_next += delta;
3973 txn->rsp.eoh += delta;
3974
Willy Tarreau3d300592007-03-18 18:34:41 +01003975 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003976 }
3977 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003978 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003979 /* replace bytes p3->p4 with the cookie name associated
3980 * with this server since we know it.
3981 */
3982 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
3983 cur_hdr->len += delta;
3984 cur_next += delta;
3985 txn->rsp.eoh += delta;
3986
Willy Tarreau3d300592007-03-18 18:34:41 +01003987 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003988 }
3989 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003990 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003991 /* insert the cookie name associated with this server
3992 * before existing cookie, and insert a delimitor between them..
3993 */
3994 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
3995 cur_hdr->len += delta;
3996 cur_next += delta;
3997 txn->rsp.eoh += delta;
3998
3999 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004000 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004001 }
4002 }
4003 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004004 else if ((t->be->appsession_name != NULL) &&
4005 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004006
4007 /* Cool... it's the right one */
4008
4009 size_t server_id_len = strlen(t->srv->id) + 1;
4010 asession_temp = &local_asession;
4011
Willy Tarreau63963c62007-05-13 21:29:55 +02004012 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004013 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4014 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4015 return;
4016 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004017 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4018 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004019 asession_temp->serverid = NULL;
4020
4021 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004022 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4023 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004024 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004025 Alert("Not enough Memory process_srv():asession:calloc().\n");
4026 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4027 return;
4028 }
4029 asession_temp->sessid = local_asession.sessid;
4030 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004031 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004032 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4033 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004034 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004035 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004036 }
4037
Willy Tarreaua15645d2007-03-18 16:22:39 +01004038 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004039 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004040 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4041 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4042 return;
4043 }
4044 asession_temp->serverid[0] = '\0';
4045 }
4046
4047 if (asession_temp->serverid[0] == '\0')
4048 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4049
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004050 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004051 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004052#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004053 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004054 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004055#endif
4056 }/* end if ((t->proxy->appsession_name != NULL) ... */
4057 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4058 } /* we're now at the end of the cookie value */
4059
4060 /* keep the link from this header to next one */
4061 old_idx = cur_idx;
4062 } /* end of cookie processing on this header */
4063}
4064
4065
4066
4067/*
4068 * Check if response is cacheable or not. Updates t->flags.
4069 */
4070void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4071{
4072 struct http_txn *txn = &t->txn;
4073 char *p1, *p2;
4074
4075 char *cur_ptr, *cur_end, *cur_next;
4076 int cur_idx;
4077
Willy Tarreau5df51872007-11-25 16:20:08 +01004078 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004079 return;
4080
4081 /* Iterate through the headers.
4082 * we start with the start line.
4083 */
4084 cur_idx = 0;
4085 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4086
4087 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4088 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004089 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004090
4091 cur_hdr = &txn->hdr_idx.v[cur_idx];
4092 cur_ptr = cur_next;
4093 cur_end = cur_ptr + cur_hdr->len;
4094 cur_next = cur_end + cur_hdr->cr + 1;
4095
4096 /* We have one full header between cur_ptr and cur_end, and the
4097 * next header starts at cur_next. We're only interested in
4098 * "Cookie:" headers.
4099 */
4100
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004101 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4102 if (val) {
4103 if ((cur_end - (cur_ptr + val) >= 8) &&
4104 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4105 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4106 return;
4107 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004108 }
4109
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004110 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4111 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004112 continue;
4113
4114 /* OK, right now we know we have a cache-control header at cur_ptr */
4115
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004116 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004117
4118 if (p1 >= cur_end) /* no more info */
4119 continue;
4120
4121 /* p1 is at the beginning of the value */
4122 p2 = p1;
4123
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004124 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004125 p2++;
4126
4127 /* we have a complete value between p1 and p2 */
4128 if (p2 < cur_end && *p2 == '=') {
4129 /* we have something of the form no-cache="set-cookie" */
4130 if ((cur_end - p1 >= 21) &&
4131 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4132 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004133 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004134 continue;
4135 }
4136
4137 /* OK, so we know that either p2 points to the end of string or to a comma */
4138 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4139 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4140 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4141 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004142 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004143 return;
4144 }
4145
4146 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004147 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004148 continue;
4149 }
4150 }
4151}
4152
4153
Willy Tarreau58f10d72006-12-04 02:26:12 +01004154/*
4155 * Try to retrieve a known appsession in the URI, then the associated server.
4156 * If the server is found, it's assigned to the session.
4157 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004158void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004159{
Willy Tarreau3d300592007-03-18 18:34:41 +01004160 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004161 appsess *asession_temp = NULL;
4162 appsess local_asession;
4163 char *request_line;
4164
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004165 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004166 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004167 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004168 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004169 return;
4170
4171 /* skip ';' */
4172 request_line++;
4173
4174 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004175 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004176 return;
4177
4178 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004179 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004180
4181 /* First try if we already have an appsession */
4182 asession_temp = &local_asession;
4183
Willy Tarreau63963c62007-05-13 21:29:55 +02004184 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004185 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4186 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4187 return;
4188 }
4189
4190 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004191 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4192 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004193 asession_temp->serverid = NULL;
4194
4195 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004196 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4197 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004198 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004199 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004200 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004201 Alert("Not enough memory process_cli():asession:calloc().\n");
4202 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4203 return;
4204 }
4205 asession_temp->sessid = local_asession.sessid;
4206 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004207 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004208 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004209 }
4210 else {
4211 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004212 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004213 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004214
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004215 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004216 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004217
Willy Tarreau58f10d72006-12-04 02:26:12 +01004218#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004219 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004220 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004221#endif
4222 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004223 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004224 Alert("Found Application Session without matching server.\n");
4225 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004226 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004227 while (srv) {
4228 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004229 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004230 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004231 txn->flags &= ~TX_CK_MASK;
4232 txn->flags |= TX_CK_VALID;
4233 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004234 t->srv = srv;
4235 break;
4236 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004237 txn->flags &= ~TX_CK_MASK;
4238 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004239 }
4240 }
4241 srv = srv->next;
4242 }
4243 }
4244}
4245
4246
Willy Tarreaub2513902006-12-17 14:52:38 +01004247/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004248 * In a GET or HEAD request, check if the requested URI matches the stats uri
4249 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004250 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004251 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004252 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004253 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004254 *
4255 * Returns 1 if the session's state changes, otherwise 0.
4256 */
4257int stats_check_uri_auth(struct session *t, struct proxy *backend)
4258{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004259 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004260 struct uri_auth *uri_auth = backend->uri_auth;
4261 struct user_auth *user;
4262 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004263 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004264
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004265 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
4266
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004267 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004268 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004269 return 0;
4270
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004271 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004272
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004273 /* the URI is in h */
4274 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004275 return 0;
4276
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004277 h += uri_auth->uri_len;
4278 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4279 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004280 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004281 break;
4282 }
4283 h++;
4284 }
4285
4286 if (uri_auth->refresh) {
4287 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4288 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4289 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004290 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004291 break;
4292 }
4293 h++;
4294 }
4295 }
4296
Willy Tarreau55bb8452007-10-17 18:44:57 +02004297 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4298 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4299 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004300 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02004301 break;
4302 }
4303 h++;
4304 }
4305
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004306 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
4307
Willy Tarreaub2513902006-12-17 14:52:38 +01004308 /* we are in front of a interceptable URI. Let's check
4309 * if there's an authentication and if it's valid.
4310 */
4311 user = uri_auth->users;
4312 if (!user) {
4313 /* no user auth required, it's OK */
4314 authenticated = 1;
4315 } else {
4316 authenticated = 0;
4317
4318 /* a user list is defined, we have to check.
4319 * skip 21 chars for "Authorization: Basic ".
4320 */
4321
4322 /* FIXME: this should move to an earlier place */
4323 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004324 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4325 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4326 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004327 if (len > 14 &&
4328 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004329 txn->auth_hdr.str = h;
4330 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004331 break;
4332 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004333 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004334 }
4335
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004336 if (txn->auth_hdr.len < 21 ||
4337 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004338 user = NULL;
4339
4340 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004341 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4342 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004343 user->user_pwd, user->user_len)) {
4344 authenticated = 1;
4345 break;
4346 }
4347 user = user->next;
4348 }
4349 }
4350
4351 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004352 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004353
4354 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004355 msg.str = trash;
4356 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004357 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01004358 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02004359 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau2df28e82008-08-17 15:20:19 +02004360 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004361 if (!(t->flags & SN_ERR_MASK))
4362 t->flags |= SN_ERR_PRXCOND;
4363 if (!(t->flags & SN_FINST_MASK))
4364 t->flags |= SN_FINST_R;
4365 return 1;
4366 }
4367
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004368 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01004369 * data.
4370 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01004371 buffer_write_dis(t->req);
Willy Tarreau72b179a2008-08-28 16:01:32 +02004372 buffer_shutw_now(t->req);
4373 buffer_shutr_now(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02004374 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01004375 t->data_source = DATA_SRC_STATS;
4376 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02004377 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau01bf8672008-12-07 18:03:29 +01004378 buffer_install_hijacker(t, t->rep, produce_content);
Willy Tarreaub2513902006-12-17 14:52:38 +01004379 return 1;
4380}
4381
4382
Willy Tarreaubaaee002006-06-26 02:48:02 +02004383/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004384 * Print a debug line with a header
4385 */
4386void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4387{
4388 int len, max;
4389 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004390 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004391 max = end - start;
4392 UBOUND(max, sizeof(trash) - len - 1);
4393 len += strlcpy2(trash + len, start, max + 1);
4394 trash[len++] = '\n';
4395 write(1, trash, len);
4396}
4397
4398
Willy Tarreau8797c062007-05-07 00:55:35 +02004399/************************************************************************/
4400/* The code below is dedicated to ACL parsing and matching */
4401/************************************************************************/
4402
4403
4404
4405
4406/* 1. Check on METHOD
4407 * We use the pre-parsed method if it is known, and store its number as an
4408 * integer. If it is unknown, we use the pointer and the length.
4409 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004410static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004411{
4412 int len, meth;
4413
Willy Tarreauae8b7962007-06-09 23:10:04 +02004414 len = strlen(*text);
4415 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004416
4417 pattern->val.i = meth;
4418 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004419 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004420 if (!pattern->ptr.str)
4421 return 0;
4422 pattern->len = len;
4423 }
4424 return 1;
4425}
4426
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004427static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004428acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4429 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004430{
4431 int meth;
4432 struct http_txn *txn = l7;
4433
Willy Tarreaub6866442008-07-14 23:54:42 +02004434 if (!txn)
4435 return 0;
4436
Willy Tarreauc11416f2007-06-17 16:58:38 +02004437 if (txn->req.msg_state != HTTP_MSG_BODY)
4438 return 0;
4439
Willy Tarreau8797c062007-05-07 00:55:35 +02004440 meth = txn->meth;
4441 test->i = meth;
4442 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004443 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4444 /* ensure the indexes are not affected */
4445 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004446 test->len = txn->req.sl.rq.m_l;
4447 test->ptr = txn->req.sol;
4448 }
4449 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4450 return 1;
4451}
4452
4453static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4454{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004455 int icase;
4456
Willy Tarreau8797c062007-05-07 00:55:35 +02004457 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02004458 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02004459
4460 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02004461 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004462
4463 /* Other method, we must compare the strings */
4464 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02004465 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004466
4467 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4468 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4469 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02004470 return ACL_PAT_FAIL;
4471 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004472}
4473
4474/* 2. Check on Request/Status Version
4475 * We simply compare strings here.
4476 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004477static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004478{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004479 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004480 if (!pattern->ptr.str)
4481 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004482 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004483 return 1;
4484}
4485
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004486static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004487acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4488 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004489{
4490 struct http_txn *txn = l7;
4491 char *ptr;
4492 int len;
4493
Willy Tarreaub6866442008-07-14 23:54:42 +02004494 if (!txn)
4495 return 0;
4496
Willy Tarreauc11416f2007-06-17 16:58:38 +02004497 if (txn->req.msg_state != HTTP_MSG_BODY)
4498 return 0;
4499
Willy Tarreau8797c062007-05-07 00:55:35 +02004500 len = txn->req.sl.rq.v_l;
4501 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4502
4503 while ((len-- > 0) && (*ptr++ != '/'));
4504 if (len <= 0)
4505 return 0;
4506
4507 test->ptr = ptr;
4508 test->len = len;
4509
4510 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4511 return 1;
4512}
4513
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004514static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004515acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4516 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004517{
4518 struct http_txn *txn = l7;
4519 char *ptr;
4520 int len;
4521
Willy Tarreaub6866442008-07-14 23:54:42 +02004522 if (!txn)
4523 return 0;
4524
Willy Tarreauc11416f2007-06-17 16:58:38 +02004525 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4526 return 0;
4527
Willy Tarreau8797c062007-05-07 00:55:35 +02004528 len = txn->rsp.sl.st.v_l;
4529 ptr = txn->rsp.sol;
4530
4531 while ((len-- > 0) && (*ptr++ != '/'));
4532 if (len <= 0)
4533 return 0;
4534
4535 test->ptr = ptr;
4536 test->len = len;
4537
4538 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4539 return 1;
4540}
4541
4542/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004543static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004544acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4545 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004546{
4547 struct http_txn *txn = l7;
4548 char *ptr;
4549 int len;
4550
Willy Tarreaub6866442008-07-14 23:54:42 +02004551 if (!txn)
4552 return 0;
4553
Willy Tarreauc11416f2007-06-17 16:58:38 +02004554 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4555 return 0;
4556
Willy Tarreau8797c062007-05-07 00:55:35 +02004557 len = txn->rsp.sl.st.c_l;
4558 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4559
4560 test->i = __strl2ui(ptr, len);
4561 test->flags = ACL_TEST_F_VOL_1ST;
4562 return 1;
4563}
4564
4565/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004566static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004567acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4568 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004569{
4570 struct http_txn *txn = l7;
4571
Willy Tarreaub6866442008-07-14 23:54:42 +02004572 if (!txn)
4573 return 0;
4574
Willy Tarreauc11416f2007-06-17 16:58:38 +02004575 if (txn->req.msg_state != HTTP_MSG_BODY)
4576 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004577
Willy Tarreauc11416f2007-06-17 16:58:38 +02004578 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4579 /* ensure the indexes are not affected */
4580 return 0;
4581
Willy Tarreau8797c062007-05-07 00:55:35 +02004582 test->len = txn->req.sl.rq.u_l;
4583 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4584
Willy Tarreauf3d25982007-05-08 22:45:09 +02004585 /* we do not need to set READ_ONLY because the data is in a buffer */
4586 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004587 return 1;
4588}
4589
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004590static int
4591acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
4592 struct acl_expr *expr, struct acl_test *test)
4593{
4594 struct http_txn *txn = l7;
4595
Willy Tarreaub6866442008-07-14 23:54:42 +02004596 if (!txn)
4597 return 0;
4598
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004599 if (txn->req.msg_state != HTTP_MSG_BODY)
4600 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004601
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004602 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4603 /* ensure the indexes are not affected */
4604 return 0;
4605
4606 /* Parse HTTP request */
4607 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4608 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
4609 test->i = AF_INET;
4610
4611 /*
4612 * If we are parsing url in frontend space, we prepare backend stage
4613 * to not parse again the same url ! optimization lazyness...
4614 */
4615 if (px->options & PR_O_HTTP_PROXY)
4616 l4->flags |= SN_ADDR_SET;
4617
4618 test->flags = ACL_TEST_F_READ_ONLY;
4619 return 1;
4620}
4621
4622static int
4623acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
4624 struct acl_expr *expr, struct acl_test *test)
4625{
4626 struct http_txn *txn = l7;
4627
Willy Tarreaub6866442008-07-14 23:54:42 +02004628 if (!txn)
4629 return 0;
4630
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004631 if (txn->req.msg_state != HTTP_MSG_BODY)
4632 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004633
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004634 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4635 /* ensure the indexes are not affected */
4636 return 0;
4637
4638 /* Same optimization as url_ip */
4639 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4640 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
4641
4642 if (px->options & PR_O_HTTP_PROXY)
4643 l4->flags |= SN_ADDR_SET;
4644
4645 test->flags = ACL_TEST_F_READ_ONLY;
4646 return 1;
4647}
4648
Willy Tarreauc11416f2007-06-17 16:58:38 +02004649/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
4650 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
4651 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02004652static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004653acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004654 struct acl_expr *expr, struct acl_test *test)
4655{
4656 struct http_txn *txn = l7;
4657 struct hdr_idx *idx = &txn->hdr_idx;
4658 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004659
Willy Tarreaub6866442008-07-14 23:54:42 +02004660 if (!txn)
4661 return 0;
4662
Willy Tarreau33a7e692007-06-10 19:45:56 +02004663 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4664 /* search for header from the beginning */
4665 ctx->idx = 0;
4666
Willy Tarreau33a7e692007-06-10 19:45:56 +02004667 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4668 test->flags |= ACL_TEST_F_FETCH_MORE;
4669 test->flags |= ACL_TEST_F_VOL_HDR;
4670 test->len = ctx->vlen;
4671 test->ptr = (char *)ctx->line + ctx->val;
4672 return 1;
4673 }
4674
4675 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4676 test->flags |= ACL_TEST_F_VOL_HDR;
4677 return 0;
4678}
4679
Willy Tarreau33a7e692007-06-10 19:45:56 +02004680static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004681acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
4682 struct acl_expr *expr, struct acl_test *test)
4683{
4684 struct http_txn *txn = l7;
4685
Willy Tarreaub6866442008-07-14 23:54:42 +02004686 if (!txn)
4687 return 0;
4688
Willy Tarreauc11416f2007-06-17 16:58:38 +02004689 if (txn->req.msg_state != HTTP_MSG_BODY)
4690 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004691
Willy Tarreauc11416f2007-06-17 16:58:38 +02004692 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4693 /* ensure the indexes are not affected */
4694 return 0;
4695
4696 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
4697}
4698
4699static int
4700acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
4701 struct acl_expr *expr, struct acl_test *test)
4702{
4703 struct http_txn *txn = l7;
4704
Willy Tarreaub6866442008-07-14 23:54:42 +02004705 if (!txn)
4706 return 0;
4707
Willy Tarreauc11416f2007-06-17 16:58:38 +02004708 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4709 return 0;
4710
4711 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
4712}
4713
4714/* 6. Check on HTTP header count. The number of occurrences is returned.
4715 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
4716 */
4717static int
4718acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004719 struct acl_expr *expr, struct acl_test *test)
4720{
4721 struct http_txn *txn = l7;
4722 struct hdr_idx *idx = &txn->hdr_idx;
4723 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004724 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02004725
Willy Tarreaub6866442008-07-14 23:54:42 +02004726 if (!txn)
4727 return 0;
4728
Willy Tarreau33a7e692007-06-10 19:45:56 +02004729 ctx.idx = 0;
4730 cnt = 0;
4731 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
4732 cnt++;
4733
4734 test->i = cnt;
4735 test->flags = ACL_TEST_F_VOL_HDR;
4736 return 1;
4737}
4738
Willy Tarreauc11416f2007-06-17 16:58:38 +02004739static int
4740acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4741 struct acl_expr *expr, struct acl_test *test)
4742{
4743 struct http_txn *txn = l7;
4744
Willy Tarreaub6866442008-07-14 23:54:42 +02004745 if (!txn)
4746 return 0;
4747
Willy Tarreauc11416f2007-06-17 16:58:38 +02004748 if (txn->req.msg_state != HTTP_MSG_BODY)
4749 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004750
Willy Tarreauc11416f2007-06-17 16:58:38 +02004751 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4752 /* ensure the indexes are not affected */
4753 return 0;
4754
4755 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
4756}
4757
4758static int
4759acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4760 struct acl_expr *expr, struct acl_test *test)
4761{
4762 struct http_txn *txn = l7;
4763
Willy Tarreaub6866442008-07-14 23:54:42 +02004764 if (!txn)
4765 return 0;
4766
Willy Tarreauc11416f2007-06-17 16:58:38 +02004767 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4768 return 0;
4769
4770 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
4771}
4772
Willy Tarreau33a7e692007-06-10 19:45:56 +02004773/* 7. Check on HTTP header's integer value. The integer value is returned.
4774 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02004775 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02004776 */
4777static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004778acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004779 struct acl_expr *expr, struct acl_test *test)
4780{
4781 struct http_txn *txn = l7;
4782 struct hdr_idx *idx = &txn->hdr_idx;
4783 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004784
Willy Tarreaub6866442008-07-14 23:54:42 +02004785 if (!txn)
4786 return 0;
4787
Willy Tarreau33a7e692007-06-10 19:45:56 +02004788 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4789 /* search for header from the beginning */
4790 ctx->idx = 0;
4791
Willy Tarreau33a7e692007-06-10 19:45:56 +02004792 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4793 test->flags |= ACL_TEST_F_FETCH_MORE;
4794 test->flags |= ACL_TEST_F_VOL_HDR;
4795 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
4796 return 1;
4797 }
4798
4799 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4800 test->flags |= ACL_TEST_F_VOL_HDR;
4801 return 0;
4802}
4803
Willy Tarreauc11416f2007-06-17 16:58:38 +02004804static int
4805acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4806 struct acl_expr *expr, struct acl_test *test)
4807{
4808 struct http_txn *txn = l7;
4809
Willy Tarreaub6866442008-07-14 23:54:42 +02004810 if (!txn)
4811 return 0;
4812
Willy Tarreauc11416f2007-06-17 16:58:38 +02004813 if (txn->req.msg_state != HTTP_MSG_BODY)
4814 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004815
Willy Tarreauc11416f2007-06-17 16:58:38 +02004816 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4817 /* ensure the indexes are not affected */
4818 return 0;
4819
4820 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
4821}
4822
4823static int
4824acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4825 struct acl_expr *expr, struct acl_test *test)
4826{
4827 struct http_txn *txn = l7;
4828
Willy Tarreaub6866442008-07-14 23:54:42 +02004829 if (!txn)
4830 return 0;
4831
Willy Tarreauc11416f2007-06-17 16:58:38 +02004832 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4833 return 0;
4834
4835 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
4836}
4837
Willy Tarreau737b0c12007-06-10 21:28:46 +02004838/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
4839 * the first '/' after the possible hostname, and ends before the possible '?'.
4840 */
4841static int
4842acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
4843 struct acl_expr *expr, struct acl_test *test)
4844{
4845 struct http_txn *txn = l7;
4846 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004847
Willy Tarreaub6866442008-07-14 23:54:42 +02004848 if (!txn)
4849 return 0;
4850
Willy Tarreauc11416f2007-06-17 16:58:38 +02004851 if (txn->req.msg_state != HTTP_MSG_BODY)
4852 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004853
Willy Tarreauc11416f2007-06-17 16:58:38 +02004854 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4855 /* ensure the indexes are not affected */
4856 return 0;
4857
Willy Tarreau21d2af32008-02-14 20:25:24 +01004858 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4859 ptr = http_get_path(txn);
4860 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02004861 return 0;
4862
4863 /* OK, we got the '/' ! */
4864 test->ptr = ptr;
4865
4866 while (ptr < end && *ptr != '?')
4867 ptr++;
4868
4869 test->len = ptr - test->ptr;
4870
4871 /* we do not need to set READ_ONLY because the data is in a buffer */
4872 test->flags = ACL_TEST_F_VOL_1ST;
4873 return 1;
4874}
4875
4876
Willy Tarreau8797c062007-05-07 00:55:35 +02004877
4878/************************************************************************/
4879/* All supported keywords must be declared here. */
4880/************************************************************************/
4881
4882/* Note: must not be declared <const> as its list will be overwritten */
4883static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004884 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
4885 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4886 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
4887 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02004888
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004889 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4890 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4891 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4892 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4893 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4894 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
4895 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4896 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
4897 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02004898
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004899 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
4900 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4901 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4902 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4903 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4904 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4905 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4906 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
4907 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
4908 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02004909
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004910 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
4911 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
4912 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
4913 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
4914 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
4915 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
4916 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
4917 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
4918 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02004919
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004920 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4921 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
4922 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
4923 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
4924 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
4925 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
4926 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02004927
Willy Tarreauf3d25982007-05-08 22:45:09 +02004928 { NULL, NULL, NULL, NULL },
4929
4930#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02004931 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
4932 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
4933 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
4934 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
4935 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
4936 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
4937 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
4938
Willy Tarreau8797c062007-05-07 00:55:35 +02004939 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
4940 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
4941 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
4942 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
4943 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
4944 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
4945 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
4946 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
4947
4948 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
4949 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
4950 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
4951 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
4952 { NULL, NULL, NULL, NULL },
4953#endif
4954}};
4955
4956
4957__attribute__((constructor))
4958static void __http_protocol_init(void)
4959{
4960 acl_register_keywords(&acl_kws);
4961}
4962
4963
Willy Tarreau58f10d72006-12-04 02:26:12 +01004964/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004965 * Local variables:
4966 * c-indent-level: 8
4967 * c-basic-offset: 8
4968 * End:
4969 */