blob: 7f37002cd22e08be71d7fb3c8d0c6732c11899f1 [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 Tarreau588bd4f2011-09-01 22:22:28 +0200506 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100507 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);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200554 ctx->tws = 0;
555 while (http_is_lws[(unsigned char)*(eol - 1)]) {
556 eol--;
557 ctx->tws++;
558 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200559 ctx->vlen = eol - sov;
560 return 1;
561 }
562 next_hdr:
563 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100564 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200565 cur_idx = idx->v[cur_idx].next;
566 }
567 return 0;
568}
569
570int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100571 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200572 struct hdr_ctx *ctx)
573{
574 return http_find_header2(name, strlen(name), sol, idx, ctx);
575}
576
Willy Tarreau68085d82010-01-18 14:54:04 +0100577/* Remove one value of a header. This only works on a <ctx> returned by one of
578 * the http_find_header functions. The value is removed, as well as surrounding
579 * commas if any. If the removed value was alone, the whole header is removed.
580 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
581 * message <msg>. The new index is returned. If it is zero, it means there is
582 * no more header, so any processing may stop. The ctx is always left in a form
583 * that can be handled by http_find_header2() to find next occurrence.
584 */
585int http_remove_header2(struct http_msg *msg, struct buffer *buf,
586 struct hdr_idx *idx, struct hdr_ctx *ctx)
587{
588 int cur_idx = ctx->idx;
589 char *sol = ctx->line;
590 struct hdr_idx_elem *hdr;
591 int delta, skip_comma;
592
593 if (!cur_idx)
594 return 0;
595
596 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200597 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100598 /* This was the only value of the header, we must now remove it entirely. */
599 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
600 http_msg_move_end(msg, delta);
601 idx->used--;
602 hdr->len = 0; /* unused entry */
603 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100604 if (idx->tail == ctx->idx)
605 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100606 ctx->idx = ctx->prev; /* walk back to the end of previous header */
607 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
608 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200609 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100610 return ctx->idx;
611 }
612
613 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200614 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
615 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100616 */
617
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200618 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100619 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200620 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100621 NULL, 0);
622 hdr->len += delta;
623 http_msg_move_end(msg, delta);
624 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200625 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100626 return ctx->idx;
627}
628
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100629/* This function handles a server error at the stream interface level. The
630 * stream interface is assumed to be already in a closed state. An optional
631 * message is copied into the input buffer, and an HTTP status code stored.
632 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100633 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100635static void http_server_error(struct session *t, struct stream_interface *si,
636 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100638 buffer_auto_read(si->ob);
639 buffer_abort(si->ob);
640 buffer_auto_close(si->ob);
641 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200642 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100643 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100644 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100645 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100646 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647 }
648 if (!(t->flags & SN_ERR_MASK))
649 t->flags |= err;
650 if (!(t->flags & SN_FINST_MASK))
651 t->flags |= finst;
652}
653
Willy Tarreau80587432006-12-24 17:47:20 +0100654/* This function returns the appropriate error location for the given session
655 * and message.
656 */
657
658struct chunk *error_message(struct session *s, int msgnum)
659{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200660 if (s->be->errmsg[msgnum].str)
661 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100662 else if (s->fe->errmsg[msgnum].str)
663 return &s->fe->errmsg[msgnum];
664 else
665 return &http_err_chunks[msgnum];
666}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667
Willy Tarreau53b6c742006-12-17 13:37:46 +0100668/*
669 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
670 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
671 */
672static http_meth_t find_http_meth(const char *str, const int len)
673{
674 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100675 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100676
677 m = ((unsigned)*str - 'A');
678
679 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100680 for (h = http_methods[m]; h->len > 0; h++) {
681 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100682 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100683 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100684 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100685 };
686 return HTTP_METH_OTHER;
687 }
688 return HTTP_METH_NONE;
689
690}
691
Willy Tarreau21d2af32008-02-14 20:25:24 +0100692/* Parse the URI from the given transaction (which is assumed to be in request
693 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
694 * It is returned otherwise.
695 */
696static char *
697http_get_path(struct http_txn *txn)
698{
699 char *ptr, *end;
700
Willy Tarreau962c3f42010-01-10 00:15:35 +0100701 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100702 end = ptr + txn->req.sl.rq.u_l;
703
704 if (ptr >= end)
705 return NULL;
706
707 /* RFC2616, par. 5.1.2 :
708 * Request-URI = "*" | absuri | abspath | authority
709 */
710
711 if (*ptr == '*')
712 return NULL;
713
714 if (isalpha((unsigned char)*ptr)) {
715 /* this is a scheme as described by RFC3986, par. 3.1 */
716 ptr++;
717 while (ptr < end &&
718 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
719 ptr++;
720 /* skip '://' */
721 if (ptr == end || *ptr++ != ':')
722 return NULL;
723 if (ptr == end || *ptr++ != '/')
724 return NULL;
725 if (ptr == end || *ptr++ != '/')
726 return NULL;
727 }
728 /* skip [user[:passwd]@]host[:[port]] */
729
730 while (ptr < end && *ptr != '/')
731 ptr++;
732
733 if (ptr == end)
734 return NULL;
735
736 /* OK, we got the '/' ! */
737 return ptr;
738}
739
Willy Tarreauefb453c2008-10-26 20:49:47 +0100740/* Returns a 302 for a redirectable request. This may only be called just after
741 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
742 * left unchanged and will follow normal proxy processing.
743 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100745{
746 struct http_txn *txn;
747 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100748 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100749 char *path;
750 int len;
751
752 /* 1: create the response header */
753 rdr.len = strlen(HTTP_302);
754 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100755 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100756 memcpy(rdr.str, HTTP_302, rdr.len);
757
Willy Tarreau827aee92011-03-10 16:55:02 +0100758 srv = target_srv(&s->target);
759
Willy Tarreauefb453c2008-10-26 20:49:47 +0100760 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100761 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100762 return;
763
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100764 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100765 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
766 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
767 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100768 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100769
770 /* 3: add the request URI */
771 txn = &s->txn;
772 path = http_get_path(txn);
773 if (!path)
774 return;
775
Willy Tarreau962c3f42010-01-10 00:15:35 +0100776 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200777 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100778 return;
779
780 memcpy(rdr.str + rdr.len, path, len);
781 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100782
783 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
784 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
785 rdr.len += 29;
786 } else {
787 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
788 rdr.len += 23;
789 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100790
791 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100792 si->shutr(si);
793 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100794 si->err_type = SI_ET_NONE;
795 si->err_loc = NULL;
796 si->state = SI_ST_CLO;
797
798 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100799 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100800
801 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100802 if (srv)
803 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100804}
805
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100806/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100807 * that the server side is closed. Note that err_type is actually a
808 * bitmask, where almost only aborts may be cumulated with other
809 * values. We consider that aborted operations are more important
810 * than timeouts or errors due to the fact that nobody else in the
811 * logs might explain incomplete retries. All others should avoid
812 * being cumulated. It should normally not be possible to have multiple
813 * aborts at once, but just in case, the first one in sequence is reported.
814 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100815void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100816{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100817 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100818
819 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100820 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
821 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100822 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100823 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
824 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100825 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100826 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
827 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100828 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100829 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
830 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100831 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100832 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
833 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100834 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100835 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
836 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100837 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100838 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
839 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100840}
841
Willy Tarreau42250582007-04-01 01:30:43 +0200842extern const char sess_term_cond[8];
843extern const char sess_fin_state[8];
844extern const char *monthname[12];
Willy Tarreauf1348312010-10-07 15:54:11 +0200845const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */
846const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
847 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
848 Set-cookie Updated, unknown, unknown */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200849struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200850struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100851
Emeric Brun3a058f32009-06-30 18:26:00 +0200852void http_sess_clflog(struct session *s)
853{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100854 char pn[INET6_ADDRSTRLEN];
Emeric Brun3a058f32009-06-30 18:26:00 +0200855 struct proxy *fe = s->fe;
856 struct proxy *be = s->be;
857 struct proxy *prx_log;
858 struct http_txn *txn = &s->txn;
859 int tolog, level, err;
860 char *uri, *h;
Willy Tarreau71904a42011-02-13 14:30:26 +0100861 const char *svid;
Emeric Brun3a058f32009-06-30 18:26:00 +0200862 struct tm tm;
863 static char tmpline[MAX_SYSLOG_LEN];
864 int hdr;
865 size_t w;
866 int t_request;
867
868 prx_log = fe;
869 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +0200870 (s->req->cons->conn_retries != be->conn_retries) ||
Emeric Brun3a058f32009-06-30 18:26:00 +0200871 txn->status >= 500;
872
Willy Tarreau631f01c2011-09-05 00:36:48 +0200873 if (addr_to_str(&s->req->prod->addr.c.from, pn, sizeof(pn)) == AF_UNIX)
Emeric Brun5bd86a82010-10-22 17:23:04 +0200874 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
Emeric Brun3a058f32009-06-30 18:26:00 +0200875
876 get_gmtime(s->logs.accept_date.tv_sec, &tm);
877
878 /* FIXME: let's limit ourselves to frontend logging for now. */
879 tolog = fe->to_log;
880
881 h = tmpline;
882
883 w = snprintf(h, sizeof(tmpline),
884 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
885 pn,
886 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
887 tm.tm_hour, tm.tm_min, tm.tm_sec);
888 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
889 goto trunc;
890 h += w;
891
892 if (h >= tmpline + sizeof(tmpline) - 4)
893 goto trunc;
894
895 *(h++) = ' ';
896 *(h++) = '\"';
897 uri = txn->uri ? txn->uri : "<BADREQ>";
898 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
899 '#', url_encode_map, uri);
900 *(h++) = '\"';
901
902 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
903 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
904 goto trunc;
905 h += w;
906
907 if (h >= tmpline + sizeof(tmpline) - 9)
908 goto trunc;
909 memcpy(h, " \"-\" \"-\"", 8);
910 h += 8;
911
912 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
913 " %d %03d",
Willy Tarreau957c0a52011-03-03 17:42:23 +0100914 s->req->prod->addr.c.from.ss_family == AF_UNIX ? s->listener->luid :
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200915 get_host_port(&s->req->prod->addr.c.from),
Emeric Brun3a058f32009-06-30 18:26:00 +0200916 (int)s->logs.accept_date.tv_usec/1000);
917 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
918 goto trunc;
919 h += w;
920
921 w = strlen(fe->id);
922 if (h >= tmpline + sizeof(tmpline) - 4 - w)
923 goto trunc;
924 *(h++) = ' ';
925 *(h++) = '\"';
926 memcpy(h, fe->id, w);
927 h += w;
928 *(h++) = '\"';
929
930 w = strlen(be->id);
931 if (h >= tmpline + sizeof(tmpline) - 4 - w)
932 goto trunc;
933 *(h++) = ' ';
934 *(h++) = '\"';
935 memcpy(h, be->id, w);
936 h += w;
937 *(h++) = '\"';
938
Willy Tarreau71904a42011-02-13 14:30:26 +0100939 if (!(tolog & LW_SVID))
940 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200941 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100942 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200943 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100944 break;
945 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200946 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100947 break;
948 default:
949 svid = "<NOSRV>";
950 break;
951 }
Emeric Brun3a058f32009-06-30 18:26:00 +0200952
953 w = strlen(svid);
954 if (h >= tmpline + sizeof(tmpline) - 4 - w)
955 goto trunc;
956 *(h++) = ' ';
957 *(h++) = '\"';
958 memcpy(h, svid, w);
959 h += w;
960 *(h++) = '\"';
961
962 t_request = -1;
963 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
964 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
965 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
966 " %d %ld %ld %ld %ld",
967 t_request,
968 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
969 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
970 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
971 s->logs.t_close);
972 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
973 goto trunc;
974 h += w;
975
976 if (h >= tmpline + sizeof(tmpline) - 8)
977 goto trunc;
978 *(h++) = ' ';
979 *(h++) = '\"';
980 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
981 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
982 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
983 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
984 *(h++) = '\"';
985
986 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
987 " %d %d %d %d %d %ld %ld",
Willy Tarreau827aee92011-03-10 16:55:02 +0100988 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Willy Tarreauee28de02010-06-01 09:51:00 +0200989 (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries,
Emeric Brun3a058f32009-06-30 18:26:00 +0200990 s->logs.srv_queue_size, s->logs.prx_queue_size);
991
992 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
993 goto trunc;
994 h += w;
995
996 if (txn->cli_cookie) {
997 w = strlen(txn->cli_cookie);
998 if (h >= tmpline + sizeof(tmpline) - 4 - w)
999 goto trunc;
1000 *(h++) = ' ';
1001 *(h++) = '\"';
1002 memcpy(h, txn->cli_cookie, w);
1003 h += w;
1004 *(h++) = '\"';
1005 } else {
1006 if (h >= tmpline + sizeof(tmpline) - 5)
1007 goto trunc;
1008 memcpy(h, " \"-\"", 4);
1009 h += 4;
1010 }
1011
1012 if (txn->srv_cookie) {
1013 w = strlen(txn->srv_cookie);
1014 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1015 goto trunc;
1016 *(h++) = ' ';
1017 *(h++) = '\"';
1018 memcpy(h, txn->srv_cookie, w);
1019 h += w;
1020 *(h++) = '\"';
1021 } else {
1022 if (h >= tmpline + sizeof(tmpline) - 5)
1023 goto trunc;
1024 memcpy(h, " \"-\"", 4);
1025 h += 4;
1026 }
1027
1028 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1029 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1030 if (h >= sizeof (tmpline) + tmpline - 4)
1031 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001032 if (txn->req.cap[hdr] != NULL) {
1033 *(h++) = ' ';
1034 *(h++) = '\"';
1035 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1036 '#', hdr_encode_map, txn->req.cap[hdr]);
1037 *(h++) = '\"';
1038 } else {
1039 memcpy(h, " \"-\"", 4);
1040 h += 4;
1041 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001042 }
1043 }
1044
1045 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1046 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1047 if (h >= sizeof (tmpline) + tmpline - 4)
1048 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001049 if (txn->rsp.cap[hdr] != NULL) {
1050 *(h++) = ' ';
1051 *(h++) = '\"';
1052 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1053 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1054 *(h++) = '\"';
1055 } else {
1056 memcpy(h, " \"-\"", 4);
1057 h += 4;
1058 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001059 }
1060 }
1061
1062trunc:
1063 *h = '\0';
1064
1065 level = LOG_INFO;
1066 if (err && (fe->options2 & PR_O2_LOGERRORS))
1067 level = LOG_ERR;
1068
1069 send_log(prx_log, level, "%s\n", tmpline);
1070
1071 s->logs.logwait = 0;
1072}
1073
Willy Tarreau42250582007-04-01 01:30:43 +02001074/*
1075 * send a log for the session when we have enough info about it.
1076 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001078void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001079{
Cyril Bontéacd7d632010-11-01 19:26:02 +01001080 char pn[INET6_ADDRSTRLEN];
Willy Tarreau42250582007-04-01 01:30:43 +02001081 struct proxy *fe = s->fe;
1082 struct proxy *be = s->be;
1083 struct proxy *prx_log;
1084 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001085 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001086 char *uri, *h;
Willy Tarreau71904a42011-02-13 14:30:26 +01001087 const char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001088 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001089 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001090 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001091 int hdr;
1092
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001093 /* if we don't want to log normal traffic, return now */
1094 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +02001095 (s->req->cons->conn_retries != be->conn_retries) ||
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001096 txn->status >= 500;
1097 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1098 return;
1099
Willy Tarreau42250582007-04-01 01:30:43 +02001100 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1101 return;
1102 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103
Emeric Brun3a058f32009-06-30 18:26:00 +02001104 if (prx_log->options2 & PR_O2_CLFLOG)
1105 return http_sess_clflog(s);
1106
Willy Tarreau631f01c2011-09-05 00:36:48 +02001107 if (addr_to_str(&s->req->prod->addr.c.from, pn, sizeof(pn)) == AF_UNIX)
1108 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
Willy Tarreau42250582007-04-01 01:30:43 +02001109
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001110 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001111
1112 /* FIXME: let's limit ourselves to frontend logging for now. */
1113 tolog = fe->to_log;
1114
1115 h = tmpline;
1116 if (fe->to_log & LW_REQHDR &&
1117 txn->req.cap &&
1118 (h < tmpline + sizeof(tmpline) - 10)) {
1119 *(h++) = ' ';
1120 *(h++) = '{';
1121 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1122 if (hdr)
1123 *(h++) = '|';
1124 if (txn->req.cap[hdr] != NULL)
1125 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1126 '#', hdr_encode_map, txn->req.cap[hdr]);
1127 }
1128 *(h++) = '}';
1129 }
1130
1131 if (fe->to_log & LW_RSPHDR &&
1132 txn->rsp.cap &&
1133 (h < tmpline + sizeof(tmpline) - 7)) {
1134 *(h++) = ' ';
1135 *(h++) = '{';
1136 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1137 if (hdr)
1138 *(h++) = '|';
1139 if (txn->rsp.cap[hdr] != NULL)
1140 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1141 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1142 }
1143 *(h++) = '}';
1144 }
1145
1146 if (h < tmpline + sizeof(tmpline) - 4) {
1147 *(h++) = ' ';
1148 *(h++) = '"';
1149 uri = txn->uri ? txn->uri : "<BADREQ>";
1150 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1151 '#', url_encode_map, uri);
1152 *(h++) = '"';
1153 }
1154 *h = '\0';
1155
Willy Tarreau71904a42011-02-13 14:30:26 +01001156 if (!(tolog & LW_SVID))
1157 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001158 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +01001159 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001160 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +01001161 break;
1162 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001163 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +01001164 break;
1165 default:
1166 svid = "<NOSRV>";
1167 break;
1168 }
Willy Tarreau42250582007-04-01 01:30:43 +02001169
Willy Tarreau70089872008-06-13 21:12:51 +02001170 t_request = -1;
1171 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1172 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1173
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001174 level = LOG_INFO;
1175 if (err && (fe->options2 & PR_O2_LOGERRORS))
1176 level = LOG_ERR;
1177
1178 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001179 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001180 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1181 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau957c0a52011-03-03 17:42:23 +01001182 (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? "unix" : pn,
1183 (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? s->listener->luid :
Willy Tarreau86ad42c2011-08-27 12:29:07 +02001184 get_host_port(&s->req->prod->addr.c.from),
Willy Tarreaufe944602007-10-25 10:34:16 +02001185 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001186 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001187 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001188 t_request,
1189 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001190 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1191 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1192 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1193 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001194 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001195 txn->cli_cookie ? txn->cli_cookie : "-",
1196 txn->srv_cookie ? txn->srv_cookie : "-",
1197 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1198 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1199 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1200 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
Willy Tarreau827aee92011-03-10 16:55:02 +01001201 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001202 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +02001203 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001204 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1205
1206 s->logs.logwait = 0;
1207}
1208
Willy Tarreau117f59e2007-03-04 18:17:17 +01001209
1210/*
1211 * Capture headers from message starting at <som> according to header list
1212 * <cap_hdr>, and fill the <idx> structure appropriately.
1213 */
1214void capture_headers(char *som, struct hdr_idx *idx,
1215 char **cap, struct cap_hdr *cap_hdr)
1216{
1217 char *eol, *sol, *col, *sov;
1218 int cur_idx;
1219 struct cap_hdr *h;
1220 int len;
1221
1222 sol = som + hdr_idx_first_pos(idx);
1223 cur_idx = hdr_idx_first_idx(idx);
1224
1225 while (cur_idx) {
1226 eol = sol + idx->v[cur_idx].len;
1227
1228 col = sol;
1229 while (col < eol && *col != ':')
1230 col++;
1231
1232 sov = col + 1;
1233 while (sov < eol && http_is_lws[(unsigned char)*sov])
1234 sov++;
1235
1236 for (h = cap_hdr; h; h = h->next) {
1237 if ((h->namelen == col - sol) &&
1238 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1239 if (cap[h->index] == NULL)
1240 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001241 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001242
1243 if (cap[h->index] == NULL) {
1244 Alert("HTTP capture : out of memory.\n");
1245 continue;
1246 }
1247
1248 len = eol - sov;
1249 if (len > h->len)
1250 len = h->len;
1251
1252 memcpy(cap[h->index], sov, len);
1253 cap[h->index][len]=0;
1254 }
1255 }
1256 sol = eol + idx->v[cur_idx].cr + 1;
1257 cur_idx = idx->v[cur_idx].next;
1258 }
1259}
1260
1261
Willy Tarreau42250582007-04-01 01:30:43 +02001262/* either we find an LF at <ptr> or we jump to <bad>.
1263 */
1264#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1265
1266/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1267 * otherwise to <http_msg_ood> with <state> set to <st>.
1268 */
1269#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1270 ptr++; \
1271 if (likely(ptr < end)) \
1272 goto good; \
1273 else { \
1274 state = (st); \
1275 goto http_msg_ood; \
1276 } \
1277 } while (0)
1278
1279
Willy Tarreaubaaee002006-06-26 02:48:02 +02001280/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001281 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001282 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1283 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1284 * will give undefined results.
1285 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1286 * and that msg->sol points to the beginning of the response.
1287 * If a complete line is found (which implies that at least one CR or LF is
1288 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1289 * returned indicating an incomplete line (which does not mean that parts have
1290 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1291 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1292 * upon next call.
1293 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001294 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1296 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001297 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001299const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1300 unsigned int state, const char *ptr, const char *end,
1301 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001302{
Willy Tarreau8973c702007-01-21 23:58:29 +01001303 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001304 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001305 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001306 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001307 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1308
1309 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001310 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1312 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001313 state = HTTP_MSG_ERROR;
1314 break;
1315
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001317 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001318 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001319 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001320 goto http_msg_rpcode;
1321 }
1322 if (likely(HTTP_IS_SPHT(*ptr)))
1323 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1324 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001325 state = HTTP_MSG_ERROR;
1326 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001327
Willy Tarreau8973c702007-01-21 23:58:29 +01001328 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001329 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001330 if (likely(!HTTP_IS_LWS(*ptr)))
1331 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1332
1333 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001334 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001335 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1336 }
1337
1338 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001339 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 http_msg_rsp_reason:
1341 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001342 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 msg->sl.st.r_l = 0;
1344 goto http_msg_rpline_eol;
1345
Willy Tarreau8973c702007-01-21 23:58:29 +01001346 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001347 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001349 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 goto http_msg_rpreason;
1351 }
1352 if (likely(HTTP_IS_SPHT(*ptr)))
1353 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1354 /* so it's a CR/LF, so there is no reason phrase */
1355 goto http_msg_rsp_reason;
1356
Willy Tarreau8973c702007-01-21 23:58:29 +01001357 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001358 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001359 if (likely(!HTTP_IS_CRLF(*ptr)))
1360 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001361 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 http_msg_rpline_eol:
1363 /* We have seen the end of line. Note that we do not
1364 * necessarily have the \n yet, but at least we know that we
1365 * have EITHER \r OR \n, otherwise the response would not be
1366 * complete. We can then record the response length and return
1367 * to the caller which will be able to register it.
1368 */
1369 msg->sl.st.l = ptr - msg->sol;
1370 return ptr;
1371
1372#ifdef DEBUG_FULL
1373 default:
1374 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1375 exit(1);
1376#endif
1377 }
1378
1379 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001380 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001381 if (ret_state)
1382 *ret_state = state;
1383 if (ret_ptr)
1384 *ret_ptr = (char *)ptr;
1385 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001386}
1387
Willy Tarreau8973c702007-01-21 23:58:29 +01001388/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 * This function parses a request line between <ptr> and <end>, starting with
1390 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1391 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1392 * will give undefined results.
1393 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1394 * and that msg->sol points to the beginning of the request.
1395 * If a complete line is found (which implies that at least one CR or LF is
1396 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1397 * returned indicating an incomplete line (which does not mean that parts have
1398 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1399 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1400 * upon next call.
1401 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001402 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1404 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001405 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001406 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001407const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1408 unsigned int state, const char *ptr, const char *end,
1409 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001410{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001413 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 if (likely(HTTP_IS_TOKEN(*ptr)))
1415 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001416
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001418 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1420 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001421
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 if (likely(HTTP_IS_CRLF(*ptr))) {
1423 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001424 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001426 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001428 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001430 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 msg->sl.rq.v_l = 0;
1432 goto http_msg_rqline_eol;
1433 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001434 state = HTTP_MSG_ERROR;
1435 break;
1436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001438 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001440 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 goto http_msg_rquri;
1442 }
1443 if (likely(HTTP_IS_SPHT(*ptr)))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1445 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1446 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001449 http_msg_rquri:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 if (likely(!HTTP_IS_LWS(*ptr)))
1451 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001454 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1456 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001457
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1459 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001462 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001464 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 goto http_msg_rqver;
1466 }
1467 if (likely(HTTP_IS_SPHT(*ptr)))
1468 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1469 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1470 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001471
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001473 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001474 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001476
1477 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001478 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001479 http_msg_rqline_eol:
1480 /* We have seen the end of line. Note that we do not
1481 * necessarily have the \n yet, but at least we know that we
1482 * have EITHER \r OR \n, otherwise the request would not be
1483 * complete. We can then record the request length and return
1484 * to the caller which will be able to register it.
1485 */
1486 msg->sl.rq.l = ptr - msg->sol;
1487 return ptr;
1488 }
1489
1490 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001491 state = HTTP_MSG_ERROR;
1492 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001493
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494#ifdef DEBUG_FULL
1495 default:
1496 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1497 exit(1);
1498#endif
1499 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001500
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001502 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 if (ret_state)
1504 *ret_state = state;
1505 if (ret_ptr)
1506 *ret_ptr = (char *)ptr;
1507 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001509
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001510/*
1511 * Returns the data from Authorization header. Function may be called more
1512 * than once so data is stored in txn->auth_data. When no header is found
1513 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1514 * searching again for something we are unable to find anyway.
1515 */
1516
1517char get_http_auth_buff[BUFSIZE];
1518
1519int
1520get_http_auth(struct session *s)
1521{
1522
1523 struct http_txn *txn = &s->txn;
1524 struct chunk auth_method;
1525 struct hdr_ctx ctx;
1526 char *h, *p;
1527 int len;
1528
1529#ifdef DEBUG_AUTH
1530 printf("Auth for session %p: %d\n", s, txn->auth.method);
1531#endif
1532
1533 if (txn->auth.method == HTTP_AUTH_WRONG)
1534 return 0;
1535
1536 if (txn->auth.method)
1537 return 1;
1538
1539 txn->auth.method = HTTP_AUTH_WRONG;
1540
1541 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001542
1543 if (txn->flags & TX_USE_PX_CONN) {
1544 h = "Proxy-Authorization";
1545 len = strlen(h);
1546 } else {
1547 h = "Authorization";
1548 len = strlen(h);
1549 }
1550
1551 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001552 return 0;
1553
1554 h = ctx.line + ctx.val;
1555
1556 p = memchr(h, ' ', ctx.vlen);
1557 if (!p || p == h)
1558 return 0;
1559
1560 chunk_initlen(&auth_method, h, 0, p-h);
1561 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1562
1563 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1564
1565 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1566 get_http_auth_buff, BUFSIZE - 1);
1567
1568 if (len < 0)
1569 return 0;
1570
1571
1572 get_http_auth_buff[len] = '\0';
1573
1574 p = strchr(get_http_auth_buff, ':');
1575
1576 if (!p)
1577 return 0;
1578
1579 txn->auth.user = get_http_auth_buff;
1580 *p = '\0';
1581 txn->auth.pass = p+1;
1582
1583 txn->auth.method = HTTP_AUTH_BASIC;
1584 return 1;
1585 }
1586
1587 return 0;
1588}
1589
Willy Tarreau58f10d72006-12-04 02:26:12 +01001590
Willy Tarreau8973c702007-01-21 23:58:29 +01001591/*
1592 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001593 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001594 * when data are missing and recalled at the exact same location with no
1595 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001596 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001597 * fields. Note that msg->som and msg->sol will be initialized after completing
1598 * the first state, so that none of the msg pointers has to be initialized
1599 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001600 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1602{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001603 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001604 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001605
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001606 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 ptr = buf->lr;
1608 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001609
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 if (unlikely(ptr >= end))
1611 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001612
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001613 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001614 /*
1615 * First, states that are specific to the response only.
1616 * We check them first so that request and headers are
1617 * closer to each other (accessed more often).
1618 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001619 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001620 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001621 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001622 /* we have a start of message, but we have to check
1623 * first if we need to remove some CRLF. We can only
1624 * do this when send_max=0.
1625 */
1626 char *beg = buf->w + buf->send_max;
1627 if (beg >= buf->data + buf->size)
1628 beg -= buf->size;
1629 if (unlikely(ptr != beg)) {
1630 if (buf->send_max)
1631 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001632 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001633 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001634 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001635 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001636 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001637 hdr_idx_init(idx);
1638 state = HTTP_MSG_RPVER;
1639 goto http_msg_rpver;
1640 }
1641
1642 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1643 goto http_msg_invalid;
1644
1645 if (unlikely(*ptr == '\n'))
1646 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1647 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1648 /* stop here */
1649
Willy Tarreau8973c702007-01-21 23:58:29 +01001650 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001651 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001652 EXPECT_LF_HERE(ptr, http_msg_invalid);
1653 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1654 /* stop here */
1655
Willy Tarreau8973c702007-01-21 23:58:29 +01001656 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001657 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001658 case HTTP_MSG_RPVER_SP:
1659 case HTTP_MSG_RPCODE:
1660 case HTTP_MSG_RPCODE_SP:
1661 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001662 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001663 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001664 if (unlikely(!ptr))
1665 return;
1666
1667 /* we have a full response and we know that we have either a CR
1668 * or an LF at <ptr>.
1669 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001670 //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 +01001671 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1672
1673 msg->sol = ptr;
1674 if (likely(*ptr == '\r'))
1675 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1676 goto http_msg_rpline_end;
1677
Willy Tarreau8973c702007-01-21 23:58:29 +01001678 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001679 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001680 /* msg->sol must point to the first of CR or LF. */
1681 EXPECT_LF_HERE(ptr, http_msg_invalid);
1682 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1683 /* stop here */
1684
1685 /*
1686 * Second, states that are specific to the request only
1687 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001689 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001691 /* we have a start of message, but we have to check
1692 * first if we need to remove some CRLF. We can only
1693 * do this when send_max=0.
1694 */
1695 char *beg = buf->w + buf->send_max;
1696 if (beg >= buf->data + buf->size)
1697 beg -= buf->size;
1698 if (likely(ptr != beg)) {
1699 if (buf->send_max)
1700 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001701 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001702 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001704 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001705 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001706 /* we will need this when keep-alive will be supported
1707 hdr_idx_init(idx);
1708 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001709 state = HTTP_MSG_RQMETH;
1710 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001711 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001712
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1714 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001715
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 if (unlikely(*ptr == '\n'))
1717 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1718 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001719 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001720
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001722 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 EXPECT_LF_HERE(ptr, http_msg_invalid);
1724 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001725 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001726
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001728 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 case HTTP_MSG_RQMETH_SP:
1730 case HTTP_MSG_RQURI:
1731 case HTTP_MSG_RQURI_SP:
1732 case HTTP_MSG_RQVER:
1733 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001734 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001735 if (unlikely(!ptr))
1736 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001737
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 /* we have a full request and we know that we have either a CR
1739 * or an LF at <ptr>.
1740 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001741 //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 +01001742 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001743
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 msg->sol = ptr;
1745 if (likely(*ptr == '\r'))
1746 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001748
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001750 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 /* check for HTTP/0.9 request : no version information available.
1752 * msg->sol must point to the first of CR or LF.
1753 */
1754 if (unlikely(msg->sl.rq.v_l == 0))
1755 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001756
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 EXPECT_LF_HERE(ptr, http_msg_invalid);
1758 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001759 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001760
Willy Tarreau8973c702007-01-21 23:58:29 +01001761 /*
1762 * Common states below
1763 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001765 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001766 msg->sol = ptr;
1767 if (likely(!HTTP_IS_CRLF(*ptr))) {
1768 goto http_msg_hdr_name;
1769 }
1770
1771 if (likely(*ptr == '\r'))
1772 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1773 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001774
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001776 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 /* assumes msg->sol points to the first char */
1778 if (likely(HTTP_IS_TOKEN(*ptr)))
1779 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001780
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001781 if (likely(*ptr == ':')) {
1782 msg->col = ptr - buf->data;
1783 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1784 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001785
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001786 if (likely(msg->err_pos < -1) || *ptr == '\n')
1787 goto http_msg_invalid;
1788
1789 if (msg->err_pos == -1) /* capture error pointer */
1790 msg->err_pos = ptr - buf->data; /* >= 0 now */
1791
1792 /* and we still accept this non-token character */
1793 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001794
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001795 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001796 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 /* assumes msg->sol points to the first char and msg->col to the colon */
1798 if (likely(HTTP_IS_SPHT(*ptr)))
1799 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001800
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001801 /* header value can be basically anything except CR/LF */
1802 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001804 if (likely(!HTTP_IS_CRLF(*ptr))) {
1805 goto http_msg_hdr_val;
1806 }
1807
1808 if (likely(*ptr == '\r'))
1809 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1810 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001811
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001812 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001813 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 EXPECT_LF_HERE(ptr, http_msg_invalid);
1815 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001816
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001817 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001818 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001819 if (likely(HTTP_IS_SPHT(*ptr))) {
1820 /* replace HT,CR,LF with spaces */
1821 for (; buf->data+msg->sov < ptr; msg->sov++)
1822 buf->data[msg->sov] = ' ';
1823 goto http_msg_hdr_l1_sp;
1824 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001825 /* we had a header consisting only in spaces ! */
1826 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001827 goto http_msg_complete_header;
1828
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001829 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001830 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 /* assumes msg->sol points to the first char, msg->col to the
1832 * colon, and msg->sov points to the first character of the
1833 * value.
1834 */
1835 if (likely(!HTTP_IS_CRLF(*ptr)))
1836 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001837
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 msg->eol = ptr;
1839 /* Note: we could also copy eol into ->eoh so that we have the
1840 * real header end in case it ends with lots of LWS, but is this
1841 * really needed ?
1842 */
1843 if (likely(*ptr == '\r'))
1844 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1845 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001846
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001847 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001848 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 EXPECT_LF_HERE(ptr, http_msg_invalid);
1850 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001851
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001852 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001853 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001854 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1855 /* LWS: replace HT,CR,LF with spaces */
1856 for (; msg->eol < ptr; msg->eol++)
1857 *msg->eol = ' ';
1858 goto http_msg_hdr_val;
1859 }
1860 http_msg_complete_header:
1861 /*
1862 * It was a new header, so the last one is finished.
1863 * Assumes msg->sol points to the first char, msg->col to the
1864 * colon, msg->sov points to the first character of the value
1865 * and msg->eol to the first CR or LF so we know how the line
1866 * ends. We insert last header into the index.
1867 */
1868 /*
1869 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1870 write(2, msg->sol, msg->eol-msg->sol);
1871 fprintf(stderr,"\n");
1872 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001873
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001874 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1875 idx, idx->tail) < 0))
1876 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001877
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001878 msg->sol = ptr;
1879 if (likely(!HTTP_IS_CRLF(*ptr))) {
1880 goto http_msg_hdr_name;
1881 }
1882
1883 if (likely(*ptr == '\r'))
1884 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1885 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001886
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001888 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001889 /* Assumes msg->sol points to the first of either CR or LF */
1890 EXPECT_LF_HERE(ptr, http_msg_invalid);
1891 ptr++;
1892 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001893 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001894 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001895 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001896 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001897 return;
1898#ifdef DEBUG_FULL
1899 default:
1900 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1901 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001902#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001903 }
1904 http_msg_ood:
1905 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001906 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001907 buf->lr = ptr;
1908 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001909
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001910 http_msg_invalid:
1911 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001912 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001913 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001914 return;
1915}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001916
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001917/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1918 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1919 * nothing is done and 1 is returned.
1920 */
1921static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1922{
1923 int delta;
1924 char *cur_end;
1925
1926 if (msg->sl.rq.v_l != 0)
1927 return 1;
1928
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001929 cur_end = msg->sol + msg->sl.rq.l;
1930 delta = 0;
1931
1932 if (msg->sl.rq.u_l == 0) {
1933 /* if no URI was set, add "/" */
1934 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1935 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001936 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001937 }
1938 /* add HTTP version */
1939 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001940 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001941 cur_end += delta;
1942 cur_end = (char *)http_parse_reqline(msg, req->data,
1943 HTTP_MSG_RQMETH,
1944 msg->sol, cur_end + 1,
1945 NULL, NULL);
1946 if (unlikely(!cur_end))
1947 return 0;
1948
1949 /* we have a full HTTP/1.0 request now and we know that
1950 * we have either a CR or an LF at <ptr>.
1951 */
1952 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1953 return 1;
1954}
1955
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001956/* Parse the Connection: header of an HTTP request, looking for both "close"
1957 * and "keep-alive" values. If a buffer is provided and we already know that
1958 * some headers may safely be removed, we remove them now. The <to_del> flags
1959 * are used for that :
1960 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1961 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1962 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1963 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1964 * harmless combinations may be removed. Do not call that after changes have
1965 * been processed. If unused, the buffer can be NULL, and no data will be
1966 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001967 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001968void 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 +01001969{
Willy Tarreau5b154472009-12-21 20:11:07 +01001970 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001971 const char *hdr_val = "Connection";
1972 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001973
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001974 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001975 return;
1976
Willy Tarreau88d349d2010-01-25 12:15:43 +01001977 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1978 hdr_val = "Proxy-Connection";
1979 hdr_len = 16;
1980 }
1981
Willy Tarreau5b154472009-12-21 20:11:07 +01001982 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001983 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001984 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001985 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1986 txn->flags |= TX_HDR_CONN_KAL;
1987 if ((to_del & 2) && buf)
1988 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1989 else
1990 txn->flags |= TX_CON_KAL_SET;
1991 }
1992 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1993 txn->flags |= TX_HDR_CONN_CLO;
1994 if ((to_del & 1) && buf)
1995 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1996 else
1997 txn->flags |= TX_CON_CLO_SET;
1998 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001999 }
2000
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002001 txn->flags |= TX_HDR_CONN_PRS;
2002 return;
2003}
Willy Tarreau5b154472009-12-21 20:11:07 +01002004
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002005/* Apply desired changes on the Connection: header. Values may be removed and/or
2006 * added depending on the <wanted> flags, which are exclusively composed of
2007 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2008 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2009 */
2010void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
2011{
2012 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002013 const char *hdr_val = "Connection";
2014 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002015
2016 ctx.idx = 0;
2017
Willy Tarreau88d349d2010-01-25 12:15:43 +01002018
2019 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2020 hdr_val = "Proxy-Connection";
2021 hdr_len = 16;
2022 }
2023
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002024 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002025 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002026 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2027 if (wanted & TX_CON_KAL_SET)
2028 txn->flags |= TX_CON_KAL_SET;
2029 else
2030 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002031 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002032 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2033 if (wanted & TX_CON_CLO_SET)
2034 txn->flags |= TX_CON_CLO_SET;
2035 else
2036 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002037 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002038 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002039
2040 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2041 return;
2042
2043 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2044 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002045 hdr_val = "Connection: close";
2046 hdr_len = 17;
2047 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2048 hdr_val = "Proxy-Connection: close";
2049 hdr_len = 23;
2050 }
2051 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002052 }
2053
2054 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2055 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002056 hdr_val = "Connection: keep-alive";
2057 hdr_len = 22;
2058 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2059 hdr_val = "Proxy-Connection: keep-alive";
2060 hdr_len = 28;
2061 }
2062 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002063 }
2064 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002065}
2066
Willy Tarreaud98cf932009-12-27 22:54:55 +01002067/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2068 * first byte of body, and increments msg->sov by the number of bytes parsed,
2069 * so that we know we can forward between ->som and ->sov. Note that due to
2070 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2071 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002072 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002073 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002074 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002075int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002076{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002077 char *ptr = buf->lr;
2078 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002079 unsigned int chunk = 0;
2080
2081 /* The chunk size is in the following form, though we are only
2082 * interested in the size and CRLF :
2083 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2084 */
2085 while (1) {
2086 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002087 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002088 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002089 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002090 if (c < 0) /* not a hex digit anymore */
2091 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002092 if (++ptr >= end)
2093 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002094 if (chunk & 0xF000000) /* overflow will occur */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002095 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002096 chunk = (chunk << 4) + c;
2097 }
2098
Willy Tarreaud98cf932009-12-27 22:54:55 +01002099 /* empty size not allowed */
2100 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002101 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002102
2103 while (http_is_spht[(unsigned char)*ptr]) {
2104 if (++ptr >= end)
2105 ptr = buf->data;
2106 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002107 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002108 }
2109
Willy Tarreaud98cf932009-12-27 22:54:55 +01002110 /* Up to there, we know that at least one byte is present at *ptr. Check
2111 * for the end of chunk size.
2112 */
2113 while (1) {
2114 if (likely(HTTP_IS_CRLF(*ptr))) {
2115 /* we now have a CR or an LF at ptr */
2116 if (likely(*ptr == '\r')) {
2117 if (++ptr >= end)
2118 ptr = buf->data;
2119 if (ptr == buf->r)
2120 return 0;
2121 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002122
Willy Tarreaud98cf932009-12-27 22:54:55 +01002123 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002124 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002125 if (++ptr >= end)
2126 ptr = buf->data;
2127 /* done */
2128 break;
2129 }
2130 else if (*ptr == ';') {
2131 /* chunk extension, ends at next CRLF */
2132 if (++ptr >= end)
2133 ptr = buf->data;
2134 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002135 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002136
2137 while (!HTTP_IS_CRLF(*ptr)) {
2138 if (++ptr >= end)
2139 ptr = buf->data;
2140 if (ptr == buf->r)
2141 return 0;
2142 }
2143 /* we have a CRLF now, loop above */
2144 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002145 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002146 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002147 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002148 }
2149
Willy Tarreaud98cf932009-12-27 22:54:55 +01002150 /* OK we found our CRLF and now <ptr> points to the next byte,
2151 * which may or may not be present. We save that into ->lr and
2152 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002153 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002154 msg->sov += ptr - buf->lr;
2155 buf->lr = ptr;
Willy Tarreau124d9912011-03-01 20:30:48 +01002156 msg->chunk_len = chunk;
2157 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002158 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002159 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002160 error:
2161 msg->err_pos = ptr - buf->data;
2162 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002163}
2164
Willy Tarreaud98cf932009-12-27 22:54:55 +01002165/* This function skips trailers in the buffer <buf> associated with HTTP
2166 * message <msg>. The first visited position is buf->lr. If the end of
2167 * the trailers is found, it is automatically scheduled to be forwarded,
2168 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2169 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002170 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2171 * zero. If a parse error is encountered, the function returns < 0 and does not
2172 * change anything except maybe buf->lr and msg->sov. Note that the message
2173 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2174 * which implies that all non-trailers data have already been scheduled for
2175 * forwarding, and that the difference between msg->som and msg->sov exactly
2176 * matches the length of trailers already parsed and not forwarded. It is also
2177 * important to note that this function is designed to be able to parse wrapped
2178 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002179 */
2180int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2181{
2182 /* we have buf->lr which points to next line. Look for CRLF. */
2183 while (1) {
2184 char *p1 = NULL, *p2 = NULL;
2185 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002186 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002187
2188 /* scan current line and stop at LF or CRLF */
2189 while (1) {
2190 if (ptr == buf->r)
2191 return 0;
2192
2193 if (*ptr == '\n') {
2194 if (!p1)
2195 p1 = ptr;
2196 p2 = ptr;
2197 break;
2198 }
2199
2200 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002201 if (p1) {
2202 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002203 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002204 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002205 p1 = ptr;
2206 }
2207
2208 ptr++;
2209 if (ptr >= buf->data + buf->size)
2210 ptr = buf->data;
2211 }
2212
2213 /* after LF; point to beginning of next line */
2214 p2++;
2215 if (p2 >= buf->data + buf->size)
2216 p2 = buf->data;
2217
Willy Tarreau638cd022010-01-03 07:42:04 +01002218 bytes = p2 - buf->lr;
2219 if (bytes < 0)
2220 bytes += buf->size;
2221
2222 /* schedule this line for forwarding */
2223 msg->sov += bytes;
2224 if (msg->sov >= buf->size)
2225 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002226
Willy Tarreau638cd022010-01-03 07:42:04 +01002227 if (p1 == buf->lr) {
2228 /* LF/CRLF at beginning of line => end of trailers at p2.
2229 * Everything was scheduled for forwarding, there's nothing
2230 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002231 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002232 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002233 msg->msg_state = HTTP_MSG_DONE;
2234 return 1;
2235 }
2236 /* OK, next line then */
2237 buf->lr = p2;
2238 }
2239}
2240
2241/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2242 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2243 * ->som, buf->lr in order to include this part into the next forwarding phase.
2244 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2245 * not enough data are available, the function does not change anything and
2246 * returns zero. If a parse error is encountered, the function returns < 0 and
2247 * does not change anything. Note: this function is designed to parse wrapped
2248 * CRLF at the end of the buffer.
2249 */
2250int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2251{
2252 char *ptr;
2253 int bytes;
2254
2255 /* NB: we'll check data availabilty at the end. It's not a
2256 * problem because whatever we match first will be checked
2257 * against the correct length.
2258 */
2259 bytes = 1;
2260 ptr = buf->lr;
2261 if (*ptr == '\r') {
2262 bytes++;
2263 ptr++;
2264 if (ptr >= buf->data + buf->size)
2265 ptr = buf->data;
2266 }
2267
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002268 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002269 return 0;
2270
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002271 if (*ptr != '\n') {
2272 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002273 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002274 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002275
2276 ptr++;
2277 if (ptr >= buf->data + buf->size)
2278 ptr = buf->data;
2279 buf->lr = ptr;
2280 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2281 msg->sov = ptr - buf->data;
2282 msg->som = msg->sov - bytes;
2283 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2284 return 1;
2285}
Willy Tarreau5b154472009-12-21 20:11:07 +01002286
Willy Tarreau83e3af02009-12-28 17:39:57 +01002287void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2288{
2289 char *end = buf->data + buf->size;
2290 int off = buf->data + buf->size - buf->w;
2291
2292 /* two possible cases :
2293 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002294 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002295 */
2296 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002297 int block1 = buf->l;
2298 int block2 = 0;
2299 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002300 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002301 block1 = buf->data + buf->size - buf->w;
2302 block2 = buf->r - buf->data;
2303 }
2304 if (block2)
2305 memcpy(swap_buffer, buf->data, block2);
2306 memmove(buf->data, buf->w, block1);
2307 if (block2)
2308 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002309 }
2310
2311 /* adjust all known pointers */
2312 buf->w = buf->data;
2313 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2314 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2315 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2316 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2317
2318 /* adjust relative pointers */
2319 msg->som = 0;
2320 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2321 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2322 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2323
Willy Tarreau83e3af02009-12-28 17:39:57 +01002324 if (msg->err_pos >= 0) {
2325 msg->err_pos += off;
2326 if (msg->err_pos >= buf->size)
2327 msg->err_pos -= buf->size;
2328 }
2329
2330 buf->flags &= ~BF_FULL;
2331 if (buf->l >= buffer_max_len(buf))
2332 buf->flags |= BF_FULL;
2333}
2334
Willy Tarreaud787e662009-07-07 10:14:51 +02002335/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2336 * processing can continue on next analysers, or zero if it either needs more
2337 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2338 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2339 * when it has nothing left to do, and may remove any analyser when it wants to
2340 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002341 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002342int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002343{
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 /*
2345 * We will parse the partial (or complete) lines.
2346 * We will check the request syntax, and also join multi-line
2347 * headers. An index of all the lines will be elaborated while
2348 * parsing.
2349 *
2350 * For the parsing, we use a 28 states FSM.
2351 *
2352 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002353 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002354 * req->data + msg->eoh = end of processed headers / start of current one
2355 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002356 * req->lr = first non-visited byte
2357 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002358 *
2359 * At end of parsing, we may perform a capture of the error (if any), and
2360 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2361 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2362 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002364
Willy Tarreau59234e92008-11-30 23:51:27 +01002365 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002366 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002367 struct http_txn *txn = &s->txn;
2368 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002369 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002370
Willy Tarreau6bf17362009-02-24 10:48:35 +01002371 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2372 now_ms, __FUNCTION__,
2373 s,
2374 req,
2375 req->rex, req->wex,
2376 req->flags,
2377 req->l,
2378 req->analysers);
2379
Willy Tarreau52a0c602009-08-16 22:45:38 +02002380 /* we're speaking HTTP here, so let's speak HTTP to the client */
2381 s->srv_error = http_return_srv_error;
2382
Willy Tarreau83e3af02009-12-28 17:39:57 +01002383 /* There's a protected area at the end of the buffer for rewriting
2384 * purposes. We don't want to start to parse the request if the
2385 * protected area is affected, because we may have to move processed
2386 * data later, which is much more complicated.
2387 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002388 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002389 if ((txn->flags & TX_NOT_FIRST) &&
2390 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002391 req->r < req->lr ||
2392 req->r > req->data + req->size - global.tune.maxrewrite)) {
2393 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002394 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2395 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002396 /* some data has still not left the buffer, wake us once that's done */
2397 buffer_dont_connect(req);
2398 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2399 return 0;
2400 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002401 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002402 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002403 }
2404
Willy Tarreau065e8332010-01-08 00:30:20 +01002405 /* Note that we have the same problem with the response ; we
2406 * may want to send a redirect, error or anything which requires
2407 * some spare space. So we'll ensure that we have at least
2408 * maxrewrite bytes available in the response buffer before
2409 * processing that one. This will only affect pipelined
2410 * keep-alive requests.
2411 */
2412 if ((txn->flags & TX_NOT_FIRST) &&
2413 unlikely((s->rep->flags & BF_FULL) ||
2414 s->rep->r < s->rep->lr ||
2415 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2416 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002417 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2418 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002419 /* don't let a connection request be initiated */
2420 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002421 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002422 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002423 return 0;
2424 }
2425 }
2426
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002427 if (likely(req->lr < req->r))
2428 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002429 }
2430
Willy Tarreau59234e92008-11-30 23:51:27 +01002431 /* 1: we might have to print this header in debug mode */
2432 if (unlikely((global.mode & MODE_DEBUG) &&
2433 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002434 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002435 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002436 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002437
Willy Tarreau663308b2010-06-07 14:06:08 +02002438 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002439 eol = sol + msg->sl.rq.l;
2440 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002441
Willy Tarreau59234e92008-11-30 23:51:27 +01002442 sol += hdr_idx_first_pos(&txn->hdr_idx);
2443 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002444
Willy Tarreau59234e92008-11-30 23:51:27 +01002445 while (cur_idx) {
2446 eol = sol + txn->hdr_idx.v[cur_idx].len;
2447 debug_hdr("clihdr", s, sol, eol);
2448 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2449 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002450 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002451 }
2452
Willy Tarreau58f10d72006-12-04 02:26:12 +01002453
Willy Tarreau59234e92008-11-30 23:51:27 +01002454 /*
2455 * Now we quickly check if we have found a full valid request.
2456 * If not so, we check the FD and buffer states before leaving.
2457 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002458 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002459 * requests are checked first. When waiting for a second request
2460 * on a keep-alive session, if we encounter and error, close, t/o,
2461 * we note the error in the session flags but don't set any state.
2462 * Since the error will be noted there, it will not be counted by
2463 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002464 * Last, we may increase some tracked counters' http request errors on
2465 * the cases that are deliberately the client's fault. For instance,
2466 * a timeout or connection reset is not counted as an error. However
2467 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002468 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002469
Willy Tarreau655dce92009-11-08 13:10:58 +01002470 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002471 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002472 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002473 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002474 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002475 session_inc_http_req_ctr(s);
2476 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002477 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002478 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002479 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002480
Willy Tarreau59234e92008-11-30 23:51:27 +01002481 /* 1: Since we are in header mode, if there's no space
2482 * left for headers, we won't be able to free more
2483 * later, so the session will never terminate. We
2484 * must terminate it now.
2485 */
2486 if (unlikely(req->flags & BF_FULL)) {
2487 /* FIXME: check if URI is set and return Status
2488 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002489 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002490 session_inc_http_req_ctr(s);
2491 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002492 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002493 if (msg->err_pos < 0)
2494 msg->err_pos = req->l;
Willy Tarreau59234e92008-11-30 23:51:27 +01002495 goto return_bad_req;
2496 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002497
Willy Tarreau59234e92008-11-30 23:51:27 +01002498 /* 2: have we encountered a read error ? */
2499 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002500 if (!(s->flags & SN_ERR_MASK))
2501 s->flags |= SN_ERR_CLICL;
2502
Willy Tarreaufcffa692010-01-10 14:21:19 +01002503 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002504 goto failed_keep_alive;
2505
Willy Tarreau59234e92008-11-30 23:51:27 +01002506 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002507 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002508 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002509 session_inc_http_err_ctr(s);
2510 }
2511
Willy Tarreau59234e92008-11-30 23:51:27 +01002512 msg->msg_state = HTTP_MSG_ERROR;
2513 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002514
Willy Tarreauda7ff642010-06-23 11:44:09 +02002515 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002516 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002517 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002518 if (s->listener->counters)
2519 s->listener->counters->failed_req++;
2520
Willy Tarreau59234e92008-11-30 23:51:27 +01002521 if (!(s->flags & SN_FINST_MASK))
2522 s->flags |= SN_FINST_R;
2523 return 0;
2524 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002525
Willy Tarreau59234e92008-11-30 23:51:27 +01002526 /* 3: has the read timeout expired ? */
2527 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002528 if (!(s->flags & SN_ERR_MASK))
2529 s->flags |= SN_ERR_CLITO;
2530
Willy Tarreaufcffa692010-01-10 14:21:19 +01002531 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002532 goto failed_keep_alive;
2533
Willy Tarreau59234e92008-11-30 23:51:27 +01002534 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002535 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002536 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002537 session_inc_http_err_ctr(s);
2538 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002539 txn->status = 408;
2540 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2541 msg->msg_state = HTTP_MSG_ERROR;
2542 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002543
Willy Tarreauda7ff642010-06-23 11:44:09 +02002544 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002545 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002546 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002547 if (s->listener->counters)
2548 s->listener->counters->failed_req++;
2549
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 if (!(s->flags & SN_FINST_MASK))
2551 s->flags |= SN_FINST_R;
2552 return 0;
2553 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002554
Willy Tarreau59234e92008-11-30 23:51:27 +01002555 /* 4: have we encountered a close ? */
2556 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002557 if (!(s->flags & SN_ERR_MASK))
2558 s->flags |= SN_ERR_CLICL;
2559
Willy Tarreaufcffa692010-01-10 14:21:19 +01002560 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002561 goto failed_keep_alive;
2562
Willy Tarreau4076a152009-04-02 15:18:36 +02002563 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002564 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002565 txn->status = 400;
2566 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2567 msg->msg_state = HTTP_MSG_ERROR;
2568 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002569
Willy Tarreauda7ff642010-06-23 11:44:09 +02002570 session_inc_http_err_ctr(s);
2571 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002572 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002573 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002574 if (s->listener->counters)
2575 s->listener->counters->failed_req++;
2576
Willy Tarreau59234e92008-11-30 23:51:27 +01002577 if (!(s->flags & SN_FINST_MASK))
2578 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002579 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002580 }
2581
Willy Tarreau520d95e2009-09-19 21:04:57 +02002582 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002583 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002584 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002585
Willy Tarreaufcffa692010-01-10 14:21:19 +01002586 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2587 /* If the client starts to talk, let's fall back to
2588 * request timeout processing.
2589 */
2590 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002591 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002592 }
2593
Willy Tarreau59234e92008-11-30 23:51:27 +01002594 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002595 if (!tick_isset(req->analyse_exp)) {
2596 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2597 (txn->flags & TX_WAIT_NEXT_RQ) &&
2598 tick_isset(s->be->timeout.httpka))
2599 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2600 else
2601 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2602 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002603
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 /* we're not ready yet */
2605 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002606
2607 failed_keep_alive:
2608 /* Here we process low-level errors for keep-alive requests. In
2609 * short, if the request is not the first one and it experiences
2610 * a timeout, read error or shutdown, we just silently close so
2611 * that the client can try again.
2612 */
2613 txn->status = 0;
2614 msg->msg_state = HTTP_MSG_RQBEFORE;
2615 req->analysers = 0;
2616 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002617 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002618 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002619 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002620 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002621
Willy Tarreaud787e662009-07-07 10:14:51 +02002622 /* OK now we have a complete HTTP request with indexed headers. Let's
2623 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002624 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2625 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2626 * points to the CRLF of the request line. req->lr points to the first
2627 * byte after the last LF. msg->col and msg->sov point to the first
2628 * byte of data. msg->eol cannot be trusted because it may have been
2629 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002630 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002631
Willy Tarreauda7ff642010-06-23 11:44:09 +02002632 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002633 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2634
Willy Tarreaub16a5742010-01-10 14:46:16 +01002635 if (txn->flags & TX_WAIT_NEXT_RQ) {
2636 /* kill the pending keep-alive timeout */
2637 txn->flags &= ~TX_WAIT_NEXT_RQ;
2638 req->analyse_exp = TICK_ETERNITY;
2639 }
2640
2641
Willy Tarreaud787e662009-07-07 10:14:51 +02002642 /* Maybe we found in invalid header name while we were configured not
2643 * to block on that, so we have to capture it now.
2644 */
2645 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002646 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002647
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 /*
2649 * 1: identify the method
2650 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002651 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002652
2653 /* we can make use of server redirect on GET and HEAD */
2654 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2655 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002656
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 /*
2658 * 2: check if the URI matches the monitor_uri.
2659 * We have to do this for every request which gets in, because
2660 * the monitor-uri is defined by the frontend.
2661 */
2662 if (unlikely((s->fe->monitor_uri_len != 0) &&
2663 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002664 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002665 s->fe->monitor_uri,
2666 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002667 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002669 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002670 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002671
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002673
Willy Tarreau59234e92008-11-30 23:51:27 +01002674 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002675 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2676 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002677
Willy Tarreau59234e92008-11-30 23:51:27 +01002678 ret = acl_pass(ret);
2679 if (cond->pol == ACL_COND_UNLESS)
2680 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002681
Willy Tarreau59234e92008-11-30 23:51:27 +01002682 if (ret) {
2683 /* we fail this request, let's return 503 service unavail */
2684 txn->status = 503;
2685 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2686 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002687 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002688 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002689
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 /* nothing to fail, let's reply normaly */
2691 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002692 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 goto return_prx_cond;
2694 }
2695
2696 /*
2697 * 3: Maybe we have to copy the original REQURI for the logs ?
2698 * Note: we cannot log anymore if the request has been
2699 * classified as invalid.
2700 */
2701 if (unlikely(s->logs.logwait & LW_REQ)) {
2702 /* we have a complete HTTP request that we must log */
2703 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2704 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002705
Willy Tarreau59234e92008-11-30 23:51:27 +01002706 if (urilen >= REQURI_LEN)
2707 urilen = REQURI_LEN - 1;
2708 memcpy(txn->uri, &req->data[msg->som], urilen);
2709 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002710
Willy Tarreau59234e92008-11-30 23:51:27 +01002711 if (!(s->logs.logwait &= ~LW_REQ))
2712 s->do_log(s);
2713 } else {
2714 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002715 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002716 }
Willy Tarreau06619262006-12-17 08:37:22 +01002717
Willy Tarreau59234e92008-11-30 23:51:27 +01002718 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002719 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2720 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002721
Willy Tarreau5b154472009-12-21 20:11:07 +01002722 /* ... and check if the request is HTTP/1.1 or above */
2723 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002724 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2725 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2726 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002727 txn->flags |= TX_REQ_VER_11;
2728
2729 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002730 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002731
Willy Tarreau88d349d2010-01-25 12:15:43 +01002732 /* if the frontend has "option http-use-proxy-header", we'll check if
2733 * we have what looks like a proxied connection instead of a connection,
2734 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2735 * Note that this is *not* RFC-compliant, however browsers and proxies
2736 * happen to do that despite being non-standard :-(
2737 * We consider that a request not beginning with either '/' or '*' is
2738 * a proxied connection, which covers both "scheme://location" and
2739 * CONNECT ip:port.
2740 */
2741 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2742 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2743 txn->flags |= TX_USE_PX_CONN;
2744
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002745 /* transfer length unknown*/
2746 txn->flags &= ~TX_REQ_XFER_LEN;
2747
Willy Tarreau59234e92008-11-30 23:51:27 +01002748 /* 5: we may need to capture headers */
2749 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002750 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002751 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002752
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002753 /* 6: determine the transfer-length.
2754 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2755 * the presence of a message-body in a REQUEST and its transfer length
2756 * must be determined that way (in order of precedence) :
2757 * 1. The presence of a message-body in a request is signaled by the
2758 * inclusion of a Content-Length or Transfer-Encoding header field
2759 * in the request's header fields. When a request message contains
2760 * both a message-body of non-zero length and a method that does
2761 * not define any semantics for that request message-body, then an
2762 * origin server SHOULD either ignore the message-body or respond
2763 * with an appropriate error message (e.g., 413). A proxy or
2764 * gateway, when presented the same request, SHOULD either forward
2765 * the request inbound with the message- body or ignore the
2766 * message-body when determining a response.
2767 *
2768 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2769 * and the "chunked" transfer-coding (Section 6.2) is used, the
2770 * transfer-length is defined by the use of this transfer-coding.
2771 * If a Transfer-Encoding header field is present and the "chunked"
2772 * transfer-coding is not present, the transfer-length is defined
2773 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002774 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002775 * 3. If a Content-Length header field is present, its decimal value in
2776 * OCTETs represents both the entity-length and the transfer-length.
2777 * If a message is received with both a Transfer-Encoding header
2778 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002779 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002780 * 4. By the server closing the connection. (Closing the connection
2781 * cannot be used to indicate the end of a request body, since that
2782 * would leave no possibility for the server to send back a response.)
2783 *
2784 * Whenever a transfer-coding is applied to a message-body, the set of
2785 * transfer-codings MUST include "chunked", unless the message indicates
2786 * it is terminated by closing the connection. When the "chunked"
2787 * transfer-coding is used, it MUST be the last transfer-coding applied
2788 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002789 */
2790
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002791 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002792 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002793 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002794 while ((txn->flags & TX_REQ_VER_11) &&
2795 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002796 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2797 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2798 else if (txn->flags & TX_REQ_TE_CHNK) {
2799 /* bad transfer-encoding (chunked followed by something else) */
2800 use_close_only = 1;
2801 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2802 break;
2803 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002804 }
2805
Willy Tarreau32b47f42009-10-18 20:55:02 +02002806 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002807 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002808 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2809 signed long long cl;
2810
2811 if (!ctx.vlen)
2812 goto return_bad_req;
2813
2814 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2815 goto return_bad_req; /* parse failure */
2816
2817 if (cl < 0)
2818 goto return_bad_req;
2819
Willy Tarreau124d9912011-03-01 20:30:48 +01002820 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl))
Willy Tarreau32b47f42009-10-18 20:55:02 +02002821 goto return_bad_req; /* already specified, was different */
2822
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002823 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002824 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002825 }
2826
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002827 /* bodyless requests have a known length */
2828 if (!use_close_only)
2829 txn->flags |= TX_REQ_XFER_LEN;
2830
Willy Tarreaud787e662009-07-07 10:14:51 +02002831 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002832 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002833 req->analyse_exp = TICK_ETERNITY;
2834 return 1;
2835
2836 return_bad_req:
2837 /* We centralize bad requests processing here */
2838 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2839 /* we detected a parsing error. We want to archive this request
2840 * in the dedicated proxy area for later troubleshooting.
2841 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002842 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002843 }
2844
2845 txn->req.msg_state = HTTP_MSG_ERROR;
2846 txn->status = 400;
2847 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002848
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002849 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002850 if (s->listener->counters)
2851 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002852
2853 return_prx_cond:
2854 if (!(s->flags & SN_ERR_MASK))
2855 s->flags |= SN_ERR_PRXCOND;
2856 if (!(s->flags & SN_FINST_MASK))
2857 s->flags |= SN_FINST_R;
2858
2859 req->analysers = 0;
2860 req->analyse_exp = TICK_ETERNITY;
2861 return 0;
2862}
2863
Cyril Bonté70be45d2010-10-12 00:14:35 +02002864/* We reached the stats page through a POST request.
2865 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002866 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002867 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002868int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002869{
Cyril Bonté70be45d2010-10-12 00:14:35 +02002870 struct proxy *px;
2871 struct server *sv;
2872
2873 char *backend = NULL;
2874 int action = 0;
2875
2876 char *first_param, *cur_param, *next_param, *end_params;
2877
2878 first_param = req->data + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002879 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002880
2881 cur_param = next_param = end_params;
2882
Cyril Bonté23b39d92011-02-10 22:54:44 +01002883 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002884 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002885 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002886 return 1;
2887 }
2888 else if (end_params > req->data + req->l) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002889 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002890 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002891 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002892 }
2893
2894 *end_params = '\0';
2895
Willy Tarreau295a8372011-03-10 11:25:07 +01002896 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002897
2898 /*
2899 * Parse the parameters in reverse order to only store the last value.
2900 * From the html form, the backend and the action are at the end.
2901 */
2902 while (cur_param > first_param) {
2903 char *key, *value;
2904
2905 cur_param--;
2906 if ((*cur_param == '&') || (cur_param == first_param)) {
2907 /* Parse the key */
2908 key = cur_param;
2909 if (cur_param != first_param) {
2910 /* delimit the string for the next loop */
2911 *key++ = '\0';
2912 }
2913
2914 /* Parse the value */
2915 value = key;
2916 while (*value != '\0' && *value != '=') {
2917 value++;
2918 }
2919 if (*value == '=') {
2920 /* Ok, a value is found, we can mark the end of the key */
2921 *value++ = '\0';
2922 }
2923
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002924 if (!url_decode(key) || !url_decode(value))
2925 break;
2926
Cyril Bonté70be45d2010-10-12 00:14:35 +02002927 /* Now we can check the key to see what to do */
2928 if (!backend && strcmp(key, "b") == 0) {
2929 backend = value;
2930 }
2931 else if (!action && strcmp(key, "action") == 0) {
2932 if (strcmp(value, "disable") == 0) {
2933 action = 1;
2934 }
2935 else if (strcmp(value, "enable") == 0) {
2936 action = 2;
2937 } else {
2938 /* unknown action, no need to continue */
2939 break;
2940 }
2941 }
2942 else if (strcmp(key, "s") == 0) {
2943 if (backend && action && get_backend_server(backend, value, &px, &sv)) {
2944 switch (action) {
2945 case 1:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002946 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002947 /* Not already in maintenance, we can change the server state */
2948 sv->state |= SRV_MAINTAIN;
2949 set_server_down(sv);
Willy Tarreau295a8372011-03-10 11:25:07 +01002950 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002951 }
2952 break;
2953 case 2:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002954 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002955 /* Already in maintenance, we can change the server state */
2956 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002957 sv->health = sv->rise; /* up, but will fall down at first failure */
Willy Tarreau295a8372011-03-10 11:25:07 +01002958 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002959 }
2960 break;
2961 }
2962 }
2963 }
2964 next_param = cur_param;
2965 }
2966 }
Cyril Bonté23b39d92011-02-10 22:54:44 +01002967 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002968}
2969
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002970/* returns a pointer to the first rule which forbids access (deny or http_auth),
2971 * or NULL if everything's OK.
2972 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002973static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002974http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2975{
Willy Tarreauff011f22011-01-06 17:51:27 +01002976 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002977
Willy Tarreauff011f22011-01-06 17:51:27 +01002978 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002979 int ret = 1;
2980
Willy Tarreauff011f22011-01-06 17:51:27 +01002981 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002982 continue;
2983
2984 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002985 if (rule->cond) {
2986 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002987 ret = acl_pass(ret);
2988
Willy Tarreauff011f22011-01-06 17:51:27 +01002989 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002990 ret = !ret;
2991 }
2992
2993 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002994 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002995 return NULL; /* no problem */
2996 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002997 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002998 }
2999 }
3000 return NULL;
3001}
3002
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003003/* This stream analyser runs all HTTP request processing which is common to
3004 * frontends and backends, which means blocking ACLs, filters, connection-close,
3005 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003006 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003007 * either needs more data or wants to immediately abort the request (eg: deny,
3008 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003009 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003010int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003011{
Willy Tarreaud787e662009-07-07 10:14:51 +02003012 struct http_txn *txn = &s->txn;
3013 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003014 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003015 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003016 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003017 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003018 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003019
Willy Tarreau655dce92009-11-08 13:10:58 +01003020 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003021 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003022 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003023 return 0;
3024 }
3025
Willy Tarreau3a816292009-07-07 10:55:49 +02003026 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003027 req->analyse_exp = TICK_ETERNITY;
3028
3029 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3030 now_ms, __FUNCTION__,
3031 s,
3032 req,
3033 req->rex, req->wex,
3034 req->flags,
3035 req->l,
3036 req->analysers);
3037
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003038 /* first check whether we have some ACLs set to block this request */
3039 list_for_each_entry(cond, &px->block_cond, list) {
3040 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003041
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003042 ret = acl_pass(ret);
3043 if (cond->pol == ACL_COND_UNLESS)
3044 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003045
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003046 if (ret) {
3047 txn->status = 403;
3048 /* let's log the request time */
3049 s->logs.tv_request = now;
3050 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003051 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003052 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003053 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003054 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003055
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003056 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01003057 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003058
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003059 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003060 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003061 do_stats = stats_check_uri(s->rep->prod, txn, px);
3062 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01003063 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 +01003064 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003065 else
3066 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003067
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003068 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01003069 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003070 txn->status = 403;
3071 s->logs.tv_request = now;
3072 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003073 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003074 s->fe->fe_counters.denied_req++;
3075 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3076 s->be->be_counters.denied_req++;
3077 if (s->listener->counters)
3078 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003079 goto return_prx_cond;
3080 }
3081
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003082 /* try headers filters */
3083 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003084 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003085 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003086
Willy Tarreau59234e92008-11-30 23:51:27 +01003087 /* has the request been denied ? */
3088 if (txn->flags & TX_CLDENY) {
3089 /* no need to go further */
3090 txn->status = 403;
3091 /* let's log the request time */
3092 s->logs.tv_request = now;
3093 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003094 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003095 goto return_prx_cond;
3096 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003097
3098 /* When a connection is tarpitted, we use the tarpit timeout,
3099 * which may be the same as the connect timeout if unspecified.
3100 * If unset, then set it to zero because we really want it to
3101 * eventually expire. We build the tarpit as an analyser.
3102 */
3103 if (txn->flags & TX_CLTARPIT) {
3104 buffer_erase(s->req);
3105 /* wipe the request out so that we can drop the connection early
3106 * if the client closes first.
3107 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003108 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003109 req->analysers = 0; /* remove switching rules etc... */
3110 req->analysers |= AN_REQ_HTTP_TARPIT;
3111 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3112 if (!req->analyse_exp)
3113 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003114 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003115 return 1;
3116 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003117 }
Willy Tarreau06619262006-12-17 08:37:22 +01003118
Willy Tarreau5b154472009-12-21 20:11:07 +01003119 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3120 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003121 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3122 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003123 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3124 * avoid to redo the same work if FE and BE have the same settings (common).
3125 * The method consists in checking if options changed between the two calls
3126 * (implying that either one is non-null, or one of them is non-null and we
3127 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003128 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003129
Willy Tarreaudc008c52010-02-01 16:20:08 +01003130 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3131 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3132 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3133 (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 +01003134 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003135
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003136 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3137 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003138 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003139 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3140 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003141 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003142 tmp = TX_CON_WANT_CLO;
3143
Willy Tarreau5b154472009-12-21 20:11:07 +01003144 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3145 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003146
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003147 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3148 /* parse the Connection header and possibly clean it */
3149 int to_del = 0;
3150 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003151 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3152 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003153 to_del |= 2; /* remove "keep-alive" */
3154 if (!(txn->flags & TX_REQ_VER_11))
3155 to_del |= 1; /* remove "close" */
3156 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003157 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003158
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003159 /* check if client or config asks for explicit close in KAL/SCL */
3160 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3161 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3162 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
3163 (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 +01003164 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003165 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
3166 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003167 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3168 }
Willy Tarreau78599912009-10-17 20:12:21 +02003169
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003170 /* we can be blocked here because the request needs to be authenticated,
3171 * either to pass or to access stats.
3172 */
Willy Tarreauff011f22011-01-06 17:51:27 +01003173 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003174 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01003175 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003176
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003177 if (!realm)
3178 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3179
Willy Tarreau844a7e72010-01-31 21:46:18 +01003180 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003181 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3182 txn->status = 401;
3183 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003184 /* on 401 we still count one error, because normal browsing
3185 * won't significantly increase the counter but brute force
3186 * attempts will.
3187 */
3188 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003189 goto return_prx_cond;
3190 }
3191
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003192 /* add request headers from the rule sets in the same order */
3193 list_for_each_entry(wl, &px->req_add, list) {
3194 if (wl->cond) {
3195 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
3196 ret = acl_pass(ret);
3197 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3198 ret = !ret;
3199 if (!ret)
3200 continue;
3201 }
3202
3203 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
3204 goto return_bad_req;
3205 }
3206
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003207 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003208 struct stats_admin_rule *stats_admin_rule;
3209
3210 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003211 * FIXME!!! that one is rather dangerous, we want to
3212 * make it follow standard rules (eg: clear req->analysers).
3213 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003214
Cyril Bonté474be412010-10-12 00:14:36 +02003215 /* now check whether we have some admin rules for this request */
3216 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3217 int ret = 1;
3218
3219 if (stats_admin_rule->cond) {
3220 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
3221 ret = acl_pass(ret);
3222 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3223 ret = !ret;
3224 }
3225
3226 if (ret) {
3227 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01003228 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02003229 break;
3230 }
3231 }
3232
Cyril Bonté70be45d2010-10-12 00:14:35 +02003233 /* Was the status page requested with a POST ? */
3234 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003235 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003236 if (msg->msg_state < HTTP_MSG_100_SENT) {
3237 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3238 * send an HTTP/1.1 100 Continue intermediate response.
3239 */
3240 if (txn->flags & TX_REQ_VER_11) {
3241 struct hdr_ctx ctx;
3242 ctx.idx = 0;
3243 /* Expect is allowed in 1.1, look for it */
3244 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3245 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3246 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3247 }
3248 }
3249 msg->msg_state = HTTP_MSG_100_SENT;
3250 s->logs.tv_request = now; /* update the request timer to reflect full request */
3251 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003252 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003253 /* we need more data */
3254 req->analysers |= an_bit;
3255 buffer_dont_connect(req);
3256 return 0;
3257 }
Cyril Bonté474be412010-10-12 00:14:36 +02003258 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003259 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003260 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003261 }
3262
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003263 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003264 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003265 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003266 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003267 s->rep->prod->applet.private = s;
3268 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003269 req->analysers = 0;
3270
3271 return 0;
3272
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003273 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003274
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003275 /* check whether we have some ACLs set to redirect this request */
3276 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003277 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003278
Willy Tarreauf285f542010-01-03 20:03:03 +01003279 if (rule->cond) {
3280 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3281 ret = acl_pass(ret);
3282 if (rule->cond->pol == ACL_COND_UNLESS)
3283 ret = !ret;
3284 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003285
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003286 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003287 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003288 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003289
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003290 /* build redirect message */
3291 switch(rule->code) {
3292 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003293 msg_fmt = HTTP_303;
3294 break;
3295 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003296 msg_fmt = HTTP_301;
3297 break;
3298 case 302:
3299 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003300 msg_fmt = HTTP_302;
3301 break;
3302 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003303
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003304 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003305 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003306
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003307 switch(rule->type) {
3308 case REDIRECT_TYPE_PREFIX: {
3309 const char *path;
3310 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003311
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003312 path = http_get_path(txn);
3313 /* build message using path */
3314 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003315 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003316 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3317 int qs = 0;
3318 while (qs < pathlen) {
3319 if (path[qs] == '?') {
3320 pathlen = qs;
3321 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003322 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003323 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003324 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003325 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003326 } else {
3327 path = "/";
3328 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003329 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003330
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003331 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003332 goto return_bad_req;
3333
3334 /* add prefix. Note that if prefix == "/", we don't want to
3335 * add anything, otherwise it makes it hard for the user to
3336 * configure a self-redirection.
3337 */
3338 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003339 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3340 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003341 }
3342
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003343 /* add path */
3344 memcpy(rdr.str + rdr.len, path, pathlen);
3345 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003346
3347 /* append a slash at the end of the location is needed and missing */
3348 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3349 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3350 if (rdr.len > rdr.size - 5)
3351 goto return_bad_req;
3352 rdr.str[rdr.len] = '/';
3353 rdr.len++;
3354 }
3355
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003356 break;
3357 }
3358 case REDIRECT_TYPE_LOCATION:
3359 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003360 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003361 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003362
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003363 /* add location */
3364 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3365 rdr.len += rule->rdr_len;
3366 break;
3367 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003368
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003369 if (rule->cookie_len) {
3370 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3371 rdr.len += 14;
3372 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3373 rdr.len += rule->cookie_len;
3374 memcpy(rdr.str + rdr.len, "\r\n", 2);
3375 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003376 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003377
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003378 /* add end of headers and the keep-alive/close status.
3379 * We may choose to set keep-alive if the Location begins
3380 * with a slash, because the client will come back to the
3381 * same server.
3382 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003383 txn->status = rule->code;
3384 /* let's log the request time */
3385 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003386
3387 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3388 (txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau124d9912011-03-01 20:30:48 +01003389 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003390 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3391 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3392 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003393 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003394 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3395 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3396 rdr.len += 30;
3397 } else {
3398 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3399 rdr.len += 24;
3400 }
Willy Tarreau75661452010-01-10 10:35:01 +01003401 }
3402 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3403 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003404 buffer_write(req->prod->ob, rdr.str, rdr.len);
3405 /* "eat" the request */
3406 buffer_ignore(req, msg->sov - msg->som);
3407 msg->som = msg->sov;
3408 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003409 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3410 txn->req.msg_state = HTTP_MSG_CLOSED;
3411 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003412 break;
3413 } else {
3414 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003415 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3416 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3417 rdr.len += 29;
3418 } else {
3419 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3420 rdr.len += 23;
3421 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003422 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003423 goto return_prx_cond;
3424 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003425 }
3426 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003427
Willy Tarreau2be39392010-01-03 17:24:51 +01003428 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3429 * If this happens, then the data will not come immediately, so we must
3430 * send all what we have without waiting. Note that due to the small gain
3431 * in waiting for the body of the request, it's easier to simply put the
3432 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3433 * itself once used.
3434 */
3435 req->flags |= BF_SEND_DONTWAIT;
3436
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003437 /* that's OK for us now, let's move on to next analysers */
3438 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003439
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003440 return_bad_req:
3441 /* We centralize bad requests processing here */
3442 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3443 /* we detected a parsing error. We want to archive this request
3444 * in the dedicated proxy area for later troubleshooting.
3445 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003446 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003447 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003448
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003449 txn->req.msg_state = HTTP_MSG_ERROR;
3450 txn->status = 400;
3451 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003452
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003453 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003454 if (s->listener->counters)
3455 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003456
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003457 return_prx_cond:
3458 if (!(s->flags & SN_ERR_MASK))
3459 s->flags |= SN_ERR_PRXCOND;
3460 if (!(s->flags & SN_FINST_MASK))
3461 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003462
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003463 req->analysers = 0;
3464 req->analyse_exp = TICK_ETERNITY;
3465 return 0;
3466}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003467
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003468/* This function performs all the processing enabled for the current request.
3469 * It returns 1 if the processing can continue on next analysers, or zero if it
3470 * needs more data, encounters an error, or wants to immediately abort the
3471 * request. It relies on buffers flags, and updates s->req->analysers.
3472 */
3473int http_process_request(struct session *s, struct buffer *req, int an_bit)
3474{
3475 struct http_txn *txn = &s->txn;
3476 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003477
Willy Tarreau655dce92009-11-08 13:10:58 +01003478 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003479 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003480 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003481 return 0;
3482 }
3483
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003484 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3485 now_ms, __FUNCTION__,
3486 s,
3487 req,
3488 req->rex, req->wex,
3489 req->flags,
3490 req->l,
3491 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003492
Willy Tarreau59234e92008-11-30 23:51:27 +01003493 /*
3494 * Right now, we know that we have processed the entire headers
3495 * and that unwanted requests have been filtered out. We can do
3496 * whatever we want with the remaining request. Also, now we
3497 * may have separate values for ->fe, ->be.
3498 */
Willy Tarreau06619262006-12-17 08:37:22 +01003499
Willy Tarreau59234e92008-11-30 23:51:27 +01003500 /*
3501 * If HTTP PROXY is set we simply get remote server address
3502 * parsing incoming request.
3503 */
3504 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau957c0a52011-03-03 17:42:23 +01003505 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 +01003506 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003507
Willy Tarreau59234e92008-11-30 23:51:27 +01003508 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003509 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003510 * Note that doing so might move headers in the request, but
3511 * the fields will stay coherent and the URI will not move.
3512 * This should only be performed in the backend.
3513 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003514 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003515 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3516 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003517
Willy Tarreau59234e92008-11-30 23:51:27 +01003518 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003519 * 8: the appsession cookie was looked up very early in 1.2,
3520 * so let's do the same now.
3521 */
3522
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003523 /* It needs to look into the URI unless persistence must be ignored */
3524 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003525 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003526 }
3527
3528 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003529 * 9: add X-Forwarded-For if either the frontend or the backend
3530 * asks for it.
3531 */
3532 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003533 struct hdr_ctx ctx = { .idx = 0 };
3534
3535 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3536 http_find_header2("X-Forwarded-For", 15, txn->req.sol, &txn->hdr_idx, &ctx)) {
3537 /* The header is set to be added only if none is present
3538 * and we found it, so don't do anything.
3539 */
3540 }
3541 else if (s->req->prod->addr.c.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003542 /* Add an X-Forwarded-For header unless the source IP is
3543 * in the 'except' network range.
3544 */
3545 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003546 (((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 +01003547 != s->fe->except_net.s_addr) &&
3548 (!s->be->except_mask.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003549 (((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 +01003550 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003551 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003552 unsigned char *pn;
Willy Tarreau957c0a52011-03-03 17:42:23 +01003553 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003554
3555 /* Note: we rely on the backend to get the header name to be used for
3556 * x-forwarded-for, because the header is really meant for the backends.
3557 * However, if the backend did not specify any option, we have to rely
3558 * on the frontend's header name.
3559 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003560 if (s->be->fwdfor_hdr_len) {
3561 len = s->be->fwdfor_hdr_len;
3562 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003563 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003564 len = s->fe->fwdfor_hdr_len;
3565 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003566 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003567 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003568
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003569 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003570 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003571 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003572 }
3573 }
Willy Tarreau957c0a52011-03-03 17:42:23 +01003574 else if (s->req->prod->addr.c.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003575 /* FIXME: for the sake of completeness, we should also support
3576 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003577 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003578 int len;
3579 char pn[INET6_ADDRSTRLEN];
3580 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003581 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003582 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003583
Willy Tarreau59234e92008-11-30 23:51:27 +01003584 /* Note: we rely on the backend to get the header name to be used for
3585 * x-forwarded-for, because the header is really meant for the backends.
3586 * However, if the backend did not specify any option, we have to rely
3587 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003588 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003589 if (s->be->fwdfor_hdr_len) {
3590 len = s->be->fwdfor_hdr_len;
3591 memcpy(trash, s->be->fwdfor_hdr_name, len);
3592 } else {
3593 len = s->fe->fwdfor_hdr_len;
3594 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003595 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003596 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003597
Willy Tarreau59234e92008-11-30 23:51:27 +01003598 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003599 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003600 goto return_bad_req;
3601 }
3602 }
3603
3604 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003605 * 10: add X-Original-To if either the frontend or the backend
3606 * asks for it.
3607 */
3608 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3609
3610 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau957c0a52011-03-03 17:42:23 +01003611 if (s->req->prod->addr.c.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003612 /* Add an X-Original-To header unless the destination IP is
3613 * in the 'except' network range.
3614 */
3615 if (!(s->flags & SN_FRT_ADDR_SET))
3616 get_frt_addr(s);
3617
Willy Tarreau957c0a52011-03-03 17:42:23 +01003618 if (s->req->prod->addr.c.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003619 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003620 (((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 +02003621 != s->fe->except_to.s_addr) &&
3622 (!s->be->except_mask_to.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003623 (((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 +02003624 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003625 int len;
3626 unsigned char *pn;
Willy Tarreau957c0a52011-03-03 17:42:23 +01003627 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003628
3629 /* Note: we rely on the backend to get the header name to be used for
3630 * x-original-to, because the header is really meant for the backends.
3631 * However, if the backend did not specify any option, we have to rely
3632 * on the frontend's header name.
3633 */
3634 if (s->be->orgto_hdr_len) {
3635 len = s->be->orgto_hdr_len;
3636 memcpy(trash, s->be->orgto_hdr_name, len);
3637 } else {
3638 len = s->fe->orgto_hdr_len;
3639 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003640 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003641 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3642
3643 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003644 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003645 goto return_bad_req;
3646 }
3647 }
3648 }
3649
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003650 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3651 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003652 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003653 unsigned int want_flags = 0;
3654
3655 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003656 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3657 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3658 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003659 want_flags |= TX_CON_CLO_SET;
3660 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003661 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
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_KAL_SET;
3665 }
3666
3667 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3668 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003669 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003670
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003671
Willy Tarreau522d6c02009-12-06 18:49:18 +01003672 /* If we have no server assigned yet and we're balancing on url_param
3673 * with a POST request, we may be interested in checking the body for
3674 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003675 */
3676 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3677 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003678 s->be->url_param_post_limit != 0 &&
Willy Tarreau61a21a32011-03-01 20:35:49 +01003679 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003680 buffer_dont_connect(req);
3681 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003682 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003683
Willy Tarreaud98cf932009-12-27 22:54:55 +01003684 if (txn->flags & TX_REQ_XFER_LEN)
3685 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003686
Willy Tarreau59234e92008-11-30 23:51:27 +01003687 /*************************************************************
3688 * OK, that's finished for the headers. We have done what we *
3689 * could. Let's switch to the DATA state. *
3690 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003691 req->analyse_exp = TICK_ETERNITY;
3692 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003693
Willy Tarreau59234e92008-11-30 23:51:27 +01003694 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003695 /* OK let's go on with the BODY now */
3696 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003697
Willy Tarreau59234e92008-11-30 23:51:27 +01003698 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003699 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003700 /* we detected a parsing error. We want to archive this request
3701 * in the dedicated proxy area for later troubleshooting.
3702 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003703 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003704 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003705
Willy Tarreau59234e92008-11-30 23:51:27 +01003706 txn->req.msg_state = HTTP_MSG_ERROR;
3707 txn->status = 400;
3708 req->analysers = 0;
3709 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003710
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003711 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003712 if (s->listener->counters)
3713 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003714
Willy Tarreau59234e92008-11-30 23:51:27 +01003715 if (!(s->flags & SN_ERR_MASK))
3716 s->flags |= SN_ERR_PRXCOND;
3717 if (!(s->flags & SN_FINST_MASK))
3718 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003719 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003720}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003721
Willy Tarreau60b85b02008-11-30 23:28:40 +01003722/* This function is an analyser which processes the HTTP tarpit. It always
3723 * returns zero, at the beginning because it prevents any other processing
3724 * from occurring, and at the end because it terminates the request.
3725 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003726int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003727{
3728 struct http_txn *txn = &s->txn;
3729
3730 /* This connection is being tarpitted. The CLIENT side has
3731 * already set the connect expiration date to the right
3732 * timeout. We just have to check that the client is still
3733 * there and that the timeout has not expired.
3734 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003735 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003736 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3737 !tick_is_expired(req->analyse_exp, now_ms))
3738 return 0;
3739
3740 /* We will set the queue timer to the time spent, just for
3741 * logging purposes. We fake a 500 server error, so that the
3742 * attacker will not suspect his connection has been tarpitted.
3743 * It will not cause trouble to the logs because we can exclude
3744 * the tarpitted connections by filtering on the 'PT' status flags.
3745 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003746 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3747
3748 txn->status = 500;
3749 if (req->flags != BF_READ_ERROR)
3750 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3751
3752 req->analysers = 0;
3753 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003754
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003755 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003756 if (s->listener->counters)
3757 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003758
Willy Tarreau60b85b02008-11-30 23:28:40 +01003759 if (!(s->flags & SN_ERR_MASK))
3760 s->flags |= SN_ERR_PRXCOND;
3761 if (!(s->flags & SN_FINST_MASK))
3762 s->flags |= SN_FINST_T;
3763 return 0;
3764}
3765
Willy Tarreaud34af782008-11-30 23:36:37 +01003766/* This function is an analyser which processes the HTTP request body. It looks
3767 * for parameters to be used for the load balancing algorithm (url_param). It
3768 * must only be called after the standard HTTP request processing has occurred,
3769 * because it expects the request to be parsed. It returns zero if it needs to
3770 * read more data, or 1 once it has completed its analysis.
3771 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003772int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003773{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003774 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003775 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003776 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003777
3778 /* We have to parse the HTTP request body to find any required data.
3779 * "balance url_param check_post" should have been the only way to get
3780 * into this. We were brought here after HTTP header analysis, so all
3781 * related structures are ready.
3782 */
3783
Willy Tarreau522d6c02009-12-06 18:49:18 +01003784 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3785 goto missing_data;
3786
3787 if (msg->msg_state < HTTP_MSG_100_SENT) {
3788 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3789 * send an HTTP/1.1 100 Continue intermediate response.
3790 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003791 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003792 struct hdr_ctx ctx;
3793 ctx.idx = 0;
3794 /* Expect is allowed in 1.1, look for it */
3795 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3796 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3797 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3798 }
3799 }
3800 msg->msg_state = HTTP_MSG_100_SENT;
3801 }
3802
3803 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003804 /* we have msg->col and msg->sov which both point to the first
3805 * byte of message body. msg->som still points to the beginning
3806 * of the message. We must save the body in req->lr because it
3807 * survives buffer re-alignments.
3808 */
3809 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003810 if (txn->flags & TX_REQ_TE_CHNK)
3811 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3812 else
3813 msg->msg_state = HTTP_MSG_DATA;
3814 }
3815
3816 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003817 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003818 * set ->sov and ->lr to point to the body and switch to DATA or
3819 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003820 */
3821 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003822
Willy Tarreau115acb92009-12-26 13:56:06 +01003823 if (!ret)
3824 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003825 else if (ret < 0) {
3826 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003827 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003828 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003829 }
3830
Willy Tarreaud98cf932009-12-27 22:54:55 +01003831 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003832 * We have the first non-header byte in msg->col, which is either the
3833 * beginning of the chunk size or of the data. The first data byte is in
3834 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3835 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003836 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003837
Willy Tarreau124d9912011-03-01 20:30:48 +01003838 if (msg->body_len < limit)
3839 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003840
Willy Tarreau7c96f672009-12-27 22:47:25 +01003841 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003842 goto http_end;
3843
3844 missing_data:
3845 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003846 if (req->flags & BF_FULL) {
3847 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003848 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003849 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003850
Willy Tarreau522d6c02009-12-06 18:49:18 +01003851 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3852 txn->status = 408;
3853 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003854
3855 if (!(s->flags & SN_ERR_MASK))
3856 s->flags |= SN_ERR_CLITO;
3857 if (!(s->flags & SN_FINST_MASK))
3858 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003859 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003860 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003861
3862 /* we get here if we need to wait for more data */
3863 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003864 /* Not enough data. We'll re-use the http-request
3865 * timeout here. Ideally, we should set the timeout
3866 * relative to the accept() date. We just set the
3867 * request timeout once at the beginning of the
3868 * request.
3869 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003870 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003871 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003872 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003873 return 0;
3874 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003875
3876 http_end:
3877 /* The situation will not evolve, so let's give up on the analysis. */
3878 s->logs.tv_request = now; /* update the request timer to reflect full request */
3879 req->analysers &= ~an_bit;
3880 req->analyse_exp = TICK_ETERNITY;
3881 return 1;
3882
3883 return_bad_req: /* let's centralize all bad requests */
3884 txn->req.msg_state = HTTP_MSG_ERROR;
3885 txn->status = 400;
3886 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3887
Willy Tarreau79ebac62010-06-07 13:47:49 +02003888 if (!(s->flags & SN_ERR_MASK))
3889 s->flags |= SN_ERR_PRXCOND;
3890 if (!(s->flags & SN_FINST_MASK))
3891 s->flags |= SN_FINST_R;
3892
Willy Tarreau522d6c02009-12-06 18:49:18 +01003893 return_err_msg:
3894 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003895 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003896 if (s->listener->counters)
3897 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003898 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003899}
3900
Willy Tarreau610ecce2010-01-04 21:15:02 +01003901/* Terminate current transaction and prepare a new one. This is very tricky
3902 * right now but it works.
3903 */
3904void http_end_txn_clean_session(struct session *s)
3905{
3906 /* FIXME: We need a more portable way of releasing a backend's and a
3907 * server's connections. We need a safer way to reinitialize buffer
3908 * flags. We also need a more accurate method for computing per-request
3909 * data.
3910 */
3911 http_silent_debug(__LINE__, s);
3912
3913 s->req->cons->flags |= SI_FL_NOLINGER;
3914 s->req->cons->shutr(s->req->cons);
3915 s->req->cons->shutw(s->req->cons);
3916
3917 http_silent_debug(__LINE__, s);
3918
3919 if (s->flags & SN_BE_ASSIGNED)
3920 s->be->beconn--;
3921
3922 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3923 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003924 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003925
3926 if (s->txn.status) {
3927 int n;
3928
3929 n = s->txn.status / 100;
3930 if (n < 1 || n > 5)
3931 n = 0;
3932
3933 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003934 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003935
Willy Tarreau24657792010-02-26 10:30:28 +01003936 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003937 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003938 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003939 }
3940
3941 /* don't count other requests' data */
3942 s->logs.bytes_in -= s->req->l - s->req->send_max;
3943 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3944
3945 /* let's do a final log if we need it */
3946 if (s->logs.logwait &&
3947 !(s->flags & SN_MONITOR) &&
3948 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3949 s->do_log(s);
3950 }
3951
3952 s->logs.accept_date = date; /* user-visible date for logging */
3953 s->logs.tv_accept = now; /* corrected date for internal use */
3954 tv_zero(&s->logs.tv_request);
3955 s->logs.t_queue = -1;
3956 s->logs.t_connect = -1;
3957 s->logs.t_data = -1;
3958 s->logs.t_close = 0;
3959 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3960 s->logs.srv_queue_size = 0; /* we will get this number soon */
3961
3962 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3963 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3964
3965 if (s->pend_pos)
3966 pendconn_free(s->pend_pos);
3967
Willy Tarreau827aee92011-03-10 16:55:02 +01003968 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003969 if (s->flags & SN_CURR_SESS) {
3970 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003971 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003972 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003973 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3974 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003975 }
3976
3977 if (unlikely(s->srv_conn))
3978 sess_change_server(s, NULL);
Willy Tarreau9e000c62011-03-10 14:03:36 +01003979 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003980
3981 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3982 s->req->cons->fd = -1; /* just to help with debugging */
3983 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003984 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003985 s->req->cons->err_loc = NULL;
3986 s->req->cons->exp = TICK_ETERNITY;
3987 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003988 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3989 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 +02003990 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 +01003991 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3992 s->txn.meth = 0;
3993 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003994 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003995 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003996 s->req->cons->flags |= SI_FL_INDEP_STR;
3997
Willy Tarreau96e31212011-05-30 18:10:30 +02003998 if (s->fe->options2 & PR_O2_NODELAY) {
3999 s->req->flags |= BF_NEVER_WAIT;
4000 s->rep->flags |= BF_NEVER_WAIT;
4001 }
4002
Willy Tarreau610ecce2010-01-04 21:15:02 +01004003 /* if the request buffer is not empty, it means we're
4004 * about to process another request, so send pending
4005 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004006 * Just don't do this if the buffer is close to be full,
4007 * because the request will wait for it to flush a little
4008 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 */
Willy Tarreau065e8332010-01-08 00:30:20 +01004010 if (s->req->l > s->req->send_max) {
4011 if (s->rep->send_max &&
4012 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01004013 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
4014 s->rep->flags |= BF_EXPECT_MORE;
4015 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004016
4017 /* we're removing the analysers, we MUST re-enable events detection */
4018 buffer_auto_read(s->req);
4019 buffer_auto_close(s->req);
4020 buffer_auto_read(s->rep);
4021 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022
4023 /* make ->lr point to the first non-forwarded byte */
4024 s->req->lr = s->req->w + s->req->send_max;
4025 if (s->req->lr >= s->req->data + s->req->size)
4026 s->req->lr -= s->req->size;
4027 s->rep->lr = s->rep->w + s->rep->send_max;
4028 if (s->rep->lr >= s->rep->data + s->rep->size)
4029 s->rep->lr -= s->req->size;
4030
Willy Tarreau342b11c2010-11-24 16:22:09 +01004031 s->req->analysers = s->listener->analysers;
4032 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033 s->rep->analysers = 0;
4034
4035 http_silent_debug(__LINE__, s);
4036}
4037
4038
4039/* This function updates the request state machine according to the response
4040 * state machine and buffer flags. It returns 1 if it changes anything (flag
4041 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4042 * it is only used to find when a request/response couple is complete. Both
4043 * this function and its equivalent should loop until both return zero. It
4044 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4045 */
4046int http_sync_req_state(struct session *s)
4047{
4048 struct buffer *buf = s->req;
4049 struct http_txn *txn = &s->txn;
4050 unsigned int old_flags = buf->flags;
4051 unsigned int old_state = txn->req.msg_state;
4052
4053 http_silent_debug(__LINE__, s);
4054 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4055 return 0;
4056
4057 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004058 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004059 * We can shut the read side unless we want to abort_on_close,
4060 * or we have a POST request. The issue with POST requests is
4061 * that some browsers still send a CRLF after the request, and
4062 * this CRLF must be read so that it does not remain in the kernel
4063 * buffers, otherwise a close could cause an RST on some systems
4064 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004065 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004066 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01004067 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004068
4069 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4070 goto wait_other_side;
4071
4072 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4073 /* The server has not finished to respond, so we
4074 * don't want to move in order not to upset it.
4075 */
4076 goto wait_other_side;
4077 }
4078
4079 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4080 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004081 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004082 txn->req.msg_state = HTTP_MSG_TUNNEL;
4083 goto wait_other_side;
4084 }
4085
4086 /* When we get here, it means that both the request and the
4087 * response have finished receiving. Depending on the connection
4088 * mode, we'll have to wait for the last bytes to leave in either
4089 * direction, and sometimes for a close to be effective.
4090 */
4091
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004092 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4093 /* Server-close mode : queue a connection close to the server */
4094 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004095 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004096 }
4097 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4098 /* Option forceclose is set, or either side wants to close,
4099 * let's enforce it now that we're not expecting any new
4100 * data to come. The caller knows the session is complete
4101 * once both states are CLOSED.
4102 */
4103 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004104 buffer_shutr_now(buf);
4105 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004106 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004107 }
4108 else {
4109 /* The last possible modes are keep-alive and tunnel. Since tunnel
4110 * mode does not set the body analyser, we can't reach this place
4111 * in tunnel mode, so we're left with keep-alive only.
4112 * This mode is currently not implemented, we switch to tunnel mode.
4113 */
4114 buffer_auto_read(buf);
4115 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004116 }
4117
4118 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4119 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004120 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4121
Willy Tarreau610ecce2010-01-04 21:15:02 +01004122 if (!(buf->flags & BF_OUT_EMPTY)) {
4123 txn->req.msg_state = HTTP_MSG_CLOSING;
4124 goto http_msg_closing;
4125 }
4126 else {
4127 txn->req.msg_state = HTTP_MSG_CLOSED;
4128 goto http_msg_closed;
4129 }
4130 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004131 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004132 }
4133
4134 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4135 http_msg_closing:
4136 /* nothing else to forward, just waiting for the output buffer
4137 * to be empty and for the shutw_now to take effect.
4138 */
4139 if (buf->flags & BF_OUT_EMPTY) {
4140 txn->req.msg_state = HTTP_MSG_CLOSED;
4141 goto http_msg_closed;
4142 }
4143 else if (buf->flags & BF_SHUTW) {
4144 txn->req.msg_state = HTTP_MSG_ERROR;
4145 goto wait_other_side;
4146 }
4147 }
4148
4149 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4150 http_msg_closed:
4151 goto wait_other_side;
4152 }
4153
4154 wait_other_side:
4155 http_silent_debug(__LINE__, s);
4156 return txn->req.msg_state != old_state || buf->flags != old_flags;
4157}
4158
4159
4160/* This function updates the response state machine according to the request
4161 * state machine and buffer flags. It returns 1 if it changes anything (flag
4162 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4163 * it is only used to find when a request/response couple is complete. Both
4164 * this function and its equivalent should loop until both return zero. It
4165 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4166 */
4167int http_sync_res_state(struct session *s)
4168{
4169 struct buffer *buf = s->rep;
4170 struct http_txn *txn = &s->txn;
4171 unsigned int old_flags = buf->flags;
4172 unsigned int old_state = txn->rsp.msg_state;
4173
4174 http_silent_debug(__LINE__, s);
4175 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4176 return 0;
4177
4178 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4179 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004180 * still monitor the server connection for a possible close
4181 * while the request is being uploaded, so we don't disable
4182 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004183 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004184 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004185
4186 if (txn->req.msg_state == HTTP_MSG_ERROR)
4187 goto wait_other_side;
4188
4189 if (txn->req.msg_state < HTTP_MSG_DONE) {
4190 /* The client seems to still be sending data, probably
4191 * because we got an error response during an upload.
4192 * We have the choice of either breaking the connection
4193 * or letting it pass through. Let's do the later.
4194 */
4195 goto wait_other_side;
4196 }
4197
4198 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4199 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004200 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004201 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4202 goto wait_other_side;
4203 }
4204
4205 /* When we get here, it means that both the request and the
4206 * response have finished receiving. Depending on the connection
4207 * mode, we'll have to wait for the last bytes to leave in either
4208 * direction, and sometimes for a close to be effective.
4209 */
4210
4211 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4212 /* Server-close mode : shut read and wait for the request
4213 * side to close its output buffer. The caller will detect
4214 * when we're in DONE and the other is in CLOSED and will
4215 * catch that for the final cleanup.
4216 */
4217 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4218 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004219 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004220 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4221 /* Option forceclose is set, or either side wants to close,
4222 * let's enforce it now that we're not expecting any new
4223 * data to come. The caller knows the session is complete
4224 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004225 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004226 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4227 buffer_shutr_now(buf);
4228 buffer_shutw_now(buf);
4229 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004230 }
4231 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004232 /* The last possible modes are keep-alive and tunnel. Since tunnel
4233 * mode does not set the body analyser, we can't reach this place
4234 * in tunnel mode, so we're left with keep-alive only.
4235 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004236 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004237 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004238 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004239 }
4240
4241 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4242 /* if we've just closed an output, let's switch */
4243 if (!(buf->flags & BF_OUT_EMPTY)) {
4244 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4245 goto http_msg_closing;
4246 }
4247 else {
4248 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4249 goto http_msg_closed;
4250 }
4251 }
4252 goto wait_other_side;
4253 }
4254
4255 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4256 http_msg_closing:
4257 /* nothing else to forward, just waiting for the output buffer
4258 * to be empty and for the shutw_now to take effect.
4259 */
4260 if (buf->flags & BF_OUT_EMPTY) {
4261 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4262 goto http_msg_closed;
4263 }
4264 else if (buf->flags & BF_SHUTW) {
4265 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004266 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004267 if (target_srv(&s->target))
4268 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004269 goto wait_other_side;
4270 }
4271 }
4272
4273 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4274 http_msg_closed:
4275 /* drop any pending data */
4276 buffer_ignore(buf, buf->l - buf->send_max);
4277 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004278 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004279 goto wait_other_side;
4280 }
4281
4282 wait_other_side:
4283 http_silent_debug(__LINE__, s);
4284 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4285}
4286
4287
4288/* Resync the request and response state machines. Return 1 if either state
4289 * changes.
4290 */
4291int http_resync_states(struct session *s)
4292{
4293 struct http_txn *txn = &s->txn;
4294 int old_req_state = txn->req.msg_state;
4295 int old_res_state = txn->rsp.msg_state;
4296
4297 http_silent_debug(__LINE__, s);
4298 http_sync_req_state(s);
4299 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004300 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004301 if (!http_sync_res_state(s))
4302 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004303 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004304 if (!http_sync_req_state(s))
4305 break;
4306 }
4307 http_silent_debug(__LINE__, s);
4308 /* OK, both state machines agree on a compatible state.
4309 * There are a few cases we're interested in :
4310 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4311 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4312 * directions, so let's simply disable both analysers.
4313 * - HTTP_MSG_CLOSED on the response only means we must abort the
4314 * request.
4315 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4316 * with server-close mode means we've completed one request and we
4317 * must re-initialize the server connection.
4318 */
4319
4320 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4321 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4322 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4323 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4324 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004325 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004326 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004327 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004328 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004329 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004330 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004331 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4332 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004333 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004334 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004335 s->rep->analysers = 0;
4336 buffer_auto_close(s->rep);
4337 buffer_auto_read(s->rep);
4338 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004339 buffer_abort(s->req);
4340 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004341 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004342 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004343 }
4344 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4345 txn->rsp.msg_state == HTTP_MSG_DONE &&
4346 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4347 /* server-close: terminate this server connection and
4348 * reinitialize a fresh-new transaction.
4349 */
4350 http_end_txn_clean_session(s);
4351 }
4352
4353 http_silent_debug(__LINE__, s);
4354 return txn->req.msg_state != old_req_state ||
4355 txn->rsp.msg_state != old_res_state;
4356}
4357
Willy Tarreaud98cf932009-12-27 22:54:55 +01004358/* This function is an analyser which forwards request body (including chunk
4359 * sizes if any). It is called as soon as we must forward, even if we forward
4360 * zero byte. The only situation where it must not be called is when we're in
4361 * tunnel mode and we want to forward till the close. It's used both to forward
4362 * remaining data and to resync after end of body. It expects the msg_state to
4363 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4364 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004365 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004366 * bytes of pending data + the headers if not already done (between som and sov).
4367 * It eventually adjusts som to match sov after the data in between have been sent.
4368 */
4369int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4370{
4371 struct http_txn *txn = &s->txn;
4372 struct http_msg *msg = &s->txn.req;
4373
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004374 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4375 return 0;
4376
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004377 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4378 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004379 /* Output closed while we were sending data. We must abort and
4380 * wake the other side up.
4381 */
4382 msg->msg_state = HTTP_MSG_ERROR;
4383 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004384 return 1;
4385 }
4386
Willy Tarreau4fe41902010-06-07 22:27:41 +02004387 /* in most states, we should abort in case of early close */
4388 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004389
4390 /* Note that we don't have to send 100-continue back because we don't
4391 * need the data to complete our job, and it's up to the server to
4392 * decide whether to return 100, 417 or anything else in return of
4393 * an "Expect: 100-continue" header.
4394 */
4395
4396 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4397 /* we have msg->col and msg->sov which both point to the first
4398 * byte of message body. msg->som still points to the beginning
4399 * of the message. We must save the body in req->lr because it
4400 * survives buffer re-alignments.
4401 */
4402 req->lr = req->data + msg->sov;
4403 if (txn->flags & TX_REQ_TE_CHNK)
4404 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4405 else {
4406 msg->msg_state = HTTP_MSG_DATA;
4407 }
4408 }
4409
Willy Tarreaud98cf932009-12-27 22:54:55 +01004410 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004411 int bytes;
4412
Willy Tarreau610ecce2010-01-04 21:15:02 +01004413 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004414 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004415 bytes = msg->sov - msg->som;
4416 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004417 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004418 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4419 bytes += req->size;
4420 msg->chunk_len += (unsigned int)bytes;
4421 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004422 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004423
Willy Tarreaucaabe412010-01-03 23:08:28 +01004424 if (msg->msg_state == HTTP_MSG_DATA) {
4425 /* must still forward */
4426 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004427 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004428
4429 /* nothing left to forward */
4430 if (txn->flags & TX_REQ_TE_CHNK)
4431 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004432 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004433 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004434 }
4435 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004436 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004437 * set ->sov and ->lr to point to the body and switch to DATA or
4438 * TRAILERS state.
4439 */
4440 int ret = http_parse_chunk_size(req, msg);
4441
4442 if (!ret)
4443 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004444 else if (ret < 0) {
4445 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004446 if (msg->err_pos >= 0)
4447 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004448 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004449 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004450 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004451 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004452 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4453 /* we want the CRLF after the data */
4454 int ret;
4455
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004456 req->lr = req->w + req->send_max;
4457 if (req->lr >= req->data + req->size)
4458 req->lr -= req->size;
4459
Willy Tarreaud98cf932009-12-27 22:54:55 +01004460 ret = http_skip_chunk_crlf(req, msg);
4461
4462 if (ret == 0)
4463 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004464 else if (ret < 0) {
4465 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004466 if (msg->err_pos >= 0)
4467 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004468 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004469 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004470 /* we're in MSG_CHUNK_SIZE now */
4471 }
4472 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4473 int ret = http_forward_trailers(req, msg);
4474
4475 if (ret == 0)
4476 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004477 else if (ret < 0) {
4478 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004479 if (msg->err_pos >= 0)
4480 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004481 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004482 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004483 /* we're in HTTP_MSG_DONE now */
4484 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004485 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004486 int old_state = msg->msg_state;
4487
Willy Tarreau610ecce2010-01-04 21:15:02 +01004488 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004489 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004490 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4491 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4492 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004493 if (http_resync_states(s)) {
4494 /* some state changes occurred, maybe the analyser
4495 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004496 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004497 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4498 if (req->flags & BF_SHUTW) {
4499 /* request errors are most likely due to
4500 * the server aborting the transfer.
4501 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004502 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004503 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004504 if (msg->err_pos >= 0)
4505 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004506 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004507 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004508 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004509 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004510
4511 /* If "option abortonclose" is set on the backend, we
4512 * want to monitor the client's connection and forward
4513 * any shutdown notification to the server, which will
4514 * decide whether to close or to go on processing the
4515 * request.
4516 */
4517 if (s->be->options & PR_O_ABRT_CLOSE) {
4518 buffer_auto_read(req);
4519 buffer_auto_close(req);
4520 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004521 else if (s->txn.meth == HTTP_METH_POST) {
4522 /* POST requests may require to read extra CRLF
4523 * sent by broken browsers and which could cause
4524 * an RST to be sent upon close on some systems
4525 * (eg: Linux).
4526 */
4527 buffer_auto_read(req);
4528 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004529
Willy Tarreau610ecce2010-01-04 21:15:02 +01004530 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004531 }
4532 }
4533
Willy Tarreaud98cf932009-12-27 22:54:55 +01004534 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004535 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004536 if (req->flags & BF_SHUTR) {
4537 if (!(s->flags & SN_ERR_MASK))
4538 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004539 if (!(s->flags & SN_FINST_MASK)) {
4540 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4541 s->flags |= SN_FINST_H;
4542 else
4543 s->flags |= SN_FINST_D;
4544 }
4545
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004546 s->fe->fe_counters.cli_aborts++;
4547 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004548 if (target_srv(&s->target))
4549 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004550
4551 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004552 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004553
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004554 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004555 if (req->flags & BF_SHUTW)
4556 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004557
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004558 /* When TE: chunked is used, we need to get there again to parse remaining
4559 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4560 */
4561 if (txn->flags & TX_REQ_TE_CHNK)
4562 buffer_dont_close(req);
4563
Willy Tarreau5c620922011-05-11 19:56:11 +02004564 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004565 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4566 * system knows it must not set a PUSH on this first part. Interactive
4567 * modes are already handled by the stream sock layer.
Willy Tarreau5c620922011-05-11 19:56:11 +02004568 */
Willy Tarreau07293032011-05-30 18:29:28 +02004569 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004570
Willy Tarreau610ecce2010-01-04 21:15:02 +01004571 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004572 return 0;
4573
Willy Tarreaud98cf932009-12-27 22:54:55 +01004574 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004575 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004576 if (s->listener->counters)
4577 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004578 return_bad_req_stats_ok:
4579 txn->req.msg_state = HTTP_MSG_ERROR;
4580 if (txn->status) {
4581 /* Note: we don't send any error if some data were already sent */
4582 stream_int_retnclose(req->prod, NULL);
4583 } else {
4584 txn->status = 400;
4585 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4586 }
4587 req->analysers = 0;
4588 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004589
4590 if (!(s->flags & SN_ERR_MASK))
4591 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004592 if (!(s->flags & SN_FINST_MASK)) {
4593 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4594 s->flags |= SN_FINST_H;
4595 else
4596 s->flags |= SN_FINST_D;
4597 }
4598 return 0;
4599
4600 aborted_xfer:
4601 txn->req.msg_state = HTTP_MSG_ERROR;
4602 if (txn->status) {
4603 /* Note: we don't send any error if some data were already sent */
4604 stream_int_retnclose(req->prod, NULL);
4605 } else {
4606 txn->status = 502;
4607 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4608 }
4609 req->analysers = 0;
4610 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4611
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004612 s->fe->fe_counters.srv_aborts++;
4613 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004614 if (target_srv(&s->target))
4615 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004616
4617 if (!(s->flags & SN_ERR_MASK))
4618 s->flags |= SN_ERR_SRVCL;
4619 if (!(s->flags & SN_FINST_MASK)) {
4620 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4621 s->flags |= SN_FINST_H;
4622 else
4623 s->flags |= SN_FINST_D;
4624 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004625 return 0;
4626}
4627
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004628/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4629 * processing can continue on next analysers, or zero if it either needs more
4630 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4631 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4632 * when it has nothing left to do, and may remove any analyser when it wants to
4633 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004634 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004635int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004636{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004637 struct http_txn *txn = &s->txn;
4638 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004639 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004640 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004641 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004642 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004643
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004644 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 +02004645 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004646 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004647 rep,
4648 rep->rex, rep->wex,
4649 rep->flags,
4650 rep->l,
4651 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004652
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004653 /*
4654 * Now parse the partial (or complete) lines.
4655 * We will check the response syntax, and also join multi-line
4656 * headers. An index of all the lines will be elaborated while
4657 * parsing.
4658 *
4659 * For the parsing, we use a 28 states FSM.
4660 *
4661 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004662 * rep->data + msg->som = beginning of response
4663 * rep->data + msg->eoh = end of processed headers / start of current one
4664 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004665 * rep->lr = first non-visited byte
4666 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004667 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004668 */
4669
Willy Tarreau83e3af02009-12-28 17:39:57 +01004670 /* There's a protected area at the end of the buffer for rewriting
4671 * purposes. We don't want to start to parse the request if the
4672 * protected area is affected, because we may have to move processed
4673 * data later, which is much more complicated.
4674 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004675 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4676 if (unlikely((rep->flags & BF_FULL) ||
4677 rep->r < rep->lr ||
4678 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4679 if (rep->send_max) {
4680 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004681 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4682 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004683 buffer_dont_close(rep);
4684 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4685 return 0;
4686 }
4687 if (rep->l <= rep->size - global.tune.maxrewrite)
4688 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004689 }
4690
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004691 if (likely(rep->lr < rep->r))
4692 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004693 }
4694
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004695 /* 1: we might have to print this header in debug mode */
4696 if (unlikely((global.mode & MODE_DEBUG) &&
4697 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004698 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004699 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004700 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004701
Willy Tarreau663308b2010-06-07 14:06:08 +02004702 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004703 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004704 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004705
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004706 sol += hdr_idx_first_pos(&txn->hdr_idx);
4707 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004708
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004709 while (cur_idx) {
4710 eol = sol + txn->hdr_idx.v[cur_idx].len;
4711 debug_hdr("srvhdr", s, sol, eol);
4712 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4713 cur_idx = txn->hdr_idx.v[cur_idx].next;
4714 }
4715 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004716
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004717 /*
4718 * Now we quickly check if we have found a full valid response.
4719 * If not so, we check the FD and buffer states before leaving.
4720 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004721 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004722 * responses are checked first.
4723 *
4724 * Depending on whether the client is still there or not, we
4725 * may send an error response back or not. Note that normally
4726 * we should only check for HTTP status there, and check I/O
4727 * errors somewhere else.
4728 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004729
Willy Tarreau655dce92009-11-08 13:10:58 +01004730 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004731 /* Invalid response */
4732 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4733 /* we detected a parsing error. We want to archive this response
4734 * in the dedicated proxy area for later troubleshooting.
4735 */
4736 hdr_response_bad:
4737 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004738 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004739
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004740 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004741 if (target_srv(&s->target)) {
4742 target_srv(&s->target)->counters.failed_resp++;
4743 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004744 }
Willy Tarreau64648412010-03-05 10:41:54 +01004745 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004746 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004747 rep->analysers = 0;
4748 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004749 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004750 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004751 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4752
4753 if (!(s->flags & SN_ERR_MASK))
4754 s->flags |= SN_ERR_PRXCOND;
4755 if (!(s->flags & SN_FINST_MASK))
4756 s->flags |= SN_FINST_H;
4757
4758 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004759 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004760
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004761 /* too large response does not fit in buffer. */
4762 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004763 if (msg->err_pos < 0)
4764 msg->err_pos = rep->l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004765 goto hdr_response_bad;
4766 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004767
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004768 /* read error */
4769 else if (rep->flags & BF_READ_ERROR) {
4770 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004771 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004772
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004773 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004774 if (target_srv(&s->target)) {
4775 target_srv(&s->target)->counters.failed_resp++;
4776 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004777 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004778
Willy Tarreau90deb182010-01-07 00:20:41 +01004779 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004780 rep->analysers = 0;
4781 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004782 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004783 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004784 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004785
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004786 if (!(s->flags & SN_ERR_MASK))
4787 s->flags |= SN_ERR_SRVCL;
4788 if (!(s->flags & SN_FINST_MASK))
4789 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004790 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004791 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004792
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004793 /* read timeout : return a 504 to the client. */
4794 else if (rep->flags & BF_READ_TIMEOUT) {
4795 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004796 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004797
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004798 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004799 if (target_srv(&s->target)) {
4800 target_srv(&s->target)->counters.failed_resp++;
4801 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004802 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004803
Willy Tarreau90deb182010-01-07 00:20:41 +01004804 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004805 rep->analysers = 0;
4806 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004807 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004808 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004809 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004810
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004811 if (!(s->flags & SN_ERR_MASK))
4812 s->flags |= SN_ERR_SRVTO;
4813 if (!(s->flags & SN_FINST_MASK))
4814 s->flags |= SN_FINST_H;
4815 return 0;
4816 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004817
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004818 /* close from server */
4819 else if (rep->flags & BF_SHUTR) {
4820 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004821 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004822
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004823 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004824 if (target_srv(&s->target)) {
4825 target_srv(&s->target)->counters.failed_resp++;
4826 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004827 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004828
Willy Tarreau90deb182010-01-07 00:20:41 +01004829 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004830 rep->analysers = 0;
4831 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004832 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004833 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004834 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004835
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004836 if (!(s->flags & SN_ERR_MASK))
4837 s->flags |= SN_ERR_SRVCL;
4838 if (!(s->flags & SN_FINST_MASK))
4839 s->flags |= SN_FINST_H;
4840 return 0;
4841 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004842
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004843 /* write error to client (we don't send any message then) */
4844 else if (rep->flags & BF_WRITE_ERROR) {
4845 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004846 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004847
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004848 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004849 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004850 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004851
4852 if (!(s->flags & SN_ERR_MASK))
4853 s->flags |= SN_ERR_CLICL;
4854 if (!(s->flags & SN_FINST_MASK))
4855 s->flags |= SN_FINST_H;
4856
4857 /* process_session() will take care of the error */
4858 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004859 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004860
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004861 buffer_dont_close(rep);
4862 return 0;
4863 }
4864
4865 /* More interesting part now : we know that we have a complete
4866 * response which at least looks like HTTP. We have an indicator
4867 * of each header's length, so we can parse them quickly.
4868 */
4869
4870 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004871 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004872
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004873 /*
4874 * 1: get the status code
4875 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004876 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004877 if (n < 1 || n > 5)
4878 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004879 /* when the client triggers a 4xx from the server, it's most often due
4880 * to a missing object or permission. These events should be tracked
4881 * because if they happen often, it may indicate a brute force or a
4882 * vulnerability scan.
4883 */
4884 if (n == 4)
4885 session_inc_http_err_ctr(s);
4886
Willy Tarreau827aee92011-03-10 16:55:02 +01004887 if (target_srv(&s->target))
4888 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004889
Willy Tarreau5b154472009-12-21 20:11:07 +01004890 /* check if the response is HTTP/1.1 or above */
4891 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004892 ((msg->sol[5] > '1') ||
4893 ((msg->sol[5] == '1') &&
4894 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004895 txn->flags |= TX_RES_VER_11;
4896
4897 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004898 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 +01004899
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004900 /* transfer length unknown*/
4901 txn->flags &= ~TX_RES_XFER_LEN;
4902
Willy Tarreau962c3f42010-01-10 00:15:35 +01004903 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004904
Willy Tarreau39650402010-03-15 19:44:39 +01004905 /* Adjust server's health based on status code. Note: status codes 501
4906 * and 505 are triggered on demand by client request, so we must not
4907 * count them as server failures.
4908 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004909 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004910 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004911 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004912 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004913 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004914 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004915
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004916 /*
4917 * 2: check for cacheability.
4918 */
4919
4920 switch (txn->status) {
4921 case 200:
4922 case 203:
4923 case 206:
4924 case 300:
4925 case 301:
4926 case 410:
4927 /* RFC2616 @13.4:
4928 * "A response received with a status code of
4929 * 200, 203, 206, 300, 301 or 410 MAY be stored
4930 * by a cache (...) unless a cache-control
4931 * directive prohibits caching."
4932 *
4933 * RFC2616 @9.5: POST method :
4934 * "Responses to this method are not cacheable,
4935 * unless the response includes appropriate
4936 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004937 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004938 if (likely(txn->meth != HTTP_METH_POST) &&
4939 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4940 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4941 break;
4942 default:
4943 break;
4944 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004945
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004946 /*
4947 * 3: we may need to capture headers
4948 */
4949 s->logs.logwait &= ~LW_RESP;
4950 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004951 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004952 txn->rsp.cap, s->fe->rsp_cap);
4953
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004954 /* 4: determine the transfer-length.
4955 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4956 * the presence of a message-body in a RESPONSE and its transfer length
4957 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004958 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004959 * All responses to the HEAD request method MUST NOT include a
4960 * message-body, even though the presence of entity-header fields
4961 * might lead one to believe they do. All 1xx (informational), 204
4962 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4963 * message-body. All other responses do include a message-body,
4964 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004965 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004966 * 1. Any response which "MUST NOT" include a message-body (such as the
4967 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4968 * always terminated by the first empty line after the header fields,
4969 * regardless of the entity-header fields present in the message.
4970 *
4971 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4972 * the "chunked" transfer-coding (Section 6.2) is used, the
4973 * transfer-length is defined by the use of this transfer-coding.
4974 * If a Transfer-Encoding header field is present and the "chunked"
4975 * transfer-coding is not present, the transfer-length is defined by
4976 * the sender closing the connection.
4977 *
4978 * 3. If a Content-Length header field is present, its decimal value in
4979 * OCTETs represents both the entity-length and the transfer-length.
4980 * If a message is received with both a Transfer-Encoding header
4981 * field and a Content-Length header field, the latter MUST be ignored.
4982 *
4983 * 4. If the message uses the media type "multipart/byteranges", and
4984 * the transfer-length is not otherwise specified, then this self-
4985 * delimiting media type defines the transfer-length. This media
4986 * type MUST NOT be used unless the sender knows that the recipient
4987 * can parse it; the presence in a request of a Range header with
4988 * multiple byte-range specifiers from a 1.1 client implies that the
4989 * client can parse multipart/byteranges responses.
4990 *
4991 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004992 */
4993
4994 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004995 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004996 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004997 * FIXME: should we parse anyway and return an error on chunked encoding ?
4998 */
4999 if (txn->meth == HTTP_METH_HEAD ||
5000 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005001 txn->status == 204 || txn->status == 304) {
5002 txn->flags |= TX_RES_XFER_LEN;
5003 goto skip_content_length;
5004 }
5005
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005006 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005007 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01005008 while ((txn->flags & TX_RES_VER_11) &&
5009 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005010 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
5011 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5012 else if (txn->flags & TX_RES_TE_CHNK) {
5013 /* bad transfer-encoding (chunked followed by something else) */
5014 use_close_only = 1;
5015 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5016 break;
5017 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005018 }
5019
5020 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5021 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005022 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005023 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
5024 signed long long cl;
5025
5026 if (!ctx.vlen)
5027 goto hdr_response_bad;
5028
5029 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
5030 goto hdr_response_bad; /* parse failure */
5031
5032 if (cl < 0)
5033 goto hdr_response_bad;
5034
Willy Tarreau124d9912011-03-01 20:30:48 +01005035 if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl))
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005036 goto hdr_response_bad; /* already specified, was different */
5037
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005038 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005039 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005040 }
5041
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005042 /* FIXME: we should also implement the multipart/byterange method.
5043 * For now on, we resort to close mode in this case (unknown length).
5044 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005045skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005046
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005047 /* end of job, return OK */
5048 rep->analysers &= ~an_bit;
5049 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01005050 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005051 return 1;
5052}
5053
5054/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005055 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5056 * and updates t->rep->analysers. It might make sense to explode it into several
5057 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005058 */
5059int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
5060{
5061 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005062 struct http_msg *msg = &txn->rsp;
5063 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005064 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005065
5066 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
5067 now_ms, __FUNCTION__,
5068 t,
5069 rep,
5070 rep->rex, rep->wex,
5071 rep->flags,
5072 rep->l,
5073 rep->analysers);
5074
Willy Tarreau655dce92009-11-08 13:10:58 +01005075 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005076 return 0;
5077
5078 rep->analysers &= ~an_bit;
5079 rep->analyse_exp = TICK_ETERNITY;
5080
Willy Tarreau5b154472009-12-21 20:11:07 +01005081 /* Now we have to check if we need to modify the Connection header.
5082 * This is more difficult on the response than it is on the request,
5083 * because we can have two different HTTP versions and we don't know
5084 * how the client will interprete a response. For instance, let's say
5085 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5086 * HTTP/1.1 response without any header. Maybe it will bound itself to
5087 * HTTP/1.0 because it only knows about it, and will consider the lack
5088 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5089 * the lack of header as a keep-alive. Thus we will use two flags
5090 * indicating how a request MAY be understood by the client. In case
5091 * of multiple possibilities, we'll fix the header to be explicit. If
5092 * ambiguous cases such as both close and keepalive are seen, then we
5093 * will fall back to explicit close. Note that we won't take risks with
5094 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005095 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005096 */
5097
Willy Tarreaudc008c52010-02-01 16:20:08 +01005098 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5099 txn->status == 101)) {
5100 /* Either we've established an explicit tunnel, or we're
5101 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005102 * to understand the next protocols. We have to switch to tunnel
5103 * mode, so that we transfer the request and responses then let
5104 * this protocol pass unmodified. When we later implement specific
5105 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005106 * header which contains information about that protocol for
5107 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005108 */
5109 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5110 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005111 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5112 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5113 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005114 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005115
Willy Tarreau60466522010-01-18 19:08:45 +01005116 /* on unknown transfer length, we must close */
5117 if (!(txn->flags & TX_RES_XFER_LEN) &&
5118 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5119 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005120
Willy Tarreau60466522010-01-18 19:08:45 +01005121 /* now adjust header transformations depending on current state */
5122 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5123 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5124 to_del |= 2; /* remove "keep-alive" on any response */
5125 if (!(txn->flags & TX_RES_VER_11))
5126 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005127 }
Willy Tarreau60466522010-01-18 19:08:45 +01005128 else { /* SCL / KAL */
5129 to_del |= 1; /* remove "close" on any response */
5130 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
5131 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005132 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005133
Willy Tarreau60466522010-01-18 19:08:45 +01005134 /* Parse and remove some headers from the connection header */
5135 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005136
Willy Tarreau60466522010-01-18 19:08:45 +01005137 /* Some keep-alive responses are converted to Server-close if
5138 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005139 */
Willy Tarreau60466522010-01-18 19:08:45 +01005140 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5141 if ((txn->flags & TX_HDR_CONN_CLO) ||
5142 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
5143 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005144 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005145 }
5146
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005147 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005148 /*
5149 * 3: we will have to evaluate the filters.
5150 * As opposed to version 1.2, now they will be evaluated in the
5151 * filters order and not in the header order. This means that
5152 * each filter has to be validated among all headers.
5153 *
5154 * Filters are tried with ->be first, then with ->fe if it is
5155 * different from ->be.
5156 */
5157
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005158 cur_proxy = t->be;
5159 while (1) {
5160 struct proxy *rule_set = cur_proxy;
5161
5162 /* try headers filters */
5163 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005164 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005165 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005166 if (target_srv(&t->target)) {
5167 target_srv(&t->target)->counters.failed_resp++;
5168 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005169 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005170 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005171 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005172 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005173 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005174 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01005175 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005176 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005177 if (!(t->flags & SN_ERR_MASK))
5178 t->flags |= SN_ERR_PRXCOND;
5179 if (!(t->flags & SN_FINST_MASK))
5180 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005181 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005182 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005183 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005184
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005185 /* has the response been denied ? */
5186 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005187 if (target_srv(&t->target))
5188 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005189
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005190 t->be->be_counters.denied_resp++;
5191 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005192 if (t->listener->counters)
5193 t->listener->counters->denied_resp++;
5194
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005195 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005196 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005197
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005198 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005199 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005200 if (txn->status < 200)
5201 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005202 if (wl->cond) {
5203 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5204 ret = acl_pass(ret);
5205 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5206 ret = !ret;
5207 if (!ret)
5208 continue;
5209 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005210 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005211 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005212 }
5213
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005214 /* check whether we're already working on the frontend */
5215 if (cur_proxy == t->fe)
5216 break;
5217 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005218 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005219
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005220 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005221 * We may be facing a 100-continue response, in which case this
5222 * is not the right response, and we're waiting for the next one.
5223 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005224 * next one.
5225 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005226 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005227 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005228 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005229 msg->msg_state = HTTP_MSG_RPBEFORE;
5230 txn->status = 0;
5231 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5232 return 1;
5233 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005234 else if (unlikely(txn->status < 200))
5235 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005236
5237 /* we don't have any 1xx status code now */
5238
5239 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005240 * 4: check for server cookie.
5241 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005242 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5243 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005244 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005245
Willy Tarreaubaaee002006-06-26 02:48:02 +02005246
Willy Tarreaua15645d2007-03-18 16:22:39 +01005247 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005248 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005249 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005250 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005251 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005252
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005253 /*
5254 * 6: add server cookie in the response if needed
5255 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005256 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005257 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005258 (!(t->flags & SN_DIRECT) ||
5259 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5260 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5261 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5262 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005263 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5264 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005265 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005266 /* the server is known, it's not the one the client requested, or the
5267 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005268 * insert a set-cookie here, except if we want to insert only on POST
5269 * requests and this one isn't. Note that servers which don't have cookies
5270 * (eg: some backup servers) will return a full cookie removal request.
5271 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005272 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005273 len = sprintf(trash,
5274 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5275 t->be->cookie_name);
5276 }
5277 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005278 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005279
5280 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5281 /* emit last_date, which is mandatory */
5282 trash[len++] = COOKIE_DELIM_DATE;
5283 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5284 if (t->be->cookie_maxlife) {
5285 /* emit first_date, which is either the original one or
5286 * the current date.
5287 */
5288 trash[len++] = COOKIE_DELIM_DATE;
5289 s30tob64(txn->cookie_first_date ?
5290 txn->cookie_first_date >> 2 :
5291 (date.tv_sec+3) >> 2, trash + len);
5292 len += 5;
5293 }
5294 }
5295 len += sprintf(trash + len, "; path=/");
5296 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005297
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005298 if (t->be->cookie_domain)
5299 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005300
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005301 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005302 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005303 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005304
Willy Tarreauf1348312010-10-07 15:54:11 +02005305 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005306 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005307 /* the server did not change, only the date was updated */
5308 txn->flags |= TX_SCK_UPDATED;
5309 else
5310 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005311
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005312 /* Here, we will tell an eventual cache on the client side that we don't
5313 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5314 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5315 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5316 */
5317 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005318
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005319 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5320
5321 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005322 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005323 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005324 }
5325 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005326
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005327 /*
5328 * 7: check if result will be cacheable with a cookie.
5329 * We'll block the response if security checks have caught
5330 * nasty things such as a cacheable cookie.
5331 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005332 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5333 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005334 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005335
5336 /* we're in presence of a cacheable response containing
5337 * a set-cookie header. We'll block it as requested by
5338 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005339 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005340 if (target_srv(&t->target))
5341 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005342
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005343 t->be->be_counters.denied_resp++;
5344 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005345 if (t->listener->counters)
5346 t->listener->counters->denied_resp++;
5347
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005348 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005349 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005350 send_log(t->be, LOG_ALERT,
5351 "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 goto return_srv_prx_502;
5354 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005355
5356 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005357 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005358 */
Willy Tarreau60466522010-01-18 19:08:45 +01005359 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5360 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5361 unsigned int want_flags = 0;
5362
5363 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5364 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5365 /* we want a keep-alive response here. Keep-alive header
5366 * required if either side is not 1.1.
5367 */
5368 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5369 want_flags |= TX_CON_KAL_SET;
5370 }
5371 else {
5372 /* we want a close response here. Close header required if
5373 * the server is 1.1, regardless of the client.
5374 */
5375 if (txn->flags & TX_RES_VER_11)
5376 want_flags |= TX_CON_CLO_SET;
5377 }
5378
5379 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5380 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005381 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005382
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005383 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005384 if ((txn->flags & TX_RES_XFER_LEN) ||
5385 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005386 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005387
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005388 /*************************************************************
5389 * OK, that's finished for the headers. We have done what we *
5390 * could. Let's switch to the DATA state. *
5391 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005392
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005393 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005394
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005395 /* if the user wants to log as soon as possible, without counting
5396 * bytes from the server, then this is the right moment. We have
5397 * to temporarily assign bytes_out to log what we currently have.
5398 */
5399 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5400 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5401 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005402 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005403 t->logs.bytes_out = 0;
5404 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005405
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005406 /* Note: we must not try to cheat by jumping directly to DATA,
5407 * otherwise we would not let the client side wake up.
5408 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005409
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005410 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005411 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005412 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005413}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005414
Willy Tarreaud98cf932009-12-27 22:54:55 +01005415/* This function is an analyser which forwards response body (including chunk
5416 * sizes if any). It is called as soon as we must forward, even if we forward
5417 * zero byte. The only situation where it must not be called is when we're in
5418 * tunnel mode and we want to forward till the close. It's used both to forward
5419 * remaining data and to resync after end of body. It expects the msg_state to
5420 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5421 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005422 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005423 * bytes of pending data + the headers if not already done (between som and sov).
5424 * It eventually adjusts som to match sov after the data in between have been sent.
5425 */
5426int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5427{
5428 struct http_txn *txn = &s->txn;
5429 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005430 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005431
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005432 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5433 return 0;
5434
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005435 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005436 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005437 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005438 /* Output closed while we were sending data. We must abort and
5439 * wake the other side up.
5440 */
5441 msg->msg_state = HTTP_MSG_ERROR;
5442 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005443 return 1;
5444 }
5445
Willy Tarreau4fe41902010-06-07 22:27:41 +02005446 /* in most states, we should abort in case of early close */
5447 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005448
Willy Tarreaud98cf932009-12-27 22:54:55 +01005449 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5450 /* we have msg->col and msg->sov which both point to the first
5451 * byte of message body. msg->som still points to the beginning
5452 * of the message. We must save the body in req->lr because it
5453 * survives buffer re-alignments.
5454 */
5455 res->lr = res->data + msg->sov;
5456 if (txn->flags & TX_RES_TE_CHNK)
5457 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5458 else {
5459 msg->msg_state = HTTP_MSG_DATA;
5460 }
5461 }
5462
Willy Tarreaud98cf932009-12-27 22:54:55 +01005463 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005464 int bytes;
5465
Willy Tarreau610ecce2010-01-04 21:15:02 +01005466 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005467 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005468 bytes = msg->sov - msg->som;
5469 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005470 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005471 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5472 bytes += res->size;
5473 msg->chunk_len += (unsigned int)bytes;
5474 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005475 }
5476
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005477
Willy Tarreaucaabe412010-01-03 23:08:28 +01005478 if (msg->msg_state == HTTP_MSG_DATA) {
5479 /* must still forward */
5480 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005481 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005482
5483 /* nothing left to forward */
5484 if (txn->flags & TX_RES_TE_CHNK)
5485 msg->msg_state = HTTP_MSG_DATA_CRLF;
5486 else
5487 msg->msg_state = HTTP_MSG_DONE;
5488 }
5489 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005490 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005491 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5492 */
5493 int ret = http_parse_chunk_size(res, msg);
5494
5495 if (!ret)
5496 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005497 else if (ret < 0) {
5498 if (msg->err_pos >= 0)
5499 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005500 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005501 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005502 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005503 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005504 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5505 /* we want the CRLF after the data */
5506 int ret;
5507
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005508 res->lr = res->w + res->send_max;
5509 if (res->lr >= res->data + res->size)
5510 res->lr -= res->size;
5511
Willy Tarreaud98cf932009-12-27 22:54:55 +01005512 ret = http_skip_chunk_crlf(res, msg);
5513
5514 if (!ret)
5515 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005516 else if (ret < 0) {
5517 if (msg->err_pos >= 0)
5518 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005519 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005520 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005521 /* we're in MSG_CHUNK_SIZE now */
5522 }
5523 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5524 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005525
Willy Tarreaud98cf932009-12-27 22:54:55 +01005526 if (ret == 0)
5527 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005528 else if (ret < 0) {
5529 if (msg->err_pos >= 0)
5530 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005531 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005532 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005533 /* we're in HTTP_MSG_DONE now */
5534 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005535 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005536 int old_state = msg->msg_state;
5537
Willy Tarreau610ecce2010-01-04 21:15:02 +01005538 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005539 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005540 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5541 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5542 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005543 if (http_resync_states(s)) {
5544 http_silent_debug(__LINE__, s);
5545 /* some state changes occurred, maybe the analyser
5546 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005547 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005548 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5549 if (res->flags & BF_SHUTW) {
5550 /* response errors are most likely due to
5551 * the client aborting the transfer.
5552 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005553 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005554 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005555 if (msg->err_pos >= 0)
5556 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005557 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005558 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005559 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005560 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005561 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005562 }
5563 }
5564
Willy Tarreaud98cf932009-12-27 22:54:55 +01005565 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005566 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005567 if (res->flags & BF_SHUTR) {
5568 if (!(s->flags & SN_ERR_MASK))
5569 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005570 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005571 if (target_srv(&s->target))
5572 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005573 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005574 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005575
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005576 if (res->flags & BF_SHUTW)
5577 goto aborted_xfer;
5578
Willy Tarreau40dba092010-03-04 18:14:51 +01005579 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005580 if (!s->req->analysers)
5581 goto return_bad_res;
5582
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005583 /* forward any pending data */
5584 bytes = msg->sov - msg->som;
5585 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005586 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005587 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5588 bytes += res->size;
5589 msg->chunk_len += (unsigned int)bytes;
5590 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005591 }
5592
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005593 /* When TE: chunked is used, we need to get there again to parse remaining
5594 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5595 * Similarly, with keep-alive on the client side, we don't want to forward a
5596 * close.
5597 */
5598 if ((txn->flags & TX_RES_TE_CHNK) ||
5599 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5600 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5601 buffer_dont_close(res);
5602
Willy Tarreau5c620922011-05-11 19:56:11 +02005603 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005604 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5605 * system knows it must not set a PUSH on this first part. Interactive
5606 * modes are already handled by the stream sock layer.
Willy Tarreau5c620922011-05-11 19:56:11 +02005607 */
Willy Tarreau07293032011-05-30 18:29:28 +02005608 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005609
Willy Tarreaud98cf932009-12-27 22:54:55 +01005610 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005611 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005612 return 0;
5613
Willy Tarreau40dba092010-03-04 18:14:51 +01005614 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005615 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005616 if (target_srv(&s->target))
5617 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005618
5619 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005620 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005621 /* don't send any error message as we're in the body */
5622 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005623 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005624 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005625 if (target_srv(&s->target))
5626 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005627
5628 if (!(s->flags & SN_ERR_MASK))
5629 s->flags |= SN_ERR_PRXCOND;
5630 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005631 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005632 return 0;
5633
5634 aborted_xfer:
5635 txn->rsp.msg_state = HTTP_MSG_ERROR;
5636 /* don't send any error message as we're in the body */
5637 stream_int_retnclose(res->cons, NULL);
5638 res->analysers = 0;
5639 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5640
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005641 s->fe->fe_counters.cli_aborts++;
5642 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005643 if (target_srv(&s->target))
5644 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005645
5646 if (!(s->flags & SN_ERR_MASK))
5647 s->flags |= SN_ERR_CLICL;
5648 if (!(s->flags & SN_FINST_MASK))
5649 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005650 return 0;
5651}
5652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005653/* Iterate the same filter through all request headers.
5654 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005655 * Since it can manage the switch to another backend, it updates the per-proxy
5656 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005657 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005658int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005659{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005660 char term;
5661 char *cur_ptr, *cur_end, *cur_next;
5662 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005663 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005664 struct hdr_idx_elem *cur_hdr;
5665 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005666
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005667 last_hdr = 0;
5668
Willy Tarreau962c3f42010-01-10 00:15:35 +01005669 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005670 old_idx = 0;
5671
5672 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005673 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005674 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005675 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005676 (exp->action == ACT_ALLOW ||
5677 exp->action == ACT_DENY ||
5678 exp->action == ACT_TARPIT))
5679 return 0;
5680
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005681 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005682 if (!cur_idx)
5683 break;
5684
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005685 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005686 cur_ptr = cur_next;
5687 cur_end = cur_ptr + cur_hdr->len;
5688 cur_next = cur_end + cur_hdr->cr + 1;
5689
5690 /* Now we have one header between cur_ptr and cur_end,
5691 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005692 */
5693
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005694 /* The annoying part is that pattern matching needs
5695 * that we modify the contents to null-terminate all
5696 * strings before testing them.
5697 */
5698
5699 term = *cur_end;
5700 *cur_end = '\0';
5701
5702 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5703 switch (exp->action) {
5704 case ACT_SETBE:
5705 /* It is not possible to jump a second time.
5706 * FIXME: should we return an HTTP/500 here so that
5707 * the admin knows there's a problem ?
5708 */
5709 if (t->be != t->fe)
5710 break;
5711
5712 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005713 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005714 last_hdr = 1;
5715 break;
5716
5717 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005718 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005719 last_hdr = 1;
5720 break;
5721
5722 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005723 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005724 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005725
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005726 t->fe->fe_counters.denied_req++;
5727 if (t->fe != t->be)
5728 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005729 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005730 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005731
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005732 break;
5733
5734 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005735 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005736 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005737
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005738 t->fe->fe_counters.denied_req++;
5739 if (t->fe != t->be)
5740 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005741 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005742 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005743
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005744 break;
5745
5746 case ACT_REPLACE:
5747 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5748 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5749 /* FIXME: if the user adds a newline in the replacement, the
5750 * index will not be recalculated for now, and the new line
5751 * will not be counted as a new header.
5752 */
5753
5754 cur_end += delta;
5755 cur_next += delta;
5756 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005757 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005758 break;
5759
5760 case ACT_REMOVE:
5761 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5762 cur_next += delta;
5763
Willy Tarreaufa355d42009-11-29 18:12:29 +01005764 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005765 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5766 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005767 cur_hdr->len = 0;
5768 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005769 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005770 break;
5771
5772 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005773 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005774 if (cur_end)
5775 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005776
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005777 /* keep the link from this header to next one in case of later
5778 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005779 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005780 old_idx = cur_idx;
5781 }
5782 return 0;
5783}
5784
5785
5786/* Apply the filter to the request line.
5787 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5788 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005789 * Since it can manage the switch to another backend, it updates the per-proxy
5790 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005791 */
5792int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5793{
5794 char term;
5795 char *cur_ptr, *cur_end;
5796 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005797 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005798 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005799
Willy Tarreau58f10d72006-12-04 02:26:12 +01005800
Willy Tarreau3d300592007-03-18 18:34:41 +01005801 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005802 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005803 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005804 (exp->action == ACT_ALLOW ||
5805 exp->action == ACT_DENY ||
5806 exp->action == ACT_TARPIT))
5807 return 0;
5808 else if (exp->action == ACT_REMOVE)
5809 return 0;
5810
5811 done = 0;
5812
Willy Tarreau962c3f42010-01-10 00:15:35 +01005813 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005814 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005815
5816 /* Now we have the request line between cur_ptr and cur_end */
5817
5818 /* The annoying part is that pattern matching needs
5819 * that we modify the contents to null-terminate all
5820 * strings before testing them.
5821 */
5822
5823 term = *cur_end;
5824 *cur_end = '\0';
5825
5826 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5827 switch (exp->action) {
5828 case ACT_SETBE:
5829 /* It is not possible to jump a second time.
5830 * FIXME: should we return an HTTP/500 here so that
5831 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005832 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005833 if (t->be != t->fe)
5834 break;
5835
5836 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005837 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005838 done = 1;
5839 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005840
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005841 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005842 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005843 done = 1;
5844 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005845
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005846 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005847 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005848
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005849 t->fe->fe_counters.denied_req++;
5850 if (t->fe != t->be)
5851 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005852 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005853 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005854
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005855 done = 1;
5856 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005857
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005858 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005859 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005860
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005861 t->fe->fe_counters.denied_req++;
5862 if (t->fe != t->be)
5863 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005864 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005865 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005866
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005867 done = 1;
5868 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005869
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005870 case ACT_REPLACE:
5871 *cur_end = term; /* restore the string terminator */
5872 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5873 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5874 /* FIXME: if the user adds a newline in the replacement, the
5875 * index will not be recalculated for now, and the new line
5876 * will not be counted as a new header.
5877 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005878
Willy Tarreaufa355d42009-11-29 18:12:29 +01005879 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005880 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005881 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005882 HTTP_MSG_RQMETH,
5883 cur_ptr, cur_end + 1,
5884 NULL, NULL);
5885 if (unlikely(!cur_end))
5886 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005887
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005888 /* we have a full request and we know that we have either a CR
5889 * or an LF at <ptr>.
5890 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005891 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5892 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005893 /* there is no point trying this regex on headers */
5894 return 1;
5895 }
5896 }
5897 *cur_end = term; /* restore the string terminator */
5898 return done;
5899}
Willy Tarreau97de6242006-12-27 17:18:38 +01005900
Willy Tarreau58f10d72006-12-04 02:26:12 +01005901
Willy Tarreau58f10d72006-12-04 02:26:12 +01005902
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005903/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005904 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005905 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005906 * unparsable request. Since it can manage the switch to another backend, it
5907 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005908 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005909int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005910{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005911 struct http_txn *txn = &s->txn;
5912 struct hdr_exp *exp;
5913
5914 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005915 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005916
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005917 /*
5918 * The interleaving of transformations and verdicts
5919 * makes it difficult to decide to continue or stop
5920 * the evaluation.
5921 */
5922
Willy Tarreau6c123b12010-01-28 20:22:06 +01005923 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5924 break;
5925
Willy Tarreau3d300592007-03-18 18:34:41 +01005926 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005927 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005928 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005929 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005930
5931 /* if this filter had a condition, evaluate it now and skip to
5932 * next filter if the condition does not match.
5933 */
5934 if (exp->cond) {
5935 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5936 ret = acl_pass(ret);
5937 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5938 ret = !ret;
5939
5940 if (!ret)
5941 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005942 }
5943
5944 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005945 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005946 if (unlikely(ret < 0))
5947 return -1;
5948
5949 if (likely(ret == 0)) {
5950 /* The filter did not match the request, it can be
5951 * iterated through all headers.
5952 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005953 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005954 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005955 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005956 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005957}
5958
5959
Willy Tarreaua15645d2007-03-18 16:22:39 +01005960
Willy Tarreau58f10d72006-12-04 02:26:12 +01005961/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005962 * Try to retrieve the server associated to the appsession.
5963 * If the server is found, it's assigned to the session.
5964 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005965void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005966 struct http_txn *txn = &t->txn;
5967 appsess *asession = NULL;
5968 char *sessid_temp = NULL;
5969
Cyril Bontéb21570a2009-11-29 20:04:48 +01005970 if (len > t->be->appsession_len) {
5971 len = t->be->appsession_len;
5972 }
5973
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005974 if (t->be->options2 & PR_O2_AS_REQL) {
5975 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005976 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005977 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005978 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005979 }
5980
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005981 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005982 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5983 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5984 return;
5985 }
5986
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005987 memcpy(txn->sessid, buf, len);
5988 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005989 }
5990
5991 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5992 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5993 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5994 return;
5995 }
5996
Cyril Bontéb21570a2009-11-29 20:04:48 +01005997 memcpy(sessid_temp, buf, len);
5998 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005999
6000 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6001 /* free previously allocated memory */
6002 pool_free2(apools.sessid, sessid_temp);
6003
6004 if (asession != NULL) {
6005 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6006 if (!(t->be->options2 & PR_O2_AS_REQL))
6007 asession->request_count++;
6008
6009 if (asession->serverid != NULL) {
6010 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006011
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006012 while (srv) {
6013 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006014 if ((srv->state & SRV_RUNNING) ||
6015 (t->be->options & PR_O_PERSIST) ||
6016 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006017 /* we found the server and it's usable */
6018 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006019 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006020 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006021 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01006022
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006023 break;
6024 } else {
6025 txn->flags &= ~TX_CK_MASK;
6026 txn->flags |= TX_CK_DOWN;
6027 }
6028 }
6029 srv = srv->next;
6030 }
6031 }
6032 }
6033}
6034
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006035/* Find the end of a cookie value contained between <s> and <e>. It works the
6036 * same way as with headers above except that the semi-colon also ends a token.
6037 * See RFC2965 for more information. Note that it requires a valid header to
6038 * return a valid result.
6039 */
6040char *find_cookie_value_end(char *s, const char *e)
6041{
6042 int quoted, qdpair;
6043
6044 quoted = qdpair = 0;
6045 for (; s < e; s++) {
6046 if (qdpair) qdpair = 0;
6047 else if (quoted) {
6048 if (*s == '\\') qdpair = 1;
6049 else if (*s == '"') quoted = 0;
6050 }
6051 else if (*s == '"') quoted = 1;
6052 else if (*s == ',' || *s == ';') return s;
6053 }
6054 return s;
6055}
6056
6057/* Delete a value in a header between delimiters <from> and <next> in buffer
6058 * <buf>. The number of characters displaced is returned, and the pointer to
6059 * the first delimiter is updated if required. The function tries as much as
6060 * possible to respect the following principles :
6061 * - replace <from> delimiter by the <next> one unless <from> points to a
6062 * colon, in which case <next> is simply removed
6063 * - set exactly one space character after the new first delimiter, unless
6064 * there are not enough characters in the block being moved to do so.
6065 * - remove unneeded spaces before the previous delimiter and after the new
6066 * one.
6067 *
6068 * It is the caller's responsibility to ensure that :
6069 * - <from> points to a valid delimiter or the colon ;
6070 * - <next> points to a valid delimiter or the final CR/LF ;
6071 * - there are non-space chars before <from> ;
6072 * - there is a CR/LF at or after <next>.
6073 */
6074int del_hdr_value(struct buffer *buf, char **from, char *next)
6075{
6076 char *prev = *from;
6077
6078 if (*prev == ':') {
6079 /* We're removing the first value, preserve the colon and add a
6080 * space if possible.
6081 */
6082 if (!http_is_crlf[(unsigned char)*next])
6083 next++;
6084 prev++;
6085 if (prev < next)
6086 *prev++ = ' ';
6087
6088 while (http_is_spht[(unsigned char)*next])
6089 next++;
6090 } else {
6091 /* Remove useless spaces before the old delimiter. */
6092 while (http_is_spht[(unsigned char)*(prev-1)])
6093 prev--;
6094 *from = prev;
6095
6096 /* copy the delimiter and if possible a space if we're
6097 * not at the end of the line.
6098 */
6099 if (!http_is_crlf[(unsigned char)*next]) {
6100 *prev++ = *next++;
6101 if (prev + 1 < next)
6102 *prev++ = ' ';
6103 while (http_is_spht[(unsigned char)*next])
6104 next++;
6105 }
6106 }
6107 return buffer_replace2(buf, prev, next, NULL, 0);
6108}
6109
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006110/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006111 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006112 * desirable to call it only when needed. This code is quite complex because
6113 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6114 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006115 */
6116void manage_client_side_cookies(struct session *t, struct buffer *req)
6117{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006118 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006119 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006120 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006121 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6122 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006123
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006124 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006125 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006126 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006127
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006128 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006129 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006130 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006131
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006132 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006133 hdr_beg = hdr_next;
6134 hdr_end = hdr_beg + cur_hdr->len;
6135 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006136
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006137 /* We have one full header between hdr_beg and hdr_end, and the
6138 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006139 * "Cookie:" headers.
6140 */
6141
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006142 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006143 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006144 old_idx = cur_idx;
6145 continue;
6146 }
6147
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006148 del_from = NULL; /* nothing to be deleted */
6149 preserve_hdr = 0; /* assume we may kill the whole header */
6150
Willy Tarreau58f10d72006-12-04 02:26:12 +01006151 /* Now look for cookies. Conforming to RFC2109, we have to support
6152 * attributes whose name begin with a '$', and associate them with
6153 * the right cookie, if we want to delete this cookie.
6154 * So there are 3 cases for each cookie read :
6155 * 1) it's a special attribute, beginning with a '$' : ignore it.
6156 * 2) it's a server id cookie that we *MAY* want to delete : save
6157 * some pointers on it (last semi-colon, beginning of cookie...)
6158 * 3) it's an application cookie : we *MAY* have to delete a previous
6159 * "special" cookie.
6160 * At the end of loop, if a "special" cookie remains, we may have to
6161 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006162 * *MUST* delete it.
6163 *
6164 * Note: RFC2965 is unclear about the processing of spaces around
6165 * the equal sign in the ATTR=VALUE form. A careful inspection of
6166 * the RFC explicitly allows spaces before it, and not within the
6167 * tokens (attrs or values). An inspection of RFC2109 allows that
6168 * too but section 10.1.3 lets one think that spaces may be allowed
6169 * after the equal sign too, resulting in some (rare) buggy
6170 * implementations trying to do that. So let's do what servers do.
6171 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6172 * allowed quoted strings in values, with any possible character
6173 * after a backslash, including control chars and delimitors, which
6174 * causes parsing to become ambiguous. Browsers also allow spaces
6175 * within values even without quotes.
6176 *
6177 * We have to keep multiple pointers in order to support cookie
6178 * removal at the beginning, middle or end of header without
6179 * corrupting the header. All of these headers are valid :
6180 *
6181 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6182 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6183 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6184 * | | | | | | | | |
6185 * | | | | | | | | hdr_end <--+
6186 * | | | | | | | +--> next
6187 * | | | | | | +----> val_end
6188 * | | | | | +-----------> val_beg
6189 * | | | | +--------------> equal
6190 * | | | +----------------> att_end
6191 * | | +---------------------> att_beg
6192 * | +--------------------------> prev
6193 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006194 */
6195
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006196 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6197 /* Iterate through all cookies on this line */
6198
6199 /* find att_beg */
6200 att_beg = prev + 1;
6201 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6202 att_beg++;
6203
6204 /* find att_end : this is the first character after the last non
6205 * space before the equal. It may be equal to hdr_end.
6206 */
6207 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006208
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006209 while (equal < hdr_end) {
6210 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006211 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006212 if (http_is_spht[(unsigned char)*equal++])
6213 continue;
6214 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006215 }
6216
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006217 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6218 * is between <att_beg> and <equal>, both may be identical.
6219 */
6220
6221 /* look for end of cookie if there is an equal sign */
6222 if (equal < hdr_end && *equal == '=') {
6223 /* look for the beginning of the value */
6224 val_beg = equal + 1;
6225 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6226 val_beg++;
6227
6228 /* find the end of the value, respecting quotes */
6229 next = find_cookie_value_end(val_beg, hdr_end);
6230
6231 /* make val_end point to the first white space or delimitor after the value */
6232 val_end = next;
6233 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6234 val_end--;
6235 } else {
6236 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006237 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006238
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006239 /* We have nothing to do with attributes beginning with '$'. However,
6240 * they will automatically be removed if a header before them is removed,
6241 * since they're supposed to be linked together.
6242 */
6243 if (*att_beg == '$')
6244 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006245
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006246 /* Ignore cookies with no equal sign */
6247 if (equal == next) {
6248 /* This is not our cookie, so we must preserve it. But if we already
6249 * scheduled another cookie for removal, we cannot remove the
6250 * complete header, but we can remove the previous block itself.
6251 */
6252 preserve_hdr = 1;
6253 if (del_from != NULL) {
6254 int delta = del_hdr_value(req, &del_from, prev);
6255 val_end += delta;
6256 next += delta;
6257 hdr_end += delta;
6258 hdr_next += delta;
6259 cur_hdr->len += delta;
6260 http_msg_move_end(&txn->req, delta);
6261 prev = del_from;
6262 del_from = NULL;
6263 }
6264 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006265 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006266
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006267 /* if there are spaces around the equal sign, we need to
6268 * strip them otherwise we'll get trouble for cookie captures,
6269 * or even for rewrites. Since this happens extremely rarely,
6270 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006271 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006272 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6273 int stripped_before = 0;
6274 int stripped_after = 0;
6275
6276 if (att_end != equal) {
6277 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6278 equal += stripped_before;
6279 val_beg += stripped_before;
6280 }
6281
6282 if (val_beg > equal + 1) {
6283 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6284 val_beg += stripped_after;
6285 stripped_before += stripped_after;
6286 }
6287
6288 val_end += stripped_before;
6289 next += stripped_before;
6290 hdr_end += stripped_before;
6291 hdr_next += stripped_before;
6292 cur_hdr->len += stripped_before;
6293 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006294 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006295 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006296
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006297 /* First, let's see if we want to capture this cookie. We check
6298 * that we don't already have a client side cookie, because we
6299 * can only capture one. Also as an optimisation, we ignore
6300 * cookies shorter than the declared name.
6301 */
6302 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6303 (val_end - att_beg >= t->fe->capture_namelen) &&
6304 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6305 int log_len = val_end - att_beg;
6306
6307 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6308 Alert("HTTP logging : out of memory.\n");
6309 } else {
6310 if (log_len > t->fe->capture_len)
6311 log_len = t->fe->capture_len;
6312 memcpy(txn->cli_cookie, att_beg, log_len);
6313 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006314 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006315 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006316
Willy Tarreaubca99692010-10-06 19:25:55 +02006317 /* Persistence cookies in passive, rewrite or insert mode have the
6318 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006319 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006320 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006321 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006322 * For cookies in prefix mode, the form is :
6323 *
6324 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006325 */
6326 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6327 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6328 struct server *srv = t->be->srv;
6329 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006330
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006331 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6332 * have the server ID between val_beg and delim, and the original cookie between
6333 * delim+1 and val_end. Otherwise, delim==val_end :
6334 *
6335 * Cookie: NAME=SRV; # in all but prefix modes
6336 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6337 * | || || | |+-> next
6338 * | || || | +--> val_end
6339 * | || || +---------> delim
6340 * | || |+------------> val_beg
6341 * | || +-------------> att_end = equal
6342 * | |+-----------------> att_beg
6343 * | +------------------> prev
6344 * +-------------------------> hdr_beg
6345 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006346
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006347 if (t->be->options & PR_O_COOK_PFX) {
6348 for (delim = val_beg; delim < val_end; delim++)
6349 if (*delim == COOKIE_DELIM)
6350 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006351 } else {
6352 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006353 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006354 /* Now check if the cookie contains a date field, which would
6355 * appear after a vertical bar ('|') just after the server name
6356 * and before the delimiter.
6357 */
6358 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6359 if (vbar1) {
6360 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006361 * right is the last seen date. It is a base64 encoded
6362 * 30-bit value representing the UNIX date since the
6363 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006364 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006365 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006366 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006367 if (val_end - vbar1 >= 5) {
6368 val = b64tos30(vbar1);
6369 if (val > 0)
6370 txn->cookie_last_date = val << 2;
6371 }
6372 /* look for a second vertical bar */
6373 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6374 if (vbar1 && (val_end - vbar1 > 5)) {
6375 val = b64tos30(vbar1 + 1);
6376 if (val > 0)
6377 txn->cookie_first_date = val << 2;
6378 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006379 }
6380 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006381
Willy Tarreauf64d1412010-10-07 20:06:11 +02006382 /* if the cookie has an expiration date and the proxy wants to check
6383 * it, then we do that now. We first check if the cookie is too old,
6384 * then only if it has expired. We detect strict overflow because the
6385 * time resolution here is not great (4 seconds). Cookies with dates
6386 * in the future are ignored if their offset is beyond one day. This
6387 * allows an admin to fix timezone issues without expiring everyone
6388 * and at the same time avoids keeping unwanted side effects for too
6389 * long.
6390 */
6391 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006392 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6393 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006394 txn->flags &= ~TX_CK_MASK;
6395 txn->flags |= TX_CK_OLD;
6396 delim = val_beg; // let's pretend we have not found the cookie
6397 txn->cookie_first_date = 0;
6398 txn->cookie_last_date = 0;
6399 }
6400 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006401 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6402 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006403 txn->flags &= ~TX_CK_MASK;
6404 txn->flags |= TX_CK_EXPIRED;
6405 delim = val_beg; // let's pretend we have not found the cookie
6406 txn->cookie_first_date = 0;
6407 txn->cookie_last_date = 0;
6408 }
6409
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006410 /* Here, we'll look for the first running server which supports the cookie.
6411 * This allows to share a same cookie between several servers, for example
6412 * to dedicate backup servers to specific servers only.
6413 * However, to prevent clients from sticking to cookie-less backup server
6414 * when they have incidentely learned an empty cookie, we simply ignore
6415 * empty cookies and mark them as invalid.
6416 * The same behaviour is applied when persistence must be ignored.
6417 */
6418 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6419 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006420
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006421 while (srv) {
6422 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6423 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6424 if ((srv->state & SRV_RUNNING) ||
6425 (t->be->options & PR_O_PERSIST) ||
6426 (t->flags & SN_FORCE_PRST)) {
6427 /* we found the server and we can use it */
6428 txn->flags &= ~TX_CK_MASK;
6429 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6430 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006431 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006432 break;
6433 } else {
6434 /* we found a server, but it's down,
6435 * mark it as such and go on in case
6436 * another one is available.
6437 */
6438 txn->flags &= ~TX_CK_MASK;
6439 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006440 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006441 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006442 srv = srv->next;
6443 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006444
Willy Tarreauf64d1412010-10-07 20:06:11 +02006445 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006446 /* no server matched this cookie */
6447 txn->flags &= ~TX_CK_MASK;
6448 txn->flags |= TX_CK_INVALID;
6449 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006450
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006451 /* depending on the cookie mode, we may have to either :
6452 * - delete the complete cookie if we're in insert+indirect mode, so that
6453 * the server never sees it ;
6454 * - remove the server id from the cookie value, and tag the cookie as an
6455 * application cookie so that it does not get accidentely removed later,
6456 * if we're in cookie prefix mode
6457 */
6458 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6459 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006460
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006461 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6462 val_end += delta;
6463 next += delta;
6464 hdr_end += delta;
6465 hdr_next += delta;
6466 cur_hdr->len += delta;
6467 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006468
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006469 del_from = NULL;
6470 preserve_hdr = 1; /* we want to keep this cookie */
6471 }
6472 else if (del_from == NULL &&
6473 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6474 del_from = prev;
6475 }
6476 } else {
6477 /* This is not our cookie, so we must preserve it. But if we already
6478 * scheduled another cookie for removal, we cannot remove the
6479 * complete header, but we can remove the previous block itself.
6480 */
6481 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006482
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006483 if (del_from != NULL) {
6484 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006485 if (att_beg >= del_from)
6486 att_beg += delta;
6487 if (att_end >= del_from)
6488 att_end += delta;
6489 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006490 val_end += delta;
6491 next += delta;
6492 hdr_end += delta;
6493 hdr_next += delta;
6494 cur_hdr->len += delta;
6495 http_msg_move_end(&txn->req, delta);
6496 prev = del_from;
6497 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006498 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006499 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006500
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006501 /* Look for the appsession cookie unless persistence must be ignored */
6502 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6503 int cmp_len, value_len;
6504 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006505
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006506 if (t->be->options2 & PR_O2_AS_PFX) {
6507 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6508 value_begin = att_beg + t->be->appsession_name_len;
6509 value_len = val_end - att_beg - t->be->appsession_name_len;
6510 } else {
6511 cmp_len = att_end - att_beg;
6512 value_begin = val_beg;
6513 value_len = val_end - val_beg;
6514 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006515
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006516 /* let's see if the cookie is our appcookie */
6517 if (cmp_len == t->be->appsession_name_len &&
6518 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6519 manage_client_side_appsession(t, value_begin, value_len);
6520 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006521 }
6522
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006523 /* continue with next cookie on this header line */
6524 att_beg = next;
6525 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006526
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006527 /* There are no more cookies on this line.
6528 * We may still have one (or several) marked for deletion at the
6529 * end of the line. We must do this now in two ways :
6530 * - if some cookies must be preserved, we only delete from the
6531 * mark to the end of line ;
6532 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006533 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006534 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006535 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006536 if (preserve_hdr) {
6537 delta = del_hdr_value(req, &del_from, hdr_end);
6538 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006539 cur_hdr->len += delta;
6540 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006541 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006542
6543 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006544 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6545 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006546 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006547 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006548 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006549 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006550 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006551 }
6552
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006553 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006554 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006555 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006556}
6557
6558
Willy Tarreaua15645d2007-03-18 16:22:39 +01006559/* Iterate the same filter through all response headers contained in <rtr>.
6560 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6561 */
6562int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6563{
6564 char term;
6565 char *cur_ptr, *cur_end, *cur_next;
6566 int cur_idx, old_idx, last_hdr;
6567 struct http_txn *txn = &t->txn;
6568 struct hdr_idx_elem *cur_hdr;
6569 int len, delta;
6570
6571 last_hdr = 0;
6572
Willy Tarreau962c3f42010-01-10 00:15:35 +01006573 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006574 old_idx = 0;
6575
6576 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006577 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006578 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006579 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006580 (exp->action == ACT_ALLOW ||
6581 exp->action == ACT_DENY))
6582 return 0;
6583
6584 cur_idx = txn->hdr_idx.v[old_idx].next;
6585 if (!cur_idx)
6586 break;
6587
6588 cur_hdr = &txn->hdr_idx.v[cur_idx];
6589 cur_ptr = cur_next;
6590 cur_end = cur_ptr + cur_hdr->len;
6591 cur_next = cur_end + cur_hdr->cr + 1;
6592
6593 /* Now we have one header between cur_ptr and cur_end,
6594 * and the next header starts at cur_next.
6595 */
6596
6597 /* The annoying part is that pattern matching needs
6598 * that we modify the contents to null-terminate all
6599 * strings before testing them.
6600 */
6601
6602 term = *cur_end;
6603 *cur_end = '\0';
6604
6605 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6606 switch (exp->action) {
6607 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006608 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006609 last_hdr = 1;
6610 break;
6611
6612 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006613 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006614 last_hdr = 1;
6615 break;
6616
6617 case ACT_REPLACE:
6618 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6619 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6620 /* FIXME: if the user adds a newline in the replacement, the
6621 * index will not be recalculated for now, and the new line
6622 * will not be counted as a new header.
6623 */
6624
6625 cur_end += delta;
6626 cur_next += delta;
6627 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006628 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629 break;
6630
6631 case ACT_REMOVE:
6632 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6633 cur_next += delta;
6634
Willy Tarreaufa355d42009-11-29 18:12:29 +01006635 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006636 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6637 txn->hdr_idx.used--;
6638 cur_hdr->len = 0;
6639 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006640 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641 break;
6642
6643 }
6644 }
6645 if (cur_end)
6646 *cur_end = term; /* restore the string terminator */
6647
6648 /* keep the link from this header to next one in case of later
6649 * removal of next header.
6650 */
6651 old_idx = cur_idx;
6652 }
6653 return 0;
6654}
6655
6656
6657/* Apply the filter to the status line in the response buffer <rtr>.
6658 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6659 * or -1 if a replacement resulted in an invalid status line.
6660 */
6661int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6662{
6663 char term;
6664 char *cur_ptr, *cur_end;
6665 int done;
6666 struct http_txn *txn = &t->txn;
6667 int len, delta;
6668
6669
Willy Tarreau3d300592007-03-18 18:34:41 +01006670 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006671 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006672 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006673 (exp->action == ACT_ALLOW ||
6674 exp->action == ACT_DENY))
6675 return 0;
6676 else if (exp->action == ACT_REMOVE)
6677 return 0;
6678
6679 done = 0;
6680
Willy Tarreau962c3f42010-01-10 00:15:35 +01006681 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006682 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006683
6684 /* Now we have the status line between cur_ptr and cur_end */
6685
6686 /* The annoying part is that pattern matching needs
6687 * that we modify the contents to null-terminate all
6688 * strings before testing them.
6689 */
6690
6691 term = *cur_end;
6692 *cur_end = '\0';
6693
6694 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6695 switch (exp->action) {
6696 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006697 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006698 done = 1;
6699 break;
6700
6701 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006702 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006703 done = 1;
6704 break;
6705
6706 case ACT_REPLACE:
6707 *cur_end = term; /* restore the string terminator */
6708 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6709 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6710 /* FIXME: if the user adds a newline in the replacement, the
6711 * index will not be recalculated for now, and the new line
6712 * will not be counted as a new header.
6713 */
6714
Willy Tarreaufa355d42009-11-29 18:12:29 +01006715 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006716 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006717 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006718 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006719 cur_ptr, cur_end + 1,
6720 NULL, NULL);
6721 if (unlikely(!cur_end))
6722 return -1;
6723
6724 /* we have a full respnse and we know that we have either a CR
6725 * or an LF at <ptr>.
6726 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006727 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006728 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006729 /* there is no point trying this regex on headers */
6730 return 1;
6731 }
6732 }
6733 *cur_end = term; /* restore the string terminator */
6734 return done;
6735}
6736
6737
6738
6739/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006740 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006741 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6742 * unparsable response.
6743 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006744int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006745{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006746 struct http_txn *txn = &s->txn;
6747 struct hdr_exp *exp;
6748
6749 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006750 int ret;
6751
6752 /*
6753 * The interleaving of transformations and verdicts
6754 * makes it difficult to decide to continue or stop
6755 * the evaluation.
6756 */
6757
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006758 if (txn->flags & TX_SVDENY)
6759 break;
6760
Willy Tarreau3d300592007-03-18 18:34:41 +01006761 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006762 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6763 exp->action == ACT_PASS)) {
6764 exp = exp->next;
6765 continue;
6766 }
6767
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006768 /* if this filter had a condition, evaluate it now and skip to
6769 * next filter if the condition does not match.
6770 */
6771 if (exp->cond) {
6772 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6773 ret = acl_pass(ret);
6774 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6775 ret = !ret;
6776 if (!ret)
6777 continue;
6778 }
6779
Willy Tarreaua15645d2007-03-18 16:22:39 +01006780 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006781 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006782 if (unlikely(ret < 0))
6783 return -1;
6784
6785 if (likely(ret == 0)) {
6786 /* The filter did not match the response, it can be
6787 * iterated through all headers.
6788 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006789 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006791 }
6792 return 0;
6793}
6794
6795
Willy Tarreaua15645d2007-03-18 16:22:39 +01006796/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006797 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006798 * desirable to call it only when needed. This function is also used when we
6799 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006800 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006801void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006802{
6803 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006804 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006805 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006806 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006807 char *hdr_beg, *hdr_end, *hdr_next;
6808 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006809
Willy Tarreaua15645d2007-03-18 16:22:39 +01006810 /* Iterate through the headers.
6811 * we start with the start line.
6812 */
6813 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006814 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006815
6816 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6817 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006818 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006819
6820 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006821 hdr_beg = hdr_next;
6822 hdr_end = hdr_beg + cur_hdr->len;
6823 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006824
Willy Tarreau24581ba2010-08-31 22:39:35 +02006825 /* We have one full header between hdr_beg and hdr_end, and the
6826 * next header starts at hdr_next. We're only interested in
6827 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006828 */
6829
Willy Tarreau24581ba2010-08-31 22:39:35 +02006830 is_cookie2 = 0;
6831 prev = hdr_beg + 10;
6832 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006833 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006834 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6835 if (!val) {
6836 old_idx = cur_idx;
6837 continue;
6838 }
6839 is_cookie2 = 1;
6840 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006841 }
6842
Willy Tarreau24581ba2010-08-31 22:39:35 +02006843 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6844 * <prev> points to the colon.
6845 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006846 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006847
Willy Tarreau24581ba2010-08-31 22:39:35 +02006848 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6849 * check-cache is enabled) and we are not interested in checking
6850 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006851 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006852 if (t->be->cookie_name == NULL &&
6853 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006854 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006855 return;
6856
Willy Tarreau24581ba2010-08-31 22:39:35 +02006857 /* OK so now we know we have to process this response cookie.
6858 * The format of the Set-Cookie header is slightly different
6859 * from the format of the Cookie header in that it does not
6860 * support the comma as a cookie delimiter (thus the header
6861 * cannot be folded) because the Expires attribute described in
6862 * the original Netscape's spec may contain an unquoted date
6863 * with a comma inside. We have to live with this because
6864 * many browsers don't support Max-Age and some browsers don't
6865 * support quoted strings. However the Set-Cookie2 header is
6866 * clean.
6867 *
6868 * We have to keep multiple pointers in order to support cookie
6869 * removal at the beginning, middle or end of header without
6870 * corrupting the header (in case of set-cookie2). A special
6871 * pointer, <scav> points to the beginning of the set-cookie-av
6872 * fields after the first semi-colon. The <next> pointer points
6873 * either to the end of line (set-cookie) or next unquoted comma
6874 * (set-cookie2). All of these headers are valid :
6875 *
6876 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6877 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6878 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6879 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6880 * | | | | | | | | | |
6881 * | | | | | | | | +-> next hdr_end <--+
6882 * | | | | | | | +------------> scav
6883 * | | | | | | +--------------> val_end
6884 * | | | | | +--------------------> val_beg
6885 * | | | | +----------------------> equal
6886 * | | | +------------------------> att_end
6887 * | | +----------------------------> att_beg
6888 * | +------------------------------> prev
6889 * +-----------------------------------------> hdr_beg
6890 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006891
Willy Tarreau24581ba2010-08-31 22:39:35 +02006892 for (; prev < hdr_end; prev = next) {
6893 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006894
Willy Tarreau24581ba2010-08-31 22:39:35 +02006895 /* find att_beg */
6896 att_beg = prev + 1;
6897 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6898 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006899
Willy Tarreau24581ba2010-08-31 22:39:35 +02006900 /* find att_end : this is the first character after the last non
6901 * space before the equal. It may be equal to hdr_end.
6902 */
6903 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006904
Willy Tarreau24581ba2010-08-31 22:39:35 +02006905 while (equal < hdr_end) {
6906 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6907 break;
6908 if (http_is_spht[(unsigned char)*equal++])
6909 continue;
6910 att_end = equal;
6911 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006912
Willy Tarreau24581ba2010-08-31 22:39:35 +02006913 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6914 * is between <att_beg> and <equal>, both may be identical.
6915 */
6916
6917 /* look for end of cookie if there is an equal sign */
6918 if (equal < hdr_end && *equal == '=') {
6919 /* look for the beginning of the value */
6920 val_beg = equal + 1;
6921 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6922 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006923
Willy Tarreau24581ba2010-08-31 22:39:35 +02006924 /* find the end of the value, respecting quotes */
6925 next = find_cookie_value_end(val_beg, hdr_end);
6926
6927 /* make val_end point to the first white space or delimitor after the value */
6928 val_end = next;
6929 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6930 val_end--;
6931 } else {
6932 /* <equal> points to next comma, semi-colon or EOL */
6933 val_beg = val_end = next = equal;
6934 }
6935
6936 if (next < hdr_end) {
6937 /* Set-Cookie2 supports multiple cookies, and <next> points to
6938 * a colon or semi-colon before the end. So skip all attr-value
6939 * pairs and look for the next comma. For Set-Cookie, since
6940 * commas are permitted in values, skip to the end.
6941 */
6942 if (is_cookie2)
6943 next = find_hdr_value_end(next, hdr_end);
6944 else
6945 next = hdr_end;
6946 }
6947
6948 /* Now everything is as on the diagram above */
6949
6950 /* Ignore cookies with no equal sign */
6951 if (equal == val_end)
6952 continue;
6953
6954 /* If there are spaces around the equal sign, we need to
6955 * strip them otherwise we'll get trouble for cookie captures,
6956 * or even for rewrites. Since this happens extremely rarely,
6957 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006958 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006959 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6960 int stripped_before = 0;
6961 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006962
Willy Tarreau24581ba2010-08-31 22:39:35 +02006963 if (att_end != equal) {
6964 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6965 equal += stripped_before;
6966 val_beg += stripped_before;
6967 }
6968
6969 if (val_beg > equal + 1) {
6970 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6971 val_beg += stripped_after;
6972 stripped_before += stripped_after;
6973 }
6974
6975 val_end += stripped_before;
6976 next += stripped_before;
6977 hdr_end += stripped_before;
6978 hdr_next += stripped_before;
6979 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006980 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006981 }
6982
6983 /* First, let's see if we want to capture this cookie. We check
6984 * that we don't already have a server side cookie, because we
6985 * can only capture one. Also as an optimisation, we ignore
6986 * cookies shorter than the declared name.
6987 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006988 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006989 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006990 (val_end - att_beg >= t->fe->capture_namelen) &&
6991 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6992 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006993 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006994 Alert("HTTP logging : out of memory.\n");
6995 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006996 else {
6997 if (log_len > t->fe->capture_len)
6998 log_len = t->fe->capture_len;
6999 memcpy(txn->srv_cookie, att_beg, log_len);
7000 txn->srv_cookie[log_len] = 0;
7001 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007002 }
7003
Willy Tarreau827aee92011-03-10 16:55:02 +01007004 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007005 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007006 if (!(t->flags & SN_IGNORE_PRST) &&
7007 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7008 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007009 /* assume passive cookie by default */
7010 txn->flags &= ~TX_SCK_MASK;
7011 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007012
7013 /* If the cookie is in insert mode on a known server, we'll delete
7014 * this occurrence because we'll insert another one later.
7015 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007016 * a direct access.
7017 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007018 if (t->be->options2 & PR_O2_COOK_PSV) {
7019 /* The "preserve" flag was set, we don't want to touch the
7020 * server's cookie.
7021 */
7022 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007023 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007024 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007025 /* this cookie must be deleted */
7026 if (*prev == ':' && next == hdr_end) {
7027 /* whole header */
7028 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
7029 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7030 txn->hdr_idx.used--;
7031 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007032 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007033 hdr_next += delta;
7034 http_msg_move_end(&txn->rsp, delta);
7035 /* note: while both invalid now, <next> and <hdr_end>
7036 * are still equal, so the for() will stop as expected.
7037 */
7038 } else {
7039 /* just remove the value */
7040 int delta = del_hdr_value(res, &prev, next);
7041 next = prev;
7042 hdr_end += delta;
7043 hdr_next += delta;
7044 cur_hdr->len += delta;
7045 http_msg_move_end(&txn->rsp, delta);
7046 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007047 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007048 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007049 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007050 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007051 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007052 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007053 * with this server since we know it.
7054 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007055 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007056 next += delta;
7057 hdr_end += delta;
7058 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007059 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007060 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007061
Willy Tarreauf1348312010-10-07 15:54:11 +02007062 txn->flags &= ~TX_SCK_MASK;
7063 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007064 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007065 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007066 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007067 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007068 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007069 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007070 next += delta;
7071 hdr_end += delta;
7072 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007073 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007074 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007075
Willy Tarreau827aee92011-03-10 16:55:02 +01007076 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007077 txn->flags &= ~TX_SCK_MASK;
7078 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007079 }
7080 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007081 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7082 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007083 int cmp_len, value_len;
7084 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007085
Cyril Bontéb21570a2009-11-29 20:04:48 +01007086 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007087 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7088 value_begin = att_beg + t->be->appsession_name_len;
7089 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007090 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007091 cmp_len = att_end - att_beg;
7092 value_begin = val_beg;
7093 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007094 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007095
Cyril Bonté17530c32010-04-06 21:11:10 +02007096 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007097 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7098 /* free a possibly previously allocated memory */
7099 pool_free2(apools.sessid, txn->sessid);
7100
Cyril Bontéb21570a2009-11-29 20:04:48 +01007101 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007102 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007103 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7104 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7105 return;
7106 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007107 memcpy(txn->sessid, value_begin, value_len);
7108 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007109 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007110 }
7111 /* that's done for this cookie, check the next one on the same
7112 * line when next != hdr_end (only if is_cookie2).
7113 */
7114 }
7115 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007116 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007117 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007118
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007119 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007120 appsess *asession = NULL;
7121 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007122 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007123 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007124 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007125 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7126 Alert("Not enough Memory process_srv():asession:calloc().\n");
7127 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7128 return;
7129 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007130 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7131
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007132 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7133 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7134 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007135 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007136 return;
7137 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007138 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007139 asession->sessid[t->be->appsession_len] = 0;
7140
Willy Tarreau827aee92011-03-10 16:55:02 +01007141 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007142 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007143 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007144 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007145 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007146 return;
7147 }
7148 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01007149 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007150
7151 asession->request_count = 0;
7152 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7153 }
7154
7155 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7156 asession->request_count++;
7157 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007158}
7159
7160
Willy Tarreaua15645d2007-03-18 16:22:39 +01007161/*
7162 * Check if response is cacheable or not. Updates t->flags.
7163 */
7164void check_response_for_cacheability(struct session *t, struct buffer *rtr)
7165{
7166 struct http_txn *txn = &t->txn;
7167 char *p1, *p2;
7168
7169 char *cur_ptr, *cur_end, *cur_next;
7170 int cur_idx;
7171
Willy Tarreau5df51872007-11-25 16:20:08 +01007172 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007173 return;
7174
7175 /* Iterate through the headers.
7176 * we start with the start line.
7177 */
7178 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007179 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007180
7181 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7182 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007183 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007184
7185 cur_hdr = &txn->hdr_idx.v[cur_idx];
7186 cur_ptr = cur_next;
7187 cur_end = cur_ptr + cur_hdr->len;
7188 cur_next = cur_end + cur_hdr->cr + 1;
7189
7190 /* We have one full header between cur_ptr and cur_end, and the
7191 * next header starts at cur_next. We're only interested in
7192 * "Cookie:" headers.
7193 */
7194
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007195 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7196 if (val) {
7197 if ((cur_end - (cur_ptr + val) >= 8) &&
7198 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7199 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7200 return;
7201 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007202 }
7203
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007204 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7205 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206 continue;
7207
7208 /* OK, right now we know we have a cache-control header at cur_ptr */
7209
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007210 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007211
7212 if (p1 >= cur_end) /* no more info */
7213 continue;
7214
7215 /* p1 is at the beginning of the value */
7216 p2 = p1;
7217
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007218 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007219 p2++;
7220
7221 /* we have a complete value between p1 and p2 */
7222 if (p2 < cur_end && *p2 == '=') {
7223 /* we have something of the form no-cache="set-cookie" */
7224 if ((cur_end - p1 >= 21) &&
7225 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7226 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007227 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007228 continue;
7229 }
7230
7231 /* OK, so we know that either p2 points to the end of string or to a comma */
7232 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7233 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7234 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7235 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007236 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007237 return;
7238 }
7239
7240 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007241 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242 continue;
7243 }
7244 }
7245}
7246
7247
Willy Tarreau58f10d72006-12-04 02:26:12 +01007248/*
7249 * Try to retrieve a known appsession in the URI, then the associated server.
7250 * If the server is found, it's assigned to the session.
7251 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007252void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007253{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007254 char *end_params, *first_param, *cur_param, *next_param;
7255 char separator;
7256 int value_len;
7257
7258 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007259
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007260 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007261 (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 +01007262 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007263 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007264
Cyril Bontéb21570a2009-11-29 20:04:48 +01007265 first_param = NULL;
7266 switch (mode) {
7267 case PR_O2_AS_M_PP:
7268 first_param = memchr(begin, ';', len);
7269 break;
7270 case PR_O2_AS_M_QS:
7271 first_param = memchr(begin, '?', len);
7272 break;
7273 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007274
Cyril Bontéb21570a2009-11-29 20:04:48 +01007275 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007276 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007277 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007278
Cyril Bontéb21570a2009-11-29 20:04:48 +01007279 switch (mode) {
7280 case PR_O2_AS_M_PP:
7281 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7282 end_params = (char *) begin + len;
7283 }
7284 separator = ';';
7285 break;
7286 case PR_O2_AS_M_QS:
7287 end_params = (char *) begin + len;
7288 separator = '&';
7289 break;
7290 default:
7291 /* unknown mode, shouldn't happen */
7292 return;
7293 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007294
Cyril Bontéb21570a2009-11-29 20:04:48 +01007295 cur_param = next_param = end_params;
7296 while (cur_param > first_param) {
7297 cur_param--;
7298 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7299 /* let's see if this is the appsession parameter */
7300 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7301 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7302 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7303 /* Cool... it's the right one */
7304 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7305 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7306 if (value_len > 0) {
7307 manage_client_side_appsession(t, cur_param, value_len);
7308 }
7309 break;
7310 }
7311 next_param = cur_param;
7312 }
7313 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007314#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007315 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007316 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007317#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007318}
7319
Willy Tarreaub2513902006-12-17 14:52:38 +01007320/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007321 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007322 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007323 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007324 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007325 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007326 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007327 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007328 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007329int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007330{
7331 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007332 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007333
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007334 if (!uri_auth)
7335 return 0;
7336
Cyril Bonté70be45d2010-10-12 00:14:35 +02007337 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007338 return 0;
7339
Willy Tarreau295a8372011-03-10 11:25:07 +01007340 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007341
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007342 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007343 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007344 return 0;
7345
Willy Tarreau962c3f42010-01-10 00:15:35 +01007346 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007347
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007348 /* the URI is in h */
7349 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007350 return 0;
7351
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007352 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007353 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007354 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007355 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007356 break;
7357 }
7358 h++;
7359 }
7360
7361 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007362 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7363 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007364 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007365 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007366 break;
7367 }
7368 h++;
7369 }
7370 }
7371
Willy Tarreau962c3f42010-01-10 00:15:35 +01007372 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7373 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007374 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007375 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007376 break;
7377 }
7378 h++;
7379 }
7380
Cyril Bonté70be45d2010-10-12 00:14:35 +02007381 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7382 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7383 if (memcmp(h, ";st=", 4) == 0) {
7384 h += 4;
7385
7386 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007387 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007388 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007389 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007390 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007391 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007392 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007393 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007394 else
Willy Tarreau295a8372011-03-10 11:25:07 +01007395 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007396 break;
7397 }
7398 h++;
7399 }
7400
Willy Tarreau295a8372011-03-10 11:25:07 +01007401 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007402
Willy Tarreaub2513902006-12-17 14:52:38 +01007403 return 1;
7404}
7405
Willy Tarreau4076a152009-04-02 15:18:36 +02007406/*
7407 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007408 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7409 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007410 */
7411void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7412 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007413 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007414{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007415 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7416 int len1 = buf->size - msg->som;
7417 es->len = buf->r - (buf->data + msg->som) + buf->size;
7418 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7419 if (es->len > len1 && len1 < sizeof(es->buf))
7420 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7421 }
7422 else {
7423 es->len = buf->r - (buf->data + msg->som);
7424 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7425 }
7426
Willy Tarreau4076a152009-04-02 15:18:36 +02007427 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007428 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007429 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007430 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007431 else
7432 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7433
Willy Tarreau4076a152009-04-02 15:18:36 +02007434 es->when = date; // user-visible date
7435 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007436 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007437 es->oe = other_end;
Willy Tarreau957c0a52011-03-03 17:42:23 +01007438 es->src = s->req->prod->addr.c.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007439 es->state = state;
7440 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007441 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007442}
Willy Tarreaub2513902006-12-17 14:52:38 +01007443
Willy Tarreaubce70882009-09-07 11:51:47 +02007444/* return the IP address pointed to by occurrence <occ> of header <hname> in
7445 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
7446 * occurrence number corresponding to this value is returned. If <occ> is
7447 * strictly negative, the occurrence number before the end corresponding to
7448 * this value is returned. If <occ> is null, any value is returned, so it is
7449 * not recommended to use it that way. Negative occurrences are limited to
7450 * a small value because it is required to keep them in memory while scanning.
7451 * IP address 0.0.0.0 is returned if no match is found.
7452 */
7453unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
7454{
7455 struct hdr_ctx ctx;
7456 unsigned int hdr_hist[MAX_HDR_HISTORY];
7457 unsigned int hist_ptr;
7458 int found = 0;
7459
7460 ctx.idx = 0;
7461 if (occ >= 0) {
7462 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7463 occ--;
7464 if (occ <= 0) {
7465 found = 1;
7466 break;
7467 }
7468 }
7469 if (!found)
7470 return 0;
7471 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7472 }
7473
7474 /* negative occurrence, we scan all the list then walk back */
7475 if (-occ > MAX_HDR_HISTORY)
7476 return 0;
7477
7478 hist_ptr = 0;
7479 hdr_hist[hist_ptr] = 0;
7480 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7481 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7482 if (hist_ptr >= MAX_HDR_HISTORY)
7483 hist_ptr = 0;
7484 found++;
7485 }
7486 if (-occ > found)
7487 return 0;
7488 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7489 * find occurrence -occ, so we have to check [hist_ptr+occ].
7490 */
7491 hist_ptr += occ;
7492 if (hist_ptr >= MAX_HDR_HISTORY)
7493 hist_ptr -= MAX_HDR_HISTORY;
7494 return hdr_hist[hist_ptr];
7495}
7496
Willy Tarreaubaaee002006-06-26 02:48:02 +02007497/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007498 * Print a debug line with a header
7499 */
7500void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7501{
7502 int len, max;
7503 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007504 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007505 max = end - start;
7506 UBOUND(max, sizeof(trash) - len - 1);
7507 len += strlcpy2(trash + len, start, max + 1);
7508 trash[len++] = '\n';
7509 write(1, trash, len);
7510}
7511
Willy Tarreau0937bc42009-12-22 15:03:09 +01007512/*
7513 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7514 * the required fields are properly allocated and that we only need to (re)init
7515 * them. This should be used before processing any new request.
7516 */
7517void http_init_txn(struct session *s)
7518{
7519 struct http_txn *txn = &s->txn;
7520 struct proxy *fe = s->fe;
7521
7522 txn->flags = 0;
7523 txn->status = -1;
7524
Willy Tarreauf64d1412010-10-07 20:06:11 +02007525 txn->cookie_first_date = 0;
7526 txn->cookie_last_date = 0;
7527
Willy Tarreau0937bc42009-12-22 15:03:09 +01007528 txn->req.sol = txn->req.eol = NULL;
7529 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7530 txn->rsp.sol = txn->rsp.eol = NULL;
7531 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007532 txn->req.chunk_len = 0LL;
7533 txn->req.body_len = 0LL;
7534 txn->rsp.chunk_len = 0LL;
7535 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007536 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7537 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007538
7539 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007540
7541 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7542 if (fe->options2 & PR_O2_REQBUG_OK)
7543 txn->req.err_pos = -1; /* let buggy requests pass */
7544
Willy Tarreau46023632010-01-07 22:51:47 +01007545 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007546 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7547
Willy Tarreau46023632010-01-07 22:51:47 +01007548 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007549 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7550
7551 if (txn->hdr_idx.v)
7552 hdr_idx_init(&txn->hdr_idx);
7553}
7554
7555/* to be used at the end of a transaction */
7556void http_end_txn(struct session *s)
7557{
7558 struct http_txn *txn = &s->txn;
7559
7560 /* these ones will have been dynamically allocated */
7561 pool_free2(pool2_requri, txn->uri);
7562 pool_free2(pool2_capture, txn->cli_cookie);
7563 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007564 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007565
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007566 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007567 txn->uri = NULL;
7568 txn->srv_cookie = NULL;
7569 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007570
7571 if (txn->req.cap) {
7572 struct cap_hdr *h;
7573 for (h = s->fe->req_cap; h; h = h->next)
7574 pool_free2(h->pool, txn->req.cap[h->index]);
7575 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7576 }
7577
7578 if (txn->rsp.cap) {
7579 struct cap_hdr *h;
7580 for (h = s->fe->rsp_cap; h; h = h->next)
7581 pool_free2(h->pool, txn->rsp.cap[h->index]);
7582 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7583 }
7584
Willy Tarreau0937bc42009-12-22 15:03:09 +01007585}
7586
7587/* to be used at the end of a transaction to prepare a new one */
7588void http_reset_txn(struct session *s)
7589{
7590 http_end_txn(s);
7591 http_init_txn(s);
7592
7593 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007594 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007595 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007596 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007597 /* re-init store persistence */
7598 s->store_count = 0;
7599
Willy Tarreau0937bc42009-12-22 15:03:09 +01007600 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007601
7602 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7603
Willy Tarreau739cfba2010-01-25 23:11:14 +01007604 /* We must trim any excess data from the response buffer, because we
7605 * may have blocked an invalid response from a server that we don't
7606 * want to accidentely forward once we disable the analysers, nor do
7607 * we want those data to come along with next response. A typical
7608 * example of such data would be from a buggy server responding to
7609 * a HEAD with some data, or sending more than the advertised
7610 * content-length.
7611 */
7612 if (unlikely(s->rep->l > s->rep->send_max)) {
7613 s->rep->l = s->rep->send_max;
7614 s->rep->r = s->rep->w + s->rep->l;
7615 if (s->rep->r >= s->rep->data + s->rep->size)
7616 s->rep->r -= s->rep->size;
7617 }
7618
Willy Tarreau0937bc42009-12-22 15:03:09 +01007619 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007620 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007621
Willy Tarreaud04e8582010-05-31 12:31:35 +02007622 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007623 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007624
7625 s->req->rex = TICK_ETERNITY;
7626 s->req->wex = TICK_ETERNITY;
7627 s->req->analyse_exp = TICK_ETERNITY;
7628 s->rep->rex = TICK_ETERNITY;
7629 s->rep->wex = TICK_ETERNITY;
7630 s->rep->analyse_exp = TICK_ETERNITY;
7631}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007632
Willy Tarreauff011f22011-01-06 17:51:27 +01007633void free_http_req_rules(struct list *r) {
7634 struct http_req_rule *tr, *pr;
7635
7636 list_for_each_entry_safe(pr, tr, r, list) {
7637 LIST_DEL(&pr->list);
7638 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7639 free(pr->http_auth.realm);
7640
7641 free(pr);
7642 }
7643}
7644
7645struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7646{
7647 struct http_req_rule *rule;
7648 int cur_arg;
7649
7650 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7651 if (!rule) {
7652 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7653 return NULL;
7654 }
7655
7656 if (!*args[0]) {
7657 goto req_error_parsing;
7658 } else if (!strcmp(args[0], "allow")) {
7659 rule->action = HTTP_REQ_ACT_ALLOW;
7660 cur_arg = 1;
7661 } else if (!strcmp(args[0], "deny")) {
7662 rule->action = HTTP_REQ_ACT_DENY;
7663 cur_arg = 1;
7664 } else if (!strcmp(args[0], "auth")) {
7665 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7666 cur_arg = 1;
7667
7668 while(*args[cur_arg]) {
7669 if (!strcmp(args[cur_arg], "realm")) {
7670 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7671 cur_arg+=2;
7672 continue;
7673 } else
7674 break;
7675 }
7676 } else {
7677req_error_parsing:
7678 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7679 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7680 return NULL;
7681 }
7682
7683 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7684 struct acl_cond *cond;
7685
7686 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7687 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7688 file, linenum, args[0]);
7689 return NULL;
7690 }
7691 rule->cond = cond;
7692 }
7693 else if (*args[cur_arg]) {
7694 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7695 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7696 file, linenum, args[0], args[cur_arg]);
7697 return NULL;
7698 }
7699
7700 return rule;
7701}
7702
Willy Tarreau8797c062007-05-07 00:55:35 +02007703/************************************************************************/
7704/* The code below is dedicated to ACL parsing and matching */
7705/************************************************************************/
7706
7707
7708
7709
7710/* 1. Check on METHOD
7711 * We use the pre-parsed method if it is known, and store its number as an
7712 * integer. If it is unknown, we use the pointer and the length.
7713 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007714static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007715{
7716 int len, meth;
7717
Willy Tarreauae8b7962007-06-09 23:10:04 +02007718 len = strlen(*text);
7719 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007720
7721 pattern->val.i = meth;
7722 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007723 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007724 if (!pattern->ptr.str)
7725 return 0;
7726 pattern->len = len;
7727 }
7728 return 1;
7729}
7730
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007731static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007732acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7733 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007734{
7735 int meth;
7736 struct http_txn *txn = l7;
7737
Willy Tarreaub6866442008-07-14 23:54:42 +02007738 if (!txn)
7739 return 0;
7740
Willy Tarreau655dce92009-11-08 13:10:58 +01007741 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007742 return 0;
7743
Willy Tarreau8797c062007-05-07 00:55:35 +02007744 meth = txn->meth;
7745 test->i = meth;
7746 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007747 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7748 /* ensure the indexes are not affected */
7749 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007750 test->len = txn->req.sl.rq.m_l;
7751 test->ptr = txn->req.sol;
7752 }
7753 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7754 return 1;
7755}
7756
7757static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7758{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007759 int icase;
7760
Willy Tarreau8797c062007-05-07 00:55:35 +02007761 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02007762 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007763
7764 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02007765 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007766
7767 /* Other method, we must compare the strings */
7768 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02007769 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007770
7771 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
7772 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
7773 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007774 return ACL_PAT_FAIL;
7775 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007776}
7777
7778/* 2. Check on Request/Status Version
7779 * We simply compare strings here.
7780 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007781static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007782{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007783 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007784 if (!pattern->ptr.str)
7785 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007786 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007787 return 1;
7788}
7789
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007790static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007791acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7792 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007793{
7794 struct http_txn *txn = l7;
7795 char *ptr;
7796 int len;
7797
Willy Tarreaub6866442008-07-14 23:54:42 +02007798 if (!txn)
7799 return 0;
7800
Willy Tarreau655dce92009-11-08 13:10:58 +01007801 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007802 return 0;
7803
Willy Tarreau8797c062007-05-07 00:55:35 +02007804 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007805 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007806
7807 while ((len-- > 0) && (*ptr++ != '/'));
7808 if (len <= 0)
7809 return 0;
7810
7811 test->ptr = ptr;
7812 test->len = len;
7813
7814 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7815 return 1;
7816}
7817
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007818static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007819acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7820 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007821{
7822 struct http_txn *txn = l7;
7823 char *ptr;
7824 int len;
7825
Willy Tarreaub6866442008-07-14 23:54:42 +02007826 if (!txn)
7827 return 0;
7828
Willy Tarreau655dce92009-11-08 13:10:58 +01007829 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007830 return 0;
7831
Willy Tarreau8797c062007-05-07 00:55:35 +02007832 len = txn->rsp.sl.st.v_l;
7833 ptr = txn->rsp.sol;
7834
7835 while ((len-- > 0) && (*ptr++ != '/'));
7836 if (len <= 0)
7837 return 0;
7838
7839 test->ptr = ptr;
7840 test->len = len;
7841
7842 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7843 return 1;
7844}
7845
7846/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007847static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007848acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7849 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007850{
7851 struct http_txn *txn = l7;
7852 char *ptr;
7853 int len;
7854
Willy Tarreaub6866442008-07-14 23:54:42 +02007855 if (!txn)
7856 return 0;
7857
Willy Tarreau655dce92009-11-08 13:10:58 +01007858 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007859 return 0;
7860
Willy Tarreau8797c062007-05-07 00:55:35 +02007861 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007862 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007863
7864 test->i = __strl2ui(ptr, len);
7865 test->flags = ACL_TEST_F_VOL_1ST;
7866 return 1;
7867}
7868
7869/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007870static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007871acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7872 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007873{
7874 struct http_txn *txn = l7;
7875
Willy Tarreaub6866442008-07-14 23:54:42 +02007876 if (!txn)
7877 return 0;
7878
Willy Tarreau655dce92009-11-08 13:10:58 +01007879 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007880 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007881
Willy Tarreauc11416f2007-06-17 16:58:38 +02007882 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7883 /* ensure the indexes are not affected */
7884 return 0;
7885
Willy Tarreau8797c062007-05-07 00:55:35 +02007886 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007887 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007888
Willy Tarreauf3d25982007-05-08 22:45:09 +02007889 /* we do not need to set READ_ONLY because the data is in a buffer */
7890 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007891 return 1;
7892}
7893
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007894static int
7895acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7896 struct acl_expr *expr, struct acl_test *test)
7897{
7898 struct http_txn *txn = l7;
7899
Willy Tarreaub6866442008-07-14 23:54:42 +02007900 if (!txn)
7901 return 0;
7902
Willy Tarreau655dce92009-11-08 13:10:58 +01007903 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007904 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007905
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007906 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7907 /* ensure the indexes are not affected */
7908 return 0;
7909
7910 /* Parse HTTP request */
Willy Tarreau957c0a52011-03-03 17:42:23 +01007911 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to);
7912 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007913 test->i = AF_INET;
7914
7915 /*
7916 * If we are parsing url in frontend space, we prepare backend stage
7917 * to not parse again the same url ! optimization lazyness...
7918 */
7919 if (px->options & PR_O_HTTP_PROXY)
7920 l4->flags |= SN_ADDR_SET;
7921
7922 test->flags = ACL_TEST_F_READ_ONLY;
7923 return 1;
7924}
7925
7926static int
7927acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7928 struct acl_expr *expr, struct acl_test *test)
7929{
7930 struct http_txn *txn = l7;
7931
Willy Tarreaub6866442008-07-14 23:54:42 +02007932 if (!txn)
7933 return 0;
7934
Willy Tarreau655dce92009-11-08 13:10:58 +01007935 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007936 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007937
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007938 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7939 /* ensure the indexes are not affected */
7940 return 0;
7941
7942 /* Same optimization as url_ip */
Willy Tarreau957c0a52011-03-03 17:42:23 +01007943 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to);
7944 test->i = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007945
7946 if (px->options & PR_O_HTTP_PROXY)
7947 l4->flags |= SN_ADDR_SET;
7948
7949 test->flags = ACL_TEST_F_READ_ONLY;
7950 return 1;
7951}
7952
Willy Tarreauc11416f2007-06-17 16:58:38 +02007953/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7954 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7955 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007956static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007957acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007958 struct acl_expr *expr, struct acl_test *test)
7959{
7960 struct http_txn *txn = l7;
7961 struct hdr_idx *idx = &txn->hdr_idx;
7962 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007963
Willy Tarreaub6866442008-07-14 23:54:42 +02007964 if (!txn)
7965 return 0;
7966
Willy Tarreau33a7e692007-06-10 19:45:56 +02007967 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7968 /* search for header from the beginning */
7969 ctx->idx = 0;
7970
Willy Tarreau33a7e692007-06-10 19:45:56 +02007971 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7972 test->flags |= ACL_TEST_F_FETCH_MORE;
7973 test->flags |= ACL_TEST_F_VOL_HDR;
7974 test->len = ctx->vlen;
7975 test->ptr = (char *)ctx->line + ctx->val;
7976 return 1;
7977 }
7978
7979 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7980 test->flags |= ACL_TEST_F_VOL_HDR;
7981 return 0;
7982}
7983
Willy Tarreau33a7e692007-06-10 19:45:56 +02007984static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007985acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7986 struct acl_expr *expr, struct acl_test *test)
7987{
7988 struct http_txn *txn = l7;
7989
Willy Tarreaub6866442008-07-14 23:54:42 +02007990 if (!txn)
7991 return 0;
7992
Willy Tarreau655dce92009-11-08 13:10:58 +01007993 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007994 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007995
Willy Tarreauc11416f2007-06-17 16:58:38 +02007996 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7997 /* ensure the indexes are not affected */
7998 return 0;
7999
8000 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
8001}
8002
8003static int
8004acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
8005 struct acl_expr *expr, struct acl_test *test)
8006{
8007 struct http_txn *txn = l7;
8008
Willy Tarreaub6866442008-07-14 23:54:42 +02008009 if (!txn)
8010 return 0;
8011
Willy Tarreau655dce92009-11-08 13:10:58 +01008012 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008013 return 0;
8014
8015 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
8016}
8017
8018/* 6. Check on HTTP header count. The number of occurrences is returned.
8019 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8020 */
8021static int
8022acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008023 struct acl_expr *expr, struct acl_test *test)
8024{
8025 struct http_txn *txn = l7;
8026 struct hdr_idx *idx = &txn->hdr_idx;
8027 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008028 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008029
Willy Tarreaub6866442008-07-14 23:54:42 +02008030 if (!txn)
8031 return 0;
8032
Willy Tarreau33a7e692007-06-10 19:45:56 +02008033 ctx.idx = 0;
8034 cnt = 0;
8035 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
8036 cnt++;
8037
8038 test->i = cnt;
8039 test->flags = ACL_TEST_F_VOL_HDR;
8040 return 1;
8041}
8042
Willy Tarreauc11416f2007-06-17 16:58:38 +02008043static int
8044acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8045 struct acl_expr *expr, struct acl_test *test)
8046{
8047 struct http_txn *txn = l7;
8048
Willy Tarreaub6866442008-07-14 23:54:42 +02008049 if (!txn)
8050 return 0;
8051
Willy Tarreau655dce92009-11-08 13:10:58 +01008052 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008053 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008054
Willy Tarreauc11416f2007-06-17 16:58:38 +02008055 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8056 /* ensure the indexes are not affected */
8057 return 0;
8058
8059 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
8060}
8061
8062static int
8063acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8064 struct acl_expr *expr, struct acl_test *test)
8065{
8066 struct http_txn *txn = l7;
8067
Willy Tarreaub6866442008-07-14 23:54:42 +02008068 if (!txn)
8069 return 0;
8070
Willy Tarreau655dce92009-11-08 13:10:58 +01008071 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008072 return 0;
8073
8074 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
8075}
8076
Willy Tarreau33a7e692007-06-10 19:45:56 +02008077/* 7. Check on HTTP header's integer value. The integer value is returned.
8078 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008079 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008080 */
8081static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02008082acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008083 struct acl_expr *expr, struct acl_test *test)
8084{
8085 struct http_txn *txn = l7;
8086 struct hdr_idx *idx = &txn->hdr_idx;
8087 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008088
Willy Tarreaub6866442008-07-14 23:54:42 +02008089 if (!txn)
8090 return 0;
8091
Willy Tarreau33a7e692007-06-10 19:45:56 +02008092 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8093 /* search for header from the beginning */
8094 ctx->idx = 0;
8095
Willy Tarreau33a7e692007-06-10 19:45:56 +02008096 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8097 test->flags |= ACL_TEST_F_FETCH_MORE;
8098 test->flags |= ACL_TEST_F_VOL_HDR;
8099 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
8100 return 1;
8101 }
8102
8103 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8104 test->flags |= ACL_TEST_F_VOL_HDR;
8105 return 0;
8106}
8107
Willy Tarreauc11416f2007-06-17 16:58:38 +02008108static int
8109acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8110 struct acl_expr *expr, struct acl_test *test)
8111{
8112 struct http_txn *txn = l7;
8113
Willy Tarreaub6866442008-07-14 23:54:42 +02008114 if (!txn)
8115 return 0;
8116
Willy Tarreau655dce92009-11-08 13:10:58 +01008117 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008118 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008119
Willy Tarreauc11416f2007-06-17 16:58:38 +02008120 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8121 /* ensure the indexes are not affected */
8122 return 0;
8123
8124 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
8125}
8126
8127static int
8128acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8129 struct acl_expr *expr, struct acl_test *test)
8130{
8131 struct http_txn *txn = l7;
8132
Willy Tarreaub6866442008-07-14 23:54:42 +02008133 if (!txn)
8134 return 0;
8135
Willy Tarreau655dce92009-11-08 13:10:58 +01008136 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008137 return 0;
8138
8139 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
8140}
8141
Willy Tarreau106f9792009-09-19 07:54:16 +02008142/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
8143 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8144 */
8145static int
8146acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
8147 struct acl_expr *expr, struct acl_test *test)
8148{
8149 struct http_txn *txn = l7;
8150 struct hdr_idx *idx = &txn->hdr_idx;
8151 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
8152
8153 if (!txn)
8154 return 0;
8155
8156 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8157 /* search for header from the beginning */
8158 ctx->idx = 0;
8159
8160 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8161 test->flags |= ACL_TEST_F_FETCH_MORE;
8162 test->flags |= ACL_TEST_F_VOL_HDR;
8163 /* Same optimization as url_ip */
David du Colombier6f5ccb12011-03-10 22:26:24 +01008164 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));
8165 url2ipv4((char *)ctx->line + ctx->val, &((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr);
8166 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr;
Willy Tarreau106f9792009-09-19 07:54:16 +02008167 test->i = AF_INET;
8168 return 1;
8169 }
8170
8171 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8172 test->flags |= ACL_TEST_F_VOL_HDR;
8173 return 0;
8174}
8175
8176static int
8177acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8178 struct acl_expr *expr, struct acl_test *test)
8179{
8180 struct http_txn *txn = l7;
8181
8182 if (!txn)
8183 return 0;
8184
Willy Tarreau655dce92009-11-08 13:10:58 +01008185 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008186 return 0;
8187
8188 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8189 /* ensure the indexes are not affected */
8190 return 0;
8191
8192 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8193}
8194
8195static int
8196acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8197 struct acl_expr *expr, struct acl_test *test)
8198{
8199 struct http_txn *txn = l7;
8200
8201 if (!txn)
8202 return 0;
8203
Willy Tarreau655dce92009-11-08 13:10:58 +01008204 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008205 return 0;
8206
8207 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8208}
8209
Willy Tarreau737b0c12007-06-10 21:28:46 +02008210/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8211 * the first '/' after the possible hostname, and ends before the possible '?'.
8212 */
8213static int
8214acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8215 struct acl_expr *expr, struct acl_test *test)
8216{
8217 struct http_txn *txn = l7;
8218 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008219
Willy Tarreaub6866442008-07-14 23:54:42 +02008220 if (!txn)
8221 return 0;
8222
Willy Tarreau655dce92009-11-08 13:10:58 +01008223 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008224 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008225
Willy Tarreauc11416f2007-06-17 16:58:38 +02008226 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8227 /* ensure the indexes are not affected */
8228 return 0;
8229
Willy Tarreau962c3f42010-01-10 00:15:35 +01008230 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008231 ptr = http_get_path(txn);
8232 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008233 return 0;
8234
8235 /* OK, we got the '/' ! */
8236 test->ptr = ptr;
8237
8238 while (ptr < end && *ptr != '?')
8239 ptr++;
8240
8241 test->len = ptr - test->ptr;
8242
8243 /* we do not need to set READ_ONLY because the data is in a buffer */
8244 test->flags = ACL_TEST_F_VOL_1ST;
8245 return 1;
8246}
8247
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008248static int
8249acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8250 struct acl_expr *expr, struct acl_test *test)
8251{
8252 struct buffer *req = s->req;
8253 struct http_txn *txn = &s->txn;
8254 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008255
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008256 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8257 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8258 */
8259
8260 if (!s || !req)
8261 return 0;
8262
Willy Tarreau655dce92009-11-08 13:10:58 +01008263 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008264 /* Already decoded as OK */
8265 test->flags |= ACL_TEST_F_SET_RES_PASS;
8266 return 1;
8267 }
8268
8269 /* Try to decode HTTP request */
8270 if (likely(req->lr < req->r))
8271 http_msg_analyzer(req, msg, &txn->hdr_idx);
8272
Willy Tarreau655dce92009-11-08 13:10:58 +01008273 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008274 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8275 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8276 return 1;
8277 }
8278 /* wait for final state */
8279 test->flags |= ACL_TEST_F_MAY_CHANGE;
8280 return 0;
8281 }
8282
8283 /* OK we got a valid HTTP request. We have some minor preparation to
8284 * perform so that further checks can rely on HTTP tests.
8285 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008286 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008287 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8288 s->flags |= SN_REDIRECTABLE;
8289
8290 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8291 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8292 return 1;
8293 }
8294
8295 test->flags |= ACL_TEST_F_SET_RES_PASS;
8296 return 1;
8297}
8298
Willy Tarreau7f18e522010-10-22 20:04:13 +02008299/* return a valid test if the current request is the first one on the connection */
8300static int
8301acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8302 struct acl_expr *expr, struct acl_test *test)
8303{
8304 if (!s)
8305 return 0;
8306
8307 if (s->txn.flags & TX_NOT_FIRST)
8308 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8309 else
8310 test->flags |= ACL_TEST_F_SET_RES_PASS;
8311
8312 return 1;
8313}
8314
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008315static int
8316acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8317 struct acl_expr *expr, struct acl_test *test)
8318{
8319
8320 if (!s)
8321 return 0;
8322
8323 if (!get_http_auth(s))
8324 return 0;
8325
8326 test->ctx.a[0] = expr->arg.ul;
8327 test->ctx.a[1] = s->txn.auth.user;
8328 test->ctx.a[2] = s->txn.auth.pass;
8329
8330 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8331
8332 return 1;
8333}
Willy Tarreau8797c062007-05-07 00:55:35 +02008334
8335/************************************************************************/
8336/* All supported keywords must be declared here. */
8337/************************************************************************/
8338
8339/* Note: must not be declared <const> as its list will be overwritten */
8340static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008341 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8342
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008343 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008344 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8345 { "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 +02008346 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008347
Willy Tarreauc4262962010-05-10 23:42:40 +02008348 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008349 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8350 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8351 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8352 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8353 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8354 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008355 { "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 +02008356 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008357
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008358 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008359 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008360 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8361 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8362 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8363 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8364 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8365 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8366 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
8367 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008368 { "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 +02008369
Willy Tarreauc4262962010-05-10 23:42:40 +02008370 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008371 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8372 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8373 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8374 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8375 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8376 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8377 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
8378 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008379 { "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 +02008380
Willy Tarreauc4262962010-05-10 23:42:40 +02008381 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008382 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8383 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8384 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8385 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8386 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8387 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008388
Willy Tarreauf3d25982007-05-08 22:45:09 +02008389#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008390 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8391 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8392 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8393 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8394 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8395 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8396 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8397
Willy Tarreau8797c062007-05-07 00:55:35 +02008398 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8399 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8400 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8401 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8402 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8403 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8404 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8405 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008406#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008407
Willy Tarreau7f18e522010-10-22 20:04:13 +02008408 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8409 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8410 { "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 +02008411 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008412}};
8413
Willy Tarreau4a568972010-05-12 08:08:50 +02008414/************************************************************************/
8415/* The code below is dedicated to pattern fetching and matching */
8416/************************************************************************/
8417
8418/* extract the IP address from the last occurrence of specified header. Note
8419 * that we should normally first extract the string then convert it to IP,
8420 * but right now we have all the functions to do this seemlessly, and we will
8421 * be able to change that later without touching the configuration.
8422 */
8423static int
8424pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02008425 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008426{
8427 struct http_txn *txn = l7;
8428
Emeric Brun485479d2010-09-23 18:02:19 +02008429 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 +02008430 return data->ip.s_addr != 0;
8431}
8432
David Cournapeau16023ee2010-12-23 20:55:41 +09008433/*
8434 * Given a path string and its length, find the position of beginning of the
8435 * query string. Returns NULL if no query string is found in the path.
8436 *
8437 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8438 *
8439 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8440 */
8441static inline char *find_query_string(char *path, size_t path_l)
8442{
8443 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008444
David Cournapeau16023ee2010-12-23 20:55:41 +09008445 p = memchr(path, '?', path_l);
8446 return p ? p + 1 : NULL;
8447}
8448
8449static inline int is_param_delimiter(char c)
8450{
8451 return c == '&' || c == ';';
8452}
8453
8454/*
8455 * Given a url parameter, find the starting position of the first occurence,
8456 * or NULL if the parameter is not found.
8457 *
8458 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8459 * the function will return query_string+8.
8460 */
8461static char*
8462find_url_param_pos(char* query_string, size_t query_string_l,
8463 char* url_param_name, size_t url_param_name_l)
8464{
8465 char *pos, *last;
8466
8467 pos = query_string;
8468 last = query_string + query_string_l - url_param_name_l - 1;
8469
8470 while (pos <= last) {
8471 if (pos[url_param_name_l] == '=') {
8472 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8473 return pos;
8474 pos += url_param_name_l + 1;
8475 }
8476 while (pos <= last && !is_param_delimiter(*pos))
8477 pos++;
8478 pos++;
8479 }
8480 return NULL;
8481}
8482
8483/*
8484 * Given a url parameter name, returns its value and size into *value and
8485 * *value_l respectively, and returns non-zero. If the parameter is not found,
8486 * zero is returned and value/value_l are not touched.
8487 */
8488static int
8489find_url_param_value(char* path, size_t path_l,
8490 char* url_param_name, size_t url_param_name_l,
8491 char** value, size_t* value_l)
8492{
8493 char *query_string, *qs_end;
8494 char *arg_start;
8495 char *value_start, *value_end;
8496
8497 query_string = find_query_string(path, path_l);
8498 if (!query_string)
8499 return 0;
8500
8501 qs_end = path + path_l;
8502 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8503 url_param_name, url_param_name_l);
8504 if (!arg_start)
8505 return 0;
8506
8507 value_start = arg_start + url_param_name_l + 1;
8508 value_end = value_start;
8509
8510 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8511 value_end++;
8512
8513 *value = value_start;
8514 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008515 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008516}
8517
8518static int
8519pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8520 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8521{
8522 struct http_txn *txn = l7;
8523 struct http_msg *msg = &txn->req;
8524 char *url_param_value;
8525 size_t url_param_value_l;
8526
8527 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8528 arg_p->data.str.str, arg_p->data.str.len,
8529 &url_param_value, &url_param_value_l))
8530 return 0;
8531
8532 data->str.str = url_param_value;
8533 data->str.len = url_param_value_l;
8534 return 1;
8535}
8536
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008537/* Try to find the last occurrence of a cookie name in a cookie header value.
8538 * The pointer and size of the last occurrence of the cookie value is returned
8539 * into *value and *value_l, and the function returns non-zero if the value was
8540 * found. Otherwise if the cookie was not found, zero is returned and neither
8541 * value nor value_l are touched. The input hdr string should begin at the
8542 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8543 * value may represent a list of values (cookie headers).
8544 */
8545static int
8546extract_cookie_value(char *hdr, size_t hdr_l,
8547 char *cookie_name, size_t cookie_name_l, int list,
8548 char **value, size_t *value_l)
8549{
8550 int found = 0;
8551 char *equal, *att_end, *att_beg, *hdr_end, *val_beg, *val_end;
8552 char *next;
8553
8554 /* Note that multiple cookies may be delimited with semi-colons, so we
8555 * also have to loop on this.
8556 */
8557
8558 /* we search at least a cookie name followed by an equal, and more
8559 * generally something like this :
8560 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8561 */
8562 if (hdr_l < cookie_name_l + 1)
8563 return 0;
8564
8565 hdr_end = hdr + hdr_l;
8566
8567 for (att_beg = hdr; att_beg < hdr_end; att_beg = next + 1) {
8568 /* Iterate through all cookies on this line */
8569
8570 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8571 att_beg++;
8572
8573 /* find att_end : this is the first character after the last non
8574 * space before the equal. It may be equal to hdr_end.
8575 */
8576 equal = att_end = att_beg;
8577
8578 while (equal < hdr_end) {
8579 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8580 break;
8581 if (http_is_spht[(unsigned char)*equal++])
8582 continue;
8583 att_end = equal;
8584 }
8585
8586 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8587 * is between <att_beg> and <equal>, both may be identical.
8588 */
8589
8590 /* look for end of cookie if there is an equal sign */
8591 if (equal < hdr_end && *equal == '=') {
8592 /* look for the beginning of the value */
8593 val_beg = equal + 1;
8594 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8595 val_beg++;
8596
8597 /* find the end of the value, respecting quotes */
8598 next = find_cookie_value_end(val_beg, hdr_end);
8599
8600 /* make val_end point to the first white space or delimitor after the value */
8601 val_end = next;
8602 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8603 val_end--;
8604 } else {
8605 val_beg = val_end = next = equal;
8606 }
8607
8608 /* We have nothing to do with attributes beginning with '$'. However,
8609 * they will automatically be removed if a header before them is removed,
8610 * since they're supposed to be linked together.
8611 */
8612 if (*att_beg == '$')
8613 continue;
8614
8615 /* Ignore cookies with no equal sign */
8616 if (equal == next)
8617 continue;
8618
8619 /* Now we have the cookie name between att_beg and att_end, and
8620 * its value between val_beg and val_end.
8621 */
8622
8623 if (att_end - att_beg == cookie_name_l &&
8624 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8625 found = 1;
8626 *value = val_beg;
8627 *value_l = val_end - val_beg;
8628 /* right now we want to catch the last occurrence
8629 * of the cookie, so let's go on searching.
8630 */
8631 }
8632
8633 /* Set-Cookie headers only have the name in the first attr=value part */
8634 if (!list)
8635 break;
8636 }
8637
8638 return found;
8639}
8640
8641/* Try to find in request or response message is in <msg> and whose transaction
8642 * is in <txn> the last occurrence of a cookie name in all cookie header values
8643 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8644 * pointer and size of the last occurrence of the cookie value is returned into
8645 * <value> and <value_l>, and the function returns non-zero if the value was
8646 * found. Otherwise if the cookie was not found, zero is returned and neither
8647 * value nor value_l are touched. The input hdr string should begin at the
8648 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8649 * value may represent a list of values (cookie headers).
8650 */
8651
8652static int
8653find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8654 const char *hdr_name, int hdr_name_len,
8655 char *cookie_name, size_t cookie_name_l, int list,
8656 char **value, size_t *value_l)
8657{
8658 struct hdr_ctx ctx;
8659 int found = 0;
8660
8661 ctx.idx = 0;
8662 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
8663 if (ctx.vlen < cookie_name_l + 1)
8664 continue;
8665
8666 found |= extract_cookie_value(ctx.line + ctx.val, ctx.vlen,
8667 cookie_name, cookie_name_l, 1,
8668 value, value_l);
8669 }
8670 return found;
8671}
8672
8673static int
8674pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8675 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8676{
8677 struct http_txn *txn = l7;
8678 struct http_msg *msg = &txn->req;
8679 char *cookie_value;
8680 size_t cookie_value_l;
8681 int found = 0;
8682
8683 found = find_cookie_value(msg, txn, "Cookie", 6,
8684 arg_p->data.str.str, arg_p->data.str.len, 1,
8685 &cookie_value, &cookie_value_l);
8686 if (found) {
8687 data->str.str = cookie_value;
8688 data->str.len = cookie_value_l;
8689 }
8690
8691 return found;
8692}
8693
8694
8695static int
8696pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8697 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8698{
8699 struct http_txn *txn = l7;
8700 struct http_msg *msg = &txn->rsp;
8701 char *cookie_value;
8702 size_t cookie_value_l;
8703 int found = 0;
8704
8705 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8706 arg_p->data.str.str, arg_p->data.str.len, 1,
8707 &cookie_value, &cookie_value_l);
8708 if (found) {
8709 data->str.str = cookie_value;
8710 data->str.len = cookie_value_l;
8711 }
8712
8713 return found;
8714}
8715
Emeric Brun485479d2010-09-23 18:02:19 +02008716
Willy Tarreau4a568972010-05-12 08:08:50 +02008717/************************************************************************/
8718/* All supported keywords must be declared here. */
8719/************************************************************************/
8720/* Note: must not be declared <const> as its list will be overwritten */
8721static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Emeric Brun485479d2010-09-23 18:02:19 +02008722 { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008723 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008724 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8725 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008726 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008727}};
8728
Willy Tarreau8797c062007-05-07 00:55:35 +02008729
8730__attribute__((constructor))
8731static void __http_protocol_init(void)
8732{
8733 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008734 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008735}
8736
8737
Willy Tarreau58f10d72006-12-04 02:26:12 +01008738/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008739 * Local variables:
8740 * c-indent-level: 8
8741 * c-basic-offset: 8
8742 * End:
8743 */