blob: 10874c5fc8cbcad53379e88761a09cf561006843 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreau655dce92009-11-08 13:10:58 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
27#include <common/compat.h>
28#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010029#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/memory.h>
31#include <common/mini-clist.h>
32#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020033#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/time.h>
35#include <common/uri_auth.h>
36#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau8797c062007-05-07 00:55:35 +020041#include <proto/acl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/backend.h>
43#include <proto/buffers.h>
Maik Broemme2850cb42009-04-17 18:53:21 +020044#include <proto/client.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/fd.h>
47#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010048#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020049#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010051#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010053#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010055#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020056#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/task.h>
58
Willy Tarreau1c47f852006-07-09 08:22:27 +020059/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010060const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020061 "HTTP/1.0 200 OK\r\n"
62 "Cache-Control: no-cache\r\n"
63 "Connection: close\r\n"
64 "Content-Type: text/html\r\n"
65 "\r\n"
66 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
67
Willy Tarreau0f772532006-12-23 20:51:41 +010068const struct chunk http_200_chunk = {
69 .str = (char *)&HTTP_200,
70 .len = sizeof(HTTP_200)-1
71};
72
Willy Tarreaub463dfb2008-06-07 23:08:56 +020073const char *HTTP_301 =
74 "HTTP/1.0 301 Moved Permantenly\r\n"
75 "Cache-Control: no-cache\r\n"
76 "Connection: close\r\n"
77 "Location: "; /* not terminated since it will be concatenated with the URL */
78
Willy Tarreau0f772532006-12-23 20:51:41 +010079const char *HTTP_302 =
80 "HTTP/1.0 302 Found\r\n"
81 "Cache-Control: no-cache\r\n"
82 "Connection: close\r\n"
83 "Location: "; /* not terminated since it will be concatenated with the URL */
84
85/* same as 302 except that the browser MUST retry with the GET method */
86const char *HTTP_303 =
87 "HTTP/1.0 303 See Other\r\n"
88 "Cache-Control: no-cache\r\n"
89 "Connection: close\r\n"
90 "Location: "; /* not terminated since it will be concatenated with the URL */
91
Willy Tarreaubaaee002006-06-26 02:48:02 +020092/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
93const char *HTTP_401_fmt =
94 "HTTP/1.0 401 Unauthorized\r\n"
95 "Cache-Control: no-cache\r\n"
96 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020097 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020098 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
99 "\r\n"
100 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
101
Willy Tarreau0f772532006-12-23 20:51:41 +0100102
103const int http_err_codes[HTTP_ERR_SIZE] = {
104 [HTTP_ERR_400] = 400,
105 [HTTP_ERR_403] = 403,
106 [HTTP_ERR_408] = 408,
107 [HTTP_ERR_500] = 500,
108 [HTTP_ERR_502] = 502,
109 [HTTP_ERR_503] = 503,
110 [HTTP_ERR_504] = 504,
111};
112
Willy Tarreau80587432006-12-24 17:47:20 +0100113static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100114 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100115 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100116 "Cache-Control: no-cache\r\n"
117 "Connection: close\r\n"
118 "Content-Type: text/html\r\n"
119 "\r\n"
120 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
121
122 [HTTP_ERR_403] =
123 "HTTP/1.0 403 Forbidden\r\n"
124 "Cache-Control: no-cache\r\n"
125 "Connection: close\r\n"
126 "Content-Type: text/html\r\n"
127 "\r\n"
128 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
129
130 [HTTP_ERR_408] =
131 "HTTP/1.0 408 Request Time-out\r\n"
132 "Cache-Control: no-cache\r\n"
133 "Connection: close\r\n"
134 "Content-Type: text/html\r\n"
135 "\r\n"
136 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
137
138 [HTTP_ERR_500] =
139 "HTTP/1.0 500 Server Error\r\n"
140 "Cache-Control: no-cache\r\n"
141 "Connection: close\r\n"
142 "Content-Type: text/html\r\n"
143 "\r\n"
144 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
145
146 [HTTP_ERR_502] =
147 "HTTP/1.0 502 Bad Gateway\r\n"
148 "Cache-Control: no-cache\r\n"
149 "Connection: close\r\n"
150 "Content-Type: text/html\r\n"
151 "\r\n"
152 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
153
154 [HTTP_ERR_503] =
155 "HTTP/1.0 503 Service Unavailable\r\n"
156 "Cache-Control: no-cache\r\n"
157 "Connection: close\r\n"
158 "Content-Type: text/html\r\n"
159 "\r\n"
160 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
161
162 [HTTP_ERR_504] =
163 "HTTP/1.0 504 Gateway Time-out\r\n"
164 "Cache-Control: no-cache\r\n"
165 "Connection: close\r\n"
166 "Content-Type: text/html\r\n"
167 "\r\n"
168 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
169
170};
171
Willy Tarreau80587432006-12-24 17:47:20 +0100172/* We must put the messages here since GCC cannot initialize consts depending
173 * on strlen().
174 */
175struct chunk http_err_chunks[HTTP_ERR_SIZE];
176
Willy Tarreau42250582007-04-01 01:30:43 +0200177#define FD_SETS_ARE_BITFIELDS
178#ifdef FD_SETS_ARE_BITFIELDS
179/*
180 * This map is used with all the FD_* macros to check whether a particular bit
181 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
182 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
183 * byte should be encoded. Be careful to always pass bytes from 0 to 255
184 * exclusively to the macros.
185 */
186fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
187fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
188
189#else
190#error "Check if your OS uses bitfields for fd_sets"
191#endif
192
Willy Tarreau80587432006-12-24 17:47:20 +0100193void init_proto_http()
194{
Willy Tarreau42250582007-04-01 01:30:43 +0200195 int i;
196 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100197 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200198
Willy Tarreau80587432006-12-24 17:47:20 +0100199 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
200 if (!http_err_msgs[msg]) {
201 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
202 abort();
203 }
204
205 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
206 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
207 }
Willy Tarreau42250582007-04-01 01:30:43 +0200208
209 /* initialize the log header encoding map : '{|}"#' should be encoded with
210 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
211 * URL encoding only requires '"', '#' to be encoded as well as non-
212 * printable characters above.
213 */
214 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
215 memset(url_encode_map, 0, sizeof(url_encode_map));
216 for (i = 0; i < 32; i++) {
217 FD_SET(i, hdr_encode_map);
218 FD_SET(i, url_encode_map);
219 }
220 for (i = 127; i < 256; i++) {
221 FD_SET(i, hdr_encode_map);
222 FD_SET(i, url_encode_map);
223 }
224
225 tmp = "\"#{|}";
226 while (*tmp) {
227 FD_SET(*tmp, hdr_encode_map);
228 tmp++;
229 }
230
231 tmp = "\"#";
232 while (*tmp) {
233 FD_SET(*tmp, url_encode_map);
234 tmp++;
235 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200236
237 /* memory allocations */
238 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200239 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100240}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200241
Willy Tarreau53b6c742006-12-17 13:37:46 +0100242/*
243 * We have 26 list of methods (1 per first letter), each of which can have
244 * up to 3 entries (2 valid, 1 null).
245 */
246struct http_method_desc {
247 http_meth_t meth;
248 int len;
249 const char text[8];
250};
251
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100252const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100253 ['C' - 'A'] = {
254 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
255 },
256 ['D' - 'A'] = {
257 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
258 },
259 ['G' - 'A'] = {
260 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
261 },
262 ['H' - 'A'] = {
263 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
264 },
265 ['P' - 'A'] = {
266 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
267 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
268 },
269 ['T' - 'A'] = {
270 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
271 },
272 /* rest is empty like this :
273 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
274 */
275};
276
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100277/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200278 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100279 * RFC2616 for those chars.
280 */
281
282const char http_is_spht[256] = {
283 [' '] = 1, ['\t'] = 1,
284};
285
286const char http_is_crlf[256] = {
287 ['\r'] = 1, ['\n'] = 1,
288};
289
290const char http_is_lws[256] = {
291 [' '] = 1, ['\t'] = 1,
292 ['\r'] = 1, ['\n'] = 1,
293};
294
295const char http_is_sep[256] = {
296 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
297 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
298 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
299 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
300 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
301};
302
303const char http_is_ctl[256] = {
304 [0 ... 31] = 1,
305 [127] = 1,
306};
307
308/*
309 * A token is any ASCII char that is neither a separator nor a CTL char.
310 * Do not overwrite values in assignment since gcc-2.95 will not handle
311 * them correctly. Instead, define every non-CTL char's status.
312 */
313const char http_is_token[256] = {
314 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
315 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
316 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
317 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
318 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
319 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
320 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
321 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
322 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
323 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
324 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
325 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
326 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
327 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
328 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
329 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
330 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
331 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
332 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
333 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
334 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
335 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
336 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
337 ['|'] = 1, ['}'] = 0, ['~'] = 1,
338};
339
340
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100341/*
342 * An http ver_token is any ASCII which can be found in an HTTP version,
343 * which includes 'H', 'T', 'P', '/', '.' and any digit.
344 */
345const char http_is_ver_token[256] = {
346 ['.'] = 1, ['/'] = 1,
347 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
348 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
349 ['H'] = 1, ['P'] = 1, ['T'] = 1,
350};
351
352
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100353/*
354 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
355 * CRLF. Text length is measured first, so it cannot be NULL.
356 * The header is also automatically added to the index <hdr_idx>, and the end
357 * of headers is automatically adjusted. The number of bytes added is returned
358 * on success, otherwise <0 is returned indicating an error.
359 */
360int http_header_add_tail(struct buffer *b, struct http_msg *msg,
361 struct hdr_idx *hdr_idx, const char *text)
362{
363 int bytes, len;
364
365 len = strlen(text);
366 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
367 if (!bytes)
368 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100369 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100370 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
371}
372
373/*
374 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
375 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
376 * the buffer is only opened and the space reserved, but nothing is copied.
377 * The header is also automatically added to the index <hdr_idx>, and the end
378 * of headers is automatically adjusted. The number of bytes added is returned
379 * on success, otherwise <0 is returned indicating an error.
380 */
381int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
382 struct hdr_idx *hdr_idx, const char *text, int len)
383{
384 int bytes;
385
386 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
387 if (!bytes)
388 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100389 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100390 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
391}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200392
393/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100394 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
395 * If so, returns the position of the first non-space character relative to
396 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
397 * to return a pointer to the place after the first space. Returns 0 if the
398 * header name does not match. Checks are case-insensitive.
399 */
400int http_header_match2(const char *hdr, const char *end,
401 const char *name, int len)
402{
403 const char *val;
404
405 if (hdr + len >= end)
406 return 0;
407 if (hdr[len] != ':')
408 return 0;
409 if (strncasecmp(hdr, name, len) != 0)
410 return 0;
411 val = hdr + len + 1;
412 while (val < end && HTTP_IS_SPHT(*val))
413 val++;
414 if ((val >= end) && (len + 2 <= end - hdr))
415 return len + 2; /* we may replace starting from second space */
416 return val - hdr;
417}
418
Willy Tarreau33a7e692007-06-10 19:45:56 +0200419/* Find the end of the header value contained between <s> and <e>.
420 * See RFC2616, par 2.2 for more information. Note that it requires
421 * a valid header to return a valid result.
422 */
423const char *find_hdr_value_end(const char *s, const char *e)
424{
425 int quoted, qdpair;
426
427 quoted = qdpair = 0;
428 for (; s < e; s++) {
429 if (qdpair) qdpair = 0;
430 else if (quoted && *s == '\\') qdpair = 1;
431 else if (quoted && *s == '"') quoted = 0;
432 else if (*s == '"') quoted = 1;
433 else if (*s == ',') return s;
434 }
435 return s;
436}
437
438/* Find the first or next occurrence of header <name> in message buffer <sol>
439 * using headers index <idx>, and return it in the <ctx> structure. This
440 * structure holds everything necessary to use the header and find next
441 * occurrence. If its <idx> member is 0, the header is searched from the
442 * beginning. Otherwise, the next occurrence is returned. The function returns
443 * 1 when it finds a value, and 0 when there is no more.
444 */
445int http_find_header2(const char *name, int len,
446 const char *sol, struct hdr_idx *idx,
447 struct hdr_ctx *ctx)
448{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200449 const char *eol, *sov;
450 int cur_idx;
451
452 if (ctx->idx) {
453 /* We have previously returned a value, let's search
454 * another one on the same line.
455 */
456 cur_idx = ctx->idx;
457 sol = ctx->line;
458 sov = sol + ctx->val + ctx->vlen;
459 eol = sol + idx->v[cur_idx].len;
460
461 if (sov >= eol)
462 /* no more values in this header */
463 goto next_hdr;
464
465 /* values remaining for this header, skip the comma */
466 sov++;
467 while (sov < eol && http_is_lws[(unsigned char)*sov])
468 sov++;
469
470 goto return_hdr;
471 }
472
473 /* first request for this header */
474 sol += hdr_idx_first_pos(idx);
475 cur_idx = hdr_idx_first_idx(idx);
476
477 while (cur_idx) {
478 eol = sol + idx->v[cur_idx].len;
479
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200480 if (len == 0) {
481 /* No argument was passed, we want any header.
482 * To achieve this, we simply build a fake request. */
483 while (sol + len < eol && sol[len] != ':')
484 len++;
485 name = sol;
486 }
487
Willy Tarreau33a7e692007-06-10 19:45:56 +0200488 if ((len < eol - sol) &&
489 (sol[len] == ':') &&
490 (strncasecmp(sol, name, len) == 0)) {
491
492 sov = sol + len + 1;
493 while (sov < eol && http_is_lws[(unsigned char)*sov])
494 sov++;
495 return_hdr:
496 ctx->line = sol;
497 ctx->idx = cur_idx;
498 ctx->val = sov - sol;
499
500 eol = find_hdr_value_end(sov, eol);
501 ctx->vlen = eol - sov;
502 return 1;
503 }
504 next_hdr:
505 sol = eol + idx->v[cur_idx].cr + 1;
506 cur_idx = idx->v[cur_idx].next;
507 }
508 return 0;
509}
510
511int http_find_header(const char *name,
512 const char *sol, struct hdr_idx *idx,
513 struct hdr_ctx *ctx)
514{
515 return http_find_header2(name, strlen(name), sol, idx, ctx);
516}
517
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100518/* This function handles a server error at the stream interface level. The
519 * stream interface is assumed to be already in a closed state. An optional
520 * message is copied into the input buffer, and an HTTP status code stored.
521 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100522 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200523 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100524static void http_server_error(struct session *t, struct stream_interface *si,
525 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526{
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100527 buffer_erase(si->ob);
528 buffer_erase(si->ib);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200529 buffer_auto_close(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100530 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100531 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100532 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200533 }
534 if (!(t->flags & SN_ERR_MASK))
535 t->flags |= err;
536 if (!(t->flags & SN_FINST_MASK))
537 t->flags |= finst;
538}
539
Willy Tarreau80587432006-12-24 17:47:20 +0100540/* This function returns the appropriate error location for the given session
541 * and message.
542 */
543
544struct chunk *error_message(struct session *s, int msgnum)
545{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200546 if (s->be->errmsg[msgnum].str)
547 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100548 else if (s->fe->errmsg[msgnum].str)
549 return &s->fe->errmsg[msgnum];
550 else
551 return &http_err_chunks[msgnum];
552}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200553
Willy Tarreau53b6c742006-12-17 13:37:46 +0100554/*
555 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
556 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
557 */
558static http_meth_t find_http_meth(const char *str, const int len)
559{
560 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100561 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100562
563 m = ((unsigned)*str - 'A');
564
565 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100566 for (h = http_methods[m]; h->len > 0; h++) {
567 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100568 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100569 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100570 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100571 };
572 return HTTP_METH_OTHER;
573 }
574 return HTTP_METH_NONE;
575
576}
577
Willy Tarreau21d2af32008-02-14 20:25:24 +0100578/* Parse the URI from the given transaction (which is assumed to be in request
579 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
580 * It is returned otherwise.
581 */
582static char *
583http_get_path(struct http_txn *txn)
584{
585 char *ptr, *end;
586
587 ptr = txn->req.sol + txn->req.sl.rq.u;
588 end = ptr + txn->req.sl.rq.u_l;
589
590 if (ptr >= end)
591 return NULL;
592
593 /* RFC2616, par. 5.1.2 :
594 * Request-URI = "*" | absuri | abspath | authority
595 */
596
597 if (*ptr == '*')
598 return NULL;
599
600 if (isalpha((unsigned char)*ptr)) {
601 /* this is a scheme as described by RFC3986, par. 3.1 */
602 ptr++;
603 while (ptr < end &&
604 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
605 ptr++;
606 /* skip '://' */
607 if (ptr == end || *ptr++ != ':')
608 return NULL;
609 if (ptr == end || *ptr++ != '/')
610 return NULL;
611 if (ptr == end || *ptr++ != '/')
612 return NULL;
613 }
614 /* skip [user[:passwd]@]host[:[port]] */
615
616 while (ptr < end && *ptr != '/')
617 ptr++;
618
619 if (ptr == end)
620 return NULL;
621
622 /* OK, we got the '/' ! */
623 return ptr;
624}
625
Willy Tarreauefb453c2008-10-26 20:49:47 +0100626/* Returns a 302 for a redirectable request. This may only be called just after
627 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
628 * left unchanged and will follow normal proxy processing.
629 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100630void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100631{
632 struct http_txn *txn;
633 struct chunk rdr;
634 char *path;
635 int len;
636
637 /* 1: create the response header */
638 rdr.len = strlen(HTTP_302);
639 rdr.str = trash;
640 memcpy(rdr.str, HTTP_302, rdr.len);
641
642 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200643 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100644 return;
645
646 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
647 rdr.len += s->srv->rdr_len;
648
649 /* 3: add the request URI */
650 txn = &s->txn;
651 path = http_get_path(txn);
652 if (!path)
653 return;
654
655 len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200656 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100657 return;
658
659 memcpy(rdr.str + rdr.len, path, len);
660 rdr.len += len;
661 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
662 rdr.len += 4;
663
664 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100665 si->shutr(si);
666 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100667 si->err_type = SI_ET_NONE;
668 si->err_loc = NULL;
669 si->state = SI_ST_CLO;
670
671 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100672 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100673
674 /* FIXME: we should increase a counter of redirects per server and per backend. */
675 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100676 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100677}
678
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100679/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100680 * that the server side is closed. Note that err_type is actually a
681 * bitmask, where almost only aborts may be cumulated with other
682 * values. We consider that aborted operations are more important
683 * than timeouts or errors due to the fact that nobody else in the
684 * logs might explain incomplete retries. All others should avoid
685 * being cumulated. It should normally not be possible to have multiple
686 * aborts at once, but just in case, the first one in sequence is reported.
687 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100688void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100689{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100690 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100691
692 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100693 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
694 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100695 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100696 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
697 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100698 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100699 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
700 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100701 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100702 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
703 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100704 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100705 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
706 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100707 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100708 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
709 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100710 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100711 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
712 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100713}
714
Willy Tarreau42250582007-04-01 01:30:43 +0200715extern const char sess_term_cond[8];
716extern const char sess_fin_state[8];
717extern const char *monthname[12];
718const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
719const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
720 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
721 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200722struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200723struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100724
Emeric Brun3a058f32009-06-30 18:26:00 +0200725void http_sess_clflog(struct session *s)
726{
727 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
728 struct proxy *fe = s->fe;
729 struct proxy *be = s->be;
730 struct proxy *prx_log;
731 struct http_txn *txn = &s->txn;
732 int tolog, level, err;
733 char *uri, *h;
734 char *svid;
735 struct tm tm;
736 static char tmpline[MAX_SYSLOG_LEN];
737 int hdr;
738 size_t w;
739 int t_request;
740
741 prx_log = fe;
742 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
743 (s->conn_retries != be->conn_retries) ||
744 txn->status >= 500;
745
746 if (s->cli_addr.ss_family == AF_INET)
747 inet_ntop(AF_INET,
748 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
749 pn, sizeof(pn));
750 else
751 inet_ntop(AF_INET6,
752 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
753 pn, sizeof(pn));
754
755 get_gmtime(s->logs.accept_date.tv_sec, &tm);
756
757 /* FIXME: let's limit ourselves to frontend logging for now. */
758 tolog = fe->to_log;
759
760 h = tmpline;
761
762 w = snprintf(h, sizeof(tmpline),
763 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
764 pn,
765 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
766 tm.tm_hour, tm.tm_min, tm.tm_sec);
767 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
768 goto trunc;
769 h += w;
770
771 if (h >= tmpline + sizeof(tmpline) - 4)
772 goto trunc;
773
774 *(h++) = ' ';
775 *(h++) = '\"';
776 uri = txn->uri ? txn->uri : "<BADREQ>";
777 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
778 '#', url_encode_map, uri);
779 *(h++) = '\"';
780
781 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
782 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
783 goto trunc;
784 h += w;
785
786 if (h >= tmpline + sizeof(tmpline) - 9)
787 goto trunc;
788 memcpy(h, " \"-\" \"-\"", 8);
789 h += 8;
790
791 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
792 " %d %03d",
793 (s->cli_addr.ss_family == AF_INET) ?
794 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
795 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
796 (int)s->logs.accept_date.tv_usec/1000);
797 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
798 goto trunc;
799 h += w;
800
801 w = strlen(fe->id);
802 if (h >= tmpline + sizeof(tmpline) - 4 - w)
803 goto trunc;
804 *(h++) = ' ';
805 *(h++) = '\"';
806 memcpy(h, fe->id, w);
807 h += w;
808 *(h++) = '\"';
809
810 w = strlen(be->id);
811 if (h >= tmpline + sizeof(tmpline) - 4 - w)
812 goto trunc;
813 *(h++) = ' ';
814 *(h++) = '\"';
815 memcpy(h, be->id, w);
816 h += w;
817 *(h++) = '\"';
818
819 svid = (tolog & LW_SVID) ?
820 (s->data_source != DATA_SRC_STATS) ?
821 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
822
823 w = strlen(svid);
824 if (h >= tmpline + sizeof(tmpline) - 4 - w)
825 goto trunc;
826 *(h++) = ' ';
827 *(h++) = '\"';
828 memcpy(h, svid, w);
829 h += w;
830 *(h++) = '\"';
831
832 t_request = -1;
833 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
834 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
835 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
836 " %d %ld %ld %ld %ld",
837 t_request,
838 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
839 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
840 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
841 s->logs.t_close);
842 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
843 goto trunc;
844 h += w;
845
846 if (h >= tmpline + sizeof(tmpline) - 8)
847 goto trunc;
848 *(h++) = ' ';
849 *(h++) = '\"';
850 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
851 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
852 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
853 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
854 *(h++) = '\"';
855
856 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
857 " %d %d %d %d %d %ld %ld",
858 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
859 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
860 s->logs.srv_queue_size, s->logs.prx_queue_size);
861
862 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
863 goto trunc;
864 h += w;
865
866 if (txn->cli_cookie) {
867 w = strlen(txn->cli_cookie);
868 if (h >= tmpline + sizeof(tmpline) - 4 - w)
869 goto trunc;
870 *(h++) = ' ';
871 *(h++) = '\"';
872 memcpy(h, txn->cli_cookie, w);
873 h += w;
874 *(h++) = '\"';
875 } else {
876 if (h >= tmpline + sizeof(tmpline) - 5)
877 goto trunc;
878 memcpy(h, " \"-\"", 4);
879 h += 4;
880 }
881
882 if (txn->srv_cookie) {
883 w = strlen(txn->srv_cookie);
884 if (h >= tmpline + sizeof(tmpline) - 4 - w)
885 goto trunc;
886 *(h++) = ' ';
887 *(h++) = '\"';
888 memcpy(h, txn->srv_cookie, w);
889 h += w;
890 *(h++) = '\"';
891 } else {
892 if (h >= tmpline + sizeof(tmpline) - 5)
893 goto trunc;
894 memcpy(h, " \"-\"", 4);
895 h += 4;
896 }
897
898 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
899 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
900 if (h >= sizeof (tmpline) + tmpline - 4)
901 goto trunc;
902 *(h++) = ' ';
903 *(h++) = '\"';
904 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
905 '#', hdr_encode_map, txn->req.cap[hdr]);
906 *(h++) = '\"';
907 }
908 }
909
910 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
911 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
912 if (h >= sizeof (tmpline) + tmpline - 4)
913 goto trunc;
914 *(h++) = ' ';
915 *(h++) = '\"';
916 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
917 '#', hdr_encode_map, txn->rsp.cap[hdr]);
918 *(h++) = '\"';
919 }
920 }
921
922trunc:
923 *h = '\0';
924
925 level = LOG_INFO;
926 if (err && (fe->options2 & PR_O2_LOGERRORS))
927 level = LOG_ERR;
928
929 send_log(prx_log, level, "%s\n", tmpline);
930
931 s->logs.logwait = 0;
932}
933
Willy Tarreau42250582007-04-01 01:30:43 +0200934/*
935 * send a log for the session when we have enough info about it.
936 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100937 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100938void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200939{
940 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
941 struct proxy *fe = s->fe;
942 struct proxy *be = s->be;
943 struct proxy *prx_log;
944 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200945 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200946 char *uri, *h;
947 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200948 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200949 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200950 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200951 int hdr;
952
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200953 /* if we don't want to log normal traffic, return now */
954 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
955 (s->conn_retries != be->conn_retries) ||
956 txn->status >= 500;
957 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
958 return;
959
Willy Tarreau42250582007-04-01 01:30:43 +0200960 if (fe->logfac1 < 0 && fe->logfac2 < 0)
961 return;
962 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100963
Emeric Brun3a058f32009-06-30 18:26:00 +0200964 if (prx_log->options2 & PR_O2_CLFLOG)
965 return http_sess_clflog(s);
966
Willy Tarreau42250582007-04-01 01:30:43 +0200967 if (s->cli_addr.ss_family == AF_INET)
968 inet_ntop(AF_INET,
969 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
970 pn, sizeof(pn));
971 else
972 inet_ntop(AF_INET6,
973 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
974 pn, sizeof(pn));
975
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200976 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200977
978 /* FIXME: let's limit ourselves to frontend logging for now. */
979 tolog = fe->to_log;
980
981 h = tmpline;
982 if (fe->to_log & LW_REQHDR &&
983 txn->req.cap &&
984 (h < tmpline + sizeof(tmpline) - 10)) {
985 *(h++) = ' ';
986 *(h++) = '{';
987 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
988 if (hdr)
989 *(h++) = '|';
990 if (txn->req.cap[hdr] != NULL)
991 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
992 '#', hdr_encode_map, txn->req.cap[hdr]);
993 }
994 *(h++) = '}';
995 }
996
997 if (fe->to_log & LW_RSPHDR &&
998 txn->rsp.cap &&
999 (h < tmpline + sizeof(tmpline) - 7)) {
1000 *(h++) = ' ';
1001 *(h++) = '{';
1002 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1003 if (hdr)
1004 *(h++) = '|';
1005 if (txn->rsp.cap[hdr] != NULL)
1006 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1007 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1008 }
1009 *(h++) = '}';
1010 }
1011
1012 if (h < tmpline + sizeof(tmpline) - 4) {
1013 *(h++) = ' ';
1014 *(h++) = '"';
1015 uri = txn->uri ? txn->uri : "<BADREQ>";
1016 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1017 '#', url_encode_map, uri);
1018 *(h++) = '"';
1019 }
1020 *h = '\0';
1021
1022 svid = (tolog & LW_SVID) ?
1023 (s->data_source != DATA_SRC_STATS) ?
1024 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1025
Willy Tarreau70089872008-06-13 21:12:51 +02001026 t_request = -1;
1027 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1028 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1029
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001030 level = LOG_INFO;
1031 if (err && (fe->options2 & PR_O2_LOGERRORS))
1032 level = LOG_ERR;
1033
1034 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001035 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001036 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1037 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001038 pn,
1039 (s->cli_addr.ss_family == AF_INET) ?
1040 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1041 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001042 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001043 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001044 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001045 t_request,
1046 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001047 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1048 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1049 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1050 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001051 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001052 txn->cli_cookie ? txn->cli_cookie : "-",
1053 txn->srv_cookie ? txn->srv_cookie : "-",
1054 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1055 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1056 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1057 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1058 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001059 (s->flags & SN_REDISP)?"+":"",
1060 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001061 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1062
1063 s->logs.logwait = 0;
1064}
1065
Willy Tarreau117f59e2007-03-04 18:17:17 +01001066
1067/*
1068 * Capture headers from message starting at <som> according to header list
1069 * <cap_hdr>, and fill the <idx> structure appropriately.
1070 */
1071void capture_headers(char *som, struct hdr_idx *idx,
1072 char **cap, struct cap_hdr *cap_hdr)
1073{
1074 char *eol, *sol, *col, *sov;
1075 int cur_idx;
1076 struct cap_hdr *h;
1077 int len;
1078
1079 sol = som + hdr_idx_first_pos(idx);
1080 cur_idx = hdr_idx_first_idx(idx);
1081
1082 while (cur_idx) {
1083 eol = sol + idx->v[cur_idx].len;
1084
1085 col = sol;
1086 while (col < eol && *col != ':')
1087 col++;
1088
1089 sov = col + 1;
1090 while (sov < eol && http_is_lws[(unsigned char)*sov])
1091 sov++;
1092
1093 for (h = cap_hdr; h; h = h->next) {
1094 if ((h->namelen == col - sol) &&
1095 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1096 if (cap[h->index] == NULL)
1097 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001098 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001099
1100 if (cap[h->index] == NULL) {
1101 Alert("HTTP capture : out of memory.\n");
1102 continue;
1103 }
1104
1105 len = eol - sov;
1106 if (len > h->len)
1107 len = h->len;
1108
1109 memcpy(cap[h->index], sov, len);
1110 cap[h->index][len]=0;
1111 }
1112 }
1113 sol = eol + idx->v[cur_idx].cr + 1;
1114 cur_idx = idx->v[cur_idx].next;
1115 }
1116}
1117
1118
Willy Tarreau42250582007-04-01 01:30:43 +02001119/* either we find an LF at <ptr> or we jump to <bad>.
1120 */
1121#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1122
1123/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1124 * otherwise to <http_msg_ood> with <state> set to <st>.
1125 */
1126#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1127 ptr++; \
1128 if (likely(ptr < end)) \
1129 goto good; \
1130 else { \
1131 state = (st); \
1132 goto http_msg_ood; \
1133 } \
1134 } while (0)
1135
1136
Willy Tarreaubaaee002006-06-26 02:48:02 +02001137/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001138 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001139 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1140 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1141 * will give undefined results.
1142 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1143 * and that msg->sol points to the beginning of the response.
1144 * If a complete line is found (which implies that at least one CR or LF is
1145 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1146 * returned indicating an incomplete line (which does not mean that parts have
1147 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1148 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1149 * upon next call.
1150 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001151 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001152 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1153 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001154 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001155 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001156const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1157 unsigned int state, const char *ptr, const char *end,
1158 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001159{
Willy Tarreau8973c702007-01-21 23:58:29 +01001160 switch (state) {
1161 http_msg_rpver:
1162 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001163 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001164 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1165
1166 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001167 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1169 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001170 state = HTTP_MSG_ERROR;
1171 break;
1172
Willy Tarreau8973c702007-01-21 23:58:29 +01001173 http_msg_rpver_sp:
1174 case HTTP_MSG_RPVER_SP:
1175 if (likely(!HTTP_IS_LWS(*ptr))) {
1176 msg->sl.st.c = ptr - msg_buf;
1177 goto http_msg_rpcode;
1178 }
1179 if (likely(HTTP_IS_SPHT(*ptr)))
1180 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1181 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001182 state = HTTP_MSG_ERROR;
1183 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001184
1185 http_msg_rpcode:
1186 case HTTP_MSG_RPCODE:
1187 if (likely(!HTTP_IS_LWS(*ptr)))
1188 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1189
1190 if (likely(HTTP_IS_SPHT(*ptr))) {
1191 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1192 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1193 }
1194
1195 /* so it's a CR/LF, so there is no reason phrase */
1196 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1197 http_msg_rsp_reason:
1198 /* FIXME: should we support HTTP responses without any reason phrase ? */
1199 msg->sl.st.r = ptr - msg_buf;
1200 msg->sl.st.r_l = 0;
1201 goto http_msg_rpline_eol;
1202
1203 http_msg_rpcode_sp:
1204 case HTTP_MSG_RPCODE_SP:
1205 if (likely(!HTTP_IS_LWS(*ptr))) {
1206 msg->sl.st.r = ptr - msg_buf;
1207 goto http_msg_rpreason;
1208 }
1209 if (likely(HTTP_IS_SPHT(*ptr)))
1210 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1211 /* so it's a CR/LF, so there is no reason phrase */
1212 goto http_msg_rsp_reason;
1213
1214 http_msg_rpreason:
1215 case HTTP_MSG_RPREASON:
1216 if (likely(!HTTP_IS_CRLF(*ptr)))
1217 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1218 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1219 http_msg_rpline_eol:
1220 /* We have seen the end of line. Note that we do not
1221 * necessarily have the \n yet, but at least we know that we
1222 * have EITHER \r OR \n, otherwise the response would not be
1223 * complete. We can then record the response length and return
1224 * to the caller which will be able to register it.
1225 */
1226 msg->sl.st.l = ptr - msg->sol;
1227 return ptr;
1228
1229#ifdef DEBUG_FULL
1230 default:
1231 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1232 exit(1);
1233#endif
1234 }
1235
1236 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001237 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001238 if (ret_state)
1239 *ret_state = state;
1240 if (ret_ptr)
1241 *ret_ptr = (char *)ptr;
1242 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001243}
1244
1245
1246/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 * This function parses a request line between <ptr> and <end>, starting with
1248 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1249 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1250 * will give undefined results.
1251 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1252 * and that msg->sol points to the beginning of the request.
1253 * If a complete line is found (which implies that at least one CR or LF is
1254 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1255 * returned indicating an incomplete line (which does not mean that parts have
1256 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1257 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1258 * upon next call.
1259 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001260 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1262 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001263 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001264 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001265const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1266 unsigned int state, const char *ptr, const char *end,
1267 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001268{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001269 switch (state) {
1270 http_msg_rqmeth:
1271 case HTTP_MSG_RQMETH:
1272 if (likely(HTTP_IS_TOKEN(*ptr)))
1273 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001274
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001276 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1278 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001279
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001280 if (likely(HTTP_IS_CRLF(*ptr))) {
1281 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001282 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001283 http_msg_req09_uri:
1284 msg->sl.rq.u = ptr - msg_buf;
1285 http_msg_req09_uri_e:
1286 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1287 http_msg_req09_ver:
1288 msg->sl.rq.v = ptr - msg_buf;
1289 msg->sl.rq.v_l = 0;
1290 goto http_msg_rqline_eol;
1291 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001292 state = HTTP_MSG_ERROR;
1293 break;
1294
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 http_msg_rqmeth_sp:
1296 case HTTP_MSG_RQMETH_SP:
1297 if (likely(!HTTP_IS_LWS(*ptr))) {
1298 msg->sl.rq.u = ptr - msg_buf;
1299 goto http_msg_rquri;
1300 }
1301 if (likely(HTTP_IS_SPHT(*ptr)))
1302 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1303 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1304 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 http_msg_rquri:
1307 case HTTP_MSG_RQURI:
1308 if (likely(!HTTP_IS_LWS(*ptr)))
1309 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001311 if (likely(HTTP_IS_SPHT(*ptr))) {
1312 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1313 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1314 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001315
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001316 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1317 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001318
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001319 http_msg_rquri_sp:
1320 case HTTP_MSG_RQURI_SP:
1321 if (likely(!HTTP_IS_LWS(*ptr))) {
1322 msg->sl.rq.v = ptr - msg_buf;
1323 goto http_msg_rqver;
1324 }
1325 if (likely(HTTP_IS_SPHT(*ptr)))
1326 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1327 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1328 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001330 http_msg_rqver:
1331 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001332 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001333 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001334
1335 if (likely(HTTP_IS_CRLF(*ptr))) {
1336 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1337 http_msg_rqline_eol:
1338 /* We have seen the end of line. Note that we do not
1339 * necessarily have the \n yet, but at least we know that we
1340 * have EITHER \r OR \n, otherwise the request would not be
1341 * complete. We can then record the request length and return
1342 * to the caller which will be able to register it.
1343 */
1344 msg->sl.rq.l = ptr - msg->sol;
1345 return ptr;
1346 }
1347
1348 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001349 state = HTTP_MSG_ERROR;
1350 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001351
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352#ifdef DEBUG_FULL
1353 default:
1354 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1355 exit(1);
1356#endif
1357 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001358
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001359 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001360 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001361 if (ret_state)
1362 *ret_state = state;
1363 if (ret_ptr)
1364 *ret_ptr = (char *)ptr;
1365 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001366}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001367
1368
Willy Tarreau8973c702007-01-21 23:58:29 +01001369/*
1370 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001371 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001372 * when data are missing and recalled at the exact same location with no
1373 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001374 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
1375 * fields.
Willy Tarreau8973c702007-01-21 23:58:29 +01001376 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001377void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1378{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001379 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001380 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001381
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001382 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 ptr = buf->lr;
1384 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001385
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 if (unlikely(ptr >= end))
1387 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001390 /*
1391 * First, states that are specific to the response only.
1392 * We check them first so that request and headers are
1393 * closer to each other (accessed more often).
1394 */
1395 http_msg_rpbefore:
1396 case HTTP_MSG_RPBEFORE:
1397 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau816b9792009-09-15 21:25:21 +02001398#if !defined(PARSE_PRESERVE_EMPTY_LINES)
1399 if (likely(ptr != buf->data)) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001400 /* Remove empty leading lines, as recommended by
1401 * RFC2616. This takes a lot of time because we
1402 * must move all the buffer backwards, but this
1403 * is rarely needed. The method above will be
1404 * cleaner when we'll be able to start sending
1405 * the request from any place in the buffer.
1406 */
Willy Tarreau816b9792009-09-15 21:25:21 +02001407 ptr += buffer_replace2(buf, buf->lr, ptr, NULL, 0);
Willy Tarreau8973c702007-01-21 23:58:29 +01001408 end = buf->r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001409 }
Willy Tarreau816b9792009-09-15 21:25:21 +02001410#endif
1411 msg->sol = ptr;
1412 msg->som = ptr - buf->data;
Willy Tarreau8973c702007-01-21 23:58:29 +01001413 hdr_idx_init(idx);
1414 state = HTTP_MSG_RPVER;
1415 goto http_msg_rpver;
1416 }
1417
1418 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1419 goto http_msg_invalid;
1420
1421 if (unlikely(*ptr == '\n'))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1423 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1424 /* stop here */
1425
1426 http_msg_rpbefore_cr:
1427 case HTTP_MSG_RPBEFORE_CR:
1428 EXPECT_LF_HERE(ptr, http_msg_invalid);
1429 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1430 /* stop here */
1431
1432 http_msg_rpver:
1433 case HTTP_MSG_RPVER:
1434 case HTTP_MSG_RPVER_SP:
1435 case HTTP_MSG_RPCODE:
1436 case HTTP_MSG_RPCODE_SP:
1437 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001438 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001439 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001440 if (unlikely(!ptr))
1441 return;
1442
1443 /* we have a full response and we know that we have either a CR
1444 * or an LF at <ptr>.
1445 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001446 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001447 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1448
1449 msg->sol = ptr;
1450 if (likely(*ptr == '\r'))
1451 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1452 goto http_msg_rpline_end;
1453
1454 http_msg_rpline_end:
1455 case HTTP_MSG_RPLINE_END:
1456 /* msg->sol must point to the first of CR or LF. */
1457 EXPECT_LF_HERE(ptr, http_msg_invalid);
1458 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1459 /* stop here */
1460
1461 /*
1462 * Second, states that are specific to the request only
1463 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 http_msg_rqbefore:
1465 case HTTP_MSG_RQBEFORE:
1466 if (likely(HTTP_IS_TOKEN(*ptr))) {
1467 if (likely(ptr == buf->data)) {
1468 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001469 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 } else {
1471#if PARSE_PRESERVE_EMPTY_LINES
1472 /* only skip empty leading lines, don't remove them */
1473 msg->sol = ptr;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001474 msg->som = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001475#else
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 /* Remove empty leading lines, as recommended by
1477 * RFC2616. This takes a lot of time because we
1478 * must move all the buffer backwards, but this
1479 * is rarely needed. The method above will be
1480 * cleaner when we'll be able to start sending
1481 * the request from any place in the buffer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001482 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 buf->lr = ptr;
1484 buffer_replace2(buf, buf->data, buf->lr, NULL, 0);
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001485 msg->som = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 msg->sol = buf->data;
1487 ptr = buf->data;
1488 end = buf->r;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001489#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 }
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001491 /* we will need this when keep-alive will be supported
1492 hdr_idx_init(idx);
1493 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001494 state = HTTP_MSG_RQMETH;
1495 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001497
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1499 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001500
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 if (unlikely(*ptr == '\n'))
1502 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1503 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001504 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001505
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 http_msg_rqbefore_cr:
1507 case HTTP_MSG_RQBEFORE_CR:
1508 EXPECT_LF_HERE(ptr, http_msg_invalid);
1509 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001510 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001511
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 http_msg_rqmeth:
1513 case HTTP_MSG_RQMETH:
1514 case HTTP_MSG_RQMETH_SP:
1515 case HTTP_MSG_RQURI:
1516 case HTTP_MSG_RQURI_SP:
1517 case HTTP_MSG_RQVER:
1518 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001519 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 if (unlikely(!ptr))
1521 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001522
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 /* we have a full request and we know that we have either a CR
1524 * or an LF at <ptr>.
1525 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001526 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001528
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001529 msg->sol = ptr;
1530 if (likely(*ptr == '\r'))
1531 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001533
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 http_msg_rqline_end:
1535 case HTTP_MSG_RQLINE_END:
1536 /* check for HTTP/0.9 request : no version information available.
1537 * msg->sol must point to the first of CR or LF.
1538 */
1539 if (unlikely(msg->sl.rq.v_l == 0))
1540 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001541
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 EXPECT_LF_HERE(ptr, http_msg_invalid);
1543 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001544 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001545
Willy Tarreau8973c702007-01-21 23:58:29 +01001546 /*
1547 * Common states below
1548 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 http_msg_hdr_first:
1550 case HTTP_MSG_HDR_FIRST:
1551 msg->sol = ptr;
1552 if (likely(!HTTP_IS_CRLF(*ptr))) {
1553 goto http_msg_hdr_name;
1554 }
1555
1556 if (likely(*ptr == '\r'))
1557 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1558 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 http_msg_hdr_name:
1561 case HTTP_MSG_HDR_NAME:
1562 /* assumes msg->sol points to the first char */
1563 if (likely(HTTP_IS_TOKEN(*ptr)))
1564 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001565
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 if (likely(*ptr == ':')) {
1567 msg->col = ptr - buf->data;
1568 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1569 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001570
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001571 if (likely(msg->err_pos < -1) || *ptr == '\n')
1572 goto http_msg_invalid;
1573
1574 if (msg->err_pos == -1) /* capture error pointer */
1575 msg->err_pos = ptr - buf->data; /* >= 0 now */
1576
1577 /* and we still accept this non-token character */
1578 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001579
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 http_msg_hdr_l1_sp:
1581 case HTTP_MSG_HDR_L1_SP:
1582 /* assumes msg->sol points to the first char and msg->col to the colon */
1583 if (likely(HTTP_IS_SPHT(*ptr)))
1584 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001585
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 /* header value can be basically anything except CR/LF */
1587 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001588
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 if (likely(!HTTP_IS_CRLF(*ptr))) {
1590 goto http_msg_hdr_val;
1591 }
1592
1593 if (likely(*ptr == '\r'))
1594 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1595 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001596
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 http_msg_hdr_l1_lf:
1598 case HTTP_MSG_HDR_L1_LF:
1599 EXPECT_LF_HERE(ptr, http_msg_invalid);
1600 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001601
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602 http_msg_hdr_l1_lws:
1603 case HTTP_MSG_HDR_L1_LWS:
1604 if (likely(HTTP_IS_SPHT(*ptr))) {
1605 /* replace HT,CR,LF with spaces */
1606 for (; buf->data+msg->sov < ptr; msg->sov++)
1607 buf->data[msg->sov] = ' ';
1608 goto http_msg_hdr_l1_sp;
1609 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001610 /* we had a header consisting only in spaces ! */
1611 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001612 goto http_msg_complete_header;
1613
1614 http_msg_hdr_val:
1615 case HTTP_MSG_HDR_VAL:
1616 /* assumes msg->sol points to the first char, msg->col to the
1617 * colon, and msg->sov points to the first character of the
1618 * value.
1619 */
1620 if (likely(!HTTP_IS_CRLF(*ptr)))
1621 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 msg->eol = ptr;
1624 /* Note: we could also copy eol into ->eoh so that we have the
1625 * real header end in case it ends with lots of LWS, but is this
1626 * really needed ?
1627 */
1628 if (likely(*ptr == '\r'))
1629 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1630 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 http_msg_hdr_l2_lf:
1633 case HTTP_MSG_HDR_L2_LF:
1634 EXPECT_LF_HERE(ptr, http_msg_invalid);
1635 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 http_msg_hdr_l2_lws:
1638 case HTTP_MSG_HDR_L2_LWS:
1639 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1640 /* LWS: replace HT,CR,LF with spaces */
1641 for (; msg->eol < ptr; msg->eol++)
1642 *msg->eol = ' ';
1643 goto http_msg_hdr_val;
1644 }
1645 http_msg_complete_header:
1646 /*
1647 * It was a new header, so the last one is finished.
1648 * Assumes msg->sol points to the first char, msg->col to the
1649 * colon, msg->sov points to the first character of the value
1650 * and msg->eol to the first CR or LF so we know how the line
1651 * ends. We insert last header into the index.
1652 */
1653 /*
1654 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1655 write(2, msg->sol, msg->eol-msg->sol);
1656 fprintf(stderr,"\n");
1657 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001658
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001659 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1660 idx, idx->tail) < 0))
1661 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001662
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 msg->sol = ptr;
1664 if (likely(!HTTP_IS_CRLF(*ptr))) {
1665 goto http_msg_hdr_name;
1666 }
1667
1668 if (likely(*ptr == '\r'))
1669 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1670 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001671
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001672 http_msg_last_lf:
1673 case HTTP_MSG_LAST_LF:
1674 /* Assumes msg->sol points to the first of either CR or LF */
1675 EXPECT_LF_HERE(ptr, http_msg_invalid);
1676 ptr++;
1677 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001678 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001680 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 return;
1682#ifdef DEBUG_FULL
1683 default:
1684 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1685 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001686#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001687 }
1688 http_msg_ood:
1689 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001690 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001691 buf->lr = ptr;
1692 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001693
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 http_msg_invalid:
1695 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001696 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001697 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 return;
1699}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001700
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001701/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1702 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1703 * nothing is done and 1 is returned.
1704 */
1705static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1706{
1707 int delta;
1708 char *cur_end;
1709
1710 if (msg->sl.rq.v_l != 0)
1711 return 1;
1712
1713 msg->sol = req->data + msg->som;
1714 cur_end = msg->sol + msg->sl.rq.l;
1715 delta = 0;
1716
1717 if (msg->sl.rq.u_l == 0) {
1718 /* if no URI was set, add "/" */
1719 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1720 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001721 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001722 }
1723 /* add HTTP version */
1724 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001725 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001726 cur_end += delta;
1727 cur_end = (char *)http_parse_reqline(msg, req->data,
1728 HTTP_MSG_RQMETH,
1729 msg->sol, cur_end + 1,
1730 NULL, NULL);
1731 if (unlikely(!cur_end))
1732 return 0;
1733
1734 /* we have a full HTTP/1.0 request now and we know that
1735 * we have either a CR or an LF at <ptr>.
1736 */
1737 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1738 return 1;
1739}
1740
Willy Tarreaud787e662009-07-07 10:14:51 +02001741/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1742 * processing can continue on next analysers, or zero if it either needs more
1743 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1744 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1745 * when it has nothing left to do, and may remove any analyser when it wants to
1746 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 */
Willy Tarreau3a816292009-07-07 10:55:49 +02001748int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749{
Willy Tarreau59234e92008-11-30 23:51:27 +01001750 /*
1751 * We will parse the partial (or complete) lines.
1752 * We will check the request syntax, and also join multi-line
1753 * headers. An index of all the lines will be elaborated while
1754 * parsing.
1755 *
1756 * For the parsing, we use a 28 states FSM.
1757 *
1758 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01001759 * req->data + msg->som = beginning of request
Willy Tarreau59234e92008-11-30 23:51:27 +01001760 * req->data + req->eoh = end of processed headers / start of current one
1761 * req->data + req->eol = end of current header or line (LF or CRLF)
1762 * req->lr = first non-visited byte
1763 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02001764 *
1765 * At end of parsing, we may perform a capture of the error (if any), and
1766 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
1767 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1768 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001769 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001770
Willy Tarreau59234e92008-11-30 23:51:27 +01001771 int cur_idx;
1772 struct http_txn *txn = &s->txn;
1773 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001774 struct hdr_ctx ctx;
1775 int conn_ka, conn_cl;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001776
Willy Tarreau6bf17362009-02-24 10:48:35 +01001777 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1778 now_ms, __FUNCTION__,
1779 s,
1780 req,
1781 req->rex, req->wex,
1782 req->flags,
1783 req->l,
1784 req->analysers);
1785
Willy Tarreau52a0c602009-08-16 22:45:38 +02001786 /* we're speaking HTTP here, so let's speak HTTP to the client */
1787 s->srv_error = http_return_srv_error;
1788
Willy Tarreau59234e92008-11-30 23:51:27 +01001789 if (likely(req->lr < req->r))
1790 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001791
Willy Tarreau59234e92008-11-30 23:51:27 +01001792 /* 1: we might have to print this header in debug mode */
1793 if (unlikely((global.mode & MODE_DEBUG) &&
1794 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01001795 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001796 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001797
Willy Tarreau59234e92008-11-30 23:51:27 +01001798 sol = req->data + msg->som;
1799 eol = sol + msg->sl.rq.l;
1800 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001801
Willy Tarreau59234e92008-11-30 23:51:27 +01001802 sol += hdr_idx_first_pos(&txn->hdr_idx);
1803 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001804
Willy Tarreau59234e92008-11-30 23:51:27 +01001805 while (cur_idx) {
1806 eol = sol + txn->hdr_idx.v[cur_idx].len;
1807 debug_hdr("clihdr", s, sol, eol);
1808 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1809 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001810 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001811 }
1812
Willy Tarreau58f10d72006-12-04 02:26:12 +01001813
Willy Tarreau59234e92008-11-30 23:51:27 +01001814 /*
1815 * Now we quickly check if we have found a full valid request.
1816 * If not so, we check the FD and buffer states before leaving.
1817 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001818 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01001819 * requests are checked first.
1820 *
1821 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001822
Willy Tarreau655dce92009-11-08 13:10:58 +01001823 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001824 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001825 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001826 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001827 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
1828 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001829
Willy Tarreau59234e92008-11-30 23:51:27 +01001830 /* 1: Since we are in header mode, if there's no space
1831 * left for headers, we won't be able to free more
1832 * later, so the session will never terminate. We
1833 * must terminate it now.
1834 */
1835 if (unlikely(req->flags & BF_FULL)) {
1836 /* FIXME: check if URI is set and return Status
1837 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001838 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001839 goto return_bad_req;
1840 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001841
Willy Tarreau59234e92008-11-30 23:51:27 +01001842 /* 2: have we encountered a read error ? */
1843 else if (req->flags & BF_READ_ERROR) {
1844 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02001845 if (msg->err_pos >= 0)
1846 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001847 msg->msg_state = HTTP_MSG_ERROR;
1848 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001849
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001850 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001851 if (s->listener->counters)
1852 s->listener->counters->failed_req++;
1853
Willy Tarreau59234e92008-11-30 23:51:27 +01001854 if (!(s->flags & SN_ERR_MASK))
1855 s->flags |= SN_ERR_CLICL;
1856 if (!(s->flags & SN_FINST_MASK))
1857 s->flags |= SN_FINST_R;
1858 return 0;
1859 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001860
Willy Tarreau59234e92008-11-30 23:51:27 +01001861 /* 3: has the read timeout expired ? */
1862 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
1863 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02001864 if (msg->err_pos >= 0)
1865 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001866 txn->status = 408;
1867 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
1868 msg->msg_state = HTTP_MSG_ERROR;
1869 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001870
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001871 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001872 if (s->listener->counters)
1873 s->listener->counters->failed_req++;
1874
Willy Tarreau59234e92008-11-30 23:51:27 +01001875 if (!(s->flags & SN_ERR_MASK))
1876 s->flags |= SN_ERR_CLITO;
1877 if (!(s->flags & SN_FINST_MASK))
1878 s->flags |= SN_FINST_R;
1879 return 0;
1880 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001881
Willy Tarreau59234e92008-11-30 23:51:27 +01001882 /* 4: have we encountered a close ? */
1883 else if (req->flags & BF_SHUTR) {
Willy Tarreau4076a152009-04-02 15:18:36 +02001884 if (msg->err_pos >= 0)
1885 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001886 txn->status = 400;
1887 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
1888 msg->msg_state = HTTP_MSG_ERROR;
1889 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001890
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02001891 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001892 if (s->listener->counters)
1893 s->listener->counters->failed_req++;
1894
Willy Tarreau59234e92008-11-30 23:51:27 +01001895 if (!(s->flags & SN_ERR_MASK))
1896 s->flags |= SN_ERR_CLICL;
1897 if (!(s->flags & SN_FINST_MASK))
1898 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001899 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001900 }
1901
Willy Tarreau520d95e2009-09-19 21:04:57 +02001902 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001903 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
1904
Willy Tarreau59234e92008-11-30 23:51:27 +01001905 /* just set the request timeout once at the beginning of the request */
1906 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02001907 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001908
Willy Tarreau59234e92008-11-30 23:51:27 +01001909 /* we're not ready yet */
1910 return 0;
1911 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001912
Willy Tarreaud787e662009-07-07 10:14:51 +02001913 /* OK now we have a complete HTTP request with indexed headers. Let's
1914 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01001915 * later. At this point, we have the last CRLF at req->data + msg->eoh.
1916 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
1917 * points to the CRLF of the request line. req->lr points to the first
1918 * byte after the last LF. msg->col and msg->sov point to the first
1919 * byte of data. msg->eol cannot be trusted because it may have been
1920 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001921 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001922
Willy Tarreaud787e662009-07-07 10:14:51 +02001923 /* Maybe we found in invalid header name while we were configured not
1924 * to block on that, so we have to capture it now.
1925 */
1926 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02001927 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
1928
Willy Tarreau59234e92008-11-30 23:51:27 +01001929 /* ensure we keep this pointer to the beginning of the message */
1930 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001931
Willy Tarreau59234e92008-11-30 23:51:27 +01001932 /*
1933 * 1: identify the method
1934 */
1935 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
1936
1937 /* we can make use of server redirect on GET and HEAD */
1938 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1939 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001940
Willy Tarreau59234e92008-11-30 23:51:27 +01001941 /*
1942 * 2: check if the URI matches the monitor_uri.
1943 * We have to do this for every request which gets in, because
1944 * the monitor-uri is defined by the frontend.
1945 */
1946 if (unlikely((s->fe->monitor_uri_len != 0) &&
1947 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
1948 !memcmp(&req->data[msg->sl.rq.u],
1949 s->fe->monitor_uri,
1950 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001951 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001952 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001953 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001954 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001955
Willy Tarreau59234e92008-11-30 23:51:27 +01001956 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001957
Willy Tarreau59234e92008-11-30 23:51:27 +01001958 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02001959 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
1960 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02001961
Willy Tarreau59234e92008-11-30 23:51:27 +01001962 ret = acl_pass(ret);
1963 if (cond->pol == ACL_COND_UNLESS)
1964 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001965
Willy Tarreau59234e92008-11-30 23:51:27 +01001966 if (ret) {
1967 /* we fail this request, let's return 503 service unavail */
1968 txn->status = 503;
1969 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
1970 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001971 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001972 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001973
Willy Tarreau59234e92008-11-30 23:51:27 +01001974 /* nothing to fail, let's reply normaly */
1975 txn->status = 200;
1976 stream_int_retnclose(req->prod, &http_200_chunk);
1977 goto return_prx_cond;
1978 }
1979
1980 /*
1981 * 3: Maybe we have to copy the original REQURI for the logs ?
1982 * Note: we cannot log anymore if the request has been
1983 * classified as invalid.
1984 */
1985 if (unlikely(s->logs.logwait & LW_REQ)) {
1986 /* we have a complete HTTP request that we must log */
1987 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1988 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001989
Willy Tarreau59234e92008-11-30 23:51:27 +01001990 if (urilen >= REQURI_LEN)
1991 urilen = REQURI_LEN - 1;
1992 memcpy(txn->uri, &req->data[msg->som], urilen);
1993 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001994
Willy Tarreau59234e92008-11-30 23:51:27 +01001995 if (!(s->logs.logwait &= ~LW_REQ))
1996 s->do_log(s);
1997 } else {
1998 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001999 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002000 }
Willy Tarreau06619262006-12-17 08:37:22 +01002001
Willy Tarreau59234e92008-11-30 23:51:27 +01002002 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002003 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2004 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002005
Willy Tarreau59234e92008-11-30 23:51:27 +01002006 /* 5: we may need to capture headers */
2007 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
2008 capture_headers(req->data + msg->som, &txn->hdr_idx,
2009 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002010
Willy Tarreau32b47f42009-10-18 20:55:02 +02002011 /* 6: determine if we have a transfer-encoding or content-length.
2012 * RFC2616 #4.4 states that :
2013 * - If a Transfer-Encoding header field is present and has any value
2014 * other than "identity", then the transfer-length is defined by use
2015 * of the "chunked" transfer-coding ;
2016 *
2017 * - If a Content-Length header field is present, its decimal value in
2018 * OCTETs represents both the entity-length and the transfer-length.
2019 * If a message is received with both a Transfer-Encoding header
2020 * field and a Content-Length header field, the latter MUST be ignored.
2021 *
2022 * - If a request contains a message-body and a Content-Length is not
2023 * given, the server SHOULD respond with 400 (bad request) if it
2024 * cannot determine the length of the message, or with 411 (length
2025 * required) if it wishes to insist on receiving a valid Content-Length.
2026 */
2027
2028 /* FIXME: chunked encoding is HTTP/1.1 only */
2029 ctx.idx = 0;
2030 while (http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
2031 if (ctx.vlen == 8 && strncasecmp(ctx.line + ctx.val, "identity", 8) == 0)
2032 continue;
2033 txn->flags |= TX_REQ_TE_CHNK;
2034 break;
2035 }
2036
2037 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
2038 ctx.idx = 0;
2039 while (!(txn->flags & TX_REQ_TE_CHNK) &&
2040 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2041 signed long long cl;
2042
2043 if (!ctx.vlen)
2044 goto return_bad_req;
2045
2046 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2047 goto return_bad_req; /* parse failure */
2048
2049 if (cl < 0)
2050 goto return_bad_req;
2051
2052 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2053 goto return_bad_req; /* already specified, was different */
2054
2055 txn->flags |= TX_REQ_CNT_LEN;
2056 msg->hdr_content_len = cl;
2057 }
2058
2059 /* Determine if the client wishes keep-alive or close.
2060 * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections
2061 * are persistent unless "Connection: close" is explicitly specified.
2062 * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections.
2063 * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless
2064 * they explicitly specify "Connection: keep-alive", regardless of any
2065 * optional "keep-alive" header.
2066 */
2067
2068 /* FIXME: should we also remove any header specified in "connection" ? */
2069 conn_ka = conn_cl = 0;
2070 ctx.idx = 0;
2071 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
2072 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
2073 conn_cl = 1;
2074 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
2075 conn_ka = 1;
2076 }
2077
Willy Tarreau75a5fef2009-10-18 23:43:57 +02002078 /* prepare flags for this transaction */
2079 txn->flags |= (TX_CLI_CONN_KA | TX_SRV_CONN_KA);
2080 txn->flags |= (TX_CLI_CONN_KA | TX_SRV_CONN_KA);
2081
Willy Tarreau32b47f42009-10-18 20:55:02 +02002082 if ((msg->sl.rq.v_l == 8) &&
2083 (req->data[msg->som + msg->sl.rq.v + 5] == '1') &&
2084 (req->data[msg->som + msg->sl.rq.v + 7] == '0')) {
2085 /* HTTP/1.0 */
Willy Tarreau75a5fef2009-10-18 23:43:57 +02002086 if (!conn_ka)
2087 txn->flags &= ~(TX_CLI_CONN_KA | TX_SRV_CONN_KA);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002088 } else {
Willy Tarreau75a5fef2009-10-18 23:43:57 +02002089 /* HTTP/1.1 */
2090 if (conn_cl)
2091 txn->flags &= ~(TX_CLI_CONN_KA | TX_SRV_CONN_KA);
Willy Tarreau32b47f42009-10-18 20:55:02 +02002092 }
2093
2094 /* we can mark the connection as non-persistent if needed */
2095 if (!(txn->flags & TX_SRV_CONN_KA))
2096 s->flags |= SN_CONN_CLOSED;
2097
Willy Tarreaud787e662009-07-07 10:14:51 +02002098 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002099 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002100 req->analyse_exp = TICK_ETERNITY;
2101 return 1;
2102
2103 return_bad_req:
2104 /* We centralize bad requests processing here */
2105 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2106 /* we detected a parsing error. We want to archive this request
2107 * in the dedicated proxy area for later troubleshooting.
2108 */
2109 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2110 }
2111
2112 txn->req.msg_state = HTTP_MSG_ERROR;
2113 txn->status = 400;
2114 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002115
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002116 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002117 if (s->listener->counters)
2118 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002119
2120 return_prx_cond:
2121 if (!(s->flags & SN_ERR_MASK))
2122 s->flags |= SN_ERR_PRXCOND;
2123 if (!(s->flags & SN_FINST_MASK))
2124 s->flags |= SN_FINST_R;
2125
2126 req->analysers = 0;
2127 req->analyse_exp = TICK_ETERNITY;
2128 return 0;
2129}
2130
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002131/* This stream analyser runs all HTTP request processing which is common to
2132 * frontends and backends, which means blocking ACLs, filters, connection-close,
2133 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002134 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002135 * either needs more data or wants to immediately abort the request (eg: deny,
2136 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002137 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002138int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002139{
Willy Tarreaud787e662009-07-07 10:14:51 +02002140 struct http_txn *txn = &s->txn;
2141 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002142 struct acl_cond *cond;
2143 struct redirect_rule *rule;
2144 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002145
Willy Tarreau655dce92009-11-08 13:10:58 +01002146 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002147 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002148 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002149 return 0;
2150 }
2151
Willy Tarreau3a816292009-07-07 10:55:49 +02002152 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002153 req->analyse_exp = TICK_ETERNITY;
2154
2155 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2156 now_ms, __FUNCTION__,
2157 s,
2158 req,
2159 req->rex, req->wex,
2160 req->flags,
2161 req->l,
2162 req->analysers);
2163
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002164 /* first check whether we have some ACLs set to block this request */
2165 list_for_each_entry(cond, &px->block_cond, list) {
2166 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002167
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002168 ret = acl_pass(ret);
2169 if (cond->pol == ACL_COND_UNLESS)
2170 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002171
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002172 if (ret) {
2173 txn->status = 403;
2174 /* let's log the request time */
2175 s->logs.tv_request = now;
2176 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2177 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002178 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002179 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002180
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002181 /* try headers filters */
2182 if (px->req_exp != NULL) {
2183 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2184 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002185
Willy Tarreau59234e92008-11-30 23:51:27 +01002186 /* has the request been denied ? */
2187 if (txn->flags & TX_CLDENY) {
2188 /* no need to go further */
2189 txn->status = 403;
2190 /* let's log the request time */
2191 s->logs.tv_request = now;
2192 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2193 goto return_prx_cond;
2194 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002195
2196 /* When a connection is tarpitted, we use the tarpit timeout,
2197 * which may be the same as the connect timeout if unspecified.
2198 * If unset, then set it to zero because we really want it to
2199 * eventually expire. We build the tarpit as an analyser.
2200 */
2201 if (txn->flags & TX_CLTARPIT) {
2202 buffer_erase(s->req);
2203 /* wipe the request out so that we can drop the connection early
2204 * if the client closes first.
2205 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002206 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002207 req->analysers = 0; /* remove switching rules etc... */
2208 req->analysers |= AN_REQ_HTTP_TARPIT;
2209 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2210 if (!req->analyse_exp)
2211 req->analyse_exp = tick_add(now_ms, 0);
2212 return 1;
2213 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002214 }
Willy Tarreau06619262006-12-17 08:37:22 +01002215
Willy Tarreau42736642009-10-18 21:04:35 +02002216 /* We might have to check for "Connection:". The test for persistent
2217 * connections has already been performed, so we only enter here if
2218 * we are certain the connection is persistent.
2219 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002220 if (((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
2221 !(s->flags & SN_CONN_CLOSED)) {
2222 char *cur_ptr, *cur_end, *cur_next;
2223 int old_idx, delta, val;
2224 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002225
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002226 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
2227 old_idx = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002228
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002229 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
2230 cur_hdr = &txn->hdr_idx.v[cur_idx];
2231 cur_ptr = cur_next;
2232 cur_end = cur_ptr + cur_hdr->len;
2233 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002234
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002235 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
2236 if (val) {
2237 /* 3 possibilities :
2238 * - we have already set Connection: close,
2239 * so we remove this line.
2240 * - we have not yet set Connection: close,
2241 * but this line indicates close. We leave
2242 * it untouched and set the flag.
2243 * - we have not yet set Connection: close,
2244 * and this line indicates non-close. We
2245 * replace it.
2246 */
2247 if (s->flags & SN_CONN_CLOSED) {
2248 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreaufa355d42009-11-29 18:12:29 +01002249 http_msg_move_end(&txn->req, delta);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002250 cur_next += delta;
2251 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2252 txn->hdr_idx.used--;
2253 cur_hdr->len = 0;
2254 } else {
2255 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
2256 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2257 "close", 5);
Willy Tarreau59234e92008-11-30 23:51:27 +01002258 cur_next += delta;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002259 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01002260 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002261 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002262 s->flags |= SN_CONN_CLOSED;
Willy Tarreau42736642009-10-18 21:04:35 +02002263 txn->flags &= ~TX_SRV_CONN_KA; /* keep-alive closed on server side */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002264 }
Willy Tarreau06619262006-12-17 08:37:22 +01002265 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002266 old_idx = cur_idx;
Willy Tarreau59234e92008-11-30 23:51:27 +01002267 }
Willy Tarreau78599912009-10-17 20:12:21 +02002268
Willy Tarreau42736642009-10-18 21:04:35 +02002269 /* if there is no "Connection: keep-alive" header left and we're
2270 * in HTTP/1.0, then non-persistent connection is implied */
Willy Tarreau78599912009-10-17 20:12:21 +02002271 if (!(s->flags & SN_CONN_CLOSED) && (msg->sl.rq.v_l == 8) &&
2272 (req->data[msg->som + msg->sl.rq.v + 5] == '1') &&
Willy Tarreau42736642009-10-18 21:04:35 +02002273 (req->data[msg->som + msg->sl.rq.v + 7] == '0')) {
Willy Tarreau78599912009-10-17 20:12:21 +02002274 s->flags |= SN_CONN_CLOSED;
Willy Tarreau42736642009-10-18 21:04:35 +02002275 txn->flags &= ~TX_SRV_CONN_KA; /* keep-alive closed on server side */
2276 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002277 }
2278 /* add request headers from the rule sets in the same order */
2279 for (cur_idx = 0; cur_idx < px->nb_reqadd; cur_idx++) {
2280 if (unlikely(http_header_add_tail(req,
2281 &txn->req,
2282 &txn->hdr_idx,
2283 px->req_add[cur_idx])) < 0)
2284 goto return_bad_req;
2285 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002286
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002287 /* check if stats URI was requested, and if an auth is needed */
2288 if (px->uri_auth != NULL &&
2289 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2290 /* we have to check the URI and auth for this request.
2291 * FIXME!!! that one is rather dangerous, we want to
2292 * make it follow standard rules (eg: clear req->analysers).
2293 */
2294 if (stats_check_uri_auth(s, px)) {
2295 req->analysers = 0;
2296 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002297 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002298 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002299
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002300 /* check whether we have some ACLs set to redirect this request */
2301 list_for_each_entry(rule, &px->redirect_rules, list) {
2302 int ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreau06b917c2009-07-06 16:34:52 +02002303
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002304 ret = acl_pass(ret);
2305 if (rule->cond->pol == ACL_COND_UNLESS)
2306 ret = !ret;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002307
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002308 if (ret) {
2309 struct chunk rdr = { trash, 0 };
2310 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002311
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002312 /* build redirect message */
2313 switch(rule->code) {
2314 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002315 msg_fmt = HTTP_303;
2316 break;
2317 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002318 msg_fmt = HTTP_301;
2319 break;
2320 case 302:
2321 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002322 msg_fmt = HTTP_302;
2323 break;
2324 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002325
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002326 if (unlikely(chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002327 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002328
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002329 switch(rule->type) {
2330 case REDIRECT_TYPE_PREFIX: {
2331 const char *path;
2332 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002333
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002334 path = http_get_path(txn);
2335 /* build message using path */
2336 if (path) {
2337 pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path;
2338 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2339 int qs = 0;
2340 while (qs < pathlen) {
2341 if (path[qs] == '?') {
2342 pathlen = qs;
2343 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002344 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002345 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002346 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002347 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002348 } else {
2349 path = "/";
2350 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002351 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002352
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002353 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002354 goto return_bad_req;
2355
2356 /* add prefix. Note that if prefix == "/", we don't want to
2357 * add anything, otherwise it makes it hard for the user to
2358 * configure a self-redirection.
2359 */
2360 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002361 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2362 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002363 }
2364
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002365 /* add path */
2366 memcpy(rdr.str + rdr.len, path, pathlen);
2367 rdr.len += pathlen;
2368 break;
2369 }
2370 case REDIRECT_TYPE_LOCATION:
2371 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002372 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002373 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002374
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002375 /* add location */
2376 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2377 rdr.len += rule->rdr_len;
2378 break;
2379 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002380
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002381 if (rule->cookie_len) {
2382 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2383 rdr.len += 14;
2384 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2385 rdr.len += rule->cookie_len;
2386 memcpy(rdr.str + rdr.len, "\r\n", 2);
2387 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002388 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002389
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002390 /* add end of headers */
2391 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2392 rdr.len += 4;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002393
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002394 txn->status = rule->code;
2395 /* let's log the request time */
2396 s->logs.tv_request = now;
2397 stream_int_retnclose(req->prod, &rdr);
2398 goto return_prx_cond;
2399 }
2400 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002401
Willy Tarreauf1ba4b32009-10-17 14:37:52 +02002402 /* We can shut read side if "connection: close" && !abort_on_close && !content-length */
Willy Tarreau349a0f62009-10-18 21:17:42 +02002403 if ((s->flags & SN_CONN_CLOSED) && !(s->be->options & PR_O_ABRT_CLOSE) &&
2404 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len && (txn->meth < HTTP_METH_CONNECT))
2405 req->flags |= BF_DONT_READ;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +02002406
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002407 /* that's OK for us now, let's move on to next analysers */
2408 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002409
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002410 return_bad_req:
2411 /* We centralize bad requests processing here */
2412 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2413 /* we detected a parsing error. We want to archive this request
2414 * in the dedicated proxy area for later troubleshooting.
2415 */
2416 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2417 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002418
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002419 txn->req.msg_state = HTTP_MSG_ERROR;
2420 txn->status = 400;
2421 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002422
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002423 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002424 if (s->listener->counters)
2425 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002426
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002427 return_prx_cond:
2428 if (!(s->flags & SN_ERR_MASK))
2429 s->flags |= SN_ERR_PRXCOND;
2430 if (!(s->flags & SN_FINST_MASK))
2431 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002432
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002433 req->analysers = 0;
2434 req->analyse_exp = TICK_ETERNITY;
2435 return 0;
2436}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002437
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002438/* This function performs all the processing enabled for the current request.
2439 * It returns 1 if the processing can continue on next analysers, or zero if it
2440 * needs more data, encounters an error, or wants to immediately abort the
2441 * request. It relies on buffers flags, and updates s->req->analysers.
2442 */
2443int http_process_request(struct session *s, struct buffer *req, int an_bit)
2444{
2445 struct http_txn *txn = &s->txn;
2446 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002447
Willy Tarreau655dce92009-11-08 13:10:58 +01002448 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002449 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002450 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002451 return 0;
2452 }
2453
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002454 req->analysers &= ~an_bit;
2455 req->analyse_exp = TICK_ETERNITY;
2456
2457 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2458 now_ms, __FUNCTION__,
2459 s,
2460 req,
2461 req->rex, req->wex,
2462 req->flags,
2463 req->l,
2464 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002465
Willy Tarreau59234e92008-11-30 23:51:27 +01002466 /*
2467 * Right now, we know that we have processed the entire headers
2468 * and that unwanted requests have been filtered out. We can do
2469 * whatever we want with the remaining request. Also, now we
2470 * may have separate values for ->fe, ->be.
2471 */
Willy Tarreau06619262006-12-17 08:37:22 +01002472
Willy Tarreau59234e92008-11-30 23:51:27 +01002473 /*
2474 * If HTTP PROXY is set we simply get remote server address
2475 * parsing incoming request.
2476 */
2477 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2478 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2479 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002480
Willy Tarreau59234e92008-11-30 23:51:27 +01002481 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002482 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002483 * Note that doing so might move headers in the request, but
2484 * the fields will stay coherent and the URI will not move.
2485 * This should only be performed in the backend.
2486 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002487 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002488 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2489 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002490
Willy Tarreau59234e92008-11-30 23:51:27 +01002491 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002492 * 8: the appsession cookie was looked up very early in 1.2,
2493 * so let's do the same now.
2494 */
2495
2496 /* It needs to look into the URI */
2497 if ((s->sessid == NULL) && s->be->appsession_name) {
2498 get_srv_from_appsession(s, &req->data[msg->som + msg->sl.rq.u], msg->sl.rq.u_l);
2499 }
2500
2501 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002502 * 9: add X-Forwarded-For if either the frontend or the backend
2503 * asks for it.
2504 */
2505 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2506 if (s->cli_addr.ss_family == AF_INET) {
2507 /* Add an X-Forwarded-For header unless the source IP is
2508 * in the 'except' network range.
2509 */
2510 if ((!s->fe->except_mask.s_addr ||
2511 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2512 != s->fe->except_net.s_addr) &&
2513 (!s->be->except_mask.s_addr ||
2514 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2515 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002516 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002517 unsigned char *pn;
2518 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002519
2520 /* Note: we rely on the backend to get the header name to be used for
2521 * x-forwarded-for, because the header is really meant for the backends.
2522 * However, if the backend did not specify any option, we have to rely
2523 * on the frontend's header name.
2524 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002525 if (s->be->fwdfor_hdr_len) {
2526 len = s->be->fwdfor_hdr_len;
2527 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002528 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002529 len = s->fe->fwdfor_hdr_len;
2530 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002531 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002532 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002533
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002534 if (unlikely(http_header_add_tail2(req, &txn->req,
2535 &txn->hdr_idx, trash, len)) < 0)
Willy Tarreau06619262006-12-17 08:37:22 +01002536 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002537 }
2538 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002539 else if (s->cli_addr.ss_family == AF_INET6) {
2540 /* FIXME: for the sake of completeness, we should also support
2541 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002542 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002543 int len;
2544 char pn[INET6_ADDRSTRLEN];
2545 inet_ntop(AF_INET6,
2546 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2547 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002548
Willy Tarreau59234e92008-11-30 23:51:27 +01002549 /* Note: we rely on the backend to get the header name to be used for
2550 * x-forwarded-for, because the header is really meant for the backends.
2551 * However, if the backend did not specify any option, we have to rely
2552 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002553 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002554 if (s->be->fwdfor_hdr_len) {
2555 len = s->be->fwdfor_hdr_len;
2556 memcpy(trash, s->be->fwdfor_hdr_name, len);
2557 } else {
2558 len = s->fe->fwdfor_hdr_len;
2559 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002560 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002561 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002562
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 if (unlikely(http_header_add_tail2(req, &txn->req,
2564 &txn->hdr_idx, trash, len)) < 0)
2565 goto return_bad_req;
2566 }
2567 }
2568
2569 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002570 * 10: add X-Original-To if either the frontend or the backend
2571 * asks for it.
2572 */
2573 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
2574
2575 /* FIXME: don't know if IPv6 can handle that case too. */
2576 if (s->cli_addr.ss_family == AF_INET) {
2577 /* Add an X-Original-To header unless the destination IP is
2578 * in the 'except' network range.
2579 */
2580 if (!(s->flags & SN_FRT_ADDR_SET))
2581 get_frt_addr(s);
2582
2583 if ((!s->fe->except_mask_to.s_addr ||
2584 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
2585 != s->fe->except_to.s_addr) &&
2586 (!s->be->except_mask_to.s_addr ||
2587 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
2588 != s->be->except_to.s_addr)) {
2589 int len;
2590 unsigned char *pn;
2591 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
2592
2593 /* Note: we rely on the backend to get the header name to be used for
2594 * x-original-to, because the header is really meant for the backends.
2595 * However, if the backend did not specify any option, we have to rely
2596 * on the frontend's header name.
2597 */
2598 if (s->be->orgto_hdr_len) {
2599 len = s->be->orgto_hdr_len;
2600 memcpy(trash, s->be->orgto_hdr_name, len);
2601 } else {
2602 len = s->fe->orgto_hdr_len;
2603 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002604 }
Maik Broemme2850cb42009-04-17 18:53:21 +02002605 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
2606
2607 if (unlikely(http_header_add_tail2(req, &txn->req,
2608 &txn->hdr_idx, trash, len)) < 0)
2609 goto return_bad_req;
2610 }
2611 }
2612 }
2613
Willy Tarreau78599912009-10-17 20:12:21 +02002614 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau59234e92008-11-30 23:51:27 +01002615 if (!(s->flags & SN_CONN_CLOSED) &&
2616 ((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau78599912009-10-17 20:12:21 +02002617 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002618 "Connection: close", 17)) < 0)
2619 goto return_bad_req;
2620 s->flags |= SN_CONN_CLOSED;
2621 }
2622 /* Before we switch to data, was assignment set in manage_client_side_cookie?
2623 * If not assigned, perhaps we are balancing on url_param, but this is a
2624 * POST; and the parameters are in the body, maybe scan there to find our server.
2625 * (unless headers overflowed the buffer?)
2626 */
2627 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
2628 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
2629 s->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) &&
2630 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
2631 /* are there enough bytes here? total == l || r || rlim ?
2632 * len is unsigned, but eoh is int,
2633 * how many bytes of body have we received?
2634 * eoh is the first empty line of the header
2635 */
2636 /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */
2637 unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1);
2638
2639 /* If we have HTTP/1.1 and Expect: 100-continue, then abort.
2640 * We can't assume responsibility for the server's decision,
2641 * on this URI and header set. See rfc2616: 14.20, 8.2.3,
2642 * We also can't change our mind later, about which server to choose, so round robin.
2643 */
2644 if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) {
2645 struct hdr_ctx ctx;
2646 ctx.idx = 0;
2647 /* Expect is allowed in 1.1, look for it */
2648 http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx);
2649 if (ctx.idx != 0 &&
2650 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0))
2651 /* We can't reliablly stall and wait for data, because of
2652 * .NET clients that don't conform to rfc2616; so, no need for
2653 * the next block to check length expectations.
2654 * We could send 100 status back to the client, but then we need to
2655 * re-write headers, and send the message. And this isn't the right
2656 * place for that action.
2657 * TODO: support Expect elsewhere and delete this block.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002658 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002659 goto end_check_maybe_wait_for_body;
2660 }
2661
Willy Tarreau03a56332009-10-18 21:28:29 +02002662 if (likely(len <= s->be->url_param_post_limit)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002663 /* limit implies we are supposed to need this many bytes
Willy Tarreau03a56332009-10-18 21:28:29 +02002664 * to find the parameter. Let's see how many bytes we can
2665 * wait for.
Willy Tarreau59234e92008-11-30 23:51:27 +01002666 */
Willy Tarreau03a56332009-10-18 21:28:29 +02002667 if (txn->flags & TX_REQ_TE_CHNK) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02002668 buffer_dont_connect(req);
Willy Tarreau59234e92008-11-30 23:51:27 +01002669 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau03a56332009-10-18 21:28:29 +02002670 } else {
2671 long long hint = txn->req.hdr_content_len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */
2673 if (s->be->url_param_post_limit < hint)
2674 hint = s->be->url_param_post_limit;
Willy Tarreau03a56332009-10-18 21:28:29 +02002675
Willy Tarreau59234e92008-11-30 23:51:27 +01002676 /* now do we really need to buffer more data? */
2677 if (len < hint) {
Willy Tarreau520d95e2009-09-19 21:04:57 +02002678 buffer_dont_connect(req);
Willy Tarreau2df28e82008-08-17 15:20:19 +02002679 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau41f40ed2008-08-21 10:05:00 +02002680 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002681 /* else... There are no body bytes to wait for */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002682 }
2683 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 }
2685 end_check_maybe_wait_for_body:
Willy Tarreaubaaee002006-06-26 02:48:02 +02002686
Willy Tarreau59234e92008-11-30 23:51:27 +01002687 /*************************************************************
2688 * OK, that's finished for the headers. We have done what we *
2689 * could. Let's switch to the DATA state. *
2690 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02002691
Willy Tarreaua07a34e2009-08-16 23:27:46 +02002692 buffer_set_rlim(req, req->size); /* no more rewrite needed */
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 s->logs.tv_request = now;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002694
Willy Tarreau59234e92008-11-30 23:51:27 +01002695 /* OK let's go on with the BODY now */
2696 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002697
Willy Tarreau59234e92008-11-30 23:51:27 +01002698 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02002699 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01002700 /* we detected a parsing error. We want to archive this request
2701 * in the dedicated proxy area for later troubleshooting.
2702 */
Willy Tarreau4076a152009-04-02 15:18:36 +02002703 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01002704 }
Willy Tarreau4076a152009-04-02 15:18:36 +02002705
Willy Tarreau59234e92008-11-30 23:51:27 +01002706 txn->req.msg_state = HTTP_MSG_ERROR;
2707 txn->status = 400;
2708 req->analysers = 0;
2709 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002710
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002711 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002712 if (s->listener->counters)
2713 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002714
Willy Tarreau59234e92008-11-30 23:51:27 +01002715 if (!(s->flags & SN_ERR_MASK))
2716 s->flags |= SN_ERR_PRXCOND;
2717 if (!(s->flags & SN_FINST_MASK))
2718 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002719 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002720}
Willy Tarreauadfb8562008-08-11 15:24:42 +02002721
Willy Tarreau60b85b02008-11-30 23:28:40 +01002722/* This function is an analyser which processes the HTTP tarpit. It always
2723 * returns zero, at the beginning because it prevents any other processing
2724 * from occurring, and at the end because it terminates the request.
2725 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002726int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01002727{
2728 struct http_txn *txn = &s->txn;
2729
2730 /* This connection is being tarpitted. The CLIENT side has
2731 * already set the connect expiration date to the right
2732 * timeout. We just have to check that the client is still
2733 * there and that the timeout has not expired.
2734 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002735 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01002736 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
2737 !tick_is_expired(req->analyse_exp, now_ms))
2738 return 0;
2739
2740 /* We will set the queue timer to the time spent, just for
2741 * logging purposes. We fake a 500 server error, so that the
2742 * attacker will not suspect his connection has been tarpitted.
2743 * It will not cause trouble to the logs because we can exclude
2744 * the tarpitted connections by filtering on the 'PT' status flags.
2745 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01002746 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2747
2748 txn->status = 500;
2749 if (req->flags != BF_READ_ERROR)
2750 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
2751
2752 req->analysers = 0;
2753 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002754
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002755 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002756 if (s->listener->counters)
2757 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01002758
Willy Tarreau60b85b02008-11-30 23:28:40 +01002759 if (!(s->flags & SN_ERR_MASK))
2760 s->flags |= SN_ERR_PRXCOND;
2761 if (!(s->flags & SN_FINST_MASK))
2762 s->flags |= SN_FINST_T;
2763 return 0;
2764}
2765
Willy Tarreaud34af782008-11-30 23:36:37 +01002766/* This function is an analyser which processes the HTTP request body. It looks
2767 * for parameters to be used for the load balancing algorithm (url_param). It
2768 * must only be called after the standard HTTP request processing has occurred,
2769 * because it expects the request to be parsed. It returns zero if it needs to
2770 * read more data, or 1 once it has completed its analysis.
2771 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002772int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01002773{
2774 struct http_msg *msg = &s->txn.req;
2775 unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1;
2776 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01002777
Willy Tarreau655dce92009-11-08 13:10:58 +01002778 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002779 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002780 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002781 return 0;
2782 }
2783
Willy Tarreaud34af782008-11-30 23:36:37 +01002784 /* We have to parse the HTTP request body to find any required data.
2785 * "balance url_param check_post" should have been the only way to get
2786 * into this. We were brought here after HTTP header analysis, so all
2787 * related structures are ready.
2788 */
2789
Willy Tarreaud34af782008-11-30 23:36:37 +01002790 /* now if we have a length, we'll take the hint */
Willy Tarreau2225dd42009-10-18 21:36:47 +02002791 if (s->txn.flags & TX_REQ_TE_CHNK) {
Willy Tarreaud34af782008-11-30 23:36:37 +01002792 unsigned int chunk = 0;
2793 while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) {
2794 char c = msg->sol[body];
2795 if (ishex(c)) {
2796 unsigned int hex = toupper(c) - '0';
2797 if (hex > 9)
2798 hex -= 'A' - '9' - 1;
2799 chunk = (chunk << 4) | hex;
2800 } else
2801 break;
2802 body++;
2803 }
2804 if (body + 2 >= req->l) /* we want CRLF too */
2805 goto http_body_end; /* end of buffer? data missing! */
2806
2807 if (memcmp(msg->sol+body, "\r\n", 2) != 0)
2808 goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */
2809
2810 body += 2; // skip CRLF
2811
2812 /* if we support more then one chunk here, we have to do it again when assigning server
2813 * 1. how much entity data do we have? new var
2814 * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here
2815 * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found)
2816 */
2817
2818 if (chunk < limit)
2819 limit = chunk; /* only reading one chunk */
2820 } else {
2821 if (msg->hdr_content_len < limit)
2822 limit = msg->hdr_content_len;
2823 }
2824
2825 http_body_end:
2826 /* we leave once we know we have nothing left to do. This means that we have
2827 * enough bytes, or that we know we'll not get any more (buffer full, read
2828 * buffer closed).
2829 */
2830 if (req->l - body >= limit || /* enough bytes! */
2831 req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) ||
2832 tick_is_expired(req->analyse_exp, now_ms)) {
2833 /* The situation will not evolve, so let's give up on the analysis. */
2834 s->logs.tv_request = now; /* update the request timer to reflect full request */
Willy Tarreau3a816292009-07-07 10:55:49 +02002835 req->analysers &= ~an_bit;
Willy Tarreaud34af782008-11-30 23:36:37 +01002836 req->analyse_exp = TICK_ETERNITY;
2837 return 1;
2838 }
2839 else {
2840 /* Not enough data. We'll re-use the http-request
2841 * timeout here. Ideally, we should set the timeout
2842 * relative to the accept() date. We just set the
2843 * request timeout once at the beginning of the
2844 * request.
2845 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002846 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01002847 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002848 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01002849 return 0;
2850 }
2851}
2852
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002853/* This stream analyser waits for a complete HTTP response. It returns 1 if the
2854 * processing can continue on next analysers, or zero if it either needs more
2855 * data or wants to immediately abort the response (eg: timeout, error, ...). It
2856 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
2857 * when it has nothing left to do, and may remove any analyser when it wants to
2858 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002859 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002860int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002861{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002862 struct http_txn *txn = &s->txn;
2863 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02002864 struct hdr_ctx ctx;
2865 int conn_ka, conn_cl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002866 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002867 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02002868
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002869 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 +02002870 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002871 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02002872 rep,
2873 rep->rex, rep->wex,
2874 rep->flags,
2875 rep->l,
2876 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02002877
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002878 /*
2879 * Now parse the partial (or complete) lines.
2880 * We will check the response syntax, and also join multi-line
2881 * headers. An index of all the lines will be elaborated while
2882 * parsing.
2883 *
2884 * For the parsing, we use a 28 states FSM.
2885 *
2886 * Here is the information we currently have :
2887 * rep->data + rep->som = beginning of response
2888 * rep->data + rep->eoh = end of processed headers / start of current one
2889 * rep->data + rep->eol = end of current header or line (LF or CRLF)
2890 * rep->lr = first non-visited byte
2891 * rep->r = end of data
2892 */
2893
2894 if (likely(rep->lr < rep->r))
2895 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau7f875f62008-08-11 17:35:01 +02002896
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002897 /* 1: we might have to print this header in debug mode */
2898 if (unlikely((global.mode & MODE_DEBUG) &&
2899 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002900 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002901 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002902
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002903 sol = rep->data + msg->som;
2904 eol = sol + msg->sl.rq.l;
2905 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002906
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002907 sol += hdr_idx_first_pos(&txn->hdr_idx);
2908 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002909
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002910 while (cur_idx) {
2911 eol = sol + txn->hdr_idx.v[cur_idx].len;
2912 debug_hdr("srvhdr", s, sol, eol);
2913 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2914 cur_idx = txn->hdr_idx.v[cur_idx].next;
2915 }
2916 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002917
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002918 /*
2919 * Now we quickly check if we have found a full valid response.
2920 * If not so, we check the FD and buffer states before leaving.
2921 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002922 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002923 * responses are checked first.
2924 *
2925 * Depending on whether the client is still there or not, we
2926 * may send an error response back or not. Note that normally
2927 * we should only check for HTTP status there, and check I/O
2928 * errors somewhere else.
2929 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002930
Willy Tarreau655dce92009-11-08 13:10:58 +01002931 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002932 /* Invalid response */
2933 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2934 /* we detected a parsing error. We want to archive this response
2935 * in the dedicated proxy area for later troubleshooting.
2936 */
2937 hdr_response_bad:
2938 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
2939 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
2940
2941 s->be->counters.failed_resp++;
2942 if (s->srv)
2943 s->srv->counters.failed_resp++;
2944
2945 rep->analysers = 0;
2946 txn->status = 502;
2947 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
2948
2949 if (!(s->flags & SN_ERR_MASK))
2950 s->flags |= SN_ERR_PRXCOND;
2951 if (!(s->flags & SN_FINST_MASK))
2952 s->flags |= SN_FINST_H;
2953
2954 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002955 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002956
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002957 /* too large response does not fit in buffer. */
2958 else if (rep->flags & BF_FULL) {
2959 goto hdr_response_bad;
2960 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002961
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002962 /* read error */
2963 else if (rep->flags & BF_READ_ERROR) {
2964 if (msg->err_pos >= 0)
2965 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002966
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002967 s->be->counters.failed_resp++;
2968 if (s->srv)
2969 s->srv->counters.failed_resp++;
Willy Tarreau461f6622008-08-15 23:43:19 +02002970
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002971 rep->analysers = 0;
2972 txn->status = 502;
2973 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02002974
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002975 if (!(s->flags & SN_ERR_MASK))
2976 s->flags |= SN_ERR_SRVCL;
2977 if (!(s->flags & SN_FINST_MASK))
2978 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02002979 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02002980 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02002981
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002982 /* read timeout : return a 504 to the client. */
2983 else if (rep->flags & BF_READ_TIMEOUT) {
2984 if (msg->err_pos >= 0)
2985 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01002986
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002987 s->be->counters.failed_resp++;
2988 if (s->srv)
2989 s->srv->counters.failed_resp++;
Willy Tarreau21d2af32008-02-14 20:25:24 +01002990
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002991 rep->analysers = 0;
2992 txn->status = 504;
2993 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02002994
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002995 if (!(s->flags & SN_ERR_MASK))
2996 s->flags |= SN_ERR_SRVTO;
2997 if (!(s->flags & SN_FINST_MASK))
2998 s->flags |= SN_FINST_H;
2999 return 0;
3000 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02003001
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003002 /* close from server */
3003 else if (rep->flags & BF_SHUTR) {
3004 if (msg->err_pos >= 0)
3005 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003006
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003007 s->be->counters.failed_resp++;
3008 if (s->srv)
3009 s->srv->counters.failed_resp++;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003010
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003011 rep->analysers = 0;
3012 txn->status = 502;
3013 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01003014
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003015 if (!(s->flags & SN_ERR_MASK))
3016 s->flags |= SN_ERR_SRVCL;
3017 if (!(s->flags & SN_FINST_MASK))
3018 s->flags |= SN_FINST_H;
3019 return 0;
3020 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003021
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003022 /* write error to client (we don't send any message then) */
3023 else if (rep->flags & BF_WRITE_ERROR) {
3024 if (msg->err_pos >= 0)
3025 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003026
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003027 s->be->counters.failed_resp++;
3028 rep->analysers = 0;
3029
3030 if (!(s->flags & SN_ERR_MASK))
3031 s->flags |= SN_ERR_CLICL;
3032 if (!(s->flags & SN_FINST_MASK))
3033 s->flags |= SN_FINST_H;
3034
3035 /* process_session() will take care of the error */
3036 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003037 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003038
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003039 buffer_dont_close(rep);
3040 return 0;
3041 }
3042
3043 /* More interesting part now : we know that we have a complete
3044 * response which at least looks like HTTP. We have an indicator
3045 * of each header's length, so we can parse them quickly.
3046 */
3047
3048 if (unlikely(msg->err_pos >= 0))
3049 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3050
3051 /* ensure we keep this pointer to the beginning of the message */
3052 msg->sol = rep->data + msg->som;
3053
3054 /*
3055 * 1: get the status code
3056 */
3057 n = rep->data[msg->sl.st.c] - '0';
3058 if (n < 1 || n > 5)
3059 n = 0;
3060 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003061
3062 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
3063
3064 /*
3065 * 2: check for cacheability.
3066 */
3067
3068 switch (txn->status) {
3069 case 200:
3070 case 203:
3071 case 206:
3072 case 300:
3073 case 301:
3074 case 410:
3075 /* RFC2616 @13.4:
3076 * "A response received with a status code of
3077 * 200, 203, 206, 300, 301 or 410 MAY be stored
3078 * by a cache (...) unless a cache-control
3079 * directive prohibits caching."
3080 *
3081 * RFC2616 @9.5: POST method :
3082 * "Responses to this method are not cacheable,
3083 * unless the response includes appropriate
3084 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003085 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003086 if (likely(txn->meth != HTTP_METH_POST) &&
3087 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3088 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3089 break;
3090 default:
3091 break;
3092 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003093
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003094 /*
3095 * 3: we may need to capture headers
3096 */
3097 s->logs.logwait &= ~LW_RESP;
3098 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
3099 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3100 txn->rsp.cap, s->fe->rsp_cap);
3101
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003102 /* 4: determine if we have a transfer-encoding or content-length.
3103 * RFC2616 #4.4 states that :
3104 * - Any response which "MUST NOT" include a message-body (such as the
3105 * 1xx, 204 and 304 responses and any response to a HEAD request) is
3106 * always terminated by the first empty line after the header fields,
3107 * regardless of the entity-header fields present in the message.
3108 *
3109 * - If a Transfer-Encoding header field is present and has any value
3110 * other than "identity", then the transfer-length is defined by use
3111 * of the "chunked" transfer-coding ;
3112 *
3113 * - If a Content-Length header field is present, its decimal value in
3114 * OCTETs represents both the entity-length and the transfer-length.
3115 * If a message is received with both a Transfer-Encoding header
3116 * field and a Content-Length header field, the latter MUST be ignored.
3117 */
3118
3119 /* Skip parsing if no content length is possible. The response flags
3120 * remain 0 as well as the hdr_content_len, which may or may not mirror
3121 * the real header value.
3122 * FIXME: should we parse anyway and return an error on chunked encoding ?
3123 */
3124 if (txn->meth == HTTP_METH_HEAD ||
3125 (txn->status >= 100 && txn->status < 200) ||
3126 txn->status == 204 || txn->status == 304)
3127 goto skip_content_length;
3128
3129 /* FIXME: chunked encoding is HTTP/1.1 only */
3130 ctx.idx = 0;
3131 while (http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
3132 if (ctx.vlen == 8 && strncasecmp(ctx.line + ctx.val, "identity", 8) == 0)
3133 continue;
3134 txn->flags |= TX_RES_TE_CHNK;
3135 break;
3136 }
3137
3138 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
3139 ctx.idx = 0;
3140 while (!(txn->flags & TX_RES_TE_CHNK) &&
3141 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
3142 signed long long cl;
3143
3144 if (!ctx.vlen)
3145 goto hdr_response_bad;
3146
3147 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
3148 goto hdr_response_bad; /* parse failure */
3149
3150 if (cl < 0)
3151 goto hdr_response_bad;
3152
3153 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
3154 goto hdr_response_bad; /* already specified, was different */
3155
3156 txn->flags |= TX_RES_CNT_LEN;
3157 msg->hdr_content_len = cl;
3158 }
3159
3160skip_content_length:
3161 /* Determine if the server wishes keep-alive or close.
3162 * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections
3163 * are persistent unless "Connection: close" is explicitly specified.
3164 * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections.
3165 * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless
3166 * they explicitly specify "Connection: keep-alive", regardless of any
3167 * optional "keep-alive" header.
3168 */
3169
3170 /* FIXME: should we also remove any header specified in "connection" ? */
3171 conn_ka = conn_cl = 0;
3172 ctx.idx = 0;
3173 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
3174 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
3175 conn_cl = 1;
3176 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
3177 conn_ka = 1;
3178 }
3179
3180 if ((msg->sl.st.v_l == 8) &&
3181 (rep->data[msg->som + 5] == '1') &&
3182 (rep->data[msg->som + 7] == '0')) {
3183 /* HTTP/1.0 */
3184 if (!conn_ka)
3185 txn->flags &= ~TX_SRV_CONN_KA;
3186 } else {
3187 /* HTTP/1.1 */
3188 if (conn_cl)
3189 txn->flags &= ~TX_SRV_CONN_KA;;
3190 }
3191
3192 /* we can mark the connection as non-persistent if needed */
3193 s->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */
3194 if (!(txn->flags & TX_SRV_CONN_KA))
3195 s->flags |= SN_CONN_CLOSED;
3196
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003197 /* end of job, return OK */
3198 rep->analysers &= ~an_bit;
3199 rep->analyse_exp = TICK_ETERNITY;
3200 return 1;
3201}
3202
3203/* This function performs all the processing enabled for the current response.
3204 * It normally returns zero, but may return 1 if it absolutely needs to be
3205 * called again after other functions. It relies on buffers flags, and updates
3206 * t->rep->analysers. It might make sense to explode it into several other
3207 * functions. It works like process_request (see indications above).
3208 */
3209int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
3210{
3211 struct http_txn *txn = &t->txn;
3212 struct buffer *req = t->req;
3213 struct http_msg *msg = &txn->rsp;
3214 struct proxy *cur_proxy;
3215 int cur_idx;
3216
3217 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3218 now_ms, __FUNCTION__,
3219 t,
3220 rep,
3221 rep->rex, rep->wex,
3222 rep->flags,
3223 rep->l,
3224 rep->analysers);
3225
Willy Tarreau655dce92009-11-08 13:10:58 +01003226 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003227 return 0;
3228
3229 rep->analysers &= ~an_bit;
3230 rep->analyse_exp = TICK_ETERNITY;
3231
3232 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003233 /*
3234 * 3: we will have to evaluate the filters.
3235 * As opposed to version 1.2, now they will be evaluated in the
3236 * filters order and not in the header order. This means that
3237 * each filter has to be validated among all headers.
3238 *
3239 * Filters are tried with ->be first, then with ->fe if it is
3240 * different from ->be.
3241 */
3242
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003243 cur_proxy = t->be;
3244 while (1) {
3245 struct proxy *rule_set = cur_proxy;
3246
3247 /* try headers filters */
3248 if (rule_set->rsp_exp != NULL) {
3249 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
3250 return_bad_resp:
Willy Tarreau8365f932009-03-15 23:11:49 +01003251 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003252 t->srv->counters.failed_resp++;
3253 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003254 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02003255 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003256 txn->status = 502;
Willy Tarreau8e89b842009-10-18 23:56:35 +02003257 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003258 if (!(t->flags & SN_ERR_MASK))
3259 t->flags |= SN_ERR_PRXCOND;
3260 if (!(t->flags & SN_FINST_MASK))
3261 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02003262 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01003263 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003264 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003265
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003266 /* has the response been denied ? */
3267 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01003268 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003269 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003270
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003271 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003272 if (t->listener->counters)
3273 t->listener->counters->denied_resp++;
3274
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003275 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01003276 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003277
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003278 /* We might have to check for "Connection:" */
3279 if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
Willy Tarreau816b9792009-09-15 21:25:21 +02003280 !(t->flags & SN_CONN_CLOSED) &&
3281 txn->status >= 200) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003282 char *cur_ptr, *cur_end, *cur_next;
3283 int cur_idx, old_idx, delta, val;
3284 struct hdr_idx_elem *cur_hdr;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003285
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003286 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
3287 old_idx = 0;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003288
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003289 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
3290 cur_hdr = &txn->hdr_idx.v[cur_idx];
3291 cur_ptr = cur_next;
3292 cur_end = cur_ptr + cur_hdr->len;
3293 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003294
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003295 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
3296 if (val) {
3297 /* 3 possibilities :
3298 * - we have already set Connection: close,
3299 * so we remove this line.
3300 * - we have not yet set Connection: close,
3301 * but this line indicates close. We leave
3302 * it untouched and set the flag.
3303 * - we have not yet set Connection: close,
3304 * and this line indicates non-close. We
3305 * replace it.
3306 */
3307 if (t->flags & SN_CONN_CLOSED) {
3308 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
Willy Tarreaufa355d42009-11-29 18:12:29 +01003309 http_msg_move_end(&txn->rsp, delta);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003310 cur_next += delta;
3311 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3312 txn->hdr_idx.used--;
3313 cur_hdr->len = 0;
3314 } else {
3315 if (strncasecmp(cur_ptr + val, "close", 5) != 0) {
3316 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
3317 "close", 5);
3318 cur_next += delta;
3319 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01003320 http_msg_move_end(&txn->rsp, delta);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003321 }
3322 t->flags |= SN_CONN_CLOSED;
3323 }
3324 }
3325 old_idx = cur_idx;
Willy Tarreau541b5c22008-01-06 23:34:21 +01003326 }
3327 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003328
Willy Tarreaub50943e2009-10-18 23:52:50 +02003329 /* if there is no "Connection: keep-alive" header left and we're
3330 * in HTTP/1.0, then non-persistent connection is implied */
3331 if (!(t->flags & SN_CONN_CLOSED) && (msg->sl.st.v_l == 8) &&
3332 (rep->data[msg->som + 5] == '1') &&
3333 (rep->data[msg->som + 7] == '0')) {
3334 t->flags |= SN_CONN_CLOSED;
3335 txn->flags &= ~TX_CLI_CONN_KA; /* keep-alive closed on server side */
3336 }
3337
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003338 /* add response headers from the rule sets in the same order */
3339 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau816b9792009-09-15 21:25:21 +02003340 if (txn->status < 200)
3341 break;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003342 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
3343 rule_set->rsp_add[cur_idx])) < 0)
3344 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02003345 }
3346
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003347 /* check whether we're already working on the frontend */
3348 if (cur_proxy == t->fe)
3349 break;
3350 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003351 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003352
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003353 /*
3354 * 4: check for server cookie.
3355 */
Willy Tarreau816b9792009-09-15 21:25:21 +02003356 if ((t->be->cookie_name || t->be->appsession_name || t->fe->capture_name
3357 || (t->be->options & PR_O_CHK_CACHE)) && txn->status >= 200)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003358 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003359
Willy Tarreaubaaee002006-06-26 02:48:02 +02003360
Willy Tarreaua15645d2007-03-18 16:22:39 +01003361 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003362 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003363 */
Willy Tarreau816b9792009-09-15 21:25:21 +02003364 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0 && txn->status >= 200)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003365 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003366
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003367 /*
3368 * 6: add server cookie in the response if needed
3369 */
3370 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau816b9792009-09-15 21:25:21 +02003371 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
3372 txn->status >= 200) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003373 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003374
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003375 /* the server is known, it's not the one the client requested, we have to
3376 * insert a set-cookie here, except if we want to insert only on POST
3377 * requests and this one isn't. Note that servers which don't have cookies
3378 * (eg: some backup servers) will return a full cookie removal request.
3379 */
3380 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
3381 t->be->cookie_name,
3382 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02003383
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003384 if (t->be->cookie_domain)
3385 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003386
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003387 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3388 trash, len)) < 0)
3389 goto return_bad_resp;
3390 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003391
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003392 /* Here, we will tell an eventual cache on the client side that we don't
3393 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
3394 * Some caches understand the correct form: 'no-cache="set-cookie"', but
3395 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
3396 */
3397 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02003398
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003399 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3400
3401 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3402 "Cache-control: private", 22)) < 0)
3403 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01003404 }
3405 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003406
Willy Tarreaubaaee002006-06-26 02:48:02 +02003407
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003408 /*
3409 * 7: check if result will be cacheable with a cookie.
3410 * We'll block the response if security checks have caught
3411 * nasty things such as a cacheable cookie.
3412 */
3413 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
3414 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau816b9792009-09-15 21:25:21 +02003415 (t->be->options & PR_O_CHK_CACHE) &&
3416 txn->status >= 200) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003417
3418 /* we're in presence of a cacheable response containing
3419 * a set-cookie header. We'll block it as requested by
3420 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003421 */
Willy Tarreau8365f932009-03-15 23:11:49 +01003422 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003423 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003424
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003425 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003426 if (t->listener->counters)
3427 t->listener->counters->denied_resp++;
3428
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003429 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
3430 t->be->id, t->srv?t->srv->id:"<dispatch>");
3431 send_log(t->be, LOG_ALERT,
3432 "Blocking cacheable cookie in response from instance %s, server %s.\n",
3433 t->be->id, t->srv?t->srv->id:"<dispatch>");
3434 goto return_srv_prx_502;
3435 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003436
3437 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003438 * 8: add "Connection: close" if needed and not yet set.
3439 * Note that we do not need to add it in case of HTTP/1.0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003440 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003441 if (!(t->flags & SN_CONN_CLOSED) &&
Willy Tarreau816b9792009-09-15 21:25:21 +02003442 ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) &&
3443 txn->status >= 200) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003444 if ((unlikely(msg->sl.st.v_l != 8) ||
3445 unlikely(req->data[msg->som + 7] != '0')) &&
3446 unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
3447 "Connection: close", 17)) < 0)
3448 goto return_bad_resp;
3449 t->flags |= SN_CONN_CLOSED;
3450 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003451
Willy Tarreau816b9792009-09-15 21:25:21 +02003452 /*
3453 * 9: we may be facing a 1xx response (100 continue, 101 switching protocols),
3454 * in which case this is not the right response, and we're waiting for the
3455 * next one. Let's allow this response to go to the client and wait for the
3456 * next one.
3457 */
3458 if (txn->status < 200) {
3459 hdr_idx_init(&txn->hdr_idx);
3460 buffer_forward(rep, rep->lr - (rep->data + msg->som));
3461 msg->msg_state = HTTP_MSG_RPBEFORE;
3462 txn->status = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003463 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
3464 return 1;
Willy Tarreau816b9792009-09-15 21:25:21 +02003465 }
3466
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003467 /*************************************************************
3468 * OK, that's finished for the headers. We have done what we *
3469 * could. Let's switch to the DATA state. *
3470 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02003471
Willy Tarreaua07a34e2009-08-16 23:27:46 +02003472 buffer_set_rlim(rep, rep->size); /* no more rewrite needed */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003473 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01003474
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003475 /* if the user wants to log as soon as possible, without counting
3476 * bytes from the server, then this is the right moment. We have
3477 * to temporarily assign bytes_out to log what we currently have.
3478 */
3479 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
3480 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
3481 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01003482 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003483 t->logs.bytes_out = 0;
3484 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01003485
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003486 /* Note: we must not try to cheat by jumping directly to DATA,
3487 * otherwise we would not let the client side wake up.
3488 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01003489
Willy Tarreaudafde432008-08-17 01:00:46 +02003490 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003491 }
3492 return 0;
3493}
Willy Tarreaua15645d2007-03-18 16:22:39 +01003494
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003495/* Iterate the same filter through all request headers.
3496 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003497 * Since it can manage the switch to another backend, it updates the per-proxy
3498 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003499 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003500int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01003501{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003502 char term;
3503 char *cur_ptr, *cur_end, *cur_next;
3504 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003505 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003506 struct hdr_idx_elem *cur_hdr;
3507 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01003508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003509 last_hdr = 0;
3510
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003511 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003512 old_idx = 0;
3513
3514 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01003515 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003516 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003517 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003518 (exp->action == ACT_ALLOW ||
3519 exp->action == ACT_DENY ||
3520 exp->action == ACT_TARPIT))
3521 return 0;
3522
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003523 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003524 if (!cur_idx)
3525 break;
3526
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003527 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003528 cur_ptr = cur_next;
3529 cur_end = cur_ptr + cur_hdr->len;
3530 cur_next = cur_end + cur_hdr->cr + 1;
3531
3532 /* Now we have one header between cur_ptr and cur_end,
3533 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003534 */
3535
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003536 /* The annoying part is that pattern matching needs
3537 * that we modify the contents to null-terminate all
3538 * strings before testing them.
3539 */
3540
3541 term = *cur_end;
3542 *cur_end = '\0';
3543
3544 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3545 switch (exp->action) {
3546 case ACT_SETBE:
3547 /* It is not possible to jump a second time.
3548 * FIXME: should we return an HTTP/500 here so that
3549 * the admin knows there's a problem ?
3550 */
3551 if (t->be != t->fe)
3552 break;
3553
3554 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003555 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003556 last_hdr = 1;
3557 break;
3558
3559 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003560 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003561 last_hdr = 1;
3562 break;
3563
3564 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003565 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003566 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003567
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003568 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003569 if (t->listener->counters)
3570 t->listener->counters->denied_resp++;
3571
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003572 break;
3573
3574 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003575 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003576 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003577
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003578 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003579 if (t->listener->counters)
3580 t->listener->counters->denied_resp++;
3581
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003582 break;
3583
3584 case ACT_REPLACE:
3585 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3586 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3587 /* FIXME: if the user adds a newline in the replacement, the
3588 * index will not be recalculated for now, and the new line
3589 * will not be counted as a new header.
3590 */
3591
3592 cur_end += delta;
3593 cur_next += delta;
3594 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01003595 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003596 break;
3597
3598 case ACT_REMOVE:
3599 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
3600 cur_next += delta;
3601
Willy Tarreaufa355d42009-11-29 18:12:29 +01003602 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003603 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
3604 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003605 cur_hdr->len = 0;
3606 cur_end = NULL; /* null-term has been rewritten */
3607 break;
3608
3609 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003610 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003611 if (cur_end)
3612 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003614 /* keep the link from this header to next one in case of later
3615 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003616 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003617 old_idx = cur_idx;
3618 }
3619 return 0;
3620}
3621
3622
3623/* Apply the filter to the request line.
3624 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3625 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01003626 * Since it can manage the switch to another backend, it updates the per-proxy
3627 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003628 */
3629int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
3630{
3631 char term;
3632 char *cur_ptr, *cur_end;
3633 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003634 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003635 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003636
Willy Tarreau58f10d72006-12-04 02:26:12 +01003637
Willy Tarreau3d300592007-03-18 18:34:41 +01003638 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003639 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01003640 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003641 (exp->action == ACT_ALLOW ||
3642 exp->action == ACT_DENY ||
3643 exp->action == ACT_TARPIT))
3644 return 0;
3645 else if (exp->action == ACT_REMOVE)
3646 return 0;
3647
3648 done = 0;
3649
Willy Tarreau9cdde232007-05-02 20:58:19 +02003650 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003651 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003652
3653 /* Now we have the request line between cur_ptr and cur_end */
3654
3655 /* The annoying part is that pattern matching needs
3656 * that we modify the contents to null-terminate all
3657 * strings before testing them.
3658 */
3659
3660 term = *cur_end;
3661 *cur_end = '\0';
3662
3663 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
3664 switch (exp->action) {
3665 case ACT_SETBE:
3666 /* It is not possible to jump a second time.
3667 * FIXME: should we return an HTTP/500 here so that
3668 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01003669 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003670 if (t->be != t->fe)
3671 break;
3672
3673 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003674 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003675 done = 1;
3676 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003677
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003678 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01003679 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003680 done = 1;
3681 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003682
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003683 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01003684 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003685
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003686 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003687 if (t->listener->counters)
3688 t->listener->counters->denied_resp++;
3689
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003690 done = 1;
3691 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003692
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003693 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01003694 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003695
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003696 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003697 if (t->listener->counters)
3698 t->listener->counters->denied_resp++;
3699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003700 done = 1;
3701 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01003702
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003703 case ACT_REPLACE:
3704 *cur_end = term; /* restore the string terminator */
3705 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
3706 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
3707 /* FIXME: if the user adds a newline in the replacement, the
3708 * index will not be recalculated for now, and the new line
3709 * will not be counted as a new header.
3710 */
Willy Tarreaua496b602006-12-17 23:15:24 +01003711
Willy Tarreaufa355d42009-11-29 18:12:29 +01003712 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003713 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01003714
Willy Tarreau9cdde232007-05-02 20:58:19 +02003715 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003716 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003717 HTTP_MSG_RQMETH,
3718 cur_ptr, cur_end + 1,
3719 NULL, NULL);
3720 if (unlikely(!cur_end))
3721 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01003722
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003723 /* we have a full request and we know that we have either a CR
3724 * or an LF at <ptr>.
3725 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003726 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
3727 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003728 /* there is no point trying this regex on headers */
3729 return 1;
3730 }
3731 }
3732 *cur_end = term; /* restore the string terminator */
3733 return done;
3734}
Willy Tarreau97de6242006-12-27 17:18:38 +01003735
Willy Tarreau58f10d72006-12-04 02:26:12 +01003736
Willy Tarreau58f10d72006-12-04 02:26:12 +01003737
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003738/*
3739 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
3740 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01003741 * unparsable request. Since it can manage the switch to another backend, it
3742 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003743 */
3744int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
3745{
Willy Tarreau3d300592007-03-18 18:34:41 +01003746 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003747 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01003748 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003749 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003751 /*
3752 * The interleaving of transformations and verdicts
3753 * makes it difficult to decide to continue or stop
3754 * the evaluation.
3755 */
3756
Willy Tarreau3d300592007-03-18 18:34:41 +01003757 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003758 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3759 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
3760 exp = exp->next;
3761 continue;
3762 }
3763
3764 /* Apply the filter to the request line. */
3765 ret = apply_filter_to_req_line(t, req, exp);
3766 if (unlikely(ret < 0))
3767 return -1;
3768
3769 if (likely(ret == 0)) {
3770 /* The filter did not match the request, it can be
3771 * iterated through all headers.
3772 */
3773 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003774 }
3775 exp = exp->next;
3776 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003777 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003778}
3779
3780
Willy Tarreaua15645d2007-03-18 16:22:39 +01003781
Willy Tarreau58f10d72006-12-04 02:26:12 +01003782/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02003783 * Try to retrieve the server associated to the appsession.
3784 * If the server is found, it's assigned to the session.
3785 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01003786void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02003787 struct http_txn *txn = &t->txn;
3788 appsess *asession = NULL;
3789 char *sessid_temp = NULL;
3790
Cyril Bontéb21570a2009-11-29 20:04:48 +01003791 if (len > t->be->appsession_len) {
3792 len = t->be->appsession_len;
3793 }
3794
Cyril Bontébf47aeb2009-10-15 00:15:40 +02003795 if (t->be->options2 & PR_O2_AS_REQL) {
3796 /* request-learn option is enabled : store the sessid in the session for future use */
3797 if (t->sessid != NULL) {
3798 /* free previously allocated memory as we don't need the session id found in the URL anymore */
3799 pool_free2(apools.sessid, t->sessid);
3800 }
3801
3802 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
3803 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3804 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3805 return;
3806 }
3807
Cyril Bontéb21570a2009-11-29 20:04:48 +01003808 memcpy(t->sessid, buf, len);
3809 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02003810 }
3811
3812 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
3813 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
3814 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
3815 return;
3816 }
3817
Cyril Bontéb21570a2009-11-29 20:04:48 +01003818 memcpy(sessid_temp, buf, len);
3819 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02003820
3821 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
3822 /* free previously allocated memory */
3823 pool_free2(apools.sessid, sessid_temp);
3824
3825 if (asession != NULL) {
3826 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
3827 if (!(t->be->options2 & PR_O2_AS_REQL))
3828 asession->request_count++;
3829
3830 if (asession->serverid != NULL) {
3831 struct server *srv = t->be->srv;
3832 while (srv) {
3833 if (strcmp(srv->id, asession->serverid) == 0) {
3834 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
3835 /* we found the server and it's usable */
3836 txn->flags &= ~TX_CK_MASK;
3837 txn->flags |= TX_CK_VALID;
3838 t->flags |= SN_DIRECT | SN_ASSIGNED;
3839 t->srv = srv;
3840 break;
3841 } else {
3842 txn->flags &= ~TX_CK_MASK;
3843 txn->flags |= TX_CK_DOWN;
3844 }
3845 }
3846 srv = srv->next;
3847 }
3848 }
3849 }
3850}
3851
3852/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01003853 * Manage client-side cookie. It can impact performance by about 2% so it is
3854 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003855 */
3856void manage_client_side_cookies(struct session *t, struct buffer *req)
3857{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003858 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003859 char *p1, *p2, *p3, *p4;
3860 char *del_colon, *del_cookie, *colon;
3861 int app_cookies;
3862
Willy Tarreau58f10d72006-12-04 02:26:12 +01003863 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003864 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003865
Willy Tarreau2a324282006-12-05 00:05:46 +01003866 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01003867 * we start with the start line.
3868 */
Willy Tarreau83969f42007-01-22 08:55:47 +01003869 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003870 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003871
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003872 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003873 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003874 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003875
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01003876 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01003877 cur_ptr = cur_next;
3878 cur_end = cur_ptr + cur_hdr->len;
3879 cur_next = cur_end + cur_hdr->cr + 1;
3880
3881 /* We have one full header between cur_ptr and cur_end, and the
3882 * next header starts at cur_next. We're only interested in
3883 * "Cookie:" headers.
3884 */
3885
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003886 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
3887 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003888 old_idx = cur_idx;
3889 continue;
3890 }
3891
3892 /* Now look for cookies. Conforming to RFC2109, we have to support
3893 * attributes whose name begin with a '$', and associate them with
3894 * the right cookie, if we want to delete this cookie.
3895 * So there are 3 cases for each cookie read :
3896 * 1) it's a special attribute, beginning with a '$' : ignore it.
3897 * 2) it's a server id cookie that we *MAY* want to delete : save
3898 * some pointers on it (last semi-colon, beginning of cookie...)
3899 * 3) it's an application cookie : we *MAY* have to delete a previous
3900 * "special" cookie.
3901 * At the end of loop, if a "special" cookie remains, we may have to
3902 * remove it. If no application cookie persists in the header, we
3903 * *MUST* delete it
3904 */
3905
Willy Tarreauaa9dce32007-03-18 23:50:16 +01003906 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01003907
Willy Tarreau58f10d72006-12-04 02:26:12 +01003908 /* del_cookie == NULL => nothing to be deleted */
3909 del_colon = del_cookie = NULL;
3910 app_cookies = 0;
3911
3912 while (p1 < cur_end) {
3913 /* skip spaces and colons, but keep an eye on these ones */
3914 while (p1 < cur_end) {
3915 if (*p1 == ';' || *p1 == ',')
3916 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003917 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01003918 break;
3919 p1++;
3920 }
3921
3922 if (p1 == cur_end)
3923 break;
3924
3925 /* p1 is at the beginning of the cookie name */
3926 p2 = p1;
3927 while (p2 < cur_end && *p2 != '=')
3928 p2++;
3929
3930 if (p2 == cur_end)
3931 break;
3932
3933 p3 = p2 + 1; /* skips the '=' sign */
3934 if (p3 == cur_end)
3935 break;
3936
3937 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02003938 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01003939 p4++;
3940
3941 /* here, we have the cookie name between p1 and p2,
3942 * and its value between p3 and p4.
3943 * we can process it :
3944 *
3945 * Cookie: NAME=VALUE;
3946 * | || || |
3947 * | || || +--> p4
3948 * | || |+-------> p3
3949 * | || +--------> p2
3950 * | |+------------> p1
3951 * | +-------------> colon
3952 * +--------------------> cur_ptr
3953 */
3954
3955 if (*p1 == '$') {
3956 /* skip this one */
3957 }
3958 else {
3959 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003960 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003961 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003962 (p4 - p1 >= t->fe->capture_namelen) &&
3963 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003964 int log_len = p4 - p1;
3965
Willy Tarreau086b3b42007-05-13 21:45:51 +02003966 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003967 Alert("HTTP logging : out of memory.\n");
3968 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003969 if (log_len > t->fe->capture_len)
3970 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01003971 memcpy(txn->cli_cookie, p1, log_len);
3972 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003973 }
3974 }
3975
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003976 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
3977 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003978 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003979 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003980 char *delim;
3981
3982 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3983 * have the server ID betweek p3 and delim, and the original cookie between
3984 * delim+1 and p4. Otherwise, delim==p4 :
3985 *
3986 * Cookie: NAME=SRV~VALUE;
3987 * | || || | |
3988 * | || || | +--> p4
3989 * | || || +--------> delim
3990 * | || |+-----------> p3
3991 * | || +------------> p2
3992 * | |+----------------> p1
3993 * | +-----------------> colon
3994 * +------------------------> cur_ptr
3995 */
3996
Willy Tarreaue2e27a52007-04-01 00:01:37 +02003997 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01003998 for (delim = p3; delim < p4; delim++)
3999 if (*delim == COOKIE_DELIM)
4000 break;
4001 }
4002 else
4003 delim = p4;
4004
4005
4006 /* Here, we'll look for the first running server which supports the cookie.
4007 * This allows to share a same cookie between several servers, for example
4008 * to dedicate backup servers to specific servers only.
4009 * However, to prevent clients from sticking to cookie-less backup server
4010 * when they have incidentely learned an empty cookie, we simply ignore
4011 * empty cookies and mark them as invalid.
4012 */
4013 if (delim == p3)
4014 srv = NULL;
4015
4016 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01004017 if (srv->cookie && (srv->cklen == delim - p3) &&
4018 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004019 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004020 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01004021 txn->flags &= ~TX_CK_MASK;
4022 txn->flags |= TX_CK_VALID;
4023 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004024 t->srv = srv;
4025 break;
4026 } else {
4027 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01004028 txn->flags &= ~TX_CK_MASK;
4029 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004030 }
4031 }
4032 srv = srv->next;
4033 }
4034
Willy Tarreau3d300592007-03-18 18:34:41 +01004035 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004036 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01004037 txn->flags &= ~TX_CK_MASK;
4038 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004039 }
4040
4041 /* depending on the cookie mode, we may have to either :
4042 * - delete the complete cookie if we're in insert+indirect mode, so that
4043 * the server never sees it ;
4044 * - remove the server id from the cookie value, and tag the cookie as an
4045 * application cookie so that it does not get accidentely removed later,
4046 * if we're in cookie prefix mode
4047 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004048 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004049 int delta; /* negative */
4050
4051 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
4052 p4 += delta;
4053 cur_end += delta;
4054 cur_next += delta;
4055 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004056 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004057
4058 del_cookie = del_colon = NULL;
4059 app_cookies++; /* protect the header from deletion */
4060 }
4061 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004062 (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 +01004063 del_cookie = p1;
4064 del_colon = colon;
4065 }
4066 } else {
4067 /* now we know that we must keep this cookie since it's
4068 * not ours. But if we wanted to delete our cookie
4069 * earlier, we cannot remove the complete header, but we
4070 * can remove the previous block itself.
4071 */
4072 app_cookies++;
4073
4074 if (del_cookie != NULL) {
4075 int delta; /* negative */
4076
4077 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
4078 p4 += delta;
4079 cur_end += delta;
4080 cur_next += delta;
4081 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004082 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004083 del_cookie = del_colon = NULL;
4084 }
4085 }
4086
Cyril Bontéb21570a2009-11-29 20:04:48 +01004087 if (t->be->appsession_name != NULL) {
4088 int cmp_len, value_len;
4089 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004090
Cyril Bontéb21570a2009-11-29 20:04:48 +01004091 if (t->be->options2 & PR_O2_AS_PFX) {
4092 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
4093 value_begin = p1 + t->be->appsession_name_len;
4094 value_len = p4 - p1 - t->be->appsession_name_len;
4095 } else {
4096 cmp_len = p2 - p1;
4097 value_begin = p3;
4098 value_len = p4 - p3;
4099 }
4100
4101 /* let's see if the cookie is our appcookie */
4102 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
4103 /* Cool... it's the right one */
4104 manage_client_side_appsession(t, value_begin, value_len);
4105 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004106#if defined(DEBUG_HASH)
4107 Alert("manage_client_side_cookies\n");
4108 appsession_hash_dump(&(t->be->htbl_proxy));
4109#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004110 }/* end if ((t->proxy->appsession_name != NULL) ... */
4111 }
4112
4113 /* we'll have to look for another cookie ... */
4114 p1 = p4;
4115 } /* while (p1 < cur_end) */
4116
4117 /* There's no more cookie on this line.
4118 * We may have marked the last one(s) for deletion.
4119 * We must do this now in two ways :
4120 * - if there is no app cookie, we simply delete the header ;
4121 * - if there are app cookies, we must delete the end of the
4122 * string properly, including the colon/semi-colon before
4123 * the cookie name.
4124 */
4125 if (del_cookie != NULL) {
4126 int delta;
4127 if (app_cookies) {
4128 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
4129 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004130 cur_hdr->len += delta;
4131 } else {
4132 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004133
4134 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004135 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4136 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004137 cur_hdr->len = 0;
4138 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01004139 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004140 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004141 }
4142
4143 /* keep the link from this header to next one */
4144 old_idx = cur_idx;
4145 } /* end of cookie processing on this header */
4146}
4147
4148
Willy Tarreaua15645d2007-03-18 16:22:39 +01004149/* Iterate the same filter through all response headers contained in <rtr>.
4150 * Returns 1 if this filter can be stopped upon return, otherwise 0.
4151 */
4152int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4153{
4154 char term;
4155 char *cur_ptr, *cur_end, *cur_next;
4156 int cur_idx, old_idx, last_hdr;
4157 struct http_txn *txn = &t->txn;
4158 struct hdr_idx_elem *cur_hdr;
4159 int len, delta;
4160
4161 last_hdr = 0;
4162
4163 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4164 old_idx = 0;
4165
4166 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004167 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004168 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004169 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004170 (exp->action == ACT_ALLOW ||
4171 exp->action == ACT_DENY))
4172 return 0;
4173
4174 cur_idx = txn->hdr_idx.v[old_idx].next;
4175 if (!cur_idx)
4176 break;
4177
4178 cur_hdr = &txn->hdr_idx.v[cur_idx];
4179 cur_ptr = cur_next;
4180 cur_end = cur_ptr + cur_hdr->len;
4181 cur_next = cur_end + cur_hdr->cr + 1;
4182
4183 /* Now we have one header between cur_ptr and cur_end,
4184 * and the next header starts at cur_next.
4185 */
4186
4187 /* The annoying part is that pattern matching needs
4188 * that we modify the contents to null-terminate all
4189 * strings before testing them.
4190 */
4191
4192 term = *cur_end;
4193 *cur_end = '\0';
4194
4195 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4196 switch (exp->action) {
4197 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004198 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004199 last_hdr = 1;
4200 break;
4201
4202 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004203 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004204 last_hdr = 1;
4205 break;
4206
4207 case ACT_REPLACE:
4208 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4209 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4210 /* FIXME: if the user adds a newline in the replacement, the
4211 * index will not be recalculated for now, and the new line
4212 * will not be counted as a new header.
4213 */
4214
4215 cur_end += delta;
4216 cur_next += delta;
4217 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004218 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004219 break;
4220
4221 case ACT_REMOVE:
4222 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4223 cur_next += delta;
4224
Willy Tarreaufa355d42009-11-29 18:12:29 +01004225 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004226 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4227 txn->hdr_idx.used--;
4228 cur_hdr->len = 0;
4229 cur_end = NULL; /* null-term has been rewritten */
4230 break;
4231
4232 }
4233 }
4234 if (cur_end)
4235 *cur_end = term; /* restore the string terminator */
4236
4237 /* keep the link from this header to next one in case of later
4238 * removal of next header.
4239 */
4240 old_idx = cur_idx;
4241 }
4242 return 0;
4243}
4244
4245
4246/* Apply the filter to the status line in the response buffer <rtr>.
4247 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4248 * or -1 if a replacement resulted in an invalid status line.
4249 */
4250int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4251{
4252 char term;
4253 char *cur_ptr, *cur_end;
4254 int done;
4255 struct http_txn *txn = &t->txn;
4256 int len, delta;
4257
4258
Willy Tarreau3d300592007-03-18 18:34:41 +01004259 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004260 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004261 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004262 (exp->action == ACT_ALLOW ||
4263 exp->action == ACT_DENY))
4264 return 0;
4265 else if (exp->action == ACT_REMOVE)
4266 return 0;
4267
4268 done = 0;
4269
Willy Tarreau9cdde232007-05-02 20:58:19 +02004270 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004271 cur_end = cur_ptr + txn->rsp.sl.rq.l;
4272
4273 /* Now we have the status line between cur_ptr and cur_end */
4274
4275 /* The annoying part is that pattern matching needs
4276 * that we modify the contents to null-terminate all
4277 * strings before testing them.
4278 */
4279
4280 term = *cur_end;
4281 *cur_end = '\0';
4282
4283 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4284 switch (exp->action) {
4285 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004286 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004287 done = 1;
4288 break;
4289
4290 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004291 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004292 done = 1;
4293 break;
4294
4295 case ACT_REPLACE:
4296 *cur_end = term; /* restore the string terminator */
4297 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4298 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
4299 /* FIXME: if the user adds a newline in the replacement, the
4300 * index will not be recalculated for now, and the new line
4301 * will not be counted as a new header.
4302 */
4303
Willy Tarreaufa355d42009-11-29 18:12:29 +01004304 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004305 cur_end += delta;
4306
Willy Tarreau9cdde232007-05-02 20:58:19 +02004307 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004308 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02004309 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01004310 cur_ptr, cur_end + 1,
4311 NULL, NULL);
4312 if (unlikely(!cur_end))
4313 return -1;
4314
4315 /* we have a full respnse and we know that we have either a CR
4316 * or an LF at <ptr>.
4317 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004318 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004319 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
4320 /* there is no point trying this regex on headers */
4321 return 1;
4322 }
4323 }
4324 *cur_end = term; /* restore the string terminator */
4325 return done;
4326}
4327
4328
4329
4330/*
4331 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
4332 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
4333 * unparsable response.
4334 */
4335int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
4336{
Willy Tarreau3d300592007-03-18 18:34:41 +01004337 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004338 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004339 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004340 int ret;
4341
4342 /*
4343 * The interleaving of transformations and verdicts
4344 * makes it difficult to decide to continue or stop
4345 * the evaluation.
4346 */
4347
Willy Tarreau3d300592007-03-18 18:34:41 +01004348 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01004349 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4350 exp->action == ACT_PASS)) {
4351 exp = exp->next;
4352 continue;
4353 }
4354
4355 /* Apply the filter to the status line. */
4356 ret = apply_filter_to_sts_line(t, rtr, exp);
4357 if (unlikely(ret < 0))
4358 return -1;
4359
4360 if (likely(ret == 0)) {
4361 /* The filter did not match the response, it can be
4362 * iterated through all headers.
4363 */
4364 apply_filter_to_resp_headers(t, rtr, exp);
4365 }
4366 exp = exp->next;
4367 }
4368 return 0;
4369}
4370
4371
4372
4373/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004374 * Manage server-side cookies. It can impact performance by about 2% so it is
4375 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004376 */
4377void manage_server_side_cookies(struct session *t, struct buffer *rtr)
4378{
4379 struct http_txn *txn = &t->txn;
4380 char *p1, *p2, *p3, *p4;
4381
Willy Tarreaua15645d2007-03-18 16:22:39 +01004382 char *cur_ptr, *cur_end, *cur_next;
4383 int cur_idx, old_idx, delta;
4384
Willy Tarreaua15645d2007-03-18 16:22:39 +01004385 /* Iterate through the headers.
4386 * we start with the start line.
4387 */
4388 old_idx = 0;
4389 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4390
4391 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
4392 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004393 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004394
4395 cur_hdr = &txn->hdr_idx.v[cur_idx];
4396 cur_ptr = cur_next;
4397 cur_end = cur_ptr + cur_hdr->len;
4398 cur_next = cur_end + cur_hdr->cr + 1;
4399
4400 /* We have one full header between cur_ptr and cur_end, and the
4401 * next header starts at cur_next. We're only interested in
4402 * "Cookie:" headers.
4403 */
4404
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004405 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
4406 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004407 old_idx = cur_idx;
4408 continue;
4409 }
4410
4411 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01004412 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004413
4414
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004415 /* maybe we only wanted to see if there was a set-cookie. Note that
4416 * the cookie capture is declared in the fronend.
4417 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004418 if (t->be->cookie_name == NULL &&
4419 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004420 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004421 return;
4422
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004423 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004424
4425 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004426 if (p1 == cur_end || *p1 == ';') /* end of cookie */
4427 break;
4428
4429 /* p1 is at the beginning of the cookie name */
4430 p2 = p1;
4431
4432 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
4433 p2++;
4434
4435 if (p2 == cur_end || *p2 == ';') /* next cookie */
4436 break;
4437
4438 p3 = p2 + 1; /* skip the '=' sign */
4439 if (p3 == cur_end)
4440 break;
4441
4442 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004443 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01004444 p4++;
4445
4446 /* here, we have the cookie name between p1 and p2,
4447 * and its value between p3 and p4.
4448 * we can process it.
4449 */
4450
4451 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004452 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004453 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004454 (p4 - p1 >= t->fe->capture_namelen) &&
4455 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004456 int log_len = p4 - p1;
4457
Willy Tarreau086b3b42007-05-13 21:45:51 +02004458 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004459 Alert("HTTP logging : out of memory.\n");
4460 }
4461
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004462 if (log_len > t->fe->capture_len)
4463 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004464 memcpy(txn->srv_cookie, p1, log_len);
4465 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004466 }
4467
4468 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004469 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
4470 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004471 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01004472 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004473
4474 /* If the cookie is in insert mode on a known server, we'll delete
4475 * this occurrence because we'll insert another one later.
4476 * We'll delete it too if the "indirect" option is set and we're in
4477 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004478 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
4479 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004480 /* this header must be deleted */
4481 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
4482 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4483 txn->hdr_idx.used--;
4484 cur_hdr->len = 0;
4485 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004486 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004487
Willy Tarreau3d300592007-03-18 18:34:41 +01004488 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004489 }
4490 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004491 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004492 /* replace bytes p3->p4 with the cookie name associated
4493 * with this server since we know it.
4494 */
4495 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
4496 cur_hdr->len += delta;
4497 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004498 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004499
Willy Tarreau3d300592007-03-18 18:34:41 +01004500 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004501 }
4502 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004503 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01004504 /* insert the cookie name associated with this server
4505 * before existing cookie, and insert a delimitor between them..
4506 */
4507 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
4508 cur_hdr->len += delta;
4509 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004510 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004511
4512 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01004513 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004514 }
4515 }
4516 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01004517 else if (t->be->appsession_name != NULL) {
4518 int cmp_len, value_len;
4519 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004520
Cyril Bontéb21570a2009-11-29 20:04:48 +01004521 if (t->be->options2 & PR_O2_AS_PFX) {
4522 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
4523 value_begin = p1 + t->be->appsession_name_len;
4524 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
4525 } else {
4526 cmp_len = p2 - p1;
4527 value_begin = p3;
4528 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004529 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01004530
4531 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
4532 /* Cool... it's the right one */
4533 if (t->sessid != NULL) {
4534 /* free previously allocated memory as we don't need it anymore */
4535 pool_free2(apools.sessid, t->sessid);
4536 }
4537 /* Store the sessid in the session for future use */
4538 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
4539 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4540 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4541 return;
4542 }
4543 memcpy(t->sessid, value_begin, value_len);
4544 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004545 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01004546 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004547 break; /* we don't want to loop again since there cannot be another cookie on the same line */
4548 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004549 /* keep the link from this header to next one */
4550 old_idx = cur_idx;
4551 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004552
4553 if (t->sessid != NULL) {
4554 appsess *asession = NULL;
4555 /* only do insert, if lookup fails */
4556 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
4557 if (asession == NULL) {
4558 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
4559 Alert("Not enough Memory process_srv():asession:calloc().\n");
4560 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
4561 return;
4562 }
4563 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
4564 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4565 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4566 return;
4567 }
4568 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
4569 asession->sessid[t->be->appsession_len] = 0;
4570
4571 size_t server_id_len = strlen(t->srv->id) + 1;
4572 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
4573 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
4574 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
4575 return;
4576 }
4577 asession->serverid[0] = '\0';
4578 memcpy(asession->serverid, t->srv->id, server_id_len);
4579
4580 asession->request_count = 0;
4581 appsession_hash_insert(&(t->be->htbl_proxy), asession);
4582 }
4583
4584 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
4585 asession->request_count++;
4586 }
4587
4588#if defined(DEBUG_HASH)
4589 Alert("manage_server_side_cookies\n");
4590 appsession_hash_dump(&(t->be->htbl_proxy));
4591#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01004592}
4593
4594
4595
4596/*
4597 * Check if response is cacheable or not. Updates t->flags.
4598 */
4599void check_response_for_cacheability(struct session *t, struct buffer *rtr)
4600{
4601 struct http_txn *txn = &t->txn;
4602 char *p1, *p2;
4603
4604 char *cur_ptr, *cur_end, *cur_next;
4605 int cur_idx;
4606
Willy Tarreau5df51872007-11-25 16:20:08 +01004607 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004608 return;
4609
4610 /* Iterate through the headers.
4611 * we start with the start line.
4612 */
4613 cur_idx = 0;
4614 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4615
4616 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4617 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004618 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004619
4620 cur_hdr = &txn->hdr_idx.v[cur_idx];
4621 cur_ptr = cur_next;
4622 cur_end = cur_ptr + cur_hdr->len;
4623 cur_next = cur_end + cur_hdr->cr + 1;
4624
4625 /* We have one full header between cur_ptr and cur_end, and the
4626 * next header starts at cur_next. We're only interested in
4627 * "Cookie:" headers.
4628 */
4629
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004630 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
4631 if (val) {
4632 if ((cur_end - (cur_ptr + val) >= 8) &&
4633 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
4634 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4635 return;
4636 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004637 }
4638
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004639 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
4640 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01004641 continue;
4642
4643 /* OK, right now we know we have a cache-control header at cur_ptr */
4644
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004645 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004646
4647 if (p1 >= cur_end) /* no more info */
4648 continue;
4649
4650 /* p1 is at the beginning of the value */
4651 p2 = p1;
4652
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004653 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01004654 p2++;
4655
4656 /* we have a complete value between p1 and p2 */
4657 if (p2 < cur_end && *p2 == '=') {
4658 /* we have something of the form no-cache="set-cookie" */
4659 if ((cur_end - p1 >= 21) &&
4660 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
4661 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01004662 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004663 continue;
4664 }
4665
4666 /* OK, so we know that either p2 points to the end of string or to a comma */
4667 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
4668 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
4669 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
4670 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004671 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004672 return;
4673 }
4674
4675 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004676 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004677 continue;
4678 }
4679 }
4680}
4681
4682
Willy Tarreau58f10d72006-12-04 02:26:12 +01004683/*
4684 * Try to retrieve a known appsession in the URI, then the associated server.
4685 * If the server is found, it's assigned to the session.
4686 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004687void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004688{
Cyril Bontéb21570a2009-11-29 20:04:48 +01004689 char *end_params, *first_param, *cur_param, *next_param;
4690 char separator;
4691 int value_len;
4692
4693 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004694
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004695 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01004696 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004697 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01004698 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004699
Cyril Bontéb21570a2009-11-29 20:04:48 +01004700 first_param = NULL;
4701 switch (mode) {
4702 case PR_O2_AS_M_PP:
4703 first_param = memchr(begin, ';', len);
4704 break;
4705 case PR_O2_AS_M_QS:
4706 first_param = memchr(begin, '?', len);
4707 break;
4708 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004709
Cyril Bontéb21570a2009-11-29 20:04:48 +01004710 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004711 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01004712 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004713
Cyril Bontéb21570a2009-11-29 20:04:48 +01004714 switch (mode) {
4715 case PR_O2_AS_M_PP:
4716 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
4717 end_params = (char *) begin + len;
4718 }
4719 separator = ';';
4720 break;
4721 case PR_O2_AS_M_QS:
4722 end_params = (char *) begin + len;
4723 separator = '&';
4724 break;
4725 default:
4726 /* unknown mode, shouldn't happen */
4727 return;
4728 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004729
Cyril Bontéb21570a2009-11-29 20:04:48 +01004730 cur_param = next_param = end_params;
4731 while (cur_param > first_param) {
4732 cur_param--;
4733 if ((cur_param[0] == separator) || (cur_param == first_param)) {
4734 /* let's see if this is the appsession parameter */
4735 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
4736 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
4737 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
4738 /* Cool... it's the right one */
4739 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
4740 value_len = MIN(t->be->appsession_len, next_param - cur_param);
4741 if (value_len > 0) {
4742 manage_client_side_appsession(t, cur_param, value_len);
4743 }
4744 break;
4745 }
4746 next_param = cur_param;
4747 }
4748 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004749#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02004750 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02004751 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01004752#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01004753}
4754
4755
Willy Tarreaub2513902006-12-17 14:52:38 +01004756/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004757 * In a GET or HEAD request, check if the requested URI matches the stats uri
4758 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01004759 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004760 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02004761 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02004762 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01004763 *
4764 * Returns 1 if the session's state changes, otherwise 0.
4765 */
4766int stats_check_uri_auth(struct session *t, struct proxy *backend)
4767{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004768 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01004769 struct uri_auth *uri_auth = backend->uri_auth;
4770 struct user_auth *user;
4771 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004772 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01004773
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004774 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
4775
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004776 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004777 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01004778 return 0;
4779
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004780 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004781
Willy Tarreau0214c3a2007-01-07 13:47:30 +01004782 /* the URI is in h */
4783 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01004784 return 0;
4785
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004786 h += uri_auth->uri_len;
4787 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
4788 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004789 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004790 break;
4791 }
4792 h++;
4793 }
4794
4795 if (uri_auth->refresh) {
4796 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4797 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
4798 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004799 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02004800 break;
4801 }
4802 h++;
4803 }
4804 }
4805
Willy Tarreau55bb8452007-10-17 18:44:57 +02004806 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
4807 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
4808 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004809 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02004810 break;
4811 }
4812 h++;
4813 }
4814
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004815 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
4816
Willy Tarreaub2513902006-12-17 14:52:38 +01004817 /* we are in front of a interceptable URI. Let's check
4818 * if there's an authentication and if it's valid.
4819 */
4820 user = uri_auth->users;
4821 if (!user) {
4822 /* no user auth required, it's OK */
4823 authenticated = 1;
4824 } else {
4825 authenticated = 0;
4826
4827 /* a user list is defined, we have to check.
4828 * skip 21 chars for "Authorization: Basic ".
4829 */
4830
4831 /* FIXME: this should move to an earlier place */
4832 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004833 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
4834 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
4835 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01004836 if (len > 14 &&
4837 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02004838 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01004839 break;
4840 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004841 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01004842 }
4843
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004844 if (txn->auth_hdr.len < 21 ||
4845 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01004846 user = NULL;
4847
4848 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004849 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
4850 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01004851 user->user_pwd, user->user_len)) {
4852 authenticated = 1;
4853 break;
4854 }
4855 user = user->next;
4856 }
4857 }
4858
4859 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01004860 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01004861
4862 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004863 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
4864 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01004865 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01004866 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02004867 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004868 if (!(t->flags & SN_ERR_MASK))
4869 t->flags |= SN_ERR_PRXCOND;
4870 if (!(t->flags & SN_FINST_MASK))
4871 t->flags |= SN_FINST_R;
4872 return 1;
4873 }
4874
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01004875 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01004876 * data.
4877 */
Willy Tarreau70089872008-06-13 21:12:51 +02004878 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01004879 t->data_source = DATA_SRC_STATS;
4880 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02004881 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02004882 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
4883 t->rep->prod->private = t;
4884 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01004885 return 1;
4886}
4887
Willy Tarreau4076a152009-04-02 15:18:36 +02004888/*
4889 * Capture a bad request or response and archive it in the proxy's structure.
4890 */
4891void http_capture_bad_message(struct error_snapshot *es, struct session *s,
4892 struct buffer *buf, struct http_msg *msg,
4893 struct proxy *other_end)
4894{
Willy Tarreau2df8d712009-05-01 11:33:17 +02004895 es->len = buf->r - (buf->data + msg->som);
4896 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02004897 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02004898 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02004899 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02004900 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02004901 es->when = date; // user-visible date
4902 es->sid = s->uniq_id;
4903 es->srv = s->srv;
4904 es->oe = other_end;
4905 es->src = s->cli_addr;
4906}
Willy Tarreaub2513902006-12-17 14:52:38 +01004907
Willy Tarreaubaaee002006-06-26 02:48:02 +02004908/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01004909 * Print a debug line with a header
4910 */
4911void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
4912{
4913 int len, max;
4914 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004915 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004916 max = end - start;
4917 UBOUND(max, sizeof(trash) - len - 1);
4918 len += strlcpy2(trash + len, start, max + 1);
4919 trash[len++] = '\n';
4920 write(1, trash, len);
4921}
4922
4923
Willy Tarreau8797c062007-05-07 00:55:35 +02004924/************************************************************************/
4925/* The code below is dedicated to ACL parsing and matching */
4926/************************************************************************/
4927
4928
4929
4930
4931/* 1. Check on METHOD
4932 * We use the pre-parsed method if it is known, and store its number as an
4933 * integer. If it is unknown, we use the pointer and the length.
4934 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02004935static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02004936{
4937 int len, meth;
4938
Willy Tarreauae8b7962007-06-09 23:10:04 +02004939 len = strlen(*text);
4940 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02004941
4942 pattern->val.i = meth;
4943 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02004944 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02004945 if (!pattern->ptr.str)
4946 return 0;
4947 pattern->len = len;
4948 }
4949 return 1;
4950}
4951
Willy Tarreaud41f8d82007-06-10 10:06:18 +02004952static int
Willy Tarreau97be1452007-06-10 11:47:14 +02004953acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
4954 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02004955{
4956 int meth;
4957 struct http_txn *txn = l7;
4958
Willy Tarreaub6866442008-07-14 23:54:42 +02004959 if (!txn)
4960 return 0;
4961
Willy Tarreau655dce92009-11-08 13:10:58 +01004962 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02004963 return 0;
4964
Willy Tarreau8797c062007-05-07 00:55:35 +02004965 meth = txn->meth;
4966 test->i = meth;
4967 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02004968 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
4969 /* ensure the indexes are not affected */
4970 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02004971 test->len = txn->req.sl.rq.m_l;
4972 test->ptr = txn->req.sol;
4973 }
4974 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
4975 return 1;
4976}
4977
4978static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
4979{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004980 int icase;
4981
Willy Tarreau8797c062007-05-07 00:55:35 +02004982 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02004983 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02004984
4985 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02004986 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004987
4988 /* Other method, we must compare the strings */
4989 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02004990 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02004991
4992 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
4993 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
4994 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02004995 return ACL_PAT_FAIL;
4996 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02004997}
4998
4999/* 2. Check on Request/Status Version
5000 * We simply compare strings here.
5001 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02005002static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02005003{
Willy Tarreauae8b7962007-06-09 23:10:04 +02005004 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005005 if (!pattern->ptr.str)
5006 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02005007 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02005008 return 1;
5009}
5010
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005011static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005012acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
5013 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005014{
5015 struct http_txn *txn = l7;
5016 char *ptr;
5017 int len;
5018
Willy Tarreaub6866442008-07-14 23:54:42 +02005019 if (!txn)
5020 return 0;
5021
Willy Tarreau655dce92009-11-08 13:10:58 +01005022 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005023 return 0;
5024
Willy Tarreau8797c062007-05-07 00:55:35 +02005025 len = txn->req.sl.rq.v_l;
5026 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
5027
5028 while ((len-- > 0) && (*ptr++ != '/'));
5029 if (len <= 0)
5030 return 0;
5031
5032 test->ptr = ptr;
5033 test->len = len;
5034
5035 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5036 return 1;
5037}
5038
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005039static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005040acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
5041 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005042{
5043 struct http_txn *txn = l7;
5044 char *ptr;
5045 int len;
5046
Willy Tarreaub6866442008-07-14 23:54:42 +02005047 if (!txn)
5048 return 0;
5049
Willy Tarreau655dce92009-11-08 13:10:58 +01005050 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005051 return 0;
5052
Willy Tarreau8797c062007-05-07 00:55:35 +02005053 len = txn->rsp.sl.st.v_l;
5054 ptr = txn->rsp.sol;
5055
5056 while ((len-- > 0) && (*ptr++ != '/'));
5057 if (len <= 0)
5058 return 0;
5059
5060 test->ptr = ptr;
5061 test->len = len;
5062
5063 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
5064 return 1;
5065}
5066
5067/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005068static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005069acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
5070 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005071{
5072 struct http_txn *txn = l7;
5073 char *ptr;
5074 int len;
5075
Willy Tarreaub6866442008-07-14 23:54:42 +02005076 if (!txn)
5077 return 0;
5078
Willy Tarreau655dce92009-11-08 13:10:58 +01005079 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005080 return 0;
5081
Willy Tarreau8797c062007-05-07 00:55:35 +02005082 len = txn->rsp.sl.st.c_l;
5083 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
5084
5085 test->i = __strl2ui(ptr, len);
5086 test->flags = ACL_TEST_F_VOL_1ST;
5087 return 1;
5088}
5089
5090/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02005091static int
Willy Tarreau97be1452007-06-10 11:47:14 +02005092acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
5093 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02005094{
5095 struct http_txn *txn = l7;
5096
Willy Tarreaub6866442008-07-14 23:54:42 +02005097 if (!txn)
5098 return 0;
5099
Willy Tarreau655dce92009-11-08 13:10:58 +01005100 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005101 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005102
Willy Tarreauc11416f2007-06-17 16:58:38 +02005103 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5104 /* ensure the indexes are not affected */
5105 return 0;
5106
Willy Tarreau8797c062007-05-07 00:55:35 +02005107 test->len = txn->req.sl.rq.u_l;
5108 test->ptr = txn->req.sol + txn->req.sl.rq.u;
5109
Willy Tarreauf3d25982007-05-08 22:45:09 +02005110 /* we do not need to set READ_ONLY because the data is in a buffer */
5111 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02005112 return 1;
5113}
5114
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005115static int
5116acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5117 struct acl_expr *expr, struct acl_test *test)
5118{
5119 struct http_txn *txn = l7;
5120
Willy Tarreaub6866442008-07-14 23:54:42 +02005121 if (!txn)
5122 return 0;
5123
Willy Tarreau655dce92009-11-08 13:10:58 +01005124 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005125 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005126
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005127 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5128 /* ensure the indexes are not affected */
5129 return 0;
5130
5131 /* Parse HTTP request */
5132 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5133 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
5134 test->i = AF_INET;
5135
5136 /*
5137 * If we are parsing url in frontend space, we prepare backend stage
5138 * to not parse again the same url ! optimization lazyness...
5139 */
5140 if (px->options & PR_O_HTTP_PROXY)
5141 l4->flags |= SN_ADDR_SET;
5142
5143 test->flags = ACL_TEST_F_READ_ONLY;
5144 return 1;
5145}
5146
5147static int
5148acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
5149 struct acl_expr *expr, struct acl_test *test)
5150{
5151 struct http_txn *txn = l7;
5152
Willy Tarreaub6866442008-07-14 23:54:42 +02005153 if (!txn)
5154 return 0;
5155
Willy Tarreau655dce92009-11-08 13:10:58 +01005156 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005157 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005158
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01005159 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5160 /* ensure the indexes are not affected */
5161 return 0;
5162
5163 /* Same optimization as url_ip */
5164 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
5165 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
5166
5167 if (px->options & PR_O_HTTP_PROXY)
5168 l4->flags |= SN_ADDR_SET;
5169
5170 test->flags = ACL_TEST_F_READ_ONLY;
5171 return 1;
5172}
5173
Willy Tarreauc11416f2007-06-17 16:58:38 +02005174/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
5175 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
5176 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02005177static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005178acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005179 struct acl_expr *expr, struct acl_test *test)
5180{
5181 struct http_txn *txn = l7;
5182 struct hdr_idx *idx = &txn->hdr_idx;
5183 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005184
Willy Tarreaub6866442008-07-14 23:54:42 +02005185 if (!txn)
5186 return 0;
5187
Willy Tarreau33a7e692007-06-10 19:45:56 +02005188 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5189 /* search for header from the beginning */
5190 ctx->idx = 0;
5191
Willy Tarreau33a7e692007-06-10 19:45:56 +02005192 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5193 test->flags |= ACL_TEST_F_FETCH_MORE;
5194 test->flags |= ACL_TEST_F_VOL_HDR;
5195 test->len = ctx->vlen;
5196 test->ptr = (char *)ctx->line + ctx->val;
5197 return 1;
5198 }
5199
5200 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5201 test->flags |= ACL_TEST_F_VOL_HDR;
5202 return 0;
5203}
5204
Willy Tarreau33a7e692007-06-10 19:45:56 +02005205static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005206acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
5207 struct acl_expr *expr, struct acl_test *test)
5208{
5209 struct http_txn *txn = l7;
5210
Willy Tarreaub6866442008-07-14 23:54:42 +02005211 if (!txn)
5212 return 0;
5213
Willy Tarreau655dce92009-11-08 13:10:58 +01005214 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005215 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005216
Willy Tarreauc11416f2007-06-17 16:58:38 +02005217 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5218 /* ensure the indexes are not affected */
5219 return 0;
5220
5221 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
5222}
5223
5224static int
5225acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
5226 struct acl_expr *expr, struct acl_test *test)
5227{
5228 struct http_txn *txn = l7;
5229
Willy Tarreaub6866442008-07-14 23:54:42 +02005230 if (!txn)
5231 return 0;
5232
Willy Tarreau655dce92009-11-08 13:10:58 +01005233 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005234 return 0;
5235
5236 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
5237}
5238
5239/* 6. Check on HTTP header count. The number of occurrences is returned.
5240 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5241 */
5242static int
5243acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005244 struct acl_expr *expr, struct acl_test *test)
5245{
5246 struct http_txn *txn = l7;
5247 struct hdr_idx *idx = &txn->hdr_idx;
5248 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005249 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02005250
Willy Tarreaub6866442008-07-14 23:54:42 +02005251 if (!txn)
5252 return 0;
5253
Willy Tarreau33a7e692007-06-10 19:45:56 +02005254 ctx.idx = 0;
5255 cnt = 0;
5256 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
5257 cnt++;
5258
5259 test->i = cnt;
5260 test->flags = ACL_TEST_F_VOL_HDR;
5261 return 1;
5262}
5263
Willy Tarreauc11416f2007-06-17 16:58:38 +02005264static int
5265acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5266 struct acl_expr *expr, struct acl_test *test)
5267{
5268 struct http_txn *txn = l7;
5269
Willy Tarreaub6866442008-07-14 23:54:42 +02005270 if (!txn)
5271 return 0;
5272
Willy Tarreau655dce92009-11-08 13:10:58 +01005273 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005274 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005275
Willy Tarreauc11416f2007-06-17 16:58:38 +02005276 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5277 /* ensure the indexes are not affected */
5278 return 0;
5279
5280 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
5281}
5282
5283static int
5284acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
5285 struct acl_expr *expr, struct acl_test *test)
5286{
5287 struct http_txn *txn = l7;
5288
Willy Tarreaub6866442008-07-14 23:54:42 +02005289 if (!txn)
5290 return 0;
5291
Willy Tarreau655dce92009-11-08 13:10:58 +01005292 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005293 return 0;
5294
5295 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
5296}
5297
Willy Tarreau33a7e692007-06-10 19:45:56 +02005298/* 7. Check on HTTP header's integer value. The integer value is returned.
5299 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02005300 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02005301 */
5302static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02005303acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02005304 struct acl_expr *expr, struct acl_test *test)
5305{
5306 struct http_txn *txn = l7;
5307 struct hdr_idx *idx = &txn->hdr_idx;
5308 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005309
Willy Tarreaub6866442008-07-14 23:54:42 +02005310 if (!txn)
5311 return 0;
5312
Willy Tarreau33a7e692007-06-10 19:45:56 +02005313 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5314 /* search for header from the beginning */
5315 ctx->idx = 0;
5316
Willy Tarreau33a7e692007-06-10 19:45:56 +02005317 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5318 test->flags |= ACL_TEST_F_FETCH_MORE;
5319 test->flags |= ACL_TEST_F_VOL_HDR;
5320 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
5321 return 1;
5322 }
5323
5324 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5325 test->flags |= ACL_TEST_F_VOL_HDR;
5326 return 0;
5327}
5328
Willy Tarreauc11416f2007-06-17 16:58:38 +02005329static int
5330acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5331 struct acl_expr *expr, struct acl_test *test)
5332{
5333 struct http_txn *txn = l7;
5334
Willy Tarreaub6866442008-07-14 23:54:42 +02005335 if (!txn)
5336 return 0;
5337
Willy Tarreau655dce92009-11-08 13:10:58 +01005338 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005339 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005340
Willy Tarreauc11416f2007-06-17 16:58:38 +02005341 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5342 /* ensure the indexes are not affected */
5343 return 0;
5344
5345 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
5346}
5347
5348static int
5349acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
5350 struct acl_expr *expr, struct acl_test *test)
5351{
5352 struct http_txn *txn = l7;
5353
Willy Tarreaub6866442008-07-14 23:54:42 +02005354 if (!txn)
5355 return 0;
5356
Willy Tarreau655dce92009-11-08 13:10:58 +01005357 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005358 return 0;
5359
5360 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
5361}
5362
Willy Tarreau106f9792009-09-19 07:54:16 +02005363/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
5364 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
5365 */
5366static int
5367acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
5368 struct acl_expr *expr, struct acl_test *test)
5369{
5370 struct http_txn *txn = l7;
5371 struct hdr_idx *idx = &txn->hdr_idx;
5372 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
5373
5374 if (!txn)
5375 return 0;
5376
5377 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
5378 /* search for header from the beginning */
5379 ctx->idx = 0;
5380
5381 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
5382 test->flags |= ACL_TEST_F_FETCH_MORE;
5383 test->flags |= ACL_TEST_F_VOL_HDR;
5384 /* Same optimization as url_ip */
5385 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
5386 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
5387 test->ptr = (void *)&l4->srv_addr.sin_addr;
5388 test->i = AF_INET;
5389 return 1;
5390 }
5391
5392 test->flags &= ~ACL_TEST_F_FETCH_MORE;
5393 test->flags |= ACL_TEST_F_VOL_HDR;
5394 return 0;
5395}
5396
5397static int
5398acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5399 struct acl_expr *expr, struct acl_test *test)
5400{
5401 struct http_txn *txn = l7;
5402
5403 if (!txn)
5404 return 0;
5405
Willy Tarreau655dce92009-11-08 13:10:58 +01005406 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02005407 return 0;
5408
5409 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5410 /* ensure the indexes are not affected */
5411 return 0;
5412
5413 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
5414}
5415
5416static int
5417acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
5418 struct acl_expr *expr, struct acl_test *test)
5419{
5420 struct http_txn *txn = l7;
5421
5422 if (!txn)
5423 return 0;
5424
Willy Tarreau655dce92009-11-08 13:10:58 +01005425 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02005426 return 0;
5427
5428 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
5429}
5430
Willy Tarreau737b0c12007-06-10 21:28:46 +02005431/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
5432 * the first '/' after the possible hostname, and ends before the possible '?'.
5433 */
5434static int
5435acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
5436 struct acl_expr *expr, struct acl_test *test)
5437{
5438 struct http_txn *txn = l7;
5439 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02005440
Willy Tarreaub6866442008-07-14 23:54:42 +02005441 if (!txn)
5442 return 0;
5443
Willy Tarreau655dce92009-11-08 13:10:58 +01005444 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02005445 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02005446
Willy Tarreauc11416f2007-06-17 16:58:38 +02005447 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
5448 /* ensure the indexes are not affected */
5449 return 0;
5450
Willy Tarreau21d2af32008-02-14 20:25:24 +01005451 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
5452 ptr = http_get_path(txn);
5453 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02005454 return 0;
5455
5456 /* OK, we got the '/' ! */
5457 test->ptr = ptr;
5458
5459 while (ptr < end && *ptr != '?')
5460 ptr++;
5461
5462 test->len = ptr - test->ptr;
5463
5464 /* we do not need to set READ_ONLY because the data is in a buffer */
5465 test->flags = ACL_TEST_F_VOL_1ST;
5466 return 1;
5467}
5468
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005469static int
5470acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
5471 struct acl_expr *expr, struct acl_test *test)
5472{
5473 struct buffer *req = s->req;
5474 struct http_txn *txn = &s->txn;
5475 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02005476
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005477 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
5478 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
5479 */
5480
5481 if (!s || !req)
5482 return 0;
5483
Willy Tarreau655dce92009-11-08 13:10:58 +01005484 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005485 /* Already decoded as OK */
5486 test->flags |= ACL_TEST_F_SET_RES_PASS;
5487 return 1;
5488 }
5489
5490 /* Try to decode HTTP request */
5491 if (likely(req->lr < req->r))
5492 http_msg_analyzer(req, msg, &txn->hdr_idx);
5493
Willy Tarreau655dce92009-11-08 13:10:58 +01005494 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005495 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
5496 test->flags |= ACL_TEST_F_SET_RES_FAIL;
5497 return 1;
5498 }
5499 /* wait for final state */
5500 test->flags |= ACL_TEST_F_MAY_CHANGE;
5501 return 0;
5502 }
5503
5504 /* OK we got a valid HTTP request. We have some minor preparation to
5505 * perform so that further checks can rely on HTTP tests.
5506 */
5507 msg->sol = req->data + msg->som;
5508 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
5509 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
5510 s->flags |= SN_REDIRECTABLE;
5511
5512 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
5513 test->flags |= ACL_TEST_F_SET_RES_FAIL;
5514 return 1;
5515 }
5516
5517 test->flags |= ACL_TEST_F_SET_RES_PASS;
5518 return 1;
5519}
5520
Willy Tarreau8797c062007-05-07 00:55:35 +02005521
5522/************************************************************************/
5523/* All supported keywords must be declared here. */
5524/************************************************************************/
5525
5526/* Note: must not be declared <const> as its list will be overwritten */
5527static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02005528 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
5529
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005530 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
5531 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5532 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5533 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02005534
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005535 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5536 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5537 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5538 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5539 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5540 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5541 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5542 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
5543 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02005544
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005545 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
5546 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5547 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5548 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5549 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5550 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5551 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5552 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
5553 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
5554 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02005555 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02005556
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005557 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
5558 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
5559 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
5560 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
5561 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
5562 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
5563 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
5564 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
5565 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02005566 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005567
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02005568 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
5569 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
5570 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
5571 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
5572 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
5573 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
5574 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02005575
Willy Tarreauf3d25982007-05-08 22:45:09 +02005576 { NULL, NULL, NULL, NULL },
5577
5578#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02005579 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
5580 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
5581 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
5582 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
5583 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
5584 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
5585 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
5586
Willy Tarreau8797c062007-05-07 00:55:35 +02005587 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
5588 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
5589 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
5590 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
5591 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
5592 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
5593 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
5594 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
5595
5596 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
5597 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
5598 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
5599 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
5600 { NULL, NULL, NULL, NULL },
5601#endif
5602}};
5603
5604
5605__attribute__((constructor))
5606static void __http_protocol_init(void)
5607{
5608 acl_register_keywords(&acl_kws);
5609}
5610
5611
Willy Tarreau58f10d72006-12-04 02:26:12 +01005612/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005613 * Local variables:
5614 * c-indent-level: 8
5615 * c-basic-offset: 8
5616 * End:
5617 */