blob: a3948f51fbef0bd4e57da1e37fd67f9aa0e5b77b [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 Tarreau6d1a9882007-01-07 02:03:04 +010059#ifdef CONFIG_HAP_TCPSPLICE
60#include <libtcpsplice.h>
61#endif
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Willy Tarreau1c47f852006-07-09 08:22:27 +020063/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010064const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020065 "HTTP/1.0 200 OK\r\n"
66 "Cache-Control: no-cache\r\n"
67 "Connection: close\r\n"
68 "Content-Type: text/html\r\n"
69 "\r\n"
70 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
71
Willy Tarreau0f772532006-12-23 20:51:41 +010072const struct chunk http_200_chunk = {
73 .str = (char *)&HTTP_200,
74 .len = sizeof(HTTP_200)-1
75};
76
Willy Tarreaub463dfb2008-06-07 23:08:56 +020077const char *HTTP_301 =
78 "HTTP/1.0 301 Moved Permantenly\r\n"
79 "Cache-Control: no-cache\r\n"
80 "Connection: close\r\n"
81 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
84 "HTTP/1.0 302 Found\r\n"
85 "Cache-Control: no-cache\r\n"
86 "Connection: close\r\n"
87 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
91 "HTTP/1.0 303 See Other\r\n"
92 "Cache-Control: no-cache\r\n"
93 "Connection: close\r\n"
94 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Willy Tarreaubaaee002006-06-26 02:48:02 +020096/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
97const char *HTTP_401_fmt =
98 "HTTP/1.0 401 Unauthorized\r\n"
99 "Cache-Control: no-cache\r\n"
100 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200101 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
103 "\r\n"
104 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
105
Willy Tarreau0f772532006-12-23 20:51:41 +0100106
107const int http_err_codes[HTTP_ERR_SIZE] = {
108 [HTTP_ERR_400] = 400,
109 [HTTP_ERR_403] = 403,
110 [HTTP_ERR_408] = 408,
111 [HTTP_ERR_500] = 500,
112 [HTTP_ERR_502] = 502,
113 [HTTP_ERR_503] = 503,
114 [HTTP_ERR_504] = 504,
115};
116
Willy Tarreau80587432006-12-24 17:47:20 +0100117static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100118 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100119 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100120 "Cache-Control: no-cache\r\n"
121 "Connection: close\r\n"
122 "Content-Type: text/html\r\n"
123 "\r\n"
124 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
125
126 [HTTP_ERR_403] =
127 "HTTP/1.0 403 Forbidden\r\n"
128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "\r\n"
132 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
133
134 [HTTP_ERR_408] =
135 "HTTP/1.0 408 Request Time-out\r\n"
136 "Cache-Control: no-cache\r\n"
137 "Connection: close\r\n"
138 "Content-Type: text/html\r\n"
139 "\r\n"
140 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
141
142 [HTTP_ERR_500] =
143 "HTTP/1.0 500 Server Error\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
149
150 [HTTP_ERR_502] =
151 "HTTP/1.0 502 Bad Gateway\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
157
158 [HTTP_ERR_503] =
159 "HTTP/1.0 503 Service Unavailable\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
165
166 [HTTP_ERR_504] =
167 "HTTP/1.0 504 Gateway Time-out\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
173
174};
175
Willy Tarreau80587432006-12-24 17:47:20 +0100176/* We must put the messages here since GCC cannot initialize consts depending
177 * on strlen().
178 */
179struct chunk http_err_chunks[HTTP_ERR_SIZE];
180
Willy Tarreau42250582007-04-01 01:30:43 +0200181#define FD_SETS_ARE_BITFIELDS
182#ifdef FD_SETS_ARE_BITFIELDS
183/*
184 * This map is used with all the FD_* macros to check whether a particular bit
185 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
186 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
187 * byte should be encoded. Be careful to always pass bytes from 0 to 255
188 * exclusively to the macros.
189 */
190fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
191fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
192
193#else
194#error "Check if your OS uses bitfields for fd_sets"
195#endif
196
Willy Tarreau80587432006-12-24 17:47:20 +0100197void init_proto_http()
198{
Willy Tarreau42250582007-04-01 01:30:43 +0200199 int i;
200 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100201 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200202
Willy Tarreau80587432006-12-24 17:47:20 +0100203 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
204 if (!http_err_msgs[msg]) {
205 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
206 abort();
207 }
208
209 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
210 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
211 }
Willy Tarreau42250582007-04-01 01:30:43 +0200212
213 /* initialize the log header encoding map : '{|}"#' should be encoded with
214 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
215 * URL encoding only requires '"', '#' to be encoded as well as non-
216 * printable characters above.
217 */
218 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
219 memset(url_encode_map, 0, sizeof(url_encode_map));
220 for (i = 0; i < 32; i++) {
221 FD_SET(i, hdr_encode_map);
222 FD_SET(i, url_encode_map);
223 }
224 for (i = 127; i < 256; i++) {
225 FD_SET(i, hdr_encode_map);
226 FD_SET(i, url_encode_map);
227 }
228
229 tmp = "\"#{|}";
230 while (*tmp) {
231 FD_SET(*tmp, hdr_encode_map);
232 tmp++;
233 }
234
235 tmp = "\"#";
236 while (*tmp) {
237 FD_SET(*tmp, url_encode_map);
238 tmp++;
239 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200240
241 /* memory allocations */
242 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200243 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100244}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245
Willy Tarreau53b6c742006-12-17 13:37:46 +0100246/*
247 * We have 26 list of methods (1 per first letter), each of which can have
248 * up to 3 entries (2 valid, 1 null).
249 */
250struct http_method_desc {
251 http_meth_t meth;
252 int len;
253 const char text[8];
254};
255
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100256const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100257 ['C' - 'A'] = {
258 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
259 },
260 ['D' - 'A'] = {
261 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
262 },
263 ['G' - 'A'] = {
264 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
265 },
266 ['H' - 'A'] = {
267 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
268 },
269 ['P' - 'A'] = {
270 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
271 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
272 },
273 ['T' - 'A'] = {
274 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
275 },
276 /* rest is empty like this :
277 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
278 */
279};
280
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100281/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200282 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100283 * RFC2616 for those chars.
284 */
285
286const char http_is_spht[256] = {
287 [' '] = 1, ['\t'] = 1,
288};
289
290const char http_is_crlf[256] = {
291 ['\r'] = 1, ['\n'] = 1,
292};
293
294const char http_is_lws[256] = {
295 [' '] = 1, ['\t'] = 1,
296 ['\r'] = 1, ['\n'] = 1,
297};
298
299const char http_is_sep[256] = {
300 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
301 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
302 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
303 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
304 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
305};
306
307const char http_is_ctl[256] = {
308 [0 ... 31] = 1,
309 [127] = 1,
310};
311
312/*
313 * A token is any ASCII char that is neither a separator nor a CTL char.
314 * Do not overwrite values in assignment since gcc-2.95 will not handle
315 * them correctly. Instead, define every non-CTL char's status.
316 */
317const char http_is_token[256] = {
318 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
319 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
320 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
321 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
322 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
323 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
324 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
325 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
326 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
327 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
328 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
329 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
330 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
331 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
332 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
333 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
334 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
335 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
336 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
337 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
338 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
339 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
340 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
341 ['|'] = 1, ['}'] = 0, ['~'] = 1,
342};
343
344
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100345/*
346 * An http ver_token is any ASCII which can be found in an HTTP version,
347 * which includes 'H', 'T', 'P', '/', '.' and any digit.
348 */
349const char http_is_ver_token[256] = {
350 ['.'] = 1, ['/'] = 1,
351 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
352 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
353 ['H'] = 1, ['P'] = 1, ['T'] = 1,
354};
355
356
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100357/*
358 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
359 * CRLF. Text length is measured first, so it cannot be NULL.
360 * The header is also automatically added to the index <hdr_idx>, and the end
361 * of headers is automatically adjusted. The number of bytes added is returned
362 * on success, otherwise <0 is returned indicating an error.
363 */
364int http_header_add_tail(struct buffer *b, struct http_msg *msg,
365 struct hdr_idx *hdr_idx, const char *text)
366{
367 int bytes, len;
368
369 len = strlen(text);
370 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
371 if (!bytes)
372 return -1;
373 msg->eoh += bytes;
374 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
375}
376
377/*
378 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
379 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
380 * the buffer is only opened and the space reserved, but nothing is copied.
381 * The header is also automatically added to the index <hdr_idx>, and the end
382 * of headers is automatically adjusted. The number of bytes added is returned
383 * on success, otherwise <0 is returned indicating an error.
384 */
385int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
386 struct hdr_idx *hdr_idx, const char *text, int len)
387{
388 int bytes;
389
390 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
391 if (!bytes)
392 return -1;
393 msg->eoh += bytes;
394 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
395}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200396
397/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100398 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
399 * If so, returns the position of the first non-space character relative to
400 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
401 * to return a pointer to the place after the first space. Returns 0 if the
402 * header name does not match. Checks are case-insensitive.
403 */
404int http_header_match2(const char *hdr, const char *end,
405 const char *name, int len)
406{
407 const char *val;
408
409 if (hdr + len >= end)
410 return 0;
411 if (hdr[len] != ':')
412 return 0;
413 if (strncasecmp(hdr, name, len) != 0)
414 return 0;
415 val = hdr + len + 1;
416 while (val < end && HTTP_IS_SPHT(*val))
417 val++;
418 if ((val >= end) && (len + 2 <= end - hdr))
419 return len + 2; /* we may replace starting from second space */
420 return val - hdr;
421}
422
Willy Tarreau33a7e692007-06-10 19:45:56 +0200423/* Find the end of the header value contained between <s> and <e>.
424 * See RFC2616, par 2.2 for more information. Note that it requires
425 * a valid header to return a valid result.
426 */
427const char *find_hdr_value_end(const char *s, const char *e)
428{
429 int quoted, qdpair;
430
431 quoted = qdpair = 0;
432 for (; s < e; s++) {
433 if (qdpair) qdpair = 0;
434 else if (quoted && *s == '\\') qdpair = 1;
435 else if (quoted && *s == '"') quoted = 0;
436 else if (*s == '"') quoted = 1;
437 else if (*s == ',') return s;
438 }
439 return s;
440}
441
442/* Find the first or next occurrence of header <name> in message buffer <sol>
443 * using headers index <idx>, and return it in the <ctx> structure. This
444 * structure holds everything necessary to use the header and find next
445 * occurrence. If its <idx> member is 0, the header is searched from the
446 * beginning. Otherwise, the next occurrence is returned. The function returns
447 * 1 when it finds a value, and 0 when there is no more.
448 */
449int http_find_header2(const char *name, int len,
450 const char *sol, struct hdr_idx *idx,
451 struct hdr_ctx *ctx)
452{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200453 const char *eol, *sov;
454 int cur_idx;
455
456 if (ctx->idx) {
457 /* We have previously returned a value, let's search
458 * another one on the same line.
459 */
460 cur_idx = ctx->idx;
461 sol = ctx->line;
462 sov = sol + ctx->val + ctx->vlen;
463 eol = sol + idx->v[cur_idx].len;
464
465 if (sov >= eol)
466 /* no more values in this header */
467 goto next_hdr;
468
469 /* values remaining for this header, skip the comma */
470 sov++;
471 while (sov < eol && http_is_lws[(unsigned char)*sov])
472 sov++;
473
474 goto return_hdr;
475 }
476
477 /* first request for this header */
478 sol += hdr_idx_first_pos(idx);
479 cur_idx = hdr_idx_first_idx(idx);
480
481 while (cur_idx) {
482 eol = sol + idx->v[cur_idx].len;
483
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200484 if (len == 0) {
485 /* No argument was passed, we want any header.
486 * To achieve this, we simply build a fake request. */
487 while (sol + len < eol && sol[len] != ':')
488 len++;
489 name = sol;
490 }
491
Willy Tarreau33a7e692007-06-10 19:45:56 +0200492 if ((len < eol - sol) &&
493 (sol[len] == ':') &&
494 (strncasecmp(sol, name, len) == 0)) {
495
496 sov = sol + len + 1;
497 while (sov < eol && http_is_lws[(unsigned char)*sov])
498 sov++;
499 return_hdr:
500 ctx->line = sol;
501 ctx->idx = cur_idx;
502 ctx->val = sov - sol;
503
504 eol = find_hdr_value_end(sov, eol);
505 ctx->vlen = eol - sov;
506 return 1;
507 }
508 next_hdr:
509 sol = eol + idx->v[cur_idx].cr + 1;
510 cur_idx = idx->v[cur_idx].next;
511 }
512 return 0;
513}
514
515int http_find_header(const char *name,
516 const char *sol, struct hdr_idx *idx,
517 struct hdr_ctx *ctx)
518{
519 return http_find_header2(name, strlen(name), sol, idx, ctx);
520}
521
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100522/* This function handles a server error at the stream interface level. The
523 * stream interface is assumed to be already in a closed state. An optional
524 * message is copied into the input buffer, and an HTTP status code stored.
525 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100526 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200527 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100528static void http_server_error(struct session *t, struct stream_interface *si,
529 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200530{
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100531 buffer_erase(si->ob);
532 buffer_erase(si->ib);
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100533 buffer_write_ena(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100534 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100535 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100536 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537 }
538 if (!(t->flags & SN_ERR_MASK))
539 t->flags |= err;
540 if (!(t->flags & SN_FINST_MASK))
541 t->flags |= finst;
542}
543
Willy Tarreau80587432006-12-24 17:47:20 +0100544/* This function returns the appropriate error location for the given session
545 * and message.
546 */
547
548struct chunk *error_message(struct session *s, int msgnum)
549{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200550 if (s->be->errmsg[msgnum].str)
551 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100552 else if (s->fe->errmsg[msgnum].str)
553 return &s->fe->errmsg[msgnum];
554 else
555 return &http_err_chunks[msgnum];
556}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200557
Willy Tarreau53b6c742006-12-17 13:37:46 +0100558/*
559 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
560 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
561 */
562static http_meth_t find_http_meth(const char *str, const int len)
563{
564 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100565 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100566
567 m = ((unsigned)*str - 'A');
568
569 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100570 for (h = http_methods[m]; h->len > 0; h++) {
571 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100572 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100573 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100574 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100575 };
576 return HTTP_METH_OTHER;
577 }
578 return HTTP_METH_NONE;
579
580}
581
Willy Tarreau21d2af32008-02-14 20:25:24 +0100582/* Parse the URI from the given transaction (which is assumed to be in request
583 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
584 * It is returned otherwise.
585 */
586static char *
587http_get_path(struct http_txn *txn)
588{
589 char *ptr, *end;
590
591 ptr = txn->req.sol + txn->req.sl.rq.u;
592 end = ptr + txn->req.sl.rq.u_l;
593
594 if (ptr >= end)
595 return NULL;
596
597 /* RFC2616, par. 5.1.2 :
598 * Request-URI = "*" | absuri | abspath | authority
599 */
600
601 if (*ptr == '*')
602 return NULL;
603
604 if (isalpha((unsigned char)*ptr)) {
605 /* this is a scheme as described by RFC3986, par. 3.1 */
606 ptr++;
607 while (ptr < end &&
608 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
609 ptr++;
610 /* skip '://' */
611 if (ptr == end || *ptr++ != ':')
612 return NULL;
613 if (ptr == end || *ptr++ != '/')
614 return NULL;
615 if (ptr == end || *ptr++ != '/')
616 return NULL;
617 }
618 /* skip [user[:passwd]@]host[:[port]] */
619
620 while (ptr < end && *ptr != '/')
621 ptr++;
622
623 if (ptr == end)
624 return NULL;
625
626 /* OK, we got the '/' ! */
627 return ptr;
628}
629
Willy Tarreauefb453c2008-10-26 20:49:47 +0100630/* Returns a 302 for a redirectable request. This may only be called just after
631 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
632 * left unchanged and will follow normal proxy processing.
633 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100634void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100635{
636 struct http_txn *txn;
637 struct chunk rdr;
638 char *path;
639 int len;
640
641 /* 1: create the response header */
642 rdr.len = strlen(HTTP_302);
643 rdr.str = trash;
644 memcpy(rdr.str, HTTP_302, rdr.len);
645
646 /* 2: add the server's prefix */
647 if (rdr.len + s->srv->rdr_len > sizeof(trash))
648 return;
649
650 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
651 rdr.len += s->srv->rdr_len;
652
653 /* 3: add the request URI */
654 txn = &s->txn;
655 path = http_get_path(txn);
656 if (!path)
657 return;
658
659 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
660 if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */
661 return;
662
663 memcpy(rdr.str + rdr.len, path, len);
664 rdr.len += len;
665 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
666 rdr.len += 4;
667
668 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100669 si->shutr(si);
670 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100671 si->err_type = SI_ET_NONE;
672 si->err_loc = NULL;
673 si->state = SI_ST_CLO;
674
675 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100676 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100677
678 /* FIXME: we should increase a counter of redirects per server and per backend. */
679 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100680 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100681}
682
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100683/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100684 * that the server side is closed. Note that err_type is actually a
685 * bitmask, where almost only aborts may be cumulated with other
686 * values. We consider that aborted operations are more important
687 * than timeouts or errors due to the fact that nobody else in the
688 * logs might explain incomplete retries. All others should avoid
689 * being cumulated. It should normally not be possible to have multiple
690 * aborts at once, but just in case, the first one in sequence is reported.
691 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100692void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100693{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100694 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100695
696 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100697 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
698 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100699 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100700 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
701 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100702 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100703 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
704 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100705 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100706 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
707 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100708 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100709 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
710 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100711 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100712 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
713 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100714 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100715 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
716 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100717}
718
Willy Tarreau42250582007-04-01 01:30:43 +0200719extern const char sess_term_cond[8];
720extern const char sess_fin_state[8];
721extern const char *monthname[12];
722const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
723const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
724 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
725 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200726struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200727struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100728
Emeric Brun3a058f32009-06-30 18:26:00 +0200729void http_sess_clflog(struct session *s)
730{
731 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
732 struct proxy *fe = s->fe;
733 struct proxy *be = s->be;
734 struct proxy *prx_log;
735 struct http_txn *txn = &s->txn;
736 int tolog, level, err;
737 char *uri, *h;
738 char *svid;
739 struct tm tm;
740 static char tmpline[MAX_SYSLOG_LEN];
741 int hdr;
742 size_t w;
743 int t_request;
744
745 prx_log = fe;
746 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
747 (s->conn_retries != be->conn_retries) ||
748 txn->status >= 500;
749
750 if (s->cli_addr.ss_family == AF_INET)
751 inet_ntop(AF_INET,
752 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
753 pn, sizeof(pn));
754 else
755 inet_ntop(AF_INET6,
756 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
757 pn, sizeof(pn));
758
759 get_gmtime(s->logs.accept_date.tv_sec, &tm);
760
761 /* FIXME: let's limit ourselves to frontend logging for now. */
762 tolog = fe->to_log;
763
764 h = tmpline;
765
766 w = snprintf(h, sizeof(tmpline),
767 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
768 pn,
769 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
770 tm.tm_hour, tm.tm_min, tm.tm_sec);
771 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
772 goto trunc;
773 h += w;
774
775 if (h >= tmpline + sizeof(tmpline) - 4)
776 goto trunc;
777
778 *(h++) = ' ';
779 *(h++) = '\"';
780 uri = txn->uri ? txn->uri : "<BADREQ>";
781 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
782 '#', url_encode_map, uri);
783 *(h++) = '\"';
784
785 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
786 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
787 goto trunc;
788 h += w;
789
790 if (h >= tmpline + sizeof(tmpline) - 9)
791 goto trunc;
792 memcpy(h, " \"-\" \"-\"", 8);
793 h += 8;
794
795 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
796 " %d %03d",
797 (s->cli_addr.ss_family == AF_INET) ?
798 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
799 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
800 (int)s->logs.accept_date.tv_usec/1000);
801 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
802 goto trunc;
803 h += w;
804
805 w = strlen(fe->id);
806 if (h >= tmpline + sizeof(tmpline) - 4 - w)
807 goto trunc;
808 *(h++) = ' ';
809 *(h++) = '\"';
810 memcpy(h, fe->id, w);
811 h += w;
812 *(h++) = '\"';
813
814 w = strlen(be->id);
815 if (h >= tmpline + sizeof(tmpline) - 4 - w)
816 goto trunc;
817 *(h++) = ' ';
818 *(h++) = '\"';
819 memcpy(h, be->id, w);
820 h += w;
821 *(h++) = '\"';
822
823 svid = (tolog & LW_SVID) ?
824 (s->data_source != DATA_SRC_STATS) ?
825 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
826
827 w = strlen(svid);
828 if (h >= tmpline + sizeof(tmpline) - 4 - w)
829 goto trunc;
830 *(h++) = ' ';
831 *(h++) = '\"';
832 memcpy(h, svid, w);
833 h += w;
834 *(h++) = '\"';
835
836 t_request = -1;
837 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
838 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
839 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
840 " %d %ld %ld %ld %ld",
841 t_request,
842 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
843 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
844 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
845 s->logs.t_close);
846 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
847 goto trunc;
848 h += w;
849
850 if (h >= tmpline + sizeof(tmpline) - 8)
851 goto trunc;
852 *(h++) = ' ';
853 *(h++) = '\"';
854 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
855 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
856 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
857 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
858 *(h++) = '\"';
859
860 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
861 " %d %d %d %d %d %ld %ld",
862 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
863 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
864 s->logs.srv_queue_size, s->logs.prx_queue_size);
865
866 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
867 goto trunc;
868 h += w;
869
870 if (txn->cli_cookie) {
871 w = strlen(txn->cli_cookie);
872 if (h >= tmpline + sizeof(tmpline) - 4 - w)
873 goto trunc;
874 *(h++) = ' ';
875 *(h++) = '\"';
876 memcpy(h, txn->cli_cookie, w);
877 h += w;
878 *(h++) = '\"';
879 } else {
880 if (h >= tmpline + sizeof(tmpline) - 5)
881 goto trunc;
882 memcpy(h, " \"-\"", 4);
883 h += 4;
884 }
885
886 if (txn->srv_cookie) {
887 w = strlen(txn->srv_cookie);
888 if (h >= tmpline + sizeof(tmpline) - 4 - w)
889 goto trunc;
890 *(h++) = ' ';
891 *(h++) = '\"';
892 memcpy(h, txn->srv_cookie, w);
893 h += w;
894 *(h++) = '\"';
895 } else {
896 if (h >= tmpline + sizeof(tmpline) - 5)
897 goto trunc;
898 memcpy(h, " \"-\"", 4);
899 h += 4;
900 }
901
902 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
903 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
904 if (h >= sizeof (tmpline) + tmpline - 4)
905 goto trunc;
906 *(h++) = ' ';
907 *(h++) = '\"';
908 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
909 '#', hdr_encode_map, txn->req.cap[hdr]);
910 *(h++) = '\"';
911 }
912 }
913
914 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
915 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
916 if (h >= sizeof (tmpline) + tmpline - 4)
917 goto trunc;
918 *(h++) = ' ';
919 *(h++) = '\"';
920 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
921 '#', hdr_encode_map, txn->rsp.cap[hdr]);
922 *(h++) = '\"';
923 }
924 }
925
926trunc:
927 *h = '\0';
928
929 level = LOG_INFO;
930 if (err && (fe->options2 & PR_O2_LOGERRORS))
931 level = LOG_ERR;
932
933 send_log(prx_log, level, "%s\n", tmpline);
934
935 s->logs.logwait = 0;
936}
937
Willy Tarreau42250582007-04-01 01:30:43 +0200938/*
939 * send a log for the session when we have enough info about it.
940 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100941 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100942void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200943{
944 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
945 struct proxy *fe = s->fe;
946 struct proxy *be = s->be;
947 struct proxy *prx_log;
948 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200949 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200950 char *uri, *h;
951 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200952 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200953 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200954 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200955 int hdr;
956
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200957 /* if we don't want to log normal traffic, return now */
958 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
959 (s->conn_retries != be->conn_retries) ||
960 txn->status >= 500;
961 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
962 return;
963
Willy Tarreau42250582007-04-01 01:30:43 +0200964 if (fe->logfac1 < 0 && fe->logfac2 < 0)
965 return;
966 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100967
Emeric Brun3a058f32009-06-30 18:26:00 +0200968 if (prx_log->options2 & PR_O2_CLFLOG)
969 return http_sess_clflog(s);
970
Willy Tarreau42250582007-04-01 01:30:43 +0200971 if (s->cli_addr.ss_family == AF_INET)
972 inet_ntop(AF_INET,
973 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
974 pn, sizeof(pn));
975 else
976 inet_ntop(AF_INET6,
977 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
978 pn, sizeof(pn));
979
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200980 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200981
982 /* FIXME: let's limit ourselves to frontend logging for now. */
983 tolog = fe->to_log;
984
985 h = tmpline;
986 if (fe->to_log & LW_REQHDR &&
987 txn->req.cap &&
988 (h < tmpline + sizeof(tmpline) - 10)) {
989 *(h++) = ' ';
990 *(h++) = '{';
991 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
992 if (hdr)
993 *(h++) = '|';
994 if (txn->req.cap[hdr] != NULL)
995 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
996 '#', hdr_encode_map, txn->req.cap[hdr]);
997 }
998 *(h++) = '}';
999 }
1000
1001 if (fe->to_log & LW_RSPHDR &&
1002 txn->rsp.cap &&
1003 (h < tmpline + sizeof(tmpline) - 7)) {
1004 *(h++) = ' ';
1005 *(h++) = '{';
1006 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1007 if (hdr)
1008 *(h++) = '|';
1009 if (txn->rsp.cap[hdr] != NULL)
1010 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1011 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1012 }
1013 *(h++) = '}';
1014 }
1015
1016 if (h < tmpline + sizeof(tmpline) - 4) {
1017 *(h++) = ' ';
1018 *(h++) = '"';
1019 uri = txn->uri ? txn->uri : "<BADREQ>";
1020 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1021 '#', url_encode_map, uri);
1022 *(h++) = '"';
1023 }
1024 *h = '\0';
1025
1026 svid = (tolog & LW_SVID) ?
1027 (s->data_source != DATA_SRC_STATS) ?
1028 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1029
Willy Tarreau70089872008-06-13 21:12:51 +02001030 t_request = -1;
1031 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1032 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1033
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001034 level = LOG_INFO;
1035 if (err && (fe->options2 & PR_O2_LOGERRORS))
1036 level = LOG_ERR;
1037
1038 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001039 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001040 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1041 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001042 pn,
1043 (s->cli_addr.ss_family == AF_INET) ?
1044 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1045 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001046 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001047 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001048 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001049 t_request,
1050 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001051 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1052 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1053 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1054 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001055 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001056 txn->cli_cookie ? txn->cli_cookie : "-",
1057 txn->srv_cookie ? txn->srv_cookie : "-",
1058 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1059 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1060 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1061 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1062 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001063 (s->flags & SN_REDISP)?"+":"",
1064 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001065 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1066
1067 s->logs.logwait = 0;
1068}
1069
Willy Tarreau117f59e2007-03-04 18:17:17 +01001070
1071/*
1072 * Capture headers from message starting at <som> according to header list
1073 * <cap_hdr>, and fill the <idx> structure appropriately.
1074 */
1075void capture_headers(char *som, struct hdr_idx *idx,
1076 char **cap, struct cap_hdr *cap_hdr)
1077{
1078 char *eol, *sol, *col, *sov;
1079 int cur_idx;
1080 struct cap_hdr *h;
1081 int len;
1082
1083 sol = som + hdr_idx_first_pos(idx);
1084 cur_idx = hdr_idx_first_idx(idx);
1085
1086 while (cur_idx) {
1087 eol = sol + idx->v[cur_idx].len;
1088
1089 col = sol;
1090 while (col < eol && *col != ':')
1091 col++;
1092
1093 sov = col + 1;
1094 while (sov < eol && http_is_lws[(unsigned char)*sov])
1095 sov++;
1096
1097 for (h = cap_hdr; h; h = h->next) {
1098 if ((h->namelen == col - sol) &&
1099 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1100 if (cap[h->index] == NULL)
1101 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001102 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001103
1104 if (cap[h->index] == NULL) {
1105 Alert("HTTP capture : out of memory.\n");
1106 continue;
1107 }
1108
1109 len = eol - sov;
1110 if (len > h->len)
1111 len = h->len;
1112
1113 memcpy(cap[h->index], sov, len);
1114 cap[h->index][len]=0;
1115 }
1116 }
1117 sol = eol + idx->v[cur_idx].cr + 1;
1118 cur_idx = idx->v[cur_idx].next;
1119 }
1120}
1121
1122
Willy Tarreau42250582007-04-01 01:30:43 +02001123/* either we find an LF at <ptr> or we jump to <bad>.
1124 */
1125#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1126
1127/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1128 * otherwise to <http_msg_ood> with <state> set to <st>.
1129 */
1130#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1131 ptr++; \
1132 if (likely(ptr < end)) \
1133 goto good; \
1134 else { \
1135 state = (st); \
1136 goto http_msg_ood; \
1137 } \
1138 } while (0)
1139
1140
Willy Tarreaubaaee002006-06-26 02:48:02 +02001141/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001142 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001143 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1144 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1145 * will give undefined results.
1146 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1147 * and that msg->sol points to the beginning of the response.
1148 * If a complete line is found (which implies that at least one CR or LF is
1149 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1150 * returned indicating an incomplete line (which does not mean that parts have
1151 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1152 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1153 * upon next call.
1154 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001155 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001156 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1157 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001158 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001159 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001160const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1161 unsigned int state, const char *ptr, const char *end,
1162 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001163{
Willy Tarreau8973c702007-01-21 23:58:29 +01001164 switch (state) {
1165 http_msg_rpver:
1166 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001167 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1169
1170 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001171 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001172 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1173 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001174 state = HTTP_MSG_ERROR;
1175 break;
1176
Willy Tarreau8973c702007-01-21 23:58:29 +01001177 http_msg_rpver_sp:
1178 case HTTP_MSG_RPVER_SP:
1179 if (likely(!HTTP_IS_LWS(*ptr))) {
1180 msg->sl.st.c = ptr - msg_buf;
1181 goto http_msg_rpcode;
1182 }
1183 if (likely(HTTP_IS_SPHT(*ptr)))
1184 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1185 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001186 state = HTTP_MSG_ERROR;
1187 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001188
1189 http_msg_rpcode:
1190 case HTTP_MSG_RPCODE:
1191 if (likely(!HTTP_IS_LWS(*ptr)))
1192 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1193
1194 if (likely(HTTP_IS_SPHT(*ptr))) {
1195 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1196 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1197 }
1198
1199 /* so it's a CR/LF, so there is no reason phrase */
1200 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1201 http_msg_rsp_reason:
1202 /* FIXME: should we support HTTP responses without any reason phrase ? */
1203 msg->sl.st.r = ptr - msg_buf;
1204 msg->sl.st.r_l = 0;
1205 goto http_msg_rpline_eol;
1206
1207 http_msg_rpcode_sp:
1208 case HTTP_MSG_RPCODE_SP:
1209 if (likely(!HTTP_IS_LWS(*ptr))) {
1210 msg->sl.st.r = ptr - msg_buf;
1211 goto http_msg_rpreason;
1212 }
1213 if (likely(HTTP_IS_SPHT(*ptr)))
1214 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1215 /* so it's a CR/LF, so there is no reason phrase */
1216 goto http_msg_rsp_reason;
1217
1218 http_msg_rpreason:
1219 case HTTP_MSG_RPREASON:
1220 if (likely(!HTTP_IS_CRLF(*ptr)))
1221 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1222 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1223 http_msg_rpline_eol:
1224 /* We have seen the end of line. Note that we do not
1225 * necessarily have the \n yet, but at least we know that we
1226 * have EITHER \r OR \n, otherwise the response would not be
1227 * complete. We can then record the response length and return
1228 * to the caller which will be able to register it.
1229 */
1230 msg->sl.st.l = ptr - msg->sol;
1231 return ptr;
1232
1233#ifdef DEBUG_FULL
1234 default:
1235 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1236 exit(1);
1237#endif
1238 }
1239
1240 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001241 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001242 if (ret_state)
1243 *ret_state = state;
1244 if (ret_ptr)
1245 *ret_ptr = (char *)ptr;
1246 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001247}
1248
1249
1250/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001251 * This function parses a request line between <ptr> and <end>, starting with
1252 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1253 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1254 * will give undefined results.
1255 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1256 * and that msg->sol points to the beginning of the request.
1257 * If a complete line is found (which implies that at least one CR or LF is
1258 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1259 * returned indicating an incomplete line (which does not mean that parts have
1260 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1261 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1262 * upon next call.
1263 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001264 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1266 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001267 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001268 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001269const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1270 unsigned int state, const char *ptr, const char *end,
1271 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001272{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 switch (state) {
1274 http_msg_rqmeth:
1275 case HTTP_MSG_RQMETH:
1276 if (likely(HTTP_IS_TOKEN(*ptr)))
1277 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001278
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001279 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001280 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1282 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 if (likely(HTTP_IS_CRLF(*ptr))) {
1285 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001286 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 http_msg_req09_uri:
1288 msg->sl.rq.u = ptr - msg_buf;
1289 http_msg_req09_uri_e:
1290 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1291 http_msg_req09_ver:
1292 msg->sl.rq.v = ptr - msg_buf;
1293 msg->sl.rq.v_l = 0;
1294 goto http_msg_rqline_eol;
1295 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001296 state = HTTP_MSG_ERROR;
1297 break;
1298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001299 http_msg_rqmeth_sp:
1300 case HTTP_MSG_RQMETH_SP:
1301 if (likely(!HTTP_IS_LWS(*ptr))) {
1302 msg->sl.rq.u = ptr - msg_buf;
1303 goto http_msg_rquri;
1304 }
1305 if (likely(HTTP_IS_SPHT(*ptr)))
1306 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1307 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1308 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 http_msg_rquri:
1311 case HTTP_MSG_RQURI:
1312 if (likely(!HTTP_IS_LWS(*ptr)))
1313 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001314
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001315 if (likely(HTTP_IS_SPHT(*ptr))) {
1316 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1317 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1318 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001319
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1321 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001322
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001323 http_msg_rquri_sp:
1324 case HTTP_MSG_RQURI_SP:
1325 if (likely(!HTTP_IS_LWS(*ptr))) {
1326 msg->sl.rq.v = ptr - msg_buf;
1327 goto http_msg_rqver;
1328 }
1329 if (likely(HTTP_IS_SPHT(*ptr)))
1330 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1331 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1332 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001333
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001334 http_msg_rqver:
1335 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001336 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001338
1339 if (likely(HTTP_IS_CRLF(*ptr))) {
1340 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1341 http_msg_rqline_eol:
1342 /* We have seen the end of line. Note that we do not
1343 * necessarily have the \n yet, but at least we know that we
1344 * have EITHER \r OR \n, otherwise the request would not be
1345 * complete. We can then record the request length and return
1346 * to the caller which will be able to register it.
1347 */
1348 msg->sl.rq.l = ptr - msg->sol;
1349 return ptr;
1350 }
1351
1352 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001353 state = HTTP_MSG_ERROR;
1354 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001355
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001356#ifdef DEBUG_FULL
1357 default:
1358 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1359 exit(1);
1360#endif
1361 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001362
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001363 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001364 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 if (ret_state)
1366 *ret_state = state;
1367 if (ret_ptr)
1368 *ret_ptr = (char *)ptr;
1369 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001371
1372
Willy Tarreau8973c702007-01-21 23:58:29 +01001373/*
1374 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001375 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001376 * when data are missing and recalled at the exact same location with no
1377 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001378 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1379 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001380 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1382{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001383 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001385
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001386 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 ptr = buf->lr;
1388 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 if (unlikely(ptr >= end))
1391 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001394 /*
1395 * First, states that are specific to the response only.
1396 * We check them first so that request and headers are
1397 * closer to each other (accessed more often).
1398 */
1399 http_msg_rpbefore:
1400 case HTTP_MSG_RPBEFORE:
1401 if (likely(HTTP_IS_TOKEN(*ptr))) {
1402 if (likely(ptr == buf->data)) {
1403 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001404 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001405 } else {
1406#if PARSE_PRESERVE_EMPTY_LINES
1407 /* only skip empty leading lines, don't remove them */
1408 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001409 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001410#else
1411 /* Remove empty leading lines, as recommended by
1412 * RFC2616. This takes a lot of time because we
1413 * must move all the buffer backwards, but this
1414 * is rarely needed. The method above will be
1415 * cleaner when we'll be able to start sending
1416 * the request from any place in the buffer.
1417 */
1418 buf->lr = ptr;
1419 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001420 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001421 msg->sol = buf->data;
1422 ptr = buf->data;
1423 end = buf->r;
1424#endif
1425 }
1426 hdr_idx_init(idx);
1427 state = HTTP_MSG_RPVER;
1428 goto http_msg_rpver;
1429 }
1430
1431 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1432 goto http_msg_invalid;
1433
1434 if (unlikely(*ptr == '\n'))
1435 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1436 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1437 /* stop here */
1438
1439 http_msg_rpbefore_cr:
1440 case HTTP_MSG_RPBEFORE_CR:
1441 EXPECT_LF_HERE(ptr, http_msg_invalid);
1442 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1443 /* stop here */
1444
1445 http_msg_rpver:
1446 case HTTP_MSG_RPVER:
1447 case HTTP_MSG_RPVER_SP:
1448 case HTTP_MSG_RPCODE:
1449 case HTTP_MSG_RPCODE_SP:
1450 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001451 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001452 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001453 if (unlikely(!ptr))
1454 return;
1455
1456 /* we have a full response and we know that we have either a CR
1457 * or an LF at <ptr>.
1458 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001459 //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 +01001460 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1461
1462 msg->sol = ptr;
1463 if (likely(*ptr == '\r'))
1464 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1465 goto http_msg_rpline_end;
1466
1467 http_msg_rpline_end:
1468 case HTTP_MSG_RPLINE_END:
1469 /* msg->sol must point to the first of CR or LF. */
1470 EXPECT_LF_HERE(ptr, http_msg_invalid);
1471 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1472 /* stop here */
1473
1474 /*
1475 * Second, states that are specific to the request only
1476 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 http_msg_rqbefore:
1478 case HTTP_MSG_RQBEFORE:
1479 if (likely(HTTP_IS_TOKEN(*ptr))) {
1480 if (likely(ptr == buf->data)) {
1481 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001482 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 } else {
1484#if PARSE_PRESERVE_EMPTY_LINES
1485 /* only skip empty leading lines, don't remove them */
1486 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001487 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001488#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 /* Remove empty leading lines, as recommended by
1490 * RFC2616. This takes a lot of time because we
1491 * must move all the buffer backwards, but this
1492 * is rarely needed. The method above will be
1493 * cleaner when we'll be able to start sending
1494 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001495 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 buf->lr = ptr;
1497 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001498 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 msg->sol = buf->data;
1500 ptr = buf->data;
1501 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001502#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001504 /* we will need this when keep-alive will be supported
1505 hdr_idx_init(idx);
1506 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001507 state = HTTP_MSG_RQMETH;
1508 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001510
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1512 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 if (unlikely(*ptr == '\n'))
1515 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1516 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001517 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001518
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 http_msg_rqbefore_cr:
1520 case HTTP_MSG_RQBEFORE_CR:
1521 EXPECT_LF_HERE(ptr, http_msg_invalid);
1522 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001523 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 http_msg_rqmeth:
1526 case HTTP_MSG_RQMETH:
1527 case HTTP_MSG_RQMETH_SP:
1528 case HTTP_MSG_RQURI:
1529 case HTTP_MSG_RQURI_SP:
1530 case HTTP_MSG_RQVER:
1531 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001532 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 if (unlikely(!ptr))
1534 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001535
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 /* we have a full request and we know that we have either a CR
1537 * or an LF at <ptr>.
1538 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001539 //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 +01001540 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001541
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 msg->sol = ptr;
1543 if (likely(*ptr == '\r'))
1544 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001545 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001546
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 http_msg_rqline_end:
1548 case HTTP_MSG_RQLINE_END:
1549 /* check for HTTP/0.9 request : no version information available.
1550 * msg->sol must point to the first of CR or LF.
1551 */
1552 if (unlikely(msg->sl.rq.v_l == 0))
1553 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001554
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 EXPECT_LF_HERE(ptr, http_msg_invalid);
1556 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001557 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001558
Willy Tarreau8973c702007-01-21 23:58:29 +01001559 /*
1560 * Common states below
1561 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 http_msg_hdr_first:
1563 case HTTP_MSG_HDR_FIRST:
1564 msg->sol = ptr;
1565 if (likely(!HTTP_IS_CRLF(*ptr))) {
1566 goto http_msg_hdr_name;
1567 }
1568
1569 if (likely(*ptr == '\r'))
1570 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1571 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001572
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001573 http_msg_hdr_name:
1574 case HTTP_MSG_HDR_NAME:
1575 /* assumes msg->sol points to the first char */
1576 if (likely(HTTP_IS_TOKEN(*ptr)))
1577 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001578
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 if (likely(*ptr == ':')) {
1580 msg->col = ptr - buf->data;
1581 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1582 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001583
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001584 if (likely(msg->err_pos < -1) || *ptr == '\n')
1585 goto http_msg_invalid;
1586
1587 if (msg->err_pos == -1) /* capture error pointer */
1588 msg->err_pos = ptr - buf->data; /* >= 0 now */
1589
1590 /* and we still accept this non-token character */
1591 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001592
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 http_msg_hdr_l1_sp:
1594 case HTTP_MSG_HDR_L1_SP:
1595 /* assumes msg->sol points to the first char and msg->col to the colon */
1596 if (likely(HTTP_IS_SPHT(*ptr)))
1597 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001598
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 /* header value can be basically anything except CR/LF */
1600 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001601
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602 if (likely(!HTTP_IS_CRLF(*ptr))) {
1603 goto http_msg_hdr_val;
1604 }
1605
1606 if (likely(*ptr == '\r'))
1607 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1608 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001609
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 http_msg_hdr_l1_lf:
1611 case HTTP_MSG_HDR_L1_LF:
1612 EXPECT_LF_HERE(ptr, http_msg_invalid);
1613 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001614
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 http_msg_hdr_l1_lws:
1616 case HTTP_MSG_HDR_L1_LWS:
1617 if (likely(HTTP_IS_SPHT(*ptr))) {
1618 /* replace HT,CR,LF with spaces */
1619 for (; buf->data+msg->sov < ptr; msg->sov++)
1620 buf->data[msg->sov] = ' ';
1621 goto http_msg_hdr_l1_sp;
1622 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001623 /* we had a header consisting only in spaces ! */
1624 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 goto http_msg_complete_header;
1626
1627 http_msg_hdr_val:
1628 case HTTP_MSG_HDR_VAL:
1629 /* assumes msg->sol points to the first char, msg->col to the
1630 * colon, and msg->sov points to the first character of the
1631 * value.
1632 */
1633 if (likely(!HTTP_IS_CRLF(*ptr)))
1634 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 msg->eol = ptr;
1637 /* Note: we could also copy eol into ->eoh so that we have the
1638 * real header end in case it ends with lots of LWS, but is this
1639 * really needed ?
1640 */
1641 if (likely(*ptr == '\r'))
1642 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1643 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 http_msg_hdr_l2_lf:
1646 case HTTP_MSG_HDR_L2_LF:
1647 EXPECT_LF_HERE(ptr, http_msg_invalid);
1648 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001649
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001650 http_msg_hdr_l2_lws:
1651 case HTTP_MSG_HDR_L2_LWS:
1652 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1653 /* LWS: replace HT,CR,LF with spaces */
1654 for (; msg->eol < ptr; msg->eol++)
1655 *msg->eol = ' ';
1656 goto http_msg_hdr_val;
1657 }
1658 http_msg_complete_header:
1659 /*
1660 * It was a new header, so the last one is finished.
1661 * Assumes msg->sol points to the first char, msg->col to the
1662 * colon, msg->sov points to the first character of the value
1663 * and msg->eol to the first CR or LF so we know how the line
1664 * ends. We insert last header into the index.
1665 */
1666 /*
1667 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1668 write(2, msg->sol, msg->eol-msg->sol);
1669 fprintf(stderr,"\n");
1670 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001671
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001672 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1673 idx, idx->tail) < 0))
1674 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001675
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 msg->sol = ptr;
1677 if (likely(!HTTP_IS_CRLF(*ptr))) {
1678 goto http_msg_hdr_name;
1679 }
1680
1681 if (likely(*ptr == '\r'))
1682 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1683 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 http_msg_last_lf:
1686 case HTTP_MSG_LAST_LF:
1687 /* Assumes msg->sol points to the first of either CR or LF */
1688 EXPECT_LF_HERE(ptr, http_msg_invalid);
1689 ptr++;
1690 buf->lr = ptr;
1691 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001692 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 return;
1694#ifdef DEBUG_FULL
1695 default:
1696 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1697 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001698#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001699 }
1700 http_msg_ood:
1701 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001702 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 buf->lr = ptr;
1704 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001705
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 http_msg_invalid:
1707 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001708 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001709 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 return;
1711}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001712
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001713/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1714 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1715 * nothing is done and 1 is returned.
1716 */
1717static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1718{
1719 int delta;
1720 char *cur_end;
1721
1722 if (msg->sl.rq.v_l != 0)
1723 return 1;
1724
1725 msg->sol = req->data + msg->som;
1726 cur_end = msg->sol + msg->sl.rq.l;
1727 delta = 0;
1728
1729 if (msg->sl.rq.u_l == 0) {
1730 /* if no URI was set, add "/" */
1731 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1732 cur_end += delta;
1733 msg->eoh += delta;
1734 }
1735 /* add HTTP version */
1736 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1737 msg->eoh += delta;
1738 cur_end += delta;
1739 cur_end = (char *)http_parse_reqline(msg, req->data,
1740 HTTP_MSG_RQMETH,
1741 msg->sol, cur_end + 1,
1742 NULL, NULL);
1743 if (unlikely(!cur_end))
1744 return 0;
1745
1746 /* we have a full HTTP/1.0 request now and we know that
1747 * we have either a CR or an LF at <ptr>.
1748 */
1749 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1750 return 1;
1751}
1752
Willy Tarreaud787e662009-07-07 10:14:51 +02001753/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1754 * processing can continue on next analysers, or zero if it either needs more
1755 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1756 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1757 * when it has nothing left to do, and may remove any analyser when it wants to
1758 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001760int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001761{
Willy Tarreau59234e92008-11-30 23:51:27 +01001762 /*
1763 * We will parse the partial (or complete) lines.
1764 * We will check the request syntax, and also join multi-line
1765 * headers. An index of all the lines will be elaborated while
1766 * parsing.
1767 *
1768 * For the parsing, we use a 28 states FSM.
1769 *
1770 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01001771 * req->data + msg->som = beginning of request
Willy Tarreau59234e92008-11-30 23:51:27 +01001772 * req->data + req->eoh = end of processed headers / start of current one
1773 * req->data + req->eol = end of current header or line (LF or CRLF)
1774 * req->lr = first non-visited byte
1775 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02001776 *
1777 * At end of parsing, we may perform a capture of the error (if any), and
1778 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
1779 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1780 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001781 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001782
Willy Tarreau59234e92008-11-30 23:51:27 +01001783 int cur_idx;
1784 struct http_txn *txn = &s->txn;
1785 struct http_msg *msg = &txn->req;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001786
Willy Tarreau6bf17362009-02-24 10:48:35 +01001787 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1788 now_ms, __FUNCTION__,
1789 s,
1790 req,
1791 req->rex, req->wex,
1792 req->flags,
1793 req->l,
1794 req->analysers);
1795
Willy Tarreau59234e92008-11-30 23:51:27 +01001796 if (likely(req->lr < req->r))
1797 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001798
Willy Tarreau59234e92008-11-30 23:51:27 +01001799 /* 1: we might have to print this header in debug mode */
1800 if (unlikely((global.mode & MODE_DEBUG) &&
1801 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
1802 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
1803 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001804
Willy Tarreau59234e92008-11-30 23:51:27 +01001805 sol = req->data + msg->som;
1806 eol = sol + msg->sl.rq.l;
1807 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001808
Willy Tarreau59234e92008-11-30 23:51:27 +01001809 sol += hdr_idx_first_pos(&txn->hdr_idx);
1810 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001811
Willy Tarreau59234e92008-11-30 23:51:27 +01001812 while (cur_idx) {
1813 eol = sol + txn->hdr_idx.v[cur_idx].len;
1814 debug_hdr("clihdr", s, sol, eol);
1815 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1816 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001818 }
1819
Willy Tarreau58f10d72006-12-04 02:26:12 +01001820
Willy Tarreau59234e92008-11-30 23:51:27 +01001821 /*
1822 * Now we quickly check if we have found a full valid request.
1823 * If not so, we check the FD and buffer states before leaving.
1824 * A full request is indicated by the fact that we have seen
1825 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1826 * requests are checked first.
1827 *
1828 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001829
Willy Tarreau59234e92008-11-30 23:51:27 +01001830 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001831 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001832 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001833 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001834 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
1835 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001836
Willy Tarreau59234e92008-11-30 23:51:27 +01001837 /* 1: Since we are in header mode, if there's no space
1838 * left for headers, we won't be able to free more
1839 * later, so the session will never terminate. We
1840 * must terminate it now.
1841 */
1842 if (unlikely(req->flags & BF_FULL)) {
1843 /* FIXME: check if URI is set and return Status
1844 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001845 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001846 goto return_bad_req;
1847 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001848
Willy Tarreau59234e92008-11-30 23:51:27 +01001849 /* 2: have we encountered a read error ? */
1850 else if (req->flags & BF_READ_ERROR) {
1851 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02001852 if (msg->err_pos >= 0)
1853 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001854 msg->msg_state = HTTP_MSG_ERROR;
1855 req->analysers = 0;
1856 s->fe->failed_req++;
1857 if (!(s->flags & SN_ERR_MASK))
1858 s->flags |= SN_ERR_CLICL;
1859 if (!(s->flags & SN_FINST_MASK))
1860 s->flags |= SN_FINST_R;
1861 return 0;
1862 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001863
Willy Tarreau59234e92008-11-30 23:51:27 +01001864 /* 3: has the read timeout expired ? */
1865 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
1866 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02001867 if (msg->err_pos >= 0)
1868 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001869 txn->status = 408;
1870 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
1871 msg->msg_state = HTTP_MSG_ERROR;
1872 req->analysers = 0;
1873 s->fe->failed_req++;
1874 if (!(s->flags & SN_ERR_MASK))
1875 s->flags |= SN_ERR_CLITO;
1876 if (!(s->flags & SN_FINST_MASK))
1877 s->flags |= SN_FINST_R;
1878 return 0;
1879 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001880
Willy Tarreau59234e92008-11-30 23:51:27 +01001881 /* 4: have we encountered a close ? */
1882 else if (req->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02001883 if (msg->err_pos >= 0)
1884 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001885 txn->status = 400;
1886 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1887 msg->msg_state = HTTP_MSG_ERROR;
1888 req->analysers = 0;
1889 s->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001890
Willy Tarreau59234e92008-11-30 23:51:27 +01001891 if (!(s->flags & SN_ERR_MASK))
1892 s->flags |= SN_ERR_CLICL;
1893 if (!(s->flags & SN_FINST_MASK))
1894 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001895 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001896 }
1897
Willy Tarreau59234e92008-11-30 23:51:27 +01001898 buffer_write_dis(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001899 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
1900
Willy Tarreau59234e92008-11-30 23:51:27 +01001901 /* just set the request timeout once at the beginning of the request */
1902 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02001903 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001904
Willy Tarreau59234e92008-11-30 23:51:27 +01001905 /* we're not ready yet */
1906 return 0;
1907 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001908
Willy Tarreaud787e662009-07-07 10:14:51 +02001909 /* OK now we have a complete HTTP request with indexed headers. Let's
1910 * complete the request parsing by setting a few fields we will need
1911 * later.
1912 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001913
Willy Tarreaud787e662009-07-07 10:14:51 +02001914 /* Maybe we found in invalid header name while we were configured not
1915 * to block on that, so we have to capture it now.
1916 */
1917 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02001918 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
1919
Willy Tarreau59234e92008-11-30 23:51:27 +01001920 /* ensure we keep this pointer to the beginning of the message */
1921 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001922
Willy Tarreau59234e92008-11-30 23:51:27 +01001923 /*
1924 * 1: identify the method
1925 */
1926 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
1927
1928 /* we can make use of server redirect on GET and HEAD */
1929 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1930 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001931
Willy Tarreau59234e92008-11-30 23:51:27 +01001932 /*
1933 * 2: check if the URI matches the monitor_uri.
1934 * We have to do this for every request which gets in, because
1935 * the monitor-uri is defined by the frontend.
1936 */
1937 if (unlikely((s->fe->monitor_uri_len != 0) &&
1938 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1939 !memcmp(&req->data[msg->sl.rq.u],
1940 s->fe->monitor_uri,
1941 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001942 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001943 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001944 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001945 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001946
Willy Tarreau59234e92008-11-30 23:51:27 +01001947 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001948
Willy Tarreau59234e92008-11-30 23:51:27 +01001949 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02001950 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
1951 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001952
Willy Tarreau59234e92008-11-30 23:51:27 +01001953 ret = acl_pass(ret);
1954 if (cond->pol == ACL_COND_UNLESS)
1955 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001956
Willy Tarreau59234e92008-11-30 23:51:27 +01001957 if (ret) {
1958 /* we fail this request, let's return 503 service unavail */
1959 txn->status = 503;
1960 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
1961 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001962 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001963 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001964
Willy Tarreau59234e92008-11-30 23:51:27 +01001965 /* nothing to fail, let's reply normaly */
1966 txn->status = 200;
1967 stream_int_retnclose(req->prod, &http_200_chunk);
1968 goto return_prx_cond;
1969 }
1970
1971 /*
1972 * 3: Maybe we have to copy the original REQURI for the logs ?
1973 * Note: we cannot log anymore if the request has been
1974 * classified as invalid.
1975 */
1976 if (unlikely(s->logs.logwait & LW_REQ)) {
1977 /* we have a complete HTTP request that we must log */
1978 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1979 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001980
Willy Tarreau59234e92008-11-30 23:51:27 +01001981 if (urilen >= REQURI_LEN)
1982 urilen = REQURI_LEN - 1;
1983 memcpy(txn->uri, &req->data[msg->som], urilen);
1984 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001985
Willy Tarreau59234e92008-11-30 23:51:27 +01001986 if (!(s->logs.logwait &= ~LW_REQ))
1987 s->do_log(s);
1988 } else {
1989 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001990 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001991 }
Willy Tarreau06619262006-12-17 08:37:22 +01001992
Willy Tarreau59234e92008-11-30 23:51:27 +01001993 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001994 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
1995 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001996
Willy Tarreau59234e92008-11-30 23:51:27 +01001997 /* 5: we may need to capture headers */
1998 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
1999 capture_headers(req->data + msg->som, &txn->hdr_idx,
2000 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002001
Willy Tarreaud787e662009-07-07 10:14:51 +02002002 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002003 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002004 req->analyse_exp = TICK_ETERNITY;
2005 return 1;
2006
2007 return_bad_req:
2008 /* We centralize bad requests processing here */
2009 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2010 /* we detected a parsing error. We want to archive this request
2011 * in the dedicated proxy area for later troubleshooting.
2012 */
2013 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2014 }
2015
2016 txn->req.msg_state = HTTP_MSG_ERROR;
2017 txn->status = 400;
2018 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2019 s->fe->failed_req++;
2020
2021 return_prx_cond:
2022 if (!(s->flags & SN_ERR_MASK))
2023 s->flags |= SN_ERR_PRXCOND;
2024 if (!(s->flags & SN_FINST_MASK))
2025 s->flags |= SN_FINST_R;
2026
2027 req->analysers = 0;
2028 req->analyse_exp = TICK_ETERNITY;
2029 return 0;
2030}
2031
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002032/* This stream analyser runs all HTTP request processing which is common to
2033 * frontends and backends, which means blocking ACLs, filters, connection-close,
2034 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002035 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002036 * either needs more data or wants to immediately abort the request (eg: deny,
2037 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002038 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002039int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002040{
Willy Tarreaud787e662009-07-07 10:14:51 +02002041 struct http_txn *txn = &s->txn;
2042 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002043 struct acl_cond *cond;
2044 struct redirect_rule *rule;
2045 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002046
Willy Tarreau51aecc72009-07-12 09:47:04 +02002047 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2048 /* we need more data */
2049 buffer_write_dis(req);
2050 return 0;
2051 }
2052
Willy Tarreau3a816292009-07-07 10:55:49 +02002053 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002054 req->analyse_exp = TICK_ETERNITY;
2055
2056 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2057 now_ms, __FUNCTION__,
2058 s,
2059 req,
2060 req->rex, req->wex,
2061 req->flags,
2062 req->l,
2063 req->analysers);
2064
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002065 /* first check whether we have some ACLs set to block this request */
2066 list_for_each_entry(cond, &px->block_cond, list) {
2067 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002068
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002069 ret = acl_pass(ret);
2070 if (cond->pol == ACL_COND_UNLESS)
2071 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002072
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002073 if (ret) {
2074 txn->status = 403;
2075 /* let's log the request time */
2076 s->logs.tv_request = now;
2077 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2078 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002079 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002080 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002081
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002082 /* try headers filters */
2083 if (px->req_exp != NULL) {
2084 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2085 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002086
Willy Tarreau59234e92008-11-30 23:51:27 +01002087 /* has the request been denied ? */
2088 if (txn->flags & TX_CLDENY) {
2089 /* no need to go further */
2090 txn->status = 403;
2091 /* let's log the request time */
2092 s->logs.tv_request = now;
2093 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2094 goto return_prx_cond;
2095 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002096 }
Willy Tarreau06619262006-12-17 08:37:22 +01002097
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002098 /* We might have to check for "Connection:" */
2099 if (((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2100 !(s->flags & SN_CONN_CLOSED)) {
2101 char *cur_ptr, *cur_end, *cur_next;
2102 int old_idx, delta, val;
2103 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002104
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002105 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
2106 old_idx = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002107
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002108 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2109 cur_hdr = &txn->hdr_idx.v[cur_idx];
2110 cur_ptr = cur_next;
2111 cur_end = cur_ptr + cur_hdr->len;
2112 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002113
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002114 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2115 if (val) {
2116 /* 3 possibilities :
2117 * - we have already set Connection: close,
2118 * so we remove this line.
2119 * - we have not yet set Connection: close,
2120 * but this line indicates close. We leave
2121 * it untouched and set the flag.
2122 * - we have not yet set Connection: close,
2123 * and this line indicates non-close. We
2124 * replace it.
2125 */
2126 if (s->flags & SN_CONN_CLOSED) {
2127 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2128 txn->req.eoh += delta;
2129 cur_next += delta;
2130 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2131 txn->hdr_idx.used--;
2132 cur_hdr->len = 0;
2133 } else {
2134 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2135 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2136 "close", 5);
Willy Tarreau59234e92008-11-30 23:51:27 +01002137 cur_next += delta;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002138 cur_hdr->len += delta;
2139 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002140 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002141 s->flags |= SN_CONN_CLOSED;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002142 }
Willy Tarreau06619262006-12-17 08:37:22 +01002143 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002144 old_idx = cur_idx;
Willy Tarreau59234e92008-11-30 23:51:27 +01002145 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002146 }
2147 /* add request headers from the rule sets in the same order */
2148 for (cur_idx = 0; cur_idx < px->nb_reqadd; cur_idx++) {
2149 if (unlikely(http_header_add_tail(req,
2150 &txn->req,
2151 &txn->hdr_idx,
2152 px->req_add[cur_idx])) < 0)
2153 goto return_bad_req;
2154 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002155
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002156 /* check if stats URI was requested, and if an auth is needed */
2157 if (px->uri_auth != NULL &&
2158 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2159 /* we have to check the URI and auth for this request.
2160 * FIXME!!! that one is rather dangerous, we want to
2161 * make it follow standard rules (eg: clear req->analysers).
2162 */
2163 if (stats_check_uri_auth(s, px)) {
2164 req->analysers = 0;
2165 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002166 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002167 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002168
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002169 /* check whether we have some ACLs set to redirect this request */
2170 list_for_each_entry(rule, &px->redirect_rules, list) {
2171 int ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreau06b917c2009-07-06 16:34:52 +02002172
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002173 ret = acl_pass(ret);
2174 if (rule->cond->pol == ACL_COND_UNLESS)
2175 ret = !ret;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002176
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002177 if (ret) {
2178 struct chunk rdr = { trash, 0 };
2179 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002180
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002181 /* build redirect message */
2182 switch(rule->code) {
2183 case 303:
2184 rdr.len = strlen(HTTP_303);
2185 msg_fmt = HTTP_303;
2186 break;
2187 case 301:
2188 rdr.len = strlen(HTTP_301);
2189 msg_fmt = HTTP_301;
2190 break;
2191 case 302:
2192 default:
2193 rdr.len = strlen(HTTP_302);
2194 msg_fmt = HTTP_302;
2195 break;
2196 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002197
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002198 if (unlikely(rdr.len > sizeof(trash)))
2199 goto return_bad_req;
2200 memcpy(rdr.str, msg_fmt, rdr.len);
Willy Tarreau06b917c2009-07-06 16:34:52 +02002201
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002202 switch(rule->type) {
2203 case REDIRECT_TYPE_PREFIX: {
2204 const char *path;
2205 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002206
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002207 path = http_get_path(txn);
2208 /* build message using path */
2209 if (path) {
2210 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2211 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2212 int qs = 0;
2213 while (qs < pathlen) {
2214 if (path[qs] == '?') {
2215 pathlen = qs;
2216 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002217 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002218 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002219 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002220 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002221 } else {
2222 path = "/";
2223 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002224 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002225
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002226 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2227 goto return_bad_req;
2228
2229 /* add prefix. Note that if prefix == "/", we don't want to
2230 * add anything, otherwise it makes it hard for the user to
2231 * configure a self-redirection.
2232 */
2233 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002234 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2235 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002236 }
2237
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002238 /* add path */
2239 memcpy(rdr.str + rdr.len, path, pathlen);
2240 rdr.len += pathlen;
2241 break;
2242 }
2243 case REDIRECT_TYPE_LOCATION:
2244 default:
2245 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2246 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002247
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002248 /* add location */
2249 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2250 rdr.len += rule->rdr_len;
2251 break;
2252 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002253
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002254 if (rule->cookie_len) {
2255 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2256 rdr.len += 14;
2257 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2258 rdr.len += rule->cookie_len;
2259 memcpy(rdr.str + rdr.len, "\r\n", 2);
2260 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002261 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002262
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002263 /* add end of headers */
2264 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2265 rdr.len += 4;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002266
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002267 txn->status = rule->code;
2268 /* let's log the request time */
2269 s->logs.tv_request = now;
2270 stream_int_retnclose(req->prod, &rdr);
2271 goto return_prx_cond;
2272 }
2273 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002274
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002275 /* that's OK for us now, let's move on to next analysers */
2276 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002277
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002278 return_bad_req:
2279 /* We centralize bad requests processing here */
2280 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2281 /* we detected a parsing error. We want to archive this request
2282 * in the dedicated proxy area for later troubleshooting.
2283 */
2284 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2285 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002286
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002287 txn->req.msg_state = HTTP_MSG_ERROR;
2288 txn->status = 400;
2289 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2290 s->fe->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002291
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002292 return_prx_cond:
2293 if (!(s->flags & SN_ERR_MASK))
2294 s->flags |= SN_ERR_PRXCOND;
2295 if (!(s->flags & SN_FINST_MASK))
2296 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002297
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002298 req->analysers = 0;
2299 req->analyse_exp = TICK_ETERNITY;
2300 return 0;
2301}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002302
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002303/* This function performs all the processing enabled for the current request.
2304 * It returns 1 if the processing can continue on next analysers, or zero if it
2305 * needs more data, encounters an error, or wants to immediately abort the
2306 * request. It relies on buffers flags, and updates s->req->analysers.
2307 */
2308int http_process_request(struct session *s, struct buffer *req, int an_bit)
2309{
2310 struct http_txn *txn = &s->txn;
2311 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002312
Willy Tarreau51aecc72009-07-12 09:47:04 +02002313 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2314 /* we need more data */
2315 buffer_write_dis(req);
2316 return 0;
2317 }
2318
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002319 req->analysers &= ~an_bit;
2320 req->analyse_exp = TICK_ETERNITY;
2321
2322 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2323 now_ms, __FUNCTION__,
2324 s,
2325 req,
2326 req->rex, req->wex,
2327 req->flags,
2328 req->l,
2329 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002330
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 /*
2332 * Right now, we know that we have processed the entire headers
2333 * and that unwanted requests have been filtered out. We can do
2334 * whatever we want with the remaining request. Also, now we
2335 * may have separate values for ->fe, ->be.
2336 */
Willy Tarreau06619262006-12-17 08:37:22 +01002337
Willy Tarreau59234e92008-11-30 23:51:27 +01002338 /*
2339 * If HTTP PROXY is set we simply get remote server address
2340 * parsing incoming request.
2341 */
2342 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2343 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2344 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002345
Willy Tarreau59234e92008-11-30 23:51:27 +01002346 /*
2347 * 7: the appsession cookie was looked up very early in 1.2,
2348 * so let's do the same now.
2349 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002350
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 /* It needs to look into the URI */
2352 if (s->be->appsession_name) {
2353 get_srv_from_appsession(s, &req->data[msg->som], msg->sl.rq.l);
2354 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01002355
Willy Tarreau59234e92008-11-30 23:51:27 +01002356 /*
2357 * 8: Now we can work with the cookies.
2358 * Note that doing so might move headers in the request, but
2359 * the fields will stay coherent and the URI will not move.
2360 * This should only be performed in the backend.
2361 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002362 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2364 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002365
Willy Tarreau59234e92008-11-30 23:51:27 +01002366 /*
2367 * 9: add X-Forwarded-For if either the frontend or the backend
2368 * asks for it.
2369 */
2370 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2371 if (s->cli_addr.ss_family == AF_INET) {
2372 /* Add an X-Forwarded-For header unless the source IP is
2373 * in the 'except' network range.
2374 */
2375 if ((!s->fe->except_mask.s_addr ||
2376 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2377 != s->fe->except_net.s_addr) &&
2378 (!s->be->except_mask.s_addr ||
2379 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2380 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002381 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002382 unsigned char *pn;
2383 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002384
2385 /* Note: we rely on the backend to get the header name to be used for
2386 * x-forwarded-for, because the header is really meant for the backends.
2387 * However, if the backend did not specify any option, we have to rely
2388 * on the frontend's header name.
2389 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 if (s->be->fwdfor_hdr_len) {
2391 len = s->be->fwdfor_hdr_len;
2392 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002393 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002394 len = s->fe->fwdfor_hdr_len;
2395 memcpy(trash, s->fe->fwdfor_hdr_name, len);
2396 }
2397 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002398
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002399 if (unlikely(http_header_add_tail2(req, &txn->req,
2400 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002401 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002402 }
2403 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002404 else if (s->cli_addr.ss_family == AF_INET6) {
2405 /* FIXME: for the sake of completeness, we should also support
2406 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002407 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002408 int len;
2409 char pn[INET6_ADDRSTRLEN];
2410 inet_ntop(AF_INET6,
2411 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2412 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002413
Willy Tarreau59234e92008-11-30 23:51:27 +01002414 /* Note: we rely on the backend to get the header name to be used for
2415 * x-forwarded-for, because the header is really meant for the backends.
2416 * However, if the backend did not specify any option, we have to rely
2417 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002418 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002419 if (s->be->fwdfor_hdr_len) {
2420 len = s->be->fwdfor_hdr_len;
2421 memcpy(trash, s->be->fwdfor_hdr_name, len);
2422 } else {
2423 len = s->fe->fwdfor_hdr_len;
2424 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002425 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002426 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002427
Willy Tarreau59234e92008-11-30 23:51:27 +01002428 if (unlikely(http_header_add_tail2(req, &txn->req,
2429 &txn->hdr_idx, trash, len)) < 0)
2430 goto return_bad_req;
2431 }
2432 }
2433
2434 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002435 * 10: add X-Original-To if either the frontend or the backend
2436 * asks for it.
2437 */
2438 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
2439
2440 /* FIXME: don't know if IPv6 can handle that case too. */
2441 if (s->cli_addr.ss_family == AF_INET) {
2442 /* Add an X-Original-To header unless the destination IP is
2443 * in the 'except' network range.
2444 */
2445 if (!(s->flags & SN_FRT_ADDR_SET))
2446 get_frt_addr(s);
2447
2448 if ((!s->fe->except_mask_to.s_addr ||
2449 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
2450 != s->fe->except_to.s_addr) &&
2451 (!s->be->except_mask_to.s_addr ||
2452 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
2453 != s->be->except_to.s_addr)) {
2454 int len;
2455 unsigned char *pn;
2456 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
2457
2458 /* Note: we rely on the backend to get the header name to be used for
2459 * x-original-to, because the header is really meant for the backends.
2460 * However, if the backend did not specify any option, we have to rely
2461 * on the frontend's header name.
2462 */
2463 if (s->be->orgto_hdr_len) {
2464 len = s->be->orgto_hdr_len;
2465 memcpy(trash, s->be->orgto_hdr_name, len);
2466 } else {
2467 len = s->fe->orgto_hdr_len;
2468 memcpy(trash, s->fe->orgto_hdr_name, len);
2469 }
2470 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
2471
2472 if (unlikely(http_header_add_tail2(req, &txn->req,
2473 &txn->hdr_idx, trash, len)) < 0)
2474 goto return_bad_req;
2475 }
2476 }
2477 }
2478
2479 /*
2480 * 11: add "Connection: close" if needed and not yet set.
Willy Tarreau59234e92008-11-30 23:51:27 +01002481 * Note that we do not need to add it in case of HTTP/1.0.
2482 */
2483 if (!(s->flags & SN_CONN_CLOSED) &&
2484 ((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2485 if ((unlikely(msg->sl.rq.v_l != 8) ||
2486 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2487 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
2488 "Connection: close", 17)) < 0)
2489 goto return_bad_req;
2490 s->flags |= SN_CONN_CLOSED;
2491 }
2492 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2493 * If not assigned, perhaps we are balancing on url_param, but this is a
2494 * POST; and the parameters are in the body, maybe scan there to find our server.
2495 * (unless headers overflowed the buffer?)
2496 */
2497 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2498 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
2499 s->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
2500 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2501 /* are there enough bytes here? total == l || r || rlim ?
2502 * len is unsigned, but eoh is int,
2503 * how many bytes of body have we received?
2504 * eoh is the first empty line of the header
2505 */
2506 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
2507 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
2508
2509 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2510 * We can't assume responsibility for the server's decision,
2511 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2512 * We also can't change our mind later, about which server to choose, so round robin.
2513 */
2514 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2515 struct hdr_ctx ctx;
2516 ctx.idx = 0;
2517 /* Expect is allowed in 1.1, look for it */
2518 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2519 if (ctx.idx != 0 &&
2520 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
2521 /* We can't reliablly stall and wait for data, because of
2522 * .NET clients that don't conform to rfc2616; so, no need for
2523 * the next block to check length expectations.
2524 * We could send 100 status back to the client, but then we need to
2525 * re-write headers, and send the message. And this isn't the right
2526 * place for that action.
2527 * TODO: support Expect elsewhere and delete this block.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002528 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002529 goto end_check_maybe_wait_for_body;
2530 }
2531
2532 if (likely(len > s->be->url_param_post_limit)) {
2533 /* nothing to do, we got enough */
2534 } else {
2535 /* limit implies we are supposed to need this many bytes
2536 * to find the parameter. Let's see how many bytes we can wait for.
2537 */
2538 long long hint = len;
2539 struct hdr_ctx ctx;
2540 ctx.idx = 0;
2541 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
2542 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2543 buffer_write_dis(req);
2544 req->analysers |= AN_REQ_HTTP_BODY;
2545 }
2546 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002547 ctx.idx = 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002548 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2549 /* now if we have a length, we'll take the hint */
2550 if (ctx.idx) {
2551 /* We have Content-Length */
2552 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
2553 hint = 0; /* parse failure, untrusted client */
2554 else {
2555 if (hint > 0)
2556 msg->hdr_content_len = hint;
2557 else
2558 hint = 0; /* bad client, sent negative length */
2559 }
2560 }
2561 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
2562 if (s->be->url_param_post_limit < hint)
2563 hint = s->be->url_param_post_limit;
2564 /* now do we really need to buffer more data? */
2565 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002566 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002567 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002568 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 /* else... There are no body bytes to wait for */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002570 }
2571 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 }
2573 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002574
Willy Tarreau59234e92008-11-30 23:51:27 +01002575 /*************************************************************
2576 * OK, that's finished for the headers. We have done what we *
2577 * could. Let's switch to the DATA state. *
2578 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002579
Willy Tarreau59234e92008-11-30 23:51:27 +01002580 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
2581 s->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002582
Willy Tarreau59234e92008-11-30 23:51:27 +01002583 /* When a connection is tarpitted, we use the tarpit timeout,
2584 * which may be the same as the connect timeout if unspecified.
2585 * If unset, then set it to zero because we really want it to
2586 * eventually expire. We build the tarpit as an analyser.
2587 */
2588 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau6f0aa472009-03-08 20:33:29 +01002589 buffer_erase(s->req);
2590 /* wipe the request out so that we can drop the connection early
Willy Tarreau59234e92008-11-30 23:51:27 +01002591 * if the client closes first.
Willy Tarreau2a324282006-12-05 00:05:46 +01002592 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 buffer_write_dis(req);
2594 req->analysers |= AN_REQ_HTTP_TARPIT;
2595 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2596 if (!req->analyse_exp)
Willy Tarreau2ab85e62009-03-29 10:24:15 +02002597 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau59234e92008-11-30 23:51:27 +01002598 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002599
Willy Tarreau59234e92008-11-30 23:51:27 +01002600 /* OK let's go on with the BODY now */
2601 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002602
Willy Tarreau59234e92008-11-30 23:51:27 +01002603 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02002604 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002605 /* we detected a parsing error. We want to archive this request
2606 * in the dedicated proxy area for later troubleshooting.
2607 */
Willy Tarreau4076a152009-04-02 15:18:36 +02002608 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01002609 }
Willy Tarreau4076a152009-04-02 15:18:36 +02002610
Willy Tarreau59234e92008-11-30 23:51:27 +01002611 txn->req.msg_state = HTTP_MSG_ERROR;
2612 txn->status = 400;
2613 req->analysers = 0;
2614 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2615 s->fe->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002616
Willy Tarreau59234e92008-11-30 23:51:27 +01002617 if (!(s->flags & SN_ERR_MASK))
2618 s->flags |= SN_ERR_PRXCOND;
2619 if (!(s->flags & SN_FINST_MASK))
2620 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002621 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002622}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002623
Willy Tarreau60b85b02008-11-30 23:28:40 +01002624/* This function is an analyser which processes the HTTP tarpit. It always
2625 * returns zero, at the beginning because it prevents any other processing
2626 * from occurring, and at the end because it terminates the request.
2627 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002628int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01002629{
2630 struct http_txn *txn = &s->txn;
2631
2632 /* This connection is being tarpitted. The CLIENT side has
2633 * already set the connect expiration date to the right
2634 * timeout. We just have to check that the client is still
2635 * there and that the timeout has not expired.
2636 */
2637 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
2638 !tick_is_expired(req->analyse_exp, now_ms))
2639 return 0;
2640
2641 /* We will set the queue timer to the time spent, just for
2642 * logging purposes. We fake a 500 server error, so that the
2643 * attacker will not suspect his connection has been tarpitted.
2644 * It will not cause trouble to the logs because we can exclude
2645 * the tarpitted connections by filtering on the 'PT' status flags.
2646 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01002647 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2648
2649 txn->status = 500;
2650 if (req->flags != BF_READ_ERROR)
2651 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
2652
2653 req->analysers = 0;
2654 req->analyse_exp = TICK_ETERNITY;
2655
2656 s->fe->failed_req++;
2657 if (!(s->flags & SN_ERR_MASK))
2658 s->flags |= SN_ERR_PRXCOND;
2659 if (!(s->flags & SN_FINST_MASK))
2660 s->flags |= SN_FINST_T;
2661 return 0;
2662}
2663
Willy Tarreaud34af782008-11-30 23:36:37 +01002664/* This function is an analyser which processes the HTTP request body. It looks
2665 * for parameters to be used for the load balancing algorithm (url_param). It
2666 * must only be called after the standard HTTP request processing has occurred,
2667 * because it expects the request to be parsed. It returns zero if it needs to
2668 * read more data, or 1 once it has completed its analysis.
2669 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002670int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01002671{
2672 struct http_msg *msg = &s->txn.req;
2673 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2674 long long limit = s->be->url_param_post_limit;
2675 struct hdr_ctx ctx;
2676
Willy Tarreau51aecc72009-07-12 09:47:04 +02002677 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2678 /* we need more data */
2679 buffer_write_dis(req);
2680 return 0;
2681 }
2682
Willy Tarreaud34af782008-11-30 23:36:37 +01002683 /* We have to parse the HTTP request body to find any required data.
2684 * "balance url_param check_post" should have been the only way to get
2685 * into this. We were brought here after HTTP header analysis, so all
2686 * related structures are ready.
2687 */
2688
2689 ctx.idx = 0;
2690
2691 /* now if we have a length, we'll take the hint */
2692 http_find_header2("Transfer-Encoding", 17, msg->sol, &s->txn.hdr_idx, &ctx);
2693 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2694 unsigned int chunk = 0;
2695 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2696 char c = msg->sol[body];
2697 if (ishex(c)) {
2698 unsigned int hex = toupper(c) - '0';
2699 if (hex > 9)
2700 hex -= 'A' - '9' - 1;
2701 chunk = (chunk << 4) | hex;
2702 } else
2703 break;
2704 body++;
2705 }
2706 if (body + 2 >= req->l) /* we want CRLF too */
2707 goto http_body_end; /* end of buffer? data missing! */
2708
2709 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
2710 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
2711
2712 body += 2; // skip CRLF
2713
2714 /* if we support more then one chunk here, we have to do it again when assigning server
2715 * 1. how much entity data do we have? new var
2716 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2717 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2718 */
2719
2720 if (chunk < limit)
2721 limit = chunk; /* only reading one chunk */
2722 } else {
2723 if (msg->hdr_content_len < limit)
2724 limit = msg->hdr_content_len;
2725 }
2726
2727 http_body_end:
2728 /* we leave once we know we have nothing left to do. This means that we have
2729 * enough bytes, or that we know we'll not get any more (buffer full, read
2730 * buffer closed).
2731 */
2732 if (req->l - body >= limit || /* enough bytes! */
2733 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
2734 tick_is_expired(req->analyse_exp, now_ms)) {
2735 /* The situation will not evolve, so let's give up on the analysis. */
2736 s->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau3a816292009-07-07 10:55:49 +02002737 req->analysers &= ~an_bit;
Willy Tarreaud34af782008-11-30 23:36:37 +01002738 req->analyse_exp = TICK_ETERNITY;
2739 return 1;
2740 }
2741 else {
2742 /* Not enough data. We'll re-use the http-request
2743 * timeout here. Ideally, we should set the timeout
2744 * relative to the accept() date. We just set the
2745 * request timeout once at the beginning of the
2746 * request.
2747 */
2748 buffer_write_dis(req);
2749 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002750 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01002751 return 0;
2752 }
2753}
2754
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002755/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002756 * It normally returns zero, but may return 1 if it absolutely needs to be
2757 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002758 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002759 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002760 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002761int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002762{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002763 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002764 struct buffer *req = t->req;
2765 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002766
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002767 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 +02002768 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002769 t,
2770 rep,
2771 rep->rex, rep->wex,
2772 rep->flags,
2773 rep->l,
2774 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002775
Willy Tarreau2df28e82008-08-17 15:20:19 +02002776 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002777 /*
2778 * Now parse the partial (or complete) lines.
2779 * We will check the response syntax, and also join multi-line
2780 * headers. An index of all the lines will be elaborated while
2781 * parsing.
2782 *
2783 * For the parsing, we use a 28 states FSM.
2784 *
2785 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002786 * rep->data + rep->som = beginning of response
2787 * rep->data + rep->eoh = end of processed headers / start of current one
2788 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002789 * rep->lr = first non-visited byte
2790 * rep->r = end of data
2791 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002792
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002793 int cur_idx;
2794 struct http_msg *msg = &txn->rsp;
2795 struct proxy *cur_proxy;
2796
2797 if (likely(rep->lr < rep->r))
2798 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2799
2800 /* 1: we might have to print this header in debug mode */
2801 if (unlikely((global.mode & MODE_DEBUG) &&
2802 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2803 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2804 char *eol, *sol;
2805
2806 sol = rep->data + msg->som;
2807 eol = sol + msg->sl.rq.l;
2808 debug_hdr("srvrep", t, sol, eol);
2809
2810 sol += hdr_idx_first_pos(&txn->hdr_idx);
2811 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2812
2813 while (cur_idx) {
2814 eol = sol + txn->hdr_idx.v[cur_idx].len;
2815 debug_hdr("srvhdr", t, sol, eol);
2816 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2817 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002818 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002819 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002820
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002821 /*
2822 * Now we quickly check if we have found a full valid response.
2823 * If not so, we check the FD and buffer states before leaving.
2824 * A full response is indicated by the fact that we have seen
2825 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2826 * responses are checked first.
2827 *
2828 * Depending on whether the client is still there or not, we
2829 * may send an error response back or not. Note that normally
2830 * we should only check for HTTP status there, and check I/O
2831 * errors somewhere else.
2832 */
2833
2834 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002835 /* Invalid response */
2836 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002837 /* we detected a parsing error. We want to archive this response
2838 * in the dedicated proxy area for later troubleshooting.
2839 */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002840 hdr_response_bad:
Willy Tarreau4076a152009-04-02 15:18:36 +02002841 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
2842 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
2843
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002844 buffer_shutr_now(rep);
2845 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002846 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002847 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002848 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002849 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002850 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002851 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002852 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002853 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002854 if (!(t->flags & SN_FINST_MASK))
2855 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002856
Willy Tarreaudafde432008-08-17 01:00:46 +02002857 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002858 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002859 /* too large response does not fit in buffer. */
2860 else if (rep->flags & BF_FULL) {
2861 goto hdr_response_bad;
2862 }
2863 /* read error */
2864 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002865 if (msg->err_pos >= 0)
2866 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002867 buffer_shutr_now(rep);
2868 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002869 if (t->srv)
2870 t->srv->failed_resp++;
2871 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002872 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002873 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002874 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002875 if (!(t->flags & SN_ERR_MASK))
2876 t->flags |= SN_ERR_SRVCL;
2877 if (!(t->flags & SN_FINST_MASK))
2878 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002879 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002880 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002881 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002882 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002883 if (msg->err_pos >= 0)
2884 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002885 buffer_shutr_now(rep);
2886 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002887 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002888 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002889 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002890 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002891 txn->status = 504;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002892 stream_int_return(rep->cons, error_message(t, HTTP_ERR_504));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002893 if (!(t->flags & SN_ERR_MASK))
2894 t->flags |= SN_ERR_SRVTO;
2895 if (!(t->flags & SN_FINST_MASK))
2896 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002897 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002898 }
Willy Tarreau8365f932009-03-15 23:11:49 +01002899 /* close from server */
2900 else if (rep->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002901 if (msg->err_pos >= 0)
2902 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002903 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002904 if (t->srv)
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002905 t->srv->failed_resp++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002906 t->be->failed_resp++;
2907 rep->analysers = 0;
2908 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002909 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002910 if (!(t->flags & SN_ERR_MASK))
2911 t->flags |= SN_ERR_SRVCL;
2912 if (!(t->flags & SN_FINST_MASK))
2913 t->flags |= SN_FINST_H;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002914 return 0;
2915 }
Willy Tarreau8365f932009-03-15 23:11:49 +01002916 /* write error to client (we don't send any message then) */
2917 else if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002918 if (msg->err_pos >= 0)
2919 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreau8365f932009-03-15 23:11:49 +01002920 buffer_shutr_now(rep);
2921 t->be->failed_resp++;
2922 rep->analysers = 0;
2923 if (!(t->flags & SN_ERR_MASK))
2924 t->flags |= SN_ERR_CLICL;
2925 if (!(t->flags & SN_FINST_MASK))
2926 t->flags |= SN_FINST_H;
2927 return 0;
2928 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02002929 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002930 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002931 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002932
Willy Tarreau21d2af32008-02-14 20:25:24 +01002933
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002934 /*****************************************************************
2935 * More interesting part now : we know that we have a complete *
2936 * response which at least looks like HTTP. We have an indicator *
2937 * of each header's length, so we can parse them quickly. *
2938 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002939
Willy Tarreau4076a152009-04-02 15:18:36 +02002940 if (msg->err_pos >= 0)
2941 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
2942
Willy Tarreau2df28e82008-08-17 15:20:19 +02002943 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002944
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002945 /* ensure we keep this pointer to the beginning of the message */
2946 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002947
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002948 /*
2949 * 1: get the status code and check for cacheability.
2950 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002951
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002952 t->logs.logwait &= ~LW_RESP;
2953 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002954
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002955 switch (txn->status) {
2956 case 200:
2957 case 203:
2958 case 206:
2959 case 300:
2960 case 301:
2961 case 410:
2962 /* RFC2616 @13.4:
2963 * "A response received with a status code of
2964 * 200, 203, 206, 300, 301 or 410 MAY be stored
2965 * by a cache (...) unless a cache-control
2966 * directive prohibits caching."
2967 *
2968 * RFC2616 @9.5: POST method :
2969 * "Responses to this method are not cacheable,
2970 * unless the response includes appropriate
2971 * Cache-Control or Expires header fields."
2972 */
2973 if (likely(txn->meth != HTTP_METH_POST) &&
2974 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2975 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2976 break;
2977 default:
2978 break;
2979 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002980
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002981 /*
2982 * 2: we may need to capture headers
2983 */
2984 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2985 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2986 txn->rsp.cap, t->fe->rsp_cap);
2987
2988 /*
2989 * 3: we will have to evaluate the filters.
2990 * As opposed to version 1.2, now they will be evaluated in the
2991 * filters order and not in the header order. This means that
2992 * each filter has to be validated among all headers.
2993 *
2994 * Filters are tried with ->be first, then with ->fe if it is
2995 * different from ->be.
2996 */
2997
2998 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2999
3000 cur_proxy = t->be;
3001 while (1) {
3002 struct proxy *rule_set = cur_proxy;
3003
3004 /* try headers filters */
3005 if (rule_set->rsp_exp != NULL) {
3006 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3007 return_bad_resp:
Willy Tarreau8365f932009-03-15 23:11:49 +01003008 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003009 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003010 cur_proxy->failed_resp++;
3011 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02003012 buffer_shutr_now(rep);
3013 buffer_shutw_now(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02003014 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003015 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01003016 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003017 if (!(t->flags & SN_ERR_MASK))
3018 t->flags |= SN_ERR_PRXCOND;
3019 if (!(t->flags & SN_FINST_MASK))
3020 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02003021 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003022 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003023 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003024
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003025 /* has the response been denied ? */
3026 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01003027 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003028 t->srv->failed_secu++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003029 cur_proxy->denied_resp++;
3030 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003031 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003032
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003033 /* We might have to check for "Connection:" */
3034 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
3035 !(t->flags & SN_CONN_CLOSED)) {
3036 char *cur_ptr, *cur_end, *cur_next;
3037 int cur_idx, old_idx, delta, val;
3038 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003039
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003040 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3041 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003042
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003043 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3044 cur_hdr = &txn->hdr_idx.v[cur_idx];
3045 cur_ptr = cur_next;
3046 cur_end = cur_ptr + cur_hdr->len;
3047 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003048
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003049 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3050 if (val) {
3051 /* 3 possibilities :
3052 * - we have already set Connection: close,
3053 * so we remove this line.
3054 * - we have not yet set Connection: close,
3055 * but this line indicates close. We leave
3056 * it untouched and set the flag.
3057 * - we have not yet set Connection: close,
3058 * and this line indicates non-close. We
3059 * replace it.
3060 */
3061 if (t->flags & SN_CONN_CLOSED) {
3062 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
3063 txn->rsp.eoh += delta;
3064 cur_next += delta;
3065 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3066 txn->hdr_idx.used--;
3067 cur_hdr->len = 0;
3068 } else {
3069 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3070 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3071 "close", 5);
3072 cur_next += delta;
3073 cur_hdr->len += delta;
3074 txn->rsp.eoh += delta;
3075 }
3076 t->flags |= SN_CONN_CLOSED;
3077 }
3078 }
3079 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003080 }
3081 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003082
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003083 /* add response headers from the rule sets in the same order */
3084 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
3085 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3086 rule_set->rsp_add[cur_idx])) < 0)
3087 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003088 }
3089
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003090 /* check whether we're already working on the frontend */
3091 if (cur_proxy == t->fe)
3092 break;
3093 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003094 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003095
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003096 /*
3097 * 4: check for server cookie.
3098 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003099 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003100 || (t->be->options & PR_O_CHK_CACHE))
3101 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003102
Willy Tarreaubaaee002006-06-26 02:48:02 +02003103
Willy Tarreaua15645d2007-03-18 16:22:39 +01003104 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003105 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003106 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003107 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
3108 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003109
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003110 /*
3111 * 6: add server cookie in the response if needed
3112 */
3113 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
3114 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
3115 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003116
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003117 /* the server is known, it's not the one the client requested, we have to
3118 * insert a set-cookie here, except if we want to insert only on POST
3119 * requests and this one isn't. Note that servers which don't have cookies
3120 * (eg: some backup servers) will return a full cookie removal request.
3121 */
3122 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
3123 t->be->cookie_name,
3124 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003125
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003126 if (t->be->cookie_domain)
3127 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003128
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003129 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3130 trash, len)) < 0)
3131 goto return_bad_resp;
3132 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003133
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003134 /* Here, we will tell an eventual cache on the client side that we don't
3135 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3136 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3137 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3138 */
3139 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003140
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003141 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3142
3143 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3144 "Cache-control: private", 22)) < 0)
3145 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003146 }
3147 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003148
Willy Tarreaubaaee002006-06-26 02:48:02 +02003149
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003150 /*
3151 * 7: check if result will be cacheable with a cookie.
3152 * We'll block the response if security checks have caught
3153 * nasty things such as a cacheable cookie.
3154 */
3155 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3156 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
3157 (t->be->options & PR_O_CHK_CACHE)) {
3158
3159 /* we're in presence of a cacheable response containing
3160 * a set-cookie header. We'll block it as requested by
3161 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003162 */
Willy Tarreau8365f932009-03-15 23:11:49 +01003163 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003164 t->srv->failed_secu++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003165 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003166
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003167 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3168 t->be->id, t->srv?t->srv->id:"<dispatch>");
3169 send_log(t->be, LOG_ALERT,
3170 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3171 t->be->id, t->srv?t->srv->id:"<dispatch>");
3172 goto return_srv_prx_502;
3173 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003174
3175 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003176 * 8: add "Connection: close" if needed and not yet set.
3177 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003178 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003179 if (!(t->flags & SN_CONN_CLOSED) &&
3180 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
3181 if ((unlikely(msg->sl.st.v_l != 8) ||
3182 unlikely(req->data[msg->som + 7] != '0')) &&
3183 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3184 "Connection: close", 17)) < 0)
3185 goto return_bad_resp;
3186 t->flags |= SN_CONN_CLOSED;
3187 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003188
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003189 /*************************************************************
3190 * OK, that's finished for the headers. We have done what we *
3191 * could. Let's switch to the DATA state. *
3192 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003193
Willy Tarreaue393fe22008-08-16 22:18:07 +02003194 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003195 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003196
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003197#ifdef CONFIG_HAP_TCPSPLICE
3198 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
3199 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003200 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003201 }
3202#endif
3203 /* if the user wants to log as soon as possible, without counting
3204 * bytes from the server, then this is the right moment. We have
3205 * to temporarily assign bytes_out to log what we currently have.
3206 */
3207 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3208 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3209 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01003210 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003211 t->logs.bytes_out = 0;
3212 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003213
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003214 /* Note: we must not try to cheat by jumping directly to DATA,
3215 * otherwise we would not let the client side wake up.
3216 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003217
Willy Tarreaudafde432008-08-17 01:00:46 +02003218 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003219 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003220
Willy Tarreau2df28e82008-08-17 15:20:19 +02003221 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3222 * probably reduce one day's debugging session.
3223 */
3224#ifdef DEBUG_DEV
3225 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3226 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3227 __FILE__, __LINE__, rep->analysers);
3228 ABORT_NOW();
3229 }
3230#endif
3231 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003232 return 0;
3233}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003234
Willy Tarreaubaaee002006-06-26 02:48:02 +02003235/*
3236 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003237 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02003238 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
3239 * buffer, which it uses to keep on being called when there is free space in
Willy Tarreau01bf8672008-12-07 18:03:29 +01003240 * the buffer, or simply by letting an empty buffer upon return.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003241 */
Willy Tarreau01bf8672008-12-07 18:03:29 +01003242void produce_content(struct session *s, struct buffer *rep)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003243{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003244 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau01bf8672008-12-07 18:03:29 +01003245 buffer_stop_hijack(rep);
3246 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003247 }
3248 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003249 /* dump server statistics */
Willy Tarreau0a464892008-12-07 18:30:00 +01003250 int ret = stats_dump_http(s, rep, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003251 if (ret >= 0)
Willy Tarreau01bf8672008-12-07 18:03:29 +01003252 return;
Willy Tarreau91861262007-10-17 17:06:05 +02003253 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003254 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003255
Willy Tarreau91861262007-10-17 17:06:05 +02003256 /* unknown data source or internal error */
3257 s->txn.status = 500;
Willy Tarreau01bf8672008-12-07 18:03:29 +01003258 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_500));
Willy Tarreau91861262007-10-17 17:06:05 +02003259 if (!(s->flags & SN_ERR_MASK))
3260 s->flags |= SN_ERR_PRXCOND;
3261 if (!(s->flags & SN_FINST_MASK))
3262 s->flags |= SN_FINST_R;
Willy Tarreau01bf8672008-12-07 18:03:29 +01003263 buffer_stop_hijack(rep);
3264 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003265}
3266
3267
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003268/* Iterate the same filter through all request headers.
3269 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003270 * Since it can manage the switch to another backend, it updates the per-proxy
3271 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003272 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003273int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003274{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003275 char term;
3276 char *cur_ptr, *cur_end, *cur_next;
3277 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003278 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003279 struct hdr_idx_elem *cur_hdr;
3280 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003281
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003282 last_hdr = 0;
3283
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003284 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003285 old_idx = 0;
3286
3287 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003288 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003289 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003290 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003291 (exp->action == ACT_ALLOW ||
3292 exp->action == ACT_DENY ||
3293 exp->action == ACT_TARPIT))
3294 return 0;
3295
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003296 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003297 if (!cur_idx)
3298 break;
3299
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003300 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003301 cur_ptr = cur_next;
3302 cur_end = cur_ptr + cur_hdr->len;
3303 cur_next = cur_end + cur_hdr->cr + 1;
3304
3305 /* Now we have one header between cur_ptr and cur_end,
3306 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003307 */
3308
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003309 /* The annoying part is that pattern matching needs
3310 * that we modify the contents to null-terminate all
3311 * strings before testing them.
3312 */
3313
3314 term = *cur_end;
3315 *cur_end = '\0';
3316
3317 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3318 switch (exp->action) {
3319 case ACT_SETBE:
3320 /* It is not possible to jump a second time.
3321 * FIXME: should we return an HTTP/500 here so that
3322 * the admin knows there's a problem ?
3323 */
3324 if (t->be != t->fe)
3325 break;
3326
3327 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003328 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003329 last_hdr = 1;
3330 break;
3331
3332 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003333 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003334 last_hdr = 1;
3335 break;
3336
3337 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003338 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003339 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003340 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003341 break;
3342
3343 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003344 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003345 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003346 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003347 break;
3348
3349 case ACT_REPLACE:
3350 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3351 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3352 /* FIXME: if the user adds a newline in the replacement, the
3353 * index will not be recalculated for now, and the new line
3354 * will not be counted as a new header.
3355 */
3356
3357 cur_end += delta;
3358 cur_next += delta;
3359 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003360 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003361 break;
3362
3363 case ACT_REMOVE:
3364 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3365 cur_next += delta;
3366
3367 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003368 txn->req.eoh += delta;
3369 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3370 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003371 cur_hdr->len = 0;
3372 cur_end = NULL; /* null-term has been rewritten */
3373 break;
3374
3375 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003376 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003377 if (cur_end)
3378 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003380 /* keep the link from this header to next one in case of later
3381 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003382 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003383 old_idx = cur_idx;
3384 }
3385 return 0;
3386}
3387
3388
3389/* Apply the filter to the request line.
3390 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3391 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003392 * Since it can manage the switch to another backend, it updates the per-proxy
3393 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003394 */
3395int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3396{
3397 char term;
3398 char *cur_ptr, *cur_end;
3399 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003400 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003401 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003402
Willy Tarreau58f10d72006-12-04 02:26:12 +01003403
Willy Tarreau3d300592007-03-18 18:34:41 +01003404 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003405 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003406 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003407 (exp->action == ACT_ALLOW ||
3408 exp->action == ACT_DENY ||
3409 exp->action == ACT_TARPIT))
3410 return 0;
3411 else if (exp->action == ACT_REMOVE)
3412 return 0;
3413
3414 done = 0;
3415
Willy Tarreau9cdde232007-05-02 20:58:19 +02003416 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003417 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003418
3419 /* Now we have the request line between cur_ptr and cur_end */
3420
3421 /* The annoying part is that pattern matching needs
3422 * that we modify the contents to null-terminate all
3423 * strings before testing them.
3424 */
3425
3426 term = *cur_end;
3427 *cur_end = '\0';
3428
3429 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3430 switch (exp->action) {
3431 case ACT_SETBE:
3432 /* It is not possible to jump a second time.
3433 * FIXME: should we return an HTTP/500 here so that
3434 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003435 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003436 if (t->be != t->fe)
3437 break;
3438
3439 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003440 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003441 done = 1;
3442 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003443
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003444 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003445 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003446 done = 1;
3447 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003448
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003449 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003450 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003451 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003452 done = 1;
3453 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003455 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003456 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003457 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003458 done = 1;
3459 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003461 case ACT_REPLACE:
3462 *cur_end = term; /* restore the string terminator */
3463 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3464 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3465 /* FIXME: if the user adds a newline in the replacement, the
3466 * index will not be recalculated for now, and the new line
3467 * will not be counted as a new header.
3468 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003469
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003470 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003471 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003472
Willy Tarreau9cdde232007-05-02 20:58:19 +02003473 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003474 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003475 HTTP_MSG_RQMETH,
3476 cur_ptr, cur_end + 1,
3477 NULL, NULL);
3478 if (unlikely(!cur_end))
3479 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003481 /* we have a full request and we know that we have either a CR
3482 * or an LF at <ptr>.
3483 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003484 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3485 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003486 /* there is no point trying this regex on headers */
3487 return 1;
3488 }
3489 }
3490 *cur_end = term; /* restore the string terminator */
3491 return done;
3492}
Willy Tarreau97de6242006-12-27 17:18:38 +01003493
Willy Tarreau58f10d72006-12-04 02:26:12 +01003494
Willy Tarreau58f10d72006-12-04 02:26:12 +01003495
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003496/*
3497 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3498 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003499 * unparsable request. Since it can manage the switch to another backend, it
3500 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003501 */
3502int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3503{
Willy Tarreau3d300592007-03-18 18:34:41 +01003504 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003505 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003506 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003507 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003509 /*
3510 * The interleaving of transformations and verdicts
3511 * makes it difficult to decide to continue or stop
3512 * the evaluation.
3513 */
3514
Willy Tarreau3d300592007-03-18 18:34:41 +01003515 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003516 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3517 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3518 exp = exp->next;
3519 continue;
3520 }
3521
3522 /* Apply the filter to the request line. */
3523 ret = apply_filter_to_req_line(t, req, exp);
3524 if (unlikely(ret < 0))
3525 return -1;
3526
3527 if (likely(ret == 0)) {
3528 /* The filter did not match the request, it can be
3529 * iterated through all headers.
3530 */
3531 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003532 }
3533 exp = exp->next;
3534 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003535 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003536}
3537
3538
Willy Tarreaua15645d2007-03-18 16:22:39 +01003539
Willy Tarreau58f10d72006-12-04 02:26:12 +01003540/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003541 * Manage client-side cookie. It can impact performance by about 2% so it is
3542 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003543 */
3544void manage_client_side_cookies(struct session *t, struct buffer *req)
3545{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003546 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003547 char *p1, *p2, *p3, *p4;
3548 char *del_colon, *del_cookie, *colon;
3549 int app_cookies;
3550
3551 appsess *asession_temp = NULL;
3552 appsess local_asession;
3553
3554 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003555 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003556
Willy Tarreau2a324282006-12-05 00:05:46 +01003557 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003558 * we start with the start line.
3559 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003560 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003561 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003562
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003563 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003564 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003565 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003566
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003567 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003568 cur_ptr = cur_next;
3569 cur_end = cur_ptr + cur_hdr->len;
3570 cur_next = cur_end + cur_hdr->cr + 1;
3571
3572 /* We have one full header between cur_ptr and cur_end, and the
3573 * next header starts at cur_next. We're only interested in
3574 * "Cookie:" headers.
3575 */
3576
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003577 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3578 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003579 old_idx = cur_idx;
3580 continue;
3581 }
3582
3583 /* Now look for cookies. Conforming to RFC2109, we have to support
3584 * attributes whose name begin with a '$', and associate them with
3585 * the right cookie, if we want to delete this cookie.
3586 * So there are 3 cases for each cookie read :
3587 * 1) it's a special attribute, beginning with a '$' : ignore it.
3588 * 2) it's a server id cookie that we *MAY* want to delete : save
3589 * some pointers on it (last semi-colon, beginning of cookie...)
3590 * 3) it's an application cookie : we *MAY* have to delete a previous
3591 * "special" cookie.
3592 * At the end of loop, if a "special" cookie remains, we may have to
3593 * remove it. If no application cookie persists in the header, we
3594 * *MUST* delete it
3595 */
3596
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003597 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003598
Willy Tarreau58f10d72006-12-04 02:26:12 +01003599 /* del_cookie == NULL => nothing to be deleted */
3600 del_colon = del_cookie = NULL;
3601 app_cookies = 0;
3602
3603 while (p1 < cur_end) {
3604 /* skip spaces and colons, but keep an eye on these ones */
3605 while (p1 < cur_end) {
3606 if (*p1 == ';' || *p1 == ',')
3607 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003608 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003609 break;
3610 p1++;
3611 }
3612
3613 if (p1 == cur_end)
3614 break;
3615
3616 /* p1 is at the beginning of the cookie name */
3617 p2 = p1;
3618 while (p2 < cur_end && *p2 != '=')
3619 p2++;
3620
3621 if (p2 == cur_end)
3622 break;
3623
3624 p3 = p2 + 1; /* skips the '=' sign */
3625 if (p3 == cur_end)
3626 break;
3627
3628 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003629 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003630 p4++;
3631
3632 /* here, we have the cookie name between p1 and p2,
3633 * and its value between p3 and p4.
3634 * we can process it :
3635 *
3636 * Cookie: NAME=VALUE;
3637 * | || || |
3638 * | || || +--> p4
3639 * | || |+-------> p3
3640 * | || +--------> p2
3641 * | |+------------> p1
3642 * | +-------------> colon
3643 * +--------------------> cur_ptr
3644 */
3645
3646 if (*p1 == '$') {
3647 /* skip this one */
3648 }
3649 else {
3650 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003651 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003652 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003653 (p4 - p1 >= t->fe->capture_namelen) &&
3654 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003655 int log_len = p4 - p1;
3656
Willy Tarreau086b3b42007-05-13 21:45:51 +02003657 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003658 Alert("HTTP logging : out of memory.\n");
3659 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003660 if (log_len > t->fe->capture_len)
3661 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003662 memcpy(txn->cli_cookie, p1, log_len);
3663 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003664 }
3665 }
3666
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003667 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3668 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003669 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003670 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003671 char *delim;
3672
3673 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3674 * have the server ID betweek p3 and delim, and the original cookie between
3675 * delim+1 and p4. Otherwise, delim==p4 :
3676 *
3677 * Cookie: NAME=SRV~VALUE;
3678 * | || || | |
3679 * | || || | +--> p4
3680 * | || || +--------> delim
3681 * | || |+-----------> p3
3682 * | || +------------> p2
3683 * | |+----------------> p1
3684 * | +-----------------> colon
3685 * +------------------------> cur_ptr
3686 */
3687
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003688 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003689 for (delim = p3; delim < p4; delim++)
3690 if (*delim == COOKIE_DELIM)
3691 break;
3692 }
3693 else
3694 delim = p4;
3695
3696
3697 /* Here, we'll look for the first running server which supports the cookie.
3698 * This allows to share a same cookie between several servers, for example
3699 * to dedicate backup servers to specific servers only.
3700 * However, to prevent clients from sticking to cookie-less backup server
3701 * when they have incidentely learned an empty cookie, we simply ignore
3702 * empty cookies and mark them as invalid.
3703 */
3704 if (delim == p3)
3705 srv = NULL;
3706
3707 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003708 if (srv->cookie && (srv->cklen == delim - p3) &&
3709 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003710 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003711 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003712 txn->flags &= ~TX_CK_MASK;
3713 txn->flags |= TX_CK_VALID;
3714 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003715 t->srv = srv;
3716 break;
3717 } else {
3718 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003719 txn->flags &= ~TX_CK_MASK;
3720 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003721 }
3722 }
3723 srv = srv->next;
3724 }
3725
Willy Tarreau3d300592007-03-18 18:34:41 +01003726 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003727 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003728 txn->flags &= ~TX_CK_MASK;
3729 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003730 }
3731
3732 /* depending on the cookie mode, we may have to either :
3733 * - delete the complete cookie if we're in insert+indirect mode, so that
3734 * the server never sees it ;
3735 * - remove the server id from the cookie value, and tag the cookie as an
3736 * application cookie so that it does not get accidentely removed later,
3737 * if we're in cookie prefix mode
3738 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003739 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003740 int delta; /* negative */
3741
3742 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3743 p4 += delta;
3744 cur_end += delta;
3745 cur_next += delta;
3746 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003747 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003748
3749 del_cookie = del_colon = NULL;
3750 app_cookies++; /* protect the header from deletion */
3751 }
3752 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003753 (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 +01003754 del_cookie = p1;
3755 del_colon = colon;
3756 }
3757 } else {
3758 /* now we know that we must keep this cookie since it's
3759 * not ours. But if we wanted to delete our cookie
3760 * earlier, we cannot remove the complete header, but we
3761 * can remove the previous block itself.
3762 */
3763 app_cookies++;
3764
3765 if (del_cookie != NULL) {
3766 int delta; /* negative */
3767
3768 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3769 p4 += delta;
3770 cur_end += delta;
3771 cur_next += delta;
3772 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003773 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003774 del_cookie = del_colon = NULL;
3775 }
3776 }
3777
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003778 if ((t->be->appsession_name != NULL) &&
3779 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003780 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003781
Willy Tarreau58f10d72006-12-04 02:26:12 +01003782 /* Cool... it's the right one */
3783
3784 asession_temp = &local_asession;
3785
Willy Tarreau63963c62007-05-13 21:29:55 +02003786 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003787 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3788 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3789 return;
3790 }
3791
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003792 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3793 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003794 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003795
Willy Tarreau58f10d72006-12-04 02:26:12 +01003796 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003797 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3798 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003799 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003800 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003801 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003802 Alert("Not enough memory process_cli():asession:calloc().\n");
3803 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3804 return;
3805 }
3806
3807 asession_temp->sessid = local_asession.sessid;
3808 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003809 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02003810 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003811 } else {
3812 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003813 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003814 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003815 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003816 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003817 Alert("Found Application Session without matching server.\n");
3818 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003819 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003820 while (srv) {
3821 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003822 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003823 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003824 txn->flags &= ~TX_CK_MASK;
3825 txn->flags |= TX_CK_VALID;
3826 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003827 t->srv = srv;
3828 break;
3829 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01003830 txn->flags &= ~TX_CK_MASK;
3831 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003832 }
3833 }
3834 srv = srv->next;
3835 }/* end while(srv) */
3836 }/* end else if server == NULL */
3837
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003838 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003839 asession_temp->request_count++;
3840#if defined(DEBUG_HASH)
3841 Alert("manage_client_side_cookies\n");
3842 appsession_hash_dump(&(t->be->htbl_proxy));
3843#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01003844 }/* end if ((t->proxy->appsession_name != NULL) ... */
3845 }
3846
3847 /* we'll have to look for another cookie ... */
3848 p1 = p4;
3849 } /* while (p1 < cur_end) */
3850
3851 /* There's no more cookie on this line.
3852 * We may have marked the last one(s) for deletion.
3853 * We must do this now in two ways :
3854 * - if there is no app cookie, we simply delete the header ;
3855 * - if there are app cookies, we must delete the end of the
3856 * string properly, including the colon/semi-colon before
3857 * the cookie name.
3858 */
3859 if (del_cookie != NULL) {
3860 int delta;
3861 if (app_cookies) {
3862 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
3863 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003864 cur_hdr->len += delta;
3865 } else {
3866 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003867
3868 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003869 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3870 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003871 cur_hdr->len = 0;
3872 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01003873 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003874 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003875 }
3876
3877 /* keep the link from this header to next one */
3878 old_idx = cur_idx;
3879 } /* end of cookie processing on this header */
3880}
3881
3882
Willy Tarreaua15645d2007-03-18 16:22:39 +01003883/* Iterate the same filter through all response headers contained in <rtr>.
3884 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3885 */
3886int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3887{
3888 char term;
3889 char *cur_ptr, *cur_end, *cur_next;
3890 int cur_idx, old_idx, last_hdr;
3891 struct http_txn *txn = &t->txn;
3892 struct hdr_idx_elem *cur_hdr;
3893 int len, delta;
3894
3895 last_hdr = 0;
3896
3897 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3898 old_idx = 0;
3899
3900 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003901 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003902 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003903 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003904 (exp->action == ACT_ALLOW ||
3905 exp->action == ACT_DENY))
3906 return 0;
3907
3908 cur_idx = txn->hdr_idx.v[old_idx].next;
3909 if (!cur_idx)
3910 break;
3911
3912 cur_hdr = &txn->hdr_idx.v[cur_idx];
3913 cur_ptr = cur_next;
3914 cur_end = cur_ptr + cur_hdr->len;
3915 cur_next = cur_end + cur_hdr->cr + 1;
3916
3917 /* Now we have one header between cur_ptr and cur_end,
3918 * and the next header starts at cur_next.
3919 */
3920
3921 /* The annoying part is that pattern matching needs
3922 * that we modify the contents to null-terminate all
3923 * strings before testing them.
3924 */
3925
3926 term = *cur_end;
3927 *cur_end = '\0';
3928
3929 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3930 switch (exp->action) {
3931 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003932 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003933 last_hdr = 1;
3934 break;
3935
3936 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003937 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003938 last_hdr = 1;
3939 break;
3940
3941 case ACT_REPLACE:
3942 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3943 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3944 /* FIXME: if the user adds a newline in the replacement, the
3945 * index will not be recalculated for now, and the new line
3946 * will not be counted as a new header.
3947 */
3948
3949 cur_end += delta;
3950 cur_next += delta;
3951 cur_hdr->len += delta;
3952 txn->rsp.eoh += delta;
3953 break;
3954
3955 case ACT_REMOVE:
3956 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3957 cur_next += delta;
3958
3959 /* FIXME: this should be a separate function */
3960 txn->rsp.eoh += delta;
3961 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3962 txn->hdr_idx.used--;
3963 cur_hdr->len = 0;
3964 cur_end = NULL; /* null-term has been rewritten */
3965 break;
3966
3967 }
3968 }
3969 if (cur_end)
3970 *cur_end = term; /* restore the string terminator */
3971
3972 /* keep the link from this header to next one in case of later
3973 * removal of next header.
3974 */
3975 old_idx = cur_idx;
3976 }
3977 return 0;
3978}
3979
3980
3981/* Apply the filter to the status line in the response buffer <rtr>.
3982 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3983 * or -1 if a replacement resulted in an invalid status line.
3984 */
3985int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3986{
3987 char term;
3988 char *cur_ptr, *cur_end;
3989 int done;
3990 struct http_txn *txn = &t->txn;
3991 int len, delta;
3992
3993
Willy Tarreau3d300592007-03-18 18:34:41 +01003994 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003995 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003996 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003997 (exp->action == ACT_ALLOW ||
3998 exp->action == ACT_DENY))
3999 return 0;
4000 else if (exp->action == ACT_REMOVE)
4001 return 0;
4002
4003 done = 0;
4004
Willy Tarreau9cdde232007-05-02 20:58:19 +02004005 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004006 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4007
4008 /* Now we have the status line between cur_ptr and cur_end */
4009
4010 /* The annoying part is that pattern matching needs
4011 * that we modify the contents to null-terminate all
4012 * strings before testing them.
4013 */
4014
4015 term = *cur_end;
4016 *cur_end = '\0';
4017
4018 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4019 switch (exp->action) {
4020 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004021 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004022 done = 1;
4023 break;
4024
4025 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004026 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004027 done = 1;
4028 break;
4029
4030 case ACT_REPLACE:
4031 *cur_end = term; /* restore the string terminator */
4032 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4033 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4034 /* FIXME: if the user adds a newline in the replacement, the
4035 * index will not be recalculated for now, and the new line
4036 * will not be counted as a new header.
4037 */
4038
4039 txn->rsp.eoh += delta;
4040 cur_end += delta;
4041
Willy Tarreau9cdde232007-05-02 20:58:19 +02004042 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004043 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004044 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004045 cur_ptr, cur_end + 1,
4046 NULL, NULL);
4047 if (unlikely(!cur_end))
4048 return -1;
4049
4050 /* we have a full respnse and we know that we have either a CR
4051 * or an LF at <ptr>.
4052 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004053 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004054 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4055 /* there is no point trying this regex on headers */
4056 return 1;
4057 }
4058 }
4059 *cur_end = term; /* restore the string terminator */
4060 return done;
4061}
4062
4063
4064
4065/*
4066 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4067 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4068 * unparsable response.
4069 */
4070int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4071{
Willy Tarreau3d300592007-03-18 18:34:41 +01004072 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004073 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004074 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004075 int ret;
4076
4077 /*
4078 * The interleaving of transformations and verdicts
4079 * makes it difficult to decide to continue or stop
4080 * the evaluation.
4081 */
4082
Willy Tarreau3d300592007-03-18 18:34:41 +01004083 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004084 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4085 exp->action == ACT_PASS)) {
4086 exp = exp->next;
4087 continue;
4088 }
4089
4090 /* Apply the filter to the status line. */
4091 ret = apply_filter_to_sts_line(t, rtr, exp);
4092 if (unlikely(ret < 0))
4093 return -1;
4094
4095 if (likely(ret == 0)) {
4096 /* The filter did not match the response, it can be
4097 * iterated through all headers.
4098 */
4099 apply_filter_to_resp_headers(t, rtr, exp);
4100 }
4101 exp = exp->next;
4102 }
4103 return 0;
4104}
4105
4106
4107
4108/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004109 * Manage server-side cookies. It can impact performance by about 2% so it is
4110 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004111 */
4112void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4113{
4114 struct http_txn *txn = &t->txn;
4115 char *p1, *p2, *p3, *p4;
4116
4117 appsess *asession_temp = NULL;
4118 appsess local_asession;
4119
4120 char *cur_ptr, *cur_end, *cur_next;
4121 int cur_idx, old_idx, delta;
4122
Willy Tarreaua15645d2007-03-18 16:22:39 +01004123 /* Iterate through the headers.
4124 * we start with the start line.
4125 */
4126 old_idx = 0;
4127 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4128
4129 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4130 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004131 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004132
4133 cur_hdr = &txn->hdr_idx.v[cur_idx];
4134 cur_ptr = cur_next;
4135 cur_end = cur_ptr + cur_hdr->len;
4136 cur_next = cur_end + cur_hdr->cr + 1;
4137
4138 /* We have one full header between cur_ptr and cur_end, and the
4139 * next header starts at cur_next. We're only interested in
4140 * "Cookie:" headers.
4141 */
4142
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004143 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4144 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004145 old_idx = cur_idx;
4146 continue;
4147 }
4148
4149 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004150 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004151
4152
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004153 /* maybe we only wanted to see if there was a set-cookie. Note that
4154 * the cookie capture is declared in the fronend.
4155 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004156 if (t->be->cookie_name == NULL &&
4157 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004158 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004159 return;
4160
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004161 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004162
4163 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004164 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4165 break;
4166
4167 /* p1 is at the beginning of the cookie name */
4168 p2 = p1;
4169
4170 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4171 p2++;
4172
4173 if (p2 == cur_end || *p2 == ';') /* next cookie */
4174 break;
4175
4176 p3 = p2 + 1; /* skip the '=' sign */
4177 if (p3 == cur_end)
4178 break;
4179
4180 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004181 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004182 p4++;
4183
4184 /* here, we have the cookie name between p1 and p2,
4185 * and its value between p3 and p4.
4186 * we can process it.
4187 */
4188
4189 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004190 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004191 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004192 (p4 - p1 >= t->fe->capture_namelen) &&
4193 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004194 int log_len = p4 - p1;
4195
Willy Tarreau086b3b42007-05-13 21:45:51 +02004196 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004197 Alert("HTTP logging : out of memory.\n");
4198 }
4199
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004200 if (log_len > t->fe->capture_len)
4201 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004202 memcpy(txn->srv_cookie, p1, log_len);
4203 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004204 }
4205
4206 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004207 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4208 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004209 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004210 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004211
4212 /* If the cookie is in insert mode on a known server, we'll delete
4213 * this occurrence because we'll insert another one later.
4214 * We'll delete it too if the "indirect" option is set and we're in
4215 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004216 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4217 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004218 /* this header must be deleted */
4219 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4220 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4221 txn->hdr_idx.used--;
4222 cur_hdr->len = 0;
4223 cur_next += delta;
4224 txn->rsp.eoh += delta;
4225
Willy Tarreau3d300592007-03-18 18:34:41 +01004226 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004227 }
4228 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004229 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004230 /* replace bytes p3->p4 with the cookie name associated
4231 * with this server since we know it.
4232 */
4233 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4234 cur_hdr->len += delta;
4235 cur_next += delta;
4236 txn->rsp.eoh += delta;
4237
Willy Tarreau3d300592007-03-18 18:34:41 +01004238 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004239 }
4240 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004241 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004242 /* insert the cookie name associated with this server
4243 * before existing cookie, and insert a delimitor between them..
4244 */
4245 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4246 cur_hdr->len += delta;
4247 cur_next += delta;
4248 txn->rsp.eoh += delta;
4249
4250 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004251 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004252 }
4253 }
4254 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004255 else if ((t->be->appsession_name != NULL) &&
4256 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004257
4258 /* Cool... it's the right one */
4259
4260 size_t server_id_len = strlen(t->srv->id) + 1;
4261 asession_temp = &local_asession;
4262
Willy Tarreau63963c62007-05-13 21:29:55 +02004263 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004264 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4265 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4266 return;
4267 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004268 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4269 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004270 asession_temp->serverid = NULL;
4271
4272 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004273 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4274 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004275 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004276 Alert("Not enough Memory process_srv():asession:calloc().\n");
4277 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4278 return;
4279 }
4280 asession_temp->sessid = local_asession.sessid;
4281 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004282 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004283 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4284 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004285 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004286 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004287 }
4288
Willy Tarreaua15645d2007-03-18 16:22:39 +01004289 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004290 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004291 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4292 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4293 return;
4294 }
4295 asession_temp->serverid[0] = '\0';
4296 }
4297
4298 if (asession_temp->serverid[0] == '\0')
4299 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4300
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004301 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004302 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004303#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004304 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004305 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004306#endif
4307 }/* end if ((t->proxy->appsession_name != NULL) ... */
4308 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4309 } /* we're now at the end of the cookie value */
4310
4311 /* keep the link from this header to next one */
4312 old_idx = cur_idx;
4313 } /* end of cookie processing on this header */
4314}
4315
4316
4317
4318/*
4319 * Check if response is cacheable or not. Updates t->flags.
4320 */
4321void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4322{
4323 struct http_txn *txn = &t->txn;
4324 char *p1, *p2;
4325
4326 char *cur_ptr, *cur_end, *cur_next;
4327 int cur_idx;
4328
Willy Tarreau5df51872007-11-25 16:20:08 +01004329 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004330 return;
4331
4332 /* Iterate through the headers.
4333 * we start with the start line.
4334 */
4335 cur_idx = 0;
4336 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4337
4338 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4339 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004340 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004341
4342 cur_hdr = &txn->hdr_idx.v[cur_idx];
4343 cur_ptr = cur_next;
4344 cur_end = cur_ptr + cur_hdr->len;
4345 cur_next = cur_end + cur_hdr->cr + 1;
4346
4347 /* We have one full header between cur_ptr and cur_end, and the
4348 * next header starts at cur_next. We're only interested in
4349 * "Cookie:" headers.
4350 */
4351
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004352 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4353 if (val) {
4354 if ((cur_end - (cur_ptr + val) >= 8) &&
4355 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4356 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4357 return;
4358 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004359 }
4360
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004361 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4362 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004363 continue;
4364
4365 /* OK, right now we know we have a cache-control header at cur_ptr */
4366
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004367 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004368
4369 if (p1 >= cur_end) /* no more info */
4370 continue;
4371
4372 /* p1 is at the beginning of the value */
4373 p2 = p1;
4374
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004375 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004376 p2++;
4377
4378 /* we have a complete value between p1 and p2 */
4379 if (p2 < cur_end && *p2 == '=') {
4380 /* we have something of the form no-cache="set-cookie" */
4381 if ((cur_end - p1 >= 21) &&
4382 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4383 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004384 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004385 continue;
4386 }
4387
4388 /* OK, so we know that either p2 points to the end of string or to a comma */
4389 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4390 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4391 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4392 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004393 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004394 return;
4395 }
4396
4397 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004398 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004399 continue;
4400 }
4401 }
4402}
4403
4404
Willy Tarreau58f10d72006-12-04 02:26:12 +01004405/*
4406 * Try to retrieve a known appsession in the URI, then the associated server.
4407 * If the server is found, it's assigned to the session.
4408 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004409void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004410{
Willy Tarreau3d300592007-03-18 18:34:41 +01004411 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004412 appsess *asession_temp = NULL;
4413 appsess local_asession;
4414 char *request_line;
4415
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004416 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004417 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004418 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004419 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004420 return;
4421
4422 /* skip ';' */
4423 request_line++;
4424
4425 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004426 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004427 return;
4428
4429 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004430 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004431
4432 /* First try if we already have an appsession */
4433 asession_temp = &local_asession;
4434
Willy Tarreau63963c62007-05-13 21:29:55 +02004435 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004436 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4437 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4438 return;
4439 }
4440
4441 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004442 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4443 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004444 asession_temp->serverid = NULL;
4445
4446 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004447 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4448 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004449 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004450 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004451 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004452 Alert("Not enough memory process_cli():asession:calloc().\n");
4453 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4454 return;
4455 }
4456 asession_temp->sessid = local_asession.sessid;
4457 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004458 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004459 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004460 }
4461 else {
4462 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004463 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004464 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004465
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004466 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004467 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004468
Willy Tarreau58f10d72006-12-04 02:26:12 +01004469#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004470 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004471 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004472#endif
4473 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004474 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004475 Alert("Found Application Session without matching server.\n");
4476 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004477 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004478 while (srv) {
4479 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004480 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004481 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004482 txn->flags &= ~TX_CK_MASK;
4483 txn->flags |= TX_CK_VALID;
4484 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004485 t->srv = srv;
4486 break;
4487 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004488 txn->flags &= ~TX_CK_MASK;
4489 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004490 }
4491 }
4492 srv = srv->next;
4493 }
4494 }
4495}
4496
4497
Willy Tarreaub2513902006-12-17 14:52:38 +01004498/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004499 * In a GET or HEAD request, check if the requested URI matches the stats uri
4500 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004501 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004502 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004503 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004504 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004505 *
4506 * Returns 1 if the session's state changes, otherwise 0.
4507 */
4508int stats_check_uri_auth(struct session *t, struct proxy *backend)
4509{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004510 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004511 struct uri_auth *uri_auth = backend->uri_auth;
4512 struct user_auth *user;
4513 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004514 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004515
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004516 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
4517
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004518 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004519 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004520 return 0;
4521
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004522 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004523
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004524 /* the URI is in h */
4525 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004526 return 0;
4527
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004528 h += uri_auth->uri_len;
4529 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4530 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004531 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004532 break;
4533 }
4534 h++;
4535 }
4536
4537 if (uri_auth->refresh) {
4538 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4539 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4540 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004541 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004542 break;
4543 }
4544 h++;
4545 }
4546 }
4547
Willy Tarreau55bb8452007-10-17 18:44:57 +02004548 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4549 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4550 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004551 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02004552 break;
4553 }
4554 h++;
4555 }
4556
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004557 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
4558
Willy Tarreaub2513902006-12-17 14:52:38 +01004559 /* we are in front of a interceptable URI. Let's check
4560 * if there's an authentication and if it's valid.
4561 */
4562 user = uri_auth->users;
4563 if (!user) {
4564 /* no user auth required, it's OK */
4565 authenticated = 1;
4566 } else {
4567 authenticated = 0;
4568
4569 /* a user list is defined, we have to check.
4570 * skip 21 chars for "Authorization: Basic ".
4571 */
4572
4573 /* FIXME: this should move to an earlier place */
4574 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004575 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4576 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4577 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004578 if (len > 14 &&
4579 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004580 txn->auth_hdr.str = h;
4581 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004582 break;
4583 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004584 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004585 }
4586
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004587 if (txn->auth_hdr.len < 21 ||
4588 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004589 user = NULL;
4590
4591 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004592 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4593 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004594 user->user_pwd, user->user_len)) {
4595 authenticated = 1;
4596 break;
4597 }
4598 user = user->next;
4599 }
4600 }
4601
4602 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004603 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004604
4605 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004606 msg.str = trash;
4607 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004608 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01004609 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02004610 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004611 if (!(t->flags & SN_ERR_MASK))
4612 t->flags |= SN_ERR_PRXCOND;
4613 if (!(t->flags & SN_FINST_MASK))
4614 t->flags |= SN_FINST_R;
4615 return 1;
4616 }
4617
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004618 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01004619 * data.
4620 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01004621 buffer_write_dis(t->req);
Willy Tarreau72b179a2008-08-28 16:01:32 +02004622 buffer_shutw_now(t->req);
4623 buffer_shutr_now(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02004624 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01004625 t->data_source = DATA_SRC_STATS;
4626 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02004627 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau01bf8672008-12-07 18:03:29 +01004628 buffer_install_hijacker(t, t->rep, produce_content);
Willy Tarreaub2513902006-12-17 14:52:38 +01004629 return 1;
4630}
4631
Willy Tarreau4076a152009-04-02 15:18:36 +02004632/*
4633 * Capture a bad request or response and archive it in the proxy's structure.
4634 */
4635void http_capture_bad_message(struct error_snapshot *es, struct session *s,
4636 struct buffer *buf, struct http_msg *msg,
4637 struct proxy *other_end)
4638{
Willy Tarreau2df8d712009-05-01 11:33:17 +02004639 es->len = buf->r - (buf->data + msg->som);
4640 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02004641 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02004642 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02004643 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02004644 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02004645 es->when = date; // user-visible date
4646 es->sid = s->uniq_id;
4647 es->srv = s->srv;
4648 es->oe = other_end;
4649 es->src = s->cli_addr;
4650}
Willy Tarreaub2513902006-12-17 14:52:38 +01004651
Willy Tarreaubaaee002006-06-26 02:48:02 +02004652/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004653 * Print a debug line with a header
4654 */
4655void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4656{
4657 int len, max;
4658 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004659 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004660 max = end - start;
4661 UBOUND(max, sizeof(trash) - len - 1);
4662 len += strlcpy2(trash + len, start, max + 1);
4663 trash[len++] = '\n';
4664 write(1, trash, len);
4665}
4666
4667
Willy Tarreau8797c062007-05-07 00:55:35 +02004668/************************************************************************/
4669/* The code below is dedicated to ACL parsing and matching */
4670/************************************************************************/
4671
4672
4673
4674
4675/* 1. Check on METHOD
4676 * We use the pre-parsed method if it is known, and store its number as an
4677 * integer. If it is unknown, we use the pointer and the length.
4678 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004679static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004680{
4681 int len, meth;
4682
Willy Tarreauae8b7962007-06-09 23:10:04 +02004683 len = strlen(*text);
4684 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004685
4686 pattern->val.i = meth;
4687 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004688 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004689 if (!pattern->ptr.str)
4690 return 0;
4691 pattern->len = len;
4692 }
4693 return 1;
4694}
4695
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004696static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004697acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4698 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004699{
4700 int meth;
4701 struct http_txn *txn = l7;
4702
Willy Tarreaub6866442008-07-14 23:54:42 +02004703 if (!txn)
4704 return 0;
4705
Willy Tarreauc11416f2007-06-17 16:58:38 +02004706 if (txn->req.msg_state != HTTP_MSG_BODY)
4707 return 0;
4708
Willy Tarreau8797c062007-05-07 00:55:35 +02004709 meth = txn->meth;
4710 test->i = meth;
4711 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004712 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4713 /* ensure the indexes are not affected */
4714 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004715 test->len = txn->req.sl.rq.m_l;
4716 test->ptr = txn->req.sol;
4717 }
4718 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4719 return 1;
4720}
4721
4722static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4723{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004724 int icase;
4725
Willy Tarreau8797c062007-05-07 00:55:35 +02004726 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02004727 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02004728
4729 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02004730 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004731
4732 /* Other method, we must compare the strings */
4733 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02004734 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004735
4736 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4737 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4738 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02004739 return ACL_PAT_FAIL;
4740 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004741}
4742
4743/* 2. Check on Request/Status Version
4744 * We simply compare strings here.
4745 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004746static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004747{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004748 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004749 if (!pattern->ptr.str)
4750 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004751 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004752 return 1;
4753}
4754
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004755static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004756acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4757 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004758{
4759 struct http_txn *txn = l7;
4760 char *ptr;
4761 int len;
4762
Willy Tarreaub6866442008-07-14 23:54:42 +02004763 if (!txn)
4764 return 0;
4765
Willy Tarreauc11416f2007-06-17 16:58:38 +02004766 if (txn->req.msg_state != HTTP_MSG_BODY)
4767 return 0;
4768
Willy Tarreau8797c062007-05-07 00:55:35 +02004769 len = txn->req.sl.rq.v_l;
4770 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4771
4772 while ((len-- > 0) && (*ptr++ != '/'));
4773 if (len <= 0)
4774 return 0;
4775
4776 test->ptr = ptr;
4777 test->len = len;
4778
4779 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4780 return 1;
4781}
4782
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004783static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004784acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4785 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004786{
4787 struct http_txn *txn = l7;
4788 char *ptr;
4789 int len;
4790
Willy Tarreaub6866442008-07-14 23:54:42 +02004791 if (!txn)
4792 return 0;
4793
Willy Tarreauc11416f2007-06-17 16:58:38 +02004794 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4795 return 0;
4796
Willy Tarreau8797c062007-05-07 00:55:35 +02004797 len = txn->rsp.sl.st.v_l;
4798 ptr = txn->rsp.sol;
4799
4800 while ((len-- > 0) && (*ptr++ != '/'));
4801 if (len <= 0)
4802 return 0;
4803
4804 test->ptr = ptr;
4805 test->len = len;
4806
4807 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4808 return 1;
4809}
4810
4811/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004812static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004813acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4814 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004815{
4816 struct http_txn *txn = l7;
4817 char *ptr;
4818 int len;
4819
Willy Tarreaub6866442008-07-14 23:54:42 +02004820 if (!txn)
4821 return 0;
4822
Willy Tarreauc11416f2007-06-17 16:58:38 +02004823 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4824 return 0;
4825
Willy Tarreau8797c062007-05-07 00:55:35 +02004826 len = txn->rsp.sl.st.c_l;
4827 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4828
4829 test->i = __strl2ui(ptr, len);
4830 test->flags = ACL_TEST_F_VOL_1ST;
4831 return 1;
4832}
4833
4834/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004835static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004836acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4837 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004838{
4839 struct http_txn *txn = l7;
4840
Willy Tarreaub6866442008-07-14 23:54:42 +02004841 if (!txn)
4842 return 0;
4843
Willy Tarreauc11416f2007-06-17 16:58:38 +02004844 if (txn->req.msg_state != HTTP_MSG_BODY)
4845 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004846
Willy Tarreauc11416f2007-06-17 16:58:38 +02004847 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4848 /* ensure the indexes are not affected */
4849 return 0;
4850
Willy Tarreau8797c062007-05-07 00:55:35 +02004851 test->len = txn->req.sl.rq.u_l;
4852 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4853
Willy Tarreauf3d25982007-05-08 22:45:09 +02004854 /* we do not need to set READ_ONLY because the data is in a buffer */
4855 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004856 return 1;
4857}
4858
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004859static int
4860acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
4861 struct acl_expr *expr, struct acl_test *test)
4862{
4863 struct http_txn *txn = l7;
4864
Willy Tarreaub6866442008-07-14 23:54:42 +02004865 if (!txn)
4866 return 0;
4867
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004868 if (txn->req.msg_state != HTTP_MSG_BODY)
4869 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004870
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004871 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4872 /* ensure the indexes are not affected */
4873 return 0;
4874
4875 /* Parse HTTP request */
4876 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4877 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
4878 test->i = AF_INET;
4879
4880 /*
4881 * If we are parsing url in frontend space, we prepare backend stage
4882 * to not parse again the same url ! optimization lazyness...
4883 */
4884 if (px->options & PR_O_HTTP_PROXY)
4885 l4->flags |= SN_ADDR_SET;
4886
4887 test->flags = ACL_TEST_F_READ_ONLY;
4888 return 1;
4889}
4890
4891static int
4892acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
4893 struct acl_expr *expr, struct acl_test *test)
4894{
4895 struct http_txn *txn = l7;
4896
Willy Tarreaub6866442008-07-14 23:54:42 +02004897 if (!txn)
4898 return 0;
4899
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004900 if (txn->req.msg_state != HTTP_MSG_BODY)
4901 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004902
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004903 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4904 /* ensure the indexes are not affected */
4905 return 0;
4906
4907 /* Same optimization as url_ip */
4908 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4909 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
4910
4911 if (px->options & PR_O_HTTP_PROXY)
4912 l4->flags |= SN_ADDR_SET;
4913
4914 test->flags = ACL_TEST_F_READ_ONLY;
4915 return 1;
4916}
4917
Willy Tarreauc11416f2007-06-17 16:58:38 +02004918/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
4919 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
4920 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02004921static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004922acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004923 struct acl_expr *expr, struct acl_test *test)
4924{
4925 struct http_txn *txn = l7;
4926 struct hdr_idx *idx = &txn->hdr_idx;
4927 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004928
Willy Tarreaub6866442008-07-14 23:54:42 +02004929 if (!txn)
4930 return 0;
4931
Willy Tarreau33a7e692007-06-10 19:45:56 +02004932 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4933 /* search for header from the beginning */
4934 ctx->idx = 0;
4935
Willy Tarreau33a7e692007-06-10 19:45:56 +02004936 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4937 test->flags |= ACL_TEST_F_FETCH_MORE;
4938 test->flags |= ACL_TEST_F_VOL_HDR;
4939 test->len = ctx->vlen;
4940 test->ptr = (char *)ctx->line + ctx->val;
4941 return 1;
4942 }
4943
4944 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4945 test->flags |= ACL_TEST_F_VOL_HDR;
4946 return 0;
4947}
4948
Willy Tarreau33a7e692007-06-10 19:45:56 +02004949static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004950acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
4951 struct acl_expr *expr, struct acl_test *test)
4952{
4953 struct http_txn *txn = l7;
4954
Willy Tarreaub6866442008-07-14 23:54:42 +02004955 if (!txn)
4956 return 0;
4957
Willy Tarreauc11416f2007-06-17 16:58:38 +02004958 if (txn->req.msg_state != HTTP_MSG_BODY)
4959 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004960
Willy Tarreauc11416f2007-06-17 16:58:38 +02004961 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4962 /* ensure the indexes are not affected */
4963 return 0;
4964
4965 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
4966}
4967
4968static int
4969acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
4970 struct acl_expr *expr, struct acl_test *test)
4971{
4972 struct http_txn *txn = l7;
4973
Willy Tarreaub6866442008-07-14 23:54:42 +02004974 if (!txn)
4975 return 0;
4976
Willy Tarreauc11416f2007-06-17 16:58:38 +02004977 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4978 return 0;
4979
4980 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
4981}
4982
4983/* 6. Check on HTTP header count. The number of occurrences is returned.
4984 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
4985 */
4986static int
4987acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004988 struct acl_expr *expr, struct acl_test *test)
4989{
4990 struct http_txn *txn = l7;
4991 struct hdr_idx *idx = &txn->hdr_idx;
4992 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004993 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02004994
Willy Tarreaub6866442008-07-14 23:54:42 +02004995 if (!txn)
4996 return 0;
4997
Willy Tarreau33a7e692007-06-10 19:45:56 +02004998 ctx.idx = 0;
4999 cnt = 0;
5000 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5001 cnt++;
5002
5003 test->i = cnt;
5004 test->flags = ACL_TEST_F_VOL_HDR;
5005 return 1;
5006}
5007
Willy Tarreauc11416f2007-06-17 16:58:38 +02005008static int
5009acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5010 struct acl_expr *expr, struct acl_test *test)
5011{
5012 struct http_txn *txn = l7;
5013
Willy Tarreaub6866442008-07-14 23:54:42 +02005014 if (!txn)
5015 return 0;
5016
Willy Tarreauc11416f2007-06-17 16:58:38 +02005017 if (txn->req.msg_state != HTTP_MSG_BODY)
5018 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005019
Willy Tarreauc11416f2007-06-17 16:58:38 +02005020 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5021 /* ensure the indexes are not affected */
5022 return 0;
5023
5024 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5025}
5026
5027static int
5028acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5029 struct acl_expr *expr, struct acl_test *test)
5030{
5031 struct http_txn *txn = l7;
5032
Willy Tarreaub6866442008-07-14 23:54:42 +02005033 if (!txn)
5034 return 0;
5035
Willy Tarreauc11416f2007-06-17 16:58:38 +02005036 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5037 return 0;
5038
5039 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5040}
5041
Willy Tarreau33a7e692007-06-10 19:45:56 +02005042/* 7. Check on HTTP header's integer value. The integer value is returned.
5043 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005044 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005045 */
5046static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005047acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005048 struct acl_expr *expr, struct acl_test *test)
5049{
5050 struct http_txn *txn = l7;
5051 struct hdr_idx *idx = &txn->hdr_idx;
5052 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005053
Willy Tarreaub6866442008-07-14 23:54:42 +02005054 if (!txn)
5055 return 0;
5056
Willy Tarreau33a7e692007-06-10 19:45:56 +02005057 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5058 /* search for header from the beginning */
5059 ctx->idx = 0;
5060
Willy Tarreau33a7e692007-06-10 19:45:56 +02005061 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5062 test->flags |= ACL_TEST_F_FETCH_MORE;
5063 test->flags |= ACL_TEST_F_VOL_HDR;
5064 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5065 return 1;
5066 }
5067
5068 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5069 test->flags |= ACL_TEST_F_VOL_HDR;
5070 return 0;
5071}
5072
Willy Tarreauc11416f2007-06-17 16:58:38 +02005073static int
5074acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5075 struct acl_expr *expr, struct acl_test *test)
5076{
5077 struct http_txn *txn = l7;
5078
Willy Tarreaub6866442008-07-14 23:54:42 +02005079 if (!txn)
5080 return 0;
5081
Willy Tarreauc11416f2007-06-17 16:58:38 +02005082 if (txn->req.msg_state != HTTP_MSG_BODY)
5083 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005084
Willy Tarreauc11416f2007-06-17 16:58:38 +02005085 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5086 /* ensure the indexes are not affected */
5087 return 0;
5088
5089 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5090}
5091
5092static int
5093acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5094 struct acl_expr *expr, struct acl_test *test)
5095{
5096 struct http_txn *txn = l7;
5097
Willy Tarreaub6866442008-07-14 23:54:42 +02005098 if (!txn)
5099 return 0;
5100
Willy Tarreauc11416f2007-06-17 16:58:38 +02005101 if (txn->rsp.msg_state != HTTP_MSG_BODY)
5102 return 0;
5103
5104 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5105}
5106
Willy Tarreau737b0c12007-06-10 21:28:46 +02005107/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5108 * the first '/' after the possible hostname, and ends before the possible '?'.
5109 */
5110static int
5111acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5112 struct acl_expr *expr, struct acl_test *test)
5113{
5114 struct http_txn *txn = l7;
5115 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005116
Willy Tarreaub6866442008-07-14 23:54:42 +02005117 if (!txn)
5118 return 0;
5119
Willy Tarreauc11416f2007-06-17 16:58:38 +02005120 if (txn->req.msg_state != HTTP_MSG_BODY)
5121 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005122
Willy Tarreauc11416f2007-06-17 16:58:38 +02005123 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5124 /* ensure the indexes are not affected */
5125 return 0;
5126
Willy Tarreau21d2af32008-02-14 20:25:24 +01005127 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5128 ptr = http_get_path(txn);
5129 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005130 return 0;
5131
5132 /* OK, we got the '/' ! */
5133 test->ptr = ptr;
5134
5135 while (ptr < end && *ptr != '?')
5136 ptr++;
5137
5138 test->len = ptr - test->ptr;
5139
5140 /* we do not need to set READ_ONLY because the data is in a buffer */
5141 test->flags = ACL_TEST_F_VOL_1ST;
5142 return 1;
5143}
5144
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005145static int
5146acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
5147 struct acl_expr *expr, struct acl_test *test)
5148{
5149 struct buffer *req = s->req;
5150 struct http_txn *txn = &s->txn;
5151 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02005152
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005153 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
5154 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
5155 */
5156
5157 if (!s || !req)
5158 return 0;
5159
5160 if (unlikely(msg->msg_state == HTTP_MSG_BODY)) {
5161 /* Already decoded as OK */
5162 test->flags |= ACL_TEST_F_SET_RES_PASS;
5163 return 1;
5164 }
5165
5166 /* Try to decode HTTP request */
5167 if (likely(req->lr < req->r))
5168 http_msg_analyzer(req, msg, &txn->hdr_idx);
5169
5170 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
5171 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
5172 test->flags |= ACL_TEST_F_SET_RES_FAIL;
5173 return 1;
5174 }
5175 /* wait for final state */
5176 test->flags |= ACL_TEST_F_MAY_CHANGE;
5177 return 0;
5178 }
5179
5180 /* OK we got a valid HTTP request. We have some minor preparation to
5181 * perform so that further checks can rely on HTTP tests.
5182 */
5183 msg->sol = req->data + msg->som;
5184 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
5185 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
5186 s->flags |= SN_REDIRECTABLE;
5187
5188 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
5189 test->flags |= ACL_TEST_F_SET_RES_FAIL;
5190 return 1;
5191 }
5192
5193 test->flags |= ACL_TEST_F_SET_RES_PASS;
5194 return 1;
5195}
5196
Willy Tarreau8797c062007-05-07 00:55:35 +02005197
5198/************************************************************************/
5199/* All supported keywords must be declared here. */
5200/************************************************************************/
5201
5202/* Note: must not be declared <const> as its list will be overwritten */
5203static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005204 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
5205
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005206 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5207 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5208 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5209 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005210
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005211 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5212 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5213 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5214 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5215 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5216 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5217 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5218 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5219 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005220
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005221 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5222 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5223 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5224 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5225 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5226 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5227 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5228 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5229 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5230 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005231
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005232 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5233 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5234 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5235 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5236 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5237 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5238 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5239 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5240 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005241
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005242 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5243 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5244 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5245 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5246 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5247 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5248 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005249
Willy Tarreauf3d25982007-05-08 22:45:09 +02005250 { NULL, NULL, NULL, NULL },
5251
5252#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005253 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5254 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5255 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5256 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5257 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5258 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5259 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5260
Willy Tarreau8797c062007-05-07 00:55:35 +02005261 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5262 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5263 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5264 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5265 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5266 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5267 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5268 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5269
5270 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5271 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5272 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5273 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5274 { NULL, NULL, NULL, NULL },
5275#endif
5276}};
5277
5278
5279__attribute__((constructor))
5280static void __http_protocol_init(void)
5281{
5282 acl_register_keywords(&acl_kws);
5283}
5284
5285
Willy Tarreau58f10d72006-12-04 02:26:12 +01005286/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005287 * Local variables:
5288 * c-indent-level: 8
5289 * c-basic-offset: 8
5290 * End:
5291 */