blob: 0711bdc834519e12a64e8c7416c8073874d0a9a7 [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
Willy Tarreau42250582007-04-01 01:30:43 +0200729/*
730 * send a log for the session when we have enough info about it.
731 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100732 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100733void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200734{
735 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
736 struct proxy *fe = s->fe;
737 struct proxy *be = s->be;
738 struct proxy *prx_log;
739 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200740 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200741 char *uri, *h;
742 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200743 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200744 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200745 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200746 int hdr;
747
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200748 /* if we don't want to log normal traffic, return now */
749 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
750 (s->conn_retries != be->conn_retries) ||
751 txn->status >= 500;
752 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
753 return;
754
Willy Tarreau42250582007-04-01 01:30:43 +0200755 if (fe->logfac1 < 0 && fe->logfac2 < 0)
756 return;
757 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100758
Willy Tarreau42250582007-04-01 01:30:43 +0200759 if (s->cli_addr.ss_family == AF_INET)
760 inet_ntop(AF_INET,
761 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
762 pn, sizeof(pn));
763 else
764 inet_ntop(AF_INET6,
765 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
766 pn, sizeof(pn));
767
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200768 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200769
770 /* FIXME: let's limit ourselves to frontend logging for now. */
771 tolog = fe->to_log;
772
773 h = tmpline;
774 if (fe->to_log & LW_REQHDR &&
775 txn->req.cap &&
776 (h < tmpline + sizeof(tmpline) - 10)) {
777 *(h++) = ' ';
778 *(h++) = '{';
779 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
780 if (hdr)
781 *(h++) = '|';
782 if (txn->req.cap[hdr] != NULL)
783 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
784 '#', hdr_encode_map, txn->req.cap[hdr]);
785 }
786 *(h++) = '}';
787 }
788
789 if (fe->to_log & LW_RSPHDR &&
790 txn->rsp.cap &&
791 (h < tmpline + sizeof(tmpline) - 7)) {
792 *(h++) = ' ';
793 *(h++) = '{';
794 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
795 if (hdr)
796 *(h++) = '|';
797 if (txn->rsp.cap[hdr] != NULL)
798 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
799 '#', hdr_encode_map, txn->rsp.cap[hdr]);
800 }
801 *(h++) = '}';
802 }
803
804 if (h < tmpline + sizeof(tmpline) - 4) {
805 *(h++) = ' ';
806 *(h++) = '"';
807 uri = txn->uri ? txn->uri : "<BADREQ>";
808 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
809 '#', url_encode_map, uri);
810 *(h++) = '"';
811 }
812 *h = '\0';
813
814 svid = (tolog & LW_SVID) ?
815 (s->data_source != DATA_SRC_STATS) ?
816 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
817
Willy Tarreau70089872008-06-13 21:12:51 +0200818 t_request = -1;
819 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
820 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
821
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200822 level = LOG_INFO;
823 if (err && (fe->options2 & PR_O2_LOGERRORS))
824 level = LOG_ERR;
825
826 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +0200827 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +0200828 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
829 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200830 pn,
831 (s->cli_addr.ss_family == AF_INET) ?
832 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
833 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200834 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +0200835 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200836 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +0200837 t_request,
838 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +0200839 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
840 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
841 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
842 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100843 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200844 txn->cli_cookie ? txn->cli_cookie : "-",
845 txn->srv_cookie ? txn->srv_cookie : "-",
846 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
847 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
848 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
849 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
850 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100851 (s->flags & SN_REDISP)?"+":"",
852 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200853 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
854
855 s->logs.logwait = 0;
856}
857
Willy Tarreau117f59e2007-03-04 18:17:17 +0100858
859/*
860 * Capture headers from message starting at <som> according to header list
861 * <cap_hdr>, and fill the <idx> structure appropriately.
862 */
863void capture_headers(char *som, struct hdr_idx *idx,
864 char **cap, struct cap_hdr *cap_hdr)
865{
866 char *eol, *sol, *col, *sov;
867 int cur_idx;
868 struct cap_hdr *h;
869 int len;
870
871 sol = som + hdr_idx_first_pos(idx);
872 cur_idx = hdr_idx_first_idx(idx);
873
874 while (cur_idx) {
875 eol = sol + idx->v[cur_idx].len;
876
877 col = sol;
878 while (col < eol && *col != ':')
879 col++;
880
881 sov = col + 1;
882 while (sov < eol && http_is_lws[(unsigned char)*sov])
883 sov++;
884
885 for (h = cap_hdr; h; h = h->next) {
886 if ((h->namelen == col - sol) &&
887 (strncasecmp(sol, h->name, h->namelen) == 0)) {
888 if (cap[h->index] == NULL)
889 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200890 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100891
892 if (cap[h->index] == NULL) {
893 Alert("HTTP capture : out of memory.\n");
894 continue;
895 }
896
897 len = eol - sov;
898 if (len > h->len)
899 len = h->len;
900
901 memcpy(cap[h->index], sov, len);
902 cap[h->index][len]=0;
903 }
904 }
905 sol = eol + idx->v[cur_idx].cr + 1;
906 cur_idx = idx->v[cur_idx].next;
907 }
908}
909
910
Willy Tarreau42250582007-04-01 01:30:43 +0200911/* either we find an LF at <ptr> or we jump to <bad>.
912 */
913#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
914
915/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
916 * otherwise to <http_msg_ood> with <state> set to <st>.
917 */
918#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
919 ptr++; \
920 if (likely(ptr < end)) \
921 goto good; \
922 else { \
923 state = (st); \
924 goto http_msg_ood; \
925 } \
926 } while (0)
927
928
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100930 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100931 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
932 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
933 * will give undefined results.
934 * Note that it is upon the caller's responsibility to ensure that ptr < end,
935 * and that msg->sol points to the beginning of the response.
936 * If a complete line is found (which implies that at least one CR or LF is
937 * found before <end>, the updated <ptr> is returned, otherwise NULL is
938 * returned indicating an incomplete line (which does not mean that parts have
939 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
940 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
941 * upon next call.
942 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200943 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100944 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
945 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200946 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100947 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100948const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
949 unsigned int state, const char *ptr, const char *end,
950 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100951{
Willy Tarreau8973c702007-01-21 23:58:29 +0100952 switch (state) {
953 http_msg_rpver:
954 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100955 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100956 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
957
958 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100959 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100960 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
961 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100962 state = HTTP_MSG_ERROR;
963 break;
964
Willy Tarreau8973c702007-01-21 23:58:29 +0100965 http_msg_rpver_sp:
966 case HTTP_MSG_RPVER_SP:
967 if (likely(!HTTP_IS_LWS(*ptr))) {
968 msg->sl.st.c = ptr - msg_buf;
969 goto http_msg_rpcode;
970 }
971 if (likely(HTTP_IS_SPHT(*ptr)))
972 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
973 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100974 state = HTTP_MSG_ERROR;
975 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100976
977 http_msg_rpcode:
978 case HTTP_MSG_RPCODE:
979 if (likely(!HTTP_IS_LWS(*ptr)))
980 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
981
982 if (likely(HTTP_IS_SPHT(*ptr))) {
983 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
984 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
985 }
986
987 /* so it's a CR/LF, so there is no reason phrase */
988 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
989 http_msg_rsp_reason:
990 /* FIXME: should we support HTTP responses without any reason phrase ? */
991 msg->sl.st.r = ptr - msg_buf;
992 msg->sl.st.r_l = 0;
993 goto http_msg_rpline_eol;
994
995 http_msg_rpcode_sp:
996 case HTTP_MSG_RPCODE_SP:
997 if (likely(!HTTP_IS_LWS(*ptr))) {
998 msg->sl.st.r = ptr - msg_buf;
999 goto http_msg_rpreason;
1000 }
1001 if (likely(HTTP_IS_SPHT(*ptr)))
1002 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1003 /* so it's a CR/LF, so there is no reason phrase */
1004 goto http_msg_rsp_reason;
1005
1006 http_msg_rpreason:
1007 case HTTP_MSG_RPREASON:
1008 if (likely(!HTTP_IS_CRLF(*ptr)))
1009 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1010 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1011 http_msg_rpline_eol:
1012 /* We have seen the end of line. Note that we do not
1013 * necessarily have the \n yet, but at least we know that we
1014 * have EITHER \r OR \n, otherwise the response would not be
1015 * complete. We can then record the response length and return
1016 * to the caller which will be able to register it.
1017 */
1018 msg->sl.st.l = ptr - msg->sol;
1019 return ptr;
1020
1021#ifdef DEBUG_FULL
1022 default:
1023 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1024 exit(1);
1025#endif
1026 }
1027
1028 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001029 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001030 if (ret_state)
1031 *ret_state = state;
1032 if (ret_ptr)
1033 *ret_ptr = (char *)ptr;
1034 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001035}
1036
1037
1038/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001039 * This function parses a request line between <ptr> and <end>, starting with
1040 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1041 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1042 * will give undefined results.
1043 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1044 * and that msg->sol points to the beginning of the request.
1045 * If a complete line is found (which implies that at least one CR or LF is
1046 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1047 * returned indicating an incomplete line (which does not mean that parts have
1048 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1049 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1050 * upon next call.
1051 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001052 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001053 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1054 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001055 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001056 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001057const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1058 unsigned int state, const char *ptr, const char *end,
1059 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001060{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001061 switch (state) {
1062 http_msg_rqmeth:
1063 case HTTP_MSG_RQMETH:
1064 if (likely(HTTP_IS_TOKEN(*ptr)))
1065 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001068 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001069 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1070 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072 if (likely(HTTP_IS_CRLF(*ptr))) {
1073 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001074 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001075 http_msg_req09_uri:
1076 msg->sl.rq.u = ptr - msg_buf;
1077 http_msg_req09_uri_e:
1078 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1079 http_msg_req09_ver:
1080 msg->sl.rq.v = ptr - msg_buf;
1081 msg->sl.rq.v_l = 0;
1082 goto http_msg_rqline_eol;
1083 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001084 state = HTTP_MSG_ERROR;
1085 break;
1086
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 http_msg_rqmeth_sp:
1088 case HTTP_MSG_RQMETH_SP:
1089 if (likely(!HTTP_IS_LWS(*ptr))) {
1090 msg->sl.rq.u = ptr - msg_buf;
1091 goto http_msg_rquri;
1092 }
1093 if (likely(HTTP_IS_SPHT(*ptr)))
1094 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1095 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1096 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001097
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 http_msg_rquri:
1099 case HTTP_MSG_RQURI:
1100 if (likely(!HTTP_IS_LWS(*ptr)))
1101 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001102
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 if (likely(HTTP_IS_SPHT(*ptr))) {
1104 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1105 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1106 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001107
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001108 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1109 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001110
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001111 http_msg_rquri_sp:
1112 case HTTP_MSG_RQURI_SP:
1113 if (likely(!HTTP_IS_LWS(*ptr))) {
1114 msg->sl.rq.v = ptr - msg_buf;
1115 goto http_msg_rqver;
1116 }
1117 if (likely(HTTP_IS_SPHT(*ptr)))
1118 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1119 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1120 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001121
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001122 http_msg_rqver:
1123 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001124 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001125 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001126
1127 if (likely(HTTP_IS_CRLF(*ptr))) {
1128 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1129 http_msg_rqline_eol:
1130 /* We have seen the end of line. Note that we do not
1131 * necessarily have the \n yet, but at least we know that we
1132 * have EITHER \r OR \n, otherwise the request would not be
1133 * complete. We can then record the request length and return
1134 * to the caller which will be able to register it.
1135 */
1136 msg->sl.rq.l = ptr - msg->sol;
1137 return ptr;
1138 }
1139
1140 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001141 state = HTTP_MSG_ERROR;
1142 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001143
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001144#ifdef DEBUG_FULL
1145 default:
1146 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1147 exit(1);
1148#endif
1149 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001150
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001151 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001152 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001153 if (ret_state)
1154 *ret_state = state;
1155 if (ret_ptr)
1156 *ret_ptr = (char *)ptr;
1157 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001158}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001159
1160
Willy Tarreau8973c702007-01-21 23:58:29 +01001161/*
1162 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001163 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001164 * when data are missing and recalled at the exact same location with no
1165 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001166 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1167 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001169void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1170{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001171 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001172 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001173
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001174 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001175 ptr = buf->lr;
1176 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001177
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001178 if (unlikely(ptr >= end))
1179 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001180
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001181 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001182 /*
1183 * First, states that are specific to the response only.
1184 * We check them first so that request and headers are
1185 * closer to each other (accessed more often).
1186 */
1187 http_msg_rpbefore:
1188 case HTTP_MSG_RPBEFORE:
1189 if (likely(HTTP_IS_TOKEN(*ptr))) {
1190 if (likely(ptr == buf->data)) {
1191 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001192 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001193 } else {
1194#if PARSE_PRESERVE_EMPTY_LINES
1195 /* only skip empty leading lines, don't remove them */
1196 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001197 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001198#else
1199 /* Remove empty leading lines, as recommended by
1200 * RFC2616. This takes a lot of time because we
1201 * must move all the buffer backwards, but this
1202 * is rarely needed. The method above will be
1203 * cleaner when we'll be able to start sending
1204 * the request from any place in the buffer.
1205 */
1206 buf->lr = ptr;
1207 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001208 msg->som = 0;
Willy Tarreau8973c702007-01-21 23:58:29 +01001209 msg->sol = buf->data;
1210 ptr = buf->data;
1211 end = buf->r;
1212#endif
1213 }
1214 hdr_idx_init(idx);
1215 state = HTTP_MSG_RPVER;
1216 goto http_msg_rpver;
1217 }
1218
1219 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1220 goto http_msg_invalid;
1221
1222 if (unlikely(*ptr == '\n'))
1223 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1224 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1225 /* stop here */
1226
1227 http_msg_rpbefore_cr:
1228 case HTTP_MSG_RPBEFORE_CR:
1229 EXPECT_LF_HERE(ptr, http_msg_invalid);
1230 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1231 /* stop here */
1232
1233 http_msg_rpver:
1234 case HTTP_MSG_RPVER:
1235 case HTTP_MSG_RPVER_SP:
1236 case HTTP_MSG_RPCODE:
1237 case HTTP_MSG_RPCODE_SP:
1238 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001239 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001240 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001241 if (unlikely(!ptr))
1242 return;
1243
1244 /* we have a full response and we know that we have either a CR
1245 * or an LF at <ptr>.
1246 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001247 //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 +01001248 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1249
1250 msg->sol = ptr;
1251 if (likely(*ptr == '\r'))
1252 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1253 goto http_msg_rpline_end;
1254
1255 http_msg_rpline_end:
1256 case HTTP_MSG_RPLINE_END:
1257 /* msg->sol must point to the first of CR or LF. */
1258 EXPECT_LF_HERE(ptr, http_msg_invalid);
1259 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1260 /* stop here */
1261
1262 /*
1263 * Second, states that are specific to the request only
1264 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 http_msg_rqbefore:
1266 case HTTP_MSG_RQBEFORE:
1267 if (likely(HTTP_IS_TOKEN(*ptr))) {
1268 if (likely(ptr == buf->data)) {
1269 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001270 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001271 } else {
1272#if PARSE_PRESERVE_EMPTY_LINES
1273 /* only skip empty leading lines, don't remove them */
1274 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001275 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001276#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 /* Remove empty leading lines, as recommended by
1278 * RFC2616. This takes a lot of time because we
1279 * must move all the buffer backwards, but this
1280 * is rarely needed. The method above will be
1281 * cleaner when we'll be able to start sending
1282 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001283 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001284 buf->lr = ptr;
1285 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001286 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 msg->sol = buf->data;
1288 ptr = buf->data;
1289 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001290#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001291 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001292 /* we will need this when keep-alive will be supported
1293 hdr_idx_init(idx);
1294 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 state = HTTP_MSG_RQMETH;
1296 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001297 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001299 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1300 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001301
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001302 if (unlikely(*ptr == '\n'))
1303 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1304 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001305 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001306
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001307 http_msg_rqbefore_cr:
1308 case HTTP_MSG_RQBEFORE_CR:
1309 EXPECT_LF_HERE(ptr, http_msg_invalid);
1310 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001312
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 http_msg_rqmeth:
1314 case HTTP_MSG_RQMETH:
1315 case HTTP_MSG_RQMETH_SP:
1316 case HTTP_MSG_RQURI:
1317 case HTTP_MSG_RQURI_SP:
1318 case HTTP_MSG_RQVER:
1319 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001320 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001321 if (unlikely(!ptr))
1322 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001323
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001324 /* we have a full request and we know that we have either a CR
1325 * or an LF at <ptr>.
1326 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001327 //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 +01001328 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 msg->sol = ptr;
1331 if (likely(*ptr == '\r'))
1332 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001333 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001334
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001335 http_msg_rqline_end:
1336 case HTTP_MSG_RQLINE_END:
1337 /* check for HTTP/0.9 request : no version information available.
1338 * msg->sol must point to the first of CR or LF.
1339 */
1340 if (unlikely(msg->sl.rq.v_l == 0))
1341 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001343 EXPECT_LF_HERE(ptr, http_msg_invalid);
1344 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001346
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 /*
1348 * Common states below
1349 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 http_msg_hdr_first:
1351 case HTTP_MSG_HDR_FIRST:
1352 msg->sol = ptr;
1353 if (likely(!HTTP_IS_CRLF(*ptr))) {
1354 goto http_msg_hdr_name;
1355 }
1356
1357 if (likely(*ptr == '\r'))
1358 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1359 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001360
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001361 http_msg_hdr_name:
1362 case HTTP_MSG_HDR_NAME:
1363 /* assumes msg->sol points to the first char */
1364 if (likely(HTTP_IS_TOKEN(*ptr)))
1365 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001366
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001367 if (likely(*ptr == ':')) {
1368 msg->col = ptr - buf->data;
1369 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1370 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001371
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001372 if (likely(msg->err_pos < -1) || *ptr == '\n')
1373 goto http_msg_invalid;
1374
1375 if (msg->err_pos == -1) /* capture error pointer */
1376 msg->err_pos = ptr - buf->data; /* >= 0 now */
1377
1378 /* and we still accept this non-token character */
1379 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 http_msg_hdr_l1_sp:
1382 case HTTP_MSG_HDR_L1_SP:
1383 /* assumes msg->sol points to the first char and msg->col to the colon */
1384 if (likely(HTTP_IS_SPHT(*ptr)))
1385 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 /* header value can be basically anything except CR/LF */
1388 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 if (likely(!HTTP_IS_CRLF(*ptr))) {
1391 goto http_msg_hdr_val;
1392 }
1393
1394 if (likely(*ptr == '\r'))
1395 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1396 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 http_msg_hdr_l1_lf:
1399 case HTTP_MSG_HDR_L1_LF:
1400 EXPECT_LF_HERE(ptr, http_msg_invalid);
1401 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 http_msg_hdr_l1_lws:
1404 case HTTP_MSG_HDR_L1_LWS:
1405 if (likely(HTTP_IS_SPHT(*ptr))) {
1406 /* replace HT,CR,LF with spaces */
1407 for (; buf->data+msg->sov < ptr; msg->sov++)
1408 buf->data[msg->sov] = ' ';
1409 goto http_msg_hdr_l1_sp;
1410 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001411 /* we had a header consisting only in spaces ! */
1412 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 goto http_msg_complete_header;
1414
1415 http_msg_hdr_val:
1416 case HTTP_MSG_HDR_VAL:
1417 /* assumes msg->sol points to the first char, msg->col to the
1418 * colon, and msg->sov points to the first character of the
1419 * value.
1420 */
1421 if (likely(!HTTP_IS_CRLF(*ptr)))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 msg->eol = ptr;
1425 /* Note: we could also copy eol into ->eoh so that we have the
1426 * real header end in case it ends with lots of LWS, but is this
1427 * really needed ?
1428 */
1429 if (likely(*ptr == '\r'))
1430 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1431 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001432
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 http_msg_hdr_l2_lf:
1434 case HTTP_MSG_HDR_L2_LF:
1435 EXPECT_LF_HERE(ptr, http_msg_invalid);
1436 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001437
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 http_msg_hdr_l2_lws:
1439 case HTTP_MSG_HDR_L2_LWS:
1440 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1441 /* LWS: replace HT,CR,LF with spaces */
1442 for (; msg->eol < ptr; msg->eol++)
1443 *msg->eol = ' ';
1444 goto http_msg_hdr_val;
1445 }
1446 http_msg_complete_header:
1447 /*
1448 * It was a new header, so the last one is finished.
1449 * Assumes msg->sol points to the first char, msg->col to the
1450 * colon, msg->sov points to the first character of the value
1451 * and msg->eol to the first CR or LF so we know how the line
1452 * ends. We insert last header into the index.
1453 */
1454 /*
1455 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1456 write(2, msg->sol, msg->eol-msg->sol);
1457 fprintf(stderr,"\n");
1458 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001459
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001460 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1461 idx, idx->tail) < 0))
1462 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 msg->sol = ptr;
1465 if (likely(!HTTP_IS_CRLF(*ptr))) {
1466 goto http_msg_hdr_name;
1467 }
1468
1469 if (likely(*ptr == '\r'))
1470 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1471 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 http_msg_last_lf:
1474 case HTTP_MSG_LAST_LF:
1475 /* Assumes msg->sol points to the first of either CR or LF */
1476 EXPECT_LF_HERE(ptr, http_msg_invalid);
1477 ptr++;
1478 buf->lr = ptr;
1479 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001480 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 return;
1482#ifdef DEBUG_FULL
1483 default:
1484 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1485 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001486#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 }
1488 http_msg_ood:
1489 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001490 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 buf->lr = ptr;
1492 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001493
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 http_msg_invalid:
1495 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001496 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001497 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 return;
1499}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001500
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001501/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1502 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1503 * nothing is done and 1 is returned.
1504 */
1505static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1506{
1507 int delta;
1508 char *cur_end;
1509
1510 if (msg->sl.rq.v_l != 0)
1511 return 1;
1512
1513 msg->sol = req->data + msg->som;
1514 cur_end = msg->sol + msg->sl.rq.l;
1515 delta = 0;
1516
1517 if (msg->sl.rq.u_l == 0) {
1518 /* if no URI was set, add "/" */
1519 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1520 cur_end += delta;
1521 msg->eoh += delta;
1522 }
1523 /* add HTTP version */
1524 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
1525 msg->eoh += delta;
1526 cur_end += delta;
1527 cur_end = (char *)http_parse_reqline(msg, req->data,
1528 HTTP_MSG_RQMETH,
1529 msg->sol, cur_end + 1,
1530 NULL, NULL);
1531 if (unlikely(!cur_end))
1532 return 0;
1533
1534 /* we have a full HTTP/1.0 request now and we know that
1535 * we have either a CR or an LF at <ptr>.
1536 */
1537 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1538 return 1;
1539}
1540
Willy Tarreaud787e662009-07-07 10:14:51 +02001541/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1542 * processing can continue on next analysers, or zero if it either needs more
1543 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1544 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1545 * when it has nothing left to do, and may remove any analyser when it wants to
1546 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001548int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549{
Willy Tarreau59234e92008-11-30 23:51:27 +01001550 /*
1551 * We will parse the partial (or complete) lines.
1552 * We will check the request syntax, and also join multi-line
1553 * headers. An index of all the lines will be elaborated while
1554 * parsing.
1555 *
1556 * For the parsing, we use a 28 states FSM.
1557 *
1558 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01001559 * req->data + msg->som = beginning of request
Willy Tarreau59234e92008-11-30 23:51:27 +01001560 * req->data + req->eoh = end of processed headers / start of current one
1561 * req->data + req->eol = end of current header or line (LF or CRLF)
1562 * req->lr = first non-visited byte
1563 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02001564 *
1565 * At end of parsing, we may perform a capture of the error (if any), and
1566 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
1567 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1568 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001569 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001570
Willy Tarreau59234e92008-11-30 23:51:27 +01001571 int cur_idx;
1572 struct http_txn *txn = &s->txn;
1573 struct http_msg *msg = &txn->req;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001574
Willy Tarreau6bf17362009-02-24 10:48:35 +01001575 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1576 now_ms, __FUNCTION__,
1577 s,
1578 req,
1579 req->rex, req->wex,
1580 req->flags,
1581 req->l,
1582 req->analysers);
1583
Willy Tarreau59234e92008-11-30 23:51:27 +01001584 if (likely(req->lr < req->r))
1585 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001586
Willy Tarreau59234e92008-11-30 23:51:27 +01001587 /* 1: we might have to print this header in debug mode */
1588 if (unlikely((global.mode & MODE_DEBUG) &&
1589 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
1590 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
1591 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001592
Willy Tarreau59234e92008-11-30 23:51:27 +01001593 sol = req->data + msg->som;
1594 eol = sol + msg->sl.rq.l;
1595 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001596
Willy Tarreau59234e92008-11-30 23:51:27 +01001597 sol += hdr_idx_first_pos(&txn->hdr_idx);
1598 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001599
Willy Tarreau59234e92008-11-30 23:51:27 +01001600 while (cur_idx) {
1601 eol = sol + txn->hdr_idx.v[cur_idx].len;
1602 debug_hdr("clihdr", s, sol, eol);
1603 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1604 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001606 }
1607
Willy Tarreau58f10d72006-12-04 02:26:12 +01001608
Willy Tarreau59234e92008-11-30 23:51:27 +01001609 /*
1610 * Now we quickly check if we have found a full valid request.
1611 * If not so, we check the FD and buffer states before leaving.
1612 * A full request is indicated by the fact that we have seen
1613 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
1614 * requests are checked first.
1615 *
1616 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001617
Willy Tarreau59234e92008-11-30 23:51:27 +01001618 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001619 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001620 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001621 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001622 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
1623 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001624
Willy Tarreau59234e92008-11-30 23:51:27 +01001625 /* 1: Since we are in header mode, if there's no space
1626 * left for headers, we won't be able to free more
1627 * later, so the session will never terminate. We
1628 * must terminate it now.
1629 */
1630 if (unlikely(req->flags & BF_FULL)) {
1631 /* FIXME: check if URI is set and return Status
1632 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001633 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001634 goto return_bad_req;
1635 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001636
Willy Tarreau59234e92008-11-30 23:51:27 +01001637 /* 2: have we encountered a read error ? */
1638 else if (req->flags & BF_READ_ERROR) {
1639 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02001640 if (msg->err_pos >= 0)
1641 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001642 msg->msg_state = HTTP_MSG_ERROR;
1643 req->analysers = 0;
1644 s->fe->failed_req++;
1645 if (!(s->flags & SN_ERR_MASK))
1646 s->flags |= SN_ERR_CLICL;
1647 if (!(s->flags & SN_FINST_MASK))
1648 s->flags |= SN_FINST_R;
1649 return 0;
1650 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001651
Willy Tarreau59234e92008-11-30 23:51:27 +01001652 /* 3: has the read timeout expired ? */
1653 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
1654 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02001655 if (msg->err_pos >= 0)
1656 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001657 txn->status = 408;
1658 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
1659 msg->msg_state = HTTP_MSG_ERROR;
1660 req->analysers = 0;
1661 s->fe->failed_req++;
1662 if (!(s->flags & SN_ERR_MASK))
1663 s->flags |= SN_ERR_CLITO;
1664 if (!(s->flags & SN_FINST_MASK))
1665 s->flags |= SN_FINST_R;
1666 return 0;
1667 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001668
Willy Tarreau59234e92008-11-30 23:51:27 +01001669 /* 4: have we encountered a close ? */
1670 else if (req->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02001671 if (msg->err_pos >= 0)
1672 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001673 txn->status = 400;
1674 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1675 msg->msg_state = HTTP_MSG_ERROR;
1676 req->analysers = 0;
1677 s->fe->failed_req++;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001678
Willy Tarreau59234e92008-11-30 23:51:27 +01001679 if (!(s->flags & SN_ERR_MASK))
1680 s->flags |= SN_ERR_CLICL;
1681 if (!(s->flags & SN_FINST_MASK))
1682 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001683 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001684 }
1685
Willy Tarreau59234e92008-11-30 23:51:27 +01001686 buffer_write_dis(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001687 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
1688
Willy Tarreau59234e92008-11-30 23:51:27 +01001689 /* just set the request timeout once at the beginning of the request */
1690 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02001691 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001692
Willy Tarreau59234e92008-11-30 23:51:27 +01001693 /* we're not ready yet */
1694 return 0;
1695 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001696
Willy Tarreaud787e662009-07-07 10:14:51 +02001697 /* OK now we have a complete HTTP request with indexed headers. Let's
1698 * complete the request parsing by setting a few fields we will need
1699 * later.
1700 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001701
Willy Tarreaud787e662009-07-07 10:14:51 +02001702 /* Maybe we found in invalid header name while we were configured not
1703 * to block on that, so we have to capture it now.
1704 */
1705 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02001706 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
1707
Willy Tarreau59234e92008-11-30 23:51:27 +01001708 /* ensure we keep this pointer to the beginning of the message */
1709 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001710
Willy Tarreau59234e92008-11-30 23:51:27 +01001711 /*
1712 * 1: identify the method
1713 */
1714 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
1715
1716 /* we can make use of server redirect on GET and HEAD */
1717 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1718 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001719
Willy Tarreau59234e92008-11-30 23:51:27 +01001720 /*
1721 * 2: check if the URI matches the monitor_uri.
1722 * We have to do this for every request which gets in, because
1723 * the monitor-uri is defined by the frontend.
1724 */
1725 if (unlikely((s->fe->monitor_uri_len != 0) &&
1726 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1727 !memcmp(&req->data[msg->sl.rq.u],
1728 s->fe->monitor_uri,
1729 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001730 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001731 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001732 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001733 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001734
Willy Tarreau59234e92008-11-30 23:51:27 +01001735 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001736
Willy Tarreau59234e92008-11-30 23:51:27 +01001737 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02001738 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
1739 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001740
Willy Tarreau59234e92008-11-30 23:51:27 +01001741 ret = acl_pass(ret);
1742 if (cond->pol == ACL_COND_UNLESS)
1743 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001744
Willy Tarreau59234e92008-11-30 23:51:27 +01001745 if (ret) {
1746 /* we fail this request, let's return 503 service unavail */
1747 txn->status = 503;
1748 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
1749 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001750 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001752
Willy Tarreau59234e92008-11-30 23:51:27 +01001753 /* nothing to fail, let's reply normaly */
1754 txn->status = 200;
1755 stream_int_retnclose(req->prod, &http_200_chunk);
1756 goto return_prx_cond;
1757 }
1758
1759 /*
1760 * 3: Maybe we have to copy the original REQURI for the logs ?
1761 * Note: we cannot log anymore if the request has been
1762 * classified as invalid.
1763 */
1764 if (unlikely(s->logs.logwait & LW_REQ)) {
1765 /* we have a complete HTTP request that we must log */
1766 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1767 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768
Willy Tarreau59234e92008-11-30 23:51:27 +01001769 if (urilen >= REQURI_LEN)
1770 urilen = REQURI_LEN - 1;
1771 memcpy(txn->uri, &req->data[msg->som], urilen);
1772 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773
Willy Tarreau59234e92008-11-30 23:51:27 +01001774 if (!(s->logs.logwait &= ~LW_REQ))
1775 s->do_log(s);
1776 } else {
1777 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001778 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001779 }
Willy Tarreau06619262006-12-17 08:37:22 +01001780
Willy Tarreau59234e92008-11-30 23:51:27 +01001781 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001782 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
1783 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001784
Willy Tarreau59234e92008-11-30 23:51:27 +01001785 /* 5: we may need to capture headers */
1786 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
1787 capture_headers(req->data + msg->som, &txn->hdr_idx,
1788 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001789
Willy Tarreaud787e662009-07-07 10:14:51 +02001790 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001791 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001792 req->analyse_exp = TICK_ETERNITY;
1793 return 1;
1794
1795 return_bad_req:
1796 /* We centralize bad requests processing here */
1797 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1798 /* we detected a parsing error. We want to archive this request
1799 * in the dedicated proxy area for later troubleshooting.
1800 */
1801 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
1802 }
1803
1804 txn->req.msg_state = HTTP_MSG_ERROR;
1805 txn->status = 400;
1806 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1807 s->fe->failed_req++;
1808
1809 return_prx_cond:
1810 if (!(s->flags & SN_ERR_MASK))
1811 s->flags |= SN_ERR_PRXCOND;
1812 if (!(s->flags & SN_FINST_MASK))
1813 s->flags |= SN_FINST_R;
1814
1815 req->analysers = 0;
1816 req->analyse_exp = TICK_ETERNITY;
1817 return 0;
1818}
1819
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001820/* This stream analyser runs all HTTP request processing which is common to
1821 * frontends and backends, which means blocking ACLs, filters, connection-close,
1822 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02001823 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001824 * either needs more data or wants to immediately abort the request (eg: deny,
1825 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02001826 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001827int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02001828{
Willy Tarreaud787e662009-07-07 10:14:51 +02001829 struct http_txn *txn = &s->txn;
1830 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001831 struct acl_cond *cond;
1832 struct redirect_rule *rule;
1833 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02001834
Willy Tarreau51aecc72009-07-12 09:47:04 +02001835 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
1836 /* we need more data */
1837 buffer_write_dis(req);
1838 return 0;
1839 }
1840
Willy Tarreau3a816292009-07-07 10:55:49 +02001841 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001842 req->analyse_exp = TICK_ETERNITY;
1843
1844 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1845 now_ms, __FUNCTION__,
1846 s,
1847 req,
1848 req->rex, req->wex,
1849 req->flags,
1850 req->l,
1851 req->analysers);
1852
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001853 /* first check whether we have some ACLs set to block this request */
1854 list_for_each_entry(cond, &px->block_cond, list) {
1855 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02001856
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001857 ret = acl_pass(ret);
1858 if (cond->pol == ACL_COND_UNLESS)
1859 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001860
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001861 if (ret) {
1862 txn->status = 403;
1863 /* let's log the request time */
1864 s->logs.tv_request = now;
1865 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
1866 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01001867 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001868 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001869
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001870 /* try headers filters */
1871 if (px->req_exp != NULL) {
1872 if (apply_filters_to_request(s, req, px->req_exp) < 0)
1873 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01001874
Willy Tarreau59234e92008-11-30 23:51:27 +01001875 /* has the request been denied ? */
1876 if (txn->flags & TX_CLDENY) {
1877 /* no need to go further */
1878 txn->status = 403;
1879 /* let's log the request time */
1880 s->logs.tv_request = now;
1881 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
1882 goto return_prx_cond;
1883 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001884 }
Willy Tarreau06619262006-12-17 08:37:22 +01001885
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001886 /* We might have to check for "Connection:" */
1887 if (((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
1888 !(s->flags & SN_CONN_CLOSED)) {
1889 char *cur_ptr, *cur_end, *cur_next;
1890 int old_idx, delta, val;
1891 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001893 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
1894 old_idx = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001895
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001896 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
1897 cur_hdr = &txn->hdr_idx.v[cur_idx];
1898 cur_ptr = cur_next;
1899 cur_end = cur_ptr + cur_hdr->len;
1900 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01001901
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001902 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
1903 if (val) {
1904 /* 3 possibilities :
1905 * - we have already set Connection: close,
1906 * so we remove this line.
1907 * - we have not yet set Connection: close,
1908 * but this line indicates close. We leave
1909 * it untouched and set the flag.
1910 * - we have not yet set Connection: close,
1911 * and this line indicates non-close. We
1912 * replace it.
1913 */
1914 if (s->flags & SN_CONN_CLOSED) {
1915 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
1916 txn->req.eoh += delta;
1917 cur_next += delta;
1918 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
1919 txn->hdr_idx.used--;
1920 cur_hdr->len = 0;
1921 } else {
1922 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
1923 delta = buffer_replace2(req, cur_ptr + val, cur_end,
1924 "close", 5);
Willy Tarreau59234e92008-11-30 23:51:27 +01001925 cur_next += delta;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001926 cur_hdr->len += delta;
1927 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001928 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001929 s->flags |= SN_CONN_CLOSED;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001930 }
Willy Tarreau06619262006-12-17 08:37:22 +01001931 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001932 old_idx = cur_idx;
Willy Tarreau59234e92008-11-30 23:51:27 +01001933 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001934 }
1935 /* add request headers from the rule sets in the same order */
1936 for (cur_idx = 0; cur_idx < px->nb_reqadd; cur_idx++) {
1937 if (unlikely(http_header_add_tail(req,
1938 &txn->req,
1939 &txn->hdr_idx,
1940 px->req_add[cur_idx])) < 0)
1941 goto return_bad_req;
1942 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001943
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001944 /* check if stats URI was requested, and if an auth is needed */
1945 if (px->uri_auth != NULL &&
1946 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
1947 /* we have to check the URI and auth for this request.
1948 * FIXME!!! that one is rather dangerous, we want to
1949 * make it follow standard rules (eg: clear req->analysers).
1950 */
1951 if (stats_check_uri_auth(s, px)) {
1952 req->analysers = 0;
1953 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001954 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001955 }
Willy Tarreaub2513902006-12-17 14:52:38 +01001956
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001957 /* check whether we have some ACLs set to redirect this request */
1958 list_for_each_entry(rule, &px->redirect_rules, list) {
1959 int ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreau06b917c2009-07-06 16:34:52 +02001960
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001961 ret = acl_pass(ret);
1962 if (rule->cond->pol == ACL_COND_UNLESS)
1963 ret = !ret;
Willy Tarreau06b917c2009-07-06 16:34:52 +02001964
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001965 if (ret) {
1966 struct chunk rdr = { trash, 0 };
1967 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02001968
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001969 /* build redirect message */
1970 switch(rule->code) {
1971 case 303:
1972 rdr.len = strlen(HTTP_303);
1973 msg_fmt = HTTP_303;
1974 break;
1975 case 301:
1976 rdr.len = strlen(HTTP_301);
1977 msg_fmt = HTTP_301;
1978 break;
1979 case 302:
1980 default:
1981 rdr.len = strlen(HTTP_302);
1982 msg_fmt = HTTP_302;
1983 break;
1984 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02001985
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001986 if (unlikely(rdr.len > sizeof(trash)))
1987 goto return_bad_req;
1988 memcpy(rdr.str, msg_fmt, rdr.len);
Willy Tarreau06b917c2009-07-06 16:34:52 +02001989
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001990 switch(rule->type) {
1991 case REDIRECT_TYPE_PREFIX: {
1992 const char *path;
1993 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02001994
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001995 path = http_get_path(txn);
1996 /* build message using path */
1997 if (path) {
1998 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
1999 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2000 int qs = 0;
2001 while (qs < pathlen) {
2002 if (path[qs] == '?') {
2003 pathlen = qs;
2004 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002005 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002006 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002007 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002008 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002009 } else {
2010 path = "/";
2011 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002012 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002013
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002014 if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4)
2015 goto return_bad_req;
2016
2017 /* add prefix. Note that if prefix == "/", we don't want to
2018 * add anything, otherwise it makes it hard for the user to
2019 * configure a self-redirection.
2020 */
2021 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002022 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2023 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002024 }
2025
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002026 /* add path */
2027 memcpy(rdr.str + rdr.len, path, pathlen);
2028 rdr.len += pathlen;
2029 break;
2030 }
2031 case REDIRECT_TYPE_LOCATION:
2032 default:
2033 if (rdr.len + rule->rdr_len > sizeof(trash) - 4)
2034 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002035
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002036 /* add location */
2037 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2038 rdr.len += rule->rdr_len;
2039 break;
2040 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002041
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002042 if (rule->cookie_len) {
2043 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2044 rdr.len += 14;
2045 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2046 rdr.len += rule->cookie_len;
2047 memcpy(rdr.str + rdr.len, "\r\n", 2);
2048 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002049 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002050
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002051 /* add end of headers */
2052 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2053 rdr.len += 4;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002054
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002055 txn->status = rule->code;
2056 /* let's log the request time */
2057 s->logs.tv_request = now;
2058 stream_int_retnclose(req->prod, &rdr);
2059 goto return_prx_cond;
2060 }
2061 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002062
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002063 /* that's OK for us now, let's move on to next analysers */
2064 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002065
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002066 return_bad_req:
2067 /* We centralize bad requests processing here */
2068 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2069 /* we detected a parsing error. We want to archive this request
2070 * in the dedicated proxy area for later troubleshooting.
2071 */
2072 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2073 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002074
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002075 txn->req.msg_state = HTTP_MSG_ERROR;
2076 txn->status = 400;
2077 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2078 s->fe->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002079
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002080 return_prx_cond:
2081 if (!(s->flags & SN_ERR_MASK))
2082 s->flags |= SN_ERR_PRXCOND;
2083 if (!(s->flags & SN_FINST_MASK))
2084 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002085
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002086 req->analysers = 0;
2087 req->analyse_exp = TICK_ETERNITY;
2088 return 0;
2089}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002090
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002091/* This function performs all the processing enabled for the current request.
2092 * It returns 1 if the processing can continue on next analysers, or zero if it
2093 * needs more data, encounters an error, or wants to immediately abort the
2094 * request. It relies on buffers flags, and updates s->req->analysers.
2095 */
2096int http_process_request(struct session *s, struct buffer *req, int an_bit)
2097{
2098 struct http_txn *txn = &s->txn;
2099 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002100
Willy Tarreau51aecc72009-07-12 09:47:04 +02002101 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2102 /* we need more data */
2103 buffer_write_dis(req);
2104 return 0;
2105 }
2106
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002107 req->analysers &= ~an_bit;
2108 req->analyse_exp = TICK_ETERNITY;
2109
2110 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2111 now_ms, __FUNCTION__,
2112 s,
2113 req,
2114 req->rex, req->wex,
2115 req->flags,
2116 req->l,
2117 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002118
Willy Tarreau59234e92008-11-30 23:51:27 +01002119 /*
2120 * Right now, we know that we have processed the entire headers
2121 * and that unwanted requests have been filtered out. We can do
2122 * whatever we want with the remaining request. Also, now we
2123 * may have separate values for ->fe, ->be.
2124 */
Willy Tarreau06619262006-12-17 08:37:22 +01002125
Willy Tarreau59234e92008-11-30 23:51:27 +01002126 /*
2127 * If HTTP PROXY is set we simply get remote server address
2128 * parsing incoming request.
2129 */
2130 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2131 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2132 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002133
Willy Tarreau59234e92008-11-30 23:51:27 +01002134 /*
2135 * 7: the appsession cookie was looked up very early in 1.2,
2136 * so let's do the same now.
2137 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002138
Willy Tarreau59234e92008-11-30 23:51:27 +01002139 /* It needs to look into the URI */
2140 if (s->be->appsession_name) {
2141 get_srv_from_appsession(s, &req->data[msg->som], msg->sl.rq.l);
2142 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01002143
Willy Tarreau59234e92008-11-30 23:51:27 +01002144 /*
2145 * 8: Now we can work with the cookies.
2146 * Note that doing so might move headers in the request, but
2147 * the fields will stay coherent and the URI will not move.
2148 * This should only be performed in the backend.
2149 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002150 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002151 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2152 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002153
Willy Tarreau59234e92008-11-30 23:51:27 +01002154 /*
2155 * 9: add X-Forwarded-For if either the frontend or the backend
2156 * asks for it.
2157 */
2158 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2159 if (s->cli_addr.ss_family == AF_INET) {
2160 /* Add an X-Forwarded-For header unless the source IP is
2161 * in the 'except' network range.
2162 */
2163 if ((!s->fe->except_mask.s_addr ||
2164 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2165 != s->fe->except_net.s_addr) &&
2166 (!s->be->except_mask.s_addr ||
2167 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2168 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002169 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002170 unsigned char *pn;
2171 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002172
2173 /* Note: we rely on the backend to get the header name to be used for
2174 * x-forwarded-for, because the header is really meant for the backends.
2175 * However, if the backend did not specify any option, we have to rely
2176 * on the frontend's header name.
2177 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002178 if (s->be->fwdfor_hdr_len) {
2179 len = s->be->fwdfor_hdr_len;
2180 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002181 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002182 len = s->fe->fwdfor_hdr_len;
2183 memcpy(trash, s->fe->fwdfor_hdr_name, len);
2184 }
2185 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002186
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002187 if (unlikely(http_header_add_tail2(req, &txn->req,
2188 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002189 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002190 }
2191 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002192 else if (s->cli_addr.ss_family == AF_INET6) {
2193 /* FIXME: for the sake of completeness, we should also support
2194 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002195 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002196 int len;
2197 char pn[INET6_ADDRSTRLEN];
2198 inet_ntop(AF_INET6,
2199 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2200 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002201
Willy Tarreau59234e92008-11-30 23:51:27 +01002202 /* Note: we rely on the backend to get the header name to be used for
2203 * x-forwarded-for, because the header is really meant for the backends.
2204 * However, if the backend did not specify any option, we have to rely
2205 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002206 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002207 if (s->be->fwdfor_hdr_len) {
2208 len = s->be->fwdfor_hdr_len;
2209 memcpy(trash, s->be->fwdfor_hdr_name, len);
2210 } else {
2211 len = s->fe->fwdfor_hdr_len;
2212 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002213 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002214 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002215
Willy Tarreau59234e92008-11-30 23:51:27 +01002216 if (unlikely(http_header_add_tail2(req, &txn->req,
2217 &txn->hdr_idx, trash, len)) < 0)
2218 goto return_bad_req;
2219 }
2220 }
2221
2222 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002223 * 10: add X-Original-To if either the frontend or the backend
2224 * asks for it.
2225 */
2226 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
2227
2228 /* FIXME: don't know if IPv6 can handle that case too. */
2229 if (s->cli_addr.ss_family == AF_INET) {
2230 /* Add an X-Original-To header unless the destination IP is
2231 * in the 'except' network range.
2232 */
2233 if (!(s->flags & SN_FRT_ADDR_SET))
2234 get_frt_addr(s);
2235
2236 if ((!s->fe->except_mask_to.s_addr ||
2237 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
2238 != s->fe->except_to.s_addr) &&
2239 (!s->be->except_mask_to.s_addr ||
2240 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
2241 != s->be->except_to.s_addr)) {
2242 int len;
2243 unsigned char *pn;
2244 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
2245
2246 /* Note: we rely on the backend to get the header name to be used for
2247 * x-original-to, because the header is really meant for the backends.
2248 * However, if the backend did not specify any option, we have to rely
2249 * on the frontend's header name.
2250 */
2251 if (s->be->orgto_hdr_len) {
2252 len = s->be->orgto_hdr_len;
2253 memcpy(trash, s->be->orgto_hdr_name, len);
2254 } else {
2255 len = s->fe->orgto_hdr_len;
2256 memcpy(trash, s->fe->orgto_hdr_name, len);
2257 }
2258 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
2259
2260 if (unlikely(http_header_add_tail2(req, &txn->req,
2261 &txn->hdr_idx, trash, len)) < 0)
2262 goto return_bad_req;
2263 }
2264 }
2265 }
2266
2267 /*
2268 * 11: add "Connection: close" if needed and not yet set.
Willy Tarreau59234e92008-11-30 23:51:27 +01002269 * Note that we do not need to add it in case of HTTP/1.0.
2270 */
2271 if (!(s->flags & SN_CONN_CLOSED) &&
2272 ((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2273 if ((unlikely(msg->sl.rq.v_l != 8) ||
2274 unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) &&
2275 unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
2276 "Connection: close", 17)) < 0)
2277 goto return_bad_req;
2278 s->flags |= SN_CONN_CLOSED;
2279 }
2280 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2281 * If not assigned, perhaps we are balancing on url_param, but this is a
2282 * POST; and the parameters are in the body, maybe scan there to find our server.
2283 * (unless headers overflowed the buffer?)
2284 */
2285 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2286 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
2287 s->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
2288 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2289 /* are there enough bytes here? total == l || r || rlim ?
2290 * len is unsigned, but eoh is int,
2291 * how many bytes of body have we received?
2292 * eoh is the first empty line of the header
2293 */
2294 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
2295 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
2296
2297 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2298 * We can't assume responsibility for the server's decision,
2299 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2300 * We also can't change our mind later, about which server to choose, so round robin.
2301 */
2302 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2303 struct hdr_ctx ctx;
2304 ctx.idx = 0;
2305 /* Expect is allowed in 1.1, look for it */
2306 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2307 if (ctx.idx != 0 &&
2308 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
2309 /* We can't reliablly stall and wait for data, because of
2310 * .NET clients that don't conform to rfc2616; so, no need for
2311 * the next block to check length expectations.
2312 * We could send 100 status back to the client, but then we need to
2313 * re-write headers, and send the message. And this isn't the right
2314 * place for that action.
2315 * TODO: support Expect elsewhere and delete this block.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002316 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002317 goto end_check_maybe_wait_for_body;
2318 }
2319
2320 if (likely(len > s->be->url_param_post_limit)) {
2321 /* nothing to do, we got enough */
2322 } else {
2323 /* limit implies we are supposed to need this many bytes
2324 * to find the parameter. Let's see how many bytes we can wait for.
2325 */
2326 long long hint = len;
2327 struct hdr_ctx ctx;
2328 ctx.idx = 0;
2329 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx);
2330 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2331 buffer_write_dis(req);
2332 req->analysers |= AN_REQ_HTTP_BODY;
2333 }
2334 else {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002335 ctx.idx = 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002336 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx);
2337 /* now if we have a length, we'll take the hint */
2338 if (ctx.idx) {
2339 /* We have Content-Length */
2340 if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint))
2341 hint = 0; /* parse failure, untrusted client */
2342 else {
2343 if (hint > 0)
2344 msg->hdr_content_len = hint;
2345 else
2346 hint = 0; /* bad client, sent negative length */
2347 }
2348 }
2349 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
2350 if (s->be->url_param_post_limit < hint)
2351 hint = s->be->url_param_post_limit;
2352 /* now do we really need to buffer more data? */
2353 if (len < hint) {
Willy Tarreau3da77c52008-08-29 09:58:42 +02002354 buffer_write_dis(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002355 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002356 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 /* else... There are no body bytes to wait for */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002358 }
2359 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 }
2361 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002362
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 /*************************************************************
2364 * OK, that's finished for the headers. We have done what we *
2365 * could. Let's switch to the DATA state. *
2366 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002367
Willy Tarreau59234e92008-11-30 23:51:27 +01002368 buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
2369 s->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002370
Willy Tarreau59234e92008-11-30 23:51:27 +01002371 /* When a connection is tarpitted, we use the tarpit timeout,
2372 * which may be the same as the connect timeout if unspecified.
2373 * If unset, then set it to zero because we really want it to
2374 * eventually expire. We build the tarpit as an analyser.
2375 */
2376 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau6f0aa472009-03-08 20:33:29 +01002377 buffer_erase(s->req);
2378 /* wipe the request out so that we can drop the connection early
Willy Tarreau59234e92008-11-30 23:51:27 +01002379 * if the client closes first.
Willy Tarreau2a324282006-12-05 00:05:46 +01002380 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 buffer_write_dis(req);
2382 req->analysers |= AN_REQ_HTTP_TARPIT;
2383 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2384 if (!req->analyse_exp)
Willy Tarreau2ab85e62009-03-29 10:24:15 +02002385 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau59234e92008-11-30 23:51:27 +01002386 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002387
Willy Tarreau59234e92008-11-30 23:51:27 +01002388 /* OK let's go on with the BODY now */
2389 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002390
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02002392 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002393 /* we detected a parsing error. We want to archive this request
2394 * in the dedicated proxy area for later troubleshooting.
2395 */
Willy Tarreau4076a152009-04-02 15:18:36 +02002396 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01002397 }
Willy Tarreau4076a152009-04-02 15:18:36 +02002398
Willy Tarreau59234e92008-11-30 23:51:27 +01002399 txn->req.msg_state = HTTP_MSG_ERROR;
2400 txn->status = 400;
2401 req->analysers = 0;
2402 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2403 s->fe->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002404
Willy Tarreau59234e92008-11-30 23:51:27 +01002405 if (!(s->flags & SN_ERR_MASK))
2406 s->flags |= SN_ERR_PRXCOND;
2407 if (!(s->flags & SN_FINST_MASK))
2408 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002409 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002410}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002411
Willy Tarreau60b85b02008-11-30 23:28:40 +01002412/* This function is an analyser which processes the HTTP tarpit. It always
2413 * returns zero, at the beginning because it prevents any other processing
2414 * from occurring, and at the end because it terminates the request.
2415 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002416int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01002417{
2418 struct http_txn *txn = &s->txn;
2419
2420 /* This connection is being tarpitted. The CLIENT side has
2421 * already set the connect expiration date to the right
2422 * timeout. We just have to check that the client is still
2423 * there and that the timeout has not expired.
2424 */
2425 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
2426 !tick_is_expired(req->analyse_exp, now_ms))
2427 return 0;
2428
2429 /* We will set the queue timer to the time spent, just for
2430 * logging purposes. We fake a 500 server error, so that the
2431 * attacker will not suspect his connection has been tarpitted.
2432 * It will not cause trouble to the logs because we can exclude
2433 * the tarpitted connections by filtering on the 'PT' status flags.
2434 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01002435 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2436
2437 txn->status = 500;
2438 if (req->flags != BF_READ_ERROR)
2439 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
2440
2441 req->analysers = 0;
2442 req->analyse_exp = TICK_ETERNITY;
2443
2444 s->fe->failed_req++;
2445 if (!(s->flags & SN_ERR_MASK))
2446 s->flags |= SN_ERR_PRXCOND;
2447 if (!(s->flags & SN_FINST_MASK))
2448 s->flags |= SN_FINST_T;
2449 return 0;
2450}
2451
Willy Tarreaud34af782008-11-30 23:36:37 +01002452/* This function is an analyser which processes the HTTP request body. It looks
2453 * for parameters to be used for the load balancing algorithm (url_param). It
2454 * must only be called after the standard HTTP request processing has occurred,
2455 * because it expects the request to be parsed. It returns zero if it needs to
2456 * read more data, or 1 once it has completed its analysis.
2457 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002458int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01002459{
2460 struct http_msg *msg = &s->txn.req;
2461 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2462 long long limit = s->be->url_param_post_limit;
2463 struct hdr_ctx ctx;
2464
Willy Tarreau51aecc72009-07-12 09:47:04 +02002465 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
2466 /* we need more data */
2467 buffer_write_dis(req);
2468 return 0;
2469 }
2470
Willy Tarreaud34af782008-11-30 23:36:37 +01002471 /* We have to parse the HTTP request body to find any required data.
2472 * "balance url_param check_post" should have been the only way to get
2473 * into this. We were brought here after HTTP header analysis, so all
2474 * related structures are ready.
2475 */
2476
2477 ctx.idx = 0;
2478
2479 /* now if we have a length, we'll take the hint */
2480 http_find_header2("Transfer-Encoding", 17, msg->sol, &s->txn.hdr_idx, &ctx);
2481 if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) {
2482 unsigned int chunk = 0;
2483 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2484 char c = msg->sol[body];
2485 if (ishex(c)) {
2486 unsigned int hex = toupper(c) - '0';
2487 if (hex > 9)
2488 hex -= 'A' - '9' - 1;
2489 chunk = (chunk << 4) | hex;
2490 } else
2491 break;
2492 body++;
2493 }
2494 if (body + 2 >= req->l) /* we want CRLF too */
2495 goto http_body_end; /* end of buffer? data missing! */
2496
2497 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
2498 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
2499
2500 body += 2; // skip CRLF
2501
2502 /* if we support more then one chunk here, we have to do it again when assigning server
2503 * 1. how much entity data do we have? new var
2504 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2505 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2506 */
2507
2508 if (chunk < limit)
2509 limit = chunk; /* only reading one chunk */
2510 } else {
2511 if (msg->hdr_content_len < limit)
2512 limit = msg->hdr_content_len;
2513 }
2514
2515 http_body_end:
2516 /* we leave once we know we have nothing left to do. This means that we have
2517 * enough bytes, or that we know we'll not get any more (buffer full, read
2518 * buffer closed).
2519 */
2520 if (req->l - body >= limit || /* enough bytes! */
2521 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
2522 tick_is_expired(req->analyse_exp, now_ms)) {
2523 /* The situation will not evolve, so let's give up on the analysis. */
2524 s->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau3a816292009-07-07 10:55:49 +02002525 req->analysers &= ~an_bit;
Willy Tarreaud34af782008-11-30 23:36:37 +01002526 req->analyse_exp = TICK_ETERNITY;
2527 return 1;
2528 }
2529 else {
2530 /* Not enough data. We'll re-use the http-request
2531 * timeout here. Ideally, we should set the timeout
2532 * relative to the accept() date. We just set the
2533 * request timeout once at the beginning of the
2534 * request.
2535 */
2536 buffer_write_dis(req);
2537 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002538 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01002539 return 0;
2540 }
2541}
2542
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002543/* This function performs all the processing enabled for the current response.
Willy Tarreaudafde432008-08-17 01:00:46 +02002544 * It normally returns zero, but may return 1 if it absolutely needs to be
2545 * called again after other functions. It relies on buffers flags, and updates
Willy Tarreau2df28e82008-08-17 15:20:19 +02002546 * t->rep->analysers. It might make sense to explode it into several other
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002547 * functions. It works like process_request (see indications above).
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002548 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002549int process_response(struct session *t)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002550{
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002551 struct http_txn *txn = &t->txn;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002552 struct buffer *req = t->req;
2553 struct buffer *rep = t->rep;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002554
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002555 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 +02002556 now_ms, __FUNCTION__,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002557 t,
2558 rep,
2559 rep->rex, rep->wex,
2560 rep->flags,
2561 rep->l,
2562 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002563
Willy Tarreau2df28e82008-08-17 15:20:19 +02002564 if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002565 /*
2566 * Now parse the partial (or complete) lines.
2567 * We will check the response syntax, and also join multi-line
2568 * headers. An index of all the lines will be elaborated while
2569 * parsing.
2570 *
2571 * For the parsing, we use a 28 states FSM.
2572 *
2573 * Here is the information we currently have :
Willy Tarreaue393fe22008-08-16 22:18:07 +02002574 * rep->data + rep->som = beginning of response
2575 * rep->data + rep->eoh = end of processed headers / start of current one
2576 * rep->data + rep->eol = end of current header or line (LF or CRLF)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002577 * rep->lr = first non-visited byte
2578 * rep->r = end of data
2579 */
Willy Tarreau7f875f62008-08-11 17:35:01 +02002580
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002581 int cur_idx;
2582 struct http_msg *msg = &txn->rsp;
2583 struct proxy *cur_proxy;
2584
2585 if (likely(rep->lr < rep->r))
2586 http_msg_analyzer(rep, msg, &txn->hdr_idx);
2587
2588 /* 1: we might have to print this header in debug mode */
2589 if (unlikely((global.mode & MODE_DEBUG) &&
2590 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
2591 (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
2592 char *eol, *sol;
2593
2594 sol = rep->data + msg->som;
2595 eol = sol + msg->sl.rq.l;
2596 debug_hdr("srvrep", t, sol, eol);
2597
2598 sol += hdr_idx_first_pos(&txn->hdr_idx);
2599 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
2600
2601 while (cur_idx) {
2602 eol = sol + txn->hdr_idx.v[cur_idx].len;
2603 debug_hdr("srvhdr", t, sol, eol);
2604 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2605 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002606 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002607 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002608
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002609 /*
2610 * Now we quickly check if we have found a full valid response.
2611 * If not so, we check the FD and buffer states before leaving.
2612 * A full response is indicated by the fact that we have seen
2613 * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid
2614 * responses are checked first.
2615 *
2616 * Depending on whether the client is still there or not, we
2617 * may send an error response back or not. Note that normally
2618 * we should only check for HTTP status there, and check I/O
2619 * errors somewhere else.
2620 */
2621
2622 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002623 /* Invalid response */
2624 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002625 /* we detected a parsing error. We want to archive this response
2626 * in the dedicated proxy area for later troubleshooting.
2627 */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002628 hdr_response_bad:
Willy Tarreau4076a152009-04-02 15:18:36 +02002629 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
2630 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
2631
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002632 buffer_shutr_now(rep);
2633 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002634 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002635 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002636 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002637 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002638 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002639 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002640 if (!(t->flags & SN_ERR_MASK))
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002641 t->flags |= SN_ERR_PRXCOND;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002642 if (!(t->flags & SN_FINST_MASK))
2643 t->flags |= SN_FINST_H;
Willy Tarreau461f6622008-08-15 23:43:19 +02002644
Willy Tarreaudafde432008-08-17 01:00:46 +02002645 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002646 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002647 /* too large response does not fit in buffer. */
2648 else if (rep->flags & BF_FULL) {
2649 goto hdr_response_bad;
2650 }
2651 /* read error */
2652 else if (rep->flags & BF_READ_ERROR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002653 if (msg->err_pos >= 0)
2654 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002655 buffer_shutr_now(rep);
2656 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002657 if (t->srv)
2658 t->srv->failed_resp++;
2659 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002660 rep->analysers = 0;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002661 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002662 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002663 if (!(t->flags & SN_ERR_MASK))
2664 t->flags |= SN_ERR_SRVCL;
2665 if (!(t->flags & SN_FINST_MASK))
2666 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002667 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002668 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002669 /* read timeout : return a 504 to the client. */
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002670 else if (rep->flags & BF_READ_TIMEOUT) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002671 if (msg->err_pos >= 0)
2672 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002673 buffer_shutr_now(rep);
2674 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002675 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002676 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002677 t->be->failed_resp++;
Willy Tarreau2df28e82008-08-17 15:20:19 +02002678 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002679 txn->status = 504;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002680 stream_int_return(rep->cons, error_message(t, HTTP_ERR_504));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002681 if (!(t->flags & SN_ERR_MASK))
2682 t->flags |= SN_ERR_SRVTO;
2683 if (!(t->flags & SN_FINST_MASK))
2684 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002685 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002686 }
Willy Tarreau8365f932009-03-15 23:11:49 +01002687 /* close from server */
2688 else if (rep->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002689 if (msg->err_pos >= 0)
2690 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002691 buffer_shutw_now(req);
Willy Tarreau8365f932009-03-15 23:11:49 +01002692 if (t->srv)
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002693 t->srv->failed_resp++;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002694 t->be->failed_resp++;
2695 rep->analysers = 0;
2696 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002697 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002698 if (!(t->flags & SN_ERR_MASK))
2699 t->flags |= SN_ERR_SRVCL;
2700 if (!(t->flags & SN_FINST_MASK))
2701 t->flags |= SN_FINST_H;
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002702 return 0;
2703 }
Willy Tarreau8365f932009-03-15 23:11:49 +01002704 /* write error to client (we don't send any message then) */
2705 else if (rep->flags & BF_WRITE_ERROR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002706 if (msg->err_pos >= 0)
2707 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
Willy Tarreau8365f932009-03-15 23:11:49 +01002708 buffer_shutr_now(rep);
2709 t->be->failed_resp++;
2710 rep->analysers = 0;
2711 if (!(t->flags & SN_ERR_MASK))
2712 t->flags |= SN_ERR_CLICL;
2713 if (!(t->flags & SN_FINST_MASK))
2714 t->flags |= SN_FINST_H;
2715 return 0;
2716 }
Willy Tarreau3da77c52008-08-29 09:58:42 +02002717 buffer_write_dis(rep);
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002718 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002719 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002720
Willy Tarreau21d2af32008-02-14 20:25:24 +01002721
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002722 /*****************************************************************
2723 * More interesting part now : we know that we have a complete *
2724 * response which at least looks like HTTP. We have an indicator *
2725 * of each header's length, so we can parse them quickly. *
2726 ****************************************************************/
Willy Tarreau21d2af32008-02-14 20:25:24 +01002727
Willy Tarreau4076a152009-04-02 15:18:36 +02002728 if (msg->err_pos >= 0)
2729 http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe);
2730
Willy Tarreau2df28e82008-08-17 15:20:19 +02002731 rep->analysers &= ~AN_RTR_HTTP_HDR;
Willy Tarreaua7c52762008-08-16 18:40:18 +02002732
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002733 /* ensure we keep this pointer to the beginning of the message */
2734 msg->sol = rep->data + msg->som;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002735
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002736 /*
2737 * 1: get the status code and check for cacheability.
2738 */
Willy Tarreau21d2af32008-02-14 20:25:24 +01002739
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002740 t->logs.logwait &= ~LW_RESP;
2741 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002742
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002743 switch (txn->status) {
2744 case 200:
2745 case 203:
2746 case 206:
2747 case 300:
2748 case 301:
2749 case 410:
2750 /* RFC2616 @13.4:
2751 * "A response received with a status code of
2752 * 200, 203, 206, 300, 301 or 410 MAY be stored
2753 * by a cache (...) unless a cache-control
2754 * directive prohibits caching."
2755 *
2756 * RFC2616 @9.5: POST method :
2757 * "Responses to this method are not cacheable,
2758 * unless the response includes appropriate
2759 * Cache-Control or Expires header fields."
2760 */
2761 if (likely(txn->meth != HTTP_METH_POST) &&
2762 (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
2763 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
2764 break;
2765 default:
2766 break;
2767 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002768
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002769 /*
2770 * 2: we may need to capture headers
2771 */
2772 if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap))
2773 capture_headers(rep->data + msg->som, &txn->hdr_idx,
2774 txn->rsp.cap, t->fe->rsp_cap);
2775
2776 /*
2777 * 3: we will have to evaluate the filters.
2778 * As opposed to version 1.2, now they will be evaluated in the
2779 * filters order and not in the header order. This means that
2780 * each filter has to be validated among all headers.
2781 *
2782 * Filters are tried with ->be first, then with ->fe if it is
2783 * different from ->be.
2784 */
2785
2786 t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
2787
2788 cur_proxy = t->be;
2789 while (1) {
2790 struct proxy *rule_set = cur_proxy;
2791
2792 /* try headers filters */
2793 if (rule_set->rsp_exp != NULL) {
2794 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
2795 return_bad_resp:
Willy Tarreau8365f932009-03-15 23:11:49 +01002796 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002797 t->srv->failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002798 cur_proxy->failed_resp++;
2799 return_srv_prx_502:
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002800 buffer_shutr_now(rep);
2801 buffer_shutw_now(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002802 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002803 txn->status = 502;
Willy Tarreau81acfab2008-11-30 19:22:53 +01002804 stream_int_return(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002805 if (!(t->flags & SN_ERR_MASK))
2806 t->flags |= SN_ERR_PRXCOND;
2807 if (!(t->flags & SN_FINST_MASK))
2808 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02002809 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002810 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002811 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01002812
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002813 /* has the response been denied ? */
2814 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01002815 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002816 t->srv->failed_secu++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002817 cur_proxy->denied_resp++;
2818 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01002819 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002820
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002821 /* We might have to check for "Connection:" */
2822 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2823 !(t->flags & SN_CONN_CLOSED)) {
2824 char *cur_ptr, *cur_end, *cur_next;
2825 int cur_idx, old_idx, delta, val;
2826 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002827
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002828 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
2829 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002830
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002831 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2832 cur_hdr = &txn->hdr_idx.v[cur_idx];
2833 cur_ptr = cur_next;
2834 cur_end = cur_ptr + cur_hdr->len;
2835 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002836
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002837 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2838 if (val) {
2839 /* 3 possibilities :
2840 * - we have already set Connection: close,
2841 * so we remove this line.
2842 * - we have not yet set Connection: close,
2843 * but this line indicates close. We leave
2844 * it untouched and set the flag.
2845 * - we have not yet set Connection: close,
2846 * and this line indicates non-close. We
2847 * replace it.
2848 */
2849 if (t->flags & SN_CONN_CLOSED) {
2850 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
2851 txn->rsp.eoh += delta;
2852 cur_next += delta;
2853 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2854 txn->hdr_idx.used--;
2855 cur_hdr->len = 0;
2856 } else {
2857 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2858 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
2859 "close", 5);
2860 cur_next += delta;
2861 cur_hdr->len += delta;
2862 txn->rsp.eoh += delta;
2863 }
2864 t->flags |= SN_CONN_CLOSED;
2865 }
2866 }
2867 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01002868 }
2869 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002870
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002871 /* add response headers from the rule sets in the same order */
2872 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
2873 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
2874 rule_set->rsp_add[cur_idx])) < 0)
2875 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02002876 }
2877
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002878 /* check whether we're already working on the frontend */
2879 if (cur_proxy == t->fe)
2880 break;
2881 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002882 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002883
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002884 /*
2885 * 4: check for server cookie.
2886 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002887 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002888 || (t->be->options & PR_O_CHK_CACHE))
2889 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002890
Willy Tarreaubaaee002006-06-26 02:48:02 +02002891
Willy Tarreaua15645d2007-03-18 16:22:39 +01002892 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002893 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002894 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002895 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
2896 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002897
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002898 /*
2899 * 6: add server cookie in the response if needed
2900 */
2901 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
2902 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
2903 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002904
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002905 /* the server is known, it's not the one the client requested, we have to
2906 * insert a set-cookie here, except if we want to insert only on POST
2907 * requests and this one isn't. Note that servers which don't have cookies
2908 * (eg: some backup servers) will return a full cookie removal request.
2909 */
2910 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
2911 t->be->cookie_name,
2912 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02002913
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002914 if (t->be->cookie_domain)
2915 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002916
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002917 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2918 trash, len)) < 0)
2919 goto return_bad_resp;
2920 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002921
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002922 /* Here, we will tell an eventual cache on the client side that we don't
2923 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2924 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2925 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2926 */
2927 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02002928
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002929 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2930
2931 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2932 "Cache-control: private", 22)) < 0)
2933 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002934 }
2935 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002936
Willy Tarreaubaaee002006-06-26 02:48:02 +02002937
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002938 /*
2939 * 7: check if result will be cacheable with a cookie.
2940 * We'll block the response if security checks have caught
2941 * nasty things such as a cacheable cookie.
2942 */
2943 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
2944 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
2945 (t->be->options & PR_O_CHK_CACHE)) {
2946
2947 /* we're in presence of a cacheable response containing
2948 * a set-cookie header. We'll block it as requested by
2949 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002950 */
Willy Tarreau8365f932009-03-15 23:11:49 +01002951 if (t->srv)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002952 t->srv->failed_secu++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002953 t->be->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01002954
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002955 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2956 t->be->id, t->srv?t->srv->id:"<dispatch>");
2957 send_log(t->be, LOG_ALERT,
2958 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2959 t->be->id, t->srv?t->srv->id:"<dispatch>");
2960 goto return_srv_prx_502;
2961 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002962
2963 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002964 * 8: add "Connection: close" if needed and not yet set.
2965 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01002966 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002967 if (!(t->flags & SN_CONN_CLOSED) &&
2968 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
2969 if ((unlikely(msg->sl.st.v_l != 8) ||
2970 unlikely(req->data[msg->som + 7] != '0')) &&
2971 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
2972 "Connection: close", 17)) < 0)
2973 goto return_bad_resp;
2974 t->flags |= SN_CONN_CLOSED;
2975 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01002976
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002977 /*************************************************************
2978 * OK, that's finished for the headers. We have done what we *
2979 * could. Let's switch to the DATA state. *
2980 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002981
Willy Tarreaue393fe22008-08-16 22:18:07 +02002982 buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002983 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01002984
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002985#ifdef CONFIG_HAP_TCPSPLICE
2986 if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) {
2987 /* TCP splicing supported by both FE and BE */
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002988 tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002989 }
2990#endif
2991 /* if the user wants to log as soon as possible, without counting
2992 * bytes from the server, then this is the right moment. We have
2993 * to temporarily assign bytes_out to log what we currently have.
2994 */
2995 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
2996 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
2997 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002998 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002999 t->logs.bytes_out = 0;
3000 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003001
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003002 /* Note: we must not try to cheat by jumping directly to DATA,
3003 * otherwise we would not let the client side wake up.
3004 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003005
Willy Tarreaudafde432008-08-17 01:00:46 +02003006 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003007 }
Willy Tarreaudafde432008-08-17 01:00:46 +02003008
Willy Tarreau2df28e82008-08-17 15:20:19 +02003009 /* Note: eventhough nobody should set an unknown flag, clearing them right now will
3010 * probably reduce one day's debugging session.
3011 */
3012#ifdef DEBUG_DEV
3013 if (rep->analysers & ~(AN_RTR_HTTP_HDR)) {
3014 fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n",
3015 __FILE__, __LINE__, rep->analysers);
3016 ABORT_NOW();
3017 }
3018#endif
3019 rep->analysers &= AN_RTR_HTTP_HDR;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003020 return 0;
3021}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003022
Willy Tarreaubaaee002006-06-26 02:48:02 +02003023/*
3024 * Produces data for the session <s> depending on its source. Expects to be
Willy Tarreau1ae3a052008-08-16 10:56:30 +02003025 * called with client socket shut down on input. Right now, only statistics can
Willy Tarreau72b179a2008-08-28 16:01:32 +02003026 * be produced. It stops by itself by unsetting the BF_HIJACK flag from the
3027 * buffer, which it uses to keep on being called when there is free space in
Willy Tarreau01bf8672008-12-07 18:03:29 +01003028 * the buffer, or simply by letting an empty buffer upon return.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003029 */
Willy Tarreau01bf8672008-12-07 18:03:29 +01003030void produce_content(struct session *s, struct buffer *rep)
Willy Tarreaubaaee002006-06-26 02:48:02 +02003031{
Willy Tarreaubaaee002006-06-26 02:48:02 +02003032 if (s->data_source == DATA_SRC_NONE) {
Willy Tarreau01bf8672008-12-07 18:03:29 +01003033 buffer_stop_hijack(rep);
3034 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003035 }
3036 else if (s->data_source == DATA_SRC_STATS) {
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003037 /* dump server statistics */
Willy Tarreau0a464892008-12-07 18:30:00 +01003038 int ret = stats_dump_http(s, rep, s->be->uri_auth);
Willy Tarreau91861262007-10-17 17:06:05 +02003039 if (ret >= 0)
Willy Tarreau01bf8672008-12-07 18:03:29 +01003040 return;
Willy Tarreau91861262007-10-17 17:06:05 +02003041 /* -1 indicates an error */
Willy Tarreauc0dde7a2007-01-01 21:38:07 +01003042 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003043
Willy Tarreau91861262007-10-17 17:06:05 +02003044 /* unknown data source or internal error */
3045 s->txn.status = 500;
Willy Tarreau01bf8672008-12-07 18:03:29 +01003046 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_500));
Willy Tarreau91861262007-10-17 17:06:05 +02003047 if (!(s->flags & SN_ERR_MASK))
3048 s->flags |= SN_ERR_PRXCOND;
3049 if (!(s->flags & SN_FINST_MASK))
3050 s->flags |= SN_FINST_R;
Willy Tarreau01bf8672008-12-07 18:03:29 +01003051 buffer_stop_hijack(rep);
3052 return;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003053}
3054
3055
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003056/* Iterate the same filter through all request headers.
3057 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003058 * Since it can manage the switch to another backend, it updates the per-proxy
3059 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003060 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003061int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003062{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003063 char term;
3064 char *cur_ptr, *cur_end, *cur_next;
3065 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003066 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003067 struct hdr_idx_elem *cur_hdr;
3068 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003069
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003070 last_hdr = 0;
3071
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003072 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003073 old_idx = 0;
3074
3075 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003076 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003077 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003078 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003079 (exp->action == ACT_ALLOW ||
3080 exp->action == ACT_DENY ||
3081 exp->action == ACT_TARPIT))
3082 return 0;
3083
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003084 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003085 if (!cur_idx)
3086 break;
3087
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003088 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003089 cur_ptr = cur_next;
3090 cur_end = cur_ptr + cur_hdr->len;
3091 cur_next = cur_end + cur_hdr->cr + 1;
3092
3093 /* Now we have one header between cur_ptr and cur_end,
3094 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003095 */
3096
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003097 /* The annoying part is that pattern matching needs
3098 * that we modify the contents to null-terminate all
3099 * strings before testing them.
3100 */
3101
3102 term = *cur_end;
3103 *cur_end = '\0';
3104
3105 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3106 switch (exp->action) {
3107 case ACT_SETBE:
3108 /* It is not possible to jump a second time.
3109 * FIXME: should we return an HTTP/500 here so that
3110 * the admin knows there's a problem ?
3111 */
3112 if (t->be != t->fe)
3113 break;
3114
3115 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003116 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003117 last_hdr = 1;
3118 break;
3119
3120 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003121 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003122 last_hdr = 1;
3123 break;
3124
3125 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003126 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003127 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003128 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003129 break;
3130
3131 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003132 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003133 last_hdr = 1;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003134 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003135 break;
3136
3137 case ACT_REPLACE:
3138 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3139 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3140 /* FIXME: if the user adds a newline in the replacement, the
3141 * index will not be recalculated for now, and the new line
3142 * will not be counted as a new header.
3143 */
3144
3145 cur_end += delta;
3146 cur_next += delta;
3147 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003148 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003149 break;
3150
3151 case ACT_REMOVE:
3152 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3153 cur_next += delta;
3154
3155 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003156 txn->req.eoh += delta;
3157 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3158 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003159 cur_hdr->len = 0;
3160 cur_end = NULL; /* null-term has been rewritten */
3161 break;
3162
3163 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003164 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003165 if (cur_end)
3166 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003168 /* keep the link from this header to next one in case of later
3169 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003170 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003171 old_idx = cur_idx;
3172 }
3173 return 0;
3174}
3175
3176
3177/* Apply the filter to the request line.
3178 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3179 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003180 * Since it can manage the switch to another backend, it updates the per-proxy
3181 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003182 */
3183int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3184{
3185 char term;
3186 char *cur_ptr, *cur_end;
3187 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003188 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003189 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003190
Willy Tarreau58f10d72006-12-04 02:26:12 +01003191
Willy Tarreau3d300592007-03-18 18:34:41 +01003192 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003193 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003194 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003195 (exp->action == ACT_ALLOW ||
3196 exp->action == ACT_DENY ||
3197 exp->action == ACT_TARPIT))
3198 return 0;
3199 else if (exp->action == ACT_REMOVE)
3200 return 0;
3201
3202 done = 0;
3203
Willy Tarreau9cdde232007-05-02 20:58:19 +02003204 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003205 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003206
3207 /* Now we have the request line between cur_ptr and cur_end */
3208
3209 /* The annoying part is that pattern matching needs
3210 * that we modify the contents to null-terminate all
3211 * strings before testing them.
3212 */
3213
3214 term = *cur_end;
3215 *cur_end = '\0';
3216
3217 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3218 switch (exp->action) {
3219 case ACT_SETBE:
3220 /* It is not possible to jump a second time.
3221 * FIXME: should we return an HTTP/500 here so that
3222 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003223 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003224 if (t->be != t->fe)
3225 break;
3226
3227 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003228 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003229 done = 1;
3230 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003231
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003232 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003233 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003234 done = 1;
3235 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003236
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003237 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003238 txn->flags |= TX_CLDENY;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003239 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003240 done = 1;
3241 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003242
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003243 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003244 txn->flags |= TX_CLTARPIT;
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003245 t->be->denied_req++;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003246 done = 1;
3247 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003248
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003249 case ACT_REPLACE:
3250 *cur_end = term; /* restore the string terminator */
3251 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3252 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3253 /* FIXME: if the user adds a newline in the replacement, the
3254 * index will not be recalculated for now, and the new line
3255 * will not be counted as a new header.
3256 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003257
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003258 txn->req.eoh += delta;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003259 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003260
Willy Tarreau9cdde232007-05-02 20:58:19 +02003261 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003262 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003263 HTTP_MSG_RQMETH,
3264 cur_ptr, cur_end + 1,
3265 NULL, NULL);
3266 if (unlikely(!cur_end))
3267 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003268
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003269 /* we have a full request and we know that we have either a CR
3270 * or an LF at <ptr>.
3271 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003272 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3273 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003274 /* there is no point trying this regex on headers */
3275 return 1;
3276 }
3277 }
3278 *cur_end = term; /* restore the string terminator */
3279 return done;
3280}
Willy Tarreau97de6242006-12-27 17:18:38 +01003281
Willy Tarreau58f10d72006-12-04 02:26:12 +01003282
Willy Tarreau58f10d72006-12-04 02:26:12 +01003283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003284/*
3285 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3286 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003287 * unparsable request. Since it can manage the switch to another backend, it
3288 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003289 */
3290int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3291{
Willy Tarreau3d300592007-03-18 18:34:41 +01003292 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003293 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003294 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003295 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003296
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003297 /*
3298 * The interleaving of transformations and verdicts
3299 * makes it difficult to decide to continue or stop
3300 * the evaluation.
3301 */
3302
Willy Tarreau3d300592007-03-18 18:34:41 +01003303 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003304 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3305 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3306 exp = exp->next;
3307 continue;
3308 }
3309
3310 /* Apply the filter to the request line. */
3311 ret = apply_filter_to_req_line(t, req, exp);
3312 if (unlikely(ret < 0))
3313 return -1;
3314
3315 if (likely(ret == 0)) {
3316 /* The filter did not match the request, it can be
3317 * iterated through all headers.
3318 */
3319 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003320 }
3321 exp = exp->next;
3322 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003323 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003324}
3325
3326
Willy Tarreaua15645d2007-03-18 16:22:39 +01003327
Willy Tarreau58f10d72006-12-04 02:26:12 +01003328/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003329 * Manage client-side cookie. It can impact performance by about 2% so it is
3330 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003331 */
3332void manage_client_side_cookies(struct session *t, struct buffer *req)
3333{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003334 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003335 char *p1, *p2, *p3, *p4;
3336 char *del_colon, *del_cookie, *colon;
3337 int app_cookies;
3338
3339 appsess *asession_temp = NULL;
3340 appsess local_asession;
3341
3342 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003343 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003344
Willy Tarreau2a324282006-12-05 00:05:46 +01003345 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003346 * we start with the start line.
3347 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003348 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003349 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003350
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003351 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003352 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003353 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003354
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003355 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003356 cur_ptr = cur_next;
3357 cur_end = cur_ptr + cur_hdr->len;
3358 cur_next = cur_end + cur_hdr->cr + 1;
3359
3360 /* We have one full header between cur_ptr and cur_end, and the
3361 * next header starts at cur_next. We're only interested in
3362 * "Cookie:" headers.
3363 */
3364
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003365 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3366 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003367 old_idx = cur_idx;
3368 continue;
3369 }
3370
3371 /* Now look for cookies. Conforming to RFC2109, we have to support
3372 * attributes whose name begin with a '$', and associate them with
3373 * the right cookie, if we want to delete this cookie.
3374 * So there are 3 cases for each cookie read :
3375 * 1) it's a special attribute, beginning with a '$' : ignore it.
3376 * 2) it's a server id cookie that we *MAY* want to delete : save
3377 * some pointers on it (last semi-colon, beginning of cookie...)
3378 * 3) it's an application cookie : we *MAY* have to delete a previous
3379 * "special" cookie.
3380 * At the end of loop, if a "special" cookie remains, we may have to
3381 * remove it. If no application cookie persists in the header, we
3382 * *MUST* delete it
3383 */
3384
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003385 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003386
Willy Tarreau58f10d72006-12-04 02:26:12 +01003387 /* del_cookie == NULL => nothing to be deleted */
3388 del_colon = del_cookie = NULL;
3389 app_cookies = 0;
3390
3391 while (p1 < cur_end) {
3392 /* skip spaces and colons, but keep an eye on these ones */
3393 while (p1 < cur_end) {
3394 if (*p1 == ';' || *p1 == ',')
3395 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003396 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003397 break;
3398 p1++;
3399 }
3400
3401 if (p1 == cur_end)
3402 break;
3403
3404 /* p1 is at the beginning of the cookie name */
3405 p2 = p1;
3406 while (p2 < cur_end && *p2 != '=')
3407 p2++;
3408
3409 if (p2 == cur_end)
3410 break;
3411
3412 p3 = p2 + 1; /* skips the '=' sign */
3413 if (p3 == cur_end)
3414 break;
3415
3416 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003417 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003418 p4++;
3419
3420 /* here, we have the cookie name between p1 and p2,
3421 * and its value between p3 and p4.
3422 * we can process it :
3423 *
3424 * Cookie: NAME=VALUE;
3425 * | || || |
3426 * | || || +--> p4
3427 * | || |+-------> p3
3428 * | || +--------> p2
3429 * | |+------------> p1
3430 * | +-------------> colon
3431 * +--------------------> cur_ptr
3432 */
3433
3434 if (*p1 == '$') {
3435 /* skip this one */
3436 }
3437 else {
3438 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003439 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003440 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003441 (p4 - p1 >= t->fe->capture_namelen) &&
3442 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003443 int log_len = p4 - p1;
3444
Willy Tarreau086b3b42007-05-13 21:45:51 +02003445 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003446 Alert("HTTP logging : out of memory.\n");
3447 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003448 if (log_len > t->fe->capture_len)
3449 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003450 memcpy(txn->cli_cookie, p1, log_len);
3451 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003452 }
3453 }
3454
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003455 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3456 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003457 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003458 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003459 char *delim;
3460
3461 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3462 * have the server ID betweek p3 and delim, and the original cookie between
3463 * delim+1 and p4. Otherwise, delim==p4 :
3464 *
3465 * Cookie: NAME=SRV~VALUE;
3466 * | || || | |
3467 * | || || | +--> p4
3468 * | || || +--------> delim
3469 * | || |+-----------> p3
3470 * | || +------------> p2
3471 * | |+----------------> p1
3472 * | +-----------------> colon
3473 * +------------------------> cur_ptr
3474 */
3475
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003476 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003477 for (delim = p3; delim < p4; delim++)
3478 if (*delim == COOKIE_DELIM)
3479 break;
3480 }
3481 else
3482 delim = p4;
3483
3484
3485 /* Here, we'll look for the first running server which supports the cookie.
3486 * This allows to share a same cookie between several servers, for example
3487 * to dedicate backup servers to specific servers only.
3488 * However, to prevent clients from sticking to cookie-less backup server
3489 * when they have incidentely learned an empty cookie, we simply ignore
3490 * empty cookies and mark them as invalid.
3491 */
3492 if (delim == p3)
3493 srv = NULL;
3494
3495 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01003496 if (srv->cookie && (srv->cklen == delim - p3) &&
3497 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003498 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003499 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003500 txn->flags &= ~TX_CK_MASK;
3501 txn->flags |= TX_CK_VALID;
3502 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003503 t->srv = srv;
3504 break;
3505 } else {
3506 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01003507 txn->flags &= ~TX_CK_MASK;
3508 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003509 }
3510 }
3511 srv = srv->next;
3512 }
3513
Willy Tarreau3d300592007-03-18 18:34:41 +01003514 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003515 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01003516 txn->flags &= ~TX_CK_MASK;
3517 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003518 }
3519
3520 /* depending on the cookie mode, we may have to either :
3521 * - delete the complete cookie if we're in insert+indirect mode, so that
3522 * the server never sees it ;
3523 * - remove the server id from the cookie value, and tag the cookie as an
3524 * application cookie so that it does not get accidentely removed later,
3525 * if we're in cookie prefix mode
3526 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003527 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003528 int delta; /* negative */
3529
3530 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
3531 p4 += delta;
3532 cur_end += delta;
3533 cur_next += delta;
3534 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003535 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003536
3537 del_cookie = del_colon = NULL;
3538 app_cookies++; /* protect the header from deletion */
3539 }
3540 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003541 (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 +01003542 del_cookie = p1;
3543 del_colon = colon;
3544 }
3545 } else {
3546 /* now we know that we must keep this cookie since it's
3547 * not ours. But if we wanted to delete our cookie
3548 * earlier, we cannot remove the complete header, but we
3549 * can remove the previous block itself.
3550 */
3551 app_cookies++;
3552
3553 if (del_cookie != NULL) {
3554 int delta; /* negative */
3555
3556 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
3557 p4 += delta;
3558 cur_end += delta;
3559 cur_next += delta;
3560 cur_hdr->len += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003561 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003562 del_cookie = del_colon = NULL;
3563 }
3564 }
3565
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003566 if ((t->be->appsession_name != NULL) &&
3567 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003568 /* first, let's see if the cookie is our appcookie*/
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003569
Willy Tarreau58f10d72006-12-04 02:26:12 +01003570 /* Cool... it's the right one */
3571
3572 asession_temp = &local_asession;
3573
Willy Tarreau63963c62007-05-13 21:29:55 +02003574 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003575 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3576 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3577 return;
3578 }
3579
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003580 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
3581 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003582 asession_temp->serverid = NULL;
Willy Tarreau51041c72007-09-09 21:56:53 +02003583
Willy Tarreau58f10d72006-12-04 02:26:12 +01003584 /* only do insert, if lookup fails */
Willy Tarreau51041c72007-09-09 21:56:53 +02003585 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
3586 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02003587 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003588 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003589 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003590 Alert("Not enough memory process_cli():asession:calloc().\n");
3591 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
3592 return;
3593 }
3594
3595 asession_temp->sessid = local_asession.sessid;
3596 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003597 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02003598 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003599 } else {
3600 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02003601 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003602 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003603 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003604 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003605 Alert("Found Application Session without matching server.\n");
3606 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003607 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003608 while (srv) {
3609 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003610 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003611 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01003612 txn->flags &= ~TX_CK_MASK;
3613 txn->flags |= TX_CK_VALID;
3614 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003615 t->srv = srv;
3616 break;
3617 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01003618 txn->flags &= ~TX_CK_MASK;
3619 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003620 }
3621 }
3622 srv = srv->next;
3623 }/* end while(srv) */
3624 }/* end else if server == NULL */
3625
Willy Tarreau0c303ee2008-07-07 00:09:58 +02003626 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02003627 asession_temp->request_count++;
3628#if defined(DEBUG_HASH)
3629 Alert("manage_client_side_cookies\n");
3630 appsession_hash_dump(&(t->be->htbl_proxy));
3631#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01003632 }/* end if ((t->proxy->appsession_name != NULL) ... */
3633 }
3634
3635 /* we'll have to look for another cookie ... */
3636 p1 = p4;
3637 } /* while (p1 < cur_end) */
3638
3639 /* There's no more cookie on this line.
3640 * We may have marked the last one(s) for deletion.
3641 * We must do this now in two ways :
3642 * - if there is no app cookie, we simply delete the header ;
3643 * - if there are app cookies, we must delete the end of the
3644 * string properly, including the colon/semi-colon before
3645 * the cookie name.
3646 */
3647 if (del_cookie != NULL) {
3648 int delta;
3649 if (app_cookies) {
3650 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
3651 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003652 cur_hdr->len += delta;
3653 } else {
3654 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003655
3656 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003657 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3658 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003659 cur_hdr->len = 0;
3660 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01003661 cur_next += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003662 txn->req.eoh += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003663 }
3664
3665 /* keep the link from this header to next one */
3666 old_idx = cur_idx;
3667 } /* end of cookie processing on this header */
3668}
3669
3670
Willy Tarreaua15645d2007-03-18 16:22:39 +01003671/* Iterate the same filter through all response headers contained in <rtr>.
3672 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3673 */
3674int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3675{
3676 char term;
3677 char *cur_ptr, *cur_end, *cur_next;
3678 int cur_idx, old_idx, last_hdr;
3679 struct http_txn *txn = &t->txn;
3680 struct hdr_idx_elem *cur_hdr;
3681 int len, delta;
3682
3683 last_hdr = 0;
3684
3685 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3686 old_idx = 0;
3687
3688 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003689 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003690 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003691 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003692 (exp->action == ACT_ALLOW ||
3693 exp->action == ACT_DENY))
3694 return 0;
3695
3696 cur_idx = txn->hdr_idx.v[old_idx].next;
3697 if (!cur_idx)
3698 break;
3699
3700 cur_hdr = &txn->hdr_idx.v[cur_idx];
3701 cur_ptr = cur_next;
3702 cur_end = cur_ptr + cur_hdr->len;
3703 cur_next = cur_end + cur_hdr->cr + 1;
3704
3705 /* Now we have one header between cur_ptr and cur_end,
3706 * and the next header starts at cur_next.
3707 */
3708
3709 /* The annoying part is that pattern matching needs
3710 * that we modify the contents to null-terminate all
3711 * strings before testing them.
3712 */
3713
3714 term = *cur_end;
3715 *cur_end = '\0';
3716
3717 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3718 switch (exp->action) {
3719 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003720 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003721 last_hdr = 1;
3722 break;
3723
3724 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003725 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003726 last_hdr = 1;
3727 break;
3728
3729 case ACT_REPLACE:
3730 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3731 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3732 /* FIXME: if the user adds a newline in the replacement, the
3733 * index will not be recalculated for now, and the new line
3734 * will not be counted as a new header.
3735 */
3736
3737 cur_end += delta;
3738 cur_next += delta;
3739 cur_hdr->len += delta;
3740 txn->rsp.eoh += delta;
3741 break;
3742
3743 case ACT_REMOVE:
3744 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
3745 cur_next += delta;
3746
3747 /* FIXME: this should be a separate function */
3748 txn->rsp.eoh += delta;
3749 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3750 txn->hdr_idx.used--;
3751 cur_hdr->len = 0;
3752 cur_end = NULL; /* null-term has been rewritten */
3753 break;
3754
3755 }
3756 }
3757 if (cur_end)
3758 *cur_end = term; /* restore the string terminator */
3759
3760 /* keep the link from this header to next one in case of later
3761 * removal of next header.
3762 */
3763 old_idx = cur_idx;
3764 }
3765 return 0;
3766}
3767
3768
3769/* Apply the filter to the status line in the response buffer <rtr>.
3770 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3771 * or -1 if a replacement resulted in an invalid status line.
3772 */
3773int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3774{
3775 char term;
3776 char *cur_ptr, *cur_end;
3777 int done;
3778 struct http_txn *txn = &t->txn;
3779 int len, delta;
3780
3781
Willy Tarreau3d300592007-03-18 18:34:41 +01003782 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01003783 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003784 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003785 (exp->action == ACT_ALLOW ||
3786 exp->action == ACT_DENY))
3787 return 0;
3788 else if (exp->action == ACT_REMOVE)
3789 return 0;
3790
3791 done = 0;
3792
Willy Tarreau9cdde232007-05-02 20:58:19 +02003793 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003794 cur_end = cur_ptr + txn->rsp.sl.rq.l;
3795
3796 /* Now we have the status line between cur_ptr and cur_end */
3797
3798 /* The annoying part is that pattern matching needs
3799 * that we modify the contents to null-terminate all
3800 * strings before testing them.
3801 */
3802
3803 term = *cur_end;
3804 *cur_end = '\0';
3805
3806 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3807 switch (exp->action) {
3808 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003809 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003810 done = 1;
3811 break;
3812
3813 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003814 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003815 done = 1;
3816 break;
3817
3818 case ACT_REPLACE:
3819 *cur_end = term; /* restore the string terminator */
3820 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3821 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
3822 /* FIXME: if the user adds a newline in the replacement, the
3823 * index will not be recalculated for now, and the new line
3824 * will not be counted as a new header.
3825 */
3826
3827 txn->rsp.eoh += delta;
3828 cur_end += delta;
3829
Willy Tarreau9cdde232007-05-02 20:58:19 +02003830 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003831 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02003832 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01003833 cur_ptr, cur_end + 1,
3834 NULL, NULL);
3835 if (unlikely(!cur_end))
3836 return -1;
3837
3838 /* we have a full respnse and we know that we have either a CR
3839 * or an LF at <ptr>.
3840 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003841 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003842 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
3843 /* there is no point trying this regex on headers */
3844 return 1;
3845 }
3846 }
3847 *cur_end = term; /* restore the string terminator */
3848 return done;
3849}
3850
3851
3852
3853/*
3854 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
3855 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3856 * unparsable response.
3857 */
3858int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
3859{
Willy Tarreau3d300592007-03-18 18:34:41 +01003860 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003861 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003862 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003863 int ret;
3864
3865 /*
3866 * The interleaving of transformations and verdicts
3867 * makes it difficult to decide to continue or stop
3868 * the evaluation.
3869 */
3870
Willy Tarreau3d300592007-03-18 18:34:41 +01003871 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01003872 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3873 exp->action == ACT_PASS)) {
3874 exp = exp->next;
3875 continue;
3876 }
3877
3878 /* Apply the filter to the status line. */
3879 ret = apply_filter_to_sts_line(t, rtr, exp);
3880 if (unlikely(ret < 0))
3881 return -1;
3882
3883 if (likely(ret == 0)) {
3884 /* The filter did not match the response, it can be
3885 * iterated through all headers.
3886 */
3887 apply_filter_to_resp_headers(t, rtr, exp);
3888 }
3889 exp = exp->next;
3890 }
3891 return 0;
3892}
3893
3894
3895
3896/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003897 * Manage server-side cookies. It can impact performance by about 2% so it is
3898 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003899 */
3900void manage_server_side_cookies(struct session *t, struct buffer *rtr)
3901{
3902 struct http_txn *txn = &t->txn;
3903 char *p1, *p2, *p3, *p4;
3904
3905 appsess *asession_temp = NULL;
3906 appsess local_asession;
3907
3908 char *cur_ptr, *cur_end, *cur_next;
3909 int cur_idx, old_idx, delta;
3910
Willy Tarreaua15645d2007-03-18 16:22:39 +01003911 /* Iterate through the headers.
3912 * we start with the start line.
3913 */
3914 old_idx = 0;
3915 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3916
3917 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3918 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003919 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003920
3921 cur_hdr = &txn->hdr_idx.v[cur_idx];
3922 cur_ptr = cur_next;
3923 cur_end = cur_ptr + cur_hdr->len;
3924 cur_next = cur_end + cur_hdr->cr + 1;
3925
3926 /* We have one full header between cur_ptr and cur_end, and the
3927 * next header starts at cur_next. We're only interested in
3928 * "Cookie:" headers.
3929 */
3930
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003931 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
3932 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003933 old_idx = cur_idx;
3934 continue;
3935 }
3936
3937 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01003938 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003939
3940
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003941 /* maybe we only wanted to see if there was a set-cookie. Note that
3942 * the cookie capture is declared in the fronend.
3943 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003944 if (t->be->cookie_name == NULL &&
3945 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003946 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01003947 return;
3948
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003949 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003950
3951 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003952 if (p1 == cur_end || *p1 == ';') /* end of cookie */
3953 break;
3954
3955 /* p1 is at the beginning of the cookie name */
3956 p2 = p1;
3957
3958 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
3959 p2++;
3960
3961 if (p2 == cur_end || *p2 == ';') /* next cookie */
3962 break;
3963
3964 p3 = p2 + 1; /* skip the '=' sign */
3965 if (p3 == cur_end)
3966 break;
3967
3968 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003969 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01003970 p4++;
3971
3972 /* here, we have the cookie name between p1 and p2,
3973 * and its value between p3 and p4.
3974 * we can process it.
3975 */
3976
3977 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003978 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003979 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003980 (p4 - p1 >= t->fe->capture_namelen) &&
3981 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003982 int log_len = p4 - p1;
3983
Willy Tarreau086b3b42007-05-13 21:45:51 +02003984 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003985 Alert("HTTP logging : out of memory.\n");
3986 }
3987
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003988 if (log_len > t->fe->capture_len)
3989 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003990 memcpy(txn->srv_cookie, p1, log_len);
3991 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003992 }
3993
3994 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003995 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3996 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01003997 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01003998 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003999
4000 /* If the cookie is in insert mode on a known server, we'll delete
4001 * this occurrence because we'll insert another one later.
4002 * We'll delete it too if the "indirect" option is set and we're in
4003 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004004 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4005 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004006 /* this header must be deleted */
4007 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4008 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4009 txn->hdr_idx.used--;
4010 cur_hdr->len = 0;
4011 cur_next += delta;
4012 txn->rsp.eoh += delta;
4013
Willy Tarreau3d300592007-03-18 18:34:41 +01004014 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004015 }
4016 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004017 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004018 /* replace bytes p3->p4 with the cookie name associated
4019 * with this server since we know it.
4020 */
4021 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4022 cur_hdr->len += delta;
4023 cur_next += delta;
4024 txn->rsp.eoh += delta;
4025
Willy Tarreau3d300592007-03-18 18:34:41 +01004026 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004027 }
4028 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004029 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004030 /* insert the cookie name associated with this server
4031 * before existing cookie, and insert a delimitor between them..
4032 */
4033 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4034 cur_hdr->len += delta;
4035 cur_next += delta;
4036 txn->rsp.eoh += delta;
4037
4038 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004039 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004040 }
4041 }
4042 /* next, let's see if the cookie is our appcookie */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004043 else if ((t->be->appsession_name != NULL) &&
4044 (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004045
4046 /* Cool... it's the right one */
4047
4048 size_t server_id_len = strlen(t->srv->id) + 1;
4049 asession_temp = &local_asession;
4050
Willy Tarreau63963c62007-05-13 21:29:55 +02004051 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004052 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4053 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4054 return;
4055 }
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004056 memcpy(asession_temp->sessid, p3, t->be->appsession_len);
4057 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004058 asession_temp->serverid = NULL;
4059
4060 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004061 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4062 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004063 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004064 Alert("Not enough Memory process_srv():asession:calloc().\n");
4065 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4066 return;
4067 }
4068 asession_temp->sessid = local_asession.sessid;
4069 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004070 asession_temp->request_count = 0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004071 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
4072 } else {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004073 /* free wasted memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004074 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau51041c72007-09-09 21:56:53 +02004075 }
4076
Willy Tarreaua15645d2007-03-18 16:22:39 +01004077 if (asession_temp->serverid == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004078 if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004079 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4080 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4081 return;
4082 }
4083 asession_temp->serverid[0] = '\0';
4084 }
4085
4086 if (asession_temp->serverid[0] == '\0')
4087 memcpy(asession_temp->serverid, t->srv->id, server_id_len);
4088
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004089 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004090 asession_temp->request_count++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004091#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004092 Alert("manage_server_side_cookies\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004093 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreaua15645d2007-03-18 16:22:39 +01004094#endif
4095 }/* end if ((t->proxy->appsession_name != NULL) ... */
4096 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4097 } /* we're now at the end of the cookie value */
4098
4099 /* keep the link from this header to next one */
4100 old_idx = cur_idx;
4101 } /* end of cookie processing on this header */
4102}
4103
4104
4105
4106/*
4107 * Check if response is cacheable or not. Updates t->flags.
4108 */
4109void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4110{
4111 struct http_txn *txn = &t->txn;
4112 char *p1, *p2;
4113
4114 char *cur_ptr, *cur_end, *cur_next;
4115 int cur_idx;
4116
Willy Tarreau5df51872007-11-25 16:20:08 +01004117 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004118 return;
4119
4120 /* Iterate through the headers.
4121 * we start with the start line.
4122 */
4123 cur_idx = 0;
4124 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4125
4126 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4127 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004128 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004129
4130 cur_hdr = &txn->hdr_idx.v[cur_idx];
4131 cur_ptr = cur_next;
4132 cur_end = cur_ptr + cur_hdr->len;
4133 cur_next = cur_end + cur_hdr->cr + 1;
4134
4135 /* We have one full header between cur_ptr and cur_end, and the
4136 * next header starts at cur_next. We're only interested in
4137 * "Cookie:" headers.
4138 */
4139
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004140 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4141 if (val) {
4142 if ((cur_end - (cur_ptr + val) >= 8) &&
4143 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4144 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4145 return;
4146 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004147 }
4148
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004149 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4150 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004151 continue;
4152
4153 /* OK, right now we know we have a cache-control header at cur_ptr */
4154
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004155 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004156
4157 if (p1 >= cur_end) /* no more info */
4158 continue;
4159
4160 /* p1 is at the beginning of the value */
4161 p2 = p1;
4162
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004163 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004164 p2++;
4165
4166 /* we have a complete value between p1 and p2 */
4167 if (p2 < cur_end && *p2 == '=') {
4168 /* we have something of the form no-cache="set-cookie" */
4169 if ((cur_end - p1 >= 21) &&
4170 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4171 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004172 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004173 continue;
4174 }
4175
4176 /* OK, so we know that either p2 points to the end of string or to a comma */
4177 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4178 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4179 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4180 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004181 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004182 return;
4183 }
4184
4185 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004186 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004187 continue;
4188 }
4189 }
4190}
4191
4192
Willy Tarreau58f10d72006-12-04 02:26:12 +01004193/*
4194 * Try to retrieve a known appsession in the URI, then the associated server.
4195 * If the server is found, it's assigned to the session.
4196 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004197void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004198{
Willy Tarreau3d300592007-03-18 18:34:41 +01004199 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004200 appsess *asession_temp = NULL;
4201 appsess local_asession;
4202 char *request_line;
4203
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004204 if (t->be->appsession_name == NULL ||
Willy Tarreaub326fcc2007-03-03 13:54:32 +01004205 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) ||
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004206 (request_line = memchr(begin, ';', len)) == NULL ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004207 ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line)))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004208 return;
4209
4210 /* skip ';' */
4211 request_line++;
4212
4213 /* look if we have a jsessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004214 if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004215 return;
4216
4217 /* skip jsessionid= */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004218 request_line += t->be->appsession_name_len + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004219
4220 /* First try if we already have an appsession */
4221 asession_temp = &local_asession;
4222
Willy Tarreau63963c62007-05-13 21:29:55 +02004223 if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004224 Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n");
4225 send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n");
4226 return;
4227 }
4228
4229 /* Copy the sessionid */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004230 memcpy(asession_temp->sessid, request_line, t->be->appsession_len);
4231 asession_temp->sessid[t->be->appsession_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004232 asession_temp->serverid = NULL;
4233
4234 /* only do insert, if lookup fails */
Ryan Warnick6d0b1fa2008-02-17 11:24:35 +01004235 asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid);
4236 if (asession_temp == NULL) {
Willy Tarreau63963c62007-05-13 21:29:55 +02004237 if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004238 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004239 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004240 Alert("Not enough memory process_cli():asession:calloc().\n");
4241 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n");
4242 return;
4243 }
4244 asession_temp->sessid = local_asession.sessid;
4245 asession_temp->serverid = local_asession.serverid;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004246 asession_temp->request_count=0;
Willy Tarreau51041c72007-09-09 21:56:53 +02004247 appsession_hash_insert(&(t->be->htbl_proxy), asession_temp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004248 }
4249 else {
4250 /* free previously allocated memory */
Willy Tarreau63963c62007-05-13 21:29:55 +02004251 pool_free2(apools.sessid, local_asession.sessid);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004252 }
Willy Tarreau51041c72007-09-09 21:56:53 +02004253
Willy Tarreau0c303ee2008-07-07 00:09:58 +02004254 asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004255 asession_temp->request_count++;
Willy Tarreau51041c72007-09-09 21:56:53 +02004256
Willy Tarreau58f10d72006-12-04 02:26:12 +01004257#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004258 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004259 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004260#endif
4261 if (asession_temp->serverid == NULL) {
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004262 /* TODO redispatch request */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004263 Alert("Found Application Session without matching server.\n");
4264 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004265 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004266 while (srv) {
4267 if (strcmp(srv->id, asession_temp->serverid) == 0) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004268 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004269 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004270 txn->flags &= ~TX_CK_MASK;
4271 txn->flags |= TX_CK_VALID;
4272 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004273 t->srv = srv;
4274 break;
4275 } else {
Willy Tarreau3d300592007-03-18 18:34:41 +01004276 txn->flags &= ~TX_CK_MASK;
4277 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004278 }
4279 }
4280 srv = srv->next;
4281 }
4282 }
4283}
4284
4285
Willy Tarreaub2513902006-12-17 14:52:38 +01004286/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004287 * In a GET or HEAD request, check if the requested URI matches the stats uri
4288 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004289 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004290 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004291 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004292 * produce_content() can be called to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004293 *
4294 * Returns 1 if the session's state changes, otherwise 0.
4295 */
4296int stats_check_uri_auth(struct session *t, struct proxy *backend)
4297{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004298 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004299 struct uri_auth *uri_auth = backend->uri_auth;
4300 struct user_auth *user;
4301 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004302 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004303
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004304 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
4305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004306 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004307 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004308 return 0;
4309
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004310 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004311
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004312 /* the URI is in h */
4313 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004314 return 0;
4315
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004316 h += uri_auth->uri_len;
4317 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4318 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004319 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004320 break;
4321 }
4322 h++;
4323 }
4324
4325 if (uri_auth->refresh) {
4326 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4327 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4328 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004329 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004330 break;
4331 }
4332 h++;
4333 }
4334 }
4335
Willy Tarreau55bb8452007-10-17 18:44:57 +02004336 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4337 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4338 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004339 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02004340 break;
4341 }
4342 h++;
4343 }
4344
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004345 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
4346
Willy Tarreaub2513902006-12-17 14:52:38 +01004347 /* we are in front of a interceptable URI. Let's check
4348 * if there's an authentication and if it's valid.
4349 */
4350 user = uri_auth->users;
4351 if (!user) {
4352 /* no user auth required, it's OK */
4353 authenticated = 1;
4354 } else {
4355 authenticated = 0;
4356
4357 /* a user list is defined, we have to check.
4358 * skip 21 chars for "Authorization: Basic ".
4359 */
4360
4361 /* FIXME: this should move to an earlier place */
4362 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004363 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4364 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4365 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004366 if (len > 14 &&
4367 !strncasecmp("Authorization:", h, 14)) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004368 txn->auth_hdr.str = h;
4369 txn->auth_hdr.len = len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004370 break;
4371 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004372 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004373 }
4374
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004375 if (txn->auth_hdr.len < 21 ||
4376 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004377 user = NULL;
4378
4379 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004380 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4381 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004382 user->user_pwd, user->user_len)) {
4383 authenticated = 1;
4384 break;
4385 }
4386 user = user->next;
4387 }
4388 }
4389
4390 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004391 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004392
4393 /* no need to go further */
Willy Tarreau0f772532006-12-23 20:51:41 +01004394 msg.str = trash;
4395 msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004396 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01004397 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02004398 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004399 if (!(t->flags & SN_ERR_MASK))
4400 t->flags |= SN_ERR_PRXCOND;
4401 if (!(t->flags & SN_FINST_MASK))
4402 t->flags |= SN_FINST_R;
4403 return 1;
4404 }
4405
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004406 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01004407 * data.
4408 */
Willy Tarreauefb453c2008-10-26 20:49:47 +01004409 buffer_write_dis(t->req);
Willy Tarreau72b179a2008-08-28 16:01:32 +02004410 buffer_shutw_now(t->req);
4411 buffer_shutr_now(t->rep);
Willy Tarreau70089872008-06-13 21:12:51 +02004412 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01004413 t->data_source = DATA_SRC_STATS;
4414 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02004415 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau01bf8672008-12-07 18:03:29 +01004416 buffer_install_hijacker(t, t->rep, produce_content);
Willy Tarreaub2513902006-12-17 14:52:38 +01004417 return 1;
4418}
4419
Willy Tarreau4076a152009-04-02 15:18:36 +02004420/*
4421 * Capture a bad request or response and archive it in the proxy's structure.
4422 */
4423void http_capture_bad_message(struct error_snapshot *es, struct session *s,
4424 struct buffer *buf, struct http_msg *msg,
4425 struct proxy *other_end)
4426{
Willy Tarreau2df8d712009-05-01 11:33:17 +02004427 es->len = buf->r - (buf->data + msg->som);
4428 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02004429 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02004430 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02004431 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02004432 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02004433 es->when = date; // user-visible date
4434 es->sid = s->uniq_id;
4435 es->srv = s->srv;
4436 es->oe = other_end;
4437 es->src = s->cli_addr;
4438}
Willy Tarreaub2513902006-12-17 14:52:38 +01004439
Willy Tarreaubaaee002006-06-26 02:48:02 +02004440/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004441 * Print a debug line with a header
4442 */
4443void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4444{
4445 int len, max;
4446 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004447 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004448 max = end - start;
4449 UBOUND(max, sizeof(trash) - len - 1);
4450 len += strlcpy2(trash + len, start, max + 1);
4451 trash[len++] = '\n';
4452 write(1, trash, len);
4453}
4454
4455
Willy Tarreau8797c062007-05-07 00:55:35 +02004456/************************************************************************/
4457/* The code below is dedicated to ACL parsing and matching */
4458/************************************************************************/
4459
4460
4461
4462
4463/* 1. Check on METHOD
4464 * We use the pre-parsed method if it is known, and store its number as an
4465 * integer. If it is unknown, we use the pointer and the length.
4466 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004467static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004468{
4469 int len, meth;
4470
Willy Tarreauae8b7962007-06-09 23:10:04 +02004471 len = strlen(*text);
4472 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004473
4474 pattern->val.i = meth;
4475 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004476 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004477 if (!pattern->ptr.str)
4478 return 0;
4479 pattern->len = len;
4480 }
4481 return 1;
4482}
4483
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004484static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004485acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4486 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004487{
4488 int meth;
4489 struct http_txn *txn = l7;
4490
Willy Tarreaub6866442008-07-14 23:54:42 +02004491 if (!txn)
4492 return 0;
4493
Willy Tarreauc11416f2007-06-17 16:58:38 +02004494 if (txn->req.msg_state != HTTP_MSG_BODY)
4495 return 0;
4496
Willy Tarreau8797c062007-05-07 00:55:35 +02004497 meth = txn->meth;
4498 test->i = meth;
4499 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004500 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4501 /* ensure the indexes are not affected */
4502 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004503 test->len = txn->req.sl.rq.m_l;
4504 test->ptr = txn->req.sol;
4505 }
4506 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4507 return 1;
4508}
4509
4510static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4511{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004512 int icase;
4513
Willy Tarreau8797c062007-05-07 00:55:35 +02004514 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02004515 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02004516
4517 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02004518 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004519
4520 /* Other method, we must compare the strings */
4521 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02004522 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004523
4524 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4525 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4526 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02004527 return ACL_PAT_FAIL;
4528 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004529}
4530
4531/* 2. Check on Request/Status Version
4532 * We simply compare strings here.
4533 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004534static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004535{
Willy Tarreauae8b7962007-06-09 23:10:04 +02004536 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004537 if (!pattern->ptr.str)
4538 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02004539 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004540 return 1;
4541}
4542
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004543static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004544acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
4545 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004546{
4547 struct http_txn *txn = l7;
4548 char *ptr;
4549 int len;
4550
Willy Tarreaub6866442008-07-14 23:54:42 +02004551 if (!txn)
4552 return 0;
4553
Willy Tarreauc11416f2007-06-17 16:58:38 +02004554 if (txn->req.msg_state != HTTP_MSG_BODY)
4555 return 0;
4556
Willy Tarreau8797c062007-05-07 00:55:35 +02004557 len = txn->req.sl.rq.v_l;
4558 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
4559
4560 while ((len-- > 0) && (*ptr++ != '/'));
4561 if (len <= 0)
4562 return 0;
4563
4564 test->ptr = ptr;
4565 test->len = len;
4566
4567 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4568 return 1;
4569}
4570
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004571static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004572acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
4573 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004574{
4575 struct http_txn *txn = l7;
4576 char *ptr;
4577 int len;
4578
Willy Tarreaub6866442008-07-14 23:54:42 +02004579 if (!txn)
4580 return 0;
4581
Willy Tarreauc11416f2007-06-17 16:58:38 +02004582 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4583 return 0;
4584
Willy Tarreau8797c062007-05-07 00:55:35 +02004585 len = txn->rsp.sl.st.v_l;
4586 ptr = txn->rsp.sol;
4587
4588 while ((len-- > 0) && (*ptr++ != '/'));
4589 if (len <= 0)
4590 return 0;
4591
4592 test->ptr = ptr;
4593 test->len = len;
4594
4595 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4596 return 1;
4597}
4598
4599/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004600static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004601acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
4602 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004603{
4604 struct http_txn *txn = l7;
4605 char *ptr;
4606 int len;
4607
Willy Tarreaub6866442008-07-14 23:54:42 +02004608 if (!txn)
4609 return 0;
4610
Willy Tarreauc11416f2007-06-17 16:58:38 +02004611 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4612 return 0;
4613
Willy Tarreau8797c062007-05-07 00:55:35 +02004614 len = txn->rsp.sl.st.c_l;
4615 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
4616
4617 test->i = __strl2ui(ptr, len);
4618 test->flags = ACL_TEST_F_VOL_1ST;
4619 return 1;
4620}
4621
4622/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004623static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004624acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
4625 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004626{
4627 struct http_txn *txn = l7;
4628
Willy Tarreaub6866442008-07-14 23:54:42 +02004629 if (!txn)
4630 return 0;
4631
Willy Tarreauc11416f2007-06-17 16:58:38 +02004632 if (txn->req.msg_state != HTTP_MSG_BODY)
4633 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004634
Willy Tarreauc11416f2007-06-17 16:58:38 +02004635 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4636 /* ensure the indexes are not affected */
4637 return 0;
4638
Willy Tarreau8797c062007-05-07 00:55:35 +02004639 test->len = txn->req.sl.rq.u_l;
4640 test->ptr = txn->req.sol + txn->req.sl.rq.u;
4641
Willy Tarreauf3d25982007-05-08 22:45:09 +02004642 /* we do not need to set READ_ONLY because the data is in a buffer */
4643 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02004644 return 1;
4645}
4646
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004647static int
4648acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
4649 struct acl_expr *expr, struct acl_test *test)
4650{
4651 struct http_txn *txn = l7;
4652
Willy Tarreaub6866442008-07-14 23:54:42 +02004653 if (!txn)
4654 return 0;
4655
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004656 if (txn->req.msg_state != HTTP_MSG_BODY)
4657 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004658
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004659 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4660 /* ensure the indexes are not affected */
4661 return 0;
4662
4663 /* Parse HTTP request */
4664 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4665 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
4666 test->i = AF_INET;
4667
4668 /*
4669 * If we are parsing url in frontend space, we prepare backend stage
4670 * to not parse again the same url ! optimization lazyness...
4671 */
4672 if (px->options & PR_O_HTTP_PROXY)
4673 l4->flags |= SN_ADDR_SET;
4674
4675 test->flags = ACL_TEST_F_READ_ONLY;
4676 return 1;
4677}
4678
4679static int
4680acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
4681 struct acl_expr *expr, struct acl_test *test)
4682{
4683 struct http_txn *txn = l7;
4684
Willy Tarreaub6866442008-07-14 23:54:42 +02004685 if (!txn)
4686 return 0;
4687
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004688 if (txn->req.msg_state != HTTP_MSG_BODY)
4689 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004690
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01004691 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4692 /* ensure the indexes are not affected */
4693 return 0;
4694
4695 /* Same optimization as url_ip */
4696 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
4697 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
4698
4699 if (px->options & PR_O_HTTP_PROXY)
4700 l4->flags |= SN_ADDR_SET;
4701
4702 test->flags = ACL_TEST_F_READ_ONLY;
4703 return 1;
4704}
4705
Willy Tarreauc11416f2007-06-17 16:58:38 +02004706/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
4707 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
4708 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02004709static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004710acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004711 struct acl_expr *expr, struct acl_test *test)
4712{
4713 struct http_txn *txn = l7;
4714 struct hdr_idx *idx = &txn->hdr_idx;
4715 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004716
Willy Tarreaub6866442008-07-14 23:54:42 +02004717 if (!txn)
4718 return 0;
4719
Willy Tarreau33a7e692007-06-10 19:45:56 +02004720 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4721 /* search for header from the beginning */
4722 ctx->idx = 0;
4723
Willy Tarreau33a7e692007-06-10 19:45:56 +02004724 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4725 test->flags |= ACL_TEST_F_FETCH_MORE;
4726 test->flags |= ACL_TEST_F_VOL_HDR;
4727 test->len = ctx->vlen;
4728 test->ptr = (char *)ctx->line + ctx->val;
4729 return 1;
4730 }
4731
4732 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4733 test->flags |= ACL_TEST_F_VOL_HDR;
4734 return 0;
4735}
4736
Willy Tarreau33a7e692007-06-10 19:45:56 +02004737static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004738acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
4739 struct acl_expr *expr, struct acl_test *test)
4740{
4741 struct http_txn *txn = l7;
4742
Willy Tarreaub6866442008-07-14 23:54:42 +02004743 if (!txn)
4744 return 0;
4745
Willy Tarreauc11416f2007-06-17 16:58:38 +02004746 if (txn->req.msg_state != HTTP_MSG_BODY)
4747 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004748
Willy Tarreauc11416f2007-06-17 16:58:38 +02004749 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4750 /* ensure the indexes are not affected */
4751 return 0;
4752
4753 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
4754}
4755
4756static int
4757acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
4758 struct acl_expr *expr, struct acl_test *test)
4759{
4760 struct http_txn *txn = l7;
4761
Willy Tarreaub6866442008-07-14 23:54:42 +02004762 if (!txn)
4763 return 0;
4764
Willy Tarreauc11416f2007-06-17 16:58:38 +02004765 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4766 return 0;
4767
4768 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
4769}
4770
4771/* 6. Check on HTTP header count. The number of occurrences is returned.
4772 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
4773 */
4774static int
4775acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004776 struct acl_expr *expr, struct acl_test *test)
4777{
4778 struct http_txn *txn = l7;
4779 struct hdr_idx *idx = &txn->hdr_idx;
4780 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004781 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02004782
Willy Tarreaub6866442008-07-14 23:54:42 +02004783 if (!txn)
4784 return 0;
4785
Willy Tarreau33a7e692007-06-10 19:45:56 +02004786 ctx.idx = 0;
4787 cnt = 0;
4788 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
4789 cnt++;
4790
4791 test->i = cnt;
4792 test->flags = ACL_TEST_F_VOL_HDR;
4793 return 1;
4794}
4795
Willy Tarreauc11416f2007-06-17 16:58:38 +02004796static int
4797acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4798 struct acl_expr *expr, struct acl_test *test)
4799{
4800 struct http_txn *txn = l7;
4801
Willy Tarreaub6866442008-07-14 23:54:42 +02004802 if (!txn)
4803 return 0;
4804
Willy Tarreauc11416f2007-06-17 16:58:38 +02004805 if (txn->req.msg_state != HTTP_MSG_BODY)
4806 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004807
Willy Tarreauc11416f2007-06-17 16:58:38 +02004808 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4809 /* ensure the indexes are not affected */
4810 return 0;
4811
4812 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
4813}
4814
4815static int
4816acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
4817 struct acl_expr *expr, struct acl_test *test)
4818{
4819 struct http_txn *txn = l7;
4820
Willy Tarreaub6866442008-07-14 23:54:42 +02004821 if (!txn)
4822 return 0;
4823
Willy Tarreauc11416f2007-06-17 16:58:38 +02004824 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4825 return 0;
4826
4827 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
4828}
4829
Willy Tarreau33a7e692007-06-10 19:45:56 +02004830/* 7. Check on HTTP header's integer value. The integer value is returned.
4831 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02004832 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02004833 */
4834static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02004835acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02004836 struct acl_expr *expr, struct acl_test *test)
4837{
4838 struct http_txn *txn = l7;
4839 struct hdr_idx *idx = &txn->hdr_idx;
4840 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004841
Willy Tarreaub6866442008-07-14 23:54:42 +02004842 if (!txn)
4843 return 0;
4844
Willy Tarreau33a7e692007-06-10 19:45:56 +02004845 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
4846 /* search for header from the beginning */
4847 ctx->idx = 0;
4848
Willy Tarreau33a7e692007-06-10 19:45:56 +02004849 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
4850 test->flags |= ACL_TEST_F_FETCH_MORE;
4851 test->flags |= ACL_TEST_F_VOL_HDR;
4852 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
4853 return 1;
4854 }
4855
4856 test->flags &= ~ACL_TEST_F_FETCH_MORE;
4857 test->flags |= ACL_TEST_F_VOL_HDR;
4858 return 0;
4859}
4860
Willy Tarreauc11416f2007-06-17 16:58:38 +02004861static int
4862acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4863 struct acl_expr *expr, struct acl_test *test)
4864{
4865 struct http_txn *txn = l7;
4866
Willy Tarreaub6866442008-07-14 23:54:42 +02004867 if (!txn)
4868 return 0;
4869
Willy Tarreauc11416f2007-06-17 16:58:38 +02004870 if (txn->req.msg_state != HTTP_MSG_BODY)
4871 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004872
Willy Tarreauc11416f2007-06-17 16:58:38 +02004873 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4874 /* ensure the indexes are not affected */
4875 return 0;
4876
4877 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
4878}
4879
4880static int
4881acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
4882 struct acl_expr *expr, struct acl_test *test)
4883{
4884 struct http_txn *txn = l7;
4885
Willy Tarreaub6866442008-07-14 23:54:42 +02004886 if (!txn)
4887 return 0;
4888
Willy Tarreauc11416f2007-06-17 16:58:38 +02004889 if (txn->rsp.msg_state != HTTP_MSG_BODY)
4890 return 0;
4891
4892 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
4893}
4894
Willy Tarreau737b0c12007-06-10 21:28:46 +02004895/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
4896 * the first '/' after the possible hostname, and ends before the possible '?'.
4897 */
4898static int
4899acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
4900 struct acl_expr *expr, struct acl_test *test)
4901{
4902 struct http_txn *txn = l7;
4903 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02004904
Willy Tarreaub6866442008-07-14 23:54:42 +02004905 if (!txn)
4906 return 0;
4907
Willy Tarreauc11416f2007-06-17 16:58:38 +02004908 if (txn->req.msg_state != HTTP_MSG_BODY)
4909 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02004910
Willy Tarreauc11416f2007-06-17 16:58:38 +02004911 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4912 /* ensure the indexes are not affected */
4913 return 0;
4914
Willy Tarreau21d2af32008-02-14 20:25:24 +01004915 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
4916 ptr = http_get_path(txn);
4917 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02004918 return 0;
4919
4920 /* OK, we got the '/' ! */
4921 test->ptr = ptr;
4922
4923 while (ptr < end && *ptr != '?')
4924 ptr++;
4925
4926 test->len = ptr - test->ptr;
4927
4928 /* we do not need to set READ_ONLY because the data is in a buffer */
4929 test->flags = ACL_TEST_F_VOL_1ST;
4930 return 1;
4931}
4932
Willy Tarreau2492d5b2009-07-11 00:06:00 +02004933static int
4934acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
4935 struct acl_expr *expr, struct acl_test *test)
4936{
4937 struct buffer *req = s->req;
4938 struct http_txn *txn = &s->txn;
4939 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02004940
Willy Tarreau2492d5b2009-07-11 00:06:00 +02004941 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
4942 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
4943 */
4944
4945 if (!s || !req)
4946 return 0;
4947
4948 if (unlikely(msg->msg_state == HTTP_MSG_BODY)) {
4949 /* Already decoded as OK */
4950 test->flags |= ACL_TEST_F_SET_RES_PASS;
4951 return 1;
4952 }
4953
4954 /* Try to decode HTTP request */
4955 if (likely(req->lr < req->r))
4956 http_msg_analyzer(req, msg, &txn->hdr_idx);
4957
4958 if (unlikely(msg->msg_state != HTTP_MSG_BODY)) {
4959 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
4960 test->flags |= ACL_TEST_F_SET_RES_FAIL;
4961 return 1;
4962 }
4963 /* wait for final state */
4964 test->flags |= ACL_TEST_F_MAY_CHANGE;
4965 return 0;
4966 }
4967
4968 /* OK we got a valid HTTP request. We have some minor preparation to
4969 * perform so that further checks can rely on HTTP tests.
4970 */
4971 msg->sol = req->data + msg->som;
4972 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
4973 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4974 s->flags |= SN_REDIRECTABLE;
4975
4976 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
4977 test->flags |= ACL_TEST_F_SET_RES_FAIL;
4978 return 1;
4979 }
4980
4981 test->flags |= ACL_TEST_F_SET_RES_PASS;
4982 return 1;
4983}
4984
Willy Tarreau8797c062007-05-07 00:55:35 +02004985
4986/************************************************************************/
4987/* All supported keywords must be declared here. */
4988/************************************************************************/
4989
4990/* Note: must not be declared <const> as its list will be overwritten */
4991static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02004992 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
4993
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004994 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
4995 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
4996 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
4997 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02004998
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02004999 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5000 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5001 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5002 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5003 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5004 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5005 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5006 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5007 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005008
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005009 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5010 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5011 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5012 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5013 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5014 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5015 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5016 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5017 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5018 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005019
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005020 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5021 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5022 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5023 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5024 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5025 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5026 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5027 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5028 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005029
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005030 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5031 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5032 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5033 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5034 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5035 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5036 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005037
Willy Tarreauf3d25982007-05-08 22:45:09 +02005038 { NULL, NULL, NULL, NULL },
5039
5040#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005041 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5042 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5043 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5044 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5045 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5046 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5047 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5048
Willy Tarreau8797c062007-05-07 00:55:35 +02005049 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5050 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5051 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5052 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5053 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5054 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5055 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5056 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5057
5058 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5059 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5060 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5061 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5062 { NULL, NULL, NULL, NULL },
5063#endif
5064}};
5065
5066
5067__attribute__((constructor))
5068static void __http_protocol_init(void)
5069{
5070 acl_register_keywords(&acl_kws);
5071}
5072
5073
Willy Tarreau58f10d72006-12-04 02:26:12 +01005074/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005075 * Local variables:
5076 * c-indent-level: 8
5077 * c-basic-offset: 8
5078 * End:
5079 */