blob: 1648083d4eb32ae8ed34a511329155a206c03663 [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 Tarreau21c5e4d2010-01-03 00:19:31 +01003259 if (req->flags & (BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
Willy Tarreau082b01c2010-01-02 23:58:04 +01003260 req->analysers &= ~an_bit;
3261 return 1;
3262 }
3263
Willy Tarreaud98cf932009-12-27 22:54:55 +01003264 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3265 return 0;
3266
3267 /* Note that we don't have to send 100-continue back because we don't
3268 * need the data to complete our job, and it's up to the server to
3269 * decide whether to return 100, 417 or anything else in return of
3270 * an "Expect: 100-continue" header.
3271 */
3272
3273 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3274 /* we have msg->col and msg->sov which both point to the first
3275 * byte of message body. msg->som still points to the beginning
3276 * of the message. We must save the body in req->lr because it
3277 * survives buffer re-alignments.
3278 */
3279 req->lr = req->data + msg->sov;
3280 if (txn->flags & TX_REQ_TE_CHNK)
3281 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3282 else {
3283 msg->msg_state = HTTP_MSG_DATA;
3284 }
3285 }
3286
Willy Tarreaud98cf932009-12-27 22:54:55 +01003287 while (1) {
Willy Tarreau638cd022010-01-03 07:42:04 +01003288 /* we may have some data pending */
3289 if (msg->hdr_content_len || msg->som != msg->sov) {
3290 int bytes = msg->sov - msg->som;
3291 if (bytes < 0) /* sov may have wrapped at the end */
3292 bytes += req->size;
3293 buffer_forward(req, bytes + msg->hdr_content_len);
3294 msg->hdr_content_len = 0; /* don't forward that again */
3295 msg->som = msg->sov;
3296 }
Willy Tarreau5523b322009-12-29 12:05:52 +01003297
Willy Tarreaud98cf932009-12-27 22:54:55 +01003298 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
3299 /* read the chunk size and assign it to ->hdr_content_len, then
3300 * set ->sov and ->lr to point to the body and switch to DATA or
3301 * TRAILERS state.
3302 */
3303 int ret = http_parse_chunk_size(req, msg);
3304
3305 if (!ret)
3306 goto missing_data;
3307 else if (ret < 0)
3308 goto return_bad_req;
3309 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003310 }
3311 else if (msg->msg_state == HTTP_MSG_DATA) {
3312 /* must still forward */
3313 if (req->to_forward)
3314 return 0;
3315
3316 /* we're sending the last bits of request, the server's response
3317 * is expected in a short time. Most often the first read is enough
3318 * to bring all the headers, so we're preparing the response buffer
3319 * to read the response now. Note that we should probably move that
3320 * to a more appropriate place.
3321 */
Willy Tarreau5523b322009-12-29 12:05:52 +01003322 if (txn->rsp.msg_state == HTTP_MSG_RPBEFORE) {
3323 s->rep->flags &= ~BF_DONT_READ;
3324 s->rep->flags |= BF_READ_DONTWAIT;
3325 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01003326
3327 /* nothing left to forward */
3328 if (txn->flags & TX_REQ_TE_CHNK)
3329 msg->msg_state = HTTP_MSG_DATA_CRLF;
3330 else {
3331 msg->msg_state = HTTP_MSG_DONE;
3332 }
3333 }
3334 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
3335 /* we want the CRLF after the data */
3336 int ret;
3337
3338 if (!(req->flags & BF_OUT_EMPTY))
3339 return 0;
3340 /* The output pointer does not move anymore, next unsent data
3341 * are available at ->w. Let's save that.
3342 */
3343 req->lr = req->w;
3344 ret = http_skip_chunk_crlf(req, msg);
3345
3346 if (ret == 0)
3347 goto missing_data;
3348 else if (ret < 0)
3349 goto return_bad_req;
3350 /* we're in MSG_CHUNK_SIZE now */
3351 }
3352 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
3353 int ret = http_forward_trailers(req, msg);
3354
3355 if (ret == 0)
3356 goto missing_data;
3357 else if (ret < 0)
3358 goto return_bad_req;
3359 /* we're in HTTP_MSG_DONE now */
3360 }
3361 else if (msg->msg_state == HTTP_MSG_DONE) {
Willy Tarreau5523b322009-12-29 12:05:52 +01003362 /* No need to read anymore, the request was completely parsed */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003363 req->flags |= BF_DONT_READ;
3364
Willy Tarreau5523b322009-12-29 12:05:52 +01003365 if (txn->rsp.msg_state < HTTP_MSG_DONE && txn->rsp.msg_state != HTTP_MSG_ERROR) {
3366 /* The server has not finished to respond, so we
3367 * don't want to move in order not to upset it.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003368 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01003369 return 0;
3370 }
3371
3372 /* when we support keep-alive or server-close modes, we'll have
3373 * to reset the transaction here.
3374 */
Willy Tarreau5523b322009-12-29 12:05:52 +01003375
Willy Tarreaub608feb2010-01-02 22:47:18 +01003376 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3377 /* initiate a connection close to the server */
3378 req->cons->flags |= SI_FL_NOLINGER;
3379 buffer_shutw_now(req);
3380 }
3381 else if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) {
Willy Tarreau9438c712009-12-29 14:39:48 +01003382 /* Option forceclose is set, let's enforce it now
3383 * that the transfer is complete. We can safely speed
3384 * up the close because we know the server has received
3385 * everything we wanted it to receive.
3386 */
3387 req->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau82eeaf22009-12-29 12:09:05 +01003388 buffer_abort(req);
3389 }
3390
Willy Tarreau5523b322009-12-29 12:05:52 +01003391 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3392 if (req->flags & BF_OUT_EMPTY)
3393 msg->msg_state = HTTP_MSG_CLOSED;
3394 else
3395 msg->msg_state = HTTP_MSG_CLOSING;
3396 }
3397 else {
3398 /* for other modes, we let further requests pass for now */
3399 req->flags &= ~BF_DONT_READ;
3400 /* FIXME: we're still forced to do that here */
3401 s->rep->flags &= ~BF_DONT_READ;
3402 break;
3403 }
3404 }
3405 else if (msg->msg_state == HTTP_MSG_CLOSING) {
3406 /* nothing else to forward, just waiting for the buffer to be empty */
3407 if (!(req->flags & BF_OUT_EMPTY))
3408 return 0;
3409 msg->msg_state = HTTP_MSG_CLOSED;
3410 }
3411 else if (msg->msg_state == HTTP_MSG_CLOSED) {
3412 req->flags &= ~BF_DONT_READ;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003413
3414 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3415
3416 /* FIXME : this part is 1) awful, 2) tricky, 3) duplicated
3417 * ... but it works.
3418 * We need a better way to force a connection close without
3419 * any risk of propagation to the other side. We need a more
3420 * portable way of releasing a backend's and a server's
3421 * connections. We need a safer way to reinitialize buffer
3422 * flags. We also need a more accurate method for computing
3423 * per-request data.
3424 */
3425 s->req->cons->flags |= SI_FL_NOLINGER;
3426 s->req->cons->shutr(s->req->cons);
3427 s->req->cons->shutw(s->req->cons);
3428
3429 if (s->flags & SN_BE_ASSIGNED)
3430 s->be->beconn--;
3431
3432 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3433 session_process_counters(s);
3434
3435 if (s->txn.status) {
3436 int n;
3437
3438 n = s->txn.status / 100;
3439 if (n < 1 || n > 5)
3440 n = 0;
3441
3442 if (s->fe->mode == PR_MODE_HTTP)
3443 s->fe->counters.p.http.rsp[n]++;
3444
3445 if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) &&
3446 (s->be->mode == PR_MODE_HTTP))
3447 s->be->counters.p.http.rsp[n]++;
3448 }
3449
3450 /* don't count other requests' data */
3451 s->logs.bytes_in -= s->req->l - s->req->send_max;
3452 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3453
3454 /* let's do a final log if we need it */
3455 if (s->logs.logwait &&
3456 !(s->flags & SN_MONITOR) &&
3457 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3458 s->do_log(s);
3459 }
3460
3461 s->logs.accept_date = date; /* user-visible date for logging */
3462 s->logs.tv_accept = now; /* corrected date for internal use */
3463 tv_zero(&s->logs.tv_request);
3464 s->logs.t_queue = -1;
3465 s->logs.t_connect = -1;
3466 s->logs.t_data = -1;
3467 s->logs.t_close = 0;
3468 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3469 s->logs.srv_queue_size = 0; /* we will get this number soon */
3470
3471 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3472 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3473
3474 if (s->pend_pos)
3475 pendconn_free(s->pend_pos);
3476
3477 if (s->srv) {
3478 if (s->flags & SN_CURR_SESS) {
3479 s->flags &= ~SN_CURR_SESS;
3480 s->srv->cur_sess--;
3481 }
3482 if (may_dequeue_tasks(s->srv, s->be))
3483 process_srv_queue(s->srv);
3484 }
3485
3486 if (unlikely(s->srv_conn))
3487 sess_change_server(s, NULL);
3488 s->srv = NULL;
3489
3490 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3491 s->req->cons->fd = -1; /* just to help with debugging */
3492 s->req->cons->err_type = SI_ET_NONE;
3493 s->req->cons->err_loc = NULL;
3494 s->req->cons->exp = TICK_ETERNITY;
3495 s->req->cons->flags = SI_FL_NONE;
3496 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_AUTO_CLOSE);
3497 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);
3498 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED);
3499 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3500 s->txn.meth = 0;
3501 http_reset_txn(s);
3502 txn->flags |= TX_NOT_FIRST;
3503 if (s->be->options2 & PR_O2_INDEPSTR)
3504 s->req->cons->flags |= SI_FL_INDEP_STR;
3505
Willy Tarreauface8392010-01-03 11:37:54 +01003506 /* if the request buffer is not empty, it means we're
3507 * about to process another request, so send pending
3508 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau5e8949c2010-01-03 14:38:03 +01003509 * Also, let's not start reading a small request packet,
3510 * we may prefer to read a larger one later.
Willy Tarreauface8392010-01-03 11:37:54 +01003511 */
Willy Tarreau5e8949c2010-01-03 14:38:03 +01003512 if (s->req->l > s->req->send_max) {
Willy Tarreauface8392010-01-03 11:37:54 +01003513 s->rep->flags |= BF_EXPECT_MORE;
Willy Tarreau5e8949c2010-01-03 14:38:03 +01003514 s->req->flags |= BF_DONT_READ;
3515 }
Willy Tarreauface8392010-01-03 11:37:54 +01003516
Willy Tarreaub608feb2010-01-02 22:47:18 +01003517 /* make ->lr point to the first non-forwarded byte */
3518 s->req->lr = s->req->w + s->req->send_max;
3519 if (s->req->lr >= s->req->data + s->req->size)
3520 s->req->lr -= s->req->size;
3521 s->rep->lr = s->rep->w + s->rep->send_max;
3522 if (s->rep->lr >= s->rep->data + s->rep->size)
3523 s->rep->lr -= s->req->size;
3524
3525 s->req->analysers |= s->fe->fe_req_ana;
3526 s->rep->analysers = 0;
3527 }
3528
Willy Tarreau5523b322009-12-29 12:05:52 +01003529 /* FIXME: we're still forced to do that here */
3530 s->rep->flags &= ~BF_DONT_READ;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003531 break;
3532 }
3533 }
3534
3535 /* OK we're done with the data phase */
3536 req->analysers &= ~an_bit;
3537 return 1;
3538
3539 missing_data:
3540 /* forward the chunk size as well as any pending data */
3541 if (msg->hdr_content_len || msg->som != msg->sov) {
3542 buffer_forward(req, msg->sov - msg->som + msg->hdr_content_len);
3543 msg->hdr_content_len = 0; /* don't forward that again */
3544 msg->som = msg->sov;
3545 }
3546
3547 if (req->flags & BF_FULL)
3548 goto return_bad_req;
3549 /* the session handler will take care of timeouts and errors */
3550 return 0;
3551
3552 return_bad_req: /* let's centralize all bad requests */
3553 txn->req.msg_state = HTTP_MSG_ERROR;
3554 txn->status = 400;
3555 /* Note: we don't send any error if some data were already sent */
3556 stream_int_cond_close(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
3557
3558 req->analysers = 0;
3559 s->fe->counters.failed_req++;
3560 if (s->listener->counters)
3561 s->listener->counters->failed_req++;
3562
3563 if (!(s->flags & SN_ERR_MASK))
3564 s->flags |= SN_ERR_PRXCOND;
3565 if (!(s->flags & SN_FINST_MASK))
3566 s->flags |= SN_FINST_R;
3567 return 0;
3568}
3569
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003570/* This stream analyser waits for a complete HTTP response. It returns 1 if the
3571 * processing can continue on next analysers, or zero if it either needs more
3572 * data or wants to immediately abort the response (eg: timeout, error, ...). It
3573 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
3574 * when it has nothing left to do, and may remove any analyser when it wants to
3575 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003576 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003577int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003578{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003579 struct http_txn *txn = &s->txn;
3580 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003581 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003582 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003583 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003584 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003585
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003586 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 +02003587 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003588 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02003589 rep,
3590 rep->rex, rep->wex,
3591 rep->flags,
3592 rep->l,
3593 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02003594
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003595 /*
3596 * Now parse the partial (or complete) lines.
3597 * We will check the response syntax, and also join multi-line
3598 * headers. An index of all the lines will be elaborated while
3599 * parsing.
3600 *
3601 * For the parsing, we use a 28 states FSM.
3602 *
3603 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01003604 * rep->data + msg->som = beginning of response
3605 * rep->data + msg->eoh = end of processed headers / start of current one
3606 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003607 * rep->lr = first non-visited byte
3608 * rep->r = end of data
3609 */
3610
Willy Tarreau83e3af02009-12-28 17:39:57 +01003611 /* There's a protected area at the end of the buffer for rewriting
3612 * purposes. We don't want to start to parse the request if the
3613 * protected area is affected, because we may have to move processed
3614 * data later, which is much more complicated.
3615 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003616 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
3617 if (unlikely((rep->flags & BF_FULL) ||
3618 rep->r < rep->lr ||
3619 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
3620 if (rep->send_max) {
3621 /* some data has still not left the buffer, wake us once that's done */
3622 buffer_dont_close(rep);
3623 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
3624 return 0;
3625 }
3626 if (rep->l <= rep->size - global.tune.maxrewrite)
3627 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003628 }
3629
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01003630 if (likely(rep->lr < rep->r))
3631 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01003632 }
3633
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003634 /* 1: we might have to print this header in debug mode */
3635 if (unlikely((global.mode & MODE_DEBUG) &&
3636 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01003637 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003638 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003639
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003640 sol = rep->data + msg->som;
3641 eol = sol + msg->sl.rq.l;
3642 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003643
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003644 sol += hdr_idx_first_pos(&txn->hdr_idx);
3645 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003646
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003647 while (cur_idx) {
3648 eol = sol + txn->hdr_idx.v[cur_idx].len;
3649 debug_hdr("srvhdr", s, sol, eol);
3650 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
3651 cur_idx = txn->hdr_idx.v[cur_idx].next;
3652 }
3653 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003654
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003655 /*
3656 * Now we quickly check if we have found a full valid response.
3657 * If not so, we check the FD and buffer states before leaving.
3658 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01003659 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003660 * responses are checked first.
3661 *
3662 * Depending on whether the client is still there or not, we
3663 * may send an error response back or not. Note that normally
3664 * we should only check for HTTP status there, and check I/O
3665 * errors somewhere else.
3666 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003667
Willy Tarreau655dce92009-11-08 13:10:58 +01003668 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003669 /* Invalid response */
3670 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
3671 /* we detected a parsing error. We want to archive this response
3672 * in the dedicated proxy area for later troubleshooting.
3673 */
3674 hdr_response_bad:
3675 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
3676 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3677
3678 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003679 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003680 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003681 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
3682 }
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003683
3684 rep->analysers = 0;
3685 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003686 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003687 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
3688
3689 if (!(s->flags & SN_ERR_MASK))
3690 s->flags |= SN_ERR_PRXCOND;
3691 if (!(s->flags & SN_FINST_MASK))
3692 s->flags |= SN_FINST_H;
3693
3694 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003695 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003696
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003697 /* too large response does not fit in buffer. */
3698 else if (rep->flags & BF_FULL) {
3699 goto hdr_response_bad;
3700 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003701
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003702 /* read error */
3703 else if (rep->flags & BF_READ_ERROR) {
3704 if (msg->err_pos >= 0)
3705 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02003706
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003707 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003708 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003709 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003710 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
3711 }
Willy Tarreau461f6622008-08-15 23:43:19 +02003712
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003713 rep->analysers = 0;
3714 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003715 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003716 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02003717
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003718 if (!(s->flags & SN_ERR_MASK))
3719 s->flags |= SN_ERR_SRVCL;
3720 if (!(s->flags & SN_FINST_MASK))
3721 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02003722 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003723 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003724
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003725 /* read timeout : return a 504 to the client. */
3726 else if (rep->flags & BF_READ_TIMEOUT) {
3727 if (msg->err_pos >= 0)
3728 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003729
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003730 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003731 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003732 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003733 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
3734 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003735
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003736 rep->analysers = 0;
3737 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003738 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003739 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02003740
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003741 if (!(s->flags & SN_ERR_MASK))
3742 s->flags |= SN_ERR_SRVTO;
3743 if (!(s->flags & SN_FINST_MASK))
3744 s->flags |= SN_FINST_H;
3745 return 0;
3746 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02003747
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003748 /* close from server */
3749 else if (rep->flags & BF_SHUTR) {
3750 if (msg->err_pos >= 0)
3751 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01003752
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003753 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003754 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003755 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003756 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
3757 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003758
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003759 rep->analysers = 0;
3760 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01003761 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003762 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01003763
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003764 if (!(s->flags & SN_ERR_MASK))
3765 s->flags |= SN_ERR_SRVCL;
3766 if (!(s->flags & SN_FINST_MASK))
3767 s->flags |= SN_FINST_H;
3768 return 0;
3769 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003770
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003771 /* write error to client (we don't send any message then) */
3772 else if (rep->flags & BF_WRITE_ERROR) {
3773 if (msg->err_pos >= 0)
3774 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003775
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003776 s->be->counters.failed_resp++;
3777 rep->analysers = 0;
3778
3779 if (!(s->flags & SN_ERR_MASK))
3780 s->flags |= SN_ERR_CLICL;
3781 if (!(s->flags & SN_FINST_MASK))
3782 s->flags |= SN_FINST_H;
3783
3784 /* process_session() will take care of the error */
3785 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003786 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01003787
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003788 buffer_dont_close(rep);
3789 return 0;
3790 }
3791
3792 /* More interesting part now : we know that we have a complete
3793 * response which at least looks like HTTP. We have an indicator
3794 * of each header's length, so we can parse them quickly.
3795 */
3796
3797 if (unlikely(msg->err_pos >= 0))
3798 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
3799
3800 /* ensure we keep this pointer to the beginning of the message */
3801 msg->sol = rep->data + msg->som;
3802
3803 /*
3804 * 1: get the status code
3805 */
3806 n = rep->data[msg->sl.st.c] - '0';
3807 if (n < 1 || n > 5)
3808 n = 0;
3809 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003810
Willy Tarreau5b154472009-12-21 20:11:07 +01003811 /* check if the response is HTTP/1.1 or above */
3812 if ((msg->sl.st.v_l == 8) &&
3813 ((rep->data[msg->som + 5] > '1') ||
3814 ((rep->data[msg->som + 5] == '1') &&
3815 (rep->data[msg->som + 7] >= '1'))))
3816 txn->flags |= TX_RES_VER_11;
3817
3818 /* "connection" has not been parsed yet */
3819 txn->flags &= ~TX_CON_HDR_PARS;
3820
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003821 /* transfer length unknown*/
3822 txn->flags &= ~TX_RES_XFER_LEN;
3823
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003824 txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l);
3825
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003826 if (txn->status >= 100 && txn->status < 500)
3827 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
3828 else
3829 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
3830
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003831 /*
3832 * 2: check for cacheability.
3833 */
3834
3835 switch (txn->status) {
3836 case 200:
3837 case 203:
3838 case 206:
3839 case 300:
3840 case 301:
3841 case 410:
3842 /* RFC2616 @13.4:
3843 * "A response received with a status code of
3844 * 200, 203, 206, 300, 301 or 410 MAY be stored
3845 * by a cache (...) unless a cache-control
3846 * directive prohibits caching."
3847 *
3848 * RFC2616 @9.5: POST method :
3849 * "Responses to this method are not cacheable,
3850 * unless the response includes appropriate
3851 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003852 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003853 if (likely(txn->meth != HTTP_METH_POST) &&
3854 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
3855 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3856 break;
3857 default:
3858 break;
3859 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02003860
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003861 /*
3862 * 3: we may need to capture headers
3863 */
3864 s->logs.logwait &= ~LW_RESP;
3865 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
3866 capture_headers(rep->data + msg->som, &txn->hdr_idx,
3867 txn->rsp.cap, s->fe->rsp_cap);
3868
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003869 /* 4: determine the transfer-length.
3870 * According to RFC2616 #4.4, amended by the HTTPbis working group,
3871 * the presence of a message-body in a RESPONSE and its transfer length
3872 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003873 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003874 * All responses to the HEAD request method MUST NOT include a
3875 * message-body, even though the presence of entity-header fields
3876 * might lead one to believe they do. All 1xx (informational), 204
3877 * (No Content), and 304 (Not Modified) responses MUST NOT include a
3878 * message-body. All other responses do include a message-body,
3879 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003880 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003881 * 1. Any response which "MUST NOT" include a message-body (such as the
3882 * 1xx, 204 and 304 responses and any response to a HEAD request) is
3883 * always terminated by the first empty line after the header fields,
3884 * regardless of the entity-header fields present in the message.
3885 *
3886 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
3887 * the "chunked" transfer-coding (Section 6.2) is used, the
3888 * transfer-length is defined by the use of this transfer-coding.
3889 * If a Transfer-Encoding header field is present and the "chunked"
3890 * transfer-coding is not present, the transfer-length is defined by
3891 * the sender closing the connection.
3892 *
3893 * 3. If a Content-Length header field is present, its decimal value in
3894 * OCTETs represents both the entity-length and the transfer-length.
3895 * If a message is received with both a Transfer-Encoding header
3896 * field and a Content-Length header field, the latter MUST be ignored.
3897 *
3898 * 4. If the message uses the media type "multipart/byteranges", and
3899 * the transfer-length is not otherwise specified, then this self-
3900 * delimiting media type defines the transfer-length. This media
3901 * type MUST NOT be used unless the sender knows that the recipient
3902 * can parse it; the presence in a request of a Range header with
3903 * multiple byte-range specifiers from a 1.1 client implies that the
3904 * client can parse multipart/byteranges responses.
3905 *
3906 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003907 */
3908
3909 /* Skip parsing if no content length is possible. The response flags
3910 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003911 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003912 * FIXME: should we parse anyway and return an error on chunked encoding ?
3913 */
3914 if (txn->meth == HTTP_METH_HEAD ||
3915 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003916 txn->status == 204 || txn->status == 304) {
3917 txn->flags |= TX_RES_XFER_LEN;
3918 goto skip_content_length;
3919 }
3920
3921 if (txn->meth == HTTP_METH_CONNECT)
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003922 goto skip_content_length;
3923
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003924 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003925 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003926 while ((txn->flags & TX_RES_VER_11) &&
3927 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003928 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
3929 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3930 else if (txn->flags & TX_RES_TE_CHNK) {
3931 /* bad transfer-encoding (chunked followed by something else) */
3932 use_close_only = 1;
3933 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
3934 break;
3935 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003936 }
3937
3938 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
3939 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003940 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003941 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
3942 signed long long cl;
3943
3944 if (!ctx.vlen)
3945 goto hdr_response_bad;
3946
3947 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
3948 goto hdr_response_bad; /* parse failure */
3949
3950 if (cl < 0)
3951 goto hdr_response_bad;
3952
3953 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
3954 goto hdr_response_bad; /* already specified, was different */
3955
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003956 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003957 msg->hdr_content_len = cl;
3958 }
3959
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003960 /* FIXME: we should also implement the multipart/byterange method.
3961 * For now on, we resort to close mode in this case (unknown length).
3962 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003963skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02003964
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003965 /* end of job, return OK */
3966 rep->analysers &= ~an_bit;
3967 rep->analyse_exp = TICK_ETERNITY;
3968 return 1;
3969}
3970
3971/* This function performs all the processing enabled for the current response.
3972 * It normally returns zero, but may return 1 if it absolutely needs to be
3973 * called again after other functions. It relies on buffers flags, and updates
3974 * t->rep->analysers. It might make sense to explode it into several other
3975 * functions. It works like process_request (see indications above).
3976 */
3977int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
3978{
3979 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003980 struct http_msg *msg = &txn->rsp;
3981 struct proxy *cur_proxy;
3982 int cur_idx;
Willy Tarreau5b154472009-12-21 20:11:07 +01003983 int conn_ka = 0, conn_cl = 0;
3984 int must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003985 int must_del_close = 0, must_keep = 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003986
3987 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3988 now_ms, __FUNCTION__,
3989 t,
3990 rep,
3991 rep->rex, rep->wex,
3992 rep->flags,
3993 rep->l,
3994 rep->analysers);
3995
Willy Tarreau655dce92009-11-08 13:10:58 +01003996 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02003997 return 0;
3998
3999 rep->analysers &= ~an_bit;
4000 rep->analyse_exp = TICK_ETERNITY;
4001
Willy Tarreau5b154472009-12-21 20:11:07 +01004002 /* Now we have to check if we need to modify the Connection header.
4003 * This is more difficult on the response than it is on the request,
4004 * because we can have two different HTTP versions and we don't know
4005 * how the client will interprete a response. For instance, let's say
4006 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4007 * HTTP/1.1 response without any header. Maybe it will bound itself to
4008 * HTTP/1.0 because it only knows about it, and will consider the lack
4009 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4010 * the lack of header as a keep-alive. Thus we will use two flags
4011 * indicating how a request MAY be understood by the client. In case
4012 * of multiple possibilities, we'll fix the header to be explicit. If
4013 * ambiguous cases such as both close and keepalive are seen, then we
4014 * will fall back to explicit close. Note that we won't take risks with
4015 * HTTP/1.0 clients which may not necessarily understand keep-alive.
4016 */
4017
4018 if ((txn->meth != HTTP_METH_CONNECT) &&
4019 (txn->status >= 200) &&
4020 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN &&
4021 !(txn->flags & TX_CON_HDR_PARS)) {
4022 int may_keep = 0, may_close = 0; /* how it may be understood */
4023 struct hdr_ctx ctx;
4024
4025 ctx.idx = 0;
4026 while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) {
4027 if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0)
4028 conn_cl = 1;
4029 else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0)
4030 conn_ka = 1;
4031 }
4032
4033 if (conn_cl) {
4034 /* close header present */
4035 may_close = 1;
4036 if (conn_ka) /* we have both close and keep-alive */
4037 may_keep = 1;
4038 }
4039 else if (conn_ka) {
4040 /* keep-alive alone */
4041 may_keep = 1;
4042 }
4043 else {
4044 /* no close nor keep-alive header */
4045 if (txn->flags & TX_RES_VER_11)
4046 may_keep = 1;
4047 else
4048 may_close = 1;
4049
4050 if (txn->flags & TX_REQ_VER_11)
4051 may_keep = 1;
4052 else
4053 may_close = 1;
4054 }
4055
4056 /* let's update the transaction status to reflect any close.
4057 * Note that ambiguous cases with keep & close will also be
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004058 * handled. We also explicitly state that we will close in
4059 * case of an ambiguous response having no content-length.
Willy Tarreau5b154472009-12-21 20:11:07 +01004060 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004061 if ((may_close &&
4062 (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) ||
4063 !(txn->flags & TX_RES_XFER_LEN))
Willy Tarreau5b154472009-12-21 20:11:07 +01004064 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4065
4066 /* Now we must adjust the response header :
4067 * - set "close" if may_keep and WANT_CLO
4068 * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK)
4069 * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length
4070 *
4071 * Until we support the server-close mode, we'll only support the set "close".
4072 */
4073 if (may_keep && (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO)
4074 must_close = 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004075 else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
4076 may_close && (txn->flags & TX_RES_XFER_LEN)) {
4077 must_del_close = 1;
4078 if (!(txn->flags & TX_REQ_VER_11))
4079 must_keep = 1;
4080 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004081
4082 txn->flags |= TX_CON_HDR_PARS;
4083 }
4084
4085 /* We might have to check for "Connection:" if the server
4086 * returns a connection status that is not compatible with
4087 * the client's or with the config.
4088 */
Willy Tarreaub608feb2010-01-02 22:47:18 +01004089 if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004090 char *cur_ptr, *cur_end, *cur_next;
4091 int cur_idx, old_idx, delta, val;
4092 int must_delete;
4093 struct hdr_idx_elem *cur_hdr;
4094
4095 /* we just have to remove the headers if both sides are 1.0 */
4096 must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004097
4098 /* same if we want to re-enable keep-alive on 1.1 */
4099 must_delete |= must_del_close;
4100
Willy Tarreau5b154472009-12-21 20:11:07 +01004101 cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
4102
4103 for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) {
4104 cur_hdr = &txn->hdr_idx.v[cur_idx];
4105 cur_ptr = cur_next;
4106 cur_end = cur_ptr + cur_hdr->len;
4107 cur_next = cur_end + cur_hdr->cr + 1;
4108
4109 val = http_header_match2(cur_ptr, cur_end, "Connection", 10);
4110 if (!val)
4111 continue;
4112
4113 /* 3 possibilities :
4114 * - we have already set "Connection: close" or we're in
4115 * HTTP/1.0, so we remove this line.
4116 * - we have not yet set "Connection: close", but this line
4117 * indicates close. We leave it untouched and set the flag.
4118 * - we have not yet set "Connection: close", and this line
4119 * indicates non-close. We replace it and set the flag.
4120 */
4121 if (must_delete) {
4122 delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0);
4123 http_msg_move_end(&txn->rsp, delta);
4124 cur_next += delta;
4125 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4126 txn->hdr_idx.used--;
4127 cur_hdr->len = 0;
4128 must_close = 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004129 must_del_close = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004130 } else {
4131 if (cur_end - cur_ptr - val != 5 ||
4132 strncasecmp(cur_ptr + val, "close", 5) != 0) {
4133 delta = buffer_replace2(rep, cur_ptr + val, cur_end,
4134 "close", 5);
4135 cur_next += delta;
4136 cur_hdr->len += delta;
4137 http_msg_move_end(&txn->rsp, delta);
4138 }
4139 must_delete = 1;
4140 must_close = 0;
4141 }
4142 } /* for loop */
4143 } /* if must close keep-alive */
4144
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004145 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004146 /*
4147 * 3: we will have to evaluate the filters.
4148 * As opposed to version 1.2, now they will be evaluated in the
4149 * filters order and not in the header order. This means that
4150 * each filter has to be validated among all headers.
4151 *
4152 * Filters are tried with ->be first, then with ->fe if it is
4153 * different from ->be.
4154 */
4155
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004156 cur_proxy = t->be;
4157 while (1) {
4158 struct proxy *rule_set = cur_proxy;
4159
4160 /* try headers filters */
4161 if (rule_set->rsp_exp != NULL) {
4162 if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) {
4163 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004164 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004165 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004166 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4167 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004168 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004169 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004170 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004171 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004172 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau8e89b842009-10-18 23:56:35 +02004173 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004174 if (!(t->flags & SN_ERR_MASK))
4175 t->flags |= SN_ERR_PRXCOND;
4176 if (!(t->flags & SN_FINST_MASK))
4177 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004178 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004179 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004180 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004181
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004182 /* has the response been denied ? */
4183 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004184 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004185 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004186
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004187 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004188 if (t->listener->counters)
4189 t->listener->counters->denied_resp++;
4190
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004191 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004192 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004193
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004194 /* add response headers from the rule sets in the same order */
4195 for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004196 if (txn->status < 200)
4197 break;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004198 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004199 rule_set->rsp_add[cur_idx]) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004200 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004201 }
4202
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004203 /* check whether we're already working on the frontend */
4204 if (cur_proxy == t->fe)
4205 break;
4206 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004207 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004208
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004209 /*
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004210 * We may be facing a 1xx response (100 continue, 101 switching protocols),
4211 * in which case this is not the right response, and we're waiting for the
4212 * next one. Let's allow this response to go to the client and wait for the
4213 * next one.
4214 */
4215 if (txn->status < 200) {
4216 hdr_idx_init(&txn->hdr_idx);
4217 buffer_forward(rep, rep->lr - (rep->data + msg->som));
4218 msg->msg_state = HTTP_MSG_RPBEFORE;
4219 txn->status = 0;
4220 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4221 return 1;
4222 }
4223
4224 /* we don't have any 1xx status code now */
4225
4226 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004227 * 4: check for server cookie.
4228 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004229 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4230 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004231 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004232
Willy Tarreaubaaee002006-06-26 02:48:02 +02004233
Willy Tarreaua15645d2007-03-18 16:22:39 +01004234 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004235 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004236 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004237 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004238 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004239
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004240 /*
4241 * 6: add server cookie in the response if needed
4242 */
4243 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004244 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004245 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004246
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004247 /* the server is known, it's not the one the client requested, we have to
4248 * insert a set-cookie here, except if we want to insert only on POST
4249 * requests and this one isn't. Note that servers which don't have cookies
4250 * (eg: some backup servers) will return a full cookie removal request.
4251 */
4252 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4253 t->be->cookie_name,
4254 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004255
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004256 if (t->be->cookie_domain)
4257 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004258
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004259 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004260 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004261 goto return_bad_resp;
4262 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004263
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004264 /* Here, we will tell an eventual cache on the client side that we don't
4265 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4266 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4267 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4268 */
4269 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004270
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004271 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4272
4273 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004274 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004275 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004276 }
4277 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004278
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004279 /*
4280 * 7: check if result will be cacheable with a cookie.
4281 * We'll block the response if security checks have caught
4282 * nasty things such as a cacheable cookie.
4283 */
4284 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4285 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004286 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004287
4288 /* we're in presence of a cacheable response containing
4289 * a set-cookie header. We'll block it as requested by
4290 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004291 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004292 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004293 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004294
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004295 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004296 if (t->listener->counters)
4297 t->listener->counters->denied_resp++;
4298
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004299 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4300 t->be->id, t->srv?t->srv->id:"<dispatch>");
4301 send_log(t->be, LOG_ALERT,
4302 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4303 t->be->id, t->srv?t->srv->id:"<dispatch>");
4304 goto return_srv_prx_502;
4305 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004306
4307 /*
Willy Tarreau5b154472009-12-21 20:11:07 +01004308 * 8: add "Connection: close" if needed and not yet set. This is
4309 * only needed for 1.1 responses since we know there is no other
4310 * Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004311 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004312 if (must_close && (txn->flags & TX_RES_VER_11)) {
Willy Tarreau5b154472009-12-21 20:11:07 +01004313 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004314 "Connection: close", 17) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004315 goto return_bad_resp;
Willy Tarreau5b154472009-12-21 20:11:07 +01004316 must_close = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004317 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004318 else if (must_keep && !(txn->flags & TX_REQ_VER_11)) {
4319 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
4320 "Connection: keep-alive", 22) < 0))
4321 goto return_bad_resp;
4322 must_keep = 0;
4323 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004324
Willy Tarreaud98cf932009-12-27 22:54:55 +01004325 if (txn->flags & TX_RES_XFER_LEN)
4326 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004327
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004328 /*************************************************************
4329 * OK, that's finished for the headers. We have done what we *
4330 * could. Let's switch to the DATA state. *
4331 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004332
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004333 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004334
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004335 /* if the user wants to log as soon as possible, without counting
4336 * bytes from the server, then this is the right moment. We have
4337 * to temporarily assign bytes_out to log what we currently have.
4338 */
4339 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4340 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4341 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004342 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004343 t->logs.bytes_out = 0;
4344 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004345
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004346 /* Note: we must not try to cheat by jumping directly to DATA,
4347 * otherwise we would not let the client side wake up.
4348 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004349
Willy Tarreaudafde432008-08-17 01:00:46 +02004350 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004351 }
4352 return 0;
4353}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004354
Willy Tarreaud98cf932009-12-27 22:54:55 +01004355/* This function is an analyser which forwards response body (including chunk
4356 * sizes if any). It is called as soon as we must forward, even if we forward
4357 * zero byte. The only situation where it must not be called is when we're in
4358 * tunnel mode and we want to forward till the close. It's used both to forward
4359 * remaining data and to resync after end of body. It expects the msg_state to
4360 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4361 * read more data, or 1 once we can go on with next request or end the session.
4362 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4363 * bytes of pending data + the headers if not already done (between som and sov).
4364 * It eventually adjusts som to match sov after the data in between have been sent.
4365 */
4366int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4367{
4368 struct http_txn *txn = &s->txn;
4369 struct http_msg *msg = &s->txn.rsp;
4370
Willy Tarreau21c5e4d2010-01-03 00:19:31 +01004371 if (res->flags & (BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) {
Willy Tarreau082b01c2010-01-02 23:58:04 +01004372 res->analysers &= ~an_bit;
4373 return 1;
4374 }
4375
Willy Tarreaud98cf932009-12-27 22:54:55 +01004376 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4377 return 0;
4378
Willy Tarreaub608feb2010-01-02 22:47:18 +01004379 /* note: in server-close mode, we don't want to automatically close the
4380 * output when the input is closed.
4381 */
4382 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4383 buffer_dont_close(res);
4384
Willy Tarreaud98cf932009-12-27 22:54:55 +01004385 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4386 /* we have msg->col and msg->sov which both point to the first
4387 * byte of message body. msg->som still points to the beginning
4388 * of the message. We must save the body in req->lr because it
4389 * survives buffer re-alignments.
4390 */
4391 res->lr = res->data + msg->sov;
4392 if (txn->flags & TX_RES_TE_CHNK)
4393 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4394 else {
4395 msg->msg_state = HTTP_MSG_DATA;
4396 }
4397 }
4398
Willy Tarreaud98cf932009-12-27 22:54:55 +01004399 while (1) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004400 /* we may have some data pending */
4401 if (msg->hdr_content_len || msg->som != msg->sov) {
4402 int bytes = msg->sov - msg->som;
4403 if (bytes < 0) /* sov may have wrapped at the end */
4404 bytes += res->size;
4405 buffer_forward(res, bytes + msg->hdr_content_len);
4406 msg->hdr_content_len = 0; /* don't forward that again */
4407 msg->som = msg->sov;
4408 }
4409
Willy Tarreaud98cf932009-12-27 22:54:55 +01004410 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
4411 /* read the chunk size and assign it to ->hdr_content_len, then
4412 * set ->sov to point to the body and switch to DATA or TRAILERS state.
4413 */
4414 int ret = http_parse_chunk_size(res, msg);
4415
4416 if (!ret)
4417 goto missing_data;
4418 else if (ret < 0)
4419 goto return_bad_res;
4420 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004421 }
4422 else if (msg->msg_state == HTTP_MSG_DATA) {
4423 /* must still forward */
4424 if (res->to_forward)
4425 return 0;
4426
4427 /* nothing left to forward */
4428 if (txn->flags & TX_RES_TE_CHNK)
4429 msg->msg_state = HTTP_MSG_DATA_CRLF;
4430 else {
4431 msg->msg_state = HTTP_MSG_DONE;
4432 }
4433 }
4434 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4435 /* we want the CRLF after the data */
4436 int ret;
4437
4438 if (!(res->flags & BF_OUT_EMPTY))
4439 return 0;
4440 /* The output pointer does not move anymore, next unsent data
4441 * are available at ->w. Let's save that.
4442 */
4443 res->lr = res->w;
4444 ret = http_skip_chunk_crlf(res, msg);
4445
4446 if (!ret)
4447 goto missing_data;
4448 else if (ret < 0)
4449 goto return_bad_res;
4450 /* we're in MSG_CHUNK_SIZE now */
4451 }
4452 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4453 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01004454
Willy Tarreaud98cf932009-12-27 22:54:55 +01004455 if (ret == 0)
4456 goto missing_data;
4457 else if (ret < 0)
4458 goto return_bad_res;
4459 /* we're in HTTP_MSG_DONE now */
4460 }
4461 else if (msg->msg_state == HTTP_MSG_DONE) {
Willy Tarreau5523b322009-12-29 12:05:52 +01004462 /* In theory, we don't need to read anymore, but we must
4463 * still monitor the server connection for a possible close,
4464 * so we don't set the BF_DONT_READ flag here.
4465 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004466
Willy Tarreau5523b322009-12-29 12:05:52 +01004467 if (txn->req.msg_state < HTTP_MSG_DONE && txn->req.msg_state != HTTP_MSG_ERROR) {
4468 /* The client seems to still be sending data, probably
4469 * because we got an error response during an upload.
4470 * We have the choice of either breaking the connection
4471 * or letting it pass through. Let's do the later.
4472 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004473 return 0;
Willy Tarreau5523b322009-12-29 12:05:52 +01004474 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004475
4476 /* when we support keep-alive or server-close modes, we'll have
4477 * to reset the transaction here.
4478 */
Willy Tarreau5523b322009-12-29 12:05:52 +01004479
Willy Tarreau82eeaf22009-12-29 12:09:05 +01004480 if ((s->fe->options | s->be->options) & PR_O_FORCE_CLO) {
4481 /* option forceclose is set, let's enforce it now that the transfer is complete. */
4482 buffer_abort(res);
4483 }
Willy Tarreaub608feb2010-01-02 22:47:18 +01004484 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4485 /* server close is handled entirely on the req analyser */
4486 s->req->cons->flags |= SI_FL_NOLINGER;
4487 buffer_shutw_now(s->req);
4488 }
Willy Tarreau82eeaf22009-12-29 12:09:05 +01004489
Willy Tarreau5523b322009-12-29 12:05:52 +01004490 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4491 if (res->flags & BF_OUT_EMPTY)
4492 msg->msg_state = HTTP_MSG_CLOSED;
4493 else
4494 msg->msg_state = HTTP_MSG_CLOSING;
4495 }
4496 else {
4497 /* for other modes, we let further responses pass for now */
4498 res->flags &= ~BF_DONT_READ;
4499 /* FIXME: we're still forced to do that here */
4500 s->req->flags &= ~BF_DONT_READ;
4501 break;
4502 }
4503 }
4504 else if (msg->msg_state == HTTP_MSG_CLOSING) {
4505 /* nothing else to forward, just waiting for the buffer to be empty */
4506 if (!(res->flags & BF_OUT_EMPTY))
4507 return 0;
4508 msg->msg_state = HTTP_MSG_CLOSED;
4509 }
4510 else if (msg->msg_state == HTTP_MSG_CLOSED) {
4511 res->flags &= ~BF_DONT_READ;
4512 /* FIXME: we're still forced to do that here */
4513 s->req->flags &= ~BF_DONT_READ;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004514 break;
4515 }
4516 }
4517
4518 res->analysers &= ~an_bit;
4519 return 1;
4520
4521 missing_data:
4522 /* forward the chunk size as well as any pending data */
4523 if (msg->hdr_content_len || msg->som != msg->sov) {
4524 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
4525 msg->hdr_content_len = 0; /* don't forward that again */
4526 msg->som = msg->sov;
4527 }
4528
4529 if (res->flags & BF_FULL)
4530 goto return_bad_res;
4531 /* the session handler will take care of timeouts and errors */
4532 return 0;
4533
4534 return_bad_res: /* let's centralize all bad resuests */
4535 txn->rsp.msg_state = HTTP_MSG_ERROR;
4536 txn->status = 502;
4537 stream_int_cond_close(res->cons, NULL);
4538
4539 res->analysers = 0;
4540 s->be->counters.failed_resp++;
4541 if (s->srv) {
4542 s->srv->counters.failed_resp++;
4543 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4544 }
4545
4546 if (!(s->flags & SN_ERR_MASK))
4547 s->flags |= SN_ERR_PRXCOND;
4548 if (!(s->flags & SN_FINST_MASK))
4549 s->flags |= SN_FINST_R;
4550 return 0;
4551}
4552
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004553/* Iterate the same filter through all request headers.
4554 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004555 * Since it can manage the switch to another backend, it updates the per-proxy
4556 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004557 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004558int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01004559{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004560 char term;
4561 char *cur_ptr, *cur_end, *cur_next;
4562 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004563 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004564 struct hdr_idx_elem *cur_hdr;
4565 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01004566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004567 last_hdr = 0;
4568
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004569 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004570 old_idx = 0;
4571
4572 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01004573 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004574 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004575 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004576 (exp->action == ACT_ALLOW ||
4577 exp->action == ACT_DENY ||
4578 exp->action == ACT_TARPIT))
4579 return 0;
4580
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004581 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004582 if (!cur_idx)
4583 break;
4584
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004585 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004586 cur_ptr = cur_next;
4587 cur_end = cur_ptr + cur_hdr->len;
4588 cur_next = cur_end + cur_hdr->cr + 1;
4589
4590 /* Now we have one header between cur_ptr and cur_end,
4591 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004592 */
4593
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004594 /* The annoying part is that pattern matching needs
4595 * that we modify the contents to null-terminate all
4596 * strings before testing them.
4597 */
4598
4599 term = *cur_end;
4600 *cur_end = '\0';
4601
4602 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4603 switch (exp->action) {
4604 case ACT_SETBE:
4605 /* It is not possible to jump a second time.
4606 * FIXME: should we return an HTTP/500 here so that
4607 * the admin knows there's a problem ?
4608 */
4609 if (t->be != t->fe)
4610 break;
4611
4612 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004613 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004614 last_hdr = 1;
4615 break;
4616
4617 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004618 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004619 last_hdr = 1;
4620 break;
4621
4622 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004623 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004624 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004625
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004626 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004627 if (t->listener->counters)
4628 t->listener->counters->denied_resp++;
4629
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004630 break;
4631
4632 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004633 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004634 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004635
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004636 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004637 if (t->listener->counters)
4638 t->listener->counters->denied_resp++;
4639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004640 break;
4641
4642 case ACT_REPLACE:
4643 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4644 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4645 /* FIXME: if the user adds a newline in the replacement, the
4646 * index will not be recalculated for now, and the new line
4647 * will not be counted as a new header.
4648 */
4649
4650 cur_end += delta;
4651 cur_next += delta;
4652 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01004653 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004654 break;
4655
4656 case ACT_REMOVE:
4657 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
4658 cur_next += delta;
4659
Willy Tarreaufa355d42009-11-29 18:12:29 +01004660 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004661 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
4662 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004663 cur_hdr->len = 0;
4664 cur_end = NULL; /* null-term has been rewritten */
4665 break;
4666
4667 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004668 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004669 if (cur_end)
4670 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004671
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004672 /* keep the link from this header to next one in case of later
4673 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004674 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004675 old_idx = cur_idx;
4676 }
4677 return 0;
4678}
4679
4680
4681/* Apply the filter to the request line.
4682 * Returns 0 if nothing has been done, 1 if the filter has been applied,
4683 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004684 * Since it can manage the switch to another backend, it updates the per-proxy
4685 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004686 */
4687int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
4688{
4689 char term;
4690 char *cur_ptr, *cur_end;
4691 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004692 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004693 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004694
Willy Tarreau58f10d72006-12-04 02:26:12 +01004695
Willy Tarreau3d300592007-03-18 18:34:41 +01004696 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004697 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01004698 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004699 (exp->action == ACT_ALLOW ||
4700 exp->action == ACT_DENY ||
4701 exp->action == ACT_TARPIT))
4702 return 0;
4703 else if (exp->action == ACT_REMOVE)
4704 return 0;
4705
4706 done = 0;
4707
Willy Tarreau9cdde232007-05-02 20:58:19 +02004708 cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004709 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004710
4711 /* Now we have the request line between cur_ptr and cur_end */
4712
4713 /* The annoying part is that pattern matching needs
4714 * that we modify the contents to null-terminate all
4715 * strings before testing them.
4716 */
4717
4718 term = *cur_end;
4719 *cur_end = '\0';
4720
4721 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
4722 switch (exp->action) {
4723 case ACT_SETBE:
4724 /* It is not possible to jump a second time.
4725 * FIXME: should we return an HTTP/500 here so that
4726 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01004727 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004728 if (t->be != t->fe)
4729 break;
4730
4731 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004732 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004733 done = 1;
4734 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004735
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004736 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01004737 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004738 done = 1;
4739 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004740
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004741 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01004742 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004743
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004744 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004745 if (t->listener->counters)
4746 t->listener->counters->denied_resp++;
4747
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004748 done = 1;
4749 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004751 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01004752 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004753
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004754 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004755 if (t->listener->counters)
4756 t->listener->counters->denied_resp++;
4757
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004758 done = 1;
4759 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01004760
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004761 case ACT_REPLACE:
4762 *cur_end = term; /* restore the string terminator */
4763 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
4764 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
4765 /* FIXME: if the user adds a newline in the replacement, the
4766 * index will not be recalculated for now, and the new line
4767 * will not be counted as a new header.
4768 */
Willy Tarreaua496b602006-12-17 23:15:24 +01004769
Willy Tarreaufa355d42009-11-29 18:12:29 +01004770 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004771 cur_end += delta;
Willy Tarreaua496b602006-12-17 23:15:24 +01004772
Willy Tarreau9cdde232007-05-02 20:58:19 +02004773 txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004774 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004775 HTTP_MSG_RQMETH,
4776 cur_ptr, cur_end + 1,
4777 NULL, NULL);
4778 if (unlikely(!cur_end))
4779 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01004780
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004781 /* we have a full request and we know that we have either a CR
4782 * or an LF at <ptr>.
4783 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004784 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
4785 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004786 /* there is no point trying this regex on headers */
4787 return 1;
4788 }
4789 }
4790 *cur_end = term; /* restore the string terminator */
4791 return done;
4792}
Willy Tarreau97de6242006-12-27 17:18:38 +01004793
Willy Tarreau58f10d72006-12-04 02:26:12 +01004794
Willy Tarreau58f10d72006-12-04 02:26:12 +01004795
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004796/*
4797 * Apply all the req filters <exp> to all headers in buffer <req> of session <t>.
4798 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01004799 * unparsable request. Since it can manage the switch to another backend, it
4800 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004801 */
4802int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp)
4803{
Willy Tarreau3d300592007-03-18 18:34:41 +01004804 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004805 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01004806 while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004807 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004808
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004809 /*
4810 * The interleaving of transformations and verdicts
4811 * makes it difficult to decide to continue or stop
4812 * the evaluation.
4813 */
4814
Willy Tarreau3d300592007-03-18 18:34:41 +01004815 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004816 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
4817 exp->action == ACT_TARPIT || exp->action == ACT_PASS)) {
4818 exp = exp->next;
4819 continue;
4820 }
4821
4822 /* Apply the filter to the request line. */
4823 ret = apply_filter_to_req_line(t, req, exp);
4824 if (unlikely(ret < 0))
4825 return -1;
4826
4827 if (likely(ret == 0)) {
4828 /* The filter did not match the request, it can be
4829 * iterated through all headers.
4830 */
4831 apply_filter_to_req_headers(t, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004832 }
4833 exp = exp->next;
4834 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004835 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004836}
4837
4838
Willy Tarreaua15645d2007-03-18 16:22:39 +01004839
Willy Tarreau58f10d72006-12-04 02:26:12 +01004840/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004841 * Try to retrieve the server associated to the appsession.
4842 * If the server is found, it's assigned to the session.
4843 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01004844void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004845 struct http_txn *txn = &t->txn;
4846 appsess *asession = NULL;
4847 char *sessid_temp = NULL;
4848
Cyril Bontéb21570a2009-11-29 20:04:48 +01004849 if (len > t->be->appsession_len) {
4850 len = t->be->appsession_len;
4851 }
4852
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004853 if (t->be->options2 & PR_O2_AS_REQL) {
4854 /* request-learn option is enabled : store the sessid in the session for future use */
4855 if (t->sessid != NULL) {
4856 /* free previously allocated memory as we don't need the session id found in the URL anymore */
4857 pool_free2(apools.sessid, t->sessid);
4858 }
4859
4860 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
4861 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4862 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4863 return;
4864 }
4865
Cyril Bontéb21570a2009-11-29 20:04:48 +01004866 memcpy(t->sessid, buf, len);
4867 t->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004868 }
4869
4870 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
4871 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
4872 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
4873 return;
4874 }
4875
Cyril Bontéb21570a2009-11-29 20:04:48 +01004876 memcpy(sessid_temp, buf, len);
4877 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02004878
4879 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
4880 /* free previously allocated memory */
4881 pool_free2(apools.sessid, sessid_temp);
4882
4883 if (asession != NULL) {
4884 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
4885 if (!(t->be->options2 & PR_O2_AS_REQL))
4886 asession->request_count++;
4887
4888 if (asession->serverid != NULL) {
4889 struct server *srv = t->be->srv;
4890 while (srv) {
4891 if (strcmp(srv->id, asession->serverid) == 0) {
4892 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
4893 /* we found the server and it's usable */
4894 txn->flags &= ~TX_CK_MASK;
4895 txn->flags |= TX_CK_VALID;
4896 t->flags |= SN_DIRECT | SN_ASSIGNED;
4897 t->srv = srv;
4898 break;
4899 } else {
4900 txn->flags &= ~TX_CK_MASK;
4901 txn->flags |= TX_CK_DOWN;
4902 }
4903 }
4904 srv = srv->next;
4905 }
4906 }
4907 }
4908}
4909
4910/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01004911 * Manage client-side cookie. It can impact performance by about 2% so it is
4912 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004913 */
4914void manage_client_side_cookies(struct session *t, struct buffer *req)
4915{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004916 struct http_txn *txn = &t->txn;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004917 char *p1, *p2, *p3, *p4;
4918 char *del_colon, *del_cookie, *colon;
4919 int app_cookies;
4920
Willy Tarreau58f10d72006-12-04 02:26:12 +01004921 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01004922 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004923
Willy Tarreau2a324282006-12-05 00:05:46 +01004924 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01004925 * we start with the start line.
4926 */
Willy Tarreau83969f42007-01-22 08:55:47 +01004927 old_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004928 cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004929
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004930 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004931 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004932 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01004933
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01004934 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01004935 cur_ptr = cur_next;
4936 cur_end = cur_ptr + cur_hdr->len;
4937 cur_next = cur_end + cur_hdr->cr + 1;
4938
4939 /* We have one full header between cur_ptr and cur_end, and the
4940 * next header starts at cur_next. We're only interested in
4941 * "Cookie:" headers.
4942 */
4943
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004944 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
4945 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01004946 old_idx = cur_idx;
4947 continue;
4948 }
4949
4950 /* Now look for cookies. Conforming to RFC2109, we have to support
4951 * attributes whose name begin with a '$', and associate them with
4952 * the right cookie, if we want to delete this cookie.
4953 * So there are 3 cases for each cookie read :
4954 * 1) it's a special attribute, beginning with a '$' : ignore it.
4955 * 2) it's a server id cookie that we *MAY* want to delete : save
4956 * some pointers on it (last semi-colon, beginning of cookie...)
4957 * 3) it's an application cookie : we *MAY* have to delete a previous
4958 * "special" cookie.
4959 * At the end of loop, if a "special" cookie remains, we may have to
4960 * remove it. If no application cookie persists in the header, we
4961 * *MUST* delete it
4962 */
4963
Willy Tarreauaa9dce32007-03-18 23:50:16 +01004964 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01004965
Willy Tarreau58f10d72006-12-04 02:26:12 +01004966 /* del_cookie == NULL => nothing to be deleted */
4967 del_colon = del_cookie = NULL;
4968 app_cookies = 0;
4969
4970 while (p1 < cur_end) {
4971 /* skip spaces and colons, but keep an eye on these ones */
4972 while (p1 < cur_end) {
4973 if (*p1 == ';' || *p1 == ',')
4974 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004975 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01004976 break;
4977 p1++;
4978 }
4979
4980 if (p1 == cur_end)
4981 break;
4982
4983 /* p1 is at the beginning of the cookie name */
4984 p2 = p1;
4985 while (p2 < cur_end && *p2 != '=')
4986 p2++;
4987
4988 if (p2 == cur_end)
4989 break;
4990
4991 p3 = p2 + 1; /* skips the '=' sign */
4992 if (p3 == cur_end)
4993 break;
4994
4995 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02004996 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',')
Willy Tarreau58f10d72006-12-04 02:26:12 +01004997 p4++;
4998
4999 /* here, we have the cookie name between p1 and p2,
5000 * and its value between p3 and p4.
5001 * we can process it :
5002 *
5003 * Cookie: NAME=VALUE;
5004 * | || || |
5005 * | || || +--> p4
5006 * | || |+-------> p3
5007 * | || +--------> p2
5008 * | |+------------> p1
5009 * | +-------------> colon
5010 * +--------------------> cur_ptr
5011 */
5012
5013 if (*p1 == '$') {
5014 /* skip this one */
5015 }
5016 else {
5017 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005018 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005019 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005020 (p4 - p1 >= t->fe->capture_namelen) &&
5021 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005022 int log_len = p4 - p1;
5023
Willy Tarreau086b3b42007-05-13 21:45:51 +02005024 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005025 Alert("HTTP logging : out of memory.\n");
5026 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005027 if (log_len > t->fe->capture_len)
5028 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005029 memcpy(txn->cli_cookie, p1, log_len);
5030 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005031 }
5032 }
5033
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005034 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5035 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005036 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005037 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005038 char *delim;
5039
5040 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5041 * have the server ID betweek p3 and delim, and the original cookie between
5042 * delim+1 and p4. Otherwise, delim==p4 :
5043 *
5044 * Cookie: NAME=SRV~VALUE;
5045 * | || || | |
5046 * | || || | +--> p4
5047 * | || || +--------> delim
5048 * | || |+-----------> p3
5049 * | || +------------> p2
5050 * | |+----------------> p1
5051 * | +-----------------> colon
5052 * +------------------------> cur_ptr
5053 */
5054
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005055 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005056 for (delim = p3; delim < p4; delim++)
5057 if (*delim == COOKIE_DELIM)
5058 break;
5059 }
5060 else
5061 delim = p4;
5062
5063
5064 /* Here, we'll look for the first running server which supports the cookie.
5065 * This allows to share a same cookie between several servers, for example
5066 * to dedicate backup servers to specific servers only.
5067 * However, to prevent clients from sticking to cookie-less backup server
5068 * when they have incidentely learned an empty cookie, we simply ignore
5069 * empty cookies and mark them as invalid.
5070 */
5071 if (delim == p3)
5072 srv = NULL;
5073
5074 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005075 if (srv->cookie && (srv->cklen == delim - p3) &&
5076 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005077 if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005078 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005079 txn->flags &= ~TX_CK_MASK;
5080 txn->flags |= TX_CK_VALID;
5081 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005082 t->srv = srv;
5083 break;
5084 } else {
5085 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005086 txn->flags &= ~TX_CK_MASK;
5087 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005088 }
5089 }
5090 srv = srv->next;
5091 }
5092
Willy Tarreau3d300592007-03-18 18:34:41 +01005093 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005094 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005095 txn->flags &= ~TX_CK_MASK;
5096 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005097 }
5098
5099 /* depending on the cookie mode, we may have to either :
5100 * - delete the complete cookie if we're in insert+indirect mode, so that
5101 * the server never sees it ;
5102 * - remove the server id from the cookie value, and tag the cookie as an
5103 * application cookie so that it does not get accidentely removed later,
5104 * if we're in cookie prefix mode
5105 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005106 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005107 int delta; /* negative */
5108
5109 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5110 p4 += delta;
5111 cur_end += delta;
5112 cur_next += delta;
5113 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005114 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005115
5116 del_cookie = del_colon = NULL;
5117 app_cookies++; /* protect the header from deletion */
5118 }
5119 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005120 (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 +01005121 del_cookie = p1;
5122 del_colon = colon;
5123 }
5124 } else {
5125 /* now we know that we must keep this cookie since it's
5126 * not ours. But if we wanted to delete our cookie
5127 * earlier, we cannot remove the complete header, but we
5128 * can remove the previous block itself.
5129 */
5130 app_cookies++;
5131
5132 if (del_cookie != NULL) {
5133 int delta; /* negative */
5134
5135 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5136 p4 += delta;
5137 cur_end += delta;
5138 cur_next += delta;
5139 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005140 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005141 del_cookie = del_colon = NULL;
5142 }
5143 }
5144
Cyril Bontéb21570a2009-11-29 20:04:48 +01005145 if (t->be->appsession_name != NULL) {
5146 int cmp_len, value_len;
5147 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005148
Cyril Bontéb21570a2009-11-29 20:04:48 +01005149 if (t->be->options2 & PR_O2_AS_PFX) {
5150 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5151 value_begin = p1 + t->be->appsession_name_len;
5152 value_len = p4 - p1 - t->be->appsession_name_len;
5153 } else {
5154 cmp_len = p2 - p1;
5155 value_begin = p3;
5156 value_len = p4 - p3;
5157 }
5158
5159 /* let's see if the cookie is our appcookie */
5160 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5161 /* Cool... it's the right one */
5162 manage_client_side_appsession(t, value_begin, value_len);
5163 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005164#if defined(DEBUG_HASH)
5165 Alert("manage_client_side_cookies\n");
5166 appsession_hash_dump(&(t->be->htbl_proxy));
5167#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005168 }/* end if ((t->proxy->appsession_name != NULL) ... */
5169 }
5170
5171 /* we'll have to look for another cookie ... */
5172 p1 = p4;
5173 } /* while (p1 < cur_end) */
5174
5175 /* There's no more cookie on this line.
5176 * We may have marked the last one(s) for deletion.
5177 * We must do this now in two ways :
5178 * - if there is no app cookie, we simply delete the header ;
5179 * - if there are app cookies, we must delete the end of the
5180 * string properly, including the colon/semi-colon before
5181 * the cookie name.
5182 */
5183 if (del_cookie != NULL) {
5184 int delta;
5185 if (app_cookies) {
5186 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5187 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005188 cur_hdr->len += delta;
5189 } else {
5190 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005191
5192 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005193 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5194 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005195 cur_hdr->len = 0;
5196 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005197 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005198 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005199 }
5200
5201 /* keep the link from this header to next one */
5202 old_idx = cur_idx;
5203 } /* end of cookie processing on this header */
5204}
5205
5206
Willy Tarreaua15645d2007-03-18 16:22:39 +01005207/* Iterate the same filter through all response headers contained in <rtr>.
5208 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5209 */
5210int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5211{
5212 char term;
5213 char *cur_ptr, *cur_end, *cur_next;
5214 int cur_idx, old_idx, last_hdr;
5215 struct http_txn *txn = &t->txn;
5216 struct hdr_idx_elem *cur_hdr;
5217 int len, delta;
5218
5219 last_hdr = 0;
5220
5221 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5222 old_idx = 0;
5223
5224 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005225 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005226 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005227 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005228 (exp->action == ACT_ALLOW ||
5229 exp->action == ACT_DENY))
5230 return 0;
5231
5232 cur_idx = txn->hdr_idx.v[old_idx].next;
5233 if (!cur_idx)
5234 break;
5235
5236 cur_hdr = &txn->hdr_idx.v[cur_idx];
5237 cur_ptr = cur_next;
5238 cur_end = cur_ptr + cur_hdr->len;
5239 cur_next = cur_end + cur_hdr->cr + 1;
5240
5241 /* Now we have one header between cur_ptr and cur_end,
5242 * and the next header starts at cur_next.
5243 */
5244
5245 /* The annoying part is that pattern matching needs
5246 * that we modify the contents to null-terminate all
5247 * strings before testing them.
5248 */
5249
5250 term = *cur_end;
5251 *cur_end = '\0';
5252
5253 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5254 switch (exp->action) {
5255 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005256 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005257 last_hdr = 1;
5258 break;
5259
5260 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005261 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005262 last_hdr = 1;
5263 break;
5264
5265 case ACT_REPLACE:
5266 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5267 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5268 /* FIXME: if the user adds a newline in the replacement, the
5269 * index will not be recalculated for now, and the new line
5270 * will not be counted as a new header.
5271 */
5272
5273 cur_end += delta;
5274 cur_next += delta;
5275 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005276 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005277 break;
5278
5279 case ACT_REMOVE:
5280 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5281 cur_next += delta;
5282
Willy Tarreaufa355d42009-11-29 18:12:29 +01005283 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005284 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5285 txn->hdr_idx.used--;
5286 cur_hdr->len = 0;
5287 cur_end = NULL; /* null-term has been rewritten */
5288 break;
5289
5290 }
5291 }
5292 if (cur_end)
5293 *cur_end = term; /* restore the string terminator */
5294
5295 /* keep the link from this header to next one in case of later
5296 * removal of next header.
5297 */
5298 old_idx = cur_idx;
5299 }
5300 return 0;
5301}
5302
5303
5304/* Apply the filter to the status line in the response buffer <rtr>.
5305 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5306 * or -1 if a replacement resulted in an invalid status line.
5307 */
5308int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5309{
5310 char term;
5311 char *cur_ptr, *cur_end;
5312 int done;
5313 struct http_txn *txn = &t->txn;
5314 int len, delta;
5315
5316
Willy Tarreau3d300592007-03-18 18:34:41 +01005317 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005318 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005319 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005320 (exp->action == ACT_ALLOW ||
5321 exp->action == ACT_DENY))
5322 return 0;
5323 else if (exp->action == ACT_REMOVE)
5324 return 0;
5325
5326 done = 0;
5327
Willy Tarreau9cdde232007-05-02 20:58:19 +02005328 cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005329 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5330
5331 /* Now we have the status line between cur_ptr and cur_end */
5332
5333 /* The annoying part is that pattern matching needs
5334 * that we modify the contents to null-terminate all
5335 * strings before testing them.
5336 */
5337
5338 term = *cur_end;
5339 *cur_end = '\0';
5340
5341 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5342 switch (exp->action) {
5343 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005344 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005345 done = 1;
5346 break;
5347
5348 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005349 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005350 done = 1;
5351 break;
5352
5353 case ACT_REPLACE:
5354 *cur_end = term; /* restore the string terminator */
5355 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5356 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5357 /* FIXME: if the user adds a newline in the replacement, the
5358 * index will not be recalculated for now, and the new line
5359 * will not be counted as a new header.
5360 */
5361
Willy Tarreaufa355d42009-11-29 18:12:29 +01005362 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005363 cur_end += delta;
5364
Willy Tarreau9cdde232007-05-02 20:58:19 +02005365 txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005366 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005367 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005368 cur_ptr, cur_end + 1,
5369 NULL, NULL);
5370 if (unlikely(!cur_end))
5371 return -1;
5372
5373 /* we have a full respnse and we know that we have either a CR
5374 * or an LF at <ptr>.
5375 */
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005376 txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005377 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
5378 /* there is no point trying this regex on headers */
5379 return 1;
5380 }
5381 }
5382 *cur_end = term; /* restore the string terminator */
5383 return done;
5384}
5385
5386
5387
5388/*
5389 * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>.
5390 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
5391 * unparsable response.
5392 */
5393int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5394{
Willy Tarreau3d300592007-03-18 18:34:41 +01005395 struct http_txn *txn = &t->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005396 /* iterate through the filters in the outer loop */
Willy Tarreau3d300592007-03-18 18:34:41 +01005397 while (exp && !(txn->flags & TX_SVDENY)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005398 int ret;
5399
5400 /*
5401 * The interleaving of transformations and verdicts
5402 * makes it difficult to decide to continue or stop
5403 * the evaluation.
5404 */
5405
Willy Tarreau3d300592007-03-18 18:34:41 +01005406 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005407 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
5408 exp->action == ACT_PASS)) {
5409 exp = exp->next;
5410 continue;
5411 }
5412
5413 /* Apply the filter to the status line. */
5414 ret = apply_filter_to_sts_line(t, rtr, exp);
5415 if (unlikely(ret < 0))
5416 return -1;
5417
5418 if (likely(ret == 0)) {
5419 /* The filter did not match the response, it can be
5420 * iterated through all headers.
5421 */
5422 apply_filter_to_resp_headers(t, rtr, exp);
5423 }
5424 exp = exp->next;
5425 }
5426 return 0;
5427}
5428
5429
5430
5431/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005432 * Manage server-side cookies. It can impact performance by about 2% so it is
5433 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005434 */
5435void manage_server_side_cookies(struct session *t, struct buffer *rtr)
5436{
5437 struct http_txn *txn = &t->txn;
5438 char *p1, *p2, *p3, *p4;
5439
Willy Tarreaua15645d2007-03-18 16:22:39 +01005440 char *cur_ptr, *cur_end, *cur_next;
5441 int cur_idx, old_idx, delta;
5442
Willy Tarreaua15645d2007-03-18 16:22:39 +01005443 /* Iterate through the headers.
5444 * we start with the start line.
5445 */
5446 old_idx = 0;
5447 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5448
5449 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
5450 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005451 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005452
5453 cur_hdr = &txn->hdr_idx.v[cur_idx];
5454 cur_ptr = cur_next;
5455 cur_end = cur_ptr + cur_hdr->len;
5456 cur_next = cur_end + cur_hdr->cr + 1;
5457
5458 /* We have one full header between cur_ptr and cur_end, and the
5459 * next header starts at cur_next. We're only interested in
5460 * "Cookie:" headers.
5461 */
5462
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005463 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
5464 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005465 old_idx = cur_idx;
5466 continue;
5467 }
5468
5469 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01005470 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005471
5472
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005473 /* maybe we only wanted to see if there was a set-cookie. Note that
5474 * the cookie capture is declared in the fronend.
5475 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005476 if (t->be->cookie_name == NULL &&
5477 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005478 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005479 return;
5480
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005481 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005482
5483 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005484 if (p1 == cur_end || *p1 == ';') /* end of cookie */
5485 break;
5486
5487 /* p1 is at the beginning of the cookie name */
5488 p2 = p1;
5489
5490 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
5491 p2++;
5492
5493 if (p2 == cur_end || *p2 == ';') /* next cookie */
5494 break;
5495
5496 p3 = p2 + 1; /* skip the '=' sign */
5497 if (p3 == cur_end)
5498 break;
5499
5500 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005501 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01005502 p4++;
5503
5504 /* here, we have the cookie name between p1 and p2,
5505 * and its value between p3 and p4.
5506 * we can process it.
5507 */
5508
5509 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005510 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005511 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005512 (p4 - p1 >= t->fe->capture_namelen) &&
5513 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005514 int log_len = p4 - p1;
5515
Willy Tarreau086b3b42007-05-13 21:45:51 +02005516 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005517 Alert("HTTP logging : out of memory.\n");
5518 }
5519
Willy Tarreaufd39dda2008-10-17 12:01:58 +02005520 if (log_len > t->fe->capture_len)
5521 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005522 memcpy(txn->srv_cookie, p1, log_len);
5523 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005524 }
5525
5526 /* now check if we need to process it for persistence */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005527 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5528 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005529 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01005530 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005531
5532 /* If the cookie is in insert mode on a known server, we'll delete
5533 * this occurrence because we'll insert another one later.
5534 * We'll delete it too if the "indirect" option is set and we're in
5535 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005536 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
5537 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005538 /* this header must be deleted */
5539 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5540 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5541 txn->hdr_idx.used--;
5542 cur_hdr->len = 0;
5543 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005544 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005545
Willy Tarreau3d300592007-03-18 18:34:41 +01005546 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005547 }
5548 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005549 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005550 /* replace bytes p3->p4 with the cookie name associated
5551 * with this server since we know it.
5552 */
5553 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
5554 cur_hdr->len += delta;
5555 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005556 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005557
Willy Tarreau3d300592007-03-18 18:34:41 +01005558 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005559 }
5560 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005561 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01005562 /* insert the cookie name associated with this server
5563 * before existing cookie, and insert a delimitor between them..
5564 */
5565 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
5566 cur_hdr->len += delta;
5567 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005568 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005569
5570 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01005571 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005572 }
5573 }
5574 /* next, let's see if the cookie is our appcookie */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005575 else if (t->be->appsession_name != NULL) {
5576 int cmp_len, value_len;
5577 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005578
Cyril Bontéb21570a2009-11-29 20:04:48 +01005579 if (t->be->options2 & PR_O2_AS_PFX) {
5580 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5581 value_begin = p1 + t->be->appsession_name_len;
5582 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
5583 } else {
5584 cmp_len = p2 - p1;
5585 value_begin = p3;
5586 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005587 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005588
5589 if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) {
5590 /* Cool... it's the right one */
5591 if (t->sessid != NULL) {
5592 /* free previously allocated memory as we don't need it anymore */
5593 pool_free2(apools.sessid, t->sessid);
5594 }
5595 /* Store the sessid in the session for future use */
5596 if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) {
5597 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5598 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5599 return;
5600 }
5601 memcpy(t->sessid, value_begin, value_len);
5602 t->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005603 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01005604 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005605 break; /* we don't want to loop again since there cannot be another cookie on the same line */
5606 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005607 /* keep the link from this header to next one */
5608 old_idx = cur_idx;
5609 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005610
5611 if (t->sessid != NULL) {
5612 appsess *asession = NULL;
5613 /* only do insert, if lookup fails */
5614 asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid);
5615 if (asession == NULL) {
5616 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
5617 Alert("Not enough Memory process_srv():asession:calloc().\n");
5618 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
5619 return;
5620 }
5621 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
5622 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
5623 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
5624 return;
5625 }
5626 memcpy(asession->sessid, t->sessid, t->be->appsession_len);
5627 asession->sessid[t->be->appsession_len] = 0;
5628
5629 size_t server_id_len = strlen(t->srv->id) + 1;
5630 if ((asession->serverid = pool_alloc2(apools.serverid)) == 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 asession->serverid[0] = '\0';
5636 memcpy(asession->serverid, t->srv->id, server_id_len);
5637
5638 asession->request_count = 0;
5639 appsession_hash_insert(&(t->be->htbl_proxy), asession);
5640 }
5641
5642 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5643 asession->request_count++;
5644 }
5645
5646#if defined(DEBUG_HASH)
5647 Alert("manage_server_side_cookies\n");
5648 appsession_hash_dump(&(t->be->htbl_proxy));
5649#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01005650}
5651
5652
5653
5654/*
5655 * Check if response is cacheable or not. Updates t->flags.
5656 */
5657void check_response_for_cacheability(struct session *t, struct buffer *rtr)
5658{
5659 struct http_txn *txn = &t->txn;
5660 char *p1, *p2;
5661
5662 char *cur_ptr, *cur_end, *cur_next;
5663 int cur_idx;
5664
Willy Tarreau5df51872007-11-25 16:20:08 +01005665 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005666 return;
5667
5668 /* Iterate through the headers.
5669 * we start with the start line.
5670 */
5671 cur_idx = 0;
5672 cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx);
5673
5674 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5675 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005676 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005677
5678 cur_hdr = &txn->hdr_idx.v[cur_idx];
5679 cur_ptr = cur_next;
5680 cur_end = cur_ptr + cur_hdr->len;
5681 cur_next = cur_end + cur_hdr->cr + 1;
5682
5683 /* We have one full header between cur_ptr and cur_end, and the
5684 * next header starts at cur_next. We're only interested in
5685 * "Cookie:" headers.
5686 */
5687
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005688 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
5689 if (val) {
5690 if ((cur_end - (cur_ptr + val) >= 8) &&
5691 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
5692 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5693 return;
5694 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005695 }
5696
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005697 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
5698 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01005699 continue;
5700
5701 /* OK, right now we know we have a cache-control header at cur_ptr */
5702
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005703 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005704
5705 if (p1 >= cur_end) /* no more info */
5706 continue;
5707
5708 /* p1 is at the beginning of the value */
5709 p2 = p1;
5710
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005711 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005712 p2++;
5713
5714 /* we have a complete value between p1 and p2 */
5715 if (p2 < cur_end && *p2 == '=') {
5716 /* we have something of the form no-cache="set-cookie" */
5717 if ((cur_end - p1 >= 21) &&
5718 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
5719 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01005720 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005721 continue;
5722 }
5723
5724 /* OK, so we know that either p2 points to the end of string or to a comma */
5725 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
5726 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
5727 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
5728 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005729 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005730 return;
5731 }
5732
5733 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005734 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005735 continue;
5736 }
5737 }
5738}
5739
5740
Willy Tarreau58f10d72006-12-04 02:26:12 +01005741/*
5742 * Try to retrieve a known appsession in the URI, then the associated server.
5743 * If the server is found, it's assigned to the session.
5744 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005745void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005746{
Cyril Bontéb21570a2009-11-29 20:04:48 +01005747 char *end_params, *first_param, *cur_param, *next_param;
5748 char separator;
5749 int value_len;
5750
5751 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005752
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005753 if (t->be->appsession_name == NULL ||
Cyril Bontéb21570a2009-11-29 20:04:48 +01005754 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005755 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005756 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005757
Cyril Bontéb21570a2009-11-29 20:04:48 +01005758 first_param = NULL;
5759 switch (mode) {
5760 case PR_O2_AS_M_PP:
5761 first_param = memchr(begin, ';', len);
5762 break;
5763 case PR_O2_AS_M_QS:
5764 first_param = memchr(begin, '?', len);
5765 break;
5766 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005767
Cyril Bontéb21570a2009-11-29 20:04:48 +01005768 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005769 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01005770 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005771
Cyril Bontéb21570a2009-11-29 20:04:48 +01005772 switch (mode) {
5773 case PR_O2_AS_M_PP:
5774 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
5775 end_params = (char *) begin + len;
5776 }
5777 separator = ';';
5778 break;
5779 case PR_O2_AS_M_QS:
5780 end_params = (char *) begin + len;
5781 separator = '&';
5782 break;
5783 default:
5784 /* unknown mode, shouldn't happen */
5785 return;
5786 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005787
Cyril Bontéb21570a2009-11-29 20:04:48 +01005788 cur_param = next_param = end_params;
5789 while (cur_param > first_param) {
5790 cur_param--;
5791 if ((cur_param[0] == separator) || (cur_param == first_param)) {
5792 /* let's see if this is the appsession parameter */
5793 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
5794 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
5795 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
5796 /* Cool... it's the right one */
5797 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
5798 value_len = MIN(t->be->appsession_len, next_param - cur_param);
5799 if (value_len > 0) {
5800 manage_client_side_appsession(t, cur_param, value_len);
5801 }
5802 break;
5803 }
5804 next_param = cur_param;
5805 }
5806 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005807#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005808 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02005809 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01005810#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005811}
5812
5813
Willy Tarreaub2513902006-12-17 14:52:38 +01005814/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005815 * In a GET or HEAD request, check if the requested URI matches the stats uri
5816 * for the current backend, and if an authorization has been passed and is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01005817 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005818 * It is assumed that the request is either a HEAD or GET and that the
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005819 * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005820 * the stats I/O handler will be registered to start sending data.
Willy Tarreaub2513902006-12-17 14:52:38 +01005821 *
5822 * Returns 1 if the session's state changes, otherwise 0.
5823 */
5824int stats_check_uri_auth(struct session *t, struct proxy *backend)
5825{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005826 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01005827 struct uri_auth *uri_auth = backend->uri_auth;
5828 struct user_auth *user;
5829 int authenticated, cur_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005830 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01005831
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005832 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
5833
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005834 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005835 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01005836 return 0;
5837
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005838 h = t->req->data + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005839
Willy Tarreau0214c3a2007-01-07 13:47:30 +01005840 /* the URI is in h */
5841 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01005842 return 0;
5843
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005844 h += uri_auth->uri_len;
5845 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
5846 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005847 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005848 break;
5849 }
5850 h++;
5851 }
5852
5853 if (uri_auth->refresh) {
5854 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5855 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
5856 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005857 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02005858 break;
5859 }
5860 h++;
5861 }
5862 }
5863
Willy Tarreau55bb8452007-10-17 18:44:57 +02005864 h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len;
5865 while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
5866 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005867 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02005868 break;
5869 }
5870 h++;
5871 }
5872
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005873 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
5874
Willy Tarreaub2513902006-12-17 14:52:38 +01005875 /* we are in front of a interceptable URI. Let's check
5876 * if there's an authentication and if it's valid.
5877 */
5878 user = uri_auth->users;
5879 if (!user) {
5880 /* no user auth required, it's OK */
5881 authenticated = 1;
5882 } else {
5883 authenticated = 0;
5884
5885 /* a user list is defined, we have to check.
5886 * skip 21 chars for "Authorization: Basic ".
5887 */
5888
5889 /* FIXME: this should move to an earlier place */
5890 cur_idx = 0;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005891 h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx);
5892 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
5893 int len = txn->hdr_idx.v[cur_idx].len;
Willy Tarreaub2513902006-12-17 14:52:38 +01005894 if (len > 14 &&
5895 !strncasecmp("Authorization:", h, 14)) {
Krzysztof Piotr Oledzki6f61b212009-10-04 23:34:15 +02005896 chunk_initlen(&txn->auth_hdr, h, 0, len);
Willy Tarreaub2513902006-12-17 14:52:38 +01005897 break;
5898 }
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005899 h += len + txn->hdr_idx.v[cur_idx].cr + 1;
Willy Tarreaub2513902006-12-17 14:52:38 +01005900 }
5901
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005902 if (txn->auth_hdr.len < 21 ||
5903 memcmp(txn->auth_hdr.str + 14, " Basic ", 7))
Willy Tarreaub2513902006-12-17 14:52:38 +01005904 user = NULL;
5905
5906 while (user) {
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005907 if ((txn->auth_hdr.len == user->user_len + 14 + 7)
5908 && !memcmp(txn->auth_hdr.str + 14 + 7,
Willy Tarreaub2513902006-12-17 14:52:38 +01005909 user->user_pwd, user->user_len)) {
5910 authenticated = 1;
5911 break;
5912 }
5913 user = user->next;
5914 }
5915 }
5916
5917 if (!authenticated) {
Willy Tarreau0f772532006-12-23 20:51:41 +01005918 struct chunk msg;
Willy Tarreaub2513902006-12-17 14:52:38 +01005919
5920 /* no need to go further */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005921 sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm);
5922 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005923 txn->status = 401;
Willy Tarreaudded32d2008-11-30 19:48:07 +01005924 stream_int_retnclose(t->req->prod, &msg);
Willy Tarreau2df28e82008-08-17 15:20:19 +02005925 t->req->analysers = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005926 if (!(t->flags & SN_ERR_MASK))
5927 t->flags |= SN_ERR_PRXCOND;
5928 if (!(t->flags & SN_FINST_MASK))
5929 t->flags |= SN_FINST_R;
5930 return 1;
5931 }
5932
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01005933 /* The request is valid, the user is authenticated. Let's start sending
Willy Tarreaub2513902006-12-17 14:52:38 +01005934 * data.
5935 */
Willy Tarreau70089872008-06-13 21:12:51 +02005936 t->logs.tv_request = now;
Willy Tarreaub2513902006-12-17 14:52:38 +01005937 t->data_source = DATA_SRC_STATS;
5938 t->data_state = DATA_ST_INIT;
Willy Tarreau91e99932008-06-30 07:51:00 +02005939 t->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02005940 stream_int_register_handler(t->rep->prod, http_stats_io_handler);
5941 t->rep->prod->private = t;
5942 t->rep->prod->st0 = t->rep->prod->st1 = 0;
Willy Tarreaub2513902006-12-17 14:52:38 +01005943 return 1;
5944}
5945
Willy Tarreau4076a152009-04-02 15:18:36 +02005946/*
5947 * Capture a bad request or response and archive it in the proxy's structure.
5948 */
5949void http_capture_bad_message(struct error_snapshot *es, struct session *s,
5950 struct buffer *buf, struct http_msg *msg,
5951 struct proxy *other_end)
5952{
Willy Tarreau2df8d712009-05-01 11:33:17 +02005953 es->len = buf->r - (buf->data + msg->som);
5954 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02005955 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02005956 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02005957 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02005958 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02005959 es->when = date; // user-visible date
5960 es->sid = s->uniq_id;
5961 es->srv = s->srv;
5962 es->oe = other_end;
5963 es->src = s->cli_addr;
5964}
Willy Tarreaub2513902006-12-17 14:52:38 +01005965
Willy Tarreaubaaee002006-06-26 02:48:02 +02005966/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01005967 * Print a debug line with a header
5968 */
5969void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
5970{
5971 int len, max;
5972 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005973 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005974 max = end - start;
5975 UBOUND(max, sizeof(trash) - len - 1);
5976 len += strlcpy2(trash + len, start, max + 1);
5977 trash[len++] = '\n';
5978 write(1, trash, len);
5979}
5980
Willy Tarreau0937bc42009-12-22 15:03:09 +01005981/*
5982 * Initialize a new HTTP transaction for session <s>. It is assumed that all
5983 * the required fields are properly allocated and that we only need to (re)init
5984 * them. This should be used before processing any new request.
5985 */
5986void http_init_txn(struct session *s)
5987{
5988 struct http_txn *txn = &s->txn;
5989 struct proxy *fe = s->fe;
5990
5991 txn->flags = 0;
5992 txn->status = -1;
5993
5994 txn->req.sol = txn->req.eol = NULL;
5995 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
5996 txn->rsp.sol = txn->rsp.eol = NULL;
5997 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
5998 txn->req.hdr_content_len = 0LL;
5999 txn->rsp.hdr_content_len = 0LL;
6000 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6001 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
6002 chunk_reset(&txn->auth_hdr);
6003
6004 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6005 if (fe->options2 & PR_O2_REQBUG_OK)
6006 txn->req.err_pos = -1; /* let buggy requests pass */
6007
6008 if (txn->req.cap)
6009 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6010
6011 if (txn->rsp.cap)
6012 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6013
6014 if (txn->hdr_idx.v)
6015 hdr_idx_init(&txn->hdr_idx);
6016}
6017
6018/* to be used at the end of a transaction */
6019void http_end_txn(struct session *s)
6020{
6021 struct http_txn *txn = &s->txn;
6022
6023 /* these ones will have been dynamically allocated */
6024 pool_free2(pool2_requri, txn->uri);
6025 pool_free2(pool2_capture, txn->cli_cookie);
6026 pool_free2(pool2_capture, txn->srv_cookie);
6027 txn->uri = NULL;
6028 txn->srv_cookie = NULL;
6029 txn->cli_cookie = NULL;
6030}
6031
6032/* to be used at the end of a transaction to prepare a new one */
6033void http_reset_txn(struct session *s)
6034{
6035 http_end_txn(s);
6036 http_init_txn(s);
6037
6038 s->be = s->fe;
6039 s->req->analysers = s->listener->analysers;
6040 s->logs.logwait = s->fe->to_log;
6041 s->srv = s->prev_srv = s->srv_conn = NULL;
6042 s->pend_pos = NULL;
6043 s->conn_retries = s->be->conn_retries;
6044
6045 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6046
6047 s->req->rto = s->fe->timeout.client;
6048 s->req->wto = s->be->timeout.server;
6049 s->req->cto = s->be->timeout.connect;
6050
6051 s->rep->rto = s->be->timeout.server;
6052 s->rep->wto = s->fe->timeout.client;
6053 s->rep->cto = TICK_ETERNITY;
6054
6055 s->req->rex = TICK_ETERNITY;
6056 s->req->wex = TICK_ETERNITY;
6057 s->req->analyse_exp = TICK_ETERNITY;
6058 s->rep->rex = TICK_ETERNITY;
6059 s->rep->wex = TICK_ETERNITY;
6060 s->rep->analyse_exp = TICK_ETERNITY;
6061}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006062
Willy Tarreau8797c062007-05-07 00:55:35 +02006063/************************************************************************/
6064/* The code below is dedicated to ACL parsing and matching */
6065/************************************************************************/
6066
6067
6068
6069
6070/* 1. Check on METHOD
6071 * We use the pre-parsed method if it is known, and store its number as an
6072 * integer. If it is unknown, we use the pointer and the length.
6073 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006074static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006075{
6076 int len, meth;
6077
Willy Tarreauae8b7962007-06-09 23:10:04 +02006078 len = strlen(*text);
6079 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006080
6081 pattern->val.i = meth;
6082 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006083 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006084 if (!pattern->ptr.str)
6085 return 0;
6086 pattern->len = len;
6087 }
6088 return 1;
6089}
6090
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006091static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006092acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6093 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006094{
6095 int meth;
6096 struct http_txn *txn = l7;
6097
Willy Tarreaub6866442008-07-14 23:54:42 +02006098 if (!txn)
6099 return 0;
6100
Willy Tarreau655dce92009-11-08 13:10:58 +01006101 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006102 return 0;
6103
Willy Tarreau8797c062007-05-07 00:55:35 +02006104 meth = txn->meth;
6105 test->i = meth;
6106 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006107 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6108 /* ensure the indexes are not affected */
6109 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006110 test->len = txn->req.sl.rq.m_l;
6111 test->ptr = txn->req.sol;
6112 }
6113 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6114 return 1;
6115}
6116
6117static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6118{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006119 int icase;
6120
Willy Tarreau8797c062007-05-07 00:55:35 +02006121 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006122 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006123
6124 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006125 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006126
6127 /* Other method, we must compare the strings */
6128 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006129 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006130
6131 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6132 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6133 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006134 return ACL_PAT_FAIL;
6135 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006136}
6137
6138/* 2. Check on Request/Status Version
6139 * We simply compare strings here.
6140 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006141static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006142{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006143 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006144 if (!pattern->ptr.str)
6145 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006146 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006147 return 1;
6148}
6149
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006150static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006151acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6152 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006153{
6154 struct http_txn *txn = l7;
6155 char *ptr;
6156 int len;
6157
Willy Tarreaub6866442008-07-14 23:54:42 +02006158 if (!txn)
6159 return 0;
6160
Willy Tarreau655dce92009-11-08 13:10:58 +01006161 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006162 return 0;
6163
Willy Tarreau8797c062007-05-07 00:55:35 +02006164 len = txn->req.sl.rq.v_l;
6165 ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som;
6166
6167 while ((len-- > 0) && (*ptr++ != '/'));
6168 if (len <= 0)
6169 return 0;
6170
6171 test->ptr = ptr;
6172 test->len = len;
6173
6174 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6175 return 1;
6176}
6177
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006178static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006179acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6180 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006181{
6182 struct http_txn *txn = l7;
6183 char *ptr;
6184 int len;
6185
Willy Tarreaub6866442008-07-14 23:54:42 +02006186 if (!txn)
6187 return 0;
6188
Willy Tarreau655dce92009-11-08 13:10:58 +01006189 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006190 return 0;
6191
Willy Tarreau8797c062007-05-07 00:55:35 +02006192 len = txn->rsp.sl.st.v_l;
6193 ptr = txn->rsp.sol;
6194
6195 while ((len-- > 0) && (*ptr++ != '/'));
6196 if (len <= 0)
6197 return 0;
6198
6199 test->ptr = ptr;
6200 test->len = len;
6201
6202 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6203 return 1;
6204}
6205
6206/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006207static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006208acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6209 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006210{
6211 struct http_txn *txn = l7;
6212 char *ptr;
6213 int len;
6214
Willy Tarreaub6866442008-07-14 23:54:42 +02006215 if (!txn)
6216 return 0;
6217
Willy Tarreau655dce92009-11-08 13:10:58 +01006218 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006219 return 0;
6220
Willy Tarreau8797c062007-05-07 00:55:35 +02006221 len = txn->rsp.sl.st.c_l;
6222 ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som;
6223
6224 test->i = __strl2ui(ptr, len);
6225 test->flags = ACL_TEST_F_VOL_1ST;
6226 return 1;
6227}
6228
6229/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006230static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006231acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6232 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006233{
6234 struct http_txn *txn = l7;
6235
Willy Tarreaub6866442008-07-14 23:54:42 +02006236 if (!txn)
6237 return 0;
6238
Willy Tarreau655dce92009-11-08 13:10:58 +01006239 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006240 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006241
Willy Tarreauc11416f2007-06-17 16:58:38 +02006242 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6243 /* ensure the indexes are not affected */
6244 return 0;
6245
Willy Tarreau8797c062007-05-07 00:55:35 +02006246 test->len = txn->req.sl.rq.u_l;
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006247 test->ptr = txn->req.sol - txn->req.som + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006248
Willy Tarreauf3d25982007-05-08 22:45:09 +02006249 /* we do not need to set READ_ONLY because the data is in a buffer */
6250 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006251 return 1;
6252}
6253
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006254static int
6255acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6256 struct acl_expr *expr, struct acl_test *test)
6257{
6258 struct http_txn *txn = l7;
6259
Willy Tarreaub6866442008-07-14 23:54:42 +02006260 if (!txn)
6261 return 0;
6262
Willy Tarreau655dce92009-11-08 13:10:58 +01006263 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006264 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006265
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006266 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6267 /* ensure the indexes are not affected */
6268 return 0;
6269
6270 /* Parse HTTP request */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006271 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 +01006272 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6273 test->i = AF_INET;
6274
6275 /*
6276 * If we are parsing url in frontend space, we prepare backend stage
6277 * to not parse again the same url ! optimization lazyness...
6278 */
6279 if (px->options & PR_O_HTTP_PROXY)
6280 l4->flags |= SN_ADDR_SET;
6281
6282 test->flags = ACL_TEST_F_READ_ONLY;
6283 return 1;
6284}
6285
6286static int
6287acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6288 struct acl_expr *expr, struct acl_test *test)
6289{
6290 struct http_txn *txn = l7;
6291
Willy Tarreaub6866442008-07-14 23:54:42 +02006292 if (!txn)
6293 return 0;
6294
Willy Tarreau655dce92009-11-08 13:10:58 +01006295 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006296 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006297
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006298 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6299 /* ensure the indexes are not affected */
6300 return 0;
6301
6302 /* Same optimization as url_ip */
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006303 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 +01006304 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6305
6306 if (px->options & PR_O_HTTP_PROXY)
6307 l4->flags |= SN_ADDR_SET;
6308
6309 test->flags = ACL_TEST_F_READ_ONLY;
6310 return 1;
6311}
6312
Willy Tarreauc11416f2007-06-17 16:58:38 +02006313/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6314 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6315 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006316static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006317acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006318 struct acl_expr *expr, struct acl_test *test)
6319{
6320 struct http_txn *txn = l7;
6321 struct hdr_idx *idx = &txn->hdr_idx;
6322 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006323
Willy Tarreaub6866442008-07-14 23:54:42 +02006324 if (!txn)
6325 return 0;
6326
Willy Tarreau33a7e692007-06-10 19:45:56 +02006327 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6328 /* search for header from the beginning */
6329 ctx->idx = 0;
6330
Willy Tarreau33a7e692007-06-10 19:45:56 +02006331 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6332 test->flags |= ACL_TEST_F_FETCH_MORE;
6333 test->flags |= ACL_TEST_F_VOL_HDR;
6334 test->len = ctx->vlen;
6335 test->ptr = (char *)ctx->line + ctx->val;
6336 return 1;
6337 }
6338
6339 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6340 test->flags |= ACL_TEST_F_VOL_HDR;
6341 return 0;
6342}
6343
Willy Tarreau33a7e692007-06-10 19:45:56 +02006344static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006345acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
6346 struct acl_expr *expr, struct acl_test *test)
6347{
6348 struct http_txn *txn = l7;
6349
Willy Tarreaub6866442008-07-14 23:54:42 +02006350 if (!txn)
6351 return 0;
6352
Willy Tarreau655dce92009-11-08 13:10:58 +01006353 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006354 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006355
Willy Tarreauc11416f2007-06-17 16:58:38 +02006356 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6357 /* ensure the indexes are not affected */
6358 return 0;
6359
6360 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
6361}
6362
6363static int
6364acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
6365 struct acl_expr *expr, struct acl_test *test)
6366{
6367 struct http_txn *txn = l7;
6368
Willy Tarreaub6866442008-07-14 23:54:42 +02006369 if (!txn)
6370 return 0;
6371
Willy Tarreau655dce92009-11-08 13:10:58 +01006372 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006373 return 0;
6374
6375 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
6376}
6377
6378/* 6. Check on HTTP header count. The number of occurrences is returned.
6379 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6380 */
6381static int
6382acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006383 struct acl_expr *expr, struct acl_test *test)
6384{
6385 struct http_txn *txn = l7;
6386 struct hdr_idx *idx = &txn->hdr_idx;
6387 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006388 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02006389
Willy Tarreaub6866442008-07-14 23:54:42 +02006390 if (!txn)
6391 return 0;
6392
Willy Tarreau33a7e692007-06-10 19:45:56 +02006393 ctx.idx = 0;
6394 cnt = 0;
6395 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
6396 cnt++;
6397
6398 test->i = cnt;
6399 test->flags = ACL_TEST_F_VOL_HDR;
6400 return 1;
6401}
6402
Willy Tarreauc11416f2007-06-17 16:58:38 +02006403static int
6404acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6405 struct acl_expr *expr, struct acl_test *test)
6406{
6407 struct http_txn *txn = l7;
6408
Willy Tarreaub6866442008-07-14 23:54:42 +02006409 if (!txn)
6410 return 0;
6411
Willy Tarreau655dce92009-11-08 13:10:58 +01006412 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006413 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006414
Willy Tarreauc11416f2007-06-17 16:58:38 +02006415 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6416 /* ensure the indexes are not affected */
6417 return 0;
6418
6419 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
6420}
6421
6422static int
6423acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
6424 struct acl_expr *expr, struct acl_test *test)
6425{
6426 struct http_txn *txn = l7;
6427
Willy Tarreaub6866442008-07-14 23:54:42 +02006428 if (!txn)
6429 return 0;
6430
Willy Tarreau655dce92009-11-08 13:10:58 +01006431 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006432 return 0;
6433
6434 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
6435}
6436
Willy Tarreau33a7e692007-06-10 19:45:56 +02006437/* 7. Check on HTTP header's integer value. The integer value is returned.
6438 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02006439 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02006440 */
6441static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006442acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006443 struct acl_expr *expr, struct acl_test *test)
6444{
6445 struct http_txn *txn = l7;
6446 struct hdr_idx *idx = &txn->hdr_idx;
6447 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006448
Willy Tarreaub6866442008-07-14 23:54:42 +02006449 if (!txn)
6450 return 0;
6451
Willy Tarreau33a7e692007-06-10 19:45:56 +02006452 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6453 /* search for header from the beginning */
6454 ctx->idx = 0;
6455
Willy Tarreau33a7e692007-06-10 19:45:56 +02006456 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6457 test->flags |= ACL_TEST_F_FETCH_MORE;
6458 test->flags |= ACL_TEST_F_VOL_HDR;
6459 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
6460 return 1;
6461 }
6462
6463 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6464 test->flags |= ACL_TEST_F_VOL_HDR;
6465 return 0;
6466}
6467
Willy Tarreauc11416f2007-06-17 16:58:38 +02006468static int
6469acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6470 struct acl_expr *expr, struct acl_test *test)
6471{
6472 struct http_txn *txn = l7;
6473
Willy Tarreaub6866442008-07-14 23:54:42 +02006474 if (!txn)
6475 return 0;
6476
Willy Tarreau655dce92009-11-08 13:10:58 +01006477 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006478 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006479
Willy Tarreauc11416f2007-06-17 16:58:38 +02006480 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6481 /* ensure the indexes are not affected */
6482 return 0;
6483
6484 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
6485}
6486
6487static int
6488acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
6489 struct acl_expr *expr, struct acl_test *test)
6490{
6491 struct http_txn *txn = l7;
6492
Willy Tarreaub6866442008-07-14 23:54:42 +02006493 if (!txn)
6494 return 0;
6495
Willy Tarreau655dce92009-11-08 13:10:58 +01006496 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006497 return 0;
6498
6499 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
6500}
6501
Willy Tarreau106f9792009-09-19 07:54:16 +02006502/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
6503 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
6504 */
6505static int
6506acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
6507 struct acl_expr *expr, struct acl_test *test)
6508{
6509 struct http_txn *txn = l7;
6510 struct hdr_idx *idx = &txn->hdr_idx;
6511 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
6512
6513 if (!txn)
6514 return 0;
6515
6516 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
6517 /* search for header from the beginning */
6518 ctx->idx = 0;
6519
6520 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
6521 test->flags |= ACL_TEST_F_FETCH_MORE;
6522 test->flags |= ACL_TEST_F_VOL_HDR;
6523 /* Same optimization as url_ip */
6524 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
6525 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
6526 test->ptr = (void *)&l4->srv_addr.sin_addr;
6527 test->i = AF_INET;
6528 return 1;
6529 }
6530
6531 test->flags &= ~ACL_TEST_F_FETCH_MORE;
6532 test->flags |= ACL_TEST_F_VOL_HDR;
6533 return 0;
6534}
6535
6536static int
6537acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6538 struct acl_expr *expr, struct acl_test *test)
6539{
6540 struct http_txn *txn = l7;
6541
6542 if (!txn)
6543 return 0;
6544
Willy Tarreau655dce92009-11-08 13:10:58 +01006545 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006546 return 0;
6547
6548 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6549 /* ensure the indexes are not affected */
6550 return 0;
6551
6552 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
6553}
6554
6555static int
6556acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6557 struct acl_expr *expr, struct acl_test *test)
6558{
6559 struct http_txn *txn = l7;
6560
6561 if (!txn)
6562 return 0;
6563
Willy Tarreau655dce92009-11-08 13:10:58 +01006564 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02006565 return 0;
6566
6567 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
6568}
6569
Willy Tarreau737b0c12007-06-10 21:28:46 +02006570/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
6571 * the first '/' after the possible hostname, and ends before the possible '?'.
6572 */
6573static int
6574acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
6575 struct acl_expr *expr, struct acl_test *test)
6576{
6577 struct http_txn *txn = l7;
6578 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006579
Willy Tarreaub6866442008-07-14 23:54:42 +02006580 if (!txn)
6581 return 0;
6582
Willy Tarreau655dce92009-11-08 13:10:58 +01006583 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006584 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006585
Willy Tarreauc11416f2007-06-17 16:58:38 +02006586 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6587 /* ensure the indexes are not affected */
6588 return 0;
6589
Willy Tarreaua95a1f42010-01-03 13:04:35 +01006590 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 +01006591 ptr = http_get_path(txn);
6592 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02006593 return 0;
6594
6595 /* OK, we got the '/' ! */
6596 test->ptr = ptr;
6597
6598 while (ptr < end && *ptr != '?')
6599 ptr++;
6600
6601 test->len = ptr - test->ptr;
6602
6603 /* we do not need to set READ_ONLY because the data is in a buffer */
6604 test->flags = ACL_TEST_F_VOL_1ST;
6605 return 1;
6606}
6607
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006608static int
6609acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
6610 struct acl_expr *expr, struct acl_test *test)
6611{
6612 struct buffer *req = s->req;
6613 struct http_txn *txn = &s->txn;
6614 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02006615
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006616 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
6617 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
6618 */
6619
6620 if (!s || !req)
6621 return 0;
6622
Willy Tarreau655dce92009-11-08 13:10:58 +01006623 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006624 /* Already decoded as OK */
6625 test->flags |= ACL_TEST_F_SET_RES_PASS;
6626 return 1;
6627 }
6628
6629 /* Try to decode HTTP request */
6630 if (likely(req->lr < req->r))
6631 http_msg_analyzer(req, msg, &txn->hdr_idx);
6632
Willy Tarreau655dce92009-11-08 13:10:58 +01006633 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006634 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
6635 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6636 return 1;
6637 }
6638 /* wait for final state */
6639 test->flags |= ACL_TEST_F_MAY_CHANGE;
6640 return 0;
6641 }
6642
6643 /* OK we got a valid HTTP request. We have some minor preparation to
6644 * perform so that further checks can rely on HTTP tests.
6645 */
6646 msg->sol = req->data + msg->som;
6647 txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l);
6648 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
6649 s->flags |= SN_REDIRECTABLE;
6650
6651 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
6652 test->flags |= ACL_TEST_F_SET_RES_FAIL;
6653 return 1;
6654 }
6655
6656 test->flags |= ACL_TEST_F_SET_RES_PASS;
6657 return 1;
6658}
6659
Willy Tarreau8797c062007-05-07 00:55:35 +02006660
6661/************************************************************************/
6662/* All supported keywords must be declared here. */
6663/************************************************************************/
6664
6665/* Note: must not be declared <const> as its list will be overwritten */
6666static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02006667 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
6668
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006669 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
6670 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6671 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6672 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02006673
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006674 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6675 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6676 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6677 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6678 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6679 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6680 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6681 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
6682 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02006683
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006684 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
6685 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6686 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6687 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6688 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6689 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6690 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6691 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
6692 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
6693 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006694 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02006695
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006696 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
6697 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
6698 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
6699 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
6700 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
6701 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
6702 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
6703 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
6704 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02006705 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006706
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02006707 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
6708 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
6709 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
6710 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
6711 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
6712 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
6713 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02006714
Willy Tarreauf3d25982007-05-08 22:45:09 +02006715 { NULL, NULL, NULL, NULL },
6716
6717#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02006718 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
6719 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
6720 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
6721 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
6722 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
6723 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
6724 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
6725
Willy Tarreau8797c062007-05-07 00:55:35 +02006726 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
6727 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
6728 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
6729 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
6730 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
6731 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
6732 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
6733 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
6734
6735 { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str },
6736 { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg },
6737 { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str },
6738 { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 },
6739 { NULL, NULL, NULL, NULL },
6740#endif
6741}};
6742
6743
6744__attribute__((constructor))
6745static void __http_protocol_init(void)
6746{
6747 acl_register_keywords(&acl_kws);
6748}
6749
6750
Willy Tarreau58f10d72006-12-04 02:26:12 +01006751/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006752 * Local variables:
6753 * c-indent-level: 8
6754 * c-basic-offset: 8
6755 * End:
6756 */