blob: df037bd0ec619f62876f60fafb89e55a51ad1b37 [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 Tarreau0c303ee2008-07-07 00:09:58 +0200676 t->expire = tick_first(tick_first(s->req->rex, s->req->wex),
677 tick_first(s->rep->rex, s->rep->wex));
678 t->expire = tick_first(t->expire, s->req->cex);
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200679 if (s->analysis & AN_REQ_ANY) {
680 if (s->analysis & AN_REQ_INSPECT)
681 t->expire = tick_first(t->expire, s->inspect_exp);
682 else if (s->analysis & AN_REQ_HTTP_HDR)
683 t->expire = tick_first(t->expire, s->txn.exp);
684 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200685
686 /* restore t to its place in the task list */
687 task_queue(t);
688
Willy Tarreaud825eef2007-05-12 22:35:00 +0200689 *next = t->expire;
690 return; /* nothing more to do */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200691 }
692
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100693 s->fe->feconn--;
694 if (s->flags & SN_BE_ASSIGNED)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200695 s->be->beconn--;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200696 actconn--;
697
Willy Tarreauf41d4b12007-04-28 23:26:14 +0200698 if (unlikely((global.mode & MODE_DEBUG) &&
699 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200700 int len;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100701 len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200702 s->uniq_id, s->be->id,
Willy Tarreau45e73e32006-12-17 00:05:15 +0100703 (unsigned short)s->cli_fd, (unsigned short)s->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200704 write(1, trash, len);
705 }
706
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200707 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100708 session_process_counters(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200709
710 /* let's do a final log if we need it */
Willy Tarreau1c47f852006-07-09 08:22:27 +0200711 if (s->logs.logwait &&
712 !(s->flags & SN_MONITOR) &&
Willy Tarreau42250582007-04-01 01:30:43 +0200713 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
714 if (s->fe->to_log & LW_REQ)
715 http_sess_log(s);
716 else
717 tcp_sess_log(s);
718 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200719
720 /* the task MUST not be in the run queue anymore */
721 task_delete(t);
722 session_free(s);
723 task_free(t);
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200724 *next = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200725}
726
727
Willy Tarreau42250582007-04-01 01:30:43 +0200728extern const char sess_term_cond[8];
729extern const char sess_fin_state[8];
730extern const char *monthname[12];
731const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
732const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
733 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
734 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200735struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200736struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100737
Willy Tarreau42250582007-04-01 01:30:43 +0200738/*
739 * send a log for the session when we have enough info about it.
740 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100741 */
Willy Tarreau42250582007-04-01 01:30:43 +0200742static void http_sess_log(struct session *s)
743{
744 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
745 struct proxy *fe = s->fe;
746 struct proxy *be = s->be;
747 struct proxy *prx_log;
748 struct http_txn *txn = &s->txn;
749 int tolog;
750 char *uri, *h;
751 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200752 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200753 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200754 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200755 int hdr;
756
757 if (fe->logfac1 < 0 && fe->logfac2 < 0)
758 return;
759 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100760
Willy Tarreau42250582007-04-01 01:30:43 +0200761 if (s->cli_addr.ss_family == AF_INET)
762 inet_ntop(AF_INET,
763 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
764 pn, sizeof(pn));
765 else
766 inet_ntop(AF_INET6,
767 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
768 pn, sizeof(pn));
769
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200770 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200771
772 /* FIXME: let's limit ourselves to frontend logging for now. */
773 tolog = fe->to_log;
774
775 h = tmpline;
776 if (fe->to_log & LW_REQHDR &&
777 txn->req.cap &&
778 (h < tmpline + sizeof(tmpline) - 10)) {
779 *(h++) = ' ';
780 *(h++) = '{';
781 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
782 if (hdr)
783 *(h++) = '|';
784 if (txn->req.cap[hdr] != NULL)
785 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
786 '#', hdr_encode_map, txn->req.cap[hdr]);
787 }
788 *(h++) = '}';
789 }
790
791 if (fe->to_log & LW_RSPHDR &&
792 txn->rsp.cap &&
793 (h < tmpline + sizeof(tmpline) - 7)) {
794 *(h++) = ' ';
795 *(h++) = '{';
796 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
797 if (hdr)
798 *(h++) = '|';
799 if (txn->rsp.cap[hdr] != NULL)
800 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
801 '#', hdr_encode_map, txn->rsp.cap[hdr]);
802 }
803 *(h++) = '}';
804 }
805
806 if (h < tmpline + sizeof(tmpline) - 4) {
807 *(h++) = ' ';
808 *(h++) = '"';
809 uri = txn->uri ? txn->uri : "<BADREQ>";
810 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
811 '#', url_encode_map, uri);
812 *(h++) = '"';
813 }
814 *h = '\0';
815
816 svid = (tolog & LW_SVID) ?
817 (s->data_source != DATA_SRC_STATS) ?
818 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
819
Willy Tarreau70089872008-06-13 21:12:51 +0200820 t_request = -1;
821 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
822 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
823
Willy Tarreau42250582007-04-01 01:30:43 +0200824 send_log(prx_log, LOG_INFO,
825 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
826 " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100827 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200828 pn,
829 (s->cli_addr.ss_family == AF_INET) ?
830 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
831 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200832 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200833 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200834 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200835 t_request,
836 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200837 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
838 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
839 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
840 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100841 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200842 txn->cli_cookie ? txn->cli_cookie : "-",
843 txn->srv_cookie ? txn->srv_cookie : "-",
844 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
845 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
846 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
847 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
848 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100849 (s->flags & SN_REDISP)?"+":"",
850 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200851 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
852
853 s->logs.logwait = 0;
854}
855
Willy Tarreau117f59e2007-03-04 18:17:17 +0100856
857/*
858 * Capture headers from message starting at <som> according to header list
859 * <cap_hdr>, and fill the <idx> structure appropriately.
860 */
861void capture_headers(char *som, struct hdr_idx *idx,
862 char **cap, struct cap_hdr *cap_hdr)
863{
864 char *eol, *sol, *col, *sov;
865 int cur_idx;
866 struct cap_hdr *h;
867 int len;
868
869 sol = som + hdr_idx_first_pos(idx);
870 cur_idx = hdr_idx_first_idx(idx);
871
872 while (cur_idx) {
873 eol = sol + idx->v[cur_idx].len;
874
875 col = sol;
876 while (col < eol && *col != ':')
877 col++;
878
879 sov = col + 1;
880 while (sov < eol && http_is_lws[(unsigned char)*sov])
881 sov++;
882
883 for (h = cap_hdr; h; h = h->next) {
884 if ((h->namelen == col - sol) &&
885 (strncasecmp(sol, h->name, h->namelen) == 0)) {
886 if (cap[h->index] == NULL)
887 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200888 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100889
890 if (cap[h->index] == NULL) {
891 Alert("HTTP capture : out of memory.\n");
892 continue;
893 }
894
895 len = eol - sov;
896 if (len > h->len)
897 len = h->len;
898
899 memcpy(cap[h->index], sov, len);
900 cap[h->index][len]=0;
901 }
902 }
903 sol = eol + idx->v[cur_idx].cr + 1;
904 cur_idx = idx->v[cur_idx].next;
905 }
906}
907
908
Willy Tarreau42250582007-04-01 01:30:43 +0200909/* either we find an LF at <ptr> or we jump to <bad>.
910 */
911#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
912
913/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
914 * otherwise to <http_msg_ood> with <state> set to <st>.
915 */
916#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
917 ptr++; \
918 if (likely(ptr < end)) \
919 goto good; \
920 else { \
921 state = (st); \
922 goto http_msg_ood; \
923 } \
924 } while (0)
925
926
Willy Tarreaubaaee002006-06-26 02:48:02 +0200927/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100928 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100929 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
930 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
931 * will give undefined results.
932 * Note that it is upon the caller's responsibility to ensure that ptr < end,
933 * and that msg->sol points to the beginning of the response.
934 * If a complete line is found (which implies that at least one CR or LF is
935 * found before <end>, the updated <ptr> is returned, otherwise NULL is
936 * returned indicating an incomplete line (which does not mean that parts have
937 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
938 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
939 * upon next call.
940 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200941 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100942 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
943 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200944 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100945 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100946const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
947 unsigned int state, const char *ptr, const char *end,
948 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100949{
950 __label__
951 http_msg_rpver,
952 http_msg_rpver_sp,
953 http_msg_rpcode,
954 http_msg_rpcode_sp,
955 http_msg_rpreason,
956 http_msg_rpline_eol,
957 http_msg_ood, /* out of data */
958 http_msg_invalid;
959
960 switch (state) {
961 http_msg_rpver:
962 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100963 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100964 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
965
966 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100967 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100968 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
969 }
970 goto http_msg_invalid;
971
972 http_msg_rpver_sp:
973 case HTTP_MSG_RPVER_SP:
974 if (likely(!HTTP_IS_LWS(*ptr))) {
975 msg->sl.st.c = ptr - msg_buf;
976 goto http_msg_rpcode;
977 }
978 if (likely(HTTP_IS_SPHT(*ptr)))
979 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
980 /* so it's a CR/LF, this is invalid */
981 goto http_msg_invalid;
982
983 http_msg_rpcode:
984 case HTTP_MSG_RPCODE:
985 if (likely(!HTTP_IS_LWS(*ptr)))
986 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
987
988 if (likely(HTTP_IS_SPHT(*ptr))) {
989 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
990 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
991 }
992
993 /* so it's a CR/LF, so there is no reason phrase */
994 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
995 http_msg_rsp_reason:
996 /* FIXME: should we support HTTP responses without any reason phrase ? */
997 msg->sl.st.r = ptr - msg_buf;
998 msg->sl.st.r_l = 0;
999 goto http_msg_rpline_eol;
1000
1001 http_msg_rpcode_sp:
1002 case HTTP_MSG_RPCODE_SP:
1003 if (likely(!HTTP_IS_LWS(*ptr))) {
1004 msg->sl.st.r = ptr - msg_buf;
1005 goto http_msg_rpreason;
1006 }
1007 if (likely(HTTP_IS_SPHT(*ptr)))
1008 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1009 /* so it's a CR/LF, so there is no reason phrase */
1010 goto http_msg_rsp_reason;
1011
1012 http_msg_rpreason:
1013 case HTTP_MSG_RPREASON:
1014 if (likely(!HTTP_IS_CRLF(*ptr)))
1015 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1016 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1017 http_msg_rpline_eol:
1018 /* We have seen the end of line. Note that we do not
1019 * necessarily have the \n yet, but at least we know that we
1020 * have EITHER \r OR \n, otherwise the response would not be
1021 * complete. We can then record the response length and return
1022 * to the caller which will be able to register it.
1023 */
1024 msg->sl.st.l = ptr - msg->sol;
1025 return ptr;
1026
1027#ifdef DEBUG_FULL
1028 default:
1029 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1030 exit(1);
1031#endif
1032 }
1033
1034 http_msg_ood:
1035 /* out of data */
1036 if (ret_state)
1037 *ret_state = state;
1038 if (ret_ptr)
1039 *ret_ptr = (char *)ptr;
1040 return NULL;
1041
1042 http_msg_invalid:
1043 /* invalid message */
1044 if (ret_state)
1045 *ret_state = HTTP_MSG_ERROR;
1046 return NULL;
1047}
1048
1049
1050/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001051 * This function parses a request line between <ptr> and <end>, starting with
1052 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1053 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1054 * will give undefined results.
1055 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1056 * and that msg->sol points to the beginning of the request.
1057 * If a complete line is found (which implies that at least one CR or LF is
1058 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1059 * returned indicating an incomplete line (which does not mean that parts have
1060 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1061 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1062 * upon next call.
1063 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001064 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001065 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1066 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001067 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001068 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001069const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1070 unsigned int state, const char *ptr, const char *end,
1071 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001072{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 __label__
1074 http_msg_rqmeth,
1075 http_msg_rqmeth_sp,
1076 http_msg_rquri,
1077 http_msg_rquri_sp,
1078 http_msg_rqver,
1079 http_msg_rqline_eol,
1080 http_msg_ood, /* out of data */
1081 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 switch (state) {
1084 http_msg_rqmeth:
1085 case HTTP_MSG_RQMETH:
1086 if (likely(HTTP_IS_TOKEN(*ptr)))
1087 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001088
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001089 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001090 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1092 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001093
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001094 if (likely(HTTP_IS_CRLF(*ptr))) {
1095 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001096 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001097 http_msg_req09_uri:
1098 msg->sl.rq.u = ptr - msg_buf;
1099 http_msg_req09_uri_e:
1100 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1101 http_msg_req09_ver:
1102 msg->sl.rq.v = ptr - msg_buf;
1103 msg->sl.rq.v_l = 0;
1104 goto http_msg_rqline_eol;
1105 }
1106 goto http_msg_invalid;
1107
1108 http_msg_rqmeth_sp:
1109 case HTTP_MSG_RQMETH_SP:
1110 if (likely(!HTTP_IS_LWS(*ptr))) {
1111 msg->sl.rq.u = ptr - msg_buf;
1112 goto http_msg_rquri;
1113 }
1114 if (likely(HTTP_IS_SPHT(*ptr)))
1115 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1116 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1117 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001118
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001119 http_msg_rquri:
1120 case HTTP_MSG_RQURI:
1121 if (likely(!HTTP_IS_LWS(*ptr)))
1122 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001123
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001124 if (likely(HTTP_IS_SPHT(*ptr))) {
1125 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1126 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1127 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001128
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001129 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1130 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001132 http_msg_rquri_sp:
1133 case HTTP_MSG_RQURI_SP:
1134 if (likely(!HTTP_IS_LWS(*ptr))) {
1135 msg->sl.rq.v = ptr - msg_buf;
1136 goto http_msg_rqver;
1137 }
1138 if (likely(HTTP_IS_SPHT(*ptr)))
1139 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1140 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1141 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001142
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001143 http_msg_rqver:
1144 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001145 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001146 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001147
1148 if (likely(HTTP_IS_CRLF(*ptr))) {
1149 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1150 http_msg_rqline_eol:
1151 /* We have seen the end of line. Note that we do not
1152 * necessarily have the \n yet, but at least we know that we
1153 * have EITHER \r OR \n, otherwise the request would not be
1154 * complete. We can then record the request length and return
1155 * to the caller which will be able to register it.
1156 */
1157 msg->sl.rq.l = ptr - msg->sol;
1158 return ptr;
1159 }
1160
1161 /* neither an HTTP_VER token nor a CRLF */
1162 goto http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001163
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001164#ifdef DEBUG_FULL
1165 default:
1166 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1167 exit(1);
1168#endif
1169 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001170
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001171 http_msg_ood:
1172 /* out of data */
1173 if (ret_state)
1174 *ret_state = state;
1175 if (ret_ptr)
1176 *ret_ptr = (char *)ptr;
1177 return NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001178
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001179 http_msg_invalid:
1180 /* invalid message */
1181 if (ret_state)
1182 *ret_state = HTTP_MSG_ERROR;
1183 return NULL;
1184}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001185
1186
Willy Tarreau8973c702007-01-21 23:58:29 +01001187/*
1188 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001189 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 * when data are missing and recalled at the exact same location with no
1191 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001192 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1193 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001194 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001195void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1196{
1197 __label__
1198 http_msg_rqbefore,
1199 http_msg_rqbefore_cr,
1200 http_msg_rqmeth,
1201 http_msg_rqline_end,
1202 http_msg_hdr_first,
1203 http_msg_hdr_name,
1204 http_msg_hdr_l1_sp,
1205 http_msg_hdr_l1_lf,
1206 http_msg_hdr_l1_lws,
1207 http_msg_hdr_val,
1208 http_msg_hdr_l2_lf,
1209 http_msg_hdr_l2_lws,
1210 http_msg_complete_header,
1211 http_msg_last_lf,
1212 http_msg_ood, /* out of data */
1213 http_msg_invalid;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001214
Willy Tarreaue69eada2008-01-27 00:34:10 +01001215 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001216 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001217
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001218 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001219 ptr = buf->lr;
1220 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001221
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001222 if (unlikely(ptr >= end))
1223 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001224
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001225 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001226 /*
1227 * First, states that are specific to the response only.
1228 * We check them first so that request and headers are
1229 * closer to each other (accessed more often).
1230 */
1231 http_msg_rpbefore:
1232 case HTTP_MSG_RPBEFORE:
1233 if (likely(HTTP_IS_TOKEN(*ptr))) {
1234 if (likely(ptr == buf->data)) {
1235 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001236 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001237 } else {
1238#if PARSE_PRESERVE_EMPTY_LINES
1239 /* only skip empty leading lines, don't remove them */
1240 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001241 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001242#else
1243 /* Remove empty leading lines, as recommended by
1244 * RFC2616. This takes a lot of time because we
1245 * must move all the buffer backwards, but this
1246 * is rarely needed. The method above will be
1247 * cleaner when we'll be able to start sending
1248 * the request from any place in the buffer.
1249 */
1250 buf->lr = ptr;
1251 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001252 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001253 msg->sol = buf->data;
1254 ptr = buf->data;
1255 end = buf->r;
1256#endif
1257 }
1258 hdr_idx_init(idx);
1259 state = HTTP_MSG_RPVER;
1260 goto http_msg_rpver;
1261 }
1262
1263 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1264 goto http_msg_invalid;
1265
1266 if (unlikely(*ptr == '\n'))
1267 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1268 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1269 /* stop here */
1270
1271 http_msg_rpbefore_cr:
1272 case HTTP_MSG_RPBEFORE_CR:
1273 EXPECT_LF_HERE(ptr, http_msg_invalid);
1274 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1275 /* stop here */
1276
1277 http_msg_rpver:
1278 case HTTP_MSG_RPVER:
1279 case HTTP_MSG_RPVER_SP:
1280 case HTTP_MSG_RPCODE:
1281 case HTTP_MSG_RPCODE_SP:
1282 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001283 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001284 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001285 if (unlikely(!ptr))
1286 return;
1287
1288 /* we have a full response and we know that we have either a CR
1289 * or an LF at <ptr>.
1290 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001291 //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 +01001292 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1293
1294 msg->sol = ptr;
1295 if (likely(*ptr == '\r'))
1296 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1297 goto http_msg_rpline_end;
1298
1299 http_msg_rpline_end:
1300 case HTTP_MSG_RPLINE_END:
1301 /* msg->sol must point to the first of CR or LF. */
1302 EXPECT_LF_HERE(ptr, http_msg_invalid);
1303 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1304 /* stop here */
1305
1306 /*
1307 * Second, states that are specific to the request only
1308 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001309 http_msg_rqbefore:
1310 case HTTP_MSG_RQBEFORE:
1311 if (likely(HTTP_IS_TOKEN(*ptr))) {
1312 if (likely(ptr == buf->data)) {
1313 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001314 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001315 } else {
1316#if PARSE_PRESERVE_EMPTY_LINES
1317 /* only skip empty leading lines, don't remove them */
1318 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001319 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001320#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 /* Remove empty leading lines, as recommended by
1322 * RFC2616. This takes a lot of time because we
1323 * must move all the buffer backwards, but this
1324 * is rarely needed. The method above will be
1325 * cleaner when we'll be able to start sending
1326 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001327 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 buf->lr = ptr;
1329 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001330 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 msg->sol = buf->data;
1332 ptr = buf->data;
1333 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001334#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001335 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001336 /* we will need this when keep-alive will be supported
1337 hdr_idx_init(idx);
1338 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 state = HTTP_MSG_RQMETH;
1340 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001343 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1344 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001345
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001346 if (unlikely(*ptr == '\n'))
1347 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1348 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001350
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001351 http_msg_rqbefore_cr:
1352 case HTTP_MSG_RQBEFORE_CR:
1353 EXPECT_LF_HERE(ptr, http_msg_invalid);
1354 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001357 http_msg_rqmeth:
1358 case HTTP_MSG_RQMETH:
1359 case HTTP_MSG_RQMETH_SP:
1360 case HTTP_MSG_RQURI:
1361 case HTTP_MSG_RQURI_SP:
1362 case HTTP_MSG_RQVER:
1363 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001364 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 if (unlikely(!ptr))
1366 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001367
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001368 /* we have a full request and we know that we have either a CR
1369 * or an LF at <ptr>.
1370 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001371 //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 +01001372 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001373
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001374 msg->sol = ptr;
1375 if (likely(*ptr == '\r'))
1376 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001377 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001378
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001379 http_msg_rqline_end:
1380 case HTTP_MSG_RQLINE_END:
1381 /* check for HTTP/0.9 request : no version information available.
1382 * msg->sol must point to the first of CR or LF.
1383 */
1384 if (unlikely(msg->sl.rq.v_l == 0))
1385 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 EXPECT_LF_HERE(ptr, http_msg_invalid);
1388 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001389 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001390
Willy Tarreau8973c702007-01-21 23:58:29 +01001391 /*
1392 * Common states below
1393 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 http_msg_hdr_first:
1395 case HTTP_MSG_HDR_FIRST:
1396 msg->sol = ptr;
1397 if (likely(!HTTP_IS_CRLF(*ptr))) {
1398 goto http_msg_hdr_name;
1399 }
1400
1401 if (likely(*ptr == '\r'))
1402 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1403 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001404
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 http_msg_hdr_name:
1406 case HTTP_MSG_HDR_NAME:
1407 /* assumes msg->sol points to the first char */
1408 if (likely(HTTP_IS_TOKEN(*ptr)))
1409 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 if (likely(*ptr == ':')) {
1412 msg->col = ptr - buf->data;
1413 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1414 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001415
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001417
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 http_msg_hdr_l1_sp:
1419 case HTTP_MSG_HDR_L1_SP:
1420 /* assumes msg->sol points to the first char and msg->col to the colon */
1421 if (likely(HTTP_IS_SPHT(*ptr)))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 /* header value can be basically anything except CR/LF */
1425 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001426
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 if (likely(!HTTP_IS_CRLF(*ptr))) {
1428 goto http_msg_hdr_val;
1429 }
1430
1431 if (likely(*ptr == '\r'))
1432 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1433 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001434
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 http_msg_hdr_l1_lf:
1436 case HTTP_MSG_HDR_L1_LF:
1437 EXPECT_LF_HERE(ptr, http_msg_invalid);
1438 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 http_msg_hdr_l1_lws:
1441 case HTTP_MSG_HDR_L1_LWS:
1442 if (likely(HTTP_IS_SPHT(*ptr))) {
1443 /* replace HT,CR,LF with spaces */
1444 for (; buf->data+msg->sov < ptr; msg->sov++)
1445 buf->data[msg->sov] = ' ';
1446 goto http_msg_hdr_l1_sp;
1447 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001448 /* we had a header consisting only in spaces ! */
1449 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 goto http_msg_complete_header;
1451
1452 http_msg_hdr_val:
1453 case HTTP_MSG_HDR_VAL:
1454 /* assumes msg->sol points to the first char, msg->col to the
1455 * colon, and msg->sov points to the first character of the
1456 * value.
1457 */
1458 if (likely(!HTTP_IS_CRLF(*ptr)))
1459 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 msg->eol = ptr;
1462 /* Note: we could also copy eol into ->eoh so that we have the
1463 * real header end in case it ends with lots of LWS, but is this
1464 * really needed ?
1465 */
1466 if (likely(*ptr == '\r'))
1467 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1468 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001469
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 http_msg_hdr_l2_lf:
1471 case HTTP_MSG_HDR_L2_LF:
1472 EXPECT_LF_HERE(ptr, http_msg_invalid);
1473 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 http_msg_hdr_l2_lws:
1476 case HTTP_MSG_HDR_L2_LWS:
1477 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1478 /* LWS: replace HT,CR,LF with spaces */
1479 for (; msg->eol < ptr; msg->eol++)
1480 *msg->eol = ' ';
1481 goto http_msg_hdr_val;
1482 }
1483 http_msg_complete_header:
1484 /*
1485 * It was a new header, so the last one is finished.
1486 * Assumes msg->sol points to the first char, msg->col to the
1487 * colon, msg->sov points to the first character of the value
1488 * and msg->eol to the first CR or LF so we know how the line
1489 * ends. We insert last header into the index.
1490 */
1491 /*
1492 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1493 write(2, msg->sol, msg->eol-msg->sol);
1494 fprintf(stderr,"\n");
1495 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001496
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1498 idx, idx->tail) < 0))
1499 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001500
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 msg->sol = ptr;
1502 if (likely(!HTTP_IS_CRLF(*ptr))) {
1503 goto http_msg_hdr_name;
1504 }
1505
1506 if (likely(*ptr == '\r'))
1507 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1508 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001509
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 http_msg_last_lf:
1511 case HTTP_MSG_LAST_LF:
1512 /* Assumes msg->sol points to the first of either CR or LF */
1513 EXPECT_LF_HERE(ptr, http_msg_invalid);
1514 ptr++;
1515 buf->lr = ptr;
1516 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001517 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 return;
1519#ifdef DEBUG_FULL
1520 default:
1521 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1522 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001523#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 }
1525 http_msg_ood:
1526 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001527 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 buf->lr = ptr;
1529 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 http_msg_invalid:
1532 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001533 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 return;
1535}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537/*
1538 * manages the client FSM and its socket. BTW, it also tries to handle the
1539 * cookie. It returns 1 if a state has changed (and a resync may be needed),
1540 * 0 else.
1541 */
1542int process_cli(struct session *t)
1543{
1544 int s = t->srv_state;
1545 int c = t->cli_state;
1546 struct buffer *req = t->req;
1547 struct buffer *rep = t->rep;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001548
Willy Tarreau6468d922008-08-03 19:15:35 +02001549 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 +01001550 cli_stnames[c], srv_stnames[s],
Willy Tarreauf161a342007-04-08 16:59:42 +02001551 EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR),
Willy Tarreau6468d922008-08-03 19:15:35 +02001552 req->rex, rep->wex);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001553
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001554 if (t->analysis & AN_REQ_INSPECT) {
Willy Tarreaub6866442008-07-14 23:54:42 +02001555 struct tcp_rule *rule;
1556 int partial;
1557
1558 /* We will abort if we encounter a read error. In theory,
1559 * we should not abort if we get a close, it might be
1560 * valid, also very unlikely. FIXME: we'll abort for now,
1561 * this will be easier to change later.
1562 */
1563 if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1564 t->inspect_exp = TICK_ETERNITY;
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001565 buffer_shutr_done(req);
1566 buffer_shutw_done(rep);
Willy Tarreaub6866442008-07-14 23:54:42 +02001567 fd_delete(t->cli_fd);
1568 t->cli_state = CL_STCLOSE;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001569 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001570 t->fe->failed_req++;
1571 if (!(t->flags & SN_ERR_MASK))
1572 t->flags |= SN_ERR_CLICL;
1573 if (!(t->flags & SN_FINST_MASK))
1574 t->flags |= SN_FINST_R;
1575 return 1;
1576 }
1577
1578 /* Abort if client read timeout has expired */
1579 else if (unlikely(tick_is_expired(req->rex, now_ms))) {
1580 t->inspect_exp = TICK_ETERNITY;
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001581 buffer_shutr_done(req);
1582 buffer_shutw_done(rep);
Willy Tarreaub6866442008-07-14 23:54:42 +02001583 fd_delete(t->cli_fd);
1584 t->cli_state = CL_STCLOSE;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001585 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001586 t->fe->failed_req++;
1587 if (!(t->flags & SN_ERR_MASK))
1588 t->flags |= SN_ERR_CLITO;
1589 if (!(t->flags & SN_FINST_MASK))
1590 t->flags |= SN_FINST_R;
1591 return 1;
1592 }
1593
1594 /* We don't know whether we have enough data, so must proceed
1595 * this way :
1596 * - iterate through all rules in their declaration order
1597 * - if one rule returns MISS, it means the inspect delay is
1598 * not over yet, then return immediately, otherwise consider
1599 * it as a non-match.
1600 * - if one rule returns OK, then return OK
1601 * - if one rule returns KO, then return KO
1602 */
1603
1604 if (tick_is_expired(t->inspect_exp, now_ms))
1605 partial = 0;
1606 else
1607 partial = ACL_PARTIAL;
1608
1609 list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) {
1610 int ret = ACL_PAT_PASS;
1611
1612 if (rule->cond) {
1613 ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial);
1614 if (ret == ACL_PAT_MISS) {
1615 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
1616 return 0;
1617 }
1618 ret = acl_pass(ret);
1619 if (rule->cond->pol == ACL_COND_UNLESS)
1620 ret = !ret;
1621 }
1622
1623 if (ret) {
1624 /* we have a matching rule. */
1625 if (rule->action == TCP_ACT_REJECT) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001626 buffer_shutr_done(req);
1627 buffer_shutw_done(rep);
Willy Tarreaub6866442008-07-14 23:54:42 +02001628 fd_delete(t->cli_fd);
1629 t->cli_state = CL_STCLOSE;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001630 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub6866442008-07-14 23:54:42 +02001631 t->fe->failed_req++;
1632 if (!(t->flags & SN_ERR_MASK))
1633 t->flags |= SN_ERR_PRXCOND;
1634 if (!(t->flags & SN_FINST_MASK))
1635 t->flags |= SN_FINST_R;
1636 t->inspect_exp = TICK_ETERNITY;
1637 return 1;
1638 }
1639 /* otherwise accept */
1640 break;
1641 }
1642 }
1643
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001644 /* if we get there, it means we have no rule which matches, or
1645 * we have an explicit accept, so we apply the default accept.
Willy Tarreaub6866442008-07-14 23:54:42 +02001646 */
1647 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001648 t->analysis &= ~AN_REQ_INSPECT;
1649 if (t->analysis & AN_REQ_HTTP_HDR)
Willy Tarreaub6866442008-07-14 23:54:42 +02001650 t->txn.exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001651 else
Willy Tarreau718f0ef2008-08-10 16:21:32 +02001652 req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001653
Willy Tarreaub6866442008-07-14 23:54:42 +02001654 t->inspect_exp = TICK_ETERNITY;
1655 return 1;
1656 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001657
1658 if (t->analysis & AN_REQ_HTTP_HDR) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001659 /*
1660 * Now parse the partial (or complete) lines.
1661 * We will check the request syntax, and also join multi-line
1662 * headers. An index of all the lines will be elaborated while
1663 * parsing.
1664 *
Willy Tarreau8973c702007-01-21 23:58:29 +01001665 * For the parsing, we use a 28 states FSM.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 *
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001667 * Here is the information we currently have :
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001668 * req->data + req->som = beginning of request
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 * req->data + req->eoh = end of processed headers / start of current one
1670 * req->data + req->eol = end of current header or line (LF or CRLF)
1671 * req->lr = first non-visited byte
1672 * req->r = end of data
1673 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001674
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 int cur_idx;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001676 struct http_txn *txn = &t->txn;
1677 struct http_msg *msg = &txn->req;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001678 struct proxy *cur_proxy;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001679
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001680 if (likely(req->lr < req->r))
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001681 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001682
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001683 /* 1: we might have to print this header in debug mode */
1684 if (unlikely((global.mode & MODE_DEBUG) &&
1685 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001686 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001687 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001688
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001689 sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 eol = sol + msg->sl.rq.l;
1691 debug_hdr("clireq", t, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001692
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001693 sol += hdr_idx_first_pos(&txn->hdr_idx);
1694 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001695
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001696 while (cur_idx) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001697 eol = sol + txn->hdr_idx.v[cur_idx].len;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 debug_hdr("clihdr", t, sol, eol);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001699 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1700 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001701 }
1702 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001703
Willy Tarreau58f10d72006-12-04 02:26:12 +01001704
1705 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 * Now we quickly check if we have found a full valid request.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001707 * If not so, we check the FD and buffer states before leaving.
1708 * A full request is indicated by the fact that we have seen
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1710 * requests are checked first.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001711 *
1712 */
1713
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001714 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001715 /*
1716 * First, let's catch bad requests.
1717 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001718 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001720
1721 /* 1: Since we are in header mode, if there's no space
1722 * left for headers, we won't be able to free more
1723 * later, so the session will never terminate. We
1724 * must terminate it now.
1725 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 if (unlikely(req->l >= req->rlim - req->data)) {
1727 /* FIXME: check if URI is set and return Status
1728 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001729 */
Willy Tarreau06619262006-12-17 08:37:22 +01001730 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001731 }
1732
1733 /* 2: have we encountered a read error or a close ? */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) {
1735 /* read error, or last read : give up. */
Willy Tarreau89edf5e2008-08-03 17:25:14 +02001736 buffer_shutr_done(req);
1737 buffer_shutw_done(rep);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001738 fd_delete(t->cli_fd);
1739 t->cli_state = CL_STCLOSE;
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001740 t->analysis &= ~AN_REQ_ANY;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001741 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001742 if (!(t->flags & SN_ERR_MASK))
1743 t->flags |= SN_ERR_CLICL;
1744 if (!(t->flags & SN_FINST_MASK))
1745 t->flags |= SN_FINST_R;
1746 return 1;
1747 }
1748
1749 /* 3: has the read timeout expired ? */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001750 else if (unlikely(tick_is_expired(req->rex, now_ms) ||
1751 tick_is_expired(txn->exp, now_ms))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001752 /* read timeout : give up with an error message. */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001753 txn->status = 408;
Willy Tarreau80587432006-12-24 17:47:20 +01001754 client_retnclose(t, error_message(t, HTTP_ERR_408));
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001755 t->analysis &= ~AN_REQ_ANY;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01001756 t->fe->failed_req++;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001757 if (!(t->flags & SN_ERR_MASK))
1758 t->flags |= SN_ERR_CLITO;
1759 if (!(t->flags & SN_FINST_MASK))
1760 t->flags |= SN_FINST_R;
1761 return 1;
1762 }
1763
1764 /* 4: do we need to re-enable the read socket ? */
Willy Tarreauadfb8562008-08-11 15:24:42 +02001765 else if (!tick_isset(req->rex)) {
1766 EV_FD_COND_S(t->cli_fd, DIR_RD);
Willy Tarreauf161a342007-04-08 16:59:42 +02001767 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreau58f10d72006-12-04 02:26:12 +01001768 * full. We cannot loop here since stream_sock_read will disable it only if
1769 * req->l == rlim-data
1770 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02001771 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001772 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001773 /* we don't need to resync if the state has not changed */
1774 return !(t->analysis & AN_REQ_HTTP_HDR);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001775 }
1776
1777
1778 /****************************************************************
1779 * More interesting part now : we know that we have a complete *
1780 * request which at least looks like HTTP. We have an indicator *
1781 * of each header's length, so we can parse them quickly. *
1782 ****************************************************************/
1783
Willy Tarreau67f0eea2008-08-10 22:55:22 +02001784 t->analysis &= ~AN_REQ_HTTP_HDR;
1785
Willy Tarreau9cdde232007-05-02 20:58:19 +02001786 /* ensure we keep this pointer to the beginning of the message */
1787 msg->sol = req->data + msg->som;
1788
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001789 /*
1790 * 1: identify the method
1791 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001792 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001793
1794 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001795 * 2: check if the URI matches the monitor_uri.
Willy Tarreau06619262006-12-17 08:37:22 +01001796 * We have to do this for every request which gets in, because
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 * the monitor-uri is defined by the frontend.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001798 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001799 if (unlikely((t->fe->monitor_uri_len != 0) &&
1800 (t->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1801 !memcmp(&req->data[msg->sl.rq.u],
1802 t->fe->monitor_uri,
1803 t->fe->monitor_uri_len))) {
1804 /*
1805 * We have found the monitor URI
1806 */
Willy Tarreaub80c2302007-11-30 20:51:32 +01001807 struct acl_cond *cond;
1808 cur_proxy = t->fe;
1809
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001810 t->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001811
1812 /* Check if we want to fail this monitor request or not */
1813 list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) {
1814 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001815
1816 ret = acl_pass(ret);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001817 if (cond->pol == ACL_COND_UNLESS)
1818 ret = !ret;
1819
1820 if (ret) {
1821 /* we fail this request, let's return 503 service unavail */
1822 txn->status = 503;
1823 client_retnclose(t, error_message(t, HTTP_ERR_503));
1824 goto return_prx_cond;
1825 }
1826 }
1827
1828 /* nothing to fail, let's reply normaly */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001829 txn->status = 200;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 client_retnclose(t, &http_200_chunk);
1831 goto return_prx_cond;
1832 }
1833
1834 /*
1835 * 3: Maybe we have to copy the original REQURI for the logs ?
1836 * Note: we cannot log anymore if the request has been
1837 * classified as invalid.
1838 */
1839 if (unlikely(t->logs.logwait & LW_REQ)) {
1840 /* we have a complete HTTP request that we must log */
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001841 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001842 int urilen = msg->sl.rq.l;
1843
1844 if (urilen >= REQURI_LEN)
1845 urilen = REQURI_LEN - 1;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01001846 memcpy(txn->uri, &req->data[msg->som], urilen);
1847 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001848
1849 if (!(t->logs.logwait &= ~LW_REQ))
Willy Tarreau42250582007-04-01 01:30:43 +02001850 http_sess_log(t);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001851 } else {
1852 Alert("HTTP logging : out of memory.\n");
1853 }
1854 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001855
Willy Tarreau06619262006-12-17 08:37:22 +01001856
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001857 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1858 if (unlikely(msg->sl.rq.v_l == 0)) {
1859 int delta;
1860 char *cur_end;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001861 msg->sol = req->data + msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001862 cur_end = msg->sol + msg->sl.rq.l;
1863 delta = 0;
Willy Tarreau06619262006-12-17 08:37:22 +01001864
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001865 if (msg->sl.rq.u_l == 0) {
1866 /* if no URI was set, add "/" */
1867 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1868 cur_end += delta;
1869 msg->eoh += delta;
Willy Tarreau06619262006-12-17 08:37:22 +01001870 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001871 /* add HTTP version */
1872 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1873 msg->eoh += delta;
1874 cur_end += delta;
1875 cur_end = (char *)http_parse_reqline(msg, req->data,
1876 HTTP_MSG_RQMETH,
1877 msg->sol, cur_end + 1,
1878 NULL, NULL);
1879 if (unlikely(!cur_end))
1880 goto return_bad_req;
1881
1882 /* we have a full HTTP/1.0 request now and we know that
1883 * we have either a CR or an LF at <ptr>.
1884 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01001885 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
Willy Tarreau58f10d72006-12-04 02:26:12 +01001886 }
1887
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001888
1889 /* 5: we may need to capture headers */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001890 if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001891 capture_headers(req->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001892 txn->req.cap, t->fe->req_cap);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001893
1894 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001895 * 6: we will have to evaluate the filters.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001896 * As opposed to version 1.2, now they will be evaluated in the
1897 * filters order and not in the header order. This means that
1898 * each filter has to be validated among all headers.
Willy Tarreau06619262006-12-17 08:37:22 +01001899 *
1900 * We can now check whether we want to switch to another
1901 * backend, in which case we will re-check the backend's
1902 * filters and various options. In order to support 3-level
1903 * switching, here's how we should proceed :
1904 *
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001905 * a) run be.
Willy Tarreau830ff452006-12-17 19:31:23 +01001906 * if (switch) then switch ->be to the new backend.
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001907 * b) run be if (be != fe).
Willy Tarreau06619262006-12-17 08:37:22 +01001908 * There cannot be any switch from there, so ->be cannot be
1909 * changed anymore.
1910 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001911 * => filters always apply to ->be, then ->be may change.
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001912 *
Willy Tarreau830ff452006-12-17 19:31:23 +01001913 * The response path will be able to apply either ->be, or
1914 * ->be then ->fe filters in order to match the reverse of
1915 * the forward sequence.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001916 */
1917
Willy Tarreau06619262006-12-17 08:37:22 +01001918 do {
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02001919 struct acl_cond *cond;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001920 struct redirect_rule *rule;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001921 struct proxy *rule_set = t->be;
Willy Tarreau830ff452006-12-17 19:31:23 +01001922 cur_proxy = t->be;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001923
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001924 /* first check whether we have some ACLs set to redirect this request */
1925 list_for_each_entry(rule, &cur_proxy->redirect_rules, list) {
1926 int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001927
1928 ret = acl_pass(ret);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001929 if (rule->cond->pol == ACL_COND_UNLESS)
1930 ret = !ret;
1931
1932 if (ret) {
1933 struct chunk rdr = { trash, 0 };
1934 const char *msg_fmt;
1935
1936 /* build redirect message */
1937 switch(rule->code) {
1938 case 303:
1939 rdr.len = strlen(HTTP_303);
1940 msg_fmt = HTTP_303;
1941 break;
1942 case 301:
1943 rdr.len = strlen(HTTP_301);
1944 msg_fmt = HTTP_301;
1945 break;
1946 case 302:
1947 default:
1948 rdr.len = strlen(HTTP_302);
1949 msg_fmt = HTTP_302;
1950 break;
1951 }
1952
1953 if (unlikely(rdr.len > sizeof(trash)))
1954 goto return_bad_req;
1955 memcpy(rdr.str, msg_fmt, rdr.len);
1956
1957 switch(rule->type) {
1958 case REDIRECT_TYPE_PREFIX: {
1959 const char *path;
1960 int pathlen;
1961
1962 path = http_get_path(txn);
1963 /* build message using path */
1964 if (path) {
1965 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
1966 } else {
1967 path = "/";
1968 pathlen = 1;
1969 }
1970
1971 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
1972 goto return_bad_req;
1973
1974 /* add prefix */
1975 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1976 rdr.len += rule->rdr_len;
1977
1978 /* add path */
1979 memcpy(rdr.str + rdr.len, path, pathlen);
1980 rdr.len += pathlen;
1981 break;
1982 }
1983 case REDIRECT_TYPE_LOCATION:
1984 default:
1985 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
1986 goto return_bad_req;
1987
1988 /* add location */
1989 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
1990 rdr.len += rule->rdr_len;
1991 break;
1992 }
1993
1994 /* add end of headers */
1995 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
1996 rdr.len += 4;
1997
1998 txn->status = rule->code;
1999 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002000 t->logs.tv_request = now;
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002001 client_retnclose(t, &rdr);
2002 goto return_prx_cond;
2003 }
2004 }
2005
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002006 /* first check whether we have some ACLs set to block this request */
2007 list_for_each_entry(cond, &cur_proxy->block_cond, list) {
Willy Tarreaud41f8d82007-06-10 10:06:18 +02002008 int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002009
2010 ret = acl_pass(ret);
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002011 if (cond->pol == ACL_COND_UNLESS)
2012 ret = !ret;
2013
2014 if (ret) {
2015 txn->status = 403;
2016 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002017 t->logs.tv_request = now;
Willy Tarreau5c8e3e02007-05-07 00:58:25 +02002018 client_retnclose(t, error_message(t, HTTP_ERR_403));
2019 goto return_prx_cond;
2020 }
2021 }
2022
Willy Tarreau06619262006-12-17 08:37:22 +01002023 /* try headers filters */
Willy Tarreau53b6c742006-12-17 13:37:46 +01002024 if (rule_set->req_exp != NULL) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002025 if (apply_filters_to_request(t, req, rule_set->req_exp) < 0)
2026 goto return_bad_req;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002027 }
2028
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002029 if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) {
2030 /* to ensure correct connection accounting on
2031 * the backend, we count the connection for the
2032 * one managing the queue.
2033 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002034 t->be->beconn++;
2035 if (t->be->beconn > t->be->beconn_max)
2036 t->be->beconn_max = t->be->beconn;
2037 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002038 t->flags |= SN_BE_ASSIGNED;
2039 }
2040
Willy Tarreau06619262006-12-17 08:37:22 +01002041 /* has the request been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01002042 if (txn->flags & TX_CLDENY) {
Willy Tarreau06619262006-12-17 08:37:22 +01002043 /* no need to go further */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002044 txn->status = 403;
Willy Tarreau06619262006-12-17 08:37:22 +01002045 /* let's log the request time */
Willy Tarreau70089872008-06-13 21:12:51 +02002046 t->logs.tv_request = now;
Willy Tarreau80587432006-12-24 17:47:20 +01002047 client_retnclose(t, error_message(t, HTTP_ERR_403));
Willy Tarreau06619262006-12-17 08:37:22 +01002048 goto return_prx_cond;
2049 }
2050
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002051 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002052 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002053 !(t->flags & SN_CONN_CLOSED)) {
2054 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002055 int cur_idx, old_idx, delta, val;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002056 struct hdr_idx_elem *cur_hdr;
Willy Tarreau06619262006-12-17 08:37:22 +01002057
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002058 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002059 old_idx = 0;
2060
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002061 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2062 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002063 cur_ptr = cur_next;
2064 cur_end = cur_ptr + cur_hdr->len;
2065 cur_next = cur_end + cur_hdr->cr + 1;
2066
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002067 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2068 if (val) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002069 /* 3 possibilities :
2070 * - we have already set Connection: close,
2071 * so we remove this line.
2072 * - we have not yet set Connection: close,
2073 * but this line indicates close. We leave
2074 * it untouched and set the flag.
2075 * - we have not yet set Connection: close,
2076 * and this line indicates non-close. We
2077 * replace it.
2078 */
2079 if (t->flags & SN_CONN_CLOSED) {
2080 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002081 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002082 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002083 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2084 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002085 cur_hdr->len = 0;
2086 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01002087 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2088 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2089 "close", 5);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002090 cur_next += delta;
2091 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002092 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002093 }
2094 t->flags |= SN_CONN_CLOSED;
2095 }
2096 }
2097 old_idx = cur_idx;
2098 }
Willy Tarreauf2f0ee82007-03-30 12:02:43 +02002099 }
2100 /* add request headers from the rule sets in the same order */
2101 for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) {
2102 if (unlikely(http_header_add_tail(req,
2103 &txn->req,
2104 &txn->hdr_idx,
2105 rule_set->req_add[cur_idx])) < 0)
2106 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002107 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002109 /* check if stats URI was requested, and if an auth is needed */
Willy Tarreau0214c3a2007-01-07 13:47:30 +01002110 if (rule_set->uri_auth != NULL &&
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002111 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
Willy Tarreaub2513902006-12-17 14:52:38 +01002112 /* we have to check the URI and auth for this request */
2113 if (stats_check_uri_auth(t, rule_set))
2114 return 1;
2115 }
2116
Willy Tarreau55ea7572007-06-17 19:56:27 +02002117 /* now check whether we have some switching rules for this request */
2118 if (!(t->flags & SN_BE_ASSIGNED)) {
2119 struct switching_rule *rule;
2120
2121 list_for_each_entry(rule, &cur_proxy->switching_rules, list) {
2122 int ret;
2123
2124 ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002125
2126 ret = acl_pass(ret);
Willy Tarreaua8cfa342008-07-09 11:23:31 +02002127 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreau55ea7572007-06-17 19:56:27 +02002128 ret = !ret;
2129
2130 if (ret) {
2131 t->be = rule->be.backend;
2132 t->be->beconn++;
2133 if (t->be->beconn > t->be->beconn_max)
2134 t->be->beconn_max = t->be->beconn;
2135 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002136
2137 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002138 t->rep->rto = t->req->wto = t->be->timeout.server;
2139 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002140 t->conn_retries = t->be->conn_retries;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002141 t->flags |= SN_BE_ASSIGNED;
2142 break;
2143 }
2144 }
2145 }
2146
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002147 if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) {
2148 /* No backend was set, but there was a default
2149 * backend set in the frontend, so we use it and
2150 * loop again.
2151 */
2152 t->be = cur_proxy->defbe.be;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002153 t->be->beconn++;
2154 if (t->be->beconn > t->be->beconn_max)
2155 t->be->beconn_max = t->be->beconn;
2156 t->be->cum_beconn++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002157
2158 /* assign new parameters to the session from the new backend */
Willy Tarreaud7c30f92007-12-03 01:38:36 +01002159 t->rep->rto = t->req->wto = t->be->timeout.server;
2160 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002161 t->conn_retries = t->be->conn_retries;
Willy Tarreau5fdfb912007-01-01 23:11:07 +01002162 t->flags |= SN_BE_ASSIGNED;
2163 }
2164 } while (t->be != cur_proxy); /* we loop only if t->be has changed */
Willy Tarreau2a324282006-12-05 00:05:46 +01002165
Willy Tarreau58f10d72006-12-04 02:26:12 +01002166
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002167 if (!(t->flags & SN_BE_ASSIGNED)) {
2168 /* To ensure correct connection accounting on
2169 * the backend, we count the connection for the
2170 * one managing the queue.
2171 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002172 t->be->beconn++;
2173 if (t->be->beconn > t->be->beconn_max)
2174 t->be->beconn_max = t->be->beconn;
2175 t->be->cum_beconn++;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002176 t->flags |= SN_BE_ASSIGNED;
2177 }
2178
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002179 /*
2180 * Right now, we know that we have processed the entire headers
Willy Tarreau2a324282006-12-05 00:05:46 +01002181 * and that unwanted requests have been filtered out. We can do
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002182 * whatever we want with the remaining request. Also, now we
Willy Tarreau830ff452006-12-17 19:31:23 +01002183 * may have separate values for ->fe, ->be.
Willy Tarreau2a324282006-12-05 00:05:46 +01002184 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002185
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002186 /*
2187 * If HTTP PROXY is set we simply get remote server address
2188 * parsing incoming request.
2189 */
2190 if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) {
2191 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr);
2192 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002193
Willy Tarreau2a324282006-12-05 00:05:46 +01002194 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002195 * 7: the appsession cookie was looked up very early in 1.2,
Willy Tarreau06619262006-12-17 08:37:22 +01002196 * so let's do the same now.
2197 */
2198
2199 /* It needs to look into the URI */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002200 if (t->be->appsession_name) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002201 get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l);
Willy Tarreau06619262006-12-17 08:37:22 +01002202 }
2203
2204
2205 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002206 * 8: Now we can work with the cookies.
Willy Tarreau2a324282006-12-05 00:05:46 +01002207 * Note that doing so might move headers in the request, but
2208 * the fields will stay coherent and the URI will not move.
Willy Tarreau06619262006-12-17 08:37:22 +01002209 * This should only be performed in the backend.
Willy Tarreau2a324282006-12-05 00:05:46 +01002210 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01002211 if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name)
2212 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
Willy Tarreau2a324282006-12-05 00:05:46 +01002213 manage_client_side_cookies(t, req);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002214
Willy Tarreau58f10d72006-12-04 02:26:12 +01002215
Willy Tarreau2a324282006-12-05 00:05:46 +01002216 /*
Willy Tarreaubb046ac2007-03-03 19:17:03 +01002217 * 9: add X-Forwarded-For if either the frontend or the backend
2218 * asks for it.
Willy Tarreau2a324282006-12-05 00:05:46 +01002219 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002220 if ((t->fe->options | t->be->options) & PR_O_FWDFOR) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002221 if (t->cli_addr.ss_family == AF_INET) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002222 /* Add an X-Forwarded-For header unless the source IP is
2223 * in the 'except' network range.
2224 */
2225 if ((!t->fe->except_mask.s_addr ||
2226 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr)
2227 != t->fe->except_net.s_addr) &&
2228 (!t->be->except_mask.s_addr ||
2229 (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr)
2230 != t->be->except_net.s_addr)) {
2231 int len;
2232 unsigned char *pn;
2233 pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr;
Willy Tarreau45e73e32006-12-17 00:05:15 +01002234
Ross Westaf72a1d2008-08-03 10:51:45 +02002235 /* Note: we rely on the backend to get the header name to be used for
2236 * x-forwarded-for, because the header is really meant for the backends.
2237 * However, if the backend did not specify any option, we have to rely
2238 * on the frontend's header name.
2239 */
2240 if (t->be->fwdfor_hdr_len) {
2241 len = t->be->fwdfor_hdr_len;
2242 memcpy(trash, t->be->fwdfor_hdr_name, len);
2243 } else {
2244 len = t->fe->fwdfor_hdr_len;
2245 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2246 }
2247 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002248
Ross Westaf72a1d2008-08-03 10:51:45 +02002249 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002250 &txn->hdr_idx, trash, len)) < 0)
2251 goto return_bad_req;
2252 }
Willy Tarreau2a324282006-12-05 00:05:46 +01002253 }
2254 else if (t->cli_addr.ss_family == AF_INET6) {
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002255 /* FIXME: for the sake of completeness, we should also support
2256 * 'except' here, although it is mostly useless in this case.
2257 */
Willy Tarreau2a324282006-12-05 00:05:46 +01002258 int len;
2259 char pn[INET6_ADDRSTRLEN];
2260 inet_ntop(AF_INET6,
2261 (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr,
2262 pn, sizeof(pn));
Ross Westaf72a1d2008-08-03 10:51:45 +02002263
2264 /* Note: we rely on the backend to get the header name to be used for
2265 * x-forwarded-for, because the header is really meant for the backends.
2266 * However, if the backend did not specify any option, we have to rely
2267 * on the frontend's header name.
2268 */
2269 if (t->be->fwdfor_hdr_len) {
2270 len = t->be->fwdfor_hdr_len;
2271 memcpy(trash, t->be->fwdfor_hdr_name, len);
2272 } else {
2273 len = t->fe->fwdfor_hdr_len;
2274 memcpy(trash, t->fe->fwdfor_hdr_name, len);
2275 }
2276 len += sprintf(trash + len, ": %s", pn);
2277
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002278 if (unlikely(http_header_add_tail2(req, &txn->req,
2279 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002280 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002281 }
2282 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002283
Willy Tarreau2a324282006-12-05 00:05:46 +01002284 /*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002285 * 10: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02002286 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaub2513902006-12-17 14:52:38 +01002287 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02002288 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01002289 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02002290 if ((unlikely(msg->sl.rq.v_l != 8) ||
2291 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2292 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002293 "Connection: close", 17)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002294 goto return_bad_req;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002295 t->flags |= SN_CONN_CLOSED;
Willy Tarreaue15d9132006-12-14 22:26:42 +01002296 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002297 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2298 * If not assigned, perhaps we are balancing on url_param, but this is a
2299 * POST; and the parameters are in the body, maybe scan there to find our server.
2300 * (unless headers overflowed the buffer?)
2301 */
2302 if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2303 t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL &&
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002304 t->be->url_param_post_limit != 0 && req->l < BUFSIZE &&
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002305 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2306 /* are there enough bytes here? total == l || r || rlim ?
2307 * len is unsigned, but eoh is int,
2308 * how many bytes of body have we received?
2309 * eoh is the first empty line of the header
2310 */
2311 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
Willy Tarreaufb0528b2008-08-11 00:21:56 +02002312 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 +02002313
2314 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2315 * We can't assume responsibility for the server's decision,
2316 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2317 * We also can't change our mind later, about which server to choose, so round robin.
2318 */
2319 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2320 struct hdr_ctx ctx;
2321 ctx.idx = 0;
2322 /* Expect is allowed in 1.1, look for it */
2323 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2324 if (ctx.idx != 0 &&
Willy Tarreauadfb8562008-08-11 15:24:42 +02002325 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002326 /* We can't reliablly stall and wait for data, because of
2327 * .NET clients that don't conform to rfc2616; so, no need for
2328 * the next block to check length expectations.
2329 * We could send 100 status back to the client, but then we need to
2330 * re-write headers, and send the message. And this isn't the right
2331 * place for that action.
2332 * TODO: support Expect elsewhere and delete this block.
2333 */
2334 goto end_check_maybe_wait_for_body;
2335 }
Willy Tarreauadfb8562008-08-11 15:24:42 +02002336
2337 if (likely(len > t->be->url_param_post_limit)) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002338 /* nothing to do, we got enough */
2339 } else {
2340 /* limit implies we are supposed to need this many bytes
2341 * to find the parameter. Let's see how many bytes we can wait for.
2342 */
2343 long long hint = len;
2344 struct hdr_ctx ctx;
2345 ctx.idx = 0;
2346 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002347 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0)
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002348 t->analysis |= AN_REQ_HTTP_BODY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002349 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002350 ctx.idx = 0;
2351 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2352 /* now if we have a length, we'll take the hint */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002353 if (ctx.idx) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002354 /* We have Content-Length */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002355 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002356 hint = 0; /* parse failure, untrusted client */
2357 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02002358 if (hint > 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002359 msg->hdr_content_len = hint;
2360 else
2361 hint = 0; /* bad client, sent negative length */
2362 }
2363 }
2364 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002365 if (t->be->url_param_post_limit < hint)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002366 hint = t->be->url_param_post_limit;
2367 /* now do we really need to buffer more data? */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002368 if (len < hint)
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002369 t->analysis |= AN_REQ_HTTP_BODY;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002370 /* else... There are no body bytes to wait for */
2371 }
2372 }
2373 }
2374 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002375
Willy Tarreau2a324282006-12-05 00:05:46 +01002376 /*************************************************************
2377 * OK, that's finished for the headers. We have done what we *
2378 * could. Let's switch to the DATA state. *
2379 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002380
Willy Tarreauadfb8562008-08-11 15:24:42 +02002381 if (!(t->analysis & AN_REQ_ANY))
2382 req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002383
Willy Tarreauadfb8562008-08-11 15:24:42 +02002384 req->rlim = req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02002385 t->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002386
Willy Tarreauadfb8562008-08-11 15:24:42 +02002387 /* This is a bit tricky. We don't want the client timeout to strike
2388 * while intially waiting for the server to respond. So we rely
2389 * entirely on the server timeout to ensure that we cannot wait
2390 * indefinitely.
2391 */
2392 if (t->fe->timeout.client || req->l >= req->rlim - req->data)
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002393 req->rex = TICK_ETERNITY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002394 else if (t->analysis || !t->be->timeout.server || rep->flags & BF_MAY_FORWARD)
2395 req->rex = tick_add(now_ms, t->fe->timeout.client);
2396 else
2397 req->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002398
Willy Tarreau1fa31262007-12-03 00:36:16 +01002399 /* When a connection is tarpitted, we use the tarpit timeout,
2400 * which may be the same as the connect timeout if unspecified.
2401 * If unset, then set it to zero because we really want it to
2402 * eventually expire.
Willy Tarreau2a324282006-12-05 00:05:46 +01002403 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002404 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002405 t->req->l = 0;
2406 /* flush the request so that we can drop the connection early
2407 * if the client closes first.
2408 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002409 req->cex = tick_add_ifset(now_ms, t->be->timeout.tarpit);
2410 if (!req->cex)
2411 req->cex = now_ms;
Willy Tarreau2a324282006-12-05 00:05:46 +01002412 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002413
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002414 /* OK let's go on with the BODY now */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002415 goto end_of_headers;
Willy Tarreau06619262006-12-17 08:37:22 +01002416
2417 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01002418 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01002419 txn->status = 400;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002420 t->analysis &= ~AN_REQ_ANY;
Willy Tarreau80587432006-12-24 17:47:20 +01002421 client_retnclose(t, error_message(t, HTTP_ERR_400));
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01002422 t->fe->failed_req++;
Willy Tarreau06619262006-12-17 08:37:22 +01002423 return_prx_cond:
2424 if (!(t->flags & SN_ERR_MASK))
2425 t->flags |= SN_ERR_PRXCOND;
2426 if (!(t->flags & SN_FINST_MASK))
2427 t->flags |= SN_FINST_R;
2428 return 1;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002429 end_of_headers:
2430 ; // to make gcc happy
2431 }
2432
2433 if (t->analysis & AN_REQ_HTTP_BODY) {
2434 /* We have to parse the HTTP request body to find any required data.
2435 * "balance url_param check_post" should have been the only way to get
2436 * into this. We were brought here after HTTP header analysis, so all
2437 * related structures are ready.
2438 */
2439 struct http_msg *msg = &t->txn.req;
2440 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2441 long long limit = t->be->url_param_post_limit;
2442 struct hdr_ctx ctx;
2443
2444 ctx.idx = 0;
2445
2446 /* now if we have a length, we'll take the hint */
2447 http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx);
2448 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2449 unsigned int chunk = 0;
2450 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2451 char c = msg->sol[body];
2452 if (ishex(c)) {
2453 unsigned int hex = toupper(c) - '0';
2454 if (hex > 9)
2455 hex -= 'A' - '9' - 1;
2456 chunk = (chunk << 4) | hex;
2457 } else
2458 break;
2459 body++;
2460 }
2461 if (body + 2 >= req->l) /* we want CRLF too */
2462 goto http_body_incomplete; /* end of buffer? data missing! */
2463
2464 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
2465 goto http_body_incomplete; /* chunked encoding len ends with CRLF, and we don't have it yet */
2466
2467 body += 2; // skip CRLF
2468
2469 /* if we support more then one chunk here, we have to do it again when assigning server
2470 * 1. how much entity data do we have? new var
2471 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2472 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2473 */
2474
2475 if (chunk < limit)
2476 limit = chunk; /* only reading one chunk */
2477 } else {
2478 if (msg->hdr_content_len < limit)
2479 limit = msg->hdr_content_len;
2480 }
2481
2482 if (req->l - body >= limit) {
2483 /* we have enough bytes ! */
2484 t->logs.tv_request = now; /* update the request timer to reflect full request */
2485 t->analysis &= ~AN_REQ_HTTP_BODY;
2486 req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD;
2487
2488 /* This is a bit tricky. We don't want the client timeout to strike
2489 * while intially waiting for the server to respond. So we rely
2490 * entirely on the server timeout to ensure that we cannot wait
2491 * indefinitely.
2492 */
2493 if (t->fe->timeout.client || req->l >= req->rlim - req->data)
2494 req->rex = TICK_ETERNITY;
2495 else if (t->analysis || !t->be->timeout.server || rep->flags & BF_MAY_FORWARD)
2496 req->rex = tick_add(now_ms, t->fe->timeout.client);
2497 else
2498 req->rex = TICK_ETERNITY;
2499 return 1;
2500 }
2501
2502 http_body_incomplete:
2503 if (req->l >= req->rlim - req->data ||
2504 req->flags & (BF_READ_ERROR | BF_READ_NULL) ||
2505 tick_is_expired(req->rex, now_ms)) {
2506 /* The situation will not evolve, so let's give up on the analysis. */
2507 t->logs.tv_request = now; /* update the request timer to reflect full request */
2508 t->analysis &= ~AN_REQ_HTTP_BODY;
2509 req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD;
2510
2511 /* This is a bit tricky. We don't want the client timeout to strike
2512 * while intially waiting for the server to respond. So we rely
2513 * entirely on the server timeout to ensure that we cannot wait
2514 * indefinitely.
2515 */
2516 if (t->fe->timeout.client || req->l >= req->rlim - req->data)
2517 req->rex = TICK_ETERNITY;
2518 else if (t->analysis || !t->be->timeout.server || rep->flags & BF_MAY_FORWARD)
2519 req->rex = tick_add(now_ms, t->fe->timeout.client);
2520 else
2521 req->rex = TICK_ETERNITY;
2522 return 1;
2523 }
2524
2525 if (!tick_isset(req->rex)) {
2526 EV_FD_COND_S(t->cli_fd, DIR_RD);
2527 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
2528 }
2529 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002530 }
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002531
2532 if (c == CL_STDATA) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002533 /* FIXME: this error handling is partly buggy because we always report
2534 * a 'DATA' phase while we don't know if the server was in IDLE, CONN
2535 * or HEADER phase. BTW, it's not logical to expire the client while
2536 * we're waiting for the server to connect.
2537 */
2538 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002539 if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002540 buffer_shutr_done(req);
2541 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002542 fd_delete(t->cli_fd);
2543 t->cli_state = CL_STCLOSE;
2544 if (!(t->flags & SN_ERR_MASK))
2545 t->flags |= SN_ERR_CLICL;
2546 if (!(t->flags & SN_FINST_MASK)) {
2547 if (t->pend_pos)
2548 t->flags |= SN_FINST_Q;
2549 else if (s == SV_STCONN)
2550 t->flags |= SN_FINST_C;
2551 else
2552 t->flags |= SN_FINST_D;
2553 }
2554 return 1;
2555 }
2556 /* last read, or end of server write */
Willy Tarreau718f0ef2008-08-10 16:21:32 +02002557 else if (req->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002558 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002559 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002560 t->cli_state = CL_STSHUTR;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002561 /* we must allow immediate connection if not already done */
2562 if (!req->flags & (BF_MAY_CONNECT | BF_MAY_FORWARD))
2563 req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002564 return 1;
2565 }
2566 /* last server read and buffer empty */
Willy Tarreau718f0ef2008-08-10 16:21:32 +02002567 else if (rep->flags & BF_SHUTR_STATUS && rep->l == 0) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002568 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002569 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002570 shutdown(t->cli_fd, SHUT_WR);
2571 /* We must ensure that the read part is still alive when switching
2572 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002573 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002574 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002575 t->cli_state = CL_STSHUTW;
2576 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
2577 return 1;
2578 }
2579 /* read timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002580 else if (tick_is_expired(req->rex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002581 EV_FD_CLR(t->cli_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02002582 buffer_shutr(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002583 t->cli_state = CL_STSHUTR;
2584 if (!(t->flags & SN_ERR_MASK))
2585 t->flags |= SN_ERR_CLITO;
2586 if (!(t->flags & SN_FINST_MASK)) {
2587 if (t->pend_pos)
2588 t->flags |= SN_FINST_Q;
2589 else if (s == SV_STCONN)
2590 t->flags |= SN_FINST_C;
2591 else
2592 t->flags |= SN_FINST_D;
2593 }
2594 return 1;
2595 }
2596 /* write timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002597 else if (tick_is_expired(rep->wex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02002598 EV_FD_CLR(t->cli_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002599 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002600 shutdown(t->cli_fd, SHUT_WR);
2601 /* We must ensure that the read part is still alive when switching
2602 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02002603 EV_FD_SET(t->cli_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002604 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002605
2606 t->cli_state = CL_STSHUTW;
2607 if (!(t->flags & SN_ERR_MASK))
2608 t->flags |= SN_ERR_CLITO;
2609 if (!(t->flags & SN_FINST_MASK)) {
2610 if (t->pend_pos)
2611 t->flags |= SN_FINST_Q;
2612 else if (s == SV_STCONN)
2613 t->flags |= SN_FINST_C;
2614 else
2615 t->flags |= SN_FINST_D;
2616 }
2617 return 1;
2618 }
2619
2620 if (req->l >= req->rlim - req->data) {
2621 /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02002622 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002623 /* stop reading until we get some space */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002624 req->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002625 }
2626 } else {
2627 /* there's still some space in the buffer */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002628 if (!tick_isset(req->rex)) {
2629 EV_FD_COND_S(t->cli_fd, DIR_RD);
2630 /* This is a bit tricky. We don't want the client timeout to strike
2631 * while intially waiting for the server to respond. So we rely
2632 * entirely on the server timeout to ensure that we cannot wait
2633 * indefinitely.
2634 */
2635 if (t->fe->timeout.client)
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002636 req->rex = TICK_ETERNITY;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002637 else if (t->analysis || !t->be->timeout.server || rep->flags & BF_MAY_FORWARD)
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002638 req->rex = tick_add(now_ms, t->fe->timeout.client);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002639 else
2640 req->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002641 }
2642 }
2643
Willy Tarreau718f0ef2008-08-10 16:21:32 +02002644 /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */
2645 if ((rep->l == 0) || !(rep->flags & BF_MAY_FORWARD)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002646 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2647 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002648 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002649 }
2650 } else {
2651 /* buffer not empty */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002652 if (!tick_isset(rep->wex)) {
2653 EV_FD_COND_S(t->cli_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02002654 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002655 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002656 if (tick_isset(rep->wex)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002657 /* FIXME: to prevent the client from expiring read timeouts during writes,
2658 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02002659 req->rex = rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002660 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002661 }
2662 }
2663 return 0; /* other cases change nothing */
2664 }
2665 else if (c == CL_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002666 if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002667 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002668 fd_delete(t->cli_fd);
2669 t->cli_state = CL_STCLOSE;
2670 if (!(t->flags & SN_ERR_MASK))
2671 t->flags |= SN_ERR_CLICL;
2672 if (!(t->flags & SN_FINST_MASK)) {
2673 if (t->pend_pos)
2674 t->flags |= SN_FINST_Q;
2675 else if (s == SV_STCONN)
2676 t->flags |= SN_FINST_C;
2677 else
2678 t->flags |= SN_FINST_D;
2679 }
2680 return 1;
2681 }
Willy Tarreau718f0ef2008-08-10 16:21:32 +02002682 else if ((rep->flags & BF_SHUTR_STATUS) && (rep->l == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002683 && !(t->flags & SN_SELF_GEN)) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002684 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002685 fd_delete(t->cli_fd);
2686 t->cli_state = CL_STCLOSE;
2687 return 1;
2688 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002689 else if (tick_is_expired(rep->wex, now_ms)) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002690 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002691 fd_delete(t->cli_fd);
2692 t->cli_state = CL_STCLOSE;
2693 if (!(t->flags & SN_ERR_MASK))
2694 t->flags |= SN_ERR_CLITO;
2695 if (!(t->flags & SN_FINST_MASK)) {
2696 if (t->pend_pos)
2697 t->flags |= SN_FINST_Q;
2698 else if (s == SV_STCONN)
2699 t->flags |= SN_FINST_C;
2700 else
2701 t->flags |= SN_FINST_D;
2702 }
2703 return 1;
2704 }
2705
2706 if (t->flags & SN_SELF_GEN) {
2707 produce_content(t);
2708 if (rep->l == 0) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002709 buffer_shutw_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002710 fd_delete(t->cli_fd);
2711 t->cli_state = CL_STCLOSE;
2712 return 1;
2713 }
2714 }
2715
Willy Tarreau718f0ef2008-08-10 16:21:32 +02002716 if ((rep->l == 0) || !(rep->flags & BF_MAY_FORWARD)) {
Willy Tarreau66319382007-04-08 17:17:37 +02002717 if (EV_FD_COND_C(t->cli_fd, DIR_WR)) {
2718 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002719 rep->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002720 }
2721 } else {
2722 /* buffer not empty */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002723 if (!tick_isset(rep->wex)) {
2724 EV_FD_COND_S(t->cli_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02002725 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002726 rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002727 }
2728 }
2729 return 0;
2730 }
2731 else if (c == CL_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002732 if (req->flags & BF_READ_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002733 buffer_shutr_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002734 fd_delete(t->cli_fd);
2735 t->cli_state = CL_STCLOSE;
2736 if (!(t->flags & SN_ERR_MASK))
2737 t->flags |= SN_ERR_CLICL;
2738 if (!(t->flags & SN_FINST_MASK)) {
2739 if (t->pend_pos)
2740 t->flags |= SN_FINST_Q;
2741 else if (s == SV_STCONN)
2742 t->flags |= SN_FINST_C;
2743 else
2744 t->flags |= SN_FINST_D;
2745 }
2746 return 1;
2747 }
Willy Tarreau718f0ef2008-08-10 16:21:32 +02002748 else if (req->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002749 buffer_shutr_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002750 fd_delete(t->cli_fd);
2751 t->cli_state = CL_STCLOSE;
2752 return 1;
2753 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002754 else if (tick_is_expired(req->rex, now_ms)) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02002755 buffer_shutr_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002756 fd_delete(t->cli_fd);
2757 t->cli_state = CL_STCLOSE;
2758 if (!(t->flags & SN_ERR_MASK))
2759 t->flags |= SN_ERR_CLITO;
2760 if (!(t->flags & SN_FINST_MASK)) {
2761 if (t->pend_pos)
2762 t->flags |= SN_FINST_Q;
2763 else if (s == SV_STCONN)
2764 t->flags |= SN_FINST_C;
2765 else
2766 t->flags |= SN_FINST_D;
2767 }
2768 return 1;
2769 }
2770 else if (req->l >= req->rlim - req->data) {
2771 /* no room to read more data */
2772
2773 /* FIXME-20050705: is it possible for a client to maintain a session
2774 * after the timeout by sending more data after it receives a close ?
2775 */
2776
Willy Tarreau66319382007-04-08 17:17:37 +02002777 if (EV_FD_COND_C(t->cli_fd, DIR_RD)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002778 /* stop reading until we get some space */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002779 req->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002780 }
2781 } else {
2782 /* there's still some space in the buffer */
Willy Tarreauadfb8562008-08-11 15:24:42 +02002783 if (!tick_isset(req->rex)) {
2784 EV_FD_COND_S(t->cli_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002785 req->rex = tick_add_ifset(now_ms, t->fe->timeout.client);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002786 }
2787 }
2788 return 0;
2789 }
2790 else { /* CL_STCLOSE: nothing to do */
2791 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
2792 int len;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02002793 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 +02002794 write(1, trash, len);
2795 }
2796 return 0;
2797 }
2798 return 0;
2799}
2800
2801
2802/*
2803 * manages the server FSM and its socket. It returns 1 if a state has changed
2804 * (and a resync may be needed), 0 else.
2805 */
2806int process_srv(struct session *t)
2807{
2808 int s = t->srv_state;
Willy Tarreau3d300592007-03-18 18:34:41 +01002809 struct http_txn *txn = &t->txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002810 struct buffer *req = t->req;
2811 struct buffer *rep = t->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002812 int conn_err;
2813
Willy Tarreau6468d922008-08-03 19:15:35 +02002814 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 +02002815 cli_stnames[t->cli_state], srv_stnames[t->srv_state],
Willy Tarreau6468d922008-08-03 19:15:35 +02002816 EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR),
2817 rep->rex, req->wex);
Willy Tarreauee991362007-05-14 14:37:50 +02002818
Willy Tarreaubaaee002006-06-26 02:48:02 +02002819 if (s == SV_STIDLE) {
Willy Tarreaudc0a6a02008-08-03 20:38:13 +02002820 if ((rep->flags & BF_SHUTW_STATUS) ||
Willy Tarreau6468d922008-08-03 19:15:35 +02002821 ((req->flags & BF_SHUTR_STATUS) &&
2822 (req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002823 req->cex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002824 if (t->pend_pos)
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002825 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002826 /* note that this must not return any error because it would be able to
2827 * overwrite the client_retnclose() output.
2828 */
Willy Tarreau3d300592007-03-18 18:34:41 +01002829 if (txn->flags & TX_CLTARPIT)
Willy Tarreau0f772532006-12-23 20:51:41 +01002830 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL);
Willy Tarreau08fa2e32006-09-03 10:47:37 +02002831 else
Willy Tarreau0f772532006-12-23 20:51:41 +01002832 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 +02002833
2834 return 1;
2835 }
Willy Tarreaudc0a6a02008-08-03 20:38:13 +02002836 else if (req->flags & BF_MAY_CONNECT) {
2837 /* the client allows the server to connect */
Willy Tarreau3d300592007-03-18 18:34:41 +01002838 if (txn->flags & TX_CLTARPIT) {
Willy Tarreaub8750a82006-09-03 09:56:00 +02002839 /* This connection is being tarpitted. The CLIENT side has
2840 * already set the connect expiration date to the right
2841 * timeout. We just have to check that it has not expired.
2842 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002843 if (!tick_is_expired(req->cex, now_ms))
Willy Tarreaub8750a82006-09-03 09:56:00 +02002844 return 0;
2845
2846 /* We will set the queue timer to the time spent, just for
2847 * logging purposes. We fake a 500 server error, so that the
2848 * attacker will not suspect his connection has been tarpitted.
2849 * It will not cause trouble to the logs because we can exclude
2850 * the tarpitted connections by filtering on the 'PT' status flags.
2851 */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002852 req->cex = TICK_ETERNITY;
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002853 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaub8750a82006-09-03 09:56:00 +02002854 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T,
Willy Tarreau80587432006-12-24 17:47:20 +01002855 500, error_message(t, HTTP_ERR_500));
Willy Tarreaub8750a82006-09-03 09:56:00 +02002856 return 1;
2857 }
2858
Willy Tarreaubaaee002006-06-26 02:48:02 +02002859 /* Right now, we will need to create a connection to the server.
2860 * We might already have tried, and got a connection pending, in
2861 * which case we will not do anything till it's pending. It's up
2862 * to any other session to release it and wake us up again.
2863 */
2864 if (t->pend_pos) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002865 if (!tick_is_expired(req->cex, now_ms)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002866 return 0;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002867 } else {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002868 /* we've been waiting too long here */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002869 req->cex = TICK_ETERNITY;
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002870 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002871 srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau80587432006-12-24 17:47:20 +01002872 503, error_message(t, HTTP_ERR_503));
Willy Tarreaubaaee002006-06-26 02:48:02 +02002873 if (t->srv)
2874 t->srv->failed_conns++;
Willy Tarreau50fd1e12007-12-10 15:25:35 +01002875 t->be->failed_conns++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002876 return 1;
2877 }
2878 }
2879
2880 do {
2881 /* first, get a connection */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002882 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2883 t->flags |= SN_REDIRECTABLE;
2884
Willy Tarreaubaaee002006-06-26 02:48:02 +02002885 if (srv_redispatch_connect(t))
2886 return t->srv_state != SV_STIDLE;
2887
Willy Tarreau21d2af32008-02-14 20:25:24 +01002888 if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) {
2889 /* Server supporting redirection and it is possible.
2890 * Invalid requests are reported as such. It concerns all
2891 * the largest ones.
2892 */
2893 struct chunk rdr;
2894 char *path;
2895 int len;
2896
2897 /* 1: create the response header */
2898 rdr.len = strlen(HTTP_302);
2899 rdr.str = trash;
2900 memcpy(rdr.str, HTTP_302, rdr.len);
2901
2902 /* 2: add the server's prefix */
2903 if (rdr.len + t->srv->rdr_len > sizeof(trash))
2904 goto cancel_redir;
2905
2906 memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len);
2907 rdr.len += t->srv->rdr_len;
2908
2909 /* 3: add the request URI */
2910 path = http_get_path(txn);
2911 if (!path)
2912 goto cancel_redir;
2913 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2914 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
2915 goto cancel_redir;
2916
2917 memcpy(rdr.str + rdr.len, path, len);
2918 rdr.len += len;
2919 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2920 rdr.len += 4;
2921
2922 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
2923 /* FIXME: we should increase a counter of redirects per server and per backend. */
2924 if (t->srv)
2925 t->srv->cum_sess++;
2926 return 1;
2927 cancel_redir:
2928 txn->status = 400;
2929 t->fe->failed_req++;
2930 srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C,
2931 400, error_message(t, HTTP_ERR_400));
2932 return 1;
2933 }
2934
Willy Tarreaubaaee002006-06-26 02:48:02 +02002935 /* try to (re-)connect to the server, and fail if we expire the
2936 * number of retries.
2937 */
2938 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02002939 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002940 return t->srv_state != SV_STIDLE;
2941 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002942 } while (1);
2943 }
2944 }
2945 else if (s == SV_STCONN) { /* connection in progress */
Willy Tarreau6468d922008-08-03 19:15:35 +02002946 if ((rep->flags & BF_SHUTW_STATUS) ||
2947 ((req->flags & BF_SHUTR_STATUS) &&
2948 ((req->l == 0 && !(req->flags & BF_WRITE_STATUS)) ||
Willy Tarreauc9b654b2007-05-08 14:46:53 +02002949 t->be->options & PR_O_ABRT_CLOSE))) { /* give up */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002950 req->cex = TICK_ETERNITY;
Willy Tarreauf899b942008-03-28 18:09:38 +01002951 if (!(t->flags & SN_CONN_TAR)) {
2952 /* if we are in turn-around, we have already closed the FD */
2953 fd_delete(t->srv_fd);
2954 if (t->srv) {
2955 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002956 sess_change_server(t, NULL);
Willy Tarreauf899b942008-03-28 18:09:38 +01002957 }
Willy Tarreau51406232008-03-10 22:04:20 +01002958 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002959
2960 /* note that this must not return any error because it would be able to
2961 * overwrite the client_retnclose() output.
2962 */
Willy Tarreau0f772532006-12-23 20:51:41 +01002963 srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002964 return 1;
2965 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002966 if (!(req->flags & BF_WRITE_STATUS) && !tick_is_expired(req->cex, now_ms)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002967 return 0; /* nothing changed */
2968 }
Willy Tarreau0f9f5052006-07-29 17:39:25 +02002969 else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002970 /* timeout, asynchronous connect error or first write error */
2971 //fprintf(stderr,"2: c=%d, s=%d\n", c, s);
2972
Willy Tarreau541b5c22008-01-06 23:34:21 +01002973 if (t->flags & SN_CONN_TAR) {
2974 /* We are doing a turn-around waiting for a new connection attempt. */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02002975 if (!tick_is_expired(req->cex, now_ms))
Willy Tarreau541b5c22008-01-06 23:34:21 +01002976 return 0;
2977 t->flags &= ~SN_CONN_TAR;
2978 }
2979 else {
2980 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01002981 if (t->srv) {
Willy Tarreau541b5c22008-01-06 23:34:21 +01002982 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002983 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01002984 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002985
Willy Tarreau541b5c22008-01-06 23:34:21 +01002986 if (!(req->flags & BF_WRITE_STATUS))
2987 conn_err = SN_ERR_SRVTO; // it was a connect timeout.
2988 else
2989 conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error.
2990
2991 /* ensure that we have enough retries left */
2992 if (srv_count_retry_down(t, conn_err))
2993 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002994
Willy Tarreau541b5c22008-01-06 23:34:21 +01002995 if (req->flags & BF_WRITE_ERROR) {
2996 /* we encountered an immediate connection error, and we
2997 * will have to retry connecting to the same server, most
2998 * likely leading to the same result. To avoid this, we
2999 * fake a connection timeout to retry after a turn-around
3000 * time of 1 second. We will wait in the previous if block.
3001 */
3002 t->flags |= SN_CONN_TAR;
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003003 req->cex = tick_add(now_ms, MS_TO_TICKS(1000));
Willy Tarreau541b5c22008-01-06 23:34:21 +01003004 return 0;
3005 }
3006 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003007
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003008 if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) {
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003009 /* We're on our last chance, and the REDISP option was specified.
3010 * We will ignore cookie and force to balance or use the dispatcher.
3011 */
3012 /* let's try to offer this slot to anybody */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003013 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003014 process_srv_queue(t->srv);
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003015
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01003016 /* it's left to the dispatcher to choose a server */
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003017 t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau7c669d72008-06-20 15:04:11 +02003018 t->prev_srv = t->srv;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003019
3020 /* first, get a connection */
3021 if (srv_redispatch_connect(t))
Willy Tarreau00559e72008-01-06 23:46:19 +01003022 return t->srv_state != SV_STCONN;
Krzysztof Piotr Oledzki626a19b2008-02-04 02:10:09 +01003023 } else {
3024 if (t->srv)
3025 t->srv->retries++;
3026 t->be->retries++;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003027 }
3028
Willy Tarreaubaaee002006-06-26 02:48:02 +02003029 do {
3030 /* Now we will try to either reconnect to the same server or
3031 * connect to another server. If the connection gets queued
3032 * because all servers are saturated, then we will go back to
3033 * the SV_STIDLE state.
3034 */
3035 if (srv_retryable_connect(t)) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003036 t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003037 return t->srv_state != SV_STCONN;
3038 }
3039
3040 /* we need to redispatch the connection to another server */
3041 if (srv_redispatch_connect(t))
3042 return t->srv_state != SV_STCONN;
3043 } while (1);
3044 }
3045 else { /* no error or write 0 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003046 t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003047
3048 //fprintf(stderr,"3: c=%d, s=%d\n", c, s);
3049 if (req->l == 0) /* nothing to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02003050 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003051 req->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003052 } else /* need the right to write */ {
Willy Tarreauf161a342007-04-08 16:59:42 +02003053 EV_FD_SET(t->srv_fd, DIR_WR);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003054 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003055 if (tick_isset(req->wex)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003056 /* FIXME: to prevent the server from expiring read timeouts during writes,
3057 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003058 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003059 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003060 }
3061
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003062 if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreauf161a342007-04-08 16:59:42 +02003063 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003064 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003065 t->srv_state = SV_STDATA;
Willy Tarreau718f0ef2008-08-10 16:21:32 +02003066 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003067 rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */
3068
3069 /* if the user wants to log as soon as possible, without counting
3070 bytes from the server, then this is the right moment. */
Willy Tarreau73de9892006-11-30 11:40:23 +01003071 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003072 t->logs.t_close = t->logs.t_connect; /* to get a valid end date */
Willy Tarreau42250582007-04-01 01:30:43 +02003073 tcp_sess_log(t);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003074 }
Willy Tarreau6d1a9882007-01-07 02:03:04 +01003075#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003076 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreau6d1a9882007-01-07 02:03:04 +01003077 /* TCP splicing supported by both FE and BE */
3078 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
3079 }
3080#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +02003081 }
3082 else {
3083 t->srv_state = SV_STHEADERS;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003084 rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003085 t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
3086 /* reset hdr_idx which was already initialized by the request.
3087 * right now, the http parser does it.
3088 * hdr_idx_init(&t->txn.hdr_idx);
3089 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02003090 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003091 req->cex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003092 return 1;
3093 }
3094 }
3095 else if (s == SV_STHEADERS) { /* receiving server headers */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003096 /*
3097 * Now parse the partial (or complete) lines.
3098 * We will check the response syntax, and also join multi-line
3099 * headers. An index of all the lines will be elaborated while
3100 * parsing.
3101 *
3102 * For the parsing, we use a 28 states FSM.
3103 *
3104 * Here is the information we currently have :
3105 * rep->data + req->som = beginning of response
3106 * rep->data + req->eoh = end of processed headers / start of current one
3107 * rep->data + req->eol = end of current header or line (LF or CRLF)
3108 * rep->lr = first non-visited byte
3109 * rep->r = end of data
3110 */
3111
3112 int cur_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003113 struct http_msg *msg = &txn->rsp;
3114 struct proxy *cur_proxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003115
Willy Tarreaua15645d2007-03-18 16:22:39 +01003116 if (likely(rep->lr < rep->r))
3117 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003118
Willy Tarreaua15645d2007-03-18 16:22:39 +01003119 /* 1: we might have to print this header in debug mode */
3120 if (unlikely((global.mode & MODE_DEBUG) &&
3121 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
3122 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
3123 char *eol, *sol;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003124
Willy Tarreaua15645d2007-03-18 16:22:39 +01003125 sol = rep->data + msg->som;
3126 eol = sol + msg->sl.rq.l;
3127 debug_hdr("srvrep", t, sol, eol);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003128
Willy Tarreaua15645d2007-03-18 16:22:39 +01003129 sol += hdr_idx_first_pos(&txn->hdr_idx);
3130 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003131
Willy Tarreaua15645d2007-03-18 16:22:39 +01003132 while (cur_idx) {
3133 eol = sol + txn->hdr_idx.v[cur_idx].len;
3134 debug_hdr("srvhdr", t, sol, eol);
3135 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3136 cur_idx = txn->hdr_idx.v[cur_idx].next;
3137 }
3138 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003139
Willy Tarreaubaaee002006-06-26 02:48:02 +02003140
Willy Tarreauadfb8562008-08-11 15:24:42 +02003141 if ((rep->l < rep->rlim - rep->data) && !tick_isset(rep->rex)) {
3142 EV_FD_COND_S(t->srv_fd, DIR_RD);
Willy Tarreauf161a342007-04-08 16:59:42 +02003143 /* fd in DIR_RD was disabled, perhaps because of a previous buffer
Willy Tarreaua15645d2007-03-18 16:22:39 +01003144 * full. We cannot loop here since stream_sock_read will disable it only if
3145 * rep->l == rlim-data
3146 */
Willy Tarreauce09c522008-08-11 10:35:07 +02003147 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003148 }
3149
3150
3151 /*
3152 * Now we quickly check if we have found a full valid response.
3153 * If not so, we check the FD and buffer states before leaving.
3154 * A full response is indicated by the fact that we have seen
3155 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
3156 * responses are checked first.
3157 *
3158 * Depending on whether the client is still there or not, we
3159 * may send an error response back or not. Note that normally
3160 * we should only check for HTTP status there, and check I/O
3161 * errors somewhere else.
3162 */
3163
3164 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
3165
3166 /* Invalid response, or read error or write error */
3167 if (unlikely((msg->msg_state == HTTP_MSG_ERROR) ||
3168 (req->flags & BF_WRITE_ERROR) ||
3169 (rep->flags & BF_READ_ERROR))) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003170 buffer_shutr_done(rep);
3171 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003172 fd_delete(t->srv_fd);
3173 if (t->srv) {
3174 t->srv->cur_sess--;
3175 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003176 sess_change_server(t, NULL);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003177 }
3178 t->be->failed_resp++;
3179 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003180 rep->flags |= BF_MAY_FORWARD;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003181 txn->status = 502;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003182 client_return(t, error_message(t, HTTP_ERR_502));
3183 if (!(t->flags & SN_ERR_MASK))
3184 t->flags |= SN_ERR_SRVCL;
3185 if (!(t->flags & SN_FINST_MASK))
3186 t->flags |= SN_FINST_H;
3187 /* We used to have a free connection slot. Since we'll never use it,
3188 * we have to inform the server that it may be used by another session.
3189 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003190 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003191 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003192
Willy Tarreaua15645d2007-03-18 16:22:39 +01003193 return 1;
3194 }
3195
3196 /* end of client write or end of server read.
3197 * since we are in header mode, if there's no space left for headers, we
3198 * won't be able to free more later, so the session will never terminate.
3199 */
Willy Tarreau6468d922008-08-03 19:15:35 +02003200 else if (unlikely(rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01003201 rep->l >= rep->rlim - rep->data)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003202 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003203 buffer_shutr_done(rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003204 t->srv_state = SV_STSHUTR;
3205 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3206 return 1;
3207 }
3208
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003209 /* read timeout : return a 504 to the client. */
Willy Tarreauf161a342007-04-08 16:59:42 +02003210 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) &&
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003211 tick_is_expired(rep->rex, now_ms))) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003212 buffer_shutr_done(rep);
3213 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003214 fd_delete(t->srv_fd);
3215 if (t->srv) {
3216 t->srv->cur_sess--;
3217 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003218 sess_change_server(t, NULL);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003219 }
3220 t->be->failed_resp++;
3221 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003222 rep->flags |= BF_MAY_FORWARD;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003223 txn->status = 504;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003224 client_return(t, error_message(t, HTTP_ERR_504));
3225 if (!(t->flags & SN_ERR_MASK))
3226 t->flags |= SN_ERR_SRVTO;
3227 if (!(t->flags & SN_FINST_MASK))
3228 t->flags |= SN_FINST_H;
3229 /* We used to have a free connection slot. Since we'll never use it,
3230 * we have to inform the server that it may be used by another session.
3231 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003232 if (t->srv && may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003233 process_srv_queue(t->srv);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003234 return 1;
3235 }
3236
3237 /* last client read and buffer empty */
3238 /* FIXME!!! here, we don't want to switch to SHUTW if the
3239 * client shuts read too early, because we may still have
3240 * some work to do on the headers.
3241 * The side-effect is that if the client completely closes its
3242 * connection during SV_STHEADER, the connection to the server
3243 * is kept until a response comes back or the timeout is reached.
3244 */
Willy Tarreau6468d922008-08-03 19:15:35 +02003245 else if (0 && /* we don't want to switch to shutw for now */
3246 unlikely(req->flags & BF_SHUTR_STATUS && (req->l == 0))) {
3247
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
3251 /* We must ensure that the read part is still
3252 * alive 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 shutdown(t->srv_fd, SHUT_WR);
3257 t->srv_state = SV_STSHUTW;
3258 return 1;
3259 }
3260
3261 /* write timeout */
3262 /* FIXME!!! here, we don't want to switch to SHUTW if the
3263 * client shuts read too early, because we may still have
3264 * some work to do on the headers.
3265 */
Willy Tarreauf161a342007-04-08 16:59:42 +02003266 else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) &&
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003267 tick_is_expired(req->wex, now_ms))) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003268 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003269 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003270 shutdown(t->srv_fd, SHUT_WR);
3271 /* We must ensure that the read part is still alive
3272 * when switching to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003273 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003274 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003275
3276 t->srv_state = SV_STSHUTW;
3277 if (!(t->flags & SN_ERR_MASK))
3278 t->flags |= SN_ERR_SRVTO;
3279 if (!(t->flags & SN_FINST_MASK))
3280 t->flags |= SN_FINST_H;
3281 return 1;
3282 }
3283
3284 /*
3285 * And now the non-error cases.
3286 */
3287
3288 /* Data remaining in the request buffer.
3289 * This happens during the first pass here, and during
3290 * long posts.
3291 */
3292 else if (likely(req->l)) {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003293 if (!tick_isset(req->wex)) {
3294 EV_FD_COND_S(t->srv_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02003295 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003296 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003297 if (tick_isset(req->wex)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003298 /* FIXME: to prevent the server from expiring read timeouts during writes,
3299 * we refresh it. */
3300 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003301 }
3302 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003303 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003304
Willy Tarreaua15645d2007-03-18 16:22:39 +01003305 /* nothing left in the request buffer */
3306 else {
Willy Tarreau66319382007-04-08 17:17:37 +02003307 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3308 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003309 req->wex = TICK_ETERNITY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003310 }
3311 }
3312
3313 return t->srv_state != SV_STHEADERS;
3314 }
3315
3316
3317 /*****************************************************************
3318 * More interesting part now : we know that we have a complete *
3319 * response which at least looks like HTTP. We have an indicator *
3320 * of each header's length, so we can parse them quickly. *
3321 ****************************************************************/
3322
Willy Tarreau9cdde232007-05-02 20:58:19 +02003323 /* ensure we keep this pointer to the beginning of the message */
3324 msg->sol = rep->data + msg->som;
3325
Willy Tarreaua15645d2007-03-18 16:22:39 +01003326 /*
3327 * 1: get the status code and check for cacheability.
3328 */
3329
3330 t->logs.logwait &= ~LW_RESP;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003331 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003332
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003333 switch (txn->status) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003334 case 200:
3335 case 203:
3336 case 206:
3337 case 300:
3338 case 301:
3339 case 410:
3340 /* RFC2616 @13.4:
3341 * "A response received with a status code of
3342 * 200, 203, 206, 300, 301 or 410 MAY be stored
3343 * by a cache (...) unless a cache-control
3344 * directive prohibits caching."
3345 *
3346 * RFC2616 @9.5: POST method :
3347 * "Responses to this method are not cacheable,
3348 * unless the response includes appropriate
3349 * Cache-Control or Expires header fields."
3350 */
3351 if (likely(txn->meth != HTTP_METH_POST) &&
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003352 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
Willy Tarreau3d300592007-03-18 18:34:41 +01003353 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003354 break;
3355 default:
3356 break;
3357 }
3358
3359 /*
3360 * 2: we may need to capture headers
3361 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003362 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003363 capture_headers(rep->data + msg->som, &txn->hdr_idx,
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003364 txn->rsp.cap, t->fe->rsp_cap);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003365
3366 /*
3367 * 3: we will have to evaluate the filters.
3368 * As opposed to version 1.2, now they will be evaluated in the
3369 * filters order and not in the header order. This means that
3370 * each filter has to be validated among all headers.
3371 *
3372 * Filters are tried with ->be first, then with ->fe if it is
3373 * different from ->be.
3374 */
3375
3376 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
3377
3378 cur_proxy = t->be;
3379 while (1) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003380 struct proxy *rule_set = cur_proxy;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003381
3382 /* try headers filters */
3383 if (rule_set->rsp_exp != NULL) {
3384 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3385 return_bad_resp:
Willy Tarreaubaaee002006-06-26 02:48:02 +02003386 if (t->srv) {
3387 t->srv->cur_sess--;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003388 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003389 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003390 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003391 cur_proxy->failed_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003392 return_srv_prx_502:
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003393 buffer_shutr_done(rep);
3394 buffer_shutw_done(req);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003395 fd_delete(t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003396 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003397 rep->flags |= BF_MAY_FORWARD;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003398 txn->status = 502;
Willy Tarreau80587432006-12-24 17:47:20 +01003399 client_return(t, error_message(t, HTTP_ERR_502));
Willy Tarreaubaaee002006-06-26 02:48:02 +02003400 if (!(t->flags & SN_ERR_MASK))
3401 t->flags |= SN_ERR_PRXCOND;
3402 if (!(t->flags & SN_FINST_MASK))
3403 t->flags |= SN_FINST_H;
3404 /* We used to have a free connection slot. Since we'll never use it,
3405 * we have to inform the server that it may be used by another session.
3406 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003407 if (t->srv && may_dequeue_tasks(t->srv, cur_proxy))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003408 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003409 return 1;
3410 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003411 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003412
Willy Tarreaua15645d2007-03-18 16:22:39 +01003413 /* has the response been denied ? */
Willy Tarreau3d300592007-03-18 18:34:41 +01003414 if (txn->flags & TX_SVDENY) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003415 if (t->srv) {
3416 t->srv->cur_sess--;
3417 t->srv->failed_secu++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003418 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003419 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003420 cur_proxy->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003421 goto return_srv_prx_502;
3422 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003423
Willy Tarreaua15645d2007-03-18 16:22:39 +01003424 /* We might have to check for "Connection:" */
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003425 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003426 !(t->flags & SN_CONN_CLOSED)) {
3427 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003428 int cur_idx, old_idx, delta, val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003429 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003430
Willy Tarreaua15645d2007-03-18 16:22:39 +01003431 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3432 old_idx = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003433
Willy Tarreaua15645d2007-03-18 16:22:39 +01003434 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3435 cur_hdr = &txn->hdr_idx.v[cur_idx];
3436 cur_ptr = cur_next;
3437 cur_end = cur_ptr + cur_hdr->len;
3438 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003439
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003440 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3441 if (val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003442 /* 3 possibilities :
3443 * - we have already set Connection: close,
3444 * so we remove this line.
3445 * - we have not yet set Connection: close,
3446 * but this line indicates close. We leave
3447 * it untouched and set the flag.
3448 * - we have not yet set Connection: close,
3449 * and this line indicates non-close. We
3450 * replace it.
3451 */
3452 if (t->flags & SN_CONN_CLOSED) {
3453 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3454 txn->rsp.eoh += delta;
3455 cur_next += delta;
3456 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3457 txn->hdr_idx.used--;
3458 cur_hdr->len = 0;
3459 } else {
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003460 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3461 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3462 "close", 5);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003463 cur_next += delta;
3464 cur_hdr->len += delta;
3465 txn->rsp.eoh += delta;
3466 }
3467 t->flags |= SN_CONN_CLOSED;
3468 }
3469 }
3470 old_idx = cur_idx;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003471 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003472 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003473
Willy Tarreaua15645d2007-03-18 16:22:39 +01003474 /* add response headers from the rule sets in the same order */
3475 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003476 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3477 rule_set->rsp_add[cur_idx])) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003478 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003479 }
3480
Willy Tarreaua15645d2007-03-18 16:22:39 +01003481 /* check whether we're already working on the frontend */
3482 if (cur_proxy == t->fe)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003483 break;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003484 cur_proxy = t->fe;
3485 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003486
Willy Tarreaua15645d2007-03-18 16:22:39 +01003487 /*
3488 * 4: check for server cookie.
3489 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01003490 if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name
3491 || (t->be->options & PR_O_CHK_CACHE))
3492 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003493
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003494
Willy Tarreaua15645d2007-03-18 16:22:39 +01003495 /*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003496 * 5: check for cache-control or pragma headers if required.
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003497 */
Willy Tarreau396d2c62007-11-04 19:30:00 +01003498 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3499 check_response_for_cacheability(t, rep);
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003500
3501 /*
3502 * 6: add server cookie in the response if needed
Willy Tarreaua15645d2007-03-18 16:22:39 +01003503 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003504 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3505 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003506 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003507
Willy Tarreaua15645d2007-03-18 16:22:39 +01003508 /* the server is known, it's not the one the client requested, we have to
3509 * insert a set-cookie here, except if we want to insert only on POST
3510 * requests and this one isn't. Note that servers which don't have cookies
3511 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003512 */
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003513 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003514 t->be->cookie_name,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003515 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003516
Krzysztof Piotr Oledzki1acf2172008-05-29 23:03:34 +02003517 if (t->be->cookie_domain)
3518 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +02003519
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003520 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3521 trash, len)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003522 goto return_bad_resp;
Willy Tarreau3d300592007-03-18 18:34:41 +01003523 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003524
Willy Tarreaua15645d2007-03-18 16:22:39 +01003525 /* Here, we will tell an eventual cache on the client side that we don't
3526 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3527 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3528 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3529 */
Krzysztof Oledzki9198ab52007-10-11 18:56:27 +02003530 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
3531
3532 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3533
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003534 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3535 "Cache-control: private", 22)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003536 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003537 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003538 }
3539
3540
3541 /*
Willy Tarreaua15645d2007-03-18 16:22:39 +01003542 * 7: check if result will be cacheable with a cookie.
3543 * We'll block the response if security checks have caught
3544 * nasty things such as a cacheable cookie.
3545 */
Willy Tarreau3d300592007-03-18 18:34:41 +01003546 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3547 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003548 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003549
Willy Tarreaua15645d2007-03-18 16:22:39 +01003550 /* we're in presence of a cacheable response containing
3551 * a set-cookie header. We'll block it as requested by
3552 * the 'checkcache' option, and send an alert.
3553 */
3554 if (t->srv) {
3555 t->srv->cur_sess--;
3556 t->srv->failed_secu++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003557 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003558 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003559 t->be->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003560
Willy Tarreaua15645d2007-03-18 16:22:39 +01003561 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003562 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003563 send_log(t->be, LOG_ALERT,
3564 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003565 t->be->id, t->srv?t->srv->id:"<dispatch>");
Willy Tarreaua15645d2007-03-18 16:22:39 +01003566 goto return_srv_prx_502;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003567 }
3568
Willy Tarreaua15645d2007-03-18 16:22:39 +01003569 /*
3570 * 8: add "Connection: close" if needed and not yet set.
Willy Tarreau2807efd2007-03-25 23:47:23 +02003571 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003572 */
Willy Tarreau2807efd2007-03-25 23:47:23 +02003573 if (!(t->flags & SN_CONN_CLOSED) &&
Krzysztof Oledzki336d4752007-12-25 02:40:22 +01003574 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau2807efd2007-03-25 23:47:23 +02003575 if ((unlikely(msg->sl.st.v_l != 8) ||
3576 unlikely(req->data[msg->som + 7] != '0')) &&
3577 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003578 "Connection: close", 17)) < 0)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003579 goto return_bad_resp;
3580 t->flags |= SN_CONN_CLOSED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003581 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003582
Willy Tarreaubaaee002006-06-26 02:48:02 +02003583
Willy Tarreaua15645d2007-03-18 16:22:39 +01003584 /*************************************************************
3585 * OK, that's finished for the headers. We have done what we *
3586 * could. Let's switch to the DATA state. *
3587 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003588
Willy Tarreaua15645d2007-03-18 16:22:39 +01003589 t->srv_state = SV_STDATA;
Willy Tarreau718f0ef2008-08-10 16:21:32 +02003590 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003591 rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau42aae5c2007-04-29 17:43:56 +02003592 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003593
3594 /* client connection already closed or option 'forceclose' required :
3595 * we close the server's outgoing connection right now.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003596 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003597 if ((req->l == 0) &&
Willy Tarreau6468d922008-08-03 19:15:35 +02003598 (req->flags & BF_SHUTR_STATUS || t->be->options & PR_O_FORCE_CLO)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003599 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003600 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003601
3602 /* We must ensure that the read part is still alive when switching
3603 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003604 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003605 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003606
Willy Tarreaua15645d2007-03-18 16:22:39 +01003607 shutdown(t->srv_fd, SHUT_WR);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003608 t->srv_state = SV_STSHUTW;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003609 }
3610
Willy Tarreaua15645d2007-03-18 16:22:39 +01003611#ifdef CONFIG_HAP_TCPSPLICE
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003612 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003613 /* TCP splicing supported by both FE and BE */
3614 tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003615 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003616#endif
3617 /* if the user wants to log as soon as possible, without counting
Krzysztof Piotr Oledzkif1e1cb42008-01-20 23:27:02 +01003618 * bytes from the server, then this is the right moment. We have
3619 * to temporarily assign bytes_out to log what we currently have.
3620 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003621 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3622 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
Willy Tarreau8b3977f2008-01-18 11:16:32 +01003623 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreau42250582007-04-01 01:30:43 +02003624 if (t->fe->to_log & LW_REQ)
3625 http_sess_log(t);
3626 else
3627 tcp_sess_log(t);
Krzysztof Piotr Oledzkif1e1cb42008-01-20 23:27:02 +01003628 t->logs.bytes_out = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003629 }
3630
Willy Tarreaua15645d2007-03-18 16:22:39 +01003631 /* Note: we must not try to cheat by jumping directly to DATA,
3632 * otherwise we would not let the client side wake up.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003633 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003634
3635 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003636 }
3637 else if (s == SV_STDATA) {
3638 /* read or write error */
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003639 if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) {
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003640 buffer_shutr_done(rep);
3641 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003642 fd_delete(t->srv_fd);
3643 if (t->srv) {
3644 t->srv->cur_sess--;
3645 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003646 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003647 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003648 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003649 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003650 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003651 if (!(t->flags & SN_ERR_MASK))
3652 t->flags |= SN_ERR_SRVCL;
3653 if (!(t->flags & SN_FINST_MASK))
3654 t->flags |= SN_FINST_D;
3655 /* We used to have a free connection slot. Since we'll never use it,
3656 * we have to inform the server that it may be used by another session.
3657 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003658 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003659 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003660
3661 return 1;
3662 }
3663 /* last read, or end of client write */
Willy Tarreau6468d922008-08-03 19:15:35 +02003664 else if (rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003665 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003666 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003667 t->srv_state = SV_STSHUTR;
3668 //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state);
3669 return 1;
3670 }
3671 /* end of client read and no more data to send */
Willy Tarreau6468d922008-08-03 19:15:35 +02003672 else if (req->flags & BF_SHUTR_STATUS && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003673 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003674 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003675 shutdown(t->srv_fd, SHUT_WR);
3676 /* We must ensure that the read part is still alive when switching
3677 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003678 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003679 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003680
3681 t->srv_state = SV_STSHUTW;
3682 return 1;
3683 }
3684 /* read timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003685 else if (tick_is_expired(rep->rex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003686 EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreaufa645582007-06-03 15:59:52 +02003687 buffer_shutr(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003688 t->srv_state = SV_STSHUTR;
3689 if (!(t->flags & SN_ERR_MASK))
3690 t->flags |= SN_ERR_SRVTO;
3691 if (!(t->flags & SN_FINST_MASK))
3692 t->flags |= SN_FINST_D;
3693 return 1;
3694 }
3695 /* write timeout */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003696 else if (tick_is_expired(req->wex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003697 EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003698 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003699 shutdown(t->srv_fd, SHUT_WR);
3700 /* We must ensure that the read part is still alive when switching
3701 * to shutw */
Willy Tarreauf161a342007-04-08 16:59:42 +02003702 EV_FD_SET(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003703 rep->cex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003704 t->srv_state = SV_STSHUTW;
3705 if (!(t->flags & SN_ERR_MASK))
3706 t->flags |= SN_ERR_SRVTO;
3707 if (!(t->flags & SN_FINST_MASK))
3708 t->flags |= SN_FINST_D;
3709 return 1;
3710 }
3711
3712 /* recompute request time-outs */
3713 if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003714 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3715 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003716 req->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003717 }
3718 }
3719 else { /* buffer not empty, there are still data to be transferred */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003720 if (!tick_isset(req->wex)) {
3721 EV_FD_COND_S(t->srv_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02003722 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003723 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003724 if (tick_isset(req->wex)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003725 /* FIXME: to prevent the server from expiring read timeouts during writes,
3726 * we refresh it. */
Willy Tarreaud7971282006-07-29 18:36:34 +02003727 rep->rex = req->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003728 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003729 }
3730 }
3731
3732 /* recompute response time-outs */
3733 if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003734 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003735 rep->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003736 }
3737 }
3738 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003739 if (!tick_isset(rep->rex)) {
3740 EV_FD_COND_S(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003741 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003742 }
3743 }
3744
3745 return 0; /* other cases change nothing */
3746 }
3747 else if (s == SV_STSHUTR) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003748 if (req->flags & BF_WRITE_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003749 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003750 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003751 fd_delete(t->srv_fd);
3752 if (t->srv) {
3753 t->srv->cur_sess--;
3754 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003755 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003756 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003757 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003758 //close(t->srv_fd);
3759 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003760 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003761 if (!(t->flags & SN_ERR_MASK))
3762 t->flags |= SN_ERR_SRVCL;
3763 if (!(t->flags & SN_FINST_MASK))
3764 t->flags |= SN_FINST_D;
3765 /* We used to have a free connection slot. Since we'll never use it,
3766 * we have to inform the server that it may be used by another session.
3767 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003768 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003769 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003770
3771 return 1;
3772 }
Willy Tarreau6468d922008-08-03 19:15:35 +02003773 else if (req->flags & BF_SHUTR_STATUS && (req->l == 0)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003774 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003775 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003776 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003777 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003778 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003779 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003780 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003781 //close(t->srv_fd);
3782 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003783 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003784 /* We used to have a free connection slot. Since we'll never use it,
3785 * we have to inform the server that it may be used by another session.
3786 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003787 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003788 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003789
3790 return 1;
3791 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003792 else if (tick_is_expired(req->wex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003793 //EV_FD_CLR(t->srv_fd, DIR_WR);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003794 buffer_shutw_done(req);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003795 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003796 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003797 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003798 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003799 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003800 //close(t->srv_fd);
3801 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003802 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003803 if (!(t->flags & SN_ERR_MASK))
3804 t->flags |= SN_ERR_SRVTO;
3805 if (!(t->flags & SN_FINST_MASK))
3806 t->flags |= SN_FINST_D;
3807 /* We used to have a free connection slot. Since we'll never use it,
3808 * we have to inform the server that it may be used by another session.
3809 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003810 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003811 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003812
3813 return 1;
3814 }
3815 else if (req->l == 0) {
Willy Tarreau66319382007-04-08 17:17:37 +02003816 if (EV_FD_COND_C(t->srv_fd, DIR_WR)) {
3817 /* stop writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003818 req->wex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003819 }
3820 }
3821 else { /* buffer not empty */
Willy Tarreauadfb8562008-08-11 15:24:42 +02003822 if (!tick_isset(req->wex)) {
3823 EV_FD_COND_S(t->srv_fd, DIR_WR);
Willy Tarreau66319382007-04-08 17:17:37 +02003824 /* restart writing */
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003825 req->wex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003826 }
3827 }
3828 return 0;
3829 }
3830 else if (s == SV_STSHUTW) {
Willy Tarreau0f9f5052006-07-29 17:39:25 +02003831 if (rep->flags & BF_READ_ERROR) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003832 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003833 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003834 fd_delete(t->srv_fd);
3835 if (t->srv) {
3836 t->srv->cur_sess--;
3837 t->srv->failed_resp++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003838 sess_change_server(t, NULL);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003839 }
Willy Tarreau73de9892006-11-30 11:40:23 +01003840 t->be->failed_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003841 //close(t->srv_fd);
3842 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003843 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003844 if (!(t->flags & SN_ERR_MASK))
3845 t->flags |= SN_ERR_SRVCL;
3846 if (!(t->flags & SN_FINST_MASK))
3847 t->flags |= SN_FINST_D;
3848 /* We used to have a free connection slot. Since we'll never use it,
3849 * we have to inform the server that it may be used by another session.
3850 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003851 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003852 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003853
3854 return 1;
3855 }
Willy Tarreau6468d922008-08-03 19:15:35 +02003856 else if (rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003857 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003858 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003859 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003860 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003861 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003862 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003863 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003864 //close(t->srv_fd);
3865 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003866 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003867 /* We used to have a free connection slot. Since we'll never use it,
3868 * we have to inform the server that it may be used by another session.
3869 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003870 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003871 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003872
3873 return 1;
3874 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003875 else if (tick_is_expired(rep->rex, now_ms)) {
Willy Tarreauf161a342007-04-08 16:59:42 +02003876 //EV_FD_CLR(t->srv_fd, DIR_RD);
Willy Tarreau89edf5e2008-08-03 17:25:14 +02003877 buffer_shutr_done(rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003878 fd_delete(t->srv_fd);
Willy Tarreau51406232008-03-10 22:04:20 +01003879 if (t->srv) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003880 t->srv->cur_sess--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02003881 sess_change_server(t, NULL);
Willy Tarreau51406232008-03-10 22:04:20 +01003882 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003883 //close(t->srv_fd);
3884 t->srv_state = SV_STCLOSE;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003885 rep->flags |= BF_MAY_FORWARD;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003886 if (!(t->flags & SN_ERR_MASK))
3887 t->flags |= SN_ERR_SRVTO;
3888 if (!(t->flags & SN_FINST_MASK))
3889 t->flags |= SN_FINST_D;
3890 /* We used to have a free connection slot. Since we'll never use it,
3891 * we have to inform the server that it may be used by another session.
3892 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003893 if (may_dequeue_tasks(t->srv, t->be))
Willy Tarreau7c669d72008-06-20 15:04:11 +02003894 process_srv_queue(t->srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003895
3896 return 1;
3897 }
3898 else if (rep->l == BUFSIZE) { /* no room to read more data */
Willy Tarreau66319382007-04-08 17:17:37 +02003899 if (EV_FD_COND_C(t->srv_fd, DIR_RD)) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003900 rep->rex = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003901 }
3902 }
3903 else {
Willy Tarreauadfb8562008-08-11 15:24:42 +02003904 if (!tick_isset(rep->rex)) {
3905 EV_FD_COND_S(t->srv_fd, DIR_RD);
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003906 rep->rex = tick_add_ifset(now_ms, t->be->timeout.server);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003907 }
3908 }
3909 return 0;
3910 }
3911 else { /* SV_STCLOSE : nothing to do */
3912 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
3913 int len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003914 len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003915 t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003916 write(1, trash, len);
3917 }
3918 return 0;
3919 }
3920 return 0;
3921}
3922
3923
3924/*
3925 * Produces data for the session <s> depending on its source. Expects to be
3926 * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be
3927 * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the
3928 * session, which it uses to keep on being called when there is free space in
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003929 * the buffer, or simply by letting an empty buffer upon return. It returns 1
Willy Tarreaubaaee002006-06-26 02:48:02 +02003930 * if it changes the session state from CL_STSHUTR, otherwise 0.
3931 */
3932int produce_content(struct session *s)
3933{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003934 if (s->data_source == DATA_SRC_NONE) {
3935 s->flags &= ~SN_SELF_GEN;
3936 return 1;
3937 }
3938 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003939 /* dump server statistics */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003940 int ret = stats_dump_http(s, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003941 if (ret >= 0)
3942 return ret;
3943 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003944 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003945
Willy Tarreau91861262007-10-17 17:06:05 +02003946 /* unknown data source or internal error */
3947 s->txn.status = 500;
3948 client_retnclose(s, error_message(s, HTTP_ERR_500));
3949 if (!(s->flags & SN_ERR_MASK))
3950 s->flags |= SN_ERR_PRXCOND;
3951 if (!(s->flags & SN_FINST_MASK))
3952 s->flags |= SN_FINST_R;
3953 s->flags &= ~SN_SELF_GEN;
3954 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003955}
3956
3957
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003958/* Iterate the same filter through all request headers.
3959 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003960 * Since it can manage the switch to another backend, it updates the per-proxy
3961 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003962 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003963int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003964{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003965 char term;
3966 char *cur_ptr, *cur_end, *cur_next;
3967 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003968 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003969 struct hdr_idx_elem *cur_hdr;
3970 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003971
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003972 last_hdr = 0;
3973
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003974 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003975 old_idx = 0;
3976
3977 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003978 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003979 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003980 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003981 (exp->action == ACT_ALLOW ||
3982 exp->action == ACT_DENY ||
3983 exp->action == ACT_TARPIT))
3984 return 0;
3985
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003986 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003987 if (!cur_idx)
3988 break;
3989
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003990 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003991 cur_ptr = cur_next;
3992 cur_end = cur_ptr + cur_hdr->len;
3993 cur_next = cur_end + cur_hdr->cr + 1;
3994
3995 /* Now we have one header between cur_ptr and cur_end,
3996 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003997 */
3998
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003999 /* The annoying part is that pattern matching needs
4000 * that we modify the contents to null-terminate all
4001 * strings before testing them.
4002 */
4003
4004 term = *cur_end;
4005 *cur_end = '\0';
4006
4007 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4008 switch (exp->action) {
4009 case ACT_SETBE:
4010 /* It is not possible to jump a second time.
4011 * FIXME: should we return an HTTP/500 here so that
4012 * the admin knows there's a problem ?
4013 */
4014 if (t->be != t->fe)
4015 break;
4016
4017 /* Swithing Proxy */
4018 t->be = (struct proxy *) exp->replace;
4019
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004020 /* right now, the backend switch is not overly complicated
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004021 * because we have associated req_cap and rsp_cap to the
4022 * frontend, and the beconn will be updated later.
4023 */
4024
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004025 t->rep->rto = t->req->wto = t->be->timeout.server;
4026 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004027 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004028 last_hdr = 1;
4029 break;
4030
4031 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004032 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004033 last_hdr = 1;
4034 break;
4035
4036 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004037 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004038 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004039 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004040 break;
4041
4042 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004043 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004044 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004045 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004046 break;
4047
4048 case ACT_REPLACE:
4049 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4050 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4051 /* FIXME: if the user adds a newline in the replacement, the
4052 * index will not be recalculated for now, and the new line
4053 * will not be counted as a new header.
4054 */
4055
4056 cur_end += delta;
4057 cur_next += delta;
4058 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004059 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004060 break;
4061
4062 case ACT_REMOVE:
4063 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4064 cur_next += delta;
4065
4066 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004067 txn->req.eoh += delta;
4068 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4069 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004070 cur_hdr->len = 0;
4071 cur_end = NULL; /* null-term has been rewritten */
4072 break;
4073
4074 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004075 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004076 if (cur_end)
4077 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004079 /* keep the link from this header to next one in case of later
4080 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004081 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004082 old_idx = cur_idx;
4083 }
4084 return 0;
4085}
4086
4087
4088/* Apply the filter to the request line.
4089 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4090 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004091 * Since it can manage the switch to another backend, it updates the per-proxy
4092 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004093 */
4094int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4095{
4096 char term;
4097 char *cur_ptr, *cur_end;
4098 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004099 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004100 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004101
Willy Tarreau58f10d72006-12-04 02:26:12 +01004102
Willy Tarreau3d300592007-03-18 18:34:41 +01004103 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004104 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004105 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004106 (exp->action == ACT_ALLOW ||
4107 exp->action == ACT_DENY ||
4108 exp->action == ACT_TARPIT))
4109 return 0;
4110 else if (exp->action == ACT_REMOVE)
4111 return 0;
4112
4113 done = 0;
4114
Willy Tarreau9cdde232007-05-02 20:58:19 +02004115 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004116 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004117
4118 /* Now we have the request line between cur_ptr and cur_end */
4119
4120 /* The annoying part is that pattern matching needs
4121 * that we modify the contents to null-terminate all
4122 * strings before testing them.
4123 */
4124
4125 term = *cur_end;
4126 *cur_end = '\0';
4127
4128 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4129 switch (exp->action) {
4130 case ACT_SETBE:
4131 /* It is not possible to jump a second time.
4132 * FIXME: should we return an HTTP/500 here so that
4133 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004134 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004135 if (t->be != t->fe)
4136 break;
4137
4138 /* Swithing Proxy */
4139 t->be = (struct proxy *) exp->replace;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004140
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004141 /* right now, the backend switch is not too much complicated
4142 * because we have associated req_cap and rsp_cap to the
4143 * frontend, and the beconn will be updated later.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004144 */
4145
Willy Tarreaud7c30f92007-12-03 01:38:36 +01004146 t->rep->rto = t->req->wto = t->be->timeout.server;
4147 t->req->cto = t->be->timeout.connect;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004148 t->conn_retries = t->be->conn_retries;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004149 done = 1;
4150 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004151
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004152 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004153 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004154 done = 1;
4155 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004157 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004158 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004159 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004160 done = 1;
4161 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004162
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004163 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004164 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004165 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004166 done = 1;
4167 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004168
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004169 case ACT_REPLACE:
4170 *cur_end = term; /* restore the string terminator */
4171 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4172 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4173 /* FIXME: if the user adds a newline in the replacement, the
4174 * index will not be recalculated for now, and the new line
4175 * will not be counted as a new header.
4176 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004177
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004178 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004179 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004180
Willy Tarreau9cdde232007-05-02 20:58:19 +02004181 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004182 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004183 HTTP_MSG_RQMETH,
4184 cur_ptr, cur_end + 1,
4185 NULL, NULL);
4186 if (unlikely(!cur_end))
4187 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004188
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004189 /* we have a full request and we know that we have either a CR
4190 * or an LF at <ptr>.
4191 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004192 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4193 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004194 /* there is no point trying this regex on headers */
4195 return 1;
4196 }
4197 }
4198 *cur_end = term; /* restore the string terminator */
4199 return done;
4200}
Willy Tarreau97de6242006-12-27 17:18:38 +01004201
Willy Tarreau58f10d72006-12-04 02:26:12 +01004202
Willy Tarreau58f10d72006-12-04 02:26:12 +01004203
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004204/*
4205 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4206 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004207 * unparsable request. Since it can manage the switch to another backend, it
4208 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004209 */
4210int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4211{
Willy Tarreau3d300592007-03-18 18:34:41 +01004212 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004213 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004214 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004215 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004216
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004217 /*
4218 * The interleaving of transformations and verdicts
4219 * makes it difficult to decide to continue or stop
4220 * the evaluation.
4221 */
4222
Willy Tarreau3d300592007-03-18 18:34:41 +01004223 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004224 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4225 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4226 exp = exp->next;
4227 continue;
4228 }
4229
4230 /* Apply the filter to the request line. */
4231 ret = apply_filter_to_req_line(t, req, exp);
4232 if (unlikely(ret < 0))
4233 return -1;
4234
4235 if (likely(ret == 0)) {
4236 /* The filter did not match the request, it can be
4237 * iterated through all headers.
4238 */
4239 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004240 }
4241 exp = exp->next;
4242 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004243 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004244}
4245
4246
Willy Tarreaua15645d2007-03-18 16:22:39 +01004247
Willy Tarreau58f10d72006-12-04 02:26:12 +01004248/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004249 * Manage client-side cookie. It can impact performance by about 2% so it is
4250 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004251 */
4252void manage_client_side_cookies(struct session *t, struct buffer *req)
4253{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004254 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004255 char *p1, *p2, *p3, *p4;
4256 char *del_colon, *del_cookie, *colon;
4257 int app_cookies;
4258
4259 appsess *asession_temp = NULL;
4260 appsess local_asession;
4261
4262 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004263 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004264
Willy Tarreau2a324282006-12-05 00:05:46 +01004265 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004266 * we start with the start line.
4267 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004268 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004269 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004270
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004271 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004272 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004273 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004274
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004275 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004276 cur_ptr = cur_next;
4277 cur_end = cur_ptr + cur_hdr->len;
4278 cur_next = cur_end + cur_hdr->cr + 1;
4279
4280 /* We have one full header between cur_ptr and cur_end, and the
4281 * next header starts at cur_next. We're only interested in
4282 * "Cookie:" headers.
4283 */
4284
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004285 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4286 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004287 old_idx = cur_idx;
4288 continue;
4289 }
4290
4291 /* Now look for cookies. Conforming to RFC2109, we have to support
4292 * attributes whose name begin with a '$', and associate them with
4293 * the right cookie, if we want to delete this cookie.
4294 * So there are 3 cases for each cookie read :
4295 * 1) it's a special attribute, beginning with a '$' : ignore it.
4296 * 2) it's a server id cookie that we *MAY* want to delete : save
4297 * some pointers on it (last semi-colon, beginning of cookie...)
4298 * 3) it's an application cookie : we *MAY* have to delete a previous
4299 * "special" cookie.
4300 * At the end of loop, if a "special" cookie remains, we may have to
4301 * remove it. If no application cookie persists in the header, we
4302 * *MUST* delete it
4303 */
4304
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004305 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004306
Willy Tarreau58f10d72006-12-04 02:26:12 +01004307 /* del_cookie == NULL => nothing to be deleted */
4308 del_colon = del_cookie = NULL;
4309 app_cookies = 0;
4310
4311 while (p1 < cur_end) {
4312 /* skip spaces and colons, but keep an eye on these ones */
4313 while (p1 < cur_end) {
4314 if (*p1 == ';' || *p1 == ',')
4315 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004316 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004317 break;
4318 p1++;
4319 }
4320
4321 if (p1 == cur_end)
4322 break;
4323
4324 /* p1 is at the beginning of the cookie name */
4325 p2 = p1;
4326 while (p2 < cur_end && *p2 != '=')
4327 p2++;
4328
4329 if (p2 == cur_end)
4330 break;
4331
4332 p3 = p2 + 1; /* skips the '=' sign */
4333 if (p3 == cur_end)
4334 break;
4335
4336 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004337 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004338 p4++;
4339
4340 /* here, we have the cookie name between p1 and p2,
4341 * and its value between p3 and p4.
4342 * we can process it :
4343 *
4344 * Cookie: NAME=VALUE;
4345 * | || || |
4346 * | || || +--> p4
4347 * | || |+-------> p3
4348 * | || +--------> p2
4349 * | |+------------> p1
4350 * | +-------------> colon
4351 * +--------------------> cur_ptr
4352 */
4353
4354 if (*p1 == '$') {
4355 /* skip this one */
4356 }
4357 else {
4358 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004359 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004360 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004361 (p4 - p1 >= t->fe->capture_namelen) &&
4362 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004363 int log_len = p4 - p1;
4364
Willy Tarreau086b3b42007-05-13 21:45:51 +02004365 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004366 Alert("HTTP logging : out of memory.\n");
4367 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004368 if (log_len > t->fe->capture_len)
4369 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004370 memcpy(txn->cli_cookie, p1, log_len);
4371 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004372 }
4373 }
4374
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004375 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4376 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004377 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004378 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004379 char *delim;
4380
4381 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4382 * have the server ID betweek p3 and delim, and the original cookie between
4383 * delim+1 and p4. Otherwise, delim==p4 :
4384 *
4385 * Cookie: NAME=SRV~VALUE;
4386 * | || || | |
4387 * | || || | +--> p4
4388 * | || || +--------> delim
4389 * | || |+-----------> p3
4390 * | || +------------> p2
4391 * | |+----------------> p1
4392 * | +-----------------> colon
4393 * +------------------------> cur_ptr
4394 */
4395
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004396 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004397 for (delim = p3; delim < p4; delim++)
4398 if (*delim == COOKIE_DELIM)
4399 break;
4400 }
4401 else
4402 delim = p4;
4403
4404
4405 /* Here, we'll look for the first running server which supports the cookie.
4406 * This allows to share a same cookie between several servers, for example
4407 * to dedicate backup servers to specific servers only.
4408 * However, to prevent clients from sticking to cookie-less backup server
4409 * when they have incidentely learned an empty cookie, we simply ignore
4410 * empty cookies and mark them as invalid.
4411 */
4412 if (delim == p3)
4413 srv = NULL;
4414
4415 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004416 if (srv->cookie && (srv->cklen == delim - p3) &&
4417 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004418 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004419 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004420 txn->flags &= ~TX_CK_MASK;
4421 txn->flags |= TX_CK_VALID;
4422 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004423 t->srv = srv;
4424 break;
4425 } else {
4426 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004427 txn->flags &= ~TX_CK_MASK;
4428 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004429 }
4430 }
4431 srv = srv->next;
4432 }
4433
Willy Tarreau3d300592007-03-18 18:34:41 +01004434 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004435 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004436 txn->flags &= ~TX_CK_MASK;
4437 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004438 }
4439
4440 /* depending on the cookie mode, we may have to either :
4441 * - delete the complete cookie if we're in insert+indirect mode, so that
4442 * the server never sees it ;
4443 * - remove the server id from the cookie value, and tag the cookie as an
4444 * application cookie so that it does not get accidentely removed later,
4445 * if we're in cookie prefix mode
4446 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004447 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004448 int delta; /* negative */
4449
4450 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4451 p4 += delta;
4452 cur_end += delta;
4453 cur_next += delta;
4454 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004455 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004456
4457 del_cookie = del_colon = NULL;
4458 app_cookies++; /* protect the header from deletion */
4459 }
4460 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004461 (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 +01004462 del_cookie = p1;
4463 del_colon = colon;
4464 }
4465 } else {
4466 /* now we know that we must keep this cookie since it's
4467 * not ours. But if we wanted to delete our cookie
4468 * earlier, we cannot remove the complete header, but we
4469 * can remove the previous block itself.
4470 */
4471 app_cookies++;
4472
4473 if (del_cookie != NULL) {
4474 int delta; /* negative */
4475
4476 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4477 p4 += delta;
4478 cur_end += delta;
4479 cur_next += delta;
4480 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004481 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004482 del_cookie = del_colon = NULL;
4483 }
4484 }
4485
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004486 if ((t->be->appsession_name != NULL) &&
4487 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004488 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004489
Willy Tarreau58f10d72006-12-04 02:26:12 +01004490 /* Cool... it's the right one */
4491
4492 asession_temp = &local_asession;
4493
Willy Tarreau63963c62007-05-13 21:29:55 +02004494 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004495 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4496 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4497 return;
4498 }
4499
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004500 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4501 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004502 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02004503
Willy Tarreau58f10d72006-12-04 02:26:12 +01004504 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02004505 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4506 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004507 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004508 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004509 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004510 Alert("Not enough memory process_cli():asession:calloc().\n");
4511 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4512 return;
4513 }
4514
4515 asession_temp->sessid = local_asession.sessid;
4516 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004517 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004518 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004519 } else {
4520 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004521 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004522 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004523 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004524 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004525 Alert("Found Application Session without matching server.\n");
4526 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004527 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004528 while (srv) {
4529 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004530 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004531 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004532 txn->flags &= ~TX_CK_MASK;
4533 txn->flags |= TX_CK_VALID;
4534 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004535 t->srv = srv;
4536 break;
4537 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004538 txn->flags &= ~TX_CK_MASK;
4539 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004540 }
4541 }
4542 srv = srv->next;
4543 }/* end while(srv) */
4544 }/* end else if server == NULL */
4545
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004546 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004547 asession_temp->request_count++;
4548#if defined(DEBUG_HASH)
4549 Alert("manage_client_side_cookies\n");
4550 appsession_hash_dump(&(t->be->htbl_proxy));
4551#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004552 }/* end if ((t->proxy->appsession_name != NULL) ... */
4553 }
4554
4555 /* we'll have to look for another cookie ... */
4556 p1 = p4;
4557 } /* while (p1 < cur_end) */
4558
4559 /* There's no more cookie on this line.
4560 * We may have marked the last one(s) for deletion.
4561 * We must do this now in two ways :
4562 * - if there is no app cookie, we simply delete the header ;
4563 * - if there are app cookies, we must delete the end of the
4564 * string properly, including the colon/semi-colon before
4565 * the cookie name.
4566 */
4567 if (del_cookie != NULL) {
4568 int delta;
4569 if (app_cookies) {
4570 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4571 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004572 cur_hdr->len += delta;
4573 } else {
4574 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004575
4576 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004577 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4578 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004579 cur_hdr->len = 0;
4580 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004581 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004582 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004583 }
4584
4585 /* keep the link from this header to next one */
4586 old_idx = cur_idx;
4587 } /* end of cookie processing on this header */
4588}
4589
4590
Willy Tarreaua15645d2007-03-18 16:22:39 +01004591/* Iterate the same filter through all response headers contained in <rtr>.
4592 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4593 */
4594int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4595{
4596 char term;
4597 char *cur_ptr, *cur_end, *cur_next;
4598 int cur_idx, old_idx, last_hdr;
4599 struct http_txn *txn = &t->txn;
4600 struct hdr_idx_elem *cur_hdr;
4601 int len, delta;
4602
4603 last_hdr = 0;
4604
4605 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4606 old_idx = 0;
4607
4608 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004609 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004610 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004611 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004612 (exp->action == ACT_ALLOW ||
4613 exp->action == ACT_DENY))
4614 return 0;
4615
4616 cur_idx = txn->hdr_idx.v[old_idx].next;
4617 if (!cur_idx)
4618 break;
4619
4620 cur_hdr = &txn->hdr_idx.v[cur_idx];
4621 cur_ptr = cur_next;
4622 cur_end = cur_ptr + cur_hdr->len;
4623 cur_next = cur_end + cur_hdr->cr + 1;
4624
4625 /* Now we have one header between cur_ptr and cur_end,
4626 * and the next header starts at cur_next.
4627 */
4628
4629 /* The annoying part is that pattern matching needs
4630 * that we modify the contents to null-terminate all
4631 * strings before testing them.
4632 */
4633
4634 term = *cur_end;
4635 *cur_end = '\0';
4636
4637 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4638 switch (exp->action) {
4639 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004640 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004641 last_hdr = 1;
4642 break;
4643
4644 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004645 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004646 last_hdr = 1;
4647 break;
4648
4649 case ACT_REPLACE:
4650 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4651 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4652 /* FIXME: if the user adds a newline in the replacement, the
4653 * index will not be recalculated for now, and the new line
4654 * will not be counted as a new header.
4655 */
4656
4657 cur_end += delta;
4658 cur_next += delta;
4659 cur_hdr->len += delta;
4660 txn->rsp.eoh += delta;
4661 break;
4662
4663 case ACT_REMOVE:
4664 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4665 cur_next += delta;
4666
4667 /* FIXME: this should be a separate function */
4668 txn->rsp.eoh += delta;
4669 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4670 txn->hdr_idx.used--;
4671 cur_hdr->len = 0;
4672 cur_end = NULL; /* null-term has been rewritten */
4673 break;
4674
4675 }
4676 }
4677 if (cur_end)
4678 *cur_end = term; /* restore the string terminator */
4679
4680 /* keep the link from this header to next one in case of later
4681 * removal of next header.
4682 */
4683 old_idx = cur_idx;
4684 }
4685 return 0;
4686}
4687
4688
4689/* Apply the filter to the status line in the response buffer <rtr>.
4690 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4691 * or -1 if a replacement resulted in an invalid status line.
4692 */
4693int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4694{
4695 char term;
4696 char *cur_ptr, *cur_end;
4697 int done;
4698 struct http_txn *txn = &t->txn;
4699 int len, delta;
4700
4701
Willy Tarreau3d300592007-03-18 18:34:41 +01004702 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004703 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004704 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004705 (exp->action == ACT_ALLOW ||
4706 exp->action == ACT_DENY))
4707 return 0;
4708 else if (exp->action == ACT_REMOVE)
4709 return 0;
4710
4711 done = 0;
4712
Willy Tarreau9cdde232007-05-02 20:58:19 +02004713 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004714 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4715
4716 /* Now we have the status line between cur_ptr and cur_end */
4717
4718 /* The annoying part is that pattern matching needs
4719 * that we modify the contents to null-terminate all
4720 * strings before testing them.
4721 */
4722
4723 term = *cur_end;
4724 *cur_end = '\0';
4725
4726 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4727 switch (exp->action) {
4728 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004729 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004730 done = 1;
4731 break;
4732
4733 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004734 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004735 done = 1;
4736 break;
4737
4738 case ACT_REPLACE:
4739 *cur_end = term; /* restore the string terminator */
4740 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4741 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4742 /* FIXME: if the user adds a newline in the replacement, the
4743 * index will not be recalculated for now, and the new line
4744 * will not be counted as a new header.
4745 */
4746
4747 txn->rsp.eoh += delta;
4748 cur_end += delta;
4749
Willy Tarreau9cdde232007-05-02 20:58:19 +02004750 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004751 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004752 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004753 cur_ptr, cur_end + 1,
4754 NULL, NULL);
4755 if (unlikely(!cur_end))
4756 return -1;
4757
4758 /* we have a full respnse and we know that we have either a CR
4759 * or an LF at <ptr>.
4760 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004761 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004762 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4763 /* there is no point trying this regex on headers */
4764 return 1;
4765 }
4766 }
4767 *cur_end = term; /* restore the string terminator */
4768 return done;
4769}
4770
4771
4772
4773/*
4774 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4775 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4776 * unparsable response.
4777 */
4778int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4779{
Willy Tarreau3d300592007-03-18 18:34:41 +01004780 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004781 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004782 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004783 int ret;
4784
4785 /*
4786 * The interleaving of transformations and verdicts
4787 * makes it difficult to decide to continue or stop
4788 * the evaluation.
4789 */
4790
Willy Tarreau3d300592007-03-18 18:34:41 +01004791 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004792 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4793 exp->action == ACT_PASS)) {
4794 exp = exp->next;
4795 continue;
4796 }
4797
4798 /* Apply the filter to the status line. */
4799 ret = apply_filter_to_sts_line(t, rtr, exp);
4800 if (unlikely(ret < 0))
4801 return -1;
4802
4803 if (likely(ret == 0)) {
4804 /* The filter did not match the response, it can be
4805 * iterated through all headers.
4806 */
4807 apply_filter_to_resp_headers(t, rtr, exp);
4808 }
4809 exp = exp->next;
4810 }
4811 return 0;
4812}
4813
4814
4815
4816/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004817 * Manage server-side cookies. It can impact performance by about 2% so it is
4818 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004819 */
4820void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4821{
4822 struct http_txn *txn = &t->txn;
4823 char *p1, *p2, *p3, *p4;
4824
4825 appsess *asession_temp = NULL;
4826 appsess local_asession;
4827
4828 char *cur_ptr, *cur_end, *cur_next;
4829 int cur_idx, old_idx, delta;
4830
Willy Tarreaua15645d2007-03-18 16:22:39 +01004831 /* Iterate through the headers.
4832 * we start with the start line.
4833 */
4834 old_idx = 0;
4835 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4836
4837 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4838 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004839 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004840
4841 cur_hdr = &txn->hdr_idx.v[cur_idx];
4842 cur_ptr = cur_next;
4843 cur_end = cur_ptr + cur_hdr->len;
4844 cur_next = cur_end + cur_hdr->cr + 1;
4845
4846 /* We have one full header between cur_ptr and cur_end, and the
4847 * next header starts at cur_next. We're only interested in
4848 * "Cookie:" headers.
4849 */
4850
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004851 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4852 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004853 old_idx = cur_idx;
4854 continue;
4855 }
4856
4857 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004858 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004859
4860
4861 /* maybe we only wanted to see if there was a set-cookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004862 if (t->be->cookie_name == NULL &&
4863 t->be->appsession_name == NULL &&
4864 t->be->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004865 return;
4866
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004867 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004868
4869 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004870 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4871 break;
4872
4873 /* p1 is at the beginning of the cookie name */
4874 p2 = p1;
4875
4876 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4877 p2++;
4878
4879 if (p2 == cur_end || *p2 == ';') /* next cookie */
4880 break;
4881
4882 p3 = p2 + 1; /* skip the '=' sign */
4883 if (p3 == cur_end)
4884 break;
4885
4886 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004887 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004888 p4++;
4889
4890 /* here, we have the cookie name between p1 and p2,
4891 * and its value between p3 and p4.
4892 * we can process it.
4893 */
4894
4895 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004896 if (t->be->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004897 txn->srv_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004898 (p4 - p1 >= t->be->capture_namelen) &&
4899 memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004900 int log_len = p4 - p1;
4901
Willy Tarreau086b3b42007-05-13 21:45:51 +02004902 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004903 Alert("HTTP logging : out of memory.\n");
4904 }
4905
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004906 if (log_len > t->be->capture_len)
4907 log_len = t->be->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004908 memcpy(txn->srv_cookie, p1, log_len);
4909 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004910 }
4911
4912 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004913 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4914 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004915 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004916 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004917
4918 /* If the cookie is in insert mode on a known server, we'll delete
4919 * this occurrence because we'll insert another one later.
4920 * We'll delete it too if the "indirect" option is set and we're in
4921 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004922 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4923 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004924 /* this header must be deleted */
4925 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4926 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4927 txn->hdr_idx.used--;
4928 cur_hdr->len = 0;
4929 cur_next += delta;
4930 txn->rsp.eoh += delta;
4931
Willy Tarreau3d300592007-03-18 18:34:41 +01004932 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004933 }
4934 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004935 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004936 /* replace bytes p3->p4 with the cookie name associated
4937 * with this server since we know it.
4938 */
4939 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4940 cur_hdr->len += delta;
4941 cur_next += delta;
4942 txn->rsp.eoh += delta;
4943
Willy Tarreau3d300592007-03-18 18:34:41 +01004944 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004945 }
4946 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004947 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004948 /* insert the cookie name associated with this server
4949 * before existing cookie, and insert a delimitor between them..
4950 */
4951 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4952 cur_hdr->len += delta;
4953 cur_next += delta;
4954 txn->rsp.eoh += delta;
4955
4956 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004957 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004958 }
4959 }
4960 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004961 else if ((t->be->appsession_name != NULL) &&
4962 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004963
4964 /* Cool... it's the right one */
4965
4966 size_t server_id_len = strlen(t->srv->id) + 1;
4967 asession_temp = &local_asession;
4968
Willy Tarreau63963c62007-05-13 21:29:55 +02004969 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004970 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4971 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4972 return;
4973 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004974 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4975 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004976 asession_temp->serverid = NULL;
4977
4978 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004979 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4980 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004981 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004982 Alert("Not enough Memory process_srv():asession:calloc().\n");
4983 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4984 return;
4985 }
4986 asession_temp->sessid = local_asession.sessid;
4987 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004988 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004989 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4990 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004991 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004992 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004993 }
4994
Willy Tarreaua15645d2007-03-18 16:22:39 +01004995 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004996 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004997 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4998 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4999 return;
5000 }
5001 asession_temp->serverid[0] = '\0';
5002 }
5003
5004 if (asession_temp->serverid[0] == '\0')
5005 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
5006
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005007 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005008 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005009#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005010 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005011 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01005012#endif
5013 }/* end if ((t->proxy->appsession_name != NULL) ... */
5014 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5015 } /* we're now at the end of the cookie value */
5016
5017 /* keep the link from this header to next one */
5018 old_idx = cur_idx;
5019 } /* end of cookie processing on this header */
5020}
5021
5022
5023
5024/*
5025 * Check if response is cacheable or not. Updates t->flags.
5026 */
5027void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5028{
5029 struct http_txn *txn = &t->txn;
5030 char *p1, *p2;
5031
5032 char *cur_ptr, *cur_end, *cur_next;
5033 int cur_idx;
5034
Willy Tarreau5df51872007-11-25 16:20:08 +01005035 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005036 return;
5037
5038 /* Iterate through the headers.
5039 * we start with the start line.
5040 */
5041 cur_idx = 0;
5042 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5043
5044 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5045 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005046 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005047
5048 cur_hdr = &txn->hdr_idx.v[cur_idx];
5049 cur_ptr = cur_next;
5050 cur_end = cur_ptr + cur_hdr->len;
5051 cur_next = cur_end + cur_hdr->cr + 1;
5052
5053 /* We have one full header between cur_ptr and cur_end, and the
5054 * next header starts at cur_next. We're only interested in
5055 * "Cookie:" headers.
5056 */
5057
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005058 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5059 if (val) {
5060 if ((cur_end - (cur_ptr + val) >= 8) &&
5061 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5062 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5063 return;
5064 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005065 }
5066
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005067 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5068 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005069 continue;
5070
5071 /* OK, right now we know we have a cache-control header at cur_ptr */
5072
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005073 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005074
5075 if (p1 >= cur_end) /* no more info */
5076 continue;
5077
5078 /* p1 is at the beginning of the value */
5079 p2 = p1;
5080
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005081 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005082 p2++;
5083
5084 /* we have a complete value between p1 and p2 */
5085 if (p2 < cur_end && *p2 == '=') {
5086 /* we have something of the form no-cache="set-cookie" */
5087 if ((cur_end - p1 >= 21) &&
5088 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5089 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005090 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005091 continue;
5092 }
5093
5094 /* OK, so we know that either p2 points to the end of string or to a comma */
5095 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5096 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5097 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5098 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005099 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005100 return;
5101 }
5102
5103 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005104 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005105 continue;
5106 }
5107 }
5108}
5109
5110
Willy Tarreau58f10d72006-12-04 02:26:12 +01005111/*
5112 * Try to retrieve a known appsession in the URI, then the associated server.
5113 * If the server is found, it's assigned to the session.
5114 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005115void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005116{
Willy Tarreau3d300592007-03-18 18:34:41 +01005117 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005118 appsess *asession_temp = NULL;
5119 appsess local_asession;
5120 char *request_line;
5121
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005122 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01005123 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005124 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005125 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005126 return;
5127
5128 /* skip ';' */
5129 request_line++;
5130
5131 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005132 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005133 return;
5134
5135 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005136 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005137
5138 /* First try if we already have an appsession */
5139 asession_temp = &local_asession;
5140
Willy Tarreau63963c62007-05-13 21:29:55 +02005141 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005142 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
5143 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
5144 return;
5145 }
5146
5147 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005148 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
5149 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005150 asession_temp->serverid = NULL;
5151
5152 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01005153 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
5154 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02005155 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005156 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005157 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005158 Alert("Not enough memory process_cli():asession:calloc().\n");
5159 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
5160 return;
5161 }
5162 asession_temp->sessid = local_asession.sessid;
5163 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005164 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02005165 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005166 }
5167 else {
5168 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02005169 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005170 }
Willy Tarreau51041c72007-09-09 21:56:53 +02005171
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005172 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005173 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02005174
Willy Tarreau58f10d72006-12-04 02:26:12 +01005175#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005176 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005177 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005178#endif
5179 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005180 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005181 Alert("Found Application Session without matching server.\n");
5182 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005183 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005184 while (srv) {
5185 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005186 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005187 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005188 txn->flags &= ~TX_CK_MASK;
5189 txn->flags |= TX_CK_VALID;
5190 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005191 t->srv = srv;
5192 break;
5193 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01005194 txn->flags &= ~TX_CK_MASK;
5195 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005196 }
5197 }
5198 srv = srv->next;
5199 }
5200 }
5201}
5202
5203
Willy Tarreaub2513902006-12-17 14:52:38 +01005204/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005205 * In a GET or HEAD request, check if the requested URI matches the stats uri
5206 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005207 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005208 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005209 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005210 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005211 *
5212 * Returns 1 if the session's state changes, otherwise 0.
5213 */
5214int stats_check_uri_auth(struct session *t, struct proxy *backend)
5215{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005216 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005217 struct uri_auth *uri_auth = backend->uri_auth;
5218 struct user_auth *user;
5219 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005220 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005221
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005222 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5223
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005224 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005225 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005226 return 0;
5227
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005228 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005229
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005230 /* the URI is in h */
5231 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005232 return 0;
5233
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005234 h += uri_auth->uri_len;
5235 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5236 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005237 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005238 break;
5239 }
5240 h++;
5241 }
5242
5243 if (uri_auth->refresh) {
5244 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5245 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5246 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005247 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005248 break;
5249 }
5250 h++;
5251 }
5252 }
5253
Willy Tarreau55bb8452007-10-17 18:44:57 +02005254 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5255 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5256 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005257 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005258 break;
5259 }
5260 h++;
5261 }
5262
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005263 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5264
Willy Tarreaub2513902006-12-17 14:52:38 +01005265 /* we are in front of a interceptable URI. Let's check
5266 * if there's an authentication and if it's valid.
5267 */
5268 user = uri_auth->users;
5269 if (!user) {
5270 /* no user auth required, it's OK */
5271 authenticated = 1;
5272 } else {
5273 authenticated = 0;
5274
5275 /* a user list is defined, we have to check.
5276 * skip 21 chars for "Authorization: Basic ".
5277 */
5278
5279 /* FIXME: this should move to an earlier place */
5280 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005281 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5282 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5283 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005284 if (len > 14 &&
5285 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005286 txn->auth_hdr.str = h;
5287 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005288 break;
5289 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005290 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005291 }
5292
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005293 if (txn->auth_hdr.len < 21 ||
5294 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005295 user = NULL;
5296
5297 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005298 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5299 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005300 user->user_pwd, user->user_len)) {
5301 authenticated = 1;
5302 break;
5303 }
5304 user = user->next;
5305 }
5306 }
5307
5308 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005309 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005310
5311 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01005312 msg.str = trash;
5313 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005314 txn->status = 401;
Willy Tarreau0f772532006-12-23 20:51:41 +01005315 client_retnclose(t, &msg);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005316 t->analysis &= ~AN_REQ_ANY;
Willy Tarreaub2513902006-12-17 14:52:38 +01005317 if (!(t->flags & SN_ERR_MASK))
5318 t->flags |= SN_ERR_PRXCOND;
5319 if (!(t->flags & SN_FINST_MASK))
5320 t->flags |= SN_FINST_R;
5321 return 1;
5322 }
5323
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005324 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005325 * data.
5326 */
Willy Tarreau284c7b32008-06-29 16:38:43 +02005327 EV_FD_CLR(t->cli_fd, DIR_RD);
5328 buffer_shutr(t->req);
5329 buffer_shutr(t->rep);
Willy Tarreaub2513902006-12-17 14:52:38 +01005330 t->cli_state = CL_STSHUTR;
5331 t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */
Willy Tarreau70089872008-06-13 21:12:51 +02005332 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005333 t->data_source = DATA_SRC_STATS;
5334 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005335 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub2513902006-12-17 14:52:38 +01005336 produce_content(t);
5337 return 1;
5338}
5339
5340
Willy Tarreaubaaee002006-06-26 02:48:02 +02005341/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005342 * Print a debug line with a header
5343 */
5344void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5345{
5346 int len, max;
5347 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
5348 dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd);
5349 max = end - start;
5350 UBOUND(max, sizeof(trash) - len - 1);
5351 len += strlcpy2(trash + len, start, max + 1);
5352 trash[len++] = '\n';
5353 write(1, trash, len);
5354}
5355
5356
Willy Tarreau8797c062007-05-07 00:55:35 +02005357/************************************************************************/
5358/* The code below is dedicated to ACL parsing and matching */
5359/************************************************************************/
5360
5361
5362
5363
5364/* 1. Check on METHOD
5365 * We use the pre-parsed method if it is known, and store its number as an
5366 * integer. If it is unknown, we use the pointer and the length.
5367 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005368static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005369{
5370 int len, meth;
5371
Willy Tarreauae8b7962007-06-09 23:10:04 +02005372 len = strlen(*text);
5373 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02005374
5375 pattern->val.i = meth;
5376 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02005377 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005378 if (!pattern->ptr.str)
5379 return 0;
5380 pattern->len = len;
5381 }
5382 return 1;
5383}
5384
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005385static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005386acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
5387 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005388{
5389 int meth;
5390 struct http_txn *txn = l7;
5391
Willy Tarreaub6866442008-07-14 23:54:42 +02005392 if (!txn)
5393 return 0;
5394
Willy Tarreauc11416f2007-06-17 16:58:38 +02005395 if (txn->req.msg_state != HTTP_MSG_BODY)
5396 return 0;
5397
Willy Tarreau8797c062007-05-07 00:55:35 +02005398 meth = txn->meth;
5399 test->i = meth;
5400 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02005401 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5402 /* ensure the indexes are not affected */
5403 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02005404 test->len = txn->req.sl.rq.m_l;
5405 test->ptr = txn->req.sol;
5406 }
5407 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5408 return 1;
5409}
5410
5411static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
5412{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005413 int icase;
5414
Willy Tarreau8797c062007-05-07 00:55:35 +02005415 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02005416 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02005417
5418 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02005419 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005420
5421 /* Other method, we must compare the strings */
5422 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02005423 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02005424
5425 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
5426 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
5427 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02005428 return ACL_PAT_FAIL;
5429 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02005430}
5431
5432/* 2. Check on Request/Status Version
5433 * We simply compare strings here.
5434 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005435static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005436{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005437 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005438 if (!pattern->ptr.str)
5439 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005440 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005441 return 1;
5442}
5443
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005444static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005445acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5446 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005447{
5448 struct http_txn *txn = l7;
5449 char *ptr;
5450 int len;
5451
Willy Tarreaub6866442008-07-14 23:54:42 +02005452 if (!txn)
5453 return 0;
5454
Willy Tarreauc11416f2007-06-17 16:58:38 +02005455 if (txn->req.msg_state != HTTP_MSG_BODY)
5456 return 0;
5457
Willy Tarreau8797c062007-05-07 00:55:35 +02005458 len = txn->req.sl.rq.v_l;
5459 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5460
5461 while ((len-- > 0) && (*ptr++ != '/'));
5462 if (len <= 0)
5463 return 0;
5464
5465 test->ptr = ptr;
5466 test->len = len;
5467
5468 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5469 return 1;
5470}
5471
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005472static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005473acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5474 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005475{
5476 struct http_txn *txn = l7;
5477 char *ptr;
5478 int len;
5479
Willy Tarreaub6866442008-07-14 23:54:42 +02005480 if (!txn)
5481 return 0;
5482
Willy Tarreauc11416f2007-06-17 16:58:38 +02005483 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5484 return 0;
5485
Willy Tarreau8797c062007-05-07 00:55:35 +02005486 len = txn->rsp.sl.st.v_l;
5487 ptr = txn->rsp.sol;
5488
5489 while ((len-- > 0) && (*ptr++ != '/'));
5490 if (len <= 0)
5491 return 0;
5492
5493 test->ptr = ptr;
5494 test->len = len;
5495
5496 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5497 return 1;
5498}
5499
5500/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005501static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005502acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5503 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005504{
5505 struct http_txn *txn = l7;
5506 char *ptr;
5507 int len;
5508
Willy Tarreaub6866442008-07-14 23:54:42 +02005509 if (!txn)
5510 return 0;
5511
Willy Tarreauc11416f2007-06-17 16:58:38 +02005512 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5513 return 0;
5514
Willy Tarreau8797c062007-05-07 00:55:35 +02005515 len = txn->rsp.sl.st.c_l;
5516 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5517
5518 test->i = __strl2ui(ptr, len);
5519 test->flags = ACL_TEST_F_VOL_1ST;
5520 return 1;
5521}
5522
5523/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005524static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005525acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5526 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005527{
5528 struct http_txn *txn = l7;
5529
Willy Tarreaub6866442008-07-14 23:54:42 +02005530 if (!txn)
5531 return 0;
5532
Willy Tarreauc11416f2007-06-17 16:58:38 +02005533 if (txn->req.msg_state != HTTP_MSG_BODY)
5534 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005535
Willy Tarreauc11416f2007-06-17 16:58:38 +02005536 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5537 /* ensure the indexes are not affected */
5538 return 0;
5539
Willy Tarreau8797c062007-05-07 00:55:35 +02005540 test->len = txn->req.sl.rq.u_l;
5541 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5542
Willy Tarreauf3d25982007-05-08 22:45:09 +02005543 /* we do not need to set READ_ONLY because the data is in a buffer */
5544 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005545 return 1;
5546}
5547
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005548static int
5549acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5550 struct acl_expr *expr, struct acl_test *test)
5551{
5552 struct http_txn *txn = l7;
5553
Willy Tarreaub6866442008-07-14 23:54:42 +02005554 if (!txn)
5555 return 0;
5556
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005557 if (txn->req.msg_state != HTTP_MSG_BODY)
5558 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005559
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005560 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5561 /* ensure the indexes are not affected */
5562 return 0;
5563
5564 /* Parse HTTP request */
5565 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5566 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5567 test->i = AF_INET;
5568
5569 /*
5570 * If we are parsing url in frontend space, we prepare backend stage
5571 * to not parse again the same url ! optimization lazyness...
5572 */
5573 if (px->options & PR_O_HTTP_PROXY)
5574 l4->flags |= SN_ADDR_SET;
5575
5576 test->flags = ACL_TEST_F_READ_ONLY;
5577 return 1;
5578}
5579
5580static int
5581acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5582 struct acl_expr *expr, struct acl_test *test)
5583{
5584 struct http_txn *txn = l7;
5585
Willy Tarreaub6866442008-07-14 23:54:42 +02005586 if (!txn)
5587 return 0;
5588
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005589 if (txn->req.msg_state != HTTP_MSG_BODY)
5590 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005591
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005592 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5593 /* ensure the indexes are not affected */
5594 return 0;
5595
5596 /* Same optimization as url_ip */
5597 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5598 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5599
5600 if (px->options & PR_O_HTTP_PROXY)
5601 l4->flags |= SN_ADDR_SET;
5602
5603 test->flags = ACL_TEST_F_READ_ONLY;
5604 return 1;
5605}
5606
Willy Tarreauc11416f2007-06-17 16:58:38 +02005607/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5608 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5609 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005610static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005611acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005612 struct acl_expr *expr, struct acl_test *test)
5613{
5614 struct http_txn *txn = l7;
5615 struct hdr_idx *idx = &txn->hdr_idx;
5616 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005617
Willy Tarreaub6866442008-07-14 23:54:42 +02005618 if (!txn)
5619 return 0;
5620
Willy Tarreau33a7e692007-06-10 19:45:56 +02005621 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5622 /* search for header from the beginning */
5623 ctx->idx = 0;
5624
Willy Tarreau33a7e692007-06-10 19:45:56 +02005625 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5626 test->flags |= ACL_TEST_F_FETCH_MORE;
5627 test->flags |= ACL_TEST_F_VOL_HDR;
5628 test->len = ctx->vlen;
5629 test->ptr = (char *)ctx->line + ctx->val;
5630 return 1;
5631 }
5632
5633 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5634 test->flags |= ACL_TEST_F_VOL_HDR;
5635 return 0;
5636}
5637
Willy Tarreau33a7e692007-06-10 19:45:56 +02005638static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005639acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5640 struct acl_expr *expr, struct acl_test *test)
5641{
5642 struct http_txn *txn = l7;
5643
Willy Tarreaub6866442008-07-14 23:54:42 +02005644 if (!txn)
5645 return 0;
5646
Willy Tarreauc11416f2007-06-17 16:58:38 +02005647 if (txn->req.msg_state != HTTP_MSG_BODY)
5648 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005649
Willy Tarreauc11416f2007-06-17 16:58:38 +02005650 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5651 /* ensure the indexes are not affected */
5652 return 0;
5653
5654 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5655}
5656
5657static int
5658acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5659 struct acl_expr *expr, struct acl_test *test)
5660{
5661 struct http_txn *txn = l7;
5662
Willy Tarreaub6866442008-07-14 23:54:42 +02005663 if (!txn)
5664 return 0;
5665
Willy Tarreauc11416f2007-06-17 16:58:38 +02005666 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5667 return 0;
5668
5669 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5670}
5671
5672/* 6. Check on HTTP header count. The number of occurrences is returned.
5673 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5674 */
5675static int
5676acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005677 struct acl_expr *expr, struct acl_test *test)
5678{
5679 struct http_txn *txn = l7;
5680 struct hdr_idx *idx = &txn->hdr_idx;
5681 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005682 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005683
Willy Tarreaub6866442008-07-14 23:54:42 +02005684 if (!txn)
5685 return 0;
5686
Willy Tarreau33a7e692007-06-10 19:45:56 +02005687 ctx.idx = 0;
5688 cnt = 0;
5689 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5690 cnt++;
5691
5692 test->i = cnt;
5693 test->flags = ACL_TEST_F_VOL_HDR;
5694 return 1;
5695}
5696
Willy Tarreauc11416f2007-06-17 16:58:38 +02005697static int
5698acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5699 struct acl_expr *expr, struct acl_test *test)
5700{
5701 struct http_txn *txn = l7;
5702
Willy Tarreaub6866442008-07-14 23:54:42 +02005703 if (!txn)
5704 return 0;
5705
Willy Tarreauc11416f2007-06-17 16:58:38 +02005706 if (txn->req.msg_state != HTTP_MSG_BODY)
5707 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005708
Willy Tarreauc11416f2007-06-17 16:58:38 +02005709 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5710 /* ensure the indexes are not affected */
5711 return 0;
5712
5713 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5714}
5715
5716static int
5717acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5718 struct acl_expr *expr, struct acl_test *test)
5719{
5720 struct http_txn *txn = l7;
5721
Willy Tarreaub6866442008-07-14 23:54:42 +02005722 if (!txn)
5723 return 0;
5724
Willy Tarreauc11416f2007-06-17 16:58:38 +02005725 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5726 return 0;
5727
5728 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5729}
5730
Willy Tarreau33a7e692007-06-10 19:45:56 +02005731/* 7. Check on HTTP header's integer value. The integer value is returned.
5732 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005733 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005734 */
5735static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005736acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005737 struct acl_expr *expr, struct acl_test *test)
5738{
5739 struct http_txn *txn = l7;
5740 struct hdr_idx *idx = &txn->hdr_idx;
5741 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005742
Willy Tarreaub6866442008-07-14 23:54:42 +02005743 if (!txn)
5744 return 0;
5745
Willy Tarreau33a7e692007-06-10 19:45:56 +02005746 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5747 /* search for header from the beginning */
5748 ctx->idx = 0;
5749
Willy Tarreau33a7e692007-06-10 19:45:56 +02005750 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5751 test->flags |= ACL_TEST_F_FETCH_MORE;
5752 test->flags |= ACL_TEST_F_VOL_HDR;
5753 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5754 return 1;
5755 }
5756
5757 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5758 test->flags |= ACL_TEST_F_VOL_HDR;
5759 return 0;
5760}
5761
Willy Tarreauc11416f2007-06-17 16:58:38 +02005762static int
5763acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5764 struct acl_expr *expr, struct acl_test *test)
5765{
5766 struct http_txn *txn = l7;
5767
Willy Tarreaub6866442008-07-14 23:54:42 +02005768 if (!txn)
5769 return 0;
5770
Willy Tarreauc11416f2007-06-17 16:58:38 +02005771 if (txn->req.msg_state != HTTP_MSG_BODY)
5772 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005773
Willy Tarreauc11416f2007-06-17 16:58:38 +02005774 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5775 /* ensure the indexes are not affected */
5776 return 0;
5777
5778 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5779}
5780
5781static int
5782acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5783 struct acl_expr *expr, struct acl_test *test)
5784{
5785 struct http_txn *txn = l7;
5786
Willy Tarreaub6866442008-07-14 23:54:42 +02005787 if (!txn)
5788 return 0;
5789
Willy Tarreauc11416f2007-06-17 16:58:38 +02005790 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5791 return 0;
5792
5793 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5794}
5795
Willy Tarreau737b0c12007-06-10 21:28:46 +02005796/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5797 * the first '/' after the possible hostname, and ends before the possible '?'.
5798 */
5799static int
5800acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5801 struct acl_expr *expr, struct acl_test *test)
5802{
5803 struct http_txn *txn = l7;
5804 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005805
Willy Tarreaub6866442008-07-14 23:54:42 +02005806 if (!txn)
5807 return 0;
5808
Willy Tarreauc11416f2007-06-17 16:58:38 +02005809 if (txn->req.msg_state != HTTP_MSG_BODY)
5810 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005811
Willy Tarreauc11416f2007-06-17 16:58:38 +02005812 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5813 /* ensure the indexes are not affected */
5814 return 0;
5815
Willy Tarreau21d2af32008-02-14 20:25:24 +01005816 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5817 ptr = http_get_path(txn);
5818 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005819 return 0;
5820
5821 /* OK, we got the '/' ! */
5822 test->ptr = ptr;
5823
5824 while (ptr < end && *ptr != '?')
5825 ptr++;
5826
5827 test->len = ptr - test->ptr;
5828
5829 /* we do not need to set READ_ONLY because the data is in a buffer */
5830 test->flags = ACL_TEST_F_VOL_1ST;
5831 return 1;
5832}
5833
5834
Willy Tarreau8797c062007-05-07 00:55:35 +02005835
5836/************************************************************************/
5837/* All supported keywords must be declared here. */
5838/************************************************************************/
5839
5840/* Note: must not be declared <const> as its list will be overwritten */
5841static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005842 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5843 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5844 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5845 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005846
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005847 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5848 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5849 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5850 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5851 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5852 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5853 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5854 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5855 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005856
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005857 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5858 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5859 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5860 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5861 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5862 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5863 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5864 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5865 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5866 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005867
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005868 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5869 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5870 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5871 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5872 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5873 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5874 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5875 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5876 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005877
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005878 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5879 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5880 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5881 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5882 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5883 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5884 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005885
Willy Tarreauf3d25982007-05-08 22:45:09 +02005886 { NULL, NULL, NULL, NULL },
5887
5888#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005889 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5890 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5891 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5892 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5893 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5894 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5895 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5896
Willy Tarreau8797c062007-05-07 00:55:35 +02005897 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5898 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5899 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5900 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5901 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5902 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5903 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5904 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5905
5906 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5907 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5908 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5909 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5910 { NULL, NULL, NULL, NULL },
5911#endif
5912}};
5913
5914
5915__attribute__((constructor))
5916static void __http_protocol_init(void)
5917{
5918 acl_register_keywords(&acl_kws);
5919}
5920
5921
Willy Tarreau58f10d72006-12-04 02:26:12 +01005922/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005923 * Local variables:
5924 * c-indent-level: 8
5925 * c-basic-offset: 8
5926 * End:
5927 */