blob: 882f0dc4dca7a075ef264ea6c3e6c796c2998725 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004 * Copyright 2000-2010 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>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010027#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/compat.h>
29#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010030#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreau8797c062007-05-07 00:55:35 +020042#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010043#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/backend.h>
45#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010046#include <proto/checks.h>
Maik Broemme2850cb42009-04-17 18:53:21 +020047#include <proto/client.h>
Willy Tarreau91861262007-10-17 17:06:05 +020048#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/fd.h>
50#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010051#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020052#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010054#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010056#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010058#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020059#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/task.h>
61
Willy Tarreau522d6c02009-12-06 18:49:18 +010062const char HTTP_100[] =
63 "HTTP/1.1 100 Continue\r\n\r\n";
64
65const struct chunk http_100_chunk = {
66 .str = (char *)&HTTP_100,
67 .len = sizeof(HTTP_100)-1
68};
69
Willy Tarreau1c47f852006-07-09 08:22:27 +020070/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010071const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020072 "HTTP/1.0 200 OK\r\n"
73 "Cache-Control: no-cache\r\n"
74 "Connection: close\r\n"
75 "Content-Type: text/html\r\n"
76 "\r\n"
77 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
78
Willy Tarreau0f772532006-12-23 20:51:41 +010079const struct chunk http_200_chunk = {
80 .str = (char *)&HTTP_200,
81 .len = sizeof(HTTP_200)-1
82};
83
Willy Tarreaua9679ac2010-01-03 17:32:57 +010084/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020085const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020089 "Location: "; /* not terminated since it will be concatenated with the URL */
90
Willy Tarreau0f772532006-12-23 20:51:41 +010091const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010094 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010095 "Location: "; /* not terminated since it will be concatenated with the URL */
96
97/* same as 302 except that the browser MUST retry with the GET method */
98const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010099 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100100 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100101 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100102 "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 Tarreau844a7e72010-01-31 21:46:18 +0100114const char *HTTP_407_fmt =
115 "HTTP/1.0 407 Unauthorized\r\n"
116 "Cache-Control: no-cache\r\n"
117 "Connection: close\r\n"
118 "Content-Type: text/html\r\n"
119 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
120 "\r\n"
121 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
122
Willy Tarreau0f772532006-12-23 20:51:41 +0100123
124const int http_err_codes[HTTP_ERR_SIZE] = {
125 [HTTP_ERR_400] = 400,
126 [HTTP_ERR_403] = 403,
127 [HTTP_ERR_408] = 408,
128 [HTTP_ERR_500] = 500,
129 [HTTP_ERR_502] = 502,
130 [HTTP_ERR_503] = 503,
131 [HTTP_ERR_504] = 504,
132};
133
Willy Tarreau80587432006-12-24 17:47:20 +0100134static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100136 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100137 "Cache-Control: no-cache\r\n"
138 "Connection: close\r\n"
139 "Content-Type: text/html\r\n"
140 "\r\n"
141 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
142
143 [HTTP_ERR_403] =
144 "HTTP/1.0 403 Forbidden\r\n"
145 "Cache-Control: no-cache\r\n"
146 "Connection: close\r\n"
147 "Content-Type: text/html\r\n"
148 "\r\n"
149 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
150
151 [HTTP_ERR_408] =
152 "HTTP/1.0 408 Request Time-out\r\n"
153 "Cache-Control: no-cache\r\n"
154 "Connection: close\r\n"
155 "Content-Type: text/html\r\n"
156 "\r\n"
157 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
158
159 [HTTP_ERR_500] =
160 "HTTP/1.0 500 Server Error\r\n"
161 "Cache-Control: no-cache\r\n"
162 "Connection: close\r\n"
163 "Content-Type: text/html\r\n"
164 "\r\n"
165 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
166
167 [HTTP_ERR_502] =
168 "HTTP/1.0 502 Bad Gateway\r\n"
169 "Cache-Control: no-cache\r\n"
170 "Connection: close\r\n"
171 "Content-Type: text/html\r\n"
172 "\r\n"
173 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
174
175 [HTTP_ERR_503] =
176 "HTTP/1.0 503 Service Unavailable\r\n"
177 "Cache-Control: no-cache\r\n"
178 "Connection: close\r\n"
179 "Content-Type: text/html\r\n"
180 "\r\n"
181 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
182
183 [HTTP_ERR_504] =
184 "HTTP/1.0 504 Gateway Time-out\r\n"
185 "Cache-Control: no-cache\r\n"
186 "Connection: close\r\n"
187 "Content-Type: text/html\r\n"
188 "\r\n"
189 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
190
191};
192
Willy Tarreau80587432006-12-24 17:47:20 +0100193/* We must put the messages here since GCC cannot initialize consts depending
194 * on strlen().
195 */
196struct chunk http_err_chunks[HTTP_ERR_SIZE];
197
Willy Tarreau42250582007-04-01 01:30:43 +0200198#define FD_SETS_ARE_BITFIELDS
199#ifdef FD_SETS_ARE_BITFIELDS
200/*
201 * This map is used with all the FD_* macros to check whether a particular bit
202 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
203 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
204 * byte should be encoded. Be careful to always pass bytes from 0 to 255
205 * exclusively to the macros.
206 */
207fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
208fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
209
210#else
211#error "Check if your OS uses bitfields for fd_sets"
212#endif
213
Willy Tarreau80587432006-12-24 17:47:20 +0100214void init_proto_http()
215{
Willy Tarreau42250582007-04-01 01:30:43 +0200216 int i;
217 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100218 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200219
Willy Tarreau80587432006-12-24 17:47:20 +0100220 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
221 if (!http_err_msgs[msg]) {
222 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
223 abort();
224 }
225
226 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
227 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
228 }
Willy Tarreau42250582007-04-01 01:30:43 +0200229
230 /* initialize the log header encoding map : '{|}"#' should be encoded with
231 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
232 * URL encoding only requires '"', '#' to be encoded as well as non-
233 * printable characters above.
234 */
235 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
236 memset(url_encode_map, 0, sizeof(url_encode_map));
237 for (i = 0; i < 32; i++) {
238 FD_SET(i, hdr_encode_map);
239 FD_SET(i, url_encode_map);
240 }
241 for (i = 127; i < 256; i++) {
242 FD_SET(i, hdr_encode_map);
243 FD_SET(i, url_encode_map);
244 }
245
246 tmp = "\"#{|}";
247 while (*tmp) {
248 FD_SET(*tmp, hdr_encode_map);
249 tmp++;
250 }
251
252 tmp = "\"#";
253 while (*tmp) {
254 FD_SET(*tmp, url_encode_map);
255 tmp++;
256 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200257
258 /* memory allocations */
259 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200260 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100261}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200262
Willy Tarreau53b6c742006-12-17 13:37:46 +0100263/*
264 * We have 26 list of methods (1 per first letter), each of which can have
265 * up to 3 entries (2 valid, 1 null).
266 */
267struct http_method_desc {
268 http_meth_t meth;
269 int len;
270 const char text[8];
271};
272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100273const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100274 ['C' - 'A'] = {
275 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
276 },
277 ['D' - 'A'] = {
278 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
279 },
280 ['G' - 'A'] = {
281 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
282 },
283 ['H' - 'A'] = {
284 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
285 },
286 ['P' - 'A'] = {
287 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
288 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
289 },
290 ['T' - 'A'] = {
291 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
292 },
293 /* rest is empty like this :
294 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
295 */
296};
297
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100298/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200299 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100300 * RFC2616 for those chars.
301 */
302
303const char http_is_spht[256] = {
304 [' '] = 1, ['\t'] = 1,
305};
306
307const char http_is_crlf[256] = {
308 ['\r'] = 1, ['\n'] = 1,
309};
310
311const char http_is_lws[256] = {
312 [' '] = 1, ['\t'] = 1,
313 ['\r'] = 1, ['\n'] = 1,
314};
315
316const char http_is_sep[256] = {
317 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
318 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
319 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
320 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
321 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
322};
323
324const char http_is_ctl[256] = {
325 [0 ... 31] = 1,
326 [127] = 1,
327};
328
329/*
330 * A token is any ASCII char that is neither a separator nor a CTL char.
331 * Do not overwrite values in assignment since gcc-2.95 will not handle
332 * them correctly. Instead, define every non-CTL char's status.
333 */
334const char http_is_token[256] = {
335 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
336 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
337 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
338 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
339 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
340 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
341 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
342 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
343 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
344 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
345 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
346 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
347 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
348 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
349 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
350 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
351 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
352 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
353 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
354 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
355 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
356 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
357 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
358 ['|'] = 1, ['}'] = 0, ['~'] = 1,
359};
360
361
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100362/*
363 * An http ver_token is any ASCII which can be found in an HTTP version,
364 * which includes 'H', 'T', 'P', '/', '.' and any digit.
365 */
366const char http_is_ver_token[256] = {
367 ['.'] = 1, ['/'] = 1,
368 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
369 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
370 ['H'] = 1, ['P'] = 1, ['T'] = 1,
371};
372
373
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100374/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100375 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
376 */
377#if defined(DEBUG_FSM)
378static void http_silent_debug(int line, struct session *s)
379{
380 int size = 0;
381 size += snprintf(trash + size, sizeof(trash) - size,
382 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n",
383 line,
384 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
385 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags);
386 write(-1, trash, size);
387 size = 0;
388 size += snprintf(trash + size, sizeof(trash) - size,
389 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n",
390 line,
391 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
392 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward);
393
394 write(-1, trash, size);
395}
396#else
397#define http_silent_debug(l,s) do { } while (0)
398#endif
399
400/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100401 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
402 * CRLF. Text length is measured first, so it cannot be NULL.
403 * The header is also automatically added to the index <hdr_idx>, and the end
404 * of headers is automatically adjusted. The number of bytes added is returned
405 * on success, otherwise <0 is returned indicating an error.
406 */
407int http_header_add_tail(struct buffer *b, struct http_msg *msg,
408 struct hdr_idx *hdr_idx, const char *text)
409{
410 int bytes, len;
411
412 len = strlen(text);
413 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
414 if (!bytes)
415 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100416 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100417 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
418}
419
420/*
421 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
422 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
423 * the buffer is only opened and the space reserved, but nothing is copied.
424 * The header is also automatically added to the index <hdr_idx>, and the end
425 * of headers is automatically adjusted. The number of bytes added is returned
426 * on success, otherwise <0 is returned indicating an error.
427 */
428int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
429 struct hdr_idx *hdr_idx, const char *text, int len)
430{
431 int bytes;
432
433 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
434 if (!bytes)
435 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100436 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100437 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
438}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439
440/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100441 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
442 * If so, returns the position of the first non-space character relative to
443 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
444 * to return a pointer to the place after the first space. Returns 0 if the
445 * header name does not match. Checks are case-insensitive.
446 */
447int http_header_match2(const char *hdr, const char *end,
448 const char *name, int len)
449{
450 const char *val;
451
452 if (hdr + len >= end)
453 return 0;
454 if (hdr[len] != ':')
455 return 0;
456 if (strncasecmp(hdr, name, len) != 0)
457 return 0;
458 val = hdr + len + 1;
459 while (val < end && HTTP_IS_SPHT(*val))
460 val++;
461 if ((val >= end) && (len + 2 <= end - hdr))
462 return len + 2; /* we may replace starting from second space */
463 return val - hdr;
464}
465
Willy Tarreau68085d82010-01-18 14:54:04 +0100466/* Find the end of the header value contained between <s> and <e>. See RFC2616,
467 * par 2.2 for more information. Note that it requires a valid header to return
468 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200469 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100470char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200471{
472 int quoted, qdpair;
473
474 quoted = qdpair = 0;
475 for (; s < e; s++) {
476 if (qdpair) qdpair = 0;
477 else if (quoted && *s == '\\') qdpair = 1;
478 else if (quoted && *s == '"') quoted = 0;
479 else if (*s == '"') quoted = 1;
480 else if (*s == ',') return s;
481 }
482 return s;
483}
484
485/* Find the first or next occurrence of header <name> in message buffer <sol>
486 * using headers index <idx>, and return it in the <ctx> structure. This
487 * structure holds everything necessary to use the header and find next
488 * occurrence. If its <idx> member is 0, the header is searched from the
489 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100490 * 1 when it finds a value, and 0 when there is no more. It is designed to work
491 * with headers defined as comma-separated lists. As a special case, if ctx->val
492 * is NULL when searching for a new values of a header, the current header is
493 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200494 */
495int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100496 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200497 struct hdr_ctx *ctx)
498{
Willy Tarreau68085d82010-01-18 14:54:04 +0100499 char *eol, *sov;
500 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200501
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 cur_idx = ctx->idx;
503 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200504 /* We have previously returned a value, let's search
505 * another one on the same line.
506 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100508 ctx->del = ctx->val + ctx->vlen;
509 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 eol = sol + idx->v[cur_idx].len;
511
512 if (sov >= eol)
513 /* no more values in this header */
514 goto next_hdr;
515
Willy Tarreau68085d82010-01-18 14:54:04 +0100516 /* values remaining for this header, skip the comma but save it
517 * for later use (eg: for header deletion).
518 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200519 sov++;
520 while (sov < eol && http_is_lws[(unsigned char)*sov])
521 sov++;
522
523 goto return_hdr;
524 }
525
526 /* first request for this header */
527 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100528 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200529 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200530 while (cur_idx) {
531 eol = sol + idx->v[cur_idx].len;
532
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200533 if (len == 0) {
534 /* No argument was passed, we want any header.
535 * To achieve this, we simply build a fake request. */
536 while (sol + len < eol && sol[len] != ':')
537 len++;
538 name = sol;
539 }
540
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 if ((len < eol - sol) &&
542 (sol[len] == ':') &&
543 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100544 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200545 sov = sol + len + 1;
546 while (sov < eol && http_is_lws[(unsigned char)*sov])
547 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100548
Willy Tarreau33a7e692007-06-10 19:45:56 +0200549 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100550 ctx->prev = old_idx;
551 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200552 ctx->idx = cur_idx;
553 ctx->val = sov - sol;
554
555 eol = find_hdr_value_end(sov, eol);
556 ctx->vlen = eol - sov;
557 return 1;
558 }
559 next_hdr:
560 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100561 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200562 cur_idx = idx->v[cur_idx].next;
563 }
564 return 0;
565}
566
567int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100568 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200569 struct hdr_ctx *ctx)
570{
571 return http_find_header2(name, strlen(name), sol, idx, ctx);
572}
573
Willy Tarreau68085d82010-01-18 14:54:04 +0100574/* Remove one value of a header. This only works on a <ctx> returned by one of
575 * the http_find_header functions. The value is removed, as well as surrounding
576 * commas if any. If the removed value was alone, the whole header is removed.
577 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
578 * message <msg>. The new index is returned. If it is zero, it means there is
579 * no more header, so any processing may stop. The ctx is always left in a form
580 * that can be handled by http_find_header2() to find next occurrence.
581 */
582int http_remove_header2(struct http_msg *msg, struct buffer *buf,
583 struct hdr_idx *idx, struct hdr_ctx *ctx)
584{
585 int cur_idx = ctx->idx;
586 char *sol = ctx->line;
587 struct hdr_idx_elem *hdr;
588 int delta, skip_comma;
589
590 if (!cur_idx)
591 return 0;
592
593 hdr = &idx->v[cur_idx];
594 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) {
595 /* This was the only value of the header, we must now remove it entirely. */
596 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
597 http_msg_move_end(msg, delta);
598 idx->used--;
599 hdr->len = 0; /* unused entry */
600 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
601 ctx->idx = ctx->prev; /* walk back to the end of previous header */
602 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
603 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
604 ctx->vlen = 0;
605 return ctx->idx;
606 }
607
608 /* This was not the only value of this header. We have to remove between
609 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
610 * of the list, we remove the last separator.
611 */
612
613 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
614 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
615 sol + ctx->val + ctx->vlen + skip_comma,
616 NULL, 0);
617 hdr->len += delta;
618 http_msg_move_end(msg, delta);
619 ctx->val = ctx->del;
620 ctx->vlen = 0;
621 return ctx->idx;
622}
623
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100624/* This function handles a server error at the stream interface level. The
625 * stream interface is assumed to be already in a closed state. An optional
626 * message is copied into the input buffer, and an HTTP status code stored.
627 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100628 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100630static void http_server_error(struct session *t, struct stream_interface *si,
631 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100633 buffer_auto_read(si->ob);
634 buffer_abort(si->ob);
635 buffer_auto_close(si->ob);
636 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200637 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100638 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100639 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100640 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100641 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200642 }
643 if (!(t->flags & SN_ERR_MASK))
644 t->flags |= err;
645 if (!(t->flags & SN_FINST_MASK))
646 t->flags |= finst;
647}
648
Willy Tarreau80587432006-12-24 17:47:20 +0100649/* This function returns the appropriate error location for the given session
650 * and message.
651 */
652
653struct chunk *error_message(struct session *s, int msgnum)
654{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200655 if (s->be->errmsg[msgnum].str)
656 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100657 else if (s->fe->errmsg[msgnum].str)
658 return &s->fe->errmsg[msgnum];
659 else
660 return &http_err_chunks[msgnum];
661}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662
Willy Tarreau53b6c742006-12-17 13:37:46 +0100663/*
664 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
665 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
666 */
667static http_meth_t find_http_meth(const char *str, const int len)
668{
669 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100670 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100671
672 m = ((unsigned)*str - 'A');
673
674 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100675 for (h = http_methods[m]; h->len > 0; h++) {
676 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100677 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100678 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100679 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100680 };
681 return HTTP_METH_OTHER;
682 }
683 return HTTP_METH_NONE;
684
685}
686
Willy Tarreau21d2af32008-02-14 20:25:24 +0100687/* Parse the URI from the given transaction (which is assumed to be in request
688 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
689 * It is returned otherwise.
690 */
691static char *
692http_get_path(struct http_txn *txn)
693{
694 char *ptr, *end;
695
Willy Tarreau962c3f42010-01-10 00:15:35 +0100696 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100697 end = ptr + txn->req.sl.rq.u_l;
698
699 if (ptr >= end)
700 return NULL;
701
702 /* RFC2616, par. 5.1.2 :
703 * Request-URI = "*" | absuri | abspath | authority
704 */
705
706 if (*ptr == '*')
707 return NULL;
708
709 if (isalpha((unsigned char)*ptr)) {
710 /* this is a scheme as described by RFC3986, par. 3.1 */
711 ptr++;
712 while (ptr < end &&
713 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
714 ptr++;
715 /* skip '://' */
716 if (ptr == end || *ptr++ != ':')
717 return NULL;
718 if (ptr == end || *ptr++ != '/')
719 return NULL;
720 if (ptr == end || *ptr++ != '/')
721 return NULL;
722 }
723 /* skip [user[:passwd]@]host[:[port]] */
724
725 while (ptr < end && *ptr != '/')
726 ptr++;
727
728 if (ptr == end)
729 return NULL;
730
731 /* OK, we got the '/' ! */
732 return ptr;
733}
734
Willy Tarreauefb453c2008-10-26 20:49:47 +0100735/* Returns a 302 for a redirectable request. This may only be called just after
736 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
737 * left unchanged and will follow normal proxy processing.
738 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100740{
741 struct http_txn *txn;
742 struct chunk rdr;
743 char *path;
744 int len;
745
746 /* 1: create the response header */
747 rdr.len = strlen(HTTP_302);
748 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100749 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100750 memcpy(rdr.str, HTTP_302, rdr.len);
751
752 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200753 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100754 return;
755
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100756 /* special prefix "/" means don't change URL */
757 if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') {
758 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
759 rdr.len += s->srv->rdr_len;
760 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100761
762 /* 3: add the request URI */
763 txn = &s->txn;
764 path = http_get_path(txn);
765 if (!path)
766 return;
767
Willy Tarreau962c3f42010-01-10 00:15:35 +0100768 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200769 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100770 return;
771
772 memcpy(rdr.str + rdr.len, path, len);
773 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100774
775 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
776 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
777 rdr.len += 29;
778 } else {
779 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
780 rdr.len += 23;
781 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100782
783 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100784 si->shutr(si);
785 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100786 si->err_type = SI_ET_NONE;
787 si->err_loc = NULL;
788 si->state = SI_ST_CLO;
789
790 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100791 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792
793 /* FIXME: we should increase a counter of redirects per server and per backend. */
794 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100795 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100796}
797
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100798/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100799 * that the server side is closed. Note that err_type is actually a
800 * bitmask, where almost only aborts may be cumulated with other
801 * values. We consider that aborted operations are more important
802 * than timeouts or errors due to the fact that nobody else in the
803 * logs might explain incomplete retries. All others should avoid
804 * being cumulated. It should normally not be possible to have multiple
805 * aborts at once, but just in case, the first one in sequence is reported.
806 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100807void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100808{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100809 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100810
811 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100812 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
813 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100814 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100815 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
816 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100818 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
819 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100820 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100821 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
822 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100823 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100824 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
825 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100826 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100827 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
828 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100829 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100830 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
831 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100832}
833
Willy Tarreau42250582007-04-01 01:30:43 +0200834extern const char sess_term_cond[8];
835extern const char sess_fin_state[8];
836extern const char *monthname[12];
837const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
838const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
839 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
840 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200841struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200842struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100843
Emeric Brun3a058f32009-06-30 18:26:00 +0200844void http_sess_clflog(struct session *s)
845{
846 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
847 struct proxy *fe = s->fe;
848 struct proxy *be = s->be;
849 struct proxy *prx_log;
850 struct http_txn *txn = &s->txn;
851 int tolog, level, err;
852 char *uri, *h;
853 char *svid;
854 struct tm tm;
855 static char tmpline[MAX_SYSLOG_LEN];
856 int hdr;
857 size_t w;
858 int t_request;
859
860 prx_log = fe;
861 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
862 (s->conn_retries != be->conn_retries) ||
863 txn->status >= 500;
864
865 if (s->cli_addr.ss_family == AF_INET)
866 inet_ntop(AF_INET,
867 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
868 pn, sizeof(pn));
869 else
870 inet_ntop(AF_INET6,
871 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
872 pn, sizeof(pn));
873
874 get_gmtime(s->logs.accept_date.tv_sec, &tm);
875
876 /* FIXME: let's limit ourselves to frontend logging for now. */
877 tolog = fe->to_log;
878
879 h = tmpline;
880
881 w = snprintf(h, sizeof(tmpline),
882 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
883 pn,
884 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
885 tm.tm_hour, tm.tm_min, tm.tm_sec);
886 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
887 goto trunc;
888 h += w;
889
890 if (h >= tmpline + sizeof(tmpline) - 4)
891 goto trunc;
892
893 *(h++) = ' ';
894 *(h++) = '\"';
895 uri = txn->uri ? txn->uri : "<BADREQ>";
896 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
897 '#', url_encode_map, uri);
898 *(h++) = '\"';
899
900 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
901 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
902 goto trunc;
903 h += w;
904
905 if (h >= tmpline + sizeof(tmpline) - 9)
906 goto trunc;
907 memcpy(h, " \"-\" \"-\"", 8);
908 h += 8;
909
910 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
911 " %d %03d",
912 (s->cli_addr.ss_family == AF_INET) ?
913 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
914 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
915 (int)s->logs.accept_date.tv_usec/1000);
916 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
917 goto trunc;
918 h += w;
919
920 w = strlen(fe->id);
921 if (h >= tmpline + sizeof(tmpline) - 4 - w)
922 goto trunc;
923 *(h++) = ' ';
924 *(h++) = '\"';
925 memcpy(h, fe->id, w);
926 h += w;
927 *(h++) = '\"';
928
929 w = strlen(be->id);
930 if (h >= tmpline + sizeof(tmpline) - 4 - w)
931 goto trunc;
932 *(h++) = ' ';
933 *(h++) = '\"';
934 memcpy(h, be->id, w);
935 h += w;
936 *(h++) = '\"';
937
938 svid = (tolog & LW_SVID) ?
939 (s->data_source != DATA_SRC_STATS) ?
940 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
941
942 w = strlen(svid);
943 if (h >= tmpline + sizeof(tmpline) - 4 - w)
944 goto trunc;
945 *(h++) = ' ';
946 *(h++) = '\"';
947 memcpy(h, svid, w);
948 h += w;
949 *(h++) = '\"';
950
951 t_request = -1;
952 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
953 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
954 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
955 " %d %ld %ld %ld %ld",
956 t_request,
957 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
958 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
959 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
960 s->logs.t_close);
961 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
962 goto trunc;
963 h += w;
964
965 if (h >= tmpline + sizeof(tmpline) - 8)
966 goto trunc;
967 *(h++) = ' ';
968 *(h++) = '\"';
969 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
970 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
971 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
972 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
973 *(h++) = '\"';
974
975 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
976 " %d %d %d %d %d %ld %ld",
977 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
978 (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries,
979 s->logs.srv_queue_size, s->logs.prx_queue_size);
980
981 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
982 goto trunc;
983 h += w;
984
985 if (txn->cli_cookie) {
986 w = strlen(txn->cli_cookie);
987 if (h >= tmpline + sizeof(tmpline) - 4 - w)
988 goto trunc;
989 *(h++) = ' ';
990 *(h++) = '\"';
991 memcpy(h, txn->cli_cookie, w);
992 h += w;
993 *(h++) = '\"';
994 } else {
995 if (h >= tmpline + sizeof(tmpline) - 5)
996 goto trunc;
997 memcpy(h, " \"-\"", 4);
998 h += 4;
999 }
1000
1001 if (txn->srv_cookie) {
1002 w = strlen(txn->srv_cookie);
1003 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1004 goto trunc;
1005 *(h++) = ' ';
1006 *(h++) = '\"';
1007 memcpy(h, txn->srv_cookie, w);
1008 h += w;
1009 *(h++) = '\"';
1010 } else {
1011 if (h >= tmpline + sizeof(tmpline) - 5)
1012 goto trunc;
1013 memcpy(h, " \"-\"", 4);
1014 h += 4;
1015 }
1016
1017 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1018 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1019 if (h >= sizeof (tmpline) + tmpline - 4)
1020 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001021 if (txn->req.cap[hdr] != NULL) {
1022 *(h++) = ' ';
1023 *(h++) = '\"';
1024 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1025 '#', hdr_encode_map, txn->req.cap[hdr]);
1026 *(h++) = '\"';
1027 } else {
1028 memcpy(h, " \"-\"", 4);
1029 h += 4;
1030 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001031 }
1032 }
1033
1034 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1035 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1036 if (h >= sizeof (tmpline) + tmpline - 4)
1037 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001038 if (txn->rsp.cap[hdr] != NULL) {
1039 *(h++) = ' ';
1040 *(h++) = '\"';
1041 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1042 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1043 *(h++) = '\"';
1044 } else {
1045 memcpy(h, " \"-\"", 4);
1046 h += 4;
1047 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001048 }
1049 }
1050
1051trunc:
1052 *h = '\0';
1053
1054 level = LOG_INFO;
1055 if (err && (fe->options2 & PR_O2_LOGERRORS))
1056 level = LOG_ERR;
1057
1058 send_log(prx_log, level, "%s\n", tmpline);
1059
1060 s->logs.logwait = 0;
1061}
1062
Willy Tarreau42250582007-04-01 01:30:43 +02001063/*
1064 * send a log for the session when we have enough info about it.
1065 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001066 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001067void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001068{
1069 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1070 struct proxy *fe = s->fe;
1071 struct proxy *be = s->be;
1072 struct proxy *prx_log;
1073 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001074 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001075 char *uri, *h;
1076 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001077 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001078 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001079 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001080 int hdr;
1081
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001082 /* if we don't want to log normal traffic, return now */
1083 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
1084 (s->conn_retries != be->conn_retries) ||
1085 txn->status >= 500;
1086 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1087 return;
1088
Willy Tarreau42250582007-04-01 01:30:43 +02001089 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1090 return;
1091 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001092
Emeric Brun3a058f32009-06-30 18:26:00 +02001093 if (prx_log->options2 & PR_O2_CLFLOG)
1094 return http_sess_clflog(s);
1095
Willy Tarreau42250582007-04-01 01:30:43 +02001096 if (s->cli_addr.ss_family == AF_INET)
1097 inet_ntop(AF_INET,
1098 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1099 pn, sizeof(pn));
1100 else
1101 inet_ntop(AF_INET6,
1102 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1103 pn, sizeof(pn));
1104
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001105 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001106
1107 /* FIXME: let's limit ourselves to frontend logging for now. */
1108 tolog = fe->to_log;
1109
1110 h = tmpline;
1111 if (fe->to_log & LW_REQHDR &&
1112 txn->req.cap &&
1113 (h < tmpline + sizeof(tmpline) - 10)) {
1114 *(h++) = ' ';
1115 *(h++) = '{';
1116 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1117 if (hdr)
1118 *(h++) = '|';
1119 if (txn->req.cap[hdr] != NULL)
1120 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1121 '#', hdr_encode_map, txn->req.cap[hdr]);
1122 }
1123 *(h++) = '}';
1124 }
1125
1126 if (fe->to_log & LW_RSPHDR &&
1127 txn->rsp.cap &&
1128 (h < tmpline + sizeof(tmpline) - 7)) {
1129 *(h++) = ' ';
1130 *(h++) = '{';
1131 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1132 if (hdr)
1133 *(h++) = '|';
1134 if (txn->rsp.cap[hdr] != NULL)
1135 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1136 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1137 }
1138 *(h++) = '}';
1139 }
1140
1141 if (h < tmpline + sizeof(tmpline) - 4) {
1142 *(h++) = ' ';
1143 *(h++) = '"';
1144 uri = txn->uri ? txn->uri : "<BADREQ>";
1145 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1146 '#', url_encode_map, uri);
1147 *(h++) = '"';
1148 }
1149 *h = '\0';
1150
1151 svid = (tolog & LW_SVID) ?
1152 (s->data_source != DATA_SRC_STATS) ?
1153 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1154
Willy Tarreau70089872008-06-13 21:12:51 +02001155 t_request = -1;
1156 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1157 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1158
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001159 level = LOG_INFO;
1160 if (err && (fe->options2 & PR_O2_LOGERRORS))
1161 level = LOG_ERR;
1162
1163 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001164 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001165 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1166 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001167 pn,
1168 (s->cli_addr.ss_family == AF_INET) ?
1169 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1170 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001171 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001172 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001173 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001174 t_request,
1175 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001176 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1177 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1178 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1179 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001180 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001181 txn->cli_cookie ? txn->cli_cookie : "-",
1182 txn->srv_cookie ? txn->srv_cookie : "-",
1183 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1184 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1185 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1186 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1187 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001188 (s->flags & SN_REDISP)?"+":"",
1189 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001190 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1191
1192 s->logs.logwait = 0;
1193}
1194
Willy Tarreau117f59e2007-03-04 18:17:17 +01001195
1196/*
1197 * Capture headers from message starting at <som> according to header list
1198 * <cap_hdr>, and fill the <idx> structure appropriately.
1199 */
1200void capture_headers(char *som, struct hdr_idx *idx,
1201 char **cap, struct cap_hdr *cap_hdr)
1202{
1203 char *eol, *sol, *col, *sov;
1204 int cur_idx;
1205 struct cap_hdr *h;
1206 int len;
1207
1208 sol = som + hdr_idx_first_pos(idx);
1209 cur_idx = hdr_idx_first_idx(idx);
1210
1211 while (cur_idx) {
1212 eol = sol + idx->v[cur_idx].len;
1213
1214 col = sol;
1215 while (col < eol && *col != ':')
1216 col++;
1217
1218 sov = col + 1;
1219 while (sov < eol && http_is_lws[(unsigned char)*sov])
1220 sov++;
1221
1222 for (h = cap_hdr; h; h = h->next) {
1223 if ((h->namelen == col - sol) &&
1224 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1225 if (cap[h->index] == NULL)
1226 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001227 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001228
1229 if (cap[h->index] == NULL) {
1230 Alert("HTTP capture : out of memory.\n");
1231 continue;
1232 }
1233
1234 len = eol - sov;
1235 if (len > h->len)
1236 len = h->len;
1237
1238 memcpy(cap[h->index], sov, len);
1239 cap[h->index][len]=0;
1240 }
1241 }
1242 sol = eol + idx->v[cur_idx].cr + 1;
1243 cur_idx = idx->v[cur_idx].next;
1244 }
1245}
1246
1247
Willy Tarreau42250582007-04-01 01:30:43 +02001248/* either we find an LF at <ptr> or we jump to <bad>.
1249 */
1250#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1251
1252/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1253 * otherwise to <http_msg_ood> with <state> set to <st>.
1254 */
1255#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1256 ptr++; \
1257 if (likely(ptr < end)) \
1258 goto good; \
1259 else { \
1260 state = (st); \
1261 goto http_msg_ood; \
1262 } \
1263 } while (0)
1264
1265
Willy Tarreaubaaee002006-06-26 02:48:02 +02001266/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001267 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1269 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1270 * will give undefined results.
1271 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1272 * and that msg->sol points to the beginning of the response.
1273 * If a complete line is found (which implies that at least one CR or LF is
1274 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1275 * returned indicating an incomplete line (which does not mean that parts have
1276 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1277 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1278 * upon next call.
1279 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001280 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001281 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1282 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001283 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001285const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1286 unsigned int state, const char *ptr, const char *end,
1287 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001288{
Willy Tarreau8973c702007-01-21 23:58:29 +01001289 switch (state) {
1290 http_msg_rpver:
1291 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001292 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1294
1295 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001296 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001297 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1298 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001299 state = HTTP_MSG_ERROR;
1300 break;
1301
Willy Tarreau8973c702007-01-21 23:58:29 +01001302 http_msg_rpver_sp:
1303 case HTTP_MSG_RPVER_SP:
1304 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001305 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001306 goto http_msg_rpcode;
1307 }
1308 if (likely(HTTP_IS_SPHT(*ptr)))
1309 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1310 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001311 state = HTTP_MSG_ERROR;
1312 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001313
1314 http_msg_rpcode:
1315 case HTTP_MSG_RPCODE:
1316 if (likely(!HTTP_IS_LWS(*ptr)))
1317 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1318
1319 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001320 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1322 }
1323
1324 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001325 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001326 http_msg_rsp_reason:
1327 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001328 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001329 msg->sl.st.r_l = 0;
1330 goto http_msg_rpline_eol;
1331
1332 http_msg_rpcode_sp:
1333 case HTTP_MSG_RPCODE_SP:
1334 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001335 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 goto http_msg_rpreason;
1337 }
1338 if (likely(HTTP_IS_SPHT(*ptr)))
1339 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1340 /* so it's a CR/LF, so there is no reason phrase */
1341 goto http_msg_rsp_reason;
1342
1343 http_msg_rpreason:
1344 case HTTP_MSG_RPREASON:
1345 if (likely(!HTTP_IS_CRLF(*ptr)))
1346 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001347 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 http_msg_rpline_eol:
1349 /* We have seen the end of line. Note that we do not
1350 * necessarily have the \n yet, but at least we know that we
1351 * have EITHER \r OR \n, otherwise the response would not be
1352 * complete. We can then record the response length and return
1353 * to the caller which will be able to register it.
1354 */
1355 msg->sl.st.l = ptr - msg->sol;
1356 return ptr;
1357
1358#ifdef DEBUG_FULL
1359 default:
1360 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1361 exit(1);
1362#endif
1363 }
1364
1365 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001366 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001367 if (ret_state)
1368 *ret_state = state;
1369 if (ret_ptr)
1370 *ret_ptr = (char *)ptr;
1371 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001372}
1373
Willy Tarreau8973c702007-01-21 23:58:29 +01001374/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001375 * This function parses a request line between <ptr> and <end>, starting with
1376 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1377 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1378 * will give undefined results.
1379 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1380 * and that msg->sol points to the beginning of the request.
1381 * If a complete line is found (which implies that at least one CR or LF is
1382 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1383 * returned indicating an incomplete line (which does not mean that parts have
1384 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1385 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1386 * upon next call.
1387 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001388 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1390 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001391 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001392 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001393const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1394 unsigned int state, const char *ptr, const char *end,
1395 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001396{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 switch (state) {
1398 http_msg_rqmeth:
1399 case HTTP_MSG_RQMETH:
1400 if (likely(HTTP_IS_TOKEN(*ptr)))
1401 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001404 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1406 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001407
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 if (likely(HTTP_IS_CRLF(*ptr))) {
1409 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001410 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001412 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001414 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001416 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 msg->sl.rq.v_l = 0;
1418 goto http_msg_rqline_eol;
1419 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001420 state = HTTP_MSG_ERROR;
1421 break;
1422
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 http_msg_rqmeth_sp:
1424 case HTTP_MSG_RQMETH_SP:
1425 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001426 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 goto http_msg_rquri;
1428 }
1429 if (likely(HTTP_IS_SPHT(*ptr)))
1430 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1431 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1432 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 http_msg_rquri:
1435 case HTTP_MSG_RQURI:
1436 if (likely(!HTTP_IS_LWS(*ptr)))
1437 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001440 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1442 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001443
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1445 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 http_msg_rquri_sp:
1448 case HTTP_MSG_RQURI_SP:
1449 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001450 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 goto http_msg_rqver;
1452 }
1453 if (likely(HTTP_IS_SPHT(*ptr)))
1454 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1455 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1456 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001457
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 http_msg_rqver:
1459 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001460 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001462
1463 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001464 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001465 http_msg_rqline_eol:
1466 /* We have seen the end of line. Note that we do not
1467 * necessarily have the \n yet, but at least we know that we
1468 * have EITHER \r OR \n, otherwise the request would not be
1469 * complete. We can then record the request length and return
1470 * to the caller which will be able to register it.
1471 */
1472 msg->sl.rq.l = ptr - msg->sol;
1473 return ptr;
1474 }
1475
1476 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001477 state = HTTP_MSG_ERROR;
1478 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001479
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001480#ifdef DEBUG_FULL
1481 default:
1482 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1483 exit(1);
1484#endif
1485 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001488 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 if (ret_state)
1490 *ret_state = state;
1491 if (ret_ptr)
1492 *ret_ptr = (char *)ptr;
1493 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001495
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001496/*
1497 * Returns the data from Authorization header. Function may be called more
1498 * than once so data is stored in txn->auth_data. When no header is found
1499 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1500 * searching again for something we are unable to find anyway.
1501 */
1502
1503char get_http_auth_buff[BUFSIZE];
1504
1505int
1506get_http_auth(struct session *s)
1507{
1508
1509 struct http_txn *txn = &s->txn;
1510 struct chunk auth_method;
1511 struct hdr_ctx ctx;
1512 char *h, *p;
1513 int len;
1514
1515#ifdef DEBUG_AUTH
1516 printf("Auth for session %p: %d\n", s, txn->auth.method);
1517#endif
1518
1519 if (txn->auth.method == HTTP_AUTH_WRONG)
1520 return 0;
1521
1522 if (txn->auth.method)
1523 return 1;
1524
1525 txn->auth.method = HTTP_AUTH_WRONG;
1526
1527 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001528
1529 if (txn->flags & TX_USE_PX_CONN) {
1530 h = "Proxy-Authorization";
1531 len = strlen(h);
1532 } else {
1533 h = "Authorization";
1534 len = strlen(h);
1535 }
1536
1537 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001538 return 0;
1539
1540 h = ctx.line + ctx.val;
1541
1542 p = memchr(h, ' ', ctx.vlen);
1543 if (!p || p == h)
1544 return 0;
1545
1546 chunk_initlen(&auth_method, h, 0, p-h);
1547 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1548
1549 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1550
1551 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1552 get_http_auth_buff, BUFSIZE - 1);
1553
1554 if (len < 0)
1555 return 0;
1556
1557
1558 get_http_auth_buff[len] = '\0';
1559
1560 p = strchr(get_http_auth_buff, ':');
1561
1562 if (!p)
1563 return 0;
1564
1565 txn->auth.user = get_http_auth_buff;
1566 *p = '\0';
1567 txn->auth.pass = p+1;
1568
1569 txn->auth.method = HTTP_AUTH_BASIC;
1570 return 1;
1571 }
1572
1573 return 0;
1574}
1575
Willy Tarreau58f10d72006-12-04 02:26:12 +01001576
Willy Tarreau8973c702007-01-21 23:58:29 +01001577/*
1578 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001579 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001580 * when data are missing and recalled at the exact same location with no
1581 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001582 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001583 * fields. Note that msg->som and msg->sol will be initialized after completing
1584 * the first state, so that none of the msg pointers has to be initialized
1585 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001586 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1588{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001589 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001591
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001592 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 ptr = buf->lr;
1594 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001595
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 if (unlikely(ptr >= end))
1597 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001598
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001600 /*
1601 * First, states that are specific to the response only.
1602 * We check them first so that request and headers are
1603 * closer to each other (accessed more often).
1604 */
1605 http_msg_rpbefore:
1606 case HTTP_MSG_RPBEFORE:
1607 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001608 /* we have a start of message, but we have to check
1609 * first if we need to remove some CRLF. We can only
1610 * do this when send_max=0.
1611 */
1612 char *beg = buf->w + buf->send_max;
1613 if (beg >= buf->data + buf->size)
1614 beg -= buf->size;
1615 if (unlikely(ptr != beg)) {
1616 if (buf->send_max)
1617 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001618 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001619 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001620 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001621 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001622 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001623 hdr_idx_init(idx);
1624 state = HTTP_MSG_RPVER;
1625 goto http_msg_rpver;
1626 }
1627
1628 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1629 goto http_msg_invalid;
1630
1631 if (unlikely(*ptr == '\n'))
1632 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1633 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1634 /* stop here */
1635
1636 http_msg_rpbefore_cr:
1637 case HTTP_MSG_RPBEFORE_CR:
1638 EXPECT_LF_HERE(ptr, http_msg_invalid);
1639 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1640 /* stop here */
1641
1642 http_msg_rpver:
1643 case HTTP_MSG_RPVER:
1644 case HTTP_MSG_RPVER_SP:
1645 case HTTP_MSG_RPCODE:
1646 case HTTP_MSG_RPCODE_SP:
1647 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001648 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001649 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001650 if (unlikely(!ptr))
1651 return;
1652
1653 /* we have a full response and we know that we have either a CR
1654 * or an LF at <ptr>.
1655 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001656 //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 +01001657 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1658
1659 msg->sol = ptr;
1660 if (likely(*ptr == '\r'))
1661 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1662 goto http_msg_rpline_end;
1663
1664 http_msg_rpline_end:
1665 case HTTP_MSG_RPLINE_END:
1666 /* msg->sol must point to the first of CR or LF. */
1667 EXPECT_LF_HERE(ptr, http_msg_invalid);
1668 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1669 /* stop here */
1670
1671 /*
1672 * Second, states that are specific to the request only
1673 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 http_msg_rqbefore:
1675 case HTTP_MSG_RQBEFORE:
1676 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001677 /* we have a start of message, but we have to check
1678 * first if we need to remove some CRLF. We can only
1679 * do this when send_max=0.
1680 */
1681 char *beg = buf->w + buf->send_max;
1682 if (beg >= buf->data + buf->size)
1683 beg -= buf->size;
1684 if (likely(ptr != beg)) {
1685 if (buf->send_max)
1686 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001687 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001688 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001690 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001691 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001692 /* we will need this when keep-alive will be supported
1693 hdr_idx_init(idx);
1694 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001695 state = HTTP_MSG_RQMETH;
1696 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001698
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001699 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1700 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001701
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001702 if (unlikely(*ptr == '\n'))
1703 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1704 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001705 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001706
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 http_msg_rqbefore_cr:
1708 case HTTP_MSG_RQBEFORE_CR:
1709 EXPECT_LF_HERE(ptr, http_msg_invalid);
1710 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001711 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001712
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 http_msg_rqmeth:
1714 case HTTP_MSG_RQMETH:
1715 case HTTP_MSG_RQMETH_SP:
1716 case HTTP_MSG_RQURI:
1717 case HTTP_MSG_RQURI_SP:
1718 case HTTP_MSG_RQVER:
1719 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001720 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 if (unlikely(!ptr))
1722 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001723
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 /* we have a full request and we know that we have either a CR
1725 * or an LF at <ptr>.
1726 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001727 //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 +01001728 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001729
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 msg->sol = ptr;
1731 if (likely(*ptr == '\r'))
1732 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001734
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001735 http_msg_rqline_end:
1736 case HTTP_MSG_RQLINE_END:
1737 /* check for HTTP/0.9 request : no version information available.
1738 * msg->sol must point to the first of CR or LF.
1739 */
1740 if (unlikely(msg->sl.rq.v_l == 0))
1741 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001742
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 EXPECT_LF_HERE(ptr, http_msg_invalid);
1744 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001745 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001746
Willy Tarreau8973c702007-01-21 23:58:29 +01001747 /*
1748 * Common states below
1749 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001750 http_msg_hdr_first:
1751 case HTTP_MSG_HDR_FIRST:
1752 msg->sol = ptr;
1753 if (likely(!HTTP_IS_CRLF(*ptr))) {
1754 goto http_msg_hdr_name;
1755 }
1756
1757 if (likely(*ptr == '\r'))
1758 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1759 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001760
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001761 http_msg_hdr_name:
1762 case HTTP_MSG_HDR_NAME:
1763 /* assumes msg->sol points to the first char */
1764 if (likely(HTTP_IS_TOKEN(*ptr)))
1765 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001766
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001767 if (likely(*ptr == ':')) {
1768 msg->col = ptr - buf->data;
1769 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1770 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001771
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001772 if (likely(msg->err_pos < -1) || *ptr == '\n')
1773 goto http_msg_invalid;
1774
1775 if (msg->err_pos == -1) /* capture error pointer */
1776 msg->err_pos = ptr - buf->data; /* >= 0 now */
1777
1778 /* and we still accept this non-token character */
1779 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001780
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001781 http_msg_hdr_l1_sp:
1782 case HTTP_MSG_HDR_L1_SP:
1783 /* assumes msg->sol points to the first char and msg->col to the colon */
1784 if (likely(HTTP_IS_SPHT(*ptr)))
1785 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001786
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001787 /* header value can be basically anything except CR/LF */
1788 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001789
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 if (likely(!HTTP_IS_CRLF(*ptr))) {
1791 goto http_msg_hdr_val;
1792 }
1793
1794 if (likely(*ptr == '\r'))
1795 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1796 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001797
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001798 http_msg_hdr_l1_lf:
1799 case HTTP_MSG_HDR_L1_LF:
1800 EXPECT_LF_HERE(ptr, http_msg_invalid);
1801 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001802
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001803 http_msg_hdr_l1_lws:
1804 case HTTP_MSG_HDR_L1_LWS:
1805 if (likely(HTTP_IS_SPHT(*ptr))) {
1806 /* replace HT,CR,LF with spaces */
1807 for (; buf->data+msg->sov < ptr; msg->sov++)
1808 buf->data[msg->sov] = ' ';
1809 goto http_msg_hdr_l1_sp;
1810 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001811 /* we had a header consisting only in spaces ! */
1812 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001813 goto http_msg_complete_header;
1814
1815 http_msg_hdr_val:
1816 case HTTP_MSG_HDR_VAL:
1817 /* assumes msg->sol points to the first char, msg->col to the
1818 * colon, and msg->sov points to the first character of the
1819 * value.
1820 */
1821 if (likely(!HTTP_IS_CRLF(*ptr)))
1822 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001823
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 msg->eol = ptr;
1825 /* Note: we could also copy eol into ->eoh so that we have the
1826 * real header end in case it ends with lots of LWS, but is this
1827 * really needed ?
1828 */
1829 if (likely(*ptr == '\r'))
1830 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1831 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001832
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001833 http_msg_hdr_l2_lf:
1834 case HTTP_MSG_HDR_L2_LF:
1835 EXPECT_LF_HERE(ptr, http_msg_invalid);
1836 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001837
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 http_msg_hdr_l2_lws:
1839 case HTTP_MSG_HDR_L2_LWS:
1840 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1841 /* LWS: replace HT,CR,LF with spaces */
1842 for (; msg->eol < ptr; msg->eol++)
1843 *msg->eol = ' ';
1844 goto http_msg_hdr_val;
1845 }
1846 http_msg_complete_header:
1847 /*
1848 * It was a new header, so the last one is finished.
1849 * Assumes msg->sol points to the first char, msg->col to the
1850 * colon, msg->sov points to the first character of the value
1851 * and msg->eol to the first CR or LF so we know how the line
1852 * ends. We insert last header into the index.
1853 */
1854 /*
1855 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1856 write(2, msg->sol, msg->eol-msg->sol);
1857 fprintf(stderr,"\n");
1858 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001859
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001860 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1861 idx, idx->tail) < 0))
1862 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001863
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001864 msg->sol = ptr;
1865 if (likely(!HTTP_IS_CRLF(*ptr))) {
1866 goto http_msg_hdr_name;
1867 }
1868
1869 if (likely(*ptr == '\r'))
1870 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1871 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001872
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001873 http_msg_last_lf:
1874 case HTTP_MSG_LAST_LF:
1875 /* Assumes msg->sol points to the first of either CR or LF */
1876 EXPECT_LF_HERE(ptr, http_msg_invalid);
1877 ptr++;
1878 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001879 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001880 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001881 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001882 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 return;
1884#ifdef DEBUG_FULL
1885 default:
1886 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1887 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001888#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001889 }
1890 http_msg_ood:
1891 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001892 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001893 buf->lr = ptr;
1894 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001895
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 http_msg_invalid:
1897 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001898 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001899 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001900 return;
1901}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001902
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001903/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1904 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1905 * nothing is done and 1 is returned.
1906 */
1907static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1908{
1909 int delta;
1910 char *cur_end;
1911
1912 if (msg->sl.rq.v_l != 0)
1913 return 1;
1914
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001915 cur_end = msg->sol + msg->sl.rq.l;
1916 delta = 0;
1917
1918 if (msg->sl.rq.u_l == 0) {
1919 /* if no URI was set, add "/" */
1920 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1921 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001922 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001923 }
1924 /* add HTTP version */
1925 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001926 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001927 cur_end += delta;
1928 cur_end = (char *)http_parse_reqline(msg, req->data,
1929 HTTP_MSG_RQMETH,
1930 msg->sol, cur_end + 1,
1931 NULL, NULL);
1932 if (unlikely(!cur_end))
1933 return 0;
1934
1935 /* we have a full HTTP/1.0 request now and we know that
1936 * we have either a CR or an LF at <ptr>.
1937 */
1938 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1939 return 1;
1940}
1941
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001942/* Parse the Connection: header of an HTTP request, looking for both "close"
1943 * and "keep-alive" values. If a buffer is provided and we already know that
1944 * some headers may safely be removed, we remove them now. The <to_del> flags
1945 * are used for that :
1946 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1947 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1948 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1949 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1950 * harmless combinations may be removed. Do not call that after changes have
1951 * been processed. If unused, the buffer can be NULL, and no data will be
1952 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001953 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001954void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001955{
Willy Tarreau5b154472009-12-21 20:11:07 +01001956 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001957 const char *hdr_val = "Connection";
1958 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001959
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001960 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001961 return;
1962
Willy Tarreau88d349d2010-01-25 12:15:43 +01001963 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1964 hdr_val = "Proxy-Connection";
1965 hdr_len = 16;
1966 }
1967
Willy Tarreau5b154472009-12-21 20:11:07 +01001968 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001969 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001970 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001971 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1972 txn->flags |= TX_HDR_CONN_KAL;
1973 if ((to_del & 2) && buf)
1974 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1975 else
1976 txn->flags |= TX_CON_KAL_SET;
1977 }
1978 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1979 txn->flags |= TX_HDR_CONN_CLO;
1980 if ((to_del & 1) && buf)
1981 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1982 else
1983 txn->flags |= TX_CON_CLO_SET;
1984 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001985 }
1986
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001987 txn->flags |= TX_HDR_CONN_PRS;
1988 return;
1989}
Willy Tarreau5b154472009-12-21 20:11:07 +01001990
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001991/* Apply desired changes on the Connection: header. Values may be removed and/or
1992 * added depending on the <wanted> flags, which are exclusively composed of
1993 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1994 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1995 */
1996void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
1997{
1998 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001999 const char *hdr_val = "Connection";
2000 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002001
2002 ctx.idx = 0;
2003
Willy Tarreau88d349d2010-01-25 12:15:43 +01002004
2005 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2006 hdr_val = "Proxy-Connection";
2007 hdr_len = 16;
2008 }
2009
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002010 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002011 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002012 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2013 if (wanted & TX_CON_KAL_SET)
2014 txn->flags |= TX_CON_KAL_SET;
2015 else
2016 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002017 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002018 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2019 if (wanted & TX_CON_CLO_SET)
2020 txn->flags |= TX_CON_CLO_SET;
2021 else
2022 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002023 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002024 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002025
2026 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2027 return;
2028
2029 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2030 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002031 hdr_val = "Connection: close";
2032 hdr_len = 17;
2033 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2034 hdr_val = "Proxy-Connection: close";
2035 hdr_len = 23;
2036 }
2037 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002038 }
2039
2040 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2041 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002042 hdr_val = "Connection: keep-alive";
2043 hdr_len = 22;
2044 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2045 hdr_val = "Proxy-Connection: keep-alive";
2046 hdr_len = 28;
2047 }
2048 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002049 }
2050 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002051}
2052
Willy Tarreaud98cf932009-12-27 22:54:55 +01002053/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2054 * first byte of body, and increments msg->sov by the number of bytes parsed,
2055 * so that we know we can forward between ->som and ->sov. Note that due to
2056 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2057 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002058 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002059 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002060 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002061int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002062{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002063 char *ptr = buf->lr;
2064 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002065 unsigned int chunk = 0;
2066
2067 /* The chunk size is in the following form, though we are only
2068 * interested in the size and CRLF :
2069 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2070 */
2071 while (1) {
2072 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002073 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002074 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002075 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002076 if (c < 0) /* not a hex digit anymore */
2077 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002078 if (++ptr >= end)
2079 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002080 if (chunk & 0xF000000) /* overflow will occur */
2081 return -1;
2082 chunk = (chunk << 4) + c;
2083 }
2084
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085 /* empty size not allowed */
2086 if (ptr == buf->lr)
2087 return -1;
2088
2089 while (http_is_spht[(unsigned char)*ptr]) {
2090 if (++ptr >= end)
2091 ptr = buf->data;
2092 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002093 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002094 }
2095
Willy Tarreaud98cf932009-12-27 22:54:55 +01002096 /* Up to there, we know that at least one byte is present at *ptr. Check
2097 * for the end of chunk size.
2098 */
2099 while (1) {
2100 if (likely(HTTP_IS_CRLF(*ptr))) {
2101 /* we now have a CR or an LF at ptr */
2102 if (likely(*ptr == '\r')) {
2103 if (++ptr >= end)
2104 ptr = buf->data;
2105 if (ptr == buf->r)
2106 return 0;
2107 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002108
Willy Tarreaud98cf932009-12-27 22:54:55 +01002109 if (*ptr != '\n')
2110 return -1;
2111 if (++ptr >= end)
2112 ptr = buf->data;
2113 /* done */
2114 break;
2115 }
2116 else if (*ptr == ';') {
2117 /* chunk extension, ends at next CRLF */
2118 if (++ptr >= end)
2119 ptr = buf->data;
2120 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002121 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122
2123 while (!HTTP_IS_CRLF(*ptr)) {
2124 if (++ptr >= end)
2125 ptr = buf->data;
2126 if (ptr == buf->r)
2127 return 0;
2128 }
2129 /* we have a CRLF now, loop above */
2130 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002131 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002132 else
Willy Tarreau115acb92009-12-26 13:56:06 +01002133 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002134 }
2135
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136 /* OK we found our CRLF and now <ptr> points to the next byte,
2137 * which may or may not be present. We save that into ->lr and
2138 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002139 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002140 msg->sov += ptr - buf->lr;
2141 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01002142 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002143 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002144 return 1;
2145}
2146
Willy Tarreaud98cf932009-12-27 22:54:55 +01002147/* This function skips trailers in the buffer <buf> associated with HTTP
2148 * message <msg>. The first visited position is buf->lr. If the end of
2149 * the trailers is found, it is automatically scheduled to be forwarded,
2150 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2151 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002152 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2153 * zero. If a parse error is encountered, the function returns < 0 and does not
2154 * change anything except maybe buf->lr and msg->sov. Note that the message
2155 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2156 * which implies that all non-trailers data have already been scheduled for
2157 * forwarding, and that the difference between msg->som and msg->sov exactly
2158 * matches the length of trailers already parsed and not forwarded. It is also
2159 * important to note that this function is designed to be able to parse wrapped
2160 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002161 */
2162int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2163{
2164 /* we have buf->lr which points to next line. Look for CRLF. */
2165 while (1) {
2166 char *p1 = NULL, *p2 = NULL;
2167 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002168 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002169
2170 /* scan current line and stop at LF or CRLF */
2171 while (1) {
2172 if (ptr == buf->r)
2173 return 0;
2174
2175 if (*ptr == '\n') {
2176 if (!p1)
2177 p1 = ptr;
2178 p2 = ptr;
2179 break;
2180 }
2181
2182 if (*ptr == '\r') {
2183 if (p1)
2184 return -1;
2185 p1 = ptr;
2186 }
2187
2188 ptr++;
2189 if (ptr >= buf->data + buf->size)
2190 ptr = buf->data;
2191 }
2192
2193 /* after LF; point to beginning of next line */
2194 p2++;
2195 if (p2 >= buf->data + buf->size)
2196 p2 = buf->data;
2197
Willy Tarreau638cd022010-01-03 07:42:04 +01002198 bytes = p2 - buf->lr;
2199 if (bytes < 0)
2200 bytes += buf->size;
2201
2202 /* schedule this line for forwarding */
2203 msg->sov += bytes;
2204 if (msg->sov >= buf->size)
2205 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002206
Willy Tarreau638cd022010-01-03 07:42:04 +01002207 if (p1 == buf->lr) {
2208 /* LF/CRLF at beginning of line => end of trailers at p2.
2209 * Everything was scheduled for forwarding, there's nothing
2210 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002211 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002212 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002213 msg->msg_state = HTTP_MSG_DONE;
2214 return 1;
2215 }
2216 /* OK, next line then */
2217 buf->lr = p2;
2218 }
2219}
2220
2221/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2222 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2223 * ->som, buf->lr in order to include this part into the next forwarding phase.
2224 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2225 * not enough data are available, the function does not change anything and
2226 * returns zero. If a parse error is encountered, the function returns < 0 and
2227 * does not change anything. Note: this function is designed to parse wrapped
2228 * CRLF at the end of the buffer.
2229 */
2230int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2231{
2232 char *ptr;
2233 int bytes;
2234
2235 /* NB: we'll check data availabilty at the end. It's not a
2236 * problem because whatever we match first will be checked
2237 * against the correct length.
2238 */
2239 bytes = 1;
2240 ptr = buf->lr;
2241 if (*ptr == '\r') {
2242 bytes++;
2243 ptr++;
2244 if (ptr >= buf->data + buf->size)
2245 ptr = buf->data;
2246 }
2247
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002248 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002249 return 0;
2250
2251 if (*ptr != '\n')
2252 return -1;
2253
2254 ptr++;
2255 if (ptr >= buf->data + buf->size)
2256 ptr = buf->data;
2257 buf->lr = ptr;
2258 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2259 msg->sov = ptr - buf->data;
2260 msg->som = msg->sov - bytes;
2261 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2262 return 1;
2263}
Willy Tarreau5b154472009-12-21 20:11:07 +01002264
Willy Tarreau83e3af02009-12-28 17:39:57 +01002265void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2266{
2267 char *end = buf->data + buf->size;
2268 int off = buf->data + buf->size - buf->w;
2269
2270 /* two possible cases :
2271 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002272 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002273 */
2274 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002275 int block1 = buf->l;
2276 int block2 = 0;
2277 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002278 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002279 block1 = buf->data + buf->size - buf->w;
2280 block2 = buf->r - buf->data;
2281 }
2282 if (block2)
2283 memcpy(swap_buffer, buf->data, block2);
2284 memmove(buf->data, buf->w, block1);
2285 if (block2)
2286 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002287 }
2288
2289 /* adjust all known pointers */
2290 buf->w = buf->data;
2291 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2292 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2293 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2294 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2295
2296 /* adjust relative pointers */
2297 msg->som = 0;
2298 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2299 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2300 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2301
Willy Tarreau83e3af02009-12-28 17:39:57 +01002302 if (msg->err_pos >= 0) {
2303 msg->err_pos += off;
2304 if (msg->err_pos >= buf->size)
2305 msg->err_pos -= buf->size;
2306 }
2307
2308 buf->flags &= ~BF_FULL;
2309 if (buf->l >= buffer_max_len(buf))
2310 buf->flags |= BF_FULL;
2311}
2312
Willy Tarreaud787e662009-07-07 10:14:51 +02002313/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2314 * processing can continue on next analysers, or zero if it either needs more
2315 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2316 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2317 * when it has nothing left to do, and may remove any analyser when it wants to
2318 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002319 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002320int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002321{
Willy Tarreau59234e92008-11-30 23:51:27 +01002322 /*
2323 * We will parse the partial (or complete) lines.
2324 * We will check the request syntax, and also join multi-line
2325 * headers. An index of all the lines will be elaborated while
2326 * parsing.
2327 *
2328 * For the parsing, we use a 28 states FSM.
2329 *
2330 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002331 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002332 * req->data + msg->eoh = end of processed headers / start of current one
2333 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002334 * req->lr = first non-visited byte
2335 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002336 *
2337 * At end of parsing, we may perform a capture of the error (if any), and
2338 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2339 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2340 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002342
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002344 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 struct http_txn *txn = &s->txn;
2346 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002347 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002348
Willy Tarreau6bf17362009-02-24 10:48:35 +01002349 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2350 now_ms, __FUNCTION__,
2351 s,
2352 req,
2353 req->rex, req->wex,
2354 req->flags,
2355 req->l,
2356 req->analysers);
2357
Willy Tarreau52a0c602009-08-16 22:45:38 +02002358 /* we're speaking HTTP here, so let's speak HTTP to the client */
2359 s->srv_error = http_return_srv_error;
2360
Willy Tarreau83e3af02009-12-28 17:39:57 +01002361 /* There's a protected area at the end of the buffer for rewriting
2362 * purposes. We don't want to start to parse the request if the
2363 * protected area is affected, because we may have to move processed
2364 * data later, which is much more complicated.
2365 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002366 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002367 if ((txn->flags & TX_NOT_FIRST) &&
2368 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002369 req->r < req->lr ||
2370 req->r > req->data + req->size - global.tune.maxrewrite)) {
2371 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002372 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2373 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002374 /* some data has still not left the buffer, wake us once that's done */
2375 buffer_dont_connect(req);
2376 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2377 return 0;
2378 }
2379 if (req->l <= req->size - global.tune.maxrewrite)
2380 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002381 }
2382
Willy Tarreau065e8332010-01-08 00:30:20 +01002383 /* Note that we have the same problem with the response ; we
2384 * may want to send a redirect, error or anything which requires
2385 * some spare space. So we'll ensure that we have at least
2386 * maxrewrite bytes available in the response buffer before
2387 * processing that one. This will only affect pipelined
2388 * keep-alive requests.
2389 */
2390 if ((txn->flags & TX_NOT_FIRST) &&
2391 unlikely((s->rep->flags & BF_FULL) ||
2392 s->rep->r < s->rep->lr ||
2393 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2394 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002395 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2396 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002397 /* don't let a connection request be initiated */
2398 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002399 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau065e8332010-01-08 00:30:20 +01002400 return 0;
2401 }
2402 }
2403
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002404 if (likely(req->lr < req->r))
2405 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002406 }
2407
Willy Tarreau59234e92008-11-30 23:51:27 +01002408 /* 1: we might have to print this header in debug mode */
2409 if (unlikely((global.mode & MODE_DEBUG) &&
2410 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002411 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002412 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002413 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002414
Willy Tarreau962c3f42010-01-10 00:15:35 +01002415 sol = msg->sol;
Willy Tarreau59234e92008-11-30 23:51:27 +01002416 eol = sol + msg->sl.rq.l;
2417 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002418
Willy Tarreau59234e92008-11-30 23:51:27 +01002419 sol += hdr_idx_first_pos(&txn->hdr_idx);
2420 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002421
Willy Tarreau59234e92008-11-30 23:51:27 +01002422 while (cur_idx) {
2423 eol = sol + txn->hdr_idx.v[cur_idx].len;
2424 debug_hdr("clihdr", s, sol, eol);
2425 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2426 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002427 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002428 }
2429
Willy Tarreau58f10d72006-12-04 02:26:12 +01002430
Willy Tarreau59234e92008-11-30 23:51:27 +01002431 /*
2432 * Now we quickly check if we have found a full valid request.
2433 * If not so, we check the FD and buffer states before leaving.
2434 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002435 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002436 * requests are checked first. When waiting for a second request
2437 * on a keep-alive session, if we encounter and error, close, t/o,
2438 * we note the error in the session flags but don't set any state.
2439 * Since the error will be noted there, it will not be counted by
2440 * process_session() as a frontend error.
Willy Tarreau59234e92008-11-30 23:51:27 +01002441 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002442
Willy Tarreau655dce92009-11-08 13:10:58 +01002443 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002444 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002445 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002446 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002447 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2448 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002449 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002450 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002451
Willy Tarreau59234e92008-11-30 23:51:27 +01002452 /* 1: Since we are in header mode, if there's no space
2453 * left for headers, we won't be able to free more
2454 * later, so the session will never terminate. We
2455 * must terminate it now.
2456 */
2457 if (unlikely(req->flags & BF_FULL)) {
2458 /* FIXME: check if URI is set and return Status
2459 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002460 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002461 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002462 goto return_bad_req;
2463 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002464
Willy Tarreau59234e92008-11-30 23:51:27 +01002465 /* 2: have we encountered a read error ? */
2466 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002467 if (!(s->flags & SN_ERR_MASK))
2468 s->flags |= SN_ERR_CLICL;
2469
Willy Tarreaufcffa692010-01-10 14:21:19 +01002470 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002471 goto failed_keep_alive;
2472
Willy Tarreau59234e92008-11-30 23:51:27 +01002473 /* we cannot return any message on error */
Willy Tarreau4076a152009-04-02 15:18:36 +02002474 if (msg->err_pos >= 0)
2475 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002476 msg->msg_state = HTTP_MSG_ERROR;
2477 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002478
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002479 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002480 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002481 if (s->listener->counters)
2482 s->listener->counters->failed_req++;
2483
Willy Tarreau59234e92008-11-30 23:51:27 +01002484 if (!(s->flags & SN_FINST_MASK))
2485 s->flags |= SN_FINST_R;
2486 return 0;
2487 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002488
Willy Tarreau59234e92008-11-30 23:51:27 +01002489 /* 3: has the read timeout expired ? */
2490 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002491 if (!(s->flags & SN_ERR_MASK))
2492 s->flags |= SN_ERR_CLITO;
2493
Willy Tarreaufcffa692010-01-10 14:21:19 +01002494 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002495 goto failed_keep_alive;
2496
Willy Tarreau59234e92008-11-30 23:51:27 +01002497 /* read timeout : give up with an error message. */
Willy Tarreau4076a152009-04-02 15:18:36 +02002498 if (msg->err_pos >= 0)
2499 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002500 txn->status = 408;
2501 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2502 msg->msg_state = HTTP_MSG_ERROR;
2503 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002504
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002505 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002506 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002507 if (s->listener->counters)
2508 s->listener->counters->failed_req++;
2509
Willy Tarreau59234e92008-11-30 23:51:27 +01002510 if (!(s->flags & SN_FINST_MASK))
2511 s->flags |= SN_FINST_R;
2512 return 0;
2513 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002514
Willy Tarreau59234e92008-11-30 23:51:27 +01002515 /* 4: have we encountered a close ? */
2516 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002517 if (!(s->flags & SN_ERR_MASK))
2518 s->flags |= SN_ERR_CLICL;
2519
Willy Tarreaufcffa692010-01-10 14:21:19 +01002520 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002521 goto failed_keep_alive;
2522
Willy Tarreau4076a152009-04-02 15:18:36 +02002523 if (msg->err_pos >= 0)
2524 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002525 txn->status = 400;
2526 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2527 msg->msg_state = HTTP_MSG_ERROR;
2528 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002529
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002530 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002531 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002532 if (s->listener->counters)
2533 s->listener->counters->failed_req++;
2534
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 if (!(s->flags & SN_FINST_MASK))
2536 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002537 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002538 }
2539
Willy Tarreau520d95e2009-09-19 21:04:57 +02002540 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002541 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002542 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002543
Willy Tarreaufcffa692010-01-10 14:21:19 +01002544 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2545 /* If the client starts to talk, let's fall back to
2546 * request timeout processing.
2547 */
2548 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002549 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002550 }
2551
Willy Tarreau59234e92008-11-30 23:51:27 +01002552 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002553 if (!tick_isset(req->analyse_exp)) {
2554 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2555 (txn->flags & TX_WAIT_NEXT_RQ) &&
2556 tick_isset(s->be->timeout.httpka))
2557 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2558 else
2559 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2560 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002561
Willy Tarreau59234e92008-11-30 23:51:27 +01002562 /* we're not ready yet */
2563 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002564
2565 failed_keep_alive:
2566 /* Here we process low-level errors for keep-alive requests. In
2567 * short, if the request is not the first one and it experiences
2568 * a timeout, read error or shutdown, we just silently close so
2569 * that the client can try again.
2570 */
2571 txn->status = 0;
2572 msg->msg_state = HTTP_MSG_RQBEFORE;
2573 req->analysers = 0;
2574 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002575 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002576 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002577 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002578 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002579
Willy Tarreaud787e662009-07-07 10:14:51 +02002580 /* OK now we have a complete HTTP request with indexed headers. Let's
2581 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002582 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2583 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2584 * points to the CRLF of the request line. req->lr points to the first
2585 * byte after the last LF. msg->col and msg->sov point to the first
2586 * byte of data. msg->eol cannot be trusted because it may have been
2587 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002588 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002589
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002590 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2591
Willy Tarreaub16a5742010-01-10 14:46:16 +01002592 if (txn->flags & TX_WAIT_NEXT_RQ) {
2593 /* kill the pending keep-alive timeout */
2594 txn->flags &= ~TX_WAIT_NEXT_RQ;
2595 req->analyse_exp = TICK_ETERNITY;
2596 }
2597
2598
Willy Tarreaud787e662009-07-07 10:14:51 +02002599 /* Maybe we found in invalid header name while we were configured not
2600 * to block on that, so we have to capture it now.
2601 */
2602 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002603 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2604
Willy Tarreau59234e92008-11-30 23:51:27 +01002605 /*
2606 * 1: identify the method
2607 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002608 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002609
2610 /* we can make use of server redirect on GET and HEAD */
2611 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2612 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002613
Willy Tarreau59234e92008-11-30 23:51:27 +01002614 /*
2615 * 2: check if the URI matches the monitor_uri.
2616 * We have to do this for every request which gets in, because
2617 * the monitor-uri is defined by the frontend.
2618 */
2619 if (unlikely((s->fe->monitor_uri_len != 0) &&
2620 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002621 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002622 s->fe->monitor_uri,
2623 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002624 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002625 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002626 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002627 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002628
Willy Tarreau59234e92008-11-30 23:51:27 +01002629 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002630
Willy Tarreau59234e92008-11-30 23:51:27 +01002631 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002632 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2633 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002634
Willy Tarreau59234e92008-11-30 23:51:27 +01002635 ret = acl_pass(ret);
2636 if (cond->pol == ACL_COND_UNLESS)
2637 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002638
Willy Tarreau59234e92008-11-30 23:51:27 +01002639 if (ret) {
2640 /* we fail this request, let's return 503 service unavail */
2641 txn->status = 503;
2642 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2643 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002644 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002645 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002646
Willy Tarreau59234e92008-11-30 23:51:27 +01002647 /* nothing to fail, let's reply normaly */
2648 txn->status = 200;
2649 stream_int_retnclose(req->prod, &http_200_chunk);
2650 goto return_prx_cond;
2651 }
2652
2653 /*
2654 * 3: Maybe we have to copy the original REQURI for the logs ?
2655 * Note: we cannot log anymore if the request has been
2656 * classified as invalid.
2657 */
2658 if (unlikely(s->logs.logwait & LW_REQ)) {
2659 /* we have a complete HTTP request that we must log */
2660 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2661 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002662
Willy Tarreau59234e92008-11-30 23:51:27 +01002663 if (urilen >= REQURI_LEN)
2664 urilen = REQURI_LEN - 1;
2665 memcpy(txn->uri, &req->data[msg->som], urilen);
2666 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002667
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 if (!(s->logs.logwait &= ~LW_REQ))
2669 s->do_log(s);
2670 } else {
2671 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002672 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002673 }
Willy Tarreau06619262006-12-17 08:37:22 +01002674
Willy Tarreau59234e92008-11-30 23:51:27 +01002675 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002676 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2677 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002678
Willy Tarreau5b154472009-12-21 20:11:07 +01002679 /* ... and check if the request is HTTP/1.1 or above */
2680 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002681 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2682 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2683 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002684 txn->flags |= TX_REQ_VER_11;
2685
2686 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002687 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002688
Willy Tarreau88d349d2010-01-25 12:15:43 +01002689 /* if the frontend has "option http-use-proxy-header", we'll check if
2690 * we have what looks like a proxied connection instead of a connection,
2691 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2692 * Note that this is *not* RFC-compliant, however browsers and proxies
2693 * happen to do that despite being non-standard :-(
2694 * We consider that a request not beginning with either '/' or '*' is
2695 * a proxied connection, which covers both "scheme://location" and
2696 * CONNECT ip:port.
2697 */
2698 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2699 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2700 txn->flags |= TX_USE_PX_CONN;
2701
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002702 /* transfer length unknown*/
2703 txn->flags &= ~TX_REQ_XFER_LEN;
2704
Willy Tarreau59234e92008-11-30 23:51:27 +01002705 /* 5: we may need to capture headers */
2706 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002707 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002708 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002709
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002710 /* 6: determine the transfer-length.
2711 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2712 * the presence of a message-body in a REQUEST and its transfer length
2713 * must be determined that way (in order of precedence) :
2714 * 1. The presence of a message-body in a request is signaled by the
2715 * inclusion of a Content-Length or Transfer-Encoding header field
2716 * in the request's header fields. When a request message contains
2717 * both a message-body of non-zero length and a method that does
2718 * not define any semantics for that request message-body, then an
2719 * origin server SHOULD either ignore the message-body or respond
2720 * with an appropriate error message (e.g., 413). A proxy or
2721 * gateway, when presented the same request, SHOULD either forward
2722 * the request inbound with the message- body or ignore the
2723 * message-body when determining a response.
2724 *
2725 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2726 * and the "chunked" transfer-coding (Section 6.2) is used, the
2727 * transfer-length is defined by the use of this transfer-coding.
2728 * If a Transfer-Encoding header field is present and the "chunked"
2729 * transfer-coding is not present, the transfer-length is defined
2730 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002731 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002732 * 3. If a Content-Length header field is present, its decimal value in
2733 * OCTETs represents both the entity-length and the transfer-length.
2734 * If a message is received with both a Transfer-Encoding header
2735 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002736 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002737 * 4. By the server closing the connection. (Closing the connection
2738 * cannot be used to indicate the end of a request body, since that
2739 * would leave no possibility for the server to send back a response.)
2740 *
2741 * Whenever a transfer-coding is applied to a message-body, the set of
2742 * transfer-codings MUST include "chunked", unless the message indicates
2743 * it is terminated by closing the connection. When the "chunked"
2744 * transfer-coding is used, it MUST be the last transfer-coding applied
2745 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002746 */
2747
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002748 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002749 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002750 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002751 while ((txn->flags & TX_REQ_VER_11) &&
2752 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002753 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2754 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2755 else if (txn->flags & TX_REQ_TE_CHNK) {
2756 /* bad transfer-encoding (chunked followed by something else) */
2757 use_close_only = 1;
2758 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2759 break;
2760 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002761 }
2762
Willy Tarreau32b47f42009-10-18 20:55:02 +02002763 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002764 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002765 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2766 signed long long cl;
2767
2768 if (!ctx.vlen)
2769 goto return_bad_req;
2770
2771 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2772 goto return_bad_req; /* parse failure */
2773
2774 if (cl < 0)
2775 goto return_bad_req;
2776
2777 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2778 goto return_bad_req; /* already specified, was different */
2779
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002780 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002781 msg->hdr_content_len = cl;
2782 }
2783
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002784 /* bodyless requests have a known length */
2785 if (!use_close_only)
2786 txn->flags |= TX_REQ_XFER_LEN;
2787
Willy Tarreaud787e662009-07-07 10:14:51 +02002788 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002789 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002790 req->analyse_exp = TICK_ETERNITY;
2791 return 1;
2792
2793 return_bad_req:
2794 /* We centralize bad requests processing here */
2795 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2796 /* we detected a parsing error. We want to archive this request
2797 * in the dedicated proxy area for later troubleshooting.
2798 */
2799 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2800 }
2801
2802 txn->req.msg_state = HTTP_MSG_ERROR;
2803 txn->status = 400;
2804 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002805
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002806 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002807 if (s->listener->counters)
2808 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002809
2810 return_prx_cond:
2811 if (!(s->flags & SN_ERR_MASK))
2812 s->flags |= SN_ERR_PRXCOND;
2813 if (!(s->flags & SN_FINST_MASK))
2814 s->flags |= SN_FINST_R;
2815
2816 req->analysers = 0;
2817 req->analyse_exp = TICK_ETERNITY;
2818 return 0;
2819}
2820
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002821/* This stream analyser runs all HTTP request processing which is common to
2822 * frontends and backends, which means blocking ACLs, filters, connection-close,
2823 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002824 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002825 * either needs more data or wants to immediately abort the request (eg: deny,
2826 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002827 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002828int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002829{
Willy Tarreaud787e662009-07-07 10:14:51 +02002830 struct http_txn *txn = &s->txn;
2831 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002832 struct acl_cond *cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002833 struct req_acl_rule *req_acl, *req_acl_final = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002834 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002835 struct cond_wordlist *wl;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002836 int del_ka, del_cl, do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002837
Willy Tarreau655dce92009-11-08 13:10:58 +01002838 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002839 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002840 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002841 return 0;
2842 }
2843
Willy Tarreau3a816292009-07-07 10:55:49 +02002844 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002845 req->analyse_exp = TICK_ETERNITY;
2846
2847 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2848 now_ms, __FUNCTION__,
2849 s,
2850 req,
2851 req->rex, req->wex,
2852 req->flags,
2853 req->l,
2854 req->analysers);
2855
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002856 /* first check whether we have some ACLs set to block this request */
2857 list_for_each_entry(cond, &px->block_cond, list) {
2858 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002859
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002860 ret = acl_pass(ret);
2861 if (cond->pol == ACL_COND_UNLESS)
2862 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002863
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002864 if (ret) {
2865 txn->status = 403;
2866 /* let's log the request time */
2867 s->logs.tv_request = now;
2868 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2869 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002870 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002871 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002872
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002873 do_stats = stats_check_uri(s, px);
2874
2875 list_for_each_entry(req_acl, (do_stats?&px->uri_auth->req_acl:&px->req_acl), list) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002876 int ret = 1;
2877
2878 if (req_acl->action >= PR_REQ_ACL_ACT_MAX)
2879 continue;
2880
2881 /* check condition, but only if attached */
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01002882 if (req_acl->cond) {
2883 ret = acl_exec_cond(req_acl->cond, px, s, txn, ACL_DIR_REQ);
2884 ret = acl_pass(ret);
Willy Tarreau51425942010-02-01 10:40:19 +01002885
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01002886 if (req_acl->cond->pol == ACL_COND_UNLESS)
2887 ret = !ret;
2888 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002889
2890 if (ret) {
2891 req_acl_final = req_acl;
2892 break;
2893 }
2894 }
2895
2896 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_DENY) {
2897 txn->status = 403;
2898 s->logs.tv_request = now;
2899 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2900 goto return_prx_cond;
2901 }
2902
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002903 /* try headers filters */
2904 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002905 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002906 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002907
Willy Tarreau59234e92008-11-30 23:51:27 +01002908 /* has the request been denied ? */
2909 if (txn->flags & TX_CLDENY) {
2910 /* no need to go further */
2911 txn->status = 403;
2912 /* let's log the request time */
2913 s->logs.tv_request = now;
2914 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
2915 goto return_prx_cond;
2916 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002917
2918 /* When a connection is tarpitted, we use the tarpit timeout,
2919 * which may be the same as the connect timeout if unspecified.
2920 * If unset, then set it to zero because we really want it to
2921 * eventually expire. We build the tarpit as an analyser.
2922 */
2923 if (txn->flags & TX_CLTARPIT) {
2924 buffer_erase(s->req);
2925 /* wipe the request out so that we can drop the connection early
2926 * if the client closes first.
2927 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002928 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002929 req->analysers = 0; /* remove switching rules etc... */
2930 req->analysers |= AN_REQ_HTTP_TARPIT;
2931 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2932 if (!req->analyse_exp)
2933 req->analyse_exp = tick_add(now_ms, 0);
2934 return 1;
2935 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002936 }
Willy Tarreau06619262006-12-17 08:37:22 +01002937
Willy Tarreau5b154472009-12-21 20:11:07 +01002938 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2939 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002940 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2941 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002942 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2943 * avoid to redo the same work if FE and BE have the same settings (common).
2944 * The method consists in checking if options changed between the two calls
2945 * (implying that either one is non-null, or one of them is non-null and we
2946 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002947 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002948
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002949 del_cl = del_ka = 0;
2950
Willy Tarreaudc008c52010-02-01 16:20:08 +01002951 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2952 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2953 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2954 (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 +01002955 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002956
Willy Tarreau5b154472009-12-21 20:11:07 +01002957 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2958 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002959 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2960 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002961 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002962 tmp = TX_CON_WANT_CLO;
2963
Willy Tarreau5b154472009-12-21 20:11:07 +01002964 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2965 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002966
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002967 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2968 /* parse the Connection header and possibly clean it */
2969 int to_del = 0;
2970 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002971 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2972 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002973 to_del |= 2; /* remove "keep-alive" */
2974 if (!(txn->flags & TX_REQ_VER_11))
2975 to_del |= 1; /* remove "close" */
2976 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002977 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002978
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002979 /* check if client or config asks for explicit close in KAL/SCL */
2980 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2981 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2982 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2983 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
2984 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002985 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
2986 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002987 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2988 }
Willy Tarreau78599912009-10-17 20:12:21 +02002989
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002990 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01002991 list_for_each_entry(wl, &px->req_add, list) {
Willy Tarreau8abd4cd2010-01-31 14:30:44 +01002992 if (wl->cond) {
2993 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2994 ret = acl_pass(ret);
2995 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2996 ret = !ret;
2997 if (!ret)
2998 continue;
2999 }
3000
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01003001 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003002 goto return_bad_req;
3003 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003004
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003005 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) {
3006 struct chunk msg;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003007 char *realm = req_acl->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003008
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003009 if (!realm)
3010 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3011
Willy Tarreau844a7e72010-01-31 21:46:18 +01003012 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003013 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3014 txn->status = 401;
3015 stream_int_retnclose(req->prod, &msg);
3016 goto return_prx_cond;
3017 }
3018
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003019 if (do_stats) {
3020 /* We need to provied stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003021 * FIXME!!! that one is rather dangerous, we want to
3022 * make it follow standard rules (eg: clear req->analysers).
3023 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003024
3025 s->logs.tv_request = now;
3026 s->data_source = DATA_SRC_STATS;
3027 s->data_state = DATA_ST_INIT;
3028 s->task->nice = -32; /* small boost for HTTP statistics */
3029 stream_int_register_handler(s->rep->prod, http_stats_io_handler);
3030 s->rep->prod->private = s;
3031 s->rep->prod->st0 = s->rep->prod->st1 = 0;
3032 req->analysers = 0;
3033
3034 return 0;
3035
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003036 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003037
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003038 /* check whether we have some ACLs set to redirect this request */
3039 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003040 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003041
Willy Tarreauf285f542010-01-03 20:03:03 +01003042 if (rule->cond) {
3043 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3044 ret = acl_pass(ret);
3045 if (rule->cond->pol == ACL_COND_UNLESS)
3046 ret = !ret;
3047 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003048
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003049 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003050 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003051 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003052
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003053 /* build redirect message */
3054 switch(rule->code) {
3055 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003056 msg_fmt = HTTP_303;
3057 break;
3058 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003059 msg_fmt = HTTP_301;
3060 break;
3061 case 302:
3062 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003063 msg_fmt = HTTP_302;
3064 break;
3065 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003066
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003067 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003068 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003069
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003070 switch(rule->type) {
3071 case REDIRECT_TYPE_PREFIX: {
3072 const char *path;
3073 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003074
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003075 path = http_get_path(txn);
3076 /* build message using path */
3077 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003078 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003079 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3080 int qs = 0;
3081 while (qs < pathlen) {
3082 if (path[qs] == '?') {
3083 pathlen = qs;
3084 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003085 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003086 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003087 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003088 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003089 } else {
3090 path = "/";
3091 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003092 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003093
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003094 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003095 goto return_bad_req;
3096
3097 /* add prefix. Note that if prefix == "/", we don't want to
3098 * add anything, otherwise it makes it hard for the user to
3099 * configure a self-redirection.
3100 */
3101 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003102 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3103 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003104 }
3105
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003106 /* add path */
3107 memcpy(rdr.str + rdr.len, path, pathlen);
3108 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003109
3110 /* append a slash at the end of the location is needed and missing */
3111 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3112 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3113 if (rdr.len > rdr.size - 5)
3114 goto return_bad_req;
3115 rdr.str[rdr.len] = '/';
3116 rdr.len++;
3117 }
3118
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003119 break;
3120 }
3121 case REDIRECT_TYPE_LOCATION:
3122 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003123 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003124 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003125
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003126 /* add location */
3127 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3128 rdr.len += rule->rdr_len;
3129 break;
3130 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003131
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003132 if (rule->cookie_len) {
3133 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3134 rdr.len += 14;
3135 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3136 rdr.len += rule->cookie_len;
3137 memcpy(rdr.str + rdr.len, "\r\n", 2);
3138 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003139 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003140
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003141 /* add end of headers and the keep-alive/close status.
3142 * We may choose to set keep-alive if the Location begins
3143 * with a slash, because the client will come back to the
3144 * same server.
3145 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003146 txn->status = rule->code;
3147 /* let's log the request time */
3148 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003149
3150 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3151 (txn->flags & TX_REQ_XFER_LEN) &&
3152 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
3153 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3154 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3155 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003156 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003157 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3158 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3159 rdr.len += 30;
3160 } else {
3161 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3162 rdr.len += 24;
3163 }
Willy Tarreau75661452010-01-10 10:35:01 +01003164 }
3165 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3166 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003167 buffer_write(req->prod->ob, rdr.str, rdr.len);
3168 /* "eat" the request */
3169 buffer_ignore(req, msg->sov - msg->som);
3170 msg->som = msg->sov;
3171 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003172 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3173 txn->req.msg_state = HTTP_MSG_CLOSED;
3174 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003175 break;
3176 } else {
3177 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003178 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3179 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3180 rdr.len += 29;
3181 } else {
3182 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3183 rdr.len += 23;
3184 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003185 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003186 goto return_prx_cond;
3187 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003188 }
3189 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003190
Willy Tarreau2be39392010-01-03 17:24:51 +01003191 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3192 * If this happens, then the data will not come immediately, so we must
3193 * send all what we have without waiting. Note that due to the small gain
3194 * in waiting for the body of the request, it's easier to simply put the
3195 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3196 * itself once used.
3197 */
3198 req->flags |= BF_SEND_DONTWAIT;
3199
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003200 /* that's OK for us now, let's move on to next analysers */
3201 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003202
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003203 return_bad_req:
3204 /* We centralize bad requests processing here */
3205 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3206 /* we detected a parsing error. We want to archive this request
3207 * in the dedicated proxy area for later troubleshooting.
3208 */
3209 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
3210 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003211
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003212 txn->req.msg_state = HTTP_MSG_ERROR;
3213 txn->status = 400;
3214 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003215
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003216 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003217 if (s->listener->counters)
3218 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003219
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003220 return_prx_cond:
3221 if (!(s->flags & SN_ERR_MASK))
3222 s->flags |= SN_ERR_PRXCOND;
3223 if (!(s->flags & SN_FINST_MASK))
3224 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003225
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003226 req->analysers = 0;
3227 req->analyse_exp = TICK_ETERNITY;
3228 return 0;
3229}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003230
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003231/* This function performs all the processing enabled for the current request.
3232 * It returns 1 if the processing can continue on next analysers, or zero if it
3233 * needs more data, encounters an error, or wants to immediately abort the
3234 * request. It relies on buffers flags, and updates s->req->analysers.
3235 */
3236int http_process_request(struct session *s, struct buffer *req, int an_bit)
3237{
3238 struct http_txn *txn = &s->txn;
3239 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003240
Willy Tarreau655dce92009-11-08 13:10:58 +01003241 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003242 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003243 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003244 return 0;
3245 }
3246
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003247 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3248 now_ms, __FUNCTION__,
3249 s,
3250 req,
3251 req->rex, req->wex,
3252 req->flags,
3253 req->l,
3254 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003255
Willy Tarreau59234e92008-11-30 23:51:27 +01003256 /*
3257 * Right now, we know that we have processed the entire headers
3258 * and that unwanted requests have been filtered out. We can do
3259 * whatever we want with the remaining request. Also, now we
3260 * may have separate values for ->fe, ->be.
3261 */
Willy Tarreau06619262006-12-17 08:37:22 +01003262
Willy Tarreau59234e92008-11-30 23:51:27 +01003263 /*
3264 * If HTTP PROXY is set we simply get remote server address
3265 * parsing incoming request.
3266 */
3267 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003268 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003269 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003270
Willy Tarreau59234e92008-11-30 23:51:27 +01003271 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003272 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003273 * Note that doing so might move headers in the request, but
3274 * the fields will stay coherent and the URI will not move.
3275 * This should only be performed in the backend.
3276 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003277 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003278 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3279 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003280
Willy Tarreau59234e92008-11-30 23:51:27 +01003281 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003282 * 8: the appsession cookie was looked up very early in 1.2,
3283 * so let's do the same now.
3284 */
3285
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003286 /* It needs to look into the URI unless persistence must be ignored */
3287 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003288 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003289 }
3290
3291 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003292 * 9: add X-Forwarded-For if either the frontend or the backend
3293 * asks for it.
3294 */
3295 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3296 if (s->cli_addr.ss_family == AF_INET) {
3297 /* Add an X-Forwarded-For header unless the source IP is
3298 * in the 'except' network range.
3299 */
3300 if ((!s->fe->except_mask.s_addr ||
3301 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3302 != s->fe->except_net.s_addr) &&
3303 (!s->be->except_mask.s_addr ||
3304 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3305 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003306 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003307 unsigned char *pn;
3308 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003309
3310 /* Note: we rely on the backend to get the header name to be used for
3311 * x-forwarded-for, because the header is really meant for the backends.
3312 * However, if the backend did not specify any option, we have to rely
3313 * on the frontend's header name.
3314 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003315 if (s->be->fwdfor_hdr_len) {
3316 len = s->be->fwdfor_hdr_len;
3317 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003318 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003319 len = s->fe->fwdfor_hdr_len;
3320 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003321 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003322 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003323
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003324 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003325 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003326 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003327 }
3328 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003329 else if (s->cli_addr.ss_family == AF_INET6) {
3330 /* FIXME: for the sake of completeness, we should also support
3331 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003332 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003333 int len;
3334 char pn[INET6_ADDRSTRLEN];
3335 inet_ntop(AF_INET6,
3336 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3337 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003338
Willy Tarreau59234e92008-11-30 23:51:27 +01003339 /* Note: we rely on the backend to get the header name to be used for
3340 * x-forwarded-for, because the header is really meant for the backends.
3341 * However, if the backend did not specify any option, we have to rely
3342 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003343 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003344 if (s->be->fwdfor_hdr_len) {
3345 len = s->be->fwdfor_hdr_len;
3346 memcpy(trash, s->be->fwdfor_hdr_name, len);
3347 } else {
3348 len = s->fe->fwdfor_hdr_len;
3349 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003350 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003351 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003352
Willy Tarreau59234e92008-11-30 23:51:27 +01003353 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003354 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003355 goto return_bad_req;
3356 }
3357 }
3358
3359 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003360 * 10: add X-Original-To if either the frontend or the backend
3361 * asks for it.
3362 */
3363 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3364
3365 /* FIXME: don't know if IPv6 can handle that case too. */
3366 if (s->cli_addr.ss_family == AF_INET) {
3367 /* Add an X-Original-To header unless the destination IP is
3368 * in the 'except' network range.
3369 */
3370 if (!(s->flags & SN_FRT_ADDR_SET))
3371 get_frt_addr(s);
3372
3373 if ((!s->fe->except_mask_to.s_addr ||
3374 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3375 != s->fe->except_to.s_addr) &&
3376 (!s->be->except_mask_to.s_addr ||
3377 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3378 != s->be->except_to.s_addr)) {
3379 int len;
3380 unsigned char *pn;
3381 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3382
3383 /* Note: we rely on the backend to get the header name to be used for
3384 * x-original-to, because the header is really meant for the backends.
3385 * However, if the backend did not specify any option, we have to rely
3386 * on the frontend's header name.
3387 */
3388 if (s->be->orgto_hdr_len) {
3389 len = s->be->orgto_hdr_len;
3390 memcpy(trash, s->be->orgto_hdr_name, len);
3391 } else {
3392 len = s->fe->orgto_hdr_len;
3393 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003394 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003395 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3396
3397 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003398 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003399 goto return_bad_req;
3400 }
3401 }
3402 }
3403
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003404 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3405 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3406 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
3407 unsigned int want_flags = 0;
3408
3409 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003410 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3411 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) ||
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003412 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))
3413 want_flags |= TX_CON_CLO_SET;
3414 } else {
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003415 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3416 (((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA) &&
3417 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003418 want_flags |= TX_CON_KAL_SET;
3419 }
3420
3421 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3422 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003423 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003424
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003425
Willy Tarreau522d6c02009-12-06 18:49:18 +01003426 /* If we have no server assigned yet and we're balancing on url_param
3427 * with a POST request, we may be interested in checking the body for
3428 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003429 */
3430 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3431 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003432 s->be->url_param_post_limit != 0 &&
3433 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003434 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003435 buffer_dont_connect(req);
3436 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003437 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003438
Willy Tarreaud98cf932009-12-27 22:54:55 +01003439 if (txn->flags & TX_REQ_XFER_LEN)
3440 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003441
Willy Tarreau59234e92008-11-30 23:51:27 +01003442 /*************************************************************
3443 * OK, that's finished for the headers. We have done what we *
3444 * could. Let's switch to the DATA state. *
3445 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003446 req->analyse_exp = TICK_ETERNITY;
3447 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003448
Willy Tarreau59234e92008-11-30 23:51:27 +01003449 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003450 /* OK let's go on with the BODY now */
3451 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003452
Willy Tarreau59234e92008-11-30 23:51:27 +01003453 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003454 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003455 /* we detected a parsing error. We want to archive this request
3456 * in the dedicated proxy area for later troubleshooting.
3457 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003458 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003459 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003460
Willy Tarreau59234e92008-11-30 23:51:27 +01003461 txn->req.msg_state = HTTP_MSG_ERROR;
3462 txn->status = 400;
3463 req->analysers = 0;
3464 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003465
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003466 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003467 if (s->listener->counters)
3468 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003469
Willy Tarreau59234e92008-11-30 23:51:27 +01003470 if (!(s->flags & SN_ERR_MASK))
3471 s->flags |= SN_ERR_PRXCOND;
3472 if (!(s->flags & SN_FINST_MASK))
3473 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003474 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003475}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003476
Willy Tarreau60b85b02008-11-30 23:28:40 +01003477/* This function is an analyser which processes the HTTP tarpit. It always
3478 * returns zero, at the beginning because it prevents any other processing
3479 * from occurring, and at the end because it terminates the request.
3480 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003481int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003482{
3483 struct http_txn *txn = &s->txn;
3484
3485 /* This connection is being tarpitted. The CLIENT side has
3486 * already set the connect expiration date to the right
3487 * timeout. We just have to check that the client is still
3488 * there and that the timeout has not expired.
3489 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003490 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003491 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3492 !tick_is_expired(req->analyse_exp, now_ms))
3493 return 0;
3494
3495 /* We will set the queue timer to the time spent, just for
3496 * logging purposes. We fake a 500 server error, so that the
3497 * attacker will not suspect his connection has been tarpitted.
3498 * It will not cause trouble to the logs because we can exclude
3499 * the tarpitted connections by filtering on the 'PT' status flags.
3500 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003501 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3502
3503 txn->status = 500;
3504 if (req->flags != BF_READ_ERROR)
3505 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3506
3507 req->analysers = 0;
3508 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003509
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003510 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003511 if (s->listener->counters)
3512 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003513
Willy Tarreau60b85b02008-11-30 23:28:40 +01003514 if (!(s->flags & SN_ERR_MASK))
3515 s->flags |= SN_ERR_PRXCOND;
3516 if (!(s->flags & SN_FINST_MASK))
3517 s->flags |= SN_FINST_T;
3518 return 0;
3519}
3520
Willy Tarreaud34af782008-11-30 23:36:37 +01003521/* This function is an analyser which processes the HTTP request body. It looks
3522 * for parameters to be used for the load balancing algorithm (url_param). It
3523 * must only be called after the standard HTTP request processing has occurred,
3524 * because it expects the request to be parsed. It returns zero if it needs to
3525 * read more data, or 1 once it has completed its analysis.
3526 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003527int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003528{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003529 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003530 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003531 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003532
3533 /* We have to parse the HTTP request body to find any required data.
3534 * "balance url_param check_post" should have been the only way to get
3535 * into this. We were brought here after HTTP header analysis, so all
3536 * related structures are ready.
3537 */
3538
Willy Tarreau522d6c02009-12-06 18:49:18 +01003539 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3540 goto missing_data;
3541
3542 if (msg->msg_state < HTTP_MSG_100_SENT) {
3543 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3544 * send an HTTP/1.1 100 Continue intermediate response.
3545 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003546 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003547 struct hdr_ctx ctx;
3548 ctx.idx = 0;
3549 /* Expect is allowed in 1.1, look for it */
3550 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3551 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3552 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3553 }
3554 }
3555 msg->msg_state = HTTP_MSG_100_SENT;
3556 }
3557
3558 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003559 /* we have msg->col and msg->sov which both point to the first
3560 * byte of message body. msg->som still points to the beginning
3561 * of the message. We must save the body in req->lr because it
3562 * survives buffer re-alignments.
3563 */
3564 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003565 if (txn->flags & TX_REQ_TE_CHNK)
3566 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3567 else
3568 msg->msg_state = HTTP_MSG_DATA;
3569 }
3570
3571 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003572 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003573 * set ->sov and ->lr to point to the body and switch to DATA or
3574 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003575 */
3576 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003577
Willy Tarreau115acb92009-12-26 13:56:06 +01003578 if (!ret)
3579 goto missing_data;
3580 else if (ret < 0)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003581 goto return_bad_req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003582 }
3583
Willy Tarreaud98cf932009-12-27 22:54:55 +01003584 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003585 * We have the first non-header byte in msg->col, which is either the
3586 * beginning of the chunk size or of the data. The first data byte is in
3587 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3588 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003589 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003590
3591 if (msg->hdr_content_len < limit)
3592 limit = msg->hdr_content_len;
3593
Willy Tarreau7c96f672009-12-27 22:47:25 +01003594 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003595 goto http_end;
3596
3597 missing_data:
3598 /* we get here if we need to wait for more data */
Willy Tarreau115acb92009-12-26 13:56:06 +01003599 if (req->flags & BF_FULL)
3600 goto return_bad_req;
3601
Willy Tarreau522d6c02009-12-06 18:49:18 +01003602 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3603 txn->status = 408;
3604 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
3605 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003606 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003607
3608 /* we get here if we need to wait for more data */
3609 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003610 /* Not enough data. We'll re-use the http-request
3611 * timeout here. Ideally, we should set the timeout
3612 * relative to the accept() date. We just set the
3613 * request timeout once at the beginning of the
3614 * request.
3615 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003616 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003617 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003618 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003619 return 0;
3620 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003621
3622 http_end:
3623 /* The situation will not evolve, so let's give up on the analysis. */
3624 s->logs.tv_request = now; /* update the request timer to reflect full request */
3625 req->analysers &= ~an_bit;
3626 req->analyse_exp = TICK_ETERNITY;
3627 return 1;
3628
3629 return_bad_req: /* let's centralize all bad requests */
3630 txn->req.msg_state = HTTP_MSG_ERROR;
3631 txn->status = 400;
3632 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3633
3634 return_err_msg:
3635 req->analysers = 0;
3636 s->fe->counters.failed_req++;
3637 if (s->listener->counters)
3638 s->listener->counters->failed_req++;
3639
3640 if (!(s->flags & SN_ERR_MASK))
3641 s->flags |= SN_ERR_PRXCOND;
3642 if (!(s->flags & SN_FINST_MASK))
3643 s->flags |= SN_FINST_R;
3644 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003645}
3646
Willy Tarreau610ecce2010-01-04 21:15:02 +01003647/* Terminate current transaction and prepare a new one. This is very tricky
3648 * right now but it works.
3649 */
3650void http_end_txn_clean_session(struct session *s)
3651{
3652 /* FIXME: We need a more portable way of releasing a backend's and a
3653 * server's connections. We need a safer way to reinitialize buffer
3654 * flags. We also need a more accurate method for computing per-request
3655 * data.
3656 */
3657 http_silent_debug(__LINE__, s);
3658
3659 s->req->cons->flags |= SI_FL_NOLINGER;
3660 s->req->cons->shutr(s->req->cons);
3661 s->req->cons->shutw(s->req->cons);
3662
3663 http_silent_debug(__LINE__, s);
3664
3665 if (s->flags & SN_BE_ASSIGNED)
3666 s->be->beconn--;
3667
3668 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3669 session_process_counters(s);
3670
3671 if (s->txn.status) {
3672 int n;
3673
3674 n = s->txn.status / 100;
3675 if (n < 1 || n > 5)
3676 n = 0;
3677
3678 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01003679 s->fe->counters.fe.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003680
Willy Tarreau24657792010-02-26 10:30:28 +01003681 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003682 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01003683 s->be->counters.be.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003684 }
3685
3686 /* don't count other requests' data */
3687 s->logs.bytes_in -= s->req->l - s->req->send_max;
3688 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3689
3690 /* let's do a final log if we need it */
3691 if (s->logs.logwait &&
3692 !(s->flags & SN_MONITOR) &&
3693 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3694 s->do_log(s);
3695 }
3696
3697 s->logs.accept_date = date; /* user-visible date for logging */
3698 s->logs.tv_accept = now; /* corrected date for internal use */
3699 tv_zero(&s->logs.tv_request);
3700 s->logs.t_queue = -1;
3701 s->logs.t_connect = -1;
3702 s->logs.t_data = -1;
3703 s->logs.t_close = 0;
3704 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3705 s->logs.srv_queue_size = 0; /* we will get this number soon */
3706
3707 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3708 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3709
3710 if (s->pend_pos)
3711 pendconn_free(s->pend_pos);
3712
3713 if (s->srv) {
3714 if (s->flags & SN_CURR_SESS) {
3715 s->flags &= ~SN_CURR_SESS;
3716 s->srv->cur_sess--;
3717 }
3718 if (may_dequeue_tasks(s->srv, s->be))
3719 process_srv_queue(s->srv);
3720 }
3721
3722 if (unlikely(s->srv_conn))
3723 sess_change_server(s, NULL);
3724 s->srv = NULL;
3725
3726 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3727 s->req->cons->fd = -1; /* just to help with debugging */
3728 s->req->cons->err_type = SI_ET_NONE;
3729 s->req->cons->err_loc = NULL;
3730 s->req->cons->exp = TICK_ETERNITY;
3731 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003732 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3733 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003734 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003735 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3736 s->txn.meth = 0;
3737 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003738 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003739 if (s->be->options2 & PR_O2_INDEPSTR)
3740 s->req->cons->flags |= SI_FL_INDEP_STR;
3741
3742 /* if the request buffer is not empty, it means we're
3743 * about to process another request, so send pending
3744 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003745 * Just don't do this if the buffer is close to be full,
3746 * because the request will wait for it to flush a little
3747 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003748 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003749 if (s->req->l > s->req->send_max) {
3750 if (s->rep->send_max &&
3751 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003752 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3753 s->rep->flags |= BF_EXPECT_MORE;
3754 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003755
3756 /* we're removing the analysers, we MUST re-enable events detection */
3757 buffer_auto_read(s->req);
3758 buffer_auto_close(s->req);
3759 buffer_auto_read(s->rep);
3760 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003761
3762 /* make ->lr point to the first non-forwarded byte */
3763 s->req->lr = s->req->w + s->req->send_max;
3764 if (s->req->lr >= s->req->data + s->req->size)
3765 s->req->lr -= s->req->size;
3766 s->rep->lr = s->rep->w + s->rep->send_max;
3767 if (s->rep->lr >= s->rep->data + s->rep->size)
3768 s->rep->lr -= s->req->size;
3769
3770 s->req->analysers |= s->fe->fe_req_ana;
3771 s->rep->analysers = 0;
3772
3773 http_silent_debug(__LINE__, s);
3774}
3775
3776
3777/* This function updates the request state machine according to the response
3778 * state machine and buffer flags. It returns 1 if it changes anything (flag
3779 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3780 * it is only used to find when a request/response couple is complete. Both
3781 * this function and its equivalent should loop until both return zero. It
3782 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3783 */
3784int http_sync_req_state(struct session *s)
3785{
3786 struct buffer *buf = s->req;
3787 struct http_txn *txn = &s->txn;
3788 unsigned int old_flags = buf->flags;
3789 unsigned int old_state = txn->req.msg_state;
3790
3791 http_silent_debug(__LINE__, s);
3792 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3793 return 0;
3794
3795 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003796 /* No need to read anymore, the request was completely parsed.
3797 * We can shut the read side unless we want to abort_on_close.
3798 */
3799 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3800 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003801
3802 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3803 goto wait_other_side;
3804
3805 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3806 /* The server has not finished to respond, so we
3807 * don't want to move in order not to upset it.
3808 */
3809 goto wait_other_side;
3810 }
3811
3812 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3813 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003814 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003815 txn->req.msg_state = HTTP_MSG_TUNNEL;
3816 goto wait_other_side;
3817 }
3818
3819 /* When we get here, it means that both the request and the
3820 * response have finished receiving. Depending on the connection
3821 * mode, we'll have to wait for the last bytes to leave in either
3822 * direction, and sometimes for a close to be effective.
3823 */
3824
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003825 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3826 /* Server-close mode : queue a connection close to the server */
3827 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003828 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003829 }
3830 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3831 /* Option forceclose is set, or either side wants to close,
3832 * let's enforce it now that we're not expecting any new
3833 * data to come. The caller knows the session is complete
3834 * once both states are CLOSED.
3835 */
3836 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003837 buffer_shutr_now(buf);
3838 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003839 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003840 }
3841 else {
3842 /* The last possible modes are keep-alive and tunnel. Since tunnel
3843 * mode does not set the body analyser, we can't reach this place
3844 * in tunnel mode, so we're left with keep-alive only.
3845 * This mode is currently not implemented, we switch to tunnel mode.
3846 */
3847 buffer_auto_read(buf);
3848 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003849 }
3850
3851 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3852 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003853 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3854
Willy Tarreau610ecce2010-01-04 21:15:02 +01003855 if (!(buf->flags & BF_OUT_EMPTY)) {
3856 txn->req.msg_state = HTTP_MSG_CLOSING;
3857 goto http_msg_closing;
3858 }
3859 else {
3860 txn->req.msg_state = HTTP_MSG_CLOSED;
3861 goto http_msg_closed;
3862 }
3863 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003864 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003865 }
3866
3867 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3868 http_msg_closing:
3869 /* nothing else to forward, just waiting for the output buffer
3870 * to be empty and for the shutw_now to take effect.
3871 */
3872 if (buf->flags & BF_OUT_EMPTY) {
3873 txn->req.msg_state = HTTP_MSG_CLOSED;
3874 goto http_msg_closed;
3875 }
3876 else if (buf->flags & BF_SHUTW) {
3877 txn->req.msg_state = HTTP_MSG_ERROR;
3878 goto wait_other_side;
3879 }
3880 }
3881
3882 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3883 http_msg_closed:
3884 goto wait_other_side;
3885 }
3886
3887 wait_other_side:
3888 http_silent_debug(__LINE__, s);
3889 return txn->req.msg_state != old_state || buf->flags != old_flags;
3890}
3891
3892
3893/* This function updates the response state machine according to the request
3894 * state machine and buffer flags. It returns 1 if it changes anything (flag
3895 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3896 * it is only used to find when a request/response couple is complete. Both
3897 * this function and its equivalent should loop until both return zero. It
3898 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3899 */
3900int http_sync_res_state(struct session *s)
3901{
3902 struct buffer *buf = s->rep;
3903 struct http_txn *txn = &s->txn;
3904 unsigned int old_flags = buf->flags;
3905 unsigned int old_state = txn->rsp.msg_state;
3906
3907 http_silent_debug(__LINE__, s);
3908 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3909 return 0;
3910
3911 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3912 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003913 * still monitor the server connection for a possible close
3914 * while the request is being uploaded, so we don't disable
3915 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003916 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003917 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003918
3919 if (txn->req.msg_state == HTTP_MSG_ERROR)
3920 goto wait_other_side;
3921
3922 if (txn->req.msg_state < HTTP_MSG_DONE) {
3923 /* The client seems to still be sending data, probably
3924 * because we got an error response during an upload.
3925 * We have the choice of either breaking the connection
3926 * or letting it pass through. Let's do the later.
3927 */
3928 goto wait_other_side;
3929 }
3930
3931 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3932 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003933 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003934 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3935 goto wait_other_side;
3936 }
3937
3938 /* When we get here, it means that both the request and the
3939 * response have finished receiving. Depending on the connection
3940 * mode, we'll have to wait for the last bytes to leave in either
3941 * direction, and sometimes for a close to be effective.
3942 */
3943
3944 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3945 /* Server-close mode : shut read and wait for the request
3946 * side to close its output buffer. The caller will detect
3947 * when we're in DONE and the other is in CLOSED and will
3948 * catch that for the final cleanup.
3949 */
3950 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3951 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003952 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003953 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3954 /* Option forceclose is set, or either side wants to close,
3955 * let's enforce it now that we're not expecting any new
3956 * data to come. The caller knows the session is complete
3957 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003958 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003959 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3960 buffer_shutr_now(buf);
3961 buffer_shutw_now(buf);
3962 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003963 }
3964 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003965 /* The last possible modes are keep-alive and tunnel. Since tunnel
3966 * mode does not set the body analyser, we can't reach this place
3967 * in tunnel mode, so we're left with keep-alive only.
3968 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003969 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003970 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003971 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003972 }
3973
3974 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3975 /* if we've just closed an output, let's switch */
3976 if (!(buf->flags & BF_OUT_EMPTY)) {
3977 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3978 goto http_msg_closing;
3979 }
3980 else {
3981 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3982 goto http_msg_closed;
3983 }
3984 }
3985 goto wait_other_side;
3986 }
3987
3988 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3989 http_msg_closing:
3990 /* nothing else to forward, just waiting for the output buffer
3991 * to be empty and for the shutw_now to take effect.
3992 */
3993 if (buf->flags & BF_OUT_EMPTY) {
3994 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3995 goto http_msg_closed;
3996 }
3997 else if (buf->flags & BF_SHUTW) {
3998 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreauae526782010-03-04 20:34:23 +01003999 s->be->counters.cli_aborts++;
4000 if (s->srv)
4001 s->srv->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004002 goto wait_other_side;
4003 }
4004 }
4005
4006 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4007 http_msg_closed:
4008 /* drop any pending data */
4009 buffer_ignore(buf, buf->l - buf->send_max);
4010 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004011 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004012 goto wait_other_side;
4013 }
4014
4015 wait_other_side:
4016 http_silent_debug(__LINE__, s);
4017 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4018}
4019
4020
4021/* Resync the request and response state machines. Return 1 if either state
4022 * changes.
4023 */
4024int http_resync_states(struct session *s)
4025{
4026 struct http_txn *txn = &s->txn;
4027 int old_req_state = txn->req.msg_state;
4028 int old_res_state = txn->rsp.msg_state;
4029
4030 http_silent_debug(__LINE__, s);
4031 http_sync_req_state(s);
4032 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004033 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004034 if (!http_sync_res_state(s))
4035 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004036 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004037 if (!http_sync_req_state(s))
4038 break;
4039 }
4040 http_silent_debug(__LINE__, s);
4041 /* OK, both state machines agree on a compatible state.
4042 * There are a few cases we're interested in :
4043 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4044 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4045 * directions, so let's simply disable both analysers.
4046 * - HTTP_MSG_CLOSED on the response only means we must abort the
4047 * request.
4048 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4049 * with server-close mode means we've completed one request and we
4050 * must re-initialize the server connection.
4051 */
4052
4053 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4054 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4055 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4056 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4057 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004058 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004059 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004060 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004061 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004062 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004063 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004064 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4065 txn->rsp.msg_state == HTTP_MSG_ERROR ||
4066 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004067 s->rep->analysers = 0;
4068 buffer_auto_close(s->rep);
4069 buffer_auto_read(s->rep);
4070 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004071 buffer_abort(s->req);
4072 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004073 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004074 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004075 }
4076 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4077 txn->rsp.msg_state == HTTP_MSG_DONE &&
4078 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4079 /* server-close: terminate this server connection and
4080 * reinitialize a fresh-new transaction.
4081 */
4082 http_end_txn_clean_session(s);
4083 }
4084
4085 http_silent_debug(__LINE__, s);
4086 return txn->req.msg_state != old_req_state ||
4087 txn->rsp.msg_state != old_res_state;
4088}
4089
Willy Tarreaud98cf932009-12-27 22:54:55 +01004090/* This function is an analyser which forwards request body (including chunk
4091 * sizes if any). It is called as soon as we must forward, even if we forward
4092 * zero byte. The only situation where it must not be called is when we're in
4093 * tunnel mode and we want to forward till the close. It's used both to forward
4094 * remaining data and to resync after end of body. It expects the msg_state to
4095 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4096 * read more data, or 1 once we can go on with next request or end the session.
4097 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4098 * bytes of pending data + the headers if not already done (between som and sov).
4099 * It eventually adjusts som to match sov after the data in between have been sent.
4100 */
4101int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4102{
4103 struct http_txn *txn = &s->txn;
4104 struct http_msg *msg = &s->txn.req;
4105
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004106 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4107 return 0;
4108
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004109 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4110 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
4111 /* Output closed while we were sending data. We must abort. */
4112 buffer_ignore(req, req->l - req->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01004113 buffer_auto_read(req);
4114 buffer_auto_close(req);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004115 req->analysers &= ~an_bit;
4116 return 1;
4117 }
4118
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004119 buffer_dont_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004120
4121 /* Note that we don't have to send 100-continue back because we don't
4122 * need the data to complete our job, and it's up to the server to
4123 * decide whether to return 100, 417 or anything else in return of
4124 * an "Expect: 100-continue" header.
4125 */
4126
4127 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4128 /* we have msg->col and msg->sov which both point to the first
4129 * byte of message body. msg->som still points to the beginning
4130 * of the message. We must save the body in req->lr because it
4131 * survives buffer re-alignments.
4132 */
4133 req->lr = req->data + msg->sov;
4134 if (txn->flags & TX_REQ_TE_CHNK)
4135 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4136 else {
4137 msg->msg_state = HTTP_MSG_DATA;
4138 }
4139 }
4140
Willy Tarreaud98cf932009-12-27 22:54:55 +01004141 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004142 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004143 /* we may have some data pending */
4144 if (msg->hdr_content_len || msg->som != msg->sov) {
4145 int bytes = msg->sov - msg->som;
4146 if (bytes < 0) /* sov may have wrapped at the end */
4147 bytes += req->size;
4148 buffer_forward(req, bytes + msg->hdr_content_len);
4149 msg->hdr_content_len = 0; /* don't forward that again */
4150 msg->som = msg->sov;
4151 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004152
Willy Tarreaucaabe412010-01-03 23:08:28 +01004153 if (msg->msg_state == HTTP_MSG_DATA) {
4154 /* must still forward */
4155 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004156 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004157
4158 /* nothing left to forward */
4159 if (txn->flags & TX_REQ_TE_CHNK)
4160 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004161 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004162 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004163 }
4164 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004165 /* read the chunk size and assign it to ->hdr_content_len, then
4166 * set ->sov and ->lr to point to the body and switch to DATA or
4167 * TRAILERS state.
4168 */
4169 int ret = http_parse_chunk_size(req, msg);
4170
4171 if (!ret)
4172 goto missing_data;
4173 else if (ret < 0)
4174 goto return_bad_req;
4175 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004176 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004177 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4178 /* we want the CRLF after the data */
4179 int ret;
4180
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004181 req->lr = req->w + req->send_max;
4182 if (req->lr >= req->data + req->size)
4183 req->lr -= req->size;
4184
Willy Tarreaud98cf932009-12-27 22:54:55 +01004185 ret = http_skip_chunk_crlf(req, msg);
4186
4187 if (ret == 0)
4188 goto missing_data;
4189 else if (ret < 0)
4190 goto return_bad_req;
4191 /* we're in MSG_CHUNK_SIZE now */
4192 }
4193 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4194 int ret = http_forward_trailers(req, msg);
4195
4196 if (ret == 0)
4197 goto missing_data;
4198 else if (ret < 0)
4199 goto return_bad_req;
4200 /* we're in HTTP_MSG_DONE now */
4201 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004202 else {
4203 /* other states, DONE...TUNNEL */
4204 if (http_resync_states(s)) {
4205 /* some state changes occurred, maybe the analyser
4206 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004207 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004208 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4209 goto return_bad_req;
4210 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004211 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004212 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004213 }
4214 }
4215
Willy Tarreaud98cf932009-12-27 22:54:55 +01004216 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004217 /* stop waiting for data if the input is closed before the end */
4218 if (req->flags & BF_SHUTR)
4219 goto return_bad_req;
4220
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004221 /* waiting for the last bits to leave the buffer */
4222 if (req->flags & BF_SHUTW)
4223 goto return_bad_req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004224
4225 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004226 return 0;
4227
Willy Tarreaud98cf932009-12-27 22:54:55 +01004228 return_bad_req: /* let's centralize all bad requests */
4229 txn->req.msg_state = HTTP_MSG_ERROR;
4230 txn->status = 400;
4231 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01004232 stream_int_retnclose(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004233 req->analysers = 0;
4234 s->fe->counters.failed_req++;
4235 if (s->listener->counters)
4236 s->listener->counters->failed_req++;
4237
4238 if (!(s->flags & SN_ERR_MASK))
4239 s->flags |= SN_ERR_PRXCOND;
4240 if (!(s->flags & SN_FINST_MASK))
4241 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004242 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004243 return 0;
4244}
4245
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004246/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4247 * processing can continue on next analysers, or zero if it either needs more
4248 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4249 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4250 * when it has nothing left to do, and may remove any analyser when it wants to
4251 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004252 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004253int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004254{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004255 struct http_txn *txn = &s->txn;
4256 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004257 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004258 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004259 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004260 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004261
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004262 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 +02004263 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004264 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004265 rep,
4266 rep->rex, rep->wex,
4267 rep->flags,
4268 rep->l,
4269 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004270
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004271 /*
4272 * Now parse the partial (or complete) lines.
4273 * We will check the response syntax, and also join multi-line
4274 * headers. An index of all the lines will be elaborated while
4275 * parsing.
4276 *
4277 * For the parsing, we use a 28 states FSM.
4278 *
4279 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004280 * rep->data + msg->som = beginning of response
4281 * rep->data + msg->eoh = end of processed headers / start of current one
4282 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004283 * rep->lr = first non-visited byte
4284 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004285 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004286 */
4287
Willy Tarreau83e3af02009-12-28 17:39:57 +01004288 /* There's a protected area at the end of the buffer for rewriting
4289 * purposes. We don't want to start to parse the request if the
4290 * protected area is affected, because we may have to move processed
4291 * data later, which is much more complicated.
4292 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004293 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4294 if (unlikely((rep->flags & BF_FULL) ||
4295 rep->r < rep->lr ||
4296 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4297 if (rep->send_max) {
4298 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004299 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4300 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004301 buffer_dont_close(rep);
4302 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4303 return 0;
4304 }
4305 if (rep->l <= rep->size - global.tune.maxrewrite)
4306 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004307 }
4308
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004309 if (likely(rep->lr < rep->r))
4310 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004311 }
4312
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004313 /* 1: we might have to print this header in debug mode */
4314 if (unlikely((global.mode & MODE_DEBUG) &&
4315 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004316 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004317 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004318 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004319
Willy Tarreau962c3f42010-01-10 00:15:35 +01004320 sol = msg->sol;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004321 eol = sol + msg->sl.rq.l;
4322 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004323
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004324 sol += hdr_idx_first_pos(&txn->hdr_idx);
4325 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004326
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004327 while (cur_idx) {
4328 eol = sol + txn->hdr_idx.v[cur_idx].len;
4329 debug_hdr("srvhdr", s, sol, eol);
4330 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4331 cur_idx = txn->hdr_idx.v[cur_idx].next;
4332 }
4333 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004334
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004335 /*
4336 * Now we quickly check if we have found a full valid response.
4337 * If not so, we check the FD and buffer states before leaving.
4338 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004339 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004340 * responses are checked first.
4341 *
4342 * Depending on whether the client is still there or not, we
4343 * may send an error response back or not. Note that normally
4344 * we should only check for HTTP status there, and check I/O
4345 * errors somewhere else.
4346 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004347
Willy Tarreau655dce92009-11-08 13:10:58 +01004348 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004349 /* Invalid response */
4350 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4351 /* we detected a parsing error. We want to archive this response
4352 * in the dedicated proxy area for later troubleshooting.
4353 */
4354 hdr_response_bad:
4355 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4356 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4357
4358 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004359 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004360 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004361 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4362 }
Willy Tarreau64648412010-03-05 10:41:54 +01004363 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004364 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004365 rep->analysers = 0;
4366 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004367 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004368 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004369 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4370
4371 if (!(s->flags & SN_ERR_MASK))
4372 s->flags |= SN_ERR_PRXCOND;
4373 if (!(s->flags & SN_FINST_MASK))
4374 s->flags |= SN_FINST_H;
4375
4376 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004377 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004378
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004379 /* too large response does not fit in buffer. */
4380 else if (rep->flags & BF_FULL) {
4381 goto hdr_response_bad;
4382 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004383
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004384 /* read error */
4385 else if (rep->flags & BF_READ_ERROR) {
4386 if (msg->err_pos >= 0)
4387 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004388
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004389 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004390 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004391 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004392 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4393 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004394
Willy Tarreau90deb182010-01-07 00:20:41 +01004395 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004396 rep->analysers = 0;
4397 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004398 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004399 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004400 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004401
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004402 if (!(s->flags & SN_ERR_MASK))
4403 s->flags |= SN_ERR_SRVCL;
4404 if (!(s->flags & SN_FINST_MASK))
4405 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004406 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004407 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004408
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004409 /* read timeout : return a 504 to the client. */
4410 else if (rep->flags & BF_READ_TIMEOUT) {
4411 if (msg->err_pos >= 0)
4412 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004413
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004414 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004415 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004416 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004417 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4418 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004419
Willy Tarreau90deb182010-01-07 00:20:41 +01004420 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004421 rep->analysers = 0;
4422 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004423 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004424 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004425 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004426
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004427 if (!(s->flags & SN_ERR_MASK))
4428 s->flags |= SN_ERR_SRVTO;
4429 if (!(s->flags & SN_FINST_MASK))
4430 s->flags |= SN_FINST_H;
4431 return 0;
4432 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004433
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004434 /* close from server */
4435 else if (rep->flags & BF_SHUTR) {
4436 if (msg->err_pos >= 0)
4437 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004438
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004439 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004440 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004441 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004442 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4443 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004444
Willy Tarreau90deb182010-01-07 00:20:41 +01004445 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004446 rep->analysers = 0;
4447 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004448 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004449 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004450 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004451
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004452 if (!(s->flags & SN_ERR_MASK))
4453 s->flags |= SN_ERR_SRVCL;
4454 if (!(s->flags & SN_FINST_MASK))
4455 s->flags |= SN_FINST_H;
4456 return 0;
4457 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004458
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004459 /* write error to client (we don't send any message then) */
4460 else if (rep->flags & BF_WRITE_ERROR) {
4461 if (msg->err_pos >= 0)
4462 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004463
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004464 s->be->counters.failed_resp++;
4465 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004466 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004467
4468 if (!(s->flags & SN_ERR_MASK))
4469 s->flags |= SN_ERR_CLICL;
4470 if (!(s->flags & SN_FINST_MASK))
4471 s->flags |= SN_FINST_H;
4472
4473 /* process_session() will take care of the error */
4474 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004475 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004476
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004477 buffer_dont_close(rep);
4478 return 0;
4479 }
4480
4481 /* More interesting part now : we know that we have a complete
4482 * response which at least looks like HTTP. We have an indicator
4483 * of each header's length, so we can parse them quickly.
4484 */
4485
4486 if (unlikely(msg->err_pos >= 0))
4487 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4488
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004489 /*
4490 * 1: get the status code
4491 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004492 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004493 if (n < 1 || n > 5)
4494 n = 0;
4495 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004496
Willy Tarreau5b154472009-12-21 20:11:07 +01004497 /* check if the response is HTTP/1.1 or above */
4498 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004499 ((msg->sol[5] > '1') ||
4500 ((msg->sol[5] == '1') &&
4501 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004502 txn->flags |= TX_RES_VER_11;
4503
4504 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004505 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01004506
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004507 /* transfer length unknown*/
4508 txn->flags &= ~TX_RES_XFER_LEN;
4509
Willy Tarreau962c3f42010-01-10 00:15:35 +01004510 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004511
Willy Tarreau39650402010-03-15 19:44:39 +01004512 /* Adjust server's health based on status code. Note: status codes 501
4513 * and 505 are triggered on demand by client request, so we must not
4514 * count them as server failures.
4515 */
4516 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004517 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4518 else
4519 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4520
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004521 /*
4522 * 2: check for cacheability.
4523 */
4524
4525 switch (txn->status) {
4526 case 200:
4527 case 203:
4528 case 206:
4529 case 300:
4530 case 301:
4531 case 410:
4532 /* RFC2616 @13.4:
4533 * "A response received with a status code of
4534 * 200, 203, 206, 300, 301 or 410 MAY be stored
4535 * by a cache (...) unless a cache-control
4536 * directive prohibits caching."
4537 *
4538 * RFC2616 @9.5: POST method :
4539 * "Responses to this method are not cacheable,
4540 * unless the response includes appropriate
4541 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004542 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004543 if (likely(txn->meth != HTTP_METH_POST) &&
4544 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4545 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4546 break;
4547 default:
4548 break;
4549 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004550
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004551 /*
4552 * 3: we may need to capture headers
4553 */
4554 s->logs.logwait &= ~LW_RESP;
4555 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004556 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004557 txn->rsp.cap, s->fe->rsp_cap);
4558
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004559 /* 4: determine the transfer-length.
4560 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4561 * the presence of a message-body in a RESPONSE and its transfer length
4562 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004563 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004564 * All responses to the HEAD request method MUST NOT include a
4565 * message-body, even though the presence of entity-header fields
4566 * might lead one to believe they do. All 1xx (informational), 204
4567 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4568 * message-body. All other responses do include a message-body,
4569 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004570 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004571 * 1. Any response which "MUST NOT" include a message-body (such as the
4572 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4573 * always terminated by the first empty line after the header fields,
4574 * regardless of the entity-header fields present in the message.
4575 *
4576 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4577 * the "chunked" transfer-coding (Section 6.2) is used, the
4578 * transfer-length is defined by the use of this transfer-coding.
4579 * If a Transfer-Encoding header field is present and the "chunked"
4580 * transfer-coding is not present, the transfer-length is defined by
4581 * the sender closing the connection.
4582 *
4583 * 3. If a Content-Length header field is present, its decimal value in
4584 * OCTETs represents both the entity-length and the transfer-length.
4585 * If a message is received with both a Transfer-Encoding header
4586 * field and a Content-Length header field, the latter MUST be ignored.
4587 *
4588 * 4. If the message uses the media type "multipart/byteranges", and
4589 * the transfer-length is not otherwise specified, then this self-
4590 * delimiting media type defines the transfer-length. This media
4591 * type MUST NOT be used unless the sender knows that the recipient
4592 * can parse it; the presence in a request of a Range header with
4593 * multiple byte-range specifiers from a 1.1 client implies that the
4594 * client can parse multipart/byteranges responses.
4595 *
4596 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004597 */
4598
4599 /* Skip parsing if no content length is possible. The response flags
4600 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004601 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004602 * FIXME: should we parse anyway and return an error on chunked encoding ?
4603 */
4604 if (txn->meth == HTTP_METH_HEAD ||
4605 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004606 txn->status == 204 || txn->status == 304) {
4607 txn->flags |= TX_RES_XFER_LEN;
4608 goto skip_content_length;
4609 }
4610
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004611 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004612 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004613 while ((txn->flags & TX_RES_VER_11) &&
4614 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004615 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4616 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4617 else if (txn->flags & TX_RES_TE_CHNK) {
4618 /* bad transfer-encoding (chunked followed by something else) */
4619 use_close_only = 1;
4620 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4621 break;
4622 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004623 }
4624
4625 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4626 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004627 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004628 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4629 signed long long cl;
4630
4631 if (!ctx.vlen)
4632 goto hdr_response_bad;
4633
4634 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4635 goto hdr_response_bad; /* parse failure */
4636
4637 if (cl < 0)
4638 goto hdr_response_bad;
4639
4640 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4641 goto hdr_response_bad; /* already specified, was different */
4642
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004643 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004644 msg->hdr_content_len = cl;
4645 }
4646
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004647 /* FIXME: we should also implement the multipart/byterange method.
4648 * For now on, we resort to close mode in this case (unknown length).
4649 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004650skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004651
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004652 /* end of job, return OK */
4653 rep->analysers &= ~an_bit;
4654 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004655 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004656 return 1;
4657}
4658
4659/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004660 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4661 * and updates t->rep->analysers. It might make sense to explode it into several
4662 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004663 */
4664int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4665{
4666 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004667 struct http_msg *msg = &txn->rsp;
4668 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004669 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004670
4671 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4672 now_ms, __FUNCTION__,
4673 t,
4674 rep,
4675 rep->rex, rep->wex,
4676 rep->flags,
4677 rep->l,
4678 rep->analysers);
4679
Willy Tarreau655dce92009-11-08 13:10:58 +01004680 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004681 return 0;
4682
4683 rep->analysers &= ~an_bit;
4684 rep->analyse_exp = TICK_ETERNITY;
4685
Willy Tarreau5b154472009-12-21 20:11:07 +01004686 /* Now we have to check if we need to modify the Connection header.
4687 * This is more difficult on the response than it is on the request,
4688 * because we can have two different HTTP versions and we don't know
4689 * how the client will interprete a response. For instance, let's say
4690 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4691 * HTTP/1.1 response without any header. Maybe it will bound itself to
4692 * HTTP/1.0 because it only knows about it, and will consider the lack
4693 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4694 * the lack of header as a keep-alive. Thus we will use two flags
4695 * indicating how a request MAY be understood by the client. In case
4696 * of multiple possibilities, we'll fix the header to be explicit. If
4697 * ambiguous cases such as both close and keepalive are seen, then we
4698 * will fall back to explicit close. Note that we won't take risks with
4699 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004700 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004701 */
4702
Willy Tarreaudc008c52010-02-01 16:20:08 +01004703 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4704 txn->status == 101)) {
4705 /* Either we've established an explicit tunnel, or we're
4706 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004707 * to understand the next protocols. We have to switch to tunnel
4708 * mode, so that we transfer the request and responses then let
4709 * this protocol pass unmodified. When we later implement specific
4710 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004711 * header which contains information about that protocol for
4712 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004713 */
4714 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4715 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004716 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4717 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4718 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004719 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004720
Willy Tarreau60466522010-01-18 19:08:45 +01004721 /* on unknown transfer length, we must close */
4722 if (!(txn->flags & TX_RES_XFER_LEN) &&
4723 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4724 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004725
Willy Tarreau60466522010-01-18 19:08:45 +01004726 /* now adjust header transformations depending on current state */
4727 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4728 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4729 to_del |= 2; /* remove "keep-alive" on any response */
4730 if (!(txn->flags & TX_RES_VER_11))
4731 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004732 }
Willy Tarreau60466522010-01-18 19:08:45 +01004733 else { /* SCL / KAL */
4734 to_del |= 1; /* remove "close" on any response */
4735 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4736 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004737 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004738
Willy Tarreau60466522010-01-18 19:08:45 +01004739 /* Parse and remove some headers from the connection header */
4740 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004741
Willy Tarreau60466522010-01-18 19:08:45 +01004742 /* Some keep-alive responses are converted to Server-close if
4743 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004744 */
Willy Tarreau60466522010-01-18 19:08:45 +01004745 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4746 if ((txn->flags & TX_HDR_CONN_CLO) ||
4747 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4748 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004749 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004750 }
4751
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004752 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004753 /*
4754 * 3: we will have to evaluate the filters.
4755 * As opposed to version 1.2, now they will be evaluated in the
4756 * filters order and not in the header order. This means that
4757 * each filter has to be validated among all headers.
4758 *
4759 * Filters are tried with ->be first, then with ->fe if it is
4760 * different from ->be.
4761 */
4762
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004763 cur_proxy = t->be;
4764 while (1) {
4765 struct proxy *rule_set = cur_proxy;
4766
4767 /* try headers filters */
4768 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004769 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004770 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004771 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004772 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004773 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4774 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004775 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004776 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004777 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004778 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004779 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004780 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004781 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004782 if (!(t->flags & SN_ERR_MASK))
4783 t->flags |= SN_ERR_PRXCOND;
4784 if (!(t->flags & SN_FINST_MASK))
4785 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004786 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004787 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004788 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004789
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004790 /* has the response been denied ? */
4791 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004792 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004793 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004794
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004795 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004796 if (t->listener->counters)
4797 t->listener->counters->denied_resp++;
4798
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004799 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004800 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004801
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004802 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004803 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004804 if (txn->status < 200)
4805 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004806 if (wl->cond) {
4807 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
4808 ret = acl_pass(ret);
4809 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4810 ret = !ret;
4811 if (!ret)
4812 continue;
4813 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004814 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004815 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004816 }
4817
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004818 /* check whether we're already working on the frontend */
4819 if (cur_proxy == t->fe)
4820 break;
4821 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004822 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004823
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004824 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004825 * We may be facing a 100-continue response, in which case this
4826 * is not the right response, and we're waiting for the next one.
4827 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004828 * next one.
4829 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004830 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004831 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004832 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004833 msg->msg_state = HTTP_MSG_RPBEFORE;
4834 txn->status = 0;
4835 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4836 return 1;
4837 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004838 else if (unlikely(txn->status < 200))
4839 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004840
4841 /* we don't have any 1xx status code now */
4842
4843 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004844 * 4: check for server cookie.
4845 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004846 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4847 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004848 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004849
Willy Tarreaubaaee002006-06-26 02:48:02 +02004850
Willy Tarreaua15645d2007-03-18 16:22:39 +01004851 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004852 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004853 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004854 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004855 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004856
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004857 /*
4858 * 6: add server cookie in the response if needed
4859 */
4860 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004861 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
4862 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004863 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004864
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004865 /* the server is known, it's not the one the client requested, we have to
4866 * insert a set-cookie here, except if we want to insert only on POST
4867 * requests and this one isn't. Note that servers which don't have cookies
4868 * (eg: some backup servers) will return a full cookie removal request.
4869 */
4870 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4871 t->be->cookie_name,
4872 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004873
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004874 if (t->be->cookie_domain)
4875 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004876
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004877 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004878 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004879 goto return_bad_resp;
4880 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004881
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004882 /* Here, we will tell an eventual cache on the client side that we don't
4883 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4884 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4885 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4886 */
4887 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004888
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004889 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4890
4891 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004892 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004893 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004894 }
4895 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004896
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004897 /*
4898 * 7: check if result will be cacheable with a cookie.
4899 * We'll block the response if security checks have caught
4900 * nasty things such as a cacheable cookie.
4901 */
4902 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4903 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004904 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004905
4906 /* we're in presence of a cacheable response containing
4907 * a set-cookie header. We'll block it as requested by
4908 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004909 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004910 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004911 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004912
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004913 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004914 if (t->listener->counters)
4915 t->listener->counters->denied_resp++;
4916
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004917 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
4918 t->be->id, t->srv?t->srv->id:"<dispatch>");
4919 send_log(t->be, LOG_ALERT,
4920 "Blocking cacheable cookie in response from instance %s, server %s.\n",
4921 t->be->id, t->srv?t->srv->id:"<dispatch>");
4922 goto return_srv_prx_502;
4923 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004924
4925 /*
Willy Tarreau60466522010-01-18 19:08:45 +01004926 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004927 */
Willy Tarreau60466522010-01-18 19:08:45 +01004928 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
4929 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
4930 unsigned int want_flags = 0;
4931
4932 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4933 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4934 /* we want a keep-alive response here. Keep-alive header
4935 * required if either side is not 1.1.
4936 */
4937 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
4938 want_flags |= TX_CON_KAL_SET;
4939 }
4940 else {
4941 /* we want a close response here. Close header required if
4942 * the server is 1.1, regardless of the client.
4943 */
4944 if (txn->flags & TX_RES_VER_11)
4945 want_flags |= TX_CON_CLO_SET;
4946 }
4947
4948 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
4949 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01004950 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004951
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004952 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01004953 if ((txn->flags & TX_RES_XFER_LEN) ||
4954 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004955 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01004956
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004957 /*************************************************************
4958 * OK, that's finished for the headers. We have done what we *
4959 * could. Let's switch to the DATA state. *
4960 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02004961
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004962 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004963
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004964 /* if the user wants to log as soon as possible, without counting
4965 * bytes from the server, then this is the right moment. We have
4966 * to temporarily assign bytes_out to log what we currently have.
4967 */
4968 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
4969 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
4970 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01004971 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004972 t->logs.bytes_out = 0;
4973 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01004974
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004975 /* Note: we must not try to cheat by jumping directly to DATA,
4976 * otherwise we would not let the client side wake up.
4977 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01004978
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004979 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004980 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004981 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004982}
Willy Tarreaua15645d2007-03-18 16:22:39 +01004983
Willy Tarreaud98cf932009-12-27 22:54:55 +01004984/* This function is an analyser which forwards response body (including chunk
4985 * sizes if any). It is called as soon as we must forward, even if we forward
4986 * zero byte. The only situation where it must not be called is when we're in
4987 * tunnel mode and we want to forward till the close. It's used both to forward
4988 * remaining data and to resync after end of body. It expects the msg_state to
4989 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4990 * read more data, or 1 once we can go on with next request or end the session.
4991 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4992 * bytes of pending data + the headers if not already done (between som and sov).
4993 * It eventually adjusts som to match sov after the data in between have been sent.
4994 */
4995int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
4996{
4997 struct http_txn *txn = &s->txn;
4998 struct http_msg *msg = &s->txn.rsp;
4999
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005000 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5001 return 0;
5002
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005003 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005004 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005005 !s->req->analysers) {
5006 /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */
5007 buffer_ignore(res, res->l - res->send_max);
Willy Tarreau90deb182010-01-07 00:20:41 +01005008 buffer_auto_read(res);
5009 buffer_auto_close(res);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005010 res->analysers &= ~an_bit;
5011 return 1;
5012 }
5013
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005014 buffer_dont_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005015
Willy Tarreaud98cf932009-12-27 22:54:55 +01005016 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5017 /* we have msg->col and msg->sov which both point to the first
5018 * byte of message body. msg->som still points to the beginning
5019 * of the message. We must save the body in req->lr because it
5020 * survives buffer re-alignments.
5021 */
5022 res->lr = res->data + msg->sov;
5023 if (txn->flags & TX_RES_TE_CHNK)
5024 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5025 else {
5026 msg->msg_state = HTTP_MSG_DATA;
5027 }
5028 }
5029
Willy Tarreaud98cf932009-12-27 22:54:55 +01005030 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005031 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005032 /* we may have some data pending */
5033 if (msg->hdr_content_len || msg->som != msg->sov) {
5034 int bytes = msg->sov - msg->som;
5035 if (bytes < 0) /* sov may have wrapped at the end */
5036 bytes += res->size;
5037 buffer_forward(res, bytes + msg->hdr_content_len);
5038 msg->hdr_content_len = 0; /* don't forward that again */
5039 msg->som = msg->sov;
5040 }
5041
Willy Tarreaucaabe412010-01-03 23:08:28 +01005042 if (msg->msg_state == HTTP_MSG_DATA) {
5043 /* must still forward */
5044 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005045 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005046
5047 /* nothing left to forward */
5048 if (txn->flags & TX_RES_TE_CHNK)
5049 msg->msg_state = HTTP_MSG_DATA_CRLF;
5050 else
5051 msg->msg_state = HTTP_MSG_DONE;
5052 }
5053 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005054 /* read the chunk size and assign it to ->hdr_content_len, then
5055 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5056 */
5057 int ret = http_parse_chunk_size(res, msg);
5058
5059 if (!ret)
5060 goto missing_data;
5061 else if (ret < 0)
5062 goto return_bad_res;
5063 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005064 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005065 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5066 /* we want the CRLF after the data */
5067 int ret;
5068
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005069 res->lr = res->w + res->send_max;
5070 if (res->lr >= res->data + res->size)
5071 res->lr -= res->size;
5072
Willy Tarreaud98cf932009-12-27 22:54:55 +01005073 ret = http_skip_chunk_crlf(res, msg);
5074
5075 if (!ret)
5076 goto missing_data;
5077 else if (ret < 0)
5078 goto return_bad_res;
5079 /* we're in MSG_CHUNK_SIZE now */
5080 }
5081 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5082 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005083
Willy Tarreaud98cf932009-12-27 22:54:55 +01005084 if (ret == 0)
5085 goto missing_data;
5086 else if (ret < 0)
5087 goto return_bad_res;
5088 /* we're in HTTP_MSG_DONE now */
5089 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005090 else {
5091 /* other states, DONE...TUNNEL */
5092 if (http_resync_states(s)) {
5093 http_silent_debug(__LINE__, s);
5094 /* some state changes occurred, maybe the analyser
5095 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005096 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005097 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
5098 goto return_bad_res;
5099 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005100 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005101 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005102 }
5103 }
5104
Willy Tarreaud98cf932009-12-27 22:54:55 +01005105 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005106 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005107 if (res->flags & BF_SHUTR) {
5108 if (!(s->flags & SN_ERR_MASK))
5109 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01005110 s->be->counters.srv_aborts++;
5111 if (s->srv)
5112 s->srv->counters.srv_aborts++;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005113 goto return_bad_res;
Willy Tarreau40dba092010-03-04 18:14:51 +01005114 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005115
Willy Tarreau40dba092010-03-04 18:14:51 +01005116 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005117 if (!s->req->analysers)
5118 goto return_bad_res;
5119
Willy Tarreaud98cf932009-12-27 22:54:55 +01005120 /* forward the chunk size as well as any pending data */
5121 if (msg->hdr_content_len || msg->som != msg->sov) {
5122 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
5123 msg->hdr_content_len = 0; /* don't forward that again */
5124 msg->som = msg->sov;
5125 }
5126
Willy Tarreaud98cf932009-12-27 22:54:55 +01005127 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005128 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005129 return 0;
5130
Willy Tarreau40dba092010-03-04 18:14:51 +01005131 return_bad_res: /* let's centralize all bad responses */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005132 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005133 /* don't send any error message as we're in the body */
5134 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005135 res->analysers = 0;
5136 s->be->counters.failed_resp++;
5137 if (s->srv) {
5138 s->srv->counters.failed_resp++;
5139 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
5140 }
5141
5142 if (!(s->flags & SN_ERR_MASK))
5143 s->flags |= SN_ERR_PRXCOND;
5144 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005145 s->flags |= SN_FINST_D;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005146 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005147 return 0;
5148}
5149
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005150/* Iterate the same filter through all request headers.
5151 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005152 * Since it can manage the switch to another backend, it updates the per-proxy
5153 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005154 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005155int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005156{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005157 char term;
5158 char *cur_ptr, *cur_end, *cur_next;
5159 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005160 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005161 struct hdr_idx_elem *cur_hdr;
5162 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005163
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005164 last_hdr = 0;
5165
Willy Tarreau962c3f42010-01-10 00:15:35 +01005166 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005167 old_idx = 0;
5168
5169 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005170 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005171 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005172 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005173 (exp->action == ACT_ALLOW ||
5174 exp->action == ACT_DENY ||
5175 exp->action == ACT_TARPIT))
5176 return 0;
5177
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005178 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005179 if (!cur_idx)
5180 break;
5181
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005182 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005183 cur_ptr = cur_next;
5184 cur_end = cur_ptr + cur_hdr->len;
5185 cur_next = cur_end + cur_hdr->cr + 1;
5186
5187 /* Now we have one header between cur_ptr and cur_end,
5188 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005189 */
5190
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005191 /* The annoying part is that pattern matching needs
5192 * that we modify the contents to null-terminate all
5193 * strings before testing them.
5194 */
5195
5196 term = *cur_end;
5197 *cur_end = '\0';
5198
5199 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5200 switch (exp->action) {
5201 case ACT_SETBE:
5202 /* It is not possible to jump a second time.
5203 * FIXME: should we return an HTTP/500 here so that
5204 * the admin knows there's a problem ?
5205 */
5206 if (t->be != t->fe)
5207 break;
5208
5209 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005210 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005211 last_hdr = 1;
5212 break;
5213
5214 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005215 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005216 last_hdr = 1;
5217 break;
5218
5219 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005220 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005221 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005222
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005223 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005224 if (t->listener->counters)
5225 t->listener->counters->denied_resp++;
5226
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005227 break;
5228
5229 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005230 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005231 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005232
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005233 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005234 if (t->listener->counters)
5235 t->listener->counters->denied_resp++;
5236
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005237 break;
5238
5239 case ACT_REPLACE:
5240 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5241 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5242 /* FIXME: if the user adds a newline in the replacement, the
5243 * index will not be recalculated for now, and the new line
5244 * will not be counted as a new header.
5245 */
5246
5247 cur_end += delta;
5248 cur_next += delta;
5249 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005250 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005251 break;
5252
5253 case ACT_REMOVE:
5254 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5255 cur_next += delta;
5256
Willy Tarreaufa355d42009-11-29 18:12:29 +01005257 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005258 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5259 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005260 cur_hdr->len = 0;
5261 cur_end = NULL; /* null-term has been rewritten */
5262 break;
5263
5264 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005265 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005266 if (cur_end)
5267 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005268
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005269 /* keep the link from this header to next one in case of later
5270 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005271 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005272 old_idx = cur_idx;
5273 }
5274 return 0;
5275}
5276
5277
5278/* Apply the filter to the request line.
5279 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5280 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005281 * Since it can manage the switch to another backend, it updates the per-proxy
5282 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005283 */
5284int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5285{
5286 char term;
5287 char *cur_ptr, *cur_end;
5288 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005289 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005290 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005291
Willy Tarreau58f10d72006-12-04 02:26:12 +01005292
Willy Tarreau3d300592007-03-18 18:34:41 +01005293 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005294 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005295 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005296 (exp->action == ACT_ALLOW ||
5297 exp->action == ACT_DENY ||
5298 exp->action == ACT_TARPIT))
5299 return 0;
5300 else if (exp->action == ACT_REMOVE)
5301 return 0;
5302
5303 done = 0;
5304
Willy Tarreau962c3f42010-01-10 00:15:35 +01005305 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005306 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005307
5308 /* Now we have the request line between cur_ptr and cur_end */
5309
5310 /* The annoying part is that pattern matching needs
5311 * that we modify the contents to null-terminate all
5312 * strings before testing them.
5313 */
5314
5315 term = *cur_end;
5316 *cur_end = '\0';
5317
5318 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5319 switch (exp->action) {
5320 case ACT_SETBE:
5321 /* It is not possible to jump a second time.
5322 * FIXME: should we return an HTTP/500 here so that
5323 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005324 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005325 if (t->be != t->fe)
5326 break;
5327
5328 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005329 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005330 done = 1;
5331 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005332
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005333 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005334 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005335 done = 1;
5336 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005337
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005338 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005339 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005340
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005341 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005342 if (t->listener->counters)
5343 t->listener->counters->denied_resp++;
5344
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005345 done = 1;
5346 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005347
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005348 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005349 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005350
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005351 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005352 if (t->listener->counters)
5353 t->listener->counters->denied_resp++;
5354
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005355 done = 1;
5356 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005357
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005358 case ACT_REPLACE:
5359 *cur_end = term; /* restore the string terminator */
5360 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5361 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5362 /* FIXME: if the user adds a newline in the replacement, the
5363 * index will not be recalculated for now, and the new line
5364 * will not be counted as a new header.
5365 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005366
Willy Tarreaufa355d42009-11-29 18:12:29 +01005367 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005368 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005369 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005370 HTTP_MSG_RQMETH,
5371 cur_ptr, cur_end + 1,
5372 NULL, NULL);
5373 if (unlikely(!cur_end))
5374 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005375
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005376 /* we have a full request and we know that we have either a CR
5377 * or an LF at <ptr>.
5378 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005379 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5380 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005381 /* there is no point trying this regex on headers */
5382 return 1;
5383 }
5384 }
5385 *cur_end = term; /* restore the string terminator */
5386 return done;
5387}
Willy Tarreau97de6242006-12-27 17:18:38 +01005388
Willy Tarreau58f10d72006-12-04 02:26:12 +01005389
Willy Tarreau58f10d72006-12-04 02:26:12 +01005390
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005391/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005392 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005393 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005394 * unparsable request. Since it can manage the switch to another backend, it
5395 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005396 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005397int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005398{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005399 struct http_txn *txn = &s->txn;
5400 struct hdr_exp *exp;
5401
5402 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005403 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005404
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005405 /*
5406 * The interleaving of transformations and verdicts
5407 * makes it difficult to decide to continue or stop
5408 * the evaluation.
5409 */
5410
Willy Tarreau6c123b12010-01-28 20:22:06 +01005411 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5412 break;
5413
Willy Tarreau3d300592007-03-18 18:34:41 +01005414 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005415 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005416 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005417 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005418
5419 /* if this filter had a condition, evaluate it now and skip to
5420 * next filter if the condition does not match.
5421 */
5422 if (exp->cond) {
5423 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5424 ret = acl_pass(ret);
5425 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5426 ret = !ret;
5427
5428 if (!ret)
5429 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005430 }
5431
5432 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005433 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005434 if (unlikely(ret < 0))
5435 return -1;
5436
5437 if (likely(ret == 0)) {
5438 /* The filter did not match the request, it can be
5439 * iterated through all headers.
5440 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005441 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005442 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005443 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005444 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005445}
5446
5447
Willy Tarreaua15645d2007-03-18 16:22:39 +01005448
Willy Tarreau58f10d72006-12-04 02:26:12 +01005449/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005450 * Try to retrieve the server associated to the appsession.
5451 * If the server is found, it's assigned to the session.
5452 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005453void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005454 struct http_txn *txn = &t->txn;
5455 appsess *asession = NULL;
5456 char *sessid_temp = NULL;
5457
Cyril Bontéb21570a2009-11-29 20:04:48 +01005458 if (len > t->be->appsession_len) {
5459 len = t->be->appsession_len;
5460 }
5461
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005462 if (t->be->options2 & PR_O2_AS_REQL) {
5463 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005464 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005465 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005466 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005467 }
5468
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005469 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005470 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5471 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5472 return;
5473 }
5474
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005475 memcpy(txn->sessid, buf, len);
5476 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005477 }
5478
5479 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5480 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5481 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5482 return;
5483 }
5484
Cyril Bontéb21570a2009-11-29 20:04:48 +01005485 memcpy(sessid_temp, buf, len);
5486 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005487
5488 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5489 /* free previously allocated memory */
5490 pool_free2(apools.sessid, sessid_temp);
5491
5492 if (asession != NULL) {
5493 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5494 if (!(t->be->options2 & PR_O2_AS_REQL))
5495 asession->request_count++;
5496
5497 if (asession->serverid != NULL) {
5498 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005499
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005500 while (srv) {
5501 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005502 if ((srv->state & SRV_RUNNING) ||
5503 (t->be->options & PR_O_PERSIST) ||
5504 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005505 /* we found the server and it's usable */
5506 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005507 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005508 t->flags |= SN_DIRECT | SN_ASSIGNED;
5509 t->srv = srv;
5510 break;
5511 } else {
5512 txn->flags &= ~TX_CK_MASK;
5513 txn->flags |= TX_CK_DOWN;
5514 }
5515 }
5516 srv = srv->next;
5517 }
5518 }
5519 }
5520}
5521
5522/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005523 * Manage client-side cookie. It can impact performance by about 2% so it is
5524 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005525 */
5526void manage_client_side_cookies(struct session *t, struct buffer *req)
5527{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005528 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005529 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005530 char *del_colon, *del_cookie, *colon;
5531 int app_cookies;
5532
Willy Tarreau58f10d72006-12-04 02:26:12 +01005533 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005534 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005535
Willy Tarreau2a324282006-12-05 00:05:46 +01005536 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005537 * we start with the start line.
5538 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005539 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005540 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005541
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005542 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005543 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005544 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005545
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005546 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005547 cur_ptr = cur_next;
5548 cur_end = cur_ptr + cur_hdr->len;
5549 cur_next = cur_end + cur_hdr->cr + 1;
5550
5551 /* We have one full header between cur_ptr and cur_end, and the
5552 * next header starts at cur_next. We're only interested in
5553 * "Cookie:" headers.
5554 */
5555
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005556 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5557 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005558 old_idx = cur_idx;
5559 continue;
5560 }
5561
5562 /* Now look for cookies. Conforming to RFC2109, we have to support
5563 * attributes whose name begin with a '$', and associate them with
5564 * the right cookie, if we want to delete this cookie.
5565 * So there are 3 cases for each cookie read :
5566 * 1) it's a special attribute, beginning with a '$' : ignore it.
5567 * 2) it's a server id cookie that we *MAY* want to delete : save
5568 * some pointers on it (last semi-colon, beginning of cookie...)
5569 * 3) it's an application cookie : we *MAY* have to delete a previous
5570 * "special" cookie.
5571 * At the end of loop, if a "special" cookie remains, we may have to
5572 * remove it. If no application cookie persists in the header, we
5573 * *MUST* delete it
5574 */
5575
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005576 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005577
Willy Tarreau58f10d72006-12-04 02:26:12 +01005578 /* del_cookie == NULL => nothing to be deleted */
5579 del_colon = del_cookie = NULL;
5580 app_cookies = 0;
5581
5582 while (p1 < cur_end) {
5583 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005584 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005585 while (p1 < cur_end) {
5586 if (*p1 == ';' || *p1 == ',')
5587 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005588 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005589 break;
5590 p1++;
5591 }
5592
5593 if (p1 == cur_end)
5594 break;
5595
5596 /* p1 is at the beginning of the cookie name */
5597 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005598 while (p2 < cur_end && *p2 != '=') {
5599 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5600 /* oops, the cookie name was truncated, resync */
5601 p1 = p2;
5602 goto resync_name;
5603 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005604 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005605 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005606
5607 if (p2 == cur_end)
5608 break;
5609
5610 p3 = p2 + 1; /* skips the '=' sign */
5611 if (p3 == cur_end)
5612 break;
5613
Willy Tarreau305ae852010-01-03 19:45:54 +01005614 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5615 p5 = p4 = p3;
5616 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5617 if (!isspace((unsigned char)*p5))
5618 p4 = p5 + 1;
5619 p5++;
5620 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005621
5622 /* here, we have the cookie name between p1 and p2,
5623 * and its value between p3 and p4.
5624 * we can process it :
5625 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005626 * Cookie: NAME=VALUE ;
5627 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005628 * | || || +--> p4
5629 * | || |+-------> p3
5630 * | || +--------> p2
5631 * | |+------------> p1
5632 * | +-------------> colon
5633 * +--------------------> cur_ptr
5634 */
5635
5636 if (*p1 == '$') {
5637 /* skip this one */
5638 }
5639 else {
5640 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005641 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005642 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005643 (p4 - p1 >= t->fe->capture_namelen) &&
5644 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005645 int log_len = p4 - p1;
5646
Willy Tarreau086b3b42007-05-13 21:45:51 +02005647 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005648 Alert("HTTP logging : out of memory.\n");
5649 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005650 if (log_len > t->fe->capture_len)
5651 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005652 memcpy(txn->cli_cookie, p1, log_len);
5653 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005654 }
5655 }
5656
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005657 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5658 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005659 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005660 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005661 char *delim;
5662
5663 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5664 * have the server ID betweek p3 and delim, and the original cookie between
5665 * delim+1 and p4. Otherwise, delim==p4 :
5666 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005667 * Cookie: NAME=SRV~VALUE ;
5668 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005669 * | || || | +--> p4
5670 * | || || +--------> delim
5671 * | || |+-----------> p3
5672 * | || +------------> p2
5673 * | |+----------------> p1
5674 * | +-----------------> colon
5675 * +------------------------> cur_ptr
5676 */
5677
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005678 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005679 for (delim = p3; delim < p4; delim++)
5680 if (*delim == COOKIE_DELIM)
5681 break;
5682 }
5683 else
5684 delim = p4;
5685
5686
5687 /* Here, we'll look for the first running server which supports the cookie.
5688 * This allows to share a same cookie between several servers, for example
5689 * to dedicate backup servers to specific servers only.
5690 * However, to prevent clients from sticking to cookie-less backup server
5691 * when they have incidentely learned an empty cookie, we simply ignore
5692 * empty cookies and mark them as invalid.
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005693 * The same behaviour is applied when persistence must be ignored.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005694 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005695 if ((delim == p3) || (t->flags & SN_IGNORE_PRST))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005696 srv = NULL;
5697
5698 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005699 if (srv->cookie && (srv->cklen == delim - p3) &&
5700 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005701 if ((srv->state & SRV_RUNNING) ||
5702 (t->be->options & PR_O_PERSIST) ||
5703 (t->flags & SN_FORCE_PRST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005704 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005705 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005706 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreau3d300592007-03-18 18:34:41 +01005707 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005708 t->srv = srv;
5709 break;
5710 } else {
5711 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005712 txn->flags &= ~TX_CK_MASK;
5713 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005714 }
5715 }
5716 srv = srv->next;
5717 }
5718
Willy Tarreau3d300592007-03-18 18:34:41 +01005719 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005720 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005721 txn->flags &= ~TX_CK_MASK;
5722 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005723 }
5724
5725 /* depending on the cookie mode, we may have to either :
5726 * - delete the complete cookie if we're in insert+indirect mode, so that
5727 * the server never sees it ;
5728 * - remove the server id from the cookie value, and tag the cookie as an
5729 * application cookie so that it does not get accidentely removed later,
5730 * if we're in cookie prefix mode
5731 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005732 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005733 int delta; /* negative */
5734
5735 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5736 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005737 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005738 cur_end += delta;
5739 cur_next += delta;
5740 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005741 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005742
5743 del_cookie = del_colon = NULL;
5744 app_cookies++; /* protect the header from deletion */
5745 }
5746 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005747 (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 +01005748 del_cookie = p1;
5749 del_colon = colon;
5750 }
5751 } else {
5752 /* now we know that we must keep this cookie since it's
5753 * not ours. But if we wanted to delete our cookie
5754 * earlier, we cannot remove the complete header, but we
5755 * can remove the previous block itself.
5756 */
5757 app_cookies++;
5758
5759 if (del_cookie != NULL) {
5760 int delta; /* negative */
5761
5762 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5763 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005764 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005765 cur_end += delta;
5766 cur_next += delta;
5767 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005768 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005769 del_cookie = del_colon = NULL;
5770 }
5771 }
5772
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005773 /* Look for the appsession cookie unless persistence must be ignored */
5774 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01005775 int cmp_len, value_len;
5776 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005777
Cyril Bontéb21570a2009-11-29 20:04:48 +01005778 if (t->be->options2 & PR_O2_AS_PFX) {
5779 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5780 value_begin = p1 + t->be->appsession_name_len;
5781 value_len = p4 - p1 - t->be->appsession_name_len;
5782 } else {
5783 cmp_len = p2 - p1;
5784 value_begin = p3;
5785 value_len = p4 - p3;
5786 }
5787
5788 /* let's see if the cookie is our appcookie */
Cyril Bonté17530c32010-04-06 21:11:10 +02005789 if ((cmp_len == t->be->appsession_name_len) &&
5790 (memcmp(p1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01005791 /* Cool... it's the right one */
5792 manage_client_side_appsession(t, value_begin, value_len);
5793 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005794#if defined(DEBUG_HASH)
5795 Alert("manage_client_side_cookies\n");
5796 appsession_hash_dump(&(t->be->htbl_proxy));
5797#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005798 }/* end if ((t->proxy->appsession_name != NULL) ... */
5799 }
5800
5801 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005802 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005803 } /* while (p1 < cur_end) */
5804
5805 /* There's no more cookie on this line.
5806 * We may have marked the last one(s) for deletion.
5807 * We must do this now in two ways :
5808 * - if there is no app cookie, we simply delete the header ;
5809 * - if there are app cookies, we must delete the end of the
5810 * string properly, including the colon/semi-colon before
5811 * the cookie name.
5812 */
5813 if (del_cookie != NULL) {
5814 int delta;
5815 if (app_cookies) {
5816 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5817 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005818 cur_hdr->len += delta;
5819 } else {
5820 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005821
5822 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005823 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5824 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005825 cur_hdr->len = 0;
5826 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005827 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005828 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005829 }
5830
5831 /* keep the link from this header to next one */
5832 old_idx = cur_idx;
5833 } /* end of cookie processing on this header */
5834}
5835
5836
Willy Tarreaua15645d2007-03-18 16:22:39 +01005837/* Iterate the same filter through all response headers contained in <rtr>.
5838 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5839 */
5840int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5841{
5842 char term;
5843 char *cur_ptr, *cur_end, *cur_next;
5844 int cur_idx, old_idx, last_hdr;
5845 struct http_txn *txn = &t->txn;
5846 struct hdr_idx_elem *cur_hdr;
5847 int len, delta;
5848
5849 last_hdr = 0;
5850
Willy Tarreau962c3f42010-01-10 00:15:35 +01005851 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005852 old_idx = 0;
5853
5854 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005855 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005856 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005857 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005858 (exp->action == ACT_ALLOW ||
5859 exp->action == ACT_DENY))
5860 return 0;
5861
5862 cur_idx = txn->hdr_idx.v[old_idx].next;
5863 if (!cur_idx)
5864 break;
5865
5866 cur_hdr = &txn->hdr_idx.v[cur_idx];
5867 cur_ptr = cur_next;
5868 cur_end = cur_ptr + cur_hdr->len;
5869 cur_next = cur_end + cur_hdr->cr + 1;
5870
5871 /* Now we have one header between cur_ptr and cur_end,
5872 * and the next header starts at cur_next.
5873 */
5874
5875 /* The annoying part is that pattern matching needs
5876 * that we modify the contents to null-terminate all
5877 * strings before testing them.
5878 */
5879
5880 term = *cur_end;
5881 *cur_end = '\0';
5882
5883 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5884 switch (exp->action) {
5885 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005886 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005887 last_hdr = 1;
5888 break;
5889
5890 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005891 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005892 last_hdr = 1;
5893 break;
5894
5895 case ACT_REPLACE:
5896 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5897 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5898 /* FIXME: if the user adds a newline in the replacement, the
5899 * index will not be recalculated for now, and the new line
5900 * will not be counted as a new header.
5901 */
5902
5903 cur_end += delta;
5904 cur_next += delta;
5905 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005906 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005907 break;
5908
5909 case ACT_REMOVE:
5910 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
5911 cur_next += delta;
5912
Willy Tarreaufa355d42009-11-29 18:12:29 +01005913 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005914 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5915 txn->hdr_idx.used--;
5916 cur_hdr->len = 0;
5917 cur_end = NULL; /* null-term has been rewritten */
5918 break;
5919
5920 }
5921 }
5922 if (cur_end)
5923 *cur_end = term; /* restore the string terminator */
5924
5925 /* keep the link from this header to next one in case of later
5926 * removal of next header.
5927 */
5928 old_idx = cur_idx;
5929 }
5930 return 0;
5931}
5932
5933
5934/* Apply the filter to the status line in the response buffer <rtr>.
5935 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5936 * or -1 if a replacement resulted in an invalid status line.
5937 */
5938int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5939{
5940 char term;
5941 char *cur_ptr, *cur_end;
5942 int done;
5943 struct http_txn *txn = &t->txn;
5944 int len, delta;
5945
5946
Willy Tarreau3d300592007-03-18 18:34:41 +01005947 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005948 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005949 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005950 (exp->action == ACT_ALLOW ||
5951 exp->action == ACT_DENY))
5952 return 0;
5953 else if (exp->action == ACT_REMOVE)
5954 return 0;
5955
5956 done = 0;
5957
Willy Tarreau962c3f42010-01-10 00:15:35 +01005958 cur_ptr = txn->rsp.sol;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005959 cur_end = cur_ptr + txn->rsp.sl.rq.l;
5960
5961 /* Now we have the status line between cur_ptr and cur_end */
5962
5963 /* The annoying part is that pattern matching needs
5964 * that we modify the contents to null-terminate all
5965 * strings before testing them.
5966 */
5967
5968 term = *cur_end;
5969 *cur_end = '\0';
5970
5971 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5972 switch (exp->action) {
5973 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005974 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005975 done = 1;
5976 break;
5977
5978 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005979 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005980 done = 1;
5981 break;
5982
5983 case ACT_REPLACE:
5984 *cur_end = term; /* restore the string terminator */
5985 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5986 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
5987 /* FIXME: if the user adds a newline in the replacement, the
5988 * index will not be recalculated for now, and the new line
5989 * will not be counted as a new header.
5990 */
5991
Willy Tarreaufa355d42009-11-29 18:12:29 +01005992 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005993 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005994 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02005995 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01005996 cur_ptr, cur_end + 1,
5997 NULL, NULL);
5998 if (unlikely(!cur_end))
5999 return -1;
6000
6001 /* we have a full respnse and we know that we have either a CR
6002 * or an LF at <ptr>.
6003 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006004 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006005 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r');
6006 /* there is no point trying this regex on headers */
6007 return 1;
6008 }
6009 }
6010 *cur_end = term; /* restore the string terminator */
6011 return done;
6012}
6013
6014
6015
6016/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006017 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006018 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6019 * unparsable response.
6020 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006021int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006022{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006023 struct http_txn *txn = &s->txn;
6024 struct hdr_exp *exp;
6025
6026 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006027 int ret;
6028
6029 /*
6030 * The interleaving of transformations and verdicts
6031 * makes it difficult to decide to continue or stop
6032 * the evaluation.
6033 */
6034
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006035 if (txn->flags & TX_SVDENY)
6036 break;
6037
Willy Tarreau3d300592007-03-18 18:34:41 +01006038 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006039 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6040 exp->action == ACT_PASS)) {
6041 exp = exp->next;
6042 continue;
6043 }
6044
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006045 /* if this filter had a condition, evaluate it now and skip to
6046 * next filter if the condition does not match.
6047 */
6048 if (exp->cond) {
6049 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6050 ret = acl_pass(ret);
6051 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6052 ret = !ret;
6053 if (!ret)
6054 continue;
6055 }
6056
Willy Tarreaua15645d2007-03-18 16:22:39 +01006057 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006058 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006059 if (unlikely(ret < 0))
6060 return -1;
6061
6062 if (likely(ret == 0)) {
6063 /* The filter did not match the response, it can be
6064 * iterated through all headers.
6065 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006066 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006067 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006068 }
6069 return 0;
6070}
6071
6072
6073
6074/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006075 * Manage server-side cookies. It can impact performance by about 2% so it is
6076 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006077 */
6078void manage_server_side_cookies(struct session *t, struct buffer *rtr)
6079{
6080 struct http_txn *txn = &t->txn;
6081 char *p1, *p2, *p3, *p4;
6082
Willy Tarreaua15645d2007-03-18 16:22:39 +01006083 char *cur_ptr, *cur_end, *cur_next;
6084 int cur_idx, old_idx, delta;
6085
Willy Tarreaua15645d2007-03-18 16:22:39 +01006086 /* Iterate through the headers.
6087 * we start with the start line.
6088 */
6089 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006090 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006091
6092 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6093 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006094 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006095
6096 cur_hdr = &txn->hdr_idx.v[cur_idx];
6097 cur_ptr = cur_next;
6098 cur_end = cur_ptr + cur_hdr->len;
6099 cur_next = cur_end + cur_hdr->cr + 1;
6100
6101 /* We have one full header between cur_ptr and cur_end, and the
6102 * next header starts at cur_next. We're only interested in
6103 * "Cookie:" headers.
6104 */
6105
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006106 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
6107 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006108 old_idx = cur_idx;
6109 continue;
6110 }
6111
6112 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01006113 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006114
6115
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006116 /* maybe we only wanted to see if there was a set-cookie. Note that
6117 * the cookie capture is declared in the fronend.
6118 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006119 if (t->be->cookie_name == NULL &&
6120 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006121 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006122 return;
6123
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006124 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006125
6126 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006127 if (p1 == cur_end || *p1 == ';') /* end of cookie */
6128 break;
6129
6130 /* p1 is at the beginning of the cookie name */
6131 p2 = p1;
6132
6133 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
6134 p2++;
6135
6136 if (p2 == cur_end || *p2 == ';') /* next cookie */
6137 break;
6138
6139 p3 = p2 + 1; /* skip the '=' sign */
6140 if (p3 == cur_end)
6141 break;
6142
6143 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006144 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01006145 p4++;
6146
6147 /* here, we have the cookie name between p1 and p2,
6148 * and its value between p3 and p4.
6149 * we can process it.
6150 */
6151
6152 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006153 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006154 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006155 (p4 - p1 >= t->fe->capture_namelen) &&
6156 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006157 int log_len = p4 - p1;
6158
Willy Tarreau086b3b42007-05-13 21:45:51 +02006159 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006160 Alert("HTTP logging : out of memory.\n");
6161 }
6162
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006163 if (log_len > t->fe->capture_len)
6164 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006165 memcpy(txn->srv_cookie, p1, log_len);
6166 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006167 }
6168
6169 /* now check if we need to process it for persistence */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006170 if (!(t->flags & SN_IGNORE_PRST) && (p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006171 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006172 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01006173 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006174
6175 /* If the cookie is in insert mode on a known server, we'll delete
6176 * this occurrence because we'll insert another one later.
6177 * We'll delete it too if the "indirect" option is set and we're in
6178 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006179 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
6180 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006181 /* this header must be deleted */
6182 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6183 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6184 txn->hdr_idx.used--;
6185 cur_hdr->len = 0;
6186 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006187 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006188
Willy Tarreau3d300592007-03-18 18:34:41 +01006189 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006190 }
6191 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006192 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006193 /* replace bytes p3->p4 with the cookie name associated
6194 * with this server since we know it.
6195 */
6196 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
6197 cur_hdr->len += delta;
6198 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006199 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006200
Willy Tarreau3d300592007-03-18 18:34:41 +01006201 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006202 }
6203 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006204 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006205 /* insert the cookie name associated with this server
6206 * before existing cookie, and insert a delimitor between them..
6207 */
6208 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
6209 cur_hdr->len += delta;
6210 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006211 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006212
6213 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01006214 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006215 }
6216 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006217 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6218 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006219 int cmp_len, value_len;
6220 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006221
Cyril Bontéb21570a2009-11-29 20:04:48 +01006222 if (t->be->options2 & PR_O2_AS_PFX) {
6223 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
6224 value_begin = p1 + t->be->appsession_name_len;
6225 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
6226 } else {
6227 cmp_len = p2 - p1;
6228 value_begin = p3;
6229 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006230 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006231
Cyril Bonté17530c32010-04-06 21:11:10 +02006232 if ((cmp_len == t->be->appsession_name_len) &&
6233 (memcmp(p1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006234 /* Cool... it's the right one */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006235 if (txn->sessid != NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006236 /* free previously allocated memory as we don't need it anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006237 pool_free2(apools.sessid, txn->sessid);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006238 }
6239 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006240 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006241 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6242 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6243 return;
6244 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006245 memcpy(txn->sessid, value_begin, value_len);
6246 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006247 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006248 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006249 break; /* we don't want to loop again since there cannot be another cookie on the same line */
6250 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006251 /* keep the link from this header to next one */
6252 old_idx = cur_idx;
6253 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006254
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006255 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006256 appsess *asession = NULL;
6257 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006258 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006259 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006260 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006261 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6262 Alert("Not enough Memory process_srv():asession:calloc().\n");
6263 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6264 return;
6265 }
6266 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6267 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6268 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006269 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006270 return;
6271 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006272 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006273 asession->sessid[t->be->appsession_len] = 0;
6274
Willy Tarreau1fac7532010-01-09 19:23:06 +01006275 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006276 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
6277 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6278 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006279 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006280 return;
6281 }
6282 asession->serverid[0] = '\0';
6283 memcpy(asession->serverid, t->srv->id, server_id_len);
6284
6285 asession->request_count = 0;
6286 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6287 }
6288
6289 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6290 asession->request_count++;
6291 }
6292
6293#if defined(DEBUG_HASH)
Cyril Bonté17530c32010-04-06 21:11:10 +02006294 if (t->be->appsession_name) {
6295 Alert("manage_server_side_cookies\n");
6296 appsession_hash_dump(&(t->be->htbl_proxy));
6297 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006298#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01006299}
6300
6301
6302
6303/*
6304 * Check if response is cacheable or not. Updates t->flags.
6305 */
6306void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6307{
6308 struct http_txn *txn = &t->txn;
6309 char *p1, *p2;
6310
6311 char *cur_ptr, *cur_end, *cur_next;
6312 int cur_idx;
6313
Willy Tarreau5df51872007-11-25 16:20:08 +01006314 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006315 return;
6316
6317 /* Iterate through the headers.
6318 * we start with the start line.
6319 */
6320 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006321 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006322
6323 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6324 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006325 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006326
6327 cur_hdr = &txn->hdr_idx.v[cur_idx];
6328 cur_ptr = cur_next;
6329 cur_end = cur_ptr + cur_hdr->len;
6330 cur_next = cur_end + cur_hdr->cr + 1;
6331
6332 /* We have one full header between cur_ptr and cur_end, and the
6333 * next header starts at cur_next. We're only interested in
6334 * "Cookie:" headers.
6335 */
6336
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006337 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6338 if (val) {
6339 if ((cur_end - (cur_ptr + val) >= 8) &&
6340 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6341 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6342 return;
6343 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006344 }
6345
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006346 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6347 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006348 continue;
6349
6350 /* OK, right now we know we have a cache-control header at cur_ptr */
6351
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006352 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006353
6354 if (p1 >= cur_end) /* no more info */
6355 continue;
6356
6357 /* p1 is at the beginning of the value */
6358 p2 = p1;
6359
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006360 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006361 p2++;
6362
6363 /* we have a complete value between p1 and p2 */
6364 if (p2 < cur_end && *p2 == '=') {
6365 /* we have something of the form no-cache="set-cookie" */
6366 if ((cur_end - p1 >= 21) &&
6367 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6368 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006369 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006370 continue;
6371 }
6372
6373 /* OK, so we know that either p2 points to the end of string or to a comma */
6374 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6375 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6376 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6377 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006378 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006379 return;
6380 }
6381
6382 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006383 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006384 continue;
6385 }
6386 }
6387}
6388
6389
Willy Tarreau58f10d72006-12-04 02:26:12 +01006390/*
6391 * Try to retrieve a known appsession in the URI, then the associated server.
6392 * If the server is found, it's assigned to the session.
6393 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006394void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006395{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006396 char *end_params, *first_param, *cur_param, *next_param;
6397 char separator;
6398 int value_len;
6399
6400 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006401
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006402 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02006403 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST && t->txn.meth != HTTP_METH_HEAD)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006404 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006405 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006406
Cyril Bontéb21570a2009-11-29 20:04:48 +01006407 first_param = NULL;
6408 switch (mode) {
6409 case PR_O2_AS_M_PP:
6410 first_param = memchr(begin, ';', len);
6411 break;
6412 case PR_O2_AS_M_QS:
6413 first_param = memchr(begin, '?', len);
6414 break;
6415 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006416
Cyril Bontéb21570a2009-11-29 20:04:48 +01006417 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006418 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006419 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006420
Cyril Bontéb21570a2009-11-29 20:04:48 +01006421 switch (mode) {
6422 case PR_O2_AS_M_PP:
6423 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6424 end_params = (char *) begin + len;
6425 }
6426 separator = ';';
6427 break;
6428 case PR_O2_AS_M_QS:
6429 end_params = (char *) begin + len;
6430 separator = '&';
6431 break;
6432 default:
6433 /* unknown mode, shouldn't happen */
6434 return;
6435 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006436
Cyril Bontéb21570a2009-11-29 20:04:48 +01006437 cur_param = next_param = end_params;
6438 while (cur_param > first_param) {
6439 cur_param--;
6440 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6441 /* let's see if this is the appsession parameter */
6442 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6443 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6444 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6445 /* Cool... it's the right one */
6446 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6447 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6448 if (value_len > 0) {
6449 manage_client_side_appsession(t, cur_param, value_len);
6450 }
6451 break;
6452 }
6453 next_param = cur_param;
6454 }
6455 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006456#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006457 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006458 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006459#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006460}
6461
Willy Tarreaub2513902006-12-17 14:52:38 +01006462/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006463 * In a GET or HEAD request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006464 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01006465 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006466 * It is assumed that the request is either a HEAD or GET and that the
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006467 * t->be->uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006468 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006469 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01006470 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006471int stats_check_uri(struct session *t, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01006472{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006473 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006474 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006475 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006476
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006477 if (!uri_auth)
6478 return 0;
6479
6480 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD)
6481 return 0;
6482
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006483 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6484
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006485 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006486 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006487 return 0;
6488
Willy Tarreau962c3f42010-01-10 00:15:35 +01006489 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006490
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006491 /* the URI is in h */
6492 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006493 return 0;
6494
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006495 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006496 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006497 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006498 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006499 break;
6500 }
6501 h++;
6502 }
6503
6504 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006505 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6506 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006507 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006508 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006509 break;
6510 }
6511 h++;
6512 }
6513 }
6514
Willy Tarreau962c3f42010-01-10 00:15:35 +01006515 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6516 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006517 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006518 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006519 break;
6520 }
6521 h++;
6522 }
6523
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006524 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6525
Willy Tarreaub2513902006-12-17 14:52:38 +01006526 return 1;
6527}
6528
Willy Tarreau4076a152009-04-02 15:18:36 +02006529/*
6530 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006531 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6532 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006533 */
6534void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6535 struct buffer *buf, struct http_msg *msg,
6536 struct proxy *other_end)
6537{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006538 es->len = buf->r - (buf->data + msg->som);
6539 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006540 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006541 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006542 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006543 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006544 es->when = date; // user-visible date
6545 es->sid = s->uniq_id;
6546 es->srv = s->srv;
6547 es->oe = other_end;
6548 es->src = s->cli_addr;
6549}
Willy Tarreaub2513902006-12-17 14:52:38 +01006550
Willy Tarreaubce70882009-09-07 11:51:47 +02006551/* return the IP address pointed to by occurrence <occ> of header <hname> in
6552 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
6553 * occurrence number corresponding to this value is returned. If <occ> is
6554 * strictly negative, the occurrence number before the end corresponding to
6555 * this value is returned. If <occ> is null, any value is returned, so it is
6556 * not recommended to use it that way. Negative occurrences are limited to
6557 * a small value because it is required to keep them in memory while scanning.
6558 * IP address 0.0.0.0 is returned if no match is found.
6559 */
6560unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
6561{
6562 struct hdr_ctx ctx;
6563 unsigned int hdr_hist[MAX_HDR_HISTORY];
6564 unsigned int hist_ptr;
6565 int found = 0;
6566
6567 ctx.idx = 0;
6568 if (occ >= 0) {
6569 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
6570 occ--;
6571 if (occ <= 0) {
6572 found = 1;
6573 break;
6574 }
6575 }
6576 if (!found)
6577 return 0;
6578 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
6579 }
6580
6581 /* negative occurrence, we scan all the list then walk back */
6582 if (-occ > MAX_HDR_HISTORY)
6583 return 0;
6584
6585 hist_ptr = 0;
6586 hdr_hist[hist_ptr] = 0;
6587 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
6588 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
6589 if (hist_ptr >= MAX_HDR_HISTORY)
6590 hist_ptr = 0;
6591 found++;
6592 }
6593 if (-occ > found)
6594 return 0;
6595 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
6596 * find occurrence -occ, so we have to check [hist_ptr+occ].
6597 */
6598 hist_ptr += occ;
6599 if (hist_ptr >= MAX_HDR_HISTORY)
6600 hist_ptr -= MAX_HDR_HISTORY;
6601 return hdr_hist[hist_ptr];
6602}
6603
Willy Tarreaubaaee002006-06-26 02:48:02 +02006604/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006605 * Print a debug line with a header
6606 */
6607void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6608{
6609 int len, max;
6610 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006611 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006612 max = end - start;
6613 UBOUND(max, sizeof(trash) - len - 1);
6614 len += strlcpy2(trash + len, start, max + 1);
6615 trash[len++] = '\n';
6616 write(1, trash, len);
6617}
6618
Willy Tarreau0937bc42009-12-22 15:03:09 +01006619/*
6620 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6621 * the required fields are properly allocated and that we only need to (re)init
6622 * them. This should be used before processing any new request.
6623 */
6624void http_init_txn(struct session *s)
6625{
6626 struct http_txn *txn = &s->txn;
6627 struct proxy *fe = s->fe;
6628
6629 txn->flags = 0;
6630 txn->status = -1;
6631
6632 txn->req.sol = txn->req.eol = NULL;
6633 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6634 txn->rsp.sol = txn->rsp.eol = NULL;
6635 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6636 txn->req.hdr_content_len = 0LL;
6637 txn->rsp.hdr_content_len = 0LL;
6638 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6639 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01006640
6641 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006642
6643 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6644 if (fe->options2 & PR_O2_REQBUG_OK)
6645 txn->req.err_pos = -1; /* let buggy requests pass */
6646
Willy Tarreau46023632010-01-07 22:51:47 +01006647 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006648 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6649
Willy Tarreau46023632010-01-07 22:51:47 +01006650 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006651 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6652
6653 if (txn->hdr_idx.v)
6654 hdr_idx_init(&txn->hdr_idx);
6655}
6656
6657/* to be used at the end of a transaction */
6658void http_end_txn(struct session *s)
6659{
6660 struct http_txn *txn = &s->txn;
6661
6662 /* these ones will have been dynamically allocated */
6663 pool_free2(pool2_requri, txn->uri);
6664 pool_free2(pool2_capture, txn->cli_cookie);
6665 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006666 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01006667
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006668 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006669 txn->uri = NULL;
6670 txn->srv_cookie = NULL;
6671 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006672
6673 if (txn->req.cap) {
6674 struct cap_hdr *h;
6675 for (h = s->fe->req_cap; h; h = h->next)
6676 pool_free2(h->pool, txn->req.cap[h->index]);
6677 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6678 }
6679
6680 if (txn->rsp.cap) {
6681 struct cap_hdr *h;
6682 for (h = s->fe->rsp_cap; h; h = h->next)
6683 pool_free2(h->pool, txn->rsp.cap[h->index]);
6684 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6685 }
6686
Willy Tarreau0937bc42009-12-22 15:03:09 +01006687}
6688
6689/* to be used at the end of a transaction to prepare a new one */
6690void http_reset_txn(struct session *s)
6691{
6692 http_end_txn(s);
6693 http_init_txn(s);
6694
6695 s->be = s->fe;
6696 s->req->analysers = s->listener->analysers;
6697 s->logs.logwait = s->fe->to_log;
6698 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01006699 /* re-init store persistence */
6700 s->store_count = 0;
6701
Willy Tarreau0937bc42009-12-22 15:03:09 +01006702 s->pend_pos = NULL;
6703 s->conn_retries = s->be->conn_retries;
6704
6705 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6706
Willy Tarreau739cfba2010-01-25 23:11:14 +01006707 /* We must trim any excess data from the response buffer, because we
6708 * may have blocked an invalid response from a server that we don't
6709 * want to accidentely forward once we disable the analysers, nor do
6710 * we want those data to come along with next response. A typical
6711 * example of such data would be from a buggy server responding to
6712 * a HEAD with some data, or sending more than the advertised
6713 * content-length.
6714 */
6715 if (unlikely(s->rep->l > s->rep->send_max)) {
6716 s->rep->l = s->rep->send_max;
6717 s->rep->r = s->rep->w + s->rep->l;
6718 if (s->rep->r >= s->rep->data + s->rep->size)
6719 s->rep->r -= s->rep->size;
6720 }
6721
Willy Tarreau0937bc42009-12-22 15:03:09 +01006722 s->req->rto = s->fe->timeout.client;
6723 s->req->wto = s->be->timeout.server;
6724 s->req->cto = s->be->timeout.connect;
6725
6726 s->rep->rto = s->be->timeout.server;
6727 s->rep->wto = s->fe->timeout.client;
6728 s->rep->cto = TICK_ETERNITY;
6729
6730 s->req->rex = TICK_ETERNITY;
6731 s->req->wex = TICK_ETERNITY;
6732 s->req->analyse_exp = TICK_ETERNITY;
6733 s->rep->rex = TICK_ETERNITY;
6734 s->rep->wex = TICK_ETERNITY;
6735 s->rep->analyse_exp = TICK_ETERNITY;
6736}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006737
Willy Tarreau8797c062007-05-07 00:55:35 +02006738/************************************************************************/
6739/* The code below is dedicated to ACL parsing and matching */
6740/************************************************************************/
6741
6742
6743
6744
6745/* 1. Check on METHOD
6746 * We use the pre-parsed method if it is known, and store its number as an
6747 * integer. If it is unknown, we use the pointer and the length.
6748 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006749static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006750{
6751 int len, meth;
6752
Willy Tarreauae8b7962007-06-09 23:10:04 +02006753 len = strlen(*text);
6754 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006755
6756 pattern->val.i = meth;
6757 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006758 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006759 if (!pattern->ptr.str)
6760 return 0;
6761 pattern->len = len;
6762 }
6763 return 1;
6764}
6765
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006766static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006767acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6768 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006769{
6770 int meth;
6771 struct http_txn *txn = l7;
6772
Willy Tarreaub6866442008-07-14 23:54:42 +02006773 if (!txn)
6774 return 0;
6775
Willy Tarreau655dce92009-11-08 13:10:58 +01006776 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006777 return 0;
6778
Willy Tarreau8797c062007-05-07 00:55:35 +02006779 meth = txn->meth;
6780 test->i = meth;
6781 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006782 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6783 /* ensure the indexes are not affected */
6784 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006785 test->len = txn->req.sl.rq.m_l;
6786 test->ptr = txn->req.sol;
6787 }
6788 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6789 return 1;
6790}
6791
6792static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6793{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006794 int icase;
6795
Willy Tarreau8797c062007-05-07 00:55:35 +02006796 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006797 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006798
6799 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006800 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006801
6802 /* Other method, we must compare the strings */
6803 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006804 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006805
6806 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6807 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6808 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006809 return ACL_PAT_FAIL;
6810 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006811}
6812
6813/* 2. Check on Request/Status Version
6814 * We simply compare strings here.
6815 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006816static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006817{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006818 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006819 if (!pattern->ptr.str)
6820 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006821 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006822 return 1;
6823}
6824
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006825static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006826acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6827 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006828{
6829 struct http_txn *txn = l7;
6830 char *ptr;
6831 int len;
6832
Willy Tarreaub6866442008-07-14 23:54:42 +02006833 if (!txn)
6834 return 0;
6835
Willy Tarreau655dce92009-11-08 13:10:58 +01006836 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006837 return 0;
6838
Willy Tarreau8797c062007-05-07 00:55:35 +02006839 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006840 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006841
6842 while ((len-- > 0) && (*ptr++ != '/'));
6843 if (len <= 0)
6844 return 0;
6845
6846 test->ptr = ptr;
6847 test->len = len;
6848
6849 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6850 return 1;
6851}
6852
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006853static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006854acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6855 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006856{
6857 struct http_txn *txn = l7;
6858 char *ptr;
6859 int len;
6860
Willy Tarreaub6866442008-07-14 23:54:42 +02006861 if (!txn)
6862 return 0;
6863
Willy Tarreau655dce92009-11-08 13:10:58 +01006864 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006865 return 0;
6866
Willy Tarreau8797c062007-05-07 00:55:35 +02006867 len = txn->rsp.sl.st.v_l;
6868 ptr = txn->rsp.sol;
6869
6870 while ((len-- > 0) && (*ptr++ != '/'));
6871 if (len <= 0)
6872 return 0;
6873
6874 test->ptr = ptr;
6875 test->len = len;
6876
6877 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6878 return 1;
6879}
6880
6881/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006882static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006883acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6884 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006885{
6886 struct http_txn *txn = l7;
6887 char *ptr;
6888 int len;
6889
Willy Tarreaub6866442008-07-14 23:54:42 +02006890 if (!txn)
6891 return 0;
6892
Willy Tarreau655dce92009-11-08 13:10:58 +01006893 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006894 return 0;
6895
Willy Tarreau8797c062007-05-07 00:55:35 +02006896 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006897 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02006898
6899 test->i = __strl2ui(ptr, len);
6900 test->flags = ACL_TEST_F_VOL_1ST;
6901 return 1;
6902}
6903
6904/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006905static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006906acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
6907 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006908{
6909 struct http_txn *txn = l7;
6910
Willy Tarreaub6866442008-07-14 23:54:42 +02006911 if (!txn)
6912 return 0;
6913
Willy Tarreau655dce92009-11-08 13:10:58 +01006914 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006915 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006916
Willy Tarreauc11416f2007-06-17 16:58:38 +02006917 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6918 /* ensure the indexes are not affected */
6919 return 0;
6920
Willy Tarreau8797c062007-05-07 00:55:35 +02006921 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006922 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02006923
Willy Tarreauf3d25982007-05-08 22:45:09 +02006924 /* we do not need to set READ_ONLY because the data is in a buffer */
6925 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02006926 return 1;
6927}
6928
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006929static int
6930acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
6931 struct acl_expr *expr, struct acl_test *test)
6932{
6933 struct http_txn *txn = l7;
6934
Willy Tarreaub6866442008-07-14 23:54:42 +02006935 if (!txn)
6936 return 0;
6937
Willy Tarreau655dce92009-11-08 13:10:58 +01006938 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006939 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006940
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006941 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6942 /* ensure the indexes are not affected */
6943 return 0;
6944
6945 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006946 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006947 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
6948 test->i = AF_INET;
6949
6950 /*
6951 * If we are parsing url in frontend space, we prepare backend stage
6952 * to not parse again the same url ! optimization lazyness...
6953 */
6954 if (px->options & PR_O_HTTP_PROXY)
6955 l4->flags |= SN_ADDR_SET;
6956
6957 test->flags = ACL_TEST_F_READ_ONLY;
6958 return 1;
6959}
6960
6961static int
6962acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
6963 struct acl_expr *expr, struct acl_test *test)
6964{
6965 struct http_txn *txn = l7;
6966
Willy Tarreaub6866442008-07-14 23:54:42 +02006967 if (!txn)
6968 return 0;
6969
Willy Tarreau655dce92009-11-08 13:10:58 +01006970 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006971 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02006972
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006973 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6974 /* ensure the indexes are not affected */
6975 return 0;
6976
6977 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006978 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01006979 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
6980
6981 if (px->options & PR_O_HTTP_PROXY)
6982 l4->flags |= SN_ADDR_SET;
6983
6984 test->flags = ACL_TEST_F_READ_ONLY;
6985 return 1;
6986}
6987
Willy Tarreauc11416f2007-06-17 16:58:38 +02006988/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
6989 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
6990 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02006991static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02006992acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02006993 struct acl_expr *expr, struct acl_test *test)
6994{
6995 struct http_txn *txn = l7;
6996 struct hdr_idx *idx = &txn->hdr_idx;
6997 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02006998
Willy Tarreaub6866442008-07-14 23:54:42 +02006999 if (!txn)
7000 return 0;
7001
Willy Tarreau33a7e692007-06-10 19:45:56 +02007002 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7003 /* search for header from the beginning */
7004 ctx->idx = 0;
7005
Willy Tarreau33a7e692007-06-10 19:45:56 +02007006 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7007 test->flags |= ACL_TEST_F_FETCH_MORE;
7008 test->flags |= ACL_TEST_F_VOL_HDR;
7009 test->len = ctx->vlen;
7010 test->ptr = (char *)ctx->line + ctx->val;
7011 return 1;
7012 }
7013
7014 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7015 test->flags |= ACL_TEST_F_VOL_HDR;
7016 return 0;
7017}
7018
Willy Tarreau33a7e692007-06-10 19:45:56 +02007019static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007020acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7021 struct acl_expr *expr, struct acl_test *test)
7022{
7023 struct http_txn *txn = l7;
7024
Willy Tarreaub6866442008-07-14 23:54:42 +02007025 if (!txn)
7026 return 0;
7027
Willy Tarreau655dce92009-11-08 13:10:58 +01007028 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007029 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007030
Willy Tarreauc11416f2007-06-17 16:58:38 +02007031 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7032 /* ensure the indexes are not affected */
7033 return 0;
7034
7035 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7036}
7037
7038static int
7039acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7040 struct acl_expr *expr, struct acl_test *test)
7041{
7042 struct http_txn *txn = l7;
7043
Willy Tarreaub6866442008-07-14 23:54:42 +02007044 if (!txn)
7045 return 0;
7046
Willy Tarreau655dce92009-11-08 13:10:58 +01007047 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007048 return 0;
7049
7050 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7051}
7052
7053/* 6. Check on HTTP header count. The number of occurrences is returned.
7054 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7055 */
7056static int
7057acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007058 struct acl_expr *expr, struct acl_test *test)
7059{
7060 struct http_txn *txn = l7;
7061 struct hdr_idx *idx = &txn->hdr_idx;
7062 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007063 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007064
Willy Tarreaub6866442008-07-14 23:54:42 +02007065 if (!txn)
7066 return 0;
7067
Willy Tarreau33a7e692007-06-10 19:45:56 +02007068 ctx.idx = 0;
7069 cnt = 0;
7070 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7071 cnt++;
7072
7073 test->i = cnt;
7074 test->flags = ACL_TEST_F_VOL_HDR;
7075 return 1;
7076}
7077
Willy Tarreauc11416f2007-06-17 16:58:38 +02007078static int
7079acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7080 struct acl_expr *expr, struct acl_test *test)
7081{
7082 struct http_txn *txn = l7;
7083
Willy Tarreaub6866442008-07-14 23:54:42 +02007084 if (!txn)
7085 return 0;
7086
Willy Tarreau655dce92009-11-08 13:10:58 +01007087 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007088 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007089
Willy Tarreauc11416f2007-06-17 16:58:38 +02007090 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7091 /* ensure the indexes are not affected */
7092 return 0;
7093
7094 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7095}
7096
7097static int
7098acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7099 struct acl_expr *expr, struct acl_test *test)
7100{
7101 struct http_txn *txn = l7;
7102
Willy Tarreaub6866442008-07-14 23:54:42 +02007103 if (!txn)
7104 return 0;
7105
Willy Tarreau655dce92009-11-08 13:10:58 +01007106 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007107 return 0;
7108
7109 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7110}
7111
Willy Tarreau33a7e692007-06-10 19:45:56 +02007112/* 7. Check on HTTP header's integer value. The integer value is returned.
7113 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007114 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007115 */
7116static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007117acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007118 struct acl_expr *expr, struct acl_test *test)
7119{
7120 struct http_txn *txn = l7;
7121 struct hdr_idx *idx = &txn->hdr_idx;
7122 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007123
Willy Tarreaub6866442008-07-14 23:54:42 +02007124 if (!txn)
7125 return 0;
7126
Willy Tarreau33a7e692007-06-10 19:45:56 +02007127 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7128 /* search for header from the beginning */
7129 ctx->idx = 0;
7130
Willy Tarreau33a7e692007-06-10 19:45:56 +02007131 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7132 test->flags |= ACL_TEST_F_FETCH_MORE;
7133 test->flags |= ACL_TEST_F_VOL_HDR;
7134 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
7135 return 1;
7136 }
7137
7138 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7139 test->flags |= ACL_TEST_F_VOL_HDR;
7140 return 0;
7141}
7142
Willy Tarreauc11416f2007-06-17 16:58:38 +02007143static int
7144acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7145 struct acl_expr *expr, struct acl_test *test)
7146{
7147 struct http_txn *txn = l7;
7148
Willy Tarreaub6866442008-07-14 23:54:42 +02007149 if (!txn)
7150 return 0;
7151
Willy Tarreau655dce92009-11-08 13:10:58 +01007152 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007153 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007154
Willy Tarreauc11416f2007-06-17 16:58:38 +02007155 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7156 /* ensure the indexes are not affected */
7157 return 0;
7158
7159 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7160}
7161
7162static int
7163acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7164 struct acl_expr *expr, struct acl_test *test)
7165{
7166 struct http_txn *txn = l7;
7167
Willy Tarreaub6866442008-07-14 23:54:42 +02007168 if (!txn)
7169 return 0;
7170
Willy Tarreau655dce92009-11-08 13:10:58 +01007171 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007172 return 0;
7173
7174 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7175}
7176
Willy Tarreau106f9792009-09-19 07:54:16 +02007177/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7178 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7179 */
7180static int
7181acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7182 struct acl_expr *expr, struct acl_test *test)
7183{
7184 struct http_txn *txn = l7;
7185 struct hdr_idx *idx = &txn->hdr_idx;
7186 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7187
7188 if (!txn)
7189 return 0;
7190
7191 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7192 /* search for header from the beginning */
7193 ctx->idx = 0;
7194
7195 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7196 test->flags |= ACL_TEST_F_FETCH_MORE;
7197 test->flags |= ACL_TEST_F_VOL_HDR;
7198 /* Same optimization as url_ip */
7199 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
7200 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
7201 test->ptr = (void *)&l4->srv_addr.sin_addr;
7202 test->i = AF_INET;
7203 return 1;
7204 }
7205
7206 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7207 test->flags |= ACL_TEST_F_VOL_HDR;
7208 return 0;
7209}
7210
7211static int
7212acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7213 struct acl_expr *expr, struct acl_test *test)
7214{
7215 struct http_txn *txn = l7;
7216
7217 if (!txn)
7218 return 0;
7219
Willy Tarreau655dce92009-11-08 13:10:58 +01007220 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007221 return 0;
7222
7223 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7224 /* ensure the indexes are not affected */
7225 return 0;
7226
7227 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
7228}
7229
7230static int
7231acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7232 struct acl_expr *expr, struct acl_test *test)
7233{
7234 struct http_txn *txn = l7;
7235
7236 if (!txn)
7237 return 0;
7238
Willy Tarreau655dce92009-11-08 13:10:58 +01007239 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007240 return 0;
7241
7242 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
7243}
7244
Willy Tarreau737b0c12007-06-10 21:28:46 +02007245/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7246 * the first '/' after the possible hostname, and ends before the possible '?'.
7247 */
7248static int
7249acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
7250 struct acl_expr *expr, struct acl_test *test)
7251{
7252 struct http_txn *txn = l7;
7253 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007254
Willy Tarreaub6866442008-07-14 23:54:42 +02007255 if (!txn)
7256 return 0;
7257
Willy Tarreau655dce92009-11-08 13:10:58 +01007258 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007259 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007260
Willy Tarreauc11416f2007-06-17 16:58:38 +02007261 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7262 /* ensure the indexes are not affected */
7263 return 0;
7264
Willy Tarreau962c3f42010-01-10 00:15:35 +01007265 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007266 ptr = http_get_path(txn);
7267 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007268 return 0;
7269
7270 /* OK, we got the '/' ! */
7271 test->ptr = ptr;
7272
7273 while (ptr < end && *ptr != '?')
7274 ptr++;
7275
7276 test->len = ptr - test->ptr;
7277
7278 /* we do not need to set READ_ONLY because the data is in a buffer */
7279 test->flags = ACL_TEST_F_VOL_1ST;
7280 return 1;
7281}
7282
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007283static int
7284acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
7285 struct acl_expr *expr, struct acl_test *test)
7286{
7287 struct buffer *req = s->req;
7288 struct http_txn *txn = &s->txn;
7289 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007290
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007291 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7292 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7293 */
7294
7295 if (!s || !req)
7296 return 0;
7297
Willy Tarreau655dce92009-11-08 13:10:58 +01007298 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007299 /* Already decoded as OK */
7300 test->flags |= ACL_TEST_F_SET_RES_PASS;
7301 return 1;
7302 }
7303
7304 /* Try to decode HTTP request */
7305 if (likely(req->lr < req->r))
7306 http_msg_analyzer(req, msg, &txn->hdr_idx);
7307
Willy Tarreau655dce92009-11-08 13:10:58 +01007308 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007309 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
7310 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7311 return 1;
7312 }
7313 /* wait for final state */
7314 test->flags |= ACL_TEST_F_MAY_CHANGE;
7315 return 0;
7316 }
7317
7318 /* OK we got a valid HTTP request. We have some minor preparation to
7319 * perform so that further checks can rely on HTTP tests.
7320 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007321 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007322 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7323 s->flags |= SN_REDIRECTABLE;
7324
7325 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7326 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7327 return 1;
7328 }
7329
7330 test->flags |= ACL_TEST_F_SET_RES_PASS;
7331 return 1;
7332}
7333
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007334static int
7335acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
7336 struct acl_expr *expr, struct acl_test *test)
7337{
7338
7339 if (!s)
7340 return 0;
7341
7342 if (!get_http_auth(s))
7343 return 0;
7344
7345 test->ctx.a[0] = expr->arg.ul;
7346 test->ctx.a[1] = s->txn.auth.user;
7347 test->ctx.a[2] = s->txn.auth.pass;
7348
7349 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
7350
7351 return 1;
7352}
Willy Tarreau8797c062007-05-07 00:55:35 +02007353
7354/************************************************************************/
7355/* All supported keywords must be declared here. */
7356/************************************************************************/
7357
7358/* Note: must not be declared <const> as its list will be overwritten */
7359static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007360 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7361
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007362 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
7363 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7364 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7365 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007366
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007367 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7368 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7369 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7370 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7371 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7372 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7373 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7374 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
7375 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007376
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007377 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
7378 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7379 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7380 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7381 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7382 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7383 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7384 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7385 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7386 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007387 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE },
Willy Tarreauc11416f2007-06-17 16:58:38 +02007388
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007389 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE },
7390 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7391 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7392 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7393 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7394 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7395 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7396 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7397 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau106f9792009-09-19 07:54:16 +02007398 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007399
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007400 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE },
7401 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7402 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7403 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7404 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7405 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7406 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007407
Willy Tarreauf3d25982007-05-08 22:45:09 +02007408#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007409 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7410 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7411 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7412 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7413 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7414 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7415 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7416
Willy Tarreau8797c062007-05-07 00:55:35 +02007417 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7418 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7419 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7420 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7421 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7422 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7423 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7424 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007425#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02007426
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007427 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth },
7428 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth },
Willy Tarreau8797c062007-05-07 00:55:35 +02007429 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02007430}};
7431
7432
7433__attribute__((constructor))
7434static void __http_protocol_init(void)
7435{
7436 acl_register_keywords(&acl_kws);
7437}
7438
7439
Willy Tarreau58f10d72006-12-04 02:26:12 +01007440/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007441 * Local variables:
7442 * c-indent-level: 8
7443 * c-basic-offset: 8
7444 * End:
7445 */