blob: 32f9f263581cf3cbed9d2976a9ba2c526c10caa8 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010027#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/compat.h>
29#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010030#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreau8797c062007-05-07 00:55:35 +020042#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010043#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/backend.h>
45#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010046#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020049#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010051#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020052#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020053#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010055#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010059#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020060#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/task.h>
62
Willy Tarreau522d6c02009-12-06 18:49:18 +010063const char HTTP_100[] =
64 "HTTP/1.1 100 Continue\r\n\r\n";
65
66const struct chunk http_100_chunk = {
67 .str = (char *)&HTTP_100,
68 .len = sizeof(HTTP_100)-1
69};
70
Willy Tarreaua9679ac2010-01-03 17:32:57 +010071/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020072const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010073 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020074 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010075 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020076 "Location: "; /* not terminated since it will be concatenated with the URL */
77
Willy Tarreau0f772532006-12-23 20:51:41 +010078const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010080 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010082 "Location: "; /* not terminated since it will be concatenated with the URL */
83
84/* same as 302 except that the browser MUST retry with the GET method */
85const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010089 "Location: "; /* not terminated since it will be concatenated with the URL */
90
Willy Tarreaubaaee002006-06-26 02:48:02 +020091/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
92const char *HTTP_401_fmt =
93 "HTTP/1.0 401 Unauthorized\r\n"
94 "Cache-Control: no-cache\r\n"
95 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020096 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020097 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
98 "\r\n"
99 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
100
Willy Tarreau844a7e72010-01-31 21:46:18 +0100101const char *HTTP_407_fmt =
102 "HTTP/1.0 407 Unauthorized\r\n"
103 "Cache-Control: no-cache\r\n"
104 "Connection: close\r\n"
105 "Content-Type: text/html\r\n"
106 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
107 "\r\n"
108 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
109
Willy Tarreau0f772532006-12-23 20:51:41 +0100110
111const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200112 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100113 [HTTP_ERR_400] = 400,
114 [HTTP_ERR_403] = 403,
115 [HTTP_ERR_408] = 408,
116 [HTTP_ERR_500] = 500,
117 [HTTP_ERR_502] = 502,
118 [HTTP_ERR_503] = 503,
119 [HTTP_ERR_504] = 504,
120};
121
Willy Tarreau80587432006-12-24 17:47:20 +0100122static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200123 [HTTP_ERR_200] =
124 "HTTP/1.0 200 OK\r\n"
125 "Cache-Control: no-cache\r\n"
126 "Connection: close\r\n"
127 "Content-Type: text/html\r\n"
128 "\r\n"
129 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
130
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100132 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 "Cache-Control: no-cache\r\n"
134 "Connection: close\r\n"
135 "Content-Type: text/html\r\n"
136 "\r\n"
137 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
138
139 [HTTP_ERR_403] =
140 "HTTP/1.0 403 Forbidden\r\n"
141 "Cache-Control: no-cache\r\n"
142 "Connection: close\r\n"
143 "Content-Type: text/html\r\n"
144 "\r\n"
145 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
146
147 [HTTP_ERR_408] =
148 "HTTP/1.0 408 Request Time-out\r\n"
149 "Cache-Control: no-cache\r\n"
150 "Connection: close\r\n"
151 "Content-Type: text/html\r\n"
152 "\r\n"
153 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
154
155 [HTTP_ERR_500] =
156 "HTTP/1.0 500 Server Error\r\n"
157 "Cache-Control: no-cache\r\n"
158 "Connection: close\r\n"
159 "Content-Type: text/html\r\n"
160 "\r\n"
161 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
162
163 [HTTP_ERR_502] =
164 "HTTP/1.0 502 Bad Gateway\r\n"
165 "Cache-Control: no-cache\r\n"
166 "Connection: close\r\n"
167 "Content-Type: text/html\r\n"
168 "\r\n"
169 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
170
171 [HTTP_ERR_503] =
172 "HTTP/1.0 503 Service Unavailable\r\n"
173 "Cache-Control: no-cache\r\n"
174 "Connection: close\r\n"
175 "Content-Type: text/html\r\n"
176 "\r\n"
177 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
178
179 [HTTP_ERR_504] =
180 "HTTP/1.0 504 Gateway Time-out\r\n"
181 "Cache-Control: no-cache\r\n"
182 "Connection: close\r\n"
183 "Content-Type: text/html\r\n"
184 "\r\n"
185 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
186
187};
188
Willy Tarreau80587432006-12-24 17:47:20 +0100189/* We must put the messages here since GCC cannot initialize consts depending
190 * on strlen().
191 */
192struct chunk http_err_chunks[HTTP_ERR_SIZE];
193
Willy Tarreau42250582007-04-01 01:30:43 +0200194#define FD_SETS_ARE_BITFIELDS
195#ifdef FD_SETS_ARE_BITFIELDS
196/*
197 * This map is used with all the FD_* macros to check whether a particular bit
198 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
199 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
200 * byte should be encoded. Be careful to always pass bytes from 0 to 255
201 * exclusively to the macros.
202 */
203fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
204fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
205
206#else
207#error "Check if your OS uses bitfields for fd_sets"
208#endif
209
Willy Tarreau80587432006-12-24 17:47:20 +0100210void init_proto_http()
211{
Willy Tarreau42250582007-04-01 01:30:43 +0200212 int i;
213 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100214 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200215
Willy Tarreau80587432006-12-24 17:47:20 +0100216 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
217 if (!http_err_msgs[msg]) {
218 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
219 abort();
220 }
221
222 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
223 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
224 }
Willy Tarreau42250582007-04-01 01:30:43 +0200225
226 /* initialize the log header encoding map : '{|}"#' should be encoded with
227 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
228 * URL encoding only requires '"', '#' to be encoded as well as non-
229 * printable characters above.
230 */
231 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
232 memset(url_encode_map, 0, sizeof(url_encode_map));
233 for (i = 0; i < 32; i++) {
234 FD_SET(i, hdr_encode_map);
235 FD_SET(i, url_encode_map);
236 }
237 for (i = 127; i < 256; i++) {
238 FD_SET(i, hdr_encode_map);
239 FD_SET(i, url_encode_map);
240 }
241
242 tmp = "\"#{|}";
243 while (*tmp) {
244 FD_SET(*tmp, hdr_encode_map);
245 tmp++;
246 }
247
248 tmp = "\"#";
249 while (*tmp) {
250 FD_SET(*tmp, url_encode_map);
251 tmp++;
252 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200253
254 /* memory allocations */
255 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200256 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100257}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200258
Willy Tarreau53b6c742006-12-17 13:37:46 +0100259/*
260 * We have 26 list of methods (1 per first letter), each of which can have
261 * up to 3 entries (2 valid, 1 null).
262 */
263struct http_method_desc {
264 http_meth_t meth;
265 int len;
266 const char text[8];
267};
268
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100269const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100270 ['C' - 'A'] = {
271 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
272 },
273 ['D' - 'A'] = {
274 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
275 },
276 ['G' - 'A'] = {
277 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
278 },
279 ['H' - 'A'] = {
280 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
281 },
282 ['P' - 'A'] = {
283 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
284 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
285 },
286 ['T' - 'A'] = {
287 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
288 },
289 /* rest is empty like this :
290 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
291 */
292};
293
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100294/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200295 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100296 * RFC2616 for those chars.
297 */
298
299const char http_is_spht[256] = {
300 [' '] = 1, ['\t'] = 1,
301};
302
303const char http_is_crlf[256] = {
304 ['\r'] = 1, ['\n'] = 1,
305};
306
307const char http_is_lws[256] = {
308 [' '] = 1, ['\t'] = 1,
309 ['\r'] = 1, ['\n'] = 1,
310};
311
312const char http_is_sep[256] = {
313 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
314 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
315 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
316 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
317 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
318};
319
320const char http_is_ctl[256] = {
321 [0 ... 31] = 1,
322 [127] = 1,
323};
324
325/*
326 * A token is any ASCII char that is neither a separator nor a CTL char.
327 * Do not overwrite values in assignment since gcc-2.95 will not handle
328 * them correctly. Instead, define every non-CTL char's status.
329 */
330const char http_is_token[256] = {
331 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
332 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
333 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
334 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
335 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
336 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
337 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
338 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
339 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
340 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
341 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
342 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
343 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
344 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
345 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
346 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
347 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
348 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
349 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
350 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
351 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
352 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
353 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
354 ['|'] = 1, ['}'] = 0, ['~'] = 1,
355};
356
357
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100358/*
359 * An http ver_token is any ASCII which can be found in an HTTP version,
360 * which includes 'H', 'T', 'P', '/', '.' and any digit.
361 */
362const char http_is_ver_token[256] = {
363 ['.'] = 1, ['/'] = 1,
364 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
365 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
366 ['H'] = 1, ['P'] = 1, ['T'] = 1,
367};
368
369
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100370/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100371 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
372 */
373#if defined(DEBUG_FSM)
374static void http_silent_debug(int line, struct session *s)
375{
376 int size = 0;
377 size += snprintf(trash + size, sizeof(trash) - size,
378 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n",
379 line,
380 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
381 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags);
382 write(-1, trash, size);
383 size = 0;
384 size += snprintf(trash + size, sizeof(trash) - size,
385 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n",
386 line,
387 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
388 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward);
389
390 write(-1, trash, size);
391}
392#else
393#define http_silent_debug(l,s) do { } while (0)
394#endif
395
396/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100397 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
398 * CRLF. Text length is measured first, so it cannot be NULL.
399 * The header is also automatically added to the index <hdr_idx>, and the end
400 * of headers is automatically adjusted. The number of bytes added is returned
401 * on success, otherwise <0 is returned indicating an error.
402 */
403int http_header_add_tail(struct buffer *b, struct http_msg *msg,
404 struct hdr_idx *hdr_idx, const char *text)
405{
406 int bytes, len;
407
408 len = strlen(text);
409 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
410 if (!bytes)
411 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100412 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100413 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
414}
415
416/*
417 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
418 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
419 * the buffer is only opened and the space reserved, but nothing is copied.
420 * The header is also automatically added to the index <hdr_idx>, and the end
421 * of headers is automatically adjusted. The number of bytes added is returned
422 * on success, otherwise <0 is returned indicating an error.
423 */
424int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
425 struct hdr_idx *hdr_idx, const char *text, int len)
426{
427 int bytes;
428
429 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
430 if (!bytes)
431 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100432 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100433 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
434}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200435
436/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100437 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
438 * If so, returns the position of the first non-space character relative to
439 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
440 * to return a pointer to the place after the first space. Returns 0 if the
441 * header name does not match. Checks are case-insensitive.
442 */
443int http_header_match2(const char *hdr, const char *end,
444 const char *name, int len)
445{
446 const char *val;
447
448 if (hdr + len >= end)
449 return 0;
450 if (hdr[len] != ':')
451 return 0;
452 if (strncasecmp(hdr, name, len) != 0)
453 return 0;
454 val = hdr + len + 1;
455 while (val < end && HTTP_IS_SPHT(*val))
456 val++;
457 if ((val >= end) && (len + 2 <= end - hdr))
458 return len + 2; /* we may replace starting from second space */
459 return val - hdr;
460}
461
Willy Tarreau68085d82010-01-18 14:54:04 +0100462/* Find the end of the header value contained between <s> and <e>. See RFC2616,
463 * par 2.2 for more information. Note that it requires a valid header to return
464 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200465 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100466char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200467{
468 int quoted, qdpair;
469
470 quoted = qdpair = 0;
471 for (; s < e; s++) {
472 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200473 else if (quoted) {
474 if (*s == '\\') qdpair = 1;
475 else if (*s == '"') quoted = 0;
476 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200477 else if (*s == '"') quoted = 1;
478 else if (*s == ',') return s;
479 }
480 return s;
481}
482
483/* Find the first or next occurrence of header <name> in message buffer <sol>
484 * using headers index <idx>, and return it in the <ctx> structure. This
485 * structure holds everything necessary to use the header and find next
486 * occurrence. If its <idx> member is 0, the header is searched from the
487 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100488 * 1 when it finds a value, and 0 when there is no more. It is designed to work
489 * with headers defined as comma-separated lists. As a special case, if ctx->val
490 * is NULL when searching for a new values of a header, the current header is
491 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200492 */
493int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100494 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200495 struct hdr_ctx *ctx)
496{
Willy Tarreau68085d82010-01-18 14:54:04 +0100497 char *eol, *sov;
498 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200499
Willy Tarreau68085d82010-01-18 14:54:04 +0100500 cur_idx = ctx->idx;
501 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200502 /* We have previously returned a value, let's search
503 * another one on the same line.
504 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200505 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100506 ctx->del = ctx->val + ctx->vlen;
507 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 eol = sol + idx->v[cur_idx].len;
509
510 if (sov >= eol)
511 /* no more values in this header */
512 goto next_hdr;
513
Willy Tarreau68085d82010-01-18 14:54:04 +0100514 /* values remaining for this header, skip the comma but save it
515 * for later use (eg: for header deletion).
516 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200517 sov++;
518 while (sov < eol && http_is_lws[(unsigned char)*sov])
519 sov++;
520
521 goto return_hdr;
522 }
523
524 /* first request for this header */
525 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100526 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200527 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200528 while (cur_idx) {
529 eol = sol + idx->v[cur_idx].len;
530
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200531 if (len == 0) {
532 /* No argument was passed, we want any header.
533 * To achieve this, we simply build a fake request. */
534 while (sol + len < eol && sol[len] != ':')
535 len++;
536 name = sol;
537 }
538
Willy Tarreau33a7e692007-06-10 19:45:56 +0200539 if ((len < eol - sol) &&
540 (sol[len] == ':') &&
541 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100542 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200543 sov = sol + len + 1;
544 while (sov < eol && http_is_lws[(unsigned char)*sov])
545 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100546
Willy Tarreau33a7e692007-06-10 19:45:56 +0200547 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100548 ctx->prev = old_idx;
549 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200550 ctx->idx = cur_idx;
551 ctx->val = sov - sol;
552
553 eol = find_hdr_value_end(sov, eol);
554 ctx->vlen = eol - sov;
555 return 1;
556 }
557 next_hdr:
558 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100559 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200560 cur_idx = idx->v[cur_idx].next;
561 }
562 return 0;
563}
564
565int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100566 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200567 struct hdr_ctx *ctx)
568{
569 return http_find_header2(name, strlen(name), sol, idx, ctx);
570}
571
Willy Tarreau68085d82010-01-18 14:54:04 +0100572/* Remove one value of a header. This only works on a <ctx> returned by one of
573 * the http_find_header functions. The value is removed, as well as surrounding
574 * commas if any. If the removed value was alone, the whole header is removed.
575 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
576 * message <msg>. The new index is returned. If it is zero, it means there is
577 * no more header, so any processing may stop. The ctx is always left in a form
578 * that can be handled by http_find_header2() to find next occurrence.
579 */
580int http_remove_header2(struct http_msg *msg, struct buffer *buf,
581 struct hdr_idx *idx, struct hdr_ctx *ctx)
582{
583 int cur_idx = ctx->idx;
584 char *sol = ctx->line;
585 struct hdr_idx_elem *hdr;
586 int delta, skip_comma;
587
588 if (!cur_idx)
589 return 0;
590
591 hdr = &idx->v[cur_idx];
592 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) {
593 /* This was the only value of the header, we must now remove it entirely. */
594 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
595 http_msg_move_end(msg, delta);
596 idx->used--;
597 hdr->len = 0; /* unused entry */
598 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100599 if (idx->tail == ctx->idx)
600 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100601 ctx->idx = ctx->prev; /* walk back to the end of previous header */
602 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
603 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
604 ctx->vlen = 0;
605 return ctx->idx;
606 }
607
608 /* This was not the only value of this header. We have to remove between
609 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
610 * of the list, we remove the last separator.
611 */
612
613 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
614 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
615 sol + ctx->val + ctx->vlen + skip_comma,
616 NULL, 0);
617 hdr->len += delta;
618 http_msg_move_end(msg, delta);
619 ctx->val = ctx->del;
620 ctx->vlen = 0;
621 return ctx->idx;
622}
623
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100624/* This function handles a server error at the stream interface level. The
625 * stream interface is assumed to be already in a closed state. An optional
626 * message is copied into the input buffer, and an HTTP status code stored.
627 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100628 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100630static void http_server_error(struct session *t, struct stream_interface *si,
631 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100633 buffer_auto_read(si->ob);
634 buffer_abort(si->ob);
635 buffer_auto_close(si->ob);
636 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200637 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100638 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100639 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100640 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100641 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200642 }
643 if (!(t->flags & SN_ERR_MASK))
644 t->flags |= err;
645 if (!(t->flags & SN_FINST_MASK))
646 t->flags |= finst;
647}
648
Willy Tarreau80587432006-12-24 17:47:20 +0100649/* This function returns the appropriate error location for the given session
650 * and message.
651 */
652
653struct chunk *error_message(struct session *s, int msgnum)
654{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200655 if (s->be->errmsg[msgnum].str)
656 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100657 else if (s->fe->errmsg[msgnum].str)
658 return &s->fe->errmsg[msgnum];
659 else
660 return &http_err_chunks[msgnum];
661}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662
Willy Tarreau53b6c742006-12-17 13:37:46 +0100663/*
664 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
665 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
666 */
667static http_meth_t find_http_meth(const char *str, const int len)
668{
669 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100670 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100671
672 m = ((unsigned)*str - 'A');
673
674 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100675 for (h = http_methods[m]; h->len > 0; h++) {
676 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100677 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100678 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100679 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100680 };
681 return HTTP_METH_OTHER;
682 }
683 return HTTP_METH_NONE;
684
685}
686
Willy Tarreau21d2af32008-02-14 20:25:24 +0100687/* Parse the URI from the given transaction (which is assumed to be in request
688 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
689 * It is returned otherwise.
690 */
691static char *
692http_get_path(struct http_txn *txn)
693{
694 char *ptr, *end;
695
Willy Tarreau962c3f42010-01-10 00:15:35 +0100696 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100697 end = ptr + txn->req.sl.rq.u_l;
698
699 if (ptr >= end)
700 return NULL;
701
702 /* RFC2616, par. 5.1.2 :
703 * Request-URI = "*" | absuri | abspath | authority
704 */
705
706 if (*ptr == '*')
707 return NULL;
708
709 if (isalpha((unsigned char)*ptr)) {
710 /* this is a scheme as described by RFC3986, par. 3.1 */
711 ptr++;
712 while (ptr < end &&
713 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
714 ptr++;
715 /* skip '://' */
716 if (ptr == end || *ptr++ != ':')
717 return NULL;
718 if (ptr == end || *ptr++ != '/')
719 return NULL;
720 if (ptr == end || *ptr++ != '/')
721 return NULL;
722 }
723 /* skip [user[:passwd]@]host[:[port]] */
724
725 while (ptr < end && *ptr != '/')
726 ptr++;
727
728 if (ptr == end)
729 return NULL;
730
731 /* OK, we got the '/' ! */
732 return ptr;
733}
734
Willy Tarreauefb453c2008-10-26 20:49:47 +0100735/* Returns a 302 for a redirectable request. This may only be called just after
736 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
737 * left unchanged and will follow normal proxy processing.
738 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100740{
741 struct http_txn *txn;
742 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100743 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100744 char *path;
745 int len;
746
747 /* 1: create the response header */
748 rdr.len = strlen(HTTP_302);
749 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100750 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100751 memcpy(rdr.str, HTTP_302, rdr.len);
752
Willy Tarreau827aee92011-03-10 16:55:02 +0100753 srv = target_srv(&s->target);
754
Willy Tarreauefb453c2008-10-26 20:49:47 +0100755 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100756 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100757 return;
758
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100759 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100760 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
761 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
762 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100763 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764
765 /* 3: add the request URI */
766 txn = &s->txn;
767 path = http_get_path(txn);
768 if (!path)
769 return;
770
Willy Tarreau962c3f42010-01-10 00:15:35 +0100771 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200772 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 return;
774
775 memcpy(rdr.str + rdr.len, path, len);
776 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100777
778 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
779 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
780 rdr.len += 29;
781 } else {
782 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
783 rdr.len += 23;
784 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100785
786 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100787 si->shutr(si);
788 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 si->err_type = SI_ET_NONE;
790 si->err_loc = NULL;
791 si->state = SI_ST_CLO;
792
793 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100794 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100795
796 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100797 if (srv)
798 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100799}
800
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100801/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100802 * that the server side is closed. Note that err_type is actually a
803 * bitmask, where almost only aborts may be cumulated with other
804 * values. We consider that aborted operations are more important
805 * than timeouts or errors due to the fact that nobody else in the
806 * logs might explain incomplete retries. All others should avoid
807 * being cumulated. It should normally not be possible to have multiple
808 * aborts at once, but just in case, the first one in sequence is reported.
809 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100810void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100811{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100812 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813
814 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100815 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
816 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100818 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
819 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100820 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100821 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
822 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100823 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100824 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
825 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100826 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100827 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
828 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100829 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100830 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
831 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100832 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100833 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
834 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100835}
836
Willy Tarreau42250582007-04-01 01:30:43 +0200837extern const char sess_term_cond[8];
838extern const char sess_fin_state[8];
839extern const char *monthname[12];
Willy Tarreauf1348312010-10-07 15:54:11 +0200840const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */
841const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
842 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
843 Set-cookie Updated, unknown, unknown */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200844struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200845struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100846
Emeric Brun3a058f32009-06-30 18:26:00 +0200847void http_sess_clflog(struct session *s)
848{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100849 char pn[INET6_ADDRSTRLEN];
Emeric Brun3a058f32009-06-30 18:26:00 +0200850 struct proxy *fe = s->fe;
851 struct proxy *be = s->be;
852 struct proxy *prx_log;
853 struct http_txn *txn = &s->txn;
854 int tolog, level, err;
855 char *uri, *h;
Willy Tarreau71904a42011-02-13 14:30:26 +0100856 const char *svid;
Emeric Brun3a058f32009-06-30 18:26:00 +0200857 struct tm tm;
858 static char tmpline[MAX_SYSLOG_LEN];
859 int hdr;
860 size_t w;
861 int t_request;
862
863 prx_log = fe;
864 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +0200865 (s->req->cons->conn_retries != be->conn_retries) ||
Emeric Brun3a058f32009-06-30 18:26:00 +0200866 txn->status >= 500;
867
Willy Tarreau957c0a52011-03-03 17:42:23 +0100868 if (s->req->prod->addr.c.from.ss_family == AF_INET)
Emeric Brun3a058f32009-06-30 18:26:00 +0200869 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100870 (const void *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr,
Emeric Brun3a058f32009-06-30 18:26:00 +0200871 pn, sizeof(pn));
Willy Tarreau957c0a52011-03-03 17:42:23 +0100872 else if (s->req->prod->addr.c.from.ss_family == AF_INET6)
Emeric Brun3a058f32009-06-30 18:26:00 +0200873 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100874 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr,
Emeric Brun3a058f32009-06-30 18:26:00 +0200875 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +0200876 else
877 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
Emeric Brun3a058f32009-06-30 18:26:00 +0200878
879 get_gmtime(s->logs.accept_date.tv_sec, &tm);
880
881 /* FIXME: let's limit ourselves to frontend logging for now. */
882 tolog = fe->to_log;
883
884 h = tmpline;
885
886 w = snprintf(h, sizeof(tmpline),
887 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
888 pn,
889 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
890 tm.tm_hour, tm.tm_min, tm.tm_sec);
891 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
892 goto trunc;
893 h += w;
894
895 if (h >= tmpline + sizeof(tmpline) - 4)
896 goto trunc;
897
898 *(h++) = ' ';
899 *(h++) = '\"';
900 uri = txn->uri ? txn->uri : "<BADREQ>";
901 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
902 '#', url_encode_map, uri);
903 *(h++) = '\"';
904
905 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
906 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
907 goto trunc;
908 h += w;
909
910 if (h >= tmpline + sizeof(tmpline) - 9)
911 goto trunc;
912 memcpy(h, " \"-\" \"-\"", 8);
913 h += 8;
914
915 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
916 " %d %03d",
Willy Tarreau957c0a52011-03-03 17:42:23 +0100917 s->req->prod->addr.c.from.ss_family == AF_UNIX ? s->listener->luid :
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200918 get_host_port(&s->req->prod->addr.c.from),
Emeric Brun3a058f32009-06-30 18:26:00 +0200919 (int)s->logs.accept_date.tv_usec/1000);
920 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
921 goto trunc;
922 h += w;
923
924 w = strlen(fe->id);
925 if (h >= tmpline + sizeof(tmpline) - 4 - w)
926 goto trunc;
927 *(h++) = ' ';
928 *(h++) = '\"';
929 memcpy(h, fe->id, w);
930 h += w;
931 *(h++) = '\"';
932
933 w = strlen(be->id);
934 if (h >= tmpline + sizeof(tmpline) - 4 - w)
935 goto trunc;
936 *(h++) = ' ';
937 *(h++) = '\"';
938 memcpy(h, be->id, w);
939 h += w;
940 *(h++) = '\"';
941
Willy Tarreau71904a42011-02-13 14:30:26 +0100942 if (!(tolog & LW_SVID))
943 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200944 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100945 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200946 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100947 break;
948 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200949 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100950 break;
951 default:
952 svid = "<NOSRV>";
953 break;
954 }
Emeric Brun3a058f32009-06-30 18:26:00 +0200955
956 w = strlen(svid);
957 if (h >= tmpline + sizeof(tmpline) - 4 - w)
958 goto trunc;
959 *(h++) = ' ';
960 *(h++) = '\"';
961 memcpy(h, svid, w);
962 h += w;
963 *(h++) = '\"';
964
965 t_request = -1;
966 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
967 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
968 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
969 " %d %ld %ld %ld %ld",
970 t_request,
971 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
972 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
973 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
974 s->logs.t_close);
975 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
976 goto trunc;
977 h += w;
978
979 if (h >= tmpline + sizeof(tmpline) - 8)
980 goto trunc;
981 *(h++) = ' ';
982 *(h++) = '\"';
983 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
984 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
985 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
986 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
987 *(h++) = '\"';
988
989 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
990 " %d %d %d %d %d %ld %ld",
Willy Tarreau827aee92011-03-10 16:55:02 +0100991 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Willy Tarreauee28de02010-06-01 09:51:00 +0200992 (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries,
Emeric Brun3a058f32009-06-30 18:26:00 +0200993 s->logs.srv_queue_size, s->logs.prx_queue_size);
994
995 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
996 goto trunc;
997 h += w;
998
999 if (txn->cli_cookie) {
1000 w = strlen(txn->cli_cookie);
1001 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1002 goto trunc;
1003 *(h++) = ' ';
1004 *(h++) = '\"';
1005 memcpy(h, txn->cli_cookie, w);
1006 h += w;
1007 *(h++) = '\"';
1008 } else {
1009 if (h >= tmpline + sizeof(tmpline) - 5)
1010 goto trunc;
1011 memcpy(h, " \"-\"", 4);
1012 h += 4;
1013 }
1014
1015 if (txn->srv_cookie) {
1016 w = strlen(txn->srv_cookie);
1017 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1018 goto trunc;
1019 *(h++) = ' ';
1020 *(h++) = '\"';
1021 memcpy(h, txn->srv_cookie, w);
1022 h += w;
1023 *(h++) = '\"';
1024 } else {
1025 if (h >= tmpline + sizeof(tmpline) - 5)
1026 goto trunc;
1027 memcpy(h, " \"-\"", 4);
1028 h += 4;
1029 }
1030
1031 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1032 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1033 if (h >= sizeof (tmpline) + tmpline - 4)
1034 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001035 if (txn->req.cap[hdr] != NULL) {
1036 *(h++) = ' ';
1037 *(h++) = '\"';
1038 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1039 '#', hdr_encode_map, txn->req.cap[hdr]);
1040 *(h++) = '\"';
1041 } else {
1042 memcpy(h, " \"-\"", 4);
1043 h += 4;
1044 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001045 }
1046 }
1047
1048 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1049 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1050 if (h >= sizeof (tmpline) + tmpline - 4)
1051 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001052 if (txn->rsp.cap[hdr] != NULL) {
1053 *(h++) = ' ';
1054 *(h++) = '\"';
1055 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1056 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1057 *(h++) = '\"';
1058 } else {
1059 memcpy(h, " \"-\"", 4);
1060 h += 4;
1061 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001062 }
1063 }
1064
1065trunc:
1066 *h = '\0';
1067
1068 level = LOG_INFO;
1069 if (err && (fe->options2 & PR_O2_LOGERRORS))
1070 level = LOG_ERR;
1071
1072 send_log(prx_log, level, "%s\n", tmpline);
1073
1074 s->logs.logwait = 0;
1075}
1076
Willy Tarreau42250582007-04-01 01:30:43 +02001077/*
1078 * send a log for the session when we have enough info about it.
1079 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001080 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001081void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001082{
Cyril Bontéacd7d632010-11-01 19:26:02 +01001083 char pn[INET6_ADDRSTRLEN];
Willy Tarreau42250582007-04-01 01:30:43 +02001084 struct proxy *fe = s->fe;
1085 struct proxy *be = s->be;
1086 struct proxy *prx_log;
1087 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001088 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001089 char *uri, *h;
Willy Tarreau71904a42011-02-13 14:30:26 +01001090 const char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001091 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001092 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001093 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001094 int hdr;
1095
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001096 /* if we don't want to log normal traffic, return now */
1097 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +02001098 (s->req->cons->conn_retries != be->conn_retries) ||
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001099 txn->status >= 500;
1100 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1101 return;
1102
Willy Tarreau42250582007-04-01 01:30:43 +02001103 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1104 return;
1105 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001106
Emeric Brun3a058f32009-06-30 18:26:00 +02001107 if (prx_log->options2 & PR_O2_CLFLOG)
1108 return http_sess_clflog(s);
1109
Willy Tarreau957c0a52011-03-03 17:42:23 +01001110 if (s->req->prod->addr.c.from.ss_family == AF_INET)
Willy Tarreau42250582007-04-01 01:30:43 +02001111 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01001112 (const void *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr,
Willy Tarreau42250582007-04-01 01:30:43 +02001113 pn, sizeof(pn));
Willy Tarreau957c0a52011-03-03 17:42:23 +01001114 else if (s->req->prod->addr.c.from.ss_family == AF_INET6)
Willy Tarreau42250582007-04-01 01:30:43 +02001115 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01001116 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr,
Willy Tarreau42250582007-04-01 01:30:43 +02001117 pn, sizeof(pn));
1118
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001119 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001120
1121 /* FIXME: let's limit ourselves to frontend logging for now. */
1122 tolog = fe->to_log;
1123
1124 h = tmpline;
1125 if (fe->to_log & LW_REQHDR &&
1126 txn->req.cap &&
1127 (h < tmpline + sizeof(tmpline) - 10)) {
1128 *(h++) = ' ';
1129 *(h++) = '{';
1130 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1131 if (hdr)
1132 *(h++) = '|';
1133 if (txn->req.cap[hdr] != NULL)
1134 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1135 '#', hdr_encode_map, txn->req.cap[hdr]);
1136 }
1137 *(h++) = '}';
1138 }
1139
1140 if (fe->to_log & LW_RSPHDR &&
1141 txn->rsp.cap &&
1142 (h < tmpline + sizeof(tmpline) - 7)) {
1143 *(h++) = ' ';
1144 *(h++) = '{';
1145 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1146 if (hdr)
1147 *(h++) = '|';
1148 if (txn->rsp.cap[hdr] != NULL)
1149 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1150 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1151 }
1152 *(h++) = '}';
1153 }
1154
1155 if (h < tmpline + sizeof(tmpline) - 4) {
1156 *(h++) = ' ';
1157 *(h++) = '"';
1158 uri = txn->uri ? txn->uri : "<BADREQ>";
1159 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1160 '#', url_encode_map, uri);
1161 *(h++) = '"';
1162 }
1163 *h = '\0';
1164
Willy Tarreau71904a42011-02-13 14:30:26 +01001165 if (!(tolog & LW_SVID))
1166 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001167 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +01001168 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001169 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +01001170 break;
1171 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001172 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +01001173 break;
1174 default:
1175 svid = "<NOSRV>";
1176 break;
1177 }
Willy Tarreau42250582007-04-01 01:30:43 +02001178
Willy Tarreau70089872008-06-13 21:12:51 +02001179 t_request = -1;
1180 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1181 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1182
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001183 level = LOG_INFO;
1184 if (err && (fe->options2 & PR_O2_LOGERRORS))
1185 level = LOG_ERR;
1186
1187 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001188 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001189 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1190 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau957c0a52011-03-03 17:42:23 +01001191 (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? "unix" : pn,
1192 (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? s->listener->luid :
Willy Tarreau86ad42c2011-08-27 12:29:07 +02001193 get_host_port(&s->req->prod->addr.c.from),
Willy Tarreaufe944602007-10-25 10:34:16 +02001194 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001195 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001196 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001197 t_request,
1198 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001199 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1200 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1201 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1202 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001203 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001204 txn->cli_cookie ? txn->cli_cookie : "-",
1205 txn->srv_cookie ? txn->srv_cookie : "-",
1206 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1207 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1208 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1209 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
Willy Tarreau827aee92011-03-10 16:55:02 +01001210 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001211 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +02001212 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001213 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1214
1215 s->logs.logwait = 0;
1216}
1217
Willy Tarreau117f59e2007-03-04 18:17:17 +01001218
1219/*
1220 * Capture headers from message starting at <som> according to header list
1221 * <cap_hdr>, and fill the <idx> structure appropriately.
1222 */
1223void capture_headers(char *som, struct hdr_idx *idx,
1224 char **cap, struct cap_hdr *cap_hdr)
1225{
1226 char *eol, *sol, *col, *sov;
1227 int cur_idx;
1228 struct cap_hdr *h;
1229 int len;
1230
1231 sol = som + hdr_idx_first_pos(idx);
1232 cur_idx = hdr_idx_first_idx(idx);
1233
1234 while (cur_idx) {
1235 eol = sol + idx->v[cur_idx].len;
1236
1237 col = sol;
1238 while (col < eol && *col != ':')
1239 col++;
1240
1241 sov = col + 1;
1242 while (sov < eol && http_is_lws[(unsigned char)*sov])
1243 sov++;
1244
1245 for (h = cap_hdr; h; h = h->next) {
1246 if ((h->namelen == col - sol) &&
1247 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1248 if (cap[h->index] == NULL)
1249 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001250 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001251
1252 if (cap[h->index] == NULL) {
1253 Alert("HTTP capture : out of memory.\n");
1254 continue;
1255 }
1256
1257 len = eol - sov;
1258 if (len > h->len)
1259 len = h->len;
1260
1261 memcpy(cap[h->index], sov, len);
1262 cap[h->index][len]=0;
1263 }
1264 }
1265 sol = eol + idx->v[cur_idx].cr + 1;
1266 cur_idx = idx->v[cur_idx].next;
1267 }
1268}
1269
1270
Willy Tarreau42250582007-04-01 01:30:43 +02001271/* either we find an LF at <ptr> or we jump to <bad>.
1272 */
1273#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1274
1275/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1276 * otherwise to <http_msg_ood> with <state> set to <st>.
1277 */
1278#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1279 ptr++; \
1280 if (likely(ptr < end)) \
1281 goto good; \
1282 else { \
1283 state = (st); \
1284 goto http_msg_ood; \
1285 } \
1286 } while (0)
1287
1288
Willy Tarreaubaaee002006-06-26 02:48:02 +02001289/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001290 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001291 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1292 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1293 * will give undefined results.
1294 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1295 * and that msg->sol points to the beginning of the response.
1296 * If a complete line is found (which implies that at least one CR or LF is
1297 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1298 * returned indicating an incomplete line (which does not mean that parts have
1299 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1300 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1301 * upon next call.
1302 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001303 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001304 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1305 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001306 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001307 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001308const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1309 unsigned int state, const char *ptr, const char *end,
1310 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001311{
Willy Tarreau8973c702007-01-21 23:58:29 +01001312 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001313 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001314 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001315 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1317
1318 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001319 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001320 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1321 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001322 state = HTTP_MSG_ERROR;
1323 break;
1324
Willy Tarreau8973c702007-01-21 23:58:29 +01001325 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001326 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001327 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001328 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001329 goto http_msg_rpcode;
1330 }
1331 if (likely(HTTP_IS_SPHT(*ptr)))
1332 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1333 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001334 state = HTTP_MSG_ERROR;
1335 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001336
Willy Tarreau8973c702007-01-21 23:58:29 +01001337 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001338 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 if (likely(!HTTP_IS_LWS(*ptr)))
1340 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1341
1342 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001343 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001344 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1345 }
1346
1347 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001348 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 http_msg_rsp_reason:
1350 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001351 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 msg->sl.st.r_l = 0;
1353 goto http_msg_rpline_eol;
1354
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001356 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001357 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001358 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001359 goto http_msg_rpreason;
1360 }
1361 if (likely(HTTP_IS_SPHT(*ptr)))
1362 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1363 /* so it's a CR/LF, so there is no reason phrase */
1364 goto http_msg_rsp_reason;
1365
Willy Tarreau8973c702007-01-21 23:58:29 +01001366 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001367 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001368 if (likely(!HTTP_IS_CRLF(*ptr)))
1369 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001370 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001371 http_msg_rpline_eol:
1372 /* We have seen the end of line. Note that we do not
1373 * necessarily have the \n yet, but at least we know that we
1374 * have EITHER \r OR \n, otherwise the response would not be
1375 * complete. We can then record the response length and return
1376 * to the caller which will be able to register it.
1377 */
1378 msg->sl.st.l = ptr - msg->sol;
1379 return ptr;
1380
1381#ifdef DEBUG_FULL
1382 default:
1383 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1384 exit(1);
1385#endif
1386 }
1387
1388 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001389 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001390 if (ret_state)
1391 *ret_state = state;
1392 if (ret_ptr)
1393 *ret_ptr = (char *)ptr;
1394 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001395}
1396
Willy Tarreau8973c702007-01-21 23:58:29 +01001397/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 * This function parses a request line between <ptr> and <end>, starting with
1399 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1400 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1401 * will give undefined results.
1402 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1403 * and that msg->sol points to the beginning of the request.
1404 * If a complete line is found (which implies that at least one CR or LF is
1405 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1406 * returned indicating an incomplete line (which does not mean that parts have
1407 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1408 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1409 * upon next call.
1410 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001411 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1413 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001414 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001415 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001416const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1417 unsigned int state, const char *ptr, const char *end,
1418 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001419{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001422 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 if (likely(HTTP_IS_TOKEN(*ptr)))
1424 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001425
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001427 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1429 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001430
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 if (likely(HTTP_IS_CRLF(*ptr))) {
1432 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001433 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 http_msg_req09_uri:
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 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001437 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001439 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 msg->sl.rq.v_l = 0;
1441 goto http_msg_rqline_eol;
1442 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001443 state = HTTP_MSG_ERROR;
1444 break;
1445
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001446 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001447 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001449 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 goto http_msg_rquri;
1451 }
1452 if (likely(HTTP_IS_SPHT(*ptr)))
1453 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1454 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1455 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001458 http_msg_rquri:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 if (likely(!HTTP_IS_LWS(*ptr)))
1460 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001463 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1465 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001466
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001467 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1468 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001469
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001471 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001473 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 goto http_msg_rqver;
1475 }
1476 if (likely(HTTP_IS_SPHT(*ptr)))
1477 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1478 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1479 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001482 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001483 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001485
1486 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001487 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001488 http_msg_rqline_eol:
1489 /* We have seen the end of line. Note that we do not
1490 * necessarily have the \n yet, but at least we know that we
1491 * have EITHER \r OR \n, otherwise the request would not be
1492 * complete. We can then record the request length and return
1493 * to the caller which will be able to register it.
1494 */
1495 msg->sl.rq.l = ptr - msg->sol;
1496 return ptr;
1497 }
1498
1499 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001500 state = HTTP_MSG_ERROR;
1501 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001502
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503#ifdef DEBUG_FULL
1504 default:
1505 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1506 exit(1);
1507#endif
1508 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001509
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001511 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 if (ret_state)
1513 *ret_state = state;
1514 if (ret_ptr)
1515 *ret_ptr = (char *)ptr;
1516 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001518
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001519/*
1520 * Returns the data from Authorization header. Function may be called more
1521 * than once so data is stored in txn->auth_data. When no header is found
1522 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1523 * searching again for something we are unable to find anyway.
1524 */
1525
1526char get_http_auth_buff[BUFSIZE];
1527
1528int
1529get_http_auth(struct session *s)
1530{
1531
1532 struct http_txn *txn = &s->txn;
1533 struct chunk auth_method;
1534 struct hdr_ctx ctx;
1535 char *h, *p;
1536 int len;
1537
1538#ifdef DEBUG_AUTH
1539 printf("Auth for session %p: %d\n", s, txn->auth.method);
1540#endif
1541
1542 if (txn->auth.method == HTTP_AUTH_WRONG)
1543 return 0;
1544
1545 if (txn->auth.method)
1546 return 1;
1547
1548 txn->auth.method = HTTP_AUTH_WRONG;
1549
1550 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001551
1552 if (txn->flags & TX_USE_PX_CONN) {
1553 h = "Proxy-Authorization";
1554 len = strlen(h);
1555 } else {
1556 h = "Authorization";
1557 len = strlen(h);
1558 }
1559
1560 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001561 return 0;
1562
1563 h = ctx.line + ctx.val;
1564
1565 p = memchr(h, ' ', ctx.vlen);
1566 if (!p || p == h)
1567 return 0;
1568
1569 chunk_initlen(&auth_method, h, 0, p-h);
1570 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1571
1572 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1573
1574 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1575 get_http_auth_buff, BUFSIZE - 1);
1576
1577 if (len < 0)
1578 return 0;
1579
1580
1581 get_http_auth_buff[len] = '\0';
1582
1583 p = strchr(get_http_auth_buff, ':');
1584
1585 if (!p)
1586 return 0;
1587
1588 txn->auth.user = get_http_auth_buff;
1589 *p = '\0';
1590 txn->auth.pass = p+1;
1591
1592 txn->auth.method = HTTP_AUTH_BASIC;
1593 return 1;
1594 }
1595
1596 return 0;
1597}
1598
Willy Tarreau58f10d72006-12-04 02:26:12 +01001599
Willy Tarreau8973c702007-01-21 23:58:29 +01001600/*
1601 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001602 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001603 * when data are missing and recalled at the exact same location with no
1604 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001605 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001606 * fields. Note that msg->som and msg->sol will be initialized after completing
1607 * the first state, so that none of the msg pointers has to be initialized
1608 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001609 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1611{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001612 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001613 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001614
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001615 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 ptr = buf->lr;
1617 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619 if (unlikely(ptr >= end))
1620 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001621
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001622 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001623 /*
1624 * First, states that are specific to the response only.
1625 * We check them first so that request and headers are
1626 * closer to each other (accessed more often).
1627 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001628 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001629 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001630 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001631 /* we have a start of message, but we have to check
1632 * first if we need to remove some CRLF. We can only
1633 * do this when send_max=0.
1634 */
1635 char *beg = buf->w + buf->send_max;
1636 if (beg >= buf->data + buf->size)
1637 beg -= buf->size;
1638 if (unlikely(ptr != beg)) {
1639 if (buf->send_max)
1640 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001641 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001642 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001643 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001644 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001645 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001646 hdr_idx_init(idx);
1647 state = HTTP_MSG_RPVER;
1648 goto http_msg_rpver;
1649 }
1650
1651 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1652 goto http_msg_invalid;
1653
1654 if (unlikely(*ptr == '\n'))
1655 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1656 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1657 /* stop here */
1658
Willy Tarreau8973c702007-01-21 23:58:29 +01001659 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001660 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001661 EXPECT_LF_HERE(ptr, http_msg_invalid);
1662 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1663 /* stop here */
1664
Willy Tarreau8973c702007-01-21 23:58:29 +01001665 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001666 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001667 case HTTP_MSG_RPVER_SP:
1668 case HTTP_MSG_RPCODE:
1669 case HTTP_MSG_RPCODE_SP:
1670 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001671 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001672 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001673 if (unlikely(!ptr))
1674 return;
1675
1676 /* we have a full response and we know that we have either a CR
1677 * or an LF at <ptr>.
1678 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001679 //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 +01001680 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1681
1682 msg->sol = ptr;
1683 if (likely(*ptr == '\r'))
1684 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1685 goto http_msg_rpline_end;
1686
Willy Tarreau8973c702007-01-21 23:58:29 +01001687 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001688 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001689 /* msg->sol must point to the first of CR or LF. */
1690 EXPECT_LF_HERE(ptr, http_msg_invalid);
1691 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1692 /* stop here */
1693
1694 /*
1695 * Second, states that are specific to the request only
1696 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001697 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001698 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001699 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001700 /* we have a start of message, but we have to check
1701 * first if we need to remove some CRLF. We can only
1702 * do this when send_max=0.
1703 */
1704 char *beg = buf->w + buf->send_max;
1705 if (beg >= buf->data + buf->size)
1706 beg -= buf->size;
1707 if (likely(ptr != beg)) {
1708 if (buf->send_max)
1709 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001710 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001711 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001712 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001713 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001714 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001715 /* we will need this when keep-alive will be supported
1716 hdr_idx_init(idx);
1717 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001718 state = HTTP_MSG_RQMETH;
1719 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001721
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1723 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001724
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 if (unlikely(*ptr == '\n'))
1726 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1727 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001728 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001729
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001731 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 EXPECT_LF_HERE(ptr, http_msg_invalid);
1733 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001734 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001735
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001736 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001737 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 case HTTP_MSG_RQMETH_SP:
1739 case HTTP_MSG_RQURI:
1740 case HTTP_MSG_RQURI_SP:
1741 case HTTP_MSG_RQVER:
1742 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001743 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 if (unlikely(!ptr))
1745 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001746
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 /* we have a full request and we know that we have either a CR
1748 * or an LF at <ptr>.
1749 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001750 //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 +01001751 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001752
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 msg->sol = ptr;
1754 if (likely(*ptr == '\r'))
1755 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001757
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001759 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 /* check for HTTP/0.9 request : no version information available.
1761 * msg->sol must point to the first of CR or LF.
1762 */
1763 if (unlikely(msg->sl.rq.v_l == 0))
1764 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001765
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001766 EXPECT_LF_HERE(ptr, http_msg_invalid);
1767 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001768 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001769
Willy Tarreau8973c702007-01-21 23:58:29 +01001770 /*
1771 * Common states below
1772 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001774 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775 msg->sol = ptr;
1776 if (likely(!HTTP_IS_CRLF(*ptr))) {
1777 goto http_msg_hdr_name;
1778 }
1779
1780 if (likely(*ptr == '\r'))
1781 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1782 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001783
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001785 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786 /* assumes msg->sol points to the first char */
1787 if (likely(HTTP_IS_TOKEN(*ptr)))
1788 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001789
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 if (likely(*ptr == ':')) {
1791 msg->col = ptr - buf->data;
1792 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1793 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001794
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001795 if (likely(msg->err_pos < -1) || *ptr == '\n')
1796 goto http_msg_invalid;
1797
1798 if (msg->err_pos == -1) /* capture error pointer */
1799 msg->err_pos = ptr - buf->data; /* >= 0 now */
1800
1801 /* and we still accept this non-token character */
1802 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001804 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001805 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 /* assumes msg->sol points to the first char and msg->col to the colon */
1807 if (likely(HTTP_IS_SPHT(*ptr)))
1808 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001809
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001810 /* header value can be basically anything except CR/LF */
1811 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001812
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001813 if (likely(!HTTP_IS_CRLF(*ptr))) {
1814 goto http_msg_hdr_val;
1815 }
1816
1817 if (likely(*ptr == '\r'))
1818 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1819 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001820
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001821 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001822 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001823 EXPECT_LF_HERE(ptr, http_msg_invalid);
1824 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001825
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001827 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 if (likely(HTTP_IS_SPHT(*ptr))) {
1829 /* replace HT,CR,LF with spaces */
1830 for (; buf->data+msg->sov < ptr; msg->sov++)
1831 buf->data[msg->sov] = ' ';
1832 goto http_msg_hdr_l1_sp;
1833 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001834 /* we had a header consisting only in spaces ! */
1835 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001836 goto http_msg_complete_header;
1837
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001839 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 /* assumes msg->sol points to the first char, msg->col to the
1841 * colon, and msg->sov points to the first character of the
1842 * value.
1843 */
1844 if (likely(!HTTP_IS_CRLF(*ptr)))
1845 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001846
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001847 msg->eol = ptr;
1848 /* Note: we could also copy eol into ->eoh so that we have the
1849 * real header end in case it ends with lots of LWS, but is this
1850 * really needed ?
1851 */
1852 if (likely(*ptr == '\r'))
1853 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1854 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001855
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001856 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001857 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001858 EXPECT_LF_HERE(ptr, http_msg_invalid);
1859 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001860
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001861 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001862 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1864 /* LWS: replace HT,CR,LF with spaces */
1865 for (; msg->eol < ptr; msg->eol++)
1866 *msg->eol = ' ';
1867 goto http_msg_hdr_val;
1868 }
1869 http_msg_complete_header:
1870 /*
1871 * It was a new header, so the last one is finished.
1872 * Assumes msg->sol points to the first char, msg->col to the
1873 * colon, msg->sov points to the first character of the value
1874 * and msg->eol to the first CR or LF so we know how the line
1875 * ends. We insert last header into the index.
1876 */
1877 /*
1878 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1879 write(2, msg->sol, msg->eol-msg->sol);
1880 fprintf(stderr,"\n");
1881 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001882
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1884 idx, idx->tail) < 0))
1885 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001886
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 msg->sol = ptr;
1888 if (likely(!HTTP_IS_CRLF(*ptr))) {
1889 goto http_msg_hdr_name;
1890 }
1891
1892 if (likely(*ptr == '\r'))
1893 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1894 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001895
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001897 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001898 /* Assumes msg->sol points to the first of either CR or LF */
1899 EXPECT_LF_HERE(ptr, http_msg_invalid);
1900 ptr++;
1901 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001902 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001903 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001904 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001905 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001906 return;
1907#ifdef DEBUG_FULL
1908 default:
1909 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1910 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001911#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001912 }
1913 http_msg_ood:
1914 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001915 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001916 buf->lr = ptr;
1917 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001918
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001919 http_msg_invalid:
1920 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001921 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001922 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001923 return;
1924}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001925
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001926/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1927 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1928 * nothing is done and 1 is returned.
1929 */
1930static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1931{
1932 int delta;
1933 char *cur_end;
1934
1935 if (msg->sl.rq.v_l != 0)
1936 return 1;
1937
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001938 cur_end = msg->sol + msg->sl.rq.l;
1939 delta = 0;
1940
1941 if (msg->sl.rq.u_l == 0) {
1942 /* if no URI was set, add "/" */
1943 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1944 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001945 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001946 }
1947 /* add HTTP version */
1948 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001949 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001950 cur_end += delta;
1951 cur_end = (char *)http_parse_reqline(msg, req->data,
1952 HTTP_MSG_RQMETH,
1953 msg->sol, cur_end + 1,
1954 NULL, NULL);
1955 if (unlikely(!cur_end))
1956 return 0;
1957
1958 /* we have a full HTTP/1.0 request now and we know that
1959 * we have either a CR or an LF at <ptr>.
1960 */
1961 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1962 return 1;
1963}
1964
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001965/* Parse the Connection: header of an HTTP request, looking for both "close"
1966 * and "keep-alive" values. If a buffer is provided and we already know that
1967 * some headers may safely be removed, we remove them now. The <to_del> flags
1968 * are used for that :
1969 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1970 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1971 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1972 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1973 * harmless combinations may be removed. Do not call that after changes have
1974 * been processed. If unused, the buffer can be NULL, and no data will be
1975 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001976 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001977void 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 +01001978{
Willy Tarreau5b154472009-12-21 20:11:07 +01001979 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001980 const char *hdr_val = "Connection";
1981 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001982
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001983 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001984 return;
1985
Willy Tarreau88d349d2010-01-25 12:15:43 +01001986 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1987 hdr_val = "Proxy-Connection";
1988 hdr_len = 16;
1989 }
1990
Willy Tarreau5b154472009-12-21 20:11:07 +01001991 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001992 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001993 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001994 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1995 txn->flags |= TX_HDR_CONN_KAL;
1996 if ((to_del & 2) && buf)
1997 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1998 else
1999 txn->flags |= TX_CON_KAL_SET;
2000 }
2001 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2002 txn->flags |= TX_HDR_CONN_CLO;
2003 if ((to_del & 1) && buf)
2004 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
2005 else
2006 txn->flags |= TX_CON_CLO_SET;
2007 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002008 }
2009
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002010 txn->flags |= TX_HDR_CONN_PRS;
2011 return;
2012}
Willy Tarreau5b154472009-12-21 20:11:07 +01002013
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002014/* Apply desired changes on the Connection: header. Values may be removed and/or
2015 * added depending on the <wanted> flags, which are exclusively composed of
2016 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2017 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2018 */
2019void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
2020{
2021 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002022 const char *hdr_val = "Connection";
2023 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002024
2025 ctx.idx = 0;
2026
Willy Tarreau88d349d2010-01-25 12:15:43 +01002027
2028 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2029 hdr_val = "Proxy-Connection";
2030 hdr_len = 16;
2031 }
2032
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002033 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002034 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002035 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2036 if (wanted & TX_CON_KAL_SET)
2037 txn->flags |= TX_CON_KAL_SET;
2038 else
2039 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002040 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002041 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2042 if (wanted & TX_CON_CLO_SET)
2043 txn->flags |= TX_CON_CLO_SET;
2044 else
2045 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002046 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002047 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002048
2049 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2050 return;
2051
2052 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2053 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002054 hdr_val = "Connection: close";
2055 hdr_len = 17;
2056 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2057 hdr_val = "Proxy-Connection: close";
2058 hdr_len = 23;
2059 }
2060 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002061 }
2062
2063 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2064 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002065 hdr_val = "Connection: keep-alive";
2066 hdr_len = 22;
2067 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2068 hdr_val = "Proxy-Connection: keep-alive";
2069 hdr_len = 28;
2070 }
2071 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002072 }
2073 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002074}
2075
Willy Tarreaud98cf932009-12-27 22:54:55 +01002076/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2077 * first byte of body, and increments msg->sov by the number of bytes parsed,
2078 * so that we know we can forward between ->som and ->sov. Note that due to
2079 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2080 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002081 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002082 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002083 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002084int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002085{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002086 char *ptr = buf->lr;
2087 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002088 unsigned int chunk = 0;
2089
2090 /* The chunk size is in the following form, though we are only
2091 * interested in the size and CRLF :
2092 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2093 */
2094 while (1) {
2095 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002096 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002097 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002098 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002099 if (c < 0) /* not a hex digit anymore */
2100 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002101 if (++ptr >= end)
2102 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002103 if (chunk & 0xF000000) /* overflow will occur */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002104 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002105 chunk = (chunk << 4) + c;
2106 }
2107
Willy Tarreaud98cf932009-12-27 22:54:55 +01002108 /* empty size not allowed */
2109 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002110 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002111
2112 while (http_is_spht[(unsigned char)*ptr]) {
2113 if (++ptr >= end)
2114 ptr = buf->data;
2115 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002116 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002117 }
2118
Willy Tarreaud98cf932009-12-27 22:54:55 +01002119 /* Up to there, we know that at least one byte is present at *ptr. Check
2120 * for the end of chunk size.
2121 */
2122 while (1) {
2123 if (likely(HTTP_IS_CRLF(*ptr))) {
2124 /* we now have a CR or an LF at ptr */
2125 if (likely(*ptr == '\r')) {
2126 if (++ptr >= end)
2127 ptr = buf->data;
2128 if (ptr == buf->r)
2129 return 0;
2130 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002131
Willy Tarreaud98cf932009-12-27 22:54:55 +01002132 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002133 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002134 if (++ptr >= end)
2135 ptr = buf->data;
2136 /* done */
2137 break;
2138 }
2139 else if (*ptr == ';') {
2140 /* chunk extension, ends at next CRLF */
2141 if (++ptr >= end)
2142 ptr = buf->data;
2143 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002144 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002145
2146 while (!HTTP_IS_CRLF(*ptr)) {
2147 if (++ptr >= end)
2148 ptr = buf->data;
2149 if (ptr == buf->r)
2150 return 0;
2151 }
2152 /* we have a CRLF now, loop above */
2153 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002154 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002155 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002156 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002157 }
2158
Willy Tarreaud98cf932009-12-27 22:54:55 +01002159 /* OK we found our CRLF and now <ptr> points to the next byte,
2160 * which may or may not be present. We save that into ->lr and
2161 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002162 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002163 msg->sov += ptr - buf->lr;
2164 buf->lr = ptr;
Willy Tarreau124d9912011-03-01 20:30:48 +01002165 msg->chunk_len = chunk;
2166 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002167 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002168 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002169 error:
2170 msg->err_pos = ptr - buf->data;
2171 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002172}
2173
Willy Tarreaud98cf932009-12-27 22:54:55 +01002174/* This function skips trailers in the buffer <buf> associated with HTTP
2175 * message <msg>. The first visited position is buf->lr. If the end of
2176 * the trailers is found, it is automatically scheduled to be forwarded,
2177 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2178 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002179 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2180 * zero. If a parse error is encountered, the function returns < 0 and does not
2181 * change anything except maybe buf->lr and msg->sov. Note that the message
2182 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2183 * which implies that all non-trailers data have already been scheduled for
2184 * forwarding, and that the difference between msg->som and msg->sov exactly
2185 * matches the length of trailers already parsed and not forwarded. It is also
2186 * important to note that this function is designed to be able to parse wrapped
2187 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002188 */
2189int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2190{
2191 /* we have buf->lr which points to next line. Look for CRLF. */
2192 while (1) {
2193 char *p1 = NULL, *p2 = NULL;
2194 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002195 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002196
2197 /* scan current line and stop at LF or CRLF */
2198 while (1) {
2199 if (ptr == buf->r)
2200 return 0;
2201
2202 if (*ptr == '\n') {
2203 if (!p1)
2204 p1 = ptr;
2205 p2 = ptr;
2206 break;
2207 }
2208
2209 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002210 if (p1) {
2211 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002212 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002213 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002214 p1 = ptr;
2215 }
2216
2217 ptr++;
2218 if (ptr >= buf->data + buf->size)
2219 ptr = buf->data;
2220 }
2221
2222 /* after LF; point to beginning of next line */
2223 p2++;
2224 if (p2 >= buf->data + buf->size)
2225 p2 = buf->data;
2226
Willy Tarreau638cd022010-01-03 07:42:04 +01002227 bytes = p2 - buf->lr;
2228 if (bytes < 0)
2229 bytes += buf->size;
2230
2231 /* schedule this line for forwarding */
2232 msg->sov += bytes;
2233 if (msg->sov >= buf->size)
2234 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002235
Willy Tarreau638cd022010-01-03 07:42:04 +01002236 if (p1 == buf->lr) {
2237 /* LF/CRLF at beginning of line => end of trailers at p2.
2238 * Everything was scheduled for forwarding, there's nothing
2239 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002240 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002241 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002242 msg->msg_state = HTTP_MSG_DONE;
2243 return 1;
2244 }
2245 /* OK, next line then */
2246 buf->lr = p2;
2247 }
2248}
2249
2250/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2251 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2252 * ->som, buf->lr in order to include this part into the next forwarding phase.
2253 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2254 * not enough data are available, the function does not change anything and
2255 * returns zero. If a parse error is encountered, the function returns < 0 and
2256 * does not change anything. Note: this function is designed to parse wrapped
2257 * CRLF at the end of the buffer.
2258 */
2259int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2260{
2261 char *ptr;
2262 int bytes;
2263
2264 /* NB: we'll check data availabilty at the end. It's not a
2265 * problem because whatever we match first will be checked
2266 * against the correct length.
2267 */
2268 bytes = 1;
2269 ptr = buf->lr;
2270 if (*ptr == '\r') {
2271 bytes++;
2272 ptr++;
2273 if (ptr >= buf->data + buf->size)
2274 ptr = buf->data;
2275 }
2276
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002277 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002278 return 0;
2279
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002280 if (*ptr != '\n') {
2281 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002282 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002283 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002284
2285 ptr++;
2286 if (ptr >= buf->data + buf->size)
2287 ptr = buf->data;
2288 buf->lr = ptr;
2289 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2290 msg->sov = ptr - buf->data;
2291 msg->som = msg->sov - bytes;
2292 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2293 return 1;
2294}
Willy Tarreau5b154472009-12-21 20:11:07 +01002295
Willy Tarreau83e3af02009-12-28 17:39:57 +01002296void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2297{
2298 char *end = buf->data + buf->size;
2299 int off = buf->data + buf->size - buf->w;
2300
2301 /* two possible cases :
2302 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002303 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002304 */
2305 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002306 int block1 = buf->l;
2307 int block2 = 0;
2308 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002309 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002310 block1 = buf->data + buf->size - buf->w;
2311 block2 = buf->r - buf->data;
2312 }
2313 if (block2)
2314 memcpy(swap_buffer, buf->data, block2);
2315 memmove(buf->data, buf->w, block1);
2316 if (block2)
2317 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002318 }
2319
2320 /* adjust all known pointers */
2321 buf->w = buf->data;
2322 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2323 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2324 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2325 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2326
2327 /* adjust relative pointers */
2328 msg->som = 0;
2329 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2330 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2331 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2332
Willy Tarreau83e3af02009-12-28 17:39:57 +01002333 if (msg->err_pos >= 0) {
2334 msg->err_pos += off;
2335 if (msg->err_pos >= buf->size)
2336 msg->err_pos -= buf->size;
2337 }
2338
2339 buf->flags &= ~BF_FULL;
2340 if (buf->l >= buffer_max_len(buf))
2341 buf->flags |= BF_FULL;
2342}
2343
Willy Tarreaud787e662009-07-07 10:14:51 +02002344/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2345 * processing can continue on next analysers, or zero if it either needs more
2346 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2347 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2348 * when it has nothing left to do, and may remove any analyser when it wants to
2349 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002350 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002351int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002352{
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 /*
2354 * We will parse the partial (or complete) lines.
2355 * We will check the request syntax, and also join multi-line
2356 * headers. An index of all the lines will be elaborated while
2357 * parsing.
2358 *
2359 * For the parsing, we use a 28 states FSM.
2360 *
2361 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002362 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002363 * req->data + msg->eoh = end of processed headers / start of current one
2364 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002365 * req->lr = first non-visited byte
2366 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002367 *
2368 * At end of parsing, we may perform a capture of the error (if any), and
2369 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2370 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2371 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002372 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002373
Willy Tarreau59234e92008-11-30 23:51:27 +01002374 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002375 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002376 struct http_txn *txn = &s->txn;
2377 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002378 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002379
Willy Tarreau6bf17362009-02-24 10:48:35 +01002380 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2381 now_ms, __FUNCTION__,
2382 s,
2383 req,
2384 req->rex, req->wex,
2385 req->flags,
2386 req->l,
2387 req->analysers);
2388
Willy Tarreau52a0c602009-08-16 22:45:38 +02002389 /* we're speaking HTTP here, so let's speak HTTP to the client */
2390 s->srv_error = http_return_srv_error;
2391
Willy Tarreau83e3af02009-12-28 17:39:57 +01002392 /* There's a protected area at the end of the buffer for rewriting
2393 * purposes. We don't want to start to parse the request if the
2394 * protected area is affected, because we may have to move processed
2395 * data later, which is much more complicated.
2396 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002397 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002398 if ((txn->flags & TX_NOT_FIRST) &&
2399 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002400 req->r < req->lr ||
2401 req->r > req->data + req->size - global.tune.maxrewrite)) {
2402 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002403 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2404 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002405 /* some data has still not left the buffer, wake us once that's done */
2406 buffer_dont_connect(req);
2407 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2408 return 0;
2409 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002410 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002411 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002412 }
2413
Willy Tarreau065e8332010-01-08 00:30:20 +01002414 /* Note that we have the same problem with the response ; we
2415 * may want to send a redirect, error or anything which requires
2416 * some spare space. So we'll ensure that we have at least
2417 * maxrewrite bytes available in the response buffer before
2418 * processing that one. This will only affect pipelined
2419 * keep-alive requests.
2420 */
2421 if ((txn->flags & TX_NOT_FIRST) &&
2422 unlikely((s->rep->flags & BF_FULL) ||
2423 s->rep->r < s->rep->lr ||
2424 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2425 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002426 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2427 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002428 /* don't let a connection request be initiated */
2429 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002430 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002431 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002432 return 0;
2433 }
2434 }
2435
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002436 if (likely(req->lr < req->r))
2437 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002438 }
2439
Willy Tarreau59234e92008-11-30 23:51:27 +01002440 /* 1: we might have to print this header in debug mode */
2441 if (unlikely((global.mode & MODE_DEBUG) &&
2442 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002443 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002444 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002445 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002446
Willy Tarreau663308b2010-06-07 14:06:08 +02002447 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002448 eol = sol + msg->sl.rq.l;
2449 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002450
Willy Tarreau59234e92008-11-30 23:51:27 +01002451 sol += hdr_idx_first_pos(&txn->hdr_idx);
2452 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002453
Willy Tarreau59234e92008-11-30 23:51:27 +01002454 while (cur_idx) {
2455 eol = sol + txn->hdr_idx.v[cur_idx].len;
2456 debug_hdr("clihdr", s, sol, eol);
2457 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2458 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002459 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002460 }
2461
Willy Tarreau58f10d72006-12-04 02:26:12 +01002462
Willy Tarreau59234e92008-11-30 23:51:27 +01002463 /*
2464 * Now we quickly check if we have found a full valid request.
2465 * If not so, we check the FD and buffer states before leaving.
2466 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002467 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002468 * requests are checked first. When waiting for a second request
2469 * on a keep-alive session, if we encounter and error, close, t/o,
2470 * we note the error in the session flags but don't set any state.
2471 * Since the error will be noted there, it will not be counted by
2472 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002473 * Last, we may increase some tracked counters' http request errors on
2474 * the cases that are deliberately the client's fault. For instance,
2475 * a timeout or connection reset is not counted as an error. However
2476 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002477 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002478
Willy Tarreau655dce92009-11-08 13:10:58 +01002479 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002480 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002481 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002482 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002483 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002484 session_inc_http_req_ctr(s);
2485 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002486 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002487 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002488 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002489
Willy Tarreau59234e92008-11-30 23:51:27 +01002490 /* 1: Since we are in header mode, if there's no space
2491 * left for headers, we won't be able to free more
2492 * later, so the session will never terminate. We
2493 * must terminate it now.
2494 */
2495 if (unlikely(req->flags & BF_FULL)) {
2496 /* FIXME: check if URI is set and return Status
2497 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002498 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002499 session_inc_http_req_ctr(s);
2500 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002501 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002502 goto return_bad_req;
2503 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002504
Willy Tarreau59234e92008-11-30 23:51:27 +01002505 /* 2: have we encountered a read error ? */
2506 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002507 if (!(s->flags & SN_ERR_MASK))
2508 s->flags |= SN_ERR_CLICL;
2509
Willy Tarreaufcffa692010-01-10 14:21:19 +01002510 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002511 goto failed_keep_alive;
2512
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002514 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002515 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002516 session_inc_http_err_ctr(s);
2517 }
2518
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 msg->msg_state = HTTP_MSG_ERROR;
2520 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002521
Willy Tarreauda7ff642010-06-23 11:44:09 +02002522 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002523 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002524 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002525 if (s->listener->counters)
2526 s->listener->counters->failed_req++;
2527
Willy Tarreau59234e92008-11-30 23:51:27 +01002528 if (!(s->flags & SN_FINST_MASK))
2529 s->flags |= SN_FINST_R;
2530 return 0;
2531 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002532
Willy Tarreau59234e92008-11-30 23:51:27 +01002533 /* 3: has the read timeout expired ? */
2534 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002535 if (!(s->flags & SN_ERR_MASK))
2536 s->flags |= SN_ERR_CLITO;
2537
Willy Tarreaufcffa692010-01-10 14:21:19 +01002538 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002539 goto failed_keep_alive;
2540
Willy Tarreau59234e92008-11-30 23:51:27 +01002541 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002542 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002543 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002544 session_inc_http_err_ctr(s);
2545 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 txn->status = 408;
2547 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2548 msg->msg_state = HTTP_MSG_ERROR;
2549 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002550
Willy Tarreauda7ff642010-06-23 11:44:09 +02002551 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002552 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002553 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002554 if (s->listener->counters)
2555 s->listener->counters->failed_req++;
2556
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 if (!(s->flags & SN_FINST_MASK))
2558 s->flags |= SN_FINST_R;
2559 return 0;
2560 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002561
Willy Tarreau59234e92008-11-30 23:51:27 +01002562 /* 4: have we encountered a close ? */
2563 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002564 if (!(s->flags & SN_ERR_MASK))
2565 s->flags |= SN_ERR_CLICL;
2566
Willy Tarreaufcffa692010-01-10 14:21:19 +01002567 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002568 goto failed_keep_alive;
2569
Willy Tarreau4076a152009-04-02 15:18:36 +02002570 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002571 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 txn->status = 400;
2573 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2574 msg->msg_state = HTTP_MSG_ERROR;
2575 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002576
Willy Tarreauda7ff642010-06-23 11:44:09 +02002577 session_inc_http_err_ctr(s);
2578 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002579 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002580 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002581 if (s->listener->counters)
2582 s->listener->counters->failed_req++;
2583
Willy Tarreau59234e92008-11-30 23:51:27 +01002584 if (!(s->flags & SN_FINST_MASK))
2585 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002586 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002587 }
2588
Willy Tarreau520d95e2009-09-19 21:04:57 +02002589 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002590 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002591 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002592
Willy Tarreaufcffa692010-01-10 14:21:19 +01002593 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2594 /* If the client starts to talk, let's fall back to
2595 * request timeout processing.
2596 */
2597 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002598 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002599 }
2600
Willy Tarreau59234e92008-11-30 23:51:27 +01002601 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002602 if (!tick_isset(req->analyse_exp)) {
2603 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2604 (txn->flags & TX_WAIT_NEXT_RQ) &&
2605 tick_isset(s->be->timeout.httpka))
2606 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2607 else
2608 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2609 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002610
Willy Tarreau59234e92008-11-30 23:51:27 +01002611 /* we're not ready yet */
2612 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002613
2614 failed_keep_alive:
2615 /* Here we process low-level errors for keep-alive requests. In
2616 * short, if the request is not the first one and it experiences
2617 * a timeout, read error or shutdown, we just silently close so
2618 * that the client can try again.
2619 */
2620 txn->status = 0;
2621 msg->msg_state = HTTP_MSG_RQBEFORE;
2622 req->analysers = 0;
2623 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002624 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002625 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002626 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002627 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002628
Willy Tarreaud787e662009-07-07 10:14:51 +02002629 /* OK now we have a complete HTTP request with indexed headers. Let's
2630 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002631 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2632 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2633 * points to the CRLF of the request line. req->lr points to the first
2634 * byte after the last LF. msg->col and msg->sov point to the first
2635 * byte of data. msg->eol cannot be trusted because it may have been
2636 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002637 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002638
Willy Tarreauda7ff642010-06-23 11:44:09 +02002639 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002640 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2641
Willy Tarreaub16a5742010-01-10 14:46:16 +01002642 if (txn->flags & TX_WAIT_NEXT_RQ) {
2643 /* kill the pending keep-alive timeout */
2644 txn->flags &= ~TX_WAIT_NEXT_RQ;
2645 req->analyse_exp = TICK_ETERNITY;
2646 }
2647
2648
Willy Tarreaud787e662009-07-07 10:14:51 +02002649 /* Maybe we found in invalid header name while we were configured not
2650 * to block on that, so we have to capture it now.
2651 */
2652 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002653 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002654
Willy Tarreau59234e92008-11-30 23:51:27 +01002655 /*
2656 * 1: identify the method
2657 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002658 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002659
2660 /* we can make use of server redirect on GET and HEAD */
2661 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2662 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002663
Willy Tarreau59234e92008-11-30 23:51:27 +01002664 /*
2665 * 2: check if the URI matches the monitor_uri.
2666 * We have to do this for every request which gets in, because
2667 * the monitor-uri is defined by the frontend.
2668 */
2669 if (unlikely((s->fe->monitor_uri_len != 0) &&
2670 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002671 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 s->fe->monitor_uri,
2673 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002674 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002675 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002676 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002677 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002678
Willy Tarreau59234e92008-11-30 23:51:27 +01002679 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002680
Willy Tarreau59234e92008-11-30 23:51:27 +01002681 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002682 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2683 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002684
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 ret = acl_pass(ret);
2686 if (cond->pol == ACL_COND_UNLESS)
2687 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002688
Willy Tarreau59234e92008-11-30 23:51:27 +01002689 if (ret) {
2690 /* we fail this request, let's return 503 service unavail */
2691 txn->status = 503;
2692 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2693 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002694 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002695 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002696
Willy Tarreau59234e92008-11-30 23:51:27 +01002697 /* nothing to fail, let's reply normaly */
2698 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002699 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002700 goto return_prx_cond;
2701 }
2702
2703 /*
2704 * 3: Maybe we have to copy the original REQURI for the logs ?
2705 * Note: we cannot log anymore if the request has been
2706 * classified as invalid.
2707 */
2708 if (unlikely(s->logs.logwait & LW_REQ)) {
2709 /* we have a complete HTTP request that we must log */
2710 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2711 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002712
Willy Tarreau59234e92008-11-30 23:51:27 +01002713 if (urilen >= REQURI_LEN)
2714 urilen = REQURI_LEN - 1;
2715 memcpy(txn->uri, &req->data[msg->som], urilen);
2716 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002717
Willy Tarreau59234e92008-11-30 23:51:27 +01002718 if (!(s->logs.logwait &= ~LW_REQ))
2719 s->do_log(s);
2720 } else {
2721 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002722 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 }
Willy Tarreau06619262006-12-17 08:37:22 +01002724
Willy Tarreau59234e92008-11-30 23:51:27 +01002725 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002726 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2727 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002728
Willy Tarreau5b154472009-12-21 20:11:07 +01002729 /* ... and check if the request is HTTP/1.1 or above */
2730 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002731 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2732 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2733 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002734 txn->flags |= TX_REQ_VER_11;
2735
2736 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002737 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002738
Willy Tarreau88d349d2010-01-25 12:15:43 +01002739 /* if the frontend has "option http-use-proxy-header", we'll check if
2740 * we have what looks like a proxied connection instead of a connection,
2741 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2742 * Note that this is *not* RFC-compliant, however browsers and proxies
2743 * happen to do that despite being non-standard :-(
2744 * We consider that a request not beginning with either '/' or '*' is
2745 * a proxied connection, which covers both "scheme://location" and
2746 * CONNECT ip:port.
2747 */
2748 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2749 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2750 txn->flags |= TX_USE_PX_CONN;
2751
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002752 /* transfer length unknown*/
2753 txn->flags &= ~TX_REQ_XFER_LEN;
2754
Willy Tarreau59234e92008-11-30 23:51:27 +01002755 /* 5: we may need to capture headers */
2756 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002757 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002758 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002759
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002760 /* 6: determine the transfer-length.
2761 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2762 * the presence of a message-body in a REQUEST and its transfer length
2763 * must be determined that way (in order of precedence) :
2764 * 1. The presence of a message-body in a request is signaled by the
2765 * inclusion of a Content-Length or Transfer-Encoding header field
2766 * in the request's header fields. When a request message contains
2767 * both a message-body of non-zero length and a method that does
2768 * not define any semantics for that request message-body, then an
2769 * origin server SHOULD either ignore the message-body or respond
2770 * with an appropriate error message (e.g., 413). A proxy or
2771 * gateway, when presented the same request, SHOULD either forward
2772 * the request inbound with the message- body or ignore the
2773 * message-body when determining a response.
2774 *
2775 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2776 * and the "chunked" transfer-coding (Section 6.2) is used, the
2777 * transfer-length is defined by the use of this transfer-coding.
2778 * If a Transfer-Encoding header field is present and the "chunked"
2779 * transfer-coding is not present, the transfer-length is defined
2780 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002781 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002782 * 3. If a Content-Length header field is present, its decimal value in
2783 * OCTETs represents both the entity-length and the transfer-length.
2784 * If a message is received with both a Transfer-Encoding header
2785 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002786 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002787 * 4. By the server closing the connection. (Closing the connection
2788 * cannot be used to indicate the end of a request body, since that
2789 * would leave no possibility for the server to send back a response.)
2790 *
2791 * Whenever a transfer-coding is applied to a message-body, the set of
2792 * transfer-codings MUST include "chunked", unless the message indicates
2793 * it is terminated by closing the connection. When the "chunked"
2794 * transfer-coding is used, it MUST be the last transfer-coding applied
2795 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002796 */
2797
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002798 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002799 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002800 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002801 while ((txn->flags & TX_REQ_VER_11) &&
2802 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002803 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2804 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2805 else if (txn->flags & TX_REQ_TE_CHNK) {
2806 /* bad transfer-encoding (chunked followed by something else) */
2807 use_close_only = 1;
2808 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2809 break;
2810 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002811 }
2812
Willy Tarreau32b47f42009-10-18 20:55:02 +02002813 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002814 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002815 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2816 signed long long cl;
2817
2818 if (!ctx.vlen)
2819 goto return_bad_req;
2820
2821 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2822 goto return_bad_req; /* parse failure */
2823
2824 if (cl < 0)
2825 goto return_bad_req;
2826
Willy Tarreau124d9912011-03-01 20:30:48 +01002827 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl))
Willy Tarreau32b47f42009-10-18 20:55:02 +02002828 goto return_bad_req; /* already specified, was different */
2829
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002830 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002831 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002832 }
2833
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002834 /* bodyless requests have a known length */
2835 if (!use_close_only)
2836 txn->flags |= TX_REQ_XFER_LEN;
2837
Willy Tarreaud787e662009-07-07 10:14:51 +02002838 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002839 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002840 req->analyse_exp = TICK_ETERNITY;
2841 return 1;
2842
2843 return_bad_req:
2844 /* We centralize bad requests processing here */
2845 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2846 /* we detected a parsing error. We want to archive this request
2847 * in the dedicated proxy area for later troubleshooting.
2848 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002849 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002850 }
2851
2852 txn->req.msg_state = HTTP_MSG_ERROR;
2853 txn->status = 400;
2854 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002855
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002856 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002857 if (s->listener->counters)
2858 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002859
2860 return_prx_cond:
2861 if (!(s->flags & SN_ERR_MASK))
2862 s->flags |= SN_ERR_PRXCOND;
2863 if (!(s->flags & SN_FINST_MASK))
2864 s->flags |= SN_FINST_R;
2865
2866 req->analysers = 0;
2867 req->analyse_exp = TICK_ETERNITY;
2868 return 0;
2869}
2870
Cyril Bonté70be45d2010-10-12 00:14:35 +02002871/* We reached the stats page through a POST request.
2872 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002873 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002874 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002875int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002876{
Cyril Bonté70be45d2010-10-12 00:14:35 +02002877 struct proxy *px;
2878 struct server *sv;
2879
2880 char *backend = NULL;
2881 int action = 0;
2882
2883 char *first_param, *cur_param, *next_param, *end_params;
2884
2885 first_param = req->data + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002886 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002887
2888 cur_param = next_param = end_params;
2889
Cyril Bonté23b39d92011-02-10 22:54:44 +01002890 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002891 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002892 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002893 return 1;
2894 }
2895 else if (end_params > req->data + req->l) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002896 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002897 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002898 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002899 }
2900
2901 *end_params = '\0';
2902
Willy Tarreau295a8372011-03-10 11:25:07 +01002903 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002904
2905 /*
2906 * Parse the parameters in reverse order to only store the last value.
2907 * From the html form, the backend and the action are at the end.
2908 */
2909 while (cur_param > first_param) {
2910 char *key, *value;
2911
2912 cur_param--;
2913 if ((*cur_param == '&') || (cur_param == first_param)) {
2914 /* Parse the key */
2915 key = cur_param;
2916 if (cur_param != first_param) {
2917 /* delimit the string for the next loop */
2918 *key++ = '\0';
2919 }
2920
2921 /* Parse the value */
2922 value = key;
2923 while (*value != '\0' && *value != '=') {
2924 value++;
2925 }
2926 if (*value == '=') {
2927 /* Ok, a value is found, we can mark the end of the key */
2928 *value++ = '\0';
2929 }
2930
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002931 if (!url_decode(key) || !url_decode(value))
2932 break;
2933
Cyril Bonté70be45d2010-10-12 00:14:35 +02002934 /* Now we can check the key to see what to do */
2935 if (!backend && strcmp(key, "b") == 0) {
2936 backend = value;
2937 }
2938 else if (!action && strcmp(key, "action") == 0) {
2939 if (strcmp(value, "disable") == 0) {
2940 action = 1;
2941 }
2942 else if (strcmp(value, "enable") == 0) {
2943 action = 2;
2944 } else {
2945 /* unknown action, no need to continue */
2946 break;
2947 }
2948 }
2949 else if (strcmp(key, "s") == 0) {
2950 if (backend && action && get_backend_server(backend, value, &px, &sv)) {
2951 switch (action) {
2952 case 1:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002953 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002954 /* Not already in maintenance, we can change the server state */
2955 sv->state |= SRV_MAINTAIN;
2956 set_server_down(sv);
Willy Tarreau295a8372011-03-10 11:25:07 +01002957 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002958 }
2959 break;
2960 case 2:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002961 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002962 /* Already in maintenance, we can change the server state */
2963 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002964 sv->health = sv->rise; /* up, but will fall down at first failure */
Willy Tarreau295a8372011-03-10 11:25:07 +01002965 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002966 }
2967 break;
2968 }
2969 }
2970 }
2971 next_param = cur_param;
2972 }
2973 }
Cyril Bonté23b39d92011-02-10 22:54:44 +01002974 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002975}
2976
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002977/* returns a pointer to the first rule which forbids access (deny or http_auth),
2978 * or NULL if everything's OK.
2979 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002980static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002981http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2982{
Willy Tarreauff011f22011-01-06 17:51:27 +01002983 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002984
Willy Tarreauff011f22011-01-06 17:51:27 +01002985 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002986 int ret = 1;
2987
Willy Tarreauff011f22011-01-06 17:51:27 +01002988 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002989 continue;
2990
2991 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002992 if (rule->cond) {
2993 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002994 ret = acl_pass(ret);
2995
Willy Tarreauff011f22011-01-06 17:51:27 +01002996 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002997 ret = !ret;
2998 }
2999
3000 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003001 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003002 return NULL; /* no problem */
3003 else
Willy Tarreauff011f22011-01-06 17:51:27 +01003004 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003005 }
3006 }
3007 return NULL;
3008}
3009
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003010/* This stream analyser runs all HTTP request processing which is common to
3011 * frontends and backends, which means blocking ACLs, filters, connection-close,
3012 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003013 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003014 * either needs more data or wants to immediately abort the request (eg: deny,
3015 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003016 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003017int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003018{
Willy Tarreaud787e662009-07-07 10:14:51 +02003019 struct http_txn *txn = &s->txn;
3020 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003021 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003022 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003023 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003024 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003025 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003026
Willy Tarreau655dce92009-11-08 13:10:58 +01003027 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003028 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003029 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003030 return 0;
3031 }
3032
Willy Tarreau3a816292009-07-07 10:55:49 +02003033 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003034 req->analyse_exp = TICK_ETERNITY;
3035
3036 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3037 now_ms, __FUNCTION__,
3038 s,
3039 req,
3040 req->rex, req->wex,
3041 req->flags,
3042 req->l,
3043 req->analysers);
3044
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003045 /* first check whether we have some ACLs set to block this request */
3046 list_for_each_entry(cond, &px->block_cond, list) {
3047 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003048
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003049 ret = acl_pass(ret);
3050 if (cond->pol == ACL_COND_UNLESS)
3051 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003052
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003053 if (ret) {
3054 txn->status = 403;
3055 /* let's log the request time */
3056 s->logs.tv_request = now;
3057 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003058 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003059 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003060 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003061 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003062
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003063 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01003064 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003065
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003066 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003067 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003068 do_stats = stats_check_uri(s->rep->prod, txn, px);
3069 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01003070 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 +01003071 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003072 else
3073 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003074
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003075 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01003076 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003077 txn->status = 403;
3078 s->logs.tv_request = now;
3079 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003080 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003081 s->fe->fe_counters.denied_req++;
3082 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3083 s->be->be_counters.denied_req++;
3084 if (s->listener->counters)
3085 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003086 goto return_prx_cond;
3087 }
3088
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003089 /* try headers filters */
3090 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003091 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003092 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003093
Willy Tarreau59234e92008-11-30 23:51:27 +01003094 /* has the request been denied ? */
3095 if (txn->flags & TX_CLDENY) {
3096 /* no need to go further */
3097 txn->status = 403;
3098 /* let's log the request time */
3099 s->logs.tv_request = now;
3100 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003101 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003102 goto return_prx_cond;
3103 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003104
3105 /* When a connection is tarpitted, we use the tarpit timeout,
3106 * which may be the same as the connect timeout if unspecified.
3107 * If unset, then set it to zero because we really want it to
3108 * eventually expire. We build the tarpit as an analyser.
3109 */
3110 if (txn->flags & TX_CLTARPIT) {
3111 buffer_erase(s->req);
3112 /* wipe the request out so that we can drop the connection early
3113 * if the client closes first.
3114 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003115 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003116 req->analysers = 0; /* remove switching rules etc... */
3117 req->analysers |= AN_REQ_HTTP_TARPIT;
3118 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3119 if (!req->analyse_exp)
3120 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003121 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003122 return 1;
3123 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003124 }
Willy Tarreau06619262006-12-17 08:37:22 +01003125
Willy Tarreau5b154472009-12-21 20:11:07 +01003126 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3127 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003128 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3129 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003130 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3131 * avoid to redo the same work if FE and BE have the same settings (common).
3132 * The method consists in checking if options changed between the two calls
3133 * (implying that either one is non-null, or one of them is non-null and we
3134 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003135 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003136
Willy Tarreaudc008c52010-02-01 16:20:08 +01003137 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3138 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3139 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3140 (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 +01003141 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003142
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003143 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3144 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003145 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003146 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3147 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003148 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003149 tmp = TX_CON_WANT_CLO;
3150
Willy Tarreau5b154472009-12-21 20:11:07 +01003151 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3152 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003153
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003154 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3155 /* parse the Connection header and possibly clean it */
3156 int to_del = 0;
3157 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003158 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3159 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003160 to_del |= 2; /* remove "keep-alive" */
3161 if (!(txn->flags & TX_REQ_VER_11))
3162 to_del |= 1; /* remove "close" */
3163 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003164 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003165
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003166 /* check if client or config asks for explicit close in KAL/SCL */
3167 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3168 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3169 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
3170 (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 +01003171 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003172 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
3173 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003174 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3175 }
Willy Tarreau78599912009-10-17 20:12:21 +02003176
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003177 /* we can be blocked here because the request needs to be authenticated,
3178 * either to pass or to access stats.
3179 */
Willy Tarreauff011f22011-01-06 17:51:27 +01003180 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003181 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01003182 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003183
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003184 if (!realm)
3185 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3186
Willy Tarreau844a7e72010-01-31 21:46:18 +01003187 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003188 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3189 txn->status = 401;
3190 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003191 /* on 401 we still count one error, because normal browsing
3192 * won't significantly increase the counter but brute force
3193 * attempts will.
3194 */
3195 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003196 goto return_prx_cond;
3197 }
3198
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003199 /* add request headers from the rule sets in the same order */
3200 list_for_each_entry(wl, &px->req_add, list) {
3201 if (wl->cond) {
3202 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
3203 ret = acl_pass(ret);
3204 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3205 ret = !ret;
3206 if (!ret)
3207 continue;
3208 }
3209
3210 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
3211 goto return_bad_req;
3212 }
3213
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003214 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003215 struct stats_admin_rule *stats_admin_rule;
3216
3217 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003218 * FIXME!!! that one is rather dangerous, we want to
3219 * make it follow standard rules (eg: clear req->analysers).
3220 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003221
Cyril Bonté474be412010-10-12 00:14:36 +02003222 /* now check whether we have some admin rules for this request */
3223 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3224 int ret = 1;
3225
3226 if (stats_admin_rule->cond) {
3227 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
3228 ret = acl_pass(ret);
3229 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3230 ret = !ret;
3231 }
3232
3233 if (ret) {
3234 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01003235 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02003236 break;
3237 }
3238 }
3239
Cyril Bonté70be45d2010-10-12 00:14:35 +02003240 /* Was the status page requested with a POST ? */
3241 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003242 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003243 if (msg->msg_state < HTTP_MSG_100_SENT) {
3244 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3245 * send an HTTP/1.1 100 Continue intermediate response.
3246 */
3247 if (txn->flags & TX_REQ_VER_11) {
3248 struct hdr_ctx ctx;
3249 ctx.idx = 0;
3250 /* Expect is allowed in 1.1, look for it */
3251 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3252 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3253 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3254 }
3255 }
3256 msg->msg_state = HTTP_MSG_100_SENT;
3257 s->logs.tv_request = now; /* update the request timer to reflect full request */
3258 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003259 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003260 /* we need more data */
3261 req->analysers |= an_bit;
3262 buffer_dont_connect(req);
3263 return 0;
3264 }
Cyril Bonté474be412010-10-12 00:14:36 +02003265 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003266 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003267 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003268 }
3269
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003270 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003271 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003272 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003273 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003274 s->rep->prod->applet.private = s;
3275 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003276 req->analysers = 0;
3277
3278 return 0;
3279
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003280 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003281
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003282 /* check whether we have some ACLs set to redirect this request */
3283 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003284 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003285
Willy Tarreauf285f542010-01-03 20:03:03 +01003286 if (rule->cond) {
3287 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3288 ret = acl_pass(ret);
3289 if (rule->cond->pol == ACL_COND_UNLESS)
3290 ret = !ret;
3291 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003292
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003293 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003294 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003295 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003296
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003297 /* build redirect message */
3298 switch(rule->code) {
3299 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003300 msg_fmt = HTTP_303;
3301 break;
3302 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003303 msg_fmt = HTTP_301;
3304 break;
3305 case 302:
3306 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003307 msg_fmt = HTTP_302;
3308 break;
3309 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003310
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003311 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003312 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003313
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003314 switch(rule->type) {
3315 case REDIRECT_TYPE_PREFIX: {
3316 const char *path;
3317 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003318
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003319 path = http_get_path(txn);
3320 /* build message using path */
3321 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003322 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003323 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3324 int qs = 0;
3325 while (qs < pathlen) {
3326 if (path[qs] == '?') {
3327 pathlen = qs;
3328 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003329 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003330 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003331 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003332 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003333 } else {
3334 path = "/";
3335 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003336 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003337
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003338 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003339 goto return_bad_req;
3340
3341 /* add prefix. Note that if prefix == "/", we don't want to
3342 * add anything, otherwise it makes it hard for the user to
3343 * configure a self-redirection.
3344 */
3345 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003346 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3347 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003348 }
3349
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003350 /* add path */
3351 memcpy(rdr.str + rdr.len, path, pathlen);
3352 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003353
3354 /* append a slash at the end of the location is needed and missing */
3355 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3356 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3357 if (rdr.len > rdr.size - 5)
3358 goto return_bad_req;
3359 rdr.str[rdr.len] = '/';
3360 rdr.len++;
3361 }
3362
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003363 break;
3364 }
3365 case REDIRECT_TYPE_LOCATION:
3366 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003367 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003368 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003369
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003370 /* add location */
3371 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3372 rdr.len += rule->rdr_len;
3373 break;
3374 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003375
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003376 if (rule->cookie_len) {
3377 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3378 rdr.len += 14;
3379 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3380 rdr.len += rule->cookie_len;
3381 memcpy(rdr.str + rdr.len, "\r\n", 2);
3382 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003383 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003384
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003385 /* add end of headers and the keep-alive/close status.
3386 * We may choose to set keep-alive if the Location begins
3387 * with a slash, because the client will come back to the
3388 * same server.
3389 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003390 txn->status = rule->code;
3391 /* let's log the request time */
3392 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003393
3394 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3395 (txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau124d9912011-03-01 20:30:48 +01003396 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003397 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3398 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3399 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003400 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003401 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3402 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3403 rdr.len += 30;
3404 } else {
3405 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3406 rdr.len += 24;
3407 }
Willy Tarreau75661452010-01-10 10:35:01 +01003408 }
3409 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3410 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003411 buffer_write(req->prod->ob, rdr.str, rdr.len);
3412 /* "eat" the request */
3413 buffer_ignore(req, msg->sov - msg->som);
3414 msg->som = msg->sov;
3415 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003416 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3417 txn->req.msg_state = HTTP_MSG_CLOSED;
3418 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003419 break;
3420 } else {
3421 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003422 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3423 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3424 rdr.len += 29;
3425 } else {
3426 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3427 rdr.len += 23;
3428 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003429 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003430 goto return_prx_cond;
3431 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003432 }
3433 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003434
Willy Tarreau2be39392010-01-03 17:24:51 +01003435 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3436 * If this happens, then the data will not come immediately, so we must
3437 * send all what we have without waiting. Note that due to the small gain
3438 * in waiting for the body of the request, it's easier to simply put the
3439 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3440 * itself once used.
3441 */
3442 req->flags |= BF_SEND_DONTWAIT;
3443
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003444 /* that's OK for us now, let's move on to next analysers */
3445 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003446
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003447 return_bad_req:
3448 /* We centralize bad requests processing here */
3449 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3450 /* we detected a parsing error. We want to archive this request
3451 * in the dedicated proxy area for later troubleshooting.
3452 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003453 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003454 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003455
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003456 txn->req.msg_state = HTTP_MSG_ERROR;
3457 txn->status = 400;
3458 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003459
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003460 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003461 if (s->listener->counters)
3462 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003463
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003464 return_prx_cond:
3465 if (!(s->flags & SN_ERR_MASK))
3466 s->flags |= SN_ERR_PRXCOND;
3467 if (!(s->flags & SN_FINST_MASK))
3468 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003469
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003470 req->analysers = 0;
3471 req->analyse_exp = TICK_ETERNITY;
3472 return 0;
3473}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003474
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003475/* This function performs all the processing enabled for the current request.
3476 * It returns 1 if the processing can continue on next analysers, or zero if it
3477 * needs more data, encounters an error, or wants to immediately abort the
3478 * request. It relies on buffers flags, and updates s->req->analysers.
3479 */
3480int http_process_request(struct session *s, struct buffer *req, int an_bit)
3481{
3482 struct http_txn *txn = &s->txn;
3483 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003484
Willy Tarreau655dce92009-11-08 13:10:58 +01003485 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003486 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003487 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003488 return 0;
3489 }
3490
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003491 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3492 now_ms, __FUNCTION__,
3493 s,
3494 req,
3495 req->rex, req->wex,
3496 req->flags,
3497 req->l,
3498 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003499
Willy Tarreau59234e92008-11-30 23:51:27 +01003500 /*
3501 * Right now, we know that we have processed the entire headers
3502 * and that unwanted requests have been filtered out. We can do
3503 * whatever we want with the remaining request. Also, now we
3504 * may have separate values for ->fe, ->be.
3505 */
Willy Tarreau06619262006-12-17 08:37:22 +01003506
Willy Tarreau59234e92008-11-30 23:51:27 +01003507 /*
3508 * If HTTP PROXY is set we simply get remote server address
3509 * parsing incoming request.
3510 */
3511 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau957c0a52011-03-03 17:42:23 +01003512 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 +01003513 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003514
Willy Tarreau59234e92008-11-30 23:51:27 +01003515 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003516 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003517 * Note that doing so might move headers in the request, but
3518 * the fields will stay coherent and the URI will not move.
3519 * This should only be performed in the backend.
3520 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003521 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003522 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3523 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003524
Willy Tarreau59234e92008-11-30 23:51:27 +01003525 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003526 * 8: the appsession cookie was looked up very early in 1.2,
3527 * so let's do the same now.
3528 */
3529
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003530 /* It needs to look into the URI unless persistence must be ignored */
3531 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003532 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003533 }
3534
3535 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003536 * 9: add X-Forwarded-For if either the frontend or the backend
3537 * asks for it.
3538 */
3539 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003540 struct hdr_ctx ctx = { .idx = 0 };
3541
3542 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3543 http_find_header2("X-Forwarded-For", 15, txn->req.sol, &txn->hdr_idx, &ctx)) {
3544 /* The header is set to be added only if none is present
3545 * and we found it, so don't do anything.
3546 */
3547 }
3548 else if (s->req->prod->addr.c.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003549 /* Add an X-Forwarded-For header unless the source IP is
3550 * in the 'except' network range.
3551 */
3552 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003553 (((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 +01003554 != s->fe->except_net.s_addr) &&
3555 (!s->be->except_mask.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003556 (((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 +01003557 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003558 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003559 unsigned char *pn;
Willy Tarreau957c0a52011-03-03 17:42:23 +01003560 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003561
3562 /* Note: we rely on the backend to get the header name to be used for
3563 * x-forwarded-for, because the header is really meant for the backends.
3564 * However, if the backend did not specify any option, we have to rely
3565 * on the frontend's header name.
3566 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003567 if (s->be->fwdfor_hdr_len) {
3568 len = s->be->fwdfor_hdr_len;
3569 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003570 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003571 len = s->fe->fwdfor_hdr_len;
3572 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003573 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003574 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003575
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003576 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003577 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003578 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003579 }
3580 }
Willy Tarreau957c0a52011-03-03 17:42:23 +01003581 else if (s->req->prod->addr.c.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003582 /* FIXME: for the sake of completeness, we should also support
3583 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003584 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003585 int len;
3586 char pn[INET6_ADDRSTRLEN];
3587 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003588 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003589 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003590
Willy Tarreau59234e92008-11-30 23:51:27 +01003591 /* Note: we rely on the backend to get the header name to be used for
3592 * x-forwarded-for, because the header is really meant for the backends.
3593 * However, if the backend did not specify any option, we have to rely
3594 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003595 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003596 if (s->be->fwdfor_hdr_len) {
3597 len = s->be->fwdfor_hdr_len;
3598 memcpy(trash, s->be->fwdfor_hdr_name, len);
3599 } else {
3600 len = s->fe->fwdfor_hdr_len;
3601 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003602 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003603 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003604
Willy Tarreau59234e92008-11-30 23:51:27 +01003605 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003606 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003607 goto return_bad_req;
3608 }
3609 }
3610
3611 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003612 * 10: add X-Original-To if either the frontend or the backend
3613 * asks for it.
3614 */
3615 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3616
3617 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau957c0a52011-03-03 17:42:23 +01003618 if (s->req->prod->addr.c.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003619 /* Add an X-Original-To header unless the destination IP is
3620 * in the 'except' network range.
3621 */
3622 if (!(s->flags & SN_FRT_ADDR_SET))
3623 get_frt_addr(s);
3624
Willy Tarreau957c0a52011-03-03 17:42:23 +01003625 if (s->req->prod->addr.c.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003626 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003627 (((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 +02003628 != s->fe->except_to.s_addr) &&
3629 (!s->be->except_mask_to.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003630 (((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 +02003631 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003632 int len;
3633 unsigned char *pn;
Willy Tarreau957c0a52011-03-03 17:42:23 +01003634 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003635
3636 /* Note: we rely on the backend to get the header name to be used for
3637 * x-original-to, because the header is really meant for the backends.
3638 * However, if the backend did not specify any option, we have to rely
3639 * on the frontend's header name.
3640 */
3641 if (s->be->orgto_hdr_len) {
3642 len = s->be->orgto_hdr_len;
3643 memcpy(trash, s->be->orgto_hdr_name, len);
3644 } else {
3645 len = s->fe->orgto_hdr_len;
3646 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003647 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003648 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3649
3650 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003651 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003652 goto return_bad_req;
3653 }
3654 }
3655 }
3656
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003657 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3658 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003659 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003660 unsigned int want_flags = 0;
3661
3662 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003663 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3664 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3665 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003666 want_flags |= TX_CON_CLO_SET;
3667 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003668 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3669 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3670 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003671 want_flags |= TX_CON_KAL_SET;
3672 }
3673
3674 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3675 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003676 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003677
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003678
Willy Tarreau522d6c02009-12-06 18:49:18 +01003679 /* If we have no server assigned yet and we're balancing on url_param
3680 * with a POST request, we may be interested in checking the body for
3681 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003682 */
3683 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3684 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003685 s->be->url_param_post_limit != 0 &&
Willy Tarreau61a21a32011-03-01 20:35:49 +01003686 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003687 buffer_dont_connect(req);
3688 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003689 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003690
Willy Tarreaud98cf932009-12-27 22:54:55 +01003691 if (txn->flags & TX_REQ_XFER_LEN)
3692 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003693
Willy Tarreau59234e92008-11-30 23:51:27 +01003694 /*************************************************************
3695 * OK, that's finished for the headers. We have done what we *
3696 * could. Let's switch to the DATA state. *
3697 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003698 req->analyse_exp = TICK_ETERNITY;
3699 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003700
Willy Tarreau59234e92008-11-30 23:51:27 +01003701 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003702 /* OK let's go on with the BODY now */
3703 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003704
Willy Tarreau59234e92008-11-30 23:51:27 +01003705 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003706 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003707 /* we detected a parsing error. We want to archive this request
3708 * in the dedicated proxy area for later troubleshooting.
3709 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003710 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003711 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003712
Willy Tarreau59234e92008-11-30 23:51:27 +01003713 txn->req.msg_state = HTTP_MSG_ERROR;
3714 txn->status = 400;
3715 req->analysers = 0;
3716 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003717
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003718 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003719 if (s->listener->counters)
3720 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003721
Willy Tarreau59234e92008-11-30 23:51:27 +01003722 if (!(s->flags & SN_ERR_MASK))
3723 s->flags |= SN_ERR_PRXCOND;
3724 if (!(s->flags & SN_FINST_MASK))
3725 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003726 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003727}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003728
Willy Tarreau60b85b02008-11-30 23:28:40 +01003729/* This function is an analyser which processes the HTTP tarpit. It always
3730 * returns zero, at the beginning because it prevents any other processing
3731 * from occurring, and at the end because it terminates the request.
3732 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003733int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003734{
3735 struct http_txn *txn = &s->txn;
3736
3737 /* This connection is being tarpitted. The CLIENT side has
3738 * already set the connect expiration date to the right
3739 * timeout. We just have to check that the client is still
3740 * there and that the timeout has not expired.
3741 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003742 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003743 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3744 !tick_is_expired(req->analyse_exp, now_ms))
3745 return 0;
3746
3747 /* We will set the queue timer to the time spent, just for
3748 * logging purposes. We fake a 500 server error, so that the
3749 * attacker will not suspect his connection has been tarpitted.
3750 * It will not cause trouble to the logs because we can exclude
3751 * the tarpitted connections by filtering on the 'PT' status flags.
3752 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003753 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3754
3755 txn->status = 500;
3756 if (req->flags != BF_READ_ERROR)
3757 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3758
3759 req->analysers = 0;
3760 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003761
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003762 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003763 if (s->listener->counters)
3764 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003765
Willy Tarreau60b85b02008-11-30 23:28:40 +01003766 if (!(s->flags & SN_ERR_MASK))
3767 s->flags |= SN_ERR_PRXCOND;
3768 if (!(s->flags & SN_FINST_MASK))
3769 s->flags |= SN_FINST_T;
3770 return 0;
3771}
3772
Willy Tarreaud34af782008-11-30 23:36:37 +01003773/* This function is an analyser which processes the HTTP request body. It looks
3774 * for parameters to be used for the load balancing algorithm (url_param). It
3775 * must only be called after the standard HTTP request processing has occurred,
3776 * because it expects the request to be parsed. It returns zero if it needs to
3777 * read more data, or 1 once it has completed its analysis.
3778 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003779int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003780{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003781 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003782 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003783 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003784
3785 /* We have to parse the HTTP request body to find any required data.
3786 * "balance url_param check_post" should have been the only way to get
3787 * into this. We were brought here after HTTP header analysis, so all
3788 * related structures are ready.
3789 */
3790
Willy Tarreau522d6c02009-12-06 18:49:18 +01003791 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3792 goto missing_data;
3793
3794 if (msg->msg_state < HTTP_MSG_100_SENT) {
3795 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3796 * send an HTTP/1.1 100 Continue intermediate response.
3797 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003798 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003799 struct hdr_ctx ctx;
3800 ctx.idx = 0;
3801 /* Expect is allowed in 1.1, look for it */
3802 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3803 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3804 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3805 }
3806 }
3807 msg->msg_state = HTTP_MSG_100_SENT;
3808 }
3809
3810 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003811 /* we have msg->col and msg->sov which both point to the first
3812 * byte of message body. msg->som still points to the beginning
3813 * of the message. We must save the body in req->lr because it
3814 * survives buffer re-alignments.
3815 */
3816 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003817 if (txn->flags & TX_REQ_TE_CHNK)
3818 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3819 else
3820 msg->msg_state = HTTP_MSG_DATA;
3821 }
3822
3823 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003824 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003825 * set ->sov and ->lr to point to the body and switch to DATA or
3826 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003827 */
3828 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003829
Willy Tarreau115acb92009-12-26 13:56:06 +01003830 if (!ret)
3831 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003832 else if (ret < 0) {
3833 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003834 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003835 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003836 }
3837
Willy Tarreaud98cf932009-12-27 22:54:55 +01003838 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003839 * We have the first non-header byte in msg->col, which is either the
3840 * beginning of the chunk size or of the data. The first data byte is in
3841 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3842 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003843 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003844
Willy Tarreau124d9912011-03-01 20:30:48 +01003845 if (msg->body_len < limit)
3846 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003847
Willy Tarreau7c96f672009-12-27 22:47:25 +01003848 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003849 goto http_end;
3850
3851 missing_data:
3852 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003853 if (req->flags & BF_FULL) {
3854 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003855 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003856 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003857
Willy Tarreau522d6c02009-12-06 18:49:18 +01003858 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3859 txn->status = 408;
3860 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003861
3862 if (!(s->flags & SN_ERR_MASK))
3863 s->flags |= SN_ERR_CLITO;
3864 if (!(s->flags & SN_FINST_MASK))
3865 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003866 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003867 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003868
3869 /* we get here if we need to wait for more data */
3870 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003871 /* Not enough data. We'll re-use the http-request
3872 * timeout here. Ideally, we should set the timeout
3873 * relative to the accept() date. We just set the
3874 * request timeout once at the beginning of the
3875 * request.
3876 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003877 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003878 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003879 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003880 return 0;
3881 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003882
3883 http_end:
3884 /* The situation will not evolve, so let's give up on the analysis. */
3885 s->logs.tv_request = now; /* update the request timer to reflect full request */
3886 req->analysers &= ~an_bit;
3887 req->analyse_exp = TICK_ETERNITY;
3888 return 1;
3889
3890 return_bad_req: /* let's centralize all bad requests */
3891 txn->req.msg_state = HTTP_MSG_ERROR;
3892 txn->status = 400;
3893 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3894
Willy Tarreau79ebac62010-06-07 13:47:49 +02003895 if (!(s->flags & SN_ERR_MASK))
3896 s->flags |= SN_ERR_PRXCOND;
3897 if (!(s->flags & SN_FINST_MASK))
3898 s->flags |= SN_FINST_R;
3899
Willy Tarreau522d6c02009-12-06 18:49:18 +01003900 return_err_msg:
3901 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003902 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003903 if (s->listener->counters)
3904 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003905 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003906}
3907
Willy Tarreau610ecce2010-01-04 21:15:02 +01003908/* Terminate current transaction and prepare a new one. This is very tricky
3909 * right now but it works.
3910 */
3911void http_end_txn_clean_session(struct session *s)
3912{
3913 /* FIXME: We need a more portable way of releasing a backend's and a
3914 * server's connections. We need a safer way to reinitialize buffer
3915 * flags. We also need a more accurate method for computing per-request
3916 * data.
3917 */
3918 http_silent_debug(__LINE__, s);
3919
3920 s->req->cons->flags |= SI_FL_NOLINGER;
3921 s->req->cons->shutr(s->req->cons);
3922 s->req->cons->shutw(s->req->cons);
3923
3924 http_silent_debug(__LINE__, s);
3925
3926 if (s->flags & SN_BE_ASSIGNED)
3927 s->be->beconn--;
3928
3929 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3930 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003931 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003932
3933 if (s->txn.status) {
3934 int n;
3935
3936 n = s->txn.status / 100;
3937 if (n < 1 || n > 5)
3938 n = 0;
3939
3940 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003941 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003942
Willy Tarreau24657792010-02-26 10:30:28 +01003943 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003944 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003945 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003946 }
3947
3948 /* don't count other requests' data */
3949 s->logs.bytes_in -= s->req->l - s->req->send_max;
3950 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3951
3952 /* let's do a final log if we need it */
3953 if (s->logs.logwait &&
3954 !(s->flags & SN_MONITOR) &&
3955 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3956 s->do_log(s);
3957 }
3958
3959 s->logs.accept_date = date; /* user-visible date for logging */
3960 s->logs.tv_accept = now; /* corrected date for internal use */
3961 tv_zero(&s->logs.tv_request);
3962 s->logs.t_queue = -1;
3963 s->logs.t_connect = -1;
3964 s->logs.t_data = -1;
3965 s->logs.t_close = 0;
3966 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3967 s->logs.srv_queue_size = 0; /* we will get this number soon */
3968
3969 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3970 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3971
3972 if (s->pend_pos)
3973 pendconn_free(s->pend_pos);
3974
Willy Tarreau827aee92011-03-10 16:55:02 +01003975 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003976 if (s->flags & SN_CURR_SESS) {
3977 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003978 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003979 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003980 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3981 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003982 }
3983
3984 if (unlikely(s->srv_conn))
3985 sess_change_server(s, NULL);
Willy Tarreau9e000c62011-03-10 14:03:36 +01003986 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003987
3988 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3989 s->req->cons->fd = -1; /* just to help with debugging */
3990 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003991 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003992 s->req->cons->err_loc = NULL;
3993 s->req->cons->exp = TICK_ETERNITY;
3994 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003995 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3996 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 +02003997 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 +01003998 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3999 s->txn.meth = 0;
4000 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004001 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004002 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004003 s->req->cons->flags |= SI_FL_INDEP_STR;
4004
Willy Tarreau96e31212011-05-30 18:10:30 +02004005 if (s->fe->options2 & PR_O2_NODELAY) {
4006 s->req->flags |= BF_NEVER_WAIT;
4007 s->rep->flags |= BF_NEVER_WAIT;
4008 }
4009
Willy Tarreau610ecce2010-01-04 21:15:02 +01004010 /* if the request buffer is not empty, it means we're
4011 * about to process another request, so send pending
4012 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004013 * Just don't do this if the buffer is close to be full,
4014 * because the request will wait for it to flush a little
4015 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004016 */
Willy Tarreau065e8332010-01-08 00:30:20 +01004017 if (s->req->l > s->req->send_max) {
4018 if (s->rep->send_max &&
4019 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01004020 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
4021 s->rep->flags |= BF_EXPECT_MORE;
4022 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004023
4024 /* we're removing the analysers, we MUST re-enable events detection */
4025 buffer_auto_read(s->req);
4026 buffer_auto_close(s->req);
4027 buffer_auto_read(s->rep);
4028 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004029
4030 /* make ->lr point to the first non-forwarded byte */
4031 s->req->lr = s->req->w + s->req->send_max;
4032 if (s->req->lr >= s->req->data + s->req->size)
4033 s->req->lr -= s->req->size;
4034 s->rep->lr = s->rep->w + s->rep->send_max;
4035 if (s->rep->lr >= s->rep->data + s->rep->size)
4036 s->rep->lr -= s->req->size;
4037
Willy Tarreau342b11c2010-11-24 16:22:09 +01004038 s->req->analysers = s->listener->analysers;
4039 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004040 s->rep->analysers = 0;
4041
4042 http_silent_debug(__LINE__, s);
4043}
4044
4045
4046/* This function updates the request state machine according to the response
4047 * state machine and buffer flags. It returns 1 if it changes anything (flag
4048 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4049 * it is only used to find when a request/response couple is complete. Both
4050 * this function and its equivalent should loop until both return zero. It
4051 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4052 */
4053int http_sync_req_state(struct session *s)
4054{
4055 struct buffer *buf = s->req;
4056 struct http_txn *txn = &s->txn;
4057 unsigned int old_flags = buf->flags;
4058 unsigned int old_state = txn->req.msg_state;
4059
4060 http_silent_debug(__LINE__, s);
4061 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4062 return 0;
4063
4064 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004065 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004066 * We can shut the read side unless we want to abort_on_close,
4067 * or we have a POST request. The issue with POST requests is
4068 * that some browsers still send a CRLF after the request, and
4069 * this CRLF must be read so that it does not remain in the kernel
4070 * buffers, otherwise a close could cause an RST on some systems
4071 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004072 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004073 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01004074 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004075
4076 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4077 goto wait_other_side;
4078
4079 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4080 /* The server has not finished to respond, so we
4081 * don't want to move in order not to upset it.
4082 */
4083 goto wait_other_side;
4084 }
4085
4086 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4087 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004088 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004089 txn->req.msg_state = HTTP_MSG_TUNNEL;
4090 goto wait_other_side;
4091 }
4092
4093 /* When we get here, it means that both the request and the
4094 * response have finished receiving. Depending on the connection
4095 * mode, we'll have to wait for the last bytes to leave in either
4096 * direction, and sometimes for a close to be effective.
4097 */
4098
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004099 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4100 /* Server-close mode : queue a connection close to the server */
4101 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004102 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004103 }
4104 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4105 /* Option forceclose is set, or either side wants to close,
4106 * let's enforce it now that we're not expecting any new
4107 * data to come. The caller knows the session is complete
4108 * once both states are CLOSED.
4109 */
4110 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004111 buffer_shutr_now(buf);
4112 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004113 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004114 }
4115 else {
4116 /* The last possible modes are keep-alive and tunnel. Since tunnel
4117 * mode does not set the body analyser, we can't reach this place
4118 * in tunnel mode, so we're left with keep-alive only.
4119 * This mode is currently not implemented, we switch to tunnel mode.
4120 */
4121 buffer_auto_read(buf);
4122 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004123 }
4124
4125 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4126 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004127 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4128
Willy Tarreau610ecce2010-01-04 21:15:02 +01004129 if (!(buf->flags & BF_OUT_EMPTY)) {
4130 txn->req.msg_state = HTTP_MSG_CLOSING;
4131 goto http_msg_closing;
4132 }
4133 else {
4134 txn->req.msg_state = HTTP_MSG_CLOSED;
4135 goto http_msg_closed;
4136 }
4137 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004138 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004139 }
4140
4141 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4142 http_msg_closing:
4143 /* nothing else to forward, just waiting for the output buffer
4144 * to be empty and for the shutw_now to take effect.
4145 */
4146 if (buf->flags & BF_OUT_EMPTY) {
4147 txn->req.msg_state = HTTP_MSG_CLOSED;
4148 goto http_msg_closed;
4149 }
4150 else if (buf->flags & BF_SHUTW) {
4151 txn->req.msg_state = HTTP_MSG_ERROR;
4152 goto wait_other_side;
4153 }
4154 }
4155
4156 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4157 http_msg_closed:
4158 goto wait_other_side;
4159 }
4160
4161 wait_other_side:
4162 http_silent_debug(__LINE__, s);
4163 return txn->req.msg_state != old_state || buf->flags != old_flags;
4164}
4165
4166
4167/* This function updates the response state machine according to the request
4168 * state machine and buffer flags. It returns 1 if it changes anything (flag
4169 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4170 * it is only used to find when a request/response couple is complete. Both
4171 * this function and its equivalent should loop until both return zero. It
4172 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4173 */
4174int http_sync_res_state(struct session *s)
4175{
4176 struct buffer *buf = s->rep;
4177 struct http_txn *txn = &s->txn;
4178 unsigned int old_flags = buf->flags;
4179 unsigned int old_state = txn->rsp.msg_state;
4180
4181 http_silent_debug(__LINE__, s);
4182 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4183 return 0;
4184
4185 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4186 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004187 * still monitor the server connection for a possible close
4188 * while the request is being uploaded, so we don't disable
4189 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004190 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004191 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004192
4193 if (txn->req.msg_state == HTTP_MSG_ERROR)
4194 goto wait_other_side;
4195
4196 if (txn->req.msg_state < HTTP_MSG_DONE) {
4197 /* The client seems to still be sending data, probably
4198 * because we got an error response during an upload.
4199 * We have the choice of either breaking the connection
4200 * or letting it pass through. Let's do the later.
4201 */
4202 goto wait_other_side;
4203 }
4204
4205 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4206 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004207 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004208 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4209 goto wait_other_side;
4210 }
4211
4212 /* When we get here, it means that both the request and the
4213 * response have finished receiving. Depending on the connection
4214 * mode, we'll have to wait for the last bytes to leave in either
4215 * direction, and sometimes for a close to be effective.
4216 */
4217
4218 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4219 /* Server-close mode : shut read and wait for the request
4220 * side to close its output buffer. The caller will detect
4221 * when we're in DONE and the other is in CLOSED and will
4222 * catch that for the final cleanup.
4223 */
4224 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4225 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004226 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004227 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4228 /* Option forceclose is set, or either side wants to close,
4229 * let's enforce it now that we're not expecting any new
4230 * data to come. The caller knows the session is complete
4231 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004232 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004233 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4234 buffer_shutr_now(buf);
4235 buffer_shutw_now(buf);
4236 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004237 }
4238 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004239 /* The last possible modes are keep-alive and tunnel. Since tunnel
4240 * mode does not set the body analyser, we can't reach this place
4241 * in tunnel mode, so we're left with keep-alive only.
4242 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004243 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004244 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004245 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004246 }
4247
4248 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4249 /* if we've just closed an output, let's switch */
4250 if (!(buf->flags & BF_OUT_EMPTY)) {
4251 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4252 goto http_msg_closing;
4253 }
4254 else {
4255 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4256 goto http_msg_closed;
4257 }
4258 }
4259 goto wait_other_side;
4260 }
4261
4262 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4263 http_msg_closing:
4264 /* nothing else to forward, just waiting for the output buffer
4265 * to be empty and for the shutw_now to take effect.
4266 */
4267 if (buf->flags & BF_OUT_EMPTY) {
4268 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4269 goto http_msg_closed;
4270 }
4271 else if (buf->flags & BF_SHUTW) {
4272 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004273 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004274 if (target_srv(&s->target))
4275 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004276 goto wait_other_side;
4277 }
4278 }
4279
4280 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4281 http_msg_closed:
4282 /* drop any pending data */
4283 buffer_ignore(buf, buf->l - buf->send_max);
4284 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004285 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004286 goto wait_other_side;
4287 }
4288
4289 wait_other_side:
4290 http_silent_debug(__LINE__, s);
4291 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4292}
4293
4294
4295/* Resync the request and response state machines. Return 1 if either state
4296 * changes.
4297 */
4298int http_resync_states(struct session *s)
4299{
4300 struct http_txn *txn = &s->txn;
4301 int old_req_state = txn->req.msg_state;
4302 int old_res_state = txn->rsp.msg_state;
4303
4304 http_silent_debug(__LINE__, s);
4305 http_sync_req_state(s);
4306 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004307 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004308 if (!http_sync_res_state(s))
4309 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004310 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004311 if (!http_sync_req_state(s))
4312 break;
4313 }
4314 http_silent_debug(__LINE__, s);
4315 /* OK, both state machines agree on a compatible state.
4316 * There are a few cases we're interested in :
4317 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4318 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4319 * directions, so let's simply disable both analysers.
4320 * - HTTP_MSG_CLOSED on the response only means we must abort the
4321 * request.
4322 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4323 * with server-close mode means we've completed one request and we
4324 * must re-initialize the server connection.
4325 */
4326
4327 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4328 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4329 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4330 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4331 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004332 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004333 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004334 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004335 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004336 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004337 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004338 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4339 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004340 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004341 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004342 s->rep->analysers = 0;
4343 buffer_auto_close(s->rep);
4344 buffer_auto_read(s->rep);
4345 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004346 buffer_abort(s->req);
4347 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004348 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004349 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004350 }
4351 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4352 txn->rsp.msg_state == HTTP_MSG_DONE &&
4353 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4354 /* server-close: terminate this server connection and
4355 * reinitialize a fresh-new transaction.
4356 */
4357 http_end_txn_clean_session(s);
4358 }
4359
4360 http_silent_debug(__LINE__, s);
4361 return txn->req.msg_state != old_req_state ||
4362 txn->rsp.msg_state != old_res_state;
4363}
4364
Willy Tarreaud98cf932009-12-27 22:54:55 +01004365/* This function is an analyser which forwards request body (including chunk
4366 * sizes if any). It is called as soon as we must forward, even if we forward
4367 * zero byte. The only situation where it must not be called is when we're in
4368 * tunnel mode and we want to forward till the close. It's used both to forward
4369 * remaining data and to resync after end of body. It expects the msg_state to
4370 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4371 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004372 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004373 * bytes of pending data + the headers if not already done (between som and sov).
4374 * It eventually adjusts som to match sov after the data in between have been sent.
4375 */
4376int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4377{
4378 struct http_txn *txn = &s->txn;
4379 struct http_msg *msg = &s->txn.req;
4380
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004381 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4382 return 0;
4383
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004384 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4385 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004386 /* Output closed while we were sending data. We must abort and
4387 * wake the other side up.
4388 */
4389 msg->msg_state = HTTP_MSG_ERROR;
4390 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004391 return 1;
4392 }
4393
Willy Tarreau4fe41902010-06-07 22:27:41 +02004394 /* in most states, we should abort in case of early close */
4395 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004396
4397 /* Note that we don't have to send 100-continue back because we don't
4398 * need the data to complete our job, and it's up to the server to
4399 * decide whether to return 100, 417 or anything else in return of
4400 * an "Expect: 100-continue" header.
4401 */
4402
4403 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4404 /* we have msg->col and msg->sov which both point to the first
4405 * byte of message body. msg->som still points to the beginning
4406 * of the message. We must save the body in req->lr because it
4407 * survives buffer re-alignments.
4408 */
4409 req->lr = req->data + msg->sov;
4410 if (txn->flags & TX_REQ_TE_CHNK)
4411 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4412 else {
4413 msg->msg_state = HTTP_MSG_DATA;
4414 }
4415 }
4416
Willy Tarreaud98cf932009-12-27 22:54:55 +01004417 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004418 int bytes;
4419
Willy Tarreau610ecce2010-01-04 21:15:02 +01004420 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004421 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004422 bytes = msg->sov - msg->som;
4423 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004424 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004425 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4426 bytes += req->size;
4427 msg->chunk_len += (unsigned int)bytes;
4428 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004429 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004430
Willy Tarreaucaabe412010-01-03 23:08:28 +01004431 if (msg->msg_state == HTTP_MSG_DATA) {
4432 /* must still forward */
4433 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004434 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004435
4436 /* nothing left to forward */
4437 if (txn->flags & TX_REQ_TE_CHNK)
4438 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004439 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004440 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004441 }
4442 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004443 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004444 * set ->sov and ->lr to point to the body and switch to DATA or
4445 * TRAILERS state.
4446 */
4447 int ret = http_parse_chunk_size(req, msg);
4448
4449 if (!ret)
4450 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004451 else if (ret < 0) {
4452 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004453 if (msg->err_pos >= 0)
4454 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004455 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004456 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004457 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004458 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004459 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4460 /* we want the CRLF after the data */
4461 int ret;
4462
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004463 req->lr = req->w + req->send_max;
4464 if (req->lr >= req->data + req->size)
4465 req->lr -= req->size;
4466
Willy Tarreaud98cf932009-12-27 22:54:55 +01004467 ret = http_skip_chunk_crlf(req, msg);
4468
4469 if (ret == 0)
4470 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004471 else if (ret < 0) {
4472 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004473 if (msg->err_pos >= 0)
4474 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004475 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004476 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004477 /* we're in MSG_CHUNK_SIZE now */
4478 }
4479 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4480 int ret = http_forward_trailers(req, msg);
4481
4482 if (ret == 0)
4483 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004484 else if (ret < 0) {
4485 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004486 if (msg->err_pos >= 0)
4487 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004488 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004489 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004490 /* we're in HTTP_MSG_DONE now */
4491 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004492 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004493 int old_state = msg->msg_state;
4494
Willy Tarreau610ecce2010-01-04 21:15:02 +01004495 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004496 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004497 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4498 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4499 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004500 if (http_resync_states(s)) {
4501 /* some state changes occurred, maybe the analyser
4502 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004503 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004504 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4505 if (req->flags & BF_SHUTW) {
4506 /* request errors are most likely due to
4507 * the server aborting the transfer.
4508 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004509 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004510 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004511 if (msg->err_pos >= 0)
4512 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004513 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004514 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004515 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004516 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004517
4518 /* If "option abortonclose" is set on the backend, we
4519 * want to monitor the client's connection and forward
4520 * any shutdown notification to the server, which will
4521 * decide whether to close or to go on processing the
4522 * request.
4523 */
4524 if (s->be->options & PR_O_ABRT_CLOSE) {
4525 buffer_auto_read(req);
4526 buffer_auto_close(req);
4527 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004528 else if (s->txn.meth == HTTP_METH_POST) {
4529 /* POST requests may require to read extra CRLF
4530 * sent by broken browsers and which could cause
4531 * an RST to be sent upon close on some systems
4532 * (eg: Linux).
4533 */
4534 buffer_auto_read(req);
4535 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004536
Willy Tarreau610ecce2010-01-04 21:15:02 +01004537 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004538 }
4539 }
4540
Willy Tarreaud98cf932009-12-27 22:54:55 +01004541 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004542 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004543 if (req->flags & BF_SHUTR) {
4544 if (!(s->flags & SN_ERR_MASK))
4545 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004546 if (!(s->flags & SN_FINST_MASK)) {
4547 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4548 s->flags |= SN_FINST_H;
4549 else
4550 s->flags |= SN_FINST_D;
4551 }
4552
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004553 s->fe->fe_counters.cli_aborts++;
4554 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004555 if (target_srv(&s->target))
4556 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004557
4558 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004559 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004560
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004561 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004562 if (req->flags & BF_SHUTW)
4563 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004564
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004565 /* When TE: chunked is used, we need to get there again to parse remaining
4566 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4567 */
4568 if (txn->flags & TX_REQ_TE_CHNK)
4569 buffer_dont_close(req);
4570
Willy Tarreau5c620922011-05-11 19:56:11 +02004571 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004572 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4573 * system knows it must not set a PUSH on this first part. Interactive
4574 * modes are already handled by the stream sock layer.
Willy Tarreau5c620922011-05-11 19:56:11 +02004575 */
Willy Tarreau07293032011-05-30 18:29:28 +02004576 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004577
Willy Tarreau610ecce2010-01-04 21:15:02 +01004578 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004579 return 0;
4580
Willy Tarreaud98cf932009-12-27 22:54:55 +01004581 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004582 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004583 if (s->listener->counters)
4584 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004585 return_bad_req_stats_ok:
4586 txn->req.msg_state = HTTP_MSG_ERROR;
4587 if (txn->status) {
4588 /* Note: we don't send any error if some data were already sent */
4589 stream_int_retnclose(req->prod, NULL);
4590 } else {
4591 txn->status = 400;
4592 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4593 }
4594 req->analysers = 0;
4595 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004596
4597 if (!(s->flags & SN_ERR_MASK))
4598 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004599 if (!(s->flags & SN_FINST_MASK)) {
4600 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4601 s->flags |= SN_FINST_H;
4602 else
4603 s->flags |= SN_FINST_D;
4604 }
4605 return 0;
4606
4607 aborted_xfer:
4608 txn->req.msg_state = HTTP_MSG_ERROR;
4609 if (txn->status) {
4610 /* Note: we don't send any error if some data were already sent */
4611 stream_int_retnclose(req->prod, NULL);
4612 } else {
4613 txn->status = 502;
4614 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4615 }
4616 req->analysers = 0;
4617 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4618
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004619 s->fe->fe_counters.srv_aborts++;
4620 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004621 if (target_srv(&s->target))
4622 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004623
4624 if (!(s->flags & SN_ERR_MASK))
4625 s->flags |= SN_ERR_SRVCL;
4626 if (!(s->flags & SN_FINST_MASK)) {
4627 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4628 s->flags |= SN_FINST_H;
4629 else
4630 s->flags |= SN_FINST_D;
4631 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004632 return 0;
4633}
4634
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004635/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4636 * processing can continue on next analysers, or zero if it either needs more
4637 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4638 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4639 * when it has nothing left to do, and may remove any analyser when it wants to
4640 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004641 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004642int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004643{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004644 struct http_txn *txn = &s->txn;
4645 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004646 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004647 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004648 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004649 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004650
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004651 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 +02004652 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004653 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004654 rep,
4655 rep->rex, rep->wex,
4656 rep->flags,
4657 rep->l,
4658 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004659
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004660 /*
4661 * Now parse the partial (or complete) lines.
4662 * We will check the response syntax, and also join multi-line
4663 * headers. An index of all the lines will be elaborated while
4664 * parsing.
4665 *
4666 * For the parsing, we use a 28 states FSM.
4667 *
4668 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004669 * rep->data + msg->som = beginning of response
4670 * rep->data + msg->eoh = end of processed headers / start of current one
4671 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004672 * rep->lr = first non-visited byte
4673 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004674 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004675 */
4676
Willy Tarreau83e3af02009-12-28 17:39:57 +01004677 /* There's a protected area at the end of the buffer for rewriting
4678 * purposes. We don't want to start to parse the request if the
4679 * protected area is affected, because we may have to move processed
4680 * data later, which is much more complicated.
4681 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004682 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4683 if (unlikely((rep->flags & BF_FULL) ||
4684 rep->r < rep->lr ||
4685 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4686 if (rep->send_max) {
4687 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004688 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4689 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004690 buffer_dont_close(rep);
4691 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4692 return 0;
4693 }
4694 if (rep->l <= rep->size - global.tune.maxrewrite)
4695 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004696 }
4697
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004698 if (likely(rep->lr < rep->r))
4699 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004700 }
4701
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004702 /* 1: we might have to print this header in debug mode */
4703 if (unlikely((global.mode & MODE_DEBUG) &&
4704 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004705 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004706 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004707 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004708
Willy Tarreau663308b2010-06-07 14:06:08 +02004709 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004710 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004711 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004712
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004713 sol += hdr_idx_first_pos(&txn->hdr_idx);
4714 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004715
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004716 while (cur_idx) {
4717 eol = sol + txn->hdr_idx.v[cur_idx].len;
4718 debug_hdr("srvhdr", s, sol, eol);
4719 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4720 cur_idx = txn->hdr_idx.v[cur_idx].next;
4721 }
4722 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004723
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004724 /*
4725 * Now we quickly check if we have found a full valid response.
4726 * If not so, we check the FD and buffer states before leaving.
4727 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004728 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004729 * responses are checked first.
4730 *
4731 * Depending on whether the client is still there or not, we
4732 * may send an error response back or not. Note that normally
4733 * we should only check for HTTP status there, and check I/O
4734 * errors somewhere else.
4735 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004736
Willy Tarreau655dce92009-11-08 13:10:58 +01004737 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004738 /* Invalid response */
4739 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4740 /* we detected a parsing error. We want to archive this response
4741 * in the dedicated proxy area for later troubleshooting.
4742 */
4743 hdr_response_bad:
4744 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004745 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004746
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004747 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004748 if (target_srv(&s->target)) {
4749 target_srv(&s->target)->counters.failed_resp++;
4750 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004751 }
Willy Tarreau64648412010-03-05 10:41:54 +01004752 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004753 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004754 rep->analysers = 0;
4755 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004756 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004757 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004758 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4759
4760 if (!(s->flags & SN_ERR_MASK))
4761 s->flags |= SN_ERR_PRXCOND;
4762 if (!(s->flags & SN_FINST_MASK))
4763 s->flags |= SN_FINST_H;
4764
4765 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004766 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004767
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004768 /* too large response does not fit in buffer. */
4769 else if (rep->flags & BF_FULL) {
4770 goto hdr_response_bad;
4771 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004772
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004773 /* read error */
4774 else if (rep->flags & BF_READ_ERROR) {
4775 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004776 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004777
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004778 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004779 if (target_srv(&s->target)) {
4780 target_srv(&s->target)->counters.failed_resp++;
4781 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004782 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004783
Willy Tarreau90deb182010-01-07 00:20:41 +01004784 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004785 rep->analysers = 0;
4786 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004787 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004788 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004789 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004790
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004791 if (!(s->flags & SN_ERR_MASK))
4792 s->flags |= SN_ERR_SRVCL;
4793 if (!(s->flags & SN_FINST_MASK))
4794 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004795 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004796 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004797
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004798 /* read timeout : return a 504 to the client. */
4799 else if (rep->flags & BF_READ_TIMEOUT) {
4800 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004801 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004802
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004803 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004804 if (target_srv(&s->target)) {
4805 target_srv(&s->target)->counters.failed_resp++;
4806 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004807 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004808
Willy Tarreau90deb182010-01-07 00:20:41 +01004809 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004810 rep->analysers = 0;
4811 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004812 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004813 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004814 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004815
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004816 if (!(s->flags & SN_ERR_MASK))
4817 s->flags |= SN_ERR_SRVTO;
4818 if (!(s->flags & SN_FINST_MASK))
4819 s->flags |= SN_FINST_H;
4820 return 0;
4821 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004822
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004823 /* close from server */
4824 else if (rep->flags & BF_SHUTR) {
4825 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004826 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004827
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004828 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004829 if (target_srv(&s->target)) {
4830 target_srv(&s->target)->counters.failed_resp++;
4831 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004832 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004833
Willy Tarreau90deb182010-01-07 00:20:41 +01004834 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004835 rep->analysers = 0;
4836 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004837 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004838 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004839 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004840
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004841 if (!(s->flags & SN_ERR_MASK))
4842 s->flags |= SN_ERR_SRVCL;
4843 if (!(s->flags & SN_FINST_MASK))
4844 s->flags |= SN_FINST_H;
4845 return 0;
4846 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004847
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004848 /* write error to client (we don't send any message then) */
4849 else if (rep->flags & BF_WRITE_ERROR) {
4850 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004851 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004852
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004853 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004854 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004855 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004856
4857 if (!(s->flags & SN_ERR_MASK))
4858 s->flags |= SN_ERR_CLICL;
4859 if (!(s->flags & SN_FINST_MASK))
4860 s->flags |= SN_FINST_H;
4861
4862 /* process_session() will take care of the error */
4863 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004864 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004865
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004866 buffer_dont_close(rep);
4867 return 0;
4868 }
4869
4870 /* More interesting part now : we know that we have a complete
4871 * response which at least looks like HTTP. We have an indicator
4872 * of each header's length, so we can parse them quickly.
4873 */
4874
4875 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004876 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004877
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004878 /*
4879 * 1: get the status code
4880 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004881 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004882 if (n < 1 || n > 5)
4883 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004884 /* when the client triggers a 4xx from the server, it's most often due
4885 * to a missing object or permission. These events should be tracked
4886 * because if they happen often, it may indicate a brute force or a
4887 * vulnerability scan.
4888 */
4889 if (n == 4)
4890 session_inc_http_err_ctr(s);
4891
Willy Tarreau827aee92011-03-10 16:55:02 +01004892 if (target_srv(&s->target))
4893 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004894
Willy Tarreau5b154472009-12-21 20:11:07 +01004895 /* check if the response is HTTP/1.1 or above */
4896 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004897 ((msg->sol[5] > '1') ||
4898 ((msg->sol[5] == '1') &&
4899 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004900 txn->flags |= TX_RES_VER_11;
4901
4902 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004903 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 +01004904
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004905 /* transfer length unknown*/
4906 txn->flags &= ~TX_RES_XFER_LEN;
4907
Willy Tarreau962c3f42010-01-10 00:15:35 +01004908 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004909
Willy Tarreau39650402010-03-15 19:44:39 +01004910 /* Adjust server's health based on status code. Note: status codes 501
4911 * and 505 are triggered on demand by client request, so we must not
4912 * count them as server failures.
4913 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004914 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004915 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004916 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004917 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004918 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004919 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004920
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004921 /*
4922 * 2: check for cacheability.
4923 */
4924
4925 switch (txn->status) {
4926 case 200:
4927 case 203:
4928 case 206:
4929 case 300:
4930 case 301:
4931 case 410:
4932 /* RFC2616 @13.4:
4933 * "A response received with a status code of
4934 * 200, 203, 206, 300, 301 or 410 MAY be stored
4935 * by a cache (...) unless a cache-control
4936 * directive prohibits caching."
4937 *
4938 * RFC2616 @9.5: POST method :
4939 * "Responses to this method are not cacheable,
4940 * unless the response includes appropriate
4941 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004942 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004943 if (likely(txn->meth != HTTP_METH_POST) &&
4944 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4945 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4946 break;
4947 default:
4948 break;
4949 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004950
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004951 /*
4952 * 3: we may need to capture headers
4953 */
4954 s->logs.logwait &= ~LW_RESP;
4955 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004956 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004957 txn->rsp.cap, s->fe->rsp_cap);
4958
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004959 /* 4: determine the transfer-length.
4960 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4961 * the presence of a message-body in a RESPONSE and its transfer length
4962 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004963 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004964 * All responses to the HEAD request method MUST NOT include a
4965 * message-body, even though the presence of entity-header fields
4966 * might lead one to believe they do. All 1xx (informational), 204
4967 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4968 * message-body. All other responses do include a message-body,
4969 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004970 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004971 * 1. Any response which "MUST NOT" include a message-body (such as the
4972 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4973 * always terminated by the first empty line after the header fields,
4974 * regardless of the entity-header fields present in the message.
4975 *
4976 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4977 * the "chunked" transfer-coding (Section 6.2) is used, the
4978 * transfer-length is defined by the use of this transfer-coding.
4979 * If a Transfer-Encoding header field is present and the "chunked"
4980 * transfer-coding is not present, the transfer-length is defined by
4981 * the sender closing the connection.
4982 *
4983 * 3. If a Content-Length header field is present, its decimal value in
4984 * OCTETs represents both the entity-length and the transfer-length.
4985 * If a message is received with both a Transfer-Encoding header
4986 * field and a Content-Length header field, the latter MUST be ignored.
4987 *
4988 * 4. If the message uses the media type "multipart/byteranges", and
4989 * the transfer-length is not otherwise specified, then this self-
4990 * delimiting media type defines the transfer-length. This media
4991 * type MUST NOT be used unless the sender knows that the recipient
4992 * can parse it; the presence in a request of a Range header with
4993 * multiple byte-range specifiers from a 1.1 client implies that the
4994 * client can parse multipart/byteranges responses.
4995 *
4996 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004997 */
4998
4999 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005000 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005001 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005002 * FIXME: should we parse anyway and return an error on chunked encoding ?
5003 */
5004 if (txn->meth == HTTP_METH_HEAD ||
5005 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005006 txn->status == 204 || txn->status == 304) {
5007 txn->flags |= TX_RES_XFER_LEN;
5008 goto skip_content_length;
5009 }
5010
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005011 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005012 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01005013 while ((txn->flags & TX_RES_VER_11) &&
5014 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005015 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
5016 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5017 else if (txn->flags & TX_RES_TE_CHNK) {
5018 /* bad transfer-encoding (chunked followed by something else) */
5019 use_close_only = 1;
5020 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5021 break;
5022 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005023 }
5024
5025 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5026 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005027 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005028 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
5029 signed long long cl;
5030
5031 if (!ctx.vlen)
5032 goto hdr_response_bad;
5033
5034 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
5035 goto hdr_response_bad; /* parse failure */
5036
5037 if (cl < 0)
5038 goto hdr_response_bad;
5039
Willy Tarreau124d9912011-03-01 20:30:48 +01005040 if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl))
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005041 goto hdr_response_bad; /* already specified, was different */
5042
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005043 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005044 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005045 }
5046
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005047 /* FIXME: we should also implement the multipart/byterange method.
5048 * For now on, we resort to close mode in this case (unknown length).
5049 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005050skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005051
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005052 /* end of job, return OK */
5053 rep->analysers &= ~an_bit;
5054 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01005055 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005056 return 1;
5057}
5058
5059/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005060 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5061 * and updates t->rep->analysers. It might make sense to explode it into several
5062 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005063 */
5064int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
5065{
5066 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005067 struct http_msg *msg = &txn->rsp;
5068 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005069 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005070
5071 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
5072 now_ms, __FUNCTION__,
5073 t,
5074 rep,
5075 rep->rex, rep->wex,
5076 rep->flags,
5077 rep->l,
5078 rep->analysers);
5079
Willy Tarreau655dce92009-11-08 13:10:58 +01005080 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005081 return 0;
5082
5083 rep->analysers &= ~an_bit;
5084 rep->analyse_exp = TICK_ETERNITY;
5085
Willy Tarreau5b154472009-12-21 20:11:07 +01005086 /* Now we have to check if we need to modify the Connection header.
5087 * This is more difficult on the response than it is on the request,
5088 * because we can have two different HTTP versions and we don't know
5089 * how the client will interprete a response. For instance, let's say
5090 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5091 * HTTP/1.1 response without any header. Maybe it will bound itself to
5092 * HTTP/1.0 because it only knows about it, and will consider the lack
5093 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5094 * the lack of header as a keep-alive. Thus we will use two flags
5095 * indicating how a request MAY be understood by the client. In case
5096 * of multiple possibilities, we'll fix the header to be explicit. If
5097 * ambiguous cases such as both close and keepalive are seen, then we
5098 * will fall back to explicit close. Note that we won't take risks with
5099 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005100 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005101 */
5102
Willy Tarreaudc008c52010-02-01 16:20:08 +01005103 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5104 txn->status == 101)) {
5105 /* Either we've established an explicit tunnel, or we're
5106 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005107 * to understand the next protocols. We have to switch to tunnel
5108 * mode, so that we transfer the request and responses then let
5109 * this protocol pass unmodified. When we later implement specific
5110 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005111 * header which contains information about that protocol for
5112 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005113 */
5114 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5115 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005116 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5117 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5118 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005119 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005120
Willy Tarreau60466522010-01-18 19:08:45 +01005121 /* on unknown transfer length, we must close */
5122 if (!(txn->flags & TX_RES_XFER_LEN) &&
5123 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5124 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005125
Willy Tarreau60466522010-01-18 19:08:45 +01005126 /* now adjust header transformations depending on current state */
5127 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5128 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5129 to_del |= 2; /* remove "keep-alive" on any response */
5130 if (!(txn->flags & TX_RES_VER_11))
5131 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005132 }
Willy Tarreau60466522010-01-18 19:08:45 +01005133 else { /* SCL / KAL */
5134 to_del |= 1; /* remove "close" on any response */
5135 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
5136 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005137 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005138
Willy Tarreau60466522010-01-18 19:08:45 +01005139 /* Parse and remove some headers from the connection header */
5140 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005141
Willy Tarreau60466522010-01-18 19:08:45 +01005142 /* Some keep-alive responses are converted to Server-close if
5143 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005144 */
Willy Tarreau60466522010-01-18 19:08:45 +01005145 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5146 if ((txn->flags & TX_HDR_CONN_CLO) ||
5147 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
5148 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005149 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005150 }
5151
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005152 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005153 /*
5154 * 3: we will have to evaluate the filters.
5155 * As opposed to version 1.2, now they will be evaluated in the
5156 * filters order and not in the header order. This means that
5157 * each filter has to be validated among all headers.
5158 *
5159 * Filters are tried with ->be first, then with ->fe if it is
5160 * different from ->be.
5161 */
5162
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005163 cur_proxy = t->be;
5164 while (1) {
5165 struct proxy *rule_set = cur_proxy;
5166
5167 /* try headers filters */
5168 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005169 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005170 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005171 if (target_srv(&t->target)) {
5172 target_srv(&t->target)->counters.failed_resp++;
5173 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005174 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005175 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005176 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005177 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005178 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005179 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01005180 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005181 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005182 if (!(t->flags & SN_ERR_MASK))
5183 t->flags |= SN_ERR_PRXCOND;
5184 if (!(t->flags & SN_FINST_MASK))
5185 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005186 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005187 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005188 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005189
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005190 /* has the response been denied ? */
5191 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005192 if (target_srv(&t->target))
5193 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005194
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005195 t->be->be_counters.denied_resp++;
5196 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005197 if (t->listener->counters)
5198 t->listener->counters->denied_resp++;
5199
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005200 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005201 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005202
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005203 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005204 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005205 if (txn->status < 200)
5206 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005207 if (wl->cond) {
5208 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5209 ret = acl_pass(ret);
5210 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5211 ret = !ret;
5212 if (!ret)
5213 continue;
5214 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005215 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005216 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005217 }
5218
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005219 /* check whether we're already working on the frontend */
5220 if (cur_proxy == t->fe)
5221 break;
5222 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005223 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005224
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005225 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005226 * We may be facing a 100-continue response, in which case this
5227 * is not the right response, and we're waiting for the next one.
5228 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005229 * next one.
5230 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005231 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005232 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005233 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005234 msg->msg_state = HTTP_MSG_RPBEFORE;
5235 txn->status = 0;
5236 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5237 return 1;
5238 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005239 else if (unlikely(txn->status < 200))
5240 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005241
5242 /* we don't have any 1xx status code now */
5243
5244 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005245 * 4: check for server cookie.
5246 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005247 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5248 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005249 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005250
Willy Tarreaubaaee002006-06-26 02:48:02 +02005251
Willy Tarreaua15645d2007-03-18 16:22:39 +01005252 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005253 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005254 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005255 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005256 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005257
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005258 /*
5259 * 6: add server cookie in the response if needed
5260 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005261 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005262 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005263 (!(t->flags & SN_DIRECT) ||
5264 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5265 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5266 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5267 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005268 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5269 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005270 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005271 /* the server is known, it's not the one the client requested, or the
5272 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005273 * insert a set-cookie here, except if we want to insert only on POST
5274 * requests and this one isn't. Note that servers which don't have cookies
5275 * (eg: some backup servers) will return a full cookie removal request.
5276 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005277 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005278 len = sprintf(trash,
5279 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5280 t->be->cookie_name);
5281 }
5282 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005283 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005284
5285 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5286 /* emit last_date, which is mandatory */
5287 trash[len++] = COOKIE_DELIM_DATE;
5288 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5289 if (t->be->cookie_maxlife) {
5290 /* emit first_date, which is either the original one or
5291 * the current date.
5292 */
5293 trash[len++] = COOKIE_DELIM_DATE;
5294 s30tob64(txn->cookie_first_date ?
5295 txn->cookie_first_date >> 2 :
5296 (date.tv_sec+3) >> 2, trash + len);
5297 len += 5;
5298 }
5299 }
5300 len += sprintf(trash + len, "; path=/");
5301 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005302
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005303 if (t->be->cookie_domain)
5304 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005305
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005306 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005307 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005308 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005309
Willy Tarreauf1348312010-10-07 15:54:11 +02005310 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005311 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005312 /* the server did not change, only the date was updated */
5313 txn->flags |= TX_SCK_UPDATED;
5314 else
5315 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005316
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005317 /* Here, we will tell an eventual cache on the client side that we don't
5318 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5319 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5320 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5321 */
5322 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005323
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005324 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5325
5326 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005327 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005328 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005329 }
5330 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005331
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005332 /*
5333 * 7: check if result will be cacheable with a cookie.
5334 * We'll block the response if security checks have caught
5335 * nasty things such as a cacheable cookie.
5336 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005337 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5338 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005339 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005340
5341 /* we're in presence of a cacheable response containing
5342 * a set-cookie header. We'll block it as requested by
5343 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005344 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005345 if (target_srv(&t->target))
5346 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005347
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005348 t->be->be_counters.denied_resp++;
5349 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005350 if (t->listener->counters)
5351 t->listener->counters->denied_resp++;
5352
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005353 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005354 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005355 send_log(t->be, LOG_ALERT,
5356 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005357 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005358 goto return_srv_prx_502;
5359 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005360
5361 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005362 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005363 */
Willy Tarreau60466522010-01-18 19:08:45 +01005364 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5365 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5366 unsigned int want_flags = 0;
5367
5368 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5369 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5370 /* we want a keep-alive response here. Keep-alive header
5371 * required if either side is not 1.1.
5372 */
5373 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5374 want_flags |= TX_CON_KAL_SET;
5375 }
5376 else {
5377 /* we want a close response here. Close header required if
5378 * the server is 1.1, regardless of the client.
5379 */
5380 if (txn->flags & TX_RES_VER_11)
5381 want_flags |= TX_CON_CLO_SET;
5382 }
5383
5384 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5385 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005386 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005387
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005388 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005389 if ((txn->flags & TX_RES_XFER_LEN) ||
5390 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005391 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005392
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005393 /*************************************************************
5394 * OK, that's finished for the headers. We have done what we *
5395 * could. Let's switch to the DATA state. *
5396 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005397
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005398 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005399
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005400 /* if the user wants to log as soon as possible, without counting
5401 * bytes from the server, then this is the right moment. We have
5402 * to temporarily assign bytes_out to log what we currently have.
5403 */
5404 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5405 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5406 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005407 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005408 t->logs.bytes_out = 0;
5409 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005410
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005411 /* Note: we must not try to cheat by jumping directly to DATA,
5412 * otherwise we would not let the client side wake up.
5413 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005414
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005415 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005416 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005417 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005418}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005419
Willy Tarreaud98cf932009-12-27 22:54:55 +01005420/* This function is an analyser which forwards response body (including chunk
5421 * sizes if any). It is called as soon as we must forward, even if we forward
5422 * zero byte. The only situation where it must not be called is when we're in
5423 * tunnel mode and we want to forward till the close. It's used both to forward
5424 * remaining data and to resync after end of body. It expects the msg_state to
5425 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5426 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005427 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005428 * bytes of pending data + the headers if not already done (between som and sov).
5429 * It eventually adjusts som to match sov after the data in between have been sent.
5430 */
5431int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5432{
5433 struct http_txn *txn = &s->txn;
5434 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005435 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005436
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005437 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5438 return 0;
5439
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005440 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005441 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005442 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005443 /* Output closed while we were sending data. We must abort and
5444 * wake the other side up.
5445 */
5446 msg->msg_state = HTTP_MSG_ERROR;
5447 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005448 return 1;
5449 }
5450
Willy Tarreau4fe41902010-06-07 22:27:41 +02005451 /* in most states, we should abort in case of early close */
5452 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005453
Willy Tarreaud98cf932009-12-27 22:54:55 +01005454 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5455 /* we have msg->col and msg->sov which both point to the first
5456 * byte of message body. msg->som still points to the beginning
5457 * of the message. We must save the body in req->lr because it
5458 * survives buffer re-alignments.
5459 */
5460 res->lr = res->data + msg->sov;
5461 if (txn->flags & TX_RES_TE_CHNK)
5462 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5463 else {
5464 msg->msg_state = HTTP_MSG_DATA;
5465 }
5466 }
5467
Willy Tarreaud98cf932009-12-27 22:54:55 +01005468 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005469 int bytes;
5470
Willy Tarreau610ecce2010-01-04 21:15:02 +01005471 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005472 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005473 bytes = msg->sov - msg->som;
5474 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005475 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005476 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5477 bytes += res->size;
5478 msg->chunk_len += (unsigned int)bytes;
5479 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005480 }
5481
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005482
Willy Tarreaucaabe412010-01-03 23:08:28 +01005483 if (msg->msg_state == HTTP_MSG_DATA) {
5484 /* must still forward */
5485 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005486 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005487
5488 /* nothing left to forward */
5489 if (txn->flags & TX_RES_TE_CHNK)
5490 msg->msg_state = HTTP_MSG_DATA_CRLF;
5491 else
5492 msg->msg_state = HTTP_MSG_DONE;
5493 }
5494 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005495 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005496 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5497 */
5498 int ret = http_parse_chunk_size(res, msg);
5499
5500 if (!ret)
5501 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005502 else if (ret < 0) {
5503 if (msg->err_pos >= 0)
5504 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005505 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005506 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005507 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005508 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005509 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5510 /* we want the CRLF after the data */
5511 int ret;
5512
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005513 res->lr = res->w + res->send_max;
5514 if (res->lr >= res->data + res->size)
5515 res->lr -= res->size;
5516
Willy Tarreaud98cf932009-12-27 22:54:55 +01005517 ret = http_skip_chunk_crlf(res, msg);
5518
5519 if (!ret)
5520 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005521 else if (ret < 0) {
5522 if (msg->err_pos >= 0)
5523 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005524 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005525 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005526 /* we're in MSG_CHUNK_SIZE now */
5527 }
5528 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5529 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005530
Willy Tarreaud98cf932009-12-27 22:54:55 +01005531 if (ret == 0)
5532 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005533 else if (ret < 0) {
5534 if (msg->err_pos >= 0)
5535 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005536 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005537 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005538 /* we're in HTTP_MSG_DONE now */
5539 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005540 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005541 int old_state = msg->msg_state;
5542
Willy Tarreau610ecce2010-01-04 21:15:02 +01005543 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005544 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005545 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5546 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5547 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005548 if (http_resync_states(s)) {
5549 http_silent_debug(__LINE__, s);
5550 /* some state changes occurred, maybe the analyser
5551 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005552 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005553 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5554 if (res->flags & BF_SHUTW) {
5555 /* response errors are most likely due to
5556 * the client aborting the transfer.
5557 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005558 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005559 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005560 if (msg->err_pos >= 0)
5561 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005562 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005563 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005564 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005565 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005566 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005567 }
5568 }
5569
Willy Tarreaud98cf932009-12-27 22:54:55 +01005570 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005571 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005572 if (res->flags & BF_SHUTR) {
5573 if (!(s->flags & SN_ERR_MASK))
5574 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005575 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005576 if (target_srv(&s->target))
5577 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005578 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005579 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005580
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005581 if (res->flags & BF_SHUTW)
5582 goto aborted_xfer;
5583
Willy Tarreau40dba092010-03-04 18:14:51 +01005584 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005585 if (!s->req->analysers)
5586 goto return_bad_res;
5587
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005588 /* forward any pending data */
5589 bytes = msg->sov - msg->som;
5590 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005591 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005592 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5593 bytes += res->size;
5594 msg->chunk_len += (unsigned int)bytes;
5595 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005596 }
5597
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005598 /* When TE: chunked is used, we need to get there again to parse remaining
5599 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5600 * Similarly, with keep-alive on the client side, we don't want to forward a
5601 * close.
5602 */
5603 if ((txn->flags & TX_RES_TE_CHNK) ||
5604 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5605 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5606 buffer_dont_close(res);
5607
Willy Tarreau5c620922011-05-11 19:56:11 +02005608 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005609 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5610 * system knows it must not set a PUSH on this first part. Interactive
5611 * modes are already handled by the stream sock layer.
Willy Tarreau5c620922011-05-11 19:56:11 +02005612 */
Willy Tarreau07293032011-05-30 18:29:28 +02005613 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005614
Willy Tarreaud98cf932009-12-27 22:54:55 +01005615 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005616 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005617 return 0;
5618
Willy Tarreau40dba092010-03-04 18:14:51 +01005619 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005620 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005621 if (target_srv(&s->target))
5622 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005623
5624 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005625 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005626 /* don't send any error message as we're in the body */
5627 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005628 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005629 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005630 if (target_srv(&s->target))
5631 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005632
5633 if (!(s->flags & SN_ERR_MASK))
5634 s->flags |= SN_ERR_PRXCOND;
5635 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005636 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005637 return 0;
5638
5639 aborted_xfer:
5640 txn->rsp.msg_state = HTTP_MSG_ERROR;
5641 /* don't send any error message as we're in the body */
5642 stream_int_retnclose(res->cons, NULL);
5643 res->analysers = 0;
5644 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5645
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005646 s->fe->fe_counters.cli_aborts++;
5647 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005648 if (target_srv(&s->target))
5649 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005650
5651 if (!(s->flags & SN_ERR_MASK))
5652 s->flags |= SN_ERR_CLICL;
5653 if (!(s->flags & SN_FINST_MASK))
5654 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005655 return 0;
5656}
5657
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005658/* Iterate the same filter through all request headers.
5659 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005660 * Since it can manage the switch to another backend, it updates the per-proxy
5661 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005662 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005663int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005664{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005665 char term;
5666 char *cur_ptr, *cur_end, *cur_next;
5667 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005668 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005669 struct hdr_idx_elem *cur_hdr;
5670 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005671
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005672 last_hdr = 0;
5673
Willy Tarreau962c3f42010-01-10 00:15:35 +01005674 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005675 old_idx = 0;
5676
5677 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005678 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005679 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005680 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005681 (exp->action == ACT_ALLOW ||
5682 exp->action == ACT_DENY ||
5683 exp->action == ACT_TARPIT))
5684 return 0;
5685
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005686 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005687 if (!cur_idx)
5688 break;
5689
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005690 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005691 cur_ptr = cur_next;
5692 cur_end = cur_ptr + cur_hdr->len;
5693 cur_next = cur_end + cur_hdr->cr + 1;
5694
5695 /* Now we have one header between cur_ptr and cur_end,
5696 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005697 */
5698
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005699 /* The annoying part is that pattern matching needs
5700 * that we modify the contents to null-terminate all
5701 * strings before testing them.
5702 */
5703
5704 term = *cur_end;
5705 *cur_end = '\0';
5706
5707 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5708 switch (exp->action) {
5709 case ACT_SETBE:
5710 /* It is not possible to jump a second time.
5711 * FIXME: should we return an HTTP/500 here so that
5712 * the admin knows there's a problem ?
5713 */
5714 if (t->be != t->fe)
5715 break;
5716
5717 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005718 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005719 last_hdr = 1;
5720 break;
5721
5722 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005723 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005724 last_hdr = 1;
5725 break;
5726
5727 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005728 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005729 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005730
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005731 t->fe->fe_counters.denied_req++;
5732 if (t->fe != t->be)
5733 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005734 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005735 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005736
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005737 break;
5738
5739 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005740 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005741 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005742
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005743 t->fe->fe_counters.denied_req++;
5744 if (t->fe != t->be)
5745 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005746 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005747 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005748
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005749 break;
5750
5751 case ACT_REPLACE:
5752 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5753 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5754 /* FIXME: if the user adds a newline in the replacement, the
5755 * index will not be recalculated for now, and the new line
5756 * will not be counted as a new header.
5757 */
5758
5759 cur_end += delta;
5760 cur_next += delta;
5761 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005762 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005763 break;
5764
5765 case ACT_REMOVE:
5766 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5767 cur_next += delta;
5768
Willy Tarreaufa355d42009-11-29 18:12:29 +01005769 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005770 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5771 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005772 cur_hdr->len = 0;
5773 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005774 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005775 break;
5776
5777 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005778 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005779 if (cur_end)
5780 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005781
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005782 /* keep the link from this header to next one in case of later
5783 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005784 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005785 old_idx = cur_idx;
5786 }
5787 return 0;
5788}
5789
5790
5791/* Apply the filter to the request line.
5792 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5793 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005794 * Since it can manage the switch to another backend, it updates the per-proxy
5795 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005796 */
5797int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5798{
5799 char term;
5800 char *cur_ptr, *cur_end;
5801 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005802 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005803 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005804
Willy Tarreau58f10d72006-12-04 02:26:12 +01005805
Willy Tarreau3d300592007-03-18 18:34:41 +01005806 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005807 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005808 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005809 (exp->action == ACT_ALLOW ||
5810 exp->action == ACT_DENY ||
5811 exp->action == ACT_TARPIT))
5812 return 0;
5813 else if (exp->action == ACT_REMOVE)
5814 return 0;
5815
5816 done = 0;
5817
Willy Tarreau962c3f42010-01-10 00:15:35 +01005818 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005819 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005820
5821 /* Now we have the request line between cur_ptr and cur_end */
5822
5823 /* The annoying part is that pattern matching needs
5824 * that we modify the contents to null-terminate all
5825 * strings before testing them.
5826 */
5827
5828 term = *cur_end;
5829 *cur_end = '\0';
5830
5831 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5832 switch (exp->action) {
5833 case ACT_SETBE:
5834 /* It is not possible to jump a second time.
5835 * FIXME: should we return an HTTP/500 here so that
5836 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005837 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005838 if (t->be != t->fe)
5839 break;
5840
5841 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005842 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005843 done = 1;
5844 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005845
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005846 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005847 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005848 done = 1;
5849 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005850
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005851 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005852 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005853
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005854 t->fe->fe_counters.denied_req++;
5855 if (t->fe != t->be)
5856 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005857 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005858 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005859
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005860 done = 1;
5861 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005862
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005863 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005864 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005865
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005866 t->fe->fe_counters.denied_req++;
5867 if (t->fe != t->be)
5868 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005869 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005870 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005871
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005872 done = 1;
5873 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005874
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005875 case ACT_REPLACE:
5876 *cur_end = term; /* restore the string terminator */
5877 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5878 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5879 /* FIXME: if the user adds a newline in the replacement, the
5880 * index will not be recalculated for now, and the new line
5881 * will not be counted as a new header.
5882 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005883
Willy Tarreaufa355d42009-11-29 18:12:29 +01005884 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005885 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005886 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005887 HTTP_MSG_RQMETH,
5888 cur_ptr, cur_end + 1,
5889 NULL, NULL);
5890 if (unlikely(!cur_end))
5891 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005892
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005893 /* we have a full request and we know that we have either a CR
5894 * or an LF at <ptr>.
5895 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005896 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5897 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005898 /* there is no point trying this regex on headers */
5899 return 1;
5900 }
5901 }
5902 *cur_end = term; /* restore the string terminator */
5903 return done;
5904}
Willy Tarreau97de6242006-12-27 17:18:38 +01005905
Willy Tarreau58f10d72006-12-04 02:26:12 +01005906
Willy Tarreau58f10d72006-12-04 02:26:12 +01005907
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005908/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005909 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005910 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005911 * unparsable request. Since it can manage the switch to another backend, it
5912 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005913 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005914int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005915{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005916 struct http_txn *txn = &s->txn;
5917 struct hdr_exp *exp;
5918
5919 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005920 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005921
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005922 /*
5923 * The interleaving of transformations and verdicts
5924 * makes it difficult to decide to continue or stop
5925 * the evaluation.
5926 */
5927
Willy Tarreau6c123b12010-01-28 20:22:06 +01005928 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5929 break;
5930
Willy Tarreau3d300592007-03-18 18:34:41 +01005931 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005932 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005933 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005934 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005935
5936 /* if this filter had a condition, evaluate it now and skip to
5937 * next filter if the condition does not match.
5938 */
5939 if (exp->cond) {
5940 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5941 ret = acl_pass(ret);
5942 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5943 ret = !ret;
5944
5945 if (!ret)
5946 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005947 }
5948
5949 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005950 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005951 if (unlikely(ret < 0))
5952 return -1;
5953
5954 if (likely(ret == 0)) {
5955 /* The filter did not match the request, it can be
5956 * iterated through all headers.
5957 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005958 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005959 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005960 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005961 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005962}
5963
5964
Willy Tarreaua15645d2007-03-18 16:22:39 +01005965
Willy Tarreau58f10d72006-12-04 02:26:12 +01005966/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005967 * Try to retrieve the server associated to the appsession.
5968 * If the server is found, it's assigned to the session.
5969 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005970void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005971 struct http_txn *txn = &t->txn;
5972 appsess *asession = NULL;
5973 char *sessid_temp = NULL;
5974
Cyril Bontéb21570a2009-11-29 20:04:48 +01005975 if (len > t->be->appsession_len) {
5976 len = t->be->appsession_len;
5977 }
5978
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005979 if (t->be->options2 & PR_O2_AS_REQL) {
5980 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005981 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005982 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005983 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005984 }
5985
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005986 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005987 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5988 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5989 return;
5990 }
5991
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005992 memcpy(txn->sessid, buf, len);
5993 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005994 }
5995
5996 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5997 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5998 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5999 return;
6000 }
6001
Cyril Bontéb21570a2009-11-29 20:04:48 +01006002 memcpy(sessid_temp, buf, len);
6003 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006004
6005 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6006 /* free previously allocated memory */
6007 pool_free2(apools.sessid, sessid_temp);
6008
6009 if (asession != NULL) {
6010 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6011 if (!(t->be->options2 & PR_O2_AS_REQL))
6012 asession->request_count++;
6013
6014 if (asession->serverid != NULL) {
6015 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006016
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006017 while (srv) {
6018 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006019 if ((srv->state & SRV_RUNNING) ||
6020 (t->be->options & PR_O_PERSIST) ||
6021 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006022 /* we found the server and it's usable */
6023 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006024 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006025 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006026 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01006027
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006028 break;
6029 } else {
6030 txn->flags &= ~TX_CK_MASK;
6031 txn->flags |= TX_CK_DOWN;
6032 }
6033 }
6034 srv = srv->next;
6035 }
6036 }
6037 }
6038}
6039
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006040/* Find the end of a cookie value contained between <s> and <e>. It works the
6041 * same way as with headers above except that the semi-colon also ends a token.
6042 * See RFC2965 for more information. Note that it requires a valid header to
6043 * return a valid result.
6044 */
6045char *find_cookie_value_end(char *s, const char *e)
6046{
6047 int quoted, qdpair;
6048
6049 quoted = qdpair = 0;
6050 for (; s < e; s++) {
6051 if (qdpair) qdpair = 0;
6052 else if (quoted) {
6053 if (*s == '\\') qdpair = 1;
6054 else if (*s == '"') quoted = 0;
6055 }
6056 else if (*s == '"') quoted = 1;
6057 else if (*s == ',' || *s == ';') return s;
6058 }
6059 return s;
6060}
6061
6062/* Delete a value in a header between delimiters <from> and <next> in buffer
6063 * <buf>. The number of characters displaced is returned, and the pointer to
6064 * the first delimiter is updated if required. The function tries as much as
6065 * possible to respect the following principles :
6066 * - replace <from> delimiter by the <next> one unless <from> points to a
6067 * colon, in which case <next> is simply removed
6068 * - set exactly one space character after the new first delimiter, unless
6069 * there are not enough characters in the block being moved to do so.
6070 * - remove unneeded spaces before the previous delimiter and after the new
6071 * one.
6072 *
6073 * It is the caller's responsibility to ensure that :
6074 * - <from> points to a valid delimiter or the colon ;
6075 * - <next> points to a valid delimiter or the final CR/LF ;
6076 * - there are non-space chars before <from> ;
6077 * - there is a CR/LF at or after <next>.
6078 */
6079int del_hdr_value(struct buffer *buf, char **from, char *next)
6080{
6081 char *prev = *from;
6082
6083 if (*prev == ':') {
6084 /* We're removing the first value, preserve the colon and add a
6085 * space if possible.
6086 */
6087 if (!http_is_crlf[(unsigned char)*next])
6088 next++;
6089 prev++;
6090 if (prev < next)
6091 *prev++ = ' ';
6092
6093 while (http_is_spht[(unsigned char)*next])
6094 next++;
6095 } else {
6096 /* Remove useless spaces before the old delimiter. */
6097 while (http_is_spht[(unsigned char)*(prev-1)])
6098 prev--;
6099 *from = prev;
6100
6101 /* copy the delimiter and if possible a space if we're
6102 * not at the end of the line.
6103 */
6104 if (!http_is_crlf[(unsigned char)*next]) {
6105 *prev++ = *next++;
6106 if (prev + 1 < next)
6107 *prev++ = ' ';
6108 while (http_is_spht[(unsigned char)*next])
6109 next++;
6110 }
6111 }
6112 return buffer_replace2(buf, prev, next, NULL, 0);
6113}
6114
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006115/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006116 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006117 * desirable to call it only when needed. This code is quite complex because
6118 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6119 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006120 */
6121void manage_client_side_cookies(struct session *t, struct buffer *req)
6122{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006123 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006124 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006125 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006126 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6127 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006128
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006129 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006130 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006131 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006132
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006133 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006134 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006135 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006136
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006137 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006138 hdr_beg = hdr_next;
6139 hdr_end = hdr_beg + cur_hdr->len;
6140 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006141
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006142 /* We have one full header between hdr_beg and hdr_end, and the
6143 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006144 * "Cookie:" headers.
6145 */
6146
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006147 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006148 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006149 old_idx = cur_idx;
6150 continue;
6151 }
6152
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006153 del_from = NULL; /* nothing to be deleted */
6154 preserve_hdr = 0; /* assume we may kill the whole header */
6155
Willy Tarreau58f10d72006-12-04 02:26:12 +01006156 /* Now look for cookies. Conforming to RFC2109, we have to support
6157 * attributes whose name begin with a '$', and associate them with
6158 * the right cookie, if we want to delete this cookie.
6159 * So there are 3 cases for each cookie read :
6160 * 1) it's a special attribute, beginning with a '$' : ignore it.
6161 * 2) it's a server id cookie that we *MAY* want to delete : save
6162 * some pointers on it (last semi-colon, beginning of cookie...)
6163 * 3) it's an application cookie : we *MAY* have to delete a previous
6164 * "special" cookie.
6165 * At the end of loop, if a "special" cookie remains, we may have to
6166 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006167 * *MUST* delete it.
6168 *
6169 * Note: RFC2965 is unclear about the processing of spaces around
6170 * the equal sign in the ATTR=VALUE form. A careful inspection of
6171 * the RFC explicitly allows spaces before it, and not within the
6172 * tokens (attrs or values). An inspection of RFC2109 allows that
6173 * too but section 10.1.3 lets one think that spaces may be allowed
6174 * after the equal sign too, resulting in some (rare) buggy
6175 * implementations trying to do that. So let's do what servers do.
6176 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6177 * allowed quoted strings in values, with any possible character
6178 * after a backslash, including control chars and delimitors, which
6179 * causes parsing to become ambiguous. Browsers also allow spaces
6180 * within values even without quotes.
6181 *
6182 * We have to keep multiple pointers in order to support cookie
6183 * removal at the beginning, middle or end of header without
6184 * corrupting the header. All of these headers are valid :
6185 *
6186 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6187 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6188 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6189 * | | | | | | | | |
6190 * | | | | | | | | hdr_end <--+
6191 * | | | | | | | +--> next
6192 * | | | | | | +----> val_end
6193 * | | | | | +-----------> val_beg
6194 * | | | | +--------------> equal
6195 * | | | +----------------> att_end
6196 * | | +---------------------> att_beg
6197 * | +--------------------------> prev
6198 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006199 */
6200
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006201 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6202 /* Iterate through all cookies on this line */
6203
6204 /* find att_beg */
6205 att_beg = prev + 1;
6206 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6207 att_beg++;
6208
6209 /* find att_end : this is the first character after the last non
6210 * space before the equal. It may be equal to hdr_end.
6211 */
6212 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006213
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006214 while (equal < hdr_end) {
6215 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006216 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006217 if (http_is_spht[(unsigned char)*equal++])
6218 continue;
6219 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006220 }
6221
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006222 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6223 * is between <att_beg> and <equal>, both may be identical.
6224 */
6225
6226 /* look for end of cookie if there is an equal sign */
6227 if (equal < hdr_end && *equal == '=') {
6228 /* look for the beginning of the value */
6229 val_beg = equal + 1;
6230 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6231 val_beg++;
6232
6233 /* find the end of the value, respecting quotes */
6234 next = find_cookie_value_end(val_beg, hdr_end);
6235
6236 /* make val_end point to the first white space or delimitor after the value */
6237 val_end = next;
6238 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6239 val_end--;
6240 } else {
6241 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006242 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006243
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006244 /* We have nothing to do with attributes beginning with '$'. However,
6245 * they will automatically be removed if a header before them is removed,
6246 * since they're supposed to be linked together.
6247 */
6248 if (*att_beg == '$')
6249 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006250
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006251 /* Ignore cookies with no equal sign */
6252 if (equal == next) {
6253 /* This is not our cookie, so we must preserve it. But if we already
6254 * scheduled another cookie for removal, we cannot remove the
6255 * complete header, but we can remove the previous block itself.
6256 */
6257 preserve_hdr = 1;
6258 if (del_from != NULL) {
6259 int delta = del_hdr_value(req, &del_from, prev);
6260 val_end += delta;
6261 next += delta;
6262 hdr_end += delta;
6263 hdr_next += delta;
6264 cur_hdr->len += delta;
6265 http_msg_move_end(&txn->req, delta);
6266 prev = del_from;
6267 del_from = NULL;
6268 }
6269 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006270 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006271
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006272 /* if there are spaces around the equal sign, we need to
6273 * strip them otherwise we'll get trouble for cookie captures,
6274 * or even for rewrites. Since this happens extremely rarely,
6275 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006276 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006277 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6278 int stripped_before = 0;
6279 int stripped_after = 0;
6280
6281 if (att_end != equal) {
6282 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6283 equal += stripped_before;
6284 val_beg += stripped_before;
6285 }
6286
6287 if (val_beg > equal + 1) {
6288 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6289 val_beg += stripped_after;
6290 stripped_before += stripped_after;
6291 }
6292
6293 val_end += stripped_before;
6294 next += stripped_before;
6295 hdr_end += stripped_before;
6296 hdr_next += stripped_before;
6297 cur_hdr->len += stripped_before;
6298 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006299 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006300 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006301
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006302 /* First, let's see if we want to capture this cookie. We check
6303 * that we don't already have a client side cookie, because we
6304 * can only capture one. Also as an optimisation, we ignore
6305 * cookies shorter than the declared name.
6306 */
6307 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6308 (val_end - att_beg >= t->fe->capture_namelen) &&
6309 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6310 int log_len = val_end - att_beg;
6311
6312 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6313 Alert("HTTP logging : out of memory.\n");
6314 } else {
6315 if (log_len > t->fe->capture_len)
6316 log_len = t->fe->capture_len;
6317 memcpy(txn->cli_cookie, att_beg, log_len);
6318 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006319 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006320 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006321
Willy Tarreaubca99692010-10-06 19:25:55 +02006322 /* Persistence cookies in passive, rewrite or insert mode have the
6323 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006324 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006325 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006326 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006327 * For cookies in prefix mode, the form is :
6328 *
6329 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006330 */
6331 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6332 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6333 struct server *srv = t->be->srv;
6334 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006335
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006336 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6337 * have the server ID between val_beg and delim, and the original cookie between
6338 * delim+1 and val_end. Otherwise, delim==val_end :
6339 *
6340 * Cookie: NAME=SRV; # in all but prefix modes
6341 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6342 * | || || | |+-> next
6343 * | || || | +--> val_end
6344 * | || || +---------> delim
6345 * | || |+------------> val_beg
6346 * | || +-------------> att_end = equal
6347 * | |+-----------------> att_beg
6348 * | +------------------> prev
6349 * +-------------------------> hdr_beg
6350 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006351
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006352 if (t->be->options & PR_O_COOK_PFX) {
6353 for (delim = val_beg; delim < val_end; delim++)
6354 if (*delim == COOKIE_DELIM)
6355 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006356 } else {
6357 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006358 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006359 /* Now check if the cookie contains a date field, which would
6360 * appear after a vertical bar ('|') just after the server name
6361 * and before the delimiter.
6362 */
6363 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6364 if (vbar1) {
6365 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006366 * right is the last seen date. It is a base64 encoded
6367 * 30-bit value representing the UNIX date since the
6368 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006369 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006370 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006371 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006372 if (val_end - vbar1 >= 5) {
6373 val = b64tos30(vbar1);
6374 if (val > 0)
6375 txn->cookie_last_date = val << 2;
6376 }
6377 /* look for a second vertical bar */
6378 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6379 if (vbar1 && (val_end - vbar1 > 5)) {
6380 val = b64tos30(vbar1 + 1);
6381 if (val > 0)
6382 txn->cookie_first_date = val << 2;
6383 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006384 }
6385 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006386
Willy Tarreauf64d1412010-10-07 20:06:11 +02006387 /* if the cookie has an expiration date and the proxy wants to check
6388 * it, then we do that now. We first check if the cookie is too old,
6389 * then only if it has expired. We detect strict overflow because the
6390 * time resolution here is not great (4 seconds). Cookies with dates
6391 * in the future are ignored if their offset is beyond one day. This
6392 * allows an admin to fix timezone issues without expiring everyone
6393 * and at the same time avoids keeping unwanted side effects for too
6394 * long.
6395 */
6396 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006397 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6398 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006399 txn->flags &= ~TX_CK_MASK;
6400 txn->flags |= TX_CK_OLD;
6401 delim = val_beg; // let's pretend we have not found the cookie
6402 txn->cookie_first_date = 0;
6403 txn->cookie_last_date = 0;
6404 }
6405 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006406 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6407 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006408 txn->flags &= ~TX_CK_MASK;
6409 txn->flags |= TX_CK_EXPIRED;
6410 delim = val_beg; // let's pretend we have not found the cookie
6411 txn->cookie_first_date = 0;
6412 txn->cookie_last_date = 0;
6413 }
6414
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006415 /* Here, we'll look for the first running server which supports the cookie.
6416 * This allows to share a same cookie between several servers, for example
6417 * to dedicate backup servers to specific servers only.
6418 * However, to prevent clients from sticking to cookie-less backup server
6419 * when they have incidentely learned an empty cookie, we simply ignore
6420 * empty cookies and mark them as invalid.
6421 * The same behaviour is applied when persistence must be ignored.
6422 */
6423 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6424 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006425
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006426 while (srv) {
6427 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6428 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6429 if ((srv->state & SRV_RUNNING) ||
6430 (t->be->options & PR_O_PERSIST) ||
6431 (t->flags & SN_FORCE_PRST)) {
6432 /* we found the server and we can use it */
6433 txn->flags &= ~TX_CK_MASK;
6434 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6435 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006436 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006437 break;
6438 } else {
6439 /* we found a server, but it's down,
6440 * mark it as such and go on in case
6441 * another one is available.
6442 */
6443 txn->flags &= ~TX_CK_MASK;
6444 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006445 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006446 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006447 srv = srv->next;
6448 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006449
Willy Tarreauf64d1412010-10-07 20:06:11 +02006450 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006451 /* no server matched this cookie */
6452 txn->flags &= ~TX_CK_MASK;
6453 txn->flags |= TX_CK_INVALID;
6454 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006455
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006456 /* depending on the cookie mode, we may have to either :
6457 * - delete the complete cookie if we're in insert+indirect mode, so that
6458 * the server never sees it ;
6459 * - remove the server id from the cookie value, and tag the cookie as an
6460 * application cookie so that it does not get accidentely removed later,
6461 * if we're in cookie prefix mode
6462 */
6463 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6464 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006465
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006466 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6467 val_end += delta;
6468 next += delta;
6469 hdr_end += delta;
6470 hdr_next += delta;
6471 cur_hdr->len += delta;
6472 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006473
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006474 del_from = NULL;
6475 preserve_hdr = 1; /* we want to keep this cookie */
6476 }
6477 else if (del_from == NULL &&
6478 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6479 del_from = prev;
6480 }
6481 } else {
6482 /* This is not our cookie, so we must preserve it. But if we already
6483 * scheduled another cookie for removal, we cannot remove the
6484 * complete header, but we can remove the previous block itself.
6485 */
6486 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006487
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006488 if (del_from != NULL) {
6489 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006490 if (att_beg >= del_from)
6491 att_beg += delta;
6492 if (att_end >= del_from)
6493 att_end += delta;
6494 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006495 val_end += delta;
6496 next += delta;
6497 hdr_end += delta;
6498 hdr_next += delta;
6499 cur_hdr->len += delta;
6500 http_msg_move_end(&txn->req, delta);
6501 prev = del_from;
6502 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006503 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006504 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006505
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006506 /* Look for the appsession cookie unless persistence must be ignored */
6507 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6508 int cmp_len, value_len;
6509 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006510
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006511 if (t->be->options2 & PR_O2_AS_PFX) {
6512 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6513 value_begin = att_beg + t->be->appsession_name_len;
6514 value_len = val_end - att_beg - t->be->appsession_name_len;
6515 } else {
6516 cmp_len = att_end - att_beg;
6517 value_begin = val_beg;
6518 value_len = val_end - val_beg;
6519 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006520
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006521 /* let's see if the cookie is our appcookie */
6522 if (cmp_len == t->be->appsession_name_len &&
6523 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6524 manage_client_side_appsession(t, value_begin, value_len);
6525 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006526 }
6527
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006528 /* continue with next cookie on this header line */
6529 att_beg = next;
6530 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006531
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006532 /* There are no more cookies on this line.
6533 * We may still have one (or several) marked for deletion at the
6534 * end of the line. We must do this now in two ways :
6535 * - if some cookies must be preserved, we only delete from the
6536 * mark to the end of line ;
6537 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006538 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006539 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006540 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006541 if (preserve_hdr) {
6542 delta = del_hdr_value(req, &del_from, hdr_end);
6543 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006544 cur_hdr->len += delta;
6545 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006546 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006547
6548 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006549 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6550 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006551 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006552 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006553 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006554 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006555 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006556 }
6557
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006558 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006559 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006560 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006561}
6562
6563
Willy Tarreaua15645d2007-03-18 16:22:39 +01006564/* Iterate the same filter through all response headers contained in <rtr>.
6565 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6566 */
6567int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6568{
6569 char term;
6570 char *cur_ptr, *cur_end, *cur_next;
6571 int cur_idx, old_idx, last_hdr;
6572 struct http_txn *txn = &t->txn;
6573 struct hdr_idx_elem *cur_hdr;
6574 int len, delta;
6575
6576 last_hdr = 0;
6577
Willy Tarreau962c3f42010-01-10 00:15:35 +01006578 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006579 old_idx = 0;
6580
6581 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006582 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006583 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006584 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006585 (exp->action == ACT_ALLOW ||
6586 exp->action == ACT_DENY))
6587 return 0;
6588
6589 cur_idx = txn->hdr_idx.v[old_idx].next;
6590 if (!cur_idx)
6591 break;
6592
6593 cur_hdr = &txn->hdr_idx.v[cur_idx];
6594 cur_ptr = cur_next;
6595 cur_end = cur_ptr + cur_hdr->len;
6596 cur_next = cur_end + cur_hdr->cr + 1;
6597
6598 /* Now we have one header between cur_ptr and cur_end,
6599 * and the next header starts at cur_next.
6600 */
6601
6602 /* The annoying part is that pattern matching needs
6603 * that we modify the contents to null-terminate all
6604 * strings before testing them.
6605 */
6606
6607 term = *cur_end;
6608 *cur_end = '\0';
6609
6610 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6611 switch (exp->action) {
6612 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006613 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006614 last_hdr = 1;
6615 break;
6616
6617 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006618 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006619 last_hdr = 1;
6620 break;
6621
6622 case ACT_REPLACE:
6623 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6624 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6625 /* FIXME: if the user adds a newline in the replacement, the
6626 * index will not be recalculated for now, and the new line
6627 * will not be counted as a new header.
6628 */
6629
6630 cur_end += delta;
6631 cur_next += delta;
6632 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006633 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006634 break;
6635
6636 case ACT_REMOVE:
6637 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6638 cur_next += delta;
6639
Willy Tarreaufa355d42009-11-29 18:12:29 +01006640 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6642 txn->hdr_idx.used--;
6643 cur_hdr->len = 0;
6644 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006645 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006646 break;
6647
6648 }
6649 }
6650 if (cur_end)
6651 *cur_end = term; /* restore the string terminator */
6652
6653 /* keep the link from this header to next one in case of later
6654 * removal of next header.
6655 */
6656 old_idx = cur_idx;
6657 }
6658 return 0;
6659}
6660
6661
6662/* Apply the filter to the status line in the response buffer <rtr>.
6663 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6664 * or -1 if a replacement resulted in an invalid status line.
6665 */
6666int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6667{
6668 char term;
6669 char *cur_ptr, *cur_end;
6670 int done;
6671 struct http_txn *txn = &t->txn;
6672 int len, delta;
6673
6674
Willy Tarreau3d300592007-03-18 18:34:41 +01006675 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006676 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006677 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006678 (exp->action == ACT_ALLOW ||
6679 exp->action == ACT_DENY))
6680 return 0;
6681 else if (exp->action == ACT_REMOVE)
6682 return 0;
6683
6684 done = 0;
6685
Willy Tarreau962c3f42010-01-10 00:15:35 +01006686 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006687 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006688
6689 /* Now we have the status line between cur_ptr and cur_end */
6690
6691 /* The annoying part is that pattern matching needs
6692 * that we modify the contents to null-terminate all
6693 * strings before testing them.
6694 */
6695
6696 term = *cur_end;
6697 *cur_end = '\0';
6698
6699 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6700 switch (exp->action) {
6701 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006702 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006703 done = 1;
6704 break;
6705
6706 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006707 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708 done = 1;
6709 break;
6710
6711 case ACT_REPLACE:
6712 *cur_end = term; /* restore the string terminator */
6713 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6714 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6715 /* FIXME: if the user adds a newline in the replacement, the
6716 * index will not be recalculated for now, and the new line
6717 * will not be counted as a new header.
6718 */
6719
Willy Tarreaufa355d42009-11-29 18:12:29 +01006720 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006721 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006722 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006723 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006724 cur_ptr, cur_end + 1,
6725 NULL, NULL);
6726 if (unlikely(!cur_end))
6727 return -1;
6728
6729 /* we have a full respnse and we know that we have either a CR
6730 * or an LF at <ptr>.
6731 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006732 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006733 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006734 /* there is no point trying this regex on headers */
6735 return 1;
6736 }
6737 }
6738 *cur_end = term; /* restore the string terminator */
6739 return done;
6740}
6741
6742
6743
6744/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006745 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006746 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6747 * unparsable response.
6748 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006749int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006750{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006751 struct http_txn *txn = &s->txn;
6752 struct hdr_exp *exp;
6753
6754 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006755 int ret;
6756
6757 /*
6758 * The interleaving of transformations and verdicts
6759 * makes it difficult to decide to continue or stop
6760 * the evaluation.
6761 */
6762
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006763 if (txn->flags & TX_SVDENY)
6764 break;
6765
Willy Tarreau3d300592007-03-18 18:34:41 +01006766 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006767 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6768 exp->action == ACT_PASS)) {
6769 exp = exp->next;
6770 continue;
6771 }
6772
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006773 /* if this filter had a condition, evaluate it now and skip to
6774 * next filter if the condition does not match.
6775 */
6776 if (exp->cond) {
6777 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6778 ret = acl_pass(ret);
6779 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6780 ret = !ret;
6781 if (!ret)
6782 continue;
6783 }
6784
Willy Tarreaua15645d2007-03-18 16:22:39 +01006785 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006786 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006787 if (unlikely(ret < 0))
6788 return -1;
6789
6790 if (likely(ret == 0)) {
6791 /* The filter did not match the response, it can be
6792 * iterated through all headers.
6793 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006794 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006795 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006796 }
6797 return 0;
6798}
6799
6800
Willy Tarreaua15645d2007-03-18 16:22:39 +01006801/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006802 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006803 * desirable to call it only when needed. This function is also used when we
6804 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006805 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006806void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006807{
6808 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006809 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006810 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006811 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006812 char *hdr_beg, *hdr_end, *hdr_next;
6813 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006814
Willy Tarreaua15645d2007-03-18 16:22:39 +01006815 /* Iterate through the headers.
6816 * we start with the start line.
6817 */
6818 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006819 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006820
6821 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6822 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006823 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006824
6825 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006826 hdr_beg = hdr_next;
6827 hdr_end = hdr_beg + cur_hdr->len;
6828 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006829
Willy Tarreau24581ba2010-08-31 22:39:35 +02006830 /* We have one full header between hdr_beg and hdr_end, and the
6831 * next header starts at hdr_next. We're only interested in
6832 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833 */
6834
Willy Tarreau24581ba2010-08-31 22:39:35 +02006835 is_cookie2 = 0;
6836 prev = hdr_beg + 10;
6837 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006838 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006839 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6840 if (!val) {
6841 old_idx = cur_idx;
6842 continue;
6843 }
6844 is_cookie2 = 1;
6845 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006846 }
6847
Willy Tarreau24581ba2010-08-31 22:39:35 +02006848 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6849 * <prev> points to the colon.
6850 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006851 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006852
Willy Tarreau24581ba2010-08-31 22:39:35 +02006853 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6854 * check-cache is enabled) and we are not interested in checking
6855 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006856 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006857 if (t->be->cookie_name == NULL &&
6858 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006859 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006860 return;
6861
Willy Tarreau24581ba2010-08-31 22:39:35 +02006862 /* OK so now we know we have to process this response cookie.
6863 * The format of the Set-Cookie header is slightly different
6864 * from the format of the Cookie header in that it does not
6865 * support the comma as a cookie delimiter (thus the header
6866 * cannot be folded) because the Expires attribute described in
6867 * the original Netscape's spec may contain an unquoted date
6868 * with a comma inside. We have to live with this because
6869 * many browsers don't support Max-Age and some browsers don't
6870 * support quoted strings. However the Set-Cookie2 header is
6871 * clean.
6872 *
6873 * We have to keep multiple pointers in order to support cookie
6874 * removal at the beginning, middle or end of header without
6875 * corrupting the header (in case of set-cookie2). A special
6876 * pointer, <scav> points to the beginning of the set-cookie-av
6877 * fields after the first semi-colon. The <next> pointer points
6878 * either to the end of line (set-cookie) or next unquoted comma
6879 * (set-cookie2). All of these headers are valid :
6880 *
6881 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6882 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6883 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6884 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6885 * | | | | | | | | | |
6886 * | | | | | | | | +-> next hdr_end <--+
6887 * | | | | | | | +------------> scav
6888 * | | | | | | +--------------> val_end
6889 * | | | | | +--------------------> val_beg
6890 * | | | | +----------------------> equal
6891 * | | | +------------------------> att_end
6892 * | | +----------------------------> att_beg
6893 * | +------------------------------> prev
6894 * +-----------------------------------------> hdr_beg
6895 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006896
Willy Tarreau24581ba2010-08-31 22:39:35 +02006897 for (; prev < hdr_end; prev = next) {
6898 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006899
Willy Tarreau24581ba2010-08-31 22:39:35 +02006900 /* find att_beg */
6901 att_beg = prev + 1;
6902 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6903 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006904
Willy Tarreau24581ba2010-08-31 22:39:35 +02006905 /* find att_end : this is the first character after the last non
6906 * space before the equal. It may be equal to hdr_end.
6907 */
6908 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006909
Willy Tarreau24581ba2010-08-31 22:39:35 +02006910 while (equal < hdr_end) {
6911 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6912 break;
6913 if (http_is_spht[(unsigned char)*equal++])
6914 continue;
6915 att_end = equal;
6916 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006917
Willy Tarreau24581ba2010-08-31 22:39:35 +02006918 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6919 * is between <att_beg> and <equal>, both may be identical.
6920 */
6921
6922 /* look for end of cookie if there is an equal sign */
6923 if (equal < hdr_end && *equal == '=') {
6924 /* look for the beginning of the value */
6925 val_beg = equal + 1;
6926 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6927 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006928
Willy Tarreau24581ba2010-08-31 22:39:35 +02006929 /* find the end of the value, respecting quotes */
6930 next = find_cookie_value_end(val_beg, hdr_end);
6931
6932 /* make val_end point to the first white space or delimitor after the value */
6933 val_end = next;
6934 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6935 val_end--;
6936 } else {
6937 /* <equal> points to next comma, semi-colon or EOL */
6938 val_beg = val_end = next = equal;
6939 }
6940
6941 if (next < hdr_end) {
6942 /* Set-Cookie2 supports multiple cookies, and <next> points to
6943 * a colon or semi-colon before the end. So skip all attr-value
6944 * pairs and look for the next comma. For Set-Cookie, since
6945 * commas are permitted in values, skip to the end.
6946 */
6947 if (is_cookie2)
6948 next = find_hdr_value_end(next, hdr_end);
6949 else
6950 next = hdr_end;
6951 }
6952
6953 /* Now everything is as on the diagram above */
6954
6955 /* Ignore cookies with no equal sign */
6956 if (equal == val_end)
6957 continue;
6958
6959 /* If there are spaces around the equal sign, we need to
6960 * strip them otherwise we'll get trouble for cookie captures,
6961 * or even for rewrites. Since this happens extremely rarely,
6962 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006963 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006964 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6965 int stripped_before = 0;
6966 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006967
Willy Tarreau24581ba2010-08-31 22:39:35 +02006968 if (att_end != equal) {
6969 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6970 equal += stripped_before;
6971 val_beg += stripped_before;
6972 }
6973
6974 if (val_beg > equal + 1) {
6975 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6976 val_beg += stripped_after;
6977 stripped_before += stripped_after;
6978 }
6979
6980 val_end += stripped_before;
6981 next += stripped_before;
6982 hdr_end += stripped_before;
6983 hdr_next += stripped_before;
6984 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006985 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006986 }
6987
6988 /* First, let's see if we want to capture this cookie. We check
6989 * that we don't already have a server side cookie, because we
6990 * can only capture one. Also as an optimisation, we ignore
6991 * cookies shorter than the declared name.
6992 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006993 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006994 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006995 (val_end - att_beg >= t->fe->capture_namelen) &&
6996 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6997 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006998 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006999 Alert("HTTP logging : out of memory.\n");
7000 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007001 else {
7002 if (log_len > t->fe->capture_len)
7003 log_len = t->fe->capture_len;
7004 memcpy(txn->srv_cookie, att_beg, log_len);
7005 txn->srv_cookie[log_len] = 0;
7006 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007007 }
7008
Willy Tarreau827aee92011-03-10 16:55:02 +01007009 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007010 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007011 if (!(t->flags & SN_IGNORE_PRST) &&
7012 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7013 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007014 /* assume passive cookie by default */
7015 txn->flags &= ~TX_SCK_MASK;
7016 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007017
7018 /* If the cookie is in insert mode on a known server, we'll delete
7019 * this occurrence because we'll insert another one later.
7020 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007021 * a direct access.
7022 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007023 if (t->be->options2 & PR_O2_COOK_PSV) {
7024 /* The "preserve" flag was set, we don't want to touch the
7025 * server's cookie.
7026 */
7027 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007028 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007029 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007030 /* this cookie must be deleted */
7031 if (*prev == ':' && next == hdr_end) {
7032 /* whole header */
7033 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
7034 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7035 txn->hdr_idx.used--;
7036 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007037 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007038 hdr_next += delta;
7039 http_msg_move_end(&txn->rsp, delta);
7040 /* note: while both invalid now, <next> and <hdr_end>
7041 * are still equal, so the for() will stop as expected.
7042 */
7043 } else {
7044 /* just remove the value */
7045 int delta = del_hdr_value(res, &prev, next);
7046 next = prev;
7047 hdr_end += delta;
7048 hdr_next += delta;
7049 cur_hdr->len += delta;
7050 http_msg_move_end(&txn->rsp, delta);
7051 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007052 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007053 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007054 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007055 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007056 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007057 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007058 * with this server since we know it.
7059 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007060 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
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 Tarreauf1348312010-10-07 15:54:11 +02007067 txn->flags &= ~TX_SCK_MASK;
7068 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007069 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007070 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007071 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007072 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007073 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007074 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007075 next += delta;
7076 hdr_end += delta;
7077 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007078 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007079 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007080
Willy Tarreau827aee92011-03-10 16:55:02 +01007081 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007082 txn->flags &= ~TX_SCK_MASK;
7083 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007084 }
7085 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007086 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7087 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007088 int cmp_len, value_len;
7089 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007090
Cyril Bontéb21570a2009-11-29 20:04:48 +01007091 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007092 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7093 value_begin = att_beg + t->be->appsession_name_len;
7094 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007095 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007096 cmp_len = att_end - att_beg;
7097 value_begin = val_beg;
7098 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007099 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007100
Cyril Bonté17530c32010-04-06 21:11:10 +02007101 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007102 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7103 /* free a possibly previously allocated memory */
7104 pool_free2(apools.sessid, txn->sessid);
7105
Cyril Bontéb21570a2009-11-29 20:04:48 +01007106 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007107 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007108 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7109 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7110 return;
7111 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007112 memcpy(txn->sessid, value_begin, value_len);
7113 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007114 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007115 }
7116 /* that's done for this cookie, check the next one on the same
7117 * line when next != hdr_end (only if is_cookie2).
7118 */
7119 }
7120 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007121 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007122 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007123
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007124 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007125 appsess *asession = NULL;
7126 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007127 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007128 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007129 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007130 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7131 Alert("Not enough Memory process_srv():asession:calloc().\n");
7132 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7133 return;
7134 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007135 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7136
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007137 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7138 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7139 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007140 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007141 return;
7142 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007143 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007144 asession->sessid[t->be->appsession_len] = 0;
7145
Willy Tarreau827aee92011-03-10 16:55:02 +01007146 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007147 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007148 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007149 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007150 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007151 return;
7152 }
7153 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01007154 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007155
7156 asession->request_count = 0;
7157 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7158 }
7159
7160 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7161 asession->request_count++;
7162 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007163}
7164
7165
Willy Tarreaua15645d2007-03-18 16:22:39 +01007166/*
7167 * Check if response is cacheable or not. Updates t->flags.
7168 */
7169void check_response_for_cacheability(struct session *t, struct buffer *rtr)
7170{
7171 struct http_txn *txn = &t->txn;
7172 char *p1, *p2;
7173
7174 char *cur_ptr, *cur_end, *cur_next;
7175 int cur_idx;
7176
Willy Tarreau5df51872007-11-25 16:20:08 +01007177 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007178 return;
7179
7180 /* Iterate through the headers.
7181 * we start with the start line.
7182 */
7183 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007184 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185
7186 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7187 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007188 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007189
7190 cur_hdr = &txn->hdr_idx.v[cur_idx];
7191 cur_ptr = cur_next;
7192 cur_end = cur_ptr + cur_hdr->len;
7193 cur_next = cur_end + cur_hdr->cr + 1;
7194
7195 /* We have one full header between cur_ptr and cur_end, and the
7196 * next header starts at cur_next. We're only interested in
7197 * "Cookie:" headers.
7198 */
7199
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007200 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7201 if (val) {
7202 if ((cur_end - (cur_ptr + val) >= 8) &&
7203 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7204 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7205 return;
7206 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007207 }
7208
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007209 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7210 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007211 continue;
7212
7213 /* OK, right now we know we have a cache-control header at cur_ptr */
7214
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007215 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007216
7217 if (p1 >= cur_end) /* no more info */
7218 continue;
7219
7220 /* p1 is at the beginning of the value */
7221 p2 = p1;
7222
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007223 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007224 p2++;
7225
7226 /* we have a complete value between p1 and p2 */
7227 if (p2 < cur_end && *p2 == '=') {
7228 /* we have something of the form no-cache="set-cookie" */
7229 if ((cur_end - p1 >= 21) &&
7230 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7231 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007232 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233 continue;
7234 }
7235
7236 /* OK, so we know that either p2 points to the end of string or to a comma */
7237 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7238 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7239 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7240 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007241 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242 return;
7243 }
7244
7245 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007246 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007247 continue;
7248 }
7249 }
7250}
7251
7252
Willy Tarreau58f10d72006-12-04 02:26:12 +01007253/*
7254 * Try to retrieve a known appsession in the URI, then the associated server.
7255 * If the server is found, it's assigned to the session.
7256 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007257void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007258{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007259 char *end_params, *first_param, *cur_param, *next_param;
7260 char separator;
7261 int value_len;
7262
7263 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007264
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007265 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007266 (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 +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 first_param = NULL;
7271 switch (mode) {
7272 case PR_O2_AS_M_PP:
7273 first_param = memchr(begin, ';', len);
7274 break;
7275 case PR_O2_AS_M_QS:
7276 first_param = memchr(begin, '?', len);
7277 break;
7278 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007279
Cyril Bontéb21570a2009-11-29 20:04:48 +01007280 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007281 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007282 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007283
Cyril Bontéb21570a2009-11-29 20:04:48 +01007284 switch (mode) {
7285 case PR_O2_AS_M_PP:
7286 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7287 end_params = (char *) begin + len;
7288 }
7289 separator = ';';
7290 break;
7291 case PR_O2_AS_M_QS:
7292 end_params = (char *) begin + len;
7293 separator = '&';
7294 break;
7295 default:
7296 /* unknown mode, shouldn't happen */
7297 return;
7298 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007299
Cyril Bontéb21570a2009-11-29 20:04:48 +01007300 cur_param = next_param = end_params;
7301 while (cur_param > first_param) {
7302 cur_param--;
7303 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7304 /* let's see if this is the appsession parameter */
7305 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7306 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7307 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7308 /* Cool... it's the right one */
7309 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7310 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7311 if (value_len > 0) {
7312 manage_client_side_appsession(t, cur_param, value_len);
7313 }
7314 break;
7315 }
7316 next_param = cur_param;
7317 }
7318 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007319#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007320 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007321 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007322#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007323}
7324
Willy Tarreaub2513902006-12-17 14:52:38 +01007325/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007326 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007327 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007328 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007329 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007330 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007331 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007332 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007333 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007334int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007335{
7336 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007337 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007338
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007339 if (!uri_auth)
7340 return 0;
7341
Cyril Bonté70be45d2010-10-12 00:14:35 +02007342 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007343 return 0;
7344
Willy Tarreau295a8372011-03-10 11:25:07 +01007345 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007347 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007348 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007349 return 0;
7350
Willy Tarreau962c3f42010-01-10 00:15:35 +01007351 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007352
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007353 /* the URI is in h */
7354 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007355 return 0;
7356
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007357 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007358 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007359 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007360 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007361 break;
7362 }
7363 h++;
7364 }
7365
7366 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007367 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7368 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007369 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007370 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007371 break;
7372 }
7373 h++;
7374 }
7375 }
7376
Willy Tarreau962c3f42010-01-10 00:15:35 +01007377 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7378 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007379 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007380 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007381 break;
7382 }
7383 h++;
7384 }
7385
Cyril Bonté70be45d2010-10-12 00:14:35 +02007386 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7387 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7388 if (memcmp(h, ";st=", 4) == 0) {
7389 h += 4;
7390
7391 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007392 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007393 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007394 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007395 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007396 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007397 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007398 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007399 else
Willy Tarreau295a8372011-03-10 11:25:07 +01007400 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007401 break;
7402 }
7403 h++;
7404 }
7405
Willy Tarreau295a8372011-03-10 11:25:07 +01007406 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007407
Willy Tarreaub2513902006-12-17 14:52:38 +01007408 return 1;
7409}
7410
Willy Tarreau4076a152009-04-02 15:18:36 +02007411/*
7412 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007413 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7414 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007415 */
7416void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7417 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007418 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007419{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007420 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7421 int len1 = buf->size - msg->som;
7422 es->len = buf->r - (buf->data + msg->som) + buf->size;
7423 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7424 if (es->len > len1 && len1 < sizeof(es->buf))
7425 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7426 }
7427 else {
7428 es->len = buf->r - (buf->data + msg->som);
7429 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7430 }
7431
Willy Tarreau4076a152009-04-02 15:18:36 +02007432 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007433 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007434 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007435 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007436 else
7437 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7438
Willy Tarreau4076a152009-04-02 15:18:36 +02007439 es->when = date; // user-visible date
7440 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007441 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007442 es->oe = other_end;
Willy Tarreau957c0a52011-03-03 17:42:23 +01007443 es->src = s->req->prod->addr.c.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007444 es->state = state;
7445 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007446 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007447}
Willy Tarreaub2513902006-12-17 14:52:38 +01007448
Willy Tarreaubce70882009-09-07 11:51:47 +02007449/* return the IP address pointed to by occurrence <occ> of header <hname> in
7450 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
7451 * occurrence number corresponding to this value is returned. If <occ> is
7452 * strictly negative, the occurrence number before the end corresponding to
7453 * this value is returned. If <occ> is null, any value is returned, so it is
7454 * not recommended to use it that way. Negative occurrences are limited to
7455 * a small value because it is required to keep them in memory while scanning.
7456 * IP address 0.0.0.0 is returned if no match is found.
7457 */
7458unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
7459{
7460 struct hdr_ctx ctx;
7461 unsigned int hdr_hist[MAX_HDR_HISTORY];
7462 unsigned int hist_ptr;
7463 int found = 0;
7464
7465 ctx.idx = 0;
7466 if (occ >= 0) {
7467 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7468 occ--;
7469 if (occ <= 0) {
7470 found = 1;
7471 break;
7472 }
7473 }
7474 if (!found)
7475 return 0;
7476 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7477 }
7478
7479 /* negative occurrence, we scan all the list then walk back */
7480 if (-occ > MAX_HDR_HISTORY)
7481 return 0;
7482
7483 hist_ptr = 0;
7484 hdr_hist[hist_ptr] = 0;
7485 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7486 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7487 if (hist_ptr >= MAX_HDR_HISTORY)
7488 hist_ptr = 0;
7489 found++;
7490 }
7491 if (-occ > found)
7492 return 0;
7493 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7494 * find occurrence -occ, so we have to check [hist_ptr+occ].
7495 */
7496 hist_ptr += occ;
7497 if (hist_ptr >= MAX_HDR_HISTORY)
7498 hist_ptr -= MAX_HDR_HISTORY;
7499 return hdr_hist[hist_ptr];
7500}
7501
Willy Tarreaubaaee002006-06-26 02:48:02 +02007502/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007503 * Print a debug line with a header
7504 */
7505void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7506{
7507 int len, max;
7508 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007509 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007510 max = end - start;
7511 UBOUND(max, sizeof(trash) - len - 1);
7512 len += strlcpy2(trash + len, start, max + 1);
7513 trash[len++] = '\n';
7514 write(1, trash, len);
7515}
7516
Willy Tarreau0937bc42009-12-22 15:03:09 +01007517/*
7518 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7519 * the required fields are properly allocated and that we only need to (re)init
7520 * them. This should be used before processing any new request.
7521 */
7522void http_init_txn(struct session *s)
7523{
7524 struct http_txn *txn = &s->txn;
7525 struct proxy *fe = s->fe;
7526
7527 txn->flags = 0;
7528 txn->status = -1;
7529
Willy Tarreauf64d1412010-10-07 20:06:11 +02007530 txn->cookie_first_date = 0;
7531 txn->cookie_last_date = 0;
7532
Willy Tarreau0937bc42009-12-22 15:03:09 +01007533 txn->req.sol = txn->req.eol = NULL;
7534 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7535 txn->rsp.sol = txn->rsp.eol = NULL;
7536 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007537 txn->req.chunk_len = 0LL;
7538 txn->req.body_len = 0LL;
7539 txn->rsp.chunk_len = 0LL;
7540 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007541 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7542 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007543
7544 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007545
7546 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7547 if (fe->options2 & PR_O2_REQBUG_OK)
7548 txn->req.err_pos = -1; /* let buggy requests pass */
7549
Willy Tarreau46023632010-01-07 22:51:47 +01007550 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007551 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7552
Willy Tarreau46023632010-01-07 22:51:47 +01007553 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007554 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7555
7556 if (txn->hdr_idx.v)
7557 hdr_idx_init(&txn->hdr_idx);
7558}
7559
7560/* to be used at the end of a transaction */
7561void http_end_txn(struct session *s)
7562{
7563 struct http_txn *txn = &s->txn;
7564
7565 /* these ones will have been dynamically allocated */
7566 pool_free2(pool2_requri, txn->uri);
7567 pool_free2(pool2_capture, txn->cli_cookie);
7568 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007569 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007570
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007571 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007572 txn->uri = NULL;
7573 txn->srv_cookie = NULL;
7574 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007575
7576 if (txn->req.cap) {
7577 struct cap_hdr *h;
7578 for (h = s->fe->req_cap; h; h = h->next)
7579 pool_free2(h->pool, txn->req.cap[h->index]);
7580 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7581 }
7582
7583 if (txn->rsp.cap) {
7584 struct cap_hdr *h;
7585 for (h = s->fe->rsp_cap; h; h = h->next)
7586 pool_free2(h->pool, txn->rsp.cap[h->index]);
7587 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7588 }
7589
Willy Tarreau0937bc42009-12-22 15:03:09 +01007590}
7591
7592/* to be used at the end of a transaction to prepare a new one */
7593void http_reset_txn(struct session *s)
7594{
7595 http_end_txn(s);
7596 http_init_txn(s);
7597
7598 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007599 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007600 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007601 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007602 /* re-init store persistence */
7603 s->store_count = 0;
7604
Willy Tarreau0937bc42009-12-22 15:03:09 +01007605 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007606
7607 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7608
Willy Tarreau739cfba2010-01-25 23:11:14 +01007609 /* We must trim any excess data from the response buffer, because we
7610 * may have blocked an invalid response from a server that we don't
7611 * want to accidentely forward once we disable the analysers, nor do
7612 * we want those data to come along with next response. A typical
7613 * example of such data would be from a buggy server responding to
7614 * a HEAD with some data, or sending more than the advertised
7615 * content-length.
7616 */
7617 if (unlikely(s->rep->l > s->rep->send_max)) {
7618 s->rep->l = s->rep->send_max;
7619 s->rep->r = s->rep->w + s->rep->l;
7620 if (s->rep->r >= s->rep->data + s->rep->size)
7621 s->rep->r -= s->rep->size;
7622 }
7623
Willy Tarreau0937bc42009-12-22 15:03:09 +01007624 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007625 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007626
Willy Tarreaud04e8582010-05-31 12:31:35 +02007627 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007628 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007629
7630 s->req->rex = TICK_ETERNITY;
7631 s->req->wex = TICK_ETERNITY;
7632 s->req->analyse_exp = TICK_ETERNITY;
7633 s->rep->rex = TICK_ETERNITY;
7634 s->rep->wex = TICK_ETERNITY;
7635 s->rep->analyse_exp = TICK_ETERNITY;
7636}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007637
Willy Tarreauff011f22011-01-06 17:51:27 +01007638void free_http_req_rules(struct list *r) {
7639 struct http_req_rule *tr, *pr;
7640
7641 list_for_each_entry_safe(pr, tr, r, list) {
7642 LIST_DEL(&pr->list);
7643 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7644 free(pr->http_auth.realm);
7645
7646 free(pr);
7647 }
7648}
7649
7650struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7651{
7652 struct http_req_rule *rule;
7653 int cur_arg;
7654
7655 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7656 if (!rule) {
7657 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7658 return NULL;
7659 }
7660
7661 if (!*args[0]) {
7662 goto req_error_parsing;
7663 } else if (!strcmp(args[0], "allow")) {
7664 rule->action = HTTP_REQ_ACT_ALLOW;
7665 cur_arg = 1;
7666 } else if (!strcmp(args[0], "deny")) {
7667 rule->action = HTTP_REQ_ACT_DENY;
7668 cur_arg = 1;
7669 } else if (!strcmp(args[0], "auth")) {
7670 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7671 cur_arg = 1;
7672
7673 while(*args[cur_arg]) {
7674 if (!strcmp(args[cur_arg], "realm")) {
7675 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7676 cur_arg+=2;
7677 continue;
7678 } else
7679 break;
7680 }
7681 } else {
7682req_error_parsing:
7683 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7684 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7685 return NULL;
7686 }
7687
7688 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7689 struct acl_cond *cond;
7690
7691 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7692 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7693 file, linenum, args[0]);
7694 return NULL;
7695 }
7696 rule->cond = cond;
7697 }
7698 else if (*args[cur_arg]) {
7699 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7700 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7701 file, linenum, args[0], args[cur_arg]);
7702 return NULL;
7703 }
7704
7705 return rule;
7706}
7707
Willy Tarreau8797c062007-05-07 00:55:35 +02007708/************************************************************************/
7709/* The code below is dedicated to ACL parsing and matching */
7710/************************************************************************/
7711
7712
7713
7714
7715/* 1. Check on METHOD
7716 * We use the pre-parsed method if it is known, and store its number as an
7717 * integer. If it is unknown, we use the pointer and the length.
7718 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007719static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007720{
7721 int len, meth;
7722
Willy Tarreauae8b7962007-06-09 23:10:04 +02007723 len = strlen(*text);
7724 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007725
7726 pattern->val.i = meth;
7727 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007728 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007729 if (!pattern->ptr.str)
7730 return 0;
7731 pattern->len = len;
7732 }
7733 return 1;
7734}
7735
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007736static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007737acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7738 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007739{
7740 int meth;
7741 struct http_txn *txn = l7;
7742
Willy Tarreaub6866442008-07-14 23:54:42 +02007743 if (!txn)
7744 return 0;
7745
Willy Tarreau655dce92009-11-08 13:10:58 +01007746 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007747 return 0;
7748
Willy Tarreau8797c062007-05-07 00:55:35 +02007749 meth = txn->meth;
7750 test->i = meth;
7751 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007752 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7753 /* ensure the indexes are not affected */
7754 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007755 test->len = txn->req.sl.rq.m_l;
7756 test->ptr = txn->req.sol;
7757 }
7758 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7759 return 1;
7760}
7761
7762static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7763{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007764 int icase;
7765
Willy Tarreau8797c062007-05-07 00:55:35 +02007766 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02007767 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007768
7769 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02007770 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007771
7772 /* Other method, we must compare the strings */
7773 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02007774 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007775
7776 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
7777 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
7778 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007779 return ACL_PAT_FAIL;
7780 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007781}
7782
7783/* 2. Check on Request/Status Version
7784 * We simply compare strings here.
7785 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007786static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007787{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007788 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007789 if (!pattern->ptr.str)
7790 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007791 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007792 return 1;
7793}
7794
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007795static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007796acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7797 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007798{
7799 struct http_txn *txn = l7;
7800 char *ptr;
7801 int len;
7802
Willy Tarreaub6866442008-07-14 23:54:42 +02007803 if (!txn)
7804 return 0;
7805
Willy Tarreau655dce92009-11-08 13:10:58 +01007806 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007807 return 0;
7808
Willy Tarreau8797c062007-05-07 00:55:35 +02007809 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007810 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007811
7812 while ((len-- > 0) && (*ptr++ != '/'));
7813 if (len <= 0)
7814 return 0;
7815
7816 test->ptr = ptr;
7817 test->len = len;
7818
7819 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7820 return 1;
7821}
7822
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007823static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007824acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7825 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007826{
7827 struct http_txn *txn = l7;
7828 char *ptr;
7829 int len;
7830
Willy Tarreaub6866442008-07-14 23:54:42 +02007831 if (!txn)
7832 return 0;
7833
Willy Tarreau655dce92009-11-08 13:10:58 +01007834 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007835 return 0;
7836
Willy Tarreau8797c062007-05-07 00:55:35 +02007837 len = txn->rsp.sl.st.v_l;
7838 ptr = txn->rsp.sol;
7839
7840 while ((len-- > 0) && (*ptr++ != '/'));
7841 if (len <= 0)
7842 return 0;
7843
7844 test->ptr = ptr;
7845 test->len = len;
7846
7847 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7848 return 1;
7849}
7850
7851/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007852static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007853acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7854 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007855{
7856 struct http_txn *txn = l7;
7857 char *ptr;
7858 int len;
7859
Willy Tarreaub6866442008-07-14 23:54:42 +02007860 if (!txn)
7861 return 0;
7862
Willy Tarreau655dce92009-11-08 13:10:58 +01007863 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007864 return 0;
7865
Willy Tarreau8797c062007-05-07 00:55:35 +02007866 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007867 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007868
7869 test->i = __strl2ui(ptr, len);
7870 test->flags = ACL_TEST_F_VOL_1ST;
7871 return 1;
7872}
7873
7874/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007875static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007876acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7877 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007878{
7879 struct http_txn *txn = l7;
7880
Willy Tarreaub6866442008-07-14 23:54:42 +02007881 if (!txn)
7882 return 0;
7883
Willy Tarreau655dce92009-11-08 13:10:58 +01007884 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007885 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007886
Willy Tarreauc11416f2007-06-17 16:58:38 +02007887 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7888 /* ensure the indexes are not affected */
7889 return 0;
7890
Willy Tarreau8797c062007-05-07 00:55:35 +02007891 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007892 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007893
Willy Tarreauf3d25982007-05-08 22:45:09 +02007894 /* we do not need to set READ_ONLY because the data is in a buffer */
7895 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007896 return 1;
7897}
7898
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007899static int
7900acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7901 struct acl_expr *expr, struct acl_test *test)
7902{
7903 struct http_txn *txn = l7;
7904
Willy Tarreaub6866442008-07-14 23:54:42 +02007905 if (!txn)
7906 return 0;
7907
Willy Tarreau655dce92009-11-08 13:10:58 +01007908 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007909 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007910
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007911 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7912 /* ensure the indexes are not affected */
7913 return 0;
7914
7915 /* Parse HTTP request */
Willy Tarreau957c0a52011-03-03 17:42:23 +01007916 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to);
7917 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007918 test->i = AF_INET;
7919
7920 /*
7921 * If we are parsing url in frontend space, we prepare backend stage
7922 * to not parse again the same url ! optimization lazyness...
7923 */
7924 if (px->options & PR_O_HTTP_PROXY)
7925 l4->flags |= SN_ADDR_SET;
7926
7927 test->flags = ACL_TEST_F_READ_ONLY;
7928 return 1;
7929}
7930
7931static int
7932acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7933 struct acl_expr *expr, struct acl_test *test)
7934{
7935 struct http_txn *txn = l7;
7936
Willy Tarreaub6866442008-07-14 23:54:42 +02007937 if (!txn)
7938 return 0;
7939
Willy Tarreau655dce92009-11-08 13:10:58 +01007940 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007941 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007942
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007943 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7944 /* ensure the indexes are not affected */
7945 return 0;
7946
7947 /* Same optimization as url_ip */
Willy Tarreau957c0a52011-03-03 17:42:23 +01007948 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to);
7949 test->i = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007950
7951 if (px->options & PR_O_HTTP_PROXY)
7952 l4->flags |= SN_ADDR_SET;
7953
7954 test->flags = ACL_TEST_F_READ_ONLY;
7955 return 1;
7956}
7957
Willy Tarreauc11416f2007-06-17 16:58:38 +02007958/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7959 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7960 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007961static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007962acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007963 struct acl_expr *expr, struct acl_test *test)
7964{
7965 struct http_txn *txn = l7;
7966 struct hdr_idx *idx = &txn->hdr_idx;
7967 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007968
Willy Tarreaub6866442008-07-14 23:54:42 +02007969 if (!txn)
7970 return 0;
7971
Willy Tarreau33a7e692007-06-10 19:45:56 +02007972 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7973 /* search for header from the beginning */
7974 ctx->idx = 0;
7975
Willy Tarreau33a7e692007-06-10 19:45:56 +02007976 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7977 test->flags |= ACL_TEST_F_FETCH_MORE;
7978 test->flags |= ACL_TEST_F_VOL_HDR;
7979 test->len = ctx->vlen;
7980 test->ptr = (char *)ctx->line + ctx->val;
7981 return 1;
7982 }
7983
7984 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7985 test->flags |= ACL_TEST_F_VOL_HDR;
7986 return 0;
7987}
7988
Willy Tarreau33a7e692007-06-10 19:45:56 +02007989static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007990acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7991 struct acl_expr *expr, struct acl_test *test)
7992{
7993 struct http_txn *txn = l7;
7994
Willy Tarreaub6866442008-07-14 23:54:42 +02007995 if (!txn)
7996 return 0;
7997
Willy Tarreau655dce92009-11-08 13:10:58 +01007998 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007999 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008000
Willy Tarreauc11416f2007-06-17 16:58:38 +02008001 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8002 /* ensure the indexes are not affected */
8003 return 0;
8004
8005 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
8006}
8007
8008static int
8009acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
8010 struct acl_expr *expr, struct acl_test *test)
8011{
8012 struct http_txn *txn = l7;
8013
Willy Tarreaub6866442008-07-14 23:54:42 +02008014 if (!txn)
8015 return 0;
8016
Willy Tarreau655dce92009-11-08 13:10:58 +01008017 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008018 return 0;
8019
8020 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
8021}
8022
8023/* 6. Check on HTTP header count. The number of occurrences is returned.
8024 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8025 */
8026static int
8027acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008028 struct acl_expr *expr, struct acl_test *test)
8029{
8030 struct http_txn *txn = l7;
8031 struct hdr_idx *idx = &txn->hdr_idx;
8032 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008033 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008034
Willy Tarreaub6866442008-07-14 23:54:42 +02008035 if (!txn)
8036 return 0;
8037
Willy Tarreau33a7e692007-06-10 19:45:56 +02008038 ctx.idx = 0;
8039 cnt = 0;
8040 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
8041 cnt++;
8042
8043 test->i = cnt;
8044 test->flags = ACL_TEST_F_VOL_HDR;
8045 return 1;
8046}
8047
Willy Tarreauc11416f2007-06-17 16:58:38 +02008048static int
8049acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8050 struct acl_expr *expr, struct acl_test *test)
8051{
8052 struct http_txn *txn = l7;
8053
Willy Tarreaub6866442008-07-14 23:54:42 +02008054 if (!txn)
8055 return 0;
8056
Willy Tarreau655dce92009-11-08 13:10:58 +01008057 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008058 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008059
Willy Tarreauc11416f2007-06-17 16:58:38 +02008060 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8061 /* ensure the indexes are not affected */
8062 return 0;
8063
8064 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
8065}
8066
8067static int
8068acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8069 struct acl_expr *expr, struct acl_test *test)
8070{
8071 struct http_txn *txn = l7;
8072
Willy Tarreaub6866442008-07-14 23:54:42 +02008073 if (!txn)
8074 return 0;
8075
Willy Tarreau655dce92009-11-08 13:10:58 +01008076 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008077 return 0;
8078
8079 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
8080}
8081
Willy Tarreau33a7e692007-06-10 19:45:56 +02008082/* 7. Check on HTTP header's integer value. The integer value is returned.
8083 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008084 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008085 */
8086static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02008087acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008088 struct acl_expr *expr, struct acl_test *test)
8089{
8090 struct http_txn *txn = l7;
8091 struct hdr_idx *idx = &txn->hdr_idx;
8092 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008093
Willy Tarreaub6866442008-07-14 23:54:42 +02008094 if (!txn)
8095 return 0;
8096
Willy Tarreau33a7e692007-06-10 19:45:56 +02008097 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8098 /* search for header from the beginning */
8099 ctx->idx = 0;
8100
Willy Tarreau33a7e692007-06-10 19:45:56 +02008101 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8102 test->flags |= ACL_TEST_F_FETCH_MORE;
8103 test->flags |= ACL_TEST_F_VOL_HDR;
8104 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
8105 return 1;
8106 }
8107
8108 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8109 test->flags |= ACL_TEST_F_VOL_HDR;
8110 return 0;
8111}
8112
Willy Tarreauc11416f2007-06-17 16:58:38 +02008113static int
8114acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8115 struct acl_expr *expr, struct acl_test *test)
8116{
8117 struct http_txn *txn = l7;
8118
Willy Tarreaub6866442008-07-14 23:54:42 +02008119 if (!txn)
8120 return 0;
8121
Willy Tarreau655dce92009-11-08 13:10:58 +01008122 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008123 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008124
Willy Tarreauc11416f2007-06-17 16:58:38 +02008125 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8126 /* ensure the indexes are not affected */
8127 return 0;
8128
8129 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
8130}
8131
8132static int
8133acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8134 struct acl_expr *expr, struct acl_test *test)
8135{
8136 struct http_txn *txn = l7;
8137
Willy Tarreaub6866442008-07-14 23:54:42 +02008138 if (!txn)
8139 return 0;
8140
Willy Tarreau655dce92009-11-08 13:10:58 +01008141 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008142 return 0;
8143
8144 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
8145}
8146
Willy Tarreau106f9792009-09-19 07:54:16 +02008147/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
8148 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8149 */
8150static int
8151acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
8152 struct acl_expr *expr, struct acl_test *test)
8153{
8154 struct http_txn *txn = l7;
8155 struct hdr_idx *idx = &txn->hdr_idx;
8156 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
8157
8158 if (!txn)
8159 return 0;
8160
8161 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8162 /* search for header from the beginning */
8163 ctx->idx = 0;
8164
8165 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8166 test->flags |= ACL_TEST_F_FETCH_MORE;
8167 test->flags |= ACL_TEST_F_VOL_HDR;
8168 /* Same optimization as url_ip */
David du Colombier6f5ccb12011-03-10 22:26:24 +01008169 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));
8170 url2ipv4((char *)ctx->line + ctx->val, &((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr);
8171 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr;
Willy Tarreau106f9792009-09-19 07:54:16 +02008172 test->i = AF_INET;
8173 return 1;
8174 }
8175
8176 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8177 test->flags |= ACL_TEST_F_VOL_HDR;
8178 return 0;
8179}
8180
8181static int
8182acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8183 struct acl_expr *expr, struct acl_test *test)
8184{
8185 struct http_txn *txn = l7;
8186
8187 if (!txn)
8188 return 0;
8189
Willy Tarreau655dce92009-11-08 13:10:58 +01008190 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008191 return 0;
8192
8193 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8194 /* ensure the indexes are not affected */
8195 return 0;
8196
8197 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8198}
8199
8200static int
8201acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8202 struct acl_expr *expr, struct acl_test *test)
8203{
8204 struct http_txn *txn = l7;
8205
8206 if (!txn)
8207 return 0;
8208
Willy Tarreau655dce92009-11-08 13:10:58 +01008209 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008210 return 0;
8211
8212 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8213}
8214
Willy Tarreau737b0c12007-06-10 21:28:46 +02008215/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8216 * the first '/' after the possible hostname, and ends before the possible '?'.
8217 */
8218static int
8219acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8220 struct acl_expr *expr, struct acl_test *test)
8221{
8222 struct http_txn *txn = l7;
8223 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008224
Willy Tarreaub6866442008-07-14 23:54:42 +02008225 if (!txn)
8226 return 0;
8227
Willy Tarreau655dce92009-11-08 13:10:58 +01008228 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008229 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008230
Willy Tarreauc11416f2007-06-17 16:58:38 +02008231 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8232 /* ensure the indexes are not affected */
8233 return 0;
8234
Willy Tarreau962c3f42010-01-10 00:15:35 +01008235 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008236 ptr = http_get_path(txn);
8237 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008238 return 0;
8239
8240 /* OK, we got the '/' ! */
8241 test->ptr = ptr;
8242
8243 while (ptr < end && *ptr != '?')
8244 ptr++;
8245
8246 test->len = ptr - test->ptr;
8247
8248 /* we do not need to set READ_ONLY because the data is in a buffer */
8249 test->flags = ACL_TEST_F_VOL_1ST;
8250 return 1;
8251}
8252
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008253static int
8254acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8255 struct acl_expr *expr, struct acl_test *test)
8256{
8257 struct buffer *req = s->req;
8258 struct http_txn *txn = &s->txn;
8259 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008260
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008261 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8262 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8263 */
8264
8265 if (!s || !req)
8266 return 0;
8267
Willy Tarreau655dce92009-11-08 13:10:58 +01008268 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008269 /* Already decoded as OK */
8270 test->flags |= ACL_TEST_F_SET_RES_PASS;
8271 return 1;
8272 }
8273
8274 /* Try to decode HTTP request */
8275 if (likely(req->lr < req->r))
8276 http_msg_analyzer(req, msg, &txn->hdr_idx);
8277
Willy Tarreau655dce92009-11-08 13:10:58 +01008278 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008279 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8280 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8281 return 1;
8282 }
8283 /* wait for final state */
8284 test->flags |= ACL_TEST_F_MAY_CHANGE;
8285 return 0;
8286 }
8287
8288 /* OK we got a valid HTTP request. We have some minor preparation to
8289 * perform so that further checks can rely on HTTP tests.
8290 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008291 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008292 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8293 s->flags |= SN_REDIRECTABLE;
8294
8295 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8296 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8297 return 1;
8298 }
8299
8300 test->flags |= ACL_TEST_F_SET_RES_PASS;
8301 return 1;
8302}
8303
Willy Tarreau7f18e522010-10-22 20:04:13 +02008304/* return a valid test if the current request is the first one on the connection */
8305static int
8306acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8307 struct acl_expr *expr, struct acl_test *test)
8308{
8309 if (!s)
8310 return 0;
8311
8312 if (s->txn.flags & TX_NOT_FIRST)
8313 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8314 else
8315 test->flags |= ACL_TEST_F_SET_RES_PASS;
8316
8317 return 1;
8318}
8319
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008320static int
8321acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8322 struct acl_expr *expr, struct acl_test *test)
8323{
8324
8325 if (!s)
8326 return 0;
8327
8328 if (!get_http_auth(s))
8329 return 0;
8330
8331 test->ctx.a[0] = expr->arg.ul;
8332 test->ctx.a[1] = s->txn.auth.user;
8333 test->ctx.a[2] = s->txn.auth.pass;
8334
8335 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8336
8337 return 1;
8338}
Willy Tarreau8797c062007-05-07 00:55:35 +02008339
8340/************************************************************************/
8341/* All supported keywords must be declared here. */
8342/************************************************************************/
8343
8344/* Note: must not be declared <const> as its list will be overwritten */
8345static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008346 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8347
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008348 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008349 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8350 { "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 +02008351 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008352
Willy Tarreauc4262962010-05-10 23:42:40 +02008353 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008354 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8355 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8356 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8357 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8358 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8359 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008360 { "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 +02008361 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008362
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008363 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008364 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008365 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8366 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8367 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8368 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8369 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8370 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8371 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
8372 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008373 { "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 +02008374
Willy Tarreauc4262962010-05-10 23:42:40 +02008375 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008376 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8377 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8378 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8379 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8380 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8381 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8382 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
8383 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008384 { "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 +02008385
Willy Tarreauc4262962010-05-10 23:42:40 +02008386 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008387 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8388 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8389 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8390 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8391 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8392 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008393
Willy Tarreauf3d25982007-05-08 22:45:09 +02008394#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008395 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8396 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8397 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8398 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8399 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8400 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8401 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8402
Willy Tarreau8797c062007-05-07 00:55:35 +02008403 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8404 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8405 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8406 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8407 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8408 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8409 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8410 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008411#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008412
Willy Tarreau7f18e522010-10-22 20:04:13 +02008413 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8414 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8415 { "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 +02008416 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008417}};
8418
Willy Tarreau4a568972010-05-12 08:08:50 +02008419/************************************************************************/
8420/* The code below is dedicated to pattern fetching and matching */
8421/************************************************************************/
8422
8423/* extract the IP address from the last occurrence of specified header. Note
8424 * that we should normally first extract the string then convert it to IP,
8425 * but right now we have all the functions to do this seemlessly, and we will
8426 * be able to change that later without touching the configuration.
8427 */
8428static int
8429pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02008430 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008431{
8432 struct http_txn *txn = l7;
8433
Emeric Brun485479d2010-09-23 18:02:19 +02008434 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 +02008435 return data->ip.s_addr != 0;
8436}
8437
David Cournapeau16023ee2010-12-23 20:55:41 +09008438/*
8439 * Given a path string and its length, find the position of beginning of the
8440 * query string. Returns NULL if no query string is found in the path.
8441 *
8442 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8443 *
8444 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8445 */
8446static inline char *find_query_string(char *path, size_t path_l)
8447{
8448 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008449
David Cournapeau16023ee2010-12-23 20:55:41 +09008450 p = memchr(path, '?', path_l);
8451 return p ? p + 1 : NULL;
8452}
8453
8454static inline int is_param_delimiter(char c)
8455{
8456 return c == '&' || c == ';';
8457}
8458
8459/*
8460 * Given a url parameter, find the starting position of the first occurence,
8461 * or NULL if the parameter is not found.
8462 *
8463 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8464 * the function will return query_string+8.
8465 */
8466static char*
8467find_url_param_pos(char* query_string, size_t query_string_l,
8468 char* url_param_name, size_t url_param_name_l)
8469{
8470 char *pos, *last;
8471
8472 pos = query_string;
8473 last = query_string + query_string_l - url_param_name_l - 1;
8474
8475 while (pos <= last) {
8476 if (pos[url_param_name_l] == '=') {
8477 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8478 return pos;
8479 pos += url_param_name_l + 1;
8480 }
8481 while (pos <= last && !is_param_delimiter(*pos))
8482 pos++;
8483 pos++;
8484 }
8485 return NULL;
8486}
8487
8488/*
8489 * Given a url parameter name, returns its value and size into *value and
8490 * *value_l respectively, and returns non-zero. If the parameter is not found,
8491 * zero is returned and value/value_l are not touched.
8492 */
8493static int
8494find_url_param_value(char* path, size_t path_l,
8495 char* url_param_name, size_t url_param_name_l,
8496 char** value, size_t* value_l)
8497{
8498 char *query_string, *qs_end;
8499 char *arg_start;
8500 char *value_start, *value_end;
8501
8502 query_string = find_query_string(path, path_l);
8503 if (!query_string)
8504 return 0;
8505
8506 qs_end = path + path_l;
8507 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8508 url_param_name, url_param_name_l);
8509 if (!arg_start)
8510 return 0;
8511
8512 value_start = arg_start + url_param_name_l + 1;
8513 value_end = value_start;
8514
8515 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8516 value_end++;
8517
8518 *value = value_start;
8519 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008520 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008521}
8522
8523static int
8524pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8525 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8526{
8527 struct http_txn *txn = l7;
8528 struct http_msg *msg = &txn->req;
8529 char *url_param_value;
8530 size_t url_param_value_l;
8531
8532 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8533 arg_p->data.str.str, arg_p->data.str.len,
8534 &url_param_value, &url_param_value_l))
8535 return 0;
8536
8537 data->str.str = url_param_value;
8538 data->str.len = url_param_value_l;
8539 return 1;
8540}
8541
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008542/* Try to find the last occurrence of a cookie name in a cookie header value.
8543 * The pointer and size of the last occurrence of the cookie value is returned
8544 * into *value and *value_l, and the function returns non-zero if the value was
8545 * found. Otherwise if the cookie was not found, zero is returned and neither
8546 * value nor value_l are touched. The input hdr string should begin at the
8547 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8548 * value may represent a list of values (cookie headers).
8549 */
8550static int
8551extract_cookie_value(char *hdr, size_t hdr_l,
8552 char *cookie_name, size_t cookie_name_l, int list,
8553 char **value, size_t *value_l)
8554{
8555 int found = 0;
8556 char *equal, *att_end, *att_beg, *hdr_end, *val_beg, *val_end;
8557 char *next;
8558
8559 /* Note that multiple cookies may be delimited with semi-colons, so we
8560 * also have to loop on this.
8561 */
8562
8563 /* we search at least a cookie name followed by an equal, and more
8564 * generally something like this :
8565 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8566 */
8567 if (hdr_l < cookie_name_l + 1)
8568 return 0;
8569
8570 hdr_end = hdr + hdr_l;
8571
8572 for (att_beg = hdr; att_beg < hdr_end; att_beg = next + 1) {
8573 /* Iterate through all cookies on this line */
8574
8575 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8576 att_beg++;
8577
8578 /* find att_end : this is the first character after the last non
8579 * space before the equal. It may be equal to hdr_end.
8580 */
8581 equal = att_end = att_beg;
8582
8583 while (equal < hdr_end) {
8584 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8585 break;
8586 if (http_is_spht[(unsigned char)*equal++])
8587 continue;
8588 att_end = equal;
8589 }
8590
8591 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8592 * is between <att_beg> and <equal>, both may be identical.
8593 */
8594
8595 /* look for end of cookie if there is an equal sign */
8596 if (equal < hdr_end && *equal == '=') {
8597 /* look for the beginning of the value */
8598 val_beg = equal + 1;
8599 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8600 val_beg++;
8601
8602 /* find the end of the value, respecting quotes */
8603 next = find_cookie_value_end(val_beg, hdr_end);
8604
8605 /* make val_end point to the first white space or delimitor after the value */
8606 val_end = next;
8607 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8608 val_end--;
8609 } else {
8610 val_beg = val_end = next = equal;
8611 }
8612
8613 /* We have nothing to do with attributes beginning with '$'. However,
8614 * they will automatically be removed if a header before them is removed,
8615 * since they're supposed to be linked together.
8616 */
8617 if (*att_beg == '$')
8618 continue;
8619
8620 /* Ignore cookies with no equal sign */
8621 if (equal == next)
8622 continue;
8623
8624 /* Now we have the cookie name between att_beg and att_end, and
8625 * its value between val_beg and val_end.
8626 */
8627
8628 if (att_end - att_beg == cookie_name_l &&
8629 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8630 found = 1;
8631 *value = val_beg;
8632 *value_l = val_end - val_beg;
8633 /* right now we want to catch the last occurrence
8634 * of the cookie, so let's go on searching.
8635 */
8636 }
8637
8638 /* Set-Cookie headers only have the name in the first attr=value part */
8639 if (!list)
8640 break;
8641 }
8642
8643 return found;
8644}
8645
8646/* Try to find in request or response message is in <msg> and whose transaction
8647 * is in <txn> the last occurrence of a cookie name in all cookie header values
8648 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8649 * pointer and size of the last occurrence of the cookie value is returned into
8650 * <value> and <value_l>, and the function returns non-zero if the value was
8651 * found. Otherwise if the cookie was not found, zero is returned and neither
8652 * value nor value_l are touched. The input hdr string should begin at the
8653 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8654 * value may represent a list of values (cookie headers).
8655 */
8656
8657static int
8658find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8659 const char *hdr_name, int hdr_name_len,
8660 char *cookie_name, size_t cookie_name_l, int list,
8661 char **value, size_t *value_l)
8662{
8663 struct hdr_ctx ctx;
8664 int found = 0;
8665
8666 ctx.idx = 0;
8667 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
8668 if (ctx.vlen < cookie_name_l + 1)
8669 continue;
8670
8671 found |= extract_cookie_value(ctx.line + ctx.val, ctx.vlen,
8672 cookie_name, cookie_name_l, 1,
8673 value, value_l);
8674 }
8675 return found;
8676}
8677
8678static int
8679pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8680 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8681{
8682 struct http_txn *txn = l7;
8683 struct http_msg *msg = &txn->req;
8684 char *cookie_value;
8685 size_t cookie_value_l;
8686 int found = 0;
8687
8688 found = find_cookie_value(msg, txn, "Cookie", 6,
8689 arg_p->data.str.str, arg_p->data.str.len, 1,
8690 &cookie_value, &cookie_value_l);
8691 if (found) {
8692 data->str.str = cookie_value;
8693 data->str.len = cookie_value_l;
8694 }
8695
8696 return found;
8697}
8698
8699
8700static int
8701pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8702 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8703{
8704 struct http_txn *txn = l7;
8705 struct http_msg *msg = &txn->rsp;
8706 char *cookie_value;
8707 size_t cookie_value_l;
8708 int found = 0;
8709
8710 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8711 arg_p->data.str.str, arg_p->data.str.len, 1,
8712 &cookie_value, &cookie_value_l);
8713 if (found) {
8714 data->str.str = cookie_value;
8715 data->str.len = cookie_value_l;
8716 }
8717
8718 return found;
8719}
8720
Emeric Brun485479d2010-09-23 18:02:19 +02008721
Willy Tarreau4a568972010-05-12 08:08:50 +02008722/************************************************************************/
8723/* All supported keywords must be declared here. */
8724/************************************************************************/
8725/* Note: must not be declared <const> as its list will be overwritten */
8726static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Emeric Brun485479d2010-09-23 18:02:19 +02008727 { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008728 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008729 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8730 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008731 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008732}};
8733
Willy Tarreau8797c062007-05-07 00:55:35 +02008734
8735__attribute__((constructor))
8736static void __http_protocol_init(void)
8737{
8738 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008739 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008740}
8741
8742
Willy Tarreau58f10d72006-12-04 02:26:12 +01008743/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008744 * Local variables:
8745 * c-indent-level: 8
8746 * c-basic-offset: 8
8747 * End:
8748 */