blob: 2cab1989d64044bbc417a26ffdce703889531d40 [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>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010044#include <proto/checks.h>
Maik Broemme2850cb42009-04-17 18:53:21 +020045#include <proto/client.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/fd.h>
48#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010049#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020050#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010052#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010054#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010056#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020057#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/task.h>
59
Willy Tarreau522d6c02009-12-06 18:49:18 +010060const char HTTP_100[] =
61 "HTTP/1.1 100 Continue\r\n\r\n";
62
63const struct chunk http_100_chunk = {
64 .str = (char *)&HTTP_100,
65 .len = sizeof(HTTP_100)-1
66};
67
Willy Tarreau1c47f852006-07-09 08:22:27 +020068/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010069const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020070 "HTTP/1.0 200 OK\r\n"
71 "Cache-Control: no-cache\r\n"
72 "Connection: close\r\n"
73 "Content-Type: text/html\r\n"
74 "\r\n"
75 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
76
Willy Tarreau0f772532006-12-23 20:51:41 +010077const struct chunk http_200_chunk = {
78 .str = (char *)&HTTP_200,
79 .len = sizeof(HTTP_200)-1
80};
81
Willy Tarreaub463dfb2008-06-07 23:08:56 +020082const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020084 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020086 "Connection: close\r\n"
87 "Location: "; /* not terminated since it will be concatenated with the URL */
88
Willy Tarreau0f772532006-12-23 20:51:41 +010089const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Connection: close\r\n"
94 "Location: "; /* not terminated since it will be concatenated with the URL */
95
96/* same as 302 except that the browser MUST retry with the GET method */
97const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010098 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010099 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100100 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100101 "Connection: close\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
105const char *HTTP_401_fmt =
106 "HTTP/1.0 401 Unauthorized\r\n"
107 "Cache-Control: no-cache\r\n"
108 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200109 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
111 "\r\n"
112 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
113
Willy Tarreau0f772532006-12-23 20:51:41 +0100114
115const int http_err_codes[HTTP_ERR_SIZE] = {
116 [HTTP_ERR_400] = 400,
117 [HTTP_ERR_403] = 403,
118 [HTTP_ERR_408] = 408,
119 [HTTP_ERR_500] = 500,
120 [HTTP_ERR_502] = 502,
121 [HTTP_ERR_503] = 503,
122 [HTTP_ERR_504] = 504,
123};
124
Willy Tarreau80587432006-12-24 17:47:20 +0100125static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100126 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100127 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "\r\n"
132 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
133
134 [HTTP_ERR_403] =
135 "HTTP/1.0 403 Forbidden\r\n"
136 "Cache-Control: no-cache\r\n"
137 "Connection: close\r\n"
138 "Content-Type: text/html\r\n"
139 "\r\n"
140 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
141
142 [HTTP_ERR_408] =
143 "HTTP/1.0 408 Request Time-out\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
149
150 [HTTP_ERR_500] =
151 "HTTP/1.0 500 Server Error\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
157
158 [HTTP_ERR_502] =
159 "HTTP/1.0 502 Bad Gateway\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
165
166 [HTTP_ERR_503] =
167 "HTTP/1.0 503 Service Unavailable\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
173
174 [HTTP_ERR_504] =
175 "HTTP/1.0 504 Gateway Time-out\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
181
182};
183
Willy Tarreau80587432006-12-24 17:47:20 +0100184/* We must put the messages here since GCC cannot initialize consts depending
185 * on strlen().
186 */
187struct chunk http_err_chunks[HTTP_ERR_SIZE];
188
Willy Tarreau42250582007-04-01 01:30:43 +0200189#define FD_SETS_ARE_BITFIELDS
190#ifdef FD_SETS_ARE_BITFIELDS
191/*
192 * This map is used with all the FD_* macros to check whether a particular bit
193 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
194 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
195 * byte should be encoded. Be careful to always pass bytes from 0 to 255
196 * exclusively to the macros.
197 */
198fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
199fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
200
201#else
202#error "Check if your OS uses bitfields for fd_sets"
203#endif
204
Willy Tarreau80587432006-12-24 17:47:20 +0100205void init_proto_http()
206{
Willy Tarreau42250582007-04-01 01:30:43 +0200207 int i;
208 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100209 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200210
Willy Tarreau80587432006-12-24 17:47:20 +0100211 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
212 if (!http_err_msgs[msg]) {
213 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
214 abort();
215 }
216
217 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
218 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
219 }
Willy Tarreau42250582007-04-01 01:30:43 +0200220
221 /* initialize the log header encoding map : '{|}"#' should be encoded with
222 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
223 * URL encoding only requires '"', '#' to be encoded as well as non-
224 * printable characters above.
225 */
226 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
227 memset(url_encode_map, 0, sizeof(url_encode_map));
228 for (i = 0; i < 32; i++) {
229 FD_SET(i, hdr_encode_map);
230 FD_SET(i, url_encode_map);
231 }
232 for (i = 127; i < 256; i++) {
233 FD_SET(i, hdr_encode_map);
234 FD_SET(i, url_encode_map);
235 }
236
237 tmp = "\"#{|}";
238 while (*tmp) {
239 FD_SET(*tmp, hdr_encode_map);
240 tmp++;
241 }
242
243 tmp = "\"#";
244 while (*tmp) {
245 FD_SET(*tmp, url_encode_map);
246 tmp++;
247 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200248
249 /* memory allocations */
250 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200251 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100252}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253
Willy Tarreau53b6c742006-12-17 13:37:46 +0100254/*
255 * We have 26 list of methods (1 per first letter), each of which can have
256 * up to 3 entries (2 valid, 1 null).
257 */
258struct http_method_desc {
259 http_meth_t meth;
260 int len;
261 const char text[8];
262};
263
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100264const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100265 ['C' - 'A'] = {
266 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
267 },
268 ['D' - 'A'] = {
269 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
270 },
271 ['G' - 'A'] = {
272 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
273 },
274 ['H' - 'A'] = {
275 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
276 },
277 ['P' - 'A'] = {
278 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
279 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
280 },
281 ['T' - 'A'] = {
282 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
283 },
284 /* rest is empty like this :
285 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
286 */
287};
288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100289/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200290 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100291 * RFC2616 for those chars.
292 */
293
294const char http_is_spht[256] = {
295 [' '] = 1, ['\t'] = 1,
296};
297
298const char http_is_crlf[256] = {
299 ['\r'] = 1, ['\n'] = 1,
300};
301
302const char http_is_lws[256] = {
303 [' '] = 1, ['\t'] = 1,
304 ['\r'] = 1, ['\n'] = 1,
305};
306
307const char http_is_sep[256] = {
308 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
309 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
310 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
311 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
312 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
313};
314
315const char http_is_ctl[256] = {
316 [0 ... 31] = 1,
317 [127] = 1,
318};
319
320/*
321 * A token is any ASCII char that is neither a separator nor a CTL char.
322 * Do not overwrite values in assignment since gcc-2.95 will not handle
323 * them correctly. Instead, define every non-CTL char's status.
324 */
325const char http_is_token[256] = {
326 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
327 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
328 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
329 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
330 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
331 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
332 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
333 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
334 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
335 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
336 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
337 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
338 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
339 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
340 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
341 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
342 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
343 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
344 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
345 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
346 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
347 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
348 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
349 ['|'] = 1, ['}'] = 0, ['~'] = 1,
350};
351
352
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100353/*
354 * An http ver_token is any ASCII which can be found in an HTTP version,
355 * which includes 'H', 'T', 'P', '/', '.' and any digit.
356 */
357const char http_is_ver_token[256] = {
358 ['.'] = 1, ['/'] = 1,
359 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
360 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
361 ['H'] = 1, ['P'] = 1, ['T'] = 1,
362};
363
364
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100365/*
366 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
367 * CRLF. Text length is measured first, so it cannot be NULL.
368 * The header is also automatically added to the index <hdr_idx>, and the end
369 * of headers is automatically adjusted. The number of bytes added is returned
370 * on success, otherwise <0 is returned indicating an error.
371 */
372int http_header_add_tail(struct buffer *b, struct http_msg *msg,
373 struct hdr_idx *hdr_idx, const char *text)
374{
375 int bytes, len;
376
377 len = strlen(text);
378 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
379 if (!bytes)
380 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100381 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100382 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
383}
384
385/*
386 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
387 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
388 * the buffer is only opened and the space reserved, but nothing is copied.
389 * The header is also automatically added to the index <hdr_idx>, and the end
390 * of headers is automatically adjusted. The number of bytes added is returned
391 * on success, otherwise <0 is returned indicating an error.
392 */
393int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
394 struct hdr_idx *hdr_idx, const char *text, int len)
395{
396 int bytes;
397
398 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
399 if (!bytes)
400 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100401 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100402 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
403}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200404
405/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100406 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
407 * If so, returns the position of the first non-space character relative to
408 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
409 * to return a pointer to the place after the first space. Returns 0 if the
410 * header name does not match. Checks are case-insensitive.
411 */
412int http_header_match2(const char *hdr, const char *end,
413 const char *name, int len)
414{
415 const char *val;
416
417 if (hdr + len >= end)
418 return 0;
419 if (hdr[len] != ':')
420 return 0;
421 if (strncasecmp(hdr, name, len) != 0)
422 return 0;
423 val = hdr + len + 1;
424 while (val < end && HTTP_IS_SPHT(*val))
425 val++;
426 if ((val >= end) && (len + 2 <= end - hdr))
427 return len + 2; /* we may replace starting from second space */
428 return val - hdr;
429}
430
Willy Tarreau33a7e692007-06-10 19:45:56 +0200431/* Find the end of the header value contained between <s> and <e>.
432 * See RFC2616, par 2.2 for more information. Note that it requires
433 * a valid header to return a valid result.
434 */
435const char *find_hdr_value_end(const char *s, const char *e)
436{
437 int quoted, qdpair;
438
439 quoted = qdpair = 0;
440 for (; s < e; s++) {
441 if (qdpair) qdpair = 0;
442 else if (quoted && *s == '\\') qdpair = 1;
443 else if (quoted && *s == '"') quoted = 0;
444 else if (*s == '"') quoted = 1;
445 else if (*s == ',') return s;
446 }
447 return s;
448}
449
450/* Find the first or next occurrence of header <name> in message buffer <sol>
451 * using headers index <idx>, and return it in the <ctx> structure. This
452 * structure holds everything necessary to use the header and find next
453 * occurrence. If its <idx> member is 0, the header is searched from the
454 * beginning. Otherwise, the next occurrence is returned. The function returns
455 * 1 when it finds a value, and 0 when there is no more.
456 */
457int http_find_header2(const char *name, int len,
458 const char *sol, struct hdr_idx *idx,
459 struct hdr_ctx *ctx)
460{
Willy Tarreau33a7e692007-06-10 19:45:56 +0200461 const char *eol, *sov;
462 int cur_idx;
463
464 if (ctx->idx) {
465 /* We have previously returned a value, let's search
466 * another one on the same line.
467 */
468 cur_idx = ctx->idx;
469 sol = ctx->line;
470 sov = sol + ctx->val + ctx->vlen;
471 eol = sol + idx->v[cur_idx].len;
472
473 if (sov >= eol)
474 /* no more values in this header */
475 goto next_hdr;
476
477 /* values remaining for this header, skip the comma */
478 sov++;
479 while (sov < eol && http_is_lws[(unsigned char)*sov])
480 sov++;
481
482 goto return_hdr;
483 }
484
485 /* first request for this header */
486 sol += hdr_idx_first_pos(idx);
487 cur_idx = hdr_idx_first_idx(idx);
488
489 while (cur_idx) {
490 eol = sol + idx->v[cur_idx].len;
491
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200492 if (len == 0) {
493 /* No argument was passed, we want any header.
494 * To achieve this, we simply build a fake request. */
495 while (sol + len < eol && sol[len] != ':')
496 len++;
497 name = sol;
498 }
499
Willy Tarreau33a7e692007-06-10 19:45:56 +0200500 if ((len < eol - sol) &&
501 (sol[len] == ':') &&
502 (strncasecmp(sol, name, len) == 0)) {
503
504 sov = sol + len + 1;
505 while (sov < eol && http_is_lws[(unsigned char)*sov])
506 sov++;
507 return_hdr:
508 ctx->line = sol;
509 ctx->idx = cur_idx;
510 ctx->val = sov - sol;
511
512 eol = find_hdr_value_end(sov, eol);
513 ctx->vlen = eol - sov;
514 return 1;
515 }
516 next_hdr:
517 sol = eol + idx->v[cur_idx].cr + 1;
518 cur_idx = idx->v[cur_idx].next;
519 }
520 return 0;
521}
522
523int http_find_header(const char *name,
524 const char *sol, struct hdr_idx *idx,
525 struct hdr_ctx *ctx)
526{
527 return http_find_header2(name, strlen(name), sol, idx, ctx);
528}
529
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100530/* This function handles a server error at the stream interface level. The
531 * stream interface is assumed to be already in a closed state. An optional
532 * message is copied into the input buffer, and an HTTP status code stored.
533 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100534 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200535 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100536static void http_server_error(struct session *t, struct stream_interface *si,
537 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200538{
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100539 buffer_erase(si->ob);
540 buffer_erase(si->ib);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200541 buffer_auto_close(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100542 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100543 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100544 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545 }
546 if (!(t->flags & SN_ERR_MASK))
547 t->flags |= err;
548 if (!(t->flags & SN_FINST_MASK))
549 t->flags |= finst;
550}
551
Willy Tarreau80587432006-12-24 17:47:20 +0100552/* This function returns the appropriate error location for the given session
553 * and message.
554 */
555
556struct chunk *error_message(struct session *s, int msgnum)
557{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200558 if (s->be->errmsg[msgnum].str)
559 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100560 else if (s->fe->errmsg[msgnum].str)
561 return &s->fe->errmsg[msgnum];
562 else
563 return &http_err_chunks[msgnum];
564}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200565
Willy Tarreau53b6c742006-12-17 13:37:46 +0100566/*
567 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
568 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
569 */
570static http_meth_t find_http_meth(const char *str, const int len)
571{
572 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100573 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100574
575 m = ((unsigned)*str - 'A');
576
577 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100578 for (h = http_methods[m]; h->len > 0; h++) {
579 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100580 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100581 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100582 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100583 };
584 return HTTP_METH_OTHER;
585 }
586 return HTTP_METH_NONE;
587
588}
589
Willy Tarreau21d2af32008-02-14 20:25:24 +0100590/* Parse the URI from the given transaction (which is assumed to be in request
591 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
592 * It is returned otherwise.
593 */
594static char *
595http_get_path(struct http_txn *txn)
596{
597 char *ptr, *end;
598
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100599 ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100600 end = ptr + txn->req.sl.rq.u_l;
601
602 if (ptr >= end)
603 return NULL;
604
605 /* RFC2616, par. 5.1.2 :
606 * Request-URI = "*" | absuri | abspath | authority
607 */
608
609 if (*ptr == '*')
610 return NULL;
611
612 if (isalpha((unsigned char)*ptr)) {
613 /* this is a scheme as described by RFC3986, par. 3.1 */
614 ptr++;
615 while (ptr < end &&
616 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
617 ptr++;
618 /* skip '://' */
619 if (ptr == end || *ptr++ != ':')
620 return NULL;
621 if (ptr == end || *ptr++ != '/')
622 return NULL;
623 if (ptr == end || *ptr++ != '/')
624 return NULL;
625 }
626 /* skip [user[:passwd]@]host[:[port]] */
627
628 while (ptr < end && *ptr != '/')
629 ptr++;
630
631 if (ptr == end)
632 return NULL;
633
634 /* OK, we got the '/' ! */
635 return ptr;
636}
637
Willy Tarreauefb453c2008-10-26 20:49:47 +0100638/* Returns a 302 for a redirectable request. This may only be called just after
639 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
640 * left unchanged and will follow normal proxy processing.
641 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100642void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100643{
644 struct http_txn *txn;
645 struct chunk rdr;
646 char *path;
647 int len;
648
649 /* 1: create the response header */
650 rdr.len = strlen(HTTP_302);
651 rdr.str = trash;
652 memcpy(rdr.str, HTTP_302, rdr.len);
653
654 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200655 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100656 return;
657
658 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
659 rdr.len += s->srv->rdr_len;
660
661 /* 3: add the request URI */
662 txn = &s->txn;
663 path = http_get_path(txn);
664 if (!path)
665 return;
666
Willy Tarreaua95a1f42010-01-03 13:04:35 +0100667 len = txn->req.sl.rq.u_l + (txn->req.sol-txn->req.som+txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200668 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100669 return;
670
671 memcpy(rdr.str + rdr.len, path, len);
672 rdr.len += len;
673 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
674 rdr.len += 4;
675
676 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100677 si->shutr(si);
678 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100679 si->err_type = SI_ET_NONE;
680 si->err_loc = NULL;
681 si->state = SI_ST_CLO;
682
683 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100684 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100685
686 /* FIXME: we should increase a counter of redirects per server and per backend. */
687 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100688 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100689}
690
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100691/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100692 * that the server side is closed. Note that err_type is actually a
693 * bitmask, where almost only aborts may be cumulated with other
694 * values. We consider that aborted operations are more important
695 * than timeouts or errors due to the fact that nobody else in the
696 * logs might explain incomplete retries. All others should avoid
697 * being cumulated. It should normally not be possible to have multiple
698 * aborts at once, but just in case, the first one in sequence is reported.
699 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100700void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100701{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100702 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100703
704 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100705 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
706 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100707 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100708 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
709 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100710 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100711 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
712 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100713 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100714 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
715 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100716 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100717 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
718 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100719 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100720 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
721 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100722 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100723 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
724 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100725}
726
Willy Tarreau42250582007-04-01 01:30:43 +0200727extern const char sess_term_cond[8];
728extern const char sess_fin_state[8];
729extern const char *monthname[12];
730const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
731const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
732 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
733 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200734struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200735struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100736
Emeric Brun3a058f32009-06-30 18:26:00 +0200737void http_sess_clflog(struct session *s)
738{
739 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
740 struct proxy *fe = s->fe;
741 struct proxy *be = s->be;
742 struct proxy *prx_log;
743 struct http_txn *txn = &s->txn;
744 int tolog, level, err;
745 char *uri, *h;
746 char *svid;
747 struct tm tm;
748 static char tmpline[MAX_SYSLOG_LEN];
749 int hdr;
750 size_t w;
751 int t_request;
752
753 prx_log = fe;
754 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
755 (s->conn_retries != be->conn_retries) ||
756 txn->status >= 500;
757
758 if (s->cli_addr.ss_family == AF_INET)
759 inet_ntop(AF_INET,
760 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
761 pn, sizeof(pn));
762 else
763 inet_ntop(AF_INET6,
764 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
765 pn, sizeof(pn));
766
767 get_gmtime(s->logs.accept_date.tv_sec, &tm);
768
769 /* FIXME: let's limit ourselves to frontend logging for now. */
770 tolog = fe->to_log;
771
772 h = tmpline;
773
774 w = snprintf(h, sizeof(tmpline),
775 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
776 pn,
777 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
778 tm.tm_hour, tm.tm_min, tm.tm_sec);
779 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
780 goto trunc;
781 h += w;
782
783 if (h >= tmpline + sizeof(tmpline) - 4)
784 goto trunc;
785
786 *(h++) = ' ';
787 *(h++) = '\"';
788 uri = txn->uri ? txn->uri : "<BADREQ>";
789 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
790 '#', url_encode_map, uri);
791 *(h++) = '\"';
792
793 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
794 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
795 goto trunc;
796 h += w;
797
798 if (h >= tmpline + sizeof(tmpline) - 9)
799 goto trunc;
800 memcpy(h, " \"-\" \"-\"", 8);
801 h += 8;
802
803 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
804 " %d %03d",
805 (s->cli_addr.ss_family == AF_INET) ?
806 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
807 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
808 (int)s->logs.accept_date.tv_usec/1000);
809 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
810 goto trunc;
811 h += w;
812
813 w = strlen(fe->id);
814 if (h >= tmpline + sizeof(tmpline) - 4 - w)
815 goto trunc;
816 *(h++) = ' ';
817 *(h++) = '\"';
818 memcpy(h, fe->id, w);
819 h += w;
820 *(h++) = '\"';
821
822 w = strlen(be->id);
823 if (h >= tmpline + sizeof(tmpline) - 4 - w)
824 goto trunc;
825 *(h++) = ' ';
826 *(h++) = '\"';
827 memcpy(h, be->id, w);
828 h += w;
829 *(h++) = '\"';
830
831 svid = (tolog & LW_SVID) ?
832 (s->data_source != DATA_SRC_STATS) ?
833 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
834
835 w = strlen(svid);
836 if (h >= tmpline + sizeof(tmpline) - 4 - w)
837 goto trunc;
838 *(h++) = ' ';
839 *(h++) = '\"';
840 memcpy(h, svid, w);
841 h += w;
842 *(h++) = '\"';
843
844 t_request = -1;
845 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
846 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
847 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
848 " %d %ld %ld %ld %ld",
849 t_request,
850 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
851 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
852 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
853 s->logs.t_close);
854 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
855 goto trunc;
856 h += w;
857
858 if (h >= tmpline + sizeof(tmpline) - 8)
859 goto trunc;
860 *(h++) = ' ';
861 *(h++) = '\"';
862 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
863 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
864 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
865 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
866 *(h++) = '\"';
867
868 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
869 " %d %d %d %d %d %ld %ld",
870 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
871 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
872 s->logs.srv_queue_size, s->logs.prx_queue_size);
873
874 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
875 goto trunc;
876 h += w;
877
878 if (txn->cli_cookie) {
879 w = strlen(txn->cli_cookie);
880 if (h >= tmpline + sizeof(tmpline) - 4 - w)
881 goto trunc;
882 *(h++) = ' ';
883 *(h++) = '\"';
884 memcpy(h, txn->cli_cookie, w);
885 h += w;
886 *(h++) = '\"';
887 } else {
888 if (h >= tmpline + sizeof(tmpline) - 5)
889 goto trunc;
890 memcpy(h, " \"-\"", 4);
891 h += 4;
892 }
893
894 if (txn->srv_cookie) {
895 w = strlen(txn->srv_cookie);
896 if (h >= tmpline + sizeof(tmpline) - 4 - w)
897 goto trunc;
898 *(h++) = ' ';
899 *(h++) = '\"';
900 memcpy(h, txn->srv_cookie, w);
901 h += w;
902 *(h++) = '\"';
903 } else {
904 if (h >= tmpline + sizeof(tmpline) - 5)
905 goto trunc;
906 memcpy(h, " \"-\"", 4);
907 h += 4;
908 }
909
910 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
911 for (hdr = 0; hdr < fe->nb_req_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->req.cap[hdr]);
918 *(h++) = '\"';
919 }
920 }
921
922 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
923 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
924 if (h >= sizeof (tmpline) + tmpline - 4)
925 goto trunc;
926 *(h++) = ' ';
927 *(h++) = '\"';
928 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
929 '#', hdr_encode_map, txn->rsp.cap[hdr]);
930 *(h++) = '\"';
931 }
932 }
933
934trunc:
935 *h = '\0';
936
937 level = LOG_INFO;
938 if (err && (fe->options2 & PR_O2_LOGERRORS))
939 level = LOG_ERR;
940
941 send_log(prx_log, level, "%s\n", tmpline);
942
943 s->logs.logwait = 0;
944}
945
Willy Tarreau42250582007-04-01 01:30:43 +0200946/*
947 * send a log for the session when we have enough info about it.
948 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100949 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100950void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +0200951{
952 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
953 struct proxy *fe = s->fe;
954 struct proxy *be = s->be;
955 struct proxy *prx_log;
956 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200957 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200958 char *uri, *h;
959 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200960 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +0200961 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +0200962 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +0200963 int hdr;
964
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200965 /* if we don't want to log normal traffic, return now */
966 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
967 (s->conn_retries != be->conn_retries) ||
968 txn->status >= 500;
969 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
970 return;
971
Willy Tarreau42250582007-04-01 01:30:43 +0200972 if (fe->logfac1 < 0 && fe->logfac2 < 0)
973 return;
974 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100975
Emeric Brun3a058f32009-06-30 18:26:00 +0200976 if (prx_log->options2 & PR_O2_CLFLOG)
977 return http_sess_clflog(s);
978
Willy Tarreau42250582007-04-01 01:30:43 +0200979 if (s->cli_addr.ss_family == AF_INET)
980 inet_ntop(AF_INET,
981 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
982 pn, sizeof(pn));
983 else
984 inet_ntop(AF_INET6,
985 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
986 pn, sizeof(pn));
987
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200988 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +0200989
990 /* FIXME: let's limit ourselves to frontend logging for now. */
991 tolog = fe->to_log;
992
993 h = tmpline;
994 if (fe->to_log & LW_REQHDR &&
995 txn->req.cap &&
996 (h < tmpline + sizeof(tmpline) - 10)) {
997 *(h++) = ' ';
998 *(h++) = '{';
999 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1000 if (hdr)
1001 *(h++) = '|';
1002 if (txn->req.cap[hdr] != NULL)
1003 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1004 '#', hdr_encode_map, txn->req.cap[hdr]);
1005 }
1006 *(h++) = '}';
1007 }
1008
1009 if (fe->to_log & LW_RSPHDR &&
1010 txn->rsp.cap &&
1011 (h < tmpline + sizeof(tmpline) - 7)) {
1012 *(h++) = ' ';
1013 *(h++) = '{';
1014 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1015 if (hdr)
1016 *(h++) = '|';
1017 if (txn->rsp.cap[hdr] != NULL)
1018 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1019 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1020 }
1021 *(h++) = '}';
1022 }
1023
1024 if (h < tmpline + sizeof(tmpline) - 4) {
1025 *(h++) = ' ';
1026 *(h++) = '"';
1027 uri = txn->uri ? txn->uri : "<BADREQ>";
1028 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1029 '#', url_encode_map, uri);
1030 *(h++) = '"';
1031 }
1032 *h = '\0';
1033
1034 svid = (tolog & LW_SVID) ?
1035 (s->data_source != DATA_SRC_STATS) ?
1036 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1037
Willy Tarreau70089872008-06-13 21:12:51 +02001038 t_request = -1;
1039 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1040 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1041
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001042 level = LOG_INFO;
1043 if (err && (fe->options2 & PR_O2_LOGERRORS))
1044 level = LOG_ERR;
1045
1046 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001047 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001048 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1049 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001050 pn,
1051 (s->cli_addr.ss_family == AF_INET) ?
1052 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1053 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001054 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001055 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001056 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001057 t_request,
1058 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001059 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1060 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1061 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1062 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001063 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001064 txn->cli_cookie ? txn->cli_cookie : "-",
1065 txn->srv_cookie ? txn->srv_cookie : "-",
1066 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1067 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1068 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1069 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1070 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001071 (s->flags & SN_REDISP)?"+":"",
1072 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001073 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1074
1075 s->logs.logwait = 0;
1076}
1077
Willy Tarreau117f59e2007-03-04 18:17:17 +01001078
1079/*
1080 * Capture headers from message starting at <som> according to header list
1081 * <cap_hdr>, and fill the <idx> structure appropriately.
1082 */
1083void capture_headers(char *som, struct hdr_idx *idx,
1084 char **cap, struct cap_hdr *cap_hdr)
1085{
1086 char *eol, *sol, *col, *sov;
1087 int cur_idx;
1088 struct cap_hdr *h;
1089 int len;
1090
1091 sol = som + hdr_idx_first_pos(idx);
1092 cur_idx = hdr_idx_first_idx(idx);
1093
1094 while (cur_idx) {
1095 eol = sol + idx->v[cur_idx].len;
1096
1097 col = sol;
1098 while (col < eol && *col != ':')
1099 col++;
1100
1101 sov = col + 1;
1102 while (sov < eol && http_is_lws[(unsigned char)*sov])
1103 sov++;
1104
1105 for (h = cap_hdr; h; h = h->next) {
1106 if ((h->namelen == col - sol) &&
1107 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1108 if (cap[h->index] == NULL)
1109 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001110 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001111
1112 if (cap[h->index] == NULL) {
1113 Alert("HTTP capture : out of memory.\n");
1114 continue;
1115 }
1116
1117 len = eol - sov;
1118 if (len > h->len)
1119 len = h->len;
1120
1121 memcpy(cap[h->index], sov, len);
1122 cap[h->index][len]=0;
1123 }
1124 }
1125 sol = eol + idx->v[cur_idx].cr + 1;
1126 cur_idx = idx->v[cur_idx].next;
1127 }
1128}
1129
1130
Willy Tarreau42250582007-04-01 01:30:43 +02001131/* either we find an LF at <ptr> or we jump to <bad>.
1132 */
1133#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1134
1135/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1136 * otherwise to <http_msg_ood> with <state> set to <st>.
1137 */
1138#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1139 ptr++; \
1140 if (likely(ptr < end)) \
1141 goto good; \
1142 else { \
1143 state = (st); \
1144 goto http_msg_ood; \
1145 } \
1146 } while (0)
1147
1148
Willy Tarreaubaaee002006-06-26 02:48:02 +02001149/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001150 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001151 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1152 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1153 * will give undefined results.
1154 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1155 * and that msg->sol points to the beginning of the response.
1156 * If a complete line is found (which implies that at least one CR or LF is
1157 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1158 * returned indicating an incomplete line (which does not mean that parts have
1159 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1160 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1161 * upon next call.
1162 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001163 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001164 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1165 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001166 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001167 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001168const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1169 unsigned int state, const char *ptr, const char *end,
1170 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001171{
Willy Tarreau8973c702007-01-21 23:58:29 +01001172 switch (state) {
1173 http_msg_rpver:
1174 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001175 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001176 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1177
1178 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001179 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001180 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1181 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001182 state = HTTP_MSG_ERROR;
1183 break;
1184
Willy Tarreau8973c702007-01-21 23:58:29 +01001185 http_msg_rpver_sp:
1186 case HTTP_MSG_RPVER_SP:
1187 if (likely(!HTTP_IS_LWS(*ptr))) {
1188 msg->sl.st.c = ptr - msg_buf;
1189 goto http_msg_rpcode;
1190 }
1191 if (likely(HTTP_IS_SPHT(*ptr)))
1192 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1193 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001194 state = HTTP_MSG_ERROR;
1195 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001196
1197 http_msg_rpcode:
1198 case HTTP_MSG_RPCODE:
1199 if (likely(!HTTP_IS_LWS(*ptr)))
1200 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1201
1202 if (likely(HTTP_IS_SPHT(*ptr))) {
1203 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1204 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1205 }
1206
1207 /* so it's a CR/LF, so there is no reason phrase */
1208 msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c;
1209 http_msg_rsp_reason:
1210 /* FIXME: should we support HTTP responses without any reason phrase ? */
1211 msg->sl.st.r = ptr - msg_buf;
1212 msg->sl.st.r_l = 0;
1213 goto http_msg_rpline_eol;
1214
1215 http_msg_rpcode_sp:
1216 case HTTP_MSG_RPCODE_SP:
1217 if (likely(!HTTP_IS_LWS(*ptr))) {
1218 msg->sl.st.r = ptr - msg_buf;
1219 goto http_msg_rpreason;
1220 }
1221 if (likely(HTTP_IS_SPHT(*ptr)))
1222 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1223 /* so it's a CR/LF, so there is no reason phrase */
1224 goto http_msg_rsp_reason;
1225
1226 http_msg_rpreason:
1227 case HTTP_MSG_RPREASON:
1228 if (likely(!HTTP_IS_CRLF(*ptr)))
1229 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
1230 msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r;
1231 http_msg_rpline_eol:
1232 /* We have seen the end of line. Note that we do not
1233 * necessarily have the \n yet, but at least we know that we
1234 * have EITHER \r OR \n, otherwise the response would not be
1235 * complete. We can then record the response length and return
1236 * to the caller which will be able to register it.
1237 */
1238 msg->sl.st.l = ptr - msg->sol;
1239 return ptr;
1240
1241#ifdef DEBUG_FULL
1242 default:
1243 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1244 exit(1);
1245#endif
1246 }
1247
1248 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001249 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001250 if (ret_state)
1251 *ret_state = state;
1252 if (ret_ptr)
1253 *ret_ptr = (char *)ptr;
1254 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001255}
1256
1257
1258/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001259 * This function parses a request line between <ptr> and <end>, starting with
1260 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1261 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1262 * will give undefined results.
1263 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1264 * and that msg->sol points to the beginning of the request.
1265 * If a complete line is found (which implies that at least one CR or LF is
1266 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1267 * returned indicating an incomplete line (which does not mean that parts have
1268 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1269 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1270 * upon next call.
1271 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001272 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1274 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001275 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001276 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001277const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1278 unsigned int state, const char *ptr, const char *end,
1279 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001280{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001281 switch (state) {
1282 http_msg_rqmeth:
1283 case HTTP_MSG_RQMETH:
1284 if (likely(HTTP_IS_TOKEN(*ptr)))
1285 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001288 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1290 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001291
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001292 if (likely(HTTP_IS_CRLF(*ptr))) {
1293 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001294 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 http_msg_req09_uri:
1296 msg->sl.rq.u = ptr - msg_buf;
1297 http_msg_req09_uri_e:
1298 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1299 http_msg_req09_ver:
1300 msg->sl.rq.v = ptr - msg_buf;
1301 msg->sl.rq.v_l = 0;
1302 goto http_msg_rqline_eol;
1303 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001304 state = HTTP_MSG_ERROR;
1305 break;
1306
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001307 http_msg_rqmeth_sp:
1308 case HTTP_MSG_RQMETH_SP:
1309 if (likely(!HTTP_IS_LWS(*ptr))) {
1310 msg->sl.rq.u = ptr - msg_buf;
1311 goto http_msg_rquri;
1312 }
1313 if (likely(HTTP_IS_SPHT(*ptr)))
1314 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1315 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1316 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001317
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 http_msg_rquri:
1319 case HTTP_MSG_RQURI:
1320 if (likely(!HTTP_IS_LWS(*ptr)))
1321 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001322
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001323 if (likely(HTTP_IS_SPHT(*ptr))) {
1324 msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u;
1325 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1326 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1329 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001330
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001331 http_msg_rquri_sp:
1332 case HTTP_MSG_RQURI_SP:
1333 if (likely(!HTTP_IS_LWS(*ptr))) {
1334 msg->sl.rq.v = ptr - msg_buf;
1335 goto http_msg_rqver;
1336 }
1337 if (likely(HTTP_IS_SPHT(*ptr)))
1338 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1339 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1340 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001341
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 http_msg_rqver:
1343 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001344 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001345 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001346
1347 if (likely(HTTP_IS_CRLF(*ptr))) {
1348 msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v;
1349 http_msg_rqline_eol:
1350 /* We have seen the end of line. Note that we do not
1351 * necessarily have the \n yet, but at least we know that we
1352 * have EITHER \r OR \n, otherwise the request would not be
1353 * complete. We can then record the request length and return
1354 * to the caller which will be able to register it.
1355 */
1356 msg->sl.rq.l = ptr - msg->sol;
1357 return ptr;
1358 }
1359
1360 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001361 state = HTTP_MSG_ERROR;
1362 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001363
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001364#ifdef DEBUG_FULL
1365 default:
1366 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1367 exit(1);
1368#endif
1369 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001370
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001372 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001373 if (ret_state)
1374 *ret_state = state;
1375 if (ret_ptr)
1376 *ret_ptr = (char *)ptr;
1377 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001378}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001379
1380
Willy Tarreau8973c702007-01-21 23:58:29 +01001381/*
1382 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001383 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001384 * when data are missing and recalled at the exact same location with no
1385 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001386 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001387 * fields. Note that msg->som and msg->sol will be initialized after completing
1388 * the first state, so that none of the msg pointers has to be initialized
1389 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001390 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001391void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1392{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001393 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001395
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001396 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 ptr = buf->lr;
1398 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001399
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 if (unlikely(ptr >= end))
1401 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001404 /*
1405 * First, states that are specific to the response only.
1406 * We check them first so that request and headers are
1407 * closer to each other (accessed more often).
1408 */
1409 http_msg_rpbefore:
1410 case HTTP_MSG_RPBEFORE:
1411 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001412 /* we have a start of message, but we have to check
1413 * first if we need to remove some CRLF. We can only
1414 * do this when send_max=0.
1415 */
1416 char *beg = buf->w + buf->send_max;
1417 if (beg >= buf->data + buf->size)
1418 beg -= buf->size;
1419 if (unlikely(ptr != beg)) {
1420 if (buf->send_max)
1421 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001422 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001423 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001424 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001425 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001426 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001427 hdr_idx_init(idx);
1428 state = HTTP_MSG_RPVER;
1429 goto http_msg_rpver;
1430 }
1431
1432 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1433 goto http_msg_invalid;
1434
1435 if (unlikely(*ptr == '\n'))
1436 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1437 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1438 /* stop here */
1439
1440 http_msg_rpbefore_cr:
1441 case HTTP_MSG_RPBEFORE_CR:
1442 EXPECT_LF_HERE(ptr, http_msg_invalid);
1443 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1444 /* stop here */
1445
1446 http_msg_rpver:
1447 case HTTP_MSG_RPVER:
1448 case HTTP_MSG_RPVER_SP:
1449 case HTTP_MSG_RPCODE:
1450 case HTTP_MSG_RPCODE_SP:
1451 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001452 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001453 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001454 if (unlikely(!ptr))
1455 return;
1456
1457 /* we have a full response and we know that we have either a CR
1458 * or an LF at <ptr>.
1459 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001460 //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 +01001461 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1462
1463 msg->sol = ptr;
1464 if (likely(*ptr == '\r'))
1465 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1466 goto http_msg_rpline_end;
1467
1468 http_msg_rpline_end:
1469 case HTTP_MSG_RPLINE_END:
1470 /* msg->sol must point to the first of CR or LF. */
1471 EXPECT_LF_HERE(ptr, http_msg_invalid);
1472 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1473 /* stop here */
1474
1475 /*
1476 * Second, states that are specific to the request only
1477 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 http_msg_rqbefore:
1479 case HTTP_MSG_RQBEFORE:
1480 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001481 /* we have a start of message, but we have to check
1482 * first if we need to remove some CRLF. We can only
1483 * do this when send_max=0.
1484 */
1485 char *beg = buf->w + buf->send_max;
1486 if (beg >= buf->data + buf->size)
1487 beg -= buf->size;
1488 if (likely(ptr != beg)) {
1489 if (buf->send_max)
1490 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001491 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001492 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001494 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001495 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001496 /* we will need this when keep-alive will be supported
1497 hdr_idx_init(idx);
1498 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001499 state = HTTP_MSG_RQMETH;
1500 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001502
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1504 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001505
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 if (unlikely(*ptr == '\n'))
1507 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1508 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001509 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001510
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 http_msg_rqbefore_cr:
1512 case HTTP_MSG_RQBEFORE_CR:
1513 EXPECT_LF_HERE(ptr, http_msg_invalid);
1514 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001515 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 http_msg_rqmeth:
1518 case HTTP_MSG_RQMETH:
1519 case HTTP_MSG_RQMETH_SP:
1520 case HTTP_MSG_RQURI:
1521 case HTTP_MSG_RQURI_SP:
1522 case HTTP_MSG_RQVER:
1523 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001524 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 if (unlikely(!ptr))
1526 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001527
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 /* we have a full request and we know that we have either a CR
1529 * or an LF at <ptr>.
1530 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001531 //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 +01001532 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001533
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 msg->sol = ptr;
1535 if (likely(*ptr == '\r'))
1536 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001538
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 http_msg_rqline_end:
1540 case HTTP_MSG_RQLINE_END:
1541 /* check for HTTP/0.9 request : no version information available.
1542 * msg->sol must point to the first of CR or LF.
1543 */
1544 if (unlikely(msg->sl.rq.v_l == 0))
1545 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001546
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 EXPECT_LF_HERE(ptr, http_msg_invalid);
1548 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001549 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001550
Willy Tarreau8973c702007-01-21 23:58:29 +01001551 /*
1552 * Common states below
1553 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 http_msg_hdr_first:
1555 case HTTP_MSG_HDR_FIRST:
1556 msg->sol = ptr;
1557 if (likely(!HTTP_IS_CRLF(*ptr))) {
1558 goto http_msg_hdr_name;
1559 }
1560
1561 if (likely(*ptr == '\r'))
1562 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1563 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001564
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 http_msg_hdr_name:
1566 case HTTP_MSG_HDR_NAME:
1567 /* assumes msg->sol points to the first char */
1568 if (likely(HTTP_IS_TOKEN(*ptr)))
1569 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 if (likely(*ptr == ':')) {
1572 msg->col = ptr - buf->data;
1573 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1574 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001575
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001576 if (likely(msg->err_pos < -1) || *ptr == '\n')
1577 goto http_msg_invalid;
1578
1579 if (msg->err_pos == -1) /* capture error pointer */
1580 msg->err_pos = ptr - buf->data; /* >= 0 now */
1581
1582 /* and we still accept this non-token character */
1583 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001584
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 http_msg_hdr_l1_sp:
1586 case HTTP_MSG_HDR_L1_SP:
1587 /* assumes msg->sol points to the first char and msg->col to the colon */
1588 if (likely(HTTP_IS_SPHT(*ptr)))
1589 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001590
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 /* header value can be basically anything except CR/LF */
1592 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001593
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 if (likely(!HTTP_IS_CRLF(*ptr))) {
1595 goto http_msg_hdr_val;
1596 }
1597
1598 if (likely(*ptr == '\r'))
1599 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1600 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001601
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602 http_msg_hdr_l1_lf:
1603 case HTTP_MSG_HDR_L1_LF:
1604 EXPECT_LF_HERE(ptr, http_msg_invalid);
1605 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001606
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 http_msg_hdr_l1_lws:
1608 case HTTP_MSG_HDR_L1_LWS:
1609 if (likely(HTTP_IS_SPHT(*ptr))) {
1610 /* replace HT,CR,LF with spaces */
1611 for (; buf->data+msg->sov < ptr; msg->sov++)
1612 buf->data[msg->sov] = ' ';
1613 goto http_msg_hdr_l1_sp;
1614 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001615 /* we had a header consisting only in spaces ! */
1616 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001617 goto http_msg_complete_header;
1618
1619 http_msg_hdr_val:
1620 case HTTP_MSG_HDR_VAL:
1621 /* assumes msg->sol points to the first char, msg->col to the
1622 * colon, and msg->sov points to the first character of the
1623 * value.
1624 */
1625 if (likely(!HTTP_IS_CRLF(*ptr)))
1626 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 msg->eol = ptr;
1629 /* Note: we could also copy eol into ->eoh so that we have the
1630 * real header end in case it ends with lots of LWS, but is this
1631 * really needed ?
1632 */
1633 if (likely(*ptr == '\r'))
1634 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1635 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 http_msg_hdr_l2_lf:
1638 case HTTP_MSG_HDR_L2_LF:
1639 EXPECT_LF_HERE(ptr, http_msg_invalid);
1640 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 http_msg_hdr_l2_lws:
1643 case HTTP_MSG_HDR_L2_LWS:
1644 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1645 /* LWS: replace HT,CR,LF with spaces */
1646 for (; msg->eol < ptr; msg->eol++)
1647 *msg->eol = ' ';
1648 goto http_msg_hdr_val;
1649 }
1650 http_msg_complete_header:
1651 /*
1652 * It was a new header, so the last one is finished.
1653 * Assumes msg->sol points to the first char, msg->col to the
1654 * colon, msg->sov points to the first character of the value
1655 * and msg->eol to the first CR or LF so we know how the line
1656 * ends. We insert last header into the index.
1657 */
1658 /*
1659 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1660 write(2, msg->sol, msg->eol-msg->sol);
1661 fprintf(stderr,"\n");
1662 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1665 idx, idx->tail) < 0))
1666 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001667
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 msg->sol = ptr;
1669 if (likely(!HTTP_IS_CRLF(*ptr))) {
1670 goto http_msg_hdr_name;
1671 }
1672
1673 if (likely(*ptr == '\r'))
1674 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1675 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001676
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001677 http_msg_last_lf:
1678 case HTTP_MSG_LAST_LF:
1679 /* Assumes msg->sol points to the first of either CR or LF */
1680 EXPECT_LF_HERE(ptr, http_msg_invalid);
1681 ptr++;
1682 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001683 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 msg->eoh = msg->sol - buf->data;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001685 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001686 return;
1687#ifdef DEBUG_FULL
1688 default:
1689 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1690 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001691#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 }
1693 http_msg_ood:
1694 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001695 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001696 buf->lr = ptr;
1697 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001698
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001699 http_msg_invalid:
1700 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001701 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001702 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 return;
1704}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001705
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001706/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1707 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1708 * nothing is done and 1 is returned.
1709 */
1710static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1711{
1712 int delta;
1713 char *cur_end;
1714
1715 if (msg->sl.rq.v_l != 0)
1716 return 1;
1717
1718 msg->sol = req->data + msg->som;
1719 cur_end = msg->sol + msg->sl.rq.l;
1720 delta = 0;
1721
1722 if (msg->sl.rq.u_l == 0) {
1723 /* if no URI was set, add "/" */
1724 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1725 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001726 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001727 }
1728 /* add HTTP version */
1729 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001730 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001731 cur_end += delta;
1732 cur_end = (char *)http_parse_reqline(msg, req->data,
1733 HTTP_MSG_RQMETH,
1734 msg->sol, cur_end + 1,
1735 NULL, NULL);
1736 if (unlikely(!cur_end))
1737 return 0;
1738
1739 /* we have a full HTTP/1.0 request now and we know that
1740 * we have either a CR or an LF at <ptr>.
1741 */
1742 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1743 return 1;
1744}
1745
Willy Tarreau5b154472009-12-21 20:11:07 +01001746/* Parse the Connection: headaer of an HTTP request, and set the transaction
1747 * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag
1748 * is also set so that we don't parse a second time. If some dangerous values
1749 * are encountered, we leave the status to indicate that the request might be
1750 * interpreted as keep-alive, but we also set the connection flags to indicate
1751 * that we WANT it to be a close, so that the header will be fixed. This
1752 * function should only be called when we know we're interested in checking
1753 * the request (not a CONNECT, and FE or BE mangles the header).
1754 */
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001755void http_req_parse_connection_header(struct http_txn *txn)
Willy Tarreau5b154472009-12-21 20:11:07 +01001756{
1757 struct http_msg *msg = &txn->req;
1758 struct hdr_ctx ctx;
1759 int conn_cl, conn_ka;
1760
1761 if (txn->flags & TX_CON_HDR_PARS)
1762 return;
1763
1764 conn_cl = 0;
1765 conn_ka = 0;
1766 ctx.idx = 0;
1767
1768 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
1769 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
1770 conn_cl = 1;
1771 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
1772 conn_ka = 1;
1773 }
1774
1775 /* Determine if the client wishes keep-alive or close.
1776 * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections
1777 * are persistent unless "Connection: close" is explicitly specified.
1778 * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections.
1779 * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless
1780 * they explicitly specify "Connection: Keep-Alive", regardless of any
1781 * optional "Keep-Alive" header.
1782 * Note that if we find a request with both "Connection: close" and
1783 * "Connection: Keep-Alive", we indicate we want a close but don't have
1784 * it, so that it can be enforced later.
1785 */
1786
1787 if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */
1788 if (conn_cl) {
1789 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1790 if (!conn_ka)
1791 txn->flags |= TX_REQ_CONN_CLO;
1792 }
1793 } else { /* HTTP/1.0 */
1794 if (!conn_ka)
1795 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO | TX_REQ_CONN_CLO;
1796 else if (conn_cl)
1797 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1798 }
1799 txn->flags |= TX_CON_HDR_PARS;
1800}
1801
Willy Tarreaud98cf932009-12-27 22:54:55 +01001802/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1803 * first byte of body, and increments msg->sov by the number of bytes parsed,
1804 * so that we know we can forward between ->som and ->sov. Note that due to
1805 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1806 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001807 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001808 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001809 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001810int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001811{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001812 char *ptr = buf->lr;
1813 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001814 unsigned int chunk = 0;
1815
1816 /* The chunk size is in the following form, though we are only
1817 * interested in the size and CRLF :
1818 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1819 */
1820 while (1) {
1821 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001822 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001823 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001824 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001825 if (c < 0) /* not a hex digit anymore */
1826 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001827 if (++ptr >= end)
1828 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01001829 if (chunk & 0xF000000) /* overflow will occur */
1830 return -1;
1831 chunk = (chunk << 4) + c;
1832 }
1833
Willy Tarreaud98cf932009-12-27 22:54:55 +01001834 /* empty size not allowed */
1835 if (ptr == buf->lr)
1836 return -1;
1837
1838 while (http_is_spht[(unsigned char)*ptr]) {
1839 if (++ptr >= end)
1840 ptr = buf->data;
1841 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001842 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001843 }
1844
Willy Tarreaud98cf932009-12-27 22:54:55 +01001845 /* Up to there, we know that at least one byte is present at *ptr. Check
1846 * for the end of chunk size.
1847 */
1848 while (1) {
1849 if (likely(HTTP_IS_CRLF(*ptr))) {
1850 /* we now have a CR or an LF at ptr */
1851 if (likely(*ptr == '\r')) {
1852 if (++ptr >= end)
1853 ptr = buf->data;
1854 if (ptr == buf->r)
1855 return 0;
1856 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001857
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858 if (*ptr != '\n')
1859 return -1;
1860 if (++ptr >= end)
1861 ptr = buf->data;
1862 /* done */
1863 break;
1864 }
1865 else if (*ptr == ';') {
1866 /* chunk extension, ends at next CRLF */
1867 if (++ptr >= end)
1868 ptr = buf->data;
1869 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001870 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001871
1872 while (!HTTP_IS_CRLF(*ptr)) {
1873 if (++ptr >= end)
1874 ptr = buf->data;
1875 if (ptr == buf->r)
1876 return 0;
1877 }
1878 /* we have a CRLF now, loop above */
1879 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001880 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001881 else
Willy Tarreau115acb92009-12-26 13:56:06 +01001882 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001883 }
1884
Willy Tarreaud98cf932009-12-27 22:54:55 +01001885 /* OK we found our CRLF and now <ptr> points to the next byte,
1886 * which may or may not be present. We save that into ->lr and
1887 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001888 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001889 msg->sov += ptr - buf->lr;
1890 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01001891 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001892 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001893 return 1;
1894}
1895
Willy Tarreaud98cf932009-12-27 22:54:55 +01001896/* This function skips trailers in the buffer <buf> associated with HTTP
1897 * message <msg>. The first visited position is buf->lr. If the end of
1898 * the trailers is found, it is automatically scheduled to be forwarded,
1899 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1900 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001901 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1902 * zero. If a parse error is encountered, the function returns < 0 and does not
1903 * change anything except maybe buf->lr and msg->sov. Note that the message
1904 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1905 * which implies that all non-trailers data have already been scheduled for
1906 * forwarding, and that the difference between msg->som and msg->sov exactly
1907 * matches the length of trailers already parsed and not forwarded. It is also
1908 * important to note that this function is designed to be able to parse wrapped
1909 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001910 */
1911int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1912{
1913 /* we have buf->lr which points to next line. Look for CRLF. */
1914 while (1) {
1915 char *p1 = NULL, *p2 = NULL;
1916 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001917 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001918
1919 /* scan current line and stop at LF or CRLF */
1920 while (1) {
1921 if (ptr == buf->r)
1922 return 0;
1923
1924 if (*ptr == '\n') {
1925 if (!p1)
1926 p1 = ptr;
1927 p2 = ptr;
1928 break;
1929 }
1930
1931 if (*ptr == '\r') {
1932 if (p1)
1933 return -1;
1934 p1 = ptr;
1935 }
1936
1937 ptr++;
1938 if (ptr >= buf->data + buf->size)
1939 ptr = buf->data;
1940 }
1941
1942 /* after LF; point to beginning of next line */
1943 p2++;
1944 if (p2 >= buf->data + buf->size)
1945 p2 = buf->data;
1946
Willy Tarreau638cd022010-01-03 07:42:04 +01001947 bytes = p2 - buf->lr;
1948 if (bytes < 0)
1949 bytes += buf->size;
1950
1951 /* schedule this line for forwarding */
1952 msg->sov += bytes;
1953 if (msg->sov >= buf->size)
1954 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001955
Willy Tarreau638cd022010-01-03 07:42:04 +01001956 if (p1 == buf->lr) {
1957 /* LF/CRLF at beginning of line => end of trailers at p2.
1958 * Everything was scheduled for forwarding, there's nothing
1959 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001960 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001961 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001962 msg->msg_state = HTTP_MSG_DONE;
1963 return 1;
1964 }
1965 /* OK, next line then */
1966 buf->lr = p2;
1967 }
1968}
1969
1970/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1971 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
1972 * ->som, buf->lr in order to include this part into the next forwarding phase.
1973 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1974 * not enough data are available, the function does not change anything and
1975 * returns zero. If a parse error is encountered, the function returns < 0 and
1976 * does not change anything. Note: this function is designed to parse wrapped
1977 * CRLF at the end of the buffer.
1978 */
1979int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1980{
1981 char *ptr;
1982 int bytes;
1983
1984 /* NB: we'll check data availabilty at the end. It's not a
1985 * problem because whatever we match first will be checked
1986 * against the correct length.
1987 */
1988 bytes = 1;
1989 ptr = buf->lr;
1990 if (*ptr == '\r') {
1991 bytes++;
1992 ptr++;
1993 if (ptr >= buf->data + buf->size)
1994 ptr = buf->data;
1995 }
1996
1997 if (buf->l < bytes)
1998 return 0;
1999
2000 if (*ptr != '\n')
2001 return -1;
2002
2003 ptr++;
2004 if (ptr >= buf->data + buf->size)
2005 ptr = buf->data;
2006 buf->lr = ptr;
2007 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2008 msg->sov = ptr - buf->data;
2009 msg->som = msg->sov - bytes;
2010 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2011 return 1;
2012}
Willy Tarreau5b154472009-12-21 20:11:07 +01002013
Willy Tarreau83e3af02009-12-28 17:39:57 +01002014void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2015{
2016 char *end = buf->data + buf->size;
2017 int off = buf->data + buf->size - buf->w;
2018
2019 /* two possible cases :
2020 * - the buffer is in one contiguous block, we move it in-place
2021 * - the buffer is in two blocks, we move it via the trash
2022 */
2023 if (buf->l) {
2024 int block1 = buf->l;
2025 int block2 = 0;
2026 if (buf->r <= buf->w) {
2027 /* non-contiguous block */
2028 block1 = buf->data + buf->size - buf->w;
2029 block2 = buf->r - buf->data;
2030 }
2031 if (block2)
2032 memcpy(trash, buf->data, block2);
2033 memmove(buf->data, buf->w, block1);
2034 if (block2)
2035 memcpy(buf->data + block1, trash, block2);
2036 }
2037
2038 /* adjust all known pointers */
2039 buf->w = buf->data;
2040 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2041 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2042 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2043 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2044
2045 /* adjust relative pointers */
2046 msg->som = 0;
2047 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2048 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2049 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2050
2051 msg->sl.rq.u += off; if (msg->sl.rq.u >= buf->size) msg->sl.rq.u -= buf->size;
2052 msg->sl.rq.v += off; if (msg->sl.rq.v >= buf->size) msg->sl.rq.v -= buf->size;
2053
2054 if (msg->err_pos >= 0) {
2055 msg->err_pos += off;
2056 if (msg->err_pos >= buf->size)
2057 msg->err_pos -= buf->size;
2058 }
2059
2060 buf->flags &= ~BF_FULL;
2061 if (buf->l >= buffer_max_len(buf))
2062 buf->flags |= BF_FULL;
2063}
2064
Willy Tarreaud787e662009-07-07 10:14:51 +02002065/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2066 * processing can continue on next analysers, or zero if it either needs more
2067 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2068 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2069 * when it has nothing left to do, and may remove any analyser when it wants to
2070 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002071 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002072int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002073{
Willy Tarreau59234e92008-11-30 23:51:27 +01002074 /*
2075 * We will parse the partial (or complete) lines.
2076 * We will check the request syntax, and also join multi-line
2077 * headers. An index of all the lines will be elaborated while
2078 * parsing.
2079 *
2080 * For the parsing, we use a 28 states FSM.
2081 *
2082 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002083 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002084 * req->data + msg->eoh = end of processed headers / start of current one
2085 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002086 * req->lr = first non-visited byte
2087 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002088 *
2089 * At end of parsing, we may perform a capture of the error (if any), and
2090 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2091 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2092 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002093 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002094
Willy Tarreau59234e92008-11-30 23:51:27 +01002095 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002096 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002097 struct http_txn *txn = &s->txn;
2098 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002099 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002100
Willy Tarreau6bf17362009-02-24 10:48:35 +01002101 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2102 now_ms, __FUNCTION__,
2103 s,
2104 req,
2105 req->rex, req->wex,
2106 req->flags,
2107 req->l,
2108 req->analysers);
2109
Willy Tarreau52a0c602009-08-16 22:45:38 +02002110 /* we're speaking HTTP here, so let's speak HTTP to the client */
2111 s->srv_error = http_return_srv_error;
2112
Willy Tarreau83e3af02009-12-28 17:39:57 +01002113 /* There's a protected area at the end of the buffer for rewriting
2114 * purposes. We don't want to start to parse the request if the
2115 * protected area is affected, because we may have to move processed
2116 * data later, which is much more complicated.
2117 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002118 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
2119 if (unlikely((req->flags & BF_FULL) ||
2120 req->r < req->lr ||
2121 req->r > req->data + req->size - global.tune.maxrewrite)) {
2122 if (req->send_max) {
2123 /* some data has still not left the buffer, wake us once that's done */
2124 buffer_dont_connect(req);
2125 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2126 return 0;
2127 }
2128 if (req->l <= req->size - global.tune.maxrewrite)
2129 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002130 }
2131
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002132 if (likely(req->lr < req->r))
2133 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002134 }
2135
Willy Tarreau59234e92008-11-30 23:51:27 +01002136 /* 1: we might have to print this header in debug mode */
2137 if (unlikely((global.mode & MODE_DEBUG) &&
2138 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002139 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002140 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002141
Willy Tarreau59234e92008-11-30 23:51:27 +01002142 sol = req->data + msg->som;
2143 eol = sol + msg->sl.rq.l;
2144 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002145
Willy Tarreau59234e92008-11-30 23:51:27 +01002146 sol += hdr_idx_first_pos(&txn->hdr_idx);
2147 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002148
Willy Tarreau59234e92008-11-30 23:51:27 +01002149 while (cur_idx) {
2150 eol = sol + txn->hdr_idx.v[cur_idx].len;
2151 debug_hdr("clihdr", s, sol, eol);
2152 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2153 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002154 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002155 }
2156
Willy Tarreau58f10d72006-12-04 02:26:12 +01002157
Willy Tarreau59234e92008-11-30 23:51:27 +01002158 /*
2159 * Now we quickly check if we have found a full valid request.
2160 * If not so, we check the FD and buffer states before leaving.
2161 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002162 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreau59234e92008-11-30 23:51:27 +01002163 * requests are checked first.
2164 *
2165 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002166
Willy Tarreau655dce92009-11-08 13:10:58 +01002167 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002168 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002169 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002170 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002171 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
2172 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002173
Willy Tarreau59234e92008-11-30 23:51:27 +01002174 /* 1: Since we are in header mode, if there's no space
2175 * left for headers, we won't be able to free more
2176 * later, so the session will never terminate. We
2177 * must terminate it now.
2178 */
2179 if (unlikely(req->flags & BF_FULL)) {
2180 /* FIXME: check if URI is set and return Status
2181 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002182 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002183 goto return_bad_req;
2184 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002185
Willy Tarreau59234e92008-11-30 23:51:27 +01002186 /* 2: have we encountered a read error ? */
2187 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002188 if (txn->flags & TX_NOT_FIRST)
2189 goto failed_keep_alive;
2190
Willy Tarreau59234e92008-11-30 23:51:27 +01002191 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002192 if (msg->err_pos >= 0)
2193 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002194 msg->msg_state = HTTP_MSG_ERROR;
2195 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002196
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002197 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002198 if (s->listener->counters)
2199 s->listener->counters->failed_req++;
2200
Willy Tarreau59234e92008-11-30 23:51:27 +01002201 if (!(s->flags & SN_ERR_MASK))
2202 s->flags |= SN_ERR_CLICL;
2203 if (!(s->flags & SN_FINST_MASK))
2204 s->flags |= SN_FINST_R;
2205 return 0;
2206 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002207
Willy Tarreau59234e92008-11-30 23:51:27 +01002208 /* 3: has the read timeout expired ? */
2209 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002210 if (txn->flags & TX_NOT_FIRST)
2211 goto failed_keep_alive;
2212
Willy Tarreau59234e92008-11-30 23:51:27 +01002213 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002214 if (msg->err_pos >= 0)
2215 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002216 txn->status = 408;
2217 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2218 msg->msg_state = HTTP_MSG_ERROR;
2219 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002220
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002221 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002222 if (s->listener->counters)
2223 s->listener->counters->failed_req++;
2224
Willy Tarreau59234e92008-11-30 23:51:27 +01002225 if (!(s->flags & SN_ERR_MASK))
2226 s->flags |= SN_ERR_CLITO;
2227 if (!(s->flags & SN_FINST_MASK))
2228 s->flags |= SN_FINST_R;
2229 return 0;
2230 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002231
Willy Tarreau59234e92008-11-30 23:51:27 +01002232 /* 4: have we encountered a close ? */
2233 else if (req->flags & BF_SHUTR) {
Willy Tarreaub608feb2010-01-02 22:47:18 +01002234 if (txn->flags & TX_NOT_FIRST)
2235 goto failed_keep_alive;
2236
Willy Tarreau4076a152009-04-02 15:18:36 +02002237 if (msg->err_pos >= 0)
2238 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002239 txn->status = 400;
2240 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2241 msg->msg_state = HTTP_MSG_ERROR;
2242 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002243
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002244 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002245 if (s->listener->counters)
2246 s->listener->counters->failed_req++;
2247
Willy Tarreau59234e92008-11-30 23:51:27 +01002248 if (!(s->flags & SN_ERR_MASK))
2249 s->flags |= SN_ERR_CLICL;
2250 if (!(s->flags & SN_FINST_MASK))
2251 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002252 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002253 }
2254
Willy Tarreau520d95e2009-09-19 21:04:57 +02002255 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002256 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau5e8949c2010-01-03 14:38:03 +01002257 req->flags &= ~BF_DONT_READ;
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002258
Willy Tarreau59234e92008-11-30 23:51:27 +01002259 /* just set the request timeout once at the beginning of the request */
2260 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02002261 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002262
Willy Tarreau59234e92008-11-30 23:51:27 +01002263 /* we're not ready yet */
2264 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002265
2266 failed_keep_alive:
2267 /* Here we process low-level errors for keep-alive requests. In
2268 * short, if the request is not the first one and it experiences
2269 * a timeout, read error or shutdown, we just silently close so
2270 * that the client can try again.
2271 */
2272 txn->status = 0;
2273 msg->msg_state = HTTP_MSG_RQBEFORE;
2274 req->analysers = 0;
2275 s->logs.logwait = 0;
2276 stream_int_cond_close(req->prod, NULL);
2277 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002278 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002279
Willy Tarreaud787e662009-07-07 10:14:51 +02002280 /* OK now we have a complete HTTP request with indexed headers. Let's
2281 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002282 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2283 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2284 * points to the CRLF of the request line. req->lr points to the first
2285 * byte after the last LF. msg->col and msg->sov point to the first
2286 * byte of data. msg->eol cannot be trusted because it may have been
2287 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002288 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002289
Willy Tarreaud787e662009-07-07 10:14:51 +02002290 /* Maybe we found in invalid header name while we were configured not
2291 * to block on that, so we have to capture it now.
2292 */
2293 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002294 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2295
Willy Tarreau59234e92008-11-30 23:51:27 +01002296 /* ensure we keep this pointer to the beginning of the message */
2297 msg->sol = req->data + msg->som;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002298
Willy Tarreau59234e92008-11-30 23:51:27 +01002299 /*
2300 * 1: identify the method
2301 */
2302 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
2303
2304 /* we can make use of server redirect on GET and HEAD */
2305 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2306 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002307
Willy Tarreau59234e92008-11-30 23:51:27 +01002308 /*
2309 * 2: check if the URI matches the monitor_uri.
2310 * We have to do this for every request which gets in, because
2311 * the monitor-uri is defined by the frontend.
2312 */
2313 if (unlikely((s->fe->monitor_uri_len != 0) &&
2314 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
2315 !memcmp(&req->data[msg->sl.rq.u],
2316 s->fe->monitor_uri,
2317 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002318 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002319 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002320 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002321 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002322
Willy Tarreau59234e92008-11-30 23:51:27 +01002323 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002324
Willy Tarreau59234e92008-11-30 23:51:27 +01002325 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002326 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2327 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002328
Willy Tarreau59234e92008-11-30 23:51:27 +01002329 ret = acl_pass(ret);
2330 if (cond->pol == ACL_COND_UNLESS)
2331 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002332
Willy Tarreau59234e92008-11-30 23:51:27 +01002333 if (ret) {
2334 /* we fail this request, let's return 503 service unavail */
2335 txn->status = 503;
2336 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2337 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002338 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002339 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002340
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 /* nothing to fail, let's reply normaly */
2342 txn->status = 200;
2343 stream_int_retnclose(req->prod, &http_200_chunk);
2344 goto return_prx_cond;
2345 }
2346
2347 /*
2348 * 3: Maybe we have to copy the original REQURI for the logs ?
2349 * Note: we cannot log anymore if the request has been
2350 * classified as invalid.
2351 */
2352 if (unlikely(s->logs.logwait & LW_REQ)) {
2353 /* we have a complete HTTP request that we must log */
2354 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2355 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002356
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 if (urilen >= REQURI_LEN)
2358 urilen = REQURI_LEN - 1;
2359 memcpy(txn->uri, &req->data[msg->som], urilen);
2360 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002361
Willy Tarreau59234e92008-11-30 23:51:27 +01002362 if (!(s->logs.logwait &= ~LW_REQ))
2363 s->do_log(s);
2364 } else {
2365 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002366 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002367 }
Willy Tarreau06619262006-12-17 08:37:22 +01002368
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002370 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2371 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002372
Willy Tarreau5b154472009-12-21 20:11:07 +01002373 /* ... and check if the request is HTTP/1.1 or above */
2374 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002375 ((req->data[msg->sl.rq.v + 5] > '1') ||
2376 ((req->data[msg->sl.rq.v + 5] == '1') &&
2377 (req->data[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002378 txn->flags |= TX_REQ_VER_11;
2379
2380 /* "connection" has not been parsed yet */
2381 txn->flags &= ~TX_CON_HDR_PARS;
2382
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002383 /* transfer length unknown*/
2384 txn->flags &= ~TX_REQ_XFER_LEN;
2385
Willy Tarreau59234e92008-11-30 23:51:27 +01002386 /* 5: we may need to capture headers */
2387 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
2388 capture_headers(req->data + msg->som, &txn->hdr_idx,
2389 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002390
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002391 /* 6: determine the transfer-length.
2392 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2393 * the presence of a message-body in a REQUEST and its transfer length
2394 * must be determined that way (in order of precedence) :
2395 * 1. The presence of a message-body in a request is signaled by the
2396 * inclusion of a Content-Length or Transfer-Encoding header field
2397 * in the request's header fields. When a request message contains
2398 * both a message-body of non-zero length and a method that does
2399 * not define any semantics for that request message-body, then an
2400 * origin server SHOULD either ignore the message-body or respond
2401 * with an appropriate error message (e.g., 413). A proxy or
2402 * gateway, when presented the same request, SHOULD either forward
2403 * the request inbound with the message- body or ignore the
2404 * message-body when determining a response.
2405 *
2406 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2407 * and the "chunked" transfer-coding (Section 6.2) is used, the
2408 * transfer-length is defined by the use of this transfer-coding.
2409 * If a Transfer-Encoding header field is present and the "chunked"
2410 * transfer-coding is not present, the transfer-length is defined
2411 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002412 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002413 * 3. If a Content-Length header field is present, its decimal value in
2414 * OCTETs represents both the entity-length and the transfer-length.
2415 * If a message is received with both a Transfer-Encoding header
2416 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002417 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002418 * 4. By the server closing the connection. (Closing the connection
2419 * cannot be used to indicate the end of a request body, since that
2420 * would leave no possibility for the server to send back a response.)
2421 *
2422 * Whenever a transfer-coding is applied to a message-body, the set of
2423 * transfer-codings MUST include "chunked", unless the message indicates
2424 * it is terminated by closing the connection. When the "chunked"
2425 * transfer-coding is used, it MUST be the last transfer-coding applied
2426 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002427 */
2428
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002429 /* CONNECT sets a tunnel and ignores everything else */
2430 if (txn->meth == HTTP_METH_CONNECT)
2431 goto skip_xfer_len;
2432
2433 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002434 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002435 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002436 while ((txn->flags & TX_REQ_VER_11) &&
2437 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002438 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2439 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2440 else if (txn->flags & TX_REQ_TE_CHNK) {
2441 /* bad transfer-encoding (chunked followed by something else) */
2442 use_close_only = 1;
2443 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2444 break;
2445 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002446 }
2447
Willy Tarreau32b47f42009-10-18 20:55:02 +02002448 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002449 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002450 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2451 signed long long cl;
2452
2453 if (!ctx.vlen)
2454 goto return_bad_req;
2455
2456 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2457 goto return_bad_req; /* parse failure */
2458
2459 if (cl < 0)
2460 goto return_bad_req;
2461
2462 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2463 goto return_bad_req; /* already specified, was different */
2464
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002465 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002466 msg->hdr_content_len = cl;
2467 }
2468
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002469 /* bodyless requests have a known length */
2470 if (!use_close_only)
2471 txn->flags |= TX_REQ_XFER_LEN;
2472
2473 skip_xfer_len:
Willy Tarreaud787e662009-07-07 10:14:51 +02002474 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002475 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002476 req->analyse_exp = TICK_ETERNITY;
2477 return 1;
2478
2479 return_bad_req:
2480 /* We centralize bad requests processing here */
2481 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2482 /* we detected a parsing error. We want to archive this request
2483 * in the dedicated proxy area for later troubleshooting.
2484 */
2485 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2486 }
2487
2488 txn->req.msg_state = HTTP_MSG_ERROR;
2489 txn->status = 400;
2490 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002491
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002492 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002493 if (s->listener->counters)
2494 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002495
2496 return_prx_cond:
2497 if (!(s->flags & SN_ERR_MASK))
2498 s->flags |= SN_ERR_PRXCOND;
2499 if (!(s->flags & SN_FINST_MASK))
2500 s->flags |= SN_FINST_R;
2501
2502 req->analysers = 0;
2503 req->analyse_exp = TICK_ETERNITY;
2504 return 0;
2505}
2506
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002507/* This stream analyser runs all HTTP request processing which is common to
2508 * frontends and backends, which means blocking ACLs, filters, connection-close,
2509 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002510 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002511 * either needs more data or wants to immediately abort the request (eg: deny,
2512 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002513 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002514int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002515{
Willy Tarreaud787e662009-07-07 10:14:51 +02002516 struct http_txn *txn = &s->txn;
2517 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002518 struct acl_cond *cond;
2519 struct redirect_rule *rule;
2520 int cur_idx;
Willy Tarreaud787e662009-07-07 10:14:51 +02002521
Willy Tarreau655dce92009-11-08 13:10:58 +01002522 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002523 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002524 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002525 return 0;
2526 }
2527
Willy Tarreau3a816292009-07-07 10:55:49 +02002528 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002529 req->analyse_exp = TICK_ETERNITY;
2530
2531 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2532 now_ms, __FUNCTION__,
2533 s,
2534 req,
2535 req->rex, req->wex,
2536 req->flags,
2537 req->l,
2538 req->analysers);
2539
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002540 /* first check whether we have some ACLs set to block this request */
2541 list_for_each_entry(cond, &px->block_cond, list) {
2542 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002543
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002544 ret = acl_pass(ret);
2545 if (cond->pol == ACL_COND_UNLESS)
2546 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002547
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002548 if (ret) {
2549 txn->status = 403;
2550 /* let's log the request time */
2551 s->logs.tv_request = now;
2552 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2553 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002554 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002555 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002556
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002557 /* try headers filters */
2558 if (px->req_exp != NULL) {
2559 if (apply_filters_to_request(s, req, px->req_exp) < 0)
2560 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002561
Willy Tarreau59234e92008-11-30 23:51:27 +01002562 /* has the request been denied ? */
2563 if (txn->flags & TX_CLDENY) {
2564 /* no need to go further */
2565 txn->status = 403;
2566 /* let's log the request time */
2567 s->logs.tv_request = now;
2568 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2569 goto return_prx_cond;
2570 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002571
2572 /* When a connection is tarpitted, we use the tarpit timeout,
2573 * which may be the same as the connect timeout if unspecified.
2574 * If unset, then set it to zero because we really want it to
2575 * eventually expire. We build the tarpit as an analyser.
2576 */
2577 if (txn->flags & TX_CLTARPIT) {
2578 buffer_erase(s->req);
2579 /* wipe the request out so that we can drop the connection early
2580 * if the client closes first.
2581 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002582 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002583 req->analysers = 0; /* remove switching rules etc... */
2584 req->analysers |= AN_REQ_HTTP_TARPIT;
2585 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2586 if (!req->analyse_exp)
2587 req->analyse_exp = tick_add(now_ms, 0);
2588 return 1;
2589 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002590 }
Willy Tarreau06619262006-12-17 08:37:22 +01002591
Willy Tarreau5b154472009-12-21 20:11:07 +01002592 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2593 * only change if both the request and the config reference something else.
Willy Tarreau42736642009-10-18 21:04:35 +02002594 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002595
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002596 if ((txn->meth != HTTP_METH_CONNECT) &&
Willy Tarreaub608feb2010-01-02 22:47:18 +01002597 ((s->fe->options|s->be->options) & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01002598 int tmp = TX_CON_WANT_TUN;
2599 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2600 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002601 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2602 tmp = TX_CON_WANT_SCL;
Willy Tarreau5b154472009-12-21 20:11:07 +01002603 if ((s->fe->options|s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))
2604 tmp = TX_CON_WANT_CLO;
2605
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002606 if (!(txn->flags & TX_REQ_XFER_LEN))
2607 tmp = TX_CON_WANT_CLO;
2608
Willy Tarreau5b154472009-12-21 20:11:07 +01002609 if (!(txn->flags & TX_CON_HDR_PARS))
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002610 http_req_parse_connection_header(txn);
Willy Tarreau5b154472009-12-21 20:11:07 +01002611
2612 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2613 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2614 }
2615
2616 /* We're really certain of the connection mode (tunnel, close, keep-alive)
2617 * once we know the backend, because the tunnel mode can be implied by the
2618 * lack of any close/keepalive options in both the FE and the BE. Since
2619 * this information can evolve with time, we proceed by trying to make the
2620 * header status match the desired status. For this, we'll have to adjust
2621 * the "Connection" header. The test for persistent connections has already
2622 * been performed, so we only enter here if there is a risk the connection
2623 * is considered as persistent and we want it to be closed on the server
2624 * side. It would be nice if we could enter this place only when a
2625 * Connection header exists. Note that a CONNECT method will not enter
2626 * here.
2627 */
2628 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002629 char *cur_ptr, *cur_end, *cur_next;
2630 int old_idx, delta, val;
Willy Tarreau5b154472009-12-21 20:11:07 +01002631 int must_delete;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002632 struct hdr_idx_elem *cur_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002633
Willy Tarreau5b154472009-12-21 20:11:07 +01002634 must_delete = !(txn->flags & TX_REQ_VER_11);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002635 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002636
Willy Tarreau5b154472009-12-21 20:11:07 +01002637 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002638 cur_hdr = &txn->hdr_idx.v[cur_idx];
2639 cur_ptr = cur_next;
2640 cur_end = cur_ptr + cur_hdr->len;
2641 cur_next = cur_end + cur_hdr->cr + 1;
Willy Tarreau59234e92008-11-30 23:51:27 +01002642
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002643 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
Willy Tarreau5b154472009-12-21 20:11:07 +01002644 if (!val)
2645 continue;
2646
2647 /* 3 possibilities :
2648 * - we have already set "Connection: close" or we're in
2649 * HTTP/1.0, so we remove this line.
2650 * - we have not yet set "Connection: close", but this line
2651 * indicates close. We leave it untouched and set the flag.
2652 * - we have not yet set "Connection: close", and this line
2653 * indicates non-close. We replace it and set the flag.
2654 */
2655 if (must_delete) {
2656 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
2657 http_msg_move_end(&txn->req, delta);
2658 cur_next += delta;
2659 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
2660 txn->hdr_idx.used--;
2661 cur_hdr->len = 0;
2662 txn->flags |= TX_REQ_CONN_CLO;
2663 } else {
2664 if (cur_end - cur_ptr - val != 5 ||
2665 strncasecmp(cur_ptr + val, "close", 5) != 0) {
2666 delta = buffer_replace2(req, cur_ptr + val, cur_end,
2667 "close", 5);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002668 cur_next += delta;
Willy Tarreau5b154472009-12-21 20:11:07 +01002669 cur_hdr->len += delta;
2670 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002671 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002672 txn->flags |= TX_REQ_CONN_CLO;
2673 must_delete = 1;
Willy Tarreau06619262006-12-17 08:37:22 +01002674 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002675 } /* for loop */
2676 } /* if must close keep-alive */
Willy Tarreau78599912009-10-17 20:12:21 +02002677
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002678 /* add request headers from the rule sets in the same order */
2679 for (cur_idx = 0; cur_idx < px->nb_reqadd; cur_idx++) {
2680 if (unlikely(http_header_add_tail(req,
2681 &txn->req,
2682 &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002683 px->req_add[cur_idx]) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002684 goto return_bad_req;
2685 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002686
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002687 /* check if stats URI was requested, and if an auth is needed */
2688 if (px->uri_auth != NULL &&
2689 (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) {
2690 /* we have to check the URI and auth for this request.
2691 * FIXME!!! that one is rather dangerous, we want to
2692 * make it follow standard rules (eg: clear req->analysers).
2693 */
2694 if (stats_check_uri_auth(s, px)) {
2695 req->analysers = 0;
2696 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002697 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002698 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002699
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002700 /* check whether we have some ACLs set to redirect this request */
2701 list_for_each_entry(rule, &px->redirect_rules, list) {
2702 int ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreau06b917c2009-07-06 16:34:52 +02002703
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002704 ret = acl_pass(ret);
2705 if (rule->cond->pol == ACL_COND_UNLESS)
2706 ret = !ret;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002707
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002708 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002709 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002710 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002711
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002712 /* build redirect message */
2713 switch(rule->code) {
2714 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002715 msg_fmt = HTTP_303;
2716 break;
2717 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002718 msg_fmt = HTTP_301;
2719 break;
2720 case 302:
2721 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002722 msg_fmt = HTTP_302;
2723 break;
2724 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002725
Willy Tarreau3bb9c232010-01-03 12:24:37 +01002726 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002727 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002728
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002729 switch(rule->type) {
2730 case REDIRECT_TYPE_PREFIX: {
2731 const char *path;
2732 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002733
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002734 path = http_get_path(txn);
2735 /* build message using path */
2736 if (path) {
Willy Tarreaua95a1f42010-01-03 13:04:35 +01002737 pathlen = txn->req.sl.rq.u_l + (txn->req.sol-txn->req.som+txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002738 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2739 int qs = 0;
2740 while (qs < pathlen) {
2741 if (path[qs] == '?') {
2742 pathlen = qs;
2743 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002744 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002745 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002746 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002747 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002748 } else {
2749 path = "/";
2750 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002751 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002752
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002753 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002754 goto return_bad_req;
2755
2756 /* add prefix. Note that if prefix == "/", we don't want to
2757 * add anything, otherwise it makes it hard for the user to
2758 * configure a self-redirection.
2759 */
2760 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02002761 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2762 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002763 }
2764
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002765 /* add path */
2766 memcpy(rdr.str + rdr.len, path, pathlen);
2767 rdr.len += pathlen;
2768 break;
2769 }
2770 case REDIRECT_TYPE_LOCATION:
2771 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002772 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002773 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002774
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002775 /* add location */
2776 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
2777 rdr.len += rule->rdr_len;
2778 break;
2779 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002780
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002781 if (rule->cookie_len) {
2782 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
2783 rdr.len += 14;
2784 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
2785 rdr.len += rule->cookie_len;
2786 memcpy(rdr.str + rdr.len, "\r\n", 2);
2787 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02002788 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02002789
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002790 /* add end of headers */
2791 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
2792 rdr.len += 4;
Willy Tarreau55ea7572007-06-17 19:56:27 +02002793
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002794 txn->status = rule->code;
2795 /* let's log the request time */
2796 s->logs.tv_request = now;
2797 stream_int_retnclose(req->prod, &rdr);
2798 goto return_prx_cond;
2799 }
2800 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002801
Willy Tarreaud98cf932009-12-27 22:54:55 +01002802 /* We can shut read side if we know how we won't transfer any more data && !abort_on_close */
2803 if ((txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau5b154472009-12-21 20:11:07 +01002804 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
Willy Tarreaud98cf932009-12-27 22:54:55 +01002805 (req->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)))
Willy Tarreau349a0f62009-10-18 21:17:42 +02002806 req->flags |= BF_DONT_READ;
Willy Tarreau5e8949c2010-01-03 14:38:03 +01002807 else
2808 req->flags &= ~BF_DONT_READ;
Willy Tarreauf1ba4b32009-10-17 14:37:52 +02002809
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002810 /* that's OK for us now, let's move on to next analysers */
2811 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002812
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002813 return_bad_req:
2814 /* We centralize bad requests processing here */
2815 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2816 /* we detected a parsing error. We want to archive this request
2817 * in the dedicated proxy area for later troubleshooting.
2818 */
2819 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2820 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002821
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002822 txn->req.msg_state = HTTP_MSG_ERROR;
2823 txn->status = 400;
2824 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002825
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002826 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002827 if (s->listener->counters)
2828 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002829
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002830 return_prx_cond:
2831 if (!(s->flags & SN_ERR_MASK))
2832 s->flags |= SN_ERR_PRXCOND;
2833 if (!(s->flags & SN_FINST_MASK))
2834 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002835
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002836 req->analysers = 0;
2837 req->analyse_exp = TICK_ETERNITY;
2838 return 0;
2839}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002840
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002841/* This function performs all the processing enabled for the current request.
2842 * It returns 1 if the processing can continue on next analysers, or zero if it
2843 * needs more data, encounters an error, or wants to immediately abort the
2844 * request. It relies on buffers flags, and updates s->req->analysers.
2845 */
2846int http_process_request(struct session *s, struct buffer *req, int an_bit)
2847{
2848 struct http_txn *txn = &s->txn;
2849 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002850
Willy Tarreau655dce92009-11-08 13:10:58 +01002851 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002852 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002853 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002854 return 0;
2855 }
2856
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002857 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2858 now_ms, __FUNCTION__,
2859 s,
2860 req,
2861 req->rex, req->wex,
2862 req->flags,
2863 req->l,
2864 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002865
Willy Tarreau59234e92008-11-30 23:51:27 +01002866 /*
2867 * Right now, we know that we have processed the entire headers
2868 * and that unwanted requests have been filtered out. We can do
2869 * whatever we want with the remaining request. Also, now we
2870 * may have separate values for ->fe, ->be.
2871 */
Willy Tarreau06619262006-12-17 08:37:22 +01002872
Willy Tarreau59234e92008-11-30 23:51:27 +01002873 /*
2874 * If HTTP PROXY is set we simply get remote server address
2875 * parsing incoming request.
2876 */
2877 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
2878 url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
2879 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002880
Willy Tarreau59234e92008-11-30 23:51:27 +01002881 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002882 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002883 * Note that doing so might move headers in the request, but
2884 * the fields will stay coherent and the URI will not move.
2885 * This should only be performed in the backend.
2886 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02002887 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002888 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
2889 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002890
Willy Tarreau59234e92008-11-30 23:51:27 +01002891 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002892 * 8: the appsession cookie was looked up very early in 1.2,
2893 * so let's do the same now.
2894 */
2895
2896 /* It needs to look into the URI */
2897 if ((s->sessid == NULL) && s->be->appsession_name) {
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01002898 get_srv_from_appsession(s, &req->data[msg->sl.rq.u], msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01002899 }
2900
2901 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002902 * 9: add X-Forwarded-For if either the frontend or the backend
2903 * asks for it.
2904 */
2905 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
2906 if (s->cli_addr.ss_family == AF_INET) {
2907 /* Add an X-Forwarded-For header unless the source IP is
2908 * in the 'except' network range.
2909 */
2910 if ((!s->fe->except_mask.s_addr ||
2911 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
2912 != s->fe->except_net.s_addr) &&
2913 (!s->be->except_mask.s_addr ||
2914 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
2915 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002916 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002917 unsigned char *pn;
2918 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002919
2920 /* Note: we rely on the backend to get the header name to be used for
2921 * x-forwarded-for, because the header is really meant for the backends.
2922 * However, if the backend did not specify any option, we have to rely
2923 * on the frontend's header name.
2924 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002925 if (s->be->fwdfor_hdr_len) {
2926 len = s->be->fwdfor_hdr_len;
2927 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002928 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01002929 len = s->fe->fwdfor_hdr_len;
2930 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002931 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002932 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002933
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01002934 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002935 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01002936 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002937 }
2938 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002939 else if (s->cli_addr.ss_family == AF_INET6) {
2940 /* FIXME: for the sake of completeness, we should also support
2941 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002942 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002943 int len;
2944 char pn[INET6_ADDRSTRLEN];
2945 inet_ntop(AF_INET6,
2946 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
2947 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002948
Willy Tarreau59234e92008-11-30 23:51:27 +01002949 /* Note: we rely on the backend to get the header name to be used for
2950 * x-forwarded-for, because the header is really meant for the backends.
2951 * However, if the backend did not specify any option, we have to rely
2952 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002953 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002954 if (s->be->fwdfor_hdr_len) {
2955 len = s->be->fwdfor_hdr_len;
2956 memcpy(trash, s->be->fwdfor_hdr_name, len);
2957 } else {
2958 len = s->fe->fwdfor_hdr_len;
2959 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002960 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002961 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002962
Willy Tarreau59234e92008-11-30 23:51:27 +01002963 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01002964 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01002965 goto return_bad_req;
2966 }
2967 }
2968
2969 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002970 * 10: add X-Original-To if either the frontend or the backend
2971 * asks for it.
2972 */
2973 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
2974
2975 /* FIXME: don't know if IPv6 can handle that case too. */
2976 if (s->cli_addr.ss_family == AF_INET) {
2977 /* Add an X-Original-To header unless the destination IP is
2978 * in the 'except' network range.
2979 */
2980 if (!(s->flags & SN_FRT_ADDR_SET))
2981 get_frt_addr(s);
2982
2983 if ((!s->fe->except_mask_to.s_addr ||
2984 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
2985 != s->fe->except_to.s_addr) &&
2986 (!s->be->except_mask_to.s_addr ||
2987 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
2988 != s->be->except_to.s_addr)) {
2989 int len;
2990 unsigned char *pn;
2991 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
2992
2993 /* Note: we rely on the backend to get the header name to be used for
2994 * x-original-to, because the header is really meant for the backends.
2995 * However, if the backend did not specify any option, we have to rely
2996 * on the frontend's header name.
2997 */
2998 if (s->be->orgto_hdr_len) {
2999 len = s->be->orgto_hdr_len;
3000 memcpy(trash, s->be->orgto_hdr_name, len);
3001 } else {
3002 len = s->fe->orgto_hdr_len;
3003 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003004 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003005 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3006
3007 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003008 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003009 goto return_bad_req;
3010 }
3011 }
3012 }
3013
Willy Tarreau78599912009-10-17 20:12:21 +02003014 /* 11: add "Connection: close" if needed and not yet set. */
Willy Tarreau5b154472009-12-21 20:11:07 +01003015 if (!(txn->flags & TX_REQ_CONN_CLO) && ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL)) {
Willy Tarreau78599912009-10-17 20:12:21 +02003016 if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003017 "Connection: close", 17) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003018 goto return_bad_req;
Willy Tarreau5b154472009-12-21 20:11:07 +01003019 txn->flags |= TX_REQ_CONN_CLO;
Willy Tarreau59234e92008-11-30 23:51:27 +01003020 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003021
3022 /* If we have no server assigned yet and we're balancing on url_param
3023 * with a POST request, we may be interested in checking the body for
3024 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003025 */
3026 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3027 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003028 s->be->url_param_post_limit != 0 &&
3029 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01003030 memchr(req->data + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003031 buffer_dont_connect(req);
3032 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003033 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003034
Willy Tarreaud98cf932009-12-27 22:54:55 +01003035 if (txn->flags & TX_REQ_XFER_LEN)
3036 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003037
Willy Tarreau59234e92008-11-30 23:51:27 +01003038 /*************************************************************
3039 * OK, that's finished for the headers. We have done what we *
3040 * could. Let's switch to the DATA state. *
3041 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003042 req->analyse_exp = TICK_ETERNITY;
3043 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003044
Willy Tarreau59234e92008-11-30 23:51:27 +01003045 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003046 /* OK let's go on with the BODY now */
3047 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003048
Willy Tarreau59234e92008-11-30 23:51:27 +01003049 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003050 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003051 /* we detected a parsing error. We want to archive this request
3052 * in the dedicated proxy area for later troubleshooting.
3053 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003054 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003055 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003056
Willy Tarreau59234e92008-11-30 23:51:27 +01003057 txn->req.msg_state = HTTP_MSG_ERROR;
3058 txn->status = 400;
3059 req->analysers = 0;
3060 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003061
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003062 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003063 if (s->listener->counters)
3064 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003065
Willy Tarreau59234e92008-11-30 23:51:27 +01003066 if (!(s->flags & SN_ERR_MASK))
3067 s->flags |= SN_ERR_PRXCOND;
3068 if (!(s->flags & SN_FINST_MASK))
3069 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003070 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003071}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003072
Willy Tarreau60b85b02008-11-30 23:28:40 +01003073/* This function is an analyser which processes the HTTP tarpit. It always
3074 * returns zero, at the beginning because it prevents any other processing
3075 * from occurring, and at the end because it terminates the request.
3076 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003077int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003078{
3079 struct http_txn *txn = &s->txn;
3080
3081 /* This connection is being tarpitted. The CLIENT side has
3082 * already set the connect expiration date to the right
3083 * timeout. We just have to check that the client is still
3084 * there and that the timeout has not expired.
3085 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003086 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003087 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3088 !tick_is_expired(req->analyse_exp, now_ms))
3089 return 0;
3090
3091 /* We will set the queue timer to the time spent, just for
3092 * logging purposes. We fake a 500 server error, so that the
3093 * attacker will not suspect his connection has been tarpitted.
3094 * It will not cause trouble to the logs because we can exclude
3095 * the tarpitted connections by filtering on the 'PT' status flags.
3096 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003097 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3098
3099 txn->status = 500;
3100 if (req->flags != BF_READ_ERROR)
3101 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3102
3103 req->analysers = 0;
3104 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003105
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003106 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003107 if (s->listener->counters)
3108 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003109
Willy Tarreau60b85b02008-11-30 23:28:40 +01003110 if (!(s->flags & SN_ERR_MASK))
3111 s->flags |= SN_ERR_PRXCOND;
3112 if (!(s->flags & SN_FINST_MASK))
3113 s->flags |= SN_FINST_T;
3114 return 0;
3115}
3116
Willy Tarreaud34af782008-11-30 23:36:37 +01003117/* This function is an analyser which processes the HTTP request body. It looks
3118 * for parameters to be used for the load balancing algorithm (url_param). It
3119 * must only be called after the standard HTTP request processing has occurred,
3120 * because it expects the request to be parsed. It returns zero if it needs to
3121 * read more data, or 1 once it has completed its analysis.
3122 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003123int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003124{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003125 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003126 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003127 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003128
3129 /* We have to parse the HTTP request body to find any required data.
3130 * "balance url_param check_post" should have been the only way to get
3131 * into this. We were brought here after HTTP header analysis, so all
3132 * related structures are ready.
3133 */
3134
Willy Tarreau522d6c02009-12-06 18:49:18 +01003135 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3136 goto missing_data;
3137
3138 if (msg->msg_state < HTTP_MSG_100_SENT) {
3139 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3140 * send an HTTP/1.1 100 Continue intermediate response.
3141 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003142 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003143 struct hdr_ctx ctx;
3144 ctx.idx = 0;
3145 /* Expect is allowed in 1.1, look for it */
3146 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3147 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3148 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3149 }
3150 }
3151 msg->msg_state = HTTP_MSG_100_SENT;
3152 }
3153
3154 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003155 /* we have msg->col and msg->sov which both point to the first
3156 * byte of message body. msg->som still points to the beginning
3157 * of the message. We must save the body in req->lr because it
3158 * survives buffer re-alignments.
3159 */
3160 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003161 if (txn->flags & TX_REQ_TE_CHNK)
3162 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3163 else
3164 msg->msg_state = HTTP_MSG_DATA;
3165 }
3166
3167 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003168 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003169 * set ->sov and ->lr to point to the body and switch to DATA or
3170 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003171 */
3172 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003173
Willy Tarreau115acb92009-12-26 13:56:06 +01003174 if (!ret)
3175 goto missing_data;
3176 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003177 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003178 }
3179
Willy Tarreaud98cf932009-12-27 22:54:55 +01003180 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003181 * We have the first non-header byte in msg->col, which is either the
3182 * beginning of the chunk size or of the data. The first data byte is in
3183 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3184 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003185 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003186
3187 if (msg->hdr_content_len < limit)
3188 limit = msg->hdr_content_len;
3189
Willy Tarreau7c96f672009-12-27 22:47:25 +01003190 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003191 goto http_end;
3192
3193 missing_data:
3194 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003195 if (req->flags & BF_FULL)
3196 goto return_bad_req;
3197
Willy Tarreau522d6c02009-12-06 18:49:18 +01003198 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3199 txn->status = 408;
3200 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3201 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003202 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003203
3204 /* we get here if we need to wait for more data */
3205 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003206 /* Not enough data. We'll re-use the http-request
3207 * timeout here. Ideally, we should set the timeout
3208 * relative to the accept() date. We just set the
3209 * request timeout once at the beginning of the
3210 * request.
3211 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003212 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003213 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003214 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003215 return 0;
3216 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003217
3218 http_end:
3219 /* The situation will not evolve, so let's give up on the analysis. */
3220 s->logs.tv_request = now; /* update the request timer to reflect full request */
3221 req->analysers &= ~an_bit;
3222 req->analyse_exp = TICK_ETERNITY;
3223 return 1;
3224
3225 return_bad_req: /* let's centralize all bad requests */
3226 txn->req.msg_state = HTTP_MSG_ERROR;
3227 txn->status = 400;
3228 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3229
3230 return_err_msg:
3231 req->analysers = 0;
3232 s->fe->counters.failed_req++;
3233 if (s->listener->counters)
3234 s->listener->counters->failed_req++;
3235
3236 if (!(s->flags & SN_ERR_MASK))
3237 s->flags |= SN_ERR_PRXCOND;
3238 if (!(s->flags & SN_FINST_MASK))
3239 s->flags |= SN_FINST_R;
3240 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003241}
3242
Willy Tarreaud98cf932009-12-27 22:54:55 +01003243/* This function is an analyser which forwards request body (including chunk
3244 * sizes if any). It is called as soon as we must forward, even if we forward
3245 * zero byte. The only situation where it must not be called is when we're in
3246 * tunnel mode and we want to forward till the close. It's used both to forward
3247 * remaining data and to resync after end of body. It expects the msg_state to
3248 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
3249 * read more data, or 1 once we can go on with next request or end the session.
3250 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
3251 * bytes of pending data + the headers if not already done (between som and sov).
3252 * It eventually adjusts som to match sov after the data in between have been sent.
3253 */
3254int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
3255{
3256 struct http_txn *txn = &s->txn;
3257 struct http_msg *msg = &s->txn.req;
3258
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01003259 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
3260 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
3261 /* Output closed while we were sending data. We must abort. */
3262 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003263 req->analysers &= ~an_bit;
3264 return 1;
3265 }
3266
Willy Tarreaud98cf932009-12-27 22:54:55 +01003267 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3268 return 0;
3269
3270 /* Note that we don't have to send 100-continue back because we don't
3271 * need the data to complete our job, and it's up to the server to
3272 * decide whether to return 100, 417 or anything else in return of
3273 * an "Expect: 100-continue" header.
3274 */
3275
3276 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3277 /* we have msg->col and msg->sov which both point to the first
3278 * byte of message body. msg->som still points to the beginning
3279 * of the message. We must save the body in req->lr because it
3280 * survives buffer re-alignments.
3281 */
3282 req->lr = req->data + msg->sov;
3283 if (txn->flags & TX_REQ_TE_CHNK)
3284 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3285 else {
3286 msg->msg_state = HTTP_MSG_DATA;
3287 }
3288 }
3289
Willy Tarreaud98cf932009-12-27 22:54:55 +01003290 while (1) {
Willy Tarreau638cd022010-01-03 07:42:04 +01003291 /* we may have some data pending */
3292 if (msg->hdr_content_len || msg->som != msg->sov) {
3293 int bytes = msg->sov - msg->som;
3294 if (bytes < 0) /* sov may have wrapped at the end */
3295 bytes += req->size;
3296 buffer_forward(req, bytes + msg->hdr_content_len);
3297 msg->hdr_content_len = 0; /* don't forward that again */
3298 msg->som = msg->sov;
3299 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003300
Willy Tarreaud98cf932009-12-27 22:54:55 +01003301 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
3302 /* read the chunk size and assign it to ->hdr_content_len, then
3303 * set ->sov and ->lr to point to the body and switch to DATA or
3304 * TRAILERS state.
3305 */
3306 int ret = http_parse_chunk_size(req, msg);
3307
3308 if (!ret)
3309 goto missing_data;
3310 else if (ret < 0)
3311 goto return_bad_req;
3312 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003313 }
3314 else if (msg->msg_state == HTTP_MSG_DATA) {
3315 /* must still forward */
3316 if (req->to_forward)
3317 return 0;
3318
3319 /* we're sending the last bits of request, the server's response
3320 * is expected in a short time. Most often the first read is enough
3321 * to bring all the headers, so we're preparing the response buffer
3322 * to read the response now. Note that we should probably move that
3323 * to a more appropriate place.
3324 */
Willy Tarreau5523b322009-12-29 12:05:52 +01003325 if (txn->rsp.msg_state == HTTP_MSG_RPBEFORE) {
3326 s->rep->flags &= ~BF_DONT_READ;
3327 s->rep->flags |= BF_READ_DONTWAIT;
3328 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003329
3330 /* nothing left to forward */
3331 if (txn->flags & TX_REQ_TE_CHNK)
3332 msg->msg_state = HTTP_MSG_DATA_CRLF;
3333 else {
3334 msg->msg_state = HTTP_MSG_DONE;
3335 }
3336 }
3337 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3338 /* we want the CRLF after the data */
3339 int ret;
3340
3341 if (!(req->flags & BF_OUT_EMPTY))
3342 return 0;
3343 /* The output pointer does not move anymore, next unsent data
3344 * are available at ->w. Let's save that.
3345 */
3346 req->lr = req->w;
3347 ret = http_skip_chunk_crlf(req, msg);
3348
3349 if (ret == 0)
3350 goto missing_data;
3351 else if (ret < 0)
3352 goto return_bad_req;
3353 /* we're in MSG_CHUNK_SIZE now */
3354 }
3355 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3356 int ret = http_forward_trailers(req, msg);
3357
3358 if (ret == 0)
3359 goto missing_data;
3360 else if (ret < 0)
3361 goto return_bad_req;
3362 /* we're in HTTP_MSG_DONE now */
3363 }
3364 else if (msg->msg_state == HTTP_MSG_DONE) {
Willy Tarreau5523b322009-12-29 12:05:52 +01003365 /* No need to read anymore, the request was completely parsed */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003366 req->flags |= BF_DONT_READ;
3367
Willy Tarreau5523b322009-12-29 12:05:52 +01003368 if (txn->rsp.msg_state < HTTP_MSG_DONE && txn->rsp.msg_state != HTTP_MSG_ERROR) {
3369 /* The server has not finished to respond, so we
3370 * don't want to move in order not to upset it.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003371 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003372 return 0;
3373 }
3374
3375 /* when we support keep-alive or server-close modes, we'll have
3376 * to reset the transaction here.
3377 */
Willy Tarreau5523b322009-12-29 12:05:52 +01003378
Willy Tarreaub608feb2010-01-02 22:47:18 +01003379 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3380 /* initiate a connection close to the server */
3381 req->cons->flags |= SI_FL_NOLINGER;
3382 buffer_shutw_now(req);
3383 }
3384 else if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) {
Willy Tarreau9438c712009-12-29 14:39:48 +01003385 /* Option forceclose is set, let's enforce it now
3386 * that the transfer is complete. We can safely speed
3387 * up the close because we know the server has received
3388 * everything we wanted it to receive.
3389 */
3390 req->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau82eeaf22009-12-29 12:09:05 +01003391 buffer_abort(req);
3392 }
3393
Willy Tarreau5523b322009-12-29 12:05:52 +01003394 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3395 if (req->flags & BF_OUT_EMPTY)
3396 msg->msg_state = HTTP_MSG_CLOSED;
3397 else
3398 msg->msg_state = HTTP_MSG_CLOSING;
3399 }
3400 else {
3401 /* for other modes, we let further requests pass for now */
3402 req->flags &= ~BF_DONT_READ;
3403 /* FIXME: we're still forced to do that here */
3404 s->rep->flags &= ~BF_DONT_READ;
3405 break;
3406 }
3407 }
3408 else if (msg->msg_state == HTTP_MSG_CLOSING) {
3409 /* nothing else to forward, just waiting for the buffer to be empty */
3410 if (!(req->flags & BF_OUT_EMPTY))
3411 return 0;
3412 msg->msg_state = HTTP_MSG_CLOSED;
3413 }
3414 else if (msg->msg_state == HTTP_MSG_CLOSED) {
3415 req->flags &= ~BF_DONT_READ;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003416
3417 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3418
3419 /* FIXME : this part is 1) awful, 2) tricky, 3) duplicated
3420 * ... but it works.
3421 * We need a better way to force a connection close without
3422 * any risk of propagation to the other side. We need a more
3423 * portable way of releasing a backend's and a server's
3424 * connections. We need a safer way to reinitialize buffer
3425 * flags. We also need a more accurate method for computing
3426 * per-request data.
3427 */
3428 s->req->cons->flags |= SI_FL_NOLINGER;
3429 s->req->cons->shutr(s->req->cons);
3430 s->req->cons->shutw(s->req->cons);
3431
3432 if (s->flags & SN_BE_ASSIGNED)
3433 s->be->beconn--;
3434
3435 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3436 session_process_counters(s);
3437
3438 if (s->txn.status) {
3439 int n;
3440
3441 n = s->txn.status / 100;
3442 if (n < 1 || n > 5)
3443 n = 0;
3444
3445 if (s->fe->mode == PR_MODE_HTTP)
3446 s->fe->counters.p.http.rsp[n]++;
3447
3448 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3449 (s->be->mode == PR_MODE_HTTP))
3450 s->be->counters.p.http.rsp[n]++;
3451 }
3452
3453 /* don't count other requests' data */
3454 s->logs.bytes_in -= s->req->l - s->req->send_max;
3455 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3456
3457 /* let's do a final log if we need it */
3458 if (s->logs.logwait &&
3459 !(s->flags & SN_MONITOR) &&
3460 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3461 s->do_log(s);
3462 }
3463
3464 s->logs.accept_date = date; /* user-visible date for logging */
3465 s->logs.tv_accept = now; /* corrected date for internal use */
3466 tv_zero(&s->logs.tv_request);
3467 s->logs.t_queue = -1;
3468 s->logs.t_connect = -1;
3469 s->logs.t_data = -1;
3470 s->logs.t_close = 0;
3471 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3472 s->logs.srv_queue_size = 0; /* we will get this number soon */
3473
3474 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3475 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3476
3477 if (s->pend_pos)
3478 pendconn_free(s->pend_pos);
3479
3480 if (s->srv) {
3481 if (s->flags & SN_CURR_SESS) {
3482 s->flags &= ~SN_CURR_SESS;
3483 s->srv->cur_sess--;
3484 }
3485 if (may_dequeue_tasks(s->srv, s->be))
3486 process_srv_queue(s->srv);
3487 }
3488
3489 if (unlikely(s->srv_conn))
3490 sess_change_server(s, NULL);
3491 s->srv = NULL;
3492
3493 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3494 s->req->cons->fd = -1; /* just to help with debugging */
3495 s->req->cons->err_type = SI_ET_NONE;
3496 s->req->cons->err_loc = NULL;
3497 s->req->cons->exp = TICK_ETERNITY;
3498 s->req->cons->flags = SI_FL_NONE;
3499 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE);
3500 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE|BF_WRITE_PARTIAL);
3501 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3502 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3503 s->txn.meth = 0;
3504 http_reset_txn(s);
3505 txn->flags |= TX_NOT_FIRST;
3506 if (s->be->options2 & PR_O2_INDEPSTR)
3507 s->req->cons->flags |= SI_FL_INDEP_STR;
3508
Willy Tarreauface8392010-01-03 11:37:54 +01003509 /* if the request buffer is not empty, it means we're
3510 * about to process another request, so send pending
3511 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau5e8949c2010-01-03 14:38:03 +01003512 * Also, let's not start reading a small request packet,
3513 * we may prefer to read a larger one later.
Willy Tarreauface8392010-01-03 11:37:54 +01003514 */
Willy Tarreau5e8949c2010-01-03 14:38:03 +01003515 if (s->req->l > s->req->send_max) {
Willy Tarreauface8392010-01-03 11:37:54 +01003516 s->rep->flags |= BF_EXPECT_MORE;
Willy Tarreau5e8949c2010-01-03 14:38:03 +01003517 s->req->flags |= BF_DONT_READ;
3518 }
Willy Tarreauface8392010-01-03 11:37:54 +01003519
Willy Tarreaub608feb2010-01-02 22:47:18 +01003520 /* make ->lr point to the first non-forwarded byte */
3521 s->req->lr = s->req->w + s->req->send_max;
3522 if (s->req->lr >= s->req->data + s->req->size)
3523 s->req->lr -= s->req->size;
3524 s->rep->lr = s->rep->w + s->rep->send_max;
3525 if (s->rep->lr >= s->rep->data + s->rep->size)
3526 s->rep->lr -= s->req->size;
3527
3528 s->req->analysers |= s->fe->fe_req_ana;
3529 s->rep->analysers = 0;
3530 }
3531
Willy Tarreau5523b322009-12-29 12:05:52 +01003532 /* FIXME: we're still forced to do that here */
3533 s->rep->flags &= ~BF_DONT_READ;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003534 break;
3535 }
3536 }
3537
3538 /* OK we're done with the data phase */
3539 req->analysers &= ~an_bit;
3540 return 1;
3541
3542 missing_data:
3543 /* forward the chunk size as well as any pending data */
3544 if (msg->hdr_content_len || msg->som != msg->sov) {
3545 buffer_forward(req, msg->sov - msg->som + msg->hdr_content_len);
3546 msg->hdr_content_len = 0; /* don't forward that again */
3547 msg->som = msg->sov;
3548 }
3549
3550 if (req->flags & BF_FULL)
3551 goto return_bad_req;
3552 /* the session handler will take care of timeouts and errors */
3553 return 0;
3554
3555 return_bad_req: /* let's centralize all bad requests */
3556 txn->req.msg_state = HTTP_MSG_ERROR;
3557 txn->status = 400;
3558 /* Note: we don't send any error if some data were already sent */
3559 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3560
3561 req->analysers = 0;
3562 s->fe->counters.failed_req++;
3563 if (s->listener->counters)
3564 s->listener->counters->failed_req++;
3565
3566 if (!(s->flags & SN_ERR_MASK))
3567 s->flags |= SN_ERR_PRXCOND;
3568 if (!(s->flags & SN_FINST_MASK))
3569 s->flags |= SN_FINST_R;
3570 return 0;
3571}
3572
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003573/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3574 * processing can continue on next analysers, or zero if it either needs more
3575 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3576 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3577 * when it has nothing left to do, and may remove any analyser when it wants to
3578 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003579 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003580int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003581{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003582 struct http_txn *txn = &s->txn;
3583 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003584 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003585 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003586 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003587 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003588
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003589 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 +02003590 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003591 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003592 rep,
3593 rep->rex, rep->wex,
3594 rep->flags,
3595 rep->l,
3596 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003597
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003598 /*
3599 * Now parse the partial (or complete) lines.
3600 * We will check the response syntax, and also join multi-line
3601 * headers. An index of all the lines will be elaborated while
3602 * parsing.
3603 *
3604 * For the parsing, we use a 28 states FSM.
3605 *
3606 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003607 * rep->data + msg->som = beginning of response
3608 * rep->data + msg->eoh = end of processed headers / start of current one
3609 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003610 * rep->lr = first non-visited byte
3611 * rep->r = end of data
3612 */
3613
Willy Tarreau83e3af02009-12-28 17:39:57 +01003614 /* There's a protected area at the end of the buffer for rewriting
3615 * purposes. We don't want to start to parse the request if the
3616 * protected area is affected, because we may have to move processed
3617 * data later, which is much more complicated.
3618 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003619 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3620 if (unlikely((rep->flags & BF_FULL) ||
3621 rep->r < rep->lr ||
3622 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3623 if (rep->send_max) {
3624 /* some data has still not left the buffer, wake us once that's done */
3625 buffer_dont_close(rep);
3626 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
3627 return 0;
3628 }
3629 if (rep->l <= rep->size - global.tune.maxrewrite)
3630 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003631 }
3632
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003633 if (likely(rep->lr < rep->r))
3634 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003635 }
3636
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003637 /* 1: we might have to print this header in debug mode */
3638 if (unlikely((global.mode & MODE_DEBUG) &&
3639 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003640 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003641 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003642
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003643 sol = rep->data + msg->som;
3644 eol = sol + msg->sl.rq.l;
3645 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003646
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003647 sol += hdr_idx_first_pos(&txn->hdr_idx);
3648 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003649
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003650 while (cur_idx) {
3651 eol = sol + txn->hdr_idx.v[cur_idx].len;
3652 debug_hdr("srvhdr", s, sol, eol);
3653 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3654 cur_idx = txn->hdr_idx.v[cur_idx].next;
3655 }
3656 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003657
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003658 /*
3659 * Now we quickly check if we have found a full valid response.
3660 * If not so, we check the FD and buffer states before leaving.
3661 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01003662 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003663 * responses are checked first.
3664 *
3665 * Depending on whether the client is still there or not, we
3666 * may send an error response back or not. Note that normally
3667 * we should only check for HTTP status there, and check I/O
3668 * errors somewhere else.
3669 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003670
Willy Tarreau655dce92009-11-08 13:10:58 +01003671 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003672 /* Invalid response */
3673 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
3674 /* we detected a parsing error. We want to archive this response
3675 * in the dedicated proxy area for later troubleshooting.
3676 */
3677 hdr_response_bad:
3678 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
3679 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3680
3681 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003682 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003683 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003684 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
3685 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003686
3687 rep->analysers = 0;
3688 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003689 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003690 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
3691
3692 if (!(s->flags & SN_ERR_MASK))
3693 s->flags |= SN_ERR_PRXCOND;
3694 if (!(s->flags & SN_FINST_MASK))
3695 s->flags |= SN_FINST_H;
3696
3697 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003698 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003699
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003700 /* too large response does not fit in buffer. */
3701 else if (rep->flags & BF_FULL) {
3702 goto hdr_response_bad;
3703 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003704
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003705 /* read error */
3706 else if (rep->flags & BF_READ_ERROR) {
3707 if (msg->err_pos >= 0)
3708 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02003709
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003710 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003711 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003712 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003713 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
3714 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003715
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003716 rep->analysers = 0;
3717 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003718 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003719 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02003720
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003721 if (!(s->flags & SN_ERR_MASK))
3722 s->flags |= SN_ERR_SRVCL;
3723 if (!(s->flags & SN_FINST_MASK))
3724 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003725 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003726 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003727
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003728 /* read timeout : return a 504 to the client. */
3729 else if (rep->flags & BF_READ_TIMEOUT) {
3730 if (msg->err_pos >= 0)
3731 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003732
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003733 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003734 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003735 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003736 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
3737 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003738
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003739 rep->analysers = 0;
3740 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003741 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003742 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02003743
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003744 if (!(s->flags & SN_ERR_MASK))
3745 s->flags |= SN_ERR_SRVTO;
3746 if (!(s->flags & SN_FINST_MASK))
3747 s->flags |= SN_FINST_H;
3748 return 0;
3749 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02003750
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003751 /* close from server */
3752 else if (rep->flags & BF_SHUTR) {
3753 if (msg->err_pos >= 0)
3754 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003755
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003756 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003757 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003758 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003759 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
3760 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003761
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003762 rep->analysers = 0;
3763 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003764 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003765 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01003766
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003767 if (!(s->flags & SN_ERR_MASK))
3768 s->flags |= SN_ERR_SRVCL;
3769 if (!(s->flags & SN_FINST_MASK))
3770 s->flags |= SN_FINST_H;
3771 return 0;
3772 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003773
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003774 /* write error to client (we don't send any message then) */
3775 else if (rep->flags & BF_WRITE_ERROR) {
3776 if (msg->err_pos >= 0)
3777 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003778
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003779 s->be->counters.failed_resp++;
3780 rep->analysers = 0;
3781
3782 if (!(s->flags & SN_ERR_MASK))
3783 s->flags |= SN_ERR_CLICL;
3784 if (!(s->flags & SN_FINST_MASK))
3785 s->flags |= SN_FINST_H;
3786
3787 /* process_session() will take care of the error */
3788 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003789 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003790
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003791 buffer_dont_close(rep);
3792 return 0;
3793 }
3794
3795 /* More interesting part now : we know that we have a complete
3796 * response which at least looks like HTTP. We have an indicator
3797 * of each header's length, so we can parse them quickly.
3798 */
3799
3800 if (unlikely(msg->err_pos >= 0))
3801 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3802
3803 /* ensure we keep this pointer to the beginning of the message */
3804 msg->sol = rep->data + msg->som;
3805
3806 /*
3807 * 1: get the status code
3808 */
3809 n = rep->data[msg->sl.st.c] - '0';
3810 if (n < 1 || n > 5)
3811 n = 0;
3812 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003813
Willy Tarreau5b154472009-12-21 20:11:07 +01003814 /* check if the response is HTTP/1.1 or above */
3815 if ((msg->sl.st.v_l == 8) &&
3816 ((rep->data[msg->som + 5] > '1') ||
3817 ((rep->data[msg->som + 5] == '1') &&
3818 (rep->data[msg->som + 7] >= '1'))))
3819 txn->flags |= TX_RES_VER_11;
3820
3821 /* "connection" has not been parsed yet */
3822 txn->flags &= ~TX_CON_HDR_PARS;
3823
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003824 /* transfer length unknown*/
3825 txn->flags &= ~TX_RES_XFER_LEN;
3826
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003827 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
3828
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003829 if (txn->status >= 100 && txn->status < 500)
3830 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
3831 else
3832 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
3833
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003834 /*
3835 * 2: check for cacheability.
3836 */
3837
3838 switch (txn->status) {
3839 case 200:
3840 case 203:
3841 case 206:
3842 case 300:
3843 case 301:
3844 case 410:
3845 /* RFC2616 @13.4:
3846 * "A response received with a status code of
3847 * 200, 203, 206, 300, 301 or 410 MAY be stored
3848 * by a cache (...) unless a cache-control
3849 * directive prohibits caching."
3850 *
3851 * RFC2616 @9.5: POST method :
3852 * "Responses to this method are not cacheable,
3853 * unless the response includes appropriate
3854 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003855 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003856 if (likely(txn->meth != HTTP_METH_POST) &&
3857 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3858 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3859 break;
3860 default:
3861 break;
3862 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003863
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003864 /*
3865 * 3: we may need to capture headers
3866 */
3867 s->logs.logwait &= ~LW_RESP;
3868 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
3869 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3870 txn->rsp.cap, s->fe->rsp_cap);
3871
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003872 /* 4: determine the transfer-length.
3873 * According to RFC2616 #4.4, amended by the HTTPbis working group,
3874 * the presence of a message-body in a RESPONSE and its transfer length
3875 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003876 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003877 * All responses to the HEAD request method MUST NOT include a
3878 * message-body, even though the presence of entity-header fields
3879 * might lead one to believe they do. All 1xx (informational), 204
3880 * (No Content), and 304 (Not Modified) responses MUST NOT include a
3881 * message-body. All other responses do include a message-body,
3882 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003883 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003884 * 1. Any response which "MUST NOT" include a message-body (such as the
3885 * 1xx, 204 and 304 responses and any response to a HEAD request) is
3886 * always terminated by the first empty line after the header fields,
3887 * regardless of the entity-header fields present in the message.
3888 *
3889 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
3890 * the "chunked" transfer-coding (Section 6.2) is used, the
3891 * transfer-length is defined by the use of this transfer-coding.
3892 * If a Transfer-Encoding header field is present and the "chunked"
3893 * transfer-coding is not present, the transfer-length is defined by
3894 * the sender closing the connection.
3895 *
3896 * 3. If a Content-Length header field is present, its decimal value in
3897 * OCTETs represents both the entity-length and the transfer-length.
3898 * If a message is received with both a Transfer-Encoding header
3899 * field and a Content-Length header field, the latter MUST be ignored.
3900 *
3901 * 4. If the message uses the media type "multipart/byteranges", and
3902 * the transfer-length is not otherwise specified, then this self-
3903 * delimiting media type defines the transfer-length. This media
3904 * type MUST NOT be used unless the sender knows that the recipient
3905 * can parse it; the presence in a request of a Range header with
3906 * multiple byte-range specifiers from a 1.1 client implies that the
3907 * client can parse multipart/byteranges responses.
3908 *
3909 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003910 */
3911
3912 /* Skip parsing if no content length is possible. The response flags
3913 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003914 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003915 * FIXME: should we parse anyway and return an error on chunked encoding ?
3916 */
3917 if (txn->meth == HTTP_METH_HEAD ||
3918 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003919 txn->status == 204 || txn->status == 304) {
3920 txn->flags |= TX_RES_XFER_LEN;
3921 goto skip_content_length;
3922 }
3923
3924 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003925 goto skip_content_length;
3926
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003927 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003928 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003929 while ((txn->flags & TX_RES_VER_11) &&
3930 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003931 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
3932 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3933 else if (txn->flags & TX_RES_TE_CHNK) {
3934 /* bad transfer-encoding (chunked followed by something else) */
3935 use_close_only = 1;
3936 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3937 break;
3938 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003939 }
3940
3941 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
3942 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003943 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003944 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
3945 signed long long cl;
3946
3947 if (!ctx.vlen)
3948 goto hdr_response_bad;
3949
3950 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
3951 goto hdr_response_bad; /* parse failure */
3952
3953 if (cl < 0)
3954 goto hdr_response_bad;
3955
3956 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
3957 goto hdr_response_bad; /* already specified, was different */
3958
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003959 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003960 msg->hdr_content_len = cl;
3961 }
3962
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003963 /* FIXME: we should also implement the multipart/byterange method.
3964 * For now on, we resort to close mode in this case (unknown length).
3965 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003966skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003967
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003968 /* end of job, return OK */
3969 rep->analysers &= ~an_bit;
3970 rep->analyse_exp = TICK_ETERNITY;
3971 return 1;
3972}
3973
3974/* This function performs all the processing enabled for the current response.
3975 * It normally returns zero, but may return 1 if it absolutely needs to be
3976 * called again after other functions. It relies on buffers flags, and updates
3977 * t->rep->analysers. It might make sense to explode it into several other
3978 * functions. It works like process_request (see indications above).
3979 */
3980int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
3981{
3982 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003983 struct http_msg *msg = &txn->rsp;
3984 struct proxy *cur_proxy;
3985 int cur_idx;
Willy Tarreau5b154472009-12-21 20:11:07 +01003986 int conn_ka = 0, conn_cl = 0;
3987 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003988 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003989
3990 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3991 now_ms, __FUNCTION__,
3992 t,
3993 rep,
3994 rep->rex, rep->wex,
3995 rep->flags,
3996 rep->l,
3997 rep->analysers);
3998
Willy Tarreau655dce92009-11-08 13:10:58 +01003999 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004000 return 0;
4001
4002 rep->analysers &= ~an_bit;
4003 rep->analyse_exp = TICK_ETERNITY;
4004
Willy Tarreau5b154472009-12-21 20:11:07 +01004005 /* Now we have to check if we need to modify the Connection header.
4006 * This is more difficult on the response than it is on the request,
4007 * because we can have two different HTTP versions and we don't know
4008 * how the client will interprete a response. For instance, let's say
4009 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4010 * HTTP/1.1 response without any header. Maybe it will bound itself to
4011 * HTTP/1.0 because it only knows about it, and will consider the lack
4012 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4013 * the lack of header as a keep-alive. Thus we will use two flags
4014 * indicating how a request MAY be understood by the client. In case
4015 * of multiple possibilities, we'll fix the header to be explicit. If
4016 * ambiguous cases such as both close and keepalive are seen, then we
4017 * will fall back to explicit close. Note that we won't take risks with
4018 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4019 */
4020
4021 if ((txn->meth != HTTP_METH_CONNECT) &&
4022 (txn->status >= 200) &&
4023 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN &&
4024 !(txn->flags & TX_CON_HDR_PARS)) {
4025 int may_keep = 0, may_close = 0; /* how it may be understood */
4026 struct hdr_ctx ctx;
4027
4028 ctx.idx = 0;
4029 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4030 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4031 conn_cl = 1;
4032 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4033 conn_ka = 1;
4034 }
4035
4036 if (conn_cl) {
4037 /* close header present */
4038 may_close = 1;
4039 if (conn_ka) /* we have both close and keep-alive */
4040 may_keep = 1;
4041 }
4042 else if (conn_ka) {
4043 /* keep-alive alone */
4044 may_keep = 1;
4045 }
4046 else {
4047 /* no close nor keep-alive header */
4048 if (txn->flags & TX_RES_VER_11)
4049 may_keep = 1;
4050 else
4051 may_close = 1;
4052
4053 if (txn->flags & TX_REQ_VER_11)
4054 may_keep = 1;
4055 else
4056 may_close = 1;
4057 }
4058
4059 /* let's update the transaction status to reflect any close.
4060 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004061 * handled. We also explicitly state that we will close in
4062 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004063 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004064 if ((may_close &&
4065 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4066 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004067 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4068
4069 /* Now we must adjust the response header :
4070 * - set "close" if may_keep and WANT_CLO
4071 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4072 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
4073 *
4074 * Until we support the server-close mode, we'll only support the set "close".
4075 */
4076 if (may_keep && (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO)
4077 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004078 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4079 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4080 must_del_close = 1;
4081 if (!(txn->flags & TX_REQ_VER_11))
4082 must_keep = 1;
4083 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004084
4085 txn->flags |= TX_CON_HDR_PARS;
4086 }
4087
4088 /* We might have to check for "Connection:" if the server
4089 * returns a connection status that is not compatible with
4090 * the client's or with the config.
4091 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004092 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004093 char *cur_ptr, *cur_end, *cur_next;
4094 int cur_idx, old_idx, delta, val;
4095 int must_delete;
4096 struct hdr_idx_elem *cur_hdr;
4097
4098 /* we just have to remove the headers if both sides are 1.0 */
4099 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004100
4101 /* same if we want to re-enable keep-alive on 1.1 */
4102 must_delete |= must_del_close;
4103
Willy Tarreau5b154472009-12-21 20:11:07 +01004104 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4105
4106 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4107 cur_hdr = &txn->hdr_idx.v[cur_idx];
4108 cur_ptr = cur_next;
4109 cur_end = cur_ptr + cur_hdr->len;
4110 cur_next = cur_end + cur_hdr->cr + 1;
4111
4112 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4113 if (!val)
4114 continue;
4115
4116 /* 3 possibilities :
4117 * - we have already set "Connection: close" or we're in
4118 * HTTP/1.0, so we remove this line.
4119 * - we have not yet set "Connection: close", but this line
4120 * indicates close. We leave it untouched and set the flag.
4121 * - we have not yet set "Connection: close", and this line
4122 * indicates non-close. We replace it and set the flag.
4123 */
4124 if (must_delete) {
4125 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4126 http_msg_move_end(&txn->rsp, delta);
4127 cur_next += delta;
4128 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4129 txn->hdr_idx.used--;
4130 cur_hdr->len = 0;
4131 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004132 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004133 } else {
4134 if (cur_end - cur_ptr - val != 5 ||
4135 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4136 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4137 "close", 5);
4138 cur_next += delta;
4139 cur_hdr->len += delta;
4140 http_msg_move_end(&txn->rsp, delta);
4141 }
4142 must_delete = 1;
4143 must_close = 0;
4144 }
4145 } /* for loop */
4146 } /* if must close keep-alive */
4147
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004148 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004149 /*
4150 * 3: we will have to evaluate the filters.
4151 * As opposed to version 1.2, now they will be evaluated in the
4152 * filters order and not in the header order. This means that
4153 * each filter has to be validated among all headers.
4154 *
4155 * Filters are tried with ->be first, then with ->fe if it is
4156 * different from ->be.
4157 */
4158
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004159 cur_proxy = t->be;
4160 while (1) {
4161 struct proxy *rule_set = cur_proxy;
4162
4163 /* try headers filters */
4164 if (rule_set->rsp_exp != NULL) {
4165 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4166 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004167 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004168 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004169 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4170 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004171 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004172 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004173 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004174 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004175 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004176 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004177 if (!(t->flags & SN_ERR_MASK))
4178 t->flags |= SN_ERR_PRXCOND;
4179 if (!(t->flags & SN_FINST_MASK))
4180 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004181 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004182 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004183 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004184
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004185 /* has the response been denied ? */
4186 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004187 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004188 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004189
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004190 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004191 if (t->listener->counters)
4192 t->listener->counters->denied_resp++;
4193
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004194 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004195 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004196
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004197 /* add response headers from the rule sets in the same order */
4198 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004199 if (txn->status < 200)
4200 break;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004201 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004202 rule_set->rsp_add[cur_idx]) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004203 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004204 }
4205
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004206 /* check whether we're already working on the frontend */
4207 if (cur_proxy == t->fe)
4208 break;
4209 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004210 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004211
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004212 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004213 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4214 * in which case this is not the right response, and we're waiting for the
4215 * next one. Let's allow this response to go to the client and wait for the
4216 * next one.
4217 */
4218 if (txn->status < 200) {
4219 hdr_idx_init(&txn->hdr_idx);
4220 buffer_forward(rep, rep->lr - (rep->data + msg->som));
4221 msg->msg_state = HTTP_MSG_RPBEFORE;
4222 txn->status = 0;
4223 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4224 return 1;
4225 }
4226
4227 /* we don't have any 1xx status code now */
4228
4229 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004230 * 4: check for server cookie.
4231 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004232 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4233 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004234 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004235
Willy Tarreaubaaee002006-06-26 02:48:02 +02004236
Willy Tarreaua15645d2007-03-18 16:22:39 +01004237 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004238 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004239 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004240 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004241 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004242
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004243 /*
4244 * 6: add server cookie in the response if needed
4245 */
4246 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004247 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004248 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004249
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004250 /* the server is known, it's not the one the client requested, we have to
4251 * insert a set-cookie here, except if we want to insert only on POST
4252 * requests and this one isn't. Note that servers which don't have cookies
4253 * (eg: some backup servers) will return a full cookie removal request.
4254 */
4255 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4256 t->be->cookie_name,
4257 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004258
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004259 if (t->be->cookie_domain)
4260 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004261
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004262 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004263 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004264 goto return_bad_resp;
4265 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004266
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004267 /* Here, we will tell an eventual cache on the client side that we don't
4268 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4269 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4270 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4271 */
4272 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004273
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004274 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4275
4276 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004277 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004278 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004279 }
4280 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004281
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004282 /*
4283 * 7: check if result will be cacheable with a cookie.
4284 * We'll block the response if security checks have caught
4285 * nasty things such as a cacheable cookie.
4286 */
4287 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4288 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004289 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004290
4291 /* we're in presence of a cacheable response containing
4292 * a set-cookie header. We'll block it as requested by
4293 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004294 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004295 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004296 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004297
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004298 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004299 if (t->listener->counters)
4300 t->listener->counters->denied_resp++;
4301
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004302 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4303 t->be->id, t->srv?t->srv->id:"<dispatch>");
4304 send_log(t->be, LOG_ALERT,
4305 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4306 t->be->id, t->srv?t->srv->id:"<dispatch>");
4307 goto return_srv_prx_502;
4308 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004309
4310 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004311 * 8: add "Connection: close" if needed and not yet set. This is
4312 * only needed for 1.1 responses since we know there is no other
4313 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004314 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004315 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004316 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004317 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004318 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004319 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004320 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004321 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4322 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4323 "Connection: keep-alive", 22) < 0))
4324 goto return_bad_resp;
4325 must_keep = 0;
4326 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004327
Willy Tarreaud98cf932009-12-27 22:54:55 +01004328 if (txn->flags & TX_RES_XFER_LEN)
4329 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004330
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004331 /*************************************************************
4332 * OK, that's finished for the headers. We have done what we *
4333 * could. Let's switch to the DATA state. *
4334 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004335
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004336 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004337
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004338 /* if the user wants to log as soon as possible, without counting
4339 * bytes from the server, then this is the right moment. We have
4340 * to temporarily assign bytes_out to log what we currently have.
4341 */
4342 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4343 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4344 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004345 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004346 t->logs.bytes_out = 0;
4347 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004348
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004349 /* Note: we must not try to cheat by jumping directly to DATA,
4350 * otherwise we would not let the client side wake up.
4351 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004352
Willy Tarreaudafde432008-08-17 01:00:46 +02004353 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004354 }
4355 return 0;
4356}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004357
Willy Tarreaud98cf932009-12-27 22:54:55 +01004358/* This function is an analyser which forwards response body (including chunk
4359 * sizes if any). It is called as soon as we must forward, even if we forward
4360 * zero byte. The only situation where it must not be called is when we're in
4361 * tunnel mode and we want to forward till the close. It's used both to forward
4362 * remaining data and to resync after end of body. It expects the msg_state to
4363 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4364 * read more data, or 1 once we can go on with next request or end the session.
4365 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4366 * bytes of pending data + the headers if not already done (between som and sov).
4367 * It eventually adjusts som to match sov after the data in between have been sent.
4368 */
4369int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4370{
4371 struct http_txn *txn = &s->txn;
4372 struct http_msg *msg = &s->txn.rsp;
4373
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004374 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4375 !s->req->analysers) {
4376 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
4377 buffer_ignore(res, res->l - res->send_max);
4378 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004379 res->analysers &= ~an_bit;
4380 return 1;
4381 }
4382
Willy Tarreaud98cf932009-12-27 22:54:55 +01004383 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4384 return 0;
4385
Willy Tarreaub608feb2010-01-02 22:47:18 +01004386 /* note: in server-close mode, we don't want to automatically close the
4387 * output when the input is closed.
4388 */
4389 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4390 buffer_dont_close(res);
4391
Willy Tarreaud98cf932009-12-27 22:54:55 +01004392 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4393 /* we have msg->col and msg->sov which both point to the first
4394 * byte of message body. msg->som still points to the beginning
4395 * of the message. We must save the body in req->lr because it
4396 * survives buffer re-alignments.
4397 */
4398 res->lr = res->data + msg->sov;
4399 if (txn->flags & TX_RES_TE_CHNK)
4400 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4401 else {
4402 msg->msg_state = HTTP_MSG_DATA;
4403 }
4404 }
4405
Willy Tarreaud98cf932009-12-27 22:54:55 +01004406 while (1) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004407 /* we may have some data pending */
4408 if (msg->hdr_content_len || msg->som != msg->sov) {
4409 int bytes = msg->sov - msg->som;
4410 if (bytes < 0) /* sov may have wrapped at the end */
4411 bytes += res->size;
4412 buffer_forward(res, bytes + msg->hdr_content_len);
4413 msg->hdr_content_len = 0; /* don't forward that again */
4414 msg->som = msg->sov;
4415 }
4416
Willy Tarreaud98cf932009-12-27 22:54:55 +01004417 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
4418 /* read the chunk size and assign it to ->hdr_content_len, then
4419 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4420 */
4421 int ret = http_parse_chunk_size(res, msg);
4422
4423 if (!ret)
4424 goto missing_data;
4425 else if (ret < 0)
4426 goto return_bad_res;
4427 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004428 }
4429 else if (msg->msg_state == HTTP_MSG_DATA) {
4430 /* must still forward */
4431 if (res->to_forward)
4432 return 0;
4433
4434 /* nothing left to forward */
4435 if (txn->flags & TX_RES_TE_CHNK)
4436 msg->msg_state = HTTP_MSG_DATA_CRLF;
4437 else {
4438 msg->msg_state = HTTP_MSG_DONE;
4439 }
4440 }
4441 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4442 /* we want the CRLF after the data */
4443 int ret;
4444
4445 if (!(res->flags & BF_OUT_EMPTY))
4446 return 0;
4447 /* The output pointer does not move anymore, next unsent data
4448 * are available at ->w. Let's save that.
4449 */
4450 res->lr = res->w;
4451 ret = http_skip_chunk_crlf(res, msg);
4452
4453 if (!ret)
4454 goto missing_data;
4455 else if (ret < 0)
4456 goto return_bad_res;
4457 /* we're in MSG_CHUNK_SIZE now */
4458 }
4459 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4460 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004461
Willy Tarreaud98cf932009-12-27 22:54:55 +01004462 if (ret == 0)
4463 goto missing_data;
4464 else if (ret < 0)
4465 goto return_bad_res;
4466 /* we're in HTTP_MSG_DONE now */
4467 }
4468 else if (msg->msg_state == HTTP_MSG_DONE) {
Willy Tarreau5523b322009-12-29 12:05:52 +01004469 /* In theory, we don't need to read anymore, but we must
4470 * still monitor the server connection for a possible close,
4471 * so we don't set the BF_DONT_READ flag here.
4472 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004473
Willy Tarreau5523b322009-12-29 12:05:52 +01004474 if (txn->req.msg_state < HTTP_MSG_DONE && txn->req.msg_state != HTTP_MSG_ERROR) {
4475 /* The client seems to still be sending data, probably
4476 * because we got an error response during an upload.
4477 * We have the choice of either breaking the connection
4478 * or letting it pass through. Let's do the later.
4479 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004480 return 0;
Willy Tarreau5523b322009-12-29 12:05:52 +01004481 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004482
4483 /* when we support keep-alive or server-close modes, we'll have
4484 * to reset the transaction here.
4485 */
Willy Tarreau5523b322009-12-29 12:05:52 +01004486
Willy Tarreau82eeaf22009-12-29 12:09:05 +01004487 if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) {
4488 /* option forceclose is set, let's enforce it now that the transfer is complete. */
4489 buffer_abort(res);
4490 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004491 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4492 /* server close is handled entirely on the req analyser */
4493 s->req->cons->flags |= SI_FL_NOLINGER;
4494 buffer_shutw_now(s->req);
4495 }
Willy Tarreau82eeaf22009-12-29 12:09:05 +01004496
Willy Tarreau5523b322009-12-29 12:05:52 +01004497 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4498 if (res->flags & BF_OUT_EMPTY)
4499 msg->msg_state = HTTP_MSG_CLOSED;
4500 else
4501 msg->msg_state = HTTP_MSG_CLOSING;
4502 }
4503 else {
4504 /* for other modes, we let further responses pass for now */
4505 res->flags &= ~BF_DONT_READ;
4506 /* FIXME: we're still forced to do that here */
4507 s->req->flags &= ~BF_DONT_READ;
4508 break;
4509 }
4510 }
4511 else if (msg->msg_state == HTTP_MSG_CLOSING) {
4512 /* nothing else to forward, just waiting for the buffer to be empty */
4513 if (!(res->flags & BF_OUT_EMPTY))
4514 return 0;
4515 msg->msg_state = HTTP_MSG_CLOSED;
4516 }
4517 else if (msg->msg_state == HTTP_MSG_CLOSED) {
4518 res->flags &= ~BF_DONT_READ;
4519 /* FIXME: we're still forced to do that here */
4520 s->req->flags &= ~BF_DONT_READ;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004521 break;
4522 }
4523 }
4524
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004525 buffer_ignore(res, res->l - res->send_max);
4526 buffer_auto_close(res);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004527 res->analysers &= ~an_bit;
4528 return 1;
4529
4530 missing_data:
4531 /* forward the chunk size as well as any pending data */
4532 if (msg->hdr_content_len || msg->som != msg->sov) {
4533 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4534 msg->hdr_content_len = 0; /* don't forward that again */
4535 msg->som = msg->sov;
4536 }
4537
4538 if (res->flags & BF_FULL)
4539 goto return_bad_res;
4540 /* the session handler will take care of timeouts and errors */
4541 return 0;
4542
4543 return_bad_res: /* let's centralize all bad resuests */
4544 txn->rsp.msg_state = HTTP_MSG_ERROR;
4545 txn->status = 502;
4546 stream_int_cond_close(res->cons, NULL);
4547
4548 res->analysers = 0;
4549 s->be->counters.failed_resp++;
4550 if (s->srv) {
4551 s->srv->counters.failed_resp++;
4552 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4553 }
4554
4555 if (!(s->flags & SN_ERR_MASK))
4556 s->flags |= SN_ERR_PRXCOND;
4557 if (!(s->flags & SN_FINST_MASK))
4558 s->flags |= SN_FINST_R;
4559 return 0;
4560}
4561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004562/* Iterate the same filter through all request headers.
4563 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004564 * Since it can manage the switch to another backend, it updates the per-proxy
4565 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004566 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004567int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004568{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004569 char term;
4570 char *cur_ptr, *cur_end, *cur_next;
4571 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004572 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004573 struct hdr_idx_elem *cur_hdr;
4574 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004575
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004576 last_hdr = 0;
4577
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004578 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004579 old_idx = 0;
4580
4581 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004582 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004583 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004584 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004585 (exp->action == ACT_ALLOW ||
4586 exp->action == ACT_DENY ||
4587 exp->action == ACT_TARPIT))
4588 return 0;
4589
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004590 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004591 if (!cur_idx)
4592 break;
4593
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004594 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004595 cur_ptr = cur_next;
4596 cur_end = cur_ptr + cur_hdr->len;
4597 cur_next = cur_end + cur_hdr->cr + 1;
4598
4599 /* Now we have one header between cur_ptr and cur_end,
4600 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004601 */
4602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004603 /* The annoying part is that pattern matching needs
4604 * that we modify the contents to null-terminate all
4605 * strings before testing them.
4606 */
4607
4608 term = *cur_end;
4609 *cur_end = '\0';
4610
4611 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4612 switch (exp->action) {
4613 case ACT_SETBE:
4614 /* It is not possible to jump a second time.
4615 * FIXME: should we return an HTTP/500 here so that
4616 * the admin knows there's a problem ?
4617 */
4618 if (t->be != t->fe)
4619 break;
4620
4621 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004622 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004623 last_hdr = 1;
4624 break;
4625
4626 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004627 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004628 last_hdr = 1;
4629 break;
4630
4631 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004632 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004633 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004634
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004635 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004636 if (t->listener->counters)
4637 t->listener->counters->denied_resp++;
4638
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004639 break;
4640
4641 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004642 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004643 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004644
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004645 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004646 if (t->listener->counters)
4647 t->listener->counters->denied_resp++;
4648
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004649 break;
4650
4651 case ACT_REPLACE:
4652 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4653 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4654 /* FIXME: if the user adds a newline in the replacement, the
4655 * index will not be recalculated for now, and the new line
4656 * will not be counted as a new header.
4657 */
4658
4659 cur_end += delta;
4660 cur_next += delta;
4661 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004662 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004663 break;
4664
4665 case ACT_REMOVE:
4666 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4667 cur_next += delta;
4668
Willy Tarreaufa355d42009-11-29 18:12:29 +01004669 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004670 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4671 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004672 cur_hdr->len = 0;
4673 cur_end = NULL; /* null-term has been rewritten */
4674 break;
4675
4676 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004677 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004678 if (cur_end)
4679 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004680
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004681 /* keep the link from this header to next one in case of later
4682 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004683 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004684 old_idx = cur_idx;
4685 }
4686 return 0;
4687}
4688
4689
4690/* Apply the filter to the request line.
4691 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4692 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004693 * Since it can manage the switch to another backend, it updates the per-proxy
4694 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004695 */
4696int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4697{
4698 char term;
4699 char *cur_ptr, *cur_end;
4700 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004701 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004702 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004703
Willy Tarreau58f10d72006-12-04 02:26:12 +01004704
Willy Tarreau3d300592007-03-18 18:34:41 +01004705 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004706 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004707 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004708 (exp->action == ACT_ALLOW ||
4709 exp->action == ACT_DENY ||
4710 exp->action == ACT_TARPIT))
4711 return 0;
4712 else if (exp->action == ACT_REMOVE)
4713 return 0;
4714
4715 done = 0;
4716
Willy Tarreau9cdde232007-05-02 20:58:19 +02004717 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004718 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004719
4720 /* Now we have the request line between cur_ptr and cur_end */
4721
4722 /* The annoying part is that pattern matching needs
4723 * that we modify the contents to null-terminate all
4724 * strings before testing them.
4725 */
4726
4727 term = *cur_end;
4728 *cur_end = '\0';
4729
4730 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4731 switch (exp->action) {
4732 case ACT_SETBE:
4733 /* It is not possible to jump a second time.
4734 * FIXME: should we return an HTTP/500 here so that
4735 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004736 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004737 if (t->be != t->fe)
4738 break;
4739
4740 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004741 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004742 done = 1;
4743 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004744
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004745 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004746 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004747 done = 1;
4748 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004749
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004750 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004751 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004752
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004753 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004754 if (t->listener->counters)
4755 t->listener->counters->denied_resp++;
4756
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004757 done = 1;
4758 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004759
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004760 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004761 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004762
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004763 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004764 if (t->listener->counters)
4765 t->listener->counters->denied_resp++;
4766
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004767 done = 1;
4768 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004769
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004770 case ACT_REPLACE:
4771 *cur_end = term; /* restore the string terminator */
4772 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4773 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4774 /* FIXME: if the user adds a newline in the replacement, the
4775 * index will not be recalculated for now, and the new line
4776 * will not be counted as a new header.
4777 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004778
Willy Tarreaufa355d42009-11-29 18:12:29 +01004779 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004780 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004781
Willy Tarreau9cdde232007-05-02 20:58:19 +02004782 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004783 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004784 HTTP_MSG_RQMETH,
4785 cur_ptr, cur_end + 1,
4786 NULL, NULL);
4787 if (unlikely(!cur_end))
4788 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004789
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004790 /* we have a full request and we know that we have either a CR
4791 * or an LF at <ptr>.
4792 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004793 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4794 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004795 /* there is no point trying this regex on headers */
4796 return 1;
4797 }
4798 }
4799 *cur_end = term; /* restore the string terminator */
4800 return done;
4801}
Willy Tarreau97de6242006-12-27 17:18:38 +01004802
Willy Tarreau58f10d72006-12-04 02:26:12 +01004803
Willy Tarreau58f10d72006-12-04 02:26:12 +01004804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004805/*
4806 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4807 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004808 * unparsable request. Since it can manage the switch to another backend, it
4809 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004810 */
4811int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4812{
Willy Tarreau3d300592007-03-18 18:34:41 +01004813 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004814 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004815 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004816 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004817
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004818 /*
4819 * The interleaving of transformations and verdicts
4820 * makes it difficult to decide to continue or stop
4821 * the evaluation.
4822 */
4823
Willy Tarreau3d300592007-03-18 18:34:41 +01004824 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004825 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4826 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4827 exp = exp->next;
4828 continue;
4829 }
4830
4831 /* Apply the filter to the request line. */
4832 ret = apply_filter_to_req_line(t, req, exp);
4833 if (unlikely(ret < 0))
4834 return -1;
4835
4836 if (likely(ret == 0)) {
4837 /* The filter did not match the request, it can be
4838 * iterated through all headers.
4839 */
4840 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004841 }
4842 exp = exp->next;
4843 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004844 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004845}
4846
4847
Willy Tarreaua15645d2007-03-18 16:22:39 +01004848
Willy Tarreau58f10d72006-12-04 02:26:12 +01004849/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004850 * Try to retrieve the server associated to the appsession.
4851 * If the server is found, it's assigned to the session.
4852 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01004853void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004854 struct http_txn *txn = &t->txn;
4855 appsess *asession = NULL;
4856 char *sessid_temp = NULL;
4857
Cyril Bontéb21570a2009-11-29 20:04:48 +01004858 if (len > t->be->appsession_len) {
4859 len = t->be->appsession_len;
4860 }
4861
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004862 if (t->be->options2 & PR_O2_AS_REQL) {
4863 /* request-learn option is enabled : store the sessid in the session for future use */
4864 if (t->sessid != NULL) {
4865 /* free previously allocated memory as we don't need the session id found in the URL anymore */
4866 pool_free2(apools.sessid, t->sessid);
4867 }
4868
4869 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
4870 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4871 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4872 return;
4873 }
4874
Cyril Bontéb21570a2009-11-29 20:04:48 +01004875 memcpy(t->sessid, buf, len);
4876 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004877 }
4878
4879 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
4880 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4881 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4882 return;
4883 }
4884
Cyril Bontéb21570a2009-11-29 20:04:48 +01004885 memcpy(sessid_temp, buf, len);
4886 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004887
4888 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
4889 /* free previously allocated memory */
4890 pool_free2(apools.sessid, sessid_temp);
4891
4892 if (asession != NULL) {
4893 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
4894 if (!(t->be->options2 & PR_O2_AS_REQL))
4895 asession->request_count++;
4896
4897 if (asession->serverid != NULL) {
4898 struct server *srv = t->be->srv;
4899 while (srv) {
4900 if (strcmp(srv->id, asession->serverid) == 0) {
4901 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
4902 /* we found the server and it's usable */
4903 txn->flags &= ~TX_CK_MASK;
4904 txn->flags |= TX_CK_VALID;
4905 t->flags |= SN_DIRECT | SN_ASSIGNED;
4906 t->srv = srv;
4907 break;
4908 } else {
4909 txn->flags &= ~TX_CK_MASK;
4910 txn->flags |= TX_CK_DOWN;
4911 }
4912 }
4913 srv = srv->next;
4914 }
4915 }
4916 }
4917}
4918
4919/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004920 * Manage client-side cookie. It can impact performance by about 2% so it is
4921 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004922 */
4923void manage_client_side_cookies(struct session *t, struct buffer *req)
4924{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004925 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004926 char *p1, *p2, *p3, *p4;
4927 char *del_colon, *del_cookie, *colon;
4928 int app_cookies;
4929
Willy Tarreau58f10d72006-12-04 02:26:12 +01004930 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004931 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004932
Willy Tarreau2a324282006-12-05 00:05:46 +01004933 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004934 * we start with the start line.
4935 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004936 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004937 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004938
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004939 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004940 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004941 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004942
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004943 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004944 cur_ptr = cur_next;
4945 cur_end = cur_ptr + cur_hdr->len;
4946 cur_next = cur_end + cur_hdr->cr + 1;
4947
4948 /* We have one full header between cur_ptr and cur_end, and the
4949 * next header starts at cur_next. We're only interested in
4950 * "Cookie:" headers.
4951 */
4952
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004953 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4954 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004955 old_idx = cur_idx;
4956 continue;
4957 }
4958
4959 /* Now look for cookies. Conforming to RFC2109, we have to support
4960 * attributes whose name begin with a '$', and associate them with
4961 * the right cookie, if we want to delete this cookie.
4962 * So there are 3 cases for each cookie read :
4963 * 1) it's a special attribute, beginning with a '$' : ignore it.
4964 * 2) it's a server id cookie that we *MAY* want to delete : save
4965 * some pointers on it (last semi-colon, beginning of cookie...)
4966 * 3) it's an application cookie : we *MAY* have to delete a previous
4967 * "special" cookie.
4968 * At the end of loop, if a "special" cookie remains, we may have to
4969 * remove it. If no application cookie persists in the header, we
4970 * *MUST* delete it
4971 */
4972
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004973 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004974
Willy Tarreau58f10d72006-12-04 02:26:12 +01004975 /* del_cookie == NULL => nothing to be deleted */
4976 del_colon = del_cookie = NULL;
4977 app_cookies = 0;
4978
4979 while (p1 < cur_end) {
4980 /* skip spaces and colons, but keep an eye on these ones */
4981 while (p1 < cur_end) {
4982 if (*p1 == ';' || *p1 == ',')
4983 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004984 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004985 break;
4986 p1++;
4987 }
4988
4989 if (p1 == cur_end)
4990 break;
4991
4992 /* p1 is at the beginning of the cookie name */
4993 p2 = p1;
4994 while (p2 < cur_end && *p2 != '=')
4995 p2++;
4996
4997 if (p2 == cur_end)
4998 break;
4999
5000 p3 = p2 + 1; /* skips the '=' sign */
5001 if (p3 == cur_end)
5002 break;
5003
5004 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005005 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01005006 p4++;
5007
5008 /* here, we have the cookie name between p1 and p2,
5009 * and its value between p3 and p4.
5010 * we can process it :
5011 *
5012 * Cookie: NAME=VALUE;
5013 * | || || |
5014 * | || || +--> p4
5015 * | || |+-------> p3
5016 * | || +--------> p2
5017 * | |+------------> p1
5018 * | +-------------> colon
5019 * +--------------------> cur_ptr
5020 */
5021
5022 if (*p1 == '$') {
5023 /* skip this one */
5024 }
5025 else {
5026 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005027 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005028 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005029 (p4 - p1 >= t->fe->capture_namelen) &&
5030 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005031 int log_len = p4 - p1;
5032
Willy Tarreau086b3b42007-05-13 21:45:51 +02005033 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005034 Alert("HTTP logging : out of memory.\n");
5035 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005036 if (log_len > t->fe->capture_len)
5037 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005038 memcpy(txn->cli_cookie, p1, log_len);
5039 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005040 }
5041 }
5042
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005043 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5044 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005045 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005046 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005047 char *delim;
5048
5049 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5050 * have the server ID betweek p3 and delim, and the original cookie between
5051 * delim+1 and p4. Otherwise, delim==p4 :
5052 *
5053 * Cookie: NAME=SRV~VALUE;
5054 * | || || | |
5055 * | || || | +--> p4
5056 * | || || +--------> delim
5057 * | || |+-----------> p3
5058 * | || +------------> p2
5059 * | |+----------------> p1
5060 * | +-----------------> colon
5061 * +------------------------> cur_ptr
5062 */
5063
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005064 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005065 for (delim = p3; delim < p4; delim++)
5066 if (*delim == COOKIE_DELIM)
5067 break;
5068 }
5069 else
5070 delim = p4;
5071
5072
5073 /* Here, we'll look for the first running server which supports the cookie.
5074 * This allows to share a same cookie between several servers, for example
5075 * to dedicate backup servers to specific servers only.
5076 * However, to prevent clients from sticking to cookie-less backup server
5077 * when they have incidentely learned an empty cookie, we simply ignore
5078 * empty cookies and mark them as invalid.
5079 */
5080 if (delim == p3)
5081 srv = NULL;
5082
5083 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005084 if (srv->cookie && (srv->cklen == delim - p3) &&
5085 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005086 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005087 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005088 txn->flags &= ~TX_CK_MASK;
5089 txn->flags |= TX_CK_VALID;
5090 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005091 t->srv = srv;
5092 break;
5093 } else {
5094 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005095 txn->flags &= ~TX_CK_MASK;
5096 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005097 }
5098 }
5099 srv = srv->next;
5100 }
5101
Willy Tarreau3d300592007-03-18 18:34:41 +01005102 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005103 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005104 txn->flags &= ~TX_CK_MASK;
5105 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005106 }
5107
5108 /* depending on the cookie mode, we may have to either :
5109 * - delete the complete cookie if we're in insert+indirect mode, so that
5110 * the server never sees it ;
5111 * - remove the server id from the cookie value, and tag the cookie as an
5112 * application cookie so that it does not get accidentely removed later,
5113 * if we're in cookie prefix mode
5114 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005115 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005116 int delta; /* negative */
5117
5118 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5119 p4 += delta;
5120 cur_end += delta;
5121 cur_next += delta;
5122 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005123 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005124
5125 del_cookie = del_colon = NULL;
5126 app_cookies++; /* protect the header from deletion */
5127 }
5128 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005129 (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 +01005130 del_cookie = p1;
5131 del_colon = colon;
5132 }
5133 } else {
5134 /* now we know that we must keep this cookie since it's
5135 * not ours. But if we wanted to delete our cookie
5136 * earlier, we cannot remove the complete header, but we
5137 * can remove the previous block itself.
5138 */
5139 app_cookies++;
5140
5141 if (del_cookie != NULL) {
5142 int delta; /* negative */
5143
5144 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5145 p4 += delta;
5146 cur_end += delta;
5147 cur_next += delta;
5148 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005149 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005150 del_cookie = del_colon = NULL;
5151 }
5152 }
5153
Cyril Bontéb21570a2009-11-29 20:04:48 +01005154 if (t->be->appsession_name != NULL) {
5155 int cmp_len, value_len;
5156 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005157
Cyril Bontéb21570a2009-11-29 20:04:48 +01005158 if (t->be->options2 & PR_O2_AS_PFX) {
5159 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5160 value_begin = p1 + t->be->appsession_name_len;
5161 value_len = p4 - p1 - t->be->appsession_name_len;
5162 } else {
5163 cmp_len = p2 - p1;
5164 value_begin = p3;
5165 value_len = p4 - p3;
5166 }
5167
5168 /* let's see if the cookie is our appcookie */
5169 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5170 /* Cool... it's the right one */
5171 manage_client_side_appsession(t, value_begin, value_len);
5172 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005173#if defined(DEBUG_HASH)
5174 Alert("manage_client_side_cookies\n");
5175 appsession_hash_dump(&(t->be->htbl_proxy));
5176#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005177 }/* end if ((t->proxy->appsession_name != NULL) ... */
5178 }
5179
5180 /* we'll have to look for another cookie ... */
5181 p1 = p4;
5182 } /* while (p1 < cur_end) */
5183
5184 /* There's no more cookie on this line.
5185 * We may have marked the last one(s) for deletion.
5186 * We must do this now in two ways :
5187 * - if there is no app cookie, we simply delete the header ;
5188 * - if there are app cookies, we must delete the end of the
5189 * string properly, including the colon/semi-colon before
5190 * the cookie name.
5191 */
5192 if (del_cookie != NULL) {
5193 int delta;
5194 if (app_cookies) {
5195 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5196 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005197 cur_hdr->len += delta;
5198 } else {
5199 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005200
5201 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005202 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5203 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005204 cur_hdr->len = 0;
5205 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005206 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005207 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005208 }
5209
5210 /* keep the link from this header to next one */
5211 old_idx = cur_idx;
5212 } /* end of cookie processing on this header */
5213}
5214
5215
Willy Tarreaua15645d2007-03-18 16:22:39 +01005216/* Iterate the same filter through all response headers contained in <rtr>.
5217 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5218 */
5219int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5220{
5221 char term;
5222 char *cur_ptr, *cur_end, *cur_next;
5223 int cur_idx, old_idx, last_hdr;
5224 struct http_txn *txn = &t->txn;
5225 struct hdr_idx_elem *cur_hdr;
5226 int len, delta;
5227
5228 last_hdr = 0;
5229
5230 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5231 old_idx = 0;
5232
5233 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005234 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005235 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005236 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005237 (exp->action == ACT_ALLOW ||
5238 exp->action == ACT_DENY))
5239 return 0;
5240
5241 cur_idx = txn->hdr_idx.v[old_idx].next;
5242 if (!cur_idx)
5243 break;
5244
5245 cur_hdr = &txn->hdr_idx.v[cur_idx];
5246 cur_ptr = cur_next;
5247 cur_end = cur_ptr + cur_hdr->len;
5248 cur_next = cur_end + cur_hdr->cr + 1;
5249
5250 /* Now we have one header between cur_ptr and cur_end,
5251 * and the next header starts at cur_next.
5252 */
5253
5254 /* The annoying part is that pattern matching needs
5255 * that we modify the contents to null-terminate all
5256 * strings before testing them.
5257 */
5258
5259 term = *cur_end;
5260 *cur_end = '\0';
5261
5262 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5263 switch (exp->action) {
5264 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005265 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005266 last_hdr = 1;
5267 break;
5268
5269 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005270 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005271 last_hdr = 1;
5272 break;
5273
5274 case ACT_REPLACE:
5275 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5276 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5277 /* FIXME: if the user adds a newline in the replacement, the
5278 * index will not be recalculated for now, and the new line
5279 * will not be counted as a new header.
5280 */
5281
5282 cur_end += delta;
5283 cur_next += delta;
5284 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005285 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005286 break;
5287
5288 case ACT_REMOVE:
5289 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5290 cur_next += delta;
5291
Willy Tarreaufa355d42009-11-29 18:12:29 +01005292 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005293 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5294 txn->hdr_idx.used--;
5295 cur_hdr->len = 0;
5296 cur_end = NULL; /* null-term has been rewritten */
5297 break;
5298
5299 }
5300 }
5301 if (cur_end)
5302 *cur_end = term; /* restore the string terminator */
5303
5304 /* keep the link from this header to next one in case of later
5305 * removal of next header.
5306 */
5307 old_idx = cur_idx;
5308 }
5309 return 0;
5310}
5311
5312
5313/* Apply the filter to the status line in the response buffer <rtr>.
5314 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5315 * or -1 if a replacement resulted in an invalid status line.
5316 */
5317int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5318{
5319 char term;
5320 char *cur_ptr, *cur_end;
5321 int done;
5322 struct http_txn *txn = &t->txn;
5323 int len, delta;
5324
5325
Willy Tarreau3d300592007-03-18 18:34:41 +01005326 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005327 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005328 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005329 (exp->action == ACT_ALLOW ||
5330 exp->action == ACT_DENY))
5331 return 0;
5332 else if (exp->action == ACT_REMOVE)
5333 return 0;
5334
5335 done = 0;
5336
Willy Tarreau9cdde232007-05-02 20:58:19 +02005337 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005338 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5339
5340 /* Now we have the status line between cur_ptr and cur_end */
5341
5342 /* The annoying part is that pattern matching needs
5343 * that we modify the contents to null-terminate all
5344 * strings before testing them.
5345 */
5346
5347 term = *cur_end;
5348 *cur_end = '\0';
5349
5350 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5351 switch (exp->action) {
5352 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005353 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005354 done = 1;
5355 break;
5356
5357 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005358 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005359 done = 1;
5360 break;
5361
5362 case ACT_REPLACE:
5363 *cur_end = term; /* restore the string terminator */
5364 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5365 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5366 /* FIXME: if the user adds a newline in the replacement, the
5367 * index will not be recalculated for now, and the new line
5368 * will not be counted as a new header.
5369 */
5370
Willy Tarreaufa355d42009-11-29 18:12:29 +01005371 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005372 cur_end += delta;
5373
Willy Tarreau9cdde232007-05-02 20:58:19 +02005374 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005375 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005376 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005377 cur_ptr, cur_end + 1,
5378 NULL, NULL);
5379 if (unlikely(!cur_end))
5380 return -1;
5381
5382 /* we have a full respnse and we know that we have either a CR
5383 * or an LF at <ptr>.
5384 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005385 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005386 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5387 /* there is no point trying this regex on headers */
5388 return 1;
5389 }
5390 }
5391 *cur_end = term; /* restore the string terminator */
5392 return done;
5393}
5394
5395
5396
5397/*
5398 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5399 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5400 * unparsable response.
5401 */
5402int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5403{
Willy Tarreau3d300592007-03-18 18:34:41 +01005404 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005405 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005406 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005407 int ret;
5408
5409 /*
5410 * The interleaving of transformations and verdicts
5411 * makes it difficult to decide to continue or stop
5412 * the evaluation.
5413 */
5414
Willy Tarreau3d300592007-03-18 18:34:41 +01005415 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005416 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5417 exp->action == ACT_PASS)) {
5418 exp = exp->next;
5419 continue;
5420 }
5421
5422 /* Apply the filter to the status line. */
5423 ret = apply_filter_to_sts_line(t, rtr, exp);
5424 if (unlikely(ret < 0))
5425 return -1;
5426
5427 if (likely(ret == 0)) {
5428 /* The filter did not match the response, it can be
5429 * iterated through all headers.
5430 */
5431 apply_filter_to_resp_headers(t, rtr, exp);
5432 }
5433 exp = exp->next;
5434 }
5435 return 0;
5436}
5437
5438
5439
5440/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005441 * Manage server-side cookies. It can impact performance by about 2% so it is
5442 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005443 */
5444void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5445{
5446 struct http_txn *txn = &t->txn;
5447 char *p1, *p2, *p3, *p4;
5448
Willy Tarreaua15645d2007-03-18 16:22:39 +01005449 char *cur_ptr, *cur_end, *cur_next;
5450 int cur_idx, old_idx, delta;
5451
Willy Tarreaua15645d2007-03-18 16:22:39 +01005452 /* Iterate through the headers.
5453 * we start with the start line.
5454 */
5455 old_idx = 0;
5456 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5457
5458 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5459 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005460 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005461
5462 cur_hdr = &txn->hdr_idx.v[cur_idx];
5463 cur_ptr = cur_next;
5464 cur_end = cur_ptr + cur_hdr->len;
5465 cur_next = cur_end + cur_hdr->cr + 1;
5466
5467 /* We have one full header between cur_ptr and cur_end, and the
5468 * next header starts at cur_next. We're only interested in
5469 * "Cookie:" headers.
5470 */
5471
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005472 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5473 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005474 old_idx = cur_idx;
5475 continue;
5476 }
5477
5478 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005479 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005480
5481
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005482 /* maybe we only wanted to see if there was a set-cookie. Note that
5483 * the cookie capture is declared in the fronend.
5484 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005485 if (t->be->cookie_name == NULL &&
5486 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005487 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005488 return;
5489
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005490 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005491
5492 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005493 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5494 break;
5495
5496 /* p1 is at the beginning of the cookie name */
5497 p2 = p1;
5498
5499 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5500 p2++;
5501
5502 if (p2 == cur_end || *p2 == ';') /* next cookie */
5503 break;
5504
5505 p3 = p2 + 1; /* skip the '=' sign */
5506 if (p3 == cur_end)
5507 break;
5508
5509 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005510 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005511 p4++;
5512
5513 /* here, we have the cookie name between p1 and p2,
5514 * and its value between p3 and p4.
5515 * we can process it.
5516 */
5517
5518 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005519 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005520 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005521 (p4 - p1 >= t->fe->capture_namelen) &&
5522 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005523 int log_len = p4 - p1;
5524
Willy Tarreau086b3b42007-05-13 21:45:51 +02005525 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005526 Alert("HTTP logging : out of memory.\n");
5527 }
5528
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005529 if (log_len > t->fe->capture_len)
5530 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005531 memcpy(txn->srv_cookie, p1, log_len);
5532 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005533 }
5534
5535 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005536 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5537 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005538 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005539 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005540
5541 /* If the cookie is in insert mode on a known server, we'll delete
5542 * this occurrence because we'll insert another one later.
5543 * We'll delete it too if the "indirect" option is set and we're in
5544 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005545 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5546 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005547 /* this header must be deleted */
5548 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5549 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5550 txn->hdr_idx.used--;
5551 cur_hdr->len = 0;
5552 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005553 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005554
Willy Tarreau3d300592007-03-18 18:34:41 +01005555 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005556 }
5557 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005558 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005559 /* replace bytes p3->p4 with the cookie name associated
5560 * with this server since we know it.
5561 */
5562 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5563 cur_hdr->len += delta;
5564 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005565 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005566
Willy Tarreau3d300592007-03-18 18:34:41 +01005567 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005568 }
5569 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005570 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005571 /* insert the cookie name associated with this server
5572 * before existing cookie, and insert a delimitor between them..
5573 */
5574 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5575 cur_hdr->len += delta;
5576 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005577 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005578
5579 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005580 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005581 }
5582 }
5583 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005584 else if (t->be->appsession_name != NULL) {
5585 int cmp_len, value_len;
5586 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005587
Cyril Bontéb21570a2009-11-29 20:04:48 +01005588 if (t->be->options2 & PR_O2_AS_PFX) {
5589 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5590 value_begin = p1 + t->be->appsession_name_len;
5591 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5592 } else {
5593 cmp_len = p2 - p1;
5594 value_begin = p3;
5595 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005596 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005597
5598 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5599 /* Cool... it's the right one */
5600 if (t->sessid != NULL) {
5601 /* free previously allocated memory as we don't need it anymore */
5602 pool_free2(apools.sessid, t->sessid);
5603 }
5604 /* Store the sessid in the session for future use */
5605 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5606 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5607 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5608 return;
5609 }
5610 memcpy(t->sessid, value_begin, value_len);
5611 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005612 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005613 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005614 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5615 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005616 /* keep the link from this header to next one */
5617 old_idx = cur_idx;
5618 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005619
5620 if (t->sessid != NULL) {
5621 appsess *asession = NULL;
5622 /* only do insert, if lookup fails */
5623 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5624 if (asession == NULL) {
5625 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5626 Alert("Not enough Memory process_srv():asession:calloc().\n");
5627 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5628 return;
5629 }
5630 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5631 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5632 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5633 return;
5634 }
5635 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5636 asession->sessid[t->be->appsession_len] = 0;
5637
5638 size_t server_id_len = strlen(t->srv->id) + 1;
5639 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
5640 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5641 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5642 return;
5643 }
5644 asession->serverid[0] = '\0';
5645 memcpy(asession->serverid, t->srv->id, server_id_len);
5646
5647 asession->request_count = 0;
5648 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5649 }
5650
5651 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5652 asession->request_count++;
5653 }
5654
5655#if defined(DEBUG_HASH)
5656 Alert("manage_server_side_cookies\n");
5657 appsession_hash_dump(&(t->be->htbl_proxy));
5658#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005659}
5660
5661
5662
5663/*
5664 * Check if response is cacheable or not. Updates t->flags.
5665 */
5666void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5667{
5668 struct http_txn *txn = &t->txn;
5669 char *p1, *p2;
5670
5671 char *cur_ptr, *cur_end, *cur_next;
5672 int cur_idx;
5673
Willy Tarreau5df51872007-11-25 16:20:08 +01005674 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005675 return;
5676
5677 /* Iterate through the headers.
5678 * we start with the start line.
5679 */
5680 cur_idx = 0;
5681 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5682
5683 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5684 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005685 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005686
5687 cur_hdr = &txn->hdr_idx.v[cur_idx];
5688 cur_ptr = cur_next;
5689 cur_end = cur_ptr + cur_hdr->len;
5690 cur_next = cur_end + cur_hdr->cr + 1;
5691
5692 /* We have one full header between cur_ptr and cur_end, and the
5693 * next header starts at cur_next. We're only interested in
5694 * "Cookie:" headers.
5695 */
5696
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005697 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5698 if (val) {
5699 if ((cur_end - (cur_ptr + val) >= 8) &&
5700 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5701 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5702 return;
5703 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005704 }
5705
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005706 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5707 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005708 continue;
5709
5710 /* OK, right now we know we have a cache-control header at cur_ptr */
5711
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005712 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005713
5714 if (p1 >= cur_end) /* no more info */
5715 continue;
5716
5717 /* p1 is at the beginning of the value */
5718 p2 = p1;
5719
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005720 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005721 p2++;
5722
5723 /* we have a complete value between p1 and p2 */
5724 if (p2 < cur_end && *p2 == '=') {
5725 /* we have something of the form no-cache="set-cookie" */
5726 if ((cur_end - p1 >= 21) &&
5727 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5728 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005729 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005730 continue;
5731 }
5732
5733 /* OK, so we know that either p2 points to the end of string or to a comma */
5734 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5735 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5736 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5737 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005738 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005739 return;
5740 }
5741
5742 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005743 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005744 continue;
5745 }
5746 }
5747}
5748
5749
Willy Tarreau58f10d72006-12-04 02:26:12 +01005750/*
5751 * Try to retrieve a known appsession in the URI, then the associated server.
5752 * If the server is found, it's assigned to the session.
5753 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005754void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005755{
Cyril Bontéb21570a2009-11-29 20:04:48 +01005756 char *end_params, *first_param, *cur_param, *next_param;
5757 char separator;
5758 int value_len;
5759
5760 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005761
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005762 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01005763 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005764 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005765 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005766
Cyril Bontéb21570a2009-11-29 20:04:48 +01005767 first_param = NULL;
5768 switch (mode) {
5769 case PR_O2_AS_M_PP:
5770 first_param = memchr(begin, ';', len);
5771 break;
5772 case PR_O2_AS_M_QS:
5773 first_param = memchr(begin, '?', len);
5774 break;
5775 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005776
Cyril Bontéb21570a2009-11-29 20:04:48 +01005777 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005778 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005779 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005780
Cyril Bontéb21570a2009-11-29 20:04:48 +01005781 switch (mode) {
5782 case PR_O2_AS_M_PP:
5783 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
5784 end_params = (char *) begin + len;
5785 }
5786 separator = ';';
5787 break;
5788 case PR_O2_AS_M_QS:
5789 end_params = (char *) begin + len;
5790 separator = '&';
5791 break;
5792 default:
5793 /* unknown mode, shouldn't happen */
5794 return;
5795 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005796
Cyril Bontéb21570a2009-11-29 20:04:48 +01005797 cur_param = next_param = end_params;
5798 while (cur_param > first_param) {
5799 cur_param--;
5800 if ((cur_param[0] == separator) || (cur_param == first_param)) {
5801 /* let's see if this is the appsession parameter */
5802 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
5803 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
5804 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
5805 /* Cool... it's the right one */
5806 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
5807 value_len = MIN(t->be->appsession_len, next_param - cur_param);
5808 if (value_len > 0) {
5809 manage_client_side_appsession(t, cur_param, value_len);
5810 }
5811 break;
5812 }
5813 next_param = cur_param;
5814 }
5815 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005816#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005817 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005818 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005819#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005820}
5821
5822
Willy Tarreaub2513902006-12-17 14:52:38 +01005823/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005824 * In a GET or HEAD request, check if the requested URI matches the stats uri
5825 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005826 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005827 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005828 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005829 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005830 *
5831 * Returns 1 if the session's state changes, otherwise 0.
5832 */
5833int stats_check_uri_auth(struct session *t, struct proxy *backend)
5834{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005835 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005836 struct uri_auth *uri_auth = backend->uri_auth;
5837 struct user_auth *user;
5838 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005839 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005840
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005841 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5842
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005843 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005844 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005845 return 0;
5846
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005847 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005848
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005849 /* the URI is in h */
5850 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005851 return 0;
5852
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005853 h += uri_auth->uri_len;
5854 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5855 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005856 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005857 break;
5858 }
5859 h++;
5860 }
5861
5862 if (uri_auth->refresh) {
5863 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5864 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5865 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005866 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005867 break;
5868 }
5869 h++;
5870 }
5871 }
5872
Willy Tarreau55bb8452007-10-17 18:44:57 +02005873 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5874 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5875 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005876 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005877 break;
5878 }
5879 h++;
5880 }
5881
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005882 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5883
Willy Tarreaub2513902006-12-17 14:52:38 +01005884 /* we are in front of a interceptable URI. Let's check
5885 * if there's an authentication and if it's valid.
5886 */
5887 user = uri_auth->users;
5888 if (!user) {
5889 /* no user auth required, it's OK */
5890 authenticated = 1;
5891 } else {
5892 authenticated = 0;
5893
5894 /* a user list is defined, we have to check.
5895 * skip 21 chars for "Authorization: Basic ".
5896 */
5897
5898 /* FIXME: this should move to an earlier place */
5899 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005900 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5901 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5902 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005903 if (len > 14 &&
5904 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02005905 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01005906 break;
5907 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005908 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005909 }
5910
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005911 if (txn->auth_hdr.len < 21 ||
5912 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005913 user = NULL;
5914
5915 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005916 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5917 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005918 user->user_pwd, user->user_len)) {
5919 authenticated = 1;
5920 break;
5921 }
5922 user = user->next;
5923 }
5924 }
5925
5926 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005927 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005928
5929 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005930 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
5931 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005932 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01005933 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005934 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005935 if (!(t->flags & SN_ERR_MASK))
5936 t->flags |= SN_ERR_PRXCOND;
5937 if (!(t->flags & SN_FINST_MASK))
5938 t->flags |= SN_FINST_R;
5939 return 1;
5940 }
5941
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005942 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005943 * data.
5944 */
Willy Tarreau70089872008-06-13 21:12:51 +02005945 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005946 t->data_source = DATA_SRC_STATS;
5947 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005948 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005949 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
5950 t->rep->prod->private = t;
5951 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005952 return 1;
5953}
5954
Willy Tarreau4076a152009-04-02 15:18:36 +02005955/*
5956 * Capture a bad request or response and archive it in the proxy's structure.
5957 */
5958void http_capture_bad_message(struct error_snapshot *es, struct session *s,
5959 struct buffer *buf, struct http_msg *msg,
5960 struct proxy *other_end)
5961{
Willy Tarreau2df8d712009-05-01 11:33:17 +02005962 es->len = buf->r - (buf->data + msg->som);
5963 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02005964 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02005965 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02005966 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02005967 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02005968 es->when = date; // user-visible date
5969 es->sid = s->uniq_id;
5970 es->srv = s->srv;
5971 es->oe = other_end;
5972 es->src = s->cli_addr;
5973}
Willy Tarreaub2513902006-12-17 14:52:38 +01005974
Willy Tarreaubaaee002006-06-26 02:48:02 +02005975/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005976 * Print a debug line with a header
5977 */
5978void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5979{
5980 int len, max;
5981 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005982 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005983 max = end - start;
5984 UBOUND(max, sizeof(trash) - len - 1);
5985 len += strlcpy2(trash + len, start, max + 1);
5986 trash[len++] = '\n';
5987 write(1, trash, len);
5988}
5989
Willy Tarreau0937bc42009-12-22 15:03:09 +01005990/*
5991 * Initialize a new HTTP transaction for session <s>. It is assumed that all
5992 * the required fields are properly allocated and that we only need to (re)init
5993 * them. This should be used before processing any new request.
5994 */
5995void http_init_txn(struct session *s)
5996{
5997 struct http_txn *txn = &s->txn;
5998 struct proxy *fe = s->fe;
5999
6000 txn->flags = 0;
6001 txn->status = -1;
6002
6003 txn->req.sol = txn->req.eol = NULL;
6004 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6005 txn->rsp.sol = txn->rsp.eol = NULL;
6006 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6007 txn->req.hdr_content_len = 0LL;
6008 txn->rsp.hdr_content_len = 0LL;
6009 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6010 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6011 chunk_reset(&txn->auth_hdr);
6012
6013 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6014 if (fe->options2 & PR_O2_REQBUG_OK)
6015 txn->req.err_pos = -1; /* let buggy requests pass */
6016
6017 if (txn->req.cap)
6018 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6019
6020 if (txn->rsp.cap)
6021 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6022
6023 if (txn->hdr_idx.v)
6024 hdr_idx_init(&txn->hdr_idx);
6025}
6026
6027/* to be used at the end of a transaction */
6028void http_end_txn(struct session *s)
6029{
6030 struct http_txn *txn = &s->txn;
6031
6032 /* these ones will have been dynamically allocated */
6033 pool_free2(pool2_requri, txn->uri);
6034 pool_free2(pool2_capture, txn->cli_cookie);
6035 pool_free2(pool2_capture, txn->srv_cookie);
6036 txn->uri = NULL;
6037 txn->srv_cookie = NULL;
6038 txn->cli_cookie = NULL;
6039}
6040
6041/* to be used at the end of a transaction to prepare a new one */
6042void http_reset_txn(struct session *s)
6043{
6044 http_end_txn(s);
6045 http_init_txn(s);
6046
6047 s->be = s->fe;
6048 s->req->analysers = s->listener->analysers;
6049 s->logs.logwait = s->fe->to_log;
6050 s->srv = s->prev_srv = s->srv_conn = NULL;
6051 s->pend_pos = NULL;
6052 s->conn_retries = s->be->conn_retries;
6053
6054 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6055
6056 s->req->rto = s->fe->timeout.client;
6057 s->req->wto = s->be->timeout.server;
6058 s->req->cto = s->be->timeout.connect;
6059
6060 s->rep->rto = s->be->timeout.server;
6061 s->rep->wto = s->fe->timeout.client;
6062 s->rep->cto = TICK_ETERNITY;
6063
6064 s->req->rex = TICK_ETERNITY;
6065 s->req->wex = TICK_ETERNITY;
6066 s->req->analyse_exp = TICK_ETERNITY;
6067 s->rep->rex = TICK_ETERNITY;
6068 s->rep->wex = TICK_ETERNITY;
6069 s->rep->analyse_exp = TICK_ETERNITY;
6070}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006071
Willy Tarreau8797c062007-05-07 00:55:35 +02006072/************************************************************************/
6073/* The code below is dedicated to ACL parsing and matching */
6074/************************************************************************/
6075
6076
6077
6078
6079/* 1. Check on METHOD
6080 * We use the pre-parsed method if it is known, and store its number as an
6081 * integer. If it is unknown, we use the pointer and the length.
6082 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006083static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006084{
6085 int len, meth;
6086
Willy Tarreauae8b7962007-06-09 23:10:04 +02006087 len = strlen(*text);
6088 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006089
6090 pattern->val.i = meth;
6091 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006092 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006093 if (!pattern->ptr.str)
6094 return 0;
6095 pattern->len = len;
6096 }
6097 return 1;
6098}
6099
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006100static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006101acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6102 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006103{
6104 int meth;
6105 struct http_txn *txn = l7;
6106
Willy Tarreaub6866442008-07-14 23:54:42 +02006107 if (!txn)
6108 return 0;
6109
Willy Tarreau655dce92009-11-08 13:10:58 +01006110 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006111 return 0;
6112
Willy Tarreau8797c062007-05-07 00:55:35 +02006113 meth = txn->meth;
6114 test->i = meth;
6115 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006116 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6117 /* ensure the indexes are not affected */
6118 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006119 test->len = txn->req.sl.rq.m_l;
6120 test->ptr = txn->req.sol;
6121 }
6122 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6123 return 1;
6124}
6125
6126static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6127{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006128 int icase;
6129
Willy Tarreau8797c062007-05-07 00:55:35 +02006130 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006131 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006132
6133 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006134 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006135
6136 /* Other method, we must compare the strings */
6137 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006138 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006139
6140 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6141 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6142 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006143 return ACL_PAT_FAIL;
6144 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006145}
6146
6147/* 2. Check on Request/Status Version
6148 * We simply compare strings here.
6149 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006150static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006151{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006152 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006153 if (!pattern->ptr.str)
6154 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006155 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006156 return 1;
6157}
6158
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006159static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006160acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6161 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006162{
6163 struct http_txn *txn = l7;
6164 char *ptr;
6165 int len;
6166
Willy Tarreaub6866442008-07-14 23:54:42 +02006167 if (!txn)
6168 return 0;
6169
Willy Tarreau655dce92009-11-08 13:10:58 +01006170 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006171 return 0;
6172
Willy Tarreau8797c062007-05-07 00:55:35 +02006173 len = txn->req.sl.rq.v_l;
6174 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
6175
6176 while ((len-- > 0) && (*ptr++ != '/'));
6177 if (len <= 0)
6178 return 0;
6179
6180 test->ptr = ptr;
6181 test->len = len;
6182
6183 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6184 return 1;
6185}
6186
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006187static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006188acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6189 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006190{
6191 struct http_txn *txn = l7;
6192 char *ptr;
6193 int len;
6194
Willy Tarreaub6866442008-07-14 23:54:42 +02006195 if (!txn)
6196 return 0;
6197
Willy Tarreau655dce92009-11-08 13:10:58 +01006198 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006199 return 0;
6200
Willy Tarreau8797c062007-05-07 00:55:35 +02006201 len = txn->rsp.sl.st.v_l;
6202 ptr = txn->rsp.sol;
6203
6204 while ((len-- > 0) && (*ptr++ != '/'));
6205 if (len <= 0)
6206 return 0;
6207
6208 test->ptr = ptr;
6209 test->len = len;
6210
6211 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6212 return 1;
6213}
6214
6215/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006216static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006217acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6218 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006219{
6220 struct http_txn *txn = l7;
6221 char *ptr;
6222 int len;
6223
Willy Tarreaub6866442008-07-14 23:54:42 +02006224 if (!txn)
6225 return 0;
6226
Willy Tarreau655dce92009-11-08 13:10:58 +01006227 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006228 return 0;
6229
Willy Tarreau8797c062007-05-07 00:55:35 +02006230 len = txn->rsp.sl.st.c_l;
6231 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6232
6233 test->i = __strl2ui(ptr, len);
6234 test->flags = ACL_TEST_F_VOL_1ST;
6235 return 1;
6236}
6237
6238/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006239static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006240acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6241 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006242{
6243 struct http_txn *txn = l7;
6244
Willy Tarreaub6866442008-07-14 23:54:42 +02006245 if (!txn)
6246 return 0;
6247
Willy Tarreau655dce92009-11-08 13:10:58 +01006248 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006249 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006250
Willy Tarreauc11416f2007-06-17 16:58:38 +02006251 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6252 /* ensure the indexes are not affected */
6253 return 0;
6254
Willy Tarreau8797c062007-05-07 00:55:35 +02006255 test->len = txn->req.sl.rq.u_l;
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006256 test->ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006257
Willy Tarreauf3d25982007-05-08 22:45:09 +02006258 /* we do not need to set READ_ONLY because the data is in a buffer */
6259 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006260 return 1;
6261}
6262
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006263static int
6264acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6265 struct acl_expr *expr, struct acl_test *test)
6266{
6267 struct http_txn *txn = l7;
6268
Willy Tarreaub6866442008-07-14 23:54:42 +02006269 if (!txn)
6270 return 0;
6271
Willy Tarreau655dce92009-11-08 13:10:58 +01006272 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006273 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006274
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006275 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6276 /* ensure the indexes are not affected */
6277 return 0;
6278
6279 /* Parse HTTP request */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006280 url2sa(txn->req.sol - txn->req.som + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006281 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6282 test->i = AF_INET;
6283
6284 /*
6285 * If we are parsing url in frontend space, we prepare backend stage
6286 * to not parse again the same url ! optimization lazyness...
6287 */
6288 if (px->options & PR_O_HTTP_PROXY)
6289 l4->flags |= SN_ADDR_SET;
6290
6291 test->flags = ACL_TEST_F_READ_ONLY;
6292 return 1;
6293}
6294
6295static int
6296acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6297 struct acl_expr *expr, struct acl_test *test)
6298{
6299 struct http_txn *txn = l7;
6300
Willy Tarreaub6866442008-07-14 23:54:42 +02006301 if (!txn)
6302 return 0;
6303
Willy Tarreau655dce92009-11-08 13:10:58 +01006304 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006305 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006306
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006307 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6308 /* ensure the indexes are not affected */
6309 return 0;
6310
6311 /* Same optimization as url_ip */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006312 url2sa(txn->req.sol - txn->req.som + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006313 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6314
6315 if (px->options & PR_O_HTTP_PROXY)
6316 l4->flags |= SN_ADDR_SET;
6317
6318 test->flags = ACL_TEST_F_READ_ONLY;
6319 return 1;
6320}
6321
Willy Tarreauc11416f2007-06-17 16:58:38 +02006322/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6323 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6324 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006325static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006326acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006327 struct acl_expr *expr, struct acl_test *test)
6328{
6329 struct http_txn *txn = l7;
6330 struct hdr_idx *idx = &txn->hdr_idx;
6331 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006332
Willy Tarreaub6866442008-07-14 23:54:42 +02006333 if (!txn)
6334 return 0;
6335
Willy Tarreau33a7e692007-06-10 19:45:56 +02006336 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6337 /* search for header from the beginning */
6338 ctx->idx = 0;
6339
Willy Tarreau33a7e692007-06-10 19:45:56 +02006340 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6341 test->flags |= ACL_TEST_F_FETCH_MORE;
6342 test->flags |= ACL_TEST_F_VOL_HDR;
6343 test->len = ctx->vlen;
6344 test->ptr = (char *)ctx->line + ctx->val;
6345 return 1;
6346 }
6347
6348 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6349 test->flags |= ACL_TEST_F_VOL_HDR;
6350 return 0;
6351}
6352
Willy Tarreau33a7e692007-06-10 19:45:56 +02006353static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006354acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6355 struct acl_expr *expr, struct acl_test *test)
6356{
6357 struct http_txn *txn = l7;
6358
Willy Tarreaub6866442008-07-14 23:54:42 +02006359 if (!txn)
6360 return 0;
6361
Willy Tarreau655dce92009-11-08 13:10:58 +01006362 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006363 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006364
Willy Tarreauc11416f2007-06-17 16:58:38 +02006365 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6366 /* ensure the indexes are not affected */
6367 return 0;
6368
6369 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6370}
6371
6372static int
6373acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6374 struct acl_expr *expr, struct acl_test *test)
6375{
6376 struct http_txn *txn = l7;
6377
Willy Tarreaub6866442008-07-14 23:54:42 +02006378 if (!txn)
6379 return 0;
6380
Willy Tarreau655dce92009-11-08 13:10:58 +01006381 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006382 return 0;
6383
6384 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6385}
6386
6387/* 6. Check on HTTP header count. The number of occurrences is returned.
6388 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6389 */
6390static int
6391acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006392 struct acl_expr *expr, struct acl_test *test)
6393{
6394 struct http_txn *txn = l7;
6395 struct hdr_idx *idx = &txn->hdr_idx;
6396 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006397 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006398
Willy Tarreaub6866442008-07-14 23:54:42 +02006399 if (!txn)
6400 return 0;
6401
Willy Tarreau33a7e692007-06-10 19:45:56 +02006402 ctx.idx = 0;
6403 cnt = 0;
6404 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6405 cnt++;
6406
6407 test->i = cnt;
6408 test->flags = ACL_TEST_F_VOL_HDR;
6409 return 1;
6410}
6411
Willy Tarreauc11416f2007-06-17 16:58:38 +02006412static int
6413acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6414 struct acl_expr *expr, struct acl_test *test)
6415{
6416 struct http_txn *txn = l7;
6417
Willy Tarreaub6866442008-07-14 23:54:42 +02006418 if (!txn)
6419 return 0;
6420
Willy Tarreau655dce92009-11-08 13:10:58 +01006421 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006422 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006423
Willy Tarreauc11416f2007-06-17 16:58:38 +02006424 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6425 /* ensure the indexes are not affected */
6426 return 0;
6427
6428 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6429}
6430
6431static int
6432acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6433 struct acl_expr *expr, struct acl_test *test)
6434{
6435 struct http_txn *txn = l7;
6436
Willy Tarreaub6866442008-07-14 23:54:42 +02006437 if (!txn)
6438 return 0;
6439
Willy Tarreau655dce92009-11-08 13:10:58 +01006440 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006441 return 0;
6442
6443 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6444}
6445
Willy Tarreau33a7e692007-06-10 19:45:56 +02006446/* 7. Check on HTTP header's integer value. The integer value is returned.
6447 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006448 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006449 */
6450static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006451acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006452 struct acl_expr *expr, struct acl_test *test)
6453{
6454 struct http_txn *txn = l7;
6455 struct hdr_idx *idx = &txn->hdr_idx;
6456 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006457
Willy Tarreaub6866442008-07-14 23:54:42 +02006458 if (!txn)
6459 return 0;
6460
Willy Tarreau33a7e692007-06-10 19:45:56 +02006461 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6462 /* search for header from the beginning */
6463 ctx->idx = 0;
6464
Willy Tarreau33a7e692007-06-10 19:45:56 +02006465 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6466 test->flags |= ACL_TEST_F_FETCH_MORE;
6467 test->flags |= ACL_TEST_F_VOL_HDR;
6468 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6469 return 1;
6470 }
6471
6472 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6473 test->flags |= ACL_TEST_F_VOL_HDR;
6474 return 0;
6475}
6476
Willy Tarreauc11416f2007-06-17 16:58:38 +02006477static int
6478acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6479 struct acl_expr *expr, struct acl_test *test)
6480{
6481 struct http_txn *txn = l7;
6482
Willy Tarreaub6866442008-07-14 23:54:42 +02006483 if (!txn)
6484 return 0;
6485
Willy Tarreau655dce92009-11-08 13:10:58 +01006486 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006487 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006488
Willy Tarreauc11416f2007-06-17 16:58:38 +02006489 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6490 /* ensure the indexes are not affected */
6491 return 0;
6492
6493 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6494}
6495
6496static int
6497acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6498 struct acl_expr *expr, struct acl_test *test)
6499{
6500 struct http_txn *txn = l7;
6501
Willy Tarreaub6866442008-07-14 23:54:42 +02006502 if (!txn)
6503 return 0;
6504
Willy Tarreau655dce92009-11-08 13:10:58 +01006505 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006506 return 0;
6507
6508 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6509}
6510
Willy Tarreau106f9792009-09-19 07:54:16 +02006511/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6512 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6513 */
6514static int
6515acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6516 struct acl_expr *expr, struct acl_test *test)
6517{
6518 struct http_txn *txn = l7;
6519 struct hdr_idx *idx = &txn->hdr_idx;
6520 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6521
6522 if (!txn)
6523 return 0;
6524
6525 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6526 /* search for header from the beginning */
6527 ctx->idx = 0;
6528
6529 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6530 test->flags |= ACL_TEST_F_FETCH_MORE;
6531 test->flags |= ACL_TEST_F_VOL_HDR;
6532 /* Same optimization as url_ip */
6533 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6534 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6535 test->ptr = (void *)&l4->srv_addr.sin_addr;
6536 test->i = AF_INET;
6537 return 1;
6538 }
6539
6540 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6541 test->flags |= ACL_TEST_F_VOL_HDR;
6542 return 0;
6543}
6544
6545static int
6546acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6547 struct acl_expr *expr, struct acl_test *test)
6548{
6549 struct http_txn *txn = l7;
6550
6551 if (!txn)
6552 return 0;
6553
Willy Tarreau655dce92009-11-08 13:10:58 +01006554 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006555 return 0;
6556
6557 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6558 /* ensure the indexes are not affected */
6559 return 0;
6560
6561 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6562}
6563
6564static int
6565acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6566 struct acl_expr *expr, struct acl_test *test)
6567{
6568 struct http_txn *txn = l7;
6569
6570 if (!txn)
6571 return 0;
6572
Willy Tarreau655dce92009-11-08 13:10:58 +01006573 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006574 return 0;
6575
6576 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6577}
6578
Willy Tarreau737b0c12007-06-10 21:28:46 +02006579/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6580 * the first '/' after the possible hostname, and ends before the possible '?'.
6581 */
6582static int
6583acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6584 struct acl_expr *expr, struct acl_test *test)
6585{
6586 struct http_txn *txn = l7;
6587 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006588
Willy Tarreaub6866442008-07-14 23:54:42 +02006589 if (!txn)
6590 return 0;
6591
Willy Tarreau655dce92009-11-08 13:10:58 +01006592 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006593 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006594
Willy Tarreauc11416f2007-06-17 16:58:38 +02006595 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6596 /* ensure the indexes are not affected */
6597 return 0;
6598
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006599 end = txn->req.sol - txn->req.som + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006600 ptr = http_get_path(txn);
6601 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006602 return 0;
6603
6604 /* OK, we got the '/' ! */
6605 test->ptr = ptr;
6606
6607 while (ptr < end && *ptr != '?')
6608 ptr++;
6609
6610 test->len = ptr - test->ptr;
6611
6612 /* we do not need to set READ_ONLY because the data is in a buffer */
6613 test->flags = ACL_TEST_F_VOL_1ST;
6614 return 1;
6615}
6616
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006617static int
6618acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6619 struct acl_expr *expr, struct acl_test *test)
6620{
6621 struct buffer *req = s->req;
6622 struct http_txn *txn = &s->txn;
6623 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006624
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006625 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6626 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6627 */
6628
6629 if (!s || !req)
6630 return 0;
6631
Willy Tarreau655dce92009-11-08 13:10:58 +01006632 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006633 /* Already decoded as OK */
6634 test->flags |= ACL_TEST_F_SET_RES_PASS;
6635 return 1;
6636 }
6637
6638 /* Try to decode HTTP request */
6639 if (likely(req->lr < req->r))
6640 http_msg_analyzer(req, msg, &txn->hdr_idx);
6641
Willy Tarreau655dce92009-11-08 13:10:58 +01006642 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006643 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6644 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6645 return 1;
6646 }
6647 /* wait for final state */
6648 test->flags |= ACL_TEST_F_MAY_CHANGE;
6649 return 0;
6650 }
6651
6652 /* OK we got a valid HTTP request. We have some minor preparation to
6653 * perform so that further checks can rely on HTTP tests.
6654 */
6655 msg->sol = req->data + msg->som;
6656 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
6657 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6658 s->flags |= SN_REDIRECTABLE;
6659
6660 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
6661 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6662 return 1;
6663 }
6664
6665 test->flags |= ACL_TEST_F_SET_RES_PASS;
6666 return 1;
6667}
6668
Willy Tarreau8797c062007-05-07 00:55:35 +02006669
6670/************************************************************************/
6671/* All supported keywords must be declared here. */
6672/************************************************************************/
6673
6674/* Note: must not be declared <const> as its list will be overwritten */
6675static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006676 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
6677
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006678 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6679 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6680 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6681 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006682
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006683 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6684 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6685 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6686 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6687 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6688 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6689 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6690 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
6691 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02006692
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006693 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
6694 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6695 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6696 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6697 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6698 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6699 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6700 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6701 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
6702 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006703 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02006704
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006705 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6706 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
6707 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
6708 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
6709 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
6710 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
6711 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
6712 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
6713 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006714 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006715
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006716 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6717 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6718 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6719 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6720 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6721 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6722 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006723
Willy Tarreauf3d25982007-05-08 22:45:09 +02006724 { NULL, NULL, NULL, NULL },
6725
6726#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02006727 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
6728 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
6729 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
6730 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
6731 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
6732 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
6733 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
6734
Willy Tarreau8797c062007-05-07 00:55:35 +02006735 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
6736 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
6737 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
6738 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
6739 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
6740 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
6741 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
6742 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
6743
6744 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
6745 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
6746 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
6747 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
6748 { NULL, NULL, NULL, NULL },
6749#endif
6750}};
6751
6752
6753__attribute__((constructor))
6754static void __http_protocol_init(void)
6755{
6756 acl_register_keywords(&acl_kws);
6757}
6758
6759
Willy Tarreau58f10d72006-12-04 02:26:12 +01006760/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006761 * Local variables:
6762 * c-indent-level: 8
6763 * c-basic-offset: 8
6764 * End:
6765 */