blob: 870b025c8b6194e2995ab75675678ae3d1869ada [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 Tarreauadfb8562008-08-11 15:24:42 +0200369static char *srv_stnames[7] = { "IDL", "CON", "HDR", "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;
661 //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
662 fsm_resync |= process_cli(s);
663 //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
664 fsm_resync |= process_srv(s);
665 //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state);
666 } while (fsm_resync);
667
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200668 if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100669
670 if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED))
671 session_process_counters(s);
672
Willy Tarreau0f9f5052006-07-29 17:39:25 +0200673 s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
674 s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675
Willy Tarreau7f875f62008-08-11 17:35:01 +0200676 /* Trick: if a request is being waiting for the server to respond,
677 * and if we know the server can timeout, we don't want the timeout
678 * to expire on the client side first, but we're still interested
679 * in passing data from the client to the server (eg: POST). Thus,
680 * we can cancel the client's request timeout if the server's
681 * request timeout is set and the server has not yet sent a response.
682 */
683
684 if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR_STATUS)) == 0 &&
685 (tick_isset(s->req->cex) || tick_isset(s->req->wex) || tick_isset(s->rep->rex)))
686 s->req->rex = TICK_ETERNITY;
687
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200688 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
689 tick_first(s->rep->rex, s->rep->wex));
690 t->expire = tick_first(t->expire, s->req->cex);
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200691 if (s->analysis & AN_REQ_ANY) {
692 if (s->analysis & AN_REQ_INSPECT)
693 t->expire = tick_first(t->expire, s->inspect_exp);
694 else if (s->analysis & AN_REQ_HTTP_HDR)
695 t->expire = tick_first(t->expire, s->txn.exp);
696 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200697
698 /* restore t to its place in the task list */
699 task_queue(t);
700
Willy Tarreaud825eef2007-05-12 22:35:00 +0200701 *next = t->expire;
702 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200703 }
704
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100705 s->fe->feconn--;
706 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200707 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200708 actconn--;
709
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200710 if (unlikely((global.mode & MODE_DEBUG) &&
711 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200712 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100713 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200714 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100715 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200716 write(1, trash, len);
717 }
718
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200719 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100720 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200721
722 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200723 if (s->logs.logwait &&
724 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200725 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
726 if (s->fe->to_log & LW_REQ)
727 http_sess_log(s);
728 else
729 tcp_sess_log(s);
730 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200731
732 /* the task MUST not be in the run queue anymore */
733 task_delete(t);
734 session_free(s);
735 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200736 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200737}
738
739
Willy Tarreau42250582007-04-01 01:30:43 +0200740extern const char sess_term_cond[8];
741extern const char sess_fin_state[8];
742extern const char *monthname[12];
743const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
744const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
745 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
746 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200747struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200748struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100749
Willy Tarreau42250582007-04-01 01:30:43 +0200750/*
751 * send a log for the session when we have enough info about it.
752 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100753 */
Willy Tarreau42250582007-04-01 01:30:43 +0200754static void http_sess_log(struct session *s)
755{
756 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
757 struct proxy *fe = s->fe;
758 struct proxy *be = s->be;
759 struct proxy *prx_log;
760 struct http_txn *txn = &s->txn;
761 int tolog;
762 char *uri, *h;
763 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200764 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200765 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200766 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200767 int hdr;
768
769 if (fe->logfac1 < 0 && fe->logfac2 < 0)
770 return;
771 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100772
Willy Tarreau42250582007-04-01 01:30:43 +0200773 if (s->cli_addr.ss_family == AF_INET)
774 inet_ntop(AF_INET,
775 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
776 pn, sizeof(pn));
777 else
778 inet_ntop(AF_INET6,
779 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
780 pn, sizeof(pn));
781
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200782 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200783
784 /* FIXME: let's limit ourselves to frontend logging for now. */
785 tolog = fe->to_log;
786
787 h = tmpline;
788 if (fe->to_log & LW_REQHDR &&
789 txn->req.cap &&
790 (h < tmpline + sizeof(tmpline) - 10)) {
791 *(h++) = ' ';
792 *(h++) = '{';
793 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
794 if (hdr)
795 *(h++) = '|';
796 if (txn->req.cap[hdr] != NULL)
797 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
798 '#', hdr_encode_map, txn->req.cap[hdr]);
799 }
800 *(h++) = '}';
801 }
802
803 if (fe->to_log & LW_RSPHDR &&
804 txn->rsp.cap &&
805 (h < tmpline + sizeof(tmpline) - 7)) {
806 *(h++) = ' ';
807 *(h++) = '{';
808 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
809 if (hdr)
810 *(h++) = '|';
811 if (txn->rsp.cap[hdr] != NULL)
812 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
813 '#', hdr_encode_map, txn->rsp.cap[hdr]);
814 }
815 *(h++) = '}';
816 }
817
818 if (h < tmpline + sizeof(tmpline) - 4) {
819 *(h++) = ' ';
820 *(h++) = '"';
821 uri = txn->uri ? txn->uri : "<BADREQ>";
822 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
823 '#', url_encode_map, uri);
824 *(h++) = '"';
825 }
826 *h = '\0';
827
828 svid = (tolog & LW_SVID) ?
829 (s->data_source != DATA_SRC_STATS) ?
830 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
831
Willy Tarreau70089872008-06-13 21:12:51 +0200832 t_request = -1;
833 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
834 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
835
Willy Tarreau42250582007-04-01 01:30:43 +0200836 send_log(prx_log, LOG_INFO,
837 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
838 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100839 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200840 pn,
841 (s->cli_addr.ss_family == AF_INET) ?
842 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
843 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200844 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200845 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200846 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200847 t_request,
848 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200849 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
850 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
851 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
852 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100853 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200854 txn->cli_cookie ? txn->cli_cookie : "-",
855 txn->srv_cookie ? txn->srv_cookie : "-",
856 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
857 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
858 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
859 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
860 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100861 (s->flags & SN_REDISP)?"+":"",
862 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200863 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
864
865 s->logs.logwait = 0;
866}
867
Willy Tarreau117f59e2007-03-04 18:17:17 +0100868
869/*
870 * Capture headers from message starting at <som> according to header list
871 * <cap_hdr>, and fill the <idx> structure appropriately.
872 */
873void capture_headers(char *som, struct hdr_idx *idx,
874 char **cap, struct cap_hdr *cap_hdr)
875{
876 char *eol, *sol, *col, *sov;
877 int cur_idx;
878 struct cap_hdr *h;
879 int len;
880
881 sol = som + hdr_idx_first_pos(idx);
882 cur_idx = hdr_idx_first_idx(idx);
883
884 while (cur_idx) {
885 eol = sol + idx->v[cur_idx].len;
886
887 col = sol;
888 while (col < eol && *col != ':')
889 col++;
890
891 sov = col + 1;
892 while (sov < eol && http_is_lws[(unsigned char)*sov])
893 sov++;
894
895 for (h = cap_hdr; h; h = h->next) {
896 if ((h->namelen == col - sol) &&
897 (strncasecmp(sol, h->name, h->namelen) == 0)) {
898 if (cap[h->index] == NULL)
899 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200900 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100901
902 if (cap[h->index] == NULL) {
903 Alert("HTTP capture : out of memory.\n");
904 continue;
905 }
906
907 len = eol - sov;
908 if (len > h->len)
909 len = h->len;
910
911 memcpy(cap[h->index], sov, len);
912 cap[h->index][len]=0;
913 }
914 }
915 sol = eol + idx->v[cur_idx].cr + 1;
916 cur_idx = idx->v[cur_idx].next;
917 }
918}
919
920
Willy Tarreau42250582007-04-01 01:30:43 +0200921/* either we find an LF at <ptr> or we jump to <bad>.
922 */
923#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
924
925/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
926 * otherwise to <http_msg_ood> with <state> set to <st>.
927 */
928#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
929 ptr++; \
930 if (likely(ptr < end)) \
931 goto good; \
932 else { \
933 state = (st); \
934 goto http_msg_ood; \
935 } \
936 } while (0)
937
938
Willy Tarreaubaaee002006-06-26 02:48:02 +0200939/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100940 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100941 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
942 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
943 * will give undefined results.
944 * Note that it is upon the caller's responsibility to ensure that ptr < end,
945 * and that msg->sol points to the beginning of the response.
946 * If a complete line is found (which implies that at least one CR or LF is
947 * found before <end>, the updated <ptr> is returned, otherwise NULL is
948 * returned indicating an incomplete line (which does not mean that parts have
949 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
950 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
951 * upon next call.
952 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200953 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100954 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
955 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200956 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100957 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100958const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
959 unsigned int state, const char *ptr, const char *end,
960 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100961{
962 __label__
963 http_msg_rpver,
964 http_msg_rpver_sp,
965 http_msg_rpcode,
966 http_msg_rpcode_sp,
967 http_msg_rpreason,
968 http_msg_rpline_eol,
969 http_msg_ood, /* out of data */
970 http_msg_invalid;
971
972 switch (state) {
973 http_msg_rpver:
974 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100975 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100976 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
977
978 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100979 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100980 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
981 }
982 goto http_msg_invalid;
983
984 http_msg_rpver_sp:
985 case HTTP_MSG_RPVER_SP:
986 if (likely(!HTTP_IS_LWS(*ptr))) {
987 msg->sl.st.c = ptr - msg_buf;
988 goto http_msg_rpcode;
989 }
990 if (likely(HTTP_IS_SPHT(*ptr)))
991 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
992 /* so it's a CR/LF, this is invalid */
993 goto http_msg_invalid;
994
995 http_msg_rpcode:
996 case HTTP_MSG_RPCODE:
997 if (likely(!HTTP_IS_LWS(*ptr)))
998 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
999
1000 if (likely(HTTP_IS_SPHT(*ptr))) {
1001 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1002 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1003 }
1004
1005 /* so it's a CR/LF, so there is no reason phrase */
1006 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1007 http_msg_rsp_reason:
1008 /* FIXME: should we support HTTP responses without any reason phrase ? */
1009 msg->sl.st.r = ptr - msg_buf;
1010 msg->sl.st.r_l = 0;
1011 goto http_msg_rpline_eol;
1012
1013 http_msg_rpcode_sp:
1014 case HTTP_MSG_RPCODE_SP:
1015 if (likely(!HTTP_IS_LWS(*ptr))) {
1016 msg->sl.st.r = ptr - msg_buf;
1017 goto http_msg_rpreason;
1018 }
1019 if (likely(HTTP_IS_SPHT(*ptr)))
1020 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1021 /* so it's a CR/LF, so there is no reason phrase */
1022 goto http_msg_rsp_reason;
1023
1024 http_msg_rpreason:
1025 case HTTP_MSG_RPREASON:
1026 if (likely(!HTTP_IS_CRLF(*ptr)))
1027 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1028 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1029 http_msg_rpline_eol:
1030 /* We have seen the end of line. Note that we do not
1031 * necessarily have the \n yet, but at least we know that we
1032 * have EITHER \r OR \n, otherwise the response would not be
1033 * complete. We can then record the response length and return
1034 * to the caller which will be able to register it.
1035 */
1036 msg->sl.st.l = ptr - msg->sol;
1037 return ptr;
1038
1039#ifdef DEBUG_FULL
1040 default:
1041 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1042 exit(1);
1043#endif
1044 }
1045
1046 http_msg_ood:
1047 /* out of data */
1048 if (ret_state)
1049 *ret_state = state;
1050 if (ret_ptr)
1051 *ret_ptr = (char *)ptr;
1052 return NULL;
1053
1054 http_msg_invalid:
1055 /* invalid message */
1056 if (ret_state)
1057 *ret_state = HTTP_MSG_ERROR;
1058 return NULL;
1059}
1060
1061
1062/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001063 * This function parses a request line between <ptr> and <end>, starting with
1064 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1065 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1066 * will give undefined results.
1067 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1068 * and that msg->sol points to the beginning of the request.
1069 * If a complete line is found (which implies that at least one CR or LF is
1070 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1071 * returned indicating an incomplete line (which does not mean that parts have
1072 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1073 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1074 * upon next call.
1075 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001076 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1078 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001079 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001080 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001081const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1082 unsigned int state, const char *ptr, const char *end,
1083 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001084{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 __label__
1086 http_msg_rqmeth,
1087 http_msg_rqmeth_sp,
1088 http_msg_rquri,
1089 http_msg_rquri_sp,
1090 http_msg_rqver,
1091 http_msg_rqline_eol,
1092 http_msg_ood, /* out of data */
1093 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001094
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 switch (state) {
1096 http_msg_rqmeth:
1097 case HTTP_MSG_RQMETH:
1098 if (likely(HTTP_IS_TOKEN(*ptr)))
1099 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001100
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001101 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001102 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1104 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001105
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001106 if (likely(HTTP_IS_CRLF(*ptr))) {
1107 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001108 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 http_msg_req09_uri:
1110 msg->sl.rq.u = ptr - msg_buf;
1111 http_msg_req09_uri_e:
1112 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1113 http_msg_req09_ver:
1114 msg->sl.rq.v = ptr - msg_buf;
1115 msg->sl.rq.v_l = 0;
1116 goto http_msg_rqline_eol;
1117 }
1118 goto http_msg_invalid;
1119
1120 http_msg_rqmeth_sp:
1121 case HTTP_MSG_RQMETH_SP:
1122 if (likely(!HTTP_IS_LWS(*ptr))) {
1123 msg->sl.rq.u = ptr - msg_buf;
1124 goto http_msg_rquri;
1125 }
1126 if (likely(HTTP_IS_SPHT(*ptr)))
1127 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1128 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1129 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001130
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001131 http_msg_rquri:
1132 case HTTP_MSG_RQURI:
1133 if (likely(!HTTP_IS_LWS(*ptr)))
1134 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001135
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001136 if (likely(HTTP_IS_SPHT(*ptr))) {
1137 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1138 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1139 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001140
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001141 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1142 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001143
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001144 http_msg_rquri_sp:
1145 case HTTP_MSG_RQURI_SP:
1146 if (likely(!HTTP_IS_LWS(*ptr))) {
1147 msg->sl.rq.v = ptr - msg_buf;
1148 goto http_msg_rqver;
1149 }
1150 if (likely(HTTP_IS_SPHT(*ptr)))
1151 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1152 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1153 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001154
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001155 http_msg_rqver:
1156 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001157 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001158 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001159
1160 if (likely(HTTP_IS_CRLF(*ptr))) {
1161 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1162 http_msg_rqline_eol:
1163 /* We have seen the end of line. Note that we do not
1164 * necessarily have the \n yet, but at least we know that we
1165 * have EITHER \r OR \n, otherwise the request would not be
1166 * complete. We can then record the request length and return
1167 * to the caller which will be able to register it.
1168 */
1169 msg->sl.rq.l = ptr - msg->sol;
1170 return ptr;
1171 }
1172
1173 /* neither an HTTP_VER token nor a CRLF */
1174 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001175
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001176#ifdef DEBUG_FULL
1177 default:
1178 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1179 exit(1);
1180#endif
1181 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001182
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001183 http_msg_ood:
1184 /* out of data */
1185 if (ret_state)
1186 *ret_state = state;
1187 if (ret_ptr)
1188 *ret_ptr = (char *)ptr;
1189 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001190
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001191 http_msg_invalid:
1192 /* invalid message */
1193 if (ret_state)
1194 *ret_state = HTTP_MSG_ERROR;
1195 return NULL;
1196}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001197
1198
Willy Tarreau8973c702007-01-21 23:58:29 +01001199/*
1200 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001201 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001202 * when data are missing and recalled at the exact same location with no
1203 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001204 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1205 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001206 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001207void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1208{
1209 __label__
1210 http_msg_rqbefore,
1211 http_msg_rqbefore_cr,
1212 http_msg_rqmeth,
1213 http_msg_rqline_end,
1214 http_msg_hdr_first,
1215 http_msg_hdr_name,
1216 http_msg_hdr_l1_sp,
1217 http_msg_hdr_l1_lf,
1218 http_msg_hdr_l1_lws,
1219 http_msg_hdr_val,
1220 http_msg_hdr_l2_lf,
1221 http_msg_hdr_l2_lws,
1222 http_msg_complete_header,
1223 http_msg_last_lf,
1224 http_msg_ood, /* out of data */
1225 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001226
Willy Tarreaue69eada2008-01-27 00:34:10 +01001227 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001228 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001229
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001230 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001231 ptr = buf->lr;
1232 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001233
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234 if (unlikely(ptr >= end))
1235 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001236
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001238 /*
1239 * First, states that are specific to the response only.
1240 * We check them first so that request and headers are
1241 * closer to each other (accessed more often).
1242 */
1243 http_msg_rpbefore:
1244 case HTTP_MSG_RPBEFORE:
1245 if (likely(HTTP_IS_TOKEN(*ptr))) {
1246 if (likely(ptr == buf->data)) {
1247 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001248 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001249 } else {
1250#if PARSE_PRESERVE_EMPTY_LINES
1251 /* only skip empty leading lines, don't remove them */
1252 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001253 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001254#else
1255 /* Remove empty leading lines, as recommended by
1256 * RFC2616. This takes a lot of time because we
1257 * must move all the buffer backwards, but this
1258 * is rarely needed. The method above will be
1259 * cleaner when we'll be able to start sending
1260 * the request from any place in the buffer.
1261 */
1262 buf->lr = ptr;
1263 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001264 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001265 msg->sol = buf->data;
1266 ptr = buf->data;
1267 end = buf->r;
1268#endif
1269 }
1270 hdr_idx_init(idx);
1271 state = HTTP_MSG_RPVER;
1272 goto http_msg_rpver;
1273 }
1274
1275 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1276 goto http_msg_invalid;
1277
1278 if (unlikely(*ptr == '\n'))
1279 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1280 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1281 /* stop here */
1282
1283 http_msg_rpbefore_cr:
1284 case HTTP_MSG_RPBEFORE_CR:
1285 EXPECT_LF_HERE(ptr, http_msg_invalid);
1286 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1287 /* stop here */
1288
1289 http_msg_rpver:
1290 case HTTP_MSG_RPVER:
1291 case HTTP_MSG_RPVER_SP:
1292 case HTTP_MSG_RPCODE:
1293 case HTTP_MSG_RPCODE_SP:
1294 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001295 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001296 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001297 if (unlikely(!ptr))
1298 return;
1299
1300 /* we have a full response and we know that we have either a CR
1301 * or an LF at <ptr>.
1302 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001303 //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 +01001304 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1305
1306 msg->sol = ptr;
1307 if (likely(*ptr == '\r'))
1308 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1309 goto http_msg_rpline_end;
1310
1311 http_msg_rpline_end:
1312 case HTTP_MSG_RPLINE_END:
1313 /* msg->sol must point to the first of CR or LF. */
1314 EXPECT_LF_HERE(ptr, http_msg_invalid);
1315 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1316 /* stop here */
1317
1318 /*
1319 * Second, states that are specific to the request only
1320 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 http_msg_rqbefore:
1322 case HTTP_MSG_RQBEFORE:
1323 if (likely(HTTP_IS_TOKEN(*ptr))) {
1324 if (likely(ptr == buf->data)) {
1325 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001326 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001327 } else {
1328#if PARSE_PRESERVE_EMPTY_LINES
1329 /* only skip empty leading lines, don't remove them */
1330 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001331 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001332#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001333 /* Remove empty leading lines, as recommended by
1334 * RFC2616. This takes a lot of time because we
1335 * must move all the buffer backwards, but this
1336 * is rarely needed. The method above will be
1337 * cleaner when we'll be able to start sending
1338 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001339 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 buf->lr = ptr;
1341 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001342 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001343 msg->sol = buf->data;
1344 ptr = buf->data;
1345 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001346#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001348 /* we will need this when keep-alive will be supported
1349 hdr_idx_init(idx);
1350 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001351 state = HTTP_MSG_RQMETH;
1352 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001353 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001354
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001355 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1356 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001357
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001358 if (unlikely(*ptr == '\n'))
1359 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1360 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001361 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001362
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001363 http_msg_rqbefore_cr:
1364 case HTTP_MSG_RQBEFORE_CR:
1365 EXPECT_LF_HERE(ptr, http_msg_invalid);
1366 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001367 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001368
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001369 http_msg_rqmeth:
1370 case HTTP_MSG_RQMETH:
1371 case HTTP_MSG_RQMETH_SP:
1372 case HTTP_MSG_RQURI:
1373 case HTTP_MSG_RQURI_SP:
1374 case HTTP_MSG_RQVER:
1375 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001376 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001377 if (unlikely(!ptr))
1378 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380 /* we have a full request and we know that we have either a CR
1381 * or an LF at <ptr>.
1382 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001383 //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 +01001384 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001385
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 msg->sol = ptr;
1387 if (likely(*ptr == '\r'))
1388 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001390
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001391 http_msg_rqline_end:
1392 case HTTP_MSG_RQLINE_END:
1393 /* check for HTTP/0.9 request : no version information available.
1394 * msg->sol must point to the first of CR or LF.
1395 */
1396 if (unlikely(msg->sl.rq.v_l == 0))
1397 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001398
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001399 EXPECT_LF_HERE(ptr, http_msg_invalid);
1400 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001401 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001402
Willy Tarreau8973c702007-01-21 23:58:29 +01001403 /*
1404 * Common states below
1405 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 http_msg_hdr_first:
1407 case HTTP_MSG_HDR_FIRST:
1408 msg->sol = ptr;
1409 if (likely(!HTTP_IS_CRLF(*ptr))) {
1410 goto http_msg_hdr_name;
1411 }
1412
1413 if (likely(*ptr == '\r'))
1414 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1415 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001416
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 http_msg_hdr_name:
1418 case HTTP_MSG_HDR_NAME:
1419 /* assumes msg->sol points to the first char */
1420 if (likely(HTTP_IS_TOKEN(*ptr)))
1421 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 if (likely(*ptr == ':')) {
1424 msg->col = ptr - buf->data;
1425 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1426 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001427
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 http_msg_hdr_l1_sp:
1431 case HTTP_MSG_HDR_L1_SP:
1432 /* assumes msg->sol points to the first char and msg->col to the colon */
1433 if (likely(HTTP_IS_SPHT(*ptr)))
1434 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001435
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 /* header value can be basically anything except CR/LF */
1437 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 if (likely(!HTTP_IS_CRLF(*ptr))) {
1440 goto http_msg_hdr_val;
1441 }
1442
1443 if (likely(*ptr == '\r'))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1445 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 http_msg_hdr_l1_lf:
1448 case HTTP_MSG_HDR_L1_LF:
1449 EXPECT_LF_HERE(ptr, http_msg_invalid);
1450 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001451
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452 http_msg_hdr_l1_lws:
1453 case HTTP_MSG_HDR_L1_LWS:
1454 if (likely(HTTP_IS_SPHT(*ptr))) {
1455 /* replace HT,CR,LF with spaces */
1456 for (; buf->data+msg->sov < ptr; msg->sov++)
1457 buf->data[msg->sov] = ' ';
1458 goto http_msg_hdr_l1_sp;
1459 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001460 /* we had a header consisting only in spaces ! */
1461 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 goto http_msg_complete_header;
1463
1464 http_msg_hdr_val:
1465 case HTTP_MSG_HDR_VAL:
1466 /* assumes msg->sol points to the first char, msg->col to the
1467 * colon, and msg->sov points to the first character of the
1468 * value.
1469 */
1470 if (likely(!HTTP_IS_CRLF(*ptr)))
1471 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 msg->eol = ptr;
1474 /* Note: we could also copy eol into ->eoh so that we have the
1475 * real header end in case it ends with lots of LWS, but is this
1476 * really needed ?
1477 */
1478 if (likely(*ptr == '\r'))
1479 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1480 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001481
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001482 http_msg_hdr_l2_lf:
1483 case HTTP_MSG_HDR_L2_LF:
1484 EXPECT_LF_HERE(ptr, http_msg_invalid);
1485 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 http_msg_hdr_l2_lws:
1488 case HTTP_MSG_HDR_L2_LWS:
1489 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1490 /* LWS: replace HT,CR,LF with spaces */
1491 for (; msg->eol < ptr; msg->eol++)
1492 *msg->eol = ' ';
1493 goto http_msg_hdr_val;
1494 }
1495 http_msg_complete_header:
1496 /*
1497 * It was a new header, so the last one is finished.
1498 * Assumes msg->sol points to the first char, msg->col to the
1499 * colon, msg->sov points to the first character of the value
1500 * and msg->eol to the first CR or LF so we know how the line
1501 * ends. We insert last header into the index.
1502 */
1503 /*
1504 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1505 write(2, msg->sol, msg->eol-msg->sol);
1506 fprintf(stderr,"\n");
1507 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1510 idx, idx->tail) < 0))
1511 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001512
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 msg->sol = ptr;
1514 if (likely(!HTTP_IS_CRLF(*ptr))) {
1515 goto http_msg_hdr_name;
1516 }
1517
1518 if (likely(*ptr == '\r'))
1519 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1520 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001521
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 http_msg_last_lf:
1523 case HTTP_MSG_LAST_LF:
1524 /* Assumes msg->sol points to the first of either CR or LF */
1525 EXPECT_LF_HERE(ptr, http_msg_invalid);
1526 ptr++;
1527 buf->lr = ptr;
1528 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001529 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 return;
1531#ifdef DEBUG_FULL
1532 default:
1533 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1534 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001535#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 }
1537 http_msg_ood:
1538 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001539 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 buf->lr = ptr;
1541 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001542
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 http_msg_invalid:
1544 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001545 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 return;
1547}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001548
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549/*
1550 * manages the client FSM and its socket. BTW, it also tries to handle the
1551 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1552 * 0 else.
1553 */
1554int process_cli(struct session *t)
1555{
1556 int s = t->srv_state;
1557 int c = t->cli_state;
1558 struct buffer *req = t->req;
1559 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001560
Willy Tarreau6468d922008-08-03 19:15:35 +02001561 DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u\n",
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 cli_stnames[c], srv_stnames[s],
Willy Tarreauf161a342007-04-08 16:59:42 +02001563 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreau6468d922008-08-03 19:15:35 +02001564 req->rex, rep->wex);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001565
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001566 if (t->analysis & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001567 struct tcp_rule *rule;
1568 int partial;
1569
1570 /* We will abort if we encounter a read error. In theory,
1571 * we should not abort if we get a close, it might be
1572 * valid, also very unlikely. FIXME: we'll abort for now,
1573 * this will be easier to change later.
1574 */
1575 if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1576 t->inspect_exp = TICK_ETERNITY;
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001577 buffer_shutr_done(req);
1578 buffer_shutw_done(rep);
Willy Tarreaub6866442008-07-14 23:54:42 +02001579 fd_delete(t->cli_fd);
1580 t->cli_state = CL_STCLOSE;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001581 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001582 t->fe->failed_req++;
1583 if (!(t->flags & SN_ERR_MASK))
1584 t->flags |= SN_ERR_CLICL;
1585 if (!(t->flags & SN_FINST_MASK))
1586 t->flags |= SN_FINST_R;
1587 return 1;
1588 }
1589
1590 /* Abort if client read timeout has expired */
1591 else if (unlikely(tick_is_expired(req->rex, now_ms))) {
1592 t->inspect_exp = TICK_ETERNITY;
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001593 buffer_shutr_done(req);
1594 buffer_shutw_done(rep);
Willy Tarreaub6866442008-07-14 23:54:42 +02001595 fd_delete(t->cli_fd);
1596 t->cli_state = CL_STCLOSE;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001597 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001598 t->fe->failed_req++;
1599 if (!(t->flags & SN_ERR_MASK))
1600 t->flags |= SN_ERR_CLITO;
1601 if (!(t->flags & SN_FINST_MASK))
1602 t->flags |= SN_FINST_R;
1603 return 1;
1604 }
1605
1606 /* We don't know whether we have enough data, so must proceed
1607 * this way :
1608 * - iterate through all rules in their declaration order
1609 * - if one rule returns MISS, it means the inspect delay is
1610 * not over yet, then return immediately, otherwise consider
1611 * it as a non-match.
1612 * - if one rule returns OK, then return OK
1613 * - if one rule returns KO, then return KO
1614 */
1615
1616 if (tick_is_expired(t->inspect_exp, now_ms))
1617 partial = 0;
1618 else
1619 partial = ACL_PARTIAL;
1620
1621 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1622 int ret = ACL_PAT_PASS;
1623
1624 if (rule->cond) {
1625 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1626 if (ret == ACL_PAT_MISS) {
1627 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
1628 return 0;
1629 }
1630 ret = acl_pass(ret);
1631 if (rule->cond->pol == ACL_COND_UNLESS)
1632 ret = !ret;
1633 }
1634
1635 if (ret) {
1636 /* we have a matching rule. */
1637 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001638 buffer_shutr_done(req);
1639 buffer_shutw_done(rep);
Willy Tarreaub6866442008-07-14 23:54:42 +02001640 fd_delete(t->cli_fd);
1641 t->cli_state = CL_STCLOSE;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001642 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001643 t->fe->failed_req++;
1644 if (!(t->flags & SN_ERR_MASK))
1645 t->flags |= SN_ERR_PRXCOND;
1646 if (!(t->flags & SN_FINST_MASK))
1647 t->flags |= SN_FINST_R;
1648 t->inspect_exp = TICK_ETERNITY;
1649 return 1;
1650 }
1651 /* otherwise accept */
1652 break;
1653 }
1654 }
1655
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001656 /* if we get there, it means we have no rule which matches, or
1657 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001658 */
1659 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001660 t->analysis &= ~AN_REQ_INSPECT;
1661 if (t->analysis & AN_REQ_HTTP_HDR)
Willy Tarreaub6866442008-07-14 23:54:42 +02001662 t->txn.exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001663
Willy Tarreaub6866442008-07-14 23:54:42 +02001664 t->inspect_exp = TICK_ETERNITY;
1665 return 1;
1666 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001667
1668 if (t->analysis & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 /*
1670 * Now parse the partial (or complete) lines.
1671 * We will check the request syntax, and also join multi-line
1672 * headers. An index of all the lines will be elaborated while
1673 * parsing.
1674 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001675 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001677 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001678 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 * req->data + req->eoh = end of processed headers / start of current one
1680 * req->data + req->eol = end of current header or line (LF or CRLF)
1681 * req->lr = first non-visited byte
1682 * req->r = end of data
1683 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001686 struct http_txn *txn = &t->txn;
1687 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001689
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001691 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001692
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 /* 1: we might have to print this header in debug mode */
1694 if (unlikely((global.mode & MODE_DEBUG) &&
1695 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001696 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001698
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001699 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 eol = sol + msg->sl.rq.l;
1701 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001702
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001703 sol += hdr_idx_first_pos(&txn->hdr_idx);
1704 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001705
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001707 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001709 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1710 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001711 }
1712 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001713
Willy Tarreau58f10d72006-12-04 02:26:12 +01001714
1715 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001717 * If not so, we check the FD and buffer states before leaving.
1718 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1720 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001721 *
1722 */
1723
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001724 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 /*
1726 * First, let's catch bad requests.
1727 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001728 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001730
1731 /* 1: Since we are in header mode, if there's no space
1732 * left for headers, we won't be able to free more
1733 * later, so the session will never terminate. We
1734 * must terminate it now.
1735 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001736 if (unlikely(req->l >= req->rlim - req->data)) {
1737 /* FIXME: check if URI is set and return Status
1738 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001739 */
Willy Tarreau06619262006-12-17 08:37:22 +01001740 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001741 }
1742
1743 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1745 /* read error, or last read : give up. */
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001746 buffer_shutr_done(req);
1747 buffer_shutw_done(rep);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001748 fd_delete(t->cli_fd);
1749 t->cli_state = CL_STCLOSE;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001750 t->analysis &= ~AN_REQ_ANY;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001751 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001752 if (!(t->flags & SN_ERR_MASK))
1753 t->flags |= SN_ERR_CLICL;
1754 if (!(t->flags & SN_FINST_MASK))
1755 t->flags |= SN_FINST_R;
1756 return 1;
1757 }
1758
1759 /* 3: has the read timeout expired ? */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001760 else if (unlikely(tick_is_expired(req->rex, now_ms) ||
1761 tick_is_expired(txn->exp, now_ms))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001762 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001763 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001764 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001765 t->analysis &= ~AN_REQ_ANY;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001766 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001767 if (!(t->flags & SN_ERR_MASK))
1768 t->flags |= SN_ERR_CLITO;
1769 if (!(t->flags & SN_FINST_MASK))
1770 t->flags |= SN_FINST_R;
1771 return 1;
1772 }
1773
1774 /* 4: do we need to re-enable the read socket ? */
Willy Tarreauadfb8562008-08-11 15:24:42 +02001775 else if (!tick_isset(req->rex)) {
1776 EV_FD_COND_S(t->cli_fd, DIR_RD);
Willy Tarreauf161a342007-04-08 16:59:42 +02001777 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreau58f10d72006-12-04 02:26:12 +01001778 * full. We cannot loop here since stream_sock_read will disable it only if
1779 * req->l == rlim-data
1780 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001781 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001782 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001783 /* we don't need to resync if the state has not changed */
1784 return !(t->analysis & AN_REQ_HTTP_HDR);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001785 }
1786
1787
1788 /****************************************************************
1789 * More interesting part now : we know that we have a complete *
1790 * request which at least looks like HTTP. We have an indicator *
1791 * of each header's length, so we can parse them quickly. *
1792 ****************************************************************/
1793
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001794 t->analysis &= ~AN_REQ_HTTP_HDR;
1795
Willy Tarreau9cdde232007-05-02 20:58:19 +02001796 /* ensure we keep this pointer to the beginning of the message */
1797 msg->sol = req->data + msg->som;
1798
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001799 /*
1800 * 1: identify the method
1801 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001802 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001803
1804 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001806 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001807 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001808 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001809 if (unlikely((t->fe->monitor_uri_len != 0) &&
1810 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1811 !memcmp(&req->data[msg->sl.rq.u],
1812 t->fe->monitor_uri,
1813 t->fe->monitor_uri_len))) {
1814 /*
1815 * We have found the monitor URI
1816 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001817 struct acl_cond *cond;
1818 cur_proxy = t->fe;
1819
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001821
1822 /* Check if we want to fail this monitor request or not */
1823 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1824 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001825
1826 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001827 if (cond->pol == ACL_COND_UNLESS)
1828 ret = !ret;
1829
1830 if (ret) {
1831 /* we fail this request, let's return 503 service unavail */
1832 txn->status = 503;
1833 client_retnclose(t, error_message(t, HTTP_ERR_503));
1834 goto return_prx_cond;
1835 }
1836 }
1837
1838 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001839 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 client_retnclose(t, &http_200_chunk);
1841 goto return_prx_cond;
1842 }
1843
1844 /*
1845 * 3: Maybe we have to copy the original REQURI for the logs ?
1846 * Note: we cannot log anymore if the request has been
1847 * classified as invalid.
1848 */
1849 if (unlikely(t->logs.logwait & LW_REQ)) {
1850 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001851 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001852 int urilen = msg->sl.rq.l;
1853
1854 if (urilen >= REQURI_LEN)
1855 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001856 memcpy(txn->uri, &req->data[msg->som], urilen);
1857 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001858
1859 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001860 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001861 } else {
1862 Alert("HTTP logging : out of memory.\n");
1863 }
1864 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001865
Willy Tarreau06619262006-12-17 08:37:22 +01001866
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1868 if (unlikely(msg->sl.rq.v_l == 0)) {
1869 int delta;
1870 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001871 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001872 cur_end = msg->sol + msg->sl.rq.l;
1873 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001874
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001875 if (msg->sl.rq.u_l == 0) {
1876 /* if no URI was set, add "/" */
1877 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1878 cur_end += delta;
1879 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001880 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001881 /* add HTTP version */
1882 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1883 msg->eoh += delta;
1884 cur_end += delta;
1885 cur_end = (char *)http_parse_reqline(msg, req->data,
1886 HTTP_MSG_RQMETH,
1887 msg->sol, cur_end + 1,
1888 NULL, NULL);
1889 if (unlikely(!cur_end))
1890 goto return_bad_req;
1891
1892 /* we have a full HTTP/1.0 request now and we know that
1893 * we have either a CR or an LF at <ptr>.
1894 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001895 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001896 }
1897
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001898
1899 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001900 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001901 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001902 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001903
1904 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001905 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001906 * As opposed to version 1.2, now they will be evaluated in the
1907 * filters order and not in the header order. This means that
1908 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001909 *
1910 * We can now check whether we want to switch to another
1911 * backend, in which case we will re-check the backend's
1912 * filters and various options. In order to support 3-level
1913 * switching, here's how we should proceed :
1914 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001915 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001916 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001917 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001918 * There cannot be any switch from there, so ->be cannot be
1919 * changed anymore.
1920 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001921 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001922 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001923 * The response path will be able to apply either ->be, or
1924 * ->be then ->fe filters in order to match the reverse of
1925 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001926 */
1927
Willy Tarreau06619262006-12-17 08:37:22 +01001928 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001929 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001930 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001931 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001932 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001933
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001934 /* first check whether we have some ACLs set to redirect this request */
1935 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
1936 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001937
1938 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001939 if (rule->cond->pol == ACL_COND_UNLESS)
1940 ret = !ret;
1941
1942 if (ret) {
1943 struct chunk rdr = { trash, 0 };
1944 const char *msg_fmt;
1945
1946 /* build redirect message */
1947 switch(rule->code) {
1948 case 303:
1949 rdr.len = strlen(HTTP_303);
1950 msg_fmt = HTTP_303;
1951 break;
1952 case 301:
1953 rdr.len = strlen(HTTP_301);
1954 msg_fmt = HTTP_301;
1955 break;
1956 case 302:
1957 default:
1958 rdr.len = strlen(HTTP_302);
1959 msg_fmt = HTTP_302;
1960 break;
1961 }
1962
1963 if (unlikely(rdr.len > sizeof(trash)))
1964 goto return_bad_req;
1965 memcpy(rdr.str, msg_fmt, rdr.len);
1966
1967 switch(rule->type) {
1968 case REDIRECT_TYPE_PREFIX: {
1969 const char *path;
1970 int pathlen;
1971
1972 path = http_get_path(txn);
1973 /* build message using path */
1974 if (path) {
1975 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
1976 } else {
1977 path = "/";
1978 pathlen = 1;
1979 }
1980
1981 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
1982 goto return_bad_req;
1983
1984 /* add prefix */
1985 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1986 rdr.len += rule->rdr_len;
1987
1988 /* add path */
1989 memcpy(rdr.str + rdr.len, path, pathlen);
1990 rdr.len += pathlen;
1991 break;
1992 }
1993 case REDIRECT_TYPE_LOCATION:
1994 default:
1995 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
1996 goto return_bad_req;
1997
1998 /* add location */
1999 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2000 rdr.len += rule->rdr_len;
2001 break;
2002 }
2003
2004 /* add end of headers */
2005 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2006 rdr.len += 4;
2007
2008 txn->status = rule->code;
2009 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002010 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002011 client_retnclose(t, &rdr);
2012 goto return_prx_cond;
2013 }
2014 }
2015
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002016 /* first check whether we have some ACLs set to block this request */
2017 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002018 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002019
2020 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002021 if (cond->pol == ACL_COND_UNLESS)
2022 ret = !ret;
2023
2024 if (ret) {
2025 txn->status = 403;
2026 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002027 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002028 client_retnclose(t, error_message(t, HTTP_ERR_403));
2029 goto return_prx_cond;
2030 }
2031 }
2032
Willy Tarreau06619262006-12-17 08:37:22 +01002033 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002034 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002035 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2036 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002037 }
2038
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002039 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2040 /* to ensure correct connection accounting on
2041 * the backend, we count the connection for the
2042 * one managing the queue.
2043 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002044 t->be->beconn++;
2045 if (t->be->beconn > t->be->beconn_max)
2046 t->be->beconn_max = t->be->beconn;
2047 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002048 t->flags |= SN_BE_ASSIGNED;
2049 }
2050
Willy Tarreau06619262006-12-17 08:37:22 +01002051 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002052 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002053 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002054 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002055 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002056 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002057 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002058 goto return_prx_cond;
2059 }
2060
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002061 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002062 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002063 !(t->flags & SN_CONN_CLOSED)) {
2064 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002065 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002066 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002067
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002068 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002069 old_idx = 0;
2070
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002071 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2072 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002073 cur_ptr = cur_next;
2074 cur_end = cur_ptr + cur_hdr->len;
2075 cur_next = cur_end + cur_hdr->cr + 1;
2076
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002077 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2078 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002079 /* 3 possibilities :
2080 * - we have already set Connection: close,
2081 * so we remove this line.
2082 * - we have not yet set Connection: close,
2083 * but this line indicates close. We leave
2084 * it untouched and set the flag.
2085 * - we have not yet set Connection: close,
2086 * and this line indicates non-close. We
2087 * replace it.
2088 */
2089 if (t->flags & SN_CONN_CLOSED) {
2090 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002091 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002092 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002093 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2094 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002095 cur_hdr->len = 0;
2096 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002097 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2098 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2099 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002100 cur_next += delta;
2101 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002102 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002103 }
2104 t->flags |= SN_CONN_CLOSED;
2105 }
2106 }
2107 old_idx = cur_idx;
2108 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002109 }
2110 /* add request headers from the rule sets in the same order */
2111 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2112 if (unlikely(http_header_add_tail(req,
2113 &txn->req,
2114 &txn->hdr_idx,
2115 rule_set->req_add[cur_idx])) < 0)
2116 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002117 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002118
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002119 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002120 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002121 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01002122 /* we have to check the URI and auth for this request */
2123 if (stats_check_uri_auth(t, rule_set))
2124 return 1;
2125 }
2126
Willy Tarreau55ea7572007-06-17 19:56:27 +02002127 /* now check whether we have some switching rules for this request */
2128 if (!(t->flags & SN_BE_ASSIGNED)) {
2129 struct switching_rule *rule;
2130
2131 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2132 int ret;
2133
2134 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002135
2136 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002137 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002138 ret = !ret;
2139
2140 if (ret) {
2141 t->be = rule->be.backend;
2142 t->be->beconn++;
2143 if (t->be->beconn > t->be->beconn_max)
2144 t->be->beconn_max = t->be->beconn;
2145 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002146
2147 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002148 t->rep->rto = t->req->wto = t->be->timeout.server;
2149 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002150 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002151 t->flags |= SN_BE_ASSIGNED;
2152 break;
2153 }
2154 }
2155 }
2156
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002157 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2158 /* No backend was set, but there was a default
2159 * backend set in the frontend, so we use it and
2160 * loop again.
2161 */
2162 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002163 t->be->beconn++;
2164 if (t->be->beconn > t->be->beconn_max)
2165 t->be->beconn_max = t->be->beconn;
2166 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002167
2168 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002169 t->rep->rto = t->req->wto = t->be->timeout.server;
2170 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002171 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002172 t->flags |= SN_BE_ASSIGNED;
2173 }
2174 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002175
Willy Tarreau58f10d72006-12-04 02:26:12 +01002176
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002177 if (!(t->flags & SN_BE_ASSIGNED)) {
2178 /* To ensure correct connection accounting on
2179 * the backend, we count the connection for the
2180 * one managing the queue.
2181 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002182 t->be->beconn++;
2183 if (t->be->beconn > t->be->beconn_max)
2184 t->be->beconn_max = t->be->beconn;
2185 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002186 t->flags |= SN_BE_ASSIGNED;
2187 }
2188
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002189 /*
2190 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002191 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002192 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002193 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002194 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002195
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002196 /*
2197 * If HTTP PROXY is set we simply get remote server address
2198 * parsing incoming request.
2199 */
2200 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2201 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2202 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002203
Willy Tarreau2a324282006-12-05 00:05:46 +01002204 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002205 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002206 * so let's do the same now.
2207 */
2208
2209 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002210 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002211 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002212 }
2213
2214
2215 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002216 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002217 * Note that doing so might move headers in the request, but
2218 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002219 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002220 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002221 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2222 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002223 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002224
Willy Tarreau58f10d72006-12-04 02:26:12 +01002225
Willy Tarreau2a324282006-12-05 00:05:46 +01002226 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002227 * 9: add X-Forwarded-For if either the frontend or the backend
2228 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002229 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002230 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002231 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002232 /* Add an X-Forwarded-For header unless the source IP is
2233 * in the 'except' network range.
2234 */
2235 if ((!t->fe->except_mask.s_addr ||
2236 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2237 != t->fe->except_net.s_addr) &&
2238 (!t->be->except_mask.s_addr ||
2239 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2240 != t->be->except_net.s_addr)) {
2241 int len;
2242 unsigned char *pn;
2243 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002244
Ross Westaf72a1d2008-08-03 10:51:45 +02002245 /* Note: we rely on the backend to get the header name to be used for
2246 * x-forwarded-for, because the header is really meant for the backends.
2247 * However, if the backend did not specify any option, we have to rely
2248 * on the frontend's header name.
2249 */
2250 if (t->be->fwdfor_hdr_len) {
2251 len = t->be->fwdfor_hdr_len;
2252 memcpy(trash, t->be->fwdfor_hdr_name, len);
2253 } else {
2254 len = t->fe->fwdfor_hdr_len;
2255 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2256 }
2257 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002258
Ross Westaf72a1d2008-08-03 10:51:45 +02002259 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002260 &txn->hdr_idx, trash, len)) < 0)
2261 goto return_bad_req;
2262 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002263 }
2264 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002265 /* FIXME: for the sake of completeness, we should also support
2266 * 'except' here, although it is mostly useless in this case.
2267 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002268 int len;
2269 char pn[INET6_ADDRSTRLEN];
2270 inet_ntop(AF_INET6,
2271 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2272 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002273
2274 /* Note: we rely on the backend to get the header name to be used for
2275 * x-forwarded-for, because the header is really meant for the backends.
2276 * However, if the backend did not specify any option, we have to rely
2277 * on the frontend's header name.
2278 */
2279 if (t->be->fwdfor_hdr_len) {
2280 len = t->be->fwdfor_hdr_len;
2281 memcpy(trash, t->be->fwdfor_hdr_name, len);
2282 } else {
2283 len = t->fe->fwdfor_hdr_len;
2284 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2285 }
2286 len += sprintf(trash + len, ": %s", pn);
2287
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002288 if (unlikely(http_header_add_tail2(req, &txn->req,
2289 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002290 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002291 }
2292 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002293
Willy Tarreau2a324282006-12-05 00:05:46 +01002294 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002295 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002296 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002297 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002298 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002299 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002300 if ((unlikely(msg->sl.rq.v_l != 8) ||
2301 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2302 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002303 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002304 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002305 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002306 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002307 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2308 * If not assigned, perhaps we are balancing on url_param, but this is a
2309 * POST; and the parameters are in the body, maybe scan there to find our server.
2310 * (unless headers overflowed the buffer?)
2311 */
2312 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2313 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002314 t->be->url_param_post_limit != 0 && req->l < BUFSIZE &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002315 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2316 /* are there enough bytes here? total == l || r || rlim ?
2317 * len is unsigned, but eoh is int,
2318 * how many bytes of body have we received?
2319 * eoh is the first empty line of the header
2320 */
2321 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002322 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 +02002323
2324 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2325 * We can't assume responsibility for the server's decision,
2326 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2327 * We also can't change our mind later, about which server to choose, so round robin.
2328 */
2329 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2330 struct hdr_ctx ctx;
2331 ctx.idx = 0;
2332 /* Expect is allowed in 1.1, look for it */
2333 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2334 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002335 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002336 /* We can't reliablly stall and wait for data, because of
2337 * .NET clients that don't conform to rfc2616; so, no need for
2338 * the next block to check length expectations.
2339 * We could send 100 status back to the client, but then we need to
2340 * re-write headers, and send the message. And this isn't the right
2341 * place for that action.
2342 * TODO: support Expect elsewhere and delete this block.
2343 */
2344 goto end_check_maybe_wait_for_body;
2345 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002346
2347 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002348 /* nothing to do, we got enough */
2349 } else {
2350 /* limit implies we are supposed to need this many bytes
2351 * to find the parameter. Let's see how many bytes we can wait for.
2352 */
2353 long long hint = len;
2354 struct hdr_ctx ctx;
2355 ctx.idx = 0;
2356 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002357 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0)
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002358 t->analysis |= AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002359 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002360 ctx.idx = 0;
2361 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2362 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002363 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002364 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002365 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002366 hint = 0; /* parse failure, untrusted client */
2367 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002368 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002369 msg->hdr_content_len = hint;
2370 else
2371 hint = 0; /* bad client, sent negative length */
2372 }
2373 }
2374 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002375 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002376 hint = t->be->url_param_post_limit;
2377 /* now do we really need to buffer more data? */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002378 if (len < hint)
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002379 t->analysis |= AN_REQ_HTTP_BODY;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002380 /* else... There are no body bytes to wait for */
2381 }
2382 }
2383 }
2384 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002385
Willy Tarreau2a324282006-12-05 00:05:46 +01002386 /*************************************************************
2387 * OK, that's finished for the headers. We have done what we *
2388 * could. Let's switch to the DATA state. *
2389 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002390
Willy Tarreauadfb8562008-08-11 15:24:42 +02002391 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002392 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002393
Willy Tarreau7f875f62008-08-11 17:35:01 +02002394 if (req->l >= req->rlim - req->data) {
2395 /* no room to read more data */
2396 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
2397 /* stop reading until we get some space */
2398 req->rex = TICK_ETERNITY;
2399 }
2400 } else {
2401 EV_FD_COND_S(t->cli_fd, DIR_RD);
2402 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
2403 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002404
Willy Tarreau1fa31262007-12-03 00:36:16 +01002405 /* When a connection is tarpitted, we use the tarpit timeout,
2406 * which may be the same as the connect timeout if unspecified.
2407 * If unset, then set it to zero because we really want it to
2408 * eventually expire.
Willy Tarreau2a324282006-12-05 00:05:46 +01002409 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002410 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002411 t->req->l = 0;
2412 /* flush the request so that we can drop the connection early
2413 * if the client closes first.
2414 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002415 req->cex = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2416 if (!req->cex)
2417 req->cex = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002418 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002419
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002420 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002421 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002422
2423 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002424 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002425 txn->status = 400;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002426 t->analysis &= ~AN_REQ_ANY;
Willy Tarreau80587432006-12-24 17:47:20 +01002427 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002428 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002429 return_prx_cond:
2430 if (!(t->flags & SN_ERR_MASK))
2431 t->flags |= SN_ERR_PRXCOND;
2432 if (!(t->flags & SN_FINST_MASK))
2433 t->flags |= SN_FINST_R;
2434 return 1;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002435 end_of_headers:
2436 ; // to make gcc happy
2437 }
2438
2439 if (t->analysis & AN_REQ_HTTP_BODY) {
2440 /* We have to parse the HTTP request body to find any required data.
2441 * "balance url_param check_post" should have been the only way to get
2442 * into this. We were brought here after HTTP header analysis, so all
2443 * related structures are ready.
2444 */
2445 struct http_msg *msg = &t->txn.req;
2446 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2447 long long limit = t->be->url_param_post_limit;
2448 struct hdr_ctx ctx;
2449
2450 ctx.idx = 0;
2451
2452 /* now if we have a length, we'll take the hint */
2453 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2454 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2455 unsigned int chunk = 0;
2456 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2457 char c = msg->sol[body];
2458 if (ishex(c)) {
2459 unsigned int hex = toupper(c) - '0';
2460 if (hex > 9)
2461 hex -= 'A' - '9' - 1;
2462 chunk = (chunk << 4) | hex;
2463 } else
2464 break;
2465 body++;
2466 }
2467 if (body + 2 >= req->l) /* we want CRLF too */
2468 goto http_body_incomplete; /* end of buffer? data missing! */
2469
2470 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
2471 goto http_body_incomplete; /* chunked encoding len ends with CRLF, and we don't have it yet */
2472
2473 body += 2; // skip CRLF
2474
2475 /* if we support more then one chunk here, we have to do it again when assigning server
2476 * 1. how much entity data do we have? new var
2477 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2478 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2479 */
2480
2481 if (chunk < limit)
2482 limit = chunk; /* only reading one chunk */
2483 } else {
2484 if (msg->hdr_content_len < limit)
2485 limit = msg->hdr_content_len;
2486 }
2487
2488 if (req->l - body >= limit) {
2489 /* we have enough bytes ! */
2490 t->logs.tv_request = now; /* update the request timer to reflect full request */
2491 t->analysis &= ~AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002492
Willy Tarreau7f875f62008-08-11 17:35:01 +02002493 if (req->l >= req->rlim - req->data) {
2494 /* no room to read more data */
2495 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
2496 /* stop reading until we get some space */
2497 req->rex = TICK_ETERNITY;
2498 }
2499 } else {
2500 EV_FD_COND_S(t->cli_fd, DIR_RD);
2501 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
2502 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002503 return 1;
2504 }
2505
2506 http_body_incomplete:
2507 if (req->l >= req->rlim - req->data ||
2508 req->flags & (BF_READ_ERROR | BF_READ_NULL) ||
2509 tick_is_expired(req->rex, now_ms)) {
2510 /* The situation will not evolve, so let's give up on the analysis. */
2511 t->logs.tv_request = now; /* update the request timer to reflect full request */
2512 t->analysis &= ~AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002513
Willy Tarreau7f875f62008-08-11 17:35:01 +02002514 if (req->l >= req->rlim - req->data) {
2515 /* no room to read more data */
2516 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
2517 /* stop reading until we get some space */
2518 req->rex = TICK_ETERNITY;
2519 }
2520 } else {
2521 EV_FD_COND_S(t->cli_fd, DIR_RD);
2522 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
2523 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002524 return 1;
2525 }
2526
Willy Tarreau7f875f62008-08-11 17:35:01 +02002527 EV_FD_COND_S(t->cli_fd, DIR_RD);
2528 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002529 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002530 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002531
Willy Tarreau7f875f62008-08-11 17:35:01 +02002532 /* if no analysis remains, it's time to forward the connection */
2533 if (!(t->analysis & AN_REQ_ANY) && !(req->flags & (BF_MAY_CONNECT|BF_MAY_FORWARD)))
2534 req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD;
2535
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002536 if (c == CL_STDATA) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002537 /* FIXME: this error handling is partly buggy because we always report
2538 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
2539 * or HEADER phase. BTW, it's not logical to expire the client while
2540 * we're waiting for the server to connect.
2541 */
2542 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002543 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002544 buffer_shutr_done(req);
2545 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002546 fd_delete(t->cli_fd);
2547 t->cli_state = CL_STCLOSE;
2548 if (!(t->flags & SN_ERR_MASK))
2549 t->flags |= SN_ERR_CLICL;
2550 if (!(t->flags & SN_FINST_MASK)) {
2551 if (t->pend_pos)
2552 t->flags |= SN_FINST_Q;
2553 else if (s == SV_STCONN)
2554 t->flags |= SN_FINST_C;
2555 else
2556 t->flags |= SN_FINST_D;
2557 }
2558 return 1;
2559 }
2560 /* last read, or end of server write */
Willy Tarreau718f0ef2008-08-10 16:21:32 +02002561 else if (req->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002562 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002563 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002564 t->cli_state = CL_STSHUTR;
2565 return 1;
2566 }
2567 /* last server read and buffer empty */
Willy Tarreau718f0ef2008-08-10 16:21:32 +02002568 else if (rep->flags & BF_SHUTR_STATUS && rep->l == 0) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002569 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002570 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002571 shutdown(t->cli_fd, SHUT_WR);
2572 /* We must ensure that the read part is still alive when switching
2573 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002574 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002575 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002576 t->cli_state = CL_STSHUTW;
2577 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2578 return 1;
2579 }
2580 /* read timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002581 else if (tick_is_expired(req->rex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002582 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002583 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002584 t->cli_state = CL_STSHUTR;
2585 if (!(t->flags & SN_ERR_MASK))
2586 t->flags |= SN_ERR_CLITO;
2587 if (!(t->flags & SN_FINST_MASK)) {
2588 if (t->pend_pos)
2589 t->flags |= SN_FINST_Q;
2590 else if (s == SV_STCONN)
2591 t->flags |= SN_FINST_C;
2592 else
2593 t->flags |= SN_FINST_D;
2594 }
2595 return 1;
2596 }
2597 /* write timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002598 else if (tick_is_expired(rep->wex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002599 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002600 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002601 shutdown(t->cli_fd, SHUT_WR);
2602 /* We must ensure that the read part is still alive when switching
2603 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002604 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002605 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002606
2607 t->cli_state = CL_STSHUTW;
2608 if (!(t->flags & SN_ERR_MASK))
2609 t->flags |= SN_ERR_CLITO;
2610 if (!(t->flags & SN_FINST_MASK)) {
2611 if (t->pend_pos)
2612 t->flags |= SN_FINST_Q;
2613 else if (s == SV_STCONN)
2614 t->flags |= SN_FINST_C;
2615 else
2616 t->flags |= SN_FINST_D;
2617 }
2618 return 1;
2619 }
2620
2621 if (req->l >= req->rlim - req->data) {
2622 /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02002623 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002624 /* stop reading until we get some space */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002625 req->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002626 }
2627 } else {
Willy Tarreau7f875f62008-08-11 17:35:01 +02002628 EV_FD_COND_S(t->cli_fd, DIR_RD);
2629 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002630 }
2631
Willy Tarreau718f0ef2008-08-10 16:21:32 +02002632 /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
2633 if ((rep->l == 0) || !(rep->flags & BF_MAY_FORWARD)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002634 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2635 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002636 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002637 }
2638 } else {
2639 /* buffer not empty */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002640 if (!tick_isset(rep->wex)) {
2641 EV_FD_COND_S(t->cli_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02002642 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002643 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002644 if (tick_isset(rep->wex)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002645 /* FIXME: to prevent the client from expiring read timeouts during writes,
2646 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002647 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002648 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002649 }
2650 }
2651 return 0; /* other cases change nothing */
2652 }
2653 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002654 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002655 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002656 fd_delete(t->cli_fd);
2657 t->cli_state = CL_STCLOSE;
2658 if (!(t->flags & SN_ERR_MASK))
2659 t->flags |= SN_ERR_CLICL;
2660 if (!(t->flags & SN_FINST_MASK)) {
2661 if (t->pend_pos)
2662 t->flags |= SN_FINST_Q;
2663 else if (s == SV_STCONN)
2664 t->flags |= SN_FINST_C;
2665 else
2666 t->flags |= SN_FINST_D;
2667 }
2668 return 1;
2669 }
Willy Tarreau718f0ef2008-08-10 16:21:32 +02002670 else if ((rep->flags & BF_SHUTR_STATUS) && (rep->l == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002671 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002672 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002673 fd_delete(t->cli_fd);
2674 t->cli_state = CL_STCLOSE;
2675 return 1;
2676 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002677 else if (tick_is_expired(rep->wex, now_ms)) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002678 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002679 fd_delete(t->cli_fd);
2680 t->cli_state = CL_STCLOSE;
2681 if (!(t->flags & SN_ERR_MASK))
2682 t->flags |= SN_ERR_CLITO;
2683 if (!(t->flags & SN_FINST_MASK)) {
2684 if (t->pend_pos)
2685 t->flags |= SN_FINST_Q;
2686 else if (s == SV_STCONN)
2687 t->flags |= SN_FINST_C;
2688 else
2689 t->flags |= SN_FINST_D;
2690 }
2691 return 1;
2692 }
2693
2694 if (t->flags & SN_SELF_GEN) {
2695 produce_content(t);
2696 if (rep->l == 0) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002697 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002698 fd_delete(t->cli_fd);
2699 t->cli_state = CL_STCLOSE;
2700 return 1;
2701 }
2702 }
2703
Willy Tarreau718f0ef2008-08-10 16:21:32 +02002704 if ((rep->l == 0) || !(rep->flags & BF_MAY_FORWARD)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002705 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2706 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002707 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002708 }
2709 } else {
2710 /* buffer not empty */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002711 if (!tick_isset(rep->wex)) {
2712 EV_FD_COND_S(t->cli_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02002713 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002714 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002715 }
2716 }
2717 return 0;
2718 }
2719 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002720 if (req->flags & BF_READ_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002721 buffer_shutr_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002722 fd_delete(t->cli_fd);
2723 t->cli_state = CL_STCLOSE;
2724 if (!(t->flags & SN_ERR_MASK))
2725 t->flags |= SN_ERR_CLICL;
2726 if (!(t->flags & SN_FINST_MASK)) {
2727 if (t->pend_pos)
2728 t->flags |= SN_FINST_Q;
2729 else if (s == SV_STCONN)
2730 t->flags |= SN_FINST_C;
2731 else
2732 t->flags |= SN_FINST_D;
2733 }
2734 return 1;
2735 }
Willy Tarreau718f0ef2008-08-10 16:21:32 +02002736 else if (req->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002737 buffer_shutr_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002738 fd_delete(t->cli_fd);
2739 t->cli_state = CL_STCLOSE;
2740 return 1;
2741 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002742 else if (tick_is_expired(req->rex, now_ms)) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002743 buffer_shutr_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002744 fd_delete(t->cli_fd);
2745 t->cli_state = CL_STCLOSE;
2746 if (!(t->flags & SN_ERR_MASK))
2747 t->flags |= SN_ERR_CLITO;
2748 if (!(t->flags & SN_FINST_MASK)) {
2749 if (t->pend_pos)
2750 t->flags |= SN_FINST_Q;
2751 else if (s == SV_STCONN)
2752 t->flags |= SN_FINST_C;
2753 else
2754 t->flags |= SN_FINST_D;
2755 }
2756 return 1;
2757 }
2758 else if (req->l >= req->rlim - req->data) {
2759 /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02002760 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002761 /* stop reading until we get some space */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002762 req->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002763 }
2764 } else {
Willy Tarreau7f875f62008-08-11 17:35:01 +02002765 EV_FD_COND_S(t->cli_fd, DIR_RD);
2766 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002767 }
2768 return 0;
2769 }
2770 else { /* CL_STCLOSE: nothing to do */
2771 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2772 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002773 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);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002774 write(1, trash, len);
2775 }
2776 return 0;
2777 }
2778 return 0;
2779}
2780
2781
2782/*
2783 * manages the server FSM and its socket. It returns 1 if a state has changed
2784 * (and a resync may be needed), 0 else.
2785 */
2786int process_srv(struct session *t)
2787{
2788 int s = t->srv_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002789 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002790 struct buffer *req = t->req;
2791 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002792 int conn_err;
2793
Willy Tarreau6468d922008-08-03 19:15:35 +02002794 DPRINTF(stderr,"process_srv: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u\n",
Willy Tarreau9f1f24b2008-08-11 11:20:03 +02002795 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreau6468d922008-08-03 19:15:35 +02002796 EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR),
2797 rep->rex, req->wex);
Willy Tarreauee991362007-05-14 14:37:50 +02002798
Willy Tarreaubaaee002006-06-26 02:48:02 +02002799 if (s == SV_STIDLE) {
Willy Tarreaudc0a6a02008-08-03 20:38:13 +02002800 if ((rep->flags & BF_SHUTW_STATUS) ||
Willy Tarreau6468d922008-08-03 19:15:35 +02002801 ((req->flags & BF_SHUTR_STATUS) &&
2802 (req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002803 req->cex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002804 if (t->pend_pos)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002805 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002806 /* note that this must not return any error because it would be able to
2807 * overwrite the client_retnclose() output.
2808 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002809 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002810 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002811 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002812 srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002813
2814 return 1;
2815 }
Willy Tarreaudc0a6a02008-08-03 20:38:13 +02002816 else if (req->flags & BF_MAY_CONNECT) {
2817 /* the client allows the server to connect */
Willy Tarreau3d300592007-03-18 18:34:41 +01002818 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002819 /* This connection is being tarpitted. The CLIENT side has
2820 * already set the connect expiration date to the right
2821 * timeout. We just have to check that it has not expired.
2822 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002823 if (!tick_is_expired(req->cex, now_ms))
Willy Tarreaub8750a82006-09-03 09:56:00 +02002824 return 0;
2825
2826 /* We will set the queue timer to the time spent, just for
2827 * logging purposes. We fake a 500 server error, so that the
2828 * attacker will not suspect his connection has been tarpitted.
2829 * It will not cause trouble to the logs because we can exclude
2830 * the tarpitted connections by filtering on the 'PT' status flags.
2831 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002832 req->cex = TICK_ETERNITY;
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002833 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub8750a82006-09-03 09:56:00 +02002834 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002835 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002836 return 1;
2837 }
2838
Willy Tarreaubaaee002006-06-26 02:48:02 +02002839 /* Right now, we will need to create a connection to the server.
2840 * We might already have tried, and got a connection pending, in
2841 * which case we will not do anything till it's pending. It's up
2842 * to any other session to release it and wake us up again.
2843 */
2844 if (t->pend_pos) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002845 if (!tick_is_expired(req->cex, now_ms)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002846 return 0;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002847 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002848 /* we've been waiting too long here */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002849 req->cex = TICK_ETERNITY;
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002850 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002851 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002852 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002853 if (t->srv)
2854 t->srv->failed_conns++;
Willy Tarreau50fd1e12007-12-10 15:25:35 +01002855 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002856 return 1;
2857 }
2858 }
2859
2860 do {
2861 /* first, get a connection */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002862 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2863 t->flags |= SN_REDIRECTABLE;
2864
Willy Tarreaubaaee002006-06-26 02:48:02 +02002865 if (srv_redispatch_connect(t))
2866 return t->srv_state != SV_STIDLE;
2867
Willy Tarreau21d2af32008-02-14 20:25:24 +01002868 if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) {
2869 /* Server supporting redirection and it is possible.
2870 * Invalid requests are reported as such. It concerns all
2871 * the largest ones.
2872 */
2873 struct chunk rdr;
2874 char *path;
2875 int len;
2876
2877 /* 1: create the response header */
2878 rdr.len = strlen(HTTP_302);
2879 rdr.str = trash;
2880 memcpy(rdr.str, HTTP_302, rdr.len);
2881
2882 /* 2: add the server's prefix */
2883 if (rdr.len + t->srv->rdr_len > sizeof(trash))
2884 goto cancel_redir;
2885
2886 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
2887 rdr.len += t->srv->rdr_len;
2888
2889 /* 3: add the request URI */
2890 path = http_get_path(txn);
2891 if (!path)
2892 goto cancel_redir;
2893 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2894 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
2895 goto cancel_redir;
2896
2897 memcpy(rdr.str + rdr.len, path, len);
2898 rdr.len += len;
2899 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2900 rdr.len += 4;
2901
2902 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
2903 /* FIXME: we should increase a counter of redirects per server and per backend. */
2904 if (t->srv)
2905 t->srv->cum_sess++;
2906 return 1;
2907 cancel_redir:
2908 txn->status = 400;
2909 t->fe->failed_req++;
2910 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
2911 400, error_message(t, HTTP_ERR_400));
2912 return 1;
2913 }
2914
Willy Tarreaubaaee002006-06-26 02:48:02 +02002915 /* try to (re-)connect to the server, and fail if we expire the
2916 * number of retries.
2917 */
2918 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002919 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002920 return t->srv_state != SV_STIDLE;
2921 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002922 } while (1);
2923 }
2924 }
2925 else if (s == SV_STCONN) { /* connection in progress */
Willy Tarreau6468d922008-08-03 19:15:35 +02002926 if ((rep->flags & BF_SHUTW_STATUS) ||
2927 ((req->flags & BF_SHUTR_STATUS) &&
2928 ((req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
Willy Tarreauc9b654b2007-05-08 14:46:53 +02002929 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002930 req->cex = TICK_ETERNITY;
Willy Tarreauf899b942008-03-28 18:09:38 +01002931 if (!(t->flags & SN_CONN_TAR)) {
2932 /* if we are in turn-around, we have already closed the FD */
2933 fd_delete(t->srv_fd);
2934 if (t->srv) {
2935 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002936 sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002937 }
Willy Tarreau51406232008-03-10 22:04:20 +01002938 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002939
2940 /* note that this must not return any error because it would be able to
2941 * overwrite the client_retnclose() output.
2942 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002943 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002944 return 1;
2945 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002946 if (!(req->flags & BF_WRITE_STATUS) && !tick_is_expired(req->cex, now_ms)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002947 return 0; /* nothing changed */
2948 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002949 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002950 /* timeout, asynchronous connect error or first write error */
2951 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2952
Willy Tarreau541b5c22008-01-06 23:34:21 +01002953 if (t->flags & SN_CONN_TAR) {
2954 /* We are doing a turn-around waiting for a new connection attempt. */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002955 if (!tick_is_expired(req->cex, now_ms))
Willy Tarreau541b5c22008-01-06 23:34:21 +01002956 return 0;
2957 t->flags &= ~SN_CONN_TAR;
2958 }
2959 else {
2960 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01002961 if (t->srv) {
Willy Tarreau541b5c22008-01-06 23:34:21 +01002962 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002963 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01002964 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002965
Willy Tarreau541b5c22008-01-06 23:34:21 +01002966 if (!(req->flags & BF_WRITE_STATUS))
2967 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2968 else
2969 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2970
2971 /* ensure that we have enough retries left */
2972 if (srv_count_retry_down(t, conn_err))
2973 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002974
Willy Tarreau541b5c22008-01-06 23:34:21 +01002975 if (req->flags & BF_WRITE_ERROR) {
2976 /* we encountered an immediate connection error, and we
2977 * will have to retry connecting to the same server, most
2978 * likely leading to the same result. To avoid this, we
2979 * fake a connection timeout to retry after a turn-around
2980 * time of 1 second. We will wait in the previous if block.
2981 */
2982 t->flags |= SN_CONN_TAR;
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002983 req->cex = tick_add(now_ms, MS_TO_TICKS(1000));
Willy Tarreau541b5c22008-01-06 23:34:21 +01002984 return 0;
2985 }
2986 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002987
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002988 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002989 /* We're on our last chance, and the REDISP option was specified.
2990 * We will ignore cookie and force to balance or use the dispatcher.
2991 */
2992 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002993 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02002994 process_srv_queue(t->srv);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002995
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01002996 /* it's left to the dispatcher to choose a server */
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002997 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002998 t->prev_srv = t->srv;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002999
3000 /* first, get a connection */
3001 if (srv_redispatch_connect(t))
Willy Tarreau00559e72008-01-06 23:46:19 +01003002 return t->srv_state != SV_STCONN;
Krzysztof Piotr Oledzki626a19b2008-02-04 02:10:09 +01003003 } else {
3004 if (t->srv)
3005 t->srv->retries++;
3006 t->be->retries++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003007 }
3008
Willy Tarreaubaaee002006-06-26 02:48:02 +02003009 do {
3010 /* Now we will try to either reconnect to the same server or
3011 * connect to another server. If the connection gets queued
3012 * because all servers are saturated, then we will go back to
3013 * the SV_STIDLE state.
3014 */
3015 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003016 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003017 return t->srv_state != SV_STCONN;
3018 }
3019
3020 /* we need to redispatch the connection to another server */
3021 if (srv_redispatch_connect(t))
3022 return t->srv_state != SV_STCONN;
3023 } while (1);
3024 }
3025 else { /* no error or write 0 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003026 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003027
3028 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
3029 if (req->l == 0) /* nothing to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02003030 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003031 req->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003032 } else /* need the right to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02003033 EV_FD_SET(t->srv_fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003034 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003035 if (tick_isset(req->wex)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003036 /* FIXME: to prevent the server from expiring read timeouts during writes,
3037 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003038 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003039 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003040 }
3041
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003042 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreauf161a342007-04-08 16:59:42 +02003043 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003044 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003045 t->srv_state = SV_STDATA;
Willy Tarreau718f0ef2008-08-10 16:21:32 +02003046 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003047 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
3048
3049 /* if the user wants to log as soon as possible, without counting
3050 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01003051 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003052 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02003053 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003054 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01003055#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003056 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01003057 /* TCP splicing supported by both FE and BE */
3058 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3059 }
3060#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003061 }
3062 else {
3063 t->srv_state = SV_STHEADERS;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003064 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003065 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3066 /* reset hdr_idx which was already initialized by the request.
3067 * right now, the http parser does it.
3068 * hdr_idx_init(&t->txn.hdr_idx);
3069 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003070 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003071 req->cex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003072 return 1;
3073 }
3074 }
3075 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003076 /*
3077 * Now parse the partial (or complete) lines.
3078 * We will check the response syntax, and also join multi-line
3079 * headers. An index of all the lines will be elaborated while
3080 * parsing.
3081 *
3082 * For the parsing, we use a 28 states FSM.
3083 *
3084 * Here is the information we currently have :
3085 * rep->data + req->som = beginning of response
3086 * rep->data + req->eoh = end of processed headers / start of current one
3087 * rep->data + req->eol = end of current header or line (LF or CRLF)
3088 * rep->lr = first non-visited byte
3089 * rep->r = end of data
3090 */
3091
3092 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003093 struct http_msg *msg = &txn->rsp;
3094 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003095
Willy Tarreaua15645d2007-03-18 16:22:39 +01003096 if (likely(rep->lr < rep->r))
3097 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003098
Willy Tarreaua15645d2007-03-18 16:22:39 +01003099 /* 1: we might have to print this header in debug mode */
3100 if (unlikely((global.mode & MODE_DEBUG) &&
3101 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
3102 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
3103 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003104
Willy Tarreaua15645d2007-03-18 16:22:39 +01003105 sol = rep->data + msg->som;
3106 eol = sol + msg->sl.rq.l;
3107 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003108
Willy Tarreaua15645d2007-03-18 16:22:39 +01003109 sol += hdr_idx_first_pos(&txn->hdr_idx);
3110 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003111
Willy Tarreaua15645d2007-03-18 16:22:39 +01003112 while (cur_idx) {
3113 eol = sol + txn->hdr_idx.v[cur_idx].len;
3114 debug_hdr("srvhdr", t, sol, eol);
3115 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3116 cur_idx = txn->hdr_idx.v[cur_idx].next;
3117 }
3118 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003119
Willy Tarreaubaaee002006-06-26 02:48:02 +02003120
Willy Tarreauadfb8562008-08-11 15:24:42 +02003121 if ((rep->l < rep->rlim - rep->data) && !tick_isset(rep->rex)) {
3122 EV_FD_COND_S(t->srv_fd, DIR_RD);
Willy Tarreauf161a342007-04-08 16:59:42 +02003123 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreaua15645d2007-03-18 16:22:39 +01003124 * full. We cannot loop here since stream_sock_read will disable it only if
3125 * rep->l == rlim-data
3126 */
Willy Tarreauce09c522008-08-11 10:35:07 +02003127 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003128 }
3129
3130
3131 /*
3132 * Now we quickly check if we have found a full valid response.
3133 * If not so, we check the FD and buffer states before leaving.
3134 * A full response is indicated by the fact that we have seen
3135 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
3136 * responses are checked first.
3137 *
3138 * Depending on whether the client is still there or not, we
3139 * may send an error response back or not. Note that normally
3140 * we should only check for HTTP status there, and check I/O
3141 * errors somewhere else.
3142 */
3143
3144 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
3145
3146 /* Invalid response, or read error or write error */
3147 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
3148 (req->flags & BF_WRITE_ERROR) ||
3149 (rep->flags & BF_READ_ERROR))) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003150 buffer_shutr_done(rep);
3151 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003152 fd_delete(t->srv_fd);
3153 if (t->srv) {
3154 t->srv->cur_sess--;
3155 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003156 sess_change_server(t, NULL);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003157 }
3158 t->be->failed_resp++;
3159 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003160 rep->flags |= BF_MAY_FORWARD;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003161 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003162 client_return(t, error_message(t, HTTP_ERR_502));
3163 if (!(t->flags & SN_ERR_MASK))
3164 t->flags |= SN_ERR_SRVCL;
3165 if (!(t->flags & SN_FINST_MASK))
3166 t->flags |= SN_FINST_H;
3167 /* We used to have a free connection slot. Since we'll never use it,
3168 * we have to inform the server that it may be used by another session.
3169 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003170 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003171 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003172
Willy Tarreaua15645d2007-03-18 16:22:39 +01003173 return 1;
3174 }
3175
3176 /* end of client write or end of server read.
3177 * since we are in header mode, if there's no space left for headers, we
3178 * won't be able to free more later, so the session will never terminate.
3179 */
Willy Tarreau6468d922008-08-03 19:15:35 +02003180 else if (unlikely(rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01003181 rep->l >= rep->rlim - rep->data)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003182 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003183 buffer_shutr_done(rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003184 t->srv_state = SV_STSHUTR;
3185 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3186 return 1;
3187 }
3188
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003189 /* read timeout : return a 504 to the client. */
Willy Tarreauf161a342007-04-08 16:59:42 +02003190 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003191 tick_is_expired(rep->rex, now_ms))) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003192 buffer_shutr_done(rep);
3193 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003194 fd_delete(t->srv_fd);
3195 if (t->srv) {
3196 t->srv->cur_sess--;
3197 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003198 sess_change_server(t, NULL);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003199 }
3200 t->be->failed_resp++;
3201 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003202 rep->flags |= BF_MAY_FORWARD;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003203 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003204 client_return(t, error_message(t, HTTP_ERR_504));
3205 if (!(t->flags & SN_ERR_MASK))
3206 t->flags |= SN_ERR_SRVTO;
3207 if (!(t->flags & SN_FINST_MASK))
3208 t->flags |= SN_FINST_H;
3209 /* We used to have a free connection slot. Since we'll never use it,
3210 * we have to inform the server that it may be used by another session.
3211 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003212 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003213 process_srv_queue(t->srv);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003214 return 1;
3215 }
3216
3217 /* last client read and buffer empty */
3218 /* FIXME!!! here, we don't want to switch to SHUTW if the
3219 * client shuts read too early, because we may still have
3220 * some work to do on the headers.
3221 * The side-effect is that if the client completely closes its
3222 * connection during SV_STHEADER, the connection to the server
3223 * is kept until a response comes back or the timeout is reached.
3224 */
Willy Tarreau6468d922008-08-03 19:15:35 +02003225 else if (0 && /* we don't want to switch to shutw for now */
3226 unlikely(req->flags & BF_SHUTR_STATUS && (req->l == 0))) {
3227
Willy Tarreauf161a342007-04-08 16:59:42 +02003228 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003229 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003230
3231 /* We must ensure that the read part is still
3232 * alive when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003233 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003234 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003235
3236 shutdown(t->srv_fd, SHUT_WR);
3237 t->srv_state = SV_STSHUTW;
3238 return 1;
3239 }
3240
3241 /* write timeout */
3242 /* FIXME!!! here, we don't want to switch to SHUTW if the
3243 * client shuts read too early, because we may still have
3244 * some work to do on the headers.
3245 */
Willy Tarreauf161a342007-04-08 16:59:42 +02003246 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003247 tick_is_expired(req->wex, now_ms))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003248 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003249 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003250 shutdown(t->srv_fd, SHUT_WR);
3251 /* We must ensure that the read part is still alive
3252 * when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003253 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003254 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003255
3256 t->srv_state = SV_STSHUTW;
3257 if (!(t->flags & SN_ERR_MASK))
3258 t->flags |= SN_ERR_SRVTO;
3259 if (!(t->flags & SN_FINST_MASK))
3260 t->flags |= SN_FINST_H;
3261 return 1;
3262 }
3263
3264 /*
3265 * And now the non-error cases.
3266 */
3267
3268 /* Data remaining in the request buffer.
3269 * This happens during the first pass here, and during
3270 * long posts.
3271 */
3272 else if (likely(req->l)) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003273 if (!tick_isset(req->wex)) {
3274 EV_FD_COND_S(t->srv_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02003275 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003276 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003277 if (tick_isset(req->wex)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003278 /* FIXME: to prevent the server from expiring read timeouts during writes,
3279 * we refresh it. */
3280 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003281 }
3282 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003283 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003284
Willy Tarreaua15645d2007-03-18 16:22:39 +01003285 /* nothing left in the request buffer */
3286 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003287 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3288 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003289 req->wex = TICK_ETERNITY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003290 }
3291 }
3292
3293 return t->srv_state != SV_STHEADERS;
3294 }
3295
3296
3297 /*****************************************************************
3298 * More interesting part now : we know that we have a complete *
3299 * response which at least looks like HTTP. We have an indicator *
3300 * of each header's length, so we can parse them quickly. *
3301 ****************************************************************/
3302
Willy Tarreau9cdde232007-05-02 20:58:19 +02003303 /* ensure we keep this pointer to the beginning of the message */
3304 msg->sol = rep->data + msg->som;
3305
Willy Tarreaua15645d2007-03-18 16:22:39 +01003306 /*
3307 * 1: get the status code and check for cacheability.
3308 */
3309
3310 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003311 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003312
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003313 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003314 case 200:
3315 case 203:
3316 case 206:
3317 case 300:
3318 case 301:
3319 case 410:
3320 /* RFC2616 @13.4:
3321 * "A response received with a status code of
3322 * 200, 203, 206, 300, 301 or 410 MAY be stored
3323 * by a cache (...) unless a cache-control
3324 * directive prohibits caching."
3325 *
3326 * RFC2616 @9.5: POST method :
3327 * "Responses to this method are not cacheable,
3328 * unless the response includes appropriate
3329 * Cache-Control or Expires header fields."
3330 */
3331 if (likely(txn->meth != HTTP_METH_POST) &&
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003332 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
Willy Tarreau3d300592007-03-18 18:34:41 +01003333 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003334 break;
3335 default:
3336 break;
3337 }
3338
3339 /*
3340 * 2: we may need to capture headers
3341 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003342 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003343 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003344 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003345
3346 /*
3347 * 3: we will have to evaluate the filters.
3348 * As opposed to version 1.2, now they will be evaluated in the
3349 * filters order and not in the header order. This means that
3350 * each filter has to be validated among all headers.
3351 *
3352 * Filters are tried with ->be first, then with ->fe if it is
3353 * different from ->be.
3354 */
3355
3356 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
3357
3358 cur_proxy = t->be;
3359 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003360 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003361
3362 /* try headers filters */
3363 if (rule_set->rsp_exp != NULL) {
3364 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3365 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003366 if (t->srv) {
3367 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003368 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003369 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003370 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003371 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003372 return_srv_prx_502:
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003373 buffer_shutr_done(rep);
3374 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003375 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003376 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003377 rep->flags |= BF_MAY_FORWARD;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003378 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01003379 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003380 if (!(t->flags & SN_ERR_MASK))
3381 t->flags |= SN_ERR_PRXCOND;
3382 if (!(t->flags & SN_FINST_MASK))
3383 t->flags |= SN_FINST_H;
3384 /* We used to have a free connection slot. Since we'll never use it,
3385 * we have to inform the server that it may be used by another session.
3386 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003387 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003388 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003389 return 1;
3390 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003391 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003392
Willy Tarreaua15645d2007-03-18 16:22:39 +01003393 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01003394 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003395 if (t->srv) {
3396 t->srv->cur_sess--;
3397 t->srv->failed_secu++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003398 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003399 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003400 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003401 goto return_srv_prx_502;
3402 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003403
Willy Tarreaua15645d2007-03-18 16:22:39 +01003404 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003405 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003406 !(t->flags & SN_CONN_CLOSED)) {
3407 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003408 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003409 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003410
Willy Tarreaua15645d2007-03-18 16:22:39 +01003411 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3412 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003413
Willy Tarreaua15645d2007-03-18 16:22:39 +01003414 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3415 cur_hdr = &txn->hdr_idx.v[cur_idx];
3416 cur_ptr = cur_next;
3417 cur_end = cur_ptr + cur_hdr->len;
3418 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003419
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003420 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3421 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003422 /* 3 possibilities :
3423 * - we have already set Connection: close,
3424 * so we remove this line.
3425 * - we have not yet set Connection: close,
3426 * but this line indicates close. We leave
3427 * it untouched and set the flag.
3428 * - we have not yet set Connection: close,
3429 * and this line indicates non-close. We
3430 * replace it.
3431 */
3432 if (t->flags & SN_CONN_CLOSED) {
3433 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3434 txn->rsp.eoh += delta;
3435 cur_next += delta;
3436 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3437 txn->hdr_idx.used--;
3438 cur_hdr->len = 0;
3439 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003440 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3441 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3442 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003443 cur_next += delta;
3444 cur_hdr->len += delta;
3445 txn->rsp.eoh += delta;
3446 }
3447 t->flags |= SN_CONN_CLOSED;
3448 }
3449 }
3450 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003451 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003452 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003453
Willy Tarreaua15645d2007-03-18 16:22:39 +01003454 /* add response headers from the rule sets in the same order */
3455 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003456 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3457 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003458 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003459 }
3460
Willy Tarreaua15645d2007-03-18 16:22:39 +01003461 /* check whether we're already working on the frontend */
3462 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003463 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003464 cur_proxy = t->fe;
3465 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003466
Willy Tarreaua15645d2007-03-18 16:22:39 +01003467 /*
3468 * 4: check for server cookie.
3469 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01003470 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
3471 || (t->be->options & PR_O_CHK_CACHE))
3472 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003473
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003474
Willy Tarreaua15645d2007-03-18 16:22:39 +01003475 /*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003476 * 5: check for cache-control or pragma headers if required.
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003477 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01003478 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3479 check_response_for_cacheability(t, rep);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003480
3481 /*
3482 * 6: add server cookie in the response if needed
Willy Tarreaua15645d2007-03-18 16:22:39 +01003483 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003484 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3485 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003486 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003487
Willy Tarreaua15645d2007-03-18 16:22:39 +01003488 /* the server is known, it's not the one the client requested, we have to
3489 * insert a set-cookie here, except if we want to insert only on POST
3490 * requests and this one isn't. Note that servers which don't have cookies
3491 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003492 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003493 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003494 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003495 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003496
Krzysztof Piotr Oledzki1acf2172008-05-29 23:03:34 +02003497 if (t->be->cookie_domain)
3498 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003499
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003500 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3501 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003502 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01003503 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003504
Willy Tarreaua15645d2007-03-18 16:22:39 +01003505 /* Here, we will tell an eventual cache on the client side that we don't
3506 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3507 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3508 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3509 */
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003510 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
3511
3512 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3513
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003514 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3515 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003516 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003517 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003518 }
3519
3520
3521 /*
Willy Tarreaua15645d2007-03-18 16:22:39 +01003522 * 7: check if result will be cacheable with a cookie.
3523 * We'll block the response if security checks have caught
3524 * nasty things such as a cacheable cookie.
3525 */
Willy Tarreau3d300592007-03-18 18:34:41 +01003526 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3527 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003528 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003529
Willy Tarreaua15645d2007-03-18 16:22:39 +01003530 /* we're in presence of a cacheable response containing
3531 * a set-cookie header. We'll block it as requested by
3532 * the 'checkcache' option, and send an alert.
3533 */
3534 if (t->srv) {
3535 t->srv->cur_sess--;
3536 t->srv->failed_secu++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003537 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003538 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003539 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003540
Willy Tarreaua15645d2007-03-18 16:22:39 +01003541 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003542 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003543 send_log(t->be, LOG_ALERT,
3544 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003545 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003546 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003547 }
3548
Willy Tarreaua15645d2007-03-18 16:22:39 +01003549 /*
3550 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02003551 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003552 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02003553 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003554 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02003555 if ((unlikely(msg->sl.st.v_l != 8) ||
3556 unlikely(req->data[msg->som + 7] != '0')) &&
3557 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003558 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003559 goto return_bad_resp;
3560 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003561 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003562
Willy Tarreaubaaee002006-06-26 02:48:02 +02003563
Willy Tarreaua15645d2007-03-18 16:22:39 +01003564 /*************************************************************
3565 * OK, that's finished for the headers. We have done what we *
3566 * could. Let's switch to the DATA state. *
3567 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003568
Willy Tarreaua15645d2007-03-18 16:22:39 +01003569 t->srv_state = SV_STDATA;
Willy Tarreau718f0ef2008-08-10 16:21:32 +02003570 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003571 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003572 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003573
3574 /* client connection already closed or option 'forceclose' required :
3575 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003576 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003577 if ((req->l == 0) &&
Willy Tarreau6468d922008-08-03 19:15:35 +02003578 (req->flags & BF_SHUTR_STATUS || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003579 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003580 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003581
3582 /* We must ensure that the read part is still alive when switching
3583 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003584 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003585 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003586
Willy Tarreaua15645d2007-03-18 16:22:39 +01003587 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003588 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003589 }
3590
Willy Tarreaua15645d2007-03-18 16:22:39 +01003591#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003592 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003593 /* TCP splicing supported by both FE and BE */
3594 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003595 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003596#endif
3597 /* if the user wants to log as soon as possible, without counting
Krzysztof Piotr Oledzkif1e1cb42008-01-20 23:27:02 +01003598 * bytes from the server, then this is the right moment. We have
3599 * to temporarily assign bytes_out to log what we currently have.
3600 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003601 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3602 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreau8b3977f2008-01-18 11:16:32 +01003603 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02003604 if (t->fe->to_log & LW_REQ)
3605 http_sess_log(t);
3606 else
3607 tcp_sess_log(t);
Krzysztof Piotr Oledzkif1e1cb42008-01-20 23:27:02 +01003608 t->logs.bytes_out = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003609 }
3610
Willy Tarreaua15645d2007-03-18 16:22:39 +01003611 /* Note: we must not try to cheat by jumping directly to DATA,
3612 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003613 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003614
3615 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003616 }
3617 else if (s == SV_STDATA) {
3618 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003619 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003620 buffer_shutr_done(rep);
3621 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003622 fd_delete(t->srv_fd);
3623 if (t->srv) {
3624 t->srv->cur_sess--;
3625 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003626 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003627 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003628 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003629 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003630 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003631 if (!(t->flags & SN_ERR_MASK))
3632 t->flags |= SN_ERR_SRVCL;
3633 if (!(t->flags & SN_FINST_MASK))
3634 t->flags |= SN_FINST_D;
3635 /* We used to have a free connection slot. Since we'll never use it,
3636 * we have to inform the server that it may be used by another session.
3637 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003638 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003639 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003640
3641 return 1;
3642 }
3643 /* last read, or end of client write */
Willy Tarreau6468d922008-08-03 19:15:35 +02003644 else if (rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003645 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003646 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003647 t->srv_state = SV_STSHUTR;
3648 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3649 return 1;
3650 }
3651 /* end of client read and no more data to send */
Willy Tarreau6468d922008-08-03 19:15:35 +02003652 else if (req->flags & BF_SHUTR_STATUS && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003653 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003654 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003655 shutdown(t->srv_fd, SHUT_WR);
3656 /* We must ensure that the read part is still alive when switching
3657 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003658 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003659 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003660
3661 t->srv_state = SV_STSHUTW;
3662 return 1;
3663 }
3664 /* read timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003665 else if (tick_is_expired(rep->rex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003666 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003667 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003668 t->srv_state = SV_STSHUTR;
3669 if (!(t->flags & SN_ERR_MASK))
3670 t->flags |= SN_ERR_SRVTO;
3671 if (!(t->flags & SN_FINST_MASK))
3672 t->flags |= SN_FINST_D;
3673 return 1;
3674 }
3675 /* write timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003676 else if (tick_is_expired(req->wex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003677 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003678 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003679 shutdown(t->srv_fd, SHUT_WR);
3680 /* We must ensure that the read part is still alive when switching
3681 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003682 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003683 rep->cex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003684 t->srv_state = SV_STSHUTW;
3685 if (!(t->flags & SN_ERR_MASK))
3686 t->flags |= SN_ERR_SRVTO;
3687 if (!(t->flags & SN_FINST_MASK))
3688 t->flags |= SN_FINST_D;
3689 return 1;
3690 }
3691
3692 /* recompute request time-outs */
3693 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003694 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3695 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003696 req->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003697 }
3698 }
3699 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003700 if (!tick_isset(req->wex)) {
3701 EV_FD_COND_S(t->srv_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02003702 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003703 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003704 if (tick_isset(req->wex)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003705 /* FIXME: to prevent the server from expiring read timeouts during writes,
3706 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003707 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003708 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003709 }
3710 }
3711
3712 /* recompute response time-outs */
3713 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003714 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003715 rep->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003716 }
3717 }
3718 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003719 if (!tick_isset(rep->rex)) {
3720 EV_FD_COND_S(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003721 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003722 }
3723 }
3724
3725 return 0; /* other cases change nothing */
3726 }
3727 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003728 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003729 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003730 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003731 fd_delete(t->srv_fd);
3732 if (t->srv) {
3733 t->srv->cur_sess--;
3734 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003735 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003736 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003737 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003738 //close(t->srv_fd);
3739 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003740 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003741 if (!(t->flags & SN_ERR_MASK))
3742 t->flags |= SN_ERR_SRVCL;
3743 if (!(t->flags & SN_FINST_MASK))
3744 t->flags |= SN_FINST_D;
3745 /* We used to have a free connection slot. Since we'll never use it,
3746 * we have to inform the server that it may be used by another session.
3747 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003748 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003749 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003750
3751 return 1;
3752 }
Willy Tarreau6468d922008-08-03 19:15:35 +02003753 else if (req->flags & BF_SHUTR_STATUS && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003754 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003755 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003756 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003757 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003758 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003759 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003760 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003761 //close(t->srv_fd);
3762 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003763 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003764 /* We used to have a free connection slot. Since we'll never use it,
3765 * we have to inform the server that it may be used by another session.
3766 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003767 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003768 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003769
3770 return 1;
3771 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003772 else if (tick_is_expired(req->wex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003773 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003774 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003775 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003776 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003777 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003778 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003779 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003780 //close(t->srv_fd);
3781 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003782 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003783 if (!(t->flags & SN_ERR_MASK))
3784 t->flags |= SN_ERR_SRVTO;
3785 if (!(t->flags & SN_FINST_MASK))
3786 t->flags |= SN_FINST_D;
3787 /* We used to have a free connection slot. Since we'll never use it,
3788 * we have to inform the server that it may be used by another session.
3789 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003790 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003791 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003792
3793 return 1;
3794 }
3795 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003796 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3797 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003798 req->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003799 }
3800 }
3801 else { /* buffer not empty */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003802 if (!tick_isset(req->wex)) {
3803 EV_FD_COND_S(t->srv_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02003804 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003805 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003806 }
3807 }
3808 return 0;
3809 }
3810 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003811 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003812 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003813 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003814 fd_delete(t->srv_fd);
3815 if (t->srv) {
3816 t->srv->cur_sess--;
3817 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003818 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003819 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003820 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003821 //close(t->srv_fd);
3822 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003823 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003824 if (!(t->flags & SN_ERR_MASK))
3825 t->flags |= SN_ERR_SRVCL;
3826 if (!(t->flags & SN_FINST_MASK))
3827 t->flags |= SN_FINST_D;
3828 /* We used to have a free connection slot. Since we'll never use it,
3829 * we have to inform the server that it may be used by another session.
3830 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003831 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003832 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003833
3834 return 1;
3835 }
Willy Tarreau6468d922008-08-03 19:15:35 +02003836 else if (rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003837 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003838 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003839 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003840 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003841 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003842 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003843 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003844 //close(t->srv_fd);
3845 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003846 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003847 /* We used to have a free connection slot. Since we'll never use it,
3848 * we have to inform the server that it may be used by another session.
3849 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003850 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003851 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003852
3853 return 1;
3854 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003855 else if (tick_is_expired(rep->rex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003856 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003857 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003858 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003859 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003860 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003861 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003862 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003863 //close(t->srv_fd);
3864 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003865 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003866 if (!(t->flags & SN_ERR_MASK))
3867 t->flags |= SN_ERR_SRVTO;
3868 if (!(t->flags & SN_FINST_MASK))
3869 t->flags |= SN_FINST_D;
3870 /* We used to have a free connection slot. Since we'll never use it,
3871 * we have to inform the server that it may be used by another session.
3872 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003873 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003874 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003875
3876 return 1;
3877 }
3878 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003879 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003880 rep->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003881 }
3882 }
3883 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003884 if (!tick_isset(rep->rex)) {
3885 EV_FD_COND_S(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003886 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003887 }
3888 }
3889 return 0;
3890 }
3891 else { /* SV_STCLOSE : nothing to do */
3892 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3893 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003894 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003895 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003896 write(1, trash, len);
3897 }
3898 return 0;
3899 }
3900 return 0;
3901}
3902
3903
3904/*
3905 * Produces data for the session <s> depending on its source. Expects to be
3906 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3907 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3908 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003909 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreaubaaee002006-06-26 02:48:02 +02003910 * if it changes the session state from CL_STSHUTR, otherwise 0.
3911 */
3912int produce_content(struct session *s)
3913{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003914 if (s->data_source == DATA_SRC_NONE) {
3915 s->flags &= ~SN_SELF_GEN;
3916 return 1;
3917 }
3918 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003919 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003920 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003921 if (ret >= 0)
3922 return ret;
3923 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003924 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003925
Willy Tarreau91861262007-10-17 17:06:05 +02003926 /* unknown data source or internal error */
3927 s->txn.status = 500;
3928 client_retnclose(s, error_message(s, HTTP_ERR_500));
3929 if (!(s->flags & SN_ERR_MASK))
3930 s->flags |= SN_ERR_PRXCOND;
3931 if (!(s->flags & SN_FINST_MASK))
3932 s->flags |= SN_FINST_R;
3933 s->flags &= ~SN_SELF_GEN;
3934 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003935}
3936
3937
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003938/* Iterate the same filter through all request headers.
3939 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003940 * Since it can manage the switch to another backend, it updates the per-proxy
3941 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003942 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003943int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003944{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003945 char term;
3946 char *cur_ptr, *cur_end, *cur_next;
3947 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003948 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003949 struct hdr_idx_elem *cur_hdr;
3950 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003951
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003952 last_hdr = 0;
3953
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003954 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003955 old_idx = 0;
3956
3957 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003958 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003959 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003960 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003961 (exp->action == ACT_ALLOW ||
3962 exp->action == ACT_DENY ||
3963 exp->action == ACT_TARPIT))
3964 return 0;
3965
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003966 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003967 if (!cur_idx)
3968 break;
3969
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003970 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003971 cur_ptr = cur_next;
3972 cur_end = cur_ptr + cur_hdr->len;
3973 cur_next = cur_end + cur_hdr->cr + 1;
3974
3975 /* Now we have one header between cur_ptr and cur_end,
3976 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003977 */
3978
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003979 /* The annoying part is that pattern matching needs
3980 * that we modify the contents to null-terminate all
3981 * strings before testing them.
3982 */
3983
3984 term = *cur_end;
3985 *cur_end = '\0';
3986
3987 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3988 switch (exp->action) {
3989 case ACT_SETBE:
3990 /* It is not possible to jump a second time.
3991 * FIXME: should we return an HTTP/500 here so that
3992 * the admin knows there's a problem ?
3993 */
3994 if (t->be != t->fe)
3995 break;
3996
3997 /* Swithing Proxy */
3998 t->be = (struct proxy *) exp->replace;
3999
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004000 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004001 * because we have associated req_cap and rsp_cap to the
4002 * frontend, and the beconn will be updated later.
4003 */
4004
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004005 t->rep->rto = t->req->wto = t->be->timeout.server;
4006 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004007 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004008 last_hdr = 1;
4009 break;
4010
4011 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004012 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004013 last_hdr = 1;
4014 break;
4015
4016 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004017 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004018 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004019 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004020 break;
4021
4022 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004023 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004024 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004025 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004026 break;
4027
4028 case ACT_REPLACE:
4029 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4030 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4031 /* FIXME: if the user adds a newline in the replacement, the
4032 * index will not be recalculated for now, and the new line
4033 * will not be counted as a new header.
4034 */
4035
4036 cur_end += delta;
4037 cur_next += delta;
4038 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004039 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004040 break;
4041
4042 case ACT_REMOVE:
4043 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4044 cur_next += delta;
4045
4046 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004047 txn->req.eoh += delta;
4048 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4049 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004050 cur_hdr->len = 0;
4051 cur_end = NULL; /* null-term has been rewritten */
4052 break;
4053
4054 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004055 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004056 if (cur_end)
4057 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004058
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004059 /* keep the link from this header to next one in case of later
4060 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004061 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004062 old_idx = cur_idx;
4063 }
4064 return 0;
4065}
4066
4067
4068/* Apply the filter to the request line.
4069 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4070 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004071 * Since it can manage the switch to another backend, it updates the per-proxy
4072 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004073 */
4074int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4075{
4076 char term;
4077 char *cur_ptr, *cur_end;
4078 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004079 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004080 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004081
Willy Tarreau58f10d72006-12-04 02:26:12 +01004082
Willy Tarreau3d300592007-03-18 18:34:41 +01004083 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004084 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004085 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004086 (exp->action == ACT_ALLOW ||
4087 exp->action == ACT_DENY ||
4088 exp->action == ACT_TARPIT))
4089 return 0;
4090 else if (exp->action == ACT_REMOVE)
4091 return 0;
4092
4093 done = 0;
4094
Willy Tarreau9cdde232007-05-02 20:58:19 +02004095 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004096 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004097
4098 /* Now we have the request line between cur_ptr and cur_end */
4099
4100 /* The annoying part is that pattern matching needs
4101 * that we modify the contents to null-terminate all
4102 * strings before testing them.
4103 */
4104
4105 term = *cur_end;
4106 *cur_end = '\0';
4107
4108 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4109 switch (exp->action) {
4110 case ACT_SETBE:
4111 /* It is not possible to jump a second time.
4112 * FIXME: should we return an HTTP/500 here so that
4113 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004114 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004115 if (t->be != t->fe)
4116 break;
4117
4118 /* Swithing Proxy */
4119 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004120
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004121 /* right now, the backend switch is not too much complicated
4122 * because we have associated req_cap and rsp_cap to the
4123 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004124 */
4125
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004126 t->rep->rto = t->req->wto = t->be->timeout.server;
4127 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004128 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004129 done = 1;
4130 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004132 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004133 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004134 done = 1;
4135 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004136
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004137 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004138 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004139 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004140 done = 1;
4141 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004142
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004143 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004144 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004145 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004146 done = 1;
4147 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004148
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004149 case ACT_REPLACE:
4150 *cur_end = term; /* restore the string terminator */
4151 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4152 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4153 /* FIXME: if the user adds a newline in the replacement, the
4154 * index will not be recalculated for now, and the new line
4155 * will not be counted as a new header.
4156 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004157
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004158 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004159 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004160
Willy Tarreau9cdde232007-05-02 20:58:19 +02004161 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004162 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004163 HTTP_MSG_RQMETH,
4164 cur_ptr, cur_end + 1,
4165 NULL, NULL);
4166 if (unlikely(!cur_end))
4167 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004168
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004169 /* we have a full request and we know that we have either a CR
4170 * or an LF at <ptr>.
4171 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004172 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4173 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004174 /* there is no point trying this regex on headers */
4175 return 1;
4176 }
4177 }
4178 *cur_end = term; /* restore the string terminator */
4179 return done;
4180}
Willy Tarreau97de6242006-12-27 17:18:38 +01004181
Willy Tarreau58f10d72006-12-04 02:26:12 +01004182
Willy Tarreau58f10d72006-12-04 02:26:12 +01004183
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004184/*
4185 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4186 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004187 * unparsable request. Since it can manage the switch to another backend, it
4188 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004189 */
4190int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4191{
Willy Tarreau3d300592007-03-18 18:34:41 +01004192 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004193 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004194 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004195 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004196
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004197 /*
4198 * The interleaving of transformations and verdicts
4199 * makes it difficult to decide to continue or stop
4200 * the evaluation.
4201 */
4202
Willy Tarreau3d300592007-03-18 18:34:41 +01004203 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004204 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4205 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4206 exp = exp->next;
4207 continue;
4208 }
4209
4210 /* Apply the filter to the request line. */
4211 ret = apply_filter_to_req_line(t, req, exp);
4212 if (unlikely(ret < 0))
4213 return -1;
4214
4215 if (likely(ret == 0)) {
4216 /* The filter did not match the request, it can be
4217 * iterated through all headers.
4218 */
4219 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004220 }
4221 exp = exp->next;
4222 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004223 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004224}
4225
4226
Willy Tarreaua15645d2007-03-18 16:22:39 +01004227
Willy Tarreau58f10d72006-12-04 02:26:12 +01004228/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004229 * Manage client-side cookie. It can impact performance by about 2% so it is
4230 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004231 */
4232void manage_client_side_cookies(struct session *t, struct buffer *req)
4233{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004234 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004235 char *p1, *p2, *p3, *p4;
4236 char *del_colon, *del_cookie, *colon;
4237 int app_cookies;
4238
4239 appsess *asession_temp = NULL;
4240 appsess local_asession;
4241
4242 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004243 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004244
Willy Tarreau2a324282006-12-05 00:05:46 +01004245 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004246 * we start with the start line.
4247 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004248 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004249 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004250
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004251 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004252 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004253 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004254
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004255 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004256 cur_ptr = cur_next;
4257 cur_end = cur_ptr + cur_hdr->len;
4258 cur_next = cur_end + cur_hdr->cr + 1;
4259
4260 /* We have one full header between cur_ptr and cur_end, and the
4261 * next header starts at cur_next. We're only interested in
4262 * "Cookie:" headers.
4263 */
4264
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004265 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4266 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004267 old_idx = cur_idx;
4268 continue;
4269 }
4270
4271 /* Now look for cookies. Conforming to RFC2109, we have to support
4272 * attributes whose name begin with a '$', and associate them with
4273 * the right cookie, if we want to delete this cookie.
4274 * So there are 3 cases for each cookie read :
4275 * 1) it's a special attribute, beginning with a '$' : ignore it.
4276 * 2) it's a server id cookie that we *MAY* want to delete : save
4277 * some pointers on it (last semi-colon, beginning of cookie...)
4278 * 3) it's an application cookie : we *MAY* have to delete a previous
4279 * "special" cookie.
4280 * At the end of loop, if a "special" cookie remains, we may have to
4281 * remove it. If no application cookie persists in the header, we
4282 * *MUST* delete it
4283 */
4284
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004285 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004286
Willy Tarreau58f10d72006-12-04 02:26:12 +01004287 /* del_cookie == NULL => nothing to be deleted */
4288 del_colon = del_cookie = NULL;
4289 app_cookies = 0;
4290
4291 while (p1 < cur_end) {
4292 /* skip spaces and colons, but keep an eye on these ones */
4293 while (p1 < cur_end) {
4294 if (*p1 == ';' || *p1 == ',')
4295 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004296 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004297 break;
4298 p1++;
4299 }
4300
4301 if (p1 == cur_end)
4302 break;
4303
4304 /* p1 is at the beginning of the cookie name */
4305 p2 = p1;
4306 while (p2 < cur_end && *p2 != '=')
4307 p2++;
4308
4309 if (p2 == cur_end)
4310 break;
4311
4312 p3 = p2 + 1; /* skips the '=' sign */
4313 if (p3 == cur_end)
4314 break;
4315
4316 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004317 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004318 p4++;
4319
4320 /* here, we have the cookie name between p1 and p2,
4321 * and its value between p3 and p4.
4322 * we can process it :
4323 *
4324 * Cookie: NAME=VALUE;
4325 * | || || |
4326 * | || || +--> p4
4327 * | || |+-------> p3
4328 * | || +--------> p2
4329 * | |+------------> p1
4330 * | +-------------> colon
4331 * +--------------------> cur_ptr
4332 */
4333
4334 if (*p1 == '$') {
4335 /* skip this one */
4336 }
4337 else {
4338 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004339 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004340 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004341 (p4 - p1 >= t->fe->capture_namelen) &&
4342 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004343 int log_len = p4 - p1;
4344
Willy Tarreau086b3b42007-05-13 21:45:51 +02004345 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004346 Alert("HTTP logging : out of memory.\n");
4347 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004348 if (log_len > t->fe->capture_len)
4349 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004350 memcpy(txn->cli_cookie, p1, log_len);
4351 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004352 }
4353 }
4354
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004355 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4356 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004357 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004358 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004359 char *delim;
4360
4361 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4362 * have the server ID betweek p3 and delim, and the original cookie between
4363 * delim+1 and p4. Otherwise, delim==p4 :
4364 *
4365 * Cookie: NAME=SRV~VALUE;
4366 * | || || | |
4367 * | || || | +--> p4
4368 * | || || +--------> delim
4369 * | || |+-----------> p3
4370 * | || +------------> p2
4371 * | |+----------------> p1
4372 * | +-----------------> colon
4373 * +------------------------> cur_ptr
4374 */
4375
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004376 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004377 for (delim = p3; delim < p4; delim++)
4378 if (*delim == COOKIE_DELIM)
4379 break;
4380 }
4381 else
4382 delim = p4;
4383
4384
4385 /* Here, we'll look for the first running server which supports the cookie.
4386 * This allows to share a same cookie between several servers, for example
4387 * to dedicate backup servers to specific servers only.
4388 * However, to prevent clients from sticking to cookie-less backup server
4389 * when they have incidentely learned an empty cookie, we simply ignore
4390 * empty cookies and mark them as invalid.
4391 */
4392 if (delim == p3)
4393 srv = NULL;
4394
4395 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004396 if (srv->cookie && (srv->cklen == delim - p3) &&
4397 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004398 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004399 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004400 txn->flags &= ~TX_CK_MASK;
4401 txn->flags |= TX_CK_VALID;
4402 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004403 t->srv = srv;
4404 break;
4405 } else {
4406 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004407 txn->flags &= ~TX_CK_MASK;
4408 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004409 }
4410 }
4411 srv = srv->next;
4412 }
4413
Willy Tarreau3d300592007-03-18 18:34:41 +01004414 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004415 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004416 txn->flags &= ~TX_CK_MASK;
4417 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004418 }
4419
4420 /* depending on the cookie mode, we may have to either :
4421 * - delete the complete cookie if we're in insert+indirect mode, so that
4422 * the server never sees it ;
4423 * - remove the server id from the cookie value, and tag the cookie as an
4424 * application cookie so that it does not get accidentely removed later,
4425 * if we're in cookie prefix mode
4426 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004427 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004428 int delta; /* negative */
4429
4430 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4431 p4 += delta;
4432 cur_end += delta;
4433 cur_next += delta;
4434 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004435 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004436
4437 del_cookie = del_colon = NULL;
4438 app_cookies++; /* protect the header from deletion */
4439 }
4440 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004441 (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 +01004442 del_cookie = p1;
4443 del_colon = colon;
4444 }
4445 } else {
4446 /* now we know that we must keep this cookie since it's
4447 * not ours. But if we wanted to delete our cookie
4448 * earlier, we cannot remove the complete header, but we
4449 * can remove the previous block itself.
4450 */
4451 app_cookies++;
4452
4453 if (del_cookie != NULL) {
4454 int delta; /* negative */
4455
4456 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4457 p4 += delta;
4458 cur_end += delta;
4459 cur_next += delta;
4460 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004461 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004462 del_cookie = del_colon = NULL;
4463 }
4464 }
4465
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004466 if ((t->be->appsession_name != NULL) &&
4467 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004468 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004469
Willy Tarreau58f10d72006-12-04 02:26:12 +01004470 /* Cool... it's the right one */
4471
4472 asession_temp = &local_asession;
4473
Willy Tarreau63963c62007-05-13 21:29:55 +02004474 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004475 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4476 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4477 return;
4478 }
4479
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004480 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4481 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004482 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004483
Willy Tarreau58f10d72006-12-04 02:26:12 +01004484 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004485 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4486 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004487 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004488 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004489 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004490 Alert("Not enough memory process_cli():asession:calloc().\n");
4491 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4492 return;
4493 }
4494
4495 asession_temp->sessid = local_asession.sessid;
4496 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004497 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004498 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004499 } else {
4500 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004501 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004502 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004503 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004504 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004505 Alert("Found Application Session without matching server.\n");
4506 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004507 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004508 while (srv) {
4509 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004510 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004511 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004512 txn->flags &= ~TX_CK_MASK;
4513 txn->flags |= TX_CK_VALID;
4514 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004515 t->srv = srv;
4516 break;
4517 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004518 txn->flags &= ~TX_CK_MASK;
4519 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004520 }
4521 }
4522 srv = srv->next;
4523 }/* end while(srv) */
4524 }/* end else if server == NULL */
4525
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004526 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004527 asession_temp->request_count++;
4528#if defined(DEBUG_HASH)
4529 Alert("manage_client_side_cookies\n");
4530 appsession_hash_dump(&(t->be->htbl_proxy));
4531#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004532 }/* end if ((t->proxy->appsession_name != NULL) ... */
4533 }
4534
4535 /* we'll have to look for another cookie ... */
4536 p1 = p4;
4537 } /* while (p1 < cur_end) */
4538
4539 /* There's no more cookie on this line.
4540 * We may have marked the last one(s) for deletion.
4541 * We must do this now in two ways :
4542 * - if there is no app cookie, we simply delete the header ;
4543 * - if there are app cookies, we must delete the end of the
4544 * string properly, including the colon/semi-colon before
4545 * the cookie name.
4546 */
4547 if (del_cookie != NULL) {
4548 int delta;
4549 if (app_cookies) {
4550 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4551 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004552 cur_hdr->len += delta;
4553 } else {
4554 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004555
4556 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004557 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4558 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004559 cur_hdr->len = 0;
4560 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004561 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004562 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004563 }
4564
4565 /* keep the link from this header to next one */
4566 old_idx = cur_idx;
4567 } /* end of cookie processing on this header */
4568}
4569
4570
Willy Tarreaua15645d2007-03-18 16:22:39 +01004571/* Iterate the same filter through all response headers contained in <rtr>.
4572 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4573 */
4574int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4575{
4576 char term;
4577 char *cur_ptr, *cur_end, *cur_next;
4578 int cur_idx, old_idx, last_hdr;
4579 struct http_txn *txn = &t->txn;
4580 struct hdr_idx_elem *cur_hdr;
4581 int len, delta;
4582
4583 last_hdr = 0;
4584
4585 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4586 old_idx = 0;
4587
4588 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004589 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004590 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004591 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004592 (exp->action == ACT_ALLOW ||
4593 exp->action == ACT_DENY))
4594 return 0;
4595
4596 cur_idx = txn->hdr_idx.v[old_idx].next;
4597 if (!cur_idx)
4598 break;
4599
4600 cur_hdr = &txn->hdr_idx.v[cur_idx];
4601 cur_ptr = cur_next;
4602 cur_end = cur_ptr + cur_hdr->len;
4603 cur_next = cur_end + cur_hdr->cr + 1;
4604
4605 /* Now we have one header between cur_ptr and cur_end,
4606 * and the next header starts at cur_next.
4607 */
4608
4609 /* The annoying part is that pattern matching needs
4610 * that we modify the contents to null-terminate all
4611 * strings before testing them.
4612 */
4613
4614 term = *cur_end;
4615 *cur_end = '\0';
4616
4617 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4618 switch (exp->action) {
4619 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004620 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004621 last_hdr = 1;
4622 break;
4623
4624 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004625 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004626 last_hdr = 1;
4627 break;
4628
4629 case ACT_REPLACE:
4630 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4631 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4632 /* FIXME: if the user adds a newline in the replacement, the
4633 * index will not be recalculated for now, and the new line
4634 * will not be counted as a new header.
4635 */
4636
4637 cur_end += delta;
4638 cur_next += delta;
4639 cur_hdr->len += delta;
4640 txn->rsp.eoh += delta;
4641 break;
4642
4643 case ACT_REMOVE:
4644 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4645 cur_next += delta;
4646
4647 /* FIXME: this should be a separate function */
4648 txn->rsp.eoh += delta;
4649 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4650 txn->hdr_idx.used--;
4651 cur_hdr->len = 0;
4652 cur_end = NULL; /* null-term has been rewritten */
4653 break;
4654
4655 }
4656 }
4657 if (cur_end)
4658 *cur_end = term; /* restore the string terminator */
4659
4660 /* keep the link from this header to next one in case of later
4661 * removal of next header.
4662 */
4663 old_idx = cur_idx;
4664 }
4665 return 0;
4666}
4667
4668
4669/* Apply the filter to the status line in the response buffer <rtr>.
4670 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4671 * or -1 if a replacement resulted in an invalid status line.
4672 */
4673int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4674{
4675 char term;
4676 char *cur_ptr, *cur_end;
4677 int done;
4678 struct http_txn *txn = &t->txn;
4679 int len, delta;
4680
4681
Willy Tarreau3d300592007-03-18 18:34:41 +01004682 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004683 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004684 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004685 (exp->action == ACT_ALLOW ||
4686 exp->action == ACT_DENY))
4687 return 0;
4688 else if (exp->action == ACT_REMOVE)
4689 return 0;
4690
4691 done = 0;
4692
Willy Tarreau9cdde232007-05-02 20:58:19 +02004693 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004694 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4695
4696 /* Now we have the status line between cur_ptr and cur_end */
4697
4698 /* The annoying part is that pattern matching needs
4699 * that we modify the contents to null-terminate all
4700 * strings before testing them.
4701 */
4702
4703 term = *cur_end;
4704 *cur_end = '\0';
4705
4706 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4707 switch (exp->action) {
4708 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004709 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004710 done = 1;
4711 break;
4712
4713 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004714 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004715 done = 1;
4716 break;
4717
4718 case ACT_REPLACE:
4719 *cur_end = term; /* restore the string terminator */
4720 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4721 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4722 /* FIXME: if the user adds a newline in the replacement, the
4723 * index will not be recalculated for now, and the new line
4724 * will not be counted as a new header.
4725 */
4726
4727 txn->rsp.eoh += delta;
4728 cur_end += delta;
4729
Willy Tarreau9cdde232007-05-02 20:58:19 +02004730 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004731 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004732 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004733 cur_ptr, cur_end + 1,
4734 NULL, NULL);
4735 if (unlikely(!cur_end))
4736 return -1;
4737
4738 /* we have a full respnse and we know that we have either a CR
4739 * or an LF at <ptr>.
4740 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004741 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004742 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4743 /* there is no point trying this regex on headers */
4744 return 1;
4745 }
4746 }
4747 *cur_end = term; /* restore the string terminator */
4748 return done;
4749}
4750
4751
4752
4753/*
4754 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4755 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4756 * unparsable response.
4757 */
4758int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4759{
Willy Tarreau3d300592007-03-18 18:34:41 +01004760 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004761 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004762 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004763 int ret;
4764
4765 /*
4766 * The interleaving of transformations and verdicts
4767 * makes it difficult to decide to continue or stop
4768 * the evaluation.
4769 */
4770
Willy Tarreau3d300592007-03-18 18:34:41 +01004771 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004772 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4773 exp->action == ACT_PASS)) {
4774 exp = exp->next;
4775 continue;
4776 }
4777
4778 /* Apply the filter to the status line. */
4779 ret = apply_filter_to_sts_line(t, rtr, exp);
4780 if (unlikely(ret < 0))
4781 return -1;
4782
4783 if (likely(ret == 0)) {
4784 /* The filter did not match the response, it can be
4785 * iterated through all headers.
4786 */
4787 apply_filter_to_resp_headers(t, rtr, exp);
4788 }
4789 exp = exp->next;
4790 }
4791 return 0;
4792}
4793
4794
4795
4796/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004797 * Manage server-side cookies. It can impact performance by about 2% so it is
4798 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004799 */
4800void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4801{
4802 struct http_txn *txn = &t->txn;
4803 char *p1, *p2, *p3, *p4;
4804
4805 appsess *asession_temp = NULL;
4806 appsess local_asession;
4807
4808 char *cur_ptr, *cur_end, *cur_next;
4809 int cur_idx, old_idx, delta;
4810
Willy Tarreaua15645d2007-03-18 16:22:39 +01004811 /* Iterate through the headers.
4812 * we start with the start line.
4813 */
4814 old_idx = 0;
4815 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4816
4817 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4818 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004819 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004820
4821 cur_hdr = &txn->hdr_idx.v[cur_idx];
4822 cur_ptr = cur_next;
4823 cur_end = cur_ptr + cur_hdr->len;
4824 cur_next = cur_end + cur_hdr->cr + 1;
4825
4826 /* We have one full header between cur_ptr and cur_end, and the
4827 * next header starts at cur_next. We're only interested in
4828 * "Cookie:" headers.
4829 */
4830
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004831 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4832 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004833 old_idx = cur_idx;
4834 continue;
4835 }
4836
4837 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004838 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004839
4840
4841 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004842 if (t->be->cookie_name == NULL &&
4843 t->be->appsession_name == NULL &&
4844 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004845 return;
4846
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004847 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004848
4849 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004850 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4851 break;
4852
4853 /* p1 is at the beginning of the cookie name */
4854 p2 = p1;
4855
4856 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4857 p2++;
4858
4859 if (p2 == cur_end || *p2 == ';') /* next cookie */
4860 break;
4861
4862 p3 = p2 + 1; /* skip the '=' sign */
4863 if (p3 == cur_end)
4864 break;
4865
4866 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004867 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004868 p4++;
4869
4870 /* here, we have the cookie name between p1 and p2,
4871 * and its value between p3 and p4.
4872 * we can process it.
4873 */
4874
4875 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004876 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004877 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004878 (p4 - p1 >= t->be->capture_namelen) &&
4879 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004880 int log_len = p4 - p1;
4881
Willy Tarreau086b3b42007-05-13 21:45:51 +02004882 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004883 Alert("HTTP logging : out of memory.\n");
4884 }
4885
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004886 if (log_len > t->be->capture_len)
4887 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004888 memcpy(txn->srv_cookie, p1, log_len);
4889 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004890 }
4891
4892 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004893 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4894 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004895 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004896 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004897
4898 /* If the cookie is in insert mode on a known server, we'll delete
4899 * this occurrence because we'll insert another one later.
4900 * We'll delete it too if the "indirect" option is set and we're in
4901 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004902 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4903 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004904 /* this header must be deleted */
4905 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4906 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4907 txn->hdr_idx.used--;
4908 cur_hdr->len = 0;
4909 cur_next += delta;
4910 txn->rsp.eoh += delta;
4911
Willy Tarreau3d300592007-03-18 18:34:41 +01004912 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004913 }
4914 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004915 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004916 /* replace bytes p3->p4 with the cookie name associated
4917 * with this server since we know it.
4918 */
4919 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4920 cur_hdr->len += delta;
4921 cur_next += delta;
4922 txn->rsp.eoh += delta;
4923
Willy Tarreau3d300592007-03-18 18:34:41 +01004924 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004925 }
4926 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004927 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004928 /* insert the cookie name associated with this server
4929 * before existing cookie, and insert a delimitor between them..
4930 */
4931 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4932 cur_hdr->len += delta;
4933 cur_next += delta;
4934 txn->rsp.eoh += delta;
4935
4936 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004937 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004938 }
4939 }
4940 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004941 else if ((t->be->appsession_name != NULL) &&
4942 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004943
4944 /* Cool... it's the right one */
4945
4946 size_t server_id_len = strlen(t->srv->id) + 1;
4947 asession_temp = &local_asession;
4948
Willy Tarreau63963c62007-05-13 21:29:55 +02004949 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004950 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4951 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4952 return;
4953 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004954 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4955 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004956 asession_temp->serverid = NULL;
4957
4958 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004959 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4960 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004961 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004962 Alert("Not enough Memory process_srv():asession:calloc().\n");
4963 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4964 return;
4965 }
4966 asession_temp->sessid = local_asession.sessid;
4967 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004968 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004969 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4970 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004971 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004972 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004973 }
4974
Willy Tarreaua15645d2007-03-18 16:22:39 +01004975 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004976 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004977 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4978 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4979 return;
4980 }
4981 asession_temp->serverid[0] = '\0';
4982 }
4983
4984 if (asession_temp->serverid[0] == '\0')
4985 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4986
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004987 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004988 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004989#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004990 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004991 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004992#endif
4993 }/* end if ((t->proxy->appsession_name != NULL) ... */
4994 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4995 } /* we're now at the end of the cookie value */
4996
4997 /* keep the link from this header to next one */
4998 old_idx = cur_idx;
4999 } /* end of cookie processing on this header */
5000}
5001
5002
5003
5004/*
5005 * Check if response is cacheable or not. Updates t->flags.
5006 */
5007void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5008{
5009 struct http_txn *txn = &t->txn;
5010 char *p1, *p2;
5011
5012 char *cur_ptr, *cur_end, *cur_next;
5013 int cur_idx;
5014
Willy Tarreau5df51872007-11-25 16:20:08 +01005015 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005016 return;
5017
5018 /* Iterate through the headers.
5019 * we start with the start line.
5020 */
5021 cur_idx = 0;
5022 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5023
5024 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5025 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005026 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005027
5028 cur_hdr = &txn->hdr_idx.v[cur_idx];
5029 cur_ptr = cur_next;
5030 cur_end = cur_ptr + cur_hdr->len;
5031 cur_next = cur_end + cur_hdr->cr + 1;
5032
5033 /* We have one full header between cur_ptr and cur_end, and the
5034 * next header starts at cur_next. We're only interested in
5035 * "Cookie:" headers.
5036 */
5037
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005038 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5039 if (val) {
5040 if ((cur_end - (cur_ptr + val) >= 8) &&
5041 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5042 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5043 return;
5044 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005045 }
5046
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005047 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5048 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005049 continue;
5050
5051 /* OK, right now we know we have a cache-control header at cur_ptr */
5052
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005053 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005054
5055 if (p1 >= cur_end) /* no more info */
5056 continue;
5057
5058 /* p1 is at the beginning of the value */
5059 p2 = p1;
5060
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005061 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005062 p2++;
5063
5064 /* we have a complete value between p1 and p2 */
5065 if (p2 < cur_end && *p2 == '=') {
5066 /* we have something of the form no-cache="set-cookie" */
5067 if ((cur_end - p1 >= 21) &&
5068 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5069 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005070 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005071 continue;
5072 }
5073
5074 /* OK, so we know that either p2 points to the end of string or to a comma */
5075 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5076 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5077 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5078 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005079 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005080 return;
5081 }
5082
5083 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005084 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005085 continue;
5086 }
5087 }
5088}
5089
5090
Willy Tarreau58f10d72006-12-04 02:26:12 +01005091/*
5092 * Try to retrieve a known appsession in the URI, then the associated server.
5093 * If the server is found, it's assigned to the session.
5094 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005095void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005096{
Willy Tarreau3d300592007-03-18 18:34:41 +01005097 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005098 appsess *asession_temp = NULL;
5099 appsess local_asession;
5100 char *request_line;
5101
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005102 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005103 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005104 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005105 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005106 return;
5107
5108 /* skip ';' */
5109 request_line++;
5110
5111 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005112 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005113 return;
5114
5115 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005116 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005117
5118 /* First try if we already have an appsession */
5119 asession_temp = &local_asession;
5120
Willy Tarreau63963c62007-05-13 21:29:55 +02005121 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005122 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5123 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5124 return;
5125 }
5126
5127 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005128 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5129 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005130 asession_temp->serverid = NULL;
5131
5132 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005133 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5134 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005135 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005136 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005137 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005138 Alert("Not enough memory process_cli():asession:calloc().\n");
5139 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5140 return;
5141 }
5142 asession_temp->sessid = local_asession.sessid;
5143 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005144 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005145 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005146 }
5147 else {
5148 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005149 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005150 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005151
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005152 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005153 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005154
Willy Tarreau58f10d72006-12-04 02:26:12 +01005155#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005156 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005157 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005158#endif
5159 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005160 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005161 Alert("Found Application Session without matching server.\n");
5162 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005163 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005164 while (srv) {
5165 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005166 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005167 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005168 txn->flags &= ~TX_CK_MASK;
5169 txn->flags |= TX_CK_VALID;
5170 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005171 t->srv = srv;
5172 break;
5173 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005174 txn->flags &= ~TX_CK_MASK;
5175 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005176 }
5177 }
5178 srv = srv->next;
5179 }
5180 }
5181}
5182
5183
Willy Tarreaub2513902006-12-17 14:52:38 +01005184/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005185 * In a GET or HEAD request, check if the requested URI matches the stats uri
5186 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005187 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005188 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005189 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005190 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005191 *
5192 * Returns 1 if the session's state changes, otherwise 0.
5193 */
5194int stats_check_uri_auth(struct session *t, struct proxy *backend)
5195{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005196 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005197 struct uri_auth *uri_auth = backend->uri_auth;
5198 struct user_auth *user;
5199 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005200 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005201
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005202 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5203
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005204 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005205 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005206 return 0;
5207
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005208 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005209
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005210 /* the URI is in h */
5211 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005212 return 0;
5213
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005214 h += uri_auth->uri_len;
5215 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5216 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005217 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005218 break;
5219 }
5220 h++;
5221 }
5222
5223 if (uri_auth->refresh) {
5224 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5225 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5226 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005227 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005228 break;
5229 }
5230 h++;
5231 }
5232 }
5233
Willy Tarreau55bb8452007-10-17 18:44:57 +02005234 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5235 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5236 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005237 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005238 break;
5239 }
5240 h++;
5241 }
5242
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005243 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5244
Willy Tarreaub2513902006-12-17 14:52:38 +01005245 /* we are in front of a interceptable URI. Let's check
5246 * if there's an authentication and if it's valid.
5247 */
5248 user = uri_auth->users;
5249 if (!user) {
5250 /* no user auth required, it's OK */
5251 authenticated = 1;
5252 } else {
5253 authenticated = 0;
5254
5255 /* a user list is defined, we have to check.
5256 * skip 21 chars for "Authorization: Basic ".
5257 */
5258
5259 /* FIXME: this should move to an earlier place */
5260 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005261 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5262 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5263 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005264 if (len > 14 &&
5265 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005266 txn->auth_hdr.str = h;
5267 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005268 break;
5269 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005270 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005271 }
5272
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005273 if (txn->auth_hdr.len < 21 ||
5274 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005275 user = NULL;
5276
5277 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005278 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5279 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005280 user->user_pwd, user->user_len)) {
5281 authenticated = 1;
5282 break;
5283 }
5284 user = user->next;
5285 }
5286 }
5287
5288 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005289 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005290
5291 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005292 msg.str = trash;
5293 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005294 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005295 client_retnclose(t, &msg);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005296 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub2513902006-12-17 14:52:38 +01005297 if (!(t->flags & SN_ERR_MASK))
5298 t->flags |= SN_ERR_PRXCOND;
5299 if (!(t->flags & SN_FINST_MASK))
5300 t->flags |= SN_FINST_R;
5301 return 1;
5302 }
5303
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005304 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005305 * data.
5306 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02005307 EV_FD_CLR(t->cli_fd, DIR_RD);
5308 buffer_shutr(t->req);
5309 buffer_shutr(t->rep);
Willy Tarreaub2513902006-12-17 14:52:38 +01005310 t->cli_state = CL_STSHUTR;
5311 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02005312 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005313 t->data_source = DATA_SRC_STATS;
5314 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005315 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005316 produce_content(t);
5317 return 1;
5318}
5319
5320
Willy Tarreaubaaee002006-06-26 02:48:02 +02005321/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005322 * Print a debug line with a header
5323 */
5324void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5325{
5326 int len, max;
5327 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5328 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5329 max = end - start;
5330 UBOUND(max, sizeof(trash) - len - 1);
5331 len += strlcpy2(trash + len, start, max + 1);
5332 trash[len++] = '\n';
5333 write(1, trash, len);
5334}
5335
5336
Willy Tarreau8797c062007-05-07 00:55:35 +02005337/************************************************************************/
5338/* The code below is dedicated to ACL parsing and matching */
5339/************************************************************************/
5340
5341
5342
5343
5344/* 1. Check on METHOD
5345 * We use the pre-parsed method if it is known, and store its number as an
5346 * integer. If it is unknown, we use the pointer and the length.
5347 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005348static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005349{
5350 int len, meth;
5351
Willy Tarreauae8b7962007-06-09 23:10:04 +02005352 len = strlen(*text);
5353 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005354
5355 pattern->val.i = meth;
5356 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005357 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005358 if (!pattern->ptr.str)
5359 return 0;
5360 pattern->len = len;
5361 }
5362 return 1;
5363}
5364
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005365static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005366acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5367 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005368{
5369 int meth;
5370 struct http_txn *txn = l7;
5371
Willy Tarreaub6866442008-07-14 23:54:42 +02005372 if (!txn)
5373 return 0;
5374
Willy Tarreauc11416f2007-06-17 16:58:38 +02005375 if (txn->req.msg_state != HTTP_MSG_BODY)
5376 return 0;
5377
Willy Tarreau8797c062007-05-07 00:55:35 +02005378 meth = txn->meth;
5379 test->i = meth;
5380 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005381 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5382 /* ensure the indexes are not affected */
5383 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005384 test->len = txn->req.sl.rq.m_l;
5385 test->ptr = txn->req.sol;
5386 }
5387 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5388 return 1;
5389}
5390
5391static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5392{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005393 int icase;
5394
Willy Tarreau8797c062007-05-07 00:55:35 +02005395 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005396 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005397
5398 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005399 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005400
5401 /* Other method, we must compare the strings */
5402 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005403 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005404
5405 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5406 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5407 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005408 return ACL_PAT_FAIL;
5409 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005410}
5411
5412/* 2. Check on Request/Status Version
5413 * We simply compare strings here.
5414 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005415static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005416{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005417 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005418 if (!pattern->ptr.str)
5419 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005420 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005421 return 1;
5422}
5423
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005424static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005425acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5426 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005427{
5428 struct http_txn *txn = l7;
5429 char *ptr;
5430 int len;
5431
Willy Tarreaub6866442008-07-14 23:54:42 +02005432 if (!txn)
5433 return 0;
5434
Willy Tarreauc11416f2007-06-17 16:58:38 +02005435 if (txn->req.msg_state != HTTP_MSG_BODY)
5436 return 0;
5437
Willy Tarreau8797c062007-05-07 00:55:35 +02005438 len = txn->req.sl.rq.v_l;
5439 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5440
5441 while ((len-- > 0) && (*ptr++ != '/'));
5442 if (len <= 0)
5443 return 0;
5444
5445 test->ptr = ptr;
5446 test->len = len;
5447
5448 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5449 return 1;
5450}
5451
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005452static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005453acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5454 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005455{
5456 struct http_txn *txn = l7;
5457 char *ptr;
5458 int len;
5459
Willy Tarreaub6866442008-07-14 23:54:42 +02005460 if (!txn)
5461 return 0;
5462
Willy Tarreauc11416f2007-06-17 16:58:38 +02005463 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5464 return 0;
5465
Willy Tarreau8797c062007-05-07 00:55:35 +02005466 len = txn->rsp.sl.st.v_l;
5467 ptr = txn->rsp.sol;
5468
5469 while ((len-- > 0) && (*ptr++ != '/'));
5470 if (len <= 0)
5471 return 0;
5472
5473 test->ptr = ptr;
5474 test->len = len;
5475
5476 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5477 return 1;
5478}
5479
5480/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005481static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005482acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5483 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005484{
5485 struct http_txn *txn = l7;
5486 char *ptr;
5487 int len;
5488
Willy Tarreaub6866442008-07-14 23:54:42 +02005489 if (!txn)
5490 return 0;
5491
Willy Tarreauc11416f2007-06-17 16:58:38 +02005492 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5493 return 0;
5494
Willy Tarreau8797c062007-05-07 00:55:35 +02005495 len = txn->rsp.sl.st.c_l;
5496 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5497
5498 test->i = __strl2ui(ptr, len);
5499 test->flags = ACL_TEST_F_VOL_1ST;
5500 return 1;
5501}
5502
5503/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005504static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005505acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5506 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005507{
5508 struct http_txn *txn = l7;
5509
Willy Tarreaub6866442008-07-14 23:54:42 +02005510 if (!txn)
5511 return 0;
5512
Willy Tarreauc11416f2007-06-17 16:58:38 +02005513 if (txn->req.msg_state != HTTP_MSG_BODY)
5514 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005515
Willy Tarreauc11416f2007-06-17 16:58:38 +02005516 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5517 /* ensure the indexes are not affected */
5518 return 0;
5519
Willy Tarreau8797c062007-05-07 00:55:35 +02005520 test->len = txn->req.sl.rq.u_l;
5521 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5522
Willy Tarreauf3d25982007-05-08 22:45:09 +02005523 /* we do not need to set READ_ONLY because the data is in a buffer */
5524 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005525 return 1;
5526}
5527
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005528static int
5529acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5530 struct acl_expr *expr, struct acl_test *test)
5531{
5532 struct http_txn *txn = l7;
5533
Willy Tarreaub6866442008-07-14 23:54:42 +02005534 if (!txn)
5535 return 0;
5536
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005537 if (txn->req.msg_state != HTTP_MSG_BODY)
5538 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005539
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005540 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5541 /* ensure the indexes are not affected */
5542 return 0;
5543
5544 /* Parse HTTP request */
5545 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5546 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5547 test->i = AF_INET;
5548
5549 /*
5550 * If we are parsing url in frontend space, we prepare backend stage
5551 * to not parse again the same url ! optimization lazyness...
5552 */
5553 if (px->options & PR_O_HTTP_PROXY)
5554 l4->flags |= SN_ADDR_SET;
5555
5556 test->flags = ACL_TEST_F_READ_ONLY;
5557 return 1;
5558}
5559
5560static int
5561acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5562 struct acl_expr *expr, struct acl_test *test)
5563{
5564 struct http_txn *txn = l7;
5565
Willy Tarreaub6866442008-07-14 23:54:42 +02005566 if (!txn)
5567 return 0;
5568
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005569 if (txn->req.msg_state != HTTP_MSG_BODY)
5570 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005571
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005572 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5573 /* ensure the indexes are not affected */
5574 return 0;
5575
5576 /* Same optimization as url_ip */
5577 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5578 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5579
5580 if (px->options & PR_O_HTTP_PROXY)
5581 l4->flags |= SN_ADDR_SET;
5582
5583 test->flags = ACL_TEST_F_READ_ONLY;
5584 return 1;
5585}
5586
Willy Tarreauc11416f2007-06-17 16:58:38 +02005587/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5588 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5589 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005590static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005591acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005592 struct acl_expr *expr, struct acl_test *test)
5593{
5594 struct http_txn *txn = l7;
5595 struct hdr_idx *idx = &txn->hdr_idx;
5596 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005597
Willy Tarreaub6866442008-07-14 23:54:42 +02005598 if (!txn)
5599 return 0;
5600
Willy Tarreau33a7e692007-06-10 19:45:56 +02005601 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5602 /* search for header from the beginning */
5603 ctx->idx = 0;
5604
Willy Tarreau33a7e692007-06-10 19:45:56 +02005605 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5606 test->flags |= ACL_TEST_F_FETCH_MORE;
5607 test->flags |= ACL_TEST_F_VOL_HDR;
5608 test->len = ctx->vlen;
5609 test->ptr = (char *)ctx->line + ctx->val;
5610 return 1;
5611 }
5612
5613 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5614 test->flags |= ACL_TEST_F_VOL_HDR;
5615 return 0;
5616}
5617
Willy Tarreau33a7e692007-06-10 19:45:56 +02005618static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005619acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5620 struct acl_expr *expr, struct acl_test *test)
5621{
5622 struct http_txn *txn = l7;
5623
Willy Tarreaub6866442008-07-14 23:54:42 +02005624 if (!txn)
5625 return 0;
5626
Willy Tarreauc11416f2007-06-17 16:58:38 +02005627 if (txn->req.msg_state != HTTP_MSG_BODY)
5628 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005629
Willy Tarreauc11416f2007-06-17 16:58:38 +02005630 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5631 /* ensure the indexes are not affected */
5632 return 0;
5633
5634 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5635}
5636
5637static int
5638acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5639 struct acl_expr *expr, struct acl_test *test)
5640{
5641 struct http_txn *txn = l7;
5642
Willy Tarreaub6866442008-07-14 23:54:42 +02005643 if (!txn)
5644 return 0;
5645
Willy Tarreauc11416f2007-06-17 16:58:38 +02005646 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5647 return 0;
5648
5649 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5650}
5651
5652/* 6. Check on HTTP header count. The number of occurrences is returned.
5653 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5654 */
5655static int
5656acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005657 struct acl_expr *expr, struct acl_test *test)
5658{
5659 struct http_txn *txn = l7;
5660 struct hdr_idx *idx = &txn->hdr_idx;
5661 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005662 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005663
Willy Tarreaub6866442008-07-14 23:54:42 +02005664 if (!txn)
5665 return 0;
5666
Willy Tarreau33a7e692007-06-10 19:45:56 +02005667 ctx.idx = 0;
5668 cnt = 0;
5669 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5670 cnt++;
5671
5672 test->i = cnt;
5673 test->flags = ACL_TEST_F_VOL_HDR;
5674 return 1;
5675}
5676
Willy Tarreauc11416f2007-06-17 16:58:38 +02005677static int
5678acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5679 struct acl_expr *expr, struct acl_test *test)
5680{
5681 struct http_txn *txn = l7;
5682
Willy Tarreaub6866442008-07-14 23:54:42 +02005683 if (!txn)
5684 return 0;
5685
Willy Tarreauc11416f2007-06-17 16:58:38 +02005686 if (txn->req.msg_state != HTTP_MSG_BODY)
5687 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005688
Willy Tarreauc11416f2007-06-17 16:58:38 +02005689 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5690 /* ensure the indexes are not affected */
5691 return 0;
5692
5693 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5694}
5695
5696static int
5697acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5698 struct acl_expr *expr, struct acl_test *test)
5699{
5700 struct http_txn *txn = l7;
5701
Willy Tarreaub6866442008-07-14 23:54:42 +02005702 if (!txn)
5703 return 0;
5704
Willy Tarreauc11416f2007-06-17 16:58:38 +02005705 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5706 return 0;
5707
5708 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5709}
5710
Willy Tarreau33a7e692007-06-10 19:45:56 +02005711/* 7. Check on HTTP header's integer value. The integer value is returned.
5712 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005713 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005714 */
5715static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005716acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005717 struct acl_expr *expr, struct acl_test *test)
5718{
5719 struct http_txn *txn = l7;
5720 struct hdr_idx *idx = &txn->hdr_idx;
5721 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005722
Willy Tarreaub6866442008-07-14 23:54:42 +02005723 if (!txn)
5724 return 0;
5725
Willy Tarreau33a7e692007-06-10 19:45:56 +02005726 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5727 /* search for header from the beginning */
5728 ctx->idx = 0;
5729
Willy Tarreau33a7e692007-06-10 19:45:56 +02005730 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5731 test->flags |= ACL_TEST_F_FETCH_MORE;
5732 test->flags |= ACL_TEST_F_VOL_HDR;
5733 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5734 return 1;
5735 }
5736
5737 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5738 test->flags |= ACL_TEST_F_VOL_HDR;
5739 return 0;
5740}
5741
Willy Tarreauc11416f2007-06-17 16:58:38 +02005742static int
5743acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5744 struct acl_expr *expr, struct acl_test *test)
5745{
5746 struct http_txn *txn = l7;
5747
Willy Tarreaub6866442008-07-14 23:54:42 +02005748 if (!txn)
5749 return 0;
5750
Willy Tarreauc11416f2007-06-17 16:58:38 +02005751 if (txn->req.msg_state != HTTP_MSG_BODY)
5752 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005753
Willy Tarreauc11416f2007-06-17 16:58:38 +02005754 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5755 /* ensure the indexes are not affected */
5756 return 0;
5757
5758 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5759}
5760
5761static int
5762acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5763 struct acl_expr *expr, struct acl_test *test)
5764{
5765 struct http_txn *txn = l7;
5766
Willy Tarreaub6866442008-07-14 23:54:42 +02005767 if (!txn)
5768 return 0;
5769
Willy Tarreauc11416f2007-06-17 16:58:38 +02005770 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5771 return 0;
5772
5773 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5774}
5775
Willy Tarreau737b0c12007-06-10 21:28:46 +02005776/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5777 * the first '/' after the possible hostname, and ends before the possible '?'.
5778 */
5779static int
5780acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5781 struct acl_expr *expr, struct acl_test *test)
5782{
5783 struct http_txn *txn = l7;
5784 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005785
Willy Tarreaub6866442008-07-14 23:54:42 +02005786 if (!txn)
5787 return 0;
5788
Willy Tarreauc11416f2007-06-17 16:58:38 +02005789 if (txn->req.msg_state != HTTP_MSG_BODY)
5790 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005791
Willy Tarreauc11416f2007-06-17 16:58:38 +02005792 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5793 /* ensure the indexes are not affected */
5794 return 0;
5795
Willy Tarreau21d2af32008-02-14 20:25:24 +01005796 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5797 ptr = http_get_path(txn);
5798 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005799 return 0;
5800
5801 /* OK, we got the '/' ! */
5802 test->ptr = ptr;
5803
5804 while (ptr < end && *ptr != '?')
5805 ptr++;
5806
5807 test->len = ptr - test->ptr;
5808
5809 /* we do not need to set READ_ONLY because the data is in a buffer */
5810 test->flags = ACL_TEST_F_VOL_1ST;
5811 return 1;
5812}
5813
5814
Willy Tarreau8797c062007-05-07 00:55:35 +02005815
5816/************************************************************************/
5817/* All supported keywords must be declared here. */
5818/************************************************************************/
5819
5820/* Note: must not be declared <const> as its list will be overwritten */
5821static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005822 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5823 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5824 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5825 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005826
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005827 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5828 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5829 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5830 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5831 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5832 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5833 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5834 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5835 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005836
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005837 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5838 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5839 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5840 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5841 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5842 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5843 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5844 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5845 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5846 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005847
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005848 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5849 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5850 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5851 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5852 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5853 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5854 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5855 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5856 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005857
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005858 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5859 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5860 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5861 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5862 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5863 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5864 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005865
Willy Tarreauf3d25982007-05-08 22:45:09 +02005866 { NULL, NULL, NULL, NULL },
5867
5868#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005869 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5870 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5871 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5872 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5873 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5874 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5875 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5876
Willy Tarreau8797c062007-05-07 00:55:35 +02005877 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5878 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5879 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5880 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5881 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5882 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5883 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5884 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5885
5886 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5887 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5888 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5889 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5890 { NULL, NULL, NULL, NULL },
5891#endif
5892}};
5893
5894
5895__attribute__((constructor))
5896static void __http_protocol_init(void)
5897{
5898 acl_register_keywords(&acl_kws);
5899}
5900
5901
Willy Tarreau58f10d72006-12-04 02:26:12 +01005902/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005903 * Local variables:
5904 * c-indent-level: 8
5905 * c-basic-offset: 8
5906 * End:
5907 */