blob: 35c2f9db013cec332419d9c01c5fb90bc52ecde9 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010027#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/compat.h>
29#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010030#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreau8797c062007-05-07 00:55:35 +020042#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010043#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/backend.h>
45#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010046#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020049#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010051#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020052#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020053#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010055#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010059#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020060#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/task.h>
62
Willy Tarreau522d6c02009-12-06 18:49:18 +010063const char HTTP_100[] =
64 "HTTP/1.1 100 Continue\r\n\r\n";
65
66const struct chunk http_100_chunk = {
67 .str = (char *)&HTTP_100,
68 .len = sizeof(HTTP_100)-1
69};
70
Willy Tarreaua9679ac2010-01-03 17:32:57 +010071/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020072const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010073 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020074 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010075 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020076 "Location: "; /* not terminated since it will be concatenated with the URL */
77
Willy Tarreau0f772532006-12-23 20:51:41 +010078const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010080 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010082 "Location: "; /* not terminated since it will be concatenated with the URL */
83
84/* same as 302 except that the browser MUST retry with the GET method */
85const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010089 "Location: "; /* not terminated since it will be concatenated with the URL */
90
Willy Tarreaubaaee002006-06-26 02:48:02 +020091/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
92const char *HTTP_401_fmt =
93 "HTTP/1.0 401 Unauthorized\r\n"
94 "Cache-Control: no-cache\r\n"
95 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020096 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020097 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
98 "\r\n"
99 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
100
Willy Tarreau844a7e72010-01-31 21:46:18 +0100101const char *HTTP_407_fmt =
102 "HTTP/1.0 407 Unauthorized\r\n"
103 "Cache-Control: no-cache\r\n"
104 "Connection: close\r\n"
105 "Content-Type: text/html\r\n"
106 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
107 "\r\n"
108 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
109
Willy Tarreau0f772532006-12-23 20:51:41 +0100110
111const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200112 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100113 [HTTP_ERR_400] = 400,
114 [HTTP_ERR_403] = 403,
115 [HTTP_ERR_408] = 408,
116 [HTTP_ERR_500] = 500,
117 [HTTP_ERR_502] = 502,
118 [HTTP_ERR_503] = 503,
119 [HTTP_ERR_504] = 504,
120};
121
Willy Tarreau80587432006-12-24 17:47:20 +0100122static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200123 [HTTP_ERR_200] =
124 "HTTP/1.0 200 OK\r\n"
125 "Cache-Control: no-cache\r\n"
126 "Connection: close\r\n"
127 "Content-Type: text/html\r\n"
128 "\r\n"
129 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
130
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100132 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 "Cache-Control: no-cache\r\n"
134 "Connection: close\r\n"
135 "Content-Type: text/html\r\n"
136 "\r\n"
137 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
138
139 [HTTP_ERR_403] =
140 "HTTP/1.0 403 Forbidden\r\n"
141 "Cache-Control: no-cache\r\n"
142 "Connection: close\r\n"
143 "Content-Type: text/html\r\n"
144 "\r\n"
145 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
146
147 [HTTP_ERR_408] =
148 "HTTP/1.0 408 Request Time-out\r\n"
149 "Cache-Control: no-cache\r\n"
150 "Connection: close\r\n"
151 "Content-Type: text/html\r\n"
152 "\r\n"
153 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
154
155 [HTTP_ERR_500] =
156 "HTTP/1.0 500 Server Error\r\n"
157 "Cache-Control: no-cache\r\n"
158 "Connection: close\r\n"
159 "Content-Type: text/html\r\n"
160 "\r\n"
161 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
162
163 [HTTP_ERR_502] =
164 "HTTP/1.0 502 Bad Gateway\r\n"
165 "Cache-Control: no-cache\r\n"
166 "Connection: close\r\n"
167 "Content-Type: text/html\r\n"
168 "\r\n"
169 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
170
171 [HTTP_ERR_503] =
172 "HTTP/1.0 503 Service Unavailable\r\n"
173 "Cache-Control: no-cache\r\n"
174 "Connection: close\r\n"
175 "Content-Type: text/html\r\n"
176 "\r\n"
177 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
178
179 [HTTP_ERR_504] =
180 "HTTP/1.0 504 Gateway Time-out\r\n"
181 "Cache-Control: no-cache\r\n"
182 "Connection: close\r\n"
183 "Content-Type: text/html\r\n"
184 "\r\n"
185 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
186
187};
188
Willy Tarreau80587432006-12-24 17:47:20 +0100189/* We must put the messages here since GCC cannot initialize consts depending
190 * on strlen().
191 */
192struct chunk http_err_chunks[HTTP_ERR_SIZE];
193
Willy Tarreau42250582007-04-01 01:30:43 +0200194#define FD_SETS_ARE_BITFIELDS
195#ifdef FD_SETS_ARE_BITFIELDS
196/*
197 * This map is used with all the FD_* macros to check whether a particular bit
198 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
199 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
200 * byte should be encoded. Be careful to always pass bytes from 0 to 255
201 * exclusively to the macros.
202 */
203fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
204fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
205
206#else
207#error "Check if your OS uses bitfields for fd_sets"
208#endif
209
Willy Tarreau80587432006-12-24 17:47:20 +0100210void init_proto_http()
211{
Willy Tarreau42250582007-04-01 01:30:43 +0200212 int i;
213 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100214 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200215
Willy Tarreau80587432006-12-24 17:47:20 +0100216 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
217 if (!http_err_msgs[msg]) {
218 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
219 abort();
220 }
221
222 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
223 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
224 }
Willy Tarreau42250582007-04-01 01:30:43 +0200225
226 /* initialize the log header encoding map : '{|}"#' should be encoded with
227 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
228 * URL encoding only requires '"', '#' to be encoded as well as non-
229 * printable characters above.
230 */
231 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
232 memset(url_encode_map, 0, sizeof(url_encode_map));
233 for (i = 0; i < 32; i++) {
234 FD_SET(i, hdr_encode_map);
235 FD_SET(i, url_encode_map);
236 }
237 for (i = 127; i < 256; i++) {
238 FD_SET(i, hdr_encode_map);
239 FD_SET(i, url_encode_map);
240 }
241
242 tmp = "\"#{|}";
243 while (*tmp) {
244 FD_SET(*tmp, hdr_encode_map);
245 tmp++;
246 }
247
248 tmp = "\"#";
249 while (*tmp) {
250 FD_SET(*tmp, url_encode_map);
251 tmp++;
252 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200253
254 /* memory allocations */
255 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200256 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100257}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200258
Willy Tarreau53b6c742006-12-17 13:37:46 +0100259/*
260 * We have 26 list of methods (1 per first letter), each of which can have
261 * up to 3 entries (2 valid, 1 null).
262 */
263struct http_method_desc {
264 http_meth_t meth;
265 int len;
266 const char text[8];
267};
268
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100269const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100270 ['C' - 'A'] = {
271 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
272 },
273 ['D' - 'A'] = {
274 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
275 },
276 ['G' - 'A'] = {
277 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
278 },
279 ['H' - 'A'] = {
280 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
281 },
282 ['P' - 'A'] = {
283 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
284 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
285 },
286 ['T' - 'A'] = {
287 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
288 },
289 /* rest is empty like this :
290 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
291 */
292};
293
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100294/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200295 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100296 * RFC2616 for those chars.
297 */
298
299const char http_is_spht[256] = {
300 [' '] = 1, ['\t'] = 1,
301};
302
303const char http_is_crlf[256] = {
304 ['\r'] = 1, ['\n'] = 1,
305};
306
307const char http_is_lws[256] = {
308 [' '] = 1, ['\t'] = 1,
309 ['\r'] = 1, ['\n'] = 1,
310};
311
312const char http_is_sep[256] = {
313 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
314 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
315 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
316 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
317 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
318};
319
320const char http_is_ctl[256] = {
321 [0 ... 31] = 1,
322 [127] = 1,
323};
324
325/*
326 * A token is any ASCII char that is neither a separator nor a CTL char.
327 * Do not overwrite values in assignment since gcc-2.95 will not handle
328 * them correctly. Instead, define every non-CTL char's status.
329 */
330const char http_is_token[256] = {
331 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
332 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
333 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
334 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
335 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
336 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
337 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
338 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
339 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
340 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
341 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
342 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
343 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
344 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
345 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
346 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
347 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
348 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
349 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
350 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
351 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
352 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
353 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
354 ['|'] = 1, ['}'] = 0, ['~'] = 1,
355};
356
357
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100358/*
359 * An http ver_token is any ASCII which can be found in an HTTP version,
360 * which includes 'H', 'T', 'P', '/', '.' and any digit.
361 */
362const char http_is_ver_token[256] = {
363 ['.'] = 1, ['/'] = 1,
364 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
365 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
366 ['H'] = 1, ['P'] = 1, ['T'] = 1,
367};
368
369
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100370/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100371 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
372 */
373#if defined(DEBUG_FSM)
374static void http_silent_debug(int line, struct session *s)
375{
376 int size = 0;
377 size += snprintf(trash + size, sizeof(trash) - size,
378 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n",
379 line,
380 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
381 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags);
382 write(-1, trash, size);
383 size = 0;
384 size += snprintf(trash + size, sizeof(trash) - size,
385 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n",
386 line,
387 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
388 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward);
389
390 write(-1, trash, size);
391}
392#else
393#define http_silent_debug(l,s) do { } while (0)
394#endif
395
396/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100397 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
398 * CRLF. Text length is measured first, so it cannot be NULL.
399 * The header is also automatically added to the index <hdr_idx>, and the end
400 * of headers is automatically adjusted. The number of bytes added is returned
401 * on success, otherwise <0 is returned indicating an error.
402 */
403int http_header_add_tail(struct buffer *b, struct http_msg *msg,
404 struct hdr_idx *hdr_idx, const char *text)
405{
406 int bytes, len;
407
408 len = strlen(text);
409 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
410 if (!bytes)
411 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100412 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100413 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
414}
415
416/*
417 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
418 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
419 * the buffer is only opened and the space reserved, but nothing is copied.
420 * The header is also automatically added to the index <hdr_idx>, and the end
421 * of headers is automatically adjusted. The number of bytes added is returned
422 * on success, otherwise <0 is returned indicating an error.
423 */
424int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
425 struct hdr_idx *hdr_idx, const char *text, int len)
426{
427 int bytes;
428
429 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
430 if (!bytes)
431 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100432 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100433 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
434}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200435
436/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100437 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
438 * If so, returns the position of the first non-space character relative to
439 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
440 * to return a pointer to the place after the first space. Returns 0 if the
441 * header name does not match. Checks are case-insensitive.
442 */
443int http_header_match2(const char *hdr, const char *end,
444 const char *name, int len)
445{
446 const char *val;
447
448 if (hdr + len >= end)
449 return 0;
450 if (hdr[len] != ':')
451 return 0;
452 if (strncasecmp(hdr, name, len) != 0)
453 return 0;
454 val = hdr + len + 1;
455 while (val < end && HTTP_IS_SPHT(*val))
456 val++;
457 if ((val >= end) && (len + 2 <= end - hdr))
458 return len + 2; /* we may replace starting from second space */
459 return val - hdr;
460}
461
Willy Tarreau68085d82010-01-18 14:54:04 +0100462/* Find the end of the header value contained between <s> and <e>. See RFC2616,
463 * par 2.2 for more information. Note that it requires a valid header to return
464 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200465 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100466char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200467{
468 int quoted, qdpair;
469
470 quoted = qdpair = 0;
471 for (; s < e; s++) {
472 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200473 else if (quoted) {
474 if (*s == '\\') qdpair = 1;
475 else if (*s == '"') quoted = 0;
476 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200477 else if (*s == '"') quoted = 1;
478 else if (*s == ',') return s;
479 }
480 return s;
481}
482
483/* Find the first or next occurrence of header <name> in message buffer <sol>
484 * using headers index <idx>, and return it in the <ctx> structure. This
485 * structure holds everything necessary to use the header and find next
486 * occurrence. If its <idx> member is 0, the header is searched from the
487 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100488 * 1 when it finds a value, and 0 when there is no more. It is designed to work
489 * with headers defined as comma-separated lists. As a special case, if ctx->val
490 * is NULL when searching for a new values of a header, the current header is
491 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200492 */
493int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100494 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200495 struct hdr_ctx *ctx)
496{
Willy Tarreau68085d82010-01-18 14:54:04 +0100497 char *eol, *sov;
498 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200499
Willy Tarreau68085d82010-01-18 14:54:04 +0100500 cur_idx = ctx->idx;
501 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200502 /* We have previously returned a value, let's search
503 * another one on the same line.
504 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200505 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100506 ctx->del = ctx->val + ctx->vlen;
507 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 eol = sol + idx->v[cur_idx].len;
509
510 if (sov >= eol)
511 /* no more values in this header */
512 goto next_hdr;
513
Willy Tarreau68085d82010-01-18 14:54:04 +0100514 /* values remaining for this header, skip the comma but save it
515 * for later use (eg: for header deletion).
516 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200517 sov++;
518 while (sov < eol && http_is_lws[(unsigned char)*sov])
519 sov++;
520
521 goto return_hdr;
522 }
523
524 /* first request for this header */
525 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100526 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200527 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200528 while (cur_idx) {
529 eol = sol + idx->v[cur_idx].len;
530
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200531 if (len == 0) {
532 /* No argument was passed, we want any header.
533 * To achieve this, we simply build a fake request. */
534 while (sol + len < eol && sol[len] != ':')
535 len++;
536 name = sol;
537 }
538
Willy Tarreau33a7e692007-06-10 19:45:56 +0200539 if ((len < eol - sol) &&
540 (sol[len] == ':') &&
541 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100542 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200543 sov = sol + len + 1;
544 while (sov < eol && http_is_lws[(unsigned char)*sov])
545 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100546
Willy Tarreau33a7e692007-06-10 19:45:56 +0200547 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100548 ctx->prev = old_idx;
549 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200550 ctx->idx = cur_idx;
551 ctx->val = sov - sol;
552
553 eol = find_hdr_value_end(sov, eol);
554 ctx->vlen = eol - sov;
555 return 1;
556 }
557 next_hdr:
558 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100559 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200560 cur_idx = idx->v[cur_idx].next;
561 }
562 return 0;
563}
564
565int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100566 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200567 struct hdr_ctx *ctx)
568{
569 return http_find_header2(name, strlen(name), sol, idx, ctx);
570}
571
Willy Tarreau68085d82010-01-18 14:54:04 +0100572/* Remove one value of a header. This only works on a <ctx> returned by one of
573 * the http_find_header functions. The value is removed, as well as surrounding
574 * commas if any. If the removed value was alone, the whole header is removed.
575 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
576 * message <msg>. The new index is returned. If it is zero, it means there is
577 * no more header, so any processing may stop. The ctx is always left in a form
578 * that can be handled by http_find_header2() to find next occurrence.
579 */
580int http_remove_header2(struct http_msg *msg, struct buffer *buf,
581 struct hdr_idx *idx, struct hdr_ctx *ctx)
582{
583 int cur_idx = ctx->idx;
584 char *sol = ctx->line;
585 struct hdr_idx_elem *hdr;
586 int delta, skip_comma;
587
588 if (!cur_idx)
589 return 0;
590
591 hdr = &idx->v[cur_idx];
592 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) {
593 /* This was the only value of the header, we must now remove it entirely. */
594 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
595 http_msg_move_end(msg, delta);
596 idx->used--;
597 hdr->len = 0; /* unused entry */
598 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100599 if (idx->tail == ctx->idx)
600 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100601 ctx->idx = ctx->prev; /* walk back to the end of previous header */
602 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
603 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
604 ctx->vlen = 0;
605 return ctx->idx;
606 }
607
608 /* This was not the only value of this header. We have to remove between
609 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
610 * of the list, we remove the last separator.
611 */
612
613 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
614 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
615 sol + ctx->val + ctx->vlen + skip_comma,
616 NULL, 0);
617 hdr->len += delta;
618 http_msg_move_end(msg, delta);
619 ctx->val = ctx->del;
620 ctx->vlen = 0;
621 return ctx->idx;
622}
623
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100624/* This function handles a server error at the stream interface level. The
625 * stream interface is assumed to be already in a closed state. An optional
626 * message is copied into the input buffer, and an HTTP status code stored.
627 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100628 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100630static void http_server_error(struct session *t, struct stream_interface *si,
631 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100633 buffer_auto_read(si->ob);
634 buffer_abort(si->ob);
635 buffer_auto_close(si->ob);
636 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200637 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100638 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100639 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100640 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100641 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200642 }
643 if (!(t->flags & SN_ERR_MASK))
644 t->flags |= err;
645 if (!(t->flags & SN_FINST_MASK))
646 t->flags |= finst;
647}
648
Willy Tarreau80587432006-12-24 17:47:20 +0100649/* This function returns the appropriate error location for the given session
650 * and message.
651 */
652
653struct chunk *error_message(struct session *s, int msgnum)
654{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200655 if (s->be->errmsg[msgnum].str)
656 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100657 else if (s->fe->errmsg[msgnum].str)
658 return &s->fe->errmsg[msgnum];
659 else
660 return &http_err_chunks[msgnum];
661}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662
Willy Tarreau53b6c742006-12-17 13:37:46 +0100663/*
664 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
665 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
666 */
667static http_meth_t find_http_meth(const char *str, const int len)
668{
669 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100670 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100671
672 m = ((unsigned)*str - 'A');
673
674 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100675 for (h = http_methods[m]; h->len > 0; h++) {
676 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100677 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100678 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100679 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100680 };
681 return HTTP_METH_OTHER;
682 }
683 return HTTP_METH_NONE;
684
685}
686
Willy Tarreau21d2af32008-02-14 20:25:24 +0100687/* Parse the URI from the given transaction (which is assumed to be in request
688 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
689 * It is returned otherwise.
690 */
691static char *
692http_get_path(struct http_txn *txn)
693{
694 char *ptr, *end;
695
Willy Tarreau962c3f42010-01-10 00:15:35 +0100696 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100697 end = ptr + txn->req.sl.rq.u_l;
698
699 if (ptr >= end)
700 return NULL;
701
702 /* RFC2616, par. 5.1.2 :
703 * Request-URI = "*" | absuri | abspath | authority
704 */
705
706 if (*ptr == '*')
707 return NULL;
708
709 if (isalpha((unsigned char)*ptr)) {
710 /* this is a scheme as described by RFC3986, par. 3.1 */
711 ptr++;
712 while (ptr < end &&
713 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
714 ptr++;
715 /* skip '://' */
716 if (ptr == end || *ptr++ != ':')
717 return NULL;
718 if (ptr == end || *ptr++ != '/')
719 return NULL;
720 if (ptr == end || *ptr++ != '/')
721 return NULL;
722 }
723 /* skip [user[:passwd]@]host[:[port]] */
724
725 while (ptr < end && *ptr != '/')
726 ptr++;
727
728 if (ptr == end)
729 return NULL;
730
731 /* OK, we got the '/' ! */
732 return ptr;
733}
734
Willy Tarreauefb453c2008-10-26 20:49:47 +0100735/* Returns a 302 for a redirectable request. This may only be called just after
736 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
737 * left unchanged and will follow normal proxy processing.
738 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100740{
741 struct http_txn *txn;
742 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100743 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100744 char *path;
745 int len;
746
747 /* 1: create the response header */
748 rdr.len = strlen(HTTP_302);
749 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100750 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100751 memcpy(rdr.str, HTTP_302, rdr.len);
752
Willy Tarreau827aee92011-03-10 16:55:02 +0100753 srv = target_srv(&s->target);
754
Willy Tarreauefb453c2008-10-26 20:49:47 +0100755 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100756 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100757 return;
758
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100759 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100760 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
761 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
762 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100763 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764
765 /* 3: add the request URI */
766 txn = &s->txn;
767 path = http_get_path(txn);
768 if (!path)
769 return;
770
Willy Tarreau962c3f42010-01-10 00:15:35 +0100771 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200772 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 return;
774
775 memcpy(rdr.str + rdr.len, path, len);
776 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100777
778 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
779 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
780 rdr.len += 29;
781 } else {
782 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
783 rdr.len += 23;
784 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100785
786 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100787 si->shutr(si);
788 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 si->err_type = SI_ET_NONE;
790 si->err_loc = NULL;
791 si->state = SI_ST_CLO;
792
793 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100794 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100795
796 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100797 if (srv)
798 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100799}
800
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100801/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100802 * that the server side is closed. Note that err_type is actually a
803 * bitmask, where almost only aborts may be cumulated with other
804 * values. We consider that aborted operations are more important
805 * than timeouts or errors due to the fact that nobody else in the
806 * logs might explain incomplete retries. All others should avoid
807 * being cumulated. It should normally not be possible to have multiple
808 * aborts at once, but just in case, the first one in sequence is reported.
809 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100810void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100811{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100812 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813
814 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100815 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
816 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100818 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
819 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100820 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100821 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
822 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100823 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100824 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
825 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100826 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100827 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
828 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100829 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100830 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
831 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100832 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100833 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
834 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100835}
836
Willy Tarreau42250582007-04-01 01:30:43 +0200837extern const char sess_term_cond[8];
838extern const char sess_fin_state[8];
839extern const char *monthname[12];
Willy Tarreauf1348312010-10-07 15:54:11 +0200840const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */
841const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
842 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
843 Set-cookie Updated, unknown, unknown */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200844struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200845struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100846
Emeric Brun3a058f32009-06-30 18:26:00 +0200847void http_sess_clflog(struct session *s)
848{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100849 char pn[INET6_ADDRSTRLEN];
Emeric Brun3a058f32009-06-30 18:26:00 +0200850 struct proxy *fe = s->fe;
851 struct proxy *be = s->be;
852 struct proxy *prx_log;
853 struct http_txn *txn = &s->txn;
854 int tolog, level, err;
855 char *uri, *h;
Willy Tarreau71904a42011-02-13 14:30:26 +0100856 const char *svid;
Emeric Brun3a058f32009-06-30 18:26:00 +0200857 struct tm tm;
858 static char tmpline[MAX_SYSLOG_LEN];
859 int hdr;
860 size_t w;
861 int t_request;
862
863 prx_log = fe;
864 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +0200865 (s->req->cons->conn_retries != be->conn_retries) ||
Emeric Brun3a058f32009-06-30 18:26:00 +0200866 txn->status >= 500;
867
Willy Tarreau631f01c2011-09-05 00:36:48 +0200868 if (addr_to_str(&s->req->prod->addr.c.from, pn, sizeof(pn)) == AF_UNIX)
Emeric Brun5bd86a82010-10-22 17:23:04 +0200869 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
Emeric Brun3a058f32009-06-30 18:26:00 +0200870
871 get_gmtime(s->logs.accept_date.tv_sec, &tm);
872
873 /* FIXME: let's limit ourselves to frontend logging for now. */
874 tolog = fe->to_log;
875
876 h = tmpline;
877
878 w = snprintf(h, sizeof(tmpline),
879 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
880 pn,
881 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
882 tm.tm_hour, tm.tm_min, tm.tm_sec);
883 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
884 goto trunc;
885 h += w;
886
887 if (h >= tmpline + sizeof(tmpline) - 4)
888 goto trunc;
889
890 *(h++) = ' ';
891 *(h++) = '\"';
892 uri = txn->uri ? txn->uri : "<BADREQ>";
893 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
894 '#', url_encode_map, uri);
895 *(h++) = '\"';
896
897 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
898 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
899 goto trunc;
900 h += w;
901
902 if (h >= tmpline + sizeof(tmpline) - 9)
903 goto trunc;
904 memcpy(h, " \"-\" \"-\"", 8);
905 h += 8;
906
907 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
908 " %d %03d",
Willy Tarreau957c0a52011-03-03 17:42:23 +0100909 s->req->prod->addr.c.from.ss_family == AF_UNIX ? s->listener->luid :
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200910 get_host_port(&s->req->prod->addr.c.from),
Emeric Brun3a058f32009-06-30 18:26:00 +0200911 (int)s->logs.accept_date.tv_usec/1000);
912 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
913 goto trunc;
914 h += w;
915
916 w = strlen(fe->id);
917 if (h >= tmpline + sizeof(tmpline) - 4 - w)
918 goto trunc;
919 *(h++) = ' ';
920 *(h++) = '\"';
921 memcpy(h, fe->id, w);
922 h += w;
923 *(h++) = '\"';
924
925 w = strlen(be->id);
926 if (h >= tmpline + sizeof(tmpline) - 4 - w)
927 goto trunc;
928 *(h++) = ' ';
929 *(h++) = '\"';
930 memcpy(h, be->id, w);
931 h += w;
932 *(h++) = '\"';
933
Willy Tarreau71904a42011-02-13 14:30:26 +0100934 if (!(tolog & LW_SVID))
935 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200936 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100937 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200938 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100939 break;
940 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200941 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100942 break;
943 default:
944 svid = "<NOSRV>";
945 break;
946 }
Emeric Brun3a058f32009-06-30 18:26:00 +0200947
948 w = strlen(svid);
949 if (h >= tmpline + sizeof(tmpline) - 4 - w)
950 goto trunc;
951 *(h++) = ' ';
952 *(h++) = '\"';
953 memcpy(h, svid, w);
954 h += w;
955 *(h++) = '\"';
956
957 t_request = -1;
958 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
959 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
960 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
961 " %d %ld %ld %ld %ld",
962 t_request,
963 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
964 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
965 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
966 s->logs.t_close);
967 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
968 goto trunc;
969 h += w;
970
971 if (h >= tmpline + sizeof(tmpline) - 8)
972 goto trunc;
973 *(h++) = ' ';
974 *(h++) = '\"';
975 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
976 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
977 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
978 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
979 *(h++) = '\"';
980
981 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
982 " %d %d %d %d %d %ld %ld",
Willy Tarreau827aee92011-03-10 16:55:02 +0100983 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Willy Tarreauee28de02010-06-01 09:51:00 +0200984 (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries,
Emeric Brun3a058f32009-06-30 18:26:00 +0200985 s->logs.srv_queue_size, s->logs.prx_queue_size);
986
987 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
988 goto trunc;
989 h += w;
990
991 if (txn->cli_cookie) {
992 w = strlen(txn->cli_cookie);
993 if (h >= tmpline + sizeof(tmpline) - 4 - w)
994 goto trunc;
995 *(h++) = ' ';
996 *(h++) = '\"';
997 memcpy(h, txn->cli_cookie, w);
998 h += w;
999 *(h++) = '\"';
1000 } else {
1001 if (h >= tmpline + sizeof(tmpline) - 5)
1002 goto trunc;
1003 memcpy(h, " \"-\"", 4);
1004 h += 4;
1005 }
1006
1007 if (txn->srv_cookie) {
1008 w = strlen(txn->srv_cookie);
1009 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1010 goto trunc;
1011 *(h++) = ' ';
1012 *(h++) = '\"';
1013 memcpy(h, txn->srv_cookie, w);
1014 h += w;
1015 *(h++) = '\"';
1016 } else {
1017 if (h >= tmpline + sizeof(tmpline) - 5)
1018 goto trunc;
1019 memcpy(h, " \"-\"", 4);
1020 h += 4;
1021 }
1022
1023 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1024 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1025 if (h >= sizeof (tmpline) + tmpline - 4)
1026 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001027 if (txn->req.cap[hdr] != NULL) {
1028 *(h++) = ' ';
1029 *(h++) = '\"';
1030 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1031 '#', hdr_encode_map, txn->req.cap[hdr]);
1032 *(h++) = '\"';
1033 } else {
1034 memcpy(h, " \"-\"", 4);
1035 h += 4;
1036 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001037 }
1038 }
1039
1040 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1041 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1042 if (h >= sizeof (tmpline) + tmpline - 4)
1043 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001044 if (txn->rsp.cap[hdr] != NULL) {
1045 *(h++) = ' ';
1046 *(h++) = '\"';
1047 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1048 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1049 *(h++) = '\"';
1050 } else {
1051 memcpy(h, " \"-\"", 4);
1052 h += 4;
1053 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001054 }
1055 }
1056
1057trunc:
1058 *h = '\0';
1059
1060 level = LOG_INFO;
1061 if (err && (fe->options2 & PR_O2_LOGERRORS))
1062 level = LOG_ERR;
1063
1064 send_log(prx_log, level, "%s\n", tmpline);
1065
1066 s->logs.logwait = 0;
1067}
1068
Willy Tarreau42250582007-04-01 01:30:43 +02001069/*
1070 * send a log for the session when we have enough info about it.
1071 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001073void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001074{
Cyril Bontéacd7d632010-11-01 19:26:02 +01001075 char pn[INET6_ADDRSTRLEN];
Willy Tarreau42250582007-04-01 01:30:43 +02001076 struct proxy *fe = s->fe;
1077 struct proxy *be = s->be;
1078 struct proxy *prx_log;
1079 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001080 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001081 char *uri, *h;
Willy Tarreau71904a42011-02-13 14:30:26 +01001082 const char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001083 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001084 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001085 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001086 int hdr;
1087
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001088 /* if we don't want to log normal traffic, return now */
1089 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +02001090 (s->req->cons->conn_retries != be->conn_retries) ||
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001091 txn->status >= 500;
1092 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1093 return;
1094
Willy Tarreau42250582007-04-01 01:30:43 +02001095 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1096 return;
1097 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098
Emeric Brun3a058f32009-06-30 18:26:00 +02001099 if (prx_log->options2 & PR_O2_CLFLOG)
1100 return http_sess_clflog(s);
1101
Willy Tarreau631f01c2011-09-05 00:36:48 +02001102 if (addr_to_str(&s->req->prod->addr.c.from, pn, sizeof(pn)) == AF_UNIX)
1103 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
Willy Tarreau42250582007-04-01 01:30:43 +02001104
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001105 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001106
1107 /* FIXME: let's limit ourselves to frontend logging for now. */
1108 tolog = fe->to_log;
1109
1110 h = tmpline;
1111 if (fe->to_log & LW_REQHDR &&
1112 txn->req.cap &&
1113 (h < tmpline + sizeof(tmpline) - 10)) {
1114 *(h++) = ' ';
1115 *(h++) = '{';
1116 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1117 if (hdr)
1118 *(h++) = '|';
1119 if (txn->req.cap[hdr] != NULL)
1120 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1121 '#', hdr_encode_map, txn->req.cap[hdr]);
1122 }
1123 *(h++) = '}';
1124 }
1125
1126 if (fe->to_log & LW_RSPHDR &&
1127 txn->rsp.cap &&
1128 (h < tmpline + sizeof(tmpline) - 7)) {
1129 *(h++) = ' ';
1130 *(h++) = '{';
1131 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1132 if (hdr)
1133 *(h++) = '|';
1134 if (txn->rsp.cap[hdr] != NULL)
1135 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1136 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1137 }
1138 *(h++) = '}';
1139 }
1140
1141 if (h < tmpline + sizeof(tmpline) - 4) {
1142 *(h++) = ' ';
1143 *(h++) = '"';
1144 uri = txn->uri ? txn->uri : "<BADREQ>";
1145 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1146 '#', url_encode_map, uri);
1147 *(h++) = '"';
1148 }
1149 *h = '\0';
1150
Willy Tarreau71904a42011-02-13 14:30:26 +01001151 if (!(tolog & LW_SVID))
1152 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001153 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +01001154 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001155 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +01001156 break;
1157 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001158 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +01001159 break;
1160 default:
1161 svid = "<NOSRV>";
1162 break;
1163 }
Willy Tarreau42250582007-04-01 01:30:43 +02001164
Willy Tarreau70089872008-06-13 21:12:51 +02001165 t_request = -1;
1166 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1167 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1168
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001169 level = LOG_INFO;
1170 if (err && (fe->options2 & PR_O2_LOGERRORS))
1171 level = LOG_ERR;
1172
1173 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001174 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001175 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1176 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau957c0a52011-03-03 17:42:23 +01001177 (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? "unix" : pn,
1178 (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? s->listener->luid :
Willy Tarreau86ad42c2011-08-27 12:29:07 +02001179 get_host_port(&s->req->prod->addr.c.from),
Willy Tarreaufe944602007-10-25 10:34:16 +02001180 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001181 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001182 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001183 t_request,
1184 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001185 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1186 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1187 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1188 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001189 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001190 txn->cli_cookie ? txn->cli_cookie : "-",
1191 txn->srv_cookie ? txn->srv_cookie : "-",
1192 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1193 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1194 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1195 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
Willy Tarreau827aee92011-03-10 16:55:02 +01001196 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001197 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +02001198 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001199 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1200
1201 s->logs.logwait = 0;
1202}
1203
Willy Tarreau117f59e2007-03-04 18:17:17 +01001204
1205/*
1206 * Capture headers from message starting at <som> according to header list
1207 * <cap_hdr>, and fill the <idx> structure appropriately.
1208 */
1209void capture_headers(char *som, struct hdr_idx *idx,
1210 char **cap, struct cap_hdr *cap_hdr)
1211{
1212 char *eol, *sol, *col, *sov;
1213 int cur_idx;
1214 struct cap_hdr *h;
1215 int len;
1216
1217 sol = som + hdr_idx_first_pos(idx);
1218 cur_idx = hdr_idx_first_idx(idx);
1219
1220 while (cur_idx) {
1221 eol = sol + idx->v[cur_idx].len;
1222
1223 col = sol;
1224 while (col < eol && *col != ':')
1225 col++;
1226
1227 sov = col + 1;
1228 while (sov < eol && http_is_lws[(unsigned char)*sov])
1229 sov++;
1230
1231 for (h = cap_hdr; h; h = h->next) {
1232 if ((h->namelen == col - sol) &&
1233 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1234 if (cap[h->index] == NULL)
1235 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001236 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001237
1238 if (cap[h->index] == NULL) {
1239 Alert("HTTP capture : out of memory.\n");
1240 continue;
1241 }
1242
1243 len = eol - sov;
1244 if (len > h->len)
1245 len = h->len;
1246
1247 memcpy(cap[h->index], sov, len);
1248 cap[h->index][len]=0;
1249 }
1250 }
1251 sol = eol + idx->v[cur_idx].cr + 1;
1252 cur_idx = idx->v[cur_idx].next;
1253 }
1254}
1255
1256
Willy Tarreau42250582007-04-01 01:30:43 +02001257/* either we find an LF at <ptr> or we jump to <bad>.
1258 */
1259#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1260
1261/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1262 * otherwise to <http_msg_ood> with <state> set to <st>.
1263 */
1264#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1265 ptr++; \
1266 if (likely(ptr < end)) \
1267 goto good; \
1268 else { \
1269 state = (st); \
1270 goto http_msg_ood; \
1271 } \
1272 } while (0)
1273
1274
Willy Tarreaubaaee002006-06-26 02:48:02 +02001275/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001276 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001277 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1278 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1279 * will give undefined results.
1280 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1281 * and that msg->sol points to the beginning of the response.
1282 * If a complete line is found (which implies that at least one CR or LF is
1283 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1284 * returned indicating an incomplete line (which does not mean that parts have
1285 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1286 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1287 * upon next call.
1288 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001289 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001290 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1291 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001292 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001294const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1295 unsigned int state, const char *ptr, const char *end,
1296 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001297{
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001299 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001300 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001301 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001302 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1303
1304 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001305 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001306 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1307 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001308 state = HTTP_MSG_ERROR;
1309 break;
1310
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001312 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001313 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001314 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001315 goto http_msg_rpcode;
1316 }
1317 if (likely(HTTP_IS_SPHT(*ptr)))
1318 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1319 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001320 state = HTTP_MSG_ERROR;
1321 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001322
Willy Tarreau8973c702007-01-21 23:58:29 +01001323 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001324 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001325 if (likely(!HTTP_IS_LWS(*ptr)))
1326 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1327
1328 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001329 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001330 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1331 }
1332
1333 /* so it's a CR/LF, so there is no reason phrase */
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 http_msg_rsp_reason:
1336 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001337 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 msg->sl.st.r_l = 0;
1339 goto http_msg_rpline_eol;
1340
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001342 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001344 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 goto http_msg_rpreason;
1346 }
1347 if (likely(HTTP_IS_SPHT(*ptr)))
1348 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1349 /* so it's a CR/LF, so there is no reason phrase */
1350 goto http_msg_rsp_reason;
1351
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001353 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001354 if (likely(!HTTP_IS_CRLF(*ptr)))
1355 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001356 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001357 http_msg_rpline_eol:
1358 /* We have seen the end of line. Note that we do not
1359 * necessarily have the \n yet, but at least we know that we
1360 * have EITHER \r OR \n, otherwise the response would not be
1361 * complete. We can then record the response length and return
1362 * to the caller which will be able to register it.
1363 */
1364 msg->sl.st.l = ptr - msg->sol;
1365 return ptr;
1366
1367#ifdef DEBUG_FULL
1368 default:
1369 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1370 exit(1);
1371#endif
1372 }
1373
1374 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001375 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001376 if (ret_state)
1377 *ret_state = state;
1378 if (ret_ptr)
1379 *ret_ptr = (char *)ptr;
1380 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001381}
1382
Willy Tarreau8973c702007-01-21 23:58:29 +01001383/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 * This function parses a request line between <ptr> and <end>, starting with
1385 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1386 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1387 * will give undefined results.
1388 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1389 * and that msg->sol points to the beginning of the request.
1390 * If a complete line is found (which implies that at least one CR or LF is
1391 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1392 * returned indicating an incomplete line (which does not mean that parts have
1393 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1394 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1395 * upon next call.
1396 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001397 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1399 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001400 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001401 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001402const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1403 unsigned int state, const char *ptr, const char *end,
1404 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001405{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001408 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 if (likely(HTTP_IS_TOKEN(*ptr)))
1410 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001413 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1415 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001416
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 if (likely(HTTP_IS_CRLF(*ptr))) {
1418 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001419 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001421 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001423 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001425 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 msg->sl.rq.v_l = 0;
1427 goto http_msg_rqline_eol;
1428 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001429 state = HTTP_MSG_ERROR;
1430 break;
1431
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001433 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001435 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 goto http_msg_rquri;
1437 }
1438 if (likely(HTTP_IS_SPHT(*ptr)))
1439 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1440 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1441 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001444 http_msg_rquri:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 if (likely(!HTTP_IS_LWS(*ptr)))
1446 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001449 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1451 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1454 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001455
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001457 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001459 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001460 goto http_msg_rqver;
1461 }
1462 if (likely(HTTP_IS_SPHT(*ptr)))
1463 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1464 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1465 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001466
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001467 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001468 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001469 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001471
1472 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001473 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001474 http_msg_rqline_eol:
1475 /* We have seen the end of line. Note that we do not
1476 * necessarily have the \n yet, but at least we know that we
1477 * have EITHER \r OR \n, otherwise the request would not be
1478 * complete. We can then record the request length and return
1479 * to the caller which will be able to register it.
1480 */
1481 msg->sl.rq.l = ptr - msg->sol;
1482 return ptr;
1483 }
1484
1485 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001486 state = HTTP_MSG_ERROR;
1487 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001488
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489#ifdef DEBUG_FULL
1490 default:
1491 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1492 exit(1);
1493#endif
1494 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001495
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001497 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 if (ret_state)
1499 *ret_state = state;
1500 if (ret_ptr)
1501 *ret_ptr = (char *)ptr;
1502 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001504
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001505/*
1506 * Returns the data from Authorization header. Function may be called more
1507 * than once so data is stored in txn->auth_data. When no header is found
1508 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1509 * searching again for something we are unable to find anyway.
1510 */
1511
1512char get_http_auth_buff[BUFSIZE];
1513
1514int
1515get_http_auth(struct session *s)
1516{
1517
1518 struct http_txn *txn = &s->txn;
1519 struct chunk auth_method;
1520 struct hdr_ctx ctx;
1521 char *h, *p;
1522 int len;
1523
1524#ifdef DEBUG_AUTH
1525 printf("Auth for session %p: %d\n", s, txn->auth.method);
1526#endif
1527
1528 if (txn->auth.method == HTTP_AUTH_WRONG)
1529 return 0;
1530
1531 if (txn->auth.method)
1532 return 1;
1533
1534 txn->auth.method = HTTP_AUTH_WRONG;
1535
1536 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001537
1538 if (txn->flags & TX_USE_PX_CONN) {
1539 h = "Proxy-Authorization";
1540 len = strlen(h);
1541 } else {
1542 h = "Authorization";
1543 len = strlen(h);
1544 }
1545
1546 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001547 return 0;
1548
1549 h = ctx.line + ctx.val;
1550
1551 p = memchr(h, ' ', ctx.vlen);
1552 if (!p || p == h)
1553 return 0;
1554
1555 chunk_initlen(&auth_method, h, 0, p-h);
1556 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1557
1558 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1559
1560 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1561 get_http_auth_buff, BUFSIZE - 1);
1562
1563 if (len < 0)
1564 return 0;
1565
1566
1567 get_http_auth_buff[len] = '\0';
1568
1569 p = strchr(get_http_auth_buff, ':');
1570
1571 if (!p)
1572 return 0;
1573
1574 txn->auth.user = get_http_auth_buff;
1575 *p = '\0';
1576 txn->auth.pass = p+1;
1577
1578 txn->auth.method = HTTP_AUTH_BASIC;
1579 return 1;
1580 }
1581
1582 return 0;
1583}
1584
Willy Tarreau58f10d72006-12-04 02:26:12 +01001585
Willy Tarreau8973c702007-01-21 23:58:29 +01001586/*
1587 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001588 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001589 * when data are missing and recalled at the exact same location with no
1590 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001591 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001592 * fields. Note that msg->som and msg->sol will be initialized after completing
1593 * the first state, so that none of the msg pointers has to be initialized
1594 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001595 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1597{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001598 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001600
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001601 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602 ptr = buf->lr;
1603 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001604
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 if (unlikely(ptr >= end))
1606 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001607
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001608 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001609 /*
1610 * First, states that are specific to the response only.
1611 * We check them first so that request and headers are
1612 * closer to each other (accessed more often).
1613 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001614 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001615 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001616 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001617 /* we have a start of message, but we have to check
1618 * first if we need to remove some CRLF. We can only
1619 * do this when send_max=0.
1620 */
1621 char *beg = buf->w + buf->send_max;
1622 if (beg >= buf->data + buf->size)
1623 beg -= buf->size;
1624 if (unlikely(ptr != beg)) {
1625 if (buf->send_max)
1626 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001627 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001628 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001629 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001630 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001631 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001632 hdr_idx_init(idx);
1633 state = HTTP_MSG_RPVER;
1634 goto http_msg_rpver;
1635 }
1636
1637 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1638 goto http_msg_invalid;
1639
1640 if (unlikely(*ptr == '\n'))
1641 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1642 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1643 /* stop here */
1644
Willy Tarreau8973c702007-01-21 23:58:29 +01001645 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001646 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001647 EXPECT_LF_HERE(ptr, http_msg_invalid);
1648 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1649 /* stop here */
1650
Willy Tarreau8973c702007-01-21 23:58:29 +01001651 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001652 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001653 case HTTP_MSG_RPVER_SP:
1654 case HTTP_MSG_RPCODE:
1655 case HTTP_MSG_RPCODE_SP:
1656 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001657 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001658 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001659 if (unlikely(!ptr))
1660 return;
1661
1662 /* we have a full response and we know that we have either a CR
1663 * or an LF at <ptr>.
1664 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001665 //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 +01001666 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1667
1668 msg->sol = ptr;
1669 if (likely(*ptr == '\r'))
1670 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1671 goto http_msg_rpline_end;
1672
Willy Tarreau8973c702007-01-21 23:58:29 +01001673 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001674 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001675 /* msg->sol must point to the first of CR or LF. */
1676 EXPECT_LF_HERE(ptr, http_msg_invalid);
1677 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1678 /* stop here */
1679
1680 /*
1681 * Second, states that are specific to the request only
1682 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001683 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001684 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001686 /* we have a start of message, but we have to check
1687 * first if we need to remove some CRLF. We can only
1688 * do this when send_max=0.
1689 */
1690 char *beg = buf->w + buf->send_max;
1691 if (beg >= buf->data + buf->size)
1692 beg -= buf->size;
1693 if (likely(ptr != beg)) {
1694 if (buf->send_max)
1695 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001696 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001697 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001699 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001700 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001701 /* we will need this when keep-alive will be supported
1702 hdr_idx_init(idx);
1703 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001704 state = HTTP_MSG_RQMETH;
1705 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001707
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1709 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001710
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001711 if (unlikely(*ptr == '\n'))
1712 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1713 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001714 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001715
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001717 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001718 EXPECT_LF_HERE(ptr, http_msg_invalid);
1719 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001720 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001721
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001723 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 case HTTP_MSG_RQMETH_SP:
1725 case HTTP_MSG_RQURI:
1726 case HTTP_MSG_RQURI_SP:
1727 case HTTP_MSG_RQVER:
1728 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001729 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 if (unlikely(!ptr))
1731 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001732
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 /* we have a full request and we know that we have either a CR
1734 * or an LF at <ptr>.
1735 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001736 //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 +01001737 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001738
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 msg->sol = ptr;
1740 if (likely(*ptr == '\r'))
1741 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001743
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001745 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 /* check for HTTP/0.9 request : no version information available.
1747 * msg->sol must point to the first of CR or LF.
1748 */
1749 if (unlikely(msg->sl.rq.v_l == 0))
1750 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001751
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 EXPECT_LF_HERE(ptr, http_msg_invalid);
1753 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001754 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001755
Willy Tarreau8973c702007-01-21 23:58:29 +01001756 /*
1757 * Common states below
1758 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001760 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001761 msg->sol = ptr;
1762 if (likely(!HTTP_IS_CRLF(*ptr))) {
1763 goto http_msg_hdr_name;
1764 }
1765
1766 if (likely(*ptr == '\r'))
1767 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1768 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001769
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001770 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001771 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001772 /* assumes msg->sol points to the first char */
1773 if (likely(HTTP_IS_TOKEN(*ptr)))
1774 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001775
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 if (likely(*ptr == ':')) {
1777 msg->col = ptr - buf->data;
1778 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1779 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001780
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001781 if (likely(msg->err_pos < -1) || *ptr == '\n')
1782 goto http_msg_invalid;
1783
1784 if (msg->err_pos == -1) /* capture error pointer */
1785 msg->err_pos = ptr - buf->data; /* >= 0 now */
1786
1787 /* and we still accept this non-token character */
1788 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001789
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001791 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001792 /* assumes msg->sol points to the first char and msg->col to the colon */
1793 if (likely(HTTP_IS_SPHT(*ptr)))
1794 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001795
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001796 /* header value can be basically anything except CR/LF */
1797 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001798
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001799 if (likely(!HTTP_IS_CRLF(*ptr))) {
1800 goto http_msg_hdr_val;
1801 }
1802
1803 if (likely(*ptr == '\r'))
1804 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1805 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001806
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001807 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001808 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001809 EXPECT_LF_HERE(ptr, http_msg_invalid);
1810 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001811
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001812 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001813 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 if (likely(HTTP_IS_SPHT(*ptr))) {
1815 /* replace HT,CR,LF with spaces */
1816 for (; buf->data+msg->sov < ptr; msg->sov++)
1817 buf->data[msg->sov] = ' ';
1818 goto http_msg_hdr_l1_sp;
1819 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001820 /* we had a header consisting only in spaces ! */
1821 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 goto http_msg_complete_header;
1823
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001825 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 /* assumes msg->sol points to the first char, msg->col to the
1827 * colon, and msg->sov points to the first character of the
1828 * value.
1829 */
1830 if (likely(!HTTP_IS_CRLF(*ptr)))
1831 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001832
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001833 msg->eol = ptr;
1834 /* Note: we could also copy eol into ->eoh so that we have the
1835 * real header end in case it ends with lots of LWS, but is this
1836 * really needed ?
1837 */
1838 if (likely(*ptr == '\r'))
1839 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1840 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001841
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001842 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001843 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001844 EXPECT_LF_HERE(ptr, http_msg_invalid);
1845 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001846
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001847 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001848 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1850 /* LWS: replace HT,CR,LF with spaces */
1851 for (; msg->eol < ptr; msg->eol++)
1852 *msg->eol = ' ';
1853 goto http_msg_hdr_val;
1854 }
1855 http_msg_complete_header:
1856 /*
1857 * It was a new header, so the last one is finished.
1858 * Assumes msg->sol points to the first char, msg->col to the
1859 * colon, msg->sov points to the first character of the value
1860 * and msg->eol to the first CR or LF so we know how the line
1861 * ends. We insert last header into the index.
1862 */
1863 /*
1864 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1865 write(2, msg->sol, msg->eol-msg->sol);
1866 fprintf(stderr,"\n");
1867 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001868
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001869 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1870 idx, idx->tail) < 0))
1871 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001872
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001873 msg->sol = ptr;
1874 if (likely(!HTTP_IS_CRLF(*ptr))) {
1875 goto http_msg_hdr_name;
1876 }
1877
1878 if (likely(*ptr == '\r'))
1879 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1880 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001881
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001882 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001883 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001884 /* Assumes msg->sol points to the first of either CR or LF */
1885 EXPECT_LF_HERE(ptr, http_msg_invalid);
1886 ptr++;
1887 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001888 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001889 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001890 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001891 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 return;
1893#ifdef DEBUG_FULL
1894 default:
1895 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1896 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001897#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001898 }
1899 http_msg_ood:
1900 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001901 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001902 buf->lr = ptr;
1903 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001904
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001905 http_msg_invalid:
1906 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001907 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001908 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909 return;
1910}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001911
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001912/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1913 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1914 * nothing is done and 1 is returned.
1915 */
1916static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1917{
1918 int delta;
1919 char *cur_end;
1920
1921 if (msg->sl.rq.v_l != 0)
1922 return 1;
1923
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001924 cur_end = msg->sol + msg->sl.rq.l;
1925 delta = 0;
1926
1927 if (msg->sl.rq.u_l == 0) {
1928 /* if no URI was set, add "/" */
1929 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1930 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001931 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001932 }
1933 /* add HTTP version */
1934 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001935 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001936 cur_end += delta;
1937 cur_end = (char *)http_parse_reqline(msg, req->data,
1938 HTTP_MSG_RQMETH,
1939 msg->sol, cur_end + 1,
1940 NULL, NULL);
1941 if (unlikely(!cur_end))
1942 return 0;
1943
1944 /* we have a full HTTP/1.0 request now and we know that
1945 * we have either a CR or an LF at <ptr>.
1946 */
1947 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1948 return 1;
1949}
1950
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001951/* Parse the Connection: header of an HTTP request, looking for both "close"
1952 * and "keep-alive" values. If a buffer is provided and we already know that
1953 * some headers may safely be removed, we remove them now. The <to_del> flags
1954 * are used for that :
1955 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1956 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1957 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1958 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1959 * harmless combinations may be removed. Do not call that after changes have
1960 * been processed. If unused, the buffer can be NULL, and no data will be
1961 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001962 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001963void 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 +01001964{
Willy Tarreau5b154472009-12-21 20:11:07 +01001965 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001966 const char *hdr_val = "Connection";
1967 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001968
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001969 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001970 return;
1971
Willy Tarreau88d349d2010-01-25 12:15:43 +01001972 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1973 hdr_val = "Proxy-Connection";
1974 hdr_len = 16;
1975 }
1976
Willy Tarreau5b154472009-12-21 20:11:07 +01001977 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001978 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001979 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001980 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1981 txn->flags |= TX_HDR_CONN_KAL;
1982 if ((to_del & 2) && buf)
1983 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1984 else
1985 txn->flags |= TX_CON_KAL_SET;
1986 }
1987 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1988 txn->flags |= TX_HDR_CONN_CLO;
1989 if ((to_del & 1) && buf)
1990 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1991 else
1992 txn->flags |= TX_CON_CLO_SET;
1993 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001994 }
1995
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001996 txn->flags |= TX_HDR_CONN_PRS;
1997 return;
1998}
Willy Tarreau5b154472009-12-21 20:11:07 +01001999
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002000/* Apply desired changes on the Connection: header. Values may be removed and/or
2001 * added depending on the <wanted> flags, which are exclusively composed of
2002 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2003 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2004 */
2005void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
2006{
2007 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002008 const char *hdr_val = "Connection";
2009 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002010
2011 ctx.idx = 0;
2012
Willy Tarreau88d349d2010-01-25 12:15:43 +01002013
2014 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2015 hdr_val = "Proxy-Connection";
2016 hdr_len = 16;
2017 }
2018
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002019 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002020 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002021 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2022 if (wanted & TX_CON_KAL_SET)
2023 txn->flags |= TX_CON_KAL_SET;
2024 else
2025 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002026 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002027 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2028 if (wanted & TX_CON_CLO_SET)
2029 txn->flags |= TX_CON_CLO_SET;
2030 else
2031 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002032 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002033 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002034
2035 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2036 return;
2037
2038 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2039 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002040 hdr_val = "Connection: close";
2041 hdr_len = 17;
2042 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2043 hdr_val = "Proxy-Connection: close";
2044 hdr_len = 23;
2045 }
2046 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002047 }
2048
2049 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2050 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002051 hdr_val = "Connection: keep-alive";
2052 hdr_len = 22;
2053 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2054 hdr_val = "Proxy-Connection: keep-alive";
2055 hdr_len = 28;
2056 }
2057 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002058 }
2059 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002060}
2061
Willy Tarreaud98cf932009-12-27 22:54:55 +01002062/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2063 * first byte of body, and increments msg->sov by the number of bytes parsed,
2064 * so that we know we can forward between ->som and ->sov. Note that due to
2065 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2066 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002067 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002068 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002069 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002071{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002072 char *ptr = buf->lr;
2073 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002074 unsigned int chunk = 0;
2075
2076 /* The chunk size is in the following form, though we are only
2077 * interested in the size and CRLF :
2078 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2079 */
2080 while (1) {
2081 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002082 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002083 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002084 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002085 if (c < 0) /* not a hex digit anymore */
2086 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002087 if (++ptr >= end)
2088 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002089 if (chunk & 0xF000000) /* overflow will occur */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002090 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002091 chunk = (chunk << 4) + c;
2092 }
2093
Willy Tarreaud98cf932009-12-27 22:54:55 +01002094 /* empty size not allowed */
2095 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002096 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002097
2098 while (http_is_spht[(unsigned char)*ptr]) {
2099 if (++ptr >= end)
2100 ptr = buf->data;
2101 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002102 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002103 }
2104
Willy Tarreaud98cf932009-12-27 22:54:55 +01002105 /* Up to there, we know that at least one byte is present at *ptr. Check
2106 * for the end of chunk size.
2107 */
2108 while (1) {
2109 if (likely(HTTP_IS_CRLF(*ptr))) {
2110 /* we now have a CR or an LF at ptr */
2111 if (likely(*ptr == '\r')) {
2112 if (++ptr >= end)
2113 ptr = buf->data;
2114 if (ptr == buf->r)
2115 return 0;
2116 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002117
Willy Tarreaud98cf932009-12-27 22:54:55 +01002118 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002119 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002120 if (++ptr >= end)
2121 ptr = buf->data;
2122 /* done */
2123 break;
2124 }
2125 else if (*ptr == ';') {
2126 /* chunk extension, ends at next CRLF */
2127 if (++ptr >= end)
2128 ptr = buf->data;
2129 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002130 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002131
2132 while (!HTTP_IS_CRLF(*ptr)) {
2133 if (++ptr >= end)
2134 ptr = buf->data;
2135 if (ptr == buf->r)
2136 return 0;
2137 }
2138 /* we have a CRLF now, loop above */
2139 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002140 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002141 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002142 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002143 }
2144
Willy Tarreaud98cf932009-12-27 22:54:55 +01002145 /* OK we found our CRLF and now <ptr> points to the next byte,
2146 * which may or may not be present. We save that into ->lr and
2147 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002148 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002149 msg->sov += ptr - buf->lr;
2150 buf->lr = ptr;
Willy Tarreau124d9912011-03-01 20:30:48 +01002151 msg->chunk_len = chunk;
2152 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002153 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002154 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002155 error:
2156 msg->err_pos = ptr - buf->data;
2157 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002158}
2159
Willy Tarreaud98cf932009-12-27 22:54:55 +01002160/* This function skips trailers in the buffer <buf> associated with HTTP
2161 * message <msg>. The first visited position is buf->lr. If the end of
2162 * the trailers is found, it is automatically scheduled to be forwarded,
2163 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2164 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002165 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2166 * zero. If a parse error is encountered, the function returns < 0 and does not
2167 * change anything except maybe buf->lr and msg->sov. Note that the message
2168 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2169 * which implies that all non-trailers data have already been scheduled for
2170 * forwarding, and that the difference between msg->som and msg->sov exactly
2171 * matches the length of trailers already parsed and not forwarded. It is also
2172 * important to note that this function is designed to be able to parse wrapped
2173 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002174 */
2175int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2176{
2177 /* we have buf->lr which points to next line. Look for CRLF. */
2178 while (1) {
2179 char *p1 = NULL, *p2 = NULL;
2180 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002181 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002182
2183 /* scan current line and stop at LF or CRLF */
2184 while (1) {
2185 if (ptr == buf->r)
2186 return 0;
2187
2188 if (*ptr == '\n') {
2189 if (!p1)
2190 p1 = ptr;
2191 p2 = ptr;
2192 break;
2193 }
2194
2195 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002196 if (p1) {
2197 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002198 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002199 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002200 p1 = ptr;
2201 }
2202
2203 ptr++;
2204 if (ptr >= buf->data + buf->size)
2205 ptr = buf->data;
2206 }
2207
2208 /* after LF; point to beginning of next line */
2209 p2++;
2210 if (p2 >= buf->data + buf->size)
2211 p2 = buf->data;
2212
Willy Tarreau638cd022010-01-03 07:42:04 +01002213 bytes = p2 - buf->lr;
2214 if (bytes < 0)
2215 bytes += buf->size;
2216
2217 /* schedule this line for forwarding */
2218 msg->sov += bytes;
2219 if (msg->sov >= buf->size)
2220 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002221
Willy Tarreau638cd022010-01-03 07:42:04 +01002222 if (p1 == buf->lr) {
2223 /* LF/CRLF at beginning of line => end of trailers at p2.
2224 * Everything was scheduled for forwarding, there's nothing
2225 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002226 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002227 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002228 msg->msg_state = HTTP_MSG_DONE;
2229 return 1;
2230 }
2231 /* OK, next line then */
2232 buf->lr = p2;
2233 }
2234}
2235
2236/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2237 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2238 * ->som, buf->lr in order to include this part into the next forwarding phase.
2239 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2240 * not enough data are available, the function does not change anything and
2241 * returns zero. If a parse error is encountered, the function returns < 0 and
2242 * does not change anything. Note: this function is designed to parse wrapped
2243 * CRLF at the end of the buffer.
2244 */
2245int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2246{
2247 char *ptr;
2248 int bytes;
2249
2250 /* NB: we'll check data availabilty at the end. It's not a
2251 * problem because whatever we match first will be checked
2252 * against the correct length.
2253 */
2254 bytes = 1;
2255 ptr = buf->lr;
2256 if (*ptr == '\r') {
2257 bytes++;
2258 ptr++;
2259 if (ptr >= buf->data + buf->size)
2260 ptr = buf->data;
2261 }
2262
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002263 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002264 return 0;
2265
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002266 if (*ptr != '\n') {
2267 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002268 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002269 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002270
2271 ptr++;
2272 if (ptr >= buf->data + buf->size)
2273 ptr = buf->data;
2274 buf->lr = ptr;
2275 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2276 msg->sov = ptr - buf->data;
2277 msg->som = msg->sov - bytes;
2278 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2279 return 1;
2280}
Willy Tarreau5b154472009-12-21 20:11:07 +01002281
Willy Tarreau83e3af02009-12-28 17:39:57 +01002282void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2283{
2284 char *end = buf->data + buf->size;
2285 int off = buf->data + buf->size - buf->w;
2286
2287 /* two possible cases :
2288 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002289 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002290 */
2291 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002292 int block1 = buf->l;
2293 int block2 = 0;
2294 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002295 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002296 block1 = buf->data + buf->size - buf->w;
2297 block2 = buf->r - buf->data;
2298 }
2299 if (block2)
2300 memcpy(swap_buffer, buf->data, block2);
2301 memmove(buf->data, buf->w, block1);
2302 if (block2)
2303 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002304 }
2305
2306 /* adjust all known pointers */
2307 buf->w = buf->data;
2308 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2309 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2310 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2311 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2312
2313 /* adjust relative pointers */
2314 msg->som = 0;
2315 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2316 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2317 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2318
Willy Tarreau83e3af02009-12-28 17:39:57 +01002319 if (msg->err_pos >= 0) {
2320 msg->err_pos += off;
2321 if (msg->err_pos >= buf->size)
2322 msg->err_pos -= buf->size;
2323 }
2324
2325 buf->flags &= ~BF_FULL;
2326 if (buf->l >= buffer_max_len(buf))
2327 buf->flags |= BF_FULL;
2328}
2329
Willy Tarreaud787e662009-07-07 10:14:51 +02002330/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2331 * processing can continue on next analysers, or zero if it either needs more
2332 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2333 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2334 * when it has nothing left to do, and may remove any analyser when it wants to
2335 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002336 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002337int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002338{
Willy Tarreau59234e92008-11-30 23:51:27 +01002339 /*
2340 * We will parse the partial (or complete) lines.
2341 * We will check the request syntax, and also join multi-line
2342 * headers. An index of all the lines will be elaborated while
2343 * parsing.
2344 *
2345 * For the parsing, we use a 28 states FSM.
2346 *
2347 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002348 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002349 * req->data + msg->eoh = end of processed headers / start of current one
2350 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002351 * req->lr = first non-visited byte
2352 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002353 *
2354 * At end of parsing, we may perform a capture of the error (if any), and
2355 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2356 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2357 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002359
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002361 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002362 struct http_txn *txn = &s->txn;
2363 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002364 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002365
Willy Tarreau6bf17362009-02-24 10:48:35 +01002366 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2367 now_ms, __FUNCTION__,
2368 s,
2369 req,
2370 req->rex, req->wex,
2371 req->flags,
2372 req->l,
2373 req->analysers);
2374
Willy Tarreau52a0c602009-08-16 22:45:38 +02002375 /* we're speaking HTTP here, so let's speak HTTP to the client */
2376 s->srv_error = http_return_srv_error;
2377
Willy Tarreau83e3af02009-12-28 17:39:57 +01002378 /* There's a protected area at the end of the buffer for rewriting
2379 * purposes. We don't want to start to parse the request if the
2380 * protected area is affected, because we may have to move processed
2381 * data later, which is much more complicated.
2382 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002383 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002384 if ((txn->flags & TX_NOT_FIRST) &&
2385 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002386 req->r < req->lr ||
2387 req->r > req->data + req->size - global.tune.maxrewrite)) {
2388 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002389 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2390 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002391 /* some data has still not left the buffer, wake us once that's done */
2392 buffer_dont_connect(req);
2393 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2394 return 0;
2395 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002396 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002397 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002398 }
2399
Willy Tarreau065e8332010-01-08 00:30:20 +01002400 /* Note that we have the same problem with the response ; we
2401 * may want to send a redirect, error or anything which requires
2402 * some spare space. So we'll ensure that we have at least
2403 * maxrewrite bytes available in the response buffer before
2404 * processing that one. This will only affect pipelined
2405 * keep-alive requests.
2406 */
2407 if ((txn->flags & TX_NOT_FIRST) &&
2408 unlikely((s->rep->flags & BF_FULL) ||
2409 s->rep->r < s->rep->lr ||
2410 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2411 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002412 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2413 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002414 /* don't let a connection request be initiated */
2415 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002416 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002417 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002418 return 0;
2419 }
2420 }
2421
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002422 if (likely(req->lr < req->r))
2423 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002424 }
2425
Willy Tarreau59234e92008-11-30 23:51:27 +01002426 /* 1: we might have to print this header in debug mode */
2427 if (unlikely((global.mode & MODE_DEBUG) &&
2428 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002429 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002430 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002431 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002432
Willy Tarreau663308b2010-06-07 14:06:08 +02002433 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002434 eol = sol + msg->sl.rq.l;
2435 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002436
Willy Tarreau59234e92008-11-30 23:51:27 +01002437 sol += hdr_idx_first_pos(&txn->hdr_idx);
2438 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002439
Willy Tarreau59234e92008-11-30 23:51:27 +01002440 while (cur_idx) {
2441 eol = sol + txn->hdr_idx.v[cur_idx].len;
2442 debug_hdr("clihdr", s, sol, eol);
2443 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2444 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002445 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002446 }
2447
Willy Tarreau58f10d72006-12-04 02:26:12 +01002448
Willy Tarreau59234e92008-11-30 23:51:27 +01002449 /*
2450 * Now we quickly check if we have found a full valid request.
2451 * If not so, we check the FD and buffer states before leaving.
2452 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002453 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002454 * requests are checked first. When waiting for a second request
2455 * on a keep-alive session, if we encounter and error, close, t/o,
2456 * we note the error in the session flags but don't set any state.
2457 * Since the error will be noted there, it will not be counted by
2458 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002459 * Last, we may increase some tracked counters' http request errors on
2460 * the cases that are deliberately the client's fault. For instance,
2461 * a timeout or connection reset is not counted as an error. However
2462 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002463 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002464
Willy Tarreau655dce92009-11-08 13:10:58 +01002465 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002466 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002467 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002468 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002469 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002470 session_inc_http_req_ctr(s);
2471 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002472 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002473 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002474 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002475
Willy Tarreau59234e92008-11-30 23:51:27 +01002476 /* 1: Since we are in header mode, if there's no space
2477 * left for headers, we won't be able to free more
2478 * later, so the session will never terminate. We
2479 * must terminate it now.
2480 */
2481 if (unlikely(req->flags & BF_FULL)) {
2482 /* FIXME: check if URI is set and return Status
2483 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002484 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002485 session_inc_http_req_ctr(s);
2486 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002487 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002488 goto return_bad_req;
2489 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002490
Willy Tarreau59234e92008-11-30 23:51:27 +01002491 /* 2: have we encountered a read error ? */
2492 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002493 if (!(s->flags & SN_ERR_MASK))
2494 s->flags |= SN_ERR_CLICL;
2495
Willy Tarreaufcffa692010-01-10 14:21:19 +01002496 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002497 goto failed_keep_alive;
2498
Willy Tarreau59234e92008-11-30 23:51:27 +01002499 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002500 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002501 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002502 session_inc_http_err_ctr(s);
2503 }
2504
Willy Tarreau59234e92008-11-30 23:51:27 +01002505 msg->msg_state = HTTP_MSG_ERROR;
2506 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002507
Willy Tarreauda7ff642010-06-23 11:44:09 +02002508 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002509 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002510 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002511 if (s->listener->counters)
2512 s->listener->counters->failed_req++;
2513
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 if (!(s->flags & SN_FINST_MASK))
2515 s->flags |= SN_FINST_R;
2516 return 0;
2517 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002518
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 /* 3: has the read timeout expired ? */
2520 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002521 if (!(s->flags & SN_ERR_MASK))
2522 s->flags |= SN_ERR_CLITO;
2523
Willy Tarreaufcffa692010-01-10 14:21:19 +01002524 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002525 goto failed_keep_alive;
2526
Willy Tarreau59234e92008-11-30 23:51:27 +01002527 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002528 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002529 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002530 session_inc_http_err_ctr(s);
2531 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002532 txn->status = 408;
2533 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2534 msg->msg_state = HTTP_MSG_ERROR;
2535 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002536
Willy Tarreauda7ff642010-06-23 11:44:09 +02002537 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002538 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002539 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002540 if (s->listener->counters)
2541 s->listener->counters->failed_req++;
2542
Willy Tarreau59234e92008-11-30 23:51:27 +01002543 if (!(s->flags & SN_FINST_MASK))
2544 s->flags |= SN_FINST_R;
2545 return 0;
2546 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002547
Willy Tarreau59234e92008-11-30 23:51:27 +01002548 /* 4: have we encountered a close ? */
2549 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002550 if (!(s->flags & SN_ERR_MASK))
2551 s->flags |= SN_ERR_CLICL;
2552
Willy Tarreaufcffa692010-01-10 14:21:19 +01002553 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002554 goto failed_keep_alive;
2555
Willy Tarreau4076a152009-04-02 15:18:36 +02002556 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002557 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002558 txn->status = 400;
2559 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2560 msg->msg_state = HTTP_MSG_ERROR;
2561 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002562
Willy Tarreauda7ff642010-06-23 11:44:09 +02002563 session_inc_http_err_ctr(s);
2564 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002565 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002566 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002567 if (s->listener->counters)
2568 s->listener->counters->failed_req++;
2569
Willy Tarreau59234e92008-11-30 23:51:27 +01002570 if (!(s->flags & SN_FINST_MASK))
2571 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002572 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002573 }
2574
Willy Tarreau520d95e2009-09-19 21:04:57 +02002575 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002576 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002577 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002578
Willy Tarreaufcffa692010-01-10 14:21:19 +01002579 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2580 /* If the client starts to talk, let's fall back to
2581 * request timeout processing.
2582 */
2583 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002584 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002585 }
2586
Willy Tarreau59234e92008-11-30 23:51:27 +01002587 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002588 if (!tick_isset(req->analyse_exp)) {
2589 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2590 (txn->flags & TX_WAIT_NEXT_RQ) &&
2591 tick_isset(s->be->timeout.httpka))
2592 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2593 else
2594 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2595 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002596
Willy Tarreau59234e92008-11-30 23:51:27 +01002597 /* we're not ready yet */
2598 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002599
2600 failed_keep_alive:
2601 /* Here we process low-level errors for keep-alive requests. In
2602 * short, if the request is not the first one and it experiences
2603 * a timeout, read error or shutdown, we just silently close so
2604 * that the client can try again.
2605 */
2606 txn->status = 0;
2607 msg->msg_state = HTTP_MSG_RQBEFORE;
2608 req->analysers = 0;
2609 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002610 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002611 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002612 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002614
Willy Tarreaud787e662009-07-07 10:14:51 +02002615 /* OK now we have a complete HTTP request with indexed headers. Let's
2616 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002617 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2618 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2619 * points to the CRLF of the request line. req->lr points to the first
2620 * byte after the last LF. msg->col and msg->sov point to the first
2621 * byte of data. msg->eol cannot be trusted because it may have been
2622 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002623 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002624
Willy Tarreauda7ff642010-06-23 11:44:09 +02002625 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002626 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2627
Willy Tarreaub16a5742010-01-10 14:46:16 +01002628 if (txn->flags & TX_WAIT_NEXT_RQ) {
2629 /* kill the pending keep-alive timeout */
2630 txn->flags &= ~TX_WAIT_NEXT_RQ;
2631 req->analyse_exp = TICK_ETERNITY;
2632 }
2633
2634
Willy Tarreaud787e662009-07-07 10:14:51 +02002635 /* Maybe we found in invalid header name while we were configured not
2636 * to block on that, so we have to capture it now.
2637 */
2638 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002639 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002640
Willy Tarreau59234e92008-11-30 23:51:27 +01002641 /*
2642 * 1: identify the method
2643 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002644 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002645
2646 /* we can make use of server redirect on GET and HEAD */
2647 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2648 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002649
Willy Tarreau59234e92008-11-30 23:51:27 +01002650 /*
2651 * 2: check if the URI matches the monitor_uri.
2652 * We have to do this for every request which gets in, because
2653 * the monitor-uri is defined by the frontend.
2654 */
2655 if (unlikely((s->fe->monitor_uri_len != 0) &&
2656 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002657 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002658 s->fe->monitor_uri,
2659 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002660 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002661 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002662 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002663 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002664
Willy Tarreau59234e92008-11-30 23:51:27 +01002665 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002666
Willy Tarreau59234e92008-11-30 23:51:27 +01002667 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002668 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2669 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002670
Willy Tarreau59234e92008-11-30 23:51:27 +01002671 ret = acl_pass(ret);
2672 if (cond->pol == ACL_COND_UNLESS)
2673 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002674
Willy Tarreau59234e92008-11-30 23:51:27 +01002675 if (ret) {
2676 /* we fail this request, let's return 503 service unavail */
2677 txn->status = 503;
2678 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2679 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002680 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002681 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002682
Willy Tarreau59234e92008-11-30 23:51:27 +01002683 /* nothing to fail, let's reply normaly */
2684 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002685 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002686 goto return_prx_cond;
2687 }
2688
2689 /*
2690 * 3: Maybe we have to copy the original REQURI for the logs ?
2691 * Note: we cannot log anymore if the request has been
2692 * classified as invalid.
2693 */
2694 if (unlikely(s->logs.logwait & LW_REQ)) {
2695 /* we have a complete HTTP request that we must log */
2696 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2697 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002698
Willy Tarreau59234e92008-11-30 23:51:27 +01002699 if (urilen >= REQURI_LEN)
2700 urilen = REQURI_LEN - 1;
2701 memcpy(txn->uri, &req->data[msg->som], urilen);
2702 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002703
Willy Tarreau59234e92008-11-30 23:51:27 +01002704 if (!(s->logs.logwait &= ~LW_REQ))
2705 s->do_log(s);
2706 } else {
2707 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002708 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002709 }
Willy Tarreau06619262006-12-17 08:37:22 +01002710
Willy Tarreau59234e92008-11-30 23:51:27 +01002711 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002712 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2713 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002714
Willy Tarreau5b154472009-12-21 20:11:07 +01002715 /* ... and check if the request is HTTP/1.1 or above */
2716 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002717 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2718 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2719 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002720 txn->flags |= TX_REQ_VER_11;
2721
2722 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002723 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002724
Willy Tarreau88d349d2010-01-25 12:15:43 +01002725 /* if the frontend has "option http-use-proxy-header", we'll check if
2726 * we have what looks like a proxied connection instead of a connection,
2727 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2728 * Note that this is *not* RFC-compliant, however browsers and proxies
2729 * happen to do that despite being non-standard :-(
2730 * We consider that a request not beginning with either '/' or '*' is
2731 * a proxied connection, which covers both "scheme://location" and
2732 * CONNECT ip:port.
2733 */
2734 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2735 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2736 txn->flags |= TX_USE_PX_CONN;
2737
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002738 /* transfer length unknown*/
2739 txn->flags &= ~TX_REQ_XFER_LEN;
2740
Willy Tarreau59234e92008-11-30 23:51:27 +01002741 /* 5: we may need to capture headers */
2742 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002743 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002744 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002745
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002746 /* 6: determine the transfer-length.
2747 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2748 * the presence of a message-body in a REQUEST and its transfer length
2749 * must be determined that way (in order of precedence) :
2750 * 1. The presence of a message-body in a request is signaled by the
2751 * inclusion of a Content-Length or Transfer-Encoding header field
2752 * in the request's header fields. When a request message contains
2753 * both a message-body of non-zero length and a method that does
2754 * not define any semantics for that request message-body, then an
2755 * origin server SHOULD either ignore the message-body or respond
2756 * with an appropriate error message (e.g., 413). A proxy or
2757 * gateway, when presented the same request, SHOULD either forward
2758 * the request inbound with the message- body or ignore the
2759 * message-body when determining a response.
2760 *
2761 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2762 * and the "chunked" transfer-coding (Section 6.2) is used, the
2763 * transfer-length is defined by the use of this transfer-coding.
2764 * If a Transfer-Encoding header field is present and the "chunked"
2765 * transfer-coding is not present, the transfer-length is defined
2766 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002767 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002768 * 3. If a Content-Length header field is present, its decimal value in
2769 * OCTETs represents both the entity-length and the transfer-length.
2770 * If a message is received with both a Transfer-Encoding header
2771 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002772 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002773 * 4. By the server closing the connection. (Closing the connection
2774 * cannot be used to indicate the end of a request body, since that
2775 * would leave no possibility for the server to send back a response.)
2776 *
2777 * Whenever a transfer-coding is applied to a message-body, the set of
2778 * transfer-codings MUST include "chunked", unless the message indicates
2779 * it is terminated by closing the connection. When the "chunked"
2780 * transfer-coding is used, it MUST be the last transfer-coding applied
2781 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002782 */
2783
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002784 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002785 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002786 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002787 while ((txn->flags & TX_REQ_VER_11) &&
2788 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002789 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2790 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2791 else if (txn->flags & TX_REQ_TE_CHNK) {
2792 /* bad transfer-encoding (chunked followed by something else) */
2793 use_close_only = 1;
2794 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2795 break;
2796 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002797 }
2798
Willy Tarreau32b47f42009-10-18 20:55:02 +02002799 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002800 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002801 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2802 signed long long cl;
2803
2804 if (!ctx.vlen)
2805 goto return_bad_req;
2806
2807 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2808 goto return_bad_req; /* parse failure */
2809
2810 if (cl < 0)
2811 goto return_bad_req;
2812
Willy Tarreau124d9912011-03-01 20:30:48 +01002813 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl))
Willy Tarreau32b47f42009-10-18 20:55:02 +02002814 goto return_bad_req; /* already specified, was different */
2815
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002816 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002817 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002818 }
2819
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002820 /* bodyless requests have a known length */
2821 if (!use_close_only)
2822 txn->flags |= TX_REQ_XFER_LEN;
2823
Willy Tarreaud787e662009-07-07 10:14:51 +02002824 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002825 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002826 req->analyse_exp = TICK_ETERNITY;
2827 return 1;
2828
2829 return_bad_req:
2830 /* We centralize bad requests processing here */
2831 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2832 /* we detected a parsing error. We want to archive this request
2833 * in the dedicated proxy area for later troubleshooting.
2834 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002835 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002836 }
2837
2838 txn->req.msg_state = HTTP_MSG_ERROR;
2839 txn->status = 400;
2840 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002841
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002842 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002843 if (s->listener->counters)
2844 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002845
2846 return_prx_cond:
2847 if (!(s->flags & SN_ERR_MASK))
2848 s->flags |= SN_ERR_PRXCOND;
2849 if (!(s->flags & SN_FINST_MASK))
2850 s->flags |= SN_FINST_R;
2851
2852 req->analysers = 0;
2853 req->analyse_exp = TICK_ETERNITY;
2854 return 0;
2855}
2856
Cyril Bonté70be45d2010-10-12 00:14:35 +02002857/* We reached the stats page through a POST request.
2858 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002859 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002860 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002861int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002862{
Cyril Bonté70be45d2010-10-12 00:14:35 +02002863 struct proxy *px;
2864 struct server *sv;
2865
2866 char *backend = NULL;
2867 int action = 0;
2868
2869 char *first_param, *cur_param, *next_param, *end_params;
2870
2871 first_param = req->data + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002872 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002873
2874 cur_param = next_param = end_params;
2875
Cyril Bonté23b39d92011-02-10 22:54:44 +01002876 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002877 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002878 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002879 return 1;
2880 }
2881 else if (end_params > req->data + req->l) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002882 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002883 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002884 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002885 }
2886
2887 *end_params = '\0';
2888
Willy Tarreau295a8372011-03-10 11:25:07 +01002889 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002890
2891 /*
2892 * Parse the parameters in reverse order to only store the last value.
2893 * From the html form, the backend and the action are at the end.
2894 */
2895 while (cur_param > first_param) {
2896 char *key, *value;
2897
2898 cur_param--;
2899 if ((*cur_param == '&') || (cur_param == first_param)) {
2900 /* Parse the key */
2901 key = cur_param;
2902 if (cur_param != first_param) {
2903 /* delimit the string for the next loop */
2904 *key++ = '\0';
2905 }
2906
2907 /* Parse the value */
2908 value = key;
2909 while (*value != '\0' && *value != '=') {
2910 value++;
2911 }
2912 if (*value == '=') {
2913 /* Ok, a value is found, we can mark the end of the key */
2914 *value++ = '\0';
2915 }
2916
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002917 if (!url_decode(key) || !url_decode(value))
2918 break;
2919
Cyril Bonté70be45d2010-10-12 00:14:35 +02002920 /* Now we can check the key to see what to do */
2921 if (!backend && strcmp(key, "b") == 0) {
2922 backend = value;
2923 }
2924 else if (!action && strcmp(key, "action") == 0) {
2925 if (strcmp(value, "disable") == 0) {
2926 action = 1;
2927 }
2928 else if (strcmp(value, "enable") == 0) {
2929 action = 2;
2930 } else {
2931 /* unknown action, no need to continue */
2932 break;
2933 }
2934 }
2935 else if (strcmp(key, "s") == 0) {
2936 if (backend && action && get_backend_server(backend, value, &px, &sv)) {
2937 switch (action) {
2938 case 1:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002939 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002940 /* Not already in maintenance, we can change the server state */
2941 sv->state |= SRV_MAINTAIN;
2942 set_server_down(sv);
Willy Tarreau295a8372011-03-10 11:25:07 +01002943 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002944 }
2945 break;
2946 case 2:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002947 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002948 /* Already in maintenance, we can change the server state */
2949 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002950 sv->health = sv->rise; /* up, but will fall down at first failure */
Willy Tarreau295a8372011-03-10 11:25:07 +01002951 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002952 }
2953 break;
2954 }
2955 }
2956 }
2957 next_param = cur_param;
2958 }
2959 }
Cyril Bonté23b39d92011-02-10 22:54:44 +01002960 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002961}
2962
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002963/* returns a pointer to the first rule which forbids access (deny or http_auth),
2964 * or NULL if everything's OK.
2965 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002966static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002967http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2968{
Willy Tarreauff011f22011-01-06 17:51:27 +01002969 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002970
Willy Tarreauff011f22011-01-06 17:51:27 +01002971 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002972 int ret = 1;
2973
Willy Tarreauff011f22011-01-06 17:51:27 +01002974 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002975 continue;
2976
2977 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002978 if (rule->cond) {
2979 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002980 ret = acl_pass(ret);
2981
Willy Tarreauff011f22011-01-06 17:51:27 +01002982 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002983 ret = !ret;
2984 }
2985
2986 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002987 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002988 return NULL; /* no problem */
2989 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002990 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002991 }
2992 }
2993 return NULL;
2994}
2995
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002996/* This stream analyser runs all HTTP request processing which is common to
2997 * frontends and backends, which means blocking ACLs, filters, connection-close,
2998 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002999 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003000 * either needs more data or wants to immediately abort the request (eg: deny,
3001 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003002 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003003int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003004{
Willy Tarreaud787e662009-07-07 10:14:51 +02003005 struct http_txn *txn = &s->txn;
3006 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003007 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003008 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003009 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003010 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003011 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003012
Willy Tarreau655dce92009-11-08 13:10:58 +01003013 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003014 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003015 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003016 return 0;
3017 }
3018
Willy Tarreau3a816292009-07-07 10:55:49 +02003019 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003020 req->analyse_exp = TICK_ETERNITY;
3021
3022 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3023 now_ms, __FUNCTION__,
3024 s,
3025 req,
3026 req->rex, req->wex,
3027 req->flags,
3028 req->l,
3029 req->analysers);
3030
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003031 /* first check whether we have some ACLs set to block this request */
3032 list_for_each_entry(cond, &px->block_cond, list) {
3033 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003034
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003035 ret = acl_pass(ret);
3036 if (cond->pol == ACL_COND_UNLESS)
3037 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003038
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003039 if (ret) {
3040 txn->status = 403;
3041 /* let's log the request time */
3042 s->logs.tv_request = now;
3043 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003044 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003045 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003046 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003047 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003048
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003049 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01003050 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003051
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003052 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003053 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003054 do_stats = stats_check_uri(s->rep->prod, txn, px);
3055 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01003056 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 +01003057 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003058 else
3059 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003060
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003061 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01003062 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003063 txn->status = 403;
3064 s->logs.tv_request = now;
3065 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003066 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003067 s->fe->fe_counters.denied_req++;
3068 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3069 s->be->be_counters.denied_req++;
3070 if (s->listener->counters)
3071 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003072 goto return_prx_cond;
3073 }
3074
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003075 /* try headers filters */
3076 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003077 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003078 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003079
Willy Tarreau59234e92008-11-30 23:51:27 +01003080 /* has the request been denied ? */
3081 if (txn->flags & TX_CLDENY) {
3082 /* no need to go further */
3083 txn->status = 403;
3084 /* let's log the request time */
3085 s->logs.tv_request = now;
3086 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003087 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003088 goto return_prx_cond;
3089 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003090
3091 /* When a connection is tarpitted, we use the tarpit timeout,
3092 * which may be the same as the connect timeout if unspecified.
3093 * If unset, then set it to zero because we really want it to
3094 * eventually expire. We build the tarpit as an analyser.
3095 */
3096 if (txn->flags & TX_CLTARPIT) {
3097 buffer_erase(s->req);
3098 /* wipe the request out so that we can drop the connection early
3099 * if the client closes first.
3100 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003101 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003102 req->analysers = 0; /* remove switching rules etc... */
3103 req->analysers |= AN_REQ_HTTP_TARPIT;
3104 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3105 if (!req->analyse_exp)
3106 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003107 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003108 return 1;
3109 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003110 }
Willy Tarreau06619262006-12-17 08:37:22 +01003111
Willy Tarreau5b154472009-12-21 20:11:07 +01003112 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3113 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003114 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3115 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003116 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3117 * avoid to redo the same work if FE and BE have the same settings (common).
3118 * The method consists in checking if options changed between the two calls
3119 * (implying that either one is non-null, or one of them is non-null and we
3120 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003121 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003122
Willy Tarreaudc008c52010-02-01 16:20:08 +01003123 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3124 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3125 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3126 (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 +01003127 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003128
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003129 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3130 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003131 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003132 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3133 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003134 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003135 tmp = TX_CON_WANT_CLO;
3136
Willy Tarreau5b154472009-12-21 20:11:07 +01003137 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3138 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003139
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003140 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3141 /* parse the Connection header and possibly clean it */
3142 int to_del = 0;
3143 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003144 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3145 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003146 to_del |= 2; /* remove "keep-alive" */
3147 if (!(txn->flags & TX_REQ_VER_11))
3148 to_del |= 1; /* remove "close" */
3149 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003150 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003151
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003152 /* check if client or config asks for explicit close in KAL/SCL */
3153 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3154 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3155 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
3156 (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 +01003157 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003158 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
3159 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003160 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3161 }
Willy Tarreau78599912009-10-17 20:12:21 +02003162
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003163 /* we can be blocked here because the request needs to be authenticated,
3164 * either to pass or to access stats.
3165 */
Willy Tarreauff011f22011-01-06 17:51:27 +01003166 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003167 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01003168 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003169
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003170 if (!realm)
3171 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3172
Willy Tarreau844a7e72010-01-31 21:46:18 +01003173 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003174 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3175 txn->status = 401;
3176 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003177 /* on 401 we still count one error, because normal browsing
3178 * won't significantly increase the counter but brute force
3179 * attempts will.
3180 */
3181 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003182 goto return_prx_cond;
3183 }
3184
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003185 /* add request headers from the rule sets in the same order */
3186 list_for_each_entry(wl, &px->req_add, list) {
3187 if (wl->cond) {
3188 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
3189 ret = acl_pass(ret);
3190 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3191 ret = !ret;
3192 if (!ret)
3193 continue;
3194 }
3195
3196 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
3197 goto return_bad_req;
3198 }
3199
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003200 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003201 struct stats_admin_rule *stats_admin_rule;
3202
3203 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003204 * FIXME!!! that one is rather dangerous, we want to
3205 * make it follow standard rules (eg: clear req->analysers).
3206 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003207
Cyril Bonté474be412010-10-12 00:14:36 +02003208 /* now check whether we have some admin rules for this request */
3209 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3210 int ret = 1;
3211
3212 if (stats_admin_rule->cond) {
3213 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
3214 ret = acl_pass(ret);
3215 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3216 ret = !ret;
3217 }
3218
3219 if (ret) {
3220 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01003221 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02003222 break;
3223 }
3224 }
3225
Cyril Bonté70be45d2010-10-12 00:14:35 +02003226 /* Was the status page requested with a POST ? */
3227 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003228 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003229 if (msg->msg_state < HTTP_MSG_100_SENT) {
3230 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3231 * send an HTTP/1.1 100 Continue intermediate response.
3232 */
3233 if (txn->flags & TX_REQ_VER_11) {
3234 struct hdr_ctx ctx;
3235 ctx.idx = 0;
3236 /* Expect is allowed in 1.1, look for it */
3237 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3238 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3239 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3240 }
3241 }
3242 msg->msg_state = HTTP_MSG_100_SENT;
3243 s->logs.tv_request = now; /* update the request timer to reflect full request */
3244 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003245 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003246 /* we need more data */
3247 req->analysers |= an_bit;
3248 buffer_dont_connect(req);
3249 return 0;
3250 }
Cyril Bonté474be412010-10-12 00:14:36 +02003251 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003252 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003253 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003254 }
3255
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003256 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003257 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003258 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003259 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003260 s->rep->prod->applet.private = s;
3261 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003262 req->analysers = 0;
3263
3264 return 0;
3265
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003266 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003267
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003268 /* check whether we have some ACLs set to redirect this request */
3269 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003270 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003271
Willy Tarreauf285f542010-01-03 20:03:03 +01003272 if (rule->cond) {
3273 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3274 ret = acl_pass(ret);
3275 if (rule->cond->pol == ACL_COND_UNLESS)
3276 ret = !ret;
3277 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003278
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003279 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003280 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003281 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003282
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003283 /* build redirect message */
3284 switch(rule->code) {
3285 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003286 msg_fmt = HTTP_303;
3287 break;
3288 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003289 msg_fmt = HTTP_301;
3290 break;
3291 case 302:
3292 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003293 msg_fmt = HTTP_302;
3294 break;
3295 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003296
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003297 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003298 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003299
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003300 switch(rule->type) {
3301 case REDIRECT_TYPE_PREFIX: {
3302 const char *path;
3303 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003304
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003305 path = http_get_path(txn);
3306 /* build message using path */
3307 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003308 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003309 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3310 int qs = 0;
3311 while (qs < pathlen) {
3312 if (path[qs] == '?') {
3313 pathlen = qs;
3314 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003315 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003316 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003317 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003318 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003319 } else {
3320 path = "/";
3321 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003322 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003323
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003324 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003325 goto return_bad_req;
3326
3327 /* add prefix. Note that if prefix == "/", we don't want to
3328 * add anything, otherwise it makes it hard for the user to
3329 * configure a self-redirection.
3330 */
3331 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003332 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3333 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003334 }
3335
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003336 /* add path */
3337 memcpy(rdr.str + rdr.len, path, pathlen);
3338 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003339
3340 /* append a slash at the end of the location is needed and missing */
3341 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3342 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3343 if (rdr.len > rdr.size - 5)
3344 goto return_bad_req;
3345 rdr.str[rdr.len] = '/';
3346 rdr.len++;
3347 }
3348
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003349 break;
3350 }
3351 case REDIRECT_TYPE_LOCATION:
3352 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003353 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003354 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003355
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003356 /* add location */
3357 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3358 rdr.len += rule->rdr_len;
3359 break;
3360 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003361
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003362 if (rule->cookie_len) {
3363 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3364 rdr.len += 14;
3365 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3366 rdr.len += rule->cookie_len;
3367 memcpy(rdr.str + rdr.len, "\r\n", 2);
3368 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003369 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003370
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003371 /* add end of headers and the keep-alive/close status.
3372 * We may choose to set keep-alive if the Location begins
3373 * with a slash, because the client will come back to the
3374 * same server.
3375 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003376 txn->status = rule->code;
3377 /* let's log the request time */
3378 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003379
3380 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3381 (txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau124d9912011-03-01 20:30:48 +01003382 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003383 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3384 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3385 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003386 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003387 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3388 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3389 rdr.len += 30;
3390 } else {
3391 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3392 rdr.len += 24;
3393 }
Willy Tarreau75661452010-01-10 10:35:01 +01003394 }
3395 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3396 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003397 buffer_write(req->prod->ob, rdr.str, rdr.len);
3398 /* "eat" the request */
3399 buffer_ignore(req, msg->sov - msg->som);
3400 msg->som = msg->sov;
3401 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003402 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3403 txn->req.msg_state = HTTP_MSG_CLOSED;
3404 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003405 break;
3406 } else {
3407 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003408 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3409 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3410 rdr.len += 29;
3411 } else {
3412 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3413 rdr.len += 23;
3414 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003415 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003416 goto return_prx_cond;
3417 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003418 }
3419 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003420
Willy Tarreau2be39392010-01-03 17:24:51 +01003421 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3422 * If this happens, then the data will not come immediately, so we must
3423 * send all what we have without waiting. Note that due to the small gain
3424 * in waiting for the body of the request, it's easier to simply put the
3425 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3426 * itself once used.
3427 */
3428 req->flags |= BF_SEND_DONTWAIT;
3429
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003430 /* that's OK for us now, let's move on to next analysers */
3431 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003432
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003433 return_bad_req:
3434 /* We centralize bad requests processing here */
3435 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3436 /* we detected a parsing error. We want to archive this request
3437 * in the dedicated proxy area for later troubleshooting.
3438 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003439 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003440 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003441
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003442 txn->req.msg_state = HTTP_MSG_ERROR;
3443 txn->status = 400;
3444 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003445
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003446 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003447 if (s->listener->counters)
3448 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003449
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003450 return_prx_cond:
3451 if (!(s->flags & SN_ERR_MASK))
3452 s->flags |= SN_ERR_PRXCOND;
3453 if (!(s->flags & SN_FINST_MASK))
3454 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003455
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003456 req->analysers = 0;
3457 req->analyse_exp = TICK_ETERNITY;
3458 return 0;
3459}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003460
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003461/* This function performs all the processing enabled for the current request.
3462 * It returns 1 if the processing can continue on next analysers, or zero if it
3463 * needs more data, encounters an error, or wants to immediately abort the
3464 * request. It relies on buffers flags, and updates s->req->analysers.
3465 */
3466int http_process_request(struct session *s, struct buffer *req, int an_bit)
3467{
3468 struct http_txn *txn = &s->txn;
3469 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003470
Willy Tarreau655dce92009-11-08 13:10:58 +01003471 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003472 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003473 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003474 return 0;
3475 }
3476
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003477 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3478 now_ms, __FUNCTION__,
3479 s,
3480 req,
3481 req->rex, req->wex,
3482 req->flags,
3483 req->l,
3484 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003485
Willy Tarreau59234e92008-11-30 23:51:27 +01003486 /*
3487 * Right now, we know that we have processed the entire headers
3488 * and that unwanted requests have been filtered out. We can do
3489 * whatever we want with the remaining request. Also, now we
3490 * may have separate values for ->fe, ->be.
3491 */
Willy Tarreau06619262006-12-17 08:37:22 +01003492
Willy Tarreau59234e92008-11-30 23:51:27 +01003493 /*
3494 * If HTTP PROXY is set we simply get remote server address
3495 * parsing incoming request.
3496 */
3497 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau957c0a52011-03-03 17:42:23 +01003498 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 +01003499 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003500
Willy Tarreau59234e92008-11-30 23:51:27 +01003501 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003502 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003503 * Note that doing so might move headers in the request, but
3504 * the fields will stay coherent and the URI will not move.
3505 * This should only be performed in the backend.
3506 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003507 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003508 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3509 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003510
Willy Tarreau59234e92008-11-30 23:51:27 +01003511 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003512 * 8: the appsession cookie was looked up very early in 1.2,
3513 * so let's do the same now.
3514 */
3515
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003516 /* It needs to look into the URI unless persistence must be ignored */
3517 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003518 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003519 }
3520
3521 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003522 * 9: add X-Forwarded-For if either the frontend or the backend
3523 * asks for it.
3524 */
3525 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003526 struct hdr_ctx ctx = { .idx = 0 };
3527
3528 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3529 http_find_header2("X-Forwarded-For", 15, txn->req.sol, &txn->hdr_idx, &ctx)) {
3530 /* The header is set to be added only if none is present
3531 * and we found it, so don't do anything.
3532 */
3533 }
3534 else if (s->req->prod->addr.c.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003535 /* Add an X-Forwarded-For header unless the source IP is
3536 * in the 'except' network range.
3537 */
3538 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003539 (((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 +01003540 != s->fe->except_net.s_addr) &&
3541 (!s->be->except_mask.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003542 (((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 +01003543 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003544 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003545 unsigned char *pn;
Willy Tarreau957c0a52011-03-03 17:42:23 +01003546 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003547
3548 /* Note: we rely on the backend to get the header name to be used for
3549 * x-forwarded-for, because the header is really meant for the backends.
3550 * However, if the backend did not specify any option, we have to rely
3551 * on the frontend's header name.
3552 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003553 if (s->be->fwdfor_hdr_len) {
3554 len = s->be->fwdfor_hdr_len;
3555 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003556 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003557 len = s->fe->fwdfor_hdr_len;
3558 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003559 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003560 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003561
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003562 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003563 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003564 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003565 }
3566 }
Willy Tarreau957c0a52011-03-03 17:42:23 +01003567 else if (s->req->prod->addr.c.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003568 /* FIXME: for the sake of completeness, we should also support
3569 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003570 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003571 int len;
3572 char pn[INET6_ADDRSTRLEN];
3573 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003574 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003575 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003576
Willy Tarreau59234e92008-11-30 23:51:27 +01003577 /* Note: we rely on the backend to get the header name to be used for
3578 * x-forwarded-for, because the header is really meant for the backends.
3579 * However, if the backend did not specify any option, we have to rely
3580 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003581 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003582 if (s->be->fwdfor_hdr_len) {
3583 len = s->be->fwdfor_hdr_len;
3584 memcpy(trash, s->be->fwdfor_hdr_name, len);
3585 } else {
3586 len = s->fe->fwdfor_hdr_len;
3587 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003588 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003589 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003590
Willy Tarreau59234e92008-11-30 23:51:27 +01003591 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003592 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003593 goto return_bad_req;
3594 }
3595 }
3596
3597 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003598 * 10: add X-Original-To if either the frontend or the backend
3599 * asks for it.
3600 */
3601 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3602
3603 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau957c0a52011-03-03 17:42:23 +01003604 if (s->req->prod->addr.c.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003605 /* Add an X-Original-To header unless the destination IP is
3606 * in the 'except' network range.
3607 */
3608 if (!(s->flags & SN_FRT_ADDR_SET))
3609 get_frt_addr(s);
3610
Willy Tarreau957c0a52011-03-03 17:42:23 +01003611 if (s->req->prod->addr.c.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003612 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003613 (((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 +02003614 != s->fe->except_to.s_addr) &&
3615 (!s->be->except_mask_to.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003616 (((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 +02003617 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003618 int len;
3619 unsigned char *pn;
Willy Tarreau957c0a52011-03-03 17:42:23 +01003620 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003621
3622 /* Note: we rely on the backend to get the header name to be used for
3623 * x-original-to, because the header is really meant for the backends.
3624 * However, if the backend did not specify any option, we have to rely
3625 * on the frontend's header name.
3626 */
3627 if (s->be->orgto_hdr_len) {
3628 len = s->be->orgto_hdr_len;
3629 memcpy(trash, s->be->orgto_hdr_name, len);
3630 } else {
3631 len = s->fe->orgto_hdr_len;
3632 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003633 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003634 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3635
3636 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003637 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003638 goto return_bad_req;
3639 }
3640 }
3641 }
3642
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003643 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3644 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003645 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003646 unsigned int want_flags = 0;
3647
3648 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003649 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3650 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3651 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003652 want_flags |= TX_CON_CLO_SET;
3653 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003654 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3655 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3656 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003657 want_flags |= TX_CON_KAL_SET;
3658 }
3659
3660 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3661 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003662 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003663
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003664
Willy Tarreau522d6c02009-12-06 18:49:18 +01003665 /* If we have no server assigned yet and we're balancing on url_param
3666 * with a POST request, we may be interested in checking the body for
3667 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003668 */
3669 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3670 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003671 s->be->url_param_post_limit != 0 &&
Willy Tarreau61a21a32011-03-01 20:35:49 +01003672 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003673 buffer_dont_connect(req);
3674 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003675 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003676
Willy Tarreaud98cf932009-12-27 22:54:55 +01003677 if (txn->flags & TX_REQ_XFER_LEN)
3678 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003679
Willy Tarreau59234e92008-11-30 23:51:27 +01003680 /*************************************************************
3681 * OK, that's finished for the headers. We have done what we *
3682 * could. Let's switch to the DATA state. *
3683 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003684 req->analyse_exp = TICK_ETERNITY;
3685 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003686
Willy Tarreau59234e92008-11-30 23:51:27 +01003687 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003688 /* OK let's go on with the BODY now */
3689 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003690
Willy Tarreau59234e92008-11-30 23:51:27 +01003691 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003692 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003693 /* we detected a parsing error. We want to archive this request
3694 * in the dedicated proxy area for later troubleshooting.
3695 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003696 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003697 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003698
Willy Tarreau59234e92008-11-30 23:51:27 +01003699 txn->req.msg_state = HTTP_MSG_ERROR;
3700 txn->status = 400;
3701 req->analysers = 0;
3702 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003703
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003704 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003705 if (s->listener->counters)
3706 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003707
Willy Tarreau59234e92008-11-30 23:51:27 +01003708 if (!(s->flags & SN_ERR_MASK))
3709 s->flags |= SN_ERR_PRXCOND;
3710 if (!(s->flags & SN_FINST_MASK))
3711 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003712 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003713}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003714
Willy Tarreau60b85b02008-11-30 23:28:40 +01003715/* This function is an analyser which processes the HTTP tarpit. It always
3716 * returns zero, at the beginning because it prevents any other processing
3717 * from occurring, and at the end because it terminates the request.
3718 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003719int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003720{
3721 struct http_txn *txn = &s->txn;
3722
3723 /* This connection is being tarpitted. The CLIENT side has
3724 * already set the connect expiration date to the right
3725 * timeout. We just have to check that the client is still
3726 * there and that the timeout has not expired.
3727 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003728 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003729 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3730 !tick_is_expired(req->analyse_exp, now_ms))
3731 return 0;
3732
3733 /* We will set the queue timer to the time spent, just for
3734 * logging purposes. We fake a 500 server error, so that the
3735 * attacker will not suspect his connection has been tarpitted.
3736 * It will not cause trouble to the logs because we can exclude
3737 * the tarpitted connections by filtering on the 'PT' status flags.
3738 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003739 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3740
3741 txn->status = 500;
3742 if (req->flags != BF_READ_ERROR)
3743 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3744
3745 req->analysers = 0;
3746 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003747
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003748 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003749 if (s->listener->counters)
3750 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003751
Willy Tarreau60b85b02008-11-30 23:28:40 +01003752 if (!(s->flags & SN_ERR_MASK))
3753 s->flags |= SN_ERR_PRXCOND;
3754 if (!(s->flags & SN_FINST_MASK))
3755 s->flags |= SN_FINST_T;
3756 return 0;
3757}
3758
Willy Tarreaud34af782008-11-30 23:36:37 +01003759/* This function is an analyser which processes the HTTP request body. It looks
3760 * for parameters to be used for the load balancing algorithm (url_param). It
3761 * must only be called after the standard HTTP request processing has occurred,
3762 * because it expects the request to be parsed. It returns zero if it needs to
3763 * read more data, or 1 once it has completed its analysis.
3764 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003765int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003766{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003767 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003768 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003769 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003770
3771 /* We have to parse the HTTP request body to find any required data.
3772 * "balance url_param check_post" should have been the only way to get
3773 * into this. We were brought here after HTTP header analysis, so all
3774 * related structures are ready.
3775 */
3776
Willy Tarreau522d6c02009-12-06 18:49:18 +01003777 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3778 goto missing_data;
3779
3780 if (msg->msg_state < HTTP_MSG_100_SENT) {
3781 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3782 * send an HTTP/1.1 100 Continue intermediate response.
3783 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003784 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003785 struct hdr_ctx ctx;
3786 ctx.idx = 0;
3787 /* Expect is allowed in 1.1, look for it */
3788 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3789 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3790 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3791 }
3792 }
3793 msg->msg_state = HTTP_MSG_100_SENT;
3794 }
3795
3796 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003797 /* we have msg->col and msg->sov which both point to the first
3798 * byte of message body. msg->som still points to the beginning
3799 * of the message. We must save the body in req->lr because it
3800 * survives buffer re-alignments.
3801 */
3802 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003803 if (txn->flags & TX_REQ_TE_CHNK)
3804 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3805 else
3806 msg->msg_state = HTTP_MSG_DATA;
3807 }
3808
3809 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003810 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003811 * set ->sov and ->lr to point to the body and switch to DATA or
3812 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003813 */
3814 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003815
Willy Tarreau115acb92009-12-26 13:56:06 +01003816 if (!ret)
3817 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003818 else if (ret < 0) {
3819 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003820 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003821 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003822 }
3823
Willy Tarreaud98cf932009-12-27 22:54:55 +01003824 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003825 * We have the first non-header byte in msg->col, which is either the
3826 * beginning of the chunk size or of the data. The first data byte is in
3827 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3828 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003829 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003830
Willy Tarreau124d9912011-03-01 20:30:48 +01003831 if (msg->body_len < limit)
3832 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003833
Willy Tarreau7c96f672009-12-27 22:47:25 +01003834 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003835 goto http_end;
3836
3837 missing_data:
3838 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003839 if (req->flags & BF_FULL) {
3840 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003841 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003842 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003843
Willy Tarreau522d6c02009-12-06 18:49:18 +01003844 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3845 txn->status = 408;
3846 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003847
3848 if (!(s->flags & SN_ERR_MASK))
3849 s->flags |= SN_ERR_CLITO;
3850 if (!(s->flags & SN_FINST_MASK))
3851 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003852 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003853 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003854
3855 /* we get here if we need to wait for more data */
3856 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003857 /* Not enough data. We'll re-use the http-request
3858 * timeout here. Ideally, we should set the timeout
3859 * relative to the accept() date. We just set the
3860 * request timeout once at the beginning of the
3861 * request.
3862 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003863 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003864 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003865 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003866 return 0;
3867 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003868
3869 http_end:
3870 /* The situation will not evolve, so let's give up on the analysis. */
3871 s->logs.tv_request = now; /* update the request timer to reflect full request */
3872 req->analysers &= ~an_bit;
3873 req->analyse_exp = TICK_ETERNITY;
3874 return 1;
3875
3876 return_bad_req: /* let's centralize all bad requests */
3877 txn->req.msg_state = HTTP_MSG_ERROR;
3878 txn->status = 400;
3879 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3880
Willy Tarreau79ebac62010-06-07 13:47:49 +02003881 if (!(s->flags & SN_ERR_MASK))
3882 s->flags |= SN_ERR_PRXCOND;
3883 if (!(s->flags & SN_FINST_MASK))
3884 s->flags |= SN_FINST_R;
3885
Willy Tarreau522d6c02009-12-06 18:49:18 +01003886 return_err_msg:
3887 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003888 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003889 if (s->listener->counters)
3890 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003891 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003892}
3893
Willy Tarreau610ecce2010-01-04 21:15:02 +01003894/* Terminate current transaction and prepare a new one. This is very tricky
3895 * right now but it works.
3896 */
3897void http_end_txn_clean_session(struct session *s)
3898{
3899 /* FIXME: We need a more portable way of releasing a backend's and a
3900 * server's connections. We need a safer way to reinitialize buffer
3901 * flags. We also need a more accurate method for computing per-request
3902 * data.
3903 */
3904 http_silent_debug(__LINE__, s);
3905
3906 s->req->cons->flags |= SI_FL_NOLINGER;
3907 s->req->cons->shutr(s->req->cons);
3908 s->req->cons->shutw(s->req->cons);
3909
3910 http_silent_debug(__LINE__, s);
3911
3912 if (s->flags & SN_BE_ASSIGNED)
3913 s->be->beconn--;
3914
3915 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3916 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003917 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003918
3919 if (s->txn.status) {
3920 int n;
3921
3922 n = s->txn.status / 100;
3923 if (n < 1 || n > 5)
3924 n = 0;
3925
3926 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003927 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003928
Willy Tarreau24657792010-02-26 10:30:28 +01003929 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003930 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003931 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003932 }
3933
3934 /* don't count other requests' data */
3935 s->logs.bytes_in -= s->req->l - s->req->send_max;
3936 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3937
3938 /* let's do a final log if we need it */
3939 if (s->logs.logwait &&
3940 !(s->flags & SN_MONITOR) &&
3941 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3942 s->do_log(s);
3943 }
3944
3945 s->logs.accept_date = date; /* user-visible date for logging */
3946 s->logs.tv_accept = now; /* corrected date for internal use */
3947 tv_zero(&s->logs.tv_request);
3948 s->logs.t_queue = -1;
3949 s->logs.t_connect = -1;
3950 s->logs.t_data = -1;
3951 s->logs.t_close = 0;
3952 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3953 s->logs.srv_queue_size = 0; /* we will get this number soon */
3954
3955 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3956 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3957
3958 if (s->pend_pos)
3959 pendconn_free(s->pend_pos);
3960
Willy Tarreau827aee92011-03-10 16:55:02 +01003961 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003962 if (s->flags & SN_CURR_SESS) {
3963 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003964 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003965 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003966 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3967 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003968 }
3969
3970 if (unlikely(s->srv_conn))
3971 sess_change_server(s, NULL);
Willy Tarreau9e000c62011-03-10 14:03:36 +01003972 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003973
3974 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3975 s->req->cons->fd = -1; /* just to help with debugging */
3976 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003977 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003978 s->req->cons->err_loc = NULL;
3979 s->req->cons->exp = TICK_ETERNITY;
3980 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003981 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3982 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 +02003983 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 +01003984 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3985 s->txn.meth = 0;
3986 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003987 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003988 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003989 s->req->cons->flags |= SI_FL_INDEP_STR;
3990
Willy Tarreau96e31212011-05-30 18:10:30 +02003991 if (s->fe->options2 & PR_O2_NODELAY) {
3992 s->req->flags |= BF_NEVER_WAIT;
3993 s->rep->flags |= BF_NEVER_WAIT;
3994 }
3995
Willy Tarreau610ecce2010-01-04 21:15:02 +01003996 /* if the request buffer is not empty, it means we're
3997 * about to process another request, so send pending
3998 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003999 * Just don't do this if the buffer is close to be full,
4000 * because the request will wait for it to flush a little
4001 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004002 */
Willy Tarreau065e8332010-01-08 00:30:20 +01004003 if (s->req->l > s->req->send_max) {
4004 if (s->rep->send_max &&
4005 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01004006 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
4007 s->rep->flags |= BF_EXPECT_MORE;
4008 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004009
4010 /* we're removing the analysers, we MUST re-enable events detection */
4011 buffer_auto_read(s->req);
4012 buffer_auto_close(s->req);
4013 buffer_auto_read(s->rep);
4014 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004015
4016 /* make ->lr point to the first non-forwarded byte */
4017 s->req->lr = s->req->w + s->req->send_max;
4018 if (s->req->lr >= s->req->data + s->req->size)
4019 s->req->lr -= s->req->size;
4020 s->rep->lr = s->rep->w + s->rep->send_max;
4021 if (s->rep->lr >= s->rep->data + s->rep->size)
4022 s->rep->lr -= s->req->size;
4023
Willy Tarreau342b11c2010-11-24 16:22:09 +01004024 s->req->analysers = s->listener->analysers;
4025 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004026 s->rep->analysers = 0;
4027
4028 http_silent_debug(__LINE__, s);
4029}
4030
4031
4032/* This function updates the request state machine according to the response
4033 * state machine and buffer flags. It returns 1 if it changes anything (flag
4034 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4035 * it is only used to find when a request/response couple is complete. Both
4036 * this function and its equivalent should loop until both return zero. It
4037 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4038 */
4039int http_sync_req_state(struct session *s)
4040{
4041 struct buffer *buf = s->req;
4042 struct http_txn *txn = &s->txn;
4043 unsigned int old_flags = buf->flags;
4044 unsigned int old_state = txn->req.msg_state;
4045
4046 http_silent_debug(__LINE__, s);
4047 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4048 return 0;
4049
4050 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004051 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004052 * We can shut the read side unless we want to abort_on_close,
4053 * or we have a POST request. The issue with POST requests is
4054 * that some browsers still send a CRLF after the request, and
4055 * this CRLF must be read so that it does not remain in the kernel
4056 * buffers, otherwise a close could cause an RST on some systems
4057 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004058 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004059 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01004060 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004061
4062 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4063 goto wait_other_side;
4064
4065 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4066 /* The server has not finished to respond, so we
4067 * don't want to move in order not to upset it.
4068 */
4069 goto wait_other_side;
4070 }
4071
4072 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4073 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004074 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004075 txn->req.msg_state = HTTP_MSG_TUNNEL;
4076 goto wait_other_side;
4077 }
4078
4079 /* When we get here, it means that both the request and the
4080 * response have finished receiving. Depending on the connection
4081 * mode, we'll have to wait for the last bytes to leave in either
4082 * direction, and sometimes for a close to be effective.
4083 */
4084
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004085 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4086 /* Server-close mode : queue a connection close to the server */
4087 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004088 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004089 }
4090 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4091 /* Option forceclose is set, or either side wants to close,
4092 * let's enforce it now that we're not expecting any new
4093 * data to come. The caller knows the session is complete
4094 * once both states are CLOSED.
4095 */
4096 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004097 buffer_shutr_now(buf);
4098 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004099 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004100 }
4101 else {
4102 /* The last possible modes are keep-alive and tunnel. Since tunnel
4103 * mode does not set the body analyser, we can't reach this place
4104 * in tunnel mode, so we're left with keep-alive only.
4105 * This mode is currently not implemented, we switch to tunnel mode.
4106 */
4107 buffer_auto_read(buf);
4108 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004109 }
4110
4111 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4112 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004113 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4114
Willy Tarreau610ecce2010-01-04 21:15:02 +01004115 if (!(buf->flags & BF_OUT_EMPTY)) {
4116 txn->req.msg_state = HTTP_MSG_CLOSING;
4117 goto http_msg_closing;
4118 }
4119 else {
4120 txn->req.msg_state = HTTP_MSG_CLOSED;
4121 goto http_msg_closed;
4122 }
4123 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004124 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004125 }
4126
4127 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4128 http_msg_closing:
4129 /* nothing else to forward, just waiting for the output buffer
4130 * to be empty and for the shutw_now to take effect.
4131 */
4132 if (buf->flags & BF_OUT_EMPTY) {
4133 txn->req.msg_state = HTTP_MSG_CLOSED;
4134 goto http_msg_closed;
4135 }
4136 else if (buf->flags & BF_SHUTW) {
4137 txn->req.msg_state = HTTP_MSG_ERROR;
4138 goto wait_other_side;
4139 }
4140 }
4141
4142 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4143 http_msg_closed:
4144 goto wait_other_side;
4145 }
4146
4147 wait_other_side:
4148 http_silent_debug(__LINE__, s);
4149 return txn->req.msg_state != old_state || buf->flags != old_flags;
4150}
4151
4152
4153/* This function updates the response state machine according to the request
4154 * state machine and buffer flags. It returns 1 if it changes anything (flag
4155 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4156 * it is only used to find when a request/response couple is complete. Both
4157 * this function and its equivalent should loop until both return zero. It
4158 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4159 */
4160int http_sync_res_state(struct session *s)
4161{
4162 struct buffer *buf = s->rep;
4163 struct http_txn *txn = &s->txn;
4164 unsigned int old_flags = buf->flags;
4165 unsigned int old_state = txn->rsp.msg_state;
4166
4167 http_silent_debug(__LINE__, s);
4168 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4169 return 0;
4170
4171 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4172 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004173 * still monitor the server connection for a possible close
4174 * while the request is being uploaded, so we don't disable
4175 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004176 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004177 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004178
4179 if (txn->req.msg_state == HTTP_MSG_ERROR)
4180 goto wait_other_side;
4181
4182 if (txn->req.msg_state < HTTP_MSG_DONE) {
4183 /* The client seems to still be sending data, probably
4184 * because we got an error response during an upload.
4185 * We have the choice of either breaking the connection
4186 * or letting it pass through. Let's do the later.
4187 */
4188 goto wait_other_side;
4189 }
4190
4191 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4192 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004193 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004194 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4195 goto wait_other_side;
4196 }
4197
4198 /* When we get here, it means that both the request and the
4199 * response have finished receiving. Depending on the connection
4200 * mode, we'll have to wait for the last bytes to leave in either
4201 * direction, and sometimes for a close to be effective.
4202 */
4203
4204 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4205 /* Server-close mode : shut read and wait for the request
4206 * side to close its output buffer. The caller will detect
4207 * when we're in DONE and the other is in CLOSED and will
4208 * catch that for the final cleanup.
4209 */
4210 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4211 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004212 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004213 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4214 /* Option forceclose is set, or either side wants to close,
4215 * let's enforce it now that we're not expecting any new
4216 * data to come. The caller knows the session is complete
4217 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004218 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004219 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4220 buffer_shutr_now(buf);
4221 buffer_shutw_now(buf);
4222 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004223 }
4224 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004225 /* The last possible modes are keep-alive and tunnel. Since tunnel
4226 * mode does not set the body analyser, we can't reach this place
4227 * in tunnel mode, so we're left with keep-alive only.
4228 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004229 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004230 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004231 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004232 }
4233
4234 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4235 /* if we've just closed an output, let's switch */
4236 if (!(buf->flags & BF_OUT_EMPTY)) {
4237 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4238 goto http_msg_closing;
4239 }
4240 else {
4241 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4242 goto http_msg_closed;
4243 }
4244 }
4245 goto wait_other_side;
4246 }
4247
4248 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4249 http_msg_closing:
4250 /* nothing else to forward, just waiting for the output buffer
4251 * to be empty and for the shutw_now to take effect.
4252 */
4253 if (buf->flags & BF_OUT_EMPTY) {
4254 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4255 goto http_msg_closed;
4256 }
4257 else if (buf->flags & BF_SHUTW) {
4258 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004259 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004260 if (target_srv(&s->target))
4261 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004262 goto wait_other_side;
4263 }
4264 }
4265
4266 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4267 http_msg_closed:
4268 /* drop any pending data */
4269 buffer_ignore(buf, buf->l - buf->send_max);
4270 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004271 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004272 goto wait_other_side;
4273 }
4274
4275 wait_other_side:
4276 http_silent_debug(__LINE__, s);
4277 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4278}
4279
4280
4281/* Resync the request and response state machines. Return 1 if either state
4282 * changes.
4283 */
4284int http_resync_states(struct session *s)
4285{
4286 struct http_txn *txn = &s->txn;
4287 int old_req_state = txn->req.msg_state;
4288 int old_res_state = txn->rsp.msg_state;
4289
4290 http_silent_debug(__LINE__, s);
4291 http_sync_req_state(s);
4292 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004293 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004294 if (!http_sync_res_state(s))
4295 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004296 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004297 if (!http_sync_req_state(s))
4298 break;
4299 }
4300 http_silent_debug(__LINE__, s);
4301 /* OK, both state machines agree on a compatible state.
4302 * There are a few cases we're interested in :
4303 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4304 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4305 * directions, so let's simply disable both analysers.
4306 * - HTTP_MSG_CLOSED on the response only means we must abort the
4307 * request.
4308 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4309 * with server-close mode means we've completed one request and we
4310 * must re-initialize the server connection.
4311 */
4312
4313 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4314 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4315 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4316 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4317 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004318 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004319 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004320 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004321 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004322 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004323 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004324 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4325 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004326 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004327 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004328 s->rep->analysers = 0;
4329 buffer_auto_close(s->rep);
4330 buffer_auto_read(s->rep);
4331 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004332 buffer_abort(s->req);
4333 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004334 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004335 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004336 }
4337 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4338 txn->rsp.msg_state == HTTP_MSG_DONE &&
4339 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4340 /* server-close: terminate this server connection and
4341 * reinitialize a fresh-new transaction.
4342 */
4343 http_end_txn_clean_session(s);
4344 }
4345
4346 http_silent_debug(__LINE__, s);
4347 return txn->req.msg_state != old_req_state ||
4348 txn->rsp.msg_state != old_res_state;
4349}
4350
Willy Tarreaud98cf932009-12-27 22:54:55 +01004351/* This function is an analyser which forwards request body (including chunk
4352 * sizes if any). It is called as soon as we must forward, even if we forward
4353 * zero byte. The only situation where it must not be called is when we're in
4354 * tunnel mode and we want to forward till the close. It's used both to forward
4355 * remaining data and to resync after end of body. It expects the msg_state to
4356 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4357 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004358 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004359 * bytes of pending data + the headers if not already done (between som and sov).
4360 * It eventually adjusts som to match sov after the data in between have been sent.
4361 */
4362int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4363{
4364 struct http_txn *txn = &s->txn;
4365 struct http_msg *msg = &s->txn.req;
4366
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004367 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4368 return 0;
4369
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004370 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4371 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004372 /* Output closed while we were sending data. We must abort and
4373 * wake the other side up.
4374 */
4375 msg->msg_state = HTTP_MSG_ERROR;
4376 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004377 return 1;
4378 }
4379
Willy Tarreau4fe41902010-06-07 22:27:41 +02004380 /* in most states, we should abort in case of early close */
4381 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004382
4383 /* Note that we don't have to send 100-continue back because we don't
4384 * need the data to complete our job, and it's up to the server to
4385 * decide whether to return 100, 417 or anything else in return of
4386 * an "Expect: 100-continue" header.
4387 */
4388
4389 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4390 /* we have msg->col and msg->sov which both point to the first
4391 * byte of message body. msg->som still points to the beginning
4392 * of the message. We must save the body in req->lr because it
4393 * survives buffer re-alignments.
4394 */
4395 req->lr = req->data + msg->sov;
4396 if (txn->flags & TX_REQ_TE_CHNK)
4397 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4398 else {
4399 msg->msg_state = HTTP_MSG_DATA;
4400 }
4401 }
4402
Willy Tarreaud98cf932009-12-27 22:54:55 +01004403 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004404 int bytes;
4405
Willy Tarreau610ecce2010-01-04 21:15:02 +01004406 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004407 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004408 bytes = msg->sov - msg->som;
4409 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004410 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004411 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4412 bytes += req->size;
4413 msg->chunk_len += (unsigned int)bytes;
4414 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004415 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004416
Willy Tarreaucaabe412010-01-03 23:08:28 +01004417 if (msg->msg_state == HTTP_MSG_DATA) {
4418 /* must still forward */
4419 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004420 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004421
4422 /* nothing left to forward */
4423 if (txn->flags & TX_REQ_TE_CHNK)
4424 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004425 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004426 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004427 }
4428 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004429 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004430 * set ->sov and ->lr to point to the body and switch to DATA or
4431 * TRAILERS state.
4432 */
4433 int ret = http_parse_chunk_size(req, msg);
4434
4435 if (!ret)
4436 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004437 else if (ret < 0) {
4438 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004439 if (msg->err_pos >= 0)
4440 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004441 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004442 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004443 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004444 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004445 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4446 /* we want the CRLF after the data */
4447 int ret;
4448
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004449 req->lr = req->w + req->send_max;
4450 if (req->lr >= req->data + req->size)
4451 req->lr -= req->size;
4452
Willy Tarreaud98cf932009-12-27 22:54:55 +01004453 ret = http_skip_chunk_crlf(req, msg);
4454
4455 if (ret == 0)
4456 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004457 else if (ret < 0) {
4458 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004459 if (msg->err_pos >= 0)
4460 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004461 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004462 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004463 /* we're in MSG_CHUNK_SIZE now */
4464 }
4465 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4466 int ret = http_forward_trailers(req, msg);
4467
4468 if (ret == 0)
4469 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004470 else if (ret < 0) {
4471 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004472 if (msg->err_pos >= 0)
4473 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004474 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004475 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004476 /* we're in HTTP_MSG_DONE now */
4477 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004478 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004479 int old_state = msg->msg_state;
4480
Willy Tarreau610ecce2010-01-04 21:15:02 +01004481 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004482 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004483 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4484 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4485 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004486 if (http_resync_states(s)) {
4487 /* some state changes occurred, maybe the analyser
4488 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004489 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004490 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4491 if (req->flags & BF_SHUTW) {
4492 /* request errors are most likely due to
4493 * the server aborting the transfer.
4494 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004495 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004496 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004497 if (msg->err_pos >= 0)
4498 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004499 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004500 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004501 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004502 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004503
4504 /* If "option abortonclose" is set on the backend, we
4505 * want to monitor the client's connection and forward
4506 * any shutdown notification to the server, which will
4507 * decide whether to close or to go on processing the
4508 * request.
4509 */
4510 if (s->be->options & PR_O_ABRT_CLOSE) {
4511 buffer_auto_read(req);
4512 buffer_auto_close(req);
4513 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004514 else if (s->txn.meth == HTTP_METH_POST) {
4515 /* POST requests may require to read extra CRLF
4516 * sent by broken browsers and which could cause
4517 * an RST to be sent upon close on some systems
4518 * (eg: Linux).
4519 */
4520 buffer_auto_read(req);
4521 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004522
Willy Tarreau610ecce2010-01-04 21:15:02 +01004523 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004524 }
4525 }
4526
Willy Tarreaud98cf932009-12-27 22:54:55 +01004527 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004528 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004529 if (req->flags & BF_SHUTR) {
4530 if (!(s->flags & SN_ERR_MASK))
4531 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004532 if (!(s->flags & SN_FINST_MASK)) {
4533 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4534 s->flags |= SN_FINST_H;
4535 else
4536 s->flags |= SN_FINST_D;
4537 }
4538
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004539 s->fe->fe_counters.cli_aborts++;
4540 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004541 if (target_srv(&s->target))
4542 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004543
4544 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004545 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004546
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004547 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004548 if (req->flags & BF_SHUTW)
4549 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004550
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004551 /* When TE: chunked is used, we need to get there again to parse remaining
4552 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4553 */
4554 if (txn->flags & TX_REQ_TE_CHNK)
4555 buffer_dont_close(req);
4556
Willy Tarreau5c620922011-05-11 19:56:11 +02004557 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004558 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4559 * system knows it must not set a PUSH on this first part. Interactive
4560 * modes are already handled by the stream sock layer.
Willy Tarreau5c620922011-05-11 19:56:11 +02004561 */
Willy Tarreau07293032011-05-30 18:29:28 +02004562 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004563
Willy Tarreau610ecce2010-01-04 21:15:02 +01004564 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004565 return 0;
4566
Willy Tarreaud98cf932009-12-27 22:54:55 +01004567 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004568 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004569 if (s->listener->counters)
4570 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004571 return_bad_req_stats_ok:
4572 txn->req.msg_state = HTTP_MSG_ERROR;
4573 if (txn->status) {
4574 /* Note: we don't send any error if some data were already sent */
4575 stream_int_retnclose(req->prod, NULL);
4576 } else {
4577 txn->status = 400;
4578 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4579 }
4580 req->analysers = 0;
4581 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004582
4583 if (!(s->flags & SN_ERR_MASK))
4584 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004585 if (!(s->flags & SN_FINST_MASK)) {
4586 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4587 s->flags |= SN_FINST_H;
4588 else
4589 s->flags |= SN_FINST_D;
4590 }
4591 return 0;
4592
4593 aborted_xfer:
4594 txn->req.msg_state = HTTP_MSG_ERROR;
4595 if (txn->status) {
4596 /* Note: we don't send any error if some data were already sent */
4597 stream_int_retnclose(req->prod, NULL);
4598 } else {
4599 txn->status = 502;
4600 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4601 }
4602 req->analysers = 0;
4603 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4604
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004605 s->fe->fe_counters.srv_aborts++;
4606 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004607 if (target_srv(&s->target))
4608 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004609
4610 if (!(s->flags & SN_ERR_MASK))
4611 s->flags |= SN_ERR_SRVCL;
4612 if (!(s->flags & SN_FINST_MASK)) {
4613 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4614 s->flags |= SN_FINST_H;
4615 else
4616 s->flags |= SN_FINST_D;
4617 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004618 return 0;
4619}
4620
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004621/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4622 * processing can continue on next analysers, or zero if it either needs more
4623 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4624 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4625 * when it has nothing left to do, and may remove any analyser when it wants to
4626 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004627 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004628int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004629{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004630 struct http_txn *txn = &s->txn;
4631 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004632 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004633 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004634 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004635 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004636
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004637 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 +02004638 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004639 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004640 rep,
4641 rep->rex, rep->wex,
4642 rep->flags,
4643 rep->l,
4644 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004645
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004646 /*
4647 * Now parse the partial (or complete) lines.
4648 * We will check the response syntax, and also join multi-line
4649 * headers. An index of all the lines will be elaborated while
4650 * parsing.
4651 *
4652 * For the parsing, we use a 28 states FSM.
4653 *
4654 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004655 * rep->data + msg->som = beginning of response
4656 * rep->data + msg->eoh = end of processed headers / start of current one
4657 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004658 * rep->lr = first non-visited byte
4659 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004660 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004661 */
4662
Willy Tarreau83e3af02009-12-28 17:39:57 +01004663 /* There's a protected area at the end of the buffer for rewriting
4664 * purposes. We don't want to start to parse the request if the
4665 * protected area is affected, because we may have to move processed
4666 * data later, which is much more complicated.
4667 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004668 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4669 if (unlikely((rep->flags & BF_FULL) ||
4670 rep->r < rep->lr ||
4671 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4672 if (rep->send_max) {
4673 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004674 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4675 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004676 buffer_dont_close(rep);
4677 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4678 return 0;
4679 }
4680 if (rep->l <= rep->size - global.tune.maxrewrite)
4681 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004682 }
4683
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004684 if (likely(rep->lr < rep->r))
4685 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004686 }
4687
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004688 /* 1: we might have to print this header in debug mode */
4689 if (unlikely((global.mode & MODE_DEBUG) &&
4690 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004691 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004692 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004693 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004694
Willy Tarreau663308b2010-06-07 14:06:08 +02004695 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004696 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004697 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004698
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004699 sol += hdr_idx_first_pos(&txn->hdr_idx);
4700 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004701
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004702 while (cur_idx) {
4703 eol = sol + txn->hdr_idx.v[cur_idx].len;
4704 debug_hdr("srvhdr", s, sol, eol);
4705 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4706 cur_idx = txn->hdr_idx.v[cur_idx].next;
4707 }
4708 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004709
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004710 /*
4711 * Now we quickly check if we have found a full valid response.
4712 * If not so, we check the FD and buffer states before leaving.
4713 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004714 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004715 * responses are checked first.
4716 *
4717 * Depending on whether the client is still there or not, we
4718 * may send an error response back or not. Note that normally
4719 * we should only check for HTTP status there, and check I/O
4720 * errors somewhere else.
4721 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004722
Willy Tarreau655dce92009-11-08 13:10:58 +01004723 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004724 /* Invalid response */
4725 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4726 /* we detected a parsing error. We want to archive this response
4727 * in the dedicated proxy area for later troubleshooting.
4728 */
4729 hdr_response_bad:
4730 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004731 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004732
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004733 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004734 if (target_srv(&s->target)) {
4735 target_srv(&s->target)->counters.failed_resp++;
4736 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004737 }
Willy Tarreau64648412010-03-05 10:41:54 +01004738 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004739 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004740 rep->analysers = 0;
4741 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004742 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004743 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004744 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4745
4746 if (!(s->flags & SN_ERR_MASK))
4747 s->flags |= SN_ERR_PRXCOND;
4748 if (!(s->flags & SN_FINST_MASK))
4749 s->flags |= SN_FINST_H;
4750
4751 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004752 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004753
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004754 /* too large response does not fit in buffer. */
4755 else if (rep->flags & BF_FULL) {
4756 goto hdr_response_bad;
4757 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004758
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004759 /* read error */
4760 else if (rep->flags & BF_READ_ERROR) {
4761 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004762 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004763
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004764 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004765 if (target_srv(&s->target)) {
4766 target_srv(&s->target)->counters.failed_resp++;
4767 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004768 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004769
Willy Tarreau90deb182010-01-07 00:20:41 +01004770 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004771 rep->analysers = 0;
4772 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004773 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004774 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004775 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004776
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004777 if (!(s->flags & SN_ERR_MASK))
4778 s->flags |= SN_ERR_SRVCL;
4779 if (!(s->flags & SN_FINST_MASK))
4780 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004781 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004782 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004783
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004784 /* read timeout : return a 504 to the client. */
4785 else if (rep->flags & BF_READ_TIMEOUT) {
4786 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004787 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004788
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004789 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004790 if (target_srv(&s->target)) {
4791 target_srv(&s->target)->counters.failed_resp++;
4792 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004793 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004794
Willy Tarreau90deb182010-01-07 00:20:41 +01004795 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004796 rep->analysers = 0;
4797 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004798 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004799 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004800 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004801
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004802 if (!(s->flags & SN_ERR_MASK))
4803 s->flags |= SN_ERR_SRVTO;
4804 if (!(s->flags & SN_FINST_MASK))
4805 s->flags |= SN_FINST_H;
4806 return 0;
4807 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004808
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004809 /* close from server */
4810 else if (rep->flags & BF_SHUTR) {
4811 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004812 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004813
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004814 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004815 if (target_srv(&s->target)) {
4816 target_srv(&s->target)->counters.failed_resp++;
4817 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004818 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004819
Willy Tarreau90deb182010-01-07 00:20:41 +01004820 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004821 rep->analysers = 0;
4822 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004823 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004824 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004825 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004826
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004827 if (!(s->flags & SN_ERR_MASK))
4828 s->flags |= SN_ERR_SRVCL;
4829 if (!(s->flags & SN_FINST_MASK))
4830 s->flags |= SN_FINST_H;
4831 return 0;
4832 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004833
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004834 /* write error to client (we don't send any message then) */
4835 else if (rep->flags & BF_WRITE_ERROR) {
4836 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004837 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004838
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004839 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004840 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004841 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004842
4843 if (!(s->flags & SN_ERR_MASK))
4844 s->flags |= SN_ERR_CLICL;
4845 if (!(s->flags & SN_FINST_MASK))
4846 s->flags |= SN_FINST_H;
4847
4848 /* process_session() will take care of the error */
4849 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004850 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004851
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004852 buffer_dont_close(rep);
4853 return 0;
4854 }
4855
4856 /* More interesting part now : we know that we have a complete
4857 * response which at least looks like HTTP. We have an indicator
4858 * of each header's length, so we can parse them quickly.
4859 */
4860
4861 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004862 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004863
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004864 /*
4865 * 1: get the status code
4866 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004867 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004868 if (n < 1 || n > 5)
4869 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004870 /* when the client triggers a 4xx from the server, it's most often due
4871 * to a missing object or permission. These events should be tracked
4872 * because if they happen often, it may indicate a brute force or a
4873 * vulnerability scan.
4874 */
4875 if (n == 4)
4876 session_inc_http_err_ctr(s);
4877
Willy Tarreau827aee92011-03-10 16:55:02 +01004878 if (target_srv(&s->target))
4879 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004880
Willy Tarreau5b154472009-12-21 20:11:07 +01004881 /* check if the response is HTTP/1.1 or above */
4882 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004883 ((msg->sol[5] > '1') ||
4884 ((msg->sol[5] == '1') &&
4885 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004886 txn->flags |= TX_RES_VER_11;
4887
4888 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004889 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 +01004890
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004891 /* transfer length unknown*/
4892 txn->flags &= ~TX_RES_XFER_LEN;
4893
Willy Tarreau962c3f42010-01-10 00:15:35 +01004894 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004895
Willy Tarreau39650402010-03-15 19:44:39 +01004896 /* Adjust server's health based on status code. Note: status codes 501
4897 * and 505 are triggered on demand by client request, so we must not
4898 * count them as server failures.
4899 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004900 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004901 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004902 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004903 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004904 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004905 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004906
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004907 /*
4908 * 2: check for cacheability.
4909 */
4910
4911 switch (txn->status) {
4912 case 200:
4913 case 203:
4914 case 206:
4915 case 300:
4916 case 301:
4917 case 410:
4918 /* RFC2616 @13.4:
4919 * "A response received with a status code of
4920 * 200, 203, 206, 300, 301 or 410 MAY be stored
4921 * by a cache (...) unless a cache-control
4922 * directive prohibits caching."
4923 *
4924 * RFC2616 @9.5: POST method :
4925 * "Responses to this method are not cacheable,
4926 * unless the response includes appropriate
4927 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004928 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004929 if (likely(txn->meth != HTTP_METH_POST) &&
4930 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4931 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4932 break;
4933 default:
4934 break;
4935 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004936
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004937 /*
4938 * 3: we may need to capture headers
4939 */
4940 s->logs.logwait &= ~LW_RESP;
4941 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004942 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004943 txn->rsp.cap, s->fe->rsp_cap);
4944
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004945 /* 4: determine the transfer-length.
4946 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4947 * the presence of a message-body in a RESPONSE and its transfer length
4948 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004949 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004950 * All responses to the HEAD request method MUST NOT include a
4951 * message-body, even though the presence of entity-header fields
4952 * might lead one to believe they do. All 1xx (informational), 204
4953 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4954 * message-body. All other responses do include a message-body,
4955 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004956 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004957 * 1. Any response which "MUST NOT" include a message-body (such as the
4958 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4959 * always terminated by the first empty line after the header fields,
4960 * regardless of the entity-header fields present in the message.
4961 *
4962 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4963 * the "chunked" transfer-coding (Section 6.2) is used, the
4964 * transfer-length is defined by the use of this transfer-coding.
4965 * If a Transfer-Encoding header field is present and the "chunked"
4966 * transfer-coding is not present, the transfer-length is defined by
4967 * the sender closing the connection.
4968 *
4969 * 3. If a Content-Length header field is present, its decimal value in
4970 * OCTETs represents both the entity-length and the transfer-length.
4971 * If a message is received with both a Transfer-Encoding header
4972 * field and a Content-Length header field, the latter MUST be ignored.
4973 *
4974 * 4. If the message uses the media type "multipart/byteranges", and
4975 * the transfer-length is not otherwise specified, then this self-
4976 * delimiting media type defines the transfer-length. This media
4977 * type MUST NOT be used unless the sender knows that the recipient
4978 * can parse it; the presence in a request of a Range header with
4979 * multiple byte-range specifiers from a 1.1 client implies that the
4980 * client can parse multipart/byteranges responses.
4981 *
4982 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004983 */
4984
4985 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004986 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004987 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004988 * FIXME: should we parse anyway and return an error on chunked encoding ?
4989 */
4990 if (txn->meth == HTTP_METH_HEAD ||
4991 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004992 txn->status == 204 || txn->status == 304) {
4993 txn->flags |= TX_RES_XFER_LEN;
4994 goto skip_content_length;
4995 }
4996
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004997 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004998 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004999 while ((txn->flags & TX_RES_VER_11) &&
5000 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005001 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
5002 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5003 else if (txn->flags & TX_RES_TE_CHNK) {
5004 /* bad transfer-encoding (chunked followed by something else) */
5005 use_close_only = 1;
5006 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5007 break;
5008 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005009 }
5010
5011 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5012 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005013 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005014 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
5015 signed long long cl;
5016
5017 if (!ctx.vlen)
5018 goto hdr_response_bad;
5019
5020 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
5021 goto hdr_response_bad; /* parse failure */
5022
5023 if (cl < 0)
5024 goto hdr_response_bad;
5025
Willy Tarreau124d9912011-03-01 20:30:48 +01005026 if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl))
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005027 goto hdr_response_bad; /* already specified, was different */
5028
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005029 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005030 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005031 }
5032
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005033 /* FIXME: we should also implement the multipart/byterange method.
5034 * For now on, we resort to close mode in this case (unknown length).
5035 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005036skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005037
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005038 /* end of job, return OK */
5039 rep->analysers &= ~an_bit;
5040 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01005041 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005042 return 1;
5043}
5044
5045/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005046 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5047 * and updates t->rep->analysers. It might make sense to explode it into several
5048 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005049 */
5050int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
5051{
5052 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005053 struct http_msg *msg = &txn->rsp;
5054 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005055 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005056
5057 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
5058 now_ms, __FUNCTION__,
5059 t,
5060 rep,
5061 rep->rex, rep->wex,
5062 rep->flags,
5063 rep->l,
5064 rep->analysers);
5065
Willy Tarreau655dce92009-11-08 13:10:58 +01005066 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005067 return 0;
5068
5069 rep->analysers &= ~an_bit;
5070 rep->analyse_exp = TICK_ETERNITY;
5071
Willy Tarreau5b154472009-12-21 20:11:07 +01005072 /* Now we have to check if we need to modify the Connection header.
5073 * This is more difficult on the response than it is on the request,
5074 * because we can have two different HTTP versions and we don't know
5075 * how the client will interprete a response. For instance, let's say
5076 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5077 * HTTP/1.1 response without any header. Maybe it will bound itself to
5078 * HTTP/1.0 because it only knows about it, and will consider the lack
5079 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5080 * the lack of header as a keep-alive. Thus we will use two flags
5081 * indicating how a request MAY be understood by the client. In case
5082 * of multiple possibilities, we'll fix the header to be explicit. If
5083 * ambiguous cases such as both close and keepalive are seen, then we
5084 * will fall back to explicit close. Note that we won't take risks with
5085 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005086 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005087 */
5088
Willy Tarreaudc008c52010-02-01 16:20:08 +01005089 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5090 txn->status == 101)) {
5091 /* Either we've established an explicit tunnel, or we're
5092 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005093 * to understand the next protocols. We have to switch to tunnel
5094 * mode, so that we transfer the request and responses then let
5095 * this protocol pass unmodified. When we later implement specific
5096 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005097 * header which contains information about that protocol for
5098 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005099 */
5100 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5101 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005102 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5103 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5104 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005105 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005106
Willy Tarreau60466522010-01-18 19:08:45 +01005107 /* on unknown transfer length, we must close */
5108 if (!(txn->flags & TX_RES_XFER_LEN) &&
5109 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5110 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005111
Willy Tarreau60466522010-01-18 19:08:45 +01005112 /* now adjust header transformations depending on current state */
5113 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5114 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5115 to_del |= 2; /* remove "keep-alive" on any response */
5116 if (!(txn->flags & TX_RES_VER_11))
5117 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005118 }
Willy Tarreau60466522010-01-18 19:08:45 +01005119 else { /* SCL / KAL */
5120 to_del |= 1; /* remove "close" on any response */
5121 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
5122 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005123 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005124
Willy Tarreau60466522010-01-18 19:08:45 +01005125 /* Parse and remove some headers from the connection header */
5126 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005127
Willy Tarreau60466522010-01-18 19:08:45 +01005128 /* Some keep-alive responses are converted to Server-close if
5129 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005130 */
Willy Tarreau60466522010-01-18 19:08:45 +01005131 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5132 if ((txn->flags & TX_HDR_CONN_CLO) ||
5133 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
5134 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005135 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005136 }
5137
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005138 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005139 /*
5140 * 3: we will have to evaluate the filters.
5141 * As opposed to version 1.2, now they will be evaluated in the
5142 * filters order and not in the header order. This means that
5143 * each filter has to be validated among all headers.
5144 *
5145 * Filters are tried with ->be first, then with ->fe if it is
5146 * different from ->be.
5147 */
5148
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005149 cur_proxy = t->be;
5150 while (1) {
5151 struct proxy *rule_set = cur_proxy;
5152
5153 /* try headers filters */
5154 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005155 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005156 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005157 if (target_srv(&t->target)) {
5158 target_srv(&t->target)->counters.failed_resp++;
5159 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005160 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005161 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005162 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005163 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005164 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005165 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01005166 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005167 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005168 if (!(t->flags & SN_ERR_MASK))
5169 t->flags |= SN_ERR_PRXCOND;
5170 if (!(t->flags & SN_FINST_MASK))
5171 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005172 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005173 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005174 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005175
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005176 /* has the response been denied ? */
5177 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005178 if (target_srv(&t->target))
5179 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005180
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005181 t->be->be_counters.denied_resp++;
5182 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005183 if (t->listener->counters)
5184 t->listener->counters->denied_resp++;
5185
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005186 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005187 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005188
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005189 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005190 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005191 if (txn->status < 200)
5192 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005193 if (wl->cond) {
5194 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5195 ret = acl_pass(ret);
5196 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5197 ret = !ret;
5198 if (!ret)
5199 continue;
5200 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005201 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005202 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005203 }
5204
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005205 /* check whether we're already working on the frontend */
5206 if (cur_proxy == t->fe)
5207 break;
5208 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005209 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005210
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005211 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005212 * We may be facing a 100-continue response, in which case this
5213 * is not the right response, and we're waiting for the next one.
5214 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005215 * next one.
5216 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005217 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005218 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005219 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005220 msg->msg_state = HTTP_MSG_RPBEFORE;
5221 txn->status = 0;
5222 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5223 return 1;
5224 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005225 else if (unlikely(txn->status < 200))
5226 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005227
5228 /* we don't have any 1xx status code now */
5229
5230 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005231 * 4: check for server cookie.
5232 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005233 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5234 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005235 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005236
Willy Tarreaubaaee002006-06-26 02:48:02 +02005237
Willy Tarreaua15645d2007-03-18 16:22:39 +01005238 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005239 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005240 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005241 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005242 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005243
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005244 /*
5245 * 6: add server cookie in the response if needed
5246 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005247 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005248 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005249 (!(t->flags & SN_DIRECT) ||
5250 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5251 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5252 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5253 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005254 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5255 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005256 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005257 /* the server is known, it's not the one the client requested, or the
5258 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005259 * insert a set-cookie here, except if we want to insert only on POST
5260 * requests and this one isn't. Note that servers which don't have cookies
5261 * (eg: some backup servers) will return a full cookie removal request.
5262 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005263 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005264 len = sprintf(trash,
5265 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5266 t->be->cookie_name);
5267 }
5268 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005269 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005270
5271 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5272 /* emit last_date, which is mandatory */
5273 trash[len++] = COOKIE_DELIM_DATE;
5274 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5275 if (t->be->cookie_maxlife) {
5276 /* emit first_date, which is either the original one or
5277 * the current date.
5278 */
5279 trash[len++] = COOKIE_DELIM_DATE;
5280 s30tob64(txn->cookie_first_date ?
5281 txn->cookie_first_date >> 2 :
5282 (date.tv_sec+3) >> 2, trash + len);
5283 len += 5;
5284 }
5285 }
5286 len += sprintf(trash + len, "; path=/");
5287 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005288
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005289 if (t->be->cookie_domain)
5290 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005291
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005292 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005293 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005294 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005295
Willy Tarreauf1348312010-10-07 15:54:11 +02005296 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005297 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005298 /* the server did not change, only the date was updated */
5299 txn->flags |= TX_SCK_UPDATED;
5300 else
5301 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005302
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005303 /* Here, we will tell an eventual cache on the client side that we don't
5304 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5305 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5306 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5307 */
5308 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005309
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005310 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5311
5312 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005313 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005314 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005315 }
5316 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005317
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005318 /*
5319 * 7: check if result will be cacheable with a cookie.
5320 * We'll block the response if security checks have caught
5321 * nasty things such as a cacheable cookie.
5322 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005323 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5324 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005325 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005326
5327 /* we're in presence of a cacheable response containing
5328 * a set-cookie header. We'll block it as requested by
5329 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005330 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005331 if (target_srv(&t->target))
5332 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005333
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005334 t->be->be_counters.denied_resp++;
5335 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005336 if (t->listener->counters)
5337 t->listener->counters->denied_resp++;
5338
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005339 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005340 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005341 send_log(t->be, LOG_ALERT,
5342 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005343 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005344 goto return_srv_prx_502;
5345 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005346
5347 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005348 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005349 */
Willy Tarreau60466522010-01-18 19:08:45 +01005350 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5351 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5352 unsigned int want_flags = 0;
5353
5354 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5355 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5356 /* we want a keep-alive response here. Keep-alive header
5357 * required if either side is not 1.1.
5358 */
5359 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5360 want_flags |= TX_CON_KAL_SET;
5361 }
5362 else {
5363 /* we want a close response here. Close header required if
5364 * the server is 1.1, regardless of the client.
5365 */
5366 if (txn->flags & TX_RES_VER_11)
5367 want_flags |= TX_CON_CLO_SET;
5368 }
5369
5370 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5371 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005372 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005373
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005374 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005375 if ((txn->flags & TX_RES_XFER_LEN) ||
5376 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005377 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005378
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005379 /*************************************************************
5380 * OK, that's finished for the headers. We have done what we *
5381 * could. Let's switch to the DATA state. *
5382 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005383
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005384 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005385
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005386 /* if the user wants to log as soon as possible, without counting
5387 * bytes from the server, then this is the right moment. We have
5388 * to temporarily assign bytes_out to log what we currently have.
5389 */
5390 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5391 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5392 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005393 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005394 t->logs.bytes_out = 0;
5395 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005396
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005397 /* Note: we must not try to cheat by jumping directly to DATA,
5398 * otherwise we would not let the client side wake up.
5399 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005400
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005401 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005402 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005403 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005404}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005405
Willy Tarreaud98cf932009-12-27 22:54:55 +01005406/* This function is an analyser which forwards response body (including chunk
5407 * sizes if any). It is called as soon as we must forward, even if we forward
5408 * zero byte. The only situation where it must not be called is when we're in
5409 * tunnel mode and we want to forward till the close. It's used both to forward
5410 * remaining data and to resync after end of body. It expects the msg_state to
5411 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5412 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005413 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005414 * bytes of pending data + the headers if not already done (between som and sov).
5415 * It eventually adjusts som to match sov after the data in between have been sent.
5416 */
5417int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5418{
5419 struct http_txn *txn = &s->txn;
5420 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005421 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005422
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005423 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5424 return 0;
5425
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005426 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005427 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005428 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005429 /* Output closed while we were sending data. We must abort and
5430 * wake the other side up.
5431 */
5432 msg->msg_state = HTTP_MSG_ERROR;
5433 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005434 return 1;
5435 }
5436
Willy Tarreau4fe41902010-06-07 22:27:41 +02005437 /* in most states, we should abort in case of early close */
5438 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005439
Willy Tarreaud98cf932009-12-27 22:54:55 +01005440 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5441 /* we have msg->col and msg->sov which both point to the first
5442 * byte of message body. msg->som still points to the beginning
5443 * of the message. We must save the body in req->lr because it
5444 * survives buffer re-alignments.
5445 */
5446 res->lr = res->data + msg->sov;
5447 if (txn->flags & TX_RES_TE_CHNK)
5448 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5449 else {
5450 msg->msg_state = HTTP_MSG_DATA;
5451 }
5452 }
5453
Willy Tarreaud98cf932009-12-27 22:54:55 +01005454 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005455 int bytes;
5456
Willy Tarreau610ecce2010-01-04 21:15:02 +01005457 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005458 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005459 bytes = msg->sov - msg->som;
5460 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005461 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005462 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5463 bytes += res->size;
5464 msg->chunk_len += (unsigned int)bytes;
5465 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005466 }
5467
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005468
Willy Tarreaucaabe412010-01-03 23:08:28 +01005469 if (msg->msg_state == HTTP_MSG_DATA) {
5470 /* must still forward */
5471 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005472 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005473
5474 /* nothing left to forward */
5475 if (txn->flags & TX_RES_TE_CHNK)
5476 msg->msg_state = HTTP_MSG_DATA_CRLF;
5477 else
5478 msg->msg_state = HTTP_MSG_DONE;
5479 }
5480 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005481 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005482 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5483 */
5484 int ret = http_parse_chunk_size(res, msg);
5485
5486 if (!ret)
5487 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005488 else if (ret < 0) {
5489 if (msg->err_pos >= 0)
5490 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005491 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005492 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005493 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005494 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005495 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5496 /* we want the CRLF after the data */
5497 int ret;
5498
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005499 res->lr = res->w + res->send_max;
5500 if (res->lr >= res->data + res->size)
5501 res->lr -= res->size;
5502
Willy Tarreaud98cf932009-12-27 22:54:55 +01005503 ret = http_skip_chunk_crlf(res, msg);
5504
5505 if (!ret)
5506 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005507 else if (ret < 0) {
5508 if (msg->err_pos >= 0)
5509 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005510 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005511 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005512 /* we're in MSG_CHUNK_SIZE now */
5513 }
5514 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5515 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005516
Willy Tarreaud98cf932009-12-27 22:54:55 +01005517 if (ret == 0)
5518 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005519 else if (ret < 0) {
5520 if (msg->err_pos >= 0)
5521 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005522 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005523 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005524 /* we're in HTTP_MSG_DONE now */
5525 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005526 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005527 int old_state = msg->msg_state;
5528
Willy Tarreau610ecce2010-01-04 21:15:02 +01005529 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005530 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005531 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5532 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5533 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005534 if (http_resync_states(s)) {
5535 http_silent_debug(__LINE__, s);
5536 /* some state changes occurred, maybe the analyser
5537 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005538 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005539 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5540 if (res->flags & BF_SHUTW) {
5541 /* response errors are most likely due to
5542 * the client aborting the transfer.
5543 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005544 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005545 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005546 if (msg->err_pos >= 0)
5547 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005548 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005549 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005550 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005551 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005552 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005553 }
5554 }
5555
Willy Tarreaud98cf932009-12-27 22:54:55 +01005556 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005557 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005558 if (res->flags & BF_SHUTR) {
5559 if (!(s->flags & SN_ERR_MASK))
5560 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005561 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005562 if (target_srv(&s->target))
5563 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005564 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005565 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005566
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005567 if (res->flags & BF_SHUTW)
5568 goto aborted_xfer;
5569
Willy Tarreau40dba092010-03-04 18:14:51 +01005570 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005571 if (!s->req->analysers)
5572 goto return_bad_res;
5573
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005574 /* forward any pending data */
5575 bytes = msg->sov - msg->som;
5576 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005577 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005578 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5579 bytes += res->size;
5580 msg->chunk_len += (unsigned int)bytes;
5581 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005582 }
5583
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005584 /* When TE: chunked is used, we need to get there again to parse remaining
5585 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5586 * Similarly, with keep-alive on the client side, we don't want to forward a
5587 * close.
5588 */
5589 if ((txn->flags & TX_RES_TE_CHNK) ||
5590 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5591 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5592 buffer_dont_close(res);
5593
Willy Tarreau5c620922011-05-11 19:56:11 +02005594 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005595 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5596 * system knows it must not set a PUSH on this first part. Interactive
5597 * modes are already handled by the stream sock layer.
Willy Tarreau5c620922011-05-11 19:56:11 +02005598 */
Willy Tarreau07293032011-05-30 18:29:28 +02005599 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005600
Willy Tarreaud98cf932009-12-27 22:54:55 +01005601 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005602 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005603 return 0;
5604
Willy Tarreau40dba092010-03-04 18:14:51 +01005605 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005606 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005607 if (target_srv(&s->target))
5608 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005609
5610 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005611 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005612 /* don't send any error message as we're in the body */
5613 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005614 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005615 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005616 if (target_srv(&s->target))
5617 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005618
5619 if (!(s->flags & SN_ERR_MASK))
5620 s->flags |= SN_ERR_PRXCOND;
5621 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005622 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005623 return 0;
5624
5625 aborted_xfer:
5626 txn->rsp.msg_state = HTTP_MSG_ERROR;
5627 /* don't send any error message as we're in the body */
5628 stream_int_retnclose(res->cons, NULL);
5629 res->analysers = 0;
5630 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5631
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005632 s->fe->fe_counters.cli_aborts++;
5633 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005634 if (target_srv(&s->target))
5635 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005636
5637 if (!(s->flags & SN_ERR_MASK))
5638 s->flags |= SN_ERR_CLICL;
5639 if (!(s->flags & SN_FINST_MASK))
5640 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005641 return 0;
5642}
5643
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005644/* Iterate the same filter through all request headers.
5645 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005646 * Since it can manage the switch to another backend, it updates the per-proxy
5647 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005648 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005649int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005650{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005651 char term;
5652 char *cur_ptr, *cur_end, *cur_next;
5653 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005654 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005655 struct hdr_idx_elem *cur_hdr;
5656 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005657
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005658 last_hdr = 0;
5659
Willy Tarreau962c3f42010-01-10 00:15:35 +01005660 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005661 old_idx = 0;
5662
5663 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005664 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005665 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005666 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005667 (exp->action == ACT_ALLOW ||
5668 exp->action == ACT_DENY ||
5669 exp->action == ACT_TARPIT))
5670 return 0;
5671
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005672 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005673 if (!cur_idx)
5674 break;
5675
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005676 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005677 cur_ptr = cur_next;
5678 cur_end = cur_ptr + cur_hdr->len;
5679 cur_next = cur_end + cur_hdr->cr + 1;
5680
5681 /* Now we have one header between cur_ptr and cur_end,
5682 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005683 */
5684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005685 /* The annoying part is that pattern matching needs
5686 * that we modify the contents to null-terminate all
5687 * strings before testing them.
5688 */
5689
5690 term = *cur_end;
5691 *cur_end = '\0';
5692
5693 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5694 switch (exp->action) {
5695 case ACT_SETBE:
5696 /* It is not possible to jump a second time.
5697 * FIXME: should we return an HTTP/500 here so that
5698 * the admin knows there's a problem ?
5699 */
5700 if (t->be != t->fe)
5701 break;
5702
5703 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005704 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005705 last_hdr = 1;
5706 break;
5707
5708 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005709 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005710 last_hdr = 1;
5711 break;
5712
5713 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005714 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005715 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005716
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005717 t->fe->fe_counters.denied_req++;
5718 if (t->fe != t->be)
5719 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005720 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005721 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005722
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005723 break;
5724
5725 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005726 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005727 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005728
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005729 t->fe->fe_counters.denied_req++;
5730 if (t->fe != t->be)
5731 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005732 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005733 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005734
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005735 break;
5736
5737 case ACT_REPLACE:
5738 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5739 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5740 /* FIXME: if the user adds a newline in the replacement, the
5741 * index will not be recalculated for now, and the new line
5742 * will not be counted as a new header.
5743 */
5744
5745 cur_end += delta;
5746 cur_next += delta;
5747 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005748 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005749 break;
5750
5751 case ACT_REMOVE:
5752 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5753 cur_next += delta;
5754
Willy Tarreaufa355d42009-11-29 18:12:29 +01005755 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005756 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5757 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005758 cur_hdr->len = 0;
5759 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005760 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005761 break;
5762
5763 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005764 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005765 if (cur_end)
5766 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005767
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005768 /* keep the link from this header to next one in case of later
5769 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005770 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005771 old_idx = cur_idx;
5772 }
5773 return 0;
5774}
5775
5776
5777/* Apply the filter to the request line.
5778 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5779 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005780 * Since it can manage the switch to another backend, it updates the per-proxy
5781 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005782 */
5783int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5784{
5785 char term;
5786 char *cur_ptr, *cur_end;
5787 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005788 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005789 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005790
Willy Tarreau58f10d72006-12-04 02:26:12 +01005791
Willy Tarreau3d300592007-03-18 18:34:41 +01005792 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005793 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005794 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005795 (exp->action == ACT_ALLOW ||
5796 exp->action == ACT_DENY ||
5797 exp->action == ACT_TARPIT))
5798 return 0;
5799 else if (exp->action == ACT_REMOVE)
5800 return 0;
5801
5802 done = 0;
5803
Willy Tarreau962c3f42010-01-10 00:15:35 +01005804 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005805 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005806
5807 /* Now we have the request line between cur_ptr and cur_end */
5808
5809 /* The annoying part is that pattern matching needs
5810 * that we modify the contents to null-terminate all
5811 * strings before testing them.
5812 */
5813
5814 term = *cur_end;
5815 *cur_end = '\0';
5816
5817 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5818 switch (exp->action) {
5819 case ACT_SETBE:
5820 /* It is not possible to jump a second time.
5821 * FIXME: should we return an HTTP/500 here so that
5822 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005823 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005824 if (t->be != t->fe)
5825 break;
5826
5827 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005828 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005829 done = 1;
5830 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005831
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005832 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005833 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005834 done = 1;
5835 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005836
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005837 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005838 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005839
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005840 t->fe->fe_counters.denied_req++;
5841 if (t->fe != t->be)
5842 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005843 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005844 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005845
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005846 done = 1;
5847 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005848
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005849 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005850 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005851
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005852 t->fe->fe_counters.denied_req++;
5853 if (t->fe != t->be)
5854 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005855 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005856 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005857
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005858 done = 1;
5859 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005860
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005861 case ACT_REPLACE:
5862 *cur_end = term; /* restore the string terminator */
5863 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5864 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5865 /* FIXME: if the user adds a newline in the replacement, the
5866 * index will not be recalculated for now, and the new line
5867 * will not be counted as a new header.
5868 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005869
Willy Tarreaufa355d42009-11-29 18:12:29 +01005870 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005871 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005872 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005873 HTTP_MSG_RQMETH,
5874 cur_ptr, cur_end + 1,
5875 NULL, NULL);
5876 if (unlikely(!cur_end))
5877 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005878
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005879 /* we have a full request and we know that we have either a CR
5880 * or an LF at <ptr>.
5881 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005882 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5883 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005884 /* there is no point trying this regex on headers */
5885 return 1;
5886 }
5887 }
5888 *cur_end = term; /* restore the string terminator */
5889 return done;
5890}
Willy Tarreau97de6242006-12-27 17:18:38 +01005891
Willy Tarreau58f10d72006-12-04 02:26:12 +01005892
Willy Tarreau58f10d72006-12-04 02:26:12 +01005893
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005894/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005895 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005896 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005897 * unparsable request. Since it can manage the switch to another backend, it
5898 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005899 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005900int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005901{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005902 struct http_txn *txn = &s->txn;
5903 struct hdr_exp *exp;
5904
5905 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005906 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005907
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005908 /*
5909 * The interleaving of transformations and verdicts
5910 * makes it difficult to decide to continue or stop
5911 * the evaluation.
5912 */
5913
Willy Tarreau6c123b12010-01-28 20:22:06 +01005914 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5915 break;
5916
Willy Tarreau3d300592007-03-18 18:34:41 +01005917 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005918 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005919 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005920 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005921
5922 /* if this filter had a condition, evaluate it now and skip to
5923 * next filter if the condition does not match.
5924 */
5925 if (exp->cond) {
5926 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5927 ret = acl_pass(ret);
5928 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5929 ret = !ret;
5930
5931 if (!ret)
5932 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005933 }
5934
5935 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005936 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005937 if (unlikely(ret < 0))
5938 return -1;
5939
5940 if (likely(ret == 0)) {
5941 /* The filter did not match the request, it can be
5942 * iterated through all headers.
5943 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005944 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005945 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005946 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005947 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005948}
5949
5950
Willy Tarreaua15645d2007-03-18 16:22:39 +01005951
Willy Tarreau58f10d72006-12-04 02:26:12 +01005952/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005953 * Try to retrieve the server associated to the appsession.
5954 * If the server is found, it's assigned to the session.
5955 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005956void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005957 struct http_txn *txn = &t->txn;
5958 appsess *asession = NULL;
5959 char *sessid_temp = NULL;
5960
Cyril Bontéb21570a2009-11-29 20:04:48 +01005961 if (len > t->be->appsession_len) {
5962 len = t->be->appsession_len;
5963 }
5964
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005965 if (t->be->options2 & PR_O2_AS_REQL) {
5966 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005967 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005968 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005969 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005970 }
5971
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005972 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005973 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5974 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5975 return;
5976 }
5977
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005978 memcpy(txn->sessid, buf, len);
5979 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005980 }
5981
5982 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5983 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5984 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5985 return;
5986 }
5987
Cyril Bontéb21570a2009-11-29 20:04:48 +01005988 memcpy(sessid_temp, buf, len);
5989 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005990
5991 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5992 /* free previously allocated memory */
5993 pool_free2(apools.sessid, sessid_temp);
5994
5995 if (asession != NULL) {
5996 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5997 if (!(t->be->options2 & PR_O2_AS_REQL))
5998 asession->request_count++;
5999
6000 if (asession->serverid != NULL) {
6001 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006002
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006003 while (srv) {
6004 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006005 if ((srv->state & SRV_RUNNING) ||
6006 (t->be->options & PR_O_PERSIST) ||
6007 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006008 /* we found the server and it's usable */
6009 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006010 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006011 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006012 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01006013
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006014 break;
6015 } else {
6016 txn->flags &= ~TX_CK_MASK;
6017 txn->flags |= TX_CK_DOWN;
6018 }
6019 }
6020 srv = srv->next;
6021 }
6022 }
6023 }
6024}
6025
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006026/* Find the end of a cookie value contained between <s> and <e>. It works the
6027 * same way as with headers above except that the semi-colon also ends a token.
6028 * See RFC2965 for more information. Note that it requires a valid header to
6029 * return a valid result.
6030 */
6031char *find_cookie_value_end(char *s, const char *e)
6032{
6033 int quoted, qdpair;
6034
6035 quoted = qdpair = 0;
6036 for (; s < e; s++) {
6037 if (qdpair) qdpair = 0;
6038 else if (quoted) {
6039 if (*s == '\\') qdpair = 1;
6040 else if (*s == '"') quoted = 0;
6041 }
6042 else if (*s == '"') quoted = 1;
6043 else if (*s == ',' || *s == ';') return s;
6044 }
6045 return s;
6046}
6047
6048/* Delete a value in a header between delimiters <from> and <next> in buffer
6049 * <buf>. The number of characters displaced is returned, and the pointer to
6050 * the first delimiter is updated if required. The function tries as much as
6051 * possible to respect the following principles :
6052 * - replace <from> delimiter by the <next> one unless <from> points to a
6053 * colon, in which case <next> is simply removed
6054 * - set exactly one space character after the new first delimiter, unless
6055 * there are not enough characters in the block being moved to do so.
6056 * - remove unneeded spaces before the previous delimiter and after the new
6057 * one.
6058 *
6059 * It is the caller's responsibility to ensure that :
6060 * - <from> points to a valid delimiter or the colon ;
6061 * - <next> points to a valid delimiter or the final CR/LF ;
6062 * - there are non-space chars before <from> ;
6063 * - there is a CR/LF at or after <next>.
6064 */
6065int del_hdr_value(struct buffer *buf, char **from, char *next)
6066{
6067 char *prev = *from;
6068
6069 if (*prev == ':') {
6070 /* We're removing the first value, preserve the colon and add a
6071 * space if possible.
6072 */
6073 if (!http_is_crlf[(unsigned char)*next])
6074 next++;
6075 prev++;
6076 if (prev < next)
6077 *prev++ = ' ';
6078
6079 while (http_is_spht[(unsigned char)*next])
6080 next++;
6081 } else {
6082 /* Remove useless spaces before the old delimiter. */
6083 while (http_is_spht[(unsigned char)*(prev-1)])
6084 prev--;
6085 *from = prev;
6086
6087 /* copy the delimiter and if possible a space if we're
6088 * not at the end of the line.
6089 */
6090 if (!http_is_crlf[(unsigned char)*next]) {
6091 *prev++ = *next++;
6092 if (prev + 1 < next)
6093 *prev++ = ' ';
6094 while (http_is_spht[(unsigned char)*next])
6095 next++;
6096 }
6097 }
6098 return buffer_replace2(buf, prev, next, NULL, 0);
6099}
6100
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006101/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006102 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006103 * desirable to call it only when needed. This code is quite complex because
6104 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6105 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006106 */
6107void manage_client_side_cookies(struct session *t, struct buffer *req)
6108{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006109 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006110 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006111 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006112 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6113 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006114
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006115 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006116 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006117 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006118
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006119 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006120 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006121 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006122
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006123 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006124 hdr_beg = hdr_next;
6125 hdr_end = hdr_beg + cur_hdr->len;
6126 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006127
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006128 /* We have one full header between hdr_beg and hdr_end, and the
6129 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006130 * "Cookie:" headers.
6131 */
6132
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006133 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006134 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006135 old_idx = cur_idx;
6136 continue;
6137 }
6138
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006139 del_from = NULL; /* nothing to be deleted */
6140 preserve_hdr = 0; /* assume we may kill the whole header */
6141
Willy Tarreau58f10d72006-12-04 02:26:12 +01006142 /* Now look for cookies. Conforming to RFC2109, we have to support
6143 * attributes whose name begin with a '$', and associate them with
6144 * the right cookie, if we want to delete this cookie.
6145 * So there are 3 cases for each cookie read :
6146 * 1) it's a special attribute, beginning with a '$' : ignore it.
6147 * 2) it's a server id cookie that we *MAY* want to delete : save
6148 * some pointers on it (last semi-colon, beginning of cookie...)
6149 * 3) it's an application cookie : we *MAY* have to delete a previous
6150 * "special" cookie.
6151 * At the end of loop, if a "special" cookie remains, we may have to
6152 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006153 * *MUST* delete it.
6154 *
6155 * Note: RFC2965 is unclear about the processing of spaces around
6156 * the equal sign in the ATTR=VALUE form. A careful inspection of
6157 * the RFC explicitly allows spaces before it, and not within the
6158 * tokens (attrs or values). An inspection of RFC2109 allows that
6159 * too but section 10.1.3 lets one think that spaces may be allowed
6160 * after the equal sign too, resulting in some (rare) buggy
6161 * implementations trying to do that. So let's do what servers do.
6162 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6163 * allowed quoted strings in values, with any possible character
6164 * after a backslash, including control chars and delimitors, which
6165 * causes parsing to become ambiguous. Browsers also allow spaces
6166 * within values even without quotes.
6167 *
6168 * We have to keep multiple pointers in order to support cookie
6169 * removal at the beginning, middle or end of header without
6170 * corrupting the header. All of these headers are valid :
6171 *
6172 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6173 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6174 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6175 * | | | | | | | | |
6176 * | | | | | | | | hdr_end <--+
6177 * | | | | | | | +--> next
6178 * | | | | | | +----> val_end
6179 * | | | | | +-----------> val_beg
6180 * | | | | +--------------> equal
6181 * | | | +----------------> att_end
6182 * | | +---------------------> att_beg
6183 * | +--------------------------> prev
6184 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006185 */
6186
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006187 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6188 /* Iterate through all cookies on this line */
6189
6190 /* find att_beg */
6191 att_beg = prev + 1;
6192 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6193 att_beg++;
6194
6195 /* find att_end : this is the first character after the last non
6196 * space before the equal. It may be equal to hdr_end.
6197 */
6198 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006199
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006200 while (equal < hdr_end) {
6201 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006202 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006203 if (http_is_spht[(unsigned char)*equal++])
6204 continue;
6205 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006206 }
6207
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006208 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6209 * is between <att_beg> and <equal>, both may be identical.
6210 */
6211
6212 /* look for end of cookie if there is an equal sign */
6213 if (equal < hdr_end && *equal == '=') {
6214 /* look for the beginning of the value */
6215 val_beg = equal + 1;
6216 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6217 val_beg++;
6218
6219 /* find the end of the value, respecting quotes */
6220 next = find_cookie_value_end(val_beg, hdr_end);
6221
6222 /* make val_end point to the first white space or delimitor after the value */
6223 val_end = next;
6224 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6225 val_end--;
6226 } else {
6227 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006228 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006229
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006230 /* We have nothing to do with attributes beginning with '$'. However,
6231 * they will automatically be removed if a header before them is removed,
6232 * since they're supposed to be linked together.
6233 */
6234 if (*att_beg == '$')
6235 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006236
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006237 /* Ignore cookies with no equal sign */
6238 if (equal == next) {
6239 /* This is not our cookie, so we must preserve it. But if we already
6240 * scheduled another cookie for removal, we cannot remove the
6241 * complete header, but we can remove the previous block itself.
6242 */
6243 preserve_hdr = 1;
6244 if (del_from != NULL) {
6245 int delta = del_hdr_value(req, &del_from, prev);
6246 val_end += delta;
6247 next += delta;
6248 hdr_end += delta;
6249 hdr_next += delta;
6250 cur_hdr->len += delta;
6251 http_msg_move_end(&txn->req, delta);
6252 prev = del_from;
6253 del_from = NULL;
6254 }
6255 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006256 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006258 /* if there are spaces around the equal sign, we need to
6259 * strip them otherwise we'll get trouble for cookie captures,
6260 * or even for rewrites. Since this happens extremely rarely,
6261 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006262 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006263 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6264 int stripped_before = 0;
6265 int stripped_after = 0;
6266
6267 if (att_end != equal) {
6268 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6269 equal += stripped_before;
6270 val_beg += stripped_before;
6271 }
6272
6273 if (val_beg > equal + 1) {
6274 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6275 val_beg += stripped_after;
6276 stripped_before += stripped_after;
6277 }
6278
6279 val_end += stripped_before;
6280 next += stripped_before;
6281 hdr_end += stripped_before;
6282 hdr_next += stripped_before;
6283 cur_hdr->len += stripped_before;
6284 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006285 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006286 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006287
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 /* First, let's see if we want to capture this cookie. We check
6289 * that we don't already have a client side cookie, because we
6290 * can only capture one. Also as an optimisation, we ignore
6291 * cookies shorter than the declared name.
6292 */
6293 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6294 (val_end - att_beg >= t->fe->capture_namelen) &&
6295 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6296 int log_len = val_end - att_beg;
6297
6298 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6299 Alert("HTTP logging : out of memory.\n");
6300 } else {
6301 if (log_len > t->fe->capture_len)
6302 log_len = t->fe->capture_len;
6303 memcpy(txn->cli_cookie, att_beg, log_len);
6304 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006305 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006306 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006307
Willy Tarreaubca99692010-10-06 19:25:55 +02006308 /* Persistence cookies in passive, rewrite or insert mode have the
6309 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006310 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006311 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006312 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006313 * For cookies in prefix mode, the form is :
6314 *
6315 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006316 */
6317 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6318 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6319 struct server *srv = t->be->srv;
6320 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006321
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006322 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6323 * have the server ID between val_beg and delim, and the original cookie between
6324 * delim+1 and val_end. Otherwise, delim==val_end :
6325 *
6326 * Cookie: NAME=SRV; # in all but prefix modes
6327 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6328 * | || || | |+-> next
6329 * | || || | +--> val_end
6330 * | || || +---------> delim
6331 * | || |+------------> val_beg
6332 * | || +-------------> att_end = equal
6333 * | |+-----------------> att_beg
6334 * | +------------------> prev
6335 * +-------------------------> hdr_beg
6336 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006337
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006338 if (t->be->options & PR_O_COOK_PFX) {
6339 for (delim = val_beg; delim < val_end; delim++)
6340 if (*delim == COOKIE_DELIM)
6341 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006342 } else {
6343 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006344 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006345 /* Now check if the cookie contains a date field, which would
6346 * appear after a vertical bar ('|') just after the server name
6347 * and before the delimiter.
6348 */
6349 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6350 if (vbar1) {
6351 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006352 * right is the last seen date. It is a base64 encoded
6353 * 30-bit value representing the UNIX date since the
6354 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006355 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006356 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006357 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006358 if (val_end - vbar1 >= 5) {
6359 val = b64tos30(vbar1);
6360 if (val > 0)
6361 txn->cookie_last_date = val << 2;
6362 }
6363 /* look for a second vertical bar */
6364 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6365 if (vbar1 && (val_end - vbar1 > 5)) {
6366 val = b64tos30(vbar1 + 1);
6367 if (val > 0)
6368 txn->cookie_first_date = val << 2;
6369 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006370 }
6371 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006372
Willy Tarreauf64d1412010-10-07 20:06:11 +02006373 /* if the cookie has an expiration date and the proxy wants to check
6374 * it, then we do that now. We first check if the cookie is too old,
6375 * then only if it has expired. We detect strict overflow because the
6376 * time resolution here is not great (4 seconds). Cookies with dates
6377 * in the future are ignored if their offset is beyond one day. This
6378 * allows an admin to fix timezone issues without expiring everyone
6379 * and at the same time avoids keeping unwanted side effects for too
6380 * long.
6381 */
6382 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006383 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6384 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006385 txn->flags &= ~TX_CK_MASK;
6386 txn->flags |= TX_CK_OLD;
6387 delim = val_beg; // let's pretend we have not found the cookie
6388 txn->cookie_first_date = 0;
6389 txn->cookie_last_date = 0;
6390 }
6391 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006392 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6393 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006394 txn->flags &= ~TX_CK_MASK;
6395 txn->flags |= TX_CK_EXPIRED;
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
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006401 /* Here, we'll look for the first running server which supports the cookie.
6402 * This allows to share a same cookie between several servers, for example
6403 * to dedicate backup servers to specific servers only.
6404 * However, to prevent clients from sticking to cookie-less backup server
6405 * when they have incidentely learned an empty cookie, we simply ignore
6406 * empty cookies and mark them as invalid.
6407 * The same behaviour is applied when persistence must be ignored.
6408 */
6409 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6410 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006411
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006412 while (srv) {
6413 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6414 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6415 if ((srv->state & SRV_RUNNING) ||
6416 (t->be->options & PR_O_PERSIST) ||
6417 (t->flags & SN_FORCE_PRST)) {
6418 /* we found the server and we can use it */
6419 txn->flags &= ~TX_CK_MASK;
6420 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6421 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006422 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006423 break;
6424 } else {
6425 /* we found a server, but it's down,
6426 * mark it as such and go on in case
6427 * another one is available.
6428 */
6429 txn->flags &= ~TX_CK_MASK;
6430 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006431 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006432 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006433 srv = srv->next;
6434 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006435
Willy Tarreauf64d1412010-10-07 20:06:11 +02006436 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006437 /* no server matched this cookie */
6438 txn->flags &= ~TX_CK_MASK;
6439 txn->flags |= TX_CK_INVALID;
6440 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006441
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006442 /* depending on the cookie mode, we may have to either :
6443 * - delete the complete cookie if we're in insert+indirect mode, so that
6444 * the server never sees it ;
6445 * - remove the server id from the cookie value, and tag the cookie as an
6446 * application cookie so that it does not get accidentely removed later,
6447 * if we're in cookie prefix mode
6448 */
6449 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6450 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006451
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006452 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6453 val_end += delta;
6454 next += delta;
6455 hdr_end += delta;
6456 hdr_next += delta;
6457 cur_hdr->len += delta;
6458 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006459
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006460 del_from = NULL;
6461 preserve_hdr = 1; /* we want to keep this cookie */
6462 }
6463 else if (del_from == NULL &&
6464 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6465 del_from = prev;
6466 }
6467 } else {
6468 /* This is not our cookie, so we must preserve it. But if we already
6469 * scheduled another cookie for removal, we cannot remove the
6470 * complete header, but we can remove the previous block itself.
6471 */
6472 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006473
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006474 if (del_from != NULL) {
6475 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006476 if (att_beg >= del_from)
6477 att_beg += delta;
6478 if (att_end >= del_from)
6479 att_end += delta;
6480 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006481 val_end += delta;
6482 next += delta;
6483 hdr_end += delta;
6484 hdr_next += delta;
6485 cur_hdr->len += delta;
6486 http_msg_move_end(&txn->req, delta);
6487 prev = del_from;
6488 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006489 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006490 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006491
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006492 /* Look for the appsession cookie unless persistence must be ignored */
6493 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6494 int cmp_len, value_len;
6495 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006496
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006497 if (t->be->options2 & PR_O2_AS_PFX) {
6498 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6499 value_begin = att_beg + t->be->appsession_name_len;
6500 value_len = val_end - att_beg - t->be->appsession_name_len;
6501 } else {
6502 cmp_len = att_end - att_beg;
6503 value_begin = val_beg;
6504 value_len = val_end - val_beg;
6505 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006506
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006507 /* let's see if the cookie is our appcookie */
6508 if (cmp_len == t->be->appsession_name_len &&
6509 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6510 manage_client_side_appsession(t, value_begin, value_len);
6511 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006512 }
6513
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006514 /* continue with next cookie on this header line */
6515 att_beg = next;
6516 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006517
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006518 /* There are no more cookies on this line.
6519 * We may still have one (or several) marked for deletion at the
6520 * end of the line. We must do this now in two ways :
6521 * - if some cookies must be preserved, we only delete from the
6522 * mark to the end of line ;
6523 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006524 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006525 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006526 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006527 if (preserve_hdr) {
6528 delta = del_hdr_value(req, &del_from, hdr_end);
6529 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006530 cur_hdr->len += delta;
6531 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006532 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006533
6534 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006535 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6536 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006537 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006538 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006539 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006540 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006541 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006542 }
6543
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006544 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006545 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006546 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006547}
6548
6549
Willy Tarreaua15645d2007-03-18 16:22:39 +01006550/* Iterate the same filter through all response headers contained in <rtr>.
6551 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6552 */
6553int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6554{
6555 char term;
6556 char *cur_ptr, *cur_end, *cur_next;
6557 int cur_idx, old_idx, last_hdr;
6558 struct http_txn *txn = &t->txn;
6559 struct hdr_idx_elem *cur_hdr;
6560 int len, delta;
6561
6562 last_hdr = 0;
6563
Willy Tarreau962c3f42010-01-10 00:15:35 +01006564 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006565 old_idx = 0;
6566
6567 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006568 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006569 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006570 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006571 (exp->action == ACT_ALLOW ||
6572 exp->action == ACT_DENY))
6573 return 0;
6574
6575 cur_idx = txn->hdr_idx.v[old_idx].next;
6576 if (!cur_idx)
6577 break;
6578
6579 cur_hdr = &txn->hdr_idx.v[cur_idx];
6580 cur_ptr = cur_next;
6581 cur_end = cur_ptr + cur_hdr->len;
6582 cur_next = cur_end + cur_hdr->cr + 1;
6583
6584 /* Now we have one header between cur_ptr and cur_end,
6585 * and the next header starts at cur_next.
6586 */
6587
6588 /* The annoying part is that pattern matching needs
6589 * that we modify the contents to null-terminate all
6590 * strings before testing them.
6591 */
6592
6593 term = *cur_end;
6594 *cur_end = '\0';
6595
6596 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6597 switch (exp->action) {
6598 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006599 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006600 last_hdr = 1;
6601 break;
6602
6603 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006604 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006605 last_hdr = 1;
6606 break;
6607
6608 case ACT_REPLACE:
6609 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6610 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6611 /* FIXME: if the user adds a newline in the replacement, the
6612 * index will not be recalculated for now, and the new line
6613 * will not be counted as a new header.
6614 */
6615
6616 cur_end += delta;
6617 cur_next += delta;
6618 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006619 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006620 break;
6621
6622 case ACT_REMOVE:
6623 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6624 cur_next += delta;
6625
Willy Tarreaufa355d42009-11-29 18:12:29 +01006626 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006627 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6628 txn->hdr_idx.used--;
6629 cur_hdr->len = 0;
6630 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006631 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006632 break;
6633
6634 }
6635 }
6636 if (cur_end)
6637 *cur_end = term; /* restore the string terminator */
6638
6639 /* keep the link from this header to next one in case of later
6640 * removal of next header.
6641 */
6642 old_idx = cur_idx;
6643 }
6644 return 0;
6645}
6646
6647
6648/* Apply the filter to the status line in the response buffer <rtr>.
6649 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6650 * or -1 if a replacement resulted in an invalid status line.
6651 */
6652int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6653{
6654 char term;
6655 char *cur_ptr, *cur_end;
6656 int done;
6657 struct http_txn *txn = &t->txn;
6658 int len, delta;
6659
6660
Willy Tarreau3d300592007-03-18 18:34:41 +01006661 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006662 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006663 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006664 (exp->action == ACT_ALLOW ||
6665 exp->action == ACT_DENY))
6666 return 0;
6667 else if (exp->action == ACT_REMOVE)
6668 return 0;
6669
6670 done = 0;
6671
Willy Tarreau962c3f42010-01-10 00:15:35 +01006672 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006673 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006674
6675 /* Now we have the status line between cur_ptr and cur_end */
6676
6677 /* The annoying part is that pattern matching needs
6678 * that we modify the contents to null-terminate all
6679 * strings before testing them.
6680 */
6681
6682 term = *cur_end;
6683 *cur_end = '\0';
6684
6685 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6686 switch (exp->action) {
6687 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006688 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006689 done = 1;
6690 break;
6691
6692 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006693 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006694 done = 1;
6695 break;
6696
6697 case ACT_REPLACE:
6698 *cur_end = term; /* restore the string terminator */
6699 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6700 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6701 /* FIXME: if the user adds a newline in the replacement, the
6702 * index will not be recalculated for now, and the new line
6703 * will not be counted as a new header.
6704 */
6705
Willy Tarreaufa355d42009-11-29 18:12:29 +01006706 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006707 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006709 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006710 cur_ptr, cur_end + 1,
6711 NULL, NULL);
6712 if (unlikely(!cur_end))
6713 return -1;
6714
6715 /* we have a full respnse and we know that we have either a CR
6716 * or an LF at <ptr>.
6717 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006718 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006719 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006720 /* there is no point trying this regex on headers */
6721 return 1;
6722 }
6723 }
6724 *cur_end = term; /* restore the string terminator */
6725 return done;
6726}
6727
6728
6729
6730/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006731 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006732 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6733 * unparsable response.
6734 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006735int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006736{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006737 struct http_txn *txn = &s->txn;
6738 struct hdr_exp *exp;
6739
6740 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006741 int ret;
6742
6743 /*
6744 * The interleaving of transformations and verdicts
6745 * makes it difficult to decide to continue or stop
6746 * the evaluation.
6747 */
6748
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006749 if (txn->flags & TX_SVDENY)
6750 break;
6751
Willy Tarreau3d300592007-03-18 18:34:41 +01006752 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006753 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6754 exp->action == ACT_PASS)) {
6755 exp = exp->next;
6756 continue;
6757 }
6758
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006759 /* if this filter had a condition, evaluate it now and skip to
6760 * next filter if the condition does not match.
6761 */
6762 if (exp->cond) {
6763 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6764 ret = acl_pass(ret);
6765 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6766 ret = !ret;
6767 if (!ret)
6768 continue;
6769 }
6770
Willy Tarreaua15645d2007-03-18 16:22:39 +01006771 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006772 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006773 if (unlikely(ret < 0))
6774 return -1;
6775
6776 if (likely(ret == 0)) {
6777 /* The filter did not match the response, it can be
6778 * iterated through all headers.
6779 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006780 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006781 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006782 }
6783 return 0;
6784}
6785
6786
Willy Tarreaua15645d2007-03-18 16:22:39 +01006787/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006788 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006789 * desirable to call it only when needed. This function is also used when we
6790 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006791 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006792void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006793{
6794 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006795 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006796 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006797 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006798 char *hdr_beg, *hdr_end, *hdr_next;
6799 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006800
Willy Tarreaua15645d2007-03-18 16:22:39 +01006801 /* Iterate through the headers.
6802 * we start with the start line.
6803 */
6804 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006805 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006806
6807 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6808 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006809 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006810
6811 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006812 hdr_beg = hdr_next;
6813 hdr_end = hdr_beg + cur_hdr->len;
6814 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006815
Willy Tarreau24581ba2010-08-31 22:39:35 +02006816 /* We have one full header between hdr_beg and hdr_end, and the
6817 * next header starts at hdr_next. We're only interested in
6818 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006819 */
6820
Willy Tarreau24581ba2010-08-31 22:39:35 +02006821 is_cookie2 = 0;
6822 prev = hdr_beg + 10;
6823 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006824 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006825 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6826 if (!val) {
6827 old_idx = cur_idx;
6828 continue;
6829 }
6830 is_cookie2 = 1;
6831 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006832 }
6833
Willy Tarreau24581ba2010-08-31 22:39:35 +02006834 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6835 * <prev> points to the colon.
6836 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006837 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006838
Willy Tarreau24581ba2010-08-31 22:39:35 +02006839 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6840 * check-cache is enabled) and we are not interested in checking
6841 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006842 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006843 if (t->be->cookie_name == NULL &&
6844 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006845 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006846 return;
6847
Willy Tarreau24581ba2010-08-31 22:39:35 +02006848 /* OK so now we know we have to process this response cookie.
6849 * The format of the Set-Cookie header is slightly different
6850 * from the format of the Cookie header in that it does not
6851 * support the comma as a cookie delimiter (thus the header
6852 * cannot be folded) because the Expires attribute described in
6853 * the original Netscape's spec may contain an unquoted date
6854 * with a comma inside. We have to live with this because
6855 * many browsers don't support Max-Age and some browsers don't
6856 * support quoted strings. However the Set-Cookie2 header is
6857 * clean.
6858 *
6859 * We have to keep multiple pointers in order to support cookie
6860 * removal at the beginning, middle or end of header without
6861 * corrupting the header (in case of set-cookie2). A special
6862 * pointer, <scav> points to the beginning of the set-cookie-av
6863 * fields after the first semi-colon. The <next> pointer points
6864 * either to the end of line (set-cookie) or next unquoted comma
6865 * (set-cookie2). All of these headers are valid :
6866 *
6867 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6868 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6869 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6870 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6871 * | | | | | | | | | |
6872 * | | | | | | | | +-> next hdr_end <--+
6873 * | | | | | | | +------------> scav
6874 * | | | | | | +--------------> val_end
6875 * | | | | | +--------------------> val_beg
6876 * | | | | +----------------------> equal
6877 * | | | +------------------------> att_end
6878 * | | +----------------------------> att_beg
6879 * | +------------------------------> prev
6880 * +-----------------------------------------> hdr_beg
6881 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006882
Willy Tarreau24581ba2010-08-31 22:39:35 +02006883 for (; prev < hdr_end; prev = next) {
6884 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006885
Willy Tarreau24581ba2010-08-31 22:39:35 +02006886 /* find att_beg */
6887 att_beg = prev + 1;
6888 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6889 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006890
Willy Tarreau24581ba2010-08-31 22:39:35 +02006891 /* find att_end : this is the first character after the last non
6892 * space before the equal. It may be equal to hdr_end.
6893 */
6894 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006895
Willy Tarreau24581ba2010-08-31 22:39:35 +02006896 while (equal < hdr_end) {
6897 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6898 break;
6899 if (http_is_spht[(unsigned char)*equal++])
6900 continue;
6901 att_end = equal;
6902 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006903
Willy Tarreau24581ba2010-08-31 22:39:35 +02006904 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6905 * is between <att_beg> and <equal>, both may be identical.
6906 */
6907
6908 /* look for end of cookie if there is an equal sign */
6909 if (equal < hdr_end && *equal == '=') {
6910 /* look for the beginning of the value */
6911 val_beg = equal + 1;
6912 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6913 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006914
Willy Tarreau24581ba2010-08-31 22:39:35 +02006915 /* find the end of the value, respecting quotes */
6916 next = find_cookie_value_end(val_beg, hdr_end);
6917
6918 /* make val_end point to the first white space or delimitor after the value */
6919 val_end = next;
6920 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6921 val_end--;
6922 } else {
6923 /* <equal> points to next comma, semi-colon or EOL */
6924 val_beg = val_end = next = equal;
6925 }
6926
6927 if (next < hdr_end) {
6928 /* Set-Cookie2 supports multiple cookies, and <next> points to
6929 * a colon or semi-colon before the end. So skip all attr-value
6930 * pairs and look for the next comma. For Set-Cookie, since
6931 * commas are permitted in values, skip to the end.
6932 */
6933 if (is_cookie2)
6934 next = find_hdr_value_end(next, hdr_end);
6935 else
6936 next = hdr_end;
6937 }
6938
6939 /* Now everything is as on the diagram above */
6940
6941 /* Ignore cookies with no equal sign */
6942 if (equal == val_end)
6943 continue;
6944
6945 /* If there are spaces around the equal sign, we need to
6946 * strip them otherwise we'll get trouble for cookie captures,
6947 * or even for rewrites. Since this happens extremely rarely,
6948 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006949 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006950 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6951 int stripped_before = 0;
6952 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006953
Willy Tarreau24581ba2010-08-31 22:39:35 +02006954 if (att_end != equal) {
6955 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6956 equal += stripped_before;
6957 val_beg += stripped_before;
6958 }
6959
6960 if (val_beg > equal + 1) {
6961 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6962 val_beg += stripped_after;
6963 stripped_before += stripped_after;
6964 }
6965
6966 val_end += stripped_before;
6967 next += stripped_before;
6968 hdr_end += stripped_before;
6969 hdr_next += stripped_before;
6970 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006971 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006972 }
6973
6974 /* First, let's see if we want to capture this cookie. We check
6975 * that we don't already have a server side cookie, because we
6976 * can only capture one. Also as an optimisation, we ignore
6977 * cookies shorter than the declared name.
6978 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006979 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006980 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006981 (val_end - att_beg >= t->fe->capture_namelen) &&
6982 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6983 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006984 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006985 Alert("HTTP logging : out of memory.\n");
6986 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006987 else {
6988 if (log_len > t->fe->capture_len)
6989 log_len = t->fe->capture_len;
6990 memcpy(txn->srv_cookie, att_beg, log_len);
6991 txn->srv_cookie[log_len] = 0;
6992 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006993 }
6994
Willy Tarreau827aee92011-03-10 16:55:02 +01006995 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006996 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006997 if (!(t->flags & SN_IGNORE_PRST) &&
6998 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6999 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007000 /* assume passive cookie by default */
7001 txn->flags &= ~TX_SCK_MASK;
7002 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007003
7004 /* If the cookie is in insert mode on a known server, we'll delete
7005 * this occurrence because we'll insert another one later.
7006 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007007 * a direct access.
7008 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007009 if (t->be->options2 & PR_O2_COOK_PSV) {
7010 /* The "preserve" flag was set, we don't want to touch the
7011 * server's cookie.
7012 */
7013 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007014 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007015 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007016 /* this cookie must be deleted */
7017 if (*prev == ':' && next == hdr_end) {
7018 /* whole header */
7019 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
7020 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7021 txn->hdr_idx.used--;
7022 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007023 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007024 hdr_next += delta;
7025 http_msg_move_end(&txn->rsp, delta);
7026 /* note: while both invalid now, <next> and <hdr_end>
7027 * are still equal, so the for() will stop as expected.
7028 */
7029 } else {
7030 /* just remove the value */
7031 int delta = del_hdr_value(res, &prev, next);
7032 next = prev;
7033 hdr_end += delta;
7034 hdr_next += delta;
7035 cur_hdr->len += delta;
7036 http_msg_move_end(&txn->rsp, delta);
7037 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007038 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007039 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007040 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007041 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007042 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007043 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007044 * with this server since we know it.
7045 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007046 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007047 next += delta;
7048 hdr_end += delta;
7049 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007050 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007051 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007052
Willy Tarreauf1348312010-10-07 15:54:11 +02007053 txn->flags &= ~TX_SCK_MASK;
7054 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007055 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007056 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007057 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007058 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007059 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007060 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007061 next += delta;
7062 hdr_end += delta;
7063 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007064 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007065 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007066
Willy Tarreau827aee92011-03-10 16:55:02 +01007067 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007068 txn->flags &= ~TX_SCK_MASK;
7069 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007070 }
7071 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007072 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7073 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007074 int cmp_len, value_len;
7075 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007076
Cyril Bontéb21570a2009-11-29 20:04:48 +01007077 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007078 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7079 value_begin = att_beg + t->be->appsession_name_len;
7080 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007081 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007082 cmp_len = att_end - att_beg;
7083 value_begin = val_beg;
7084 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007085 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007086
Cyril Bonté17530c32010-04-06 21:11:10 +02007087 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007088 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7089 /* free a possibly previously allocated memory */
7090 pool_free2(apools.sessid, txn->sessid);
7091
Cyril Bontéb21570a2009-11-29 20:04:48 +01007092 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007093 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007094 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7095 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7096 return;
7097 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007098 memcpy(txn->sessid, value_begin, value_len);
7099 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007100 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007101 }
7102 /* that's done for this cookie, check the next one on the same
7103 * line when next != hdr_end (only if is_cookie2).
7104 */
7105 }
7106 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007107 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007108 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007109
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007110 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007111 appsess *asession = NULL;
7112 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007113 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007114 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007115 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007116 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7117 Alert("Not enough Memory process_srv():asession:calloc().\n");
7118 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7119 return;
7120 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007121 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7122
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007123 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7124 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7125 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007126 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007127 return;
7128 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007129 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007130 asession->sessid[t->be->appsession_len] = 0;
7131
Willy Tarreau827aee92011-03-10 16:55:02 +01007132 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007133 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007134 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007135 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007136 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007137 return;
7138 }
7139 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01007140 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007141
7142 asession->request_count = 0;
7143 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7144 }
7145
7146 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7147 asession->request_count++;
7148 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007149}
7150
7151
Willy Tarreaua15645d2007-03-18 16:22:39 +01007152/*
7153 * Check if response is cacheable or not. Updates t->flags.
7154 */
7155void check_response_for_cacheability(struct session *t, struct buffer *rtr)
7156{
7157 struct http_txn *txn = &t->txn;
7158 char *p1, *p2;
7159
7160 char *cur_ptr, *cur_end, *cur_next;
7161 int cur_idx;
7162
Willy Tarreau5df51872007-11-25 16:20:08 +01007163 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007164 return;
7165
7166 /* Iterate through the headers.
7167 * we start with the start line.
7168 */
7169 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007170 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007171
7172 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7173 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007174 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007175
7176 cur_hdr = &txn->hdr_idx.v[cur_idx];
7177 cur_ptr = cur_next;
7178 cur_end = cur_ptr + cur_hdr->len;
7179 cur_next = cur_end + cur_hdr->cr + 1;
7180
7181 /* We have one full header between cur_ptr and cur_end, and the
7182 * next header starts at cur_next. We're only interested in
7183 * "Cookie:" headers.
7184 */
7185
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007186 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7187 if (val) {
7188 if ((cur_end - (cur_ptr + val) >= 8) &&
7189 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7190 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7191 return;
7192 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007193 }
7194
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007195 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7196 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007197 continue;
7198
7199 /* OK, right now we know we have a cache-control header at cur_ptr */
7200
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007201 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007202
7203 if (p1 >= cur_end) /* no more info */
7204 continue;
7205
7206 /* p1 is at the beginning of the value */
7207 p2 = p1;
7208
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007209 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007210 p2++;
7211
7212 /* we have a complete value between p1 and p2 */
7213 if (p2 < cur_end && *p2 == '=') {
7214 /* we have something of the form no-cache="set-cookie" */
7215 if ((cur_end - p1 >= 21) &&
7216 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7217 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007218 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007219 continue;
7220 }
7221
7222 /* OK, so we know that either p2 points to the end of string or to a comma */
7223 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7224 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7225 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7226 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007227 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007228 return;
7229 }
7230
7231 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007232 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233 continue;
7234 }
7235 }
7236}
7237
7238
Willy Tarreau58f10d72006-12-04 02:26:12 +01007239/*
7240 * Try to retrieve a known appsession in the URI, then the associated server.
7241 * If the server is found, it's assigned to the session.
7242 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007243void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007244{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007245 char *end_params, *first_param, *cur_param, *next_param;
7246 char separator;
7247 int value_len;
7248
7249 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007250
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007251 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007252 (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 +01007253 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007254 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007255
Cyril Bontéb21570a2009-11-29 20:04:48 +01007256 first_param = NULL;
7257 switch (mode) {
7258 case PR_O2_AS_M_PP:
7259 first_param = memchr(begin, ';', len);
7260 break;
7261 case PR_O2_AS_M_QS:
7262 first_param = memchr(begin, '?', len);
7263 break;
7264 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007265
Cyril Bontéb21570a2009-11-29 20:04:48 +01007266 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007267 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007268 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007269
Cyril Bontéb21570a2009-11-29 20:04:48 +01007270 switch (mode) {
7271 case PR_O2_AS_M_PP:
7272 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7273 end_params = (char *) begin + len;
7274 }
7275 separator = ';';
7276 break;
7277 case PR_O2_AS_M_QS:
7278 end_params = (char *) begin + len;
7279 separator = '&';
7280 break;
7281 default:
7282 /* unknown mode, shouldn't happen */
7283 return;
7284 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007285
Cyril Bontéb21570a2009-11-29 20:04:48 +01007286 cur_param = next_param = end_params;
7287 while (cur_param > first_param) {
7288 cur_param--;
7289 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7290 /* let's see if this is the appsession parameter */
7291 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7292 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7293 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7294 /* Cool... it's the right one */
7295 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7296 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7297 if (value_len > 0) {
7298 manage_client_side_appsession(t, cur_param, value_len);
7299 }
7300 break;
7301 }
7302 next_param = cur_param;
7303 }
7304 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007305#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007306 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007307 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007308#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007309}
7310
Willy Tarreaub2513902006-12-17 14:52:38 +01007311/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007312 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007313 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007314 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007315 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007316 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007317 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007318 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007319 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007320int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007321{
7322 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007323 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007324
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007325 if (!uri_auth)
7326 return 0;
7327
Cyril Bonté70be45d2010-10-12 00:14:35 +02007328 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007329 return 0;
7330
Willy Tarreau295a8372011-03-10 11:25:07 +01007331 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007332
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007333 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007334 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007335 return 0;
7336
Willy Tarreau962c3f42010-01-10 00:15:35 +01007337 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007338
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007339 /* the URI is in h */
7340 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007341 return 0;
7342
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007343 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007344 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007345 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007346 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007347 break;
7348 }
7349 h++;
7350 }
7351
7352 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007353 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7354 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007355 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007356 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007357 break;
7358 }
7359 h++;
7360 }
7361 }
7362
Willy Tarreau962c3f42010-01-10 00:15:35 +01007363 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7364 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007365 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007366 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007367 break;
7368 }
7369 h++;
7370 }
7371
Cyril Bonté70be45d2010-10-12 00:14:35 +02007372 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 - 8) {
7374 if (memcmp(h, ";st=", 4) == 0) {
7375 h += 4;
7376
7377 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007378 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007379 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007380 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007381 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007382 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007383 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007384 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007385 else
Willy Tarreau295a8372011-03-10 11:25:07 +01007386 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007387 break;
7388 }
7389 h++;
7390 }
7391
Willy Tarreau295a8372011-03-10 11:25:07 +01007392 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007393
Willy Tarreaub2513902006-12-17 14:52:38 +01007394 return 1;
7395}
7396
Willy Tarreau4076a152009-04-02 15:18:36 +02007397/*
7398 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007399 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7400 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007401 */
7402void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7403 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007404 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007405{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007406 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7407 int len1 = buf->size - msg->som;
7408 es->len = buf->r - (buf->data + msg->som) + buf->size;
7409 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7410 if (es->len > len1 && len1 < sizeof(es->buf))
7411 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7412 }
7413 else {
7414 es->len = buf->r - (buf->data + msg->som);
7415 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7416 }
7417
Willy Tarreau4076a152009-04-02 15:18:36 +02007418 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007419 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007420 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007421 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007422 else
7423 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7424
Willy Tarreau4076a152009-04-02 15:18:36 +02007425 es->when = date; // user-visible date
7426 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007427 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007428 es->oe = other_end;
Willy Tarreau957c0a52011-03-03 17:42:23 +01007429 es->src = s->req->prod->addr.c.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007430 es->state = state;
7431 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007432 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007433}
Willy Tarreaub2513902006-12-17 14:52:38 +01007434
Willy Tarreaubce70882009-09-07 11:51:47 +02007435/* return the IP address pointed to by occurrence <occ> of header <hname> in
7436 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
7437 * occurrence number corresponding to this value is returned. If <occ> is
7438 * strictly negative, the occurrence number before the end corresponding to
7439 * this value is returned. If <occ> is null, any value is returned, so it is
7440 * not recommended to use it that way. Negative occurrences are limited to
7441 * a small value because it is required to keep them in memory while scanning.
7442 * IP address 0.0.0.0 is returned if no match is found.
7443 */
7444unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
7445{
7446 struct hdr_ctx ctx;
7447 unsigned int hdr_hist[MAX_HDR_HISTORY];
7448 unsigned int hist_ptr;
7449 int found = 0;
7450
7451 ctx.idx = 0;
7452 if (occ >= 0) {
7453 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7454 occ--;
7455 if (occ <= 0) {
7456 found = 1;
7457 break;
7458 }
7459 }
7460 if (!found)
7461 return 0;
7462 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7463 }
7464
7465 /* negative occurrence, we scan all the list then walk back */
7466 if (-occ > MAX_HDR_HISTORY)
7467 return 0;
7468
7469 hist_ptr = 0;
7470 hdr_hist[hist_ptr] = 0;
7471 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7472 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7473 if (hist_ptr >= MAX_HDR_HISTORY)
7474 hist_ptr = 0;
7475 found++;
7476 }
7477 if (-occ > found)
7478 return 0;
7479 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7480 * find occurrence -occ, so we have to check [hist_ptr+occ].
7481 */
7482 hist_ptr += occ;
7483 if (hist_ptr >= MAX_HDR_HISTORY)
7484 hist_ptr -= MAX_HDR_HISTORY;
7485 return hdr_hist[hist_ptr];
7486}
7487
Willy Tarreaubaaee002006-06-26 02:48:02 +02007488/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007489 * Print a debug line with a header
7490 */
7491void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7492{
7493 int len, max;
7494 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007495 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007496 max = end - start;
7497 UBOUND(max, sizeof(trash) - len - 1);
7498 len += strlcpy2(trash + len, start, max + 1);
7499 trash[len++] = '\n';
7500 write(1, trash, len);
7501}
7502
Willy Tarreau0937bc42009-12-22 15:03:09 +01007503/*
7504 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7505 * the required fields are properly allocated and that we only need to (re)init
7506 * them. This should be used before processing any new request.
7507 */
7508void http_init_txn(struct session *s)
7509{
7510 struct http_txn *txn = &s->txn;
7511 struct proxy *fe = s->fe;
7512
7513 txn->flags = 0;
7514 txn->status = -1;
7515
Willy Tarreauf64d1412010-10-07 20:06:11 +02007516 txn->cookie_first_date = 0;
7517 txn->cookie_last_date = 0;
7518
Willy Tarreau0937bc42009-12-22 15:03:09 +01007519 txn->req.sol = txn->req.eol = NULL;
7520 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7521 txn->rsp.sol = txn->rsp.eol = NULL;
7522 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007523 txn->req.chunk_len = 0LL;
7524 txn->req.body_len = 0LL;
7525 txn->rsp.chunk_len = 0LL;
7526 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007527 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7528 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007529
7530 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007531
7532 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7533 if (fe->options2 & PR_O2_REQBUG_OK)
7534 txn->req.err_pos = -1; /* let buggy requests pass */
7535
Willy Tarreau46023632010-01-07 22:51:47 +01007536 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007537 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7538
Willy Tarreau46023632010-01-07 22:51:47 +01007539 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007540 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7541
7542 if (txn->hdr_idx.v)
7543 hdr_idx_init(&txn->hdr_idx);
7544}
7545
7546/* to be used at the end of a transaction */
7547void http_end_txn(struct session *s)
7548{
7549 struct http_txn *txn = &s->txn;
7550
7551 /* these ones will have been dynamically allocated */
7552 pool_free2(pool2_requri, txn->uri);
7553 pool_free2(pool2_capture, txn->cli_cookie);
7554 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007555 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007556
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007557 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007558 txn->uri = NULL;
7559 txn->srv_cookie = NULL;
7560 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007561
7562 if (txn->req.cap) {
7563 struct cap_hdr *h;
7564 for (h = s->fe->req_cap; h; h = h->next)
7565 pool_free2(h->pool, txn->req.cap[h->index]);
7566 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7567 }
7568
7569 if (txn->rsp.cap) {
7570 struct cap_hdr *h;
7571 for (h = s->fe->rsp_cap; h; h = h->next)
7572 pool_free2(h->pool, txn->rsp.cap[h->index]);
7573 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7574 }
7575
Willy Tarreau0937bc42009-12-22 15:03:09 +01007576}
7577
7578/* to be used at the end of a transaction to prepare a new one */
7579void http_reset_txn(struct session *s)
7580{
7581 http_end_txn(s);
7582 http_init_txn(s);
7583
7584 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007585 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007586 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007587 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007588 /* re-init store persistence */
7589 s->store_count = 0;
7590
Willy Tarreau0937bc42009-12-22 15:03:09 +01007591 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007592
7593 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7594
Willy Tarreau739cfba2010-01-25 23:11:14 +01007595 /* We must trim any excess data from the response buffer, because we
7596 * may have blocked an invalid response from a server that we don't
7597 * want to accidentely forward once we disable the analysers, nor do
7598 * we want those data to come along with next response. A typical
7599 * example of such data would be from a buggy server responding to
7600 * a HEAD with some data, or sending more than the advertised
7601 * content-length.
7602 */
7603 if (unlikely(s->rep->l > s->rep->send_max)) {
7604 s->rep->l = s->rep->send_max;
7605 s->rep->r = s->rep->w + s->rep->l;
7606 if (s->rep->r >= s->rep->data + s->rep->size)
7607 s->rep->r -= s->rep->size;
7608 }
7609
Willy Tarreau0937bc42009-12-22 15:03:09 +01007610 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007611 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007612
Willy Tarreaud04e8582010-05-31 12:31:35 +02007613 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007614 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007615
7616 s->req->rex = TICK_ETERNITY;
7617 s->req->wex = TICK_ETERNITY;
7618 s->req->analyse_exp = TICK_ETERNITY;
7619 s->rep->rex = TICK_ETERNITY;
7620 s->rep->wex = TICK_ETERNITY;
7621 s->rep->analyse_exp = TICK_ETERNITY;
7622}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007623
Willy Tarreauff011f22011-01-06 17:51:27 +01007624void free_http_req_rules(struct list *r) {
7625 struct http_req_rule *tr, *pr;
7626
7627 list_for_each_entry_safe(pr, tr, r, list) {
7628 LIST_DEL(&pr->list);
7629 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7630 free(pr->http_auth.realm);
7631
7632 free(pr);
7633 }
7634}
7635
7636struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7637{
7638 struct http_req_rule *rule;
7639 int cur_arg;
7640
7641 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7642 if (!rule) {
7643 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7644 return NULL;
7645 }
7646
7647 if (!*args[0]) {
7648 goto req_error_parsing;
7649 } else if (!strcmp(args[0], "allow")) {
7650 rule->action = HTTP_REQ_ACT_ALLOW;
7651 cur_arg = 1;
7652 } else if (!strcmp(args[0], "deny")) {
7653 rule->action = HTTP_REQ_ACT_DENY;
7654 cur_arg = 1;
7655 } else if (!strcmp(args[0], "auth")) {
7656 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7657 cur_arg = 1;
7658
7659 while(*args[cur_arg]) {
7660 if (!strcmp(args[cur_arg], "realm")) {
7661 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7662 cur_arg+=2;
7663 continue;
7664 } else
7665 break;
7666 }
7667 } else {
7668req_error_parsing:
7669 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7670 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7671 return NULL;
7672 }
7673
7674 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7675 struct acl_cond *cond;
7676
7677 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7678 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7679 file, linenum, args[0]);
7680 return NULL;
7681 }
7682 rule->cond = cond;
7683 }
7684 else if (*args[cur_arg]) {
7685 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7686 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7687 file, linenum, args[0], args[cur_arg]);
7688 return NULL;
7689 }
7690
7691 return rule;
7692}
7693
Willy Tarreau8797c062007-05-07 00:55:35 +02007694/************************************************************************/
7695/* The code below is dedicated to ACL parsing and matching */
7696/************************************************************************/
7697
7698
7699
7700
7701/* 1. Check on METHOD
7702 * We use the pre-parsed method if it is known, and store its number as an
7703 * integer. If it is unknown, we use the pointer and the length.
7704 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007705static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007706{
7707 int len, meth;
7708
Willy Tarreauae8b7962007-06-09 23:10:04 +02007709 len = strlen(*text);
7710 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007711
7712 pattern->val.i = meth;
7713 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007714 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007715 if (!pattern->ptr.str)
7716 return 0;
7717 pattern->len = len;
7718 }
7719 return 1;
7720}
7721
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007722static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007723acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7724 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007725{
7726 int meth;
7727 struct http_txn *txn = l7;
7728
Willy Tarreaub6866442008-07-14 23:54:42 +02007729 if (!txn)
7730 return 0;
7731
Willy Tarreau655dce92009-11-08 13:10:58 +01007732 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007733 return 0;
7734
Willy Tarreau8797c062007-05-07 00:55:35 +02007735 meth = txn->meth;
7736 test->i = meth;
7737 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007738 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7739 /* ensure the indexes are not affected */
7740 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007741 test->len = txn->req.sl.rq.m_l;
7742 test->ptr = txn->req.sol;
7743 }
7744 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7745 return 1;
7746}
7747
7748static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7749{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007750 int icase;
7751
Willy Tarreau8797c062007-05-07 00:55:35 +02007752 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02007753 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007754
7755 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02007756 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007757
7758 /* Other method, we must compare the strings */
7759 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02007760 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007761
7762 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
7763 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
7764 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007765 return ACL_PAT_FAIL;
7766 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007767}
7768
7769/* 2. Check on Request/Status Version
7770 * We simply compare strings here.
7771 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007772static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007773{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007774 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007775 if (!pattern->ptr.str)
7776 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007777 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007778 return 1;
7779}
7780
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007781static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007782acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7783 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007784{
7785 struct http_txn *txn = l7;
7786 char *ptr;
7787 int len;
7788
Willy Tarreaub6866442008-07-14 23:54:42 +02007789 if (!txn)
7790 return 0;
7791
Willy Tarreau655dce92009-11-08 13:10:58 +01007792 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007793 return 0;
7794
Willy Tarreau8797c062007-05-07 00:55:35 +02007795 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007796 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007797
7798 while ((len-- > 0) && (*ptr++ != '/'));
7799 if (len <= 0)
7800 return 0;
7801
7802 test->ptr = ptr;
7803 test->len = len;
7804
7805 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7806 return 1;
7807}
7808
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007809static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007810acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7811 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007812{
7813 struct http_txn *txn = l7;
7814 char *ptr;
7815 int len;
7816
Willy Tarreaub6866442008-07-14 23:54:42 +02007817 if (!txn)
7818 return 0;
7819
Willy Tarreau655dce92009-11-08 13:10:58 +01007820 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007821 return 0;
7822
Willy Tarreau8797c062007-05-07 00:55:35 +02007823 len = txn->rsp.sl.st.v_l;
7824 ptr = txn->rsp.sol;
7825
7826 while ((len-- > 0) && (*ptr++ != '/'));
7827 if (len <= 0)
7828 return 0;
7829
7830 test->ptr = ptr;
7831 test->len = len;
7832
7833 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7834 return 1;
7835}
7836
7837/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007838static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007839acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7840 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007841{
7842 struct http_txn *txn = l7;
7843 char *ptr;
7844 int len;
7845
Willy Tarreaub6866442008-07-14 23:54:42 +02007846 if (!txn)
7847 return 0;
7848
Willy Tarreau655dce92009-11-08 13:10:58 +01007849 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007850 return 0;
7851
Willy Tarreau8797c062007-05-07 00:55:35 +02007852 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007853 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007854
7855 test->i = __strl2ui(ptr, len);
7856 test->flags = ACL_TEST_F_VOL_1ST;
7857 return 1;
7858}
7859
7860/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007861static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007862acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7863 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007864{
7865 struct http_txn *txn = l7;
7866
Willy Tarreaub6866442008-07-14 23:54:42 +02007867 if (!txn)
7868 return 0;
7869
Willy Tarreau655dce92009-11-08 13:10:58 +01007870 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007871 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007872
Willy Tarreauc11416f2007-06-17 16:58:38 +02007873 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7874 /* ensure the indexes are not affected */
7875 return 0;
7876
Willy Tarreau8797c062007-05-07 00:55:35 +02007877 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007878 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007879
Willy Tarreauf3d25982007-05-08 22:45:09 +02007880 /* we do not need to set READ_ONLY because the data is in a buffer */
7881 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007882 return 1;
7883}
7884
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007885static int
7886acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7887 struct acl_expr *expr, struct acl_test *test)
7888{
7889 struct http_txn *txn = l7;
7890
Willy Tarreaub6866442008-07-14 23:54:42 +02007891 if (!txn)
7892 return 0;
7893
Willy Tarreau655dce92009-11-08 13:10:58 +01007894 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007895 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007896
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007897 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7898 /* ensure the indexes are not affected */
7899 return 0;
7900
7901 /* Parse HTTP request */
Willy Tarreau957c0a52011-03-03 17:42:23 +01007902 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to);
7903 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007904 test->i = AF_INET;
7905
7906 /*
7907 * If we are parsing url in frontend space, we prepare backend stage
7908 * to not parse again the same url ! optimization lazyness...
7909 */
7910 if (px->options & PR_O_HTTP_PROXY)
7911 l4->flags |= SN_ADDR_SET;
7912
7913 test->flags = ACL_TEST_F_READ_ONLY;
7914 return 1;
7915}
7916
7917static int
7918acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7919 struct acl_expr *expr, struct acl_test *test)
7920{
7921 struct http_txn *txn = l7;
7922
Willy Tarreaub6866442008-07-14 23:54:42 +02007923 if (!txn)
7924 return 0;
7925
Willy Tarreau655dce92009-11-08 13:10:58 +01007926 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007927 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007928
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007929 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7930 /* ensure the indexes are not affected */
7931 return 0;
7932
7933 /* Same optimization as url_ip */
Willy Tarreau957c0a52011-03-03 17:42:23 +01007934 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to);
7935 test->i = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007936
7937 if (px->options & PR_O_HTTP_PROXY)
7938 l4->flags |= SN_ADDR_SET;
7939
7940 test->flags = ACL_TEST_F_READ_ONLY;
7941 return 1;
7942}
7943
Willy Tarreauc11416f2007-06-17 16:58:38 +02007944/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7945 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7946 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007947static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007948acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007949 struct acl_expr *expr, struct acl_test *test)
7950{
7951 struct http_txn *txn = l7;
7952 struct hdr_idx *idx = &txn->hdr_idx;
7953 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007954
Willy Tarreaub6866442008-07-14 23:54:42 +02007955 if (!txn)
7956 return 0;
7957
Willy Tarreau33a7e692007-06-10 19:45:56 +02007958 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7959 /* search for header from the beginning */
7960 ctx->idx = 0;
7961
Willy Tarreau33a7e692007-06-10 19:45:56 +02007962 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7963 test->flags |= ACL_TEST_F_FETCH_MORE;
7964 test->flags |= ACL_TEST_F_VOL_HDR;
7965 test->len = ctx->vlen;
7966 test->ptr = (char *)ctx->line + ctx->val;
7967 return 1;
7968 }
7969
7970 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7971 test->flags |= ACL_TEST_F_VOL_HDR;
7972 return 0;
7973}
7974
Willy Tarreau33a7e692007-06-10 19:45:56 +02007975static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007976acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7977 struct acl_expr *expr, struct acl_test *test)
7978{
7979 struct http_txn *txn = l7;
7980
Willy Tarreaub6866442008-07-14 23:54:42 +02007981 if (!txn)
7982 return 0;
7983
Willy Tarreau655dce92009-11-08 13:10:58 +01007984 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007985 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007986
Willy Tarreauc11416f2007-06-17 16:58:38 +02007987 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7988 /* ensure the indexes are not affected */
7989 return 0;
7990
7991 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7992}
7993
7994static int
7995acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7996 struct acl_expr *expr, struct acl_test *test)
7997{
7998 struct http_txn *txn = l7;
7999
Willy Tarreaub6866442008-07-14 23:54:42 +02008000 if (!txn)
8001 return 0;
8002
Willy Tarreau655dce92009-11-08 13:10:58 +01008003 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008004 return 0;
8005
8006 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
8007}
8008
8009/* 6. Check on HTTP header count. The number of occurrences is returned.
8010 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8011 */
8012static int
8013acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008014 struct acl_expr *expr, struct acl_test *test)
8015{
8016 struct http_txn *txn = l7;
8017 struct hdr_idx *idx = &txn->hdr_idx;
8018 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008019 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008020
Willy Tarreaub6866442008-07-14 23:54:42 +02008021 if (!txn)
8022 return 0;
8023
Willy Tarreau33a7e692007-06-10 19:45:56 +02008024 ctx.idx = 0;
8025 cnt = 0;
8026 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
8027 cnt++;
8028
8029 test->i = cnt;
8030 test->flags = ACL_TEST_F_VOL_HDR;
8031 return 1;
8032}
8033
Willy Tarreauc11416f2007-06-17 16:58:38 +02008034static int
8035acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8036 struct acl_expr *expr, struct acl_test *test)
8037{
8038 struct http_txn *txn = l7;
8039
Willy Tarreaub6866442008-07-14 23:54:42 +02008040 if (!txn)
8041 return 0;
8042
Willy Tarreau655dce92009-11-08 13:10:58 +01008043 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008044 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008045
Willy Tarreauc11416f2007-06-17 16:58:38 +02008046 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8047 /* ensure the indexes are not affected */
8048 return 0;
8049
8050 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
8051}
8052
8053static int
8054acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8055 struct acl_expr *expr, struct acl_test *test)
8056{
8057 struct http_txn *txn = l7;
8058
Willy Tarreaub6866442008-07-14 23:54:42 +02008059 if (!txn)
8060 return 0;
8061
Willy Tarreau655dce92009-11-08 13:10:58 +01008062 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008063 return 0;
8064
8065 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
8066}
8067
Willy Tarreau33a7e692007-06-10 19:45:56 +02008068/* 7. Check on HTTP header's integer value. The integer value is returned.
8069 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008070 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008071 */
8072static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02008073acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008074 struct acl_expr *expr, struct acl_test *test)
8075{
8076 struct http_txn *txn = l7;
8077 struct hdr_idx *idx = &txn->hdr_idx;
8078 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008079
Willy Tarreaub6866442008-07-14 23:54:42 +02008080 if (!txn)
8081 return 0;
8082
Willy Tarreau33a7e692007-06-10 19:45:56 +02008083 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8084 /* search for header from the beginning */
8085 ctx->idx = 0;
8086
Willy Tarreau33a7e692007-06-10 19:45:56 +02008087 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8088 test->flags |= ACL_TEST_F_FETCH_MORE;
8089 test->flags |= ACL_TEST_F_VOL_HDR;
8090 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
8091 return 1;
8092 }
8093
8094 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8095 test->flags |= ACL_TEST_F_VOL_HDR;
8096 return 0;
8097}
8098
Willy Tarreauc11416f2007-06-17 16:58:38 +02008099static int
8100acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8101 struct acl_expr *expr, struct acl_test *test)
8102{
8103 struct http_txn *txn = l7;
8104
Willy Tarreaub6866442008-07-14 23:54:42 +02008105 if (!txn)
8106 return 0;
8107
Willy Tarreau655dce92009-11-08 13:10:58 +01008108 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008109 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008110
Willy Tarreauc11416f2007-06-17 16:58:38 +02008111 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8112 /* ensure the indexes are not affected */
8113 return 0;
8114
8115 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
8116}
8117
8118static int
8119acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8120 struct acl_expr *expr, struct acl_test *test)
8121{
8122 struct http_txn *txn = l7;
8123
Willy Tarreaub6866442008-07-14 23:54:42 +02008124 if (!txn)
8125 return 0;
8126
Willy Tarreau655dce92009-11-08 13:10:58 +01008127 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008128 return 0;
8129
8130 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
8131}
8132
Willy Tarreau106f9792009-09-19 07:54:16 +02008133/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
8134 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8135 */
8136static int
8137acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
8138 struct acl_expr *expr, struct acl_test *test)
8139{
8140 struct http_txn *txn = l7;
8141 struct hdr_idx *idx = &txn->hdr_idx;
8142 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
8143
8144 if (!txn)
8145 return 0;
8146
8147 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8148 /* search for header from the beginning */
8149 ctx->idx = 0;
8150
8151 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8152 test->flags |= ACL_TEST_F_FETCH_MORE;
8153 test->flags |= ACL_TEST_F_VOL_HDR;
8154 /* Same optimization as url_ip */
David du Colombier6f5ccb12011-03-10 22:26:24 +01008155 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));
8156 url2ipv4((char *)ctx->line + ctx->val, &((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr);
8157 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr;
Willy Tarreau106f9792009-09-19 07:54:16 +02008158 test->i = AF_INET;
8159 return 1;
8160 }
8161
8162 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8163 test->flags |= ACL_TEST_F_VOL_HDR;
8164 return 0;
8165}
8166
8167static int
8168acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8169 struct acl_expr *expr, struct acl_test *test)
8170{
8171 struct http_txn *txn = l7;
8172
8173 if (!txn)
8174 return 0;
8175
Willy Tarreau655dce92009-11-08 13:10:58 +01008176 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008177 return 0;
8178
8179 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8180 /* ensure the indexes are not affected */
8181 return 0;
8182
8183 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8184}
8185
8186static int
8187acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8188 struct acl_expr *expr, struct acl_test *test)
8189{
8190 struct http_txn *txn = l7;
8191
8192 if (!txn)
8193 return 0;
8194
Willy Tarreau655dce92009-11-08 13:10:58 +01008195 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008196 return 0;
8197
8198 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8199}
8200
Willy Tarreau737b0c12007-06-10 21:28:46 +02008201/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8202 * the first '/' after the possible hostname, and ends before the possible '?'.
8203 */
8204static int
8205acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8206 struct acl_expr *expr, struct acl_test *test)
8207{
8208 struct http_txn *txn = l7;
8209 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008210
Willy Tarreaub6866442008-07-14 23:54:42 +02008211 if (!txn)
8212 return 0;
8213
Willy Tarreau655dce92009-11-08 13:10:58 +01008214 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008215 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008216
Willy Tarreauc11416f2007-06-17 16:58:38 +02008217 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8218 /* ensure the indexes are not affected */
8219 return 0;
8220
Willy Tarreau962c3f42010-01-10 00:15:35 +01008221 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008222 ptr = http_get_path(txn);
8223 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008224 return 0;
8225
8226 /* OK, we got the '/' ! */
8227 test->ptr = ptr;
8228
8229 while (ptr < end && *ptr != '?')
8230 ptr++;
8231
8232 test->len = ptr - test->ptr;
8233
8234 /* we do not need to set READ_ONLY because the data is in a buffer */
8235 test->flags = ACL_TEST_F_VOL_1ST;
8236 return 1;
8237}
8238
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008239static int
8240acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8241 struct acl_expr *expr, struct acl_test *test)
8242{
8243 struct buffer *req = s->req;
8244 struct http_txn *txn = &s->txn;
8245 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008246
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008247 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8248 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8249 */
8250
8251 if (!s || !req)
8252 return 0;
8253
Willy Tarreau655dce92009-11-08 13:10:58 +01008254 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008255 /* Already decoded as OK */
8256 test->flags |= ACL_TEST_F_SET_RES_PASS;
8257 return 1;
8258 }
8259
8260 /* Try to decode HTTP request */
8261 if (likely(req->lr < req->r))
8262 http_msg_analyzer(req, msg, &txn->hdr_idx);
8263
Willy Tarreau655dce92009-11-08 13:10:58 +01008264 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008265 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8266 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8267 return 1;
8268 }
8269 /* wait for final state */
8270 test->flags |= ACL_TEST_F_MAY_CHANGE;
8271 return 0;
8272 }
8273
8274 /* OK we got a valid HTTP request. We have some minor preparation to
8275 * perform so that further checks can rely on HTTP tests.
8276 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008277 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008278 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8279 s->flags |= SN_REDIRECTABLE;
8280
8281 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8282 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8283 return 1;
8284 }
8285
8286 test->flags |= ACL_TEST_F_SET_RES_PASS;
8287 return 1;
8288}
8289
Willy Tarreau7f18e522010-10-22 20:04:13 +02008290/* return a valid test if the current request is the first one on the connection */
8291static int
8292acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8293 struct acl_expr *expr, struct acl_test *test)
8294{
8295 if (!s)
8296 return 0;
8297
8298 if (s->txn.flags & TX_NOT_FIRST)
8299 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8300 else
8301 test->flags |= ACL_TEST_F_SET_RES_PASS;
8302
8303 return 1;
8304}
8305
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008306static int
8307acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8308 struct acl_expr *expr, struct acl_test *test)
8309{
8310
8311 if (!s)
8312 return 0;
8313
8314 if (!get_http_auth(s))
8315 return 0;
8316
8317 test->ctx.a[0] = expr->arg.ul;
8318 test->ctx.a[1] = s->txn.auth.user;
8319 test->ctx.a[2] = s->txn.auth.pass;
8320
8321 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8322
8323 return 1;
8324}
Willy Tarreau8797c062007-05-07 00:55:35 +02008325
8326/************************************************************************/
8327/* All supported keywords must be declared here. */
8328/************************************************************************/
8329
8330/* Note: must not be declared <const> as its list will be overwritten */
8331static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008332 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8333
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008334 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008335 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8336 { "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 +02008337 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008338
Willy Tarreauc4262962010-05-10 23:42:40 +02008339 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008340 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8341 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8342 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8343 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8344 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8345 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008346 { "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 +02008347 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008348
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008349 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008350 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008351 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8352 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8353 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8354 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8355 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8356 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8357 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
8358 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008359 { "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 +02008360
Willy Tarreauc4262962010-05-10 23:42:40 +02008361 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008362 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8363 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8364 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8365 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8366 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8367 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8368 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
8369 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008370 { "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 +02008371
Willy Tarreauc4262962010-05-10 23:42:40 +02008372 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008373 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8374 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8375 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8376 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8377 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8378 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008379
Willy Tarreauf3d25982007-05-08 22:45:09 +02008380#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008381 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8382 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8383 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8384 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8385 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8386 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8387 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8388
Willy Tarreau8797c062007-05-07 00:55:35 +02008389 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8390 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8391 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8392 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8393 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8394 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8395 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8396 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008397#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008398
Willy Tarreau7f18e522010-10-22 20:04:13 +02008399 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8400 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8401 { "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 +02008402 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008403}};
8404
Willy Tarreau4a568972010-05-12 08:08:50 +02008405/************************************************************************/
8406/* The code below is dedicated to pattern fetching and matching */
8407/************************************************************************/
8408
8409/* extract the IP address from the last occurrence of specified header. Note
8410 * that we should normally first extract the string then convert it to IP,
8411 * but right now we have all the functions to do this seemlessly, and we will
8412 * be able to change that later without touching the configuration.
8413 */
8414static int
8415pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02008416 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008417{
8418 struct http_txn *txn = l7;
8419
Emeric Brun485479d2010-09-23 18:02:19 +02008420 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 +02008421 return data->ip.s_addr != 0;
8422}
8423
David Cournapeau16023ee2010-12-23 20:55:41 +09008424/*
8425 * Given a path string and its length, find the position of beginning of the
8426 * query string. Returns NULL if no query string is found in the path.
8427 *
8428 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8429 *
8430 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8431 */
8432static inline char *find_query_string(char *path, size_t path_l)
8433{
8434 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008435
David Cournapeau16023ee2010-12-23 20:55:41 +09008436 p = memchr(path, '?', path_l);
8437 return p ? p + 1 : NULL;
8438}
8439
8440static inline int is_param_delimiter(char c)
8441{
8442 return c == '&' || c == ';';
8443}
8444
8445/*
8446 * Given a url parameter, find the starting position of the first occurence,
8447 * or NULL if the parameter is not found.
8448 *
8449 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8450 * the function will return query_string+8.
8451 */
8452static char*
8453find_url_param_pos(char* query_string, size_t query_string_l,
8454 char* url_param_name, size_t url_param_name_l)
8455{
8456 char *pos, *last;
8457
8458 pos = query_string;
8459 last = query_string + query_string_l - url_param_name_l - 1;
8460
8461 while (pos <= last) {
8462 if (pos[url_param_name_l] == '=') {
8463 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8464 return pos;
8465 pos += url_param_name_l + 1;
8466 }
8467 while (pos <= last && !is_param_delimiter(*pos))
8468 pos++;
8469 pos++;
8470 }
8471 return NULL;
8472}
8473
8474/*
8475 * Given a url parameter name, returns its value and size into *value and
8476 * *value_l respectively, and returns non-zero. If the parameter is not found,
8477 * zero is returned and value/value_l are not touched.
8478 */
8479static int
8480find_url_param_value(char* path, size_t path_l,
8481 char* url_param_name, size_t url_param_name_l,
8482 char** value, size_t* value_l)
8483{
8484 char *query_string, *qs_end;
8485 char *arg_start;
8486 char *value_start, *value_end;
8487
8488 query_string = find_query_string(path, path_l);
8489 if (!query_string)
8490 return 0;
8491
8492 qs_end = path + path_l;
8493 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8494 url_param_name, url_param_name_l);
8495 if (!arg_start)
8496 return 0;
8497
8498 value_start = arg_start + url_param_name_l + 1;
8499 value_end = value_start;
8500
8501 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8502 value_end++;
8503
8504 *value = value_start;
8505 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008506 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008507}
8508
8509static int
8510pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8511 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8512{
8513 struct http_txn *txn = l7;
8514 struct http_msg *msg = &txn->req;
8515 char *url_param_value;
8516 size_t url_param_value_l;
8517
8518 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8519 arg_p->data.str.str, arg_p->data.str.len,
8520 &url_param_value, &url_param_value_l))
8521 return 0;
8522
8523 data->str.str = url_param_value;
8524 data->str.len = url_param_value_l;
8525 return 1;
8526}
8527
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008528/* Try to find the last occurrence of a cookie name in a cookie header value.
8529 * The pointer and size of the last occurrence of the cookie value is returned
8530 * into *value and *value_l, and the function returns non-zero if the value was
8531 * found. Otherwise if the cookie was not found, zero is returned and neither
8532 * value nor value_l are touched. The input hdr string should begin at the
8533 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8534 * value may represent a list of values (cookie headers).
8535 */
8536static int
8537extract_cookie_value(char *hdr, size_t hdr_l,
8538 char *cookie_name, size_t cookie_name_l, int list,
8539 char **value, size_t *value_l)
8540{
8541 int found = 0;
8542 char *equal, *att_end, *att_beg, *hdr_end, *val_beg, *val_end;
8543 char *next;
8544
8545 /* Note that multiple cookies may be delimited with semi-colons, so we
8546 * also have to loop on this.
8547 */
8548
8549 /* we search at least a cookie name followed by an equal, and more
8550 * generally something like this :
8551 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8552 */
8553 if (hdr_l < cookie_name_l + 1)
8554 return 0;
8555
8556 hdr_end = hdr + hdr_l;
8557
8558 for (att_beg = hdr; att_beg < hdr_end; att_beg = next + 1) {
8559 /* Iterate through all cookies on this line */
8560
8561 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8562 att_beg++;
8563
8564 /* find att_end : this is the first character after the last non
8565 * space before the equal. It may be equal to hdr_end.
8566 */
8567 equal = att_end = att_beg;
8568
8569 while (equal < hdr_end) {
8570 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8571 break;
8572 if (http_is_spht[(unsigned char)*equal++])
8573 continue;
8574 att_end = equal;
8575 }
8576
8577 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8578 * is between <att_beg> and <equal>, both may be identical.
8579 */
8580
8581 /* look for end of cookie if there is an equal sign */
8582 if (equal < hdr_end && *equal == '=') {
8583 /* look for the beginning of the value */
8584 val_beg = equal + 1;
8585 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8586 val_beg++;
8587
8588 /* find the end of the value, respecting quotes */
8589 next = find_cookie_value_end(val_beg, hdr_end);
8590
8591 /* make val_end point to the first white space or delimitor after the value */
8592 val_end = next;
8593 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8594 val_end--;
8595 } else {
8596 val_beg = val_end = next = equal;
8597 }
8598
8599 /* We have nothing to do with attributes beginning with '$'. However,
8600 * they will automatically be removed if a header before them is removed,
8601 * since they're supposed to be linked together.
8602 */
8603 if (*att_beg == '$')
8604 continue;
8605
8606 /* Ignore cookies with no equal sign */
8607 if (equal == next)
8608 continue;
8609
8610 /* Now we have the cookie name between att_beg and att_end, and
8611 * its value between val_beg and val_end.
8612 */
8613
8614 if (att_end - att_beg == cookie_name_l &&
8615 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8616 found = 1;
8617 *value = val_beg;
8618 *value_l = val_end - val_beg;
8619 /* right now we want to catch the last occurrence
8620 * of the cookie, so let's go on searching.
8621 */
8622 }
8623
8624 /* Set-Cookie headers only have the name in the first attr=value part */
8625 if (!list)
8626 break;
8627 }
8628
8629 return found;
8630}
8631
8632/* Try to find in request or response message is in <msg> and whose transaction
8633 * is in <txn> the last occurrence of a cookie name in all cookie header values
8634 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8635 * pointer and size of the last occurrence of the cookie value is returned into
8636 * <value> and <value_l>, and the function returns non-zero if the value was
8637 * found. Otherwise if the cookie was not found, zero is returned and neither
8638 * value nor value_l are touched. The input hdr string should begin at the
8639 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8640 * value may represent a list of values (cookie headers).
8641 */
8642
8643static int
8644find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8645 const char *hdr_name, int hdr_name_len,
8646 char *cookie_name, size_t cookie_name_l, int list,
8647 char **value, size_t *value_l)
8648{
8649 struct hdr_ctx ctx;
8650 int found = 0;
8651
8652 ctx.idx = 0;
8653 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
8654 if (ctx.vlen < cookie_name_l + 1)
8655 continue;
8656
8657 found |= extract_cookie_value(ctx.line + ctx.val, ctx.vlen,
8658 cookie_name, cookie_name_l, 1,
8659 value, value_l);
8660 }
8661 return found;
8662}
8663
8664static int
8665pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8666 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8667{
8668 struct http_txn *txn = l7;
8669 struct http_msg *msg = &txn->req;
8670 char *cookie_value;
8671 size_t cookie_value_l;
8672 int found = 0;
8673
8674 found = find_cookie_value(msg, txn, "Cookie", 6,
8675 arg_p->data.str.str, arg_p->data.str.len, 1,
8676 &cookie_value, &cookie_value_l);
8677 if (found) {
8678 data->str.str = cookie_value;
8679 data->str.len = cookie_value_l;
8680 }
8681
8682 return found;
8683}
8684
8685
8686static int
8687pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8688 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8689{
8690 struct http_txn *txn = l7;
8691 struct http_msg *msg = &txn->rsp;
8692 char *cookie_value;
8693 size_t cookie_value_l;
8694 int found = 0;
8695
8696 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8697 arg_p->data.str.str, arg_p->data.str.len, 1,
8698 &cookie_value, &cookie_value_l);
8699 if (found) {
8700 data->str.str = cookie_value;
8701 data->str.len = cookie_value_l;
8702 }
8703
8704 return found;
8705}
8706
Emeric Brun485479d2010-09-23 18:02:19 +02008707
Willy Tarreau4a568972010-05-12 08:08:50 +02008708/************************************************************************/
8709/* All supported keywords must be declared here. */
8710/************************************************************************/
8711/* Note: must not be declared <const> as its list will be overwritten */
8712static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Emeric Brun485479d2010-09-23 18:02:19 +02008713 { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008714 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008715 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8716 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008717 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008718}};
8719
Willy Tarreau8797c062007-05-07 00:55:35 +02008720
8721__attribute__((constructor))
8722static void __http_protocol_init(void)
8723{
8724 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008725 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008726}
8727
8728
Willy Tarreau58f10d72006-12-04 02:26:12 +01008729/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008730 * Local variables:
8731 * c-indent-level: 8
8732 * c-basic-offset: 8
8733 * End:
8734 */