blob: a68335ca0ca5ce80019e94649954fd707627eb11 [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>
Maik Broemme2850cb42009-04-17 18:53:21 +020044#include <proto/client.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/fd.h>
47#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010048#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020049#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010051#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010053#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010055#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020056#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/task.h>
58
Willy Tarreau1c47f852006-07-09 08:22:27 +020059/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010060const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020061 "HTTP/1.0 200 OK\r\n"
62 "Cache-Control: no-cache\r\n"
63 "Connection: close\r\n"
64 "Content-Type: text/html\r\n"
65 "\r\n"
66 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
67
Willy Tarreau0f772532006-12-23 20:51:41 +010068const struct chunk http_200_chunk = {
69 .str = (char *)&HTTP_200,
70 .len = sizeof(HTTP_200)-1
71};
72
Willy Tarreaub463dfb2008-06-07 23:08:56 +020073const char *HTTP_301 =
74 "HTTP/1.0 301 Moved Permantenly\r\n"
75 "Cache-Control: no-cache\r\n"
76 "Connection: close\r\n"
77 "Location: "; /* not terminated since it will be concatenated with the URL */
78
Willy Tarreau0f772532006-12-23 20:51:41 +010079const char *HTTP_302 =
80 "HTTP/1.0 302 Found\r\n"
81 "Cache-Control: no-cache\r\n"
82 "Connection: close\r\n"
83 "Location: "; /* not terminated since it will be concatenated with the URL */
84
85/* same as 302 except that the browser MUST retry with the GET method */
86const char *HTTP_303 =
87 "HTTP/1.0 303 See Other\r\n"
88 "Cache-Control: no-cache\r\n"
89 "Connection: close\r\n"
90 "Location: "; /* not terminated since it will be concatenated with the URL */
91
Willy Tarreaubaaee002006-06-26 02:48:02 +020092/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
93const char *HTTP_401_fmt =
94 "HTTP/1.0 401 Unauthorized\r\n"
95 "Cache-Control: no-cache\r\n"
96 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020097 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020098 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
99 "\r\n"
100 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
101
Willy Tarreau0f772532006-12-23 20:51:41 +0100102
103const int http_err_codes[HTTP_ERR_SIZE] = {
104 [HTTP_ERR_400] = 400,
105 [HTTP_ERR_403] = 403,
106 [HTTP_ERR_408] = 408,
107 [HTTP_ERR_500] = 500,
108 [HTTP_ERR_502] = 502,
109 [HTTP_ERR_503] = 503,
110 [HTTP_ERR_504] = 504,
111};
112
Willy Tarreau80587432006-12-24 17:47:20 +0100113static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100114 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100115 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100116 "Cache-Control: no-cache\r\n"
117 "Connection: close\r\n"
118 "Content-Type: text/html\r\n"
119 "\r\n"
120 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
121
122 [HTTP_ERR_403] =
123 "HTTP/1.0 403 Forbidden\r\n"
124 "Cache-Control: no-cache\r\n"
125 "Connection: close\r\n"
126 "Content-Type: text/html\r\n"
127 "\r\n"
128 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
129
130 [HTTP_ERR_408] =
131 "HTTP/1.0 408 Request Time-out\r\n"
132 "Cache-Control: no-cache\r\n"
133 "Connection: close\r\n"
134 "Content-Type: text/html\r\n"
135 "\r\n"
136 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
137
138 [HTTP_ERR_500] =
139 "HTTP/1.0 500 Server Error\r\n"
140 "Cache-Control: no-cache\r\n"
141 "Connection: close\r\n"
142 "Content-Type: text/html\r\n"
143 "\r\n"
144 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
145
146 [HTTP_ERR_502] =
147 "HTTP/1.0 502 Bad Gateway\r\n"
148 "Cache-Control: no-cache\r\n"
149 "Connection: close\r\n"
150 "Content-Type: text/html\r\n"
151 "\r\n"
152 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
153
154 [HTTP_ERR_503] =
155 "HTTP/1.0 503 Service Unavailable\r\n"
156 "Cache-Control: no-cache\r\n"
157 "Connection: close\r\n"
158 "Content-Type: text/html\r\n"
159 "\r\n"
160 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
161
162 [HTTP_ERR_504] =
163 "HTTP/1.0 504 Gateway Time-out\r\n"
164 "Cache-Control: no-cache\r\n"
165 "Connection: close\r\n"
166 "Content-Type: text/html\r\n"
167 "\r\n"
168 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
169
170};
171
Willy Tarreau80587432006-12-24 17:47:20 +0100172/* We must put the messages here since GCC cannot initialize consts depending
173 * on strlen().
174 */
175struct chunk http_err_chunks[HTTP_ERR_SIZE];
176
Willy Tarreau42250582007-04-01 01:30:43 +0200177#define FD_SETS_ARE_BITFIELDS
178#ifdef FD_SETS_ARE_BITFIELDS
179/*
180 * This map is used with all the FD_* macros to check whether a particular bit
181 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
182 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
183 * byte should be encoded. Be careful to always pass bytes from 0 to 255
184 * exclusively to the macros.
185 */
186fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
187fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
188
189#else
190#error "Check if your OS uses bitfields for fd_sets"
191#endif
192
Willy Tarreau80587432006-12-24 17:47:20 +0100193void init_proto_http()
194{
Willy Tarreau42250582007-04-01 01:30:43 +0200195 int i;
196 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100197 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200198
Willy Tarreau80587432006-12-24 17:47:20 +0100199 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
200 if (!http_err_msgs[msg]) {
201 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
202 abort();
203 }
204
205 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
206 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
207 }
Willy Tarreau42250582007-04-01 01:30:43 +0200208
209 /* initialize the log header encoding map : '{|}"#' should be encoded with
210 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
211 * URL encoding only requires '"', '#' to be encoded as well as non-
212 * printable characters above.
213 */
214 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
215 memset(url_encode_map, 0, sizeof(url_encode_map));
216 for (i = 0; i < 32; i++) {
217 FD_SET(i, hdr_encode_map);
218 FD_SET(i, url_encode_map);
219 }
220 for (i = 127; i < 256; i++) {
221 FD_SET(i, hdr_encode_map);
222 FD_SET(i, url_encode_map);
223 }
224
225 tmp = "\"#{|}";
226 while (*tmp) {
227 FD_SET(*tmp, hdr_encode_map);
228 tmp++;
229 }
230
231 tmp = "\"#";
232 while (*tmp) {
233 FD_SET(*tmp, url_encode_map);
234 tmp++;
235 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200236
237 /* memory allocations */
238 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200239 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100240}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200241
Willy Tarreau53b6c742006-12-17 13:37:46 +0100242/*
243 * We have 26 list of methods (1 per first letter), each of which can have
244 * up to 3 entries (2 valid, 1 null).
245 */
246struct http_method_desc {
247 http_meth_t meth;
248 int len;
249 const char text[8];
250};
251
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100252const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100253 ['C' - 'A'] = {
254 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
255 },
256 ['D' - 'A'] = {
257 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
258 },
259 ['G' - 'A'] = {
260 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
261 },
262 ['H' - 'A'] = {
263 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
264 },
265 ['P' - 'A'] = {
266 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
267 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
268 },
269 ['T' - 'A'] = {
270 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
271 },
272 /* rest is empty like this :
273 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
274 */
275};
276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100277/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200278 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100279 * RFC2616 for those chars.
280 */
281
282const char http_is_spht[256] = {
283 [' '] = 1, ['\t'] = 1,
284};
285
286const char http_is_crlf[256] = {
287 ['\r'] = 1, ['\n'] = 1,
288};
289
290const char http_is_lws[256] = {
291 [' '] = 1, ['\t'] = 1,
292 ['\r'] = 1, ['\n'] = 1,
293};
294
295const char http_is_sep[256] = {
296 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
297 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
298 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
299 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
300 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
301};
302
303const char http_is_ctl[256] = {
304 [0 ... 31] = 1,
305 [127] = 1,
306};
307
308/*
309 * A token is any ASCII char that is neither a separator nor a CTL char.
310 * Do not overwrite values in assignment since gcc-2.95 will not handle
311 * them correctly. Instead, define every non-CTL char's status.
312 */
313const char http_is_token[256] = {
314 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
315 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
316 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
317 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
318 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
319 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
320 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
321 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
322 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
323 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
324 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
325 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
326 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
327 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
328 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
329 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
330 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
331 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
332 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
333 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
334 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
335 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
336 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
337 ['|'] = 1, ['}'] = 0, ['~'] = 1,
338};
339
340
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100341/*
342 * An http ver_token is any ASCII which can be found in an HTTP version,
343 * which includes 'H', 'T', 'P', '/', '.' and any digit.
344 */
345const char http_is_ver_token[256] = {
346 ['.'] = 1, ['/'] = 1,
347 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
348 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
349 ['H'] = 1, ['P'] = 1, ['T'] = 1,
350};
351
352
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100353/*
354 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
355 * CRLF. Text length is measured first, so it cannot be NULL.
356 * The header is also automatically added to the index <hdr_idx>, and the end
357 * of headers is automatically adjusted. The number of bytes added is returned
358 * on success, otherwise <0 is returned indicating an error.
359 */
360int http_header_add_tail(struct buffer *b, struct http_msg *msg,
361 struct hdr_idx *hdr_idx, const char *text)
362{
363 int bytes, len;
364
365 len = strlen(text);
366 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
367 if (!bytes)
368 return -1;
369 msg->eoh += bytes;
370 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
371}
372
373/*
374 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
375 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
376 * the buffer is only opened and the space reserved, but nothing is copied.
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_tail2(struct buffer *b, struct http_msg *msg,
382 struct hdr_idx *hdr_idx, const char *text, int len)
383{
384 int bytes;
385
386 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
387 if (!bytes)
388 return -1;
389 msg->eoh += bytes;
390 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
391}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200392
393/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100394 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
395 * If so, returns the position of the first non-space character relative to
396 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
397 * to return a pointer to the place after the first space. Returns 0 if the
398 * header name does not match. Checks are case-insensitive.
399 */
400int http_header_match2(const char *hdr, const char *end,
401 const char *name, int len)
402{
403 const char *val;
404
405 if (hdr + len >= end)
406 return 0;
407 if (hdr[len] != ':')
408 return 0;
409 if (strncasecmp(hdr, name, len) != 0)
410 return 0;
411 val = hdr + len + 1;
412 while (val < end && HTTP_IS_SPHT(*val))
413 val++;
414 if ((val >= end) && (len + 2 <= end - hdr))
415 return len + 2; /* we may replace starting from second space */
416 return val - hdr;
417}
418
Willy Tarreau33a7e692007-06-10 19:45:56 +0200419/* Find the end of the header value contained between <s> and <e>.
420 * See RFC2616, par 2.2 for more information. Note that it requires
421 * a valid header to return a valid result.
422 */
423const char *find_hdr_value_end(const char *s, const char *e)
424{
425 int quoted, qdpair;
426
427 quoted = qdpair = 0;
428 for (; s < e; s++) {
429 if (qdpair) qdpair = 0;
430 else if (quoted && *s == '\\') qdpair = 1;
431 else if (quoted && *s == '"') quoted = 0;
432 else if (*s == '"') quoted = 1;
433 else if (*s == ',') return s;
434 }
435 return s;
436}
437
438/* Find the first or next occurrence of header <name> in message buffer <sol>
439 * using headers index <idx>, and return it in the <ctx> structure. This
440 * structure holds everything necessary to use the header and find next
441 * occurrence. If its <idx> member is 0, the header is searched from the
442 * beginning. Otherwise, the next occurrence is returned. The function returns
443 * 1 when it finds a value, and 0 when there is no more.
444 */
445int http_find_header2(const char *name, int len,
446 const char *sol, struct hdr_idx *idx,
447 struct hdr_ctx *ctx)
448{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200449 const char *eol, *sov;
450 int cur_idx;
451
452 if (ctx->idx) {
453 /* We have previously returned a value, let's search
454 * another one on the same line.
455 */
456 cur_idx = ctx->idx;
457 sol = ctx->line;
458 sov = sol + ctx->val + ctx->vlen;
459 eol = sol + idx->v[cur_idx].len;
460
461 if (sov >= eol)
462 /* no more values in this header */
463 goto next_hdr;
464
465 /* values remaining for this header, skip the comma */
466 sov++;
467 while (sov < eol && http_is_lws[(unsigned char)*sov])
468 sov++;
469
470 goto return_hdr;
471 }
472
473 /* first request for this header */
474 sol += hdr_idx_first_pos(idx);
475 cur_idx = hdr_idx_first_idx(idx);
476
477 while (cur_idx) {
478 eol = sol + idx->v[cur_idx].len;
479
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200480 if (len == 0) {
481 /* No argument was passed, we want any header.
482 * To achieve this, we simply build a fake request. */
483 while (sol + len < eol && sol[len] != ':')
484 len++;
485 name = sol;
486 }
487
Willy Tarreau33a7e692007-06-10 19:45:56 +0200488 if ((len < eol - sol) &&
489 (sol[len] == ':') &&
490 (strncasecmp(sol, name, len) == 0)) {
491
492 sov = sol + len + 1;
493 while (sov < eol && http_is_lws[(unsigned char)*sov])
494 sov++;
495 return_hdr:
496 ctx->line = sol;
497 ctx->idx = cur_idx;
498 ctx->val = sov - sol;
499
500 eol = find_hdr_value_end(sov, eol);
501 ctx->vlen = eol - sov;
502 return 1;
503 }
504 next_hdr:
505 sol = eol + idx->v[cur_idx].cr + 1;
506 cur_idx = idx->v[cur_idx].next;
507 }
508 return 0;
509}
510
511int http_find_header(const char *name,
512 const char *sol, struct hdr_idx *idx,
513 struct hdr_ctx *ctx)
514{
515 return http_find_header2(name, strlen(name), sol, idx, ctx);
516}
517
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100518/* This function handles a server error at the stream interface level. The
519 * stream interface is assumed to be already in a closed state. An optional
520 * message is copied into the input buffer, and an HTTP status code stored.
521 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100522 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200523 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100524static void http_server_error(struct session *t, struct stream_interface *si,
525 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526{
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100527 buffer_erase(si->ob);
528 buffer_erase(si->ib);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200529 buffer_auto_close(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100530 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100531 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100532 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200533 }
534 if (!(t->flags & SN_ERR_MASK))
535 t->flags |= err;
536 if (!(t->flags & SN_FINST_MASK))
537 t->flags |= finst;
538}
539
Willy Tarreau80587432006-12-24 17:47:20 +0100540/* This function returns the appropriate error location for the given session
541 * and message.
542 */
543
544struct chunk *error_message(struct session *s, int msgnum)
545{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200546 if (s->be->errmsg[msgnum].str)
547 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100548 else if (s->fe->errmsg[msgnum].str)
549 return &s->fe->errmsg[msgnum];
550 else
551 return &http_err_chunks[msgnum];
552}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200553
Willy Tarreau53b6c742006-12-17 13:37:46 +0100554/*
555 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
556 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
557 */
558static http_meth_t find_http_meth(const char *str, const int len)
559{
560 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100561 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100562
563 m = ((unsigned)*str - 'A');
564
565 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100566 for (h = http_methods[m]; h->len > 0; h++) {
567 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100568 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100569 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100570 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100571 };
572 return HTTP_METH_OTHER;
573 }
574 return HTTP_METH_NONE;
575
576}
577
Willy Tarreau21d2af32008-02-14 20:25:24 +0100578/* Parse the URI from the given transaction (which is assumed to be in request
579 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
580 * It is returned otherwise.
581 */
582static char *
583http_get_path(struct http_txn *txn)
584{
585 char *ptr, *end;
586
587 ptr = txn->req.sol + txn->req.sl.rq.u;
588 end = ptr + txn->req.sl.rq.u_l;
589
590 if (ptr >= end)
591 return NULL;
592
593 /* RFC2616, par. 5.1.2 :
594 * Request-URI = "*" | absuri | abspath | authority
595 */
596
597 if (*ptr == '*')
598 return NULL;
599
600 if (isalpha((unsigned char)*ptr)) {
601 /* this is a scheme as described by RFC3986, par. 3.1 */
602 ptr++;
603 while (ptr < end &&
604 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
605 ptr++;
606 /* skip '://' */
607 if (ptr == end || *ptr++ != ':')
608 return NULL;
609 if (ptr == end || *ptr++ != '/')
610 return NULL;
611 if (ptr == end || *ptr++ != '/')
612 return NULL;
613 }
614 /* skip [user[:passwd]@]host[:[port]] */
615
616 while (ptr < end && *ptr != '/')
617 ptr++;
618
619 if (ptr == end)
620 return NULL;
621
622 /* OK, we got the '/' ! */
623 return ptr;
624}
625
Willy Tarreauefb453c2008-10-26 20:49:47 +0100626/* Returns a 302 for a redirectable request. This may only be called just after
627 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
628 * left unchanged and will follow normal proxy processing.
629 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100630void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100631{
632 struct http_txn *txn;
633 struct chunk rdr;
634 char *path;
635 int len;
636
637 /* 1: create the response header */
638 rdr.len = strlen(HTTP_302);
639 rdr.str = trash;
640 memcpy(rdr.str, HTTP_302, rdr.len);
641
642 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200643 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100644 return;
645
646 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
647 rdr.len += s->srv->rdr_len;
648
649 /* 3: add the request URI */
650 txn = &s->txn;
651 path = http_get_path(txn);
652 if (!path)
653 return;
654
655 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200656 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100657 return;
658
659 memcpy(rdr.str + rdr.len, path, len);
660 rdr.len += len;
661 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
662 rdr.len += 4;
663
664 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100665 si->shutr(si);
666 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100667 si->err_type = SI_ET_NONE;
668 si->err_loc = NULL;
669 si->state = SI_ST_CLO;
670
671 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100672 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100673
674 /* FIXME: we should increase a counter of redirects per server and per backend. */
675 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100676 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100677}
678
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100679/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100680 * that the server side is closed. Note that err_type is actually a
681 * bitmask, where almost only aborts may be cumulated with other
682 * values. We consider that aborted operations are more important
683 * than timeouts or errors due to the fact that nobody else in the
684 * logs might explain incomplete retries. All others should avoid
685 * being cumulated. It should normally not be possible to have multiple
686 * aborts at once, but just in case, the first one in sequence is reported.
687 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100688void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100689{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100690 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100691
692 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100693 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
694 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100695 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100696 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
697 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100698 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100699 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
700 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100701 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100702 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
703 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100704 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100705 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
706 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100707 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100708 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
709 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100710 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100711 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
712 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100713}
714
Willy Tarreau42250582007-04-01 01:30:43 +0200715extern const char sess_term_cond[8];
716extern const char sess_fin_state[8];
717extern const char *monthname[12];
718const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
719const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
720 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
721 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200722struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200723struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100724
Emeric Brun3a058f32009-06-30 18:26:00 +0200725void http_sess_clflog(struct session *s)
726{
727 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
728 struct proxy *fe = s->fe;
729 struct proxy *be = s->be;
730 struct proxy *prx_log;
731 struct http_txn *txn = &s->txn;
732 int tolog, level, err;
733 char *uri, *h;
734 char *svid;
735 struct tm tm;
736 static char tmpline[MAX_SYSLOG_LEN];
737 int hdr;
738 size_t w;
739 int t_request;
740
741 prx_log = fe;
742 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
743 (s->conn_retries != be->conn_retries) ||
744 txn->status >= 500;
745
746 if (s->cli_addr.ss_family == AF_INET)
747 inet_ntop(AF_INET,
748 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
749 pn, sizeof(pn));
750 else
751 inet_ntop(AF_INET6,
752 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
753 pn, sizeof(pn));
754
755 get_gmtime(s->logs.accept_date.tv_sec, &tm);
756
757 /* FIXME: let's limit ourselves to frontend logging for now. */
758 tolog = fe->to_log;
759
760 h = tmpline;
761
762 w = snprintf(h, sizeof(tmpline),
763 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
764 pn,
765 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
766 tm.tm_hour, tm.tm_min, tm.tm_sec);
767 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
768 goto trunc;
769 h += w;
770
771 if (h >= tmpline + sizeof(tmpline) - 4)
772 goto trunc;
773
774 *(h++) = ' ';
775 *(h++) = '\"';
776 uri = txn->uri ? txn->uri : "<BADREQ>";
777 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
778 '#', url_encode_map, uri);
779 *(h++) = '\"';
780
781 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
782 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
783 goto trunc;
784 h += w;
785
786 if (h >= tmpline + sizeof(tmpline) - 9)
787 goto trunc;
788 memcpy(h, " \"-\" \"-\"", 8);
789 h += 8;
790
791 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
792 " %d %03d",
793 (s->cli_addr.ss_family == AF_INET) ?
794 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
795 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
796 (int)s->logs.accept_date.tv_usec/1000);
797 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
798 goto trunc;
799 h += w;
800
801 w = strlen(fe->id);
802 if (h >= tmpline + sizeof(tmpline) - 4 - w)
803 goto trunc;
804 *(h++) = ' ';
805 *(h++) = '\"';
806 memcpy(h, fe->id, w);
807 h += w;
808 *(h++) = '\"';
809
810 w = strlen(be->id);
811 if (h >= tmpline + sizeof(tmpline) - 4 - w)
812 goto trunc;
813 *(h++) = ' ';
814 *(h++) = '\"';
815 memcpy(h, be->id, w);
816 h += w;
817 *(h++) = '\"';
818
819 svid = (tolog & LW_SVID) ?
820 (s->data_source != DATA_SRC_STATS) ?
821 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
822
823 w = strlen(svid);
824 if (h >= tmpline + sizeof(tmpline) - 4 - w)
825 goto trunc;
826 *(h++) = ' ';
827 *(h++) = '\"';
828 memcpy(h, svid, w);
829 h += w;
830 *(h++) = '\"';
831
832 t_request = -1;
833 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
834 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
835 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
836 " %d %ld %ld %ld %ld",
837 t_request,
838 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
839 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
840 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
841 s->logs.t_close);
842 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
843 goto trunc;
844 h += w;
845
846 if (h >= tmpline + sizeof(tmpline) - 8)
847 goto trunc;
848 *(h++) = ' ';
849 *(h++) = '\"';
850 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
851 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
852 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
853 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
854 *(h++) = '\"';
855
856 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
857 " %d %d %d %d %d %ld %ld",
858 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
859 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
860 s->logs.srv_queue_size, s->logs.prx_queue_size);
861
862 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
863 goto trunc;
864 h += w;
865
866 if (txn->cli_cookie) {
867 w = strlen(txn->cli_cookie);
868 if (h >= tmpline + sizeof(tmpline) - 4 - w)
869 goto trunc;
870 *(h++) = ' ';
871 *(h++) = '\"';
872 memcpy(h, txn->cli_cookie, w);
873 h += w;
874 *(h++) = '\"';
875 } else {
876 if (h >= tmpline + sizeof(tmpline) - 5)
877 goto trunc;
878 memcpy(h, " \"-\"", 4);
879 h += 4;
880 }
881
882 if (txn->srv_cookie) {
883 w = strlen(txn->srv_cookie);
884 if (h >= tmpline + sizeof(tmpline) - 4 - w)
885 goto trunc;
886 *(h++) = ' ';
887 *(h++) = '\"';
888 memcpy(h, txn->srv_cookie, w);
889 h += w;
890 *(h++) = '\"';
891 } else {
892 if (h >= tmpline + sizeof(tmpline) - 5)
893 goto trunc;
894 memcpy(h, " \"-\"", 4);
895 h += 4;
896 }
897
898 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
899 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
900 if (h >= sizeof (tmpline) + tmpline - 4)
901 goto trunc;
902 *(h++) = ' ';
903 *(h++) = '\"';
904 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
905 '#', hdr_encode_map, txn->req.cap[hdr]);
906 *(h++) = '\"';
907 }
908 }
909
910 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
911 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
912 if (h >= sizeof (tmpline) + tmpline - 4)
913 goto trunc;
914 *(h++) = ' ';
915 *(h++) = '\"';
916 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
917 '#', hdr_encode_map, txn->rsp.cap[hdr]);
918 *(h++) = '\"';
919 }
920 }
921
922trunc:
923 *h = '\0';
924
925 level = LOG_INFO;
926 if (err && (fe->options2 & PR_O2_LOGERRORS))
927 level = LOG_ERR;
928
929 send_log(prx_log, level, "%s\n", tmpline);
930
931 s->logs.logwait = 0;
932}
933
Willy Tarreau42250582007-04-01 01:30:43 +0200934/*
935 * send a log for the session when we have enough info about it.
936 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100937 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100938void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200939{
940 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
941 struct proxy *fe = s->fe;
942 struct proxy *be = s->be;
943 struct proxy *prx_log;
944 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200945 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200946 char *uri, *h;
947 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200948 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200949 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200950 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200951 int hdr;
952
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200953 /* if we don't want to log normal traffic, return now */
954 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
955 (s->conn_retries != be->conn_retries) ||
956 txn->status >= 500;
957 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
958 return;
959
Willy Tarreau42250582007-04-01 01:30:43 +0200960 if (fe->logfac1 < 0 && fe->logfac2 < 0)
961 return;
962 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100963
Emeric Brun3a058f32009-06-30 18:26:00 +0200964 if (prx_log->options2 & PR_O2_CLFLOG)
965 return http_sess_clflog(s);
966
Willy Tarreau42250582007-04-01 01:30:43 +0200967 if (s->cli_addr.ss_family == AF_INET)
968 inet_ntop(AF_INET,
969 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
970 pn, sizeof(pn));
971 else
972 inet_ntop(AF_INET6,
973 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
974 pn, sizeof(pn));
975
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200976 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200977
978 /* FIXME: let's limit ourselves to frontend logging for now. */
979 tolog = fe->to_log;
980
981 h = tmpline;
982 if (fe->to_log & LW_REQHDR &&
983 txn->req.cap &&
984 (h < tmpline + sizeof(tmpline) - 10)) {
985 *(h++) = ' ';
986 *(h++) = '{';
987 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
988 if (hdr)
989 *(h++) = '|';
990 if (txn->req.cap[hdr] != NULL)
991 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
992 '#', hdr_encode_map, txn->req.cap[hdr]);
993 }
994 *(h++) = '}';
995 }
996
997 if (fe->to_log & LW_RSPHDR &&
998 txn->rsp.cap &&
999 (h < tmpline + sizeof(tmpline) - 7)) {
1000 *(h++) = ' ';
1001 *(h++) = '{';
1002 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1003 if (hdr)
1004 *(h++) = '|';
1005 if (txn->rsp.cap[hdr] != NULL)
1006 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1007 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1008 }
1009 *(h++) = '}';
1010 }
1011
1012 if (h < tmpline + sizeof(tmpline) - 4) {
1013 *(h++) = ' ';
1014 *(h++) = '"';
1015 uri = txn->uri ? txn->uri : "<BADREQ>";
1016 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1017 '#', url_encode_map, uri);
1018 *(h++) = '"';
1019 }
1020 *h = '\0';
1021
1022 svid = (tolog & LW_SVID) ?
1023 (s->data_source != DATA_SRC_STATS) ?
1024 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1025
Willy Tarreau70089872008-06-13 21:12:51 +02001026 t_request = -1;
1027 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1028 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1029
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001030 level = LOG_INFO;
1031 if (err && (fe->options2 & PR_O2_LOGERRORS))
1032 level = LOG_ERR;
1033
1034 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001035 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001036 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1037 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001038 pn,
1039 (s->cli_addr.ss_family == AF_INET) ?
1040 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1041 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001042 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001043 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001044 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001045 t_request,
1046 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001047 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1048 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1049 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1050 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001051 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001052 txn->cli_cookie ? txn->cli_cookie : "-",
1053 txn->srv_cookie ? txn->srv_cookie : "-",
1054 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1055 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1056 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1057 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1058 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001059 (s->flags & SN_REDISP)?"+":"",
1060 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001061 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1062
1063 s->logs.logwait = 0;
1064}
1065
Willy Tarreau117f59e2007-03-04 18:17:17 +01001066
1067/*
1068 * Capture headers from message starting at <som> according to header list
1069 * <cap_hdr>, and fill the <idx> structure appropriately.
1070 */
1071void capture_headers(char *som, struct hdr_idx *idx,
1072 char **cap, struct cap_hdr *cap_hdr)
1073{
1074 char *eol, *sol, *col, *sov;
1075 int cur_idx;
1076 struct cap_hdr *h;
1077 int len;
1078
1079 sol = som + hdr_idx_first_pos(idx);
1080 cur_idx = hdr_idx_first_idx(idx);
1081
1082 while (cur_idx) {
1083 eol = sol + idx->v[cur_idx].len;
1084
1085 col = sol;
1086 while (col < eol && *col != ':')
1087 col++;
1088
1089 sov = col + 1;
1090 while (sov < eol && http_is_lws[(unsigned char)*sov])
1091 sov++;
1092
1093 for (h = cap_hdr; h; h = h->next) {
1094 if ((h->namelen == col - sol) &&
1095 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1096 if (cap[h->index] == NULL)
1097 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001098 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001099
1100 if (cap[h->index] == NULL) {
1101 Alert("HTTP capture : out of memory.\n");
1102 continue;
1103 }
1104
1105 len = eol - sov;
1106 if (len > h->len)
1107 len = h->len;
1108
1109 memcpy(cap[h->index], sov, len);
1110 cap[h->index][len]=0;
1111 }
1112 }
1113 sol = eol + idx->v[cur_idx].cr + 1;
1114 cur_idx = idx->v[cur_idx].next;
1115 }
1116}
1117
1118
Willy Tarreau42250582007-04-01 01:30:43 +02001119/* either we find an LF at <ptr> or we jump to <bad>.
1120 */
1121#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1122
1123/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1124 * otherwise to <http_msg_ood> with <state> set to <st>.
1125 */
1126#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1127 ptr++; \
1128 if (likely(ptr < end)) \
1129 goto good; \
1130 else { \
1131 state = (st); \
1132 goto http_msg_ood; \
1133 } \
1134 } while (0)
1135
1136
Willy Tarreaubaaee002006-06-26 02:48:02 +02001137/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001138 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001139 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1140 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1141 * will give undefined results.
1142 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1143 * and that msg->sol points to the beginning of the response.
1144 * If a complete line is found (which implies that at least one CR or LF is
1145 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1146 * returned indicating an incomplete line (which does not mean that parts have
1147 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1148 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1149 * upon next call.
1150 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001151 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001152 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1153 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001154 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001155 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001156const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1157 unsigned int state, const char *ptr, const char *end,
1158 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001159{
Willy Tarreau8973c702007-01-21 23:58:29 +01001160 switch (state) {
1161 http_msg_rpver:
1162 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001163 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001164 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1165
1166 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001167 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1169 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001170 state = HTTP_MSG_ERROR;
1171 break;
1172
Willy Tarreau8973c702007-01-21 23:58:29 +01001173 http_msg_rpver_sp:
1174 case HTTP_MSG_RPVER_SP:
1175 if (likely(!HTTP_IS_LWS(*ptr))) {
1176 msg->sl.st.c = ptr - msg_buf;
1177 goto http_msg_rpcode;
1178 }
1179 if (likely(HTTP_IS_SPHT(*ptr)))
1180 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1181 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001182 state = HTTP_MSG_ERROR;
1183 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001184
1185 http_msg_rpcode:
1186 case HTTP_MSG_RPCODE:
1187 if (likely(!HTTP_IS_LWS(*ptr)))
1188 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1189
1190 if (likely(HTTP_IS_SPHT(*ptr))) {
1191 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1192 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1193 }
1194
1195 /* so it's a CR/LF, so there is no reason phrase */
1196 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1197 http_msg_rsp_reason:
1198 /* FIXME: should we support HTTP responses without any reason phrase ? */
1199 msg->sl.st.r = ptr - msg_buf;
1200 msg->sl.st.r_l = 0;
1201 goto http_msg_rpline_eol;
1202
1203 http_msg_rpcode_sp:
1204 case HTTP_MSG_RPCODE_SP:
1205 if (likely(!HTTP_IS_LWS(*ptr))) {
1206 msg->sl.st.r = ptr - msg_buf;
1207 goto http_msg_rpreason;
1208 }
1209 if (likely(HTTP_IS_SPHT(*ptr)))
1210 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1211 /* so it's a CR/LF, so there is no reason phrase */
1212 goto http_msg_rsp_reason;
1213
1214 http_msg_rpreason:
1215 case HTTP_MSG_RPREASON:
1216 if (likely(!HTTP_IS_CRLF(*ptr)))
1217 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1218 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1219 http_msg_rpline_eol:
1220 /* We have seen the end of line. Note that we do not
1221 * necessarily have the \n yet, but at least we know that we
1222 * have EITHER \r OR \n, otherwise the response would not be
1223 * complete. We can then record the response length and return
1224 * to the caller which will be able to register it.
1225 */
1226 msg->sl.st.l = ptr - msg->sol;
1227 return ptr;
1228
1229#ifdef DEBUG_FULL
1230 default:
1231 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1232 exit(1);
1233#endif
1234 }
1235
1236 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001237 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001238 if (ret_state)
1239 *ret_state = state;
1240 if (ret_ptr)
1241 *ret_ptr = (char *)ptr;
1242 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001243}
1244
1245
1246/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 * This function parses a request line between <ptr> and <end>, starting with
1248 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1249 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1250 * will give undefined results.
1251 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1252 * and that msg->sol points to the beginning of the request.
1253 * If a complete line is found (which implies that at least one CR or LF is
1254 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1255 * returned indicating an incomplete line (which does not mean that parts have
1256 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1257 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1258 * upon next call.
1259 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001260 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1262 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001263 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001264 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001265const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1266 unsigned int state, const char *ptr, const char *end,
1267 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001268{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001269 switch (state) {
1270 http_msg_rqmeth:
1271 case HTTP_MSG_RQMETH:
1272 if (likely(HTTP_IS_TOKEN(*ptr)))
1273 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001274
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001276 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1278 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001279
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001280 if (likely(HTTP_IS_CRLF(*ptr))) {
1281 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001282 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001283 http_msg_req09_uri:
1284 msg->sl.rq.u = ptr - msg_buf;
1285 http_msg_req09_uri_e:
1286 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1287 http_msg_req09_ver:
1288 msg->sl.rq.v = ptr - msg_buf;
1289 msg->sl.rq.v_l = 0;
1290 goto http_msg_rqline_eol;
1291 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001292 state = HTTP_MSG_ERROR;
1293 break;
1294
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 http_msg_rqmeth_sp:
1296 case HTTP_MSG_RQMETH_SP:
1297 if (likely(!HTTP_IS_LWS(*ptr))) {
1298 msg->sl.rq.u = ptr - msg_buf;
1299 goto http_msg_rquri;
1300 }
1301 if (likely(HTTP_IS_SPHT(*ptr)))
1302 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1303 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1304 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 http_msg_rquri:
1307 case HTTP_MSG_RQURI:
1308 if (likely(!HTTP_IS_LWS(*ptr)))
1309 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 if (likely(HTTP_IS_SPHT(*ptr))) {
1312 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1313 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1314 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001315
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1317 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001318
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 http_msg_rquri_sp:
1320 case HTTP_MSG_RQURI_SP:
1321 if (likely(!HTTP_IS_LWS(*ptr))) {
1322 msg->sl.rq.v = ptr - msg_buf;
1323 goto http_msg_rqver;
1324 }
1325 if (likely(HTTP_IS_SPHT(*ptr)))
1326 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1327 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1328 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 http_msg_rqver:
1331 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001332 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001333 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001334
1335 if (likely(HTTP_IS_CRLF(*ptr))) {
1336 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1337 http_msg_rqline_eol:
1338 /* We have seen the end of line. Note that we do not
1339 * necessarily have the \n yet, but at least we know that we
1340 * have EITHER \r OR \n, otherwise the request would not be
1341 * complete. We can then record the request length and return
1342 * to the caller which will be able to register it.
1343 */
1344 msg->sl.rq.l = ptr - msg->sol;
1345 return ptr;
1346 }
1347
1348 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001349 state = HTTP_MSG_ERROR;
1350 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001351
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352#ifdef DEBUG_FULL
1353 default:
1354 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1355 exit(1);
1356#endif
1357 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001358
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001359 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001360 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001361 if (ret_state)
1362 *ret_state = state;
1363 if (ret_ptr)
1364 *ret_ptr = (char *)ptr;
1365 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001366}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001367
1368
Willy Tarreau8973c702007-01-21 23:58:29 +01001369/*
1370 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001371 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001372 * when data are missing and recalled at the exact same location with no
1373 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001374 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1375 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001376 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001377void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1378{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001379 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001381
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001382 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 ptr = buf->lr;
1384 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001385
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 if (unlikely(ptr >= end))
1387 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001390 /*
1391 * First, states that are specific to the response only.
1392 * We check them first so that request and headers are
1393 * closer to each other (accessed more often).
1394 */
1395 http_msg_rpbefore:
1396 case HTTP_MSG_RPBEFORE:
1397 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau816b9792009-09-15 21:25:21 +02001398#if !defined(PARSE_PRESERVE_EMPTY_LINES)
1399 if (likely(ptr != buf->data)) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001400 /* Remove empty leading lines, as recommended by
1401 * RFC2616. This takes a lot of time because we
1402 * must move all the buffer backwards, but this
1403 * is rarely needed. The method above will be
1404 * cleaner when we'll be able to start sending
1405 * the request from any place in the buffer.
1406 */
Willy Tarreau816b9792009-09-15 21:25:21 +02001407 ptr += buffer_replace2(buf, buf->lr, ptr, NULL, 0);
Willy Tarreau8973c702007-01-21 23:58:29 +01001408 end = buf->r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001409 }
Willy Tarreau816b9792009-09-15 21:25:21 +02001410#endif
1411 msg->sol = ptr;
1412 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001413 hdr_idx_init(idx);
1414 state = HTTP_MSG_RPVER;
1415 goto http_msg_rpver;
1416 }
1417
1418 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1419 goto http_msg_invalid;
1420
1421 if (unlikely(*ptr == '\n'))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1423 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1424 /* stop here */
1425
1426 http_msg_rpbefore_cr:
1427 case HTTP_MSG_RPBEFORE_CR:
1428 EXPECT_LF_HERE(ptr, http_msg_invalid);
1429 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1430 /* stop here */
1431
1432 http_msg_rpver:
1433 case HTTP_MSG_RPVER:
1434 case HTTP_MSG_RPVER_SP:
1435 case HTTP_MSG_RPCODE:
1436 case HTTP_MSG_RPCODE_SP:
1437 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001438 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001439 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001440 if (unlikely(!ptr))
1441 return;
1442
1443 /* we have a full response and we know that we have either a CR
1444 * or an LF at <ptr>.
1445 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001446 //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 +01001447 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1448
1449 msg->sol = ptr;
1450 if (likely(*ptr == '\r'))
1451 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1452 goto http_msg_rpline_end;
1453
1454 http_msg_rpline_end:
1455 case HTTP_MSG_RPLINE_END:
1456 /* msg->sol must point to the first of CR or LF. */
1457 EXPECT_LF_HERE(ptr, http_msg_invalid);
1458 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1459 /* stop here */
1460
1461 /*
1462 * Second, states that are specific to the request only
1463 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 http_msg_rqbefore:
1465 case HTTP_MSG_RQBEFORE:
1466 if (likely(HTTP_IS_TOKEN(*ptr))) {
1467 if (likely(ptr == buf->data)) {
1468 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001469 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 } else {
1471#if PARSE_PRESERVE_EMPTY_LINES
1472 /* only skip empty leading lines, don't remove them */
1473 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001474 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001475#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 /* Remove empty leading lines, as recommended by
1477 * RFC2616. This takes a lot of time because we
1478 * must move all the buffer backwards, but this
1479 * is rarely needed. The method above will be
1480 * cleaner when we'll be able to start sending
1481 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001482 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 buf->lr = ptr;
1484 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001485 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 msg->sol = buf->data;
1487 ptr = buf->data;
1488 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001489#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001491 /* we will need this when keep-alive will be supported
1492 hdr_idx_init(idx);
1493 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001494 state = HTTP_MSG_RQMETH;
1495 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001497
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1499 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001500
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 if (unlikely(*ptr == '\n'))
1502 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1503 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001504 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001505
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 http_msg_rqbefore_cr:
1507 case HTTP_MSG_RQBEFORE_CR:
1508 EXPECT_LF_HERE(ptr, http_msg_invalid);
1509 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001510 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001511
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 http_msg_rqmeth:
1513 case HTTP_MSG_RQMETH:
1514 case HTTP_MSG_RQMETH_SP:
1515 case HTTP_MSG_RQURI:
1516 case HTTP_MSG_RQURI_SP:
1517 case HTTP_MSG_RQVER:
1518 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001519 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 if (unlikely(!ptr))
1521 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001522
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 /* we have a full request and we know that we have either a CR
1524 * or an LF at <ptr>.
1525 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001526 //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 +01001527 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 msg->sol = ptr;
1530 if (likely(*ptr == '\r'))
1531 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001533
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 http_msg_rqline_end:
1535 case HTTP_MSG_RQLINE_END:
1536 /* check for HTTP/0.9 request : no version information available.
1537 * msg->sol must point to the first of CR or LF.
1538 */
1539 if (unlikely(msg->sl.rq.v_l == 0))
1540 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001541
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 EXPECT_LF_HERE(ptr, http_msg_invalid);
1543 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001544 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001545
Willy Tarreau8973c702007-01-21 23:58:29 +01001546 /*
1547 * Common states below
1548 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 http_msg_hdr_first:
1550 case HTTP_MSG_HDR_FIRST:
1551 msg->sol = ptr;
1552 if (likely(!HTTP_IS_CRLF(*ptr))) {
1553 goto http_msg_hdr_name;
1554 }
1555
1556 if (likely(*ptr == '\r'))
1557 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1558 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 http_msg_hdr_name:
1561 case HTTP_MSG_HDR_NAME:
1562 /* assumes msg->sol points to the first char */
1563 if (likely(HTTP_IS_TOKEN(*ptr)))
1564 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001565
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 if (likely(*ptr == ':')) {
1567 msg->col = ptr - buf->data;
1568 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1569 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001570
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001571 if (likely(msg->err_pos < -1) || *ptr == '\n')
1572 goto http_msg_invalid;
1573
1574 if (msg->err_pos == -1) /* capture error pointer */
1575 msg->err_pos = ptr - buf->data; /* >= 0 now */
1576
1577 /* and we still accept this non-token character */
1578 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001579
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 http_msg_hdr_l1_sp:
1581 case HTTP_MSG_HDR_L1_SP:
1582 /* assumes msg->sol points to the first char and msg->col to the colon */
1583 if (likely(HTTP_IS_SPHT(*ptr)))
1584 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001585
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 /* header value can be basically anything except CR/LF */
1587 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001588
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 if (likely(!HTTP_IS_CRLF(*ptr))) {
1590 goto http_msg_hdr_val;
1591 }
1592
1593 if (likely(*ptr == '\r'))
1594 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1595 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001596
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 http_msg_hdr_l1_lf:
1598 case HTTP_MSG_HDR_L1_LF:
1599 EXPECT_LF_HERE(ptr, http_msg_invalid);
1600 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001601
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602 http_msg_hdr_l1_lws:
1603 case HTTP_MSG_HDR_L1_LWS:
1604 if (likely(HTTP_IS_SPHT(*ptr))) {
1605 /* replace HT,CR,LF with spaces */
1606 for (; buf->data+msg->sov < ptr; msg->sov++)
1607 buf->data[msg->sov] = ' ';
1608 goto http_msg_hdr_l1_sp;
1609 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001610 /* we had a header consisting only in spaces ! */
1611 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001612 goto http_msg_complete_header;
1613
1614 http_msg_hdr_val:
1615 case HTTP_MSG_HDR_VAL:
1616 /* assumes msg->sol points to the first char, msg->col to the
1617 * colon, and msg->sov points to the first character of the
1618 * value.
1619 */
1620 if (likely(!HTTP_IS_CRLF(*ptr)))
1621 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 msg->eol = ptr;
1624 /* Note: we could also copy eol into ->eoh so that we have the
1625 * real header end in case it ends with lots of LWS, but is this
1626 * really needed ?
1627 */
1628 if (likely(*ptr == '\r'))
1629 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1630 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 http_msg_hdr_l2_lf:
1633 case HTTP_MSG_HDR_L2_LF:
1634 EXPECT_LF_HERE(ptr, http_msg_invalid);
1635 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 http_msg_hdr_l2_lws:
1638 case HTTP_MSG_HDR_L2_LWS:
1639 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1640 /* LWS: replace HT,CR,LF with spaces */
1641 for (; msg->eol < ptr; msg->eol++)
1642 *msg->eol = ' ';
1643 goto http_msg_hdr_val;
1644 }
1645 http_msg_complete_header:
1646 /*
1647 * It was a new header, so the last one is finished.
1648 * Assumes msg->sol points to the first char, msg->col to the
1649 * colon, msg->sov points to the first character of the value
1650 * and msg->eol to the first CR or LF so we know how the line
1651 * ends. We insert last header into the index.
1652 */
1653 /*
1654 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1655 write(2, msg->sol, msg->eol-msg->sol);
1656 fprintf(stderr,"\n");
1657 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001658
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001659 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1660 idx, idx->tail) < 0))
1661 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001662
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 msg->sol = ptr;
1664 if (likely(!HTTP_IS_CRLF(*ptr))) {
1665 goto http_msg_hdr_name;
1666 }
1667
1668 if (likely(*ptr == '\r'))
1669 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1670 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001671
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001672 http_msg_last_lf:
1673 case HTTP_MSG_LAST_LF:
1674 /* Assumes msg->sol points to the first of either CR or LF */
1675 EXPECT_LF_HERE(ptr, http_msg_invalid);
1676 ptr++;
1677 buf->lr = ptr;
1678 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001679 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001680 return;
1681#ifdef DEBUG_FULL
1682 default:
1683 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1684 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001685#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001686 }
1687 http_msg_ood:
1688 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001689 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 buf->lr = ptr;
1691 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001692
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 http_msg_invalid:
1694 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001695 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001696 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 return;
1698}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001699
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001700/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1701 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1702 * nothing is done and 1 is returned.
1703 */
1704static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1705{
1706 int delta;
1707 char *cur_end;
1708
1709 if (msg->sl.rq.v_l != 0)
1710 return 1;
1711
1712 msg->sol = req->data + msg->som;
1713 cur_end = msg->sol + msg->sl.rq.l;
1714 delta = 0;
1715
1716 if (msg->sl.rq.u_l == 0) {
1717 /* if no URI was set, add "/" */
1718 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1719 cur_end += delta;
1720 msg->eoh += delta;
1721 }
1722 /* add HTTP version */
1723 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1724 msg->eoh += delta;
1725 cur_end += delta;
1726 cur_end = (char *)http_parse_reqline(msg, req->data,
1727 HTTP_MSG_RQMETH,
1728 msg->sol, cur_end + 1,
1729 NULL, NULL);
1730 if (unlikely(!cur_end))
1731 return 0;
1732
1733 /* we have a full HTTP/1.0 request now and we know that
1734 * we have either a CR or an LF at <ptr>.
1735 */
1736 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1737 return 1;
1738}
1739
Willy Tarreaud787e662009-07-07 10:14:51 +02001740/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1741 * processing can continue on next analysers, or zero if it either needs more
1742 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1743 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1744 * when it has nothing left to do, and may remove any analyser when it wants to
1745 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001747int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748{
Willy Tarreau59234e92008-11-30 23:51:27 +01001749 /*
1750 * We will parse the partial (or complete) lines.
1751 * We will check the request syntax, and also join multi-line
1752 * headers. An index of all the lines will be elaborated while
1753 * parsing.
1754 *
1755 * For the parsing, we use a 28 states FSM.
1756 *
1757 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01001758 * req->data + msg->som = beginning of request
Willy Tarreau59234e92008-11-30 23:51:27 +01001759 * req->data + req->eoh = end of processed headers / start of current one
1760 * req->data + req->eol = end of current header or line (LF or CRLF)
1761 * req->lr = first non-visited byte
1762 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02001763 *
1764 * At end of parsing, we may perform a capture of the error (if any), and
1765 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
1766 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1767 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001768 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001769
Willy Tarreau59234e92008-11-30 23:51:27 +01001770 int cur_idx;
1771 struct http_txn *txn = &s->txn;
1772 struct http_msg *msg = &txn->req;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001773
Willy Tarreau6bf17362009-02-24 10:48:35 +01001774 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1775 now_ms, __FUNCTION__,
1776 s,
1777 req,
1778 req->rex, req->wex,
1779 req->flags,
1780 req->l,
1781 req->analysers);
1782
Willy Tarreau52a0c602009-08-16 22:45:38 +02001783 /* we're speaking HTTP here, so let's speak HTTP to the client */
1784 s->srv_error = http_return_srv_error;
1785
Willy Tarreau59234e92008-11-30 23:51:27 +01001786 if (likely(req->lr < req->r))
1787 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001788
Willy Tarreau59234e92008-11-30 23:51:27 +01001789 /* 1: we might have to print this header in debug mode */
1790 if (unlikely((global.mode & MODE_DEBUG) &&
1791 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
1792 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
1793 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001794
Willy Tarreau59234e92008-11-30 23:51:27 +01001795 sol = req->data + msg->som;
1796 eol = sol + msg->sl.rq.l;
1797 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001798
Willy Tarreau59234e92008-11-30 23:51:27 +01001799 sol += hdr_idx_first_pos(&txn->hdr_idx);
1800 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001801
Willy Tarreau59234e92008-11-30 23:51:27 +01001802 while (cur_idx) {
1803 eol = sol + txn->hdr_idx.v[cur_idx].len;
1804 debug_hdr("clihdr", s, sol, eol);
1805 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1806 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001807 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001808 }
1809
Willy Tarreau58f10d72006-12-04 02:26:12 +01001810
Willy Tarreau59234e92008-11-30 23:51:27 +01001811 /*
1812 * Now we quickly check if we have found a full valid request.
1813 * If not so, we check the FD and buffer states before leaving.
1814 * A full request is indicated by the fact that we have seen
1815 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1816 * requests are checked first.
1817 *
1818 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001819
Willy Tarreau59234e92008-11-30 23:51:27 +01001820 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001821 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001822 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001823 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001824 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
1825 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001826
Willy Tarreau59234e92008-11-30 23:51:27 +01001827 /* 1: Since we are in header mode, if there's no space
1828 * left for headers, we won't be able to free more
1829 * later, so the session will never terminate. We
1830 * must terminate it now.
1831 */
1832 if (unlikely(req->flags & BF_FULL)) {
1833 /* FIXME: check if URI is set and return Status
1834 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001835 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001836 goto return_bad_req;
1837 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001838
Willy Tarreau59234e92008-11-30 23:51:27 +01001839 /* 2: have we encountered a read error ? */
1840 else if (req->flags & BF_READ_ERROR) {
1841 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02001842 if (msg->err_pos >= 0)
1843 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001844 msg->msg_state = HTTP_MSG_ERROR;
1845 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001846
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001847 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001848 if (s->listener->counters)
1849 s->listener->counters->failed_req++;
1850
Willy Tarreau59234e92008-11-30 23:51:27 +01001851 if (!(s->flags & SN_ERR_MASK))
1852 s->flags |= SN_ERR_CLICL;
1853 if (!(s->flags & SN_FINST_MASK))
1854 s->flags |= SN_FINST_R;
1855 return 0;
1856 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001857
Willy Tarreau59234e92008-11-30 23:51:27 +01001858 /* 3: has the read timeout expired ? */
1859 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
1860 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02001861 if (msg->err_pos >= 0)
1862 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001863 txn->status = 408;
1864 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
1865 msg->msg_state = HTTP_MSG_ERROR;
1866 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001867
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001868 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001869 if (s->listener->counters)
1870 s->listener->counters->failed_req++;
1871
Willy Tarreau59234e92008-11-30 23:51:27 +01001872 if (!(s->flags & SN_ERR_MASK))
1873 s->flags |= SN_ERR_CLITO;
1874 if (!(s->flags & SN_FINST_MASK))
1875 s->flags |= SN_FINST_R;
1876 return 0;
1877 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001878
Willy Tarreau59234e92008-11-30 23:51:27 +01001879 /* 4: have we encountered a close ? */
1880 else if (req->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02001881 if (msg->err_pos >= 0)
1882 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001883 txn->status = 400;
1884 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1885 msg->msg_state = HTTP_MSG_ERROR;
1886 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001887
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001888 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001889 if (s->listener->counters)
1890 s->listener->counters->failed_req++;
1891
Willy Tarreau59234e92008-11-30 23:51:27 +01001892 if (!(s->flags & SN_ERR_MASK))
1893 s->flags |= SN_ERR_CLICL;
1894 if (!(s->flags & SN_FINST_MASK))
1895 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001896 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001897 }
1898
Willy Tarreau520d95e2009-09-19 21:04:57 +02001899 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001900 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
1901
Willy Tarreau59234e92008-11-30 23:51:27 +01001902 /* just set the request timeout once at the beginning of the request */
1903 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02001904 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001905
Willy Tarreau59234e92008-11-30 23:51:27 +01001906 /* we're not ready yet */
1907 return 0;
1908 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001909
Willy Tarreaud787e662009-07-07 10:14:51 +02001910 /* OK now we have a complete HTTP request with indexed headers. Let's
1911 * complete the request parsing by setting a few fields we will need
1912 * later.
1913 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001914
Willy Tarreaud787e662009-07-07 10:14:51 +02001915 /* Maybe we found in invalid header name while we were configured not
1916 * to block on that, so we have to capture it now.
1917 */
1918 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02001919 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
1920
Willy Tarreau59234e92008-11-30 23:51:27 +01001921 /* ensure we keep this pointer to the beginning of the message */
1922 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001923
Willy Tarreau59234e92008-11-30 23:51:27 +01001924 /*
1925 * 1: identify the method
1926 */
1927 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
1928
1929 /* we can make use of server redirect on GET and HEAD */
1930 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1931 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001932
Willy Tarreau59234e92008-11-30 23:51:27 +01001933 /*
1934 * 2: check if the URI matches the monitor_uri.
1935 * We have to do this for every request which gets in, because
1936 * the monitor-uri is defined by the frontend.
1937 */
1938 if (unlikely((s->fe->monitor_uri_len != 0) &&
1939 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1940 !memcmp(&req->data[msg->sl.rq.u],
1941 s->fe->monitor_uri,
1942 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001943 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001944 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001945 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001946 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001947
Willy Tarreau59234e92008-11-30 23:51:27 +01001948 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001949
Willy Tarreau59234e92008-11-30 23:51:27 +01001950 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02001951 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
1952 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001953
Willy Tarreau59234e92008-11-30 23:51:27 +01001954 ret = acl_pass(ret);
1955 if (cond->pol == ACL_COND_UNLESS)
1956 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001957
Willy Tarreau59234e92008-11-30 23:51:27 +01001958 if (ret) {
1959 /* we fail this request, let's return 503 service unavail */
1960 txn->status = 503;
1961 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
1962 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001963 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001964 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001965
Willy Tarreau59234e92008-11-30 23:51:27 +01001966 /* nothing to fail, let's reply normaly */
1967 txn->status = 200;
1968 stream_int_retnclose(req->prod, &http_200_chunk);
1969 goto return_prx_cond;
1970 }
1971
1972 /*
1973 * 3: Maybe we have to copy the original REQURI for the logs ?
1974 * Note: we cannot log anymore if the request has been
1975 * classified as invalid.
1976 */
1977 if (unlikely(s->logs.logwait & LW_REQ)) {
1978 /* we have a complete HTTP request that we must log */
1979 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1980 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001981
Willy Tarreau59234e92008-11-30 23:51:27 +01001982 if (urilen >= REQURI_LEN)
1983 urilen = REQURI_LEN - 1;
1984 memcpy(txn->uri, &req->data[msg->som], urilen);
1985 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001986
Willy Tarreau59234e92008-11-30 23:51:27 +01001987 if (!(s->logs.logwait &= ~LW_REQ))
1988 s->do_log(s);
1989 } else {
1990 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001991 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001992 }
Willy Tarreau06619262006-12-17 08:37:22 +01001993
Willy Tarreau59234e92008-11-30 23:51:27 +01001994 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001995 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
1996 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001997
Willy Tarreau59234e92008-11-30 23:51:27 +01001998 /* 5: we may need to capture headers */
1999 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
2000 capture_headers(req->data + msg->som, &txn->hdr_idx,
2001 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002002
Willy Tarreaud787e662009-07-07 10:14:51 +02002003 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002004 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002005 req->analyse_exp = TICK_ETERNITY;
2006 return 1;
2007
2008 return_bad_req:
2009 /* We centralize bad requests processing here */
2010 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2011 /* we detected a parsing error. We want to archive this request
2012 * in the dedicated proxy area for later troubleshooting.
2013 */
2014 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2015 }
2016
2017 txn->req.msg_state = HTTP_MSG_ERROR;
2018 txn->status = 400;
2019 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002020
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002021 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002022 if (s->listener->counters)
2023 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002024
2025 return_prx_cond:
2026 if (!(s->flags & SN_ERR_MASK))
2027 s->flags |= SN_ERR_PRXCOND;
2028 if (!(s->flags & SN_FINST_MASK))
2029 s->flags |= SN_FINST_R;
2030
2031 req->analysers = 0;
2032 req->analyse_exp = TICK_ETERNITY;
2033 return 0;
2034}
2035
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002036/* This stream analyser runs all HTTP request processing which is common to
2037 * frontends and backends, which means blocking ACLs, filters, connection-close,
2038 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002039 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002040 * either needs more data or wants to immediately abort the request (eg: deny,
2041 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002042 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002043int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002044{
Willy Tarreaud787e662009-07-07 10:14:51 +02002045 struct http_txn *txn = &s->txn;
2046 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002047 struct acl_cond *cond;
2048 struct redirect_rule *rule;
2049 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002050
Willy Tarreau51aecc72009-07-12 09:47:04 +02002051 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2052 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002053 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002054 return 0;
2055 }
2056
Willy Tarreau3a816292009-07-07 10:55:49 +02002057 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002058 req->analyse_exp = TICK_ETERNITY;
2059
2060 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2061 now_ms, __FUNCTION__,
2062 s,
2063 req,
2064 req->rex, req->wex,
2065 req->flags,
2066 req->l,
2067 req->analysers);
2068
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002069 /* first check whether we have some ACLs set to block this request */
2070 list_for_each_entry(cond, &px->block_cond, list) {
2071 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002072
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002073 ret = acl_pass(ret);
2074 if (cond->pol == ACL_COND_UNLESS)
2075 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002076
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002077 if (ret) {
2078 txn->status = 403;
2079 /* let's log the request time */
2080 s->logs.tv_request = now;
2081 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2082 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002083 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002084 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002085
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002086 /* try headers filters */
2087 if (px->req_exp != NULL) {
2088 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2089 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002090
Willy Tarreau59234e92008-11-30 23:51:27 +01002091 /* has the request been denied ? */
2092 if (txn->flags & TX_CLDENY) {
2093 /* no need to go further */
2094 txn->status = 403;
2095 /* let's log the request time */
2096 s->logs.tv_request = now;
2097 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2098 goto return_prx_cond;
2099 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002100
2101 /* When a connection is tarpitted, we use the tarpit timeout,
2102 * which may be the same as the connect timeout if unspecified.
2103 * If unset, then set it to zero because we really want it to
2104 * eventually expire. We build the tarpit as an analyser.
2105 */
2106 if (txn->flags & TX_CLTARPIT) {
2107 buffer_erase(s->req);
2108 /* wipe the request out so that we can drop the connection early
2109 * if the client closes first.
2110 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002111 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002112 req->analysers = 0; /* remove switching rules etc... */
2113 req->analysers |= AN_REQ_HTTP_TARPIT;
2114 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2115 if (!req->analyse_exp)
2116 req->analyse_exp = tick_add(now_ms, 0);
2117 return 1;
2118 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002119 }
Willy Tarreau06619262006-12-17 08:37:22 +01002120
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002121 /* We might have to check for "Connection:" */
2122 if (((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2123 !(s->flags & SN_CONN_CLOSED)) {
2124 char *cur_ptr, *cur_end, *cur_next;
2125 int old_idx, delta, val;
2126 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002127
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002128 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
2129 old_idx = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002130
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002131 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2132 cur_hdr = &txn->hdr_idx.v[cur_idx];
2133 cur_ptr = cur_next;
2134 cur_end = cur_ptr + cur_hdr->len;
2135 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002136
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002137 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2138 if (val) {
2139 /* 3 possibilities :
2140 * - we have already set Connection: close,
2141 * so we remove this line.
2142 * - we have not yet set Connection: close,
2143 * but this line indicates close. We leave
2144 * it untouched and set the flag.
2145 * - we have not yet set Connection: close,
2146 * and this line indicates non-close. We
2147 * replace it.
2148 */
2149 if (s->flags & SN_CONN_CLOSED) {
2150 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2151 txn->req.eoh += delta;
2152 cur_next += delta;
2153 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2154 txn->hdr_idx.used--;
2155 cur_hdr->len = 0;
2156 } else {
2157 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2158 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2159 "close", 5);
Willy Tarreau59234e92008-11-30 23:51:27 +01002160 cur_next += delta;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002161 cur_hdr->len += delta;
2162 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002163 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002164 s->flags |= SN_CONN_CLOSED;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002165 }
Willy Tarreau06619262006-12-17 08:37:22 +01002166 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002167 old_idx = cur_idx;
Willy Tarreau59234e92008-11-30 23:51:27 +01002168 }
Willy Tarreau78599912009-10-17 20:12:21 +02002169
2170 /* if there was no Connection header and we're in HTTP/1.0, then "close" is implied */
2171 if (!(s->flags & SN_CONN_CLOSED) && (msg->sl.rq.v_l == 8) &&
2172 (req->data[msg->som + msg->sl.rq.v + 5] == '1') &&
2173 (req->data[msg->som + msg->sl.rq.v + 7] == '0'))
2174 s->flags |= SN_CONN_CLOSED;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002175 }
2176 /* add request headers from the rule sets in the same order */
2177 for (cur_idx = 0; cur_idx < px->nb_reqadd; cur_idx++) {
2178 if (unlikely(http_header_add_tail(req,
2179 &txn->req,
2180 &txn->hdr_idx,
2181 px->req_add[cur_idx])) < 0)
2182 goto return_bad_req;
2183 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002184
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002185 /* check if stats URI was requested, and if an auth is needed */
2186 if (px->uri_auth != NULL &&
2187 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2188 /* we have to check the URI and auth for this request.
2189 * FIXME!!! that one is rather dangerous, we want to
2190 * make it follow standard rules (eg: clear req->analysers).
2191 */
2192 if (stats_check_uri_auth(s, px)) {
2193 req->analysers = 0;
2194 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002195 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002196 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002197
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002198 /* check whether we have some ACLs set to redirect this request */
2199 list_for_each_entry(rule, &px->redirect_rules, list) {
2200 int ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreau06b917c2009-07-06 16:34:52 +02002201
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002202 ret = acl_pass(ret);
2203 if (rule->cond->pol == ACL_COND_UNLESS)
2204 ret = !ret;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002205
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002206 if (ret) {
2207 struct chunk rdr = { trash, 0 };
2208 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002209
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002210 /* build redirect message */
2211 switch(rule->code) {
2212 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002213 msg_fmt = HTTP_303;
2214 break;
2215 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002216 msg_fmt = HTTP_301;
2217 break;
2218 case 302:
2219 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002220 msg_fmt = HTTP_302;
2221 break;
2222 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002223
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002224 if (unlikely(chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002225 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002226
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002227 switch(rule->type) {
2228 case REDIRECT_TYPE_PREFIX: {
2229 const char *path;
2230 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002231
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002232 path = http_get_path(txn);
2233 /* build message using path */
2234 if (path) {
2235 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2236 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2237 int qs = 0;
2238 while (qs < pathlen) {
2239 if (path[qs] == '?') {
2240 pathlen = qs;
2241 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002242 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002243 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002244 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002245 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002246 } else {
2247 path = "/";
2248 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002249 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002250
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002251 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002252 goto return_bad_req;
2253
2254 /* add prefix. Note that if prefix == "/", we don't want to
2255 * add anything, otherwise it makes it hard for the user to
2256 * configure a self-redirection.
2257 */
2258 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002259 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2260 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002261 }
2262
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002263 /* add path */
2264 memcpy(rdr.str + rdr.len, path, pathlen);
2265 rdr.len += pathlen;
2266 break;
2267 }
2268 case REDIRECT_TYPE_LOCATION:
2269 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002270 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002271 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002272
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002273 /* add location */
2274 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2275 rdr.len += rule->rdr_len;
2276 break;
2277 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002278
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002279 if (rule->cookie_len) {
2280 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2281 rdr.len += 14;
2282 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2283 rdr.len += rule->cookie_len;
2284 memcpy(rdr.str + rdr.len, "\r\n", 2);
2285 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002286 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002287
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002288 /* add end of headers */
2289 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2290 rdr.len += 4;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002291
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002292 txn->status = rule->code;
2293 /* let's log the request time */
2294 s->logs.tv_request = now;
2295 stream_int_retnclose(req->prod, &rdr);
2296 goto return_prx_cond;
2297 }
2298 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002299
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002300 /* that's OK for us now, let's move on to next analysers */
2301 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002302
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002303 return_bad_req:
2304 /* We centralize bad requests processing here */
2305 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2306 /* we detected a parsing error. We want to archive this request
2307 * in the dedicated proxy area for later troubleshooting.
2308 */
2309 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2310 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002311
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002312 txn->req.msg_state = HTTP_MSG_ERROR;
2313 txn->status = 400;
2314 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002315
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002316 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002317 if (s->listener->counters)
2318 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002319
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002320 return_prx_cond:
2321 if (!(s->flags & SN_ERR_MASK))
2322 s->flags |= SN_ERR_PRXCOND;
2323 if (!(s->flags & SN_FINST_MASK))
2324 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002325
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002326 req->analysers = 0;
2327 req->analyse_exp = TICK_ETERNITY;
2328 return 0;
2329}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002330
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002331/* This function performs all the processing enabled for the current request.
2332 * It returns 1 if the processing can continue on next analysers, or zero if it
2333 * needs more data, encounters an error, or wants to immediately abort the
2334 * request. It relies on buffers flags, and updates s->req->analysers.
2335 */
2336int http_process_request(struct session *s, struct buffer *req, int an_bit)
2337{
2338 struct http_txn *txn = &s->txn;
2339 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002340
Willy Tarreau51aecc72009-07-12 09:47:04 +02002341 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2342 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002343 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002344 return 0;
2345 }
2346
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002347 req->analysers &= ~an_bit;
2348 req->analyse_exp = TICK_ETERNITY;
2349
2350 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2351 now_ms, __FUNCTION__,
2352 s,
2353 req,
2354 req->rex, req->wex,
2355 req->flags,
2356 req->l,
2357 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002358
Willy Tarreau59234e92008-11-30 23:51:27 +01002359 /*
2360 * Right now, we know that we have processed the entire headers
2361 * and that unwanted requests have been filtered out. We can do
2362 * whatever we want with the remaining request. Also, now we
2363 * may have separate values for ->fe, ->be.
2364 */
Willy Tarreau06619262006-12-17 08:37:22 +01002365
Willy Tarreau59234e92008-11-30 23:51:27 +01002366 /*
2367 * If HTTP PROXY is set we simply get remote server address
2368 * parsing incoming request.
2369 */
2370 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2371 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2372 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002373
Willy Tarreau59234e92008-11-30 23:51:27 +01002374 /*
2375 * 7: the appsession cookie was looked up very early in 1.2,
2376 * so let's do the same now.
2377 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002378
Willy Tarreau59234e92008-11-30 23:51:27 +01002379 /* It needs to look into the URI */
2380 if (s->be->appsession_name) {
2381 get_srv_from_appsession(s, &req->data[msg->som], msg->sl.rq.l);
2382 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01002383
Willy Tarreau59234e92008-11-30 23:51:27 +01002384 /*
2385 * 8: Now we can work with the cookies.
2386 * Note that doing so might move headers in the request, but
2387 * the fields will stay coherent and the URI will not move.
2388 * This should only be performed in the backend.
2389 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002390 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2392 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002393
Willy Tarreau59234e92008-11-30 23:51:27 +01002394 /*
2395 * 9: add X-Forwarded-For if either the frontend or the backend
2396 * asks for it.
2397 */
2398 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2399 if (s->cli_addr.ss_family == AF_INET) {
2400 /* Add an X-Forwarded-For header unless the source IP is
2401 * in the 'except' network range.
2402 */
2403 if ((!s->fe->except_mask.s_addr ||
2404 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2405 != s->fe->except_net.s_addr) &&
2406 (!s->be->except_mask.s_addr ||
2407 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2408 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002409 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002410 unsigned char *pn;
2411 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002412
2413 /* Note: we rely on the backend to get the header name to be used for
2414 * x-forwarded-for, because the header is really meant for the backends.
2415 * However, if the backend did not specify any option, we have to rely
2416 * on the frontend's header name.
2417 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002418 if (s->be->fwdfor_hdr_len) {
2419 len = s->be->fwdfor_hdr_len;
2420 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002421 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002422 len = s->fe->fwdfor_hdr_len;
2423 memcpy(trash, s->fe->fwdfor_hdr_name, len);
2424 }
2425 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002426
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002427 if (unlikely(http_header_add_tail2(req, &txn->req,
2428 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002429 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002430 }
2431 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002432 else if (s->cli_addr.ss_family == AF_INET6) {
2433 /* FIXME: for the sake of completeness, we should also support
2434 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002435 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002436 int len;
2437 char pn[INET6_ADDRSTRLEN];
2438 inet_ntop(AF_INET6,
2439 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2440 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002441
Willy Tarreau59234e92008-11-30 23:51:27 +01002442 /* Note: we rely on the backend to get the header name to be used for
2443 * x-forwarded-for, because the header is really meant for the backends.
2444 * However, if the backend did not specify any option, we have to rely
2445 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002446 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002447 if (s->be->fwdfor_hdr_len) {
2448 len = s->be->fwdfor_hdr_len;
2449 memcpy(trash, s->be->fwdfor_hdr_name, len);
2450 } else {
2451 len = s->fe->fwdfor_hdr_len;
2452 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002453 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002454 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002455
Willy Tarreau59234e92008-11-30 23:51:27 +01002456 if (unlikely(http_header_add_tail2(req, &txn->req,
2457 &txn->hdr_idx, trash, len)) < 0)
2458 goto return_bad_req;
2459 }
2460 }
2461
2462 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002463 * 10: add X-Original-To if either the frontend or the backend
2464 * asks for it.
2465 */
2466 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
2467
2468 /* FIXME: don't know if IPv6 can handle that case too. */
2469 if (s->cli_addr.ss_family == AF_INET) {
2470 /* Add an X-Original-To header unless the destination IP is
2471 * in the 'except' network range.
2472 */
2473 if (!(s->flags & SN_FRT_ADDR_SET))
2474 get_frt_addr(s);
2475
2476 if ((!s->fe->except_mask_to.s_addr ||
2477 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
2478 != s->fe->except_to.s_addr) &&
2479 (!s->be->except_mask_to.s_addr ||
2480 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
2481 != s->be->except_to.s_addr)) {
2482 int len;
2483 unsigned char *pn;
2484 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
2485
2486 /* Note: we rely on the backend to get the header name to be used for
2487 * x-original-to, because the header is really meant for the backends.
2488 * However, if the backend did not specify any option, we have to rely
2489 * on the frontend's header name.
2490 */
2491 if (s->be->orgto_hdr_len) {
2492 len = s->be->orgto_hdr_len;
2493 memcpy(trash, s->be->orgto_hdr_name, len);
2494 } else {
2495 len = s->fe->orgto_hdr_len;
2496 memcpy(trash, s->fe->orgto_hdr_name, len);
2497 }
2498 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
2499
2500 if (unlikely(http_header_add_tail2(req, &txn->req,
2501 &txn->hdr_idx, trash, len)) < 0)
2502 goto return_bad_req;
2503 }
2504 }
2505 }
2506
Willy Tarreau78599912009-10-17 20:12:21 +02002507 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 if (!(s->flags & SN_CONN_CLOSED) &&
2509 ((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau78599912009-10-17 20:12:21 +02002510 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002511 "Connection: close", 17)) < 0)
2512 goto return_bad_req;
2513 s->flags |= SN_CONN_CLOSED;
2514 }
2515 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2516 * If not assigned, perhaps we are balancing on url_param, but this is a
2517 * POST; and the parameters are in the body, maybe scan there to find our server.
2518 * (unless headers overflowed the buffer?)
2519 */
2520 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2521 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
2522 s->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
2523 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2524 /* are there enough bytes here? total == l || r || rlim ?
2525 * len is unsigned, but eoh is int,
2526 * how many bytes of body have we received?
2527 * eoh is the first empty line of the header
2528 */
2529 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
2530 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
2531
2532 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2533 * We can't assume responsibility for the server's decision,
2534 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2535 * We also can't change our mind later, about which server to choose, so round robin.
2536 */
2537 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2538 struct hdr_ctx ctx;
2539 ctx.idx = 0;
2540 /* Expect is allowed in 1.1, look for it */
2541 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2542 if (ctx.idx != 0 &&
2543 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
2544 /* We can't reliablly stall and wait for data, because of
2545 * .NET clients that don't conform to rfc2616; so, no need for
2546 * the next block to check length expectations.
2547 * We could send 100 status back to the client, but then we need to
2548 * re-write headers, and send the message. And this isn't the right
2549 * place for that action.
2550 * TODO: support Expect elsewhere and delete this block.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002551 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002552 goto end_check_maybe_wait_for_body;
2553 }
2554
2555 if (likely(len > s->be->url_param_post_limit)) {
2556 /* nothing to do, we got enough */
2557 } else {
2558 /* limit implies we are supposed to need this many bytes
2559 * to find the parameter. Let's see how many bytes we can wait for.
2560 */
2561 long long hint = len;
2562 struct hdr_ctx ctx;
2563 ctx.idx = 0;
2564 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
2565 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02002566 buffer_dont_connect(req);
Willy Tarreau59234e92008-11-30 23:51:27 +01002567 req->analysers |= AN_REQ_HTTP_BODY;
2568 }
2569 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002570 ctx.idx = 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002571 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2572 /* now if we have a length, we'll take the hint */
2573 if (ctx.idx) {
2574 /* We have Content-Length */
2575 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
2576 hint = 0; /* parse failure, untrusted client */
2577 else {
2578 if (hint > 0)
2579 msg->hdr_content_len = hint;
2580 else
2581 hint = 0; /* bad client, sent negative length */
2582 }
2583 }
2584 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
2585 if (s->be->url_param_post_limit < hint)
2586 hint = s->be->url_param_post_limit;
2587 /* now do we really need to buffer more data? */
2588 if (len < hint) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02002589 buffer_dont_connect(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002590 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002591 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002592 /* else... There are no body bytes to wait for */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002593 }
2594 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002595 }
2596 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002597
Willy Tarreau59234e92008-11-30 23:51:27 +01002598 /*************************************************************
2599 * OK, that's finished for the headers. We have done what we *
2600 * could. Let's switch to the DATA state. *
2601 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002602
Willy Tarreaua07a34e2009-08-16 23:27:46 +02002603 buffer_set_rlim(req, req->size); /* no more rewrite needed */
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 s->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002605
Willy Tarreau59234e92008-11-30 23:51:27 +01002606 /* OK let's go on with the BODY now */
2607 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002608
Willy Tarreau59234e92008-11-30 23:51:27 +01002609 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02002610 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002611 /* we detected a parsing error. We want to archive this request
2612 * in the dedicated proxy area for later troubleshooting.
2613 */
Willy Tarreau4076a152009-04-02 15:18:36 +02002614 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01002615 }
Willy Tarreau4076a152009-04-02 15:18:36 +02002616
Willy Tarreau59234e92008-11-30 23:51:27 +01002617 txn->req.msg_state = HTTP_MSG_ERROR;
2618 txn->status = 400;
2619 req->analysers = 0;
2620 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002621
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002622 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002623 if (s->listener->counters)
2624 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002625
Willy Tarreau59234e92008-11-30 23:51:27 +01002626 if (!(s->flags & SN_ERR_MASK))
2627 s->flags |= SN_ERR_PRXCOND;
2628 if (!(s->flags & SN_FINST_MASK))
2629 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002630 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002631}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002632
Willy Tarreau60b85b02008-11-30 23:28:40 +01002633/* This function is an analyser which processes the HTTP tarpit. It always
2634 * returns zero, at the beginning because it prevents any other processing
2635 * from occurring, and at the end because it terminates the request.
2636 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002637int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01002638{
2639 struct http_txn *txn = &s->txn;
2640
2641 /* This connection is being tarpitted. The CLIENT side has
2642 * already set the connect expiration date to the right
2643 * timeout. We just have to check that the client is still
2644 * there and that the timeout has not expired.
2645 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002646 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01002647 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
2648 !tick_is_expired(req->analyse_exp, now_ms))
2649 return 0;
2650
2651 /* We will set the queue timer to the time spent, just for
2652 * logging purposes. We fake a 500 server error, so that the
2653 * attacker will not suspect his connection has been tarpitted.
2654 * It will not cause trouble to the logs because we can exclude
2655 * the tarpitted connections by filtering on the 'PT' status flags.
2656 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01002657 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2658
2659 txn->status = 500;
2660 if (req->flags != BF_READ_ERROR)
2661 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
2662
2663 req->analysers = 0;
2664 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002665
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002666 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002667 if (s->listener->counters)
2668 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01002669
Willy Tarreau60b85b02008-11-30 23:28:40 +01002670 if (!(s->flags & SN_ERR_MASK))
2671 s->flags |= SN_ERR_PRXCOND;
2672 if (!(s->flags & SN_FINST_MASK))
2673 s->flags |= SN_FINST_T;
2674 return 0;
2675}
2676
Willy Tarreaud34af782008-11-30 23:36:37 +01002677/* This function is an analyser which processes the HTTP request body. It looks
2678 * for parameters to be used for the load balancing algorithm (url_param). It
2679 * must only be called after the standard HTTP request processing has occurred,
2680 * because it expects the request to be parsed. It returns zero if it needs to
2681 * read more data, or 1 once it has completed its analysis.
2682 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002683int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01002684{
2685 struct http_msg *msg = &s->txn.req;
2686 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2687 long long limit = s->be->url_param_post_limit;
2688 struct hdr_ctx ctx;
2689
Willy Tarreau51aecc72009-07-12 09:47:04 +02002690 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2691 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002692 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002693 return 0;
2694 }
2695
Willy Tarreaud34af782008-11-30 23:36:37 +01002696 /* We have to parse the HTTP request body to find any required data.
2697 * "balance url_param check_post" should have been the only way to get
2698 * into this. We were brought here after HTTP header analysis, so all
2699 * related structures are ready.
2700 */
2701
2702 ctx.idx = 0;
2703
2704 /* now if we have a length, we'll take the hint */
2705 http_find_header2("Transfer-Encoding", 17, msg->sol, &s->txn.hdr_idx, &ctx);
2706 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2707 unsigned int chunk = 0;
2708 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2709 char c = msg->sol[body];
2710 if (ishex(c)) {
2711 unsigned int hex = toupper(c) - '0';
2712 if (hex > 9)
2713 hex -= 'A' - '9' - 1;
2714 chunk = (chunk << 4) | hex;
2715 } else
2716 break;
2717 body++;
2718 }
2719 if (body + 2 >= req->l) /* we want CRLF too */
2720 goto http_body_end; /* end of buffer? data missing! */
2721
2722 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
2723 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
2724
2725 body += 2; // skip CRLF
2726
2727 /* if we support more then one chunk here, we have to do it again when assigning server
2728 * 1. how much entity data do we have? new var
2729 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2730 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2731 */
2732
2733 if (chunk < limit)
2734 limit = chunk; /* only reading one chunk */
2735 } else {
2736 if (msg->hdr_content_len < limit)
2737 limit = msg->hdr_content_len;
2738 }
2739
2740 http_body_end:
2741 /* we leave once we know we have nothing left to do. This means that we have
2742 * enough bytes, or that we know we'll not get any more (buffer full, read
2743 * buffer closed).
2744 */
2745 if (req->l - body >= limit || /* enough bytes! */
2746 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
2747 tick_is_expired(req->analyse_exp, now_ms)) {
2748 /* The situation will not evolve, so let's give up on the analysis. */
2749 s->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau3a816292009-07-07 10:55:49 +02002750 req->analysers &= ~an_bit;
Willy Tarreaud34af782008-11-30 23:36:37 +01002751 req->analyse_exp = TICK_ETERNITY;
2752 return 1;
2753 }
2754 else {
2755 /* Not enough data. We'll re-use the http-request
2756 * timeout here. Ideally, we should set the timeout
2757 * relative to the accept() date. We just set the
2758 * request timeout once at the beginning of the
2759 * request.
2760 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002761 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01002762 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002763 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01002764 return 0;
2765 }
2766}
2767
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002768/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002769 * It normally returns zero, but may return 1 if it absolutely needs to be
2770 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002771 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002772 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002773 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002774int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002775{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002776 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002777 struct buffer *req = t->req;
2778 struct buffer *rep = t->rep;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002779 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002780
Willy Tarreau816b9792009-09-15 21:25:21 +02002781 next_response:
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002782 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002783 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002784 t,
2785 rep,
2786 rep->rex, rep->wex,
2787 rep->flags,
2788 rep->l,
2789 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002790
Willy Tarreau2df28e82008-08-17 15:20:19 +02002791 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002792 /*
2793 * Now parse the partial (or complete) lines.
2794 * We will check the response syntax, and also join multi-line
2795 * headers. An index of all the lines will be elaborated while
2796 * parsing.
2797 *
2798 * For the parsing, we use a 28 states FSM.
2799 *
2800 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002801 * rep->data + rep->som = beginning of response
2802 * rep->data + rep->eoh = end of processed headers / start of current one
2803 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002804 * rep->lr = first non-visited byte
2805 * rep->r = end of data
2806 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002807
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002808 int cur_idx;
2809 struct http_msg *msg = &txn->rsp;
2810 struct proxy *cur_proxy;
2811
2812 if (likely(rep->lr < rep->r))
2813 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2814
2815 /* 1: we might have to print this header in debug mode */
2816 if (unlikely((global.mode & MODE_DEBUG) &&
2817 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2818 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2819 char *eol, *sol;
2820
2821 sol = rep->data + msg->som;
2822 eol = sol + msg->sl.rq.l;
2823 debug_hdr("srvrep", t, sol, eol);
2824
2825 sol += hdr_idx_first_pos(&txn->hdr_idx);
2826 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2827
2828 while (cur_idx) {
2829 eol = sol + txn->hdr_idx.v[cur_idx].len;
2830 debug_hdr("srvhdr", t, sol, eol);
2831 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2832 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002833 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002834 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002835
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002836 /*
2837 * Now we quickly check if we have found a full valid response.
2838 * If not so, we check the FD and buffer states before leaving.
2839 * A full response is indicated by the fact that we have seen
2840 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2841 * responses are checked first.
2842 *
2843 * Depending on whether the client is still there or not, we
2844 * may send an error response back or not. Note that normally
2845 * we should only check for HTTP status there, and check I/O
2846 * errors somewhere else.
2847 */
2848
2849 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002850 /* Invalid response */
2851 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002852 /* we detected a parsing error. We want to archive this response
2853 * in the dedicated proxy area for later troubleshooting.
2854 */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002855 hdr_response_bad:
Willy Tarreau4076a152009-04-02 15:18:36 +02002856 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
2857 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
2858
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002859 buffer_shutr_now(rep);
2860 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002861 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002862 t->srv->counters.failed_resp++;
2863 t->be->counters.failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002864 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002865 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002866 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002867 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002868 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002869 if (!(t->flags & SN_FINST_MASK))
2870 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002871
Willy Tarreaudafde432008-08-17 01:00:46 +02002872 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002873 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002874 /* too large response does not fit in buffer. */
2875 else if (rep->flags & BF_FULL) {
2876 goto hdr_response_bad;
2877 }
2878 /* read error */
2879 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002880 if (msg->err_pos >= 0)
2881 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002882 buffer_shutr_now(rep);
2883 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002884 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002885 t->srv->counters.failed_resp++;
2886 t->be->counters.failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002887 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002888 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002889 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002890 if (!(t->flags & SN_ERR_MASK))
2891 t->flags |= SN_ERR_SRVCL;
2892 if (!(t->flags & SN_FINST_MASK))
2893 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002894 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002895 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002896 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002897 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002898 if (msg->err_pos >= 0)
2899 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002900 buffer_shutr_now(rep);
2901 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002902 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002903 t->srv->counters.failed_resp++;
2904 t->be->counters.failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002905 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002906 txn->status = 504;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002907 stream_int_return(rep->cons, error_message(t, HTTP_ERR_504));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002908 if (!(t->flags & SN_ERR_MASK))
2909 t->flags |= SN_ERR_SRVTO;
2910 if (!(t->flags & SN_FINST_MASK))
2911 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002912 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002913 }
Willy Tarreau8365f932009-03-15 23:11:49 +01002914 /* close from server */
2915 else if (rep->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002916 if (msg->err_pos >= 0)
2917 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002918 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002919 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002920 t->srv->counters.failed_resp++;
2921 t->be->counters.failed_resp++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002922 rep->analysers = 0;
2923 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002924 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002925 if (!(t->flags & SN_ERR_MASK))
2926 t->flags |= SN_ERR_SRVCL;
2927 if (!(t->flags & SN_FINST_MASK))
2928 t->flags |= SN_FINST_H;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002929 return 0;
2930 }
Willy Tarreau8365f932009-03-15 23:11:49 +01002931 /* write error to client (we don't send any message then) */
2932 else if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002933 if (msg->err_pos >= 0)
2934 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreau8365f932009-03-15 23:11:49 +01002935 buffer_shutr_now(rep);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002936 t->be->counters.failed_resp++;
Willy Tarreau8365f932009-03-15 23:11:49 +01002937 rep->analysers = 0;
2938 if (!(t->flags & SN_ERR_MASK))
2939 t->flags |= SN_ERR_CLICL;
2940 if (!(t->flags & SN_FINST_MASK))
2941 t->flags |= SN_FINST_H;
2942 return 0;
2943 }
Willy Tarreau816b9792009-09-15 21:25:21 +02002944
Willy Tarreau520d95e2009-09-19 21:04:57 +02002945 buffer_dont_close(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002946 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002947 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002948
Willy Tarreau21d2af32008-02-14 20:25:24 +01002949
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002950 /*****************************************************************
2951 * More interesting part now : we know that we have a complete *
2952 * response which at least looks like HTTP. We have an indicator *
2953 * of each header's length, so we can parse them quickly. *
2954 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002955
Willy Tarreau4076a152009-04-02 15:18:36 +02002956 if (msg->err_pos >= 0)
2957 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
2958
Willy Tarreau2df28e82008-08-17 15:20:19 +02002959 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002960
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002961 /* ensure we keep this pointer to the beginning of the message */
2962 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002963
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002964 /*
2965 * 1: get the status code and check for cacheability.
2966 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002967
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002968 t->logs.logwait &= ~LW_RESP;
2969 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002970
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002971 n = rep->data[msg->sl.st.c] - '0';
2972 if (n < 1 || n > 5)
2973 n = 0;
2974
2975 t->srv->counters.p.http.rsp[n]++;
2976 t->be->counters.p.http.rsp[n]++;
2977
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002978 switch (txn->status) {
2979 case 200:
2980 case 203:
2981 case 206:
2982 case 300:
2983 case 301:
2984 case 410:
2985 /* RFC2616 @13.4:
2986 * "A response received with a status code of
2987 * 200, 203, 206, 300, 301 or 410 MAY be stored
2988 * by a cache (...) unless a cache-control
2989 * directive prohibits caching."
2990 *
2991 * RFC2616 @9.5: POST method :
2992 * "Responses to this method are not cacheable,
2993 * unless the response includes appropriate
2994 * Cache-Control or Expires header fields."
2995 */
2996 if (likely(txn->meth != HTTP_METH_POST) &&
2997 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2998 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2999 break;
3000 default:
3001 break;
3002 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003003
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003004 /*
3005 * 2: we may need to capture headers
3006 */
3007 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
3008 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3009 txn->rsp.cap, t->fe->rsp_cap);
3010
3011 /*
3012 * 3: we will have to evaluate the filters.
3013 * As opposed to version 1.2, now they will be evaluated in the
3014 * filters order and not in the header order. This means that
3015 * each filter has to be validated among all headers.
3016 *
3017 * Filters are tried with ->be first, then with ->fe if it is
3018 * different from ->be.
3019 */
3020
3021 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
3022
3023 cur_proxy = t->be;
3024 while (1) {
3025 struct proxy *rule_set = cur_proxy;
3026
3027 /* try headers filters */
3028 if (rule_set->rsp_exp != NULL) {
3029 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3030 return_bad_resp:
Willy Tarreau8365f932009-03-15 23:11:49 +01003031 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003032 t->srv->counters.failed_resp++;
3033 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003034 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003035 buffer_shutr_now(rep);
3036 buffer_shutw_now(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02003037 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003038 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01003039 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003040 if (!(t->flags & SN_ERR_MASK))
3041 t->flags |= SN_ERR_PRXCOND;
3042 if (!(t->flags & SN_FINST_MASK))
3043 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02003044 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003045 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003046 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003047
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003048 /* has the response been denied ? */
3049 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01003050 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003051 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003052
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003053 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003054 if (t->listener->counters)
3055 t->listener->counters->denied_resp++;
3056
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003057 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003058 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003059
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003060 /* We might have to check for "Connection:" */
3061 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau816b9792009-09-15 21:25:21 +02003062 !(t->flags & SN_CONN_CLOSED) &&
3063 txn->status >= 200) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003064 char *cur_ptr, *cur_end, *cur_next;
3065 int cur_idx, old_idx, delta, val;
3066 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003067
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003068 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3069 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003070
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003071 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3072 cur_hdr = &txn->hdr_idx.v[cur_idx];
3073 cur_ptr = cur_next;
3074 cur_end = cur_ptr + cur_hdr->len;
3075 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003076
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003077 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3078 if (val) {
3079 /* 3 possibilities :
3080 * - we have already set Connection: close,
3081 * so we remove this line.
3082 * - we have not yet set Connection: close,
3083 * but this line indicates close. We leave
3084 * it untouched and set the flag.
3085 * - we have not yet set Connection: close,
3086 * and this line indicates non-close. We
3087 * replace it.
3088 */
3089 if (t->flags & SN_CONN_CLOSED) {
3090 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3091 txn->rsp.eoh += delta;
3092 cur_next += delta;
3093 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3094 txn->hdr_idx.used--;
3095 cur_hdr->len = 0;
3096 } else {
3097 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3098 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3099 "close", 5);
3100 cur_next += delta;
3101 cur_hdr->len += delta;
3102 txn->rsp.eoh += delta;
3103 }
3104 t->flags |= SN_CONN_CLOSED;
3105 }
3106 }
3107 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003108 }
3109 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003110
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003111 /* add response headers from the rule sets in the same order */
3112 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau816b9792009-09-15 21:25:21 +02003113 if (txn->status < 200)
3114 break;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003115 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3116 rule_set->rsp_add[cur_idx])) < 0)
3117 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003118 }
3119
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003120 /* check whether we're already working on the frontend */
3121 if (cur_proxy == t->fe)
3122 break;
3123 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003124 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003125
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003126 /*
3127 * 4: check for server cookie.
3128 */
Willy Tarreau816b9792009-09-15 21:25:21 +02003129 if ((t->be->cookie_name || t->be->appsession_name || t->fe->capture_name
3130 || (t->be->options & PR_O_CHK_CACHE)) && txn->status >= 200)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003131 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003132
Willy Tarreaubaaee002006-06-26 02:48:02 +02003133
Willy Tarreaua15645d2007-03-18 16:22:39 +01003134 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003135 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003136 */
Willy Tarreau816b9792009-09-15 21:25:21 +02003137 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0 && txn->status >= 200)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003138 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003139
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003140 /*
3141 * 6: add server cookie in the response if needed
3142 */
3143 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau816b9792009-09-15 21:25:21 +02003144 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
3145 txn->status >= 200) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003146 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003147
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003148 /* the server is known, it's not the one the client requested, we have to
3149 * insert a set-cookie here, except if we want to insert only on POST
3150 * requests and this one isn't. Note that servers which don't have cookies
3151 * (eg: some backup servers) will return a full cookie removal request.
3152 */
3153 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
3154 t->be->cookie_name,
3155 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003156
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003157 if (t->be->cookie_domain)
3158 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003159
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003160 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3161 trash, len)) < 0)
3162 goto return_bad_resp;
3163 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003164
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003165 /* Here, we will tell an eventual cache on the client side that we don't
3166 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3167 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3168 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3169 */
3170 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003171
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003172 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3173
3174 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3175 "Cache-control: private", 22)) < 0)
3176 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003177 }
3178 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003179
Willy Tarreaubaaee002006-06-26 02:48:02 +02003180
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003181 /*
3182 * 7: check if result will be cacheable with a cookie.
3183 * We'll block the response if security checks have caught
3184 * nasty things such as a cacheable cookie.
3185 */
3186 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3187 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau816b9792009-09-15 21:25:21 +02003188 (t->be->options & PR_O_CHK_CACHE) &&
3189 txn->status >= 200) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003190
3191 /* we're in presence of a cacheable response containing
3192 * a set-cookie header. We'll block it as requested by
3193 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003194 */
Willy Tarreau8365f932009-03-15 23:11:49 +01003195 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003196 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003197
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003198 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003199 if (t->listener->counters)
3200 t->listener->counters->denied_resp++;
3201
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003202 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3203 t->be->id, t->srv?t->srv->id:"<dispatch>");
3204 send_log(t->be, LOG_ALERT,
3205 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3206 t->be->id, t->srv?t->srv->id:"<dispatch>");
3207 goto return_srv_prx_502;
3208 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003209
3210 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003211 * 8: add "Connection: close" if needed and not yet set.
3212 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003213 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003214 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreau816b9792009-09-15 21:25:21 +02003215 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
3216 txn->status >= 200) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003217 if ((unlikely(msg->sl.st.v_l != 8) ||
3218 unlikely(req->data[msg->som + 7] != '0')) &&
3219 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3220 "Connection: close", 17)) < 0)
3221 goto return_bad_resp;
3222 t->flags |= SN_CONN_CLOSED;
3223 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003224
Willy Tarreau816b9792009-09-15 21:25:21 +02003225 /*
3226 * 9: we may be facing a 1xx response (100 continue, 101 switching protocols),
3227 * in which case this is not the right response, and we're waiting for the
3228 * next one. Let's allow this response to go to the client and wait for the
3229 * next one.
3230 */
3231 if (txn->status < 200) {
3232 hdr_idx_init(&txn->hdr_idx);
3233 buffer_forward(rep, rep->lr - (rep->data + msg->som));
3234 msg->msg_state = HTTP_MSG_RPBEFORE;
3235 txn->status = 0;
3236 rep->analysers |= AN_RTR_HTTP_HDR;
3237 goto next_response;
3238 }
3239
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003240 /*************************************************************
3241 * OK, that's finished for the headers. We have done what we *
3242 * could. Let's switch to the DATA state. *
3243 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003244
Willy Tarreaua07a34e2009-08-16 23:27:46 +02003245 buffer_set_rlim(rep, rep->size); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003246 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003247
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003248 /* if the user wants to log as soon as possible, without counting
3249 * bytes from the server, then this is the right moment. We have
3250 * to temporarily assign bytes_out to log what we currently have.
3251 */
3252 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3253 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3254 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01003255 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003256 t->logs.bytes_out = 0;
3257 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003258
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003259 /* Note: we must not try to cheat by jumping directly to DATA,
3260 * otherwise we would not let the client side wake up.
3261 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003262
Willy Tarreaudafde432008-08-17 01:00:46 +02003263 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003264 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003265
Willy Tarreau2df28e82008-08-17 15:20:19 +02003266 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3267 * probably reduce one day's debugging session.
3268 */
3269#ifdef DEBUG_DEV
3270 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3271 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3272 __FILE__, __LINE__, rep->analysers);
3273 ABORT_NOW();
3274 }
3275#endif
3276 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003277 return 0;
3278}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003279
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003280/* Iterate the same filter through all request headers.
3281 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003282 * Since it can manage the switch to another backend, it updates the per-proxy
3283 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003284 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003285int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003286{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003287 char term;
3288 char *cur_ptr, *cur_end, *cur_next;
3289 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003290 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003291 struct hdr_idx_elem *cur_hdr;
3292 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003293
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003294 last_hdr = 0;
3295
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003296 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003297 old_idx = 0;
3298
3299 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003300 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003301 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003302 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003303 (exp->action == ACT_ALLOW ||
3304 exp->action == ACT_DENY ||
3305 exp->action == ACT_TARPIT))
3306 return 0;
3307
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003308 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003309 if (!cur_idx)
3310 break;
3311
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003312 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003313 cur_ptr = cur_next;
3314 cur_end = cur_ptr + cur_hdr->len;
3315 cur_next = cur_end + cur_hdr->cr + 1;
3316
3317 /* Now we have one header between cur_ptr and cur_end,
3318 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003319 */
3320
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003321 /* The annoying part is that pattern matching needs
3322 * that we modify the contents to null-terminate all
3323 * strings before testing them.
3324 */
3325
3326 term = *cur_end;
3327 *cur_end = '\0';
3328
3329 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3330 switch (exp->action) {
3331 case ACT_SETBE:
3332 /* It is not possible to jump a second time.
3333 * FIXME: should we return an HTTP/500 here so that
3334 * the admin knows there's a problem ?
3335 */
3336 if (t->be != t->fe)
3337 break;
3338
3339 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003340 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003341 last_hdr = 1;
3342 break;
3343
3344 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003345 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003346 last_hdr = 1;
3347 break;
3348
3349 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003350 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003351 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003352
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003353 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003354 if (t->listener->counters)
3355 t->listener->counters->denied_resp++;
3356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003357 break;
3358
3359 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003360 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003361 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003362
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003363 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003364 if (t->listener->counters)
3365 t->listener->counters->denied_resp++;
3366
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003367 break;
3368
3369 case ACT_REPLACE:
3370 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3371 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3372 /* FIXME: if the user adds a newline in the replacement, the
3373 * index will not be recalculated for now, and the new line
3374 * will not be counted as a new header.
3375 */
3376
3377 cur_end += delta;
3378 cur_next += delta;
3379 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003380 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003381 break;
3382
3383 case ACT_REMOVE:
3384 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3385 cur_next += delta;
3386
3387 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003388 txn->req.eoh += delta;
3389 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3390 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003391 cur_hdr->len = 0;
3392 cur_end = NULL; /* null-term has been rewritten */
3393 break;
3394
3395 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003396 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003397 if (cur_end)
3398 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003399
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003400 /* keep the link from this header to next one in case of later
3401 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003402 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003403 old_idx = cur_idx;
3404 }
3405 return 0;
3406}
3407
3408
3409/* Apply the filter to the request line.
3410 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3411 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003412 * Since it can manage the switch to another backend, it updates the per-proxy
3413 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003414 */
3415int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3416{
3417 char term;
3418 char *cur_ptr, *cur_end;
3419 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003420 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003421 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003422
Willy Tarreau58f10d72006-12-04 02:26:12 +01003423
Willy Tarreau3d300592007-03-18 18:34:41 +01003424 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003425 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003426 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003427 (exp->action == ACT_ALLOW ||
3428 exp->action == ACT_DENY ||
3429 exp->action == ACT_TARPIT))
3430 return 0;
3431 else if (exp->action == ACT_REMOVE)
3432 return 0;
3433
3434 done = 0;
3435
Willy Tarreau9cdde232007-05-02 20:58:19 +02003436 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003437 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003438
3439 /* Now we have the request line between cur_ptr and cur_end */
3440
3441 /* The annoying part is that pattern matching needs
3442 * that we modify the contents to null-terminate all
3443 * strings before testing them.
3444 */
3445
3446 term = *cur_end;
3447 *cur_end = '\0';
3448
3449 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3450 switch (exp->action) {
3451 case ACT_SETBE:
3452 /* It is not possible to jump a second time.
3453 * FIXME: should we return an HTTP/500 here so that
3454 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003455 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003456 if (t->be != t->fe)
3457 break;
3458
3459 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003460 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003461 done = 1;
3462 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003464 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003465 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003466 done = 1;
3467 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003469 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003470 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003471
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003472 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003473 if (t->listener->counters)
3474 t->listener->counters->denied_resp++;
3475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003476 done = 1;
3477 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003478
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003479 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003480 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003481
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003482 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003483 if (t->listener->counters)
3484 t->listener->counters->denied_resp++;
3485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003486 done = 1;
3487 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003488
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003489 case ACT_REPLACE:
3490 *cur_end = term; /* restore the string terminator */
3491 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3492 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3493 /* FIXME: if the user adds a newline in the replacement, the
3494 * index will not be recalculated for now, and the new line
3495 * will not be counted as a new header.
3496 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003497
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003498 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003499 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003500
Willy Tarreau9cdde232007-05-02 20:58:19 +02003501 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003502 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003503 HTTP_MSG_RQMETH,
3504 cur_ptr, cur_end + 1,
3505 NULL, NULL);
3506 if (unlikely(!cur_end))
3507 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003509 /* we have a full request and we know that we have either a CR
3510 * or an LF at <ptr>.
3511 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003512 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3513 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003514 /* there is no point trying this regex on headers */
3515 return 1;
3516 }
3517 }
3518 *cur_end = term; /* restore the string terminator */
3519 return done;
3520}
Willy Tarreau97de6242006-12-27 17:18:38 +01003521
Willy Tarreau58f10d72006-12-04 02:26:12 +01003522
Willy Tarreau58f10d72006-12-04 02:26:12 +01003523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003524/*
3525 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3526 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003527 * unparsable request. Since it can manage the switch to another backend, it
3528 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003529 */
3530int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3531{
Willy Tarreau3d300592007-03-18 18:34:41 +01003532 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003533 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003534 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003535 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003537 /*
3538 * The interleaving of transformations and verdicts
3539 * makes it difficult to decide to continue or stop
3540 * the evaluation.
3541 */
3542
Willy Tarreau3d300592007-03-18 18:34:41 +01003543 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003544 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3545 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3546 exp = exp->next;
3547 continue;
3548 }
3549
3550 /* Apply the filter to the request line. */
3551 ret = apply_filter_to_req_line(t, req, exp);
3552 if (unlikely(ret < 0))
3553 return -1;
3554
3555 if (likely(ret == 0)) {
3556 /* The filter did not match the request, it can be
3557 * iterated through all headers.
3558 */
3559 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003560 }
3561 exp = exp->next;
3562 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003563 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003564}
3565
3566
Willy Tarreaua15645d2007-03-18 16:22:39 +01003567
Willy Tarreau58f10d72006-12-04 02:26:12 +01003568/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003569 * Manage client-side cookie. It can impact performance by about 2% so it is
3570 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003571 */
3572void manage_client_side_cookies(struct session *t, struct buffer *req)
3573{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003574 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003575 char *p1, *p2, *p3, *p4;
3576 char *del_colon, *del_cookie, *colon;
3577 int app_cookies;
3578
3579 appsess *asession_temp = NULL;
3580 appsess local_asession;
3581
3582 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003583 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003584
Willy Tarreau2a324282006-12-05 00:05:46 +01003585 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003586 * we start with the start line.
3587 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003588 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003589 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003590
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003591 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003592 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003593 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003594
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003595 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003596 cur_ptr = cur_next;
3597 cur_end = cur_ptr + cur_hdr->len;
3598 cur_next = cur_end + cur_hdr->cr + 1;
3599
3600 /* We have one full header between cur_ptr and cur_end, and the
3601 * next header starts at cur_next. We're only interested in
3602 * "Cookie:" headers.
3603 */
3604
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003605 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3606 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003607 old_idx = cur_idx;
3608 continue;
3609 }
3610
3611 /* Now look for cookies. Conforming to RFC2109, we have to support
3612 * attributes whose name begin with a '$', and associate them with
3613 * the right cookie, if we want to delete this cookie.
3614 * So there are 3 cases for each cookie read :
3615 * 1) it's a special attribute, beginning with a '$' : ignore it.
3616 * 2) it's a server id cookie that we *MAY* want to delete : save
3617 * some pointers on it (last semi-colon, beginning of cookie...)
3618 * 3) it's an application cookie : we *MAY* have to delete a previous
3619 * "special" cookie.
3620 * At the end of loop, if a "special" cookie remains, we may have to
3621 * remove it. If no application cookie persists in the header, we
3622 * *MUST* delete it
3623 */
3624
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003625 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003626
Willy Tarreau58f10d72006-12-04 02:26:12 +01003627 /* del_cookie == NULL => nothing to be deleted */
3628 del_colon = del_cookie = NULL;
3629 app_cookies = 0;
3630
3631 while (p1 < cur_end) {
3632 /* skip spaces and colons, but keep an eye on these ones */
3633 while (p1 < cur_end) {
3634 if (*p1 == ';' || *p1 == ',')
3635 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003636 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003637 break;
3638 p1++;
3639 }
3640
3641 if (p1 == cur_end)
3642 break;
3643
3644 /* p1 is at the beginning of the cookie name */
3645 p2 = p1;
3646 while (p2 < cur_end && *p2 != '=')
3647 p2++;
3648
3649 if (p2 == cur_end)
3650 break;
3651
3652 p3 = p2 + 1; /* skips the '=' sign */
3653 if (p3 == cur_end)
3654 break;
3655
3656 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003657 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003658 p4++;
3659
3660 /* here, we have the cookie name between p1 and p2,
3661 * and its value between p3 and p4.
3662 * we can process it :
3663 *
3664 * Cookie: NAME=VALUE;
3665 * | || || |
3666 * | || || +--> p4
3667 * | || |+-------> p3
3668 * | || +--------> p2
3669 * | |+------------> p1
3670 * | +-------------> colon
3671 * +--------------------> cur_ptr
3672 */
3673
3674 if (*p1 == '$') {
3675 /* skip this one */
3676 }
3677 else {
3678 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003679 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003680 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003681 (p4 - p1 >= t->fe->capture_namelen) &&
3682 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003683 int log_len = p4 - p1;
3684
Willy Tarreau086b3b42007-05-13 21:45:51 +02003685 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003686 Alert("HTTP logging : out of memory.\n");
3687 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003688 if (log_len > t->fe->capture_len)
3689 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003690 memcpy(txn->cli_cookie, p1, log_len);
3691 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003692 }
3693 }
3694
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003695 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3696 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003697 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003698 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003699 char *delim;
3700
3701 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3702 * have the server ID betweek p3 and delim, and the original cookie between
3703 * delim+1 and p4. Otherwise, delim==p4 :
3704 *
3705 * Cookie: NAME=SRV~VALUE;
3706 * | || || | |
3707 * | || || | +--> p4
3708 * | || || +--------> delim
3709 * | || |+-----------> p3
3710 * | || +------------> p2
3711 * | |+----------------> p1
3712 * | +-----------------> colon
3713 * +------------------------> cur_ptr
3714 */
3715
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003716 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003717 for (delim = p3; delim < p4; delim++)
3718 if (*delim == COOKIE_DELIM)
3719 break;
3720 }
3721 else
3722 delim = p4;
3723
3724
3725 /* Here, we'll look for the first running server which supports the cookie.
3726 * This allows to share a same cookie between several servers, for example
3727 * to dedicate backup servers to specific servers only.
3728 * However, to prevent clients from sticking to cookie-less backup server
3729 * when they have incidentely learned an empty cookie, we simply ignore
3730 * empty cookies and mark them as invalid.
3731 */
3732 if (delim == p3)
3733 srv = NULL;
3734
3735 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003736 if (srv->cookie && (srv->cklen == delim - p3) &&
3737 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003738 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003739 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003740 txn->flags &= ~TX_CK_MASK;
3741 txn->flags |= TX_CK_VALID;
3742 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003743 t->srv = srv;
3744 break;
3745 } else {
3746 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003747 txn->flags &= ~TX_CK_MASK;
3748 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003749 }
3750 }
3751 srv = srv->next;
3752 }
3753
Willy Tarreau3d300592007-03-18 18:34:41 +01003754 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003755 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003756 txn->flags &= ~TX_CK_MASK;
3757 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003758 }
3759
3760 /* depending on the cookie mode, we may have to either :
3761 * - delete the complete cookie if we're in insert+indirect mode, so that
3762 * the server never sees it ;
3763 * - remove the server id from the cookie value, and tag the cookie as an
3764 * application cookie so that it does not get accidentely removed later,
3765 * if we're in cookie prefix mode
3766 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003767 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003768 int delta; /* negative */
3769
3770 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3771 p4 += delta;
3772 cur_end += delta;
3773 cur_next += delta;
3774 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003775 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003776
3777 del_cookie = del_colon = NULL;
3778 app_cookies++; /* protect the header from deletion */
3779 }
3780 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003781 (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 +01003782 del_cookie = p1;
3783 del_colon = colon;
3784 }
3785 } else {
3786 /* now we know that we must keep this cookie since it's
3787 * not ours. But if we wanted to delete our cookie
3788 * earlier, we cannot remove the complete header, but we
3789 * can remove the previous block itself.
3790 */
3791 app_cookies++;
3792
3793 if (del_cookie != NULL) {
3794 int delta; /* negative */
3795
3796 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3797 p4 += delta;
3798 cur_end += delta;
3799 cur_next += delta;
3800 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003801 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003802 del_cookie = del_colon = NULL;
3803 }
3804 }
3805
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003806 if ((t->be->appsession_name != NULL) &&
3807 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003808 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003809
Willy Tarreau58f10d72006-12-04 02:26:12 +01003810 /* Cool... it's the right one */
3811
3812 asession_temp = &local_asession;
3813
Willy Tarreau63963c62007-05-13 21:29:55 +02003814 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003815 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3816 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3817 return;
3818 }
3819
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003820 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3821 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003822 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003823
Willy Tarreau58f10d72006-12-04 02:26:12 +01003824 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003825 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3826 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003827 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003828 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003829 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003830 Alert("Not enough memory process_cli():asession:calloc().\n");
3831 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3832 return;
3833 }
3834
3835 asession_temp->sessid = local_asession.sessid;
3836 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003837 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02003838 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003839 } else {
3840 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003841 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003842 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003843 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003844 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003845 Alert("Found Application Session without matching server.\n");
3846 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003847 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003848 while (srv) {
3849 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003850 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003851 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003852 txn->flags &= ~TX_CK_MASK;
3853 txn->flags |= TX_CK_VALID;
3854 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003855 t->srv = srv;
3856 break;
3857 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01003858 txn->flags &= ~TX_CK_MASK;
3859 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003860 }
3861 }
3862 srv = srv->next;
3863 }/* end while(srv) */
3864 }/* end else if server == NULL */
3865
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003866 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003867 asession_temp->request_count++;
3868#if defined(DEBUG_HASH)
3869 Alert("manage_client_side_cookies\n");
3870 appsession_hash_dump(&(t->be->htbl_proxy));
3871#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01003872 }/* end if ((t->proxy->appsession_name != NULL) ... */
3873 }
3874
3875 /* we'll have to look for another cookie ... */
3876 p1 = p4;
3877 } /* while (p1 < cur_end) */
3878
3879 /* There's no more cookie on this line.
3880 * We may have marked the last one(s) for deletion.
3881 * We must do this now in two ways :
3882 * - if there is no app cookie, we simply delete the header ;
3883 * - if there are app cookies, we must delete the end of the
3884 * string properly, including the colon/semi-colon before
3885 * the cookie name.
3886 */
3887 if (del_cookie != NULL) {
3888 int delta;
3889 if (app_cookies) {
3890 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
3891 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003892 cur_hdr->len += delta;
3893 } else {
3894 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003895
3896 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003897 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3898 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003899 cur_hdr->len = 0;
3900 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01003901 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003902 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003903 }
3904
3905 /* keep the link from this header to next one */
3906 old_idx = cur_idx;
3907 } /* end of cookie processing on this header */
3908}
3909
3910
Willy Tarreaua15645d2007-03-18 16:22:39 +01003911/* Iterate the same filter through all response headers contained in <rtr>.
3912 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3913 */
3914int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3915{
3916 char term;
3917 char *cur_ptr, *cur_end, *cur_next;
3918 int cur_idx, old_idx, last_hdr;
3919 struct http_txn *txn = &t->txn;
3920 struct hdr_idx_elem *cur_hdr;
3921 int len, delta;
3922
3923 last_hdr = 0;
3924
3925 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3926 old_idx = 0;
3927
3928 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003929 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003930 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003931 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003932 (exp->action == ACT_ALLOW ||
3933 exp->action == ACT_DENY))
3934 return 0;
3935
3936 cur_idx = txn->hdr_idx.v[old_idx].next;
3937 if (!cur_idx)
3938 break;
3939
3940 cur_hdr = &txn->hdr_idx.v[cur_idx];
3941 cur_ptr = cur_next;
3942 cur_end = cur_ptr + cur_hdr->len;
3943 cur_next = cur_end + cur_hdr->cr + 1;
3944
3945 /* Now we have one header between cur_ptr and cur_end,
3946 * and the next header starts at cur_next.
3947 */
3948
3949 /* The annoying part is that pattern matching needs
3950 * that we modify the contents to null-terminate all
3951 * strings before testing them.
3952 */
3953
3954 term = *cur_end;
3955 *cur_end = '\0';
3956
3957 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3958 switch (exp->action) {
3959 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003960 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003961 last_hdr = 1;
3962 break;
3963
3964 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003965 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003966 last_hdr = 1;
3967 break;
3968
3969 case ACT_REPLACE:
3970 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3971 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3972 /* FIXME: if the user adds a newline in the replacement, the
3973 * index will not be recalculated for now, and the new line
3974 * will not be counted as a new header.
3975 */
3976
3977 cur_end += delta;
3978 cur_next += delta;
3979 cur_hdr->len += delta;
3980 txn->rsp.eoh += delta;
3981 break;
3982
3983 case ACT_REMOVE:
3984 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3985 cur_next += delta;
3986
3987 /* FIXME: this should be a separate function */
3988 txn->rsp.eoh += delta;
3989 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3990 txn->hdr_idx.used--;
3991 cur_hdr->len = 0;
3992 cur_end = NULL; /* null-term has been rewritten */
3993 break;
3994
3995 }
3996 }
3997 if (cur_end)
3998 *cur_end = term; /* restore the string terminator */
3999
4000 /* keep the link from this header to next one in case of later
4001 * removal of next header.
4002 */
4003 old_idx = cur_idx;
4004 }
4005 return 0;
4006}
4007
4008
4009/* Apply the filter to the status line in the response buffer <rtr>.
4010 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4011 * or -1 if a replacement resulted in an invalid status line.
4012 */
4013int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4014{
4015 char term;
4016 char *cur_ptr, *cur_end;
4017 int done;
4018 struct http_txn *txn = &t->txn;
4019 int len, delta;
4020
4021
Willy Tarreau3d300592007-03-18 18:34:41 +01004022 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004023 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004024 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004025 (exp->action == ACT_ALLOW ||
4026 exp->action == ACT_DENY))
4027 return 0;
4028 else if (exp->action == ACT_REMOVE)
4029 return 0;
4030
4031 done = 0;
4032
Willy Tarreau9cdde232007-05-02 20:58:19 +02004033 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004034 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4035
4036 /* Now we have the status line between cur_ptr and cur_end */
4037
4038 /* The annoying part is that pattern matching needs
4039 * that we modify the contents to null-terminate all
4040 * strings before testing them.
4041 */
4042
4043 term = *cur_end;
4044 *cur_end = '\0';
4045
4046 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4047 switch (exp->action) {
4048 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004049 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004050 done = 1;
4051 break;
4052
4053 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004054 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004055 done = 1;
4056 break;
4057
4058 case ACT_REPLACE:
4059 *cur_end = term; /* restore the string terminator */
4060 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4061 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4062 /* FIXME: if the user adds a newline in the replacement, the
4063 * index will not be recalculated for now, and the new line
4064 * will not be counted as a new header.
4065 */
4066
4067 txn->rsp.eoh += delta;
4068 cur_end += delta;
4069
Willy Tarreau9cdde232007-05-02 20:58:19 +02004070 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004071 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004072 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004073 cur_ptr, cur_end + 1,
4074 NULL, NULL);
4075 if (unlikely(!cur_end))
4076 return -1;
4077
4078 /* we have a full respnse and we know that we have either a CR
4079 * or an LF at <ptr>.
4080 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004081 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004082 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4083 /* there is no point trying this regex on headers */
4084 return 1;
4085 }
4086 }
4087 *cur_end = term; /* restore the string terminator */
4088 return done;
4089}
4090
4091
4092
4093/*
4094 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4095 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4096 * unparsable response.
4097 */
4098int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4099{
Willy Tarreau3d300592007-03-18 18:34:41 +01004100 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004101 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004102 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004103 int ret;
4104
4105 /*
4106 * The interleaving of transformations and verdicts
4107 * makes it difficult to decide to continue or stop
4108 * the evaluation.
4109 */
4110
Willy Tarreau3d300592007-03-18 18:34:41 +01004111 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004112 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4113 exp->action == ACT_PASS)) {
4114 exp = exp->next;
4115 continue;
4116 }
4117
4118 /* Apply the filter to the status line. */
4119 ret = apply_filter_to_sts_line(t, rtr, exp);
4120 if (unlikely(ret < 0))
4121 return -1;
4122
4123 if (likely(ret == 0)) {
4124 /* The filter did not match the response, it can be
4125 * iterated through all headers.
4126 */
4127 apply_filter_to_resp_headers(t, rtr, exp);
4128 }
4129 exp = exp->next;
4130 }
4131 return 0;
4132}
4133
4134
4135
4136/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004137 * Manage server-side cookies. It can impact performance by about 2% so it is
4138 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004139 */
4140void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4141{
4142 struct http_txn *txn = &t->txn;
4143 char *p1, *p2, *p3, *p4;
4144
4145 appsess *asession_temp = NULL;
4146 appsess local_asession;
4147
4148 char *cur_ptr, *cur_end, *cur_next;
4149 int cur_idx, old_idx, delta;
4150
Willy Tarreaua15645d2007-03-18 16:22:39 +01004151 /* Iterate through the headers.
4152 * we start with the start line.
4153 */
4154 old_idx = 0;
4155 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4156
4157 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4158 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004159 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004160
4161 cur_hdr = &txn->hdr_idx.v[cur_idx];
4162 cur_ptr = cur_next;
4163 cur_end = cur_ptr + cur_hdr->len;
4164 cur_next = cur_end + cur_hdr->cr + 1;
4165
4166 /* We have one full header between cur_ptr and cur_end, and the
4167 * next header starts at cur_next. We're only interested in
4168 * "Cookie:" headers.
4169 */
4170
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004171 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4172 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004173 old_idx = cur_idx;
4174 continue;
4175 }
4176
4177 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004178 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004179
4180
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004181 /* maybe we only wanted to see if there was a set-cookie. Note that
4182 * the cookie capture is declared in the fronend.
4183 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004184 if (t->be->cookie_name == NULL &&
4185 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004186 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004187 return;
4188
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004189 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004190
4191 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004192 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4193 break;
4194
4195 /* p1 is at the beginning of the cookie name */
4196 p2 = p1;
4197
4198 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4199 p2++;
4200
4201 if (p2 == cur_end || *p2 == ';') /* next cookie */
4202 break;
4203
4204 p3 = p2 + 1; /* skip the '=' sign */
4205 if (p3 == cur_end)
4206 break;
4207
4208 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004209 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004210 p4++;
4211
4212 /* here, we have the cookie name between p1 and p2,
4213 * and its value between p3 and p4.
4214 * we can process it.
4215 */
4216
4217 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004218 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004219 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004220 (p4 - p1 >= t->fe->capture_namelen) &&
4221 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004222 int log_len = p4 - p1;
4223
Willy Tarreau086b3b42007-05-13 21:45:51 +02004224 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004225 Alert("HTTP logging : out of memory.\n");
4226 }
4227
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004228 if (log_len > t->fe->capture_len)
4229 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004230 memcpy(txn->srv_cookie, p1, log_len);
4231 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004232 }
4233
4234 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004235 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4236 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004237 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004238 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004239
4240 /* If the cookie is in insert mode on a known server, we'll delete
4241 * this occurrence because we'll insert another one later.
4242 * We'll delete it too if the "indirect" option is set and we're in
4243 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004244 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4245 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004246 /* this header must be deleted */
4247 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4248 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4249 txn->hdr_idx.used--;
4250 cur_hdr->len = 0;
4251 cur_next += delta;
4252 txn->rsp.eoh += delta;
4253
Willy Tarreau3d300592007-03-18 18:34:41 +01004254 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004255 }
4256 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004257 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004258 /* replace bytes p3->p4 with the cookie name associated
4259 * with this server since we know it.
4260 */
4261 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4262 cur_hdr->len += delta;
4263 cur_next += delta;
4264 txn->rsp.eoh += delta;
4265
Willy Tarreau3d300592007-03-18 18:34:41 +01004266 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004267 }
4268 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004269 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004270 /* insert the cookie name associated with this server
4271 * before existing cookie, and insert a delimitor between them..
4272 */
4273 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4274 cur_hdr->len += delta;
4275 cur_next += delta;
4276 txn->rsp.eoh += delta;
4277
4278 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004279 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004280 }
4281 }
4282 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004283 else if ((t->be->appsession_name != NULL) &&
4284 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004285
4286 /* Cool... it's the right one */
4287
4288 size_t server_id_len = strlen(t->srv->id) + 1;
4289 asession_temp = &local_asession;
4290
Willy Tarreau63963c62007-05-13 21:29:55 +02004291 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004292 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4293 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4294 return;
4295 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004296 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4297 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004298 asession_temp->serverid = NULL;
4299
4300 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004301 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4302 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004303 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004304 Alert("Not enough Memory process_srv():asession:calloc().\n");
4305 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4306 return;
4307 }
4308 asession_temp->sessid = local_asession.sessid;
4309 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004310 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004311 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4312 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004313 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004314 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004315 }
4316
Willy Tarreaua15645d2007-03-18 16:22:39 +01004317 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004318 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004319 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4320 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4321 return;
4322 }
4323 asession_temp->serverid[0] = '\0';
4324 }
4325
4326 if (asession_temp->serverid[0] == '\0')
4327 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4328
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004329 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004330 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004331#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004332 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004333 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004334#endif
4335 }/* end if ((t->proxy->appsession_name != NULL) ... */
4336 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4337 } /* we're now at the end of the cookie value */
4338
4339 /* keep the link from this header to next one */
4340 old_idx = cur_idx;
4341 } /* end of cookie processing on this header */
4342}
4343
4344
4345
4346/*
4347 * Check if response is cacheable or not. Updates t->flags.
4348 */
4349void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4350{
4351 struct http_txn *txn = &t->txn;
4352 char *p1, *p2;
4353
4354 char *cur_ptr, *cur_end, *cur_next;
4355 int cur_idx;
4356
Willy Tarreau5df51872007-11-25 16:20:08 +01004357 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004358 return;
4359
4360 /* Iterate through the headers.
4361 * we start with the start line.
4362 */
4363 cur_idx = 0;
4364 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4365
4366 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4367 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004368 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004369
4370 cur_hdr = &txn->hdr_idx.v[cur_idx];
4371 cur_ptr = cur_next;
4372 cur_end = cur_ptr + cur_hdr->len;
4373 cur_next = cur_end + cur_hdr->cr + 1;
4374
4375 /* We have one full header between cur_ptr and cur_end, and the
4376 * next header starts at cur_next. We're only interested in
4377 * "Cookie:" headers.
4378 */
4379
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004380 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4381 if (val) {
4382 if ((cur_end - (cur_ptr + val) >= 8) &&
4383 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4384 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4385 return;
4386 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004387 }
4388
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004389 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4390 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004391 continue;
4392
4393 /* OK, right now we know we have a cache-control header at cur_ptr */
4394
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004395 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004396
4397 if (p1 >= cur_end) /* no more info */
4398 continue;
4399
4400 /* p1 is at the beginning of the value */
4401 p2 = p1;
4402
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004403 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004404 p2++;
4405
4406 /* we have a complete value between p1 and p2 */
4407 if (p2 < cur_end && *p2 == '=') {
4408 /* we have something of the form no-cache="set-cookie" */
4409 if ((cur_end - p1 >= 21) &&
4410 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4411 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004412 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004413 continue;
4414 }
4415
4416 /* OK, so we know that either p2 points to the end of string or to a comma */
4417 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4418 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4419 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4420 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004421 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004422 return;
4423 }
4424
4425 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004426 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004427 continue;
4428 }
4429 }
4430}
4431
4432
Willy Tarreau58f10d72006-12-04 02:26:12 +01004433/*
4434 * Try to retrieve a known appsession in the URI, then the associated server.
4435 * If the server is found, it's assigned to the session.
4436 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004437void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004438{
Willy Tarreau3d300592007-03-18 18:34:41 +01004439 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004440 appsess *asession_temp = NULL;
4441 appsess local_asession;
4442 char *request_line;
4443
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004444 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004445 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004446 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004447 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004448 return;
4449
4450 /* skip ';' */
4451 request_line++;
4452
4453 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004454 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004455 return;
4456
4457 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004458 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004459
4460 /* First try if we already have an appsession */
4461 asession_temp = &local_asession;
4462
Willy Tarreau63963c62007-05-13 21:29:55 +02004463 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004464 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4465 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4466 return;
4467 }
4468
4469 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004470 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4471 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004472 asession_temp->serverid = NULL;
4473
4474 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004475 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4476 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004477 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004478 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004479 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004480 Alert("Not enough memory process_cli():asession:calloc().\n");
4481 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4482 return;
4483 }
4484 asession_temp->sessid = local_asession.sessid;
4485 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004486 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004487 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004488 }
4489 else {
4490 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004491 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004492 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004493
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004494 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004495 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004496
Willy Tarreau58f10d72006-12-04 02:26:12 +01004497#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004498 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004499 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004500#endif
4501 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004502 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004503 Alert("Found Application Session without matching server.\n");
4504 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004505 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004506 while (srv) {
4507 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004508 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004509 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004510 txn->flags &= ~TX_CK_MASK;
4511 txn->flags |= TX_CK_VALID;
4512 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004513 t->srv = srv;
4514 break;
4515 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004516 txn->flags &= ~TX_CK_MASK;
4517 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004518 }
4519 }
4520 srv = srv->next;
4521 }
4522 }
4523}
4524
4525
Willy Tarreaub2513902006-12-17 14:52:38 +01004526/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004527 * In a GET or HEAD request, check if the requested URI matches the stats uri
4528 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004529 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004530 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004531 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02004532 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004533 *
4534 * Returns 1 if the session's state changes, otherwise 0.
4535 */
4536int stats_check_uri_auth(struct session *t, struct proxy *backend)
4537{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004538 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004539 struct uri_auth *uri_auth = backend->uri_auth;
4540 struct user_auth *user;
4541 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004542 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004543
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004544 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
4545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004546 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004547 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004548 return 0;
4549
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004550 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004551
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004552 /* the URI is in h */
4553 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004554 return 0;
4555
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004556 h += uri_auth->uri_len;
4557 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4558 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004559 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004560 break;
4561 }
4562 h++;
4563 }
4564
4565 if (uri_auth->refresh) {
4566 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4567 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4568 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004569 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004570 break;
4571 }
4572 h++;
4573 }
4574 }
4575
Willy Tarreau55bb8452007-10-17 18:44:57 +02004576 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4577 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4578 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004579 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02004580 break;
4581 }
4582 h++;
4583 }
4584
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004585 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
4586
Willy Tarreaub2513902006-12-17 14:52:38 +01004587 /* we are in front of a interceptable URI. Let's check
4588 * if there's an authentication and if it's valid.
4589 */
4590 user = uri_auth->users;
4591 if (!user) {
4592 /* no user auth required, it's OK */
4593 authenticated = 1;
4594 } else {
4595 authenticated = 0;
4596
4597 /* a user list is defined, we have to check.
4598 * skip 21 chars for "Authorization: Basic ".
4599 */
4600
4601 /* FIXME: this should move to an earlier place */
4602 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004603 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4604 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4605 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004606 if (len > 14 &&
4607 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02004608 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01004609 break;
4610 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004611 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004612 }
4613
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004614 if (txn->auth_hdr.len < 21 ||
4615 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004616 user = NULL;
4617
4618 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004619 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4620 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004621 user->user_pwd, user->user_len)) {
4622 authenticated = 1;
4623 break;
4624 }
4625 user = user->next;
4626 }
4627 }
4628
4629 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004630 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004631
4632 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004633 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
4634 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004635 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01004636 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02004637 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004638 if (!(t->flags & SN_ERR_MASK))
4639 t->flags |= SN_ERR_PRXCOND;
4640 if (!(t->flags & SN_FINST_MASK))
4641 t->flags |= SN_FINST_R;
4642 return 1;
4643 }
4644
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004645 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01004646 * data.
4647 */
Willy Tarreau70089872008-06-13 21:12:51 +02004648 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01004649 t->data_source = DATA_SRC_STATS;
4650 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02004651 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02004652 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
4653 t->rep->prod->private = t;
4654 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004655 return 1;
4656}
4657
Willy Tarreau4076a152009-04-02 15:18:36 +02004658/*
4659 * Capture a bad request or response and archive it in the proxy's structure.
4660 */
4661void http_capture_bad_message(struct error_snapshot *es, struct session *s,
4662 struct buffer *buf, struct http_msg *msg,
4663 struct proxy *other_end)
4664{
Willy Tarreau2df8d712009-05-01 11:33:17 +02004665 es->len = buf->r - (buf->data + msg->som);
4666 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02004667 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02004668 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02004669 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02004670 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02004671 es->when = date; // user-visible date
4672 es->sid = s->uniq_id;
4673 es->srv = s->srv;
4674 es->oe = other_end;
4675 es->src = s->cli_addr;
4676}
Willy Tarreaub2513902006-12-17 14:52:38 +01004677
Willy Tarreaubaaee002006-06-26 02:48:02 +02004678/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004679 * Print a debug line with a header
4680 */
4681void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4682{
4683 int len, max;
4684 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004685 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004686 max = end - start;
4687 UBOUND(max, sizeof(trash) - len - 1);
4688 len += strlcpy2(trash + len, start, max + 1);
4689 trash[len++] = '\n';
4690 write(1, trash, len);
4691}
4692
4693
Willy Tarreau8797c062007-05-07 00:55:35 +02004694/************************************************************************/
4695/* The code below is dedicated to ACL parsing and matching */
4696/************************************************************************/
4697
4698
4699
4700
4701/* 1. Check on METHOD
4702 * We use the pre-parsed method if it is known, and store its number as an
4703 * integer. If it is unknown, we use the pointer and the length.
4704 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004705static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004706{
4707 int len, meth;
4708
Willy Tarreauae8b7962007-06-09 23:10:04 +02004709 len = strlen(*text);
4710 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004711
4712 pattern->val.i = meth;
4713 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004714 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004715 if (!pattern->ptr.str)
4716 return 0;
4717 pattern->len = len;
4718 }
4719 return 1;
4720}
4721
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004722static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004723acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4724 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004725{
4726 int meth;
4727 struct http_txn *txn = l7;
4728
Willy Tarreaub6866442008-07-14 23:54:42 +02004729 if (!txn)
4730 return 0;
4731
Willy Tarreauc11416f2007-06-17 16:58:38 +02004732 if (txn->req.msg_state != HTTP_MSG_BODY)
4733 return 0;
4734
Willy Tarreau8797c062007-05-07 00:55:35 +02004735 meth = txn->meth;
4736 test->i = meth;
4737 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004738 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4739 /* ensure the indexes are not affected */
4740 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004741 test->len = txn->req.sl.rq.m_l;
4742 test->ptr = txn->req.sol;
4743 }
4744 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4745 return 1;
4746}
4747
4748static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4749{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004750 int icase;
4751
Willy Tarreau8797c062007-05-07 00:55:35 +02004752 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02004753 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02004754
4755 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02004756 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004757
4758 /* Other method, we must compare the strings */
4759 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02004760 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004761
4762 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4763 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4764 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02004765 return ACL_PAT_FAIL;
4766 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004767}
4768
4769/* 2. Check on Request/Status Version
4770 * We simply compare strings here.
4771 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004772static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004773{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004774 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004775 if (!pattern->ptr.str)
4776 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004777 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004778 return 1;
4779}
4780
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004781static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004782acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4783 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004784{
4785 struct http_txn *txn = l7;
4786 char *ptr;
4787 int len;
4788
Willy Tarreaub6866442008-07-14 23:54:42 +02004789 if (!txn)
4790 return 0;
4791
Willy Tarreauc11416f2007-06-17 16:58:38 +02004792 if (txn->req.msg_state != HTTP_MSG_BODY)
4793 return 0;
4794
Willy Tarreau8797c062007-05-07 00:55:35 +02004795 len = txn->req.sl.rq.v_l;
4796 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4797
4798 while ((len-- > 0) && (*ptr++ != '/'));
4799 if (len <= 0)
4800 return 0;
4801
4802 test->ptr = ptr;
4803 test->len = len;
4804
4805 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4806 return 1;
4807}
4808
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004809static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004810acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4811 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004812{
4813 struct http_txn *txn = l7;
4814 char *ptr;
4815 int len;
4816
Willy Tarreaub6866442008-07-14 23:54:42 +02004817 if (!txn)
4818 return 0;
4819
Willy Tarreauc11416f2007-06-17 16:58:38 +02004820 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4821 return 0;
4822
Willy Tarreau8797c062007-05-07 00:55:35 +02004823 len = txn->rsp.sl.st.v_l;
4824 ptr = txn->rsp.sol;
4825
4826 while ((len-- > 0) && (*ptr++ != '/'));
4827 if (len <= 0)
4828 return 0;
4829
4830 test->ptr = ptr;
4831 test->len = len;
4832
4833 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4834 return 1;
4835}
4836
4837/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004838static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004839acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4840 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004841{
4842 struct http_txn *txn = l7;
4843 char *ptr;
4844 int len;
4845
Willy Tarreaub6866442008-07-14 23:54:42 +02004846 if (!txn)
4847 return 0;
4848
Willy Tarreauc11416f2007-06-17 16:58:38 +02004849 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4850 return 0;
4851
Willy Tarreau8797c062007-05-07 00:55:35 +02004852 len = txn->rsp.sl.st.c_l;
4853 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4854
4855 test->i = __strl2ui(ptr, len);
4856 test->flags = ACL_TEST_F_VOL_1ST;
4857 return 1;
4858}
4859
4860/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004861static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004862acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4863 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004864{
4865 struct http_txn *txn = l7;
4866
Willy Tarreaub6866442008-07-14 23:54:42 +02004867 if (!txn)
4868 return 0;
4869
Willy Tarreauc11416f2007-06-17 16:58:38 +02004870 if (txn->req.msg_state != HTTP_MSG_BODY)
4871 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004872
Willy Tarreauc11416f2007-06-17 16:58:38 +02004873 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4874 /* ensure the indexes are not affected */
4875 return 0;
4876
Willy Tarreau8797c062007-05-07 00:55:35 +02004877 test->len = txn->req.sl.rq.u_l;
4878 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4879
Willy Tarreauf3d25982007-05-08 22:45:09 +02004880 /* we do not need to set READ_ONLY because the data is in a buffer */
4881 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004882 return 1;
4883}
4884
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004885static int
4886acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
4887 struct acl_expr *expr, struct acl_test *test)
4888{
4889 struct http_txn *txn = l7;
4890
Willy Tarreaub6866442008-07-14 23:54:42 +02004891 if (!txn)
4892 return 0;
4893
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004894 if (txn->req.msg_state != HTTP_MSG_BODY)
4895 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004896
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004897 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4898 /* ensure the indexes are not affected */
4899 return 0;
4900
4901 /* Parse HTTP request */
4902 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4903 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
4904 test->i = AF_INET;
4905
4906 /*
4907 * If we are parsing url in frontend space, we prepare backend stage
4908 * to not parse again the same url ! optimization lazyness...
4909 */
4910 if (px->options & PR_O_HTTP_PROXY)
4911 l4->flags |= SN_ADDR_SET;
4912
4913 test->flags = ACL_TEST_F_READ_ONLY;
4914 return 1;
4915}
4916
4917static int
4918acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
4919 struct acl_expr *expr, struct acl_test *test)
4920{
4921 struct http_txn *txn = l7;
4922
Willy Tarreaub6866442008-07-14 23:54:42 +02004923 if (!txn)
4924 return 0;
4925
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004926 if (txn->req.msg_state != HTTP_MSG_BODY)
4927 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004928
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004929 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4930 /* ensure the indexes are not affected */
4931 return 0;
4932
4933 /* Same optimization as url_ip */
4934 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4935 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
4936
4937 if (px->options & PR_O_HTTP_PROXY)
4938 l4->flags |= SN_ADDR_SET;
4939
4940 test->flags = ACL_TEST_F_READ_ONLY;
4941 return 1;
4942}
4943
Willy Tarreauc11416f2007-06-17 16:58:38 +02004944/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
4945 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
4946 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02004947static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004948acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004949 struct acl_expr *expr, struct acl_test *test)
4950{
4951 struct http_txn *txn = l7;
4952 struct hdr_idx *idx = &txn->hdr_idx;
4953 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004954
Willy Tarreaub6866442008-07-14 23:54:42 +02004955 if (!txn)
4956 return 0;
4957
Willy Tarreau33a7e692007-06-10 19:45:56 +02004958 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4959 /* search for header from the beginning */
4960 ctx->idx = 0;
4961
Willy Tarreau33a7e692007-06-10 19:45:56 +02004962 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4963 test->flags |= ACL_TEST_F_FETCH_MORE;
4964 test->flags |= ACL_TEST_F_VOL_HDR;
4965 test->len = ctx->vlen;
4966 test->ptr = (char *)ctx->line + ctx->val;
4967 return 1;
4968 }
4969
4970 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4971 test->flags |= ACL_TEST_F_VOL_HDR;
4972 return 0;
4973}
4974
Willy Tarreau33a7e692007-06-10 19:45:56 +02004975static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004976acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
4977 struct acl_expr *expr, struct acl_test *test)
4978{
4979 struct http_txn *txn = l7;
4980
Willy Tarreaub6866442008-07-14 23:54:42 +02004981 if (!txn)
4982 return 0;
4983
Willy Tarreauc11416f2007-06-17 16:58:38 +02004984 if (txn->req.msg_state != HTTP_MSG_BODY)
4985 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004986
Willy Tarreauc11416f2007-06-17 16:58:38 +02004987 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4988 /* ensure the indexes are not affected */
4989 return 0;
4990
4991 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
4992}
4993
4994static int
4995acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
4996 struct acl_expr *expr, struct acl_test *test)
4997{
4998 struct http_txn *txn = l7;
4999
Willy Tarreaub6866442008-07-14 23:54:42 +02005000 if (!txn)
5001 return 0;
5002
Willy Tarreauc11416f2007-06-17 16:58:38 +02005003 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5004 return 0;
5005
5006 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5007}
5008
5009/* 6. Check on HTTP header count. The number of occurrences is returned.
5010 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5011 */
5012static int
5013acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005014 struct acl_expr *expr, struct acl_test *test)
5015{
5016 struct http_txn *txn = l7;
5017 struct hdr_idx *idx = &txn->hdr_idx;
5018 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005019 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005020
Willy Tarreaub6866442008-07-14 23:54:42 +02005021 if (!txn)
5022 return 0;
5023
Willy Tarreau33a7e692007-06-10 19:45:56 +02005024 ctx.idx = 0;
5025 cnt = 0;
5026 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5027 cnt++;
5028
5029 test->i = cnt;
5030 test->flags = ACL_TEST_F_VOL_HDR;
5031 return 1;
5032}
5033
Willy Tarreauc11416f2007-06-17 16:58:38 +02005034static int
5035acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5036 struct acl_expr *expr, struct acl_test *test)
5037{
5038 struct http_txn *txn = l7;
5039
Willy Tarreaub6866442008-07-14 23:54:42 +02005040 if (!txn)
5041 return 0;
5042
Willy Tarreauc11416f2007-06-17 16:58:38 +02005043 if (txn->req.msg_state != HTTP_MSG_BODY)
5044 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005045
Willy Tarreauc11416f2007-06-17 16:58:38 +02005046 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5047 /* ensure the indexes are not affected */
5048 return 0;
5049
5050 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5051}
5052
5053static int
5054acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5055 struct acl_expr *expr, struct acl_test *test)
5056{
5057 struct http_txn *txn = l7;
5058
Willy Tarreaub6866442008-07-14 23:54:42 +02005059 if (!txn)
5060 return 0;
5061
Willy Tarreauc11416f2007-06-17 16:58:38 +02005062 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5063 return 0;
5064
5065 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5066}
5067
Willy Tarreau33a7e692007-06-10 19:45:56 +02005068/* 7. Check on HTTP header's integer value. The integer value is returned.
5069 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005070 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005071 */
5072static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005073acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005074 struct acl_expr *expr, struct acl_test *test)
5075{
5076 struct http_txn *txn = l7;
5077 struct hdr_idx *idx = &txn->hdr_idx;
5078 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005079
Willy Tarreaub6866442008-07-14 23:54:42 +02005080 if (!txn)
5081 return 0;
5082
Willy Tarreau33a7e692007-06-10 19:45:56 +02005083 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5084 /* search for header from the beginning */
5085 ctx->idx = 0;
5086
Willy Tarreau33a7e692007-06-10 19:45:56 +02005087 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5088 test->flags |= ACL_TEST_F_FETCH_MORE;
5089 test->flags |= ACL_TEST_F_VOL_HDR;
5090 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5091 return 1;
5092 }
5093
5094 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5095 test->flags |= ACL_TEST_F_VOL_HDR;
5096 return 0;
5097}
5098
Willy Tarreauc11416f2007-06-17 16:58:38 +02005099static int
5100acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5101 struct acl_expr *expr, struct acl_test *test)
5102{
5103 struct http_txn *txn = l7;
5104
Willy Tarreaub6866442008-07-14 23:54:42 +02005105 if (!txn)
5106 return 0;
5107
Willy Tarreauc11416f2007-06-17 16:58:38 +02005108 if (txn->req.msg_state != HTTP_MSG_BODY)
5109 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005110
Willy Tarreauc11416f2007-06-17 16:58:38 +02005111 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5112 /* ensure the indexes are not affected */
5113 return 0;
5114
5115 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5116}
5117
5118static int
5119acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5120 struct acl_expr *expr, struct acl_test *test)
5121{
5122 struct http_txn *txn = l7;
5123
Willy Tarreaub6866442008-07-14 23:54:42 +02005124 if (!txn)
5125 return 0;
5126
Willy Tarreauc11416f2007-06-17 16:58:38 +02005127 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5128 return 0;
5129
5130 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5131}
5132
Willy Tarreau106f9792009-09-19 07:54:16 +02005133/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
5134 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5135 */
5136static int
5137acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
5138 struct acl_expr *expr, struct acl_test *test)
5139{
5140 struct http_txn *txn = l7;
5141 struct hdr_idx *idx = &txn->hdr_idx;
5142 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
5143
5144 if (!txn)
5145 return 0;
5146
5147 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5148 /* search for header from the beginning */
5149 ctx->idx = 0;
5150
5151 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5152 test->flags |= ACL_TEST_F_FETCH_MORE;
5153 test->flags |= ACL_TEST_F_VOL_HDR;
5154 /* Same optimization as url_ip */
5155 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
5156 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
5157 test->ptr = (void *)&l4->srv_addr.sin_addr;
5158 test->i = AF_INET;
5159 return 1;
5160 }
5161
5162 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5163 test->flags |= ACL_TEST_F_VOL_HDR;
5164 return 0;
5165}
5166
5167static int
5168acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5169 struct acl_expr *expr, struct acl_test *test)
5170{
5171 struct http_txn *txn = l7;
5172
5173 if (!txn)
5174 return 0;
5175
5176 if (txn->req.msg_state != HTTP_MSG_BODY)
5177 return 0;
5178
5179 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5180 /* ensure the indexes are not affected */
5181 return 0;
5182
5183 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
5184}
5185
5186static int
5187acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5188 struct acl_expr *expr, struct acl_test *test)
5189{
5190 struct http_txn *txn = l7;
5191
5192 if (!txn)
5193 return 0;
5194
5195 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5196 return 0;
5197
5198 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
5199}
5200
Willy Tarreau737b0c12007-06-10 21:28:46 +02005201/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5202 * the first '/' after the possible hostname, and ends before the possible '?'.
5203 */
5204static int
5205acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5206 struct acl_expr *expr, struct acl_test *test)
5207{
5208 struct http_txn *txn = l7;
5209 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005210
Willy Tarreaub6866442008-07-14 23:54:42 +02005211 if (!txn)
5212 return 0;
5213
Willy Tarreauc11416f2007-06-17 16:58:38 +02005214 if (txn->req.msg_state != HTTP_MSG_BODY)
5215 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005216
Willy Tarreauc11416f2007-06-17 16:58:38 +02005217 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5218 /* ensure the indexes are not affected */
5219 return 0;
5220
Willy Tarreau21d2af32008-02-14 20:25:24 +01005221 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5222 ptr = http_get_path(txn);
5223 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005224 return 0;
5225
5226 /* OK, we got the '/' ! */
5227 test->ptr = ptr;
5228
5229 while (ptr < end && *ptr != '?')
5230 ptr++;
5231
5232 test->len = ptr - test->ptr;
5233
5234 /* we do not need to set READ_ONLY because the data is in a buffer */
5235 test->flags = ACL_TEST_F_VOL_1ST;
5236 return 1;
5237}
5238
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005239static int
5240acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
5241 struct acl_expr *expr, struct acl_test *test)
5242{
5243 struct buffer *req = s->req;
5244 struct http_txn *txn = &s->txn;
5245 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02005246
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005247 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
5248 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
5249 */
5250
5251 if (!s || !req)
5252 return 0;
5253
5254 if (unlikely(msg->msg_state == HTTP_MSG_BODY)) {
5255 /* Already decoded as OK */
5256 test->flags |= ACL_TEST_F_SET_RES_PASS;
5257 return 1;
5258 }
5259
5260 /* Try to decode HTTP request */
5261 if (likely(req->lr < req->r))
5262 http_msg_analyzer(req, msg, &txn->hdr_idx);
5263
5264 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
5265 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
5266 test->flags |= ACL_TEST_F_SET_RES_FAIL;
5267 return 1;
5268 }
5269 /* wait for final state */
5270 test->flags |= ACL_TEST_F_MAY_CHANGE;
5271 return 0;
5272 }
5273
5274 /* OK we got a valid HTTP request. We have some minor preparation to
5275 * perform so that further checks can rely on HTTP tests.
5276 */
5277 msg->sol = req->data + msg->som;
5278 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
5279 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
5280 s->flags |= SN_REDIRECTABLE;
5281
5282 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
5283 test->flags |= ACL_TEST_F_SET_RES_FAIL;
5284 return 1;
5285 }
5286
5287 test->flags |= ACL_TEST_F_SET_RES_PASS;
5288 return 1;
5289}
5290
Willy Tarreau8797c062007-05-07 00:55:35 +02005291
5292/************************************************************************/
5293/* All supported keywords must be declared here. */
5294/************************************************************************/
5295
5296/* Note: must not be declared <const> as its list will be overwritten */
5297static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005298 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
5299
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005300 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5301 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5302 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5303 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005304
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005305 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5306 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5307 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5308 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5309 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5310 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5311 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5312 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5313 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005314
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005315 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5316 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5317 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5318 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5319 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5320 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5321 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5322 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5323 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5324 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02005325 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005326
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005327 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5328 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5329 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5330 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5331 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5332 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5333 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5334 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5335 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02005336 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005337
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005338 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5339 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5340 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5341 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5342 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5343 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5344 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005345
Willy Tarreauf3d25982007-05-08 22:45:09 +02005346 { NULL, NULL, NULL, NULL },
5347
5348#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005349 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5350 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5351 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5352 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5353 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5354 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5355 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5356
Willy Tarreau8797c062007-05-07 00:55:35 +02005357 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5358 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5359 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5360 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5361 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5362 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5363 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5364 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5365
5366 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5367 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5368 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5369 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5370 { NULL, NULL, NULL, NULL },
5371#endif
5372}};
5373
5374
5375__attribute__((constructor))
5376static void __http_protocol_init(void)
5377{
5378 acl_register_keywords(&acl_kws);
5379}
5380
5381
Willy Tarreau58f10d72006-12-04 02:26:12 +01005382/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005383 * Local variables:
5384 * c-indent-level: 8
5385 * c-basic-offset: 8
5386 * End:
5387 */