blob: 6121a93c165a39a36dcb38ae2a2b5226d047d7b5 [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>
Willy Tarreau91861262007-10-17 17:06:05 +020051#include <proto/senddata.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/session.h>
53#include <proto/task.h>
54
Willy Tarreau6d1a9882007-01-07 02:03:04 +010055#ifdef CONFIG_HAP_TCPSPLICE
56#include <libtcpsplice.h>
57#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau58f10d72006-12-04 02:26:12 +010059#define DEBUG_PARSE_NO_SPEEDUP
60#undef DEBUG_PARSE_NO_SPEEDUP
61
Willy Tarreau976f1ee2006-12-17 10:06:03 +010062/* This is used to perform a quick jump as an alternative to a break/continue
63 * instruction. The first argument is the label for normal operation, and the
64 * second one is the break/continue instruction in the no_speedup mode.
65 */
66
67#ifdef DEBUG_PARSE_NO_SPEEDUP
68#define QUICK_JUMP(x,y) y
69#else
70#define QUICK_JUMP(x,y) goto x
71#endif
72
Willy Tarreau1c47f852006-07-09 08:22:27 +020073/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010074const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020075 "HTTP/1.0 200 OK\r\n"
76 "Cache-Control: no-cache\r\n"
77 "Connection: close\r\n"
78 "Content-Type: text/html\r\n"
79 "\r\n"
80 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const struct chunk http_200_chunk = {
83 .str = (char *)&HTTP_200,
84 .len = sizeof(HTTP_200)-1
85};
86
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087const char *HTTP_301 =
88 "HTTP/1.0 301 Moved Permantenly\r\n"
89 "Cache-Control: no-cache\r\n"
90 "Connection: close\r\n"
91 "Location: "; /* not terminated since it will be concatenated with the URL */
92
Willy Tarreau0f772532006-12-23 20:51:41 +010093const char *HTTP_302 =
94 "HTTP/1.0 302 Found\r\n"
95 "Cache-Control: no-cache\r\n"
96 "Connection: close\r\n"
97 "Location: "; /* not terminated since it will be concatenated with the URL */
98
99/* same as 302 except that the browser MUST retry with the GET method */
100const char *HTTP_303 =
101 "HTTP/1.0 303 See Other\r\n"
102 "Cache-Control: no-cache\r\n"
103 "Connection: close\r\n"
104 "Location: "; /* not terminated since it will be concatenated with the URL */
105
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
107const char *HTTP_401_fmt =
108 "HTTP/1.0 401 Unauthorized\r\n"
109 "Cache-Control: no-cache\r\n"
110 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200111 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
113 "\r\n"
114 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
115
Willy Tarreau0f772532006-12-23 20:51:41 +0100116
117const int http_err_codes[HTTP_ERR_SIZE] = {
118 [HTTP_ERR_400] = 400,
119 [HTTP_ERR_403] = 403,
120 [HTTP_ERR_408] = 408,
121 [HTTP_ERR_500] = 500,
122 [HTTP_ERR_502] = 502,
123 [HTTP_ERR_503] = 503,
124 [HTTP_ERR_504] = 504,
125};
126
Willy Tarreau80587432006-12-24 17:47:20 +0100127static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100128 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100129 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100130 "Cache-Control: no-cache\r\n"
131 "Connection: close\r\n"
132 "Content-Type: text/html\r\n"
133 "\r\n"
134 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
135
136 [HTTP_ERR_403] =
137 "HTTP/1.0 403 Forbidden\r\n"
138 "Cache-Control: no-cache\r\n"
139 "Connection: close\r\n"
140 "Content-Type: text/html\r\n"
141 "\r\n"
142 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
143
144 [HTTP_ERR_408] =
145 "HTTP/1.0 408 Request Time-out\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
151
152 [HTTP_ERR_500] =
153 "HTTP/1.0 500 Server Error\r\n"
154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
159
160 [HTTP_ERR_502] =
161 "HTTP/1.0 502 Bad Gateway\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
167
168 [HTTP_ERR_503] =
169 "HTTP/1.0 503 Service Unavailable\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
175
176 [HTTP_ERR_504] =
177 "HTTP/1.0 504 Gateway Time-out\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
183
184};
185
Willy Tarreau80587432006-12-24 17:47:20 +0100186/* We must put the messages here since GCC cannot initialize consts depending
187 * on strlen().
188 */
189struct chunk http_err_chunks[HTTP_ERR_SIZE];
190
Willy Tarreau42250582007-04-01 01:30:43 +0200191#define FD_SETS_ARE_BITFIELDS
192#ifdef FD_SETS_ARE_BITFIELDS
193/*
194 * This map is used with all the FD_* macros to check whether a particular bit
195 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
196 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
197 * byte should be encoded. Be careful to always pass bytes from 0 to 255
198 * exclusively to the macros.
199 */
200fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
201fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
202
203#else
204#error "Check if your OS uses bitfields for fd_sets"
205#endif
206
Willy Tarreau80587432006-12-24 17:47:20 +0100207void init_proto_http()
208{
Willy Tarreau42250582007-04-01 01:30:43 +0200209 int i;
210 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100211 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200212
Willy Tarreau80587432006-12-24 17:47:20 +0100213 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
214 if (!http_err_msgs[msg]) {
215 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
216 abort();
217 }
218
219 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
220 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
221 }
Willy Tarreau42250582007-04-01 01:30:43 +0200222
223 /* initialize the log header encoding map : '{|}"#' should be encoded with
224 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
225 * URL encoding only requires '"', '#' to be encoded as well as non-
226 * printable characters above.
227 */
228 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
229 memset(url_encode_map, 0, sizeof(url_encode_map));
230 for (i = 0; i < 32; i++) {
231 FD_SET(i, hdr_encode_map);
232 FD_SET(i, url_encode_map);
233 }
234 for (i = 127; i < 256; i++) {
235 FD_SET(i, hdr_encode_map);
236 FD_SET(i, url_encode_map);
237 }
238
239 tmp = "\"#{|}";
240 while (*tmp) {
241 FD_SET(*tmp, hdr_encode_map);
242 tmp++;
243 }
244
245 tmp = "\"#";
246 while (*tmp) {
247 FD_SET(*tmp, url_encode_map);
248 tmp++;
249 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200250
251 /* memory allocations */
252 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200253 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100254}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255
Willy Tarreau53b6c742006-12-17 13:37:46 +0100256/*
257 * We have 26 list of methods (1 per first letter), each of which can have
258 * up to 3 entries (2 valid, 1 null).
259 */
260struct http_method_desc {
261 http_meth_t meth;
262 int len;
263 const char text[8];
264};
265
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100266const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100267 ['C' - 'A'] = {
268 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
269 },
270 ['D' - 'A'] = {
271 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
272 },
273 ['G' - 'A'] = {
274 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
275 },
276 ['H' - 'A'] = {
277 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
278 },
279 ['P' - 'A'] = {
280 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
281 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
282 },
283 ['T' - 'A'] = {
284 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
285 },
286 /* rest is empty like this :
287 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
288 */
289};
290
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100291/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200292 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100293 * RFC2616 for those chars.
294 */
295
296const char http_is_spht[256] = {
297 [' '] = 1, ['\t'] = 1,
298};
299
300const char http_is_crlf[256] = {
301 ['\r'] = 1, ['\n'] = 1,
302};
303
304const char http_is_lws[256] = {
305 [' '] = 1, ['\t'] = 1,
306 ['\r'] = 1, ['\n'] = 1,
307};
308
309const char http_is_sep[256] = {
310 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
311 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
312 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
313 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
314 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
315};
316
317const char http_is_ctl[256] = {
318 [0 ... 31] = 1,
319 [127] = 1,
320};
321
322/*
323 * A token is any ASCII char that is neither a separator nor a CTL char.
324 * Do not overwrite values in assignment since gcc-2.95 will not handle
325 * them correctly. Instead, define every non-CTL char's status.
326 */
327const char http_is_token[256] = {
328 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
329 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
330 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
331 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
332 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
333 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
334 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
335 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
336 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
337 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
338 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
339 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
340 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
341 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
342 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
343 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
344 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
345 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
346 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
347 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
348 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
349 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
350 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
351 ['|'] = 1, ['}'] = 0, ['~'] = 1,
352};
353
354
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100355/*
356 * An http ver_token is any ASCII which can be found in an HTTP version,
357 * which includes 'H', 'T', 'P', '/', '.' and any digit.
358 */
359const char http_is_ver_token[256] = {
360 ['.'] = 1, ['/'] = 1,
361 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
362 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
363 ['H'] = 1, ['P'] = 1, ['T'] = 1,
364};
365
366
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367#ifdef DEBUG_FULL
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200368static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" };
Willy Tarreauf5483bf2008-08-14 18:35:40 +0200369static char *srv_stnames[6] = { "IDL", "CON", "DAT", "SHR", "SHW", "CLS" };
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370#endif
371
Willy Tarreau42250582007-04-01 01:30:43 +0200372static void http_sess_log(struct session *s);
373
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100374/*
375 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
376 * CRLF. Text length is measured first, so it cannot be NULL.
377 * The header is also automatically added to the index <hdr_idx>, and the end
378 * of headers is automatically adjusted. The number of bytes added is returned
379 * on success, otherwise <0 is returned indicating an error.
380 */
381int http_header_add_tail(struct buffer *b, struct http_msg *msg,
382 struct hdr_idx *hdr_idx, const char *text)
383{
384 int bytes, len;
385
386 len = strlen(text);
387 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
388 if (!bytes)
389 return -1;
390 msg->eoh += bytes;
391 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
392}
393
394/*
395 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
396 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
397 * the buffer is only opened and the space reserved, but nothing is copied.
398 * The header is also automatically added to the index <hdr_idx>, and the end
399 * of headers is automatically adjusted. The number of bytes added is returned
400 * on success, otherwise <0 is returned indicating an error.
401 */
402int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
403 struct hdr_idx *hdr_idx, const char *text, int len)
404{
405 int bytes;
406
407 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
408 if (!bytes)
409 return -1;
410 msg->eoh += bytes;
411 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
412}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
414/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100415 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
416 * If so, returns the position of the first non-space character relative to
417 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
418 * to return a pointer to the place after the first space. Returns 0 if the
419 * header name does not match. Checks are case-insensitive.
420 */
421int http_header_match2(const char *hdr, const char *end,
422 const char *name, int len)
423{
424 const char *val;
425
426 if (hdr + len >= end)
427 return 0;
428 if (hdr[len] != ':')
429 return 0;
430 if (strncasecmp(hdr, name, len) != 0)
431 return 0;
432 val = hdr + len + 1;
433 while (val < end && HTTP_IS_SPHT(*val))
434 val++;
435 if ((val >= end) && (len + 2 <= end - hdr))
436 return len + 2; /* we may replace starting from second space */
437 return val - hdr;
438}
439
Willy Tarreau33a7e692007-06-10 19:45:56 +0200440/* Find the end of the header value contained between <s> and <e>.
441 * See RFC2616, par 2.2 for more information. Note that it requires
442 * a valid header to return a valid result.
443 */
444const char *find_hdr_value_end(const char *s, const char *e)
445{
446 int quoted, qdpair;
447
448 quoted = qdpair = 0;
449 for (; s < e; s++) {
450 if (qdpair) qdpair = 0;
451 else if (quoted && *s == '\\') qdpair = 1;
452 else if (quoted && *s == '"') quoted = 0;
453 else if (*s == '"') quoted = 1;
454 else if (*s == ',') return s;
455 }
456 return s;
457}
458
459/* Find the first or next occurrence of header <name> in message buffer <sol>
460 * using headers index <idx>, and return it in the <ctx> structure. This
461 * structure holds everything necessary to use the header and find next
462 * occurrence. If its <idx> member is 0, the header is searched from the
463 * beginning. Otherwise, the next occurrence is returned. The function returns
464 * 1 when it finds a value, and 0 when there is no more.
465 */
466int http_find_header2(const char *name, int len,
467 const char *sol, struct hdr_idx *idx,
468 struct hdr_ctx *ctx)
469{
470 __label__ return_hdr, next_hdr;
471 const char *eol, *sov;
472 int cur_idx;
473
474 if (ctx->idx) {
475 /* We have previously returned a value, let's search
476 * another one on the same line.
477 */
478 cur_idx = ctx->idx;
479 sol = ctx->line;
480 sov = sol + ctx->val + ctx->vlen;
481 eol = sol + idx->v[cur_idx].len;
482
483 if (sov >= eol)
484 /* no more values in this header */
485 goto next_hdr;
486
487 /* values remaining for this header, skip the comma */
488 sov++;
489 while (sov < eol && http_is_lws[(unsigned char)*sov])
490 sov++;
491
492 goto return_hdr;
493 }
494
495 /* first request for this header */
496 sol += hdr_idx_first_pos(idx);
497 cur_idx = hdr_idx_first_idx(idx);
498
499 while (cur_idx) {
500 eol = sol + idx->v[cur_idx].len;
501
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200502 if (len == 0) {
503 /* No argument was passed, we want any header.
504 * To achieve this, we simply build a fake request. */
505 while (sol + len < eol && sol[len] != ':')
506 len++;
507 name = sol;
508 }
509
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 if ((len < eol - sol) &&
511 (sol[len] == ':') &&
512 (strncasecmp(sol, name, len) == 0)) {
513
514 sov = sol + len + 1;
515 while (sov < eol && http_is_lws[(unsigned char)*sov])
516 sov++;
517 return_hdr:
518 ctx->line = sol;
519 ctx->idx = cur_idx;
520 ctx->val = sov - sol;
521
522 eol = find_hdr_value_end(sov, eol);
523 ctx->vlen = eol - sov;
524 return 1;
525 }
526 next_hdr:
527 sol = eol + idx->v[cur_idx].cr + 1;
528 cur_idx = idx->v[cur_idx].next;
529 }
530 return 0;
531}
532
533int http_find_header(const char *name,
534 const char *sol, struct hdr_idx *idx,
535 struct hdr_ctx *ctx)
536{
537 return http_find_header2(name, strlen(name), sol, idx, ctx);
538}
539
Willy Tarreaubaaee002006-06-26 02:48:02 +0200540/* This function turns the server state into the SV_STCLOSE, and sets
Willy Tarreau0f772532006-12-23 20:51:41 +0100541 * indicators accordingly. Note that if <status> is 0, or if the message
542 * pointer is NULL, then no message is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200543 */
544void srv_close_with_err(struct session *t, int err, int finst,
Willy Tarreau0f772532006-12-23 20:51:41 +0100545 int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546{
547 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +0200548 t->rep->flags |= BF_MAY_FORWARD;
Willy Tarreau89edf5e2008-08-03 17:25:14 +0200549 buffer_shutw_done(t->req);
550 buffer_shutr_done(t->rep);
Willy Tarreau0f772532006-12-23 20:51:41 +0100551 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100552 t->txn.status = status;
Willy Tarreau73de9892006-11-30 11:40:23 +0100553 if (t->fe->mode == PR_MODE_HTTP)
Willy Tarreau0f772532006-12-23 20:51:41 +0100554 client_return(t, msg);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200555 }
556 if (!(t->flags & SN_ERR_MASK))
557 t->flags |= err;
558 if (!(t->flags & SN_FINST_MASK))
559 t->flags |= finst;
560}
561
Willy Tarreau80587432006-12-24 17:47:20 +0100562/* This function returns the appropriate error location for the given session
563 * and message.
564 */
565
566struct chunk *error_message(struct session *s, int msgnum)
567{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200568 if (s->be->errmsg[msgnum].str)
569 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100570 else if (s->fe->errmsg[msgnum].str)
571 return &s->fe->errmsg[msgnum];
572 else
573 return &http_err_chunks[msgnum];
574}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200575
Willy Tarreau53b6c742006-12-17 13:37:46 +0100576/*
577 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
578 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
579 */
580static http_meth_t find_http_meth(const char *str, const int len)
581{
582 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100583 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100584
585 m = ((unsigned)*str - 'A');
586
587 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100588 for (h = http_methods[m]; h->len > 0; h++) {
589 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100590 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100591 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100592 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100593 };
594 return HTTP_METH_OTHER;
595 }
596 return HTTP_METH_NONE;
597
598}
599
Willy Tarreau21d2af32008-02-14 20:25:24 +0100600/* Parse the URI from the given transaction (which is assumed to be in request
601 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
602 * It is returned otherwise.
603 */
604static char *
605http_get_path(struct http_txn *txn)
606{
607 char *ptr, *end;
608
609 ptr = txn->req.sol + txn->req.sl.rq.u;
610 end = ptr + txn->req.sl.rq.u_l;
611
612 if (ptr >= end)
613 return NULL;
614
615 /* RFC2616, par. 5.1.2 :
616 * Request-URI = "*" | absuri | abspath | authority
617 */
618
619 if (*ptr == '*')
620 return NULL;
621
622 if (isalpha((unsigned char)*ptr)) {
623 /* this is a scheme as described by RFC3986, par. 3.1 */
624 ptr++;
625 while (ptr < end &&
626 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
627 ptr++;
628 /* skip '://' */
629 if (ptr == end || *ptr++ != ':')
630 return NULL;
631 if (ptr == end || *ptr++ != '/')
632 return NULL;
633 if (ptr == end || *ptr++ != '/')
634 return NULL;
635 }
636 /* skip [user[:passwd]@]host[:[port]] */
637
638 while (ptr < end && *ptr != '/')
639 ptr++;
640
641 if (ptr == end)
642 return NULL;
643
644 /* OK, we got the '/' ! */
645 return ptr;
646}
647
Willy Tarreaubaaee002006-06-26 02:48:02 +0200648/* Processes the client and server jobs of a session task, then
649 * puts it back to the wait queue in a clean state, or
650 * cleans up its resources if it must be deleted. Returns
651 * the time the task accepts to wait, or TIME_ETERNITY for
652 * infinity.
653 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200654void process_session(struct task *t, int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655{
656 struct session *s = t->context;
657 int fsm_resync = 0;
658
659 do {
660 fsm_resync = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200661
662 //fprintf(stderr,"before_cli:cli=%d, srv=%d, resync=%d\n", s->cli_state, s->srv_state, fsm_resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200663 fsm_resync |= process_cli(s);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200664
665 //fprintf(stderr,"before_req:cli=%d, srv=%d, resync=%d\n", s->cli_state, s->srv_state, fsm_resync);
666 if (s->analysis & AN_REQ_ANY)
667 fsm_resync |= process_request(s);
668
669 //fprintf(stderr,"before_srv:cli=%d, srv=%d, resync=%d\n", s->cli_state, s->srv_state, fsm_resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200670 fsm_resync |= process_srv(s);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200671
Willy Tarreauf5483bf2008-08-14 18:35:40 +0200672 //fprintf(stderr,"before_rep:cli=%d, srv=%d, resync=%d\n", s->cli_state, s->srv_state, fsm_resync);
673 if (s->analysis & AN_RTR_ANY)
674 fsm_resync |= process_response(s);
675
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200676 //fprintf(stderr,"endof_loop:cli=%d, srv=%d, resync=%d\n", s->cli_state, s->srv_state, fsm_resync);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200677 } while (fsm_resync);
678
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200679 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100680
681 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
682 session_process_counters(s);
683
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200684 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
685 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200686
Willy Tarreau7f875f62008-08-11 17:35:01 +0200687 /* Trick: if a request is being waiting for the server to respond,
688 * and if we know the server can timeout, we don't want the timeout
689 * to expire on the client side first, but we're still interested
690 * in passing data from the client to the server (eg: POST). Thus,
691 * we can cancel the client's request timeout if the server's
692 * request timeout is set and the server has not yet sent a response.
693 */
694
695 if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR_STATUS)) == 0 &&
696 (tick_isset(s->req->cex) || tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
697 s->req->rex = TICK_ETERNITY;
698
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200699 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
700 tick_first(s->rep->rex, s->rep->wex));
701 t->expire = tick_first(t->expire, s->req->cex);
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200702 if (s->analysis & AN_REQ_ANY) {
703 if (s->analysis & AN_REQ_INSPECT)
704 t->expire = tick_first(t->expire, s->inspect_exp);
705 else if (s->analysis & AN_REQ_HTTP_HDR)
706 t->expire = tick_first(t->expire, s->txn.exp);
707 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708
709 /* restore t to its place in the task list */
710 task_queue(t);
711
Willy Tarreaud825eef2007-05-12 22:35:00 +0200712 *next = t->expire;
713 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200714 }
715
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100716 s->fe->feconn--;
717 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200718 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200719 actconn--;
720
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200721 if (unlikely((global.mode & MODE_DEBUG) &&
722 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100724 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200725 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100726 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200727 write(1, trash, len);
728 }
729
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200730 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100731 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200732
733 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200734 if (s->logs.logwait &&
735 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200736 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
737 if (s->fe->to_log & LW_REQ)
738 http_sess_log(s);
739 else
740 tcp_sess_log(s);
741 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200742
743 /* the task MUST not be in the run queue anymore */
744 task_delete(t);
745 session_free(s);
746 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200747 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200748}
749
750
Willy Tarreau42250582007-04-01 01:30:43 +0200751extern const char sess_term_cond[8];
752extern const char sess_fin_state[8];
753extern const char *monthname[12];
754const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
755const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
756 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
757 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200758struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200759struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100760
Willy Tarreau42250582007-04-01 01:30:43 +0200761/*
762 * send a log for the session when we have enough info about it.
763 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100764 */
Willy Tarreau42250582007-04-01 01:30:43 +0200765static void http_sess_log(struct session *s)
766{
767 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
768 struct proxy *fe = s->fe;
769 struct proxy *be = s->be;
770 struct proxy *prx_log;
771 struct http_txn *txn = &s->txn;
772 int tolog;
773 char *uri, *h;
774 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200775 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200776 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200777 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200778 int hdr;
779
780 if (fe->logfac1 < 0 && fe->logfac2 < 0)
781 return;
782 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100783
Willy Tarreau42250582007-04-01 01:30:43 +0200784 if (s->cli_addr.ss_family == AF_INET)
785 inet_ntop(AF_INET,
786 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
787 pn, sizeof(pn));
788 else
789 inet_ntop(AF_INET6,
790 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
791 pn, sizeof(pn));
792
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200793 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200794
795 /* FIXME: let's limit ourselves to frontend logging for now. */
796 tolog = fe->to_log;
797
798 h = tmpline;
799 if (fe->to_log & LW_REQHDR &&
800 txn->req.cap &&
801 (h < tmpline + sizeof(tmpline) - 10)) {
802 *(h++) = ' ';
803 *(h++) = '{';
804 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
805 if (hdr)
806 *(h++) = '|';
807 if (txn->req.cap[hdr] != NULL)
808 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
809 '#', hdr_encode_map, txn->req.cap[hdr]);
810 }
811 *(h++) = '}';
812 }
813
814 if (fe->to_log & LW_RSPHDR &&
815 txn->rsp.cap &&
816 (h < tmpline + sizeof(tmpline) - 7)) {
817 *(h++) = ' ';
818 *(h++) = '{';
819 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
820 if (hdr)
821 *(h++) = '|';
822 if (txn->rsp.cap[hdr] != NULL)
823 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
824 '#', hdr_encode_map, txn->rsp.cap[hdr]);
825 }
826 *(h++) = '}';
827 }
828
829 if (h < tmpline + sizeof(tmpline) - 4) {
830 *(h++) = ' ';
831 *(h++) = '"';
832 uri = txn->uri ? txn->uri : "<BADREQ>";
833 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
834 '#', url_encode_map, uri);
835 *(h++) = '"';
836 }
837 *h = '\0';
838
839 svid = (tolog & LW_SVID) ?
840 (s->data_source != DATA_SRC_STATS) ?
841 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
842
Willy Tarreau70089872008-06-13 21:12:51 +0200843 t_request = -1;
844 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
845 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
846
Willy Tarreau42250582007-04-01 01:30:43 +0200847 send_log(prx_log, LOG_INFO,
848 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
849 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100850 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200851 pn,
852 (s->cli_addr.ss_family == AF_INET) ?
853 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
854 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200855 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200856 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200857 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200858 t_request,
859 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200860 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
861 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
862 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
863 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100864 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200865 txn->cli_cookie ? txn->cli_cookie : "-",
866 txn->srv_cookie ? txn->srv_cookie : "-",
867 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
868 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
869 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
870 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
871 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100872 (s->flags & SN_REDISP)?"+":"",
873 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200874 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
875
876 s->logs.logwait = 0;
877}
878
Willy Tarreau117f59e2007-03-04 18:17:17 +0100879
880/*
881 * Capture headers from message starting at <som> according to header list
882 * <cap_hdr>, and fill the <idx> structure appropriately.
883 */
884void capture_headers(char *som, struct hdr_idx *idx,
885 char **cap, struct cap_hdr *cap_hdr)
886{
887 char *eol, *sol, *col, *sov;
888 int cur_idx;
889 struct cap_hdr *h;
890 int len;
891
892 sol = som + hdr_idx_first_pos(idx);
893 cur_idx = hdr_idx_first_idx(idx);
894
895 while (cur_idx) {
896 eol = sol + idx->v[cur_idx].len;
897
898 col = sol;
899 while (col < eol && *col != ':')
900 col++;
901
902 sov = col + 1;
903 while (sov < eol && http_is_lws[(unsigned char)*sov])
904 sov++;
905
906 for (h = cap_hdr; h; h = h->next) {
907 if ((h->namelen == col - sol) &&
908 (strncasecmp(sol, h->name, h->namelen) == 0)) {
909 if (cap[h->index] == NULL)
910 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200911 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100912
913 if (cap[h->index] == NULL) {
914 Alert("HTTP capture : out of memory.\n");
915 continue;
916 }
917
918 len = eol - sov;
919 if (len > h->len)
920 len = h->len;
921
922 memcpy(cap[h->index], sov, len);
923 cap[h->index][len]=0;
924 }
925 }
926 sol = eol + idx->v[cur_idx].cr + 1;
927 cur_idx = idx->v[cur_idx].next;
928 }
929}
930
931
Willy Tarreau42250582007-04-01 01:30:43 +0200932/* either we find an LF at <ptr> or we jump to <bad>.
933 */
934#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
935
936/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
937 * otherwise to <http_msg_ood> with <state> set to <st>.
938 */
939#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
940 ptr++; \
941 if (likely(ptr < end)) \
942 goto good; \
943 else { \
944 state = (st); \
945 goto http_msg_ood; \
946 } \
947 } while (0)
948
949
Willy Tarreaubaaee002006-06-26 02:48:02 +0200950/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100951 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100952 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
953 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
954 * will give undefined results.
955 * Note that it is upon the caller's responsibility to ensure that ptr < end,
956 * and that msg->sol points to the beginning of the response.
957 * If a complete line is found (which implies that at least one CR or LF is
958 * found before <end>, the updated <ptr> is returned, otherwise NULL is
959 * returned indicating an incomplete line (which does not mean that parts have
960 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
961 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
962 * upon next call.
963 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200964 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100965 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
966 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200967 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100968 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100969const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
970 unsigned int state, const char *ptr, const char *end,
971 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100972{
973 __label__
974 http_msg_rpver,
975 http_msg_rpver_sp,
976 http_msg_rpcode,
977 http_msg_rpcode_sp,
978 http_msg_rpreason,
979 http_msg_rpline_eol,
980 http_msg_ood, /* out of data */
981 http_msg_invalid;
982
983 switch (state) {
984 http_msg_rpver:
985 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100986 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100987 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
988
989 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100990 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100991 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
992 }
993 goto http_msg_invalid;
994
995 http_msg_rpver_sp:
996 case HTTP_MSG_RPVER_SP:
997 if (likely(!HTTP_IS_LWS(*ptr))) {
998 msg->sl.st.c = ptr - msg_buf;
999 goto http_msg_rpcode;
1000 }
1001 if (likely(HTTP_IS_SPHT(*ptr)))
1002 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1003 /* so it's a CR/LF, this is invalid */
1004 goto http_msg_invalid;
1005
1006 http_msg_rpcode:
1007 case HTTP_MSG_RPCODE:
1008 if (likely(!HTTP_IS_LWS(*ptr)))
1009 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1010
1011 if (likely(HTTP_IS_SPHT(*ptr))) {
1012 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1013 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1014 }
1015
1016 /* so it's a CR/LF, so there is no reason phrase */
1017 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1018 http_msg_rsp_reason:
1019 /* FIXME: should we support HTTP responses without any reason phrase ? */
1020 msg->sl.st.r = ptr - msg_buf;
1021 msg->sl.st.r_l = 0;
1022 goto http_msg_rpline_eol;
1023
1024 http_msg_rpcode_sp:
1025 case HTTP_MSG_RPCODE_SP:
1026 if (likely(!HTTP_IS_LWS(*ptr))) {
1027 msg->sl.st.r = ptr - msg_buf;
1028 goto http_msg_rpreason;
1029 }
1030 if (likely(HTTP_IS_SPHT(*ptr)))
1031 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1032 /* so it's a CR/LF, so there is no reason phrase */
1033 goto http_msg_rsp_reason;
1034
1035 http_msg_rpreason:
1036 case HTTP_MSG_RPREASON:
1037 if (likely(!HTTP_IS_CRLF(*ptr)))
1038 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1039 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1040 http_msg_rpline_eol:
1041 /* We have seen the end of line. Note that we do not
1042 * necessarily have the \n yet, but at least we know that we
1043 * have EITHER \r OR \n, otherwise the response would not be
1044 * complete. We can then record the response length and return
1045 * to the caller which will be able to register it.
1046 */
1047 msg->sl.st.l = ptr - msg->sol;
1048 return ptr;
1049
1050#ifdef DEBUG_FULL
1051 default:
1052 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1053 exit(1);
1054#endif
1055 }
1056
1057 http_msg_ood:
1058 /* out of data */
1059 if (ret_state)
1060 *ret_state = state;
1061 if (ret_ptr)
1062 *ret_ptr = (char *)ptr;
1063 return NULL;
1064
1065 http_msg_invalid:
1066 /* invalid message */
1067 if (ret_state)
1068 *ret_state = HTTP_MSG_ERROR;
1069 return NULL;
1070}
1071
1072
1073/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001074 * This function parses a request line between <ptr> and <end>, starting with
1075 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1076 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1077 * will give undefined results.
1078 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1079 * and that msg->sol points to the beginning of the request.
1080 * If a complete line is found (which implies that at least one CR or LF is
1081 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1082 * returned indicating an incomplete line (which does not mean that parts have
1083 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1084 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1085 * upon next call.
1086 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001087 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1089 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001090 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001091 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001092const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1093 unsigned int state, const char *ptr, const char *end,
1094 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001095{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001096 __label__
1097 http_msg_rqmeth,
1098 http_msg_rqmeth_sp,
1099 http_msg_rquri,
1100 http_msg_rquri_sp,
1101 http_msg_rqver,
1102 http_msg_rqline_eol,
1103 http_msg_ood, /* out of data */
1104 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001105
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001106 switch (state) {
1107 http_msg_rqmeth:
1108 case HTTP_MSG_RQMETH:
1109 if (likely(HTTP_IS_TOKEN(*ptr)))
1110 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001111
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001112 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001113 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1115 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001116
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001117 if (likely(HTTP_IS_CRLF(*ptr))) {
1118 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001119 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001120 http_msg_req09_uri:
1121 msg->sl.rq.u = ptr - msg_buf;
1122 http_msg_req09_uri_e:
1123 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1124 http_msg_req09_ver:
1125 msg->sl.rq.v = ptr - msg_buf;
1126 msg->sl.rq.v_l = 0;
1127 goto http_msg_rqline_eol;
1128 }
1129 goto http_msg_invalid;
1130
1131 http_msg_rqmeth_sp:
1132 case HTTP_MSG_RQMETH_SP:
1133 if (likely(!HTTP_IS_LWS(*ptr))) {
1134 msg->sl.rq.u = ptr - msg_buf;
1135 goto http_msg_rquri;
1136 }
1137 if (likely(HTTP_IS_SPHT(*ptr)))
1138 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1139 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1140 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001141
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001142 http_msg_rquri:
1143 case HTTP_MSG_RQURI:
1144 if (likely(!HTTP_IS_LWS(*ptr)))
1145 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001146
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001147 if (likely(HTTP_IS_SPHT(*ptr))) {
1148 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1149 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1150 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001151
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001152 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1153 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001154
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001155 http_msg_rquri_sp:
1156 case HTTP_MSG_RQURI_SP:
1157 if (likely(!HTTP_IS_LWS(*ptr))) {
1158 msg->sl.rq.v = ptr - msg_buf;
1159 goto http_msg_rqver;
1160 }
1161 if (likely(HTTP_IS_SPHT(*ptr)))
1162 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1163 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1164 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001165
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001166 http_msg_rqver:
1167 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001168 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001169 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001170
1171 if (likely(HTTP_IS_CRLF(*ptr))) {
1172 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1173 http_msg_rqline_eol:
1174 /* We have seen the end of line. Note that we do not
1175 * necessarily have the \n yet, but at least we know that we
1176 * have EITHER \r OR \n, otherwise the request would not be
1177 * complete. We can then record the request length and return
1178 * to the caller which will be able to register it.
1179 */
1180 msg->sl.rq.l = ptr - msg->sol;
1181 return ptr;
1182 }
1183
1184 /* neither an HTTP_VER token nor a CRLF */
1185 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001186
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001187#ifdef DEBUG_FULL
1188 default:
1189 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1190 exit(1);
1191#endif
1192 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001193
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194 http_msg_ood:
1195 /* out of data */
1196 if (ret_state)
1197 *ret_state = state;
1198 if (ret_ptr)
1199 *ret_ptr = (char *)ptr;
1200 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001201
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001202 http_msg_invalid:
1203 /* invalid message */
1204 if (ret_state)
1205 *ret_state = HTTP_MSG_ERROR;
1206 return NULL;
1207}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001208
1209
Willy Tarreau8973c702007-01-21 23:58:29 +01001210/*
1211 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001212 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001213 * when data are missing and recalled at the exact same location with no
1214 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001215 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1216 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001217 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001218void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1219{
1220 __label__
1221 http_msg_rqbefore,
1222 http_msg_rqbefore_cr,
1223 http_msg_rqmeth,
1224 http_msg_rqline_end,
1225 http_msg_hdr_first,
1226 http_msg_hdr_name,
1227 http_msg_hdr_l1_sp,
1228 http_msg_hdr_l1_lf,
1229 http_msg_hdr_l1_lws,
1230 http_msg_hdr_val,
1231 http_msg_hdr_l2_lf,
1232 http_msg_hdr_l2_lws,
1233 http_msg_complete_header,
1234 http_msg_last_lf,
1235 http_msg_ood, /* out of data */
1236 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001237
Willy Tarreaue69eada2008-01-27 00:34:10 +01001238 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001240
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001241 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001242 ptr = buf->lr;
1243 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001244
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001245 if (unlikely(ptr >= end))
1246 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001247
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001248 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001249 /*
1250 * First, states that are specific to the response only.
1251 * We check them first so that request and headers are
1252 * closer to each other (accessed more often).
1253 */
1254 http_msg_rpbefore:
1255 case HTTP_MSG_RPBEFORE:
1256 if (likely(HTTP_IS_TOKEN(*ptr))) {
1257 if (likely(ptr == buf->data)) {
1258 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001259 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001260 } else {
1261#if PARSE_PRESERVE_EMPTY_LINES
1262 /* only skip empty leading lines, don't remove them */
1263 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001264 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001265#else
1266 /* Remove empty leading lines, as recommended by
1267 * RFC2616. This takes a lot of time because we
1268 * must move all the buffer backwards, but this
1269 * is rarely needed. The method above will be
1270 * cleaner when we'll be able to start sending
1271 * the request from any place in the buffer.
1272 */
1273 buf->lr = ptr;
1274 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001275 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 msg->sol = buf->data;
1277 ptr = buf->data;
1278 end = buf->r;
1279#endif
1280 }
1281 hdr_idx_init(idx);
1282 state = HTTP_MSG_RPVER;
1283 goto http_msg_rpver;
1284 }
1285
1286 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1287 goto http_msg_invalid;
1288
1289 if (unlikely(*ptr == '\n'))
1290 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1291 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1292 /* stop here */
1293
1294 http_msg_rpbefore_cr:
1295 case HTTP_MSG_RPBEFORE_CR:
1296 EXPECT_LF_HERE(ptr, http_msg_invalid);
1297 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1298 /* stop here */
1299
1300 http_msg_rpver:
1301 case HTTP_MSG_RPVER:
1302 case HTTP_MSG_RPVER_SP:
1303 case HTTP_MSG_RPCODE:
1304 case HTTP_MSG_RPCODE_SP:
1305 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001306 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001307 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 if (unlikely(!ptr))
1309 return;
1310
1311 /* we have a full response and we know that we have either a CR
1312 * or an LF at <ptr>.
1313 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001314 //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 +01001315 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1316
1317 msg->sol = ptr;
1318 if (likely(*ptr == '\r'))
1319 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1320 goto http_msg_rpline_end;
1321
1322 http_msg_rpline_end:
1323 case HTTP_MSG_RPLINE_END:
1324 /* msg->sol must point to the first of CR or LF. */
1325 EXPECT_LF_HERE(ptr, http_msg_invalid);
1326 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1327 /* stop here */
1328
1329 /*
1330 * Second, states that are specific to the request only
1331 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001332 http_msg_rqbefore:
1333 case HTTP_MSG_RQBEFORE:
1334 if (likely(HTTP_IS_TOKEN(*ptr))) {
1335 if (likely(ptr == buf->data)) {
1336 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001337 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001338 } else {
1339#if PARSE_PRESERVE_EMPTY_LINES
1340 /* only skip empty leading lines, don't remove them */
1341 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001342 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001343#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001344 /* Remove empty leading lines, as recommended by
1345 * RFC2616. This takes a lot of time because we
1346 * must move all the buffer backwards, but this
1347 * is rarely needed. The method above will be
1348 * cleaner when we'll be able to start sending
1349 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001350 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001351 buf->lr = ptr;
1352 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001353 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354 msg->sol = buf->data;
1355 ptr = buf->data;
1356 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001357#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001358 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001359 /* we will need this when keep-alive will be supported
1360 hdr_idx_init(idx);
1361 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 state = HTTP_MSG_RQMETH;
1363 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001365
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001366 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1367 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001368
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369 if (unlikely(*ptr == '\n'))
1370 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1371 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001372 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001373
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 http_msg_rqbefore_cr:
1375 case HTTP_MSG_RQBEFORE_CR:
1376 EXPECT_LF_HERE(ptr, http_msg_invalid);
1377 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001378 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380 http_msg_rqmeth:
1381 case HTTP_MSG_RQMETH:
1382 case HTTP_MSG_RQMETH_SP:
1383 case HTTP_MSG_RQURI:
1384 case HTTP_MSG_RQURI_SP:
1385 case HTTP_MSG_RQVER:
1386 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001387 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 if (unlikely(!ptr))
1389 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001390
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001391 /* we have a full request and we know that we have either a CR
1392 * or an LF at <ptr>.
1393 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001394 //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 +01001395 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 msg->sol = ptr;
1398 if (likely(*ptr == '\r'))
1399 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001401
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 http_msg_rqline_end:
1403 case HTTP_MSG_RQLINE_END:
1404 /* check for HTTP/0.9 request : no version information available.
1405 * msg->sol must point to the first of CR or LF.
1406 */
1407 if (unlikely(msg->sl.rq.v_l == 0))
1408 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001409
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 EXPECT_LF_HERE(ptr, http_msg_invalid);
1411 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001412 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001413
Willy Tarreau8973c702007-01-21 23:58:29 +01001414 /*
1415 * Common states below
1416 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 http_msg_hdr_first:
1418 case HTTP_MSG_HDR_FIRST:
1419 msg->sol = ptr;
1420 if (likely(!HTTP_IS_CRLF(*ptr))) {
1421 goto http_msg_hdr_name;
1422 }
1423
1424 if (likely(*ptr == '\r'))
1425 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1426 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001427
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 http_msg_hdr_name:
1429 case HTTP_MSG_HDR_NAME:
1430 /* assumes msg->sol points to the first char */
1431 if (likely(HTTP_IS_TOKEN(*ptr)))
1432 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 if (likely(*ptr == ':')) {
1435 msg->col = ptr - buf->data;
1436 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1437 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001440
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 http_msg_hdr_l1_sp:
1442 case HTTP_MSG_HDR_L1_SP:
1443 /* assumes msg->sol points to the first char and msg->col to the colon */
1444 if (likely(HTTP_IS_SPHT(*ptr)))
1445 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 /* header value can be basically anything except CR/LF */
1448 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 if (likely(!HTTP_IS_CRLF(*ptr))) {
1451 goto http_msg_hdr_val;
1452 }
1453
1454 if (likely(*ptr == '\r'))
1455 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1456 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001457
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 http_msg_hdr_l1_lf:
1459 case HTTP_MSG_HDR_L1_LF:
1460 EXPECT_LF_HERE(ptr, http_msg_invalid);
1461 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001462
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 http_msg_hdr_l1_lws:
1464 case HTTP_MSG_HDR_L1_LWS:
1465 if (likely(HTTP_IS_SPHT(*ptr))) {
1466 /* replace HT,CR,LF with spaces */
1467 for (; buf->data+msg->sov < ptr; msg->sov++)
1468 buf->data[msg->sov] = ' ';
1469 goto http_msg_hdr_l1_sp;
1470 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001471 /* we had a header consisting only in spaces ! */
1472 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 goto http_msg_complete_header;
1474
1475 http_msg_hdr_val:
1476 case HTTP_MSG_HDR_VAL:
1477 /* assumes msg->sol points to the first char, msg->col to the
1478 * colon, and msg->sov points to the first character of the
1479 * value.
1480 */
1481 if (likely(!HTTP_IS_CRLF(*ptr)))
1482 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 msg->eol = ptr;
1485 /* Note: we could also copy eol into ->eoh so that we have the
1486 * real header end in case it ends with lots of LWS, but is this
1487 * really needed ?
1488 */
1489 if (likely(*ptr == '\r'))
1490 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1491 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001492
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 http_msg_hdr_l2_lf:
1494 case HTTP_MSG_HDR_L2_LF:
1495 EXPECT_LF_HERE(ptr, http_msg_invalid);
1496 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001497
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 http_msg_hdr_l2_lws:
1499 case HTTP_MSG_HDR_L2_LWS:
1500 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1501 /* LWS: replace HT,CR,LF with spaces */
1502 for (; msg->eol < ptr; msg->eol++)
1503 *msg->eol = ' ';
1504 goto http_msg_hdr_val;
1505 }
1506 http_msg_complete_header:
1507 /*
1508 * It was a new header, so the last one is finished.
1509 * Assumes msg->sol points to the first char, msg->col to the
1510 * colon, msg->sov points to the first character of the value
1511 * and msg->eol to the first CR or LF so we know how the line
1512 * ends. We insert last header into the index.
1513 */
1514 /*
1515 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1516 write(2, msg->sol, msg->eol-msg->sol);
1517 fprintf(stderr,"\n");
1518 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1521 idx, idx->tail) < 0))
1522 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 msg->sol = ptr;
1525 if (likely(!HTTP_IS_CRLF(*ptr))) {
1526 goto http_msg_hdr_name;
1527 }
1528
1529 if (likely(*ptr == '\r'))
1530 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1531 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001532
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 http_msg_last_lf:
1534 case HTTP_MSG_LAST_LF:
1535 /* Assumes msg->sol points to the first of either CR or LF */
1536 EXPECT_LF_HERE(ptr, http_msg_invalid);
1537 ptr++;
1538 buf->lr = ptr;
1539 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001540 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 return;
1542#ifdef DEBUG_FULL
1543 default:
1544 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1545 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001546#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 }
1548 http_msg_ood:
1549 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001550 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 buf->lr = ptr;
1552 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 http_msg_invalid:
1555 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001556 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 return;
1558}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001559
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001560/* This function performs all the processing enabled for the current request.
1561 * It returns 1 if it changes its state and it believes that another function
1562 * must be updated, otherwise zero. It might make sense to explode it into
1563 * several other functions.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001565int process_request(struct session *t)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 struct buffer *req = t->req;
1568 struct buffer *rep = t->rep;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001569 int fsm_resync = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001570
Willy Tarreaue46ab552008-08-13 19:19:37 +02001571 DPRINTF(stderr,"[%u] process_req: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysis=%02x\n",
1572 now_ms,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001573 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreauf161a342007-04-08 16:59:42 +02001574 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001575 req->rex, rep->wex, req->flags, rep->flags, t->analysis);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001576
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001577 if (t->analysis & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001578 struct tcp_rule *rule;
1579 int partial;
1580
1581 /* We will abort if we encounter a read error. In theory,
1582 * we should not abort if we get a close, it might be
1583 * valid, also very unlikely. FIXME: we'll abort for now,
1584 * this will be easier to change later.
1585 */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001586 if (req->flags & BF_READ_ERROR) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001587 t->inspect_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001588 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001589 t->fe->failed_req++;
1590 if (!(t->flags & SN_ERR_MASK))
1591 t->flags |= SN_ERR_CLICL;
1592 if (!(t->flags & SN_FINST_MASK))
1593 t->flags |= SN_FINST_R;
1594 return 1;
1595 }
1596
1597 /* Abort if client read timeout has expired */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001598 else if (req->flags & BF_READ_TIMEOUT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001599 t->inspect_exp = TICK_ETERNITY;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001600 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001601 t->fe->failed_req++;
1602 if (!(t->flags & SN_ERR_MASK))
1603 t->flags |= SN_ERR_CLITO;
1604 if (!(t->flags & SN_FINST_MASK))
1605 t->flags |= SN_FINST_R;
1606 return 1;
1607 }
1608
1609 /* We don't know whether we have enough data, so must proceed
1610 * this way :
1611 * - iterate through all rules in their declaration order
1612 * - if one rule returns MISS, it means the inspect delay is
1613 * not over yet, then return immediately, otherwise consider
1614 * it as a non-match.
1615 * - if one rule returns OK, then return OK
1616 * - if one rule returns KO, then return KO
1617 */
1618
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001619 if (req->flags & (BF_READ_NULL | BF_SHUTR_STATUS) ||
1620 tick_is_expired(t->inspect_exp, now_ms))
Willy Tarreaub6866442008-07-14 23:54:42 +02001621 partial = 0;
1622 else
1623 partial = ACL_PARTIAL;
1624
1625 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1626 int ret = ACL_PAT_PASS;
1627
1628 if (rule->cond) {
1629 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1630 if (ret == ACL_PAT_MISS) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001631 /* just set the request timeout once at the beginning of the request */
1632 if (!tick_isset(t->inspect_exp))
1633 t->inspect_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay);
1634
1635 return fsm_resync;
Willy Tarreaub6866442008-07-14 23:54:42 +02001636 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001637
Willy Tarreaub6866442008-07-14 23:54:42 +02001638 ret = acl_pass(ret);
1639 if (rule->cond->pol == ACL_COND_UNLESS)
1640 ret = !ret;
1641 }
1642
1643 if (ret) {
1644 /* we have a matching rule. */
1645 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001646 buffer_shutr_done(req);
1647 buffer_shutw_done(rep);
Willy Tarreaub6866442008-07-14 23:54:42 +02001648 fd_delete(t->cli_fd);
1649 t->cli_state = CL_STCLOSE;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001650 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001651 t->fe->failed_req++;
1652 if (!(t->flags & SN_ERR_MASK))
1653 t->flags |= SN_ERR_PRXCOND;
1654 if (!(t->flags & SN_FINST_MASK))
1655 t->flags |= SN_FINST_R;
1656 t->inspect_exp = TICK_ETERNITY;
1657 return 1;
1658 }
1659 /* otherwise accept */
1660 break;
1661 }
1662 }
1663
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001664 /* if we get there, it means we have no rule which matches, or
1665 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001666 */
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001667 t->analysis &= ~AN_REQ_INSPECT;
Willy Tarreaub6866442008-07-14 23:54:42 +02001668 t->inspect_exp = TICK_ETERNITY;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001669 fsm_resync = 1;
Willy Tarreaub6866442008-07-14 23:54:42 +02001670 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001671
1672 if (t->analysis & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001673 /*
1674 * Now parse the partial (or complete) lines.
1675 * We will check the request syntax, and also join multi-line
1676 * headers. An index of all the lines will be elaborated while
1677 * parsing.
1678 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001679 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001680 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001682 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001683 * req->data + req->eoh = end of processed headers / start of current one
1684 * req->data + req->eol = end of current header or line (LF or CRLF)
1685 * req->lr = first non-visited byte
1686 * req->r = end of data
1687 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001688
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001690 struct http_txn *txn = &t->txn;
1691 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001693
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001695 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001696
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 /* 1: we might have to print this header in debug mode */
1698 if (unlikely((global.mode & MODE_DEBUG) &&
1699 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001700 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001701 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001702
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001703 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 eol = sol + msg->sl.rq.l;
1705 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001706
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001707 sol += hdr_idx_first_pos(&txn->hdr_idx);
1708 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001709
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001711 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001712 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001713 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1714 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001715 }
1716 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001717
Willy Tarreau58f10d72006-12-04 02:26:12 +01001718
1719 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001721 * If not so, we check the FD and buffer states before leaving.
1722 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1724 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001725 *
1726 */
1727
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001728 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 /*
1730 * First, let's catch bad requests.
1731 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001732 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001734
1735 /* 1: Since we are in header mode, if there's no space
1736 * left for headers, we won't be able to free more
1737 * later, so the session will never terminate. We
1738 * must terminate it now.
1739 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 if (unlikely(req->l >= req->rlim - req->data)) {
1741 /* FIXME: check if URI is set and return Status
1742 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001743 */
Willy Tarreau06619262006-12-17 08:37:22 +01001744 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001745 }
1746
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001747 /* 2: have we encountered a close ? */
1748 else if (req->flags & BF_READ_NULL) {
1749 txn->status = 400;
1750 client_retnclose(t, error_message(t, HTTP_ERR_400));
1751 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001752 t->analysis &= ~AN_REQ_ANY;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001753 t->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001754
Willy Tarreau58f10d72006-12-04 02:26:12 +01001755 if (!(t->flags & SN_ERR_MASK))
1756 t->flags |= SN_ERR_CLICL;
1757 if (!(t->flags & SN_FINST_MASK))
1758 t->flags |= SN_FINST_R;
1759 return 1;
1760 }
1761
1762 /* 3: has the read timeout expired ? */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001763 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(txn->exp, now_ms)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001764 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001765 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001766 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001767 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001768 t->analysis &= ~AN_REQ_ANY;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001769 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001770 if (!(t->flags & SN_ERR_MASK))
1771 t->flags |= SN_ERR_CLITO;
1772 if (!(t->flags & SN_FINST_MASK))
1773 t->flags |= SN_FINST_R;
1774 return 1;
1775 }
1776
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001777 /* 4: have we encountered a read error or did we have to shutdown ? */
1778 else if (req->flags & (BF_READ_ERROR | BF_SHUTR_STATUS)) {
1779 /* we cannot return any message on error */
1780 msg->msg_state = HTTP_MSG_ERROR;
1781 t->analysis &= ~AN_REQ_ANY;
1782 t->fe->failed_req++;
1783 if (!(t->flags & SN_ERR_MASK))
1784 t->flags |= SN_ERR_CLICL;
1785 if (!(t->flags & SN_FINST_MASK))
1786 t->flags |= SN_FINST_R;
1787 return 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001788 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001789
1790 /* just set the request timeout once at the beginning of the request */
1791 if (!tick_isset(t->txn.exp))
1792 t->txn.exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
1793
1794 /* we're not ready yet */
1795 return fsm_resync;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001796 }
1797
1798
1799 /****************************************************************
1800 * More interesting part now : we know that we have a complete *
1801 * request which at least looks like HTTP. We have an indicator *
1802 * of each header's length, so we can parse them quickly. *
1803 ****************************************************************/
1804
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001805 t->analysis &= ~AN_REQ_HTTP_HDR;
1806
Willy Tarreau9cdde232007-05-02 20:58:19 +02001807 /* ensure we keep this pointer to the beginning of the message */
1808 msg->sol = req->data + msg->som;
1809
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001810 /*
1811 * 1: identify the method
1812 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001813 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001814
1815 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001816 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001817 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001818 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001819 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 if (unlikely((t->fe->monitor_uri_len != 0) &&
1821 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1822 !memcmp(&req->data[msg->sl.rq.u],
1823 t->fe->monitor_uri,
1824 t->fe->monitor_uri_len))) {
1825 /*
1826 * We have found the monitor URI
1827 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001828 struct acl_cond *cond;
1829 cur_proxy = t->fe;
1830
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001832
1833 /* Check if we want to fail this monitor request or not */
1834 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1835 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001836
1837 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001838 if (cond->pol == ACL_COND_UNLESS)
1839 ret = !ret;
1840
1841 if (ret) {
1842 /* we fail this request, let's return 503 service unavail */
1843 txn->status = 503;
1844 client_retnclose(t, error_message(t, HTTP_ERR_503));
1845 goto return_prx_cond;
1846 }
1847 }
1848
1849 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001850 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001851 client_retnclose(t, &http_200_chunk);
1852 goto return_prx_cond;
1853 }
1854
1855 /*
1856 * 3: Maybe we have to copy the original REQURI for the logs ?
1857 * Note: we cannot log anymore if the request has been
1858 * classified as invalid.
1859 */
1860 if (unlikely(t->logs.logwait & LW_REQ)) {
1861 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001862 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 int urilen = msg->sl.rq.l;
1864
1865 if (urilen >= REQURI_LEN)
1866 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001867 memcpy(txn->uri, &req->data[msg->som], urilen);
1868 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001869
1870 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001871 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001872 } else {
1873 Alert("HTTP logging : out of memory.\n");
1874 }
1875 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001876
Willy Tarreau06619262006-12-17 08:37:22 +01001877
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001878 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1879 if (unlikely(msg->sl.rq.v_l == 0)) {
1880 int delta;
1881 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001882 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 cur_end = msg->sol + msg->sl.rq.l;
1884 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001885
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001886 if (msg->sl.rq.u_l == 0) {
1887 /* if no URI was set, add "/" */
1888 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1889 cur_end += delta;
1890 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001891 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 /* add HTTP version */
1893 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1894 msg->eoh += delta;
1895 cur_end += delta;
1896 cur_end = (char *)http_parse_reqline(msg, req->data,
1897 HTTP_MSG_RQMETH,
1898 msg->sol, cur_end + 1,
1899 NULL, NULL);
1900 if (unlikely(!cur_end))
1901 goto return_bad_req;
1902
1903 /* we have a full HTTP/1.0 request now and we know that
1904 * we have either a CR or an LF at <ptr>.
1905 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001906 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001907 }
1908
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909
1910 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001911 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001912 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001913 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001914
1915 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001916 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001917 * As opposed to version 1.2, now they will be evaluated in the
1918 * filters order and not in the header order. This means that
1919 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001920 *
1921 * We can now check whether we want to switch to another
1922 * backend, in which case we will re-check the backend's
1923 * filters and various options. In order to support 3-level
1924 * switching, here's how we should proceed :
1925 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001926 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001927 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001928 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001929 * There cannot be any switch from there, so ->be cannot be
1930 * changed anymore.
1931 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001932 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001933 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001934 * The response path will be able to apply either ->be, or
1935 * ->be then ->fe filters in order to match the reverse of
1936 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001937 */
1938
Willy Tarreau06619262006-12-17 08:37:22 +01001939 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001940 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001941 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001942 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001943 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001944
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001945 /* first check whether we have some ACLs set to redirect this request */
1946 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
1947 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001948
1949 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001950 if (rule->cond->pol == ACL_COND_UNLESS)
1951 ret = !ret;
1952
1953 if (ret) {
1954 struct chunk rdr = { trash, 0 };
1955 const char *msg_fmt;
1956
1957 /* build redirect message */
1958 switch(rule->code) {
1959 case 303:
1960 rdr.len = strlen(HTTP_303);
1961 msg_fmt = HTTP_303;
1962 break;
1963 case 301:
1964 rdr.len = strlen(HTTP_301);
1965 msg_fmt = HTTP_301;
1966 break;
1967 case 302:
1968 default:
1969 rdr.len = strlen(HTTP_302);
1970 msg_fmt = HTTP_302;
1971 break;
1972 }
1973
1974 if (unlikely(rdr.len > sizeof(trash)))
1975 goto return_bad_req;
1976 memcpy(rdr.str, msg_fmt, rdr.len);
1977
1978 switch(rule->type) {
1979 case REDIRECT_TYPE_PREFIX: {
1980 const char *path;
1981 int pathlen;
1982
1983 path = http_get_path(txn);
1984 /* build message using path */
1985 if (path) {
1986 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
1987 } else {
1988 path = "/";
1989 pathlen = 1;
1990 }
1991
1992 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
1993 goto return_bad_req;
1994
1995 /* add prefix */
1996 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1997 rdr.len += rule->rdr_len;
1998
1999 /* add path */
2000 memcpy(rdr.str + rdr.len, path, pathlen);
2001 rdr.len += pathlen;
2002 break;
2003 }
2004 case REDIRECT_TYPE_LOCATION:
2005 default:
2006 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2007 goto return_bad_req;
2008
2009 /* add location */
2010 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2011 rdr.len += rule->rdr_len;
2012 break;
2013 }
2014
2015 /* add end of headers */
2016 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2017 rdr.len += 4;
2018
2019 txn->status = rule->code;
2020 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002021 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002022 client_retnclose(t, &rdr);
2023 goto return_prx_cond;
2024 }
2025 }
2026
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002027 /* first check whether we have some ACLs set to block this request */
2028 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002029 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002030
2031 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002032 if (cond->pol == ACL_COND_UNLESS)
2033 ret = !ret;
2034
2035 if (ret) {
2036 txn->status = 403;
2037 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002038 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002039 client_retnclose(t, error_message(t, HTTP_ERR_403));
2040 goto return_prx_cond;
2041 }
2042 }
2043
Willy Tarreau06619262006-12-17 08:37:22 +01002044 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002045 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002046 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2047 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002048 }
2049
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002050 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2051 /* to ensure correct connection accounting on
2052 * the backend, we count the connection for the
2053 * one managing the queue.
2054 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002055 t->be->beconn++;
2056 if (t->be->beconn > t->be->beconn_max)
2057 t->be->beconn_max = t->be->beconn;
2058 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002059 t->flags |= SN_BE_ASSIGNED;
2060 }
2061
Willy Tarreau06619262006-12-17 08:37:22 +01002062 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002063 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002064 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002065 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002066 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002067 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002068 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002069 goto return_prx_cond;
2070 }
2071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002072 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002073 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002074 !(t->flags & SN_CONN_CLOSED)) {
2075 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002076 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002077 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002078
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002079 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002080 old_idx = 0;
2081
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002082 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2083 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002084 cur_ptr = cur_next;
2085 cur_end = cur_ptr + cur_hdr->len;
2086 cur_next = cur_end + cur_hdr->cr + 1;
2087
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002088 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2089 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002090 /* 3 possibilities :
2091 * - we have already set Connection: close,
2092 * so we remove this line.
2093 * - we have not yet set Connection: close,
2094 * but this line indicates close. We leave
2095 * it untouched and set the flag.
2096 * - we have not yet set Connection: close,
2097 * and this line indicates non-close. We
2098 * replace it.
2099 */
2100 if (t->flags & SN_CONN_CLOSED) {
2101 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002102 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002103 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002104 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2105 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002106 cur_hdr->len = 0;
2107 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002108 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2109 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2110 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002111 cur_next += delta;
2112 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002113 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002114 }
2115 t->flags |= SN_CONN_CLOSED;
2116 }
2117 }
2118 old_idx = cur_idx;
2119 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002120 }
2121 /* add request headers from the rule sets in the same order */
2122 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2123 if (unlikely(http_header_add_tail(req,
2124 &txn->req,
2125 &txn->hdr_idx,
2126 rule_set->req_add[cur_idx])) < 0)
2127 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002128 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002129
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002130 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002131 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002132 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002133 /* we have to check the URI and auth for this request.
2134 * FIXME!!! that one is rather dangerous, we want to
2135 * make it follow standard rules (eg: clear t->analysis).
2136 */
Willy Tarreaub2513902006-12-17 14:52:38 +01002137 if (stats_check_uri_auth(t, rule_set))
2138 return 1;
2139 }
2140
Willy Tarreau55ea7572007-06-17 19:56:27 +02002141 /* now check whether we have some switching rules for this request */
2142 if (!(t->flags & SN_BE_ASSIGNED)) {
2143 struct switching_rule *rule;
2144
2145 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2146 int ret;
2147
2148 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002149
2150 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002151 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002152 ret = !ret;
2153
2154 if (ret) {
2155 t->be = rule->be.backend;
2156 t->be->beconn++;
2157 if (t->be->beconn > t->be->beconn_max)
2158 t->be->beconn_max = t->be->beconn;
2159 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002160
2161 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002162 t->rep->rto = t->req->wto = t->be->timeout.server;
2163 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002164 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002165 t->flags |= SN_BE_ASSIGNED;
2166 break;
2167 }
2168 }
2169 }
2170
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002171 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2172 /* No backend was set, but there was a default
2173 * backend set in the frontend, so we use it and
2174 * loop again.
2175 */
2176 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002177 t->be->beconn++;
2178 if (t->be->beconn > t->be->beconn_max)
2179 t->be->beconn_max = t->be->beconn;
2180 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002181
2182 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002183 t->rep->rto = t->req->wto = t->be->timeout.server;
2184 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002185 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002186 t->flags |= SN_BE_ASSIGNED;
2187 }
2188 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002189
Willy Tarreau58f10d72006-12-04 02:26:12 +01002190
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002191 if (!(t->flags & SN_BE_ASSIGNED)) {
2192 /* To ensure correct connection accounting on
2193 * the backend, we count the connection for the
2194 * one managing the queue.
2195 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002196 t->be->beconn++;
2197 if (t->be->beconn > t->be->beconn_max)
2198 t->be->beconn_max = t->be->beconn;
2199 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002200 t->flags |= SN_BE_ASSIGNED;
2201 }
2202
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002203 /*
2204 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002205 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002206 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002207 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002208 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002209
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002210 /*
2211 * If HTTP PROXY is set we simply get remote server address
2212 * parsing incoming request.
2213 */
2214 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2215 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2216 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002217
Willy Tarreau2a324282006-12-05 00:05:46 +01002218 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002219 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002220 * so let's do the same now.
2221 */
2222
2223 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002224 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002225 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002226 }
2227
2228
2229 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002230 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002231 * Note that doing so might move headers in the request, but
2232 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002233 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002234 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002235 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2236 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002237 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002238
Willy Tarreau58f10d72006-12-04 02:26:12 +01002239
Willy Tarreau2a324282006-12-05 00:05:46 +01002240 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002241 * 9: add X-Forwarded-For if either the frontend or the backend
2242 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002243 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002244 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002245 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002246 /* Add an X-Forwarded-For header unless the source IP is
2247 * in the 'except' network range.
2248 */
2249 if ((!t->fe->except_mask.s_addr ||
2250 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2251 != t->fe->except_net.s_addr) &&
2252 (!t->be->except_mask.s_addr ||
2253 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2254 != t->be->except_net.s_addr)) {
2255 int len;
2256 unsigned char *pn;
2257 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002258
Ross Westaf72a1d2008-08-03 10:51:45 +02002259 /* Note: we rely on the backend to get the header name to be used for
2260 * x-forwarded-for, because the header is really meant for the backends.
2261 * However, if the backend did not specify any option, we have to rely
2262 * on the frontend's header name.
2263 */
2264 if (t->be->fwdfor_hdr_len) {
2265 len = t->be->fwdfor_hdr_len;
2266 memcpy(trash, t->be->fwdfor_hdr_name, len);
2267 } else {
2268 len = t->fe->fwdfor_hdr_len;
2269 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2270 }
2271 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002272
Ross Westaf72a1d2008-08-03 10:51:45 +02002273 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002274 &txn->hdr_idx, trash, len)) < 0)
2275 goto return_bad_req;
2276 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002277 }
2278 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002279 /* FIXME: for the sake of completeness, we should also support
2280 * 'except' here, although it is mostly useless in this case.
2281 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002282 int len;
2283 char pn[INET6_ADDRSTRLEN];
2284 inet_ntop(AF_INET6,
2285 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2286 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002287
2288 /* Note: we rely on the backend to get the header name to be used for
2289 * x-forwarded-for, because the header is really meant for the backends.
2290 * However, if the backend did not specify any option, we have to rely
2291 * on the frontend's header name.
2292 */
2293 if (t->be->fwdfor_hdr_len) {
2294 len = t->be->fwdfor_hdr_len;
2295 memcpy(trash, t->be->fwdfor_hdr_name, len);
2296 } else {
2297 len = t->fe->fwdfor_hdr_len;
2298 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2299 }
2300 len += sprintf(trash + len, ": %s", pn);
2301
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002302 if (unlikely(http_header_add_tail2(req, &txn->req,
2303 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002304 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002305 }
2306 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002307
Willy Tarreau2a324282006-12-05 00:05:46 +01002308 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002309 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002310 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002311 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002312 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002313 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002314 if ((unlikely(msg->sl.rq.v_l != 8) ||
2315 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2316 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002317 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002318 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002319 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002320 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002321 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2322 * If not assigned, perhaps we are balancing on url_param, but this is a
2323 * POST; and the parameters are in the body, maybe scan there to find our server.
2324 * (unless headers overflowed the buffer?)
2325 */
2326 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2327 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002328 t->be->url_param_post_limit != 0 && req->l < BUFSIZE &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002329 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2330 /* are there enough bytes here? total == l || r || rlim ?
2331 * len is unsigned, but eoh is int,
2332 * how many bytes of body have we received?
2333 * eoh is the first empty line of the header
2334 */
2335 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002336 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002337
2338 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2339 * We can't assume responsibility for the server's decision,
2340 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2341 * We also can't change our mind later, about which server to choose, so round robin.
2342 */
2343 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2344 struct hdr_ctx ctx;
2345 ctx.idx = 0;
2346 /* Expect is allowed in 1.1, look for it */
2347 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2348 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002349 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002350 /* We can't reliablly stall and wait for data, because of
2351 * .NET clients that don't conform to rfc2616; so, no need for
2352 * the next block to check length expectations.
2353 * We could send 100 status back to the client, but then we need to
2354 * re-write headers, and send the message. And this isn't the right
2355 * place for that action.
2356 * TODO: support Expect elsewhere and delete this block.
2357 */
2358 goto end_check_maybe_wait_for_body;
2359 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002360
2361 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002362 /* nothing to do, we got enough */
2363 } else {
2364 /* limit implies we are supposed to need this many bytes
2365 * to find the parameter. Let's see how many bytes we can wait for.
2366 */
2367 long long hint = len;
2368 struct hdr_ctx ctx;
2369 ctx.idx = 0;
2370 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002371 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0)
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002372 t->analysis |= AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002373 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002374 ctx.idx = 0;
2375 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2376 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002377 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002378 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002379 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002380 hint = 0; /* parse failure, untrusted client */
2381 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002382 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002383 msg->hdr_content_len = hint;
2384 else
2385 hint = 0; /* bad client, sent negative length */
2386 }
2387 }
2388 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002389 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002390 hint = t->be->url_param_post_limit;
2391 /* now do we really need to buffer more data? */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002392 if (len < hint)
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002393 t->analysis |= AN_REQ_HTTP_BODY;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002394 /* else... There are no body bytes to wait for */
2395 }
2396 }
2397 }
2398 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002399
Willy Tarreau2a324282006-12-05 00:05:46 +01002400 /*************************************************************
2401 * OK, that's finished for the headers. We have done what we *
2402 * could. Let's switch to the DATA state. *
2403 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002404
Willy Tarreauadfb8562008-08-11 15:24:42 +02002405 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002406 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002407
Willy Tarreau1fa31262007-12-03 00:36:16 +01002408 /* When a connection is tarpitted, we use the tarpit timeout,
2409 * which may be the same as the connect timeout if unspecified.
2410 * If unset, then set it to zero because we really want it to
2411 * eventually expire.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002412 * FIXME: this part should be moved elsewhere (eg: on the server side)
Willy Tarreau2a324282006-12-05 00:05:46 +01002413 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002414 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002415 t->req->l = 0;
2416 /* flush the request so that we can drop the connection early
2417 * if the client closes first.
2418 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002419 req->cex = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2420 if (!req->cex)
2421 req->cex = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002422 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002424 /* OK let's go on with the BODY now */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002425 fsm_resync = 1;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002426 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002427
2428 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002429 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002430 txn->status = 400;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002431 t->analysis &= ~AN_REQ_ANY;
Willy Tarreau80587432006-12-24 17:47:20 +01002432 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002433 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002434 return_prx_cond:
2435 if (!(t->flags & SN_ERR_MASK))
2436 t->flags |= SN_ERR_PRXCOND;
2437 if (!(t->flags & SN_FINST_MASK))
2438 t->flags |= SN_FINST_R;
2439 return 1;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002440 end_of_headers:
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002441 ; // to keep gcc happy
Willy Tarreauadfb8562008-08-11 15:24:42 +02002442 }
2443
2444 if (t->analysis & AN_REQ_HTTP_BODY) {
2445 /* We have to parse the HTTP request body to find any required data.
2446 * "balance url_param check_post" should have been the only way to get
2447 * into this. We were brought here after HTTP header analysis, so all
2448 * related structures are ready.
2449 */
2450 struct http_msg *msg = &t->txn.req;
2451 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2452 long long limit = t->be->url_param_post_limit;
2453 struct hdr_ctx ctx;
2454
2455 ctx.idx = 0;
2456
2457 /* now if we have a length, we'll take the hint */
2458 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2459 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2460 unsigned int chunk = 0;
2461 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2462 char c = msg->sol[body];
2463 if (ishex(c)) {
2464 unsigned int hex = toupper(c) - '0';
2465 if (hex > 9)
2466 hex -= 'A' - '9' - 1;
2467 chunk = (chunk << 4) | hex;
2468 } else
2469 break;
2470 body++;
2471 }
2472 if (body + 2 >= req->l) /* we want CRLF too */
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002473 goto http_body_end; /* end of buffer? data missing! */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002474
2475 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002476 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002477
2478 body += 2; // skip CRLF
2479
2480 /* if we support more then one chunk here, we have to do it again when assigning server
2481 * 1. how much entity data do we have? new var
2482 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2483 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2484 */
2485
2486 if (chunk < limit)
2487 limit = chunk; /* only reading one chunk */
2488 } else {
2489 if (msg->hdr_content_len < limit)
2490 limit = msg->hdr_content_len;
2491 }
2492
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002493 http_body_end:
2494 /* we leave once we know we have nothing left to do. This means that we have
2495 * enough bytes, or that we know we'll not get any more (buffer full, read
2496 * buffer closed).
2497 */
2498 if (req->l - body >= limit || /* enough bytes! */
2499 req->l >= req->rlim - req->data || /* full */
2500 req->flags & (BF_READ_ERROR | BF_READ_NULL | BF_READ_TIMEOUT)) {
2501 /* The situation will not evolve, so let's give up on the analysis. */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002502 t->logs.tv_request = now; /* update the request timer to reflect full request */
2503 t->analysis &= ~AN_REQ_HTTP_BODY;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002504 fsm_resync = 1;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002505 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002506 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002507
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002508 return fsm_resync;
2509}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002510
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002511/* This function performs all the processing enabled for the current response.
2512 * It returns 1 if it changes its state and it believes that another function
2513 * must be updated, otherwise zero. It might make sense to explode it into
2514 * several other functions.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002515 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002516int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002517{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002518 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002519 struct buffer *req = t->req;
2520 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002521
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002522 DPRINTF(stderr,"[%u] process_rep: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysis=%02x\n",
Willy Tarreaue46ab552008-08-13 19:19:37 +02002523 now_ms,
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002524 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002525 EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR),
2526 req->rex, rep->wex, req->flags, rep->flags, t->analysis);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002527
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002528 if (t->analysis & AN_RTR_HTTP_HDR) { /* receiving server headers */
2529 /*
2530 * Now parse the partial (or complete) lines.
2531 * We will check the response syntax, and also join multi-line
2532 * headers. An index of all the lines will be elaborated while
2533 * parsing.
2534 *
2535 * For the parsing, we use a 28 states FSM.
2536 *
2537 * Here is the information we currently have :
2538 * rep->data + req->som = beginning of response
2539 * rep->data + req->eoh = end of processed headers / start of current one
2540 * rep->data + req->eol = end of current header or line (LF or CRLF)
2541 * rep->lr = first non-visited byte
2542 * rep->r = end of data
2543 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002544
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002545 int cur_idx;
2546 struct http_msg *msg = &txn->rsp;
2547 struct proxy *cur_proxy;
2548
2549 if (likely(rep->lr < rep->r))
2550 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2551
2552 /* 1: we might have to print this header in debug mode */
2553 if (unlikely((global.mode & MODE_DEBUG) &&
2554 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2555 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2556 char *eol, *sol;
2557
2558 sol = rep->data + msg->som;
2559 eol = sol + msg->sl.rq.l;
2560 debug_hdr("srvrep", t, sol, eol);
2561
2562 sol += hdr_idx_first_pos(&txn->hdr_idx);
2563 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2564
2565 while (cur_idx) {
2566 eol = sol + txn->hdr_idx.v[cur_idx].len;
2567 debug_hdr("srvhdr", t, sol, eol);
2568 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2569 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002570 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002571 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002572
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002573
2574 if ((rep->l < rep->rlim - rep->data) && !tick_isset(rep->rex)) {
2575 EV_FD_COND_S(t->srv_fd, DIR_RD);
2576 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
2577 * full. We cannot loop here since stream_sock_read will disable it only if
2578 * rep->l == rlim-data
2579 */
2580 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002581 }
2582
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002583
2584 /*
2585 * Now we quickly check if we have found a full valid response.
2586 * If not so, we check the FD and buffer states before leaving.
2587 * A full response is indicated by the fact that we have seen
2588 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2589 * responses are checked first.
2590 *
2591 * Depending on whether the client is still there or not, we
2592 * may send an error response back or not. Note that normally
2593 * we should only check for HTTP status there, and check I/O
2594 * errors somewhere else.
2595 */
2596
2597 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002598 /* Invalid response */
2599 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2600 hdr_response_bad:
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002601 buffer_shutr_done(rep);
2602 buffer_shutw_done(req);
2603 fd_delete(t->srv_fd);
2604 if (t->srv) {
2605 t->srv->cur_sess--;
2606 t->srv->failed_resp++;
2607 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002608 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002609 t->be->failed_resp++;
2610 t->srv_state = SV_STCLOSE;
2611 t->analysis &= ~AN_RTR_ANY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002612 txn->status = 502;
2613 client_return(t, error_message(t, HTTP_ERR_502));
2614 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002615 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002616 if (!(t->flags & SN_FINST_MASK))
2617 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002618
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002619 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2620 process_srv_queue(t->srv);
2621
2622 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002623 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002624 /* write error to client, read error or close from server */
2625 if (req->flags & BF_WRITE_ERROR ||
2626 rep->flags & (BF_READ_ERROR | BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002627 buffer_shutr_done(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002628 buffer_shutw_done(req);
2629 fd_delete(t->srv_fd);
2630 if (t->srv) {
2631 t->srv->cur_sess--;
2632 t->srv->failed_resp++;
2633 sess_change_server(t, NULL);
2634 }
2635 t->be->failed_resp++;
2636 t->srv_state = SV_STCLOSE;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002637 t->analysis &= ~AN_RTR_ANY;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002638 txn->status = 502;
2639 client_return(t, error_message(t, HTTP_ERR_502));
2640 if (!(t->flags & SN_ERR_MASK))
2641 t->flags |= SN_ERR_SRVCL;
2642 if (!(t->flags & SN_FINST_MASK))
2643 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002644
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002645 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2646 process_srv_queue(t->srv);
2647
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002648 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002649 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002650 /* too large response does not fit in buffer. */
Willy Tarreau461f6622008-08-15 23:43:19 +02002651 else if (rep->l >= rep->rlim - rep->data) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002652 goto hdr_response_bad;
Willy Tarreau461f6622008-08-15 23:43:19 +02002653 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002654 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002655 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002656 buffer_shutr_done(rep);
2657 buffer_shutw_done(req);
2658 fd_delete(t->srv_fd);
2659 if (t->srv) {
2660 t->srv->cur_sess--;
2661 t->srv->failed_resp++;
2662 sess_change_server(t, NULL);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002663 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002664 t->be->failed_resp++;
2665 t->srv_state = SV_STCLOSE;
2666 t->analysis &= ~AN_RTR_ANY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002667 txn->status = 504;
2668 client_return(t, error_message(t, HTTP_ERR_504));
2669 if (!(t->flags & SN_ERR_MASK))
2670 t->flags |= SN_ERR_SRVTO;
2671 if (!(t->flags & SN_FINST_MASK))
2672 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002673
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002674 if (t->srv && may_dequeue_tasks(t->srv, t->be))
2675 process_srv_queue(t->srv);
2676 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002677 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002678 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002679 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002680
Willy Tarreau21d2af32008-02-14 20:25:24 +01002681
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002682 /*****************************************************************
2683 * More interesting part now : we know that we have a complete *
2684 * response which at least looks like HTTP. We have an indicator *
2685 * of each header's length, so we can parse them quickly. *
2686 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002687
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002688 /* ensure we keep this pointer to the beginning of the message */
2689 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002690
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002691 /*
2692 * 1: get the status code and check for cacheability.
2693 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002694
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002695 t->logs.logwait &= ~LW_RESP;
2696 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002697
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002698 switch (txn->status) {
2699 case 200:
2700 case 203:
2701 case 206:
2702 case 300:
2703 case 301:
2704 case 410:
2705 /* RFC2616 @13.4:
2706 * "A response received with a status code of
2707 * 200, 203, 206, 300, 301 or 410 MAY be stored
2708 * by a cache (...) unless a cache-control
2709 * directive prohibits caching."
2710 *
2711 * RFC2616 @9.5: POST method :
2712 * "Responses to this method are not cacheable,
2713 * unless the response includes appropriate
2714 * Cache-Control or Expires header fields."
2715 */
2716 if (likely(txn->meth != HTTP_METH_POST) &&
2717 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2718 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2719 break;
2720 default:
2721 break;
2722 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002723
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002724 /*
2725 * 2: we may need to capture headers
2726 */
2727 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2728 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2729 txn->rsp.cap, t->fe->rsp_cap);
2730
2731 /*
2732 * 3: we will have to evaluate the filters.
2733 * As opposed to version 1.2, now they will be evaluated in the
2734 * filters order and not in the header order. This means that
2735 * each filter has to be validated among all headers.
2736 *
2737 * Filters are tried with ->be first, then with ->fe if it is
2738 * different from ->be.
2739 */
2740
2741 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2742
2743 cur_proxy = t->be;
2744 while (1) {
2745 struct proxy *rule_set = cur_proxy;
2746
2747 /* try headers filters */
2748 if (rule_set->rsp_exp != NULL) {
2749 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2750 return_bad_resp:
2751 if (t->srv) {
2752 t->srv->cur_sess--;
2753 t->srv->failed_resp++;
2754 sess_change_server(t, NULL);
2755 }
2756 cur_proxy->failed_resp++;
2757 return_srv_prx_502:
2758 buffer_shutr_done(rep);
2759 buffer_shutw_done(req);
2760 fd_delete(t->srv_fd);
2761 t->srv_state = SV_STCLOSE;
2762 t->analysis &= ~AN_RTR_ANY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002763 txn->status = 502;
2764 client_return(t, error_message(t, HTTP_ERR_502));
2765 if (!(t->flags & SN_ERR_MASK))
2766 t->flags |= SN_ERR_PRXCOND;
2767 if (!(t->flags & SN_FINST_MASK))
2768 t->flags |= SN_FINST_H;
2769 /* We used to have a free connection slot. Since we'll never use it,
2770 * we have to inform the server that it may be used by another session.
2771 */
2772 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
2773 process_srv_queue(t->srv);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002774 return 1;
2775 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002776 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002777
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002778 /* has the response been denied ? */
2779 if (txn->flags & TX_SVDENY) {
Willy Tarreauf899b942008-03-28 18:09:38 +01002780 if (t->srv) {
2781 t->srv->cur_sess--;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002782 t->srv->failed_secu++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002783 sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002784 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002785 cur_proxy->denied_resp++;
2786 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002787 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002788
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002789 /* We might have to check for "Connection:" */
2790 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2791 !(t->flags & SN_CONN_CLOSED)) {
2792 char *cur_ptr, *cur_end, *cur_next;
2793 int cur_idx, old_idx, delta, val;
2794 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002795
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002796 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2797 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002798
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002799 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2800 cur_hdr = &txn->hdr_idx.v[cur_idx];
2801 cur_ptr = cur_next;
2802 cur_end = cur_ptr + cur_hdr->len;
2803 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002804
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002805 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2806 if (val) {
2807 /* 3 possibilities :
2808 * - we have already set Connection: close,
2809 * so we remove this line.
2810 * - we have not yet set Connection: close,
2811 * but this line indicates close. We leave
2812 * it untouched and set the flag.
2813 * - we have not yet set Connection: close,
2814 * and this line indicates non-close. We
2815 * replace it.
2816 */
2817 if (t->flags & SN_CONN_CLOSED) {
2818 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2819 txn->rsp.eoh += delta;
2820 cur_next += delta;
2821 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2822 txn->hdr_idx.used--;
2823 cur_hdr->len = 0;
2824 } else {
2825 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2826 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2827 "close", 5);
2828 cur_next += delta;
2829 cur_hdr->len += delta;
2830 txn->rsp.eoh += delta;
2831 }
2832 t->flags |= SN_CONN_CLOSED;
2833 }
2834 }
2835 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002836 }
2837 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002838
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002839 /* add response headers from the rule sets in the same order */
2840 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2841 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2842 rule_set->rsp_add[cur_idx])) < 0)
2843 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002844 }
2845
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002846 /* check whether we're already working on the frontend */
2847 if (cur_proxy == t->fe)
2848 break;
2849 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002850 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002851
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002852 /*
2853 * 4: check for server cookie.
2854 */
2855 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
2856 || (t->be->options & PR_O_CHK_CACHE))
2857 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002858
Willy Tarreaubaaee002006-06-26 02:48:02 +02002859
Willy Tarreaua15645d2007-03-18 16:22:39 +01002860 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002861 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002862 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002863 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2864 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002865
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002866 /*
2867 * 6: add server cookie in the response if needed
2868 */
2869 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2870 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2871 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002872
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002873 /* the server is known, it's not the one the client requested, we have to
2874 * insert a set-cookie here, except if we want to insert only on POST
2875 * requests and this one isn't. Note that servers which don't have cookies
2876 * (eg: some backup servers) will return a full cookie removal request.
2877 */
2878 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2879 t->be->cookie_name,
2880 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002881
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002882 if (t->be->cookie_domain)
2883 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002884
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002885 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2886 trash, len)) < 0)
2887 goto return_bad_resp;
2888 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002889
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002890 /* Here, we will tell an eventual cache on the client side that we don't
2891 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2892 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2893 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2894 */
2895 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002896
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002897 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2898
2899 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2900 "Cache-control: private", 22)) < 0)
2901 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002902 }
2903 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002904
Willy Tarreaubaaee002006-06-26 02:48:02 +02002905
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002906 /*
2907 * 7: check if result will be cacheable with a cookie.
2908 * We'll block the response if security checks have caught
2909 * nasty things such as a cacheable cookie.
2910 */
2911 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2912 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2913 (t->be->options & PR_O_CHK_CACHE)) {
2914
2915 /* we're in presence of a cacheable response containing
2916 * a set-cookie header. We'll block it as requested by
2917 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002918 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002919 if (t->srv) {
2920 t->srv->cur_sess--;
2921 t->srv->failed_secu++;
2922 sess_change_server(t, NULL);
2923 }
2924 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002925
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002926 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2927 t->be->id, t->srv?t->srv->id:"<dispatch>");
2928 send_log(t->be, LOG_ALERT,
2929 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2930 t->be->id, t->srv?t->srv->id:"<dispatch>");
2931 goto return_srv_prx_502;
2932 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002933
2934 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002935 * 8: add "Connection: close" if needed and not yet set.
2936 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002937 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002938 if (!(t->flags & SN_CONN_CLOSED) &&
2939 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2940 if ((unlikely(msg->sl.st.v_l != 8) ||
2941 unlikely(req->data[msg->som + 7] != '0')) &&
2942 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2943 "Connection: close", 17)) < 0)
2944 goto return_bad_resp;
2945 t->flags |= SN_CONN_CLOSED;
2946 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002947
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002948 /*************************************************************
2949 * OK, that's finished for the headers. We have done what we *
2950 * could. Let's switch to the DATA state. *
2951 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002952
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002953 t->srv_state = SV_STDATA;
2954 t->analysis &= ~AN_RTR_ANY;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002955 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
2956 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002957
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002958#ifdef CONFIG_HAP_TCPSPLICE
2959 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
2960 /* TCP splicing supported by both FE and BE */
2961 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
2962 }
2963#endif
2964 /* if the user wants to log as soon as possible, without counting
2965 * bytes from the server, then this is the right moment. We have
2966 * to temporarily assign bytes_out to log what we currently have.
2967 */
2968 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2969 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
2970 t->logs.bytes_out = txn->rsp.eoh;
2971 if (t->fe->to_log & LW_REQ)
2972 http_sess_log(t);
2973 else
2974 tcp_sess_log(t);
2975 t->logs.bytes_out = 0;
2976 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002977
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002978 /* Note: we must not try to cheat by jumping directly to DATA,
2979 * otherwise we would not let the client side wake up.
2980 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01002981
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002982 return 1;
2983 }
2984 return 0;
2985}
Willy Tarreaua15645d2007-03-18 16:22:39 +01002986
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002987/*
2988 * manages the client FSM and its socket. BTW, it also tries to handle the
2989 * cookie. It returns 1 if a state has changed (and a resync may be needed),
2990 * 0 else.
2991 */
2992int process_cli(struct session *t)
2993{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002994 struct buffer *req = t->req;
2995 struct buffer *rep = t->rep;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002996
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002997 DPRINTF(stderr,"[%u] process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002998 now_ms,
2999 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
3000 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
3001 req->rex, rep->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003002 req->flags, rep->flags,
3003 req->l, rep->l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003004
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003005 /* if no analysis remains, it's time to forward the connection */
3006 if (!(t->analysis & AN_REQ_ANY) && !(req->flags & (BF_MAY_CONNECT|BF_MAY_FORWARD)))
3007 req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003008
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003009 /* FIXME: we still have to check for CL_STSHUTR because client_retnclose
3010 * still set this state (and will do until unix sockets are converted).
3011 */
3012 if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003013 /* read or write error */
3014 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
3015 buffer_shutr_done(req);
3016 buffer_shutw_done(rep);
3017 fd_delete(t->cli_fd);
3018 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003019 trace_term(t, TT_HTTP_CLI_1);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003020 if (!(t->flags & SN_ERR_MASK))
3021 t->flags |= SN_ERR_CLICL;
3022 if (!(t->flags & SN_FINST_MASK)) {
3023 if (t->analysis & AN_REQ_ANY)
3024 t->flags |= SN_FINST_R;
3025 else if (t->pend_pos)
3026 t->flags |= SN_FINST_Q;
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003027 else if (t->srv_state == SV_STCONN)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003028 t->flags |= SN_FINST_C;
3029 else
3030 t->flags |= SN_FINST_D;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003031 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003032 return 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003033 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003034 /* last read, or end of server write */
3035 else if (!(req->flags & BF_SHUTR_STATUS) && /* already done */
3036 req->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
3037 buffer_shutr_done(req);
3038 if (!(rep->flags & BF_SHUTW_STATUS)) {
3039 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003040 trace_term(t, TT_HTTP_CLI_2);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003041 } else {
3042 /* output was already closed */
3043 fd_delete(t->cli_fd);
3044 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003045 trace_term(t, TT_HTTP_CLI_3);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003046 }
3047 return 1;
3048 }
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003049 /* last server read and buffer empty : we only check them when we're
3050 * allowed to forward the data.
3051 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003052 else if (!(rep->flags & BF_SHUTW_STATUS) && /* already done */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003053 rep->l == 0 && rep->flags & BF_MAY_FORWARD &&
3054 rep->flags & BF_SHUTR_STATUS && !(t->flags & SN_SELF_GEN)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003055 buffer_shutw_done(rep);
3056 if (!(req->flags & BF_SHUTR_STATUS)) {
3057 EV_FD_CLR(t->cli_fd, DIR_WR);
3058 shutdown(t->cli_fd, SHUT_WR);
3059 /* We must ensure that the read part is still alive when switching to shutw */
3060 /* FIXME: is this still true ? */
3061 EV_FD_SET(t->cli_fd, DIR_RD);
3062 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauf8533202008-08-16 14:55:08 +02003063 trace_term(t, TT_HTTP_CLI_4);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003064 } else {
3065 fd_delete(t->cli_fd);
3066 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003067 trace_term(t, TT_HTTP_CLI_5);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003068 }
3069 return 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003070 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003071 /* read timeout */
3072 else if (tick_is_expired(req->rex, now_ms)) {
3073 buffer_shutr_done(req);
3074 req->flags |= BF_READ_TIMEOUT;
3075 if (!(rep->flags & BF_SHUTW_STATUS)) {
3076 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003077 trace_term(t, TT_HTTP_CLI_6);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003078 } else {
3079 /* output was already closed */
3080 fd_delete(t->cli_fd);
3081 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003082 trace_term(t, TT_HTTP_CLI_7);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003083 }
3084 if (!(t->flags & SN_ERR_MASK))
3085 t->flags |= SN_ERR_CLITO;
3086 if (!(t->flags & SN_FINST_MASK)) {
3087 if (t->analysis & AN_REQ_ANY)
3088 t->flags |= SN_FINST_R;
3089 else if (t->pend_pos)
3090 t->flags |= SN_FINST_Q;
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003091 else if (t->srv_state == SV_STCONN)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003092 t->flags |= SN_FINST_C;
3093 else
3094 t->flags |= SN_FINST_D;
3095 }
3096 return 1;
3097 }
3098 /* write timeout */
3099 else if (tick_is_expired(rep->wex, now_ms)) {
3100 buffer_shutw_done(rep);
3101 rep->flags |= BF_WRITE_TIMEOUT;
3102 if (!(req->flags & BF_SHUTR_STATUS)) {
3103 EV_FD_CLR(t->cli_fd, DIR_WR);
3104 shutdown(t->cli_fd, SHUT_WR);
3105 /* We must ensure that the read part is still alive when switching to shutw */
3106 /* FIXME: is this still true ? */
3107 EV_FD_SET(t->cli_fd, DIR_RD);
3108 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauf8533202008-08-16 14:55:08 +02003109 trace_term(t, TT_HTTP_CLI_8);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003110 } else {
3111 fd_delete(t->cli_fd);
3112 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003113 trace_term(t, TT_HTTP_CLI_9);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003114 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003115
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003116 if (!(t->flags & SN_ERR_MASK))
3117 t->flags |= SN_ERR_CLITO;
3118 if (!(t->flags & SN_FINST_MASK)) {
3119 if (t->analysis & AN_REQ_ANY)
3120 t->flags |= SN_FINST_R;
3121 else if (t->pend_pos)
3122 t->flags |= SN_FINST_Q;
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003123 else if (t->srv_state == SV_STCONN)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003124 t->flags |= SN_FINST_C;
3125 else
3126 t->flags |= SN_FINST_D;
3127 }
3128 return 1;
3129 }
3130
3131 /* manage read timeout */
3132 if (!(req->flags & BF_SHUTR_STATUS)) {
3133 if (req->l >= req->rlim - req->data) {
3134 /* no room to read more data */
3135 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
3136 /* stop reading until we get some space */
3137 req->rex = TICK_ETERNITY;
3138 }
3139 } else {
3140 EV_FD_COND_S(t->cli_fd, DIR_RD);
3141 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
3142 }
3143 }
3144
3145 /* manage write timeout */
3146 if (!(rep->flags & BF_SHUTW_STATUS)) {
3147 /* first, we may have to produce data (eg: stats).
3148 * right now, this is limited to the SHUTR state.
3149 */
3150 if (req->flags & BF_SHUTR_STATUS && t->flags & SN_SELF_GEN) {
3151 produce_content(t);
3152 if (rep->l == 0) {
3153 buffer_shutw_done(rep);
3154 fd_delete(t->cli_fd);
3155 t->cli_state = CL_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003156 trace_term(t, TT_HTTP_CLI_10);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003157 return 1;
3158 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003159 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003160
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003161 /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
3162 if ((rep->l == 0) || !(rep->flags & BF_MAY_FORWARD)) {
3163 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
3164 /* stop writing */
3165 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003166 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003167 } else {
3168 /* buffer not empty */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003169 EV_FD_COND_S(t->cli_fd, DIR_WR);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003170 if (!tick_isset(rep->wex)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003171 /* restart writing */
3172 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
3173 if (!(req->flags & BF_SHUTR_STATUS) && tick_isset(rep->wex) && tick_isset(req->rex)) {
3174 /* FIXME: to prevent the client from expiring read timeouts during writes,
3175 * we refresh it, except if it was already infinite. */
3176 req->rex = rep->wex;
3177 }
3178 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003179 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003180 }
3181 return 0; /* other cases change nothing */
3182 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003183 else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003184 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3185 int len;
3186 len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
3187 write(1, trash, len);
3188 }
3189 return 0;
3190 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003191#ifdef DEBUG_FULL
3192 else {
3193 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state);
3194 exit(1);
3195 }
3196#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003197 return 0;
3198}
Willy Tarreaubaaee002006-06-26 02:48:02 +02003199
Willy Tarreaubaaee002006-06-26 02:48:02 +02003200
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003201/*
3202 * manages the server FSM and its socket. It returns 1 if a state has changed
3203 * (and a resync may be needed), 0 else.
3204 */
3205int process_srv(struct session *t)
3206{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003207 struct http_txn *txn = &t->txn;
3208 struct buffer *req = t->req;
3209 struct buffer *rep = t->rep;
3210 int conn_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003211
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003212 DPRINTF(stderr,"[%u] process_srv: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n",
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003213 now_ms,
3214 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
3215 EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR),
3216 rep->rex, req->wex,
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003217 req->flags, rep->flags,
3218 req->l, rep->l);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003219
Willy Tarreau461f6622008-08-15 23:43:19 +02003220 /* if no analysis remains, we have to let the data pass */
3221 if (!(t->analysis & AN_RTR_ANY) && !(rep->flags & BF_MAY_FORWARD))
3222 rep->flags |= BF_MAY_FORWARD;
3223
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003224 if (t->srv_state == SV_STIDLE) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003225 if ((rep->flags & BF_SHUTW_STATUS) ||
3226 ((req->flags & BF_SHUTR_STATUS) &&
3227 (req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3228 req->cex = TICK_ETERNITY;
3229 if (t->pend_pos)
3230 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3231 /* note that this must not return any error because it would be able to
3232 * overwrite the client_retnclose() output.
3233 */
3234 if (txn->flags & TX_CLTARPIT)
3235 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
3236 else
3237 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
3238
Willy Tarreauf8533202008-08-16 14:55:08 +02003239 trace_term(t, TT_HTTP_SRV_1);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003240 return 1;
3241 }
3242 else if (req->flags & BF_MAY_CONNECT) {
3243 /* the client allows the server to connect */
3244 if (txn->flags & TX_CLTARPIT) {
3245 /* This connection is being tarpitted. The CLIENT side has
3246 * already set the connect expiration date to the right
3247 * timeout. We just have to check that it has not expired.
3248 */
3249 if (!tick_is_expired(req->cex, now_ms))
3250 return 0;
3251
3252 /* We will set the queue timer to the time spent, just for
3253 * logging purposes. We fake a 500 server error, so that the
3254 * attacker will not suspect his connection has been tarpitted.
3255 * It will not cause trouble to the logs because we can exclude
3256 * the tarpitted connections by filtering on the 'PT' status flags.
3257 */
3258 req->cex = TICK_ETERNITY;
3259 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3260 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
3261 500, error_message(t, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003262 trace_term(t, TT_HTTP_SRV_2);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003263 return 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003264 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003265
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003266 /* Right now, we will need to create a connection to the server.
3267 * We might already have tried, and got a connection pending, in
3268 * which case we will not do anything till it's pending. It's up
3269 * to any other session to release it and wake us up again.
3270 */
3271 if (t->pend_pos) {
3272 if (!tick_is_expired(req->cex, now_ms)) {
3273 return 0;
3274 } else {
3275 /* we've been waiting too long here */
3276 req->cex = TICK_ETERNITY;
3277 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3278 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
3279 503, error_message(t, HTTP_ERR_503));
Willy Tarreauf8533202008-08-16 14:55:08 +02003280 trace_term(t, TT_HTTP_SRV_3);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003281 if (t->srv)
3282 t->srv->failed_conns++;
3283 t->be->failed_conns++;
3284 return 1;
3285 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003286 }
3287
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003288 do {
3289 /* first, get a connection */
3290 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3291 t->flags |= SN_REDIRECTABLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003292
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003293 if (srv_redispatch_connect(t))
3294 return t->srv_state != SV_STIDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003295
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003296 if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) {
3297 /* Server supporting redirection and it is possible.
3298 * Invalid requests are reported as such. It concerns all
3299 * the largest ones.
3300 */
3301 struct chunk rdr;
3302 char *path;
3303 int len;
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003304
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003305 /* 1: create the response header */
3306 rdr.len = strlen(HTTP_302);
3307 rdr.str = trash;
3308 memcpy(rdr.str, HTTP_302, rdr.len);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003309
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003310 /* 2: add the server's prefix */
3311 if (rdr.len + t->srv->rdr_len > sizeof(trash))
3312 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003313
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003314 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
3315 rdr.len += t->srv->rdr_len;
3316
3317 /* 3: add the request URI */
3318 path = http_get_path(txn);
3319 if (!path)
3320 goto cancel_redir;
3321 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
3322 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
3323 goto cancel_redir;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003324
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003325 memcpy(rdr.str + rdr.len, path, len);
3326 rdr.len += len;
3327 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3328 rdr.len += 4;
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003329
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003330 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauf8533202008-08-16 14:55:08 +02003331 trace_term(t, TT_HTTP_SRV_3);
3332
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003333 /* FIXME: we should increase a counter of redirects per server and per backend. */
3334 if (t->srv)
3335 t->srv->cum_sess++;
3336 return 1;
3337 cancel_redir:
3338 txn->status = 400;
3339 t->fe->failed_req++;
3340 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
3341 400, error_message(t, HTTP_ERR_400));
Willy Tarreauf8533202008-08-16 14:55:08 +02003342 trace_term(t, TT_HTTP_SRV_4);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003343 return 1;
3344 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003345
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003346 /* try to (re-)connect to the server, and fail if we expire the
3347 * number of retries.
3348 */
3349 if (srv_retryable_connect(t)) {
3350 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3351 return t->srv_state != SV_STIDLE;
3352 }
3353 } while (1);
3354 }
3355 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003356 else if (t->srv_state == SV_STCONN) { /* connection in progress */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003357 if ((rep->flags & BF_SHUTW_STATUS) ||
3358 ((req->flags & BF_SHUTR_STATUS) &&
3359 ((req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
3360 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
3361 req->cex = TICK_ETERNITY;
3362 if (!(t->flags & SN_CONN_TAR)) {
3363 /* if we are in turn-around, we have already closed the FD */
3364 fd_delete(t->srv_fd);
3365 if (t->srv) {
3366 t->srv->cur_sess--;
3367 sess_change_server(t, NULL);
3368 }
3369 }
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003370
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003371 /* note that this must not return any error because it would be able to
3372 * overwrite the client_retnclose() output.
3373 */
3374 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreauf8533202008-08-16 14:55:08 +02003375 trace_term(t, TT_HTTP_SRV_5);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003376 return 1;
3377 }
3378 if (!(req->flags & BF_WRITE_STATUS) && !tick_is_expired(req->cex, now_ms)) {
3379 return 0; /* nothing changed */
3380 }
3381 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
3382 /* timeout, asynchronous connect error or first write error */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003383 if (t->flags & SN_CONN_TAR) {
3384 /* We are doing a turn-around waiting for a new connection attempt. */
3385 if (!tick_is_expired(req->cex, now_ms))
3386 return 0;
3387 t->flags &= ~SN_CONN_TAR;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003388 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003389 else {
3390 fd_delete(t->srv_fd);
3391 if (t->srv) {
3392 t->srv->cur_sess--;
3393 sess_change_server(t, NULL);
3394 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003395
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003396 if (!(req->flags & BF_WRITE_STATUS))
3397 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
3398 else
3399 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003400
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003401 /* ensure that we have enough retries left */
3402 if (srv_count_retry_down(t, conn_err))
3403 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003404
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003405 if (req->flags & BF_WRITE_ERROR) {
3406 /* we encountered an immediate connection error, and we
3407 * will have to retry connecting to the same server, most
3408 * likely leading to the same result. To avoid this, we
3409 * fake a connection timeout to retry after a turn-around
3410 * time of 1 second. We will wait in the previous if block.
3411 */
3412 t->flags |= SN_CONN_TAR;
3413 req->cex = tick_add(now_ms, MS_TO_TICKS(1000));
3414 return 0;
3415 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003416 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003417
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003418 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
3419 /* We're on our last chance, and the REDISP option was specified.
3420 * We will ignore cookie and force to balance or use the dispatcher.
3421 */
3422 /* let's try to offer this slot to anybody */
3423 if (may_dequeue_tasks(t->srv, t->be))
3424 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003425
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003426 /* it's left to the dispatcher to choose a server */
3427 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
3428 t->prev_srv = t->srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003429
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003430 /* first, get a connection */
3431 if (srv_redispatch_connect(t))
3432 return t->srv_state != SV_STCONN;
3433 } else {
3434 if (t->srv)
3435 t->srv->retries++;
3436 t->be->retries++;
3437 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003438
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003439 do {
3440 /* Now we will try to either reconnect to the same server or
3441 * connect to another server. If the connection gets queued
3442 * because all servers are saturated, then we will go back to
3443 * the SV_STIDLE state.
3444 */
3445 if (srv_retryable_connect(t)) {
3446 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
3447 return t->srv_state != SV_STCONN;
3448 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003449
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003450 /* we need to redispatch the connection to another server */
3451 if (srv_redispatch_connect(t))
3452 return t->srv_state != SV_STCONN;
3453 } while (1);
3454 }
3455 else { /* no error or write 0 */
3456 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003457
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003458 if (req->l == 0) /* nothing to write */ {
3459 EV_FD_CLR(t->srv_fd, DIR_WR);
3460 req->wex = TICK_ETERNITY;
3461 } else /* need the right to write */ {
3462 EV_FD_SET(t->srv_fd, DIR_WR);
3463 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3464 if (tick_isset(req->wex)) {
3465 /* FIXME: to prevent the server from expiring read timeouts during writes,
3466 * we refresh it. */
3467 rep->rex = req->wex;
3468 }
3469 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003470
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003471 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
3472 EV_FD_SET(t->srv_fd, DIR_RD);
3473 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
3474 t->srv_state = SV_STDATA;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003475 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003476
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003477 /* if the user wants to log as soon as possible, without counting
3478 bytes from the server, then this is the right moment. */
3479 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3480 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
3481 tcp_sess_log(t);
3482 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003483#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003484 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3485 /* TCP splicing supported by both FE and BE */
3486 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3487 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003488#endif
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003489 }
3490 else {
3491 t->srv_state = SV_STDATA;
3492 t->analysis |= AN_RTR_HTTP_HDR;
3493 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
3494 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3495 /* reset hdr_idx which was already initialized by the request.
3496 * right now, the http parser does it.
3497 * hdr_idx_init(&t->txn.hdr_idx);
3498 */
3499 }
3500 req->cex = TICK_ETERNITY;
3501 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003502 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003503 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003504 else if (t->srv_state == SV_STDATA) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003505 /* read or write error */
Willy Tarreau461f6622008-08-15 23:43:19 +02003506 /* FIXME: what happens when we have to deal with HTTP ??? */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003507 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003508 buffer_shutr_done(rep);
3509 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003510 fd_delete(t->srv_fd);
3511 if (t->srv) {
3512 t->srv->cur_sess--;
3513 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003514 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003515 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003516 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003517 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003518 trace_term(t, TT_HTTP_SRV_6);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003519 if (!(t->flags & SN_ERR_MASK))
3520 t->flags |= SN_ERR_SRVCL;
3521 if (!(t->flags & SN_FINST_MASK))
3522 t->flags |= SN_FINST_D;
Willy Tarreau461f6622008-08-15 23:43:19 +02003523
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003524 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003525 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003526
3527 return 1;
3528 }
3529 /* last read, or end of client write */
Willy Tarreau461f6622008-08-15 23:43:19 +02003530 else if (!(rep->flags & BF_SHUTR_STATUS) && /* not already done */
3531 rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003532 buffer_shutr_done(rep);
Willy Tarreau461f6622008-08-15 23:43:19 +02003533 if (!(req->flags & BF_SHUTW_STATUS)) {
3534 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003535 trace_term(t, TT_HTTP_SRV_7);
Willy Tarreau461f6622008-08-15 23:43:19 +02003536 } else {
3537 /* output was already closed */
3538 fd_delete(t->srv_fd);
3539 if (t->srv) {
3540 t->srv->cur_sess--;
3541 sess_change_server(t, NULL);
3542 }
3543 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003544 trace_term(t, TT_HTTP_SRV_8);
Willy Tarreau461f6622008-08-15 23:43:19 +02003545
3546 if (may_dequeue_tasks(t->srv, t->be))
3547 process_srv_queue(t->srv);
3548 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003549 return 1;
3550 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003551 /* end of client read and no more data to send. We can forward
3552 * the close when we're allowed to forward data (anytime right
3553 * now). If we're using option forceclose, then we may also
3554 * shutdown the outgoing write channel once the response starts
3555 * coming from the server.
3556 */
3557 else if (!(req->flags & BF_SHUTW_STATUS) && /* not already done */
3558 req->l == 0 && req->flags & BF_MAY_FORWARD &&
3559 (req->flags & BF_SHUTR_STATUS ||
3560 (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003561 buffer_shutw_done(req);
Willy Tarreau461f6622008-08-15 23:43:19 +02003562 if (!(rep->flags & BF_SHUTR_STATUS)) {
3563 EV_FD_CLR(t->srv_fd, DIR_WR);
3564 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreauf8533202008-08-16 14:55:08 +02003565 trace_term(t, TT_HTTP_SRV_9);
Willy Tarreau461f6622008-08-15 23:43:19 +02003566 /* We must ensure that the read part is still alive when switching to shutw */
3567 /* FIXME: is this still true ? */
3568 EV_FD_SET(t->srv_fd, DIR_RD);
3569 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau461f6622008-08-15 23:43:19 +02003570 } else {
3571 fd_delete(t->srv_fd);
3572 if (t->srv) {
3573 t->srv->cur_sess--;
3574 sess_change_server(t, NULL);
3575 }
3576 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003577 trace_term(t, TT_HTTP_SRV_10);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003578
Willy Tarreau461f6622008-08-15 23:43:19 +02003579 if (may_dequeue_tasks(t->srv, t->be))
3580 process_srv_queue(t->srv);
3581 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003582 return 1;
3583 }
3584 /* read timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003585 else if (tick_is_expired(rep->rex, now_ms)) {
Willy Tarreau461f6622008-08-15 23:43:19 +02003586 buffer_shutr_done(rep);
3587 rep->flags |= BF_READ_TIMEOUT;
3588 if (!(req->flags & BF_SHUTW_STATUS)) {
3589 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreauf8533202008-08-16 14:55:08 +02003590 trace_term(t, TT_HTTP_SRV_11);
Willy Tarreau461f6622008-08-15 23:43:19 +02003591 } else {
3592 fd_delete(t->srv_fd);
3593 if (t->srv) {
3594 t->srv->cur_sess--;
3595 sess_change_server(t, NULL);
3596 }
3597 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003598 trace_term(t, TT_HTTP_SRV_12);
Willy Tarreau461f6622008-08-15 23:43:19 +02003599
3600 if (may_dequeue_tasks(t->srv, t->be))
3601 process_srv_queue(t->srv);
3602 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003603 if (!(t->flags & SN_ERR_MASK))
3604 t->flags |= SN_ERR_SRVTO;
3605 if (!(t->flags & SN_FINST_MASK))
3606 t->flags |= SN_FINST_D;
3607 return 1;
3608 }
3609 /* write timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003610 else if (tick_is_expired(req->wex, now_ms)) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003611 buffer_shutw_done(req);
Willy Tarreau461f6622008-08-15 23:43:19 +02003612 req->flags |= BF_WRITE_TIMEOUT;
3613 if (!(rep->flags & BF_SHUTR_STATUS)) {
3614 EV_FD_CLR(t->srv_fd, DIR_WR);
3615 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreauf8533202008-08-16 14:55:08 +02003616 trace_term(t, TT_HTTP_SRV_13);
Willy Tarreau461f6622008-08-15 23:43:19 +02003617 /* We must ensure that the read part is still alive when switching to shutw */
3618 /* FIXME: is this still needed ? */
3619 EV_FD_SET(t->srv_fd, DIR_RD);
3620 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau461f6622008-08-15 23:43:19 +02003621 } else {
3622 fd_delete(t->srv_fd);
3623 if (t->srv) {
3624 t->srv->cur_sess--;
3625 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003626 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003627 t->srv_state = SV_STCLOSE;
Willy Tarreauf8533202008-08-16 14:55:08 +02003628 trace_term(t, TT_HTTP_SRV_14);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003629
Willy Tarreau461f6622008-08-15 23:43:19 +02003630 if (may_dequeue_tasks(t->srv, t->be))
3631 process_srv_queue(t->srv);
Willy Tarreau51406232008-03-10 22:04:20 +01003632 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003633 if (!(t->flags & SN_ERR_MASK))
3634 t->flags |= SN_ERR_SRVTO;
3635 if (!(t->flags & SN_FINST_MASK))
3636 t->flags |= SN_FINST_D;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003637 return 1;
3638 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003639
Willy Tarreau461f6622008-08-15 23:43:19 +02003640 /* manage read timeout */
3641 if (!(rep->flags & BF_SHUTR_STATUS)) {
3642 if (rep->l >= rep->rlim - rep->data) {
3643 if (EV_FD_COND_C(t->srv_fd, DIR_RD))
3644 rep->rex = TICK_ETERNITY;
3645 } else {
3646 EV_FD_COND_S(t->srv_fd, DIR_RD);
3647 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreau51406232008-03-10 22:04:20 +01003648 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003649 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003650
Willy Tarreau461f6622008-08-15 23:43:19 +02003651 /* manage write timeout */
3652 if (!(req->flags & BF_SHUTW_STATUS)) {
3653 if (req->l == 0 || !(req->flags & BF_MAY_FORWARD)) {
3654 /* stop writing */
3655 if (EV_FD_COND_C(t->srv_fd, DIR_WR))
3656 req->wex = TICK_ETERNITY;
3657 } else {
3658 /* buffer not empty, there are still data to be transferred */
3659 EV_FD_COND_S(t->srv_fd, DIR_WR);
3660 if (!tick_isset(req->wex)) {
3661 /* restart writing */
3662 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
3663 if (!(rep->flags & BF_SHUTR_STATUS) && tick_isset(req->wex) && tick_isset(rep->rex)) {
3664 /* FIXME: to prevent the server from expiring read timeouts during writes,
3665 * we refresh it, except if it was already infinite.
3666 */
3667 rep->rex = req->wex;
3668 }
3669 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003670 }
3671 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003672 return 0; /* other cases change nothing */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003673 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003674 else if (t->srv_state == SV_STCLOSE) { /* SV_STCLOSE : nothing to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003675 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3676 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003677 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003678 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003679 write(1, trash, len);
3680 }
3681 return 0;
3682 }
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003683#ifdef DEBUG_FULL
3684 else {
3685 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->srv_state);
3686 exit(1);
3687 }
3688#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003689 return 0;
3690}
3691
3692
3693/*
3694 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003695 * called with client socket shut down on input. Right now, only statistics can
3696 * be produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
Willy Tarreaubaaee002006-06-26 02:48:02 +02003697 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003698 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003699 * when it wants to stop sending data, otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003700 */
3701int produce_content(struct session *s)
3702{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003703 if (s->data_source == DATA_SRC_NONE) {
3704 s->flags &= ~SN_SELF_GEN;
3705 return 1;
3706 }
3707 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003708 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003709 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003710 if (ret >= 0)
3711 return ret;
3712 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003713 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003714
Willy Tarreau91861262007-10-17 17:06:05 +02003715 /* unknown data source or internal error */
3716 s->txn.status = 500;
3717 client_retnclose(s, error_message(s, HTTP_ERR_500));
Willy Tarreauf8533202008-08-16 14:55:08 +02003718 trace_term(s, TT_HTTP_CNT_1);
Willy Tarreau91861262007-10-17 17:06:05 +02003719 if (!(s->flags & SN_ERR_MASK))
3720 s->flags |= SN_ERR_PRXCOND;
3721 if (!(s->flags & SN_FINST_MASK))
3722 s->flags |= SN_FINST_R;
3723 s->flags &= ~SN_SELF_GEN;
3724 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003725}
3726
3727
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003728/* Iterate the same filter through all request headers.
3729 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003730 * Since it can manage the switch to another backend, it updates the per-proxy
3731 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003732 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003733int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003734{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003735 char term;
3736 char *cur_ptr, *cur_end, *cur_next;
3737 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003738 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003739 struct hdr_idx_elem *cur_hdr;
3740 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003741
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003742 last_hdr = 0;
3743
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003744 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003745 old_idx = 0;
3746
3747 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003748 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003749 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003750 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003751 (exp->action == ACT_ALLOW ||
3752 exp->action == ACT_DENY ||
3753 exp->action == ACT_TARPIT))
3754 return 0;
3755
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003756 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003757 if (!cur_idx)
3758 break;
3759
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003760 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003761 cur_ptr = cur_next;
3762 cur_end = cur_ptr + cur_hdr->len;
3763 cur_next = cur_end + cur_hdr->cr + 1;
3764
3765 /* Now we have one header between cur_ptr and cur_end,
3766 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003767 */
3768
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003769 /* The annoying part is that pattern matching needs
3770 * that we modify the contents to null-terminate all
3771 * strings before testing them.
3772 */
3773
3774 term = *cur_end;
3775 *cur_end = '\0';
3776
3777 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3778 switch (exp->action) {
3779 case ACT_SETBE:
3780 /* It is not possible to jump a second time.
3781 * FIXME: should we return an HTTP/500 here so that
3782 * the admin knows there's a problem ?
3783 */
3784 if (t->be != t->fe)
3785 break;
3786
3787 /* Swithing Proxy */
3788 t->be = (struct proxy *) exp->replace;
3789
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003790 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003791 * because we have associated req_cap and rsp_cap to the
3792 * frontend, and the beconn will be updated later.
3793 */
3794
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003795 t->rep->rto = t->req->wto = t->be->timeout.server;
3796 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003797 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003798 last_hdr = 1;
3799 break;
3800
3801 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003802 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003803 last_hdr = 1;
3804 break;
3805
3806 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003807 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003808 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003809 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003810 break;
3811
3812 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003813 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003814 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003815 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003816 break;
3817
3818 case ACT_REPLACE:
3819 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3820 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3821 /* FIXME: if the user adds a newline in the replacement, the
3822 * index will not be recalculated for now, and the new line
3823 * will not be counted as a new header.
3824 */
3825
3826 cur_end += delta;
3827 cur_next += delta;
3828 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003829 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003830 break;
3831
3832 case ACT_REMOVE:
3833 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3834 cur_next += delta;
3835
3836 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003837 txn->req.eoh += delta;
3838 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3839 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003840 cur_hdr->len = 0;
3841 cur_end = NULL; /* null-term has been rewritten */
3842 break;
3843
3844 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003845 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003846 if (cur_end)
3847 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003848
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003849 /* keep the link from this header to next one in case of later
3850 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003851 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003852 old_idx = cur_idx;
3853 }
3854 return 0;
3855}
3856
3857
3858/* Apply the filter to the request line.
3859 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3860 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003861 * Since it can manage the switch to another backend, it updates the per-proxy
3862 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003863 */
3864int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3865{
3866 char term;
3867 char *cur_ptr, *cur_end;
3868 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003869 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003870 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003871
Willy Tarreau58f10d72006-12-04 02:26:12 +01003872
Willy Tarreau3d300592007-03-18 18:34:41 +01003873 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003874 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003875 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003876 (exp->action == ACT_ALLOW ||
3877 exp->action == ACT_DENY ||
3878 exp->action == ACT_TARPIT))
3879 return 0;
3880 else if (exp->action == ACT_REMOVE)
3881 return 0;
3882
3883 done = 0;
3884
Willy Tarreau9cdde232007-05-02 20:58:19 +02003885 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003886 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003887
3888 /* Now we have the request line between cur_ptr and cur_end */
3889
3890 /* The annoying part is that pattern matching needs
3891 * that we modify the contents to null-terminate all
3892 * strings before testing them.
3893 */
3894
3895 term = *cur_end;
3896 *cur_end = '\0';
3897
3898 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3899 switch (exp->action) {
3900 case ACT_SETBE:
3901 /* It is not possible to jump a second time.
3902 * FIXME: should we return an HTTP/500 here so that
3903 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003904 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003905 if (t->be != t->fe)
3906 break;
3907
3908 /* Swithing Proxy */
3909 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003910
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003911 /* right now, the backend switch is not too much complicated
3912 * because we have associated req_cap and rsp_cap to the
3913 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003914 */
3915
Willy Tarreaud7c30f92007-12-03 01:38:36 +01003916 t->rep->rto = t->req->wto = t->be->timeout.server;
3917 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003918 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003919 done = 1;
3920 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003921
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003922 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003923 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003924 done = 1;
3925 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003926
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003927 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003928 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003929 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003930 done = 1;
3931 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003932
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003933 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003934 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003935 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003936 done = 1;
3937 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003938
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003939 case ACT_REPLACE:
3940 *cur_end = term; /* restore the string terminator */
3941 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3942 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3943 /* FIXME: if the user adds a newline in the replacement, the
3944 * index will not be recalculated for now, and the new line
3945 * will not be counted as a new header.
3946 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003947
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003948 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003949 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003950
Willy Tarreau9cdde232007-05-02 20:58:19 +02003951 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003952 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003953 HTTP_MSG_RQMETH,
3954 cur_ptr, cur_end + 1,
3955 NULL, NULL);
3956 if (unlikely(!cur_end))
3957 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003958
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003959 /* we have a full request and we know that we have either a CR
3960 * or an LF at <ptr>.
3961 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003962 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3963 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003964 /* there is no point trying this regex on headers */
3965 return 1;
3966 }
3967 }
3968 *cur_end = term; /* restore the string terminator */
3969 return done;
3970}
Willy Tarreau97de6242006-12-27 17:18:38 +01003971
Willy Tarreau58f10d72006-12-04 02:26:12 +01003972
Willy Tarreau58f10d72006-12-04 02:26:12 +01003973
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003974/*
3975 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3976 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003977 * unparsable request. Since it can manage the switch to another backend, it
3978 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003979 */
3980int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3981{
Willy Tarreau3d300592007-03-18 18:34:41 +01003982 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003983 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003984 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003985 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003986
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003987 /*
3988 * The interleaving of transformations and verdicts
3989 * makes it difficult to decide to continue or stop
3990 * the evaluation.
3991 */
3992
Willy Tarreau3d300592007-03-18 18:34:41 +01003993 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003994 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3995 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3996 exp = exp->next;
3997 continue;
3998 }
3999
4000 /* Apply the filter to the request line. */
4001 ret = apply_filter_to_req_line(t, req, exp);
4002 if (unlikely(ret < 0))
4003 return -1;
4004
4005 if (likely(ret == 0)) {
4006 /* The filter did not match the request, it can be
4007 * iterated through all headers.
4008 */
4009 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004010 }
4011 exp = exp->next;
4012 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004013 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004014}
4015
4016
Willy Tarreaua15645d2007-03-18 16:22:39 +01004017
Willy Tarreau58f10d72006-12-04 02:26:12 +01004018/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004019 * Manage client-side cookie. It can impact performance by about 2% so it is
4020 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004021 */
4022void manage_client_side_cookies(struct session *t, struct buffer *req)
4023{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004024 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004025 char *p1, *p2, *p3, *p4;
4026 char *del_colon, *del_cookie, *colon;
4027 int app_cookies;
4028
4029 appsess *asession_temp = NULL;
4030 appsess local_asession;
4031
4032 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004033 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004034
Willy Tarreau2a324282006-12-05 00:05:46 +01004035 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004036 * we start with the start line.
4037 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004038 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004039 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004040
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004041 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004042 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004043 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004044
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004045 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004046 cur_ptr = cur_next;
4047 cur_end = cur_ptr + cur_hdr->len;
4048 cur_next = cur_end + cur_hdr->cr + 1;
4049
4050 /* We have one full header between cur_ptr and cur_end, and the
4051 * next header starts at cur_next. We're only interested in
4052 * "Cookie:" headers.
4053 */
4054
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004055 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4056 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004057 old_idx = cur_idx;
4058 continue;
4059 }
4060
4061 /* Now look for cookies. Conforming to RFC2109, we have to support
4062 * attributes whose name begin with a '$', and associate them with
4063 * the right cookie, if we want to delete this cookie.
4064 * So there are 3 cases for each cookie read :
4065 * 1) it's a special attribute, beginning with a '$' : ignore it.
4066 * 2) it's a server id cookie that we *MAY* want to delete : save
4067 * some pointers on it (last semi-colon, beginning of cookie...)
4068 * 3) it's an application cookie : we *MAY* have to delete a previous
4069 * "special" cookie.
4070 * At the end of loop, if a "special" cookie remains, we may have to
4071 * remove it. If no application cookie persists in the header, we
4072 * *MUST* delete it
4073 */
4074
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004075 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004076
Willy Tarreau58f10d72006-12-04 02:26:12 +01004077 /* del_cookie == NULL => nothing to be deleted */
4078 del_colon = del_cookie = NULL;
4079 app_cookies = 0;
4080
4081 while (p1 < cur_end) {
4082 /* skip spaces and colons, but keep an eye on these ones */
4083 while (p1 < cur_end) {
4084 if (*p1 == ';' || *p1 == ',')
4085 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004086 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004087 break;
4088 p1++;
4089 }
4090
4091 if (p1 == cur_end)
4092 break;
4093
4094 /* p1 is at the beginning of the cookie name */
4095 p2 = p1;
4096 while (p2 < cur_end && *p2 != '=')
4097 p2++;
4098
4099 if (p2 == cur_end)
4100 break;
4101
4102 p3 = p2 + 1; /* skips the '=' sign */
4103 if (p3 == cur_end)
4104 break;
4105
4106 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004107 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004108 p4++;
4109
4110 /* here, we have the cookie name between p1 and p2,
4111 * and its value between p3 and p4.
4112 * we can process it :
4113 *
4114 * Cookie: NAME=VALUE;
4115 * | || || |
4116 * | || || +--> p4
4117 * | || |+-------> p3
4118 * | || +--------> p2
4119 * | |+------------> p1
4120 * | +-------------> colon
4121 * +--------------------> cur_ptr
4122 */
4123
4124 if (*p1 == '$') {
4125 /* skip this one */
4126 }
4127 else {
4128 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004129 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004130 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004131 (p4 - p1 >= t->fe->capture_namelen) &&
4132 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004133 int log_len = p4 - p1;
4134
Willy Tarreau086b3b42007-05-13 21:45:51 +02004135 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004136 Alert("HTTP logging : out of memory.\n");
4137 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004138 if (log_len > t->fe->capture_len)
4139 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004140 memcpy(txn->cli_cookie, p1, log_len);
4141 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004142 }
4143 }
4144
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004145 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4146 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004147 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004148 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004149 char *delim;
4150
4151 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4152 * have the server ID betweek p3 and delim, and the original cookie between
4153 * delim+1 and p4. Otherwise, delim==p4 :
4154 *
4155 * Cookie: NAME=SRV~VALUE;
4156 * | || || | |
4157 * | || || | +--> p4
4158 * | || || +--------> delim
4159 * | || |+-----------> p3
4160 * | || +------------> p2
4161 * | |+----------------> p1
4162 * | +-----------------> colon
4163 * +------------------------> cur_ptr
4164 */
4165
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004166 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004167 for (delim = p3; delim < p4; delim++)
4168 if (*delim == COOKIE_DELIM)
4169 break;
4170 }
4171 else
4172 delim = p4;
4173
4174
4175 /* Here, we'll look for the first running server which supports the cookie.
4176 * This allows to share a same cookie between several servers, for example
4177 * to dedicate backup servers to specific servers only.
4178 * However, to prevent clients from sticking to cookie-less backup server
4179 * when they have incidentely learned an empty cookie, we simply ignore
4180 * empty cookies and mark them as invalid.
4181 */
4182 if (delim == p3)
4183 srv = NULL;
4184
4185 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004186 if (srv->cookie && (srv->cklen == delim - p3) &&
4187 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004188 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004189 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004190 txn->flags &= ~TX_CK_MASK;
4191 txn->flags |= TX_CK_VALID;
4192 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004193 t->srv = srv;
4194 break;
4195 } else {
4196 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004197 txn->flags &= ~TX_CK_MASK;
4198 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004199 }
4200 }
4201 srv = srv->next;
4202 }
4203
Willy Tarreau3d300592007-03-18 18:34:41 +01004204 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004205 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004206 txn->flags &= ~TX_CK_MASK;
4207 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004208 }
4209
4210 /* depending on the cookie mode, we may have to either :
4211 * - delete the complete cookie if we're in insert+indirect mode, so that
4212 * the server never sees it ;
4213 * - remove the server id from the cookie value, and tag the cookie as an
4214 * application cookie so that it does not get accidentely removed later,
4215 * if we're in cookie prefix mode
4216 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004217 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004218 int delta; /* negative */
4219
4220 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4221 p4 += delta;
4222 cur_end += delta;
4223 cur_next += delta;
4224 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004225 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004226
4227 del_cookie = del_colon = NULL;
4228 app_cookies++; /* protect the header from deletion */
4229 }
4230 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004231 (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 +01004232 del_cookie = p1;
4233 del_colon = colon;
4234 }
4235 } else {
4236 /* now we know that we must keep this cookie since it's
4237 * not ours. But if we wanted to delete our cookie
4238 * earlier, we cannot remove the complete header, but we
4239 * can remove the previous block itself.
4240 */
4241 app_cookies++;
4242
4243 if (del_cookie != NULL) {
4244 int delta; /* negative */
4245
4246 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4247 p4 += delta;
4248 cur_end += delta;
4249 cur_next += delta;
4250 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004251 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004252 del_cookie = del_colon = NULL;
4253 }
4254 }
4255
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004256 if ((t->be->appsession_name != NULL) &&
4257 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004258 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004259
Willy Tarreau58f10d72006-12-04 02:26:12 +01004260 /* Cool... it's the right one */
4261
4262 asession_temp = &local_asession;
4263
Willy Tarreau63963c62007-05-13 21:29:55 +02004264 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004265 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4266 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4267 return;
4268 }
4269
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004270 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4271 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004272 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004273
Willy Tarreau58f10d72006-12-04 02:26:12 +01004274 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004275 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4276 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004277 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004278 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004279 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004280 Alert("Not enough memory process_cli():asession:calloc().\n");
4281 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4282 return;
4283 }
4284
4285 asession_temp->sessid = local_asession.sessid;
4286 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004287 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004288 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004289 } else {
4290 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004291 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004292 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004293 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004294 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004295 Alert("Found Application Session without matching server.\n");
4296 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004297 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004298 while (srv) {
4299 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004300 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004301 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004302 txn->flags &= ~TX_CK_MASK;
4303 txn->flags |= TX_CK_VALID;
4304 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004305 t->srv = srv;
4306 break;
4307 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004308 txn->flags &= ~TX_CK_MASK;
4309 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004310 }
4311 }
4312 srv = srv->next;
4313 }/* end while(srv) */
4314 }/* end else if server == NULL */
4315
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004316 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004317 asession_temp->request_count++;
4318#if defined(DEBUG_HASH)
4319 Alert("manage_client_side_cookies\n");
4320 appsession_hash_dump(&(t->be->htbl_proxy));
4321#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004322 }/* end if ((t->proxy->appsession_name != NULL) ... */
4323 }
4324
4325 /* we'll have to look for another cookie ... */
4326 p1 = p4;
4327 } /* while (p1 < cur_end) */
4328
4329 /* There's no more cookie on this line.
4330 * We may have marked the last one(s) for deletion.
4331 * We must do this now in two ways :
4332 * - if there is no app cookie, we simply delete the header ;
4333 * - if there are app cookies, we must delete the end of the
4334 * string properly, including the colon/semi-colon before
4335 * the cookie name.
4336 */
4337 if (del_cookie != NULL) {
4338 int delta;
4339 if (app_cookies) {
4340 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4341 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004342 cur_hdr->len += delta;
4343 } else {
4344 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004345
4346 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004347 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4348 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004349 cur_hdr->len = 0;
4350 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004351 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004352 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004353 }
4354
4355 /* keep the link from this header to next one */
4356 old_idx = cur_idx;
4357 } /* end of cookie processing on this header */
4358}
4359
4360
Willy Tarreaua15645d2007-03-18 16:22:39 +01004361/* Iterate the same filter through all response headers contained in <rtr>.
4362 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4363 */
4364int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4365{
4366 char term;
4367 char *cur_ptr, *cur_end, *cur_next;
4368 int cur_idx, old_idx, last_hdr;
4369 struct http_txn *txn = &t->txn;
4370 struct hdr_idx_elem *cur_hdr;
4371 int len, delta;
4372
4373 last_hdr = 0;
4374
4375 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4376 old_idx = 0;
4377
4378 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004379 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004380 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004381 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004382 (exp->action == ACT_ALLOW ||
4383 exp->action == ACT_DENY))
4384 return 0;
4385
4386 cur_idx = txn->hdr_idx.v[old_idx].next;
4387 if (!cur_idx)
4388 break;
4389
4390 cur_hdr = &txn->hdr_idx.v[cur_idx];
4391 cur_ptr = cur_next;
4392 cur_end = cur_ptr + cur_hdr->len;
4393 cur_next = cur_end + cur_hdr->cr + 1;
4394
4395 /* Now we have one header between cur_ptr and cur_end,
4396 * and the next header starts at cur_next.
4397 */
4398
4399 /* The annoying part is that pattern matching needs
4400 * that we modify the contents to null-terminate all
4401 * strings before testing them.
4402 */
4403
4404 term = *cur_end;
4405 *cur_end = '\0';
4406
4407 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4408 switch (exp->action) {
4409 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004410 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004411 last_hdr = 1;
4412 break;
4413
4414 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004415 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004416 last_hdr = 1;
4417 break;
4418
4419 case ACT_REPLACE:
4420 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4421 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4422 /* FIXME: if the user adds a newline in the replacement, the
4423 * index will not be recalculated for now, and the new line
4424 * will not be counted as a new header.
4425 */
4426
4427 cur_end += delta;
4428 cur_next += delta;
4429 cur_hdr->len += delta;
4430 txn->rsp.eoh += delta;
4431 break;
4432
4433 case ACT_REMOVE:
4434 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4435 cur_next += delta;
4436
4437 /* FIXME: this should be a separate function */
4438 txn->rsp.eoh += delta;
4439 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4440 txn->hdr_idx.used--;
4441 cur_hdr->len = 0;
4442 cur_end = NULL; /* null-term has been rewritten */
4443 break;
4444
4445 }
4446 }
4447 if (cur_end)
4448 *cur_end = term; /* restore the string terminator */
4449
4450 /* keep the link from this header to next one in case of later
4451 * removal of next header.
4452 */
4453 old_idx = cur_idx;
4454 }
4455 return 0;
4456}
4457
4458
4459/* Apply the filter to the status line in the response buffer <rtr>.
4460 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4461 * or -1 if a replacement resulted in an invalid status line.
4462 */
4463int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4464{
4465 char term;
4466 char *cur_ptr, *cur_end;
4467 int done;
4468 struct http_txn *txn = &t->txn;
4469 int len, delta;
4470
4471
Willy Tarreau3d300592007-03-18 18:34:41 +01004472 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004473 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004474 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004475 (exp->action == ACT_ALLOW ||
4476 exp->action == ACT_DENY))
4477 return 0;
4478 else if (exp->action == ACT_REMOVE)
4479 return 0;
4480
4481 done = 0;
4482
Willy Tarreau9cdde232007-05-02 20:58:19 +02004483 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004484 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4485
4486 /* Now we have the status line between cur_ptr and cur_end */
4487
4488 /* The annoying part is that pattern matching needs
4489 * that we modify the contents to null-terminate all
4490 * strings before testing them.
4491 */
4492
4493 term = *cur_end;
4494 *cur_end = '\0';
4495
4496 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4497 switch (exp->action) {
4498 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004499 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004500 done = 1;
4501 break;
4502
4503 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004504 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004505 done = 1;
4506 break;
4507
4508 case ACT_REPLACE:
4509 *cur_end = term; /* restore the string terminator */
4510 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4511 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4512 /* FIXME: if the user adds a newline in the replacement, the
4513 * index will not be recalculated for now, and the new line
4514 * will not be counted as a new header.
4515 */
4516
4517 txn->rsp.eoh += delta;
4518 cur_end += delta;
4519
Willy Tarreau9cdde232007-05-02 20:58:19 +02004520 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004521 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004522 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004523 cur_ptr, cur_end + 1,
4524 NULL, NULL);
4525 if (unlikely(!cur_end))
4526 return -1;
4527
4528 /* we have a full respnse and we know that we have either a CR
4529 * or an LF at <ptr>.
4530 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004531 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004532 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4533 /* there is no point trying this regex on headers */
4534 return 1;
4535 }
4536 }
4537 *cur_end = term; /* restore the string terminator */
4538 return done;
4539}
4540
4541
4542
4543/*
4544 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4545 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4546 * unparsable response.
4547 */
4548int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4549{
Willy Tarreau3d300592007-03-18 18:34:41 +01004550 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004551 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004552 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004553 int ret;
4554
4555 /*
4556 * The interleaving of transformations and verdicts
4557 * makes it difficult to decide to continue or stop
4558 * the evaluation.
4559 */
4560
Willy Tarreau3d300592007-03-18 18:34:41 +01004561 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004562 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4563 exp->action == ACT_PASS)) {
4564 exp = exp->next;
4565 continue;
4566 }
4567
4568 /* Apply the filter to the status line. */
4569 ret = apply_filter_to_sts_line(t, rtr, exp);
4570 if (unlikely(ret < 0))
4571 return -1;
4572
4573 if (likely(ret == 0)) {
4574 /* The filter did not match the response, it can be
4575 * iterated through all headers.
4576 */
4577 apply_filter_to_resp_headers(t, rtr, exp);
4578 }
4579 exp = exp->next;
4580 }
4581 return 0;
4582}
4583
4584
4585
4586/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004587 * Manage server-side cookies. It can impact performance by about 2% so it is
4588 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004589 */
4590void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4591{
4592 struct http_txn *txn = &t->txn;
4593 char *p1, *p2, *p3, *p4;
4594
4595 appsess *asession_temp = NULL;
4596 appsess local_asession;
4597
4598 char *cur_ptr, *cur_end, *cur_next;
4599 int cur_idx, old_idx, delta;
4600
Willy Tarreaua15645d2007-03-18 16:22:39 +01004601 /* Iterate through the headers.
4602 * we start with the start line.
4603 */
4604 old_idx = 0;
4605 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4606
4607 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4608 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004609 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004610
4611 cur_hdr = &txn->hdr_idx.v[cur_idx];
4612 cur_ptr = cur_next;
4613 cur_end = cur_ptr + cur_hdr->len;
4614 cur_next = cur_end + cur_hdr->cr + 1;
4615
4616 /* We have one full header between cur_ptr and cur_end, and the
4617 * next header starts at cur_next. We're only interested in
4618 * "Cookie:" headers.
4619 */
4620
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004621 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4622 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004623 old_idx = cur_idx;
4624 continue;
4625 }
4626
4627 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004628 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004629
4630
4631 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004632 if (t->be->cookie_name == NULL &&
4633 t->be->appsession_name == NULL &&
4634 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004635 return;
4636
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004637 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004638
4639 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004640 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4641 break;
4642
4643 /* p1 is at the beginning of the cookie name */
4644 p2 = p1;
4645
4646 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4647 p2++;
4648
4649 if (p2 == cur_end || *p2 == ';') /* next cookie */
4650 break;
4651
4652 p3 = p2 + 1; /* skip the '=' sign */
4653 if (p3 == cur_end)
4654 break;
4655
4656 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004657 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004658 p4++;
4659
4660 /* here, we have the cookie name between p1 and p2,
4661 * and its value between p3 and p4.
4662 * we can process it.
4663 */
4664
4665 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004666 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004667 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004668 (p4 - p1 >= t->be->capture_namelen) &&
4669 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004670 int log_len = p4 - p1;
4671
Willy Tarreau086b3b42007-05-13 21:45:51 +02004672 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004673 Alert("HTTP logging : out of memory.\n");
4674 }
4675
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004676 if (log_len > t->be->capture_len)
4677 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004678 memcpy(txn->srv_cookie, p1, log_len);
4679 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004680 }
4681
4682 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004683 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4684 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004685 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004686 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004687
4688 /* If the cookie is in insert mode on a known server, we'll delete
4689 * this occurrence because we'll insert another one later.
4690 * We'll delete it too if the "indirect" option is set and we're in
4691 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004692 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4693 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004694 /* this header must be deleted */
4695 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4696 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4697 txn->hdr_idx.used--;
4698 cur_hdr->len = 0;
4699 cur_next += delta;
4700 txn->rsp.eoh += delta;
4701
Willy Tarreau3d300592007-03-18 18:34:41 +01004702 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004703 }
4704 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004705 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004706 /* replace bytes p3->p4 with the cookie name associated
4707 * with this server since we know it.
4708 */
4709 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4710 cur_hdr->len += delta;
4711 cur_next += delta;
4712 txn->rsp.eoh += delta;
4713
Willy Tarreau3d300592007-03-18 18:34:41 +01004714 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004715 }
4716 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004717 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004718 /* insert the cookie name associated with this server
4719 * before existing cookie, and insert a delimitor between them..
4720 */
4721 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4722 cur_hdr->len += delta;
4723 cur_next += delta;
4724 txn->rsp.eoh += delta;
4725
4726 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004727 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004728 }
4729 }
4730 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004731 else if ((t->be->appsession_name != NULL) &&
4732 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004733
4734 /* Cool... it's the right one */
4735
4736 size_t server_id_len = strlen(t->srv->id) + 1;
4737 asession_temp = &local_asession;
4738
Willy Tarreau63963c62007-05-13 21:29:55 +02004739 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004740 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4741 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4742 return;
4743 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004744 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4745 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004746 asession_temp->serverid = NULL;
4747
4748 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004749 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4750 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004751 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004752 Alert("Not enough Memory process_srv():asession:calloc().\n");
4753 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4754 return;
4755 }
4756 asession_temp->sessid = local_asession.sessid;
4757 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004758 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004759 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4760 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004761 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004762 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004763 }
4764
Willy Tarreaua15645d2007-03-18 16:22:39 +01004765 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004766 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004767 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4768 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4769 return;
4770 }
4771 asession_temp->serverid[0] = '\0';
4772 }
4773
4774 if (asession_temp->serverid[0] == '\0')
4775 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4776
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004777 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004778 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004779#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004780 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004781 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004782#endif
4783 }/* end if ((t->proxy->appsession_name != NULL) ... */
4784 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4785 } /* we're now at the end of the cookie value */
4786
4787 /* keep the link from this header to next one */
4788 old_idx = cur_idx;
4789 } /* end of cookie processing on this header */
4790}
4791
4792
4793
4794/*
4795 * Check if response is cacheable or not. Updates t->flags.
4796 */
4797void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4798{
4799 struct http_txn *txn = &t->txn;
4800 char *p1, *p2;
4801
4802 char *cur_ptr, *cur_end, *cur_next;
4803 int cur_idx;
4804
Willy Tarreau5df51872007-11-25 16:20:08 +01004805 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004806 return;
4807
4808 /* Iterate through the headers.
4809 * we start with the start line.
4810 */
4811 cur_idx = 0;
4812 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4813
4814 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4815 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004816 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004817
4818 cur_hdr = &txn->hdr_idx.v[cur_idx];
4819 cur_ptr = cur_next;
4820 cur_end = cur_ptr + cur_hdr->len;
4821 cur_next = cur_end + cur_hdr->cr + 1;
4822
4823 /* We have one full header between cur_ptr and cur_end, and the
4824 * next header starts at cur_next. We're only interested in
4825 * "Cookie:" headers.
4826 */
4827
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004828 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4829 if (val) {
4830 if ((cur_end - (cur_ptr + val) >= 8) &&
4831 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4832 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4833 return;
4834 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004835 }
4836
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004837 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4838 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004839 continue;
4840
4841 /* OK, right now we know we have a cache-control header at cur_ptr */
4842
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004843 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004844
4845 if (p1 >= cur_end) /* no more info */
4846 continue;
4847
4848 /* p1 is at the beginning of the value */
4849 p2 = p1;
4850
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004851 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004852 p2++;
4853
4854 /* we have a complete value between p1 and p2 */
4855 if (p2 < cur_end && *p2 == '=') {
4856 /* we have something of the form no-cache="set-cookie" */
4857 if ((cur_end - p1 >= 21) &&
4858 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4859 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004860 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004861 continue;
4862 }
4863
4864 /* OK, so we know that either p2 points to the end of string or to a comma */
4865 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4866 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4867 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4868 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004869 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004870 return;
4871 }
4872
4873 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004874 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004875 continue;
4876 }
4877 }
4878}
4879
4880
Willy Tarreau58f10d72006-12-04 02:26:12 +01004881/*
4882 * Try to retrieve a known appsession in the URI, then the associated server.
4883 * If the server is found, it's assigned to the session.
4884 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004885void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004886{
Willy Tarreau3d300592007-03-18 18:34:41 +01004887 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004888 appsess *asession_temp = NULL;
4889 appsess local_asession;
4890 char *request_line;
4891
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004892 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004893 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004894 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004895 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004896 return;
4897
4898 /* skip ';' */
4899 request_line++;
4900
4901 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004902 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004903 return;
4904
4905 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004906 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004907
4908 /* First try if we already have an appsession */
4909 asession_temp = &local_asession;
4910
Willy Tarreau63963c62007-05-13 21:29:55 +02004911 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004912 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4913 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4914 return;
4915 }
4916
4917 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004918 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4919 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004920 asession_temp->serverid = NULL;
4921
4922 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004923 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4924 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004925 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004926 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004927 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004928 Alert("Not enough memory process_cli():asession:calloc().\n");
4929 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4930 return;
4931 }
4932 asession_temp->sessid = local_asession.sessid;
4933 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004934 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004935 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004936 }
4937 else {
4938 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004939 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004940 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004941
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004942 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004943 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004944
Willy Tarreau58f10d72006-12-04 02:26:12 +01004945#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004946 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004947 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004948#endif
4949 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004950 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004951 Alert("Found Application Session without matching server.\n");
4952 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004953 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004954 while (srv) {
4955 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004956 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004957 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004958 txn->flags &= ~TX_CK_MASK;
4959 txn->flags |= TX_CK_VALID;
4960 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004961 t->srv = srv;
4962 break;
4963 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004964 txn->flags &= ~TX_CK_MASK;
4965 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004966 }
4967 }
4968 srv = srv->next;
4969 }
4970 }
4971}
4972
4973
Willy Tarreaub2513902006-12-17 14:52:38 +01004974/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004975 * In a GET or HEAD request, check if the requested URI matches the stats uri
4976 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004977 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004978 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004979 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004980 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004981 *
4982 * Returns 1 if the session's state changes, otherwise 0.
4983 */
4984int stats_check_uri_auth(struct session *t, struct proxy *backend)
4985{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004986 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004987 struct uri_auth *uri_auth = backend->uri_auth;
4988 struct user_auth *user;
4989 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004990 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004991
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004992 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
4993
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004994 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004995 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004996 return 0;
4997
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004998 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004999
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005000 /* the URI is in h */
5001 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005002 return 0;
5003
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005004 h += uri_auth->uri_len;
5005 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5006 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005007 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005008 break;
5009 }
5010 h++;
5011 }
5012
5013 if (uri_auth->refresh) {
5014 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5015 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5016 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005017 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005018 break;
5019 }
5020 h++;
5021 }
5022 }
5023
Willy Tarreau55bb8452007-10-17 18:44:57 +02005024 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5025 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5026 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005027 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005028 break;
5029 }
5030 h++;
5031 }
5032
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005033 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5034
Willy Tarreaub2513902006-12-17 14:52:38 +01005035 /* we are in front of a interceptable URI. Let's check
5036 * if there's an authentication and if it's valid.
5037 */
5038 user = uri_auth->users;
5039 if (!user) {
5040 /* no user auth required, it's OK */
5041 authenticated = 1;
5042 } else {
5043 authenticated = 0;
5044
5045 /* a user list is defined, we have to check.
5046 * skip 21 chars for "Authorization: Basic ".
5047 */
5048
5049 /* FIXME: this should move to an earlier place */
5050 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005051 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5052 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5053 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005054 if (len > 14 &&
5055 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005056 txn->auth_hdr.str = h;
5057 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005058 break;
5059 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005060 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005061 }
5062
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005063 if (txn->auth_hdr.len < 21 ||
5064 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005065 user = NULL;
5066
5067 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005068 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5069 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005070 user->user_pwd, user->user_len)) {
5071 authenticated = 1;
5072 break;
5073 }
5074 user = user->next;
5075 }
5076 }
5077
5078 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005079 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005080
5081 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005082 msg.str = trash;
5083 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005084 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005085 client_retnclose(t, &msg);
Willy Tarreauf8533202008-08-16 14:55:08 +02005086 trace_term(t, TT_HTTP_URI_1);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005087 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub2513902006-12-17 14:52:38 +01005088 if (!(t->flags & SN_ERR_MASK))
5089 t->flags |= SN_ERR_PRXCOND;
5090 if (!(t->flags & SN_FINST_MASK))
5091 t->flags |= SN_FINST_R;
5092 return 1;
5093 }
5094
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005095 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005096 * data.
5097 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02005098 EV_FD_CLR(t->cli_fd, DIR_RD);
5099 buffer_shutr(t->req);
5100 buffer_shutr(t->rep);
Willy Tarreaub2513902006-12-17 14:52:38 +01005101 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02005102 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005103 t->data_source = DATA_SRC_STATS;
5104 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005105 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005106 produce_content(t);
5107 return 1;
5108}
5109
5110
Willy Tarreaubaaee002006-06-26 02:48:02 +02005111/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005112 * Print a debug line with a header
5113 */
5114void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5115{
5116 int len, max;
5117 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5118 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5119 max = end - start;
5120 UBOUND(max, sizeof(trash) - len - 1);
5121 len += strlcpy2(trash + len, start, max + 1);
5122 trash[len++] = '\n';
5123 write(1, trash, len);
5124}
5125
5126
Willy Tarreau8797c062007-05-07 00:55:35 +02005127/************************************************************************/
5128/* The code below is dedicated to ACL parsing and matching */
5129/************************************************************************/
5130
5131
5132
5133
5134/* 1. Check on METHOD
5135 * We use the pre-parsed method if it is known, and store its number as an
5136 * integer. If it is unknown, we use the pointer and the length.
5137 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005138static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005139{
5140 int len, meth;
5141
Willy Tarreauae8b7962007-06-09 23:10:04 +02005142 len = strlen(*text);
5143 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005144
5145 pattern->val.i = meth;
5146 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005147 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005148 if (!pattern->ptr.str)
5149 return 0;
5150 pattern->len = len;
5151 }
5152 return 1;
5153}
5154
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005155static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005156acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5157 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005158{
5159 int meth;
5160 struct http_txn *txn = l7;
5161
Willy Tarreaub6866442008-07-14 23:54:42 +02005162 if (!txn)
5163 return 0;
5164
Willy Tarreauc11416f2007-06-17 16:58:38 +02005165 if (txn->req.msg_state != HTTP_MSG_BODY)
5166 return 0;
5167
Willy Tarreau8797c062007-05-07 00:55:35 +02005168 meth = txn->meth;
5169 test->i = meth;
5170 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005171 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5172 /* ensure the indexes are not affected */
5173 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005174 test->len = txn->req.sl.rq.m_l;
5175 test->ptr = txn->req.sol;
5176 }
5177 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5178 return 1;
5179}
5180
5181static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5182{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005183 int icase;
5184
Willy Tarreau8797c062007-05-07 00:55:35 +02005185 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005186 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005187
5188 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005189 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005190
5191 /* Other method, we must compare the strings */
5192 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005193 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005194
5195 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5196 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5197 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005198 return ACL_PAT_FAIL;
5199 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005200}
5201
5202/* 2. Check on Request/Status Version
5203 * We simply compare strings here.
5204 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005205static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005206{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005207 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005208 if (!pattern->ptr.str)
5209 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005210 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005211 return 1;
5212}
5213
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005214static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005215acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5216 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005217{
5218 struct http_txn *txn = l7;
5219 char *ptr;
5220 int len;
5221
Willy Tarreaub6866442008-07-14 23:54:42 +02005222 if (!txn)
5223 return 0;
5224
Willy Tarreauc11416f2007-06-17 16:58:38 +02005225 if (txn->req.msg_state != HTTP_MSG_BODY)
5226 return 0;
5227
Willy Tarreau8797c062007-05-07 00:55:35 +02005228 len = txn->req.sl.rq.v_l;
5229 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5230
5231 while ((len-- > 0) && (*ptr++ != '/'));
5232 if (len <= 0)
5233 return 0;
5234
5235 test->ptr = ptr;
5236 test->len = len;
5237
5238 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5239 return 1;
5240}
5241
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005242static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005243acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5244 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005245{
5246 struct http_txn *txn = l7;
5247 char *ptr;
5248 int len;
5249
Willy Tarreaub6866442008-07-14 23:54:42 +02005250 if (!txn)
5251 return 0;
5252
Willy Tarreauc11416f2007-06-17 16:58:38 +02005253 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5254 return 0;
5255
Willy Tarreau8797c062007-05-07 00:55:35 +02005256 len = txn->rsp.sl.st.v_l;
5257 ptr = txn->rsp.sol;
5258
5259 while ((len-- > 0) && (*ptr++ != '/'));
5260 if (len <= 0)
5261 return 0;
5262
5263 test->ptr = ptr;
5264 test->len = len;
5265
5266 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5267 return 1;
5268}
5269
5270/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005271static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005272acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5273 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005274{
5275 struct http_txn *txn = l7;
5276 char *ptr;
5277 int len;
5278
Willy Tarreaub6866442008-07-14 23:54:42 +02005279 if (!txn)
5280 return 0;
5281
Willy Tarreauc11416f2007-06-17 16:58:38 +02005282 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5283 return 0;
5284
Willy Tarreau8797c062007-05-07 00:55:35 +02005285 len = txn->rsp.sl.st.c_l;
5286 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5287
5288 test->i = __strl2ui(ptr, len);
5289 test->flags = ACL_TEST_F_VOL_1ST;
5290 return 1;
5291}
5292
5293/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005294static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005295acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5296 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005297{
5298 struct http_txn *txn = l7;
5299
Willy Tarreaub6866442008-07-14 23:54:42 +02005300 if (!txn)
5301 return 0;
5302
Willy Tarreauc11416f2007-06-17 16:58:38 +02005303 if (txn->req.msg_state != HTTP_MSG_BODY)
5304 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005305
Willy Tarreauc11416f2007-06-17 16:58:38 +02005306 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5307 /* ensure the indexes are not affected */
5308 return 0;
5309
Willy Tarreau8797c062007-05-07 00:55:35 +02005310 test->len = txn->req.sl.rq.u_l;
5311 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5312
Willy Tarreauf3d25982007-05-08 22:45:09 +02005313 /* we do not need to set READ_ONLY because the data is in a buffer */
5314 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005315 return 1;
5316}
5317
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005318static int
5319acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5320 struct acl_expr *expr, struct acl_test *test)
5321{
5322 struct http_txn *txn = l7;
5323
Willy Tarreaub6866442008-07-14 23:54:42 +02005324 if (!txn)
5325 return 0;
5326
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005327 if (txn->req.msg_state != HTTP_MSG_BODY)
5328 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005329
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005330 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5331 /* ensure the indexes are not affected */
5332 return 0;
5333
5334 /* Parse HTTP request */
5335 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5336 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5337 test->i = AF_INET;
5338
5339 /*
5340 * If we are parsing url in frontend space, we prepare backend stage
5341 * to not parse again the same url ! optimization lazyness...
5342 */
5343 if (px->options & PR_O_HTTP_PROXY)
5344 l4->flags |= SN_ADDR_SET;
5345
5346 test->flags = ACL_TEST_F_READ_ONLY;
5347 return 1;
5348}
5349
5350static int
5351acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5352 struct acl_expr *expr, struct acl_test *test)
5353{
5354 struct http_txn *txn = l7;
5355
Willy Tarreaub6866442008-07-14 23:54:42 +02005356 if (!txn)
5357 return 0;
5358
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005359 if (txn->req.msg_state != HTTP_MSG_BODY)
5360 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005361
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005362 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5363 /* ensure the indexes are not affected */
5364 return 0;
5365
5366 /* Same optimization as url_ip */
5367 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5368 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5369
5370 if (px->options & PR_O_HTTP_PROXY)
5371 l4->flags |= SN_ADDR_SET;
5372
5373 test->flags = ACL_TEST_F_READ_ONLY;
5374 return 1;
5375}
5376
Willy Tarreauc11416f2007-06-17 16:58:38 +02005377/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5378 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5379 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005380static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005381acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005382 struct acl_expr *expr, struct acl_test *test)
5383{
5384 struct http_txn *txn = l7;
5385 struct hdr_idx *idx = &txn->hdr_idx;
5386 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005387
Willy Tarreaub6866442008-07-14 23:54:42 +02005388 if (!txn)
5389 return 0;
5390
Willy Tarreau33a7e692007-06-10 19:45:56 +02005391 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5392 /* search for header from the beginning */
5393 ctx->idx = 0;
5394
Willy Tarreau33a7e692007-06-10 19:45:56 +02005395 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5396 test->flags |= ACL_TEST_F_FETCH_MORE;
5397 test->flags |= ACL_TEST_F_VOL_HDR;
5398 test->len = ctx->vlen;
5399 test->ptr = (char *)ctx->line + ctx->val;
5400 return 1;
5401 }
5402
5403 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5404 test->flags |= ACL_TEST_F_VOL_HDR;
5405 return 0;
5406}
5407
Willy Tarreau33a7e692007-06-10 19:45:56 +02005408static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005409acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5410 struct acl_expr *expr, struct acl_test *test)
5411{
5412 struct http_txn *txn = l7;
5413
Willy Tarreaub6866442008-07-14 23:54:42 +02005414 if (!txn)
5415 return 0;
5416
Willy Tarreauc11416f2007-06-17 16:58:38 +02005417 if (txn->req.msg_state != HTTP_MSG_BODY)
5418 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005419
Willy Tarreauc11416f2007-06-17 16:58:38 +02005420 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5421 /* ensure the indexes are not affected */
5422 return 0;
5423
5424 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5425}
5426
5427static int
5428acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5429 struct acl_expr *expr, struct acl_test *test)
5430{
5431 struct http_txn *txn = l7;
5432
Willy Tarreaub6866442008-07-14 23:54:42 +02005433 if (!txn)
5434 return 0;
5435
Willy Tarreauc11416f2007-06-17 16:58:38 +02005436 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5437 return 0;
5438
5439 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5440}
5441
5442/* 6. Check on HTTP header count. The number of occurrences is returned.
5443 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5444 */
5445static int
5446acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005447 struct acl_expr *expr, struct acl_test *test)
5448{
5449 struct http_txn *txn = l7;
5450 struct hdr_idx *idx = &txn->hdr_idx;
5451 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005452 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005453
Willy Tarreaub6866442008-07-14 23:54:42 +02005454 if (!txn)
5455 return 0;
5456
Willy Tarreau33a7e692007-06-10 19:45:56 +02005457 ctx.idx = 0;
5458 cnt = 0;
5459 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5460 cnt++;
5461
5462 test->i = cnt;
5463 test->flags = ACL_TEST_F_VOL_HDR;
5464 return 1;
5465}
5466
Willy Tarreauc11416f2007-06-17 16:58:38 +02005467static int
5468acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5469 struct acl_expr *expr, struct acl_test *test)
5470{
5471 struct http_txn *txn = l7;
5472
Willy Tarreaub6866442008-07-14 23:54:42 +02005473 if (!txn)
5474 return 0;
5475
Willy Tarreauc11416f2007-06-17 16:58:38 +02005476 if (txn->req.msg_state != HTTP_MSG_BODY)
5477 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005478
Willy Tarreauc11416f2007-06-17 16:58:38 +02005479 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5480 /* ensure the indexes are not affected */
5481 return 0;
5482
5483 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5484}
5485
5486static int
5487acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5488 struct acl_expr *expr, struct acl_test *test)
5489{
5490 struct http_txn *txn = l7;
5491
Willy Tarreaub6866442008-07-14 23:54:42 +02005492 if (!txn)
5493 return 0;
5494
Willy Tarreauc11416f2007-06-17 16:58:38 +02005495 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5496 return 0;
5497
5498 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5499}
5500
Willy Tarreau33a7e692007-06-10 19:45:56 +02005501/* 7. Check on HTTP header's integer value. The integer value is returned.
5502 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005503 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005504 */
5505static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005506acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005507 struct acl_expr *expr, struct acl_test *test)
5508{
5509 struct http_txn *txn = l7;
5510 struct hdr_idx *idx = &txn->hdr_idx;
5511 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005512
Willy Tarreaub6866442008-07-14 23:54:42 +02005513 if (!txn)
5514 return 0;
5515
Willy Tarreau33a7e692007-06-10 19:45:56 +02005516 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5517 /* search for header from the beginning */
5518 ctx->idx = 0;
5519
Willy Tarreau33a7e692007-06-10 19:45:56 +02005520 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5521 test->flags |= ACL_TEST_F_FETCH_MORE;
5522 test->flags |= ACL_TEST_F_VOL_HDR;
5523 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5524 return 1;
5525 }
5526
5527 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5528 test->flags |= ACL_TEST_F_VOL_HDR;
5529 return 0;
5530}
5531
Willy Tarreauc11416f2007-06-17 16:58:38 +02005532static int
5533acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5534 struct acl_expr *expr, struct acl_test *test)
5535{
5536 struct http_txn *txn = l7;
5537
Willy Tarreaub6866442008-07-14 23:54:42 +02005538 if (!txn)
5539 return 0;
5540
Willy Tarreauc11416f2007-06-17 16:58:38 +02005541 if (txn->req.msg_state != HTTP_MSG_BODY)
5542 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005543
Willy Tarreauc11416f2007-06-17 16:58:38 +02005544 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5545 /* ensure the indexes are not affected */
5546 return 0;
5547
5548 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5549}
5550
5551static int
5552acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5553 struct acl_expr *expr, struct acl_test *test)
5554{
5555 struct http_txn *txn = l7;
5556
Willy Tarreaub6866442008-07-14 23:54:42 +02005557 if (!txn)
5558 return 0;
5559
Willy Tarreauc11416f2007-06-17 16:58:38 +02005560 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5561 return 0;
5562
5563 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5564}
5565
Willy Tarreau737b0c12007-06-10 21:28:46 +02005566/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5567 * the first '/' after the possible hostname, and ends before the possible '?'.
5568 */
5569static int
5570acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5571 struct acl_expr *expr, struct acl_test *test)
5572{
5573 struct http_txn *txn = l7;
5574 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005575
Willy Tarreaub6866442008-07-14 23:54:42 +02005576 if (!txn)
5577 return 0;
5578
Willy Tarreauc11416f2007-06-17 16:58:38 +02005579 if (txn->req.msg_state != HTTP_MSG_BODY)
5580 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005581
Willy Tarreauc11416f2007-06-17 16:58:38 +02005582 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5583 /* ensure the indexes are not affected */
5584 return 0;
5585
Willy Tarreau21d2af32008-02-14 20:25:24 +01005586 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5587 ptr = http_get_path(txn);
5588 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005589 return 0;
5590
5591 /* OK, we got the '/' ! */
5592 test->ptr = ptr;
5593
5594 while (ptr < end && *ptr != '?')
5595 ptr++;
5596
5597 test->len = ptr - test->ptr;
5598
5599 /* we do not need to set READ_ONLY because the data is in a buffer */
5600 test->flags = ACL_TEST_F_VOL_1ST;
5601 return 1;
5602}
5603
5604
Willy Tarreau8797c062007-05-07 00:55:35 +02005605
5606/************************************************************************/
5607/* All supported keywords must be declared here. */
5608/************************************************************************/
5609
5610/* Note: must not be declared <const> as its list will be overwritten */
5611static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005612 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5613 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5614 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5615 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005616
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005617 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5618 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5619 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5620 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5621 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5622 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5623 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5624 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5625 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005626
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005627 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5628 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5629 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5630 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5631 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5632 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5633 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5634 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5635 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5636 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005637
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005638 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5639 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5640 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5641 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5642 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5643 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5644 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5645 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5646 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005647
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005648 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5649 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5650 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5651 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5652 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5653 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5654 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005655
Willy Tarreauf3d25982007-05-08 22:45:09 +02005656 { NULL, NULL, NULL, NULL },
5657
5658#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005659 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5660 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5661 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5662 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5663 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5664 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5665 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5666
Willy Tarreau8797c062007-05-07 00:55:35 +02005667 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5668 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5669 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5670 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5671 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5672 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5673 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5674 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5675
5676 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5677 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5678 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5679 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5680 { NULL, NULL, NULL, NULL },
5681#endif
5682}};
5683
5684
5685__attribute__((constructor))
5686static void __http_protocol_init(void)
5687{
5688 acl_register_keywords(&acl_kws);
5689}
5690
5691
Willy Tarreau58f10d72006-12-04 02:26:12 +01005692/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005693 * Local variables:
5694 * c-indent-level: 8
5695 * c-basic-offset: 8
5696 * End:
5697 */