blob: 69232f9447297d99c06ba5f15d2fc0343b0d0e45 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 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>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020049#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010051#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020052#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020053#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010055#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010059#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020060#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/task.h>
62
Willy Tarreau522d6c02009-12-06 18:49:18 +010063const char HTTP_100[] =
64 "HTTP/1.1 100 Continue\r\n\r\n";
65
66const struct chunk http_100_chunk = {
67 .str = (char *)&HTTP_100,
68 .len = sizeof(HTTP_100)-1
69};
70
Willy Tarreaua9679ac2010-01-03 17:32:57 +010071/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020072const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010073 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020074 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010075 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020076 "Location: "; /* not terminated since it will be concatenated with the URL */
77
Willy Tarreau0f772532006-12-23 20:51:41 +010078const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010080 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010082 "Location: "; /* not terminated since it will be concatenated with the URL */
83
84/* same as 302 except that the browser MUST retry with the GET method */
85const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010089 "Location: "; /* not terminated since it will be concatenated with the URL */
90
Willy Tarreaubaaee002006-06-26 02:48:02 +020091/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
92const char *HTTP_401_fmt =
93 "HTTP/1.0 401 Unauthorized\r\n"
94 "Cache-Control: no-cache\r\n"
95 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020096 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020097 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
98 "\r\n"
99 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
100
Willy Tarreau844a7e72010-01-31 21:46:18 +0100101const char *HTTP_407_fmt =
102 "HTTP/1.0 407 Unauthorized\r\n"
103 "Cache-Control: no-cache\r\n"
104 "Connection: close\r\n"
105 "Content-Type: text/html\r\n"
106 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
107 "\r\n"
108 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
109
Willy Tarreau0f772532006-12-23 20:51:41 +0100110
111const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200112 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100113 [HTTP_ERR_400] = 400,
114 [HTTP_ERR_403] = 403,
115 [HTTP_ERR_408] = 408,
116 [HTTP_ERR_500] = 500,
117 [HTTP_ERR_502] = 502,
118 [HTTP_ERR_503] = 503,
119 [HTTP_ERR_504] = 504,
120};
121
Willy Tarreau80587432006-12-24 17:47:20 +0100122static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200123 [HTTP_ERR_200] =
124 "HTTP/1.0 200 OK\r\n"
125 "Cache-Control: no-cache\r\n"
126 "Connection: close\r\n"
127 "Content-Type: text/html\r\n"
128 "\r\n"
129 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
130
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100132 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 "Cache-Control: no-cache\r\n"
134 "Connection: close\r\n"
135 "Content-Type: text/html\r\n"
136 "\r\n"
137 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
138
139 [HTTP_ERR_403] =
140 "HTTP/1.0 403 Forbidden\r\n"
141 "Cache-Control: no-cache\r\n"
142 "Connection: close\r\n"
143 "Content-Type: text/html\r\n"
144 "\r\n"
145 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
146
147 [HTTP_ERR_408] =
148 "HTTP/1.0 408 Request Time-out\r\n"
149 "Cache-Control: no-cache\r\n"
150 "Connection: close\r\n"
151 "Content-Type: text/html\r\n"
152 "\r\n"
153 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
154
155 [HTTP_ERR_500] =
156 "HTTP/1.0 500 Server Error\r\n"
157 "Cache-Control: no-cache\r\n"
158 "Connection: close\r\n"
159 "Content-Type: text/html\r\n"
160 "\r\n"
161 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
162
163 [HTTP_ERR_502] =
164 "HTTP/1.0 502 Bad Gateway\r\n"
165 "Cache-Control: no-cache\r\n"
166 "Connection: close\r\n"
167 "Content-Type: text/html\r\n"
168 "\r\n"
169 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
170
171 [HTTP_ERR_503] =
172 "HTTP/1.0 503 Service Unavailable\r\n"
173 "Cache-Control: no-cache\r\n"
174 "Connection: close\r\n"
175 "Content-Type: text/html\r\n"
176 "\r\n"
177 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
178
179 [HTTP_ERR_504] =
180 "HTTP/1.0 504 Gateway Time-out\r\n"
181 "Cache-Control: no-cache\r\n"
182 "Connection: close\r\n"
183 "Content-Type: text/html\r\n"
184 "\r\n"
185 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
186
187};
188
Willy Tarreau80587432006-12-24 17:47:20 +0100189/* We must put the messages here since GCC cannot initialize consts depending
190 * on strlen().
191 */
192struct chunk http_err_chunks[HTTP_ERR_SIZE];
193
Willy Tarreau42250582007-04-01 01:30:43 +0200194#define FD_SETS_ARE_BITFIELDS
195#ifdef FD_SETS_ARE_BITFIELDS
196/*
197 * This map is used with all the FD_* macros to check whether a particular bit
198 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
199 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
200 * byte should be encoded. Be careful to always pass bytes from 0 to 255
201 * exclusively to the macros.
202 */
203fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
204fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
205
206#else
207#error "Check if your OS uses bitfields for fd_sets"
208#endif
209
Willy Tarreau80587432006-12-24 17:47:20 +0100210void init_proto_http()
211{
Willy Tarreau42250582007-04-01 01:30:43 +0200212 int i;
213 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100214 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200215
Willy Tarreau80587432006-12-24 17:47:20 +0100216 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
217 if (!http_err_msgs[msg]) {
218 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
219 abort();
220 }
221
222 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
223 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
224 }
Willy Tarreau42250582007-04-01 01:30:43 +0200225
226 /* initialize the log header encoding map : '{|}"#' should be encoded with
227 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
228 * URL encoding only requires '"', '#' to be encoded as well as non-
229 * printable characters above.
230 */
231 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
232 memset(url_encode_map, 0, sizeof(url_encode_map));
233 for (i = 0; i < 32; i++) {
234 FD_SET(i, hdr_encode_map);
235 FD_SET(i, url_encode_map);
236 }
237 for (i = 127; i < 256; i++) {
238 FD_SET(i, hdr_encode_map);
239 FD_SET(i, url_encode_map);
240 }
241
242 tmp = "\"#{|}";
243 while (*tmp) {
244 FD_SET(*tmp, hdr_encode_map);
245 tmp++;
246 }
247
248 tmp = "\"#";
249 while (*tmp) {
250 FD_SET(*tmp, url_encode_map);
251 tmp++;
252 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200253
254 /* memory allocations */
255 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200256 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100257}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200258
Willy Tarreau53b6c742006-12-17 13:37:46 +0100259/*
260 * We have 26 list of methods (1 per first letter), each of which can have
261 * up to 3 entries (2 valid, 1 null).
262 */
263struct http_method_desc {
264 http_meth_t meth;
265 int len;
266 const char text[8];
267};
268
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100269const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100270 ['C' - 'A'] = {
271 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
272 },
273 ['D' - 'A'] = {
274 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
275 },
276 ['G' - 'A'] = {
277 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
278 },
279 ['H' - 'A'] = {
280 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
281 },
282 ['P' - 'A'] = {
283 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
284 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
285 },
286 ['T' - 'A'] = {
287 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
288 },
289 /* rest is empty like this :
290 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
291 */
292};
293
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100294/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200295 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100296 * RFC2616 for those chars.
297 */
298
299const char http_is_spht[256] = {
300 [' '] = 1, ['\t'] = 1,
301};
302
303const char http_is_crlf[256] = {
304 ['\r'] = 1, ['\n'] = 1,
305};
306
307const char http_is_lws[256] = {
308 [' '] = 1, ['\t'] = 1,
309 ['\r'] = 1, ['\n'] = 1,
310};
311
312const char http_is_sep[256] = {
313 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
314 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
315 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
316 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
317 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
318};
319
320const char http_is_ctl[256] = {
321 [0 ... 31] = 1,
322 [127] = 1,
323};
324
325/*
326 * A token is any ASCII char that is neither a separator nor a CTL char.
327 * Do not overwrite values in assignment since gcc-2.95 will not handle
328 * them correctly. Instead, define every non-CTL char's status.
329 */
330const char http_is_token[256] = {
331 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
332 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
333 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
334 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
335 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
336 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
337 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
338 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
339 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
340 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
341 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
342 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
343 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
344 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
345 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
346 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
347 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
348 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
349 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
350 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
351 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
352 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
353 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
354 ['|'] = 1, ['}'] = 0, ['~'] = 1,
355};
356
357
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100358/*
359 * An http ver_token is any ASCII which can be found in an HTTP version,
360 * which includes 'H', 'T', 'P', '/', '.' and any digit.
361 */
362const char http_is_ver_token[256] = {
363 ['.'] = 1, ['/'] = 1,
364 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
365 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
366 ['H'] = 1, ['P'] = 1, ['T'] = 1,
367};
368
369
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100370/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100371 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
372 */
373#if defined(DEBUG_FSM)
374static void http_silent_debug(int line, struct session *s)
375{
376 int size = 0;
377 size += snprintf(trash + size, sizeof(trash) - size,
378 "[%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",
379 line,
380 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
381 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);
382 write(-1, trash, size);
383 size = 0;
384 size += snprintf(trash + size, sizeof(trash) - size,
385 " %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",
386 line,
387 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
388 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);
389
390 write(-1, trash, size);
391}
392#else
393#define http_silent_debug(l,s) do { } while (0)
394#endif
395
396/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100397 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
398 * CRLF. Text length is measured first, so it cannot be NULL.
399 * The header is also automatically added to the index <hdr_idx>, and the end
400 * of headers is automatically adjusted. The number of bytes added is returned
401 * on success, otherwise <0 is returned indicating an error.
402 */
403int http_header_add_tail(struct buffer *b, struct http_msg *msg,
404 struct hdr_idx *hdr_idx, const char *text)
405{
406 int bytes, len;
407
408 len = strlen(text);
409 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
410 if (!bytes)
411 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100412 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100413 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
414}
415
416/*
417 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
418 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
419 * the buffer is only opened and the space reserved, but nothing is copied.
420 * The header is also automatically added to the index <hdr_idx>, and the end
421 * of headers is automatically adjusted. The number of bytes added is returned
422 * on success, otherwise <0 is returned indicating an error.
423 */
424int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
425 struct hdr_idx *hdr_idx, const char *text, int len)
426{
427 int bytes;
428
429 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
430 if (!bytes)
431 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100432 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100433 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
434}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200435
436/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100437 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
438 * If so, returns the position of the first non-space character relative to
439 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
440 * to return a pointer to the place after the first space. Returns 0 if the
441 * header name does not match. Checks are case-insensitive.
442 */
443int http_header_match2(const char *hdr, const char *end,
444 const char *name, int len)
445{
446 const char *val;
447
448 if (hdr + len >= end)
449 return 0;
450 if (hdr[len] != ':')
451 return 0;
452 if (strncasecmp(hdr, name, len) != 0)
453 return 0;
454 val = hdr + len + 1;
455 while (val < end && HTTP_IS_SPHT(*val))
456 val++;
457 if ((val >= end) && (len + 2 <= end - hdr))
458 return len + 2; /* we may replace starting from second space */
459 return val - hdr;
460}
461
Willy Tarreau68085d82010-01-18 14:54:04 +0100462/* Find the end of the header value contained between <s> and <e>. See RFC2616,
463 * par 2.2 for more information. Note that it requires a valid header to return
464 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200465 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100466char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200467{
468 int quoted, qdpair;
469
470 quoted = qdpair = 0;
471 for (; s < e; s++) {
472 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200473 else if (quoted) {
474 if (*s == '\\') qdpair = 1;
475 else if (*s == '"') quoted = 0;
476 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200477 else if (*s == '"') quoted = 1;
478 else if (*s == ',') return s;
479 }
480 return s;
481}
482
483/* Find the first or next occurrence of header <name> in message buffer <sol>
484 * using headers index <idx>, and return it in the <ctx> structure. This
485 * structure holds everything necessary to use the header and find next
486 * occurrence. If its <idx> member is 0, the header is searched from the
487 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100488 * 1 when it finds a value, and 0 when there is no more. It is designed to work
489 * with headers defined as comma-separated lists. As a special case, if ctx->val
490 * is NULL when searching for a new values of a header, the current header is
491 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200492 */
493int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100494 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200495 struct hdr_ctx *ctx)
496{
Willy Tarreau68085d82010-01-18 14:54:04 +0100497 char *eol, *sov;
498 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200499
Willy Tarreau68085d82010-01-18 14:54:04 +0100500 cur_idx = ctx->idx;
501 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200502 /* We have previously returned a value, let's search
503 * another one on the same line.
504 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200505 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100506 ctx->del = ctx->val + ctx->vlen;
507 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 eol = sol + idx->v[cur_idx].len;
509
510 if (sov >= eol)
511 /* no more values in this header */
512 goto next_hdr;
513
Willy Tarreau68085d82010-01-18 14:54:04 +0100514 /* values remaining for this header, skip the comma but save it
515 * for later use (eg: for header deletion).
516 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200517 sov++;
518 while (sov < eol && http_is_lws[(unsigned char)*sov])
519 sov++;
520
521 goto return_hdr;
522 }
523
524 /* first request for this header */
525 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100526 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200527 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200528 while (cur_idx) {
529 eol = sol + idx->v[cur_idx].len;
530
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200531 if (len == 0) {
532 /* No argument was passed, we want any header.
533 * To achieve this, we simply build a fake request. */
534 while (sol + len < eol && sol[len] != ':')
535 len++;
536 name = sol;
537 }
538
Willy Tarreau33a7e692007-06-10 19:45:56 +0200539 if ((len < eol - sol) &&
540 (sol[len] == ':') &&
541 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100542 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200543 sov = sol + len + 1;
544 while (sov < eol && http_is_lws[(unsigned char)*sov])
545 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100546
Willy Tarreau33a7e692007-06-10 19:45:56 +0200547 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100548 ctx->prev = old_idx;
549 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200550 ctx->idx = cur_idx;
551 ctx->val = sov - sol;
552
553 eol = find_hdr_value_end(sov, eol);
554 ctx->vlen = eol - sov;
555 return 1;
556 }
557 next_hdr:
558 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100559 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200560 cur_idx = idx->v[cur_idx].next;
561 }
562 return 0;
563}
564
565int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100566 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200567 struct hdr_ctx *ctx)
568{
569 return http_find_header2(name, strlen(name), sol, idx, ctx);
570}
571
Willy Tarreau68085d82010-01-18 14:54:04 +0100572/* Remove one value of a header. This only works on a <ctx> returned by one of
573 * the http_find_header functions. The value is removed, as well as surrounding
574 * commas if any. If the removed value was alone, the whole header is removed.
575 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
576 * message <msg>. The new index is returned. If it is zero, it means there is
577 * no more header, so any processing may stop. The ctx is always left in a form
578 * that can be handled by http_find_header2() to find next occurrence.
579 */
580int http_remove_header2(struct http_msg *msg, struct buffer *buf,
581 struct hdr_idx *idx, struct hdr_ctx *ctx)
582{
583 int cur_idx = ctx->idx;
584 char *sol = ctx->line;
585 struct hdr_idx_elem *hdr;
586 int delta, skip_comma;
587
588 if (!cur_idx)
589 return 0;
590
591 hdr = &idx->v[cur_idx];
592 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) {
593 /* This was the only value of the header, we must now remove it entirely. */
594 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
595 http_msg_move_end(msg, delta);
596 idx->used--;
597 hdr->len = 0; /* unused entry */
598 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100599 if (idx->tail == ctx->idx)
600 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100601 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;
Willy Tarreau827aee92011-03-10 16:55:02 +0100743 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100744 char *path;
745 int len;
746
747 /* 1: create the response header */
748 rdr.len = strlen(HTTP_302);
749 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100750 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100751 memcpy(rdr.str, HTTP_302, rdr.len);
752
Willy Tarreau827aee92011-03-10 16:55:02 +0100753 srv = target_srv(&s->target);
754
Willy Tarreauefb453c2008-10-26 20:49:47 +0100755 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100756 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100757 return;
758
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100759 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100760 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
761 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
762 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100763 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764
765 /* 3: add the request URI */
766 txn = &s->txn;
767 path = http_get_path(txn);
768 if (!path)
769 return;
770
Willy Tarreau962c3f42010-01-10 00:15:35 +0100771 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200772 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 return;
774
775 memcpy(rdr.str + rdr.len, path, len);
776 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100777
778 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
779 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
780 rdr.len += 29;
781 } else {
782 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
783 rdr.len += 23;
784 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100785
786 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100787 si->shutr(si);
788 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 si->err_type = SI_ET_NONE;
790 si->err_loc = NULL;
791 si->state = SI_ST_CLO;
792
793 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100794 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100795
796 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100797 if (srv)
798 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100799}
800
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100801/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100802 * that the server side is closed. Note that err_type is actually a
803 * bitmask, where almost only aborts may be cumulated with other
804 * values. We consider that aborted operations are more important
805 * than timeouts or errors due to the fact that nobody else in the
806 * logs might explain incomplete retries. All others should avoid
807 * being cumulated. It should normally not be possible to have multiple
808 * aborts at once, but just in case, the first one in sequence is reported.
809 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100810void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100811{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100812 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813
814 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100815 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
816 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100818 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
819 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100820 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100821 http_server_error(s, si, SN_ERR_SRVTO, 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_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100824 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
825 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100826 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100827 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
828 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100829 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100830 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
831 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100832 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100833 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
834 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100835}
836
Willy Tarreau42250582007-04-01 01:30:43 +0200837extern const char sess_term_cond[8];
838extern const char sess_fin_state[8];
839extern const char *monthname[12];
Willy Tarreauf1348312010-10-07 15:54:11 +0200840const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */
841const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
842 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
843 Set-cookie Updated, unknown, unknown */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200844struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200845struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100846
Emeric Brun3a058f32009-06-30 18:26:00 +0200847void http_sess_clflog(struct session *s)
848{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100849 char pn[INET6_ADDRSTRLEN];
Emeric Brun3a058f32009-06-30 18:26:00 +0200850 struct proxy *fe = s->fe;
851 struct proxy *be = s->be;
852 struct proxy *prx_log;
853 struct http_txn *txn = &s->txn;
854 int tolog, level, err;
855 char *uri, *h;
Willy Tarreau71904a42011-02-13 14:30:26 +0100856 const char *svid;
Emeric Brun3a058f32009-06-30 18:26:00 +0200857 struct tm tm;
858 static char tmpline[MAX_SYSLOG_LEN];
859 int hdr;
860 size_t w;
861 int t_request;
862
863 prx_log = fe;
864 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +0200865 (s->req->cons->conn_retries != be->conn_retries) ||
Emeric Brun3a058f32009-06-30 18:26:00 +0200866 txn->status >= 500;
867
Willy Tarreau957c0a52011-03-03 17:42:23 +0100868 if (s->req->prod->addr.c.from.ss_family == AF_INET)
Emeric Brun3a058f32009-06-30 18:26:00 +0200869 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100870 (const void *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr,
Emeric Brun3a058f32009-06-30 18:26:00 +0200871 pn, sizeof(pn));
Willy Tarreau957c0a52011-03-03 17:42:23 +0100872 else if (s->req->prod->addr.c.from.ss_family == AF_INET6)
Emeric Brun3a058f32009-06-30 18:26:00 +0200873 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100874 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr,
Emeric Brun3a058f32009-06-30 18:26:00 +0200875 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +0200876 else
877 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
Emeric Brun3a058f32009-06-30 18:26:00 +0200878
879 get_gmtime(s->logs.accept_date.tv_sec, &tm);
880
881 /* FIXME: let's limit ourselves to frontend logging for now. */
882 tolog = fe->to_log;
883
884 h = tmpline;
885
886 w = snprintf(h, sizeof(tmpline),
887 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
888 pn,
889 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
890 tm.tm_hour, tm.tm_min, tm.tm_sec);
891 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
892 goto trunc;
893 h += w;
894
895 if (h >= tmpline + sizeof(tmpline) - 4)
896 goto trunc;
897
898 *(h++) = ' ';
899 *(h++) = '\"';
900 uri = txn->uri ? txn->uri : "<BADREQ>";
901 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
902 '#', url_encode_map, uri);
903 *(h++) = '\"';
904
905 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
906 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
907 goto trunc;
908 h += w;
909
910 if (h >= tmpline + sizeof(tmpline) - 9)
911 goto trunc;
912 memcpy(h, " \"-\" \"-\"", 8);
913 h += 8;
914
915 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
916 " %d %03d",
Willy Tarreau957c0a52011-03-03 17:42:23 +0100917 s->req->prod->addr.c.from.ss_family == AF_UNIX ? s->listener->luid :
918 ntohs((s->req->prod->addr.c.from.ss_family == AF_INET) ?
919 ((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_port :
920 ((struct sockaddr_in6 *)&s->req->prod->addr.c.from)->sin6_port),
Emeric Brun3a058f32009-06-30 18:26:00 +0200921 (int)s->logs.accept_date.tv_usec/1000);
922 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
923 goto trunc;
924 h += w;
925
926 w = strlen(fe->id);
927 if (h >= tmpline + sizeof(tmpline) - 4 - w)
928 goto trunc;
929 *(h++) = ' ';
930 *(h++) = '\"';
931 memcpy(h, fe->id, w);
932 h += w;
933 *(h++) = '\"';
934
935 w = strlen(be->id);
936 if (h >= tmpline + sizeof(tmpline) - 4 - w)
937 goto trunc;
938 *(h++) = ' ';
939 *(h++) = '\"';
940 memcpy(h, be->id, w);
941 h += w;
942 *(h++) = '\"';
943
Willy Tarreau71904a42011-02-13 14:30:26 +0100944 if (!(tolog & LW_SVID))
945 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200946 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100947 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200948 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100949 break;
950 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200951 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100952 break;
953 default:
954 svid = "<NOSRV>";
955 break;
956 }
Emeric Brun3a058f32009-06-30 18:26:00 +0200957
958 w = strlen(svid);
959 if (h >= tmpline + sizeof(tmpline) - 4 - w)
960 goto trunc;
961 *(h++) = ' ';
962 *(h++) = '\"';
963 memcpy(h, svid, w);
964 h += w;
965 *(h++) = '\"';
966
967 t_request = -1;
968 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
969 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
970 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
971 " %d %ld %ld %ld %ld",
972 t_request,
973 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
974 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
975 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
976 s->logs.t_close);
977 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
978 goto trunc;
979 h += w;
980
981 if (h >= tmpline + sizeof(tmpline) - 8)
982 goto trunc;
983 *(h++) = ' ';
984 *(h++) = '\"';
985 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
986 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
987 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
988 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
989 *(h++) = '\"';
990
991 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
992 " %d %d %d %d %d %ld %ld",
Willy Tarreau827aee92011-03-10 16:55:02 +0100993 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Willy Tarreauee28de02010-06-01 09:51:00 +0200994 (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries,
Emeric Brun3a058f32009-06-30 18:26:00 +0200995 s->logs.srv_queue_size, s->logs.prx_queue_size);
996
997 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
998 goto trunc;
999 h += w;
1000
1001 if (txn->cli_cookie) {
1002 w = strlen(txn->cli_cookie);
1003 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1004 goto trunc;
1005 *(h++) = ' ';
1006 *(h++) = '\"';
1007 memcpy(h, txn->cli_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 (txn->srv_cookie) {
1018 w = strlen(txn->srv_cookie);
1019 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1020 goto trunc;
1021 *(h++) = ' ';
1022 *(h++) = '\"';
1023 memcpy(h, txn->srv_cookie, w);
1024 h += w;
1025 *(h++) = '\"';
1026 } else {
1027 if (h >= tmpline + sizeof(tmpline) - 5)
1028 goto trunc;
1029 memcpy(h, " \"-\"", 4);
1030 h += 4;
1031 }
1032
1033 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1034 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1035 if (h >= sizeof (tmpline) + tmpline - 4)
1036 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001037 if (txn->req.cap[hdr] != NULL) {
1038 *(h++) = ' ';
1039 *(h++) = '\"';
1040 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1041 '#', hdr_encode_map, txn->req.cap[hdr]);
1042 *(h++) = '\"';
1043 } else {
1044 memcpy(h, " \"-\"", 4);
1045 h += 4;
1046 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001047 }
1048 }
1049
1050 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1051 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1052 if (h >= sizeof (tmpline) + tmpline - 4)
1053 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001054 if (txn->rsp.cap[hdr] != NULL) {
1055 *(h++) = ' ';
1056 *(h++) = '\"';
1057 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1058 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1059 *(h++) = '\"';
1060 } else {
1061 memcpy(h, " \"-\"", 4);
1062 h += 4;
1063 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001064 }
1065 }
1066
1067trunc:
1068 *h = '\0';
1069
1070 level = LOG_INFO;
1071 if (err && (fe->options2 & PR_O2_LOGERRORS))
1072 level = LOG_ERR;
1073
1074 send_log(prx_log, level, "%s\n", tmpline);
1075
1076 s->logs.logwait = 0;
1077}
1078
Willy Tarreau42250582007-04-01 01:30:43 +02001079/*
1080 * send a log for the session when we have enough info about it.
1081 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001082 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001083void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001084{
Cyril Bontéacd7d632010-11-01 19:26:02 +01001085 char pn[INET6_ADDRSTRLEN];
Willy Tarreau42250582007-04-01 01:30:43 +02001086 struct proxy *fe = s->fe;
1087 struct proxy *be = s->be;
1088 struct proxy *prx_log;
1089 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001090 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001091 char *uri, *h;
Willy Tarreau71904a42011-02-13 14:30:26 +01001092 const char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001093 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001094 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001095 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001096 int hdr;
1097
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001098 /* if we don't want to log normal traffic, return now */
1099 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +02001100 (s->req->cons->conn_retries != be->conn_retries) ||
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001101 txn->status >= 500;
1102 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1103 return;
1104
Willy Tarreau42250582007-04-01 01:30:43 +02001105 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1106 return;
1107 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001108
Emeric Brun3a058f32009-06-30 18:26:00 +02001109 if (prx_log->options2 & PR_O2_CLFLOG)
1110 return http_sess_clflog(s);
1111
Willy Tarreau957c0a52011-03-03 17:42:23 +01001112 if (s->req->prod->addr.c.from.ss_family == AF_INET)
Willy Tarreau42250582007-04-01 01:30:43 +02001113 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01001114 (const void *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr,
Willy Tarreau42250582007-04-01 01:30:43 +02001115 pn, sizeof(pn));
Willy Tarreau957c0a52011-03-03 17:42:23 +01001116 else if (s->req->prod->addr.c.from.ss_family == AF_INET6)
Willy Tarreau42250582007-04-01 01:30:43 +02001117 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01001118 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr,
Willy Tarreau42250582007-04-01 01:30:43 +02001119 pn, sizeof(pn));
1120
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001121 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001122
1123 /* FIXME: let's limit ourselves to frontend logging for now. */
1124 tolog = fe->to_log;
1125
1126 h = tmpline;
1127 if (fe->to_log & LW_REQHDR &&
1128 txn->req.cap &&
1129 (h < tmpline + sizeof(tmpline) - 10)) {
1130 *(h++) = ' ';
1131 *(h++) = '{';
1132 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1133 if (hdr)
1134 *(h++) = '|';
1135 if (txn->req.cap[hdr] != NULL)
1136 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1137 '#', hdr_encode_map, txn->req.cap[hdr]);
1138 }
1139 *(h++) = '}';
1140 }
1141
1142 if (fe->to_log & LW_RSPHDR &&
1143 txn->rsp.cap &&
1144 (h < tmpline + sizeof(tmpline) - 7)) {
1145 *(h++) = ' ';
1146 *(h++) = '{';
1147 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1148 if (hdr)
1149 *(h++) = '|';
1150 if (txn->rsp.cap[hdr] != NULL)
1151 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1152 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1153 }
1154 *(h++) = '}';
1155 }
1156
1157 if (h < tmpline + sizeof(tmpline) - 4) {
1158 *(h++) = ' ';
1159 *(h++) = '"';
1160 uri = txn->uri ? txn->uri : "<BADREQ>";
1161 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1162 '#', url_encode_map, uri);
1163 *(h++) = '"';
1164 }
1165 *h = '\0';
1166
Willy Tarreau71904a42011-02-13 14:30:26 +01001167 if (!(tolog & LW_SVID))
1168 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001169 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +01001170 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001171 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +01001172 break;
1173 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001174 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +01001175 break;
1176 default:
1177 svid = "<NOSRV>";
1178 break;
1179 }
Willy Tarreau42250582007-04-01 01:30:43 +02001180
Willy Tarreau70089872008-06-13 21:12:51 +02001181 t_request = -1;
1182 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1183 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1184
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001185 level = LOG_INFO;
1186 if (err && (fe->options2 & PR_O2_LOGERRORS))
1187 level = LOG_ERR;
1188
1189 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001190 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001191 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1192 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau957c0a52011-03-03 17:42:23 +01001193 (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? "unix" : pn,
1194 (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? s->listener->luid :
1195 ntohs((s->req->prod->addr.c.from.ss_family == AF_INET) ?
1196 ((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_port :
1197 ((struct sockaddr_in6 *)&s->req->prod->addr.c.from)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001198 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001199 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001200 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001201 t_request,
1202 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001203 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1204 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1205 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1206 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001207 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001208 txn->cli_cookie ? txn->cli_cookie : "-",
1209 txn->srv_cookie ? txn->srv_cookie : "-",
1210 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1211 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1212 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1213 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
Willy Tarreau827aee92011-03-10 16:55:02 +01001214 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001215 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +02001216 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001217 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1218
1219 s->logs.logwait = 0;
1220}
1221
Willy Tarreau117f59e2007-03-04 18:17:17 +01001222
1223/*
1224 * Capture headers from message starting at <som> according to header list
1225 * <cap_hdr>, and fill the <idx> structure appropriately.
1226 */
1227void capture_headers(char *som, struct hdr_idx *idx,
1228 char **cap, struct cap_hdr *cap_hdr)
1229{
1230 char *eol, *sol, *col, *sov;
1231 int cur_idx;
1232 struct cap_hdr *h;
1233 int len;
1234
1235 sol = som + hdr_idx_first_pos(idx);
1236 cur_idx = hdr_idx_first_idx(idx);
1237
1238 while (cur_idx) {
1239 eol = sol + idx->v[cur_idx].len;
1240
1241 col = sol;
1242 while (col < eol && *col != ':')
1243 col++;
1244
1245 sov = col + 1;
1246 while (sov < eol && http_is_lws[(unsigned char)*sov])
1247 sov++;
1248
1249 for (h = cap_hdr; h; h = h->next) {
1250 if ((h->namelen == col - sol) &&
1251 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1252 if (cap[h->index] == NULL)
1253 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001254 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001255
1256 if (cap[h->index] == NULL) {
1257 Alert("HTTP capture : out of memory.\n");
1258 continue;
1259 }
1260
1261 len = eol - sov;
1262 if (len > h->len)
1263 len = h->len;
1264
1265 memcpy(cap[h->index], sov, len);
1266 cap[h->index][len]=0;
1267 }
1268 }
1269 sol = eol + idx->v[cur_idx].cr + 1;
1270 cur_idx = idx->v[cur_idx].next;
1271 }
1272}
1273
1274
Willy Tarreau42250582007-04-01 01:30:43 +02001275/* either we find an LF at <ptr> or we jump to <bad>.
1276 */
1277#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1278
1279/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1280 * otherwise to <http_msg_ood> with <state> set to <st>.
1281 */
1282#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1283 ptr++; \
1284 if (likely(ptr < end)) \
1285 goto good; \
1286 else { \
1287 state = (st); \
1288 goto http_msg_ood; \
1289 } \
1290 } while (0)
1291
1292
Willy Tarreaubaaee002006-06-26 02:48:02 +02001293/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001294 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1296 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1297 * will give undefined results.
1298 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1299 * and that msg->sol points to the beginning of the response.
1300 * If a complete line is found (which implies that at least one CR or LF is
1301 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1302 * returned indicating an incomplete line (which does not mean that parts have
1303 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1304 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1305 * upon next call.
1306 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001307 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1309 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001310 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001312const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1313 unsigned int state, const char *ptr, const char *end,
1314 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001315{
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001317 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001318 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001319 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001320 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1321
1322 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001323 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001324 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1325 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001326 state = HTTP_MSG_ERROR;
1327 break;
1328
Willy Tarreau8973c702007-01-21 23:58:29 +01001329 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001330 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001332 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 goto http_msg_rpcode;
1334 }
1335 if (likely(HTTP_IS_SPHT(*ptr)))
1336 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1337 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001338 state = HTTP_MSG_ERROR;
1339 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001340
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001342 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 if (likely(!HTTP_IS_LWS(*ptr)))
1344 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1345
1346 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001347 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1349 }
1350
1351 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001352 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 http_msg_rsp_reason:
1354 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001355 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001356 msg->sl.st.r_l = 0;
1357 goto http_msg_rpline_eol;
1358
Willy Tarreau8973c702007-01-21 23:58:29 +01001359 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001360 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001361 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001362 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001363 goto http_msg_rpreason;
1364 }
1365 if (likely(HTTP_IS_SPHT(*ptr)))
1366 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1367 /* so it's a CR/LF, so there is no reason phrase */
1368 goto http_msg_rsp_reason;
1369
Willy Tarreau8973c702007-01-21 23:58:29 +01001370 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001371 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001372 if (likely(!HTTP_IS_CRLF(*ptr)))
1373 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001374 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001375 http_msg_rpline_eol:
1376 /* We have seen the end of line. Note that we do not
1377 * necessarily have the \n yet, but at least we know that we
1378 * have EITHER \r OR \n, otherwise the response would not be
1379 * complete. We can then record the response length and return
1380 * to the caller which will be able to register it.
1381 */
1382 msg->sl.st.l = ptr - msg->sol;
1383 return ptr;
1384
1385#ifdef DEBUG_FULL
1386 default:
1387 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1388 exit(1);
1389#endif
1390 }
1391
1392 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001393 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001394 if (ret_state)
1395 *ret_state = state;
1396 if (ret_ptr)
1397 *ret_ptr = (char *)ptr;
1398 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001399}
1400
Willy Tarreau8973c702007-01-21 23:58:29 +01001401/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 * This function parses a request line between <ptr> and <end>, starting with
1403 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1404 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1405 * will give undefined results.
1406 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1407 * and that msg->sol points to the beginning of the request.
1408 * If a complete line is found (which implies that at least one CR or LF is
1409 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1410 * returned indicating an incomplete line (which does not mean that parts have
1411 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1412 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1413 * upon next call.
1414 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001415 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1417 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001418 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001419 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001420const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1421 unsigned int state, const char *ptr, const char *end,
1422 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001423{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001426 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 if (likely(HTTP_IS_TOKEN(*ptr)))
1428 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001431 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1433 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001434
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 if (likely(HTTP_IS_CRLF(*ptr))) {
1436 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001437 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001439 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001441 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001443 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 msg->sl.rq.v_l = 0;
1445 goto http_msg_rqline_eol;
1446 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001447 state = HTTP_MSG_ERROR;
1448 break;
1449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001451 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001453 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 goto http_msg_rquri;
1455 }
1456 if (likely(HTTP_IS_SPHT(*ptr)))
1457 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1458 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1459 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001462 http_msg_rquri:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 if (likely(!HTTP_IS_LWS(*ptr)))
1464 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001467 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1469 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1472 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001473
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001475 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001477 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 goto http_msg_rqver;
1479 }
1480 if (likely(HTTP_IS_SPHT(*ptr)))
1481 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1482 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1483 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001484
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001486 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001487 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001489
1490 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001491 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001492 http_msg_rqline_eol:
1493 /* We have seen the end of line. Note that we do not
1494 * necessarily have the \n yet, but at least we know that we
1495 * have EITHER \r OR \n, otherwise the request would not be
1496 * complete. We can then record the request length and return
1497 * to the caller which will be able to register it.
1498 */
1499 msg->sl.rq.l = ptr - msg->sol;
1500 return ptr;
1501 }
1502
1503 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001504 state = HTTP_MSG_ERROR;
1505 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001506
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507#ifdef DEBUG_FULL
1508 default:
1509 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1510 exit(1);
1511#endif
1512 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001513
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001515 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 if (ret_state)
1517 *ret_state = state;
1518 if (ret_ptr)
1519 *ret_ptr = (char *)ptr;
1520 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001522
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001523/*
1524 * Returns the data from Authorization header. Function may be called more
1525 * than once so data is stored in txn->auth_data. When no header is found
1526 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1527 * searching again for something we are unable to find anyway.
1528 */
1529
1530char get_http_auth_buff[BUFSIZE];
1531
1532int
1533get_http_auth(struct session *s)
1534{
1535
1536 struct http_txn *txn = &s->txn;
1537 struct chunk auth_method;
1538 struct hdr_ctx ctx;
1539 char *h, *p;
1540 int len;
1541
1542#ifdef DEBUG_AUTH
1543 printf("Auth for session %p: %d\n", s, txn->auth.method);
1544#endif
1545
1546 if (txn->auth.method == HTTP_AUTH_WRONG)
1547 return 0;
1548
1549 if (txn->auth.method)
1550 return 1;
1551
1552 txn->auth.method = HTTP_AUTH_WRONG;
1553
1554 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001555
1556 if (txn->flags & TX_USE_PX_CONN) {
1557 h = "Proxy-Authorization";
1558 len = strlen(h);
1559 } else {
1560 h = "Authorization";
1561 len = strlen(h);
1562 }
1563
1564 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001565 return 0;
1566
1567 h = ctx.line + ctx.val;
1568
1569 p = memchr(h, ' ', ctx.vlen);
1570 if (!p || p == h)
1571 return 0;
1572
1573 chunk_initlen(&auth_method, h, 0, p-h);
1574 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1575
1576 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1577
1578 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1579 get_http_auth_buff, BUFSIZE - 1);
1580
1581 if (len < 0)
1582 return 0;
1583
1584
1585 get_http_auth_buff[len] = '\0';
1586
1587 p = strchr(get_http_auth_buff, ':');
1588
1589 if (!p)
1590 return 0;
1591
1592 txn->auth.user = get_http_auth_buff;
1593 *p = '\0';
1594 txn->auth.pass = p+1;
1595
1596 txn->auth.method = HTTP_AUTH_BASIC;
1597 return 1;
1598 }
1599
1600 return 0;
1601}
1602
Willy Tarreau58f10d72006-12-04 02:26:12 +01001603
Willy Tarreau8973c702007-01-21 23:58:29 +01001604/*
1605 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001606 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001607 * when data are missing and recalled at the exact same location with no
1608 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001609 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001610 * fields. Note that msg->som and msg->sol will be initialized after completing
1611 * the first state, so that none of the msg pointers has to be initialized
1612 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001613 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1615{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001616 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001617 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001618
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001619 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 ptr = buf->lr;
1621 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 if (unlikely(ptr >= end))
1624 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001625
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001627 /*
1628 * First, states that are specific to the response only.
1629 * We check them first so that request and headers are
1630 * closer to each other (accessed more often).
1631 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001632 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001633 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001634 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001635 /* we have a start of message, but we have to check
1636 * first if we need to remove some CRLF. We can only
1637 * do this when send_max=0.
1638 */
1639 char *beg = buf->w + buf->send_max;
1640 if (beg >= buf->data + buf->size)
1641 beg -= buf->size;
1642 if (unlikely(ptr != beg)) {
1643 if (buf->send_max)
1644 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001645 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001646 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001647 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001648 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001649 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001650 hdr_idx_init(idx);
1651 state = HTTP_MSG_RPVER;
1652 goto http_msg_rpver;
1653 }
1654
1655 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1656 goto http_msg_invalid;
1657
1658 if (unlikely(*ptr == '\n'))
1659 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1660 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1661 /* stop here */
1662
Willy Tarreau8973c702007-01-21 23:58:29 +01001663 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001664 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001665 EXPECT_LF_HERE(ptr, http_msg_invalid);
1666 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1667 /* stop here */
1668
Willy Tarreau8973c702007-01-21 23:58:29 +01001669 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001670 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001671 case HTTP_MSG_RPVER_SP:
1672 case HTTP_MSG_RPCODE:
1673 case HTTP_MSG_RPCODE_SP:
1674 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001675 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001676 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001677 if (unlikely(!ptr))
1678 return;
1679
1680 /* we have a full response and we know that we have either a CR
1681 * or an LF at <ptr>.
1682 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001683 //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 +01001684 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1685
1686 msg->sol = ptr;
1687 if (likely(*ptr == '\r'))
1688 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1689 goto http_msg_rpline_end;
1690
Willy Tarreau8973c702007-01-21 23:58:29 +01001691 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001692 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001693 /* msg->sol must point to the first of CR or LF. */
1694 EXPECT_LF_HERE(ptr, http_msg_invalid);
1695 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1696 /* stop here */
1697
1698 /*
1699 * Second, states that are specific to the request only
1700 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001701 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001702 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001704 /* we have a start of message, but we have to check
1705 * first if we need to remove some CRLF. We can only
1706 * do this when send_max=0.
1707 */
1708 char *beg = buf->w + buf->send_max;
1709 if (beg >= buf->data + buf->size)
1710 beg -= buf->size;
1711 if (likely(ptr != beg)) {
1712 if (buf->send_max)
1713 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001714 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001715 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001717 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001718 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001719 /* we will need this when keep-alive will be supported
1720 hdr_idx_init(idx);
1721 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001722 state = HTTP_MSG_RQMETH;
1723 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001725
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001726 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1727 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001728
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 if (unlikely(*ptr == '\n'))
1730 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1731 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001732 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001735 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001736 EXPECT_LF_HERE(ptr, http_msg_invalid);
1737 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001738 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001741 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 case HTTP_MSG_RQMETH_SP:
1743 case HTTP_MSG_RQURI:
1744 case HTTP_MSG_RQURI_SP:
1745 case HTTP_MSG_RQVER:
1746 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001747 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 if (unlikely(!ptr))
1749 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 /* we have a full request and we know that we have either a CR
1752 * or an LF at <ptr>.
1753 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001754 //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 +01001755 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001756
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 msg->sol = ptr;
1758 if (likely(*ptr == '\r'))
1759 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001761
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001763 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 /* check for HTTP/0.9 request : no version information available.
1765 * msg->sol must point to the first of CR or LF.
1766 */
1767 if (unlikely(msg->sl.rq.v_l == 0))
1768 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001769
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001770 EXPECT_LF_HERE(ptr, http_msg_invalid);
1771 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001772 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001773
Willy Tarreau8973c702007-01-21 23:58:29 +01001774 /*
1775 * Common states below
1776 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001778 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001779 msg->sol = ptr;
1780 if (likely(!HTTP_IS_CRLF(*ptr))) {
1781 goto http_msg_hdr_name;
1782 }
1783
1784 if (likely(*ptr == '\r'))
1785 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1786 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001789 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 /* assumes msg->sol points to the first char */
1791 if (likely(HTTP_IS_TOKEN(*ptr)))
1792 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001793
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 if (likely(*ptr == ':')) {
1795 msg->col = ptr - buf->data;
1796 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1797 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001798
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001799 if (likely(msg->err_pos < -1) || *ptr == '\n')
1800 goto http_msg_invalid;
1801
1802 if (msg->err_pos == -1) /* capture error pointer */
1803 msg->err_pos = ptr - buf->data; /* >= 0 now */
1804
1805 /* and we still accept this non-token character */
1806 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001807
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001808 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001809 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001810 /* assumes msg->sol points to the first char and msg->col to the colon */
1811 if (likely(HTTP_IS_SPHT(*ptr)))
1812 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001813
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 /* header value can be basically anything except CR/LF */
1815 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001816
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 if (likely(!HTTP_IS_CRLF(*ptr))) {
1818 goto http_msg_hdr_val;
1819 }
1820
1821 if (likely(*ptr == '\r'))
1822 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1823 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001824
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001825 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001826 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001827 EXPECT_LF_HERE(ptr, http_msg_invalid);
1828 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001829
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001831 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001832 if (likely(HTTP_IS_SPHT(*ptr))) {
1833 /* replace HT,CR,LF with spaces */
1834 for (; buf->data+msg->sov < ptr; msg->sov++)
1835 buf->data[msg->sov] = ' ';
1836 goto http_msg_hdr_l1_sp;
1837 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001838 /* we had a header consisting only in spaces ! */
1839 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 goto http_msg_complete_header;
1841
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001842 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001843 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001844 /* assumes msg->sol points to the first char, msg->col to the
1845 * colon, and msg->sov points to the first character of the
1846 * value.
1847 */
1848 if (likely(!HTTP_IS_CRLF(*ptr)))
1849 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001850
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001851 msg->eol = ptr;
1852 /* Note: we could also copy eol into ->eoh so that we have the
1853 * real header end in case it ends with lots of LWS, but is this
1854 * really needed ?
1855 */
1856 if (likely(*ptr == '\r'))
1857 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1858 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001859
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001860 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001861 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001862 EXPECT_LF_HERE(ptr, http_msg_invalid);
1863 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001864
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001865 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001866 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1868 /* LWS: replace HT,CR,LF with spaces */
1869 for (; msg->eol < ptr; msg->eol++)
1870 *msg->eol = ' ';
1871 goto http_msg_hdr_val;
1872 }
1873 http_msg_complete_header:
1874 /*
1875 * It was a new header, so the last one is finished.
1876 * Assumes msg->sol points to the first char, msg->col to the
1877 * colon, msg->sov points to the first character of the value
1878 * and msg->eol to the first CR or LF so we know how the line
1879 * ends. We insert last header into the index.
1880 */
1881 /*
1882 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1883 write(2, msg->sol, msg->eol-msg->sol);
1884 fprintf(stderr,"\n");
1885 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001886
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1888 idx, idx->tail) < 0))
1889 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001890
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001891 msg->sol = ptr;
1892 if (likely(!HTTP_IS_CRLF(*ptr))) {
1893 goto http_msg_hdr_name;
1894 }
1895
1896 if (likely(*ptr == '\r'))
1897 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1898 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001899
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001900 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001901 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001902 /* Assumes msg->sol points to the first of either CR or LF */
1903 EXPECT_LF_HERE(ptr, http_msg_invalid);
1904 ptr++;
1905 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001906 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001907 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001908 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001909 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001910 return;
1911#ifdef DEBUG_FULL
1912 default:
1913 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1914 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001915#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001916 }
1917 http_msg_ood:
1918 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001919 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001920 buf->lr = ptr;
1921 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001922
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001923 http_msg_invalid:
1924 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001925 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001926 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001927 return;
1928}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001929
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001930/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1931 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1932 * nothing is done and 1 is returned.
1933 */
1934static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1935{
1936 int delta;
1937 char *cur_end;
1938
1939 if (msg->sl.rq.v_l != 0)
1940 return 1;
1941
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001942 cur_end = msg->sol + msg->sl.rq.l;
1943 delta = 0;
1944
1945 if (msg->sl.rq.u_l == 0) {
1946 /* if no URI was set, add "/" */
1947 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1948 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001949 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001950 }
1951 /* add HTTP version */
1952 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001953 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001954 cur_end += delta;
1955 cur_end = (char *)http_parse_reqline(msg, req->data,
1956 HTTP_MSG_RQMETH,
1957 msg->sol, cur_end + 1,
1958 NULL, NULL);
1959 if (unlikely(!cur_end))
1960 return 0;
1961
1962 /* we have a full HTTP/1.0 request now and we know that
1963 * we have either a CR or an LF at <ptr>.
1964 */
1965 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1966 return 1;
1967}
1968
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001969/* Parse the Connection: header of an HTTP request, looking for both "close"
1970 * and "keep-alive" values. If a buffer is provided and we already know that
1971 * some headers may safely be removed, we remove them now. The <to_del> flags
1972 * are used for that :
1973 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1974 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1975 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1976 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1977 * harmless combinations may be removed. Do not call that after changes have
1978 * been processed. If unused, the buffer can be NULL, and no data will be
1979 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001980 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001981void 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 +01001982{
Willy Tarreau5b154472009-12-21 20:11:07 +01001983 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001984 const char *hdr_val = "Connection";
1985 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001986
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001987 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001988 return;
1989
Willy Tarreau88d349d2010-01-25 12:15:43 +01001990 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1991 hdr_val = "Proxy-Connection";
1992 hdr_len = 16;
1993 }
1994
Willy Tarreau5b154472009-12-21 20:11:07 +01001995 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001996 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001997 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001998 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1999 txn->flags |= TX_HDR_CONN_KAL;
2000 if ((to_del & 2) && buf)
2001 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
2002 else
2003 txn->flags |= TX_CON_KAL_SET;
2004 }
2005 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2006 txn->flags |= TX_HDR_CONN_CLO;
2007 if ((to_del & 1) && buf)
2008 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
2009 else
2010 txn->flags |= TX_CON_CLO_SET;
2011 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002012 }
2013
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002014 txn->flags |= TX_HDR_CONN_PRS;
2015 return;
2016}
Willy Tarreau5b154472009-12-21 20:11:07 +01002017
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002018/* Apply desired changes on the Connection: header. Values may be removed and/or
2019 * added depending on the <wanted> flags, which are exclusively composed of
2020 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2021 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2022 */
2023void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
2024{
2025 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002026 const char *hdr_val = "Connection";
2027 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002028
2029 ctx.idx = 0;
2030
Willy Tarreau88d349d2010-01-25 12:15:43 +01002031
2032 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2033 hdr_val = "Proxy-Connection";
2034 hdr_len = 16;
2035 }
2036
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002037 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002038 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002039 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2040 if (wanted & TX_CON_KAL_SET)
2041 txn->flags |= TX_CON_KAL_SET;
2042 else
2043 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002044 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002045 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2046 if (wanted & TX_CON_CLO_SET)
2047 txn->flags |= TX_CON_CLO_SET;
2048 else
2049 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002050 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002051 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002052
2053 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2054 return;
2055
2056 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2057 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002058 hdr_val = "Connection: close";
2059 hdr_len = 17;
2060 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2061 hdr_val = "Proxy-Connection: close";
2062 hdr_len = 23;
2063 }
2064 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002065 }
2066
2067 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2068 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002069 hdr_val = "Connection: keep-alive";
2070 hdr_len = 22;
2071 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2072 hdr_val = "Proxy-Connection: keep-alive";
2073 hdr_len = 28;
2074 }
2075 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002076 }
2077 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002078}
2079
Willy Tarreaud98cf932009-12-27 22:54:55 +01002080/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2081 * first byte of body, and increments msg->sov by the number of bytes parsed,
2082 * so that we know we can forward between ->som and ->sov. Note that due to
2083 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2084 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002085 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002086 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002087 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002088int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002089{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002090 char *ptr = buf->lr;
2091 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002092 unsigned int chunk = 0;
2093
2094 /* The chunk size is in the following form, though we are only
2095 * interested in the size and CRLF :
2096 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2097 */
2098 while (1) {
2099 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002100 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002101 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002102 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002103 if (c < 0) /* not a hex digit anymore */
2104 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002105 if (++ptr >= end)
2106 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002107 if (chunk & 0xF000000) /* overflow will occur */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002108 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002109 chunk = (chunk << 4) + c;
2110 }
2111
Willy Tarreaud98cf932009-12-27 22:54:55 +01002112 /* empty size not allowed */
2113 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002114 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002115
2116 while (http_is_spht[(unsigned char)*ptr]) {
2117 if (++ptr >= end)
2118 ptr = buf->data;
2119 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002120 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002121 }
2122
Willy Tarreaud98cf932009-12-27 22:54:55 +01002123 /* Up to there, we know that at least one byte is present at *ptr. Check
2124 * for the end of chunk size.
2125 */
2126 while (1) {
2127 if (likely(HTTP_IS_CRLF(*ptr))) {
2128 /* we now have a CR or an LF at ptr */
2129 if (likely(*ptr == '\r')) {
2130 if (++ptr >= end)
2131 ptr = buf->data;
2132 if (ptr == buf->r)
2133 return 0;
2134 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002135
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002137 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002138 if (++ptr >= end)
2139 ptr = buf->data;
2140 /* done */
2141 break;
2142 }
2143 else if (*ptr == ';') {
2144 /* chunk extension, ends at next CRLF */
2145 if (++ptr >= end)
2146 ptr = buf->data;
2147 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002148 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002149
2150 while (!HTTP_IS_CRLF(*ptr)) {
2151 if (++ptr >= end)
2152 ptr = buf->data;
2153 if (ptr == buf->r)
2154 return 0;
2155 }
2156 /* we have a CRLF now, loop above */
2157 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002158 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002159 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002160 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002161 }
2162
Willy Tarreaud98cf932009-12-27 22:54:55 +01002163 /* OK we found our CRLF and now <ptr> points to the next byte,
2164 * which may or may not be present. We save that into ->lr and
2165 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002166 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002167 msg->sov += ptr - buf->lr;
2168 buf->lr = ptr;
Willy Tarreau124d9912011-03-01 20:30:48 +01002169 msg->chunk_len = chunk;
2170 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002171 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002172 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002173 error:
2174 msg->err_pos = ptr - buf->data;
2175 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002176}
2177
Willy Tarreaud98cf932009-12-27 22:54:55 +01002178/* This function skips trailers in the buffer <buf> associated with HTTP
2179 * message <msg>. The first visited position is buf->lr. If the end of
2180 * the trailers is found, it is automatically scheduled to be forwarded,
2181 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2182 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002183 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2184 * zero. If a parse error is encountered, the function returns < 0 and does not
2185 * change anything except maybe buf->lr and msg->sov. Note that the message
2186 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2187 * which implies that all non-trailers data have already been scheduled for
2188 * forwarding, and that the difference between msg->som and msg->sov exactly
2189 * matches the length of trailers already parsed and not forwarded. It is also
2190 * important to note that this function is designed to be able to parse wrapped
2191 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002192 */
2193int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2194{
2195 /* we have buf->lr which points to next line. Look for CRLF. */
2196 while (1) {
2197 char *p1 = NULL, *p2 = NULL;
2198 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002199 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002200
2201 /* scan current line and stop at LF or CRLF */
2202 while (1) {
2203 if (ptr == buf->r)
2204 return 0;
2205
2206 if (*ptr == '\n') {
2207 if (!p1)
2208 p1 = ptr;
2209 p2 = ptr;
2210 break;
2211 }
2212
2213 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002214 if (p1) {
2215 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002216 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002217 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002218 p1 = ptr;
2219 }
2220
2221 ptr++;
2222 if (ptr >= buf->data + buf->size)
2223 ptr = buf->data;
2224 }
2225
2226 /* after LF; point to beginning of next line */
2227 p2++;
2228 if (p2 >= buf->data + buf->size)
2229 p2 = buf->data;
2230
Willy Tarreau638cd022010-01-03 07:42:04 +01002231 bytes = p2 - buf->lr;
2232 if (bytes < 0)
2233 bytes += buf->size;
2234
2235 /* schedule this line for forwarding */
2236 msg->sov += bytes;
2237 if (msg->sov >= buf->size)
2238 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002239
Willy Tarreau638cd022010-01-03 07:42:04 +01002240 if (p1 == buf->lr) {
2241 /* LF/CRLF at beginning of line => end of trailers at p2.
2242 * Everything was scheduled for forwarding, there's nothing
2243 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002244 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002245 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002246 msg->msg_state = HTTP_MSG_DONE;
2247 return 1;
2248 }
2249 /* OK, next line then */
2250 buf->lr = p2;
2251 }
2252}
2253
2254/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2255 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2256 * ->som, buf->lr in order to include this part into the next forwarding phase.
2257 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2258 * not enough data are available, the function does not change anything and
2259 * returns zero. If a parse error is encountered, the function returns < 0 and
2260 * does not change anything. Note: this function is designed to parse wrapped
2261 * CRLF at the end of the buffer.
2262 */
2263int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2264{
2265 char *ptr;
2266 int bytes;
2267
2268 /* NB: we'll check data availabilty at the end. It's not a
2269 * problem because whatever we match first will be checked
2270 * against the correct length.
2271 */
2272 bytes = 1;
2273 ptr = buf->lr;
2274 if (*ptr == '\r') {
2275 bytes++;
2276 ptr++;
2277 if (ptr >= buf->data + buf->size)
2278 ptr = buf->data;
2279 }
2280
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002281 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002282 return 0;
2283
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002284 if (*ptr != '\n') {
2285 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002286 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002287 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002288
2289 ptr++;
2290 if (ptr >= buf->data + buf->size)
2291 ptr = buf->data;
2292 buf->lr = ptr;
2293 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2294 msg->sov = ptr - buf->data;
2295 msg->som = msg->sov - bytes;
2296 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2297 return 1;
2298}
Willy Tarreau5b154472009-12-21 20:11:07 +01002299
Willy Tarreau83e3af02009-12-28 17:39:57 +01002300void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2301{
2302 char *end = buf->data + buf->size;
2303 int off = buf->data + buf->size - buf->w;
2304
2305 /* two possible cases :
2306 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002307 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002308 */
2309 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002310 int block1 = buf->l;
2311 int block2 = 0;
2312 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002313 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002314 block1 = buf->data + buf->size - buf->w;
2315 block2 = buf->r - buf->data;
2316 }
2317 if (block2)
2318 memcpy(swap_buffer, buf->data, block2);
2319 memmove(buf->data, buf->w, block1);
2320 if (block2)
2321 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002322 }
2323
2324 /* adjust all known pointers */
2325 buf->w = buf->data;
2326 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2327 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2328 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2329 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2330
2331 /* adjust relative pointers */
2332 msg->som = 0;
2333 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2334 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2335 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2336
Willy Tarreau83e3af02009-12-28 17:39:57 +01002337 if (msg->err_pos >= 0) {
2338 msg->err_pos += off;
2339 if (msg->err_pos >= buf->size)
2340 msg->err_pos -= buf->size;
2341 }
2342
2343 buf->flags &= ~BF_FULL;
2344 if (buf->l >= buffer_max_len(buf))
2345 buf->flags |= BF_FULL;
2346}
2347
Willy Tarreaud787e662009-07-07 10:14:51 +02002348/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2349 * processing can continue on next analysers, or zero if it either needs more
2350 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2351 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2352 * when it has nothing left to do, and may remove any analyser when it wants to
2353 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002354 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002355int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002356{
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 /*
2358 * We will parse the partial (or complete) lines.
2359 * We will check the request syntax, and also join multi-line
2360 * headers. An index of all the lines will be elaborated while
2361 * parsing.
2362 *
2363 * For the parsing, we use a 28 states FSM.
2364 *
2365 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002366 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002367 * req->data + msg->eoh = end of processed headers / start of current one
2368 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 * req->lr = first non-visited byte
2370 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002371 *
2372 * At end of parsing, we may perform a capture of the error (if any), and
2373 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2374 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2375 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002376 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002377
Willy Tarreau59234e92008-11-30 23:51:27 +01002378 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002379 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002380 struct http_txn *txn = &s->txn;
2381 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002382 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002383
Willy Tarreau6bf17362009-02-24 10:48:35 +01002384 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2385 now_ms, __FUNCTION__,
2386 s,
2387 req,
2388 req->rex, req->wex,
2389 req->flags,
2390 req->l,
2391 req->analysers);
2392
Willy Tarreau52a0c602009-08-16 22:45:38 +02002393 /* we're speaking HTTP here, so let's speak HTTP to the client */
2394 s->srv_error = http_return_srv_error;
2395
Willy Tarreau83e3af02009-12-28 17:39:57 +01002396 /* There's a protected area at the end of the buffer for rewriting
2397 * purposes. We don't want to start to parse the request if the
2398 * protected area is affected, because we may have to move processed
2399 * data later, which is much more complicated.
2400 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002401 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002402 if ((txn->flags & TX_NOT_FIRST) &&
2403 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002404 req->r < req->lr ||
2405 req->r > req->data + req->size - global.tune.maxrewrite)) {
2406 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002407 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2408 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002409 /* some data has still not left the buffer, wake us once that's done */
2410 buffer_dont_connect(req);
2411 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2412 return 0;
2413 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002414 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002415 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002416 }
2417
Willy Tarreau065e8332010-01-08 00:30:20 +01002418 /* Note that we have the same problem with the response ; we
2419 * may want to send a redirect, error or anything which requires
2420 * some spare space. So we'll ensure that we have at least
2421 * maxrewrite bytes available in the response buffer before
2422 * processing that one. This will only affect pipelined
2423 * keep-alive requests.
2424 */
2425 if ((txn->flags & TX_NOT_FIRST) &&
2426 unlikely((s->rep->flags & BF_FULL) ||
2427 s->rep->r < s->rep->lr ||
2428 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2429 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002430 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2431 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002432 /* don't let a connection request be initiated */
2433 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002434 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002435 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002436 return 0;
2437 }
2438 }
2439
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002440 if (likely(req->lr < req->r))
2441 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002442 }
2443
Willy Tarreau59234e92008-11-30 23:51:27 +01002444 /* 1: we might have to print this header in debug mode */
2445 if (unlikely((global.mode & MODE_DEBUG) &&
2446 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002447 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002448 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002449 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002450
Willy Tarreau663308b2010-06-07 14:06:08 +02002451 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002452 eol = sol + msg->sl.rq.l;
2453 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002454
Willy Tarreau59234e92008-11-30 23:51:27 +01002455 sol += hdr_idx_first_pos(&txn->hdr_idx);
2456 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002457
Willy Tarreau59234e92008-11-30 23:51:27 +01002458 while (cur_idx) {
2459 eol = sol + txn->hdr_idx.v[cur_idx].len;
2460 debug_hdr("clihdr", s, sol, eol);
2461 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2462 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002463 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002464 }
2465
Willy Tarreau58f10d72006-12-04 02:26:12 +01002466
Willy Tarreau59234e92008-11-30 23:51:27 +01002467 /*
2468 * Now we quickly check if we have found a full valid request.
2469 * If not so, we check the FD and buffer states before leaving.
2470 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002471 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002472 * requests are checked first. When waiting for a second request
2473 * on a keep-alive session, if we encounter and error, close, t/o,
2474 * we note the error in the session flags but don't set any state.
2475 * Since the error will be noted there, it will not be counted by
2476 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002477 * Last, we may increase some tracked counters' http request errors on
2478 * the cases that are deliberately the client's fault. For instance,
2479 * a timeout or connection reset is not counted as an error. However
2480 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002481 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002482
Willy Tarreau655dce92009-11-08 13:10:58 +01002483 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002484 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002485 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002486 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002487 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002488 session_inc_http_req_ctr(s);
2489 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002490 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002491 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002492 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002493
Willy Tarreau59234e92008-11-30 23:51:27 +01002494 /* 1: Since we are in header mode, if there's no space
2495 * left for headers, we won't be able to free more
2496 * later, so the session will never terminate. We
2497 * must terminate it now.
2498 */
2499 if (unlikely(req->flags & BF_FULL)) {
2500 /* FIXME: check if URI is set and return Status
2501 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002502 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002503 session_inc_http_req_ctr(s);
2504 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002505 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002506 goto return_bad_req;
2507 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002508
Willy Tarreau59234e92008-11-30 23:51:27 +01002509 /* 2: have we encountered a read error ? */
2510 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002511 if (!(s->flags & SN_ERR_MASK))
2512 s->flags |= SN_ERR_CLICL;
2513
Willy Tarreaufcffa692010-01-10 14:21:19 +01002514 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002515 goto failed_keep_alive;
2516
Willy Tarreau59234e92008-11-30 23:51:27 +01002517 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002518 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002519 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002520 session_inc_http_err_ctr(s);
2521 }
2522
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 msg->msg_state = HTTP_MSG_ERROR;
2524 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002525
Willy Tarreauda7ff642010-06-23 11:44:09 +02002526 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002527 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002528 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002529 if (s->listener->counters)
2530 s->listener->counters->failed_req++;
2531
Willy Tarreau59234e92008-11-30 23:51:27 +01002532 if (!(s->flags & SN_FINST_MASK))
2533 s->flags |= SN_FINST_R;
2534 return 0;
2535 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002536
Willy Tarreau59234e92008-11-30 23:51:27 +01002537 /* 3: has the read timeout expired ? */
2538 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002539 if (!(s->flags & SN_ERR_MASK))
2540 s->flags |= SN_ERR_CLITO;
2541
Willy Tarreaufcffa692010-01-10 14:21:19 +01002542 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002543 goto failed_keep_alive;
2544
Willy Tarreau59234e92008-11-30 23:51:27 +01002545 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002546 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002547 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002548 session_inc_http_err_ctr(s);
2549 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 txn->status = 408;
2551 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2552 msg->msg_state = HTTP_MSG_ERROR;
2553 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002554
Willy Tarreauda7ff642010-06-23 11:44:09 +02002555 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002556 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002557 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002558 if (s->listener->counters)
2559 s->listener->counters->failed_req++;
2560
Willy Tarreau59234e92008-11-30 23:51:27 +01002561 if (!(s->flags & SN_FINST_MASK))
2562 s->flags |= SN_FINST_R;
2563 return 0;
2564 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002565
Willy Tarreau59234e92008-11-30 23:51:27 +01002566 /* 4: have we encountered a close ? */
2567 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002568 if (!(s->flags & SN_ERR_MASK))
2569 s->flags |= SN_ERR_CLICL;
2570
Willy Tarreaufcffa692010-01-10 14:21:19 +01002571 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002572 goto failed_keep_alive;
2573
Willy Tarreau4076a152009-04-02 15:18:36 +02002574 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002575 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002576 txn->status = 400;
2577 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2578 msg->msg_state = HTTP_MSG_ERROR;
2579 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002580
Willy Tarreauda7ff642010-06-23 11:44:09 +02002581 session_inc_http_err_ctr(s);
2582 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002583 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002584 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002585 if (s->listener->counters)
2586 s->listener->counters->failed_req++;
2587
Willy Tarreau59234e92008-11-30 23:51:27 +01002588 if (!(s->flags & SN_FINST_MASK))
2589 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002590 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002591 }
2592
Willy Tarreau520d95e2009-09-19 21:04:57 +02002593 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002594 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002595 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002596
Willy Tarreaufcffa692010-01-10 14:21:19 +01002597 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2598 /* If the client starts to talk, let's fall back to
2599 * request timeout processing.
2600 */
2601 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002602 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002603 }
2604
Willy Tarreau59234e92008-11-30 23:51:27 +01002605 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002606 if (!tick_isset(req->analyse_exp)) {
2607 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2608 (txn->flags & TX_WAIT_NEXT_RQ) &&
2609 tick_isset(s->be->timeout.httpka))
2610 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2611 else
2612 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2613 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002614
Willy Tarreau59234e92008-11-30 23:51:27 +01002615 /* we're not ready yet */
2616 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002617
2618 failed_keep_alive:
2619 /* Here we process low-level errors for keep-alive requests. In
2620 * short, if the request is not the first one and it experiences
2621 * a timeout, read error or shutdown, we just silently close so
2622 * that the client can try again.
2623 */
2624 txn->status = 0;
2625 msg->msg_state = HTTP_MSG_RQBEFORE;
2626 req->analysers = 0;
2627 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002628 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002629 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002630 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002631 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002632
Willy Tarreaud787e662009-07-07 10:14:51 +02002633 /* OK now we have a complete HTTP request with indexed headers. Let's
2634 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002635 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2636 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2637 * points to the CRLF of the request line. req->lr points to the first
2638 * byte after the last LF. msg->col and msg->sov point to the first
2639 * byte of data. msg->eol cannot be trusted because it may have been
2640 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002641 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002642
Willy Tarreauda7ff642010-06-23 11:44:09 +02002643 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002644 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2645
Willy Tarreaub16a5742010-01-10 14:46:16 +01002646 if (txn->flags & TX_WAIT_NEXT_RQ) {
2647 /* kill the pending keep-alive timeout */
2648 txn->flags &= ~TX_WAIT_NEXT_RQ;
2649 req->analyse_exp = TICK_ETERNITY;
2650 }
2651
2652
Willy Tarreaud787e662009-07-07 10:14:51 +02002653 /* Maybe we found in invalid header name while we were configured not
2654 * to block on that, so we have to capture it now.
2655 */
2656 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002657 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002658
Willy Tarreau59234e92008-11-30 23:51:27 +01002659 /*
2660 * 1: identify the method
2661 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002662 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002663
2664 /* we can make use of server redirect on GET and HEAD */
2665 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2666 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002667
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 /*
2669 * 2: check if the URI matches the monitor_uri.
2670 * We have to do this for every request which gets in, because
2671 * the monitor-uri is defined by the frontend.
2672 */
2673 if (unlikely((s->fe->monitor_uri_len != 0) &&
2674 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002675 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002676 s->fe->monitor_uri,
2677 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002678 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002679 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002680 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002681 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002682
Willy Tarreau59234e92008-11-30 23:51:27 +01002683 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002684
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002686 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2687 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002688
Willy Tarreau59234e92008-11-30 23:51:27 +01002689 ret = acl_pass(ret);
2690 if (cond->pol == ACL_COND_UNLESS)
2691 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002692
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 if (ret) {
2694 /* we fail this request, let's return 503 service unavail */
2695 txn->status = 503;
2696 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2697 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002698 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002699 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002700
Willy Tarreau59234e92008-11-30 23:51:27 +01002701 /* nothing to fail, let's reply normaly */
2702 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002703 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002704 goto return_prx_cond;
2705 }
2706
2707 /*
2708 * 3: Maybe we have to copy the original REQURI for the logs ?
2709 * Note: we cannot log anymore if the request has been
2710 * classified as invalid.
2711 */
2712 if (unlikely(s->logs.logwait & LW_REQ)) {
2713 /* we have a complete HTTP request that we must log */
2714 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2715 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002716
Willy Tarreau59234e92008-11-30 23:51:27 +01002717 if (urilen >= REQURI_LEN)
2718 urilen = REQURI_LEN - 1;
2719 memcpy(txn->uri, &req->data[msg->som], urilen);
2720 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002721
Willy Tarreau59234e92008-11-30 23:51:27 +01002722 if (!(s->logs.logwait &= ~LW_REQ))
2723 s->do_log(s);
2724 } else {
2725 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002726 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002727 }
Willy Tarreau06619262006-12-17 08:37:22 +01002728
Willy Tarreau59234e92008-11-30 23:51:27 +01002729 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002730 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2731 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002732
Willy Tarreau5b154472009-12-21 20:11:07 +01002733 /* ... and check if the request is HTTP/1.1 or above */
2734 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002735 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2736 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2737 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002738 txn->flags |= TX_REQ_VER_11;
2739
2740 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002741 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002742
Willy Tarreau88d349d2010-01-25 12:15:43 +01002743 /* if the frontend has "option http-use-proxy-header", we'll check if
2744 * we have what looks like a proxied connection instead of a connection,
2745 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2746 * Note that this is *not* RFC-compliant, however browsers and proxies
2747 * happen to do that despite being non-standard :-(
2748 * We consider that a request not beginning with either '/' or '*' is
2749 * a proxied connection, which covers both "scheme://location" and
2750 * CONNECT ip:port.
2751 */
2752 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2753 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2754 txn->flags |= TX_USE_PX_CONN;
2755
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002756 /* transfer length unknown*/
2757 txn->flags &= ~TX_REQ_XFER_LEN;
2758
Willy Tarreau59234e92008-11-30 23:51:27 +01002759 /* 5: we may need to capture headers */
2760 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002761 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002762 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002763
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002764 /* 6: determine the transfer-length.
2765 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2766 * the presence of a message-body in a REQUEST and its transfer length
2767 * must be determined that way (in order of precedence) :
2768 * 1. The presence of a message-body in a request is signaled by the
2769 * inclusion of a Content-Length or Transfer-Encoding header field
2770 * in the request's header fields. When a request message contains
2771 * both a message-body of non-zero length and a method that does
2772 * not define any semantics for that request message-body, then an
2773 * origin server SHOULD either ignore the message-body or respond
2774 * with an appropriate error message (e.g., 413). A proxy or
2775 * gateway, when presented the same request, SHOULD either forward
2776 * the request inbound with the message- body or ignore the
2777 * message-body when determining a response.
2778 *
2779 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2780 * and the "chunked" transfer-coding (Section 6.2) is used, the
2781 * transfer-length is defined by the use of this transfer-coding.
2782 * If a Transfer-Encoding header field is present and the "chunked"
2783 * transfer-coding is not present, the transfer-length is defined
2784 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002785 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002786 * 3. If a Content-Length header field is present, its decimal value in
2787 * OCTETs represents both the entity-length and the transfer-length.
2788 * If a message is received with both a Transfer-Encoding header
2789 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002790 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002791 * 4. By the server closing the connection. (Closing the connection
2792 * cannot be used to indicate the end of a request body, since that
2793 * would leave no possibility for the server to send back a response.)
2794 *
2795 * Whenever a transfer-coding is applied to a message-body, the set of
2796 * transfer-codings MUST include "chunked", unless the message indicates
2797 * it is terminated by closing the connection. When the "chunked"
2798 * transfer-coding is used, it MUST be the last transfer-coding applied
2799 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002800 */
2801
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002802 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002803 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002804 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002805 while ((txn->flags & TX_REQ_VER_11) &&
2806 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002807 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2808 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2809 else if (txn->flags & TX_REQ_TE_CHNK) {
2810 /* bad transfer-encoding (chunked followed by something else) */
2811 use_close_only = 1;
2812 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2813 break;
2814 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002815 }
2816
Willy Tarreau32b47f42009-10-18 20:55:02 +02002817 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002818 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002819 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2820 signed long long cl;
2821
2822 if (!ctx.vlen)
2823 goto return_bad_req;
2824
2825 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2826 goto return_bad_req; /* parse failure */
2827
2828 if (cl < 0)
2829 goto return_bad_req;
2830
Willy Tarreau124d9912011-03-01 20:30:48 +01002831 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl))
Willy Tarreau32b47f42009-10-18 20:55:02 +02002832 goto return_bad_req; /* already specified, was different */
2833
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002834 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002835 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002836 }
2837
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002838 /* bodyless requests have a known length */
2839 if (!use_close_only)
2840 txn->flags |= TX_REQ_XFER_LEN;
2841
Willy Tarreaud787e662009-07-07 10:14:51 +02002842 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002843 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002844 req->analyse_exp = TICK_ETERNITY;
2845 return 1;
2846
2847 return_bad_req:
2848 /* We centralize bad requests processing here */
2849 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2850 /* we detected a parsing error. We want to archive this request
2851 * in the dedicated proxy area for later troubleshooting.
2852 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002853 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002854 }
2855
2856 txn->req.msg_state = HTTP_MSG_ERROR;
2857 txn->status = 400;
2858 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002859
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002860 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002861 if (s->listener->counters)
2862 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002863
2864 return_prx_cond:
2865 if (!(s->flags & SN_ERR_MASK))
2866 s->flags |= SN_ERR_PRXCOND;
2867 if (!(s->flags & SN_FINST_MASK))
2868 s->flags |= SN_FINST_R;
2869
2870 req->analysers = 0;
2871 req->analyse_exp = TICK_ETERNITY;
2872 return 0;
2873}
2874
Cyril Bonté70be45d2010-10-12 00:14:35 +02002875/* We reached the stats page through a POST request.
2876 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002877 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002878 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002879int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002880{
Cyril Bonté70be45d2010-10-12 00:14:35 +02002881 struct proxy *px;
2882 struct server *sv;
2883
2884 char *backend = NULL;
2885 int action = 0;
2886
2887 char *first_param, *cur_param, *next_param, *end_params;
2888
2889 first_param = req->data + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002890 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002891
2892 cur_param = next_param = end_params;
2893
Cyril Bonté23b39d92011-02-10 22:54:44 +01002894 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002895 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002896 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002897 return 1;
2898 }
2899 else if (end_params > req->data + req->l) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002900 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002901 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002902 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002903 }
2904
2905 *end_params = '\0';
2906
Willy Tarreau295a8372011-03-10 11:25:07 +01002907 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002908
2909 /*
2910 * Parse the parameters in reverse order to only store the last value.
2911 * From the html form, the backend and the action are at the end.
2912 */
2913 while (cur_param > first_param) {
2914 char *key, *value;
2915
2916 cur_param--;
2917 if ((*cur_param == '&') || (cur_param == first_param)) {
2918 /* Parse the key */
2919 key = cur_param;
2920 if (cur_param != first_param) {
2921 /* delimit the string for the next loop */
2922 *key++ = '\0';
2923 }
2924
2925 /* Parse the value */
2926 value = key;
2927 while (*value != '\0' && *value != '=') {
2928 value++;
2929 }
2930 if (*value == '=') {
2931 /* Ok, a value is found, we can mark the end of the key */
2932 *value++ = '\0';
2933 }
2934
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002935 if (!url_decode(key) || !url_decode(value))
2936 break;
2937
Cyril Bonté70be45d2010-10-12 00:14:35 +02002938 /* Now we can check the key to see what to do */
2939 if (!backend && strcmp(key, "b") == 0) {
2940 backend = value;
2941 }
2942 else if (!action && strcmp(key, "action") == 0) {
2943 if (strcmp(value, "disable") == 0) {
2944 action = 1;
2945 }
2946 else if (strcmp(value, "enable") == 0) {
2947 action = 2;
2948 } else {
2949 /* unknown action, no need to continue */
2950 break;
2951 }
2952 }
2953 else if (strcmp(key, "s") == 0) {
2954 if (backend && action && get_backend_server(backend, value, &px, &sv)) {
2955 switch (action) {
2956 case 1:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002957 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002958 /* Not already in maintenance, we can change the server state */
2959 sv->state |= SRV_MAINTAIN;
2960 set_server_down(sv);
Willy Tarreau295a8372011-03-10 11:25:07 +01002961 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002962 }
2963 break;
2964 case 2:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002965 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002966 /* Already in maintenance, we can change the server state */
2967 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002968 sv->health = sv->rise; /* up, but will fall down at first failure */
Willy Tarreau295a8372011-03-10 11:25:07 +01002969 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002970 }
2971 break;
2972 }
2973 }
2974 }
2975 next_param = cur_param;
2976 }
2977 }
Cyril Bonté23b39d92011-02-10 22:54:44 +01002978 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002979}
2980
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002981/* returns a pointer to the first rule which forbids access (deny or http_auth),
2982 * or NULL if everything's OK.
2983 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002984static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002985http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2986{
Willy Tarreauff011f22011-01-06 17:51:27 +01002987 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002988
Willy Tarreauff011f22011-01-06 17:51:27 +01002989 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002990 int ret = 1;
2991
Willy Tarreauff011f22011-01-06 17:51:27 +01002992 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002993 continue;
2994
2995 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002996 if (rule->cond) {
2997 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002998 ret = acl_pass(ret);
2999
Willy Tarreauff011f22011-01-06 17:51:27 +01003000 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003001 ret = !ret;
3002 }
3003
3004 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003005 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003006 return NULL; /* no problem */
3007 else
Willy Tarreauff011f22011-01-06 17:51:27 +01003008 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003009 }
3010 }
3011 return NULL;
3012}
3013
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003014/* This stream analyser runs all HTTP request processing which is common to
3015 * frontends and backends, which means blocking ACLs, filters, connection-close,
3016 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003017 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003018 * either needs more data or wants to immediately abort the request (eg: deny,
3019 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003020 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003021int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003022{
Willy Tarreaud787e662009-07-07 10:14:51 +02003023 struct http_txn *txn = &s->txn;
3024 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003025 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003026 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003027 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003028 struct cond_wordlist *wl;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003029 int del_ka, del_cl, do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003030
Willy Tarreau655dce92009-11-08 13:10:58 +01003031 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003032 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003033 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003034 return 0;
3035 }
3036
Willy Tarreau3a816292009-07-07 10:55:49 +02003037 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003038 req->analyse_exp = TICK_ETERNITY;
3039
3040 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3041 now_ms, __FUNCTION__,
3042 s,
3043 req,
3044 req->rex, req->wex,
3045 req->flags,
3046 req->l,
3047 req->analysers);
3048
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003049 /* first check whether we have some ACLs set to block this request */
3050 list_for_each_entry(cond, &px->block_cond, list) {
3051 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003052
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003053 ret = acl_pass(ret);
3054 if (cond->pol == ACL_COND_UNLESS)
3055 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003056
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003057 if (ret) {
3058 txn->status = 403;
3059 /* let's log the request time */
3060 s->logs.tv_request = now;
3061 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003062 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003063 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003064 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003065 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003066
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003067 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01003068 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003069
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003070 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003071 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003072 do_stats = stats_check_uri(s->rep->prod, txn, px);
3073 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01003074 http_req_last_rule = http_check_access_rule(px, &px->uri_auth->http_req_rules, s, txn);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003075 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003076 else
3077 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003078
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003079 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01003080 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003081 txn->status = 403;
3082 s->logs.tv_request = now;
3083 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003084 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003085 s->fe->fe_counters.denied_req++;
3086 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3087 s->be->be_counters.denied_req++;
3088 if (s->listener->counters)
3089 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003090 goto return_prx_cond;
3091 }
3092
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003093 /* try headers filters */
3094 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003095 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003096 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003097
Willy Tarreau59234e92008-11-30 23:51:27 +01003098 /* has the request been denied ? */
3099 if (txn->flags & TX_CLDENY) {
3100 /* no need to go further */
3101 txn->status = 403;
3102 /* let's log the request time */
3103 s->logs.tv_request = now;
3104 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003105 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003106 goto return_prx_cond;
3107 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003108
3109 /* When a connection is tarpitted, we use the tarpit timeout,
3110 * which may be the same as the connect timeout if unspecified.
3111 * If unset, then set it to zero because we really want it to
3112 * eventually expire. We build the tarpit as an analyser.
3113 */
3114 if (txn->flags & TX_CLTARPIT) {
3115 buffer_erase(s->req);
3116 /* wipe the request out so that we can drop the connection early
3117 * if the client closes first.
3118 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003119 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003120 req->analysers = 0; /* remove switching rules etc... */
3121 req->analysers |= AN_REQ_HTTP_TARPIT;
3122 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3123 if (!req->analyse_exp)
3124 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003125 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003126 return 1;
3127 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003128 }
Willy Tarreau06619262006-12-17 08:37:22 +01003129
Willy Tarreau5b154472009-12-21 20:11:07 +01003130 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3131 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003132 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3133 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003134 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3135 * avoid to redo the same work if FE and BE have the same settings (common).
3136 * The method consists in checking if options changed between the two calls
3137 * (implying that either one is non-null, or one of them is non-null and we
3138 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003139 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003140
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003141 del_cl = del_ka = 0;
3142
Willy Tarreaudc008c52010-02-01 16:20:08 +01003143 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3144 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3145 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3146 (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 +01003147 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003148
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003149 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3150 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003151 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003152 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3153 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003154 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003155 tmp = TX_CON_WANT_CLO;
3156
Willy Tarreau5b154472009-12-21 20:11:07 +01003157 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3158 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003159
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003160 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3161 /* parse the Connection header and possibly clean it */
3162 int to_del = 0;
3163 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003164 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3165 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003166 to_del |= 2; /* remove "keep-alive" */
3167 if (!(txn->flags & TX_REQ_VER_11))
3168 to_del |= 1; /* remove "close" */
3169 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003170 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003171
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003172 /* check if client or config asks for explicit close in KAL/SCL */
3173 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3174 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3175 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
3176 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003177 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003178 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
3179 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003180 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3181 }
Willy Tarreau78599912009-10-17 20:12:21 +02003182
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003183 /* we can be blocked here because the request needs to be authenticated,
3184 * either to pass or to access stats.
3185 */
Willy Tarreauff011f22011-01-06 17:51:27 +01003186 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003187 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01003188 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003189
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003190 if (!realm)
3191 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3192
Willy Tarreau844a7e72010-01-31 21:46:18 +01003193 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003194 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3195 txn->status = 401;
3196 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003197 /* on 401 we still count one error, because normal browsing
3198 * won't significantly increase the counter but brute force
3199 * attempts will.
3200 */
3201 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003202 goto return_prx_cond;
3203 }
3204
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003205 /* add request headers from the rule sets in the same order */
3206 list_for_each_entry(wl, &px->req_add, list) {
3207 if (wl->cond) {
3208 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
3209 ret = acl_pass(ret);
3210 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3211 ret = !ret;
3212 if (!ret)
3213 continue;
3214 }
3215
3216 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
3217 goto return_bad_req;
3218 }
3219
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003220 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003221 struct stats_admin_rule *stats_admin_rule;
3222
3223 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003224 * FIXME!!! that one is rather dangerous, we want to
3225 * make it follow standard rules (eg: clear req->analysers).
3226 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003227
Cyril Bonté474be412010-10-12 00:14:36 +02003228 /* now check whether we have some admin rules for this request */
3229 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3230 int ret = 1;
3231
3232 if (stats_admin_rule->cond) {
3233 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
3234 ret = acl_pass(ret);
3235 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3236 ret = !ret;
3237 }
3238
3239 if (ret) {
3240 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01003241 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02003242 break;
3243 }
3244 }
3245
Cyril Bonté70be45d2010-10-12 00:14:35 +02003246 /* Was the status page requested with a POST ? */
3247 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003248 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003249 if (msg->msg_state < HTTP_MSG_100_SENT) {
3250 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3251 * send an HTTP/1.1 100 Continue intermediate response.
3252 */
3253 if (txn->flags & TX_REQ_VER_11) {
3254 struct hdr_ctx ctx;
3255 ctx.idx = 0;
3256 /* Expect is allowed in 1.1, look for it */
3257 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3258 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3259 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3260 }
3261 }
3262 msg->msg_state = HTTP_MSG_100_SENT;
3263 s->logs.tv_request = now; /* update the request timer to reflect full request */
3264 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003265 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003266 /* we need more data */
3267 req->analysers |= an_bit;
3268 buffer_dont_connect(req);
3269 return 0;
3270 }
Cyril Bonté474be412010-10-12 00:14:36 +02003271 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003272 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003273 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003274 }
3275
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003276 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003277 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003278 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003279 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003280 s->rep->prod->applet.private = s;
3281 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003282 req->analysers = 0;
3283
3284 return 0;
3285
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003286 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003287
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003288 /* check whether we have some ACLs set to redirect this request */
3289 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003290 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003291
Willy Tarreauf285f542010-01-03 20:03:03 +01003292 if (rule->cond) {
3293 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3294 ret = acl_pass(ret);
3295 if (rule->cond->pol == ACL_COND_UNLESS)
3296 ret = !ret;
3297 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003298
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003299 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003300 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003301 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003302
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003303 /* build redirect message */
3304 switch(rule->code) {
3305 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003306 msg_fmt = HTTP_303;
3307 break;
3308 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003309 msg_fmt = HTTP_301;
3310 break;
3311 case 302:
3312 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003313 msg_fmt = HTTP_302;
3314 break;
3315 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003316
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003317 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003318 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003319
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003320 switch(rule->type) {
3321 case REDIRECT_TYPE_PREFIX: {
3322 const char *path;
3323 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003324
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003325 path = http_get_path(txn);
3326 /* build message using path */
3327 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003328 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003329 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3330 int qs = 0;
3331 while (qs < pathlen) {
3332 if (path[qs] == '?') {
3333 pathlen = qs;
3334 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003335 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003336 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003337 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003338 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003339 } else {
3340 path = "/";
3341 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003342 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003343
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003344 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003345 goto return_bad_req;
3346
3347 /* add prefix. Note that if prefix == "/", we don't want to
3348 * add anything, otherwise it makes it hard for the user to
3349 * configure a self-redirection.
3350 */
3351 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003352 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3353 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003354 }
3355
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003356 /* add path */
3357 memcpy(rdr.str + rdr.len, path, pathlen);
3358 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003359
3360 /* append a slash at the end of the location is needed and missing */
3361 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3362 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3363 if (rdr.len > rdr.size - 5)
3364 goto return_bad_req;
3365 rdr.str[rdr.len] = '/';
3366 rdr.len++;
3367 }
3368
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003369 break;
3370 }
3371 case REDIRECT_TYPE_LOCATION:
3372 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003373 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003374 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003375
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003376 /* add location */
3377 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3378 rdr.len += rule->rdr_len;
3379 break;
3380 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003381
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003382 if (rule->cookie_len) {
3383 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3384 rdr.len += 14;
3385 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3386 rdr.len += rule->cookie_len;
3387 memcpy(rdr.str + rdr.len, "\r\n", 2);
3388 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003389 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003390
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003391 /* add end of headers and the keep-alive/close status.
3392 * We may choose to set keep-alive if the Location begins
3393 * with a slash, because the client will come back to the
3394 * same server.
3395 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003396 txn->status = rule->code;
3397 /* let's log the request time */
3398 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003399
3400 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3401 (txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau124d9912011-03-01 20:30:48 +01003402 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003403 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3404 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3405 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003406 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003407 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3408 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3409 rdr.len += 30;
3410 } else {
3411 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3412 rdr.len += 24;
3413 }
Willy Tarreau75661452010-01-10 10:35:01 +01003414 }
3415 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3416 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003417 buffer_write(req->prod->ob, rdr.str, rdr.len);
3418 /* "eat" the request */
3419 buffer_ignore(req, msg->sov - msg->som);
3420 msg->som = msg->sov;
3421 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003422 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3423 txn->req.msg_state = HTTP_MSG_CLOSED;
3424 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003425 break;
3426 } else {
3427 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003428 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3429 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3430 rdr.len += 29;
3431 } else {
3432 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3433 rdr.len += 23;
3434 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003435 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003436 goto return_prx_cond;
3437 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003438 }
3439 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003440
Willy Tarreau2be39392010-01-03 17:24:51 +01003441 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3442 * If this happens, then the data will not come immediately, so we must
3443 * send all what we have without waiting. Note that due to the small gain
3444 * in waiting for the body of the request, it's easier to simply put the
3445 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3446 * itself once used.
3447 */
3448 req->flags |= BF_SEND_DONTWAIT;
3449
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003450 /* that's OK for us now, let's move on to next analysers */
3451 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003452
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003453 return_bad_req:
3454 /* We centralize bad requests processing here */
3455 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3456 /* we detected a parsing error. We want to archive this request
3457 * in the dedicated proxy area for later troubleshooting.
3458 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003459 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003460 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003461
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003462 txn->req.msg_state = HTTP_MSG_ERROR;
3463 txn->status = 400;
3464 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003465
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003466 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003467 if (s->listener->counters)
3468 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003469
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003470 return_prx_cond:
3471 if (!(s->flags & SN_ERR_MASK))
3472 s->flags |= SN_ERR_PRXCOND;
3473 if (!(s->flags & SN_FINST_MASK))
3474 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003475
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003476 req->analysers = 0;
3477 req->analyse_exp = TICK_ETERNITY;
3478 return 0;
3479}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003480
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003481/* This function performs all the processing enabled for the current request.
3482 * It returns 1 if the processing can continue on next analysers, or zero if it
3483 * needs more data, encounters an error, or wants to immediately abort the
3484 * request. It relies on buffers flags, and updates s->req->analysers.
3485 */
3486int http_process_request(struct session *s, struct buffer *req, int an_bit)
3487{
3488 struct http_txn *txn = &s->txn;
3489 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003490
Willy Tarreau655dce92009-11-08 13:10:58 +01003491 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003492 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003493 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003494 return 0;
3495 }
3496
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003497 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3498 now_ms, __FUNCTION__,
3499 s,
3500 req,
3501 req->rex, req->wex,
3502 req->flags,
3503 req->l,
3504 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003505
Willy Tarreau59234e92008-11-30 23:51:27 +01003506 /*
3507 * Right now, we know that we have processed the entire headers
3508 * and that unwanted requests have been filtered out. We can do
3509 * whatever we want with the remaining request. Also, now we
3510 * may have separate values for ->fe, ->be.
3511 */
Willy Tarreau06619262006-12-17 08:37:22 +01003512
Willy Tarreau59234e92008-11-30 23:51:27 +01003513 /*
3514 * If HTTP PROXY is set we simply get remote server address
3515 * parsing incoming request.
3516 */
3517 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau957c0a52011-03-03 17:42:23 +01003518 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.s.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003519 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003520
Willy Tarreau59234e92008-11-30 23:51:27 +01003521 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003522 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003523 * Note that doing so might move headers in the request, but
3524 * the fields will stay coherent and the URI will not move.
3525 * This should only be performed in the backend.
3526 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003527 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003528 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3529 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003530
Willy Tarreau59234e92008-11-30 23:51:27 +01003531 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003532 * 8: the appsession cookie was looked up very early in 1.2,
3533 * so let's do the same now.
3534 */
3535
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003536 /* It needs to look into the URI unless persistence must be ignored */
3537 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003538 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003539 }
3540
3541 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003542 * 9: add X-Forwarded-For if either the frontend or the backend
3543 * asks for it.
3544 */
3545 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau957c0a52011-03-03 17:42:23 +01003546 if (s->req->prod->addr.c.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003547 /* Add an X-Forwarded-For header unless the source IP is
3548 * in the 'except' network range.
3549 */
3550 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003551 (((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003552 != s->fe->except_net.s_addr) &&
3553 (!s->be->except_mask.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003554 (((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003555 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003556 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003557 unsigned char *pn;
Willy Tarreau957c0a52011-03-03 17:42:23 +01003558 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003559
3560 /* Note: we rely on the backend to get the header name to be used for
3561 * x-forwarded-for, because the header is really meant for the backends.
3562 * However, if the backend did not specify any option, we have to rely
3563 * on the frontend's header name.
3564 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003565 if (s->be->fwdfor_hdr_len) {
3566 len = s->be->fwdfor_hdr_len;
3567 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003568 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003569 len = s->fe->fwdfor_hdr_len;
3570 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003571 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003572 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003573
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003574 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003575 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003576 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003577 }
3578 }
Willy Tarreau957c0a52011-03-03 17:42:23 +01003579 else if (s->req->prod->addr.c.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003580 /* FIXME: for the sake of completeness, we should also support
3581 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003582 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003583 int len;
3584 char pn[INET6_ADDRSTRLEN];
3585 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003586 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003587 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003588
Willy Tarreau59234e92008-11-30 23:51:27 +01003589 /* Note: we rely on the backend to get the header name to be used for
3590 * x-forwarded-for, because the header is really meant for the backends.
3591 * However, if the backend did not specify any option, we have to rely
3592 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003593 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003594 if (s->be->fwdfor_hdr_len) {
3595 len = s->be->fwdfor_hdr_len;
3596 memcpy(trash, s->be->fwdfor_hdr_name, len);
3597 } else {
3598 len = s->fe->fwdfor_hdr_len;
3599 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003600 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003601 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003602
Willy Tarreau59234e92008-11-30 23:51:27 +01003603 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003604 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003605 goto return_bad_req;
3606 }
3607 }
3608
3609 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003610 * 10: add X-Original-To if either the frontend or the backend
3611 * asks for it.
3612 */
3613 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3614
3615 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau957c0a52011-03-03 17:42:23 +01003616 if (s->req->prod->addr.c.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003617 /* Add an X-Original-To header unless the destination IP is
3618 * in the 'except' network range.
3619 */
3620 if (!(s->flags & SN_FRT_ADDR_SET))
3621 get_frt_addr(s);
3622
Willy Tarreau957c0a52011-03-03 17:42:23 +01003623 if (s->req->prod->addr.c.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003624 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003625 (((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003626 != s->fe->except_to.s_addr) &&
3627 (!s->be->except_mask_to.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003628 (((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003629 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003630 int len;
3631 unsigned char *pn;
Willy Tarreau957c0a52011-03-03 17:42:23 +01003632 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003633
3634 /* Note: we rely on the backend to get the header name to be used for
3635 * x-original-to, because the header is really meant for the backends.
3636 * However, if the backend did not specify any option, we have to rely
3637 * on the frontend's header name.
3638 */
3639 if (s->be->orgto_hdr_len) {
3640 len = s->be->orgto_hdr_len;
3641 memcpy(trash, s->be->orgto_hdr_name, len);
3642 } else {
3643 len = s->fe->orgto_hdr_len;
3644 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003645 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003646 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3647
3648 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003649 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003650 goto return_bad_req;
3651 }
3652 }
3653 }
3654
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003655 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3656 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003657 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003658 unsigned int want_flags = 0;
3659
3660 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003661 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3662 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3663 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003664 want_flags |= TX_CON_CLO_SET;
3665 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003666 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3667 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3668 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003669 want_flags |= TX_CON_KAL_SET;
3670 }
3671
3672 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3673 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003674 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003675
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003676
Willy Tarreau522d6c02009-12-06 18:49:18 +01003677 /* If we have no server assigned yet and we're balancing on url_param
3678 * with a POST request, we may be interested in checking the body for
3679 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003680 */
3681 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3682 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003683 s->be->url_param_post_limit != 0 &&
Willy Tarreau61a21a32011-03-01 20:35:49 +01003684 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003685 buffer_dont_connect(req);
3686 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003687 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003688
Willy Tarreaud98cf932009-12-27 22:54:55 +01003689 if (txn->flags & TX_REQ_XFER_LEN)
3690 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003691
Willy Tarreau59234e92008-11-30 23:51:27 +01003692 /*************************************************************
3693 * OK, that's finished for the headers. We have done what we *
3694 * could. Let's switch to the DATA state. *
3695 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003696 req->analyse_exp = TICK_ETERNITY;
3697 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003698
Willy Tarreau59234e92008-11-30 23:51:27 +01003699 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003700 /* OK let's go on with the BODY now */
3701 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003702
Willy Tarreau59234e92008-11-30 23:51:27 +01003703 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003704 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003705 /* we detected a parsing error. We want to archive this request
3706 * in the dedicated proxy area for later troubleshooting.
3707 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003708 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003709 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003710
Willy Tarreau59234e92008-11-30 23:51:27 +01003711 txn->req.msg_state = HTTP_MSG_ERROR;
3712 txn->status = 400;
3713 req->analysers = 0;
3714 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003715
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003716 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003717 if (s->listener->counters)
3718 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003719
Willy Tarreau59234e92008-11-30 23:51:27 +01003720 if (!(s->flags & SN_ERR_MASK))
3721 s->flags |= SN_ERR_PRXCOND;
3722 if (!(s->flags & SN_FINST_MASK))
3723 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003724 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003725}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003726
Willy Tarreau60b85b02008-11-30 23:28:40 +01003727/* This function is an analyser which processes the HTTP tarpit. It always
3728 * returns zero, at the beginning because it prevents any other processing
3729 * from occurring, and at the end because it terminates the request.
3730 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003731int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003732{
3733 struct http_txn *txn = &s->txn;
3734
3735 /* This connection is being tarpitted. The CLIENT side has
3736 * already set the connect expiration date to the right
3737 * timeout. We just have to check that the client is still
3738 * there and that the timeout has not expired.
3739 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003740 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003741 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3742 !tick_is_expired(req->analyse_exp, now_ms))
3743 return 0;
3744
3745 /* We will set the queue timer to the time spent, just for
3746 * logging purposes. We fake a 500 server error, so that the
3747 * attacker will not suspect his connection has been tarpitted.
3748 * It will not cause trouble to the logs because we can exclude
3749 * the tarpitted connections by filtering on the 'PT' status flags.
3750 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003751 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3752
3753 txn->status = 500;
3754 if (req->flags != BF_READ_ERROR)
3755 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3756
3757 req->analysers = 0;
3758 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003759
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003760 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003761 if (s->listener->counters)
3762 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003763
Willy Tarreau60b85b02008-11-30 23:28:40 +01003764 if (!(s->flags & SN_ERR_MASK))
3765 s->flags |= SN_ERR_PRXCOND;
3766 if (!(s->flags & SN_FINST_MASK))
3767 s->flags |= SN_FINST_T;
3768 return 0;
3769}
3770
Willy Tarreaud34af782008-11-30 23:36:37 +01003771/* This function is an analyser which processes the HTTP request body. It looks
3772 * for parameters to be used for the load balancing algorithm (url_param). It
3773 * must only be called after the standard HTTP request processing has occurred,
3774 * because it expects the request to be parsed. It returns zero if it needs to
3775 * read more data, or 1 once it has completed its analysis.
3776 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003777int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003778{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003779 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003780 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003781 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003782
3783 /* We have to parse the HTTP request body to find any required data.
3784 * "balance url_param check_post" should have been the only way to get
3785 * into this. We were brought here after HTTP header analysis, so all
3786 * related structures are ready.
3787 */
3788
Willy Tarreau522d6c02009-12-06 18:49:18 +01003789 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3790 goto missing_data;
3791
3792 if (msg->msg_state < HTTP_MSG_100_SENT) {
3793 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3794 * send an HTTP/1.1 100 Continue intermediate response.
3795 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003796 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003797 struct hdr_ctx ctx;
3798 ctx.idx = 0;
3799 /* Expect is allowed in 1.1, look for it */
3800 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3801 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3802 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3803 }
3804 }
3805 msg->msg_state = HTTP_MSG_100_SENT;
3806 }
3807
3808 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003809 /* we have msg->col and msg->sov which both point to the first
3810 * byte of message body. msg->som still points to the beginning
3811 * of the message. We must save the body in req->lr because it
3812 * survives buffer re-alignments.
3813 */
3814 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003815 if (txn->flags & TX_REQ_TE_CHNK)
3816 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3817 else
3818 msg->msg_state = HTTP_MSG_DATA;
3819 }
3820
3821 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003822 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003823 * set ->sov and ->lr to point to the body and switch to DATA or
3824 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003825 */
3826 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003827
Willy Tarreau115acb92009-12-26 13:56:06 +01003828 if (!ret)
3829 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003830 else if (ret < 0) {
3831 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003832 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003833 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003834 }
3835
Willy Tarreaud98cf932009-12-27 22:54:55 +01003836 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003837 * We have the first non-header byte in msg->col, which is either the
3838 * beginning of the chunk size or of the data. The first data byte is in
3839 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3840 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003841 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003842
Willy Tarreau124d9912011-03-01 20:30:48 +01003843 if (msg->body_len < limit)
3844 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003845
Willy Tarreau7c96f672009-12-27 22:47:25 +01003846 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003847 goto http_end;
3848
3849 missing_data:
3850 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003851 if (req->flags & BF_FULL) {
3852 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003853 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003854 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003855
Willy Tarreau522d6c02009-12-06 18:49:18 +01003856 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3857 txn->status = 408;
3858 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003859
3860 if (!(s->flags & SN_ERR_MASK))
3861 s->flags |= SN_ERR_CLITO;
3862 if (!(s->flags & SN_FINST_MASK))
3863 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003864 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003865 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003866
3867 /* we get here if we need to wait for more data */
3868 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003869 /* Not enough data. We'll re-use the http-request
3870 * timeout here. Ideally, we should set the timeout
3871 * relative to the accept() date. We just set the
3872 * request timeout once at the beginning of the
3873 * request.
3874 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003875 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003876 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003877 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003878 return 0;
3879 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003880
3881 http_end:
3882 /* The situation will not evolve, so let's give up on the analysis. */
3883 s->logs.tv_request = now; /* update the request timer to reflect full request */
3884 req->analysers &= ~an_bit;
3885 req->analyse_exp = TICK_ETERNITY;
3886 return 1;
3887
3888 return_bad_req: /* let's centralize all bad requests */
3889 txn->req.msg_state = HTTP_MSG_ERROR;
3890 txn->status = 400;
3891 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3892
Willy Tarreau79ebac62010-06-07 13:47:49 +02003893 if (!(s->flags & SN_ERR_MASK))
3894 s->flags |= SN_ERR_PRXCOND;
3895 if (!(s->flags & SN_FINST_MASK))
3896 s->flags |= SN_FINST_R;
3897
Willy Tarreau522d6c02009-12-06 18:49:18 +01003898 return_err_msg:
3899 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003900 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003901 if (s->listener->counters)
3902 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003903 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003904}
3905
Willy Tarreau610ecce2010-01-04 21:15:02 +01003906/* Terminate current transaction and prepare a new one. This is very tricky
3907 * right now but it works.
3908 */
3909void http_end_txn_clean_session(struct session *s)
3910{
3911 /* FIXME: We need a more portable way of releasing a backend's and a
3912 * server's connections. We need a safer way to reinitialize buffer
3913 * flags. We also need a more accurate method for computing per-request
3914 * data.
3915 */
3916 http_silent_debug(__LINE__, s);
3917
3918 s->req->cons->flags |= SI_FL_NOLINGER;
3919 s->req->cons->shutr(s->req->cons);
3920 s->req->cons->shutw(s->req->cons);
3921
3922 http_silent_debug(__LINE__, s);
3923
3924 if (s->flags & SN_BE_ASSIGNED)
3925 s->be->beconn--;
3926
3927 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3928 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003929 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003930
3931 if (s->txn.status) {
3932 int n;
3933
3934 n = s->txn.status / 100;
3935 if (n < 1 || n > 5)
3936 n = 0;
3937
3938 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003939 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003940
Willy Tarreau24657792010-02-26 10:30:28 +01003941 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003942 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003943 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003944 }
3945
3946 /* don't count other requests' data */
3947 s->logs.bytes_in -= s->req->l - s->req->send_max;
3948 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3949
3950 /* let's do a final log if we need it */
3951 if (s->logs.logwait &&
3952 !(s->flags & SN_MONITOR) &&
3953 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3954 s->do_log(s);
3955 }
3956
3957 s->logs.accept_date = date; /* user-visible date for logging */
3958 s->logs.tv_accept = now; /* corrected date for internal use */
3959 tv_zero(&s->logs.tv_request);
3960 s->logs.t_queue = -1;
3961 s->logs.t_connect = -1;
3962 s->logs.t_data = -1;
3963 s->logs.t_close = 0;
3964 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3965 s->logs.srv_queue_size = 0; /* we will get this number soon */
3966
3967 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3968 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3969
3970 if (s->pend_pos)
3971 pendconn_free(s->pend_pos);
3972
Willy Tarreau827aee92011-03-10 16:55:02 +01003973 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003974 if (s->flags & SN_CURR_SESS) {
3975 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003976 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003977 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003978 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3979 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003980 }
3981
3982 if (unlikely(s->srv_conn))
3983 sess_change_server(s, NULL);
Willy Tarreau9e000c62011-03-10 14:03:36 +01003984 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003985
3986 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3987 s->req->cons->fd = -1; /* just to help with debugging */
3988 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003989 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003990 s->req->cons->err_loc = NULL;
3991 s->req->cons->exp = TICK_ETERNITY;
3992 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003993 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3994 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|BF_NEVER_WAIT);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003995 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 +01003996 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3997 s->txn.meth = 0;
3998 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003999 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004000 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004001 s->req->cons->flags |= SI_FL_INDEP_STR;
4002
Willy Tarreau96e31212011-05-30 18:10:30 +02004003 if (s->fe->options2 & PR_O2_NODELAY) {
4004 s->req->flags |= BF_NEVER_WAIT;
4005 s->rep->flags |= BF_NEVER_WAIT;
4006 }
4007
Willy Tarreau610ecce2010-01-04 21:15:02 +01004008 /* if the request buffer is not empty, it means we're
4009 * about to process another request, so send pending
4010 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004011 * Just don't do this if the buffer is close to be full,
4012 * because the request will wait for it to flush a little
4013 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004014 */
Willy Tarreau065e8332010-01-08 00:30:20 +01004015 if (s->req->l > s->req->send_max) {
4016 if (s->rep->send_max &&
4017 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01004018 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
4019 s->rep->flags |= BF_EXPECT_MORE;
4020 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004021
4022 /* we're removing the analysers, we MUST re-enable events detection */
4023 buffer_auto_read(s->req);
4024 buffer_auto_close(s->req);
4025 buffer_auto_read(s->rep);
4026 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004027
4028 /* make ->lr point to the first non-forwarded byte */
4029 s->req->lr = s->req->w + s->req->send_max;
4030 if (s->req->lr >= s->req->data + s->req->size)
4031 s->req->lr -= s->req->size;
4032 s->rep->lr = s->rep->w + s->rep->send_max;
4033 if (s->rep->lr >= s->rep->data + s->rep->size)
4034 s->rep->lr -= s->req->size;
4035
Willy Tarreau342b11c2010-11-24 16:22:09 +01004036 s->req->analysers = s->listener->analysers;
4037 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004038 s->rep->analysers = 0;
4039
4040 http_silent_debug(__LINE__, s);
4041}
4042
4043
4044/* This function updates the request state machine according to the response
4045 * state machine and buffer flags. It returns 1 if it changes anything (flag
4046 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4047 * it is only used to find when a request/response couple is complete. Both
4048 * this function and its equivalent should loop until both return zero. It
4049 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4050 */
4051int http_sync_req_state(struct session *s)
4052{
4053 struct buffer *buf = s->req;
4054 struct http_txn *txn = &s->txn;
4055 unsigned int old_flags = buf->flags;
4056 unsigned int old_state = txn->req.msg_state;
4057
4058 http_silent_debug(__LINE__, s);
4059 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4060 return 0;
4061
4062 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004063 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004064 * We can shut the read side unless we want to abort_on_close,
4065 * or we have a POST request. The issue with POST requests is
4066 * that some browsers still send a CRLF after the request, and
4067 * this CRLF must be read so that it does not remain in the kernel
4068 * buffers, otherwise a close could cause an RST on some systems
4069 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004070 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004071 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01004072 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004073
4074 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4075 goto wait_other_side;
4076
4077 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4078 /* The server has not finished to respond, so we
4079 * don't want to move in order not to upset it.
4080 */
4081 goto wait_other_side;
4082 }
4083
4084 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4085 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004086 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004087 txn->req.msg_state = HTTP_MSG_TUNNEL;
4088 goto wait_other_side;
4089 }
4090
4091 /* When we get here, it means that both the request and the
4092 * response have finished receiving. Depending on the connection
4093 * mode, we'll have to wait for the last bytes to leave in either
4094 * direction, and sometimes for a close to be effective.
4095 */
4096
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004097 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4098 /* Server-close mode : queue a connection close to the server */
4099 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004100 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004101 }
4102 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4103 /* Option forceclose is set, or either side wants to close,
4104 * let's enforce it now that we're not expecting any new
4105 * data to come. The caller knows the session is complete
4106 * once both states are CLOSED.
4107 */
4108 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004109 buffer_shutr_now(buf);
4110 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004111 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004112 }
4113 else {
4114 /* The last possible modes are keep-alive and tunnel. Since tunnel
4115 * mode does not set the body analyser, we can't reach this place
4116 * in tunnel mode, so we're left with keep-alive only.
4117 * This mode is currently not implemented, we switch to tunnel mode.
4118 */
4119 buffer_auto_read(buf);
4120 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004121 }
4122
4123 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4124 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004125 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4126
Willy Tarreau610ecce2010-01-04 21:15:02 +01004127 if (!(buf->flags & BF_OUT_EMPTY)) {
4128 txn->req.msg_state = HTTP_MSG_CLOSING;
4129 goto http_msg_closing;
4130 }
4131 else {
4132 txn->req.msg_state = HTTP_MSG_CLOSED;
4133 goto http_msg_closed;
4134 }
4135 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004136 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004137 }
4138
4139 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4140 http_msg_closing:
4141 /* nothing else to forward, just waiting for the output buffer
4142 * to be empty and for the shutw_now to take effect.
4143 */
4144 if (buf->flags & BF_OUT_EMPTY) {
4145 txn->req.msg_state = HTTP_MSG_CLOSED;
4146 goto http_msg_closed;
4147 }
4148 else if (buf->flags & BF_SHUTW) {
4149 txn->req.msg_state = HTTP_MSG_ERROR;
4150 goto wait_other_side;
4151 }
4152 }
4153
4154 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4155 http_msg_closed:
4156 goto wait_other_side;
4157 }
4158
4159 wait_other_side:
4160 http_silent_debug(__LINE__, s);
4161 return txn->req.msg_state != old_state || buf->flags != old_flags;
4162}
4163
4164
4165/* This function updates the response state machine according to the request
4166 * state machine and buffer flags. It returns 1 if it changes anything (flag
4167 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4168 * it is only used to find when a request/response couple is complete. Both
4169 * this function and its equivalent should loop until both return zero. It
4170 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4171 */
4172int http_sync_res_state(struct session *s)
4173{
4174 struct buffer *buf = s->rep;
4175 struct http_txn *txn = &s->txn;
4176 unsigned int old_flags = buf->flags;
4177 unsigned int old_state = txn->rsp.msg_state;
4178
4179 http_silent_debug(__LINE__, s);
4180 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4181 return 0;
4182
4183 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4184 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004185 * still monitor the server connection for a possible close
4186 * while the request is being uploaded, so we don't disable
4187 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004188 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004189 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004190
4191 if (txn->req.msg_state == HTTP_MSG_ERROR)
4192 goto wait_other_side;
4193
4194 if (txn->req.msg_state < HTTP_MSG_DONE) {
4195 /* The client seems to still be sending data, probably
4196 * because we got an error response during an upload.
4197 * We have the choice of either breaking the connection
4198 * or letting it pass through. Let's do the later.
4199 */
4200 goto wait_other_side;
4201 }
4202
4203 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4204 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004205 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004206 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4207 goto wait_other_side;
4208 }
4209
4210 /* When we get here, it means that both the request and the
4211 * response have finished receiving. Depending on the connection
4212 * mode, we'll have to wait for the last bytes to leave in either
4213 * direction, and sometimes for a close to be effective.
4214 */
4215
4216 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4217 /* Server-close mode : shut read and wait for the request
4218 * side to close its output buffer. The caller will detect
4219 * when we're in DONE and the other is in CLOSED and will
4220 * catch that for the final cleanup.
4221 */
4222 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4223 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004224 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004225 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4226 /* Option forceclose is set, or either side wants to close,
4227 * let's enforce it now that we're not expecting any new
4228 * data to come. The caller knows the session is complete
4229 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004230 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004231 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4232 buffer_shutr_now(buf);
4233 buffer_shutw_now(buf);
4234 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004235 }
4236 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004237 /* The last possible modes are keep-alive and tunnel. Since tunnel
4238 * mode does not set the body analyser, we can't reach this place
4239 * in tunnel mode, so we're left with keep-alive only.
4240 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004241 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004242 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004243 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004244 }
4245
4246 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4247 /* if we've just closed an output, let's switch */
4248 if (!(buf->flags & BF_OUT_EMPTY)) {
4249 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4250 goto http_msg_closing;
4251 }
4252 else {
4253 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4254 goto http_msg_closed;
4255 }
4256 }
4257 goto wait_other_side;
4258 }
4259
4260 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4261 http_msg_closing:
4262 /* nothing else to forward, just waiting for the output buffer
4263 * to be empty and for the shutw_now to take effect.
4264 */
4265 if (buf->flags & BF_OUT_EMPTY) {
4266 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4267 goto http_msg_closed;
4268 }
4269 else if (buf->flags & BF_SHUTW) {
4270 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004271 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004272 if (target_srv(&s->target))
4273 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004274 goto wait_other_side;
4275 }
4276 }
4277
4278 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4279 http_msg_closed:
4280 /* drop any pending data */
4281 buffer_ignore(buf, buf->l - buf->send_max);
4282 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004283 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004284 goto wait_other_side;
4285 }
4286
4287 wait_other_side:
4288 http_silent_debug(__LINE__, s);
4289 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4290}
4291
4292
4293/* Resync the request and response state machines. Return 1 if either state
4294 * changes.
4295 */
4296int http_resync_states(struct session *s)
4297{
4298 struct http_txn *txn = &s->txn;
4299 int old_req_state = txn->req.msg_state;
4300 int old_res_state = txn->rsp.msg_state;
4301
4302 http_silent_debug(__LINE__, s);
4303 http_sync_req_state(s);
4304 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004305 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004306 if (!http_sync_res_state(s))
4307 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004308 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004309 if (!http_sync_req_state(s))
4310 break;
4311 }
4312 http_silent_debug(__LINE__, s);
4313 /* OK, both state machines agree on a compatible state.
4314 * There are a few cases we're interested in :
4315 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4316 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4317 * directions, so let's simply disable both analysers.
4318 * - HTTP_MSG_CLOSED on the response only means we must abort the
4319 * request.
4320 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4321 * with server-close mode means we've completed one request and we
4322 * must re-initialize the server connection.
4323 */
4324
4325 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4326 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4327 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4328 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4329 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004330 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004331 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004332 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004333 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004334 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004335 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004336 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4337 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004338 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004339 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004340 s->rep->analysers = 0;
4341 buffer_auto_close(s->rep);
4342 buffer_auto_read(s->rep);
4343 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004344 buffer_abort(s->req);
4345 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004346 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004347 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004348 }
4349 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4350 txn->rsp.msg_state == HTTP_MSG_DONE &&
4351 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4352 /* server-close: terminate this server connection and
4353 * reinitialize a fresh-new transaction.
4354 */
4355 http_end_txn_clean_session(s);
4356 }
4357
4358 http_silent_debug(__LINE__, s);
4359 return txn->req.msg_state != old_req_state ||
4360 txn->rsp.msg_state != old_res_state;
4361}
4362
Willy Tarreaud98cf932009-12-27 22:54:55 +01004363/* This function is an analyser which forwards request body (including chunk
4364 * sizes if any). It is called as soon as we must forward, even if we forward
4365 * zero byte. The only situation where it must not be called is when we're in
4366 * tunnel mode and we want to forward till the close. It's used both to forward
4367 * remaining data and to resync after end of body. It expects the msg_state to
4368 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4369 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004370 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004371 * bytes of pending data + the headers if not already done (between som and sov).
4372 * It eventually adjusts som to match sov after the data in between have been sent.
4373 */
4374int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4375{
4376 struct http_txn *txn = &s->txn;
4377 struct http_msg *msg = &s->txn.req;
4378
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004379 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4380 return 0;
4381
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004382 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4383 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004384 /* Output closed while we were sending data. We must abort and
4385 * wake the other side up.
4386 */
4387 msg->msg_state = HTTP_MSG_ERROR;
4388 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004389 return 1;
4390 }
4391
Willy Tarreau4fe41902010-06-07 22:27:41 +02004392 /* in most states, we should abort in case of early close */
4393 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004394
4395 /* Note that we don't have to send 100-continue back because we don't
4396 * need the data to complete our job, and it's up to the server to
4397 * decide whether to return 100, 417 or anything else in return of
4398 * an "Expect: 100-continue" header.
4399 */
4400
4401 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4402 /* we have msg->col and msg->sov which both point to the first
4403 * byte of message body. msg->som still points to the beginning
4404 * of the message. We must save the body in req->lr because it
4405 * survives buffer re-alignments.
4406 */
4407 req->lr = req->data + msg->sov;
4408 if (txn->flags & TX_REQ_TE_CHNK)
4409 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4410 else {
4411 msg->msg_state = HTTP_MSG_DATA;
4412 }
4413 }
4414
Willy Tarreaud98cf932009-12-27 22:54:55 +01004415 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004416 int bytes;
4417
Willy Tarreau610ecce2010-01-04 21:15:02 +01004418 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004419 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004420 bytes = msg->sov - msg->som;
4421 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004422 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004423 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4424 bytes += req->size;
4425 msg->chunk_len += (unsigned int)bytes;
4426 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004427 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004428
Willy Tarreaucaabe412010-01-03 23:08:28 +01004429 if (msg->msg_state == HTTP_MSG_DATA) {
4430 /* must still forward */
4431 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004432 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004433
4434 /* nothing left to forward */
4435 if (txn->flags & TX_REQ_TE_CHNK)
4436 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004437 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004438 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004439 }
4440 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004441 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004442 * set ->sov and ->lr to point to the body and switch to DATA or
4443 * TRAILERS state.
4444 */
4445 int ret = http_parse_chunk_size(req, msg);
4446
4447 if (!ret)
4448 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004449 else if (ret < 0) {
4450 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004451 if (msg->err_pos >= 0)
4452 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004453 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004454 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004455 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004456 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004457 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4458 /* we want the CRLF after the data */
4459 int ret;
4460
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004461 req->lr = req->w + req->send_max;
4462 if (req->lr >= req->data + req->size)
4463 req->lr -= req->size;
4464
Willy Tarreaud98cf932009-12-27 22:54:55 +01004465 ret = http_skip_chunk_crlf(req, msg);
4466
4467 if (ret == 0)
4468 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004469 else if (ret < 0) {
4470 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004471 if (msg->err_pos >= 0)
4472 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004473 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004474 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004475 /* we're in MSG_CHUNK_SIZE now */
4476 }
4477 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4478 int ret = http_forward_trailers(req, msg);
4479
4480 if (ret == 0)
4481 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004482 else if (ret < 0) {
4483 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004484 if (msg->err_pos >= 0)
4485 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004486 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004487 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004488 /* we're in HTTP_MSG_DONE now */
4489 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004490 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004491 int old_state = msg->msg_state;
4492
Willy Tarreau610ecce2010-01-04 21:15:02 +01004493 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004494 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004495 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4496 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4497 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004498 if (http_resync_states(s)) {
4499 /* some state changes occurred, maybe the analyser
4500 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004501 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004502 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4503 if (req->flags & BF_SHUTW) {
4504 /* request errors are most likely due to
4505 * the server aborting the transfer.
4506 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004507 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004508 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004509 if (msg->err_pos >= 0)
4510 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004511 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004512 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004513 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004514 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004515
4516 /* If "option abortonclose" is set on the backend, we
4517 * want to monitor the client's connection and forward
4518 * any shutdown notification to the server, which will
4519 * decide whether to close or to go on processing the
4520 * request.
4521 */
4522 if (s->be->options & PR_O_ABRT_CLOSE) {
4523 buffer_auto_read(req);
4524 buffer_auto_close(req);
4525 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004526 else if (s->txn.meth == HTTP_METH_POST) {
4527 /* POST requests may require to read extra CRLF
4528 * sent by broken browsers and which could cause
4529 * an RST to be sent upon close on some systems
4530 * (eg: Linux).
4531 */
4532 buffer_auto_read(req);
4533 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004534
Willy Tarreau610ecce2010-01-04 21:15:02 +01004535 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004536 }
4537 }
4538
Willy Tarreaud98cf932009-12-27 22:54:55 +01004539 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004540 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004541 if (req->flags & BF_SHUTR) {
4542 if (!(s->flags & SN_ERR_MASK))
4543 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004544 if (!(s->flags & SN_FINST_MASK)) {
4545 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4546 s->flags |= SN_FINST_H;
4547 else
4548 s->flags |= SN_FINST_D;
4549 }
4550
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004551 s->fe->fe_counters.cli_aborts++;
4552 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004553 if (target_srv(&s->target))
4554 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004555
4556 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004557 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004558
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004559 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004560 if (req->flags & BF_SHUTW)
4561 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004562
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004563 /* When TE: chunked is used, we need to get there again to parse remaining
4564 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4565 */
4566 if (txn->flags & TX_REQ_TE_CHNK)
4567 buffer_dont_close(req);
4568
Willy Tarreau5c620922011-05-11 19:56:11 +02004569 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004570 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4571 * system knows it must not set a PUSH on this first part. Interactive
4572 * modes are already handled by the stream sock layer.
Willy Tarreau5c620922011-05-11 19:56:11 +02004573 */
Willy Tarreau07293032011-05-30 18:29:28 +02004574 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004575
Willy Tarreau610ecce2010-01-04 21:15:02 +01004576 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004577 return 0;
4578
Willy Tarreaud98cf932009-12-27 22:54:55 +01004579 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004580 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004581 if (s->listener->counters)
4582 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004583 return_bad_req_stats_ok:
4584 txn->req.msg_state = HTTP_MSG_ERROR;
4585 if (txn->status) {
4586 /* Note: we don't send any error if some data were already sent */
4587 stream_int_retnclose(req->prod, NULL);
4588 } else {
4589 txn->status = 400;
4590 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4591 }
4592 req->analysers = 0;
4593 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004594
4595 if (!(s->flags & SN_ERR_MASK))
4596 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004597 if (!(s->flags & SN_FINST_MASK)) {
4598 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4599 s->flags |= SN_FINST_H;
4600 else
4601 s->flags |= SN_FINST_D;
4602 }
4603 return 0;
4604
4605 aborted_xfer:
4606 txn->req.msg_state = HTTP_MSG_ERROR;
4607 if (txn->status) {
4608 /* Note: we don't send any error if some data were already sent */
4609 stream_int_retnclose(req->prod, NULL);
4610 } else {
4611 txn->status = 502;
4612 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4613 }
4614 req->analysers = 0;
4615 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4616
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004617 s->fe->fe_counters.srv_aborts++;
4618 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004619 if (target_srv(&s->target))
4620 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004621
4622 if (!(s->flags & SN_ERR_MASK))
4623 s->flags |= SN_ERR_SRVCL;
4624 if (!(s->flags & SN_FINST_MASK)) {
4625 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4626 s->flags |= SN_FINST_H;
4627 else
4628 s->flags |= SN_FINST_D;
4629 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004630 return 0;
4631}
4632
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004633/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4634 * processing can continue on next analysers, or zero if it either needs more
4635 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4636 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4637 * when it has nothing left to do, and may remove any analyser when it wants to
4638 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004639 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004640int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004641{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004642 struct http_txn *txn = &s->txn;
4643 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004644 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004645 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004646 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004647 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004648
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004649 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 +02004650 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004651 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004652 rep,
4653 rep->rex, rep->wex,
4654 rep->flags,
4655 rep->l,
4656 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004657
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004658 /*
4659 * Now parse the partial (or complete) lines.
4660 * We will check the response syntax, and also join multi-line
4661 * headers. An index of all the lines will be elaborated while
4662 * parsing.
4663 *
4664 * For the parsing, we use a 28 states FSM.
4665 *
4666 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004667 * rep->data + msg->som = beginning of response
4668 * rep->data + msg->eoh = end of processed headers / start of current one
4669 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004670 * rep->lr = first non-visited byte
4671 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004672 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004673 */
4674
Willy Tarreau83e3af02009-12-28 17:39:57 +01004675 /* There's a protected area at the end of the buffer for rewriting
4676 * purposes. We don't want to start to parse the request if the
4677 * protected area is affected, because we may have to move processed
4678 * data later, which is much more complicated.
4679 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004680 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4681 if (unlikely((rep->flags & BF_FULL) ||
4682 rep->r < rep->lr ||
4683 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4684 if (rep->send_max) {
4685 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004686 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4687 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004688 buffer_dont_close(rep);
4689 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4690 return 0;
4691 }
4692 if (rep->l <= rep->size - global.tune.maxrewrite)
4693 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004694 }
4695
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004696 if (likely(rep->lr < rep->r))
4697 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004698 }
4699
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004700 /* 1: we might have to print this header in debug mode */
4701 if (unlikely((global.mode & MODE_DEBUG) &&
4702 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004703 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004704 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004705 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004706
Willy Tarreau663308b2010-06-07 14:06:08 +02004707 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004708 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004709 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004710
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004711 sol += hdr_idx_first_pos(&txn->hdr_idx);
4712 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004713
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004714 while (cur_idx) {
4715 eol = sol + txn->hdr_idx.v[cur_idx].len;
4716 debug_hdr("srvhdr", s, sol, eol);
4717 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4718 cur_idx = txn->hdr_idx.v[cur_idx].next;
4719 }
4720 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004721
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004722 /*
4723 * Now we quickly check if we have found a full valid response.
4724 * If not so, we check the FD and buffer states before leaving.
4725 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004726 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004727 * responses are checked first.
4728 *
4729 * Depending on whether the client is still there or not, we
4730 * may send an error response back or not. Note that normally
4731 * we should only check for HTTP status there, and check I/O
4732 * errors somewhere else.
4733 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004734
Willy Tarreau655dce92009-11-08 13:10:58 +01004735 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004736 /* Invalid response */
4737 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4738 /* we detected a parsing error. We want to archive this response
4739 * in the dedicated proxy area for later troubleshooting.
4740 */
4741 hdr_response_bad:
4742 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004743 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004744
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004745 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004746 if (target_srv(&s->target)) {
4747 target_srv(&s->target)->counters.failed_resp++;
4748 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004749 }
Willy Tarreau64648412010-03-05 10:41:54 +01004750 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004751 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004752 rep->analysers = 0;
4753 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004754 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004755 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004756 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4757
4758 if (!(s->flags & SN_ERR_MASK))
4759 s->flags |= SN_ERR_PRXCOND;
4760 if (!(s->flags & SN_FINST_MASK))
4761 s->flags |= SN_FINST_H;
4762
4763 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004764 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004765
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004766 /* too large response does not fit in buffer. */
4767 else if (rep->flags & BF_FULL) {
4768 goto hdr_response_bad;
4769 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004770
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004771 /* read error */
4772 else if (rep->flags & BF_READ_ERROR) {
4773 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004774 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004775
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004776 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004777 if (target_srv(&s->target)) {
4778 target_srv(&s->target)->counters.failed_resp++;
4779 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004780 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004781
Willy Tarreau90deb182010-01-07 00:20:41 +01004782 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004783 rep->analysers = 0;
4784 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004785 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004786 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004787 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004788
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004789 if (!(s->flags & SN_ERR_MASK))
4790 s->flags |= SN_ERR_SRVCL;
4791 if (!(s->flags & SN_FINST_MASK))
4792 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004793 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004794 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004795
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004796 /* read timeout : return a 504 to the client. */
4797 else if (rep->flags & BF_READ_TIMEOUT) {
4798 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004799 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004800
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004801 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004802 if (target_srv(&s->target)) {
4803 target_srv(&s->target)->counters.failed_resp++;
4804 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004805 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004806
Willy Tarreau90deb182010-01-07 00:20:41 +01004807 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004808 rep->analysers = 0;
4809 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004810 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004811 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004812 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004813
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004814 if (!(s->flags & SN_ERR_MASK))
4815 s->flags |= SN_ERR_SRVTO;
4816 if (!(s->flags & SN_FINST_MASK))
4817 s->flags |= SN_FINST_H;
4818 return 0;
4819 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004820
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004821 /* close from server */
4822 else if (rep->flags & BF_SHUTR) {
4823 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004824 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004825
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004826 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004827 if (target_srv(&s->target)) {
4828 target_srv(&s->target)->counters.failed_resp++;
4829 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004830 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004831
Willy Tarreau90deb182010-01-07 00:20:41 +01004832 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004833 rep->analysers = 0;
4834 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004835 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004836 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004837 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004838
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004839 if (!(s->flags & SN_ERR_MASK))
4840 s->flags |= SN_ERR_SRVCL;
4841 if (!(s->flags & SN_FINST_MASK))
4842 s->flags |= SN_FINST_H;
4843 return 0;
4844 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004845
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004846 /* write error to client (we don't send any message then) */
4847 else if (rep->flags & BF_WRITE_ERROR) {
4848 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004849 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004850
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004851 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004852 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004853 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004854
4855 if (!(s->flags & SN_ERR_MASK))
4856 s->flags |= SN_ERR_CLICL;
4857 if (!(s->flags & SN_FINST_MASK))
4858 s->flags |= SN_FINST_H;
4859
4860 /* process_session() will take care of the error */
4861 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004862 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004863
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004864 buffer_dont_close(rep);
4865 return 0;
4866 }
4867
4868 /* More interesting part now : we know that we have a complete
4869 * response which at least looks like HTTP. We have an indicator
4870 * of each header's length, so we can parse them quickly.
4871 */
4872
4873 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004874 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004875
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004876 /*
4877 * 1: get the status code
4878 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004879 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004880 if (n < 1 || n > 5)
4881 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004882 /* when the client triggers a 4xx from the server, it's most often due
4883 * to a missing object or permission. These events should be tracked
4884 * because if they happen often, it may indicate a brute force or a
4885 * vulnerability scan.
4886 */
4887 if (n == 4)
4888 session_inc_http_err_ctr(s);
4889
Willy Tarreau827aee92011-03-10 16:55:02 +01004890 if (target_srv(&s->target))
4891 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004892
Willy Tarreau5b154472009-12-21 20:11:07 +01004893 /* check if the response is HTTP/1.1 or above */
4894 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004895 ((msg->sol[5] > '1') ||
4896 ((msg->sol[5] == '1') &&
4897 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004898 txn->flags |= TX_RES_VER_11;
4899
4900 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004901 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 +01004902
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004903 /* transfer length unknown*/
4904 txn->flags &= ~TX_RES_XFER_LEN;
4905
Willy Tarreau962c3f42010-01-10 00:15:35 +01004906 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004907
Willy Tarreau39650402010-03-15 19:44:39 +01004908 /* Adjust server's health based on status code. Note: status codes 501
4909 * and 505 are triggered on demand by client request, so we must not
4910 * count them as server failures.
4911 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004912 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004913 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004914 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004915 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004916 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004917 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004918
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004919 /*
4920 * 2: check for cacheability.
4921 */
4922
4923 switch (txn->status) {
4924 case 200:
4925 case 203:
4926 case 206:
4927 case 300:
4928 case 301:
4929 case 410:
4930 /* RFC2616 @13.4:
4931 * "A response received with a status code of
4932 * 200, 203, 206, 300, 301 or 410 MAY be stored
4933 * by a cache (...) unless a cache-control
4934 * directive prohibits caching."
4935 *
4936 * RFC2616 @9.5: POST method :
4937 * "Responses to this method are not cacheable,
4938 * unless the response includes appropriate
4939 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004940 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004941 if (likely(txn->meth != HTTP_METH_POST) &&
4942 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4943 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4944 break;
4945 default:
4946 break;
4947 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004948
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004949 /*
4950 * 3: we may need to capture headers
4951 */
4952 s->logs.logwait &= ~LW_RESP;
4953 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004954 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004955 txn->rsp.cap, s->fe->rsp_cap);
4956
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004957 /* 4: determine the transfer-length.
4958 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4959 * the presence of a message-body in a RESPONSE and its transfer length
4960 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004961 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004962 * All responses to the HEAD request method MUST NOT include a
4963 * message-body, even though the presence of entity-header fields
4964 * might lead one to believe they do. All 1xx (informational), 204
4965 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4966 * message-body. All other responses do include a message-body,
4967 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004968 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004969 * 1. Any response which "MUST NOT" include a message-body (such as the
4970 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4971 * always terminated by the first empty line after the header fields,
4972 * regardless of the entity-header fields present in the message.
4973 *
4974 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4975 * the "chunked" transfer-coding (Section 6.2) is used, the
4976 * transfer-length is defined by the use of this transfer-coding.
4977 * If a Transfer-Encoding header field is present and the "chunked"
4978 * transfer-coding is not present, the transfer-length is defined by
4979 * the sender closing the connection.
4980 *
4981 * 3. If a Content-Length header field is present, its decimal value in
4982 * OCTETs represents both the entity-length and the transfer-length.
4983 * If a message is received with both a Transfer-Encoding header
4984 * field and a Content-Length header field, the latter MUST be ignored.
4985 *
4986 * 4. If the message uses the media type "multipart/byteranges", and
4987 * the transfer-length is not otherwise specified, then this self-
4988 * delimiting media type defines the transfer-length. This media
4989 * type MUST NOT be used unless the sender knows that the recipient
4990 * can parse it; the presence in a request of a Range header with
4991 * multiple byte-range specifiers from a 1.1 client implies that the
4992 * client can parse multipart/byteranges responses.
4993 *
4994 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004995 */
4996
4997 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004998 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004999 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005000 * FIXME: should we parse anyway and return an error on chunked encoding ?
5001 */
5002 if (txn->meth == HTTP_METH_HEAD ||
5003 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005004 txn->status == 204 || txn->status == 304) {
5005 txn->flags |= TX_RES_XFER_LEN;
5006 goto skip_content_length;
5007 }
5008
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005009 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005010 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01005011 while ((txn->flags & TX_RES_VER_11) &&
5012 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005013 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
5014 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5015 else if (txn->flags & TX_RES_TE_CHNK) {
5016 /* bad transfer-encoding (chunked followed by something else) */
5017 use_close_only = 1;
5018 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5019 break;
5020 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005021 }
5022
5023 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5024 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005025 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005026 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
5027 signed long long cl;
5028
5029 if (!ctx.vlen)
5030 goto hdr_response_bad;
5031
5032 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
5033 goto hdr_response_bad; /* parse failure */
5034
5035 if (cl < 0)
5036 goto hdr_response_bad;
5037
Willy Tarreau124d9912011-03-01 20:30:48 +01005038 if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl))
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005039 goto hdr_response_bad; /* already specified, was different */
5040
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005041 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005042 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005043 }
5044
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005045 /* FIXME: we should also implement the multipart/byterange method.
5046 * For now on, we resort to close mode in this case (unknown length).
5047 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005048skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005049
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005050 /* end of job, return OK */
5051 rep->analysers &= ~an_bit;
5052 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01005053 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005054 return 1;
5055}
5056
5057/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005058 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5059 * and updates t->rep->analysers. It might make sense to explode it into several
5060 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005061 */
5062int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
5063{
5064 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005065 struct http_msg *msg = &txn->rsp;
5066 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005067 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005068
5069 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
5070 now_ms, __FUNCTION__,
5071 t,
5072 rep,
5073 rep->rex, rep->wex,
5074 rep->flags,
5075 rep->l,
5076 rep->analysers);
5077
Willy Tarreau655dce92009-11-08 13:10:58 +01005078 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005079 return 0;
5080
5081 rep->analysers &= ~an_bit;
5082 rep->analyse_exp = TICK_ETERNITY;
5083
Willy Tarreau5b154472009-12-21 20:11:07 +01005084 /* Now we have to check if we need to modify the Connection header.
5085 * This is more difficult on the response than it is on the request,
5086 * because we can have two different HTTP versions and we don't know
5087 * how the client will interprete a response. For instance, let's say
5088 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5089 * HTTP/1.1 response without any header. Maybe it will bound itself to
5090 * HTTP/1.0 because it only knows about it, and will consider the lack
5091 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5092 * the lack of header as a keep-alive. Thus we will use two flags
5093 * indicating how a request MAY be understood by the client. In case
5094 * of multiple possibilities, we'll fix the header to be explicit. If
5095 * ambiguous cases such as both close and keepalive are seen, then we
5096 * will fall back to explicit close. Note that we won't take risks with
5097 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005098 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005099 */
5100
Willy Tarreaudc008c52010-02-01 16:20:08 +01005101 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5102 txn->status == 101)) {
5103 /* Either we've established an explicit tunnel, or we're
5104 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005105 * to understand the next protocols. We have to switch to tunnel
5106 * mode, so that we transfer the request and responses then let
5107 * this protocol pass unmodified. When we later implement specific
5108 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005109 * header which contains information about that protocol for
5110 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005111 */
5112 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5113 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005114 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5115 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5116 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005117 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005118
Willy Tarreau60466522010-01-18 19:08:45 +01005119 /* on unknown transfer length, we must close */
5120 if (!(txn->flags & TX_RES_XFER_LEN) &&
5121 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5122 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005123
Willy Tarreau60466522010-01-18 19:08:45 +01005124 /* now adjust header transformations depending on current state */
5125 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5126 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5127 to_del |= 2; /* remove "keep-alive" on any response */
5128 if (!(txn->flags & TX_RES_VER_11))
5129 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005130 }
Willy Tarreau60466522010-01-18 19:08:45 +01005131 else { /* SCL / KAL */
5132 to_del |= 1; /* remove "close" on any response */
5133 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
5134 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005135 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005136
Willy Tarreau60466522010-01-18 19:08:45 +01005137 /* Parse and remove some headers from the connection header */
5138 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005139
Willy Tarreau60466522010-01-18 19:08:45 +01005140 /* Some keep-alive responses are converted to Server-close if
5141 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005142 */
Willy Tarreau60466522010-01-18 19:08:45 +01005143 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5144 if ((txn->flags & TX_HDR_CONN_CLO) ||
5145 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
5146 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005147 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005148 }
5149
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005150 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005151 /*
5152 * 3: we will have to evaluate the filters.
5153 * As opposed to version 1.2, now they will be evaluated in the
5154 * filters order and not in the header order. This means that
5155 * each filter has to be validated among all headers.
5156 *
5157 * Filters are tried with ->be first, then with ->fe if it is
5158 * different from ->be.
5159 */
5160
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005161 cur_proxy = t->be;
5162 while (1) {
5163 struct proxy *rule_set = cur_proxy;
5164
5165 /* try headers filters */
5166 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005167 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005168 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005169 if (target_srv(&t->target)) {
5170 target_srv(&t->target)->counters.failed_resp++;
5171 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005172 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005173 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005174 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005175 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005176 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005177 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01005178 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005179 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005180 if (!(t->flags & SN_ERR_MASK))
5181 t->flags |= SN_ERR_PRXCOND;
5182 if (!(t->flags & SN_FINST_MASK))
5183 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005184 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005185 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005186 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005187
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005188 /* has the response been denied ? */
5189 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005190 if (target_srv(&t->target))
5191 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005192
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005193 t->be->be_counters.denied_resp++;
5194 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005195 if (t->listener->counters)
5196 t->listener->counters->denied_resp++;
5197
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005198 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005199 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005200
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005201 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005202 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005203 if (txn->status < 200)
5204 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005205 if (wl->cond) {
5206 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5207 ret = acl_pass(ret);
5208 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5209 ret = !ret;
5210 if (!ret)
5211 continue;
5212 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005213 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005214 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005215 }
5216
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005217 /* check whether we're already working on the frontend */
5218 if (cur_proxy == t->fe)
5219 break;
5220 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005221 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005222
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005223 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005224 * We may be facing a 100-continue response, in which case this
5225 * is not the right response, and we're waiting for the next one.
5226 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005227 * next one.
5228 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005229 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005230 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005231 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005232 msg->msg_state = HTTP_MSG_RPBEFORE;
5233 txn->status = 0;
5234 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5235 return 1;
5236 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005237 else if (unlikely(txn->status < 200))
5238 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005239
5240 /* we don't have any 1xx status code now */
5241
5242 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005243 * 4: check for server cookie.
5244 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005245 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5246 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005247 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005248
Willy Tarreaubaaee002006-06-26 02:48:02 +02005249
Willy Tarreaua15645d2007-03-18 16:22:39 +01005250 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005251 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005252 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005253 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005254 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005255
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005256 /*
5257 * 6: add server cookie in the response if needed
5258 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005259 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005260 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005261 (!(t->flags & SN_DIRECT) ||
5262 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5263 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5264 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5265 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005266 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5267 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005268 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005269 /* the server is known, it's not the one the client requested, or the
5270 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005271 * insert a set-cookie here, except if we want to insert only on POST
5272 * requests and this one isn't. Note that servers which don't have cookies
5273 * (eg: some backup servers) will return a full cookie removal request.
5274 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005275 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005276 len = sprintf(trash,
5277 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5278 t->be->cookie_name);
5279 }
5280 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005281 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005282
5283 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5284 /* emit last_date, which is mandatory */
5285 trash[len++] = COOKIE_DELIM_DATE;
5286 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5287 if (t->be->cookie_maxlife) {
5288 /* emit first_date, which is either the original one or
5289 * the current date.
5290 */
5291 trash[len++] = COOKIE_DELIM_DATE;
5292 s30tob64(txn->cookie_first_date ?
5293 txn->cookie_first_date >> 2 :
5294 (date.tv_sec+3) >> 2, trash + len);
5295 len += 5;
5296 }
5297 }
5298 len += sprintf(trash + len, "; path=/");
5299 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005300
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005301 if (t->be->cookie_domain)
5302 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005303
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005304 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005305 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005306 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005307
Willy Tarreauf1348312010-10-07 15:54:11 +02005308 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005309 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005310 /* the server did not change, only the date was updated */
5311 txn->flags |= TX_SCK_UPDATED;
5312 else
5313 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005314
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005315 /* Here, we will tell an eventual cache on the client side that we don't
5316 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5317 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5318 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5319 */
5320 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005321
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005322 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5323
5324 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005325 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005326 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005327 }
5328 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005329
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005330 /*
5331 * 7: check if result will be cacheable with a cookie.
5332 * We'll block the response if security checks have caught
5333 * nasty things such as a cacheable cookie.
5334 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005335 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5336 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005337 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005338
5339 /* we're in presence of a cacheable response containing
5340 * a set-cookie header. We'll block it as requested by
5341 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005342 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005343 if (target_srv(&t->target))
5344 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005345
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005346 t->be->be_counters.denied_resp++;
5347 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005348 if (t->listener->counters)
5349 t->listener->counters->denied_resp++;
5350
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005351 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005352 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005353 send_log(t->be, LOG_ALERT,
5354 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005355 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005356 goto return_srv_prx_502;
5357 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005358
5359 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005360 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005361 */
Willy Tarreau60466522010-01-18 19:08:45 +01005362 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5363 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5364 unsigned int want_flags = 0;
5365
5366 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5367 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5368 /* we want a keep-alive response here. Keep-alive header
5369 * required if either side is not 1.1.
5370 */
5371 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5372 want_flags |= TX_CON_KAL_SET;
5373 }
5374 else {
5375 /* we want a close response here. Close header required if
5376 * the server is 1.1, regardless of the client.
5377 */
5378 if (txn->flags & TX_RES_VER_11)
5379 want_flags |= TX_CON_CLO_SET;
5380 }
5381
5382 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5383 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005384 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005385
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005386 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005387 if ((txn->flags & TX_RES_XFER_LEN) ||
5388 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005389 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005390
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005391 /*************************************************************
5392 * OK, that's finished for the headers. We have done what we *
5393 * could. Let's switch to the DATA state. *
5394 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005395
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005396 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005397
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005398 /* if the user wants to log as soon as possible, without counting
5399 * bytes from the server, then this is the right moment. We have
5400 * to temporarily assign bytes_out to log what we currently have.
5401 */
5402 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5403 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5404 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005405 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005406 t->logs.bytes_out = 0;
5407 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005408
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005409 /* Note: we must not try to cheat by jumping directly to DATA,
5410 * otherwise we would not let the client side wake up.
5411 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005412
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005413 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005414 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005415 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005416}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005417
Willy Tarreaud98cf932009-12-27 22:54:55 +01005418/* This function is an analyser which forwards response body (including chunk
5419 * sizes if any). It is called as soon as we must forward, even if we forward
5420 * zero byte. The only situation where it must not be called is when we're in
5421 * tunnel mode and we want to forward till the close. It's used both to forward
5422 * remaining data and to resync after end of body. It expects the msg_state to
5423 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5424 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005425 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005426 * bytes of pending data + the headers if not already done (between som and sov).
5427 * It eventually adjusts som to match sov after the data in between have been sent.
5428 */
5429int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5430{
5431 struct http_txn *txn = &s->txn;
5432 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005433 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005434
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005435 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5436 return 0;
5437
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005438 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005439 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005440 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005441 /* Output closed while we were sending data. We must abort and
5442 * wake the other side up.
5443 */
5444 msg->msg_state = HTTP_MSG_ERROR;
5445 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005446 return 1;
5447 }
5448
Willy Tarreau4fe41902010-06-07 22:27:41 +02005449 /* in most states, we should abort in case of early close */
5450 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005451
Willy Tarreaud98cf932009-12-27 22:54:55 +01005452 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5453 /* we have msg->col and msg->sov which both point to the first
5454 * byte of message body. msg->som still points to the beginning
5455 * of the message. We must save the body in req->lr because it
5456 * survives buffer re-alignments.
5457 */
5458 res->lr = res->data + msg->sov;
5459 if (txn->flags & TX_RES_TE_CHNK)
5460 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5461 else {
5462 msg->msg_state = HTTP_MSG_DATA;
5463 }
5464 }
5465
Willy Tarreaud98cf932009-12-27 22:54:55 +01005466 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005467 int bytes;
5468
Willy Tarreau610ecce2010-01-04 21:15:02 +01005469 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005470 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005471 bytes = msg->sov - msg->som;
5472 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005473 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005474 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5475 bytes += res->size;
5476 msg->chunk_len += (unsigned int)bytes;
5477 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005478 }
5479
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005480
Willy Tarreaucaabe412010-01-03 23:08:28 +01005481 if (msg->msg_state == HTTP_MSG_DATA) {
5482 /* must still forward */
5483 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005484 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005485
5486 /* nothing left to forward */
5487 if (txn->flags & TX_RES_TE_CHNK)
5488 msg->msg_state = HTTP_MSG_DATA_CRLF;
5489 else
5490 msg->msg_state = HTTP_MSG_DONE;
5491 }
5492 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005493 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005494 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5495 */
5496 int ret = http_parse_chunk_size(res, msg);
5497
5498 if (!ret)
5499 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005500 else if (ret < 0) {
5501 if (msg->err_pos >= 0)
5502 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005503 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005504 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005505 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005506 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005507 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5508 /* we want the CRLF after the data */
5509 int ret;
5510
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005511 res->lr = res->w + res->send_max;
5512 if (res->lr >= res->data + res->size)
5513 res->lr -= res->size;
5514
Willy Tarreaud98cf932009-12-27 22:54:55 +01005515 ret = http_skip_chunk_crlf(res, msg);
5516
5517 if (!ret)
5518 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005519 else if (ret < 0) {
5520 if (msg->err_pos >= 0)
5521 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005522 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005523 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005524 /* we're in MSG_CHUNK_SIZE now */
5525 }
5526 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5527 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005528
Willy Tarreaud98cf932009-12-27 22:54:55 +01005529 if (ret == 0)
5530 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005531 else if (ret < 0) {
5532 if (msg->err_pos >= 0)
5533 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005534 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005535 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005536 /* we're in HTTP_MSG_DONE now */
5537 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005538 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005539 int old_state = msg->msg_state;
5540
Willy Tarreau610ecce2010-01-04 21:15:02 +01005541 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005542 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005543 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5544 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5545 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005546 if (http_resync_states(s)) {
5547 http_silent_debug(__LINE__, s);
5548 /* some state changes occurred, maybe the analyser
5549 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005550 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005551 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5552 if (res->flags & BF_SHUTW) {
5553 /* response errors are most likely due to
5554 * the client aborting the transfer.
5555 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005556 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005557 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005558 if (msg->err_pos >= 0)
5559 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005560 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005561 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005562 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005563 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005564 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005565 }
5566 }
5567
Willy Tarreaud98cf932009-12-27 22:54:55 +01005568 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005569 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005570 if (res->flags & BF_SHUTR) {
5571 if (!(s->flags & SN_ERR_MASK))
5572 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005573 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005574 if (target_srv(&s->target))
5575 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005576 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005577 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005578
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005579 if (res->flags & BF_SHUTW)
5580 goto aborted_xfer;
5581
Willy Tarreau40dba092010-03-04 18:14:51 +01005582 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005583 if (!s->req->analysers)
5584 goto return_bad_res;
5585
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005586 /* forward any pending data */
5587 bytes = msg->sov - msg->som;
5588 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005589 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005590 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5591 bytes += res->size;
5592 msg->chunk_len += (unsigned int)bytes;
5593 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005594 }
5595
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005596 /* When TE: chunked is used, we need to get there again to parse remaining
5597 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5598 * Similarly, with keep-alive on the client side, we don't want to forward a
5599 * close.
5600 */
5601 if ((txn->flags & TX_RES_TE_CHNK) ||
5602 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5603 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5604 buffer_dont_close(res);
5605
Willy Tarreau5c620922011-05-11 19:56:11 +02005606 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005607 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5608 * system knows it must not set a PUSH on this first part. Interactive
5609 * modes are already handled by the stream sock layer.
Willy Tarreau5c620922011-05-11 19:56:11 +02005610 */
Willy Tarreau07293032011-05-30 18:29:28 +02005611 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005612
Willy Tarreaud98cf932009-12-27 22:54:55 +01005613 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005614 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005615 return 0;
5616
Willy Tarreau40dba092010-03-04 18:14:51 +01005617 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005618 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005619 if (target_srv(&s->target))
5620 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005621
5622 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005623 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005624 /* don't send any error message as we're in the body */
5625 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005626 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005627 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005628 if (target_srv(&s->target))
5629 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005630
5631 if (!(s->flags & SN_ERR_MASK))
5632 s->flags |= SN_ERR_PRXCOND;
5633 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005634 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005635 return 0;
5636
5637 aborted_xfer:
5638 txn->rsp.msg_state = HTTP_MSG_ERROR;
5639 /* don't send any error message as we're in the body */
5640 stream_int_retnclose(res->cons, NULL);
5641 res->analysers = 0;
5642 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5643
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005644 s->fe->fe_counters.cli_aborts++;
5645 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005646 if (target_srv(&s->target))
5647 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005648
5649 if (!(s->flags & SN_ERR_MASK))
5650 s->flags |= SN_ERR_CLICL;
5651 if (!(s->flags & SN_FINST_MASK))
5652 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005653 return 0;
5654}
5655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005656/* Iterate the same filter through all request headers.
5657 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005658 * Since it can manage the switch to another backend, it updates the per-proxy
5659 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005660 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005661int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005662{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005663 char term;
5664 char *cur_ptr, *cur_end, *cur_next;
5665 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005666 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005667 struct hdr_idx_elem *cur_hdr;
5668 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005669
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005670 last_hdr = 0;
5671
Willy Tarreau962c3f42010-01-10 00:15:35 +01005672 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005673 old_idx = 0;
5674
5675 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005676 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005677 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005678 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005679 (exp->action == ACT_ALLOW ||
5680 exp->action == ACT_DENY ||
5681 exp->action == ACT_TARPIT))
5682 return 0;
5683
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005684 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005685 if (!cur_idx)
5686 break;
5687
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005688 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005689 cur_ptr = cur_next;
5690 cur_end = cur_ptr + cur_hdr->len;
5691 cur_next = cur_end + cur_hdr->cr + 1;
5692
5693 /* Now we have one header between cur_ptr and cur_end,
5694 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005695 */
5696
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005697 /* The annoying part is that pattern matching needs
5698 * that we modify the contents to null-terminate all
5699 * strings before testing them.
5700 */
5701
5702 term = *cur_end;
5703 *cur_end = '\0';
5704
5705 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5706 switch (exp->action) {
5707 case ACT_SETBE:
5708 /* It is not possible to jump a second time.
5709 * FIXME: should we return an HTTP/500 here so that
5710 * the admin knows there's a problem ?
5711 */
5712 if (t->be != t->fe)
5713 break;
5714
5715 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005716 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005717 last_hdr = 1;
5718 break;
5719
5720 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005721 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005722 last_hdr = 1;
5723 break;
5724
5725 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005726 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005727 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005728
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005729 t->fe->fe_counters.denied_req++;
5730 if (t->fe != t->be)
5731 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005732 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005733 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005734
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005735 break;
5736
5737 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005738 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005739 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005740
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005741 t->fe->fe_counters.denied_req++;
5742 if (t->fe != t->be)
5743 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005744 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005745 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005746
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005747 break;
5748
5749 case ACT_REPLACE:
5750 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5751 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5752 /* FIXME: if the user adds a newline in the replacement, the
5753 * index will not be recalculated for now, and the new line
5754 * will not be counted as a new header.
5755 */
5756
5757 cur_end += delta;
5758 cur_next += delta;
5759 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005760 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005761 break;
5762
5763 case ACT_REMOVE:
5764 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5765 cur_next += delta;
5766
Willy Tarreaufa355d42009-11-29 18:12:29 +01005767 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005768 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5769 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005770 cur_hdr->len = 0;
5771 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005772 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005773 break;
5774
5775 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005776 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005777 if (cur_end)
5778 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005779
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005780 /* keep the link from this header to next one in case of later
5781 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005782 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005783 old_idx = cur_idx;
5784 }
5785 return 0;
5786}
5787
5788
5789/* Apply the filter to the request line.
5790 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5791 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005792 * Since it can manage the switch to another backend, it updates the per-proxy
5793 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005794 */
5795int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5796{
5797 char term;
5798 char *cur_ptr, *cur_end;
5799 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005800 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005801 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005802
Willy Tarreau58f10d72006-12-04 02:26:12 +01005803
Willy Tarreau3d300592007-03-18 18:34:41 +01005804 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005805 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005806 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005807 (exp->action == ACT_ALLOW ||
5808 exp->action == ACT_DENY ||
5809 exp->action == ACT_TARPIT))
5810 return 0;
5811 else if (exp->action == ACT_REMOVE)
5812 return 0;
5813
5814 done = 0;
5815
Willy Tarreau962c3f42010-01-10 00:15:35 +01005816 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005817 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005818
5819 /* Now we have the request line between cur_ptr and cur_end */
5820
5821 /* The annoying part is that pattern matching needs
5822 * that we modify the contents to null-terminate all
5823 * strings before testing them.
5824 */
5825
5826 term = *cur_end;
5827 *cur_end = '\0';
5828
5829 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5830 switch (exp->action) {
5831 case ACT_SETBE:
5832 /* It is not possible to jump a second time.
5833 * FIXME: should we return an HTTP/500 here so that
5834 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005835 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005836 if (t->be != t->fe)
5837 break;
5838
5839 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005840 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005841 done = 1;
5842 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005843
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005844 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005845 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005846 done = 1;
5847 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005848
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005849 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005850 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005851
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005852 t->fe->fe_counters.denied_req++;
5853 if (t->fe != t->be)
5854 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005855 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005856 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005857
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005858 done = 1;
5859 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005860
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005861 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005862 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005863
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005864 t->fe->fe_counters.denied_req++;
5865 if (t->fe != t->be)
5866 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005867 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005868 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005869
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005870 done = 1;
5871 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005872
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005873 case ACT_REPLACE:
5874 *cur_end = term; /* restore the string terminator */
5875 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5876 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5877 /* FIXME: if the user adds a newline in the replacement, the
5878 * index will not be recalculated for now, and the new line
5879 * will not be counted as a new header.
5880 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005881
Willy Tarreaufa355d42009-11-29 18:12:29 +01005882 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005883 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005884 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005885 HTTP_MSG_RQMETH,
5886 cur_ptr, cur_end + 1,
5887 NULL, NULL);
5888 if (unlikely(!cur_end))
5889 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005890
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005891 /* we have a full request and we know that we have either a CR
5892 * or an LF at <ptr>.
5893 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005894 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5895 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005896 /* there is no point trying this regex on headers */
5897 return 1;
5898 }
5899 }
5900 *cur_end = term; /* restore the string terminator */
5901 return done;
5902}
Willy Tarreau97de6242006-12-27 17:18:38 +01005903
Willy Tarreau58f10d72006-12-04 02:26:12 +01005904
Willy Tarreau58f10d72006-12-04 02:26:12 +01005905
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005906/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005907 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005908 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005909 * unparsable request. Since it can manage the switch to another backend, it
5910 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005911 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005912int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005913{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005914 struct http_txn *txn = &s->txn;
5915 struct hdr_exp *exp;
5916
5917 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005918 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005919
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005920 /*
5921 * The interleaving of transformations and verdicts
5922 * makes it difficult to decide to continue or stop
5923 * the evaluation.
5924 */
5925
Willy Tarreau6c123b12010-01-28 20:22:06 +01005926 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5927 break;
5928
Willy Tarreau3d300592007-03-18 18:34:41 +01005929 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005930 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005931 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005932 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005933
5934 /* if this filter had a condition, evaluate it now and skip to
5935 * next filter if the condition does not match.
5936 */
5937 if (exp->cond) {
5938 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5939 ret = acl_pass(ret);
5940 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5941 ret = !ret;
5942
5943 if (!ret)
5944 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005945 }
5946
5947 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005948 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005949 if (unlikely(ret < 0))
5950 return -1;
5951
5952 if (likely(ret == 0)) {
5953 /* The filter did not match the request, it can be
5954 * iterated through all headers.
5955 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005956 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005957 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005958 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005959 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005960}
5961
5962
Willy Tarreaua15645d2007-03-18 16:22:39 +01005963
Willy Tarreau58f10d72006-12-04 02:26:12 +01005964/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005965 * Try to retrieve the server associated to the appsession.
5966 * If the server is found, it's assigned to the session.
5967 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005968void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005969 struct http_txn *txn = &t->txn;
5970 appsess *asession = NULL;
5971 char *sessid_temp = NULL;
5972
Cyril Bontéb21570a2009-11-29 20:04:48 +01005973 if (len > t->be->appsession_len) {
5974 len = t->be->appsession_len;
5975 }
5976
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005977 if (t->be->options2 & PR_O2_AS_REQL) {
5978 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005979 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005980 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005981 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005982 }
5983
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005984 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005985 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5986 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5987 return;
5988 }
5989
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005990 memcpy(txn->sessid, buf, len);
5991 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005992 }
5993
5994 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5995 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5996 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5997 return;
5998 }
5999
Cyril Bontéb21570a2009-11-29 20:04:48 +01006000 memcpy(sessid_temp, buf, len);
6001 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006002
6003 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6004 /* free previously allocated memory */
6005 pool_free2(apools.sessid, sessid_temp);
6006
6007 if (asession != NULL) {
6008 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6009 if (!(t->be->options2 & PR_O2_AS_REQL))
6010 asession->request_count++;
6011
6012 if (asession->serverid != NULL) {
6013 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006014
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006015 while (srv) {
6016 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006017 if ((srv->state & SRV_RUNNING) ||
6018 (t->be->options & PR_O_PERSIST) ||
6019 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006020 /* we found the server and it's usable */
6021 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006022 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006023 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006024 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01006025
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006026 break;
6027 } else {
6028 txn->flags &= ~TX_CK_MASK;
6029 txn->flags |= TX_CK_DOWN;
6030 }
6031 }
6032 srv = srv->next;
6033 }
6034 }
6035 }
6036}
6037
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006038/* Find the end of a cookie value contained between <s> and <e>. It works the
6039 * same way as with headers above except that the semi-colon also ends a token.
6040 * See RFC2965 for more information. Note that it requires a valid header to
6041 * return a valid result.
6042 */
6043char *find_cookie_value_end(char *s, const char *e)
6044{
6045 int quoted, qdpair;
6046
6047 quoted = qdpair = 0;
6048 for (; s < e; s++) {
6049 if (qdpair) qdpair = 0;
6050 else if (quoted) {
6051 if (*s == '\\') qdpair = 1;
6052 else if (*s == '"') quoted = 0;
6053 }
6054 else if (*s == '"') quoted = 1;
6055 else if (*s == ',' || *s == ';') return s;
6056 }
6057 return s;
6058}
6059
6060/* Delete a value in a header between delimiters <from> and <next> in buffer
6061 * <buf>. The number of characters displaced is returned, and the pointer to
6062 * the first delimiter is updated if required. The function tries as much as
6063 * possible to respect the following principles :
6064 * - replace <from> delimiter by the <next> one unless <from> points to a
6065 * colon, in which case <next> is simply removed
6066 * - set exactly one space character after the new first delimiter, unless
6067 * there are not enough characters in the block being moved to do so.
6068 * - remove unneeded spaces before the previous delimiter and after the new
6069 * one.
6070 *
6071 * It is the caller's responsibility to ensure that :
6072 * - <from> points to a valid delimiter or the colon ;
6073 * - <next> points to a valid delimiter or the final CR/LF ;
6074 * - there are non-space chars before <from> ;
6075 * - there is a CR/LF at or after <next>.
6076 */
6077int del_hdr_value(struct buffer *buf, char **from, char *next)
6078{
6079 char *prev = *from;
6080
6081 if (*prev == ':') {
6082 /* We're removing the first value, preserve the colon and add a
6083 * space if possible.
6084 */
6085 if (!http_is_crlf[(unsigned char)*next])
6086 next++;
6087 prev++;
6088 if (prev < next)
6089 *prev++ = ' ';
6090
6091 while (http_is_spht[(unsigned char)*next])
6092 next++;
6093 } else {
6094 /* Remove useless spaces before the old delimiter. */
6095 while (http_is_spht[(unsigned char)*(prev-1)])
6096 prev--;
6097 *from = prev;
6098
6099 /* copy the delimiter and if possible a space if we're
6100 * not at the end of the line.
6101 */
6102 if (!http_is_crlf[(unsigned char)*next]) {
6103 *prev++ = *next++;
6104 if (prev + 1 < next)
6105 *prev++ = ' ';
6106 while (http_is_spht[(unsigned char)*next])
6107 next++;
6108 }
6109 }
6110 return buffer_replace2(buf, prev, next, NULL, 0);
6111}
6112
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006113/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006114 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006115 * desirable to call it only when needed. This code is quite complex because
6116 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6117 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006118 */
6119void manage_client_side_cookies(struct session *t, struct buffer *req)
6120{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006121 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006122 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006123 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006124 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6125 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006126
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006127 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006128 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006129 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006130
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006131 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006132 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006133 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006134
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006135 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006136 hdr_beg = hdr_next;
6137 hdr_end = hdr_beg + cur_hdr->len;
6138 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006139
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006140 /* We have one full header between hdr_beg and hdr_end, and the
6141 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006142 * "Cookie:" headers.
6143 */
6144
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006145 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006146 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006147 old_idx = cur_idx;
6148 continue;
6149 }
6150
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006151 del_from = NULL; /* nothing to be deleted */
6152 preserve_hdr = 0; /* assume we may kill the whole header */
6153
Willy Tarreau58f10d72006-12-04 02:26:12 +01006154 /* Now look for cookies. Conforming to RFC2109, we have to support
6155 * attributes whose name begin with a '$', and associate them with
6156 * the right cookie, if we want to delete this cookie.
6157 * So there are 3 cases for each cookie read :
6158 * 1) it's a special attribute, beginning with a '$' : ignore it.
6159 * 2) it's a server id cookie that we *MAY* want to delete : save
6160 * some pointers on it (last semi-colon, beginning of cookie...)
6161 * 3) it's an application cookie : we *MAY* have to delete a previous
6162 * "special" cookie.
6163 * At the end of loop, if a "special" cookie remains, we may have to
6164 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006165 * *MUST* delete it.
6166 *
6167 * Note: RFC2965 is unclear about the processing of spaces around
6168 * the equal sign in the ATTR=VALUE form. A careful inspection of
6169 * the RFC explicitly allows spaces before it, and not within the
6170 * tokens (attrs or values). An inspection of RFC2109 allows that
6171 * too but section 10.1.3 lets one think that spaces may be allowed
6172 * after the equal sign too, resulting in some (rare) buggy
6173 * implementations trying to do that. So let's do what servers do.
6174 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6175 * allowed quoted strings in values, with any possible character
6176 * after a backslash, including control chars and delimitors, which
6177 * causes parsing to become ambiguous. Browsers also allow spaces
6178 * within values even without quotes.
6179 *
6180 * We have to keep multiple pointers in order to support cookie
6181 * removal at the beginning, middle or end of header without
6182 * corrupting the header. All of these headers are valid :
6183 *
6184 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6185 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6186 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6187 * | | | | | | | | |
6188 * | | | | | | | | hdr_end <--+
6189 * | | | | | | | +--> next
6190 * | | | | | | +----> val_end
6191 * | | | | | +-----------> val_beg
6192 * | | | | +--------------> equal
6193 * | | | +----------------> att_end
6194 * | | +---------------------> att_beg
6195 * | +--------------------------> prev
6196 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006197 */
6198
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006199 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6200 /* Iterate through all cookies on this line */
6201
6202 /* find att_beg */
6203 att_beg = prev + 1;
6204 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6205 att_beg++;
6206
6207 /* find att_end : this is the first character after the last non
6208 * space before the equal. It may be equal to hdr_end.
6209 */
6210 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006211
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006212 while (equal < hdr_end) {
6213 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006214 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006215 if (http_is_spht[(unsigned char)*equal++])
6216 continue;
6217 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006218 }
6219
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006220 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6221 * is between <att_beg> and <equal>, both may be identical.
6222 */
6223
6224 /* look for end of cookie if there is an equal sign */
6225 if (equal < hdr_end && *equal == '=') {
6226 /* look for the beginning of the value */
6227 val_beg = equal + 1;
6228 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6229 val_beg++;
6230
6231 /* find the end of the value, respecting quotes */
6232 next = find_cookie_value_end(val_beg, hdr_end);
6233
6234 /* make val_end point to the first white space or delimitor after the value */
6235 val_end = next;
6236 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6237 val_end--;
6238 } else {
6239 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006240 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006241
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006242 /* We have nothing to do with attributes beginning with '$'. However,
6243 * they will automatically be removed if a header before them is removed,
6244 * since they're supposed to be linked together.
6245 */
6246 if (*att_beg == '$')
6247 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006248
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006249 /* Ignore cookies with no equal sign */
6250 if (equal == next) {
6251 /* This is not our cookie, so we must preserve it. But if we already
6252 * scheduled another cookie for removal, we cannot remove the
6253 * complete header, but we can remove the previous block itself.
6254 */
6255 preserve_hdr = 1;
6256 if (del_from != NULL) {
6257 int delta = del_hdr_value(req, &del_from, prev);
6258 val_end += delta;
6259 next += delta;
6260 hdr_end += delta;
6261 hdr_next += delta;
6262 cur_hdr->len += delta;
6263 http_msg_move_end(&txn->req, delta);
6264 prev = del_from;
6265 del_from = NULL;
6266 }
6267 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006268 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006269
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006270 /* if there are spaces around the equal sign, we need to
6271 * strip them otherwise we'll get trouble for cookie captures,
6272 * or even for rewrites. Since this happens extremely rarely,
6273 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006274 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006275 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6276 int stripped_before = 0;
6277 int stripped_after = 0;
6278
6279 if (att_end != equal) {
6280 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6281 equal += stripped_before;
6282 val_beg += stripped_before;
6283 }
6284
6285 if (val_beg > equal + 1) {
6286 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6287 val_beg += stripped_after;
6288 stripped_before += stripped_after;
6289 }
6290
6291 val_end += stripped_before;
6292 next += stripped_before;
6293 hdr_end += stripped_before;
6294 hdr_next += stripped_before;
6295 cur_hdr->len += stripped_before;
6296 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006297 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006298 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006299
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006300 /* First, let's see if we want to capture this cookie. We check
6301 * that we don't already have a client side cookie, because we
6302 * can only capture one. Also as an optimisation, we ignore
6303 * cookies shorter than the declared name.
6304 */
6305 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6306 (val_end - att_beg >= t->fe->capture_namelen) &&
6307 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6308 int log_len = val_end - att_beg;
6309
6310 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6311 Alert("HTTP logging : out of memory.\n");
6312 } else {
6313 if (log_len > t->fe->capture_len)
6314 log_len = t->fe->capture_len;
6315 memcpy(txn->cli_cookie, att_beg, log_len);
6316 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006317 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006318 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006319
Willy Tarreaubca99692010-10-06 19:25:55 +02006320 /* Persistence cookies in passive, rewrite or insert mode have the
6321 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006322 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006323 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006324 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006325 * For cookies in prefix mode, the form is :
6326 *
6327 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006328 */
6329 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6330 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6331 struct server *srv = t->be->srv;
6332 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006333
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006334 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6335 * have the server ID between val_beg and delim, and the original cookie between
6336 * delim+1 and val_end. Otherwise, delim==val_end :
6337 *
6338 * Cookie: NAME=SRV; # in all but prefix modes
6339 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6340 * | || || | |+-> next
6341 * | || || | +--> val_end
6342 * | || || +---------> delim
6343 * | || |+------------> val_beg
6344 * | || +-------------> att_end = equal
6345 * | |+-----------------> att_beg
6346 * | +------------------> prev
6347 * +-------------------------> hdr_beg
6348 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006349
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006350 if (t->be->options & PR_O_COOK_PFX) {
6351 for (delim = val_beg; delim < val_end; delim++)
6352 if (*delim == COOKIE_DELIM)
6353 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006354 } else {
6355 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006356 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006357 /* Now check if the cookie contains a date field, which would
6358 * appear after a vertical bar ('|') just after the server name
6359 * and before the delimiter.
6360 */
6361 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6362 if (vbar1) {
6363 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006364 * right is the last seen date. It is a base64 encoded
6365 * 30-bit value representing the UNIX date since the
6366 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006367 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006368 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006369 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006370 if (val_end - vbar1 >= 5) {
6371 val = b64tos30(vbar1);
6372 if (val > 0)
6373 txn->cookie_last_date = val << 2;
6374 }
6375 /* look for a second vertical bar */
6376 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6377 if (vbar1 && (val_end - vbar1 > 5)) {
6378 val = b64tos30(vbar1 + 1);
6379 if (val > 0)
6380 txn->cookie_first_date = val << 2;
6381 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006382 }
6383 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006384
Willy Tarreauf64d1412010-10-07 20:06:11 +02006385 /* if the cookie has an expiration date and the proxy wants to check
6386 * it, then we do that now. We first check if the cookie is too old,
6387 * then only if it has expired. We detect strict overflow because the
6388 * time resolution here is not great (4 seconds). Cookies with dates
6389 * in the future are ignored if their offset is beyond one day. This
6390 * allows an admin to fix timezone issues without expiring everyone
6391 * and at the same time avoids keeping unwanted side effects for too
6392 * long.
6393 */
6394 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006395 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6396 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006397 txn->flags &= ~TX_CK_MASK;
6398 txn->flags |= TX_CK_OLD;
6399 delim = val_beg; // let's pretend we have not found the cookie
6400 txn->cookie_first_date = 0;
6401 txn->cookie_last_date = 0;
6402 }
6403 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006404 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6405 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006406 txn->flags &= ~TX_CK_MASK;
6407 txn->flags |= TX_CK_EXPIRED;
6408 delim = val_beg; // let's pretend we have not found the cookie
6409 txn->cookie_first_date = 0;
6410 txn->cookie_last_date = 0;
6411 }
6412
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006413 /* Here, we'll look for the first running server which supports the cookie.
6414 * This allows to share a same cookie between several servers, for example
6415 * to dedicate backup servers to specific servers only.
6416 * However, to prevent clients from sticking to cookie-less backup server
6417 * when they have incidentely learned an empty cookie, we simply ignore
6418 * empty cookies and mark them as invalid.
6419 * The same behaviour is applied when persistence must be ignored.
6420 */
6421 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6422 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006423
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006424 while (srv) {
6425 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6426 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6427 if ((srv->state & SRV_RUNNING) ||
6428 (t->be->options & PR_O_PERSIST) ||
6429 (t->flags & SN_FORCE_PRST)) {
6430 /* we found the server and we can use it */
6431 txn->flags &= ~TX_CK_MASK;
6432 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6433 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006434 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006435 break;
6436 } else {
6437 /* we found a server, but it's down,
6438 * mark it as such and go on in case
6439 * another one is available.
6440 */
6441 txn->flags &= ~TX_CK_MASK;
6442 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006443 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006444 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006445 srv = srv->next;
6446 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006447
Willy Tarreauf64d1412010-10-07 20:06:11 +02006448 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006449 /* no server matched this cookie */
6450 txn->flags &= ~TX_CK_MASK;
6451 txn->flags |= TX_CK_INVALID;
6452 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006453
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006454 /* depending on the cookie mode, we may have to either :
6455 * - delete the complete cookie if we're in insert+indirect mode, so that
6456 * the server never sees it ;
6457 * - remove the server id from the cookie value, and tag the cookie as an
6458 * application cookie so that it does not get accidentely removed later,
6459 * if we're in cookie prefix mode
6460 */
6461 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6462 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006463
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006464 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6465 val_end += delta;
6466 next += delta;
6467 hdr_end += delta;
6468 hdr_next += delta;
6469 cur_hdr->len += delta;
6470 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006471
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006472 del_from = NULL;
6473 preserve_hdr = 1; /* we want to keep this cookie */
6474 }
6475 else if (del_from == NULL &&
6476 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6477 del_from = prev;
6478 }
6479 } else {
6480 /* This is not our cookie, so we must preserve it. But if we already
6481 * scheduled another cookie for removal, we cannot remove the
6482 * complete header, but we can remove the previous block itself.
6483 */
6484 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006485
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006486 if (del_from != NULL) {
6487 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006488 if (att_beg >= del_from)
6489 att_beg += delta;
6490 if (att_end >= del_from)
6491 att_end += delta;
6492 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006493 val_end += delta;
6494 next += delta;
6495 hdr_end += delta;
6496 hdr_next += delta;
6497 cur_hdr->len += delta;
6498 http_msg_move_end(&txn->req, delta);
6499 prev = del_from;
6500 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006501 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006502 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006503
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006504 /* Look for the appsession cookie unless persistence must be ignored */
6505 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6506 int cmp_len, value_len;
6507 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006508
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006509 if (t->be->options2 & PR_O2_AS_PFX) {
6510 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6511 value_begin = att_beg + t->be->appsession_name_len;
6512 value_len = val_end - att_beg - t->be->appsession_name_len;
6513 } else {
6514 cmp_len = att_end - att_beg;
6515 value_begin = val_beg;
6516 value_len = val_end - val_beg;
6517 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006518
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006519 /* let's see if the cookie is our appcookie */
6520 if (cmp_len == t->be->appsession_name_len &&
6521 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6522 manage_client_side_appsession(t, value_begin, value_len);
6523 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006524 }
6525
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006526 /* continue with next cookie on this header line */
6527 att_beg = next;
6528 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006529
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006530 /* There are no more cookies on this line.
6531 * We may still have one (or several) marked for deletion at the
6532 * end of the line. We must do this now in two ways :
6533 * - if some cookies must be preserved, we only delete from the
6534 * mark to the end of line ;
6535 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006536 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006537 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006538 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006539 if (preserve_hdr) {
6540 delta = del_hdr_value(req, &del_from, hdr_end);
6541 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006542 cur_hdr->len += delta;
6543 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006544 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006545
6546 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006547 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6548 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006549 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006550 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006551 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006552 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006553 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006554 }
6555
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006556 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006557 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006558 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006559}
6560
6561
Willy Tarreaua15645d2007-03-18 16:22:39 +01006562/* Iterate the same filter through all response headers contained in <rtr>.
6563 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6564 */
6565int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6566{
6567 char term;
6568 char *cur_ptr, *cur_end, *cur_next;
6569 int cur_idx, old_idx, last_hdr;
6570 struct http_txn *txn = &t->txn;
6571 struct hdr_idx_elem *cur_hdr;
6572 int len, delta;
6573
6574 last_hdr = 0;
6575
Willy Tarreau962c3f42010-01-10 00:15:35 +01006576 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006577 old_idx = 0;
6578
6579 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006580 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006581 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006582 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006583 (exp->action == ACT_ALLOW ||
6584 exp->action == ACT_DENY))
6585 return 0;
6586
6587 cur_idx = txn->hdr_idx.v[old_idx].next;
6588 if (!cur_idx)
6589 break;
6590
6591 cur_hdr = &txn->hdr_idx.v[cur_idx];
6592 cur_ptr = cur_next;
6593 cur_end = cur_ptr + cur_hdr->len;
6594 cur_next = cur_end + cur_hdr->cr + 1;
6595
6596 /* Now we have one header between cur_ptr and cur_end,
6597 * and the next header starts at cur_next.
6598 */
6599
6600 /* The annoying part is that pattern matching needs
6601 * that we modify the contents to null-terminate all
6602 * strings before testing them.
6603 */
6604
6605 term = *cur_end;
6606 *cur_end = '\0';
6607
6608 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6609 switch (exp->action) {
6610 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006611 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006612 last_hdr = 1;
6613 break;
6614
6615 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006616 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006617 last_hdr = 1;
6618 break;
6619
6620 case ACT_REPLACE:
6621 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6622 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6623 /* FIXME: if the user adds a newline in the replacement, the
6624 * index will not be recalculated for now, and the new line
6625 * will not be counted as a new header.
6626 */
6627
6628 cur_end += delta;
6629 cur_next += delta;
6630 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006631 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006632 break;
6633
6634 case ACT_REMOVE:
6635 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6636 cur_next += delta;
6637
Willy Tarreaufa355d42009-11-29 18:12:29 +01006638 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006639 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6640 txn->hdr_idx.used--;
6641 cur_hdr->len = 0;
6642 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006643 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006644 break;
6645
6646 }
6647 }
6648 if (cur_end)
6649 *cur_end = term; /* restore the string terminator */
6650
6651 /* keep the link from this header to next one in case of later
6652 * removal of next header.
6653 */
6654 old_idx = cur_idx;
6655 }
6656 return 0;
6657}
6658
6659
6660/* Apply the filter to the status line in the response buffer <rtr>.
6661 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6662 * or -1 if a replacement resulted in an invalid status line.
6663 */
6664int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6665{
6666 char term;
6667 char *cur_ptr, *cur_end;
6668 int done;
6669 struct http_txn *txn = &t->txn;
6670 int len, delta;
6671
6672
Willy Tarreau3d300592007-03-18 18:34:41 +01006673 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006674 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006675 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006676 (exp->action == ACT_ALLOW ||
6677 exp->action == ACT_DENY))
6678 return 0;
6679 else if (exp->action == ACT_REMOVE)
6680 return 0;
6681
6682 done = 0;
6683
Willy Tarreau962c3f42010-01-10 00:15:35 +01006684 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006685 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006686
6687 /* Now we have the status line between cur_ptr and cur_end */
6688
6689 /* The annoying part is that pattern matching needs
6690 * that we modify the contents to null-terminate all
6691 * strings before testing them.
6692 */
6693
6694 term = *cur_end;
6695 *cur_end = '\0';
6696
6697 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6698 switch (exp->action) {
6699 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006700 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006701 done = 1;
6702 break;
6703
6704 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006705 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006706 done = 1;
6707 break;
6708
6709 case ACT_REPLACE:
6710 *cur_end = term; /* restore the string terminator */
6711 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6712 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6713 /* FIXME: if the user adds a newline in the replacement, the
6714 * index will not be recalculated for now, and the new line
6715 * will not be counted as a new header.
6716 */
6717
Willy Tarreaufa355d42009-11-29 18:12:29 +01006718 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006719 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006720 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006721 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006722 cur_ptr, cur_end + 1,
6723 NULL, NULL);
6724 if (unlikely(!cur_end))
6725 return -1;
6726
6727 /* we have a full respnse and we know that we have either a CR
6728 * or an LF at <ptr>.
6729 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006730 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006731 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006732 /* there is no point trying this regex on headers */
6733 return 1;
6734 }
6735 }
6736 *cur_end = term; /* restore the string terminator */
6737 return done;
6738}
6739
6740
6741
6742/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006743 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006744 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6745 * unparsable response.
6746 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006747int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006748{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006749 struct http_txn *txn = &s->txn;
6750 struct hdr_exp *exp;
6751
6752 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006753 int ret;
6754
6755 /*
6756 * The interleaving of transformations and verdicts
6757 * makes it difficult to decide to continue or stop
6758 * the evaluation.
6759 */
6760
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006761 if (txn->flags & TX_SVDENY)
6762 break;
6763
Willy Tarreau3d300592007-03-18 18:34:41 +01006764 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006765 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6766 exp->action == ACT_PASS)) {
6767 exp = exp->next;
6768 continue;
6769 }
6770
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006771 /* if this filter had a condition, evaluate it now and skip to
6772 * next filter if the condition does not match.
6773 */
6774 if (exp->cond) {
6775 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6776 ret = acl_pass(ret);
6777 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6778 ret = !ret;
6779 if (!ret)
6780 continue;
6781 }
6782
Willy Tarreaua15645d2007-03-18 16:22:39 +01006783 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006784 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006785 if (unlikely(ret < 0))
6786 return -1;
6787
6788 if (likely(ret == 0)) {
6789 /* The filter did not match the response, it can be
6790 * iterated through all headers.
6791 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006792 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006793 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006794 }
6795 return 0;
6796}
6797
6798
Willy Tarreaua15645d2007-03-18 16:22:39 +01006799/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006800 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006801 * desirable to call it only when needed. This function is also used when we
6802 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006804void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006805{
6806 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006807 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006808 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006809 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006810 char *hdr_beg, *hdr_end, *hdr_next;
6811 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006812
Willy Tarreaua15645d2007-03-18 16:22:39 +01006813 /* Iterate through the headers.
6814 * we start with the start line.
6815 */
6816 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006817 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006818
6819 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6820 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006821 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006822
6823 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006824 hdr_beg = hdr_next;
6825 hdr_end = hdr_beg + cur_hdr->len;
6826 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006827
Willy Tarreau24581ba2010-08-31 22:39:35 +02006828 /* We have one full header between hdr_beg and hdr_end, and the
6829 * next header starts at hdr_next. We're only interested in
6830 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006831 */
6832
Willy Tarreau24581ba2010-08-31 22:39:35 +02006833 is_cookie2 = 0;
6834 prev = hdr_beg + 10;
6835 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006836 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006837 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6838 if (!val) {
6839 old_idx = cur_idx;
6840 continue;
6841 }
6842 is_cookie2 = 1;
6843 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006844 }
6845
Willy Tarreau24581ba2010-08-31 22:39:35 +02006846 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6847 * <prev> points to the colon.
6848 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006849 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006850
Willy Tarreau24581ba2010-08-31 22:39:35 +02006851 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6852 * check-cache is enabled) and we are not interested in checking
6853 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006854 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006855 if (t->be->cookie_name == NULL &&
6856 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006857 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006858 return;
6859
Willy Tarreau24581ba2010-08-31 22:39:35 +02006860 /* OK so now we know we have to process this response cookie.
6861 * The format of the Set-Cookie header is slightly different
6862 * from the format of the Cookie header in that it does not
6863 * support the comma as a cookie delimiter (thus the header
6864 * cannot be folded) because the Expires attribute described in
6865 * the original Netscape's spec may contain an unquoted date
6866 * with a comma inside. We have to live with this because
6867 * many browsers don't support Max-Age and some browsers don't
6868 * support quoted strings. However the Set-Cookie2 header is
6869 * clean.
6870 *
6871 * We have to keep multiple pointers in order to support cookie
6872 * removal at the beginning, middle or end of header without
6873 * corrupting the header (in case of set-cookie2). A special
6874 * pointer, <scav> points to the beginning of the set-cookie-av
6875 * fields after the first semi-colon. The <next> pointer points
6876 * either to the end of line (set-cookie) or next unquoted comma
6877 * (set-cookie2). All of these headers are valid :
6878 *
6879 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6880 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6881 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6882 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6883 * | | | | | | | | | |
6884 * | | | | | | | | +-> next hdr_end <--+
6885 * | | | | | | | +------------> scav
6886 * | | | | | | +--------------> val_end
6887 * | | | | | +--------------------> val_beg
6888 * | | | | +----------------------> equal
6889 * | | | +------------------------> att_end
6890 * | | +----------------------------> att_beg
6891 * | +------------------------------> prev
6892 * +-----------------------------------------> hdr_beg
6893 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006894
Willy Tarreau24581ba2010-08-31 22:39:35 +02006895 for (; prev < hdr_end; prev = next) {
6896 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006897
Willy Tarreau24581ba2010-08-31 22:39:35 +02006898 /* find att_beg */
6899 att_beg = prev + 1;
6900 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6901 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006902
Willy Tarreau24581ba2010-08-31 22:39:35 +02006903 /* find att_end : this is the first character after the last non
6904 * space before the equal. It may be equal to hdr_end.
6905 */
6906 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006907
Willy Tarreau24581ba2010-08-31 22:39:35 +02006908 while (equal < hdr_end) {
6909 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6910 break;
6911 if (http_is_spht[(unsigned char)*equal++])
6912 continue;
6913 att_end = equal;
6914 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006915
Willy Tarreau24581ba2010-08-31 22:39:35 +02006916 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6917 * is between <att_beg> and <equal>, both may be identical.
6918 */
6919
6920 /* look for end of cookie if there is an equal sign */
6921 if (equal < hdr_end && *equal == '=') {
6922 /* look for the beginning of the value */
6923 val_beg = equal + 1;
6924 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6925 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006926
Willy Tarreau24581ba2010-08-31 22:39:35 +02006927 /* find the end of the value, respecting quotes */
6928 next = find_cookie_value_end(val_beg, hdr_end);
6929
6930 /* make val_end point to the first white space or delimitor after the value */
6931 val_end = next;
6932 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6933 val_end--;
6934 } else {
6935 /* <equal> points to next comma, semi-colon or EOL */
6936 val_beg = val_end = next = equal;
6937 }
6938
6939 if (next < hdr_end) {
6940 /* Set-Cookie2 supports multiple cookies, and <next> points to
6941 * a colon or semi-colon before the end. So skip all attr-value
6942 * pairs and look for the next comma. For Set-Cookie, since
6943 * commas are permitted in values, skip to the end.
6944 */
6945 if (is_cookie2)
6946 next = find_hdr_value_end(next, hdr_end);
6947 else
6948 next = hdr_end;
6949 }
6950
6951 /* Now everything is as on the diagram above */
6952
6953 /* Ignore cookies with no equal sign */
6954 if (equal == val_end)
6955 continue;
6956
6957 /* If there are spaces around the equal sign, we need to
6958 * strip them otherwise we'll get trouble for cookie captures,
6959 * or even for rewrites. Since this happens extremely rarely,
6960 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006961 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006962 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6963 int stripped_before = 0;
6964 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006965
Willy Tarreau24581ba2010-08-31 22:39:35 +02006966 if (att_end != equal) {
6967 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6968 equal += stripped_before;
6969 val_beg += stripped_before;
6970 }
6971
6972 if (val_beg > equal + 1) {
6973 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6974 val_beg += stripped_after;
6975 stripped_before += stripped_after;
6976 }
6977
6978 val_end += stripped_before;
6979 next += stripped_before;
6980 hdr_end += stripped_before;
6981 hdr_next += stripped_before;
6982 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006983 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006984 }
6985
6986 /* First, let's see if we want to capture this cookie. We check
6987 * that we don't already have a server side cookie, because we
6988 * can only capture one. Also as an optimisation, we ignore
6989 * cookies shorter than the declared name.
6990 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006991 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006992 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006993 (val_end - att_beg >= t->fe->capture_namelen) &&
6994 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6995 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006996 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006997 Alert("HTTP logging : out of memory.\n");
6998 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006999 else {
7000 if (log_len > t->fe->capture_len)
7001 log_len = t->fe->capture_len;
7002 memcpy(txn->srv_cookie, att_beg, log_len);
7003 txn->srv_cookie[log_len] = 0;
7004 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007005 }
7006
Willy Tarreau827aee92011-03-10 16:55:02 +01007007 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007008 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007009 if (!(t->flags & SN_IGNORE_PRST) &&
7010 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7011 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007012 /* assume passive cookie by default */
7013 txn->flags &= ~TX_SCK_MASK;
7014 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007015
7016 /* If the cookie is in insert mode on a known server, we'll delete
7017 * this occurrence because we'll insert another one later.
7018 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007019 * a direct access.
7020 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007021 if (t->be->options2 & PR_O2_COOK_PSV) {
7022 /* The "preserve" flag was set, we don't want to touch the
7023 * server's cookie.
7024 */
7025 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007026 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007027 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007028 /* this cookie must be deleted */
7029 if (*prev == ':' && next == hdr_end) {
7030 /* whole header */
7031 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
7032 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7033 txn->hdr_idx.used--;
7034 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007035 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007036 hdr_next += delta;
7037 http_msg_move_end(&txn->rsp, delta);
7038 /* note: while both invalid now, <next> and <hdr_end>
7039 * are still equal, so the for() will stop as expected.
7040 */
7041 } else {
7042 /* just remove the value */
7043 int delta = del_hdr_value(res, &prev, next);
7044 next = prev;
7045 hdr_end += delta;
7046 hdr_next += delta;
7047 cur_hdr->len += delta;
7048 http_msg_move_end(&txn->rsp, delta);
7049 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007050 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007051 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007052 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007053 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007054 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007055 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007056 * with this server since we know it.
7057 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007058 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007059 next += delta;
7060 hdr_end += delta;
7061 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007062 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007063 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007064
Willy Tarreauf1348312010-10-07 15:54:11 +02007065 txn->flags &= ~TX_SCK_MASK;
7066 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007067 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007068 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007069 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007070 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007071 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007072 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007073 next += delta;
7074 hdr_end += delta;
7075 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007076 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007077 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007078
Willy Tarreau827aee92011-03-10 16:55:02 +01007079 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007080 txn->flags &= ~TX_SCK_MASK;
7081 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007082 }
7083 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007084 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7085 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007086 int cmp_len, value_len;
7087 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007088
Cyril Bontéb21570a2009-11-29 20:04:48 +01007089 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007090 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7091 value_begin = att_beg + t->be->appsession_name_len;
7092 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007093 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007094 cmp_len = att_end - att_beg;
7095 value_begin = val_beg;
7096 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007097 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007098
Cyril Bonté17530c32010-04-06 21:11:10 +02007099 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007100 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7101 /* free a possibly previously allocated memory */
7102 pool_free2(apools.sessid, txn->sessid);
7103
Cyril Bontéb21570a2009-11-29 20:04:48 +01007104 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007105 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007106 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7107 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7108 return;
7109 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007110 memcpy(txn->sessid, value_begin, value_len);
7111 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007112 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007113 }
7114 /* that's done for this cookie, check the next one on the same
7115 * line when next != hdr_end (only if is_cookie2).
7116 */
7117 }
7118 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007119 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007120 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007121
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007122 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007123 appsess *asession = NULL;
7124 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007125 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007126 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007127 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007128 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7129 Alert("Not enough Memory process_srv():asession:calloc().\n");
7130 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7131 return;
7132 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007133 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7134
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007135 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7136 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7137 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007138 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007139 return;
7140 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007141 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007142 asession->sessid[t->be->appsession_len] = 0;
7143
Willy Tarreau827aee92011-03-10 16:55:02 +01007144 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007145 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007146 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007147 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007148 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007149 return;
7150 }
7151 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01007152 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007153
7154 asession->request_count = 0;
7155 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7156 }
7157
7158 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7159 asession->request_count++;
7160 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007161}
7162
7163
Willy Tarreaua15645d2007-03-18 16:22:39 +01007164/*
7165 * Check if response is cacheable or not. Updates t->flags.
7166 */
7167void check_response_for_cacheability(struct session *t, struct buffer *rtr)
7168{
7169 struct http_txn *txn = &t->txn;
7170 char *p1, *p2;
7171
7172 char *cur_ptr, *cur_end, *cur_next;
7173 int cur_idx;
7174
Willy Tarreau5df51872007-11-25 16:20:08 +01007175 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176 return;
7177
7178 /* Iterate through the headers.
7179 * we start with the start line.
7180 */
7181 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007182 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007183
7184 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7185 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007186 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007187
7188 cur_hdr = &txn->hdr_idx.v[cur_idx];
7189 cur_ptr = cur_next;
7190 cur_end = cur_ptr + cur_hdr->len;
7191 cur_next = cur_end + cur_hdr->cr + 1;
7192
7193 /* We have one full header between cur_ptr and cur_end, and the
7194 * next header starts at cur_next. We're only interested in
7195 * "Cookie:" headers.
7196 */
7197
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007198 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7199 if (val) {
7200 if ((cur_end - (cur_ptr + val) >= 8) &&
7201 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7202 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7203 return;
7204 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007205 }
7206
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007207 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7208 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007209 continue;
7210
7211 /* OK, right now we know we have a cache-control header at cur_ptr */
7212
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007213 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007214
7215 if (p1 >= cur_end) /* no more info */
7216 continue;
7217
7218 /* p1 is at the beginning of the value */
7219 p2 = p1;
7220
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007221 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007222 p2++;
7223
7224 /* we have a complete value between p1 and p2 */
7225 if (p2 < cur_end && *p2 == '=') {
7226 /* we have something of the form no-cache="set-cookie" */
7227 if ((cur_end - p1 >= 21) &&
7228 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7229 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007230 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007231 continue;
7232 }
7233
7234 /* OK, so we know that either p2 points to the end of string or to a comma */
7235 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7236 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7237 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7238 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007239 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007240 return;
7241 }
7242
7243 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007244 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245 continue;
7246 }
7247 }
7248}
7249
7250
Willy Tarreau58f10d72006-12-04 02:26:12 +01007251/*
7252 * Try to retrieve a known appsession in the URI, then the associated server.
7253 * If the server is found, it's assigned to the session.
7254 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007255void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007256{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007257 char *end_params, *first_param, *cur_param, *next_param;
7258 char separator;
7259 int value_len;
7260
7261 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007262
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007263 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007264 (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 +01007265 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007266 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007267
Cyril Bontéb21570a2009-11-29 20:04:48 +01007268 first_param = NULL;
7269 switch (mode) {
7270 case PR_O2_AS_M_PP:
7271 first_param = memchr(begin, ';', len);
7272 break;
7273 case PR_O2_AS_M_QS:
7274 first_param = memchr(begin, '?', len);
7275 break;
7276 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007277
Cyril Bontéb21570a2009-11-29 20:04:48 +01007278 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007279 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007280 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007281
Cyril Bontéb21570a2009-11-29 20:04:48 +01007282 switch (mode) {
7283 case PR_O2_AS_M_PP:
7284 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7285 end_params = (char *) begin + len;
7286 }
7287 separator = ';';
7288 break;
7289 case PR_O2_AS_M_QS:
7290 end_params = (char *) begin + len;
7291 separator = '&';
7292 break;
7293 default:
7294 /* unknown mode, shouldn't happen */
7295 return;
7296 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007297
Cyril Bontéb21570a2009-11-29 20:04:48 +01007298 cur_param = next_param = end_params;
7299 while (cur_param > first_param) {
7300 cur_param--;
7301 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7302 /* let's see if this is the appsession parameter */
7303 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7304 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7305 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7306 /* Cool... it's the right one */
7307 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7308 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7309 if (value_len > 0) {
7310 manage_client_side_appsession(t, cur_param, value_len);
7311 }
7312 break;
7313 }
7314 next_param = cur_param;
7315 }
7316 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007317#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007318 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007319 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007320#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007321}
7322
Willy Tarreaub2513902006-12-17 14:52:38 +01007323/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007324 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007325 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007326 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007327 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007328 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007329 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007330 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007331 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007332int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007333{
7334 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007335 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007336
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007337 if (!uri_auth)
7338 return 0;
7339
Cyril Bonté70be45d2010-10-12 00:14:35 +02007340 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007341 return 0;
7342
Willy Tarreau295a8372011-03-10 11:25:07 +01007343 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007344
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007345 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007346 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007347 return 0;
7348
Willy Tarreau962c3f42010-01-10 00:15:35 +01007349 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007350
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007351 /* the URI is in h */
7352 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007353 return 0;
7354
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007355 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007356 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007357 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007358 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007359 break;
7360 }
7361 h++;
7362 }
7363
7364 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007365 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7366 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007367 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007368 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007369 break;
7370 }
7371 h++;
7372 }
7373 }
7374
Willy Tarreau962c3f42010-01-10 00:15:35 +01007375 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7376 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007377 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007378 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007379 break;
7380 }
7381 h++;
7382 }
7383
Cyril Bonté70be45d2010-10-12 00:14:35 +02007384 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7385 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7386 if (memcmp(h, ";st=", 4) == 0) {
7387 h += 4;
7388
7389 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007390 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007391 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007392 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007393 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007394 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007395 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007396 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007397 else
Willy Tarreau295a8372011-03-10 11:25:07 +01007398 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007399 break;
7400 }
7401 h++;
7402 }
7403
Willy Tarreau295a8372011-03-10 11:25:07 +01007404 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007405
Willy Tarreaub2513902006-12-17 14:52:38 +01007406 return 1;
7407}
7408
Willy Tarreau4076a152009-04-02 15:18:36 +02007409/*
7410 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007411 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7412 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007413 */
7414void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7415 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007416 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007417{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007418 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7419 int len1 = buf->size - msg->som;
7420 es->len = buf->r - (buf->data + msg->som) + buf->size;
7421 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7422 if (es->len > len1 && len1 < sizeof(es->buf))
7423 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7424 }
7425 else {
7426 es->len = buf->r - (buf->data + msg->som);
7427 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7428 }
7429
Willy Tarreau4076a152009-04-02 15:18:36 +02007430 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007431 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007432 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007433 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007434 else
7435 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7436
Willy Tarreau4076a152009-04-02 15:18:36 +02007437 es->when = date; // user-visible date
7438 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007439 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007440 es->oe = other_end;
Willy Tarreau957c0a52011-03-03 17:42:23 +01007441 es->src = s->req->prod->addr.c.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007442 es->state = state;
7443 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007444 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007445}
Willy Tarreaub2513902006-12-17 14:52:38 +01007446
Willy Tarreaubce70882009-09-07 11:51:47 +02007447/* return the IP address pointed to by occurrence <occ> of header <hname> in
7448 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
7449 * occurrence number corresponding to this value is returned. If <occ> is
7450 * strictly negative, the occurrence number before the end corresponding to
7451 * this value is returned. If <occ> is null, any value is returned, so it is
7452 * not recommended to use it that way. Negative occurrences are limited to
7453 * a small value because it is required to keep them in memory while scanning.
7454 * IP address 0.0.0.0 is returned if no match is found.
7455 */
7456unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
7457{
7458 struct hdr_ctx ctx;
7459 unsigned int hdr_hist[MAX_HDR_HISTORY];
7460 unsigned int hist_ptr;
7461 int found = 0;
7462
7463 ctx.idx = 0;
7464 if (occ >= 0) {
7465 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7466 occ--;
7467 if (occ <= 0) {
7468 found = 1;
7469 break;
7470 }
7471 }
7472 if (!found)
7473 return 0;
7474 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7475 }
7476
7477 /* negative occurrence, we scan all the list then walk back */
7478 if (-occ > MAX_HDR_HISTORY)
7479 return 0;
7480
7481 hist_ptr = 0;
7482 hdr_hist[hist_ptr] = 0;
7483 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7484 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7485 if (hist_ptr >= MAX_HDR_HISTORY)
7486 hist_ptr = 0;
7487 found++;
7488 }
7489 if (-occ > found)
7490 return 0;
7491 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7492 * find occurrence -occ, so we have to check [hist_ptr+occ].
7493 */
7494 hist_ptr += occ;
7495 if (hist_ptr >= MAX_HDR_HISTORY)
7496 hist_ptr -= MAX_HDR_HISTORY;
7497 return hdr_hist[hist_ptr];
7498}
7499
Willy Tarreaubaaee002006-06-26 02:48:02 +02007500/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007501 * Print a debug line with a header
7502 */
7503void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7504{
7505 int len, max;
7506 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007507 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007508 max = end - start;
7509 UBOUND(max, sizeof(trash) - len - 1);
7510 len += strlcpy2(trash + len, start, max + 1);
7511 trash[len++] = '\n';
7512 write(1, trash, len);
7513}
7514
Willy Tarreau0937bc42009-12-22 15:03:09 +01007515/*
7516 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7517 * the required fields are properly allocated and that we only need to (re)init
7518 * them. This should be used before processing any new request.
7519 */
7520void http_init_txn(struct session *s)
7521{
7522 struct http_txn *txn = &s->txn;
7523 struct proxy *fe = s->fe;
7524
7525 txn->flags = 0;
7526 txn->status = -1;
7527
Willy Tarreauf64d1412010-10-07 20:06:11 +02007528 txn->cookie_first_date = 0;
7529 txn->cookie_last_date = 0;
7530
Willy Tarreau0937bc42009-12-22 15:03:09 +01007531 txn->req.sol = txn->req.eol = NULL;
7532 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7533 txn->rsp.sol = txn->rsp.eol = NULL;
7534 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007535 txn->req.chunk_len = 0LL;
7536 txn->req.body_len = 0LL;
7537 txn->rsp.chunk_len = 0LL;
7538 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007539 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7540 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007541
7542 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007543
7544 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7545 if (fe->options2 & PR_O2_REQBUG_OK)
7546 txn->req.err_pos = -1; /* let buggy requests pass */
7547
Willy Tarreau46023632010-01-07 22:51:47 +01007548 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007549 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7550
Willy Tarreau46023632010-01-07 22:51:47 +01007551 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007552 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7553
7554 if (txn->hdr_idx.v)
7555 hdr_idx_init(&txn->hdr_idx);
7556}
7557
7558/* to be used at the end of a transaction */
7559void http_end_txn(struct session *s)
7560{
7561 struct http_txn *txn = &s->txn;
7562
7563 /* these ones will have been dynamically allocated */
7564 pool_free2(pool2_requri, txn->uri);
7565 pool_free2(pool2_capture, txn->cli_cookie);
7566 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007567 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007568
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007569 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007570 txn->uri = NULL;
7571 txn->srv_cookie = NULL;
7572 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007573
7574 if (txn->req.cap) {
7575 struct cap_hdr *h;
7576 for (h = s->fe->req_cap; h; h = h->next)
7577 pool_free2(h->pool, txn->req.cap[h->index]);
7578 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7579 }
7580
7581 if (txn->rsp.cap) {
7582 struct cap_hdr *h;
7583 for (h = s->fe->rsp_cap; h; h = h->next)
7584 pool_free2(h->pool, txn->rsp.cap[h->index]);
7585 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7586 }
7587
Willy Tarreau0937bc42009-12-22 15:03:09 +01007588}
7589
7590/* to be used at the end of a transaction to prepare a new one */
7591void http_reset_txn(struct session *s)
7592{
7593 http_end_txn(s);
7594 http_init_txn(s);
7595
7596 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007597 s->logs.logwait = s->fe->to_log;
Willy Tarreau827aee92011-03-10 16:55:02 +01007598 s->srv_conn = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +01007599 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007600 /* re-init store persistence */
7601 s->store_count = 0;
7602
Willy Tarreau0937bc42009-12-22 15:03:09 +01007603 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007604
7605 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7606
Willy Tarreau739cfba2010-01-25 23:11:14 +01007607 /* We must trim any excess data from the response buffer, because we
7608 * may have blocked an invalid response from a server that we don't
7609 * want to accidentely forward once we disable the analysers, nor do
7610 * we want those data to come along with next response. A typical
7611 * example of such data would be from a buggy server responding to
7612 * a HEAD with some data, or sending more than the advertised
7613 * content-length.
7614 */
7615 if (unlikely(s->rep->l > s->rep->send_max)) {
7616 s->rep->l = s->rep->send_max;
7617 s->rep->r = s->rep->w + s->rep->l;
7618 if (s->rep->r >= s->rep->data + s->rep->size)
7619 s->rep->r -= s->rep->size;
7620 }
7621
Willy Tarreau0937bc42009-12-22 15:03:09 +01007622 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007623 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007624
Willy Tarreaud04e8582010-05-31 12:31:35 +02007625 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007626 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007627
7628 s->req->rex = TICK_ETERNITY;
7629 s->req->wex = TICK_ETERNITY;
7630 s->req->analyse_exp = TICK_ETERNITY;
7631 s->rep->rex = TICK_ETERNITY;
7632 s->rep->wex = TICK_ETERNITY;
7633 s->rep->analyse_exp = TICK_ETERNITY;
7634}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007635
Willy Tarreauff011f22011-01-06 17:51:27 +01007636void free_http_req_rules(struct list *r) {
7637 struct http_req_rule *tr, *pr;
7638
7639 list_for_each_entry_safe(pr, tr, r, list) {
7640 LIST_DEL(&pr->list);
7641 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7642 free(pr->http_auth.realm);
7643
7644 free(pr);
7645 }
7646}
7647
7648struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7649{
7650 struct http_req_rule *rule;
7651 int cur_arg;
7652
7653 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7654 if (!rule) {
7655 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7656 return NULL;
7657 }
7658
7659 if (!*args[0]) {
7660 goto req_error_parsing;
7661 } else if (!strcmp(args[0], "allow")) {
7662 rule->action = HTTP_REQ_ACT_ALLOW;
7663 cur_arg = 1;
7664 } else if (!strcmp(args[0], "deny")) {
7665 rule->action = HTTP_REQ_ACT_DENY;
7666 cur_arg = 1;
7667 } else if (!strcmp(args[0], "auth")) {
7668 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7669 cur_arg = 1;
7670
7671 while(*args[cur_arg]) {
7672 if (!strcmp(args[cur_arg], "realm")) {
7673 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7674 cur_arg+=2;
7675 continue;
7676 } else
7677 break;
7678 }
7679 } else {
7680req_error_parsing:
7681 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7682 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7683 return NULL;
7684 }
7685
7686 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7687 struct acl_cond *cond;
7688
7689 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7690 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7691 file, linenum, args[0]);
7692 return NULL;
7693 }
7694 rule->cond = cond;
7695 }
7696 else if (*args[cur_arg]) {
7697 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7698 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7699 file, linenum, args[0], args[cur_arg]);
7700 return NULL;
7701 }
7702
7703 return rule;
7704}
7705
Willy Tarreau8797c062007-05-07 00:55:35 +02007706/************************************************************************/
7707/* The code below is dedicated to ACL parsing and matching */
7708/************************************************************************/
7709
7710
7711
7712
7713/* 1. Check on METHOD
7714 * We use the pre-parsed method if it is known, and store its number as an
7715 * integer. If it is unknown, we use the pointer and the length.
7716 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007717static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007718{
7719 int len, meth;
7720
Willy Tarreauae8b7962007-06-09 23:10:04 +02007721 len = strlen(*text);
7722 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007723
7724 pattern->val.i = meth;
7725 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007726 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007727 if (!pattern->ptr.str)
7728 return 0;
7729 pattern->len = len;
7730 }
7731 return 1;
7732}
7733
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007734static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007735acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7736 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007737{
7738 int meth;
7739 struct http_txn *txn = l7;
7740
Willy Tarreaub6866442008-07-14 23:54:42 +02007741 if (!txn)
7742 return 0;
7743
Willy Tarreau655dce92009-11-08 13:10:58 +01007744 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007745 return 0;
7746
Willy Tarreau8797c062007-05-07 00:55:35 +02007747 meth = txn->meth;
7748 test->i = meth;
7749 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007750 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7751 /* ensure the indexes are not affected */
7752 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007753 test->len = txn->req.sl.rq.m_l;
7754 test->ptr = txn->req.sol;
7755 }
7756 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7757 return 1;
7758}
7759
7760static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7761{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007762 int icase;
7763
Willy Tarreau8797c062007-05-07 00:55:35 +02007764 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02007765 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007766
7767 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02007768 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007769
7770 /* Other method, we must compare the strings */
7771 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02007772 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007773
7774 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
7775 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
7776 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007777 return ACL_PAT_FAIL;
7778 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007779}
7780
7781/* 2. Check on Request/Status Version
7782 * We simply compare strings here.
7783 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007784static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007785{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007786 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007787 if (!pattern->ptr.str)
7788 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007789 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007790 return 1;
7791}
7792
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007793static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007794acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7795 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007796{
7797 struct http_txn *txn = l7;
7798 char *ptr;
7799 int len;
7800
Willy Tarreaub6866442008-07-14 23:54:42 +02007801 if (!txn)
7802 return 0;
7803
Willy Tarreau655dce92009-11-08 13:10:58 +01007804 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007805 return 0;
7806
Willy Tarreau8797c062007-05-07 00:55:35 +02007807 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007808 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007809
7810 while ((len-- > 0) && (*ptr++ != '/'));
7811 if (len <= 0)
7812 return 0;
7813
7814 test->ptr = ptr;
7815 test->len = len;
7816
7817 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7818 return 1;
7819}
7820
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007821static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007822acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7823 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007824{
7825 struct http_txn *txn = l7;
7826 char *ptr;
7827 int len;
7828
Willy Tarreaub6866442008-07-14 23:54:42 +02007829 if (!txn)
7830 return 0;
7831
Willy Tarreau655dce92009-11-08 13:10:58 +01007832 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007833 return 0;
7834
Willy Tarreau8797c062007-05-07 00:55:35 +02007835 len = txn->rsp.sl.st.v_l;
7836 ptr = txn->rsp.sol;
7837
7838 while ((len-- > 0) && (*ptr++ != '/'));
7839 if (len <= 0)
7840 return 0;
7841
7842 test->ptr = ptr;
7843 test->len = len;
7844
7845 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7846 return 1;
7847}
7848
7849/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007850static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007851acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7852 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007853{
7854 struct http_txn *txn = l7;
7855 char *ptr;
7856 int len;
7857
Willy Tarreaub6866442008-07-14 23:54:42 +02007858 if (!txn)
7859 return 0;
7860
Willy Tarreau655dce92009-11-08 13:10:58 +01007861 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007862 return 0;
7863
Willy Tarreau8797c062007-05-07 00:55:35 +02007864 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007865 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007866
7867 test->i = __strl2ui(ptr, len);
7868 test->flags = ACL_TEST_F_VOL_1ST;
7869 return 1;
7870}
7871
7872/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007873static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007874acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7875 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007876{
7877 struct http_txn *txn = l7;
7878
Willy Tarreaub6866442008-07-14 23:54:42 +02007879 if (!txn)
7880 return 0;
7881
Willy Tarreau655dce92009-11-08 13:10:58 +01007882 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007883 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007884
Willy Tarreauc11416f2007-06-17 16:58:38 +02007885 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7886 /* ensure the indexes are not affected */
7887 return 0;
7888
Willy Tarreau8797c062007-05-07 00:55:35 +02007889 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007890 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007891
Willy Tarreauf3d25982007-05-08 22:45:09 +02007892 /* we do not need to set READ_ONLY because the data is in a buffer */
7893 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007894 return 1;
7895}
7896
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007897static int
7898acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7899 struct acl_expr *expr, struct acl_test *test)
7900{
7901 struct http_txn *txn = l7;
7902
Willy Tarreaub6866442008-07-14 23:54:42 +02007903 if (!txn)
7904 return 0;
7905
Willy Tarreau655dce92009-11-08 13:10:58 +01007906 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007907 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007908
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007909 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7910 /* ensure the indexes are not affected */
7911 return 0;
7912
7913 /* Parse HTTP request */
Willy Tarreau957c0a52011-03-03 17:42:23 +01007914 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to);
7915 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007916 test->i = AF_INET;
7917
7918 /*
7919 * If we are parsing url in frontend space, we prepare backend stage
7920 * to not parse again the same url ! optimization lazyness...
7921 */
7922 if (px->options & PR_O_HTTP_PROXY)
7923 l4->flags |= SN_ADDR_SET;
7924
7925 test->flags = ACL_TEST_F_READ_ONLY;
7926 return 1;
7927}
7928
7929static int
7930acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7931 struct acl_expr *expr, struct acl_test *test)
7932{
7933 struct http_txn *txn = l7;
7934
Willy Tarreaub6866442008-07-14 23:54:42 +02007935 if (!txn)
7936 return 0;
7937
Willy Tarreau655dce92009-11-08 13:10:58 +01007938 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007939 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007940
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007941 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7942 /* ensure the indexes are not affected */
7943 return 0;
7944
7945 /* Same optimization as url_ip */
Willy Tarreau957c0a52011-03-03 17:42:23 +01007946 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to);
7947 test->i = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007948
7949 if (px->options & PR_O_HTTP_PROXY)
7950 l4->flags |= SN_ADDR_SET;
7951
7952 test->flags = ACL_TEST_F_READ_ONLY;
7953 return 1;
7954}
7955
Willy Tarreauc11416f2007-06-17 16:58:38 +02007956/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7957 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7958 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007959static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007960acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007961 struct acl_expr *expr, struct acl_test *test)
7962{
7963 struct http_txn *txn = l7;
7964 struct hdr_idx *idx = &txn->hdr_idx;
7965 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007966
Willy Tarreaub6866442008-07-14 23:54:42 +02007967 if (!txn)
7968 return 0;
7969
Willy Tarreau33a7e692007-06-10 19:45:56 +02007970 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7971 /* search for header from the beginning */
7972 ctx->idx = 0;
7973
Willy Tarreau33a7e692007-06-10 19:45:56 +02007974 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7975 test->flags |= ACL_TEST_F_FETCH_MORE;
7976 test->flags |= ACL_TEST_F_VOL_HDR;
7977 test->len = ctx->vlen;
7978 test->ptr = (char *)ctx->line + ctx->val;
7979 return 1;
7980 }
7981
7982 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7983 test->flags |= ACL_TEST_F_VOL_HDR;
7984 return 0;
7985}
7986
Willy Tarreau33a7e692007-06-10 19:45:56 +02007987static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007988acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7989 struct acl_expr *expr, struct acl_test *test)
7990{
7991 struct http_txn *txn = l7;
7992
Willy Tarreaub6866442008-07-14 23:54:42 +02007993 if (!txn)
7994 return 0;
7995
Willy Tarreau655dce92009-11-08 13:10:58 +01007996 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007997 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007998
Willy Tarreauc11416f2007-06-17 16:58:38 +02007999 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8000 /* ensure the indexes are not affected */
8001 return 0;
8002
8003 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
8004}
8005
8006static int
8007acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
8008 struct acl_expr *expr, struct acl_test *test)
8009{
8010 struct http_txn *txn = l7;
8011
Willy Tarreaub6866442008-07-14 23:54:42 +02008012 if (!txn)
8013 return 0;
8014
Willy Tarreau655dce92009-11-08 13:10:58 +01008015 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008016 return 0;
8017
8018 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
8019}
8020
8021/* 6. Check on HTTP header count. The number of occurrences is returned.
8022 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8023 */
8024static int
8025acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008026 struct acl_expr *expr, struct acl_test *test)
8027{
8028 struct http_txn *txn = l7;
8029 struct hdr_idx *idx = &txn->hdr_idx;
8030 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008031 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008032
Willy Tarreaub6866442008-07-14 23:54:42 +02008033 if (!txn)
8034 return 0;
8035
Willy Tarreau33a7e692007-06-10 19:45:56 +02008036 ctx.idx = 0;
8037 cnt = 0;
8038 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
8039 cnt++;
8040
8041 test->i = cnt;
8042 test->flags = ACL_TEST_F_VOL_HDR;
8043 return 1;
8044}
8045
Willy Tarreauc11416f2007-06-17 16:58:38 +02008046static int
8047acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8048 struct acl_expr *expr, struct acl_test *test)
8049{
8050 struct http_txn *txn = l7;
8051
Willy Tarreaub6866442008-07-14 23:54:42 +02008052 if (!txn)
8053 return 0;
8054
Willy Tarreau655dce92009-11-08 13:10:58 +01008055 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008056 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008057
Willy Tarreauc11416f2007-06-17 16:58:38 +02008058 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8059 /* ensure the indexes are not affected */
8060 return 0;
8061
8062 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
8063}
8064
8065static int
8066acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8067 struct acl_expr *expr, struct acl_test *test)
8068{
8069 struct http_txn *txn = l7;
8070
Willy Tarreaub6866442008-07-14 23:54:42 +02008071 if (!txn)
8072 return 0;
8073
Willy Tarreau655dce92009-11-08 13:10:58 +01008074 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008075 return 0;
8076
8077 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
8078}
8079
Willy Tarreau33a7e692007-06-10 19:45:56 +02008080/* 7. Check on HTTP header's integer value. The integer value is returned.
8081 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008082 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008083 */
8084static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02008085acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008086 struct acl_expr *expr, struct acl_test *test)
8087{
8088 struct http_txn *txn = l7;
8089 struct hdr_idx *idx = &txn->hdr_idx;
8090 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008091
Willy Tarreaub6866442008-07-14 23:54:42 +02008092 if (!txn)
8093 return 0;
8094
Willy Tarreau33a7e692007-06-10 19:45:56 +02008095 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8096 /* search for header from the beginning */
8097 ctx->idx = 0;
8098
Willy Tarreau33a7e692007-06-10 19:45:56 +02008099 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8100 test->flags |= ACL_TEST_F_FETCH_MORE;
8101 test->flags |= ACL_TEST_F_VOL_HDR;
8102 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
8103 return 1;
8104 }
8105
8106 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8107 test->flags |= ACL_TEST_F_VOL_HDR;
8108 return 0;
8109}
8110
Willy Tarreauc11416f2007-06-17 16:58:38 +02008111static int
8112acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8113 struct acl_expr *expr, struct acl_test *test)
8114{
8115 struct http_txn *txn = l7;
8116
Willy Tarreaub6866442008-07-14 23:54:42 +02008117 if (!txn)
8118 return 0;
8119
Willy Tarreau655dce92009-11-08 13:10:58 +01008120 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008121 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008122
Willy Tarreauc11416f2007-06-17 16:58:38 +02008123 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8124 /* ensure the indexes are not affected */
8125 return 0;
8126
8127 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
8128}
8129
8130static int
8131acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8132 struct acl_expr *expr, struct acl_test *test)
8133{
8134 struct http_txn *txn = l7;
8135
Willy Tarreaub6866442008-07-14 23:54:42 +02008136 if (!txn)
8137 return 0;
8138
Willy Tarreau655dce92009-11-08 13:10:58 +01008139 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008140 return 0;
8141
8142 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
8143}
8144
Willy Tarreau106f9792009-09-19 07:54:16 +02008145/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
8146 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8147 */
8148static int
8149acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
8150 struct acl_expr *expr, struct acl_test *test)
8151{
8152 struct http_txn *txn = l7;
8153 struct hdr_idx *idx = &txn->hdr_idx;
8154 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
8155
8156 if (!txn)
8157 return 0;
8158
8159 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8160 /* search for header from the beginning */
8161 ctx->idx = 0;
8162
8163 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8164 test->flags |= ACL_TEST_F_FETCH_MORE;
8165 test->flags |= ACL_TEST_F_VOL_HDR;
8166 /* Same optimization as url_ip */
David du Colombier6f5ccb12011-03-10 22:26:24 +01008167 memset(&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr, 0, sizeof(((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr));
8168 url2ipv4((char *)ctx->line + ctx->val, &((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr);
8169 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr;
Willy Tarreau106f9792009-09-19 07:54:16 +02008170 test->i = AF_INET;
8171 return 1;
8172 }
8173
8174 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8175 test->flags |= ACL_TEST_F_VOL_HDR;
8176 return 0;
8177}
8178
8179static int
8180acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8181 struct acl_expr *expr, struct acl_test *test)
8182{
8183 struct http_txn *txn = l7;
8184
8185 if (!txn)
8186 return 0;
8187
Willy Tarreau655dce92009-11-08 13:10:58 +01008188 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008189 return 0;
8190
8191 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8192 /* ensure the indexes are not affected */
8193 return 0;
8194
8195 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8196}
8197
8198static int
8199acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8200 struct acl_expr *expr, struct acl_test *test)
8201{
8202 struct http_txn *txn = l7;
8203
8204 if (!txn)
8205 return 0;
8206
Willy Tarreau655dce92009-11-08 13:10:58 +01008207 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008208 return 0;
8209
8210 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8211}
8212
Willy Tarreau737b0c12007-06-10 21:28:46 +02008213/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8214 * the first '/' after the possible hostname, and ends before the possible '?'.
8215 */
8216static int
8217acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8218 struct acl_expr *expr, struct acl_test *test)
8219{
8220 struct http_txn *txn = l7;
8221 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008222
Willy Tarreaub6866442008-07-14 23:54:42 +02008223 if (!txn)
8224 return 0;
8225
Willy Tarreau655dce92009-11-08 13:10:58 +01008226 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008227 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008228
Willy Tarreauc11416f2007-06-17 16:58:38 +02008229 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8230 /* ensure the indexes are not affected */
8231 return 0;
8232
Willy Tarreau962c3f42010-01-10 00:15:35 +01008233 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008234 ptr = http_get_path(txn);
8235 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008236 return 0;
8237
8238 /* OK, we got the '/' ! */
8239 test->ptr = ptr;
8240
8241 while (ptr < end && *ptr != '?')
8242 ptr++;
8243
8244 test->len = ptr - test->ptr;
8245
8246 /* we do not need to set READ_ONLY because the data is in a buffer */
8247 test->flags = ACL_TEST_F_VOL_1ST;
8248 return 1;
8249}
8250
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008251static int
8252acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8253 struct acl_expr *expr, struct acl_test *test)
8254{
8255 struct buffer *req = s->req;
8256 struct http_txn *txn = &s->txn;
8257 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008258
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008259 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8260 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8261 */
8262
8263 if (!s || !req)
8264 return 0;
8265
Willy Tarreau655dce92009-11-08 13:10:58 +01008266 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008267 /* Already decoded as OK */
8268 test->flags |= ACL_TEST_F_SET_RES_PASS;
8269 return 1;
8270 }
8271
8272 /* Try to decode HTTP request */
8273 if (likely(req->lr < req->r))
8274 http_msg_analyzer(req, msg, &txn->hdr_idx);
8275
Willy Tarreau655dce92009-11-08 13:10:58 +01008276 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008277 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8278 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8279 return 1;
8280 }
8281 /* wait for final state */
8282 test->flags |= ACL_TEST_F_MAY_CHANGE;
8283 return 0;
8284 }
8285
8286 /* OK we got a valid HTTP request. We have some minor preparation to
8287 * perform so that further checks can rely on HTTP tests.
8288 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008289 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008290 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8291 s->flags |= SN_REDIRECTABLE;
8292
8293 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8294 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8295 return 1;
8296 }
8297
8298 test->flags |= ACL_TEST_F_SET_RES_PASS;
8299 return 1;
8300}
8301
Willy Tarreau7f18e522010-10-22 20:04:13 +02008302/* return a valid test if the current request is the first one on the connection */
8303static int
8304acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8305 struct acl_expr *expr, struct acl_test *test)
8306{
8307 if (!s)
8308 return 0;
8309
8310 if (s->txn.flags & TX_NOT_FIRST)
8311 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8312 else
8313 test->flags |= ACL_TEST_F_SET_RES_PASS;
8314
8315 return 1;
8316}
8317
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008318static int
8319acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8320 struct acl_expr *expr, struct acl_test *test)
8321{
8322
8323 if (!s)
8324 return 0;
8325
8326 if (!get_http_auth(s))
8327 return 0;
8328
8329 test->ctx.a[0] = expr->arg.ul;
8330 test->ctx.a[1] = s->txn.auth.user;
8331 test->ctx.a[2] = s->txn.auth.pass;
8332
8333 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8334
8335 return 1;
8336}
Willy Tarreau8797c062007-05-07 00:55:35 +02008337
8338/************************************************************************/
8339/* All supported keywords must be declared here. */
8340/************************************************************************/
8341
8342/* Note: must not be declared <const> as its list will be overwritten */
8343static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008344 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8345
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008346 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008347 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8348 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008349 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008350
Willy Tarreauc4262962010-05-10 23:42:40 +02008351 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008352 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8353 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8354 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8355 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8356 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8357 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008358 { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008359 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008360
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008361 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008362 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008363 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8364 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8365 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8366 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8367 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8368 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8369 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
8370 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008371 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreauc11416f2007-06-17 16:58:38 +02008372
Willy Tarreauc4262962010-05-10 23:42:40 +02008373 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008374 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8375 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8376 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8377 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8378 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8379 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8380 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
8381 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008382 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008383
Willy Tarreauc4262962010-05-10 23:42:40 +02008384 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008385 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8386 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8387 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8388 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8389 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8390 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008391
Willy Tarreauf3d25982007-05-08 22:45:09 +02008392#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008393 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8394 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8395 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8396 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8397 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8398 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8399 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8400
Willy Tarreau8797c062007-05-07 00:55:35 +02008401 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8402 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8403 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8404 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8405 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8406 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8407 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8408 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008409#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008410
Willy Tarreau7f18e522010-10-22 20:04:13 +02008411 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8412 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8413 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008414 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008415}};
8416
Willy Tarreau4a568972010-05-12 08:08:50 +02008417/************************************************************************/
8418/* The code below is dedicated to pattern fetching and matching */
8419/************************************************************************/
8420
8421/* extract the IP address from the last occurrence of specified header. Note
8422 * that we should normally first extract the string then convert it to IP,
8423 * but right now we have all the functions to do this seemlessly, and we will
8424 * be able to change that later without touching the configuration.
8425 */
8426static int
8427pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02008428 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008429{
8430 struct http_txn *txn = l7;
8431
Emeric Brun485479d2010-09-23 18:02:19 +02008432 data->ip.s_addr = htonl(get_ip_from_hdr2(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx, -1));
Willy Tarreau4a568972010-05-12 08:08:50 +02008433 return data->ip.s_addr != 0;
8434}
8435
David Cournapeau16023ee2010-12-23 20:55:41 +09008436/*
8437 * Given a path string and its length, find the position of beginning of the
8438 * query string. Returns NULL if no query string is found in the path.
8439 *
8440 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8441 *
8442 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8443 */
8444static inline char *find_query_string(char *path, size_t path_l)
8445{
8446 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008447
David Cournapeau16023ee2010-12-23 20:55:41 +09008448 p = memchr(path, '?', path_l);
8449 return p ? p + 1 : NULL;
8450}
8451
8452static inline int is_param_delimiter(char c)
8453{
8454 return c == '&' || c == ';';
8455}
8456
8457/*
8458 * Given a url parameter, find the starting position of the first occurence,
8459 * or NULL if the parameter is not found.
8460 *
8461 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8462 * the function will return query_string+8.
8463 */
8464static char*
8465find_url_param_pos(char* query_string, size_t query_string_l,
8466 char* url_param_name, size_t url_param_name_l)
8467{
8468 char *pos, *last;
8469
8470 pos = query_string;
8471 last = query_string + query_string_l - url_param_name_l - 1;
8472
8473 while (pos <= last) {
8474 if (pos[url_param_name_l] == '=') {
8475 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8476 return pos;
8477 pos += url_param_name_l + 1;
8478 }
8479 while (pos <= last && !is_param_delimiter(*pos))
8480 pos++;
8481 pos++;
8482 }
8483 return NULL;
8484}
8485
8486/*
8487 * Given a url parameter name, returns its value and size into *value and
8488 * *value_l respectively, and returns non-zero. If the parameter is not found,
8489 * zero is returned and value/value_l are not touched.
8490 */
8491static int
8492find_url_param_value(char* path, size_t path_l,
8493 char* url_param_name, size_t url_param_name_l,
8494 char** value, size_t* value_l)
8495{
8496 char *query_string, *qs_end;
8497 char *arg_start;
8498 char *value_start, *value_end;
8499
8500 query_string = find_query_string(path, path_l);
8501 if (!query_string)
8502 return 0;
8503
8504 qs_end = path + path_l;
8505 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8506 url_param_name, url_param_name_l);
8507 if (!arg_start)
8508 return 0;
8509
8510 value_start = arg_start + url_param_name_l + 1;
8511 value_end = value_start;
8512
8513 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8514 value_end++;
8515
8516 *value = value_start;
8517 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008518 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008519}
8520
8521static int
8522pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8523 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8524{
8525 struct http_txn *txn = l7;
8526 struct http_msg *msg = &txn->req;
8527 char *url_param_value;
8528 size_t url_param_value_l;
8529
8530 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8531 arg_p->data.str.str, arg_p->data.str.len,
8532 &url_param_value, &url_param_value_l))
8533 return 0;
8534
8535 data->str.str = url_param_value;
8536 data->str.len = url_param_value_l;
8537 return 1;
8538}
8539
Emeric Brun485479d2010-09-23 18:02:19 +02008540
Willy Tarreau4a568972010-05-12 08:08:50 +02008541/************************************************************************/
8542/* All supported keywords must be declared here. */
8543/************************************************************************/
8544/* Note: must not be declared <const> as its list will be overwritten */
8545static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Emeric Brun485479d2010-09-23 18:02:19 +02008546 { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008547 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Emeric Brun485479d2010-09-23 18:02:19 +02008548 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008549}};
8550
Willy Tarreau8797c062007-05-07 00:55:35 +02008551
8552__attribute__((constructor))
8553static void __http_protocol_init(void)
8554{
8555 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008556 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008557}
8558
8559
Willy Tarreau58f10d72006-12-04 02:26:12 +01008560/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008561 * Local variables:
8562 * c-indent-level: 8
8563 * c-basic-offset: 8
8564 * End:
8565 */