blob: ec1e1808ae3856d68fb49f63bb9dceb2844b78f5 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010027#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/compat.h>
29#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010030#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreau8797c062007-05-07 00:55:35 +020042#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010043#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/backend.h>
45#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010046#include <proto/checks.h>
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 Tarreau1c47f852006-07-09 08:22:27 +020071/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010072const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020073 "HTTP/1.0 200 OK\r\n"
74 "Cache-Control: no-cache\r\n"
75 "Connection: close\r\n"
76 "Content-Type: text/html\r\n"
77 "\r\n"
78 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
79
Willy Tarreau0f772532006-12-23 20:51:41 +010080const struct chunk http_200_chunk = {
81 .str = (char *)&HTTP_200,
82 .len = sizeof(HTTP_200)-1
83};
84
Willy Tarreaua9679ac2010-01-03 17:32:57 +010085/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020086const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020090 "Location: "; /* not terminated since it will be concatenated with the URL */
91
Willy Tarreau0f772532006-12-23 20:51:41 +010092const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010095 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010096 "Location: "; /* not terminated since it will be concatenated with the URL */
97
98/* same as 302 except that the browser MUST retry with the GET method */
99const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100100 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100101 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100102 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
106const char *HTTP_401_fmt =
107 "HTTP/1.0 401 Unauthorized\r\n"
108 "Cache-Control: no-cache\r\n"
109 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200110 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
112 "\r\n"
113 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
114
Willy Tarreau844a7e72010-01-31 21:46:18 +0100115const char *HTTP_407_fmt =
116 "HTTP/1.0 407 Unauthorized\r\n"
117 "Cache-Control: no-cache\r\n"
118 "Connection: close\r\n"
119 "Content-Type: text/html\r\n"
120 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
121 "\r\n"
122 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
123
Willy Tarreau0f772532006-12-23 20:51:41 +0100124
125const int http_err_codes[HTTP_ERR_SIZE] = {
126 [HTTP_ERR_400] = 400,
127 [HTTP_ERR_403] = 403,
128 [HTTP_ERR_408] = 408,
129 [HTTP_ERR_500] = 500,
130 [HTTP_ERR_502] = 502,
131 [HTTP_ERR_503] = 503,
132 [HTTP_ERR_504] = 504,
133};
134
Willy Tarreau80587432006-12-24 17:47:20 +0100135static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100137 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 "Cache-Control: no-cache\r\n"
139 "Connection: close\r\n"
140 "Content-Type: text/html\r\n"
141 "\r\n"
142 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
143
144 [HTTP_ERR_403] =
145 "HTTP/1.0 403 Forbidden\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
151
152 [HTTP_ERR_408] =
153 "HTTP/1.0 408 Request Time-out\r\n"
154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
159
160 [HTTP_ERR_500] =
161 "HTTP/1.0 500 Server Error\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
167
168 [HTTP_ERR_502] =
169 "HTTP/1.0 502 Bad Gateway\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
175
176 [HTTP_ERR_503] =
177 "HTTP/1.0 503 Service Unavailable\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
183
184 [HTTP_ERR_504] =
185 "HTTP/1.0 504 Gateway Time-out\r\n"
186 "Cache-Control: no-cache\r\n"
187 "Connection: close\r\n"
188 "Content-Type: text/html\r\n"
189 "\r\n"
190 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
191
192};
193
Willy Tarreau80587432006-12-24 17:47:20 +0100194/* We must put the messages here since GCC cannot initialize consts depending
195 * on strlen().
196 */
197struct chunk http_err_chunks[HTTP_ERR_SIZE];
198
Willy Tarreau42250582007-04-01 01:30:43 +0200199#define FD_SETS_ARE_BITFIELDS
200#ifdef FD_SETS_ARE_BITFIELDS
201/*
202 * This map is used with all the FD_* macros to check whether a particular bit
203 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
204 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
205 * byte should be encoded. Be careful to always pass bytes from 0 to 255
206 * exclusively to the macros.
207 */
208fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
209fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
210
211#else
212#error "Check if your OS uses bitfields for fd_sets"
213#endif
214
Willy Tarreau80587432006-12-24 17:47:20 +0100215void init_proto_http()
216{
Willy Tarreau42250582007-04-01 01:30:43 +0200217 int i;
218 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100219 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200220
Willy Tarreau80587432006-12-24 17:47:20 +0100221 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
222 if (!http_err_msgs[msg]) {
223 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
224 abort();
225 }
226
227 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
228 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
229 }
Willy Tarreau42250582007-04-01 01:30:43 +0200230
231 /* initialize the log header encoding map : '{|}"#' should be encoded with
232 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
233 * URL encoding only requires '"', '#' to be encoded as well as non-
234 * printable characters above.
235 */
236 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
237 memset(url_encode_map, 0, sizeof(url_encode_map));
238 for (i = 0; i < 32; i++) {
239 FD_SET(i, hdr_encode_map);
240 FD_SET(i, url_encode_map);
241 }
242 for (i = 127; i < 256; i++) {
243 FD_SET(i, hdr_encode_map);
244 FD_SET(i, url_encode_map);
245 }
246
247 tmp = "\"#{|}";
248 while (*tmp) {
249 FD_SET(*tmp, hdr_encode_map);
250 tmp++;
251 }
252
253 tmp = "\"#";
254 while (*tmp) {
255 FD_SET(*tmp, url_encode_map);
256 tmp++;
257 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200258
259 /* memory allocations */
260 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200261 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100262}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263
Willy Tarreau53b6c742006-12-17 13:37:46 +0100264/*
265 * We have 26 list of methods (1 per first letter), each of which can have
266 * up to 3 entries (2 valid, 1 null).
267 */
268struct http_method_desc {
269 http_meth_t meth;
270 int len;
271 const char text[8];
272};
273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100274const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100275 ['C' - 'A'] = {
276 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
277 },
278 ['D' - 'A'] = {
279 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
280 },
281 ['G' - 'A'] = {
282 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
283 },
284 ['H' - 'A'] = {
285 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
286 },
287 ['P' - 'A'] = {
288 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
289 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
290 },
291 ['T' - 'A'] = {
292 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
293 },
294 /* rest is empty like this :
295 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
296 */
297};
298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100299/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200300 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100301 * RFC2616 for those chars.
302 */
303
304const char http_is_spht[256] = {
305 [' '] = 1, ['\t'] = 1,
306};
307
308const char http_is_crlf[256] = {
309 ['\r'] = 1, ['\n'] = 1,
310};
311
312const char http_is_lws[256] = {
313 [' '] = 1, ['\t'] = 1,
314 ['\r'] = 1, ['\n'] = 1,
315};
316
317const char http_is_sep[256] = {
318 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
319 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
320 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
321 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
322 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
323};
324
325const char http_is_ctl[256] = {
326 [0 ... 31] = 1,
327 [127] = 1,
328};
329
330/*
331 * A token is any ASCII char that is neither a separator nor a CTL char.
332 * Do not overwrite values in assignment since gcc-2.95 will not handle
333 * them correctly. Instead, define every non-CTL char's status.
334 */
335const char http_is_token[256] = {
336 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
337 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
338 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
339 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
340 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
341 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
342 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
343 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
344 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
345 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
346 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
347 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
348 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
349 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
350 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
351 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
352 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
353 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
354 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
355 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
356 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
357 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
358 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
359 ['|'] = 1, ['}'] = 0, ['~'] = 1,
360};
361
362
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100363/*
364 * An http ver_token is any ASCII which can be found in an HTTP version,
365 * which includes 'H', 'T', 'P', '/', '.' and any digit.
366 */
367const char http_is_ver_token[256] = {
368 ['.'] = 1, ['/'] = 1,
369 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
370 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
371 ['H'] = 1, ['P'] = 1, ['T'] = 1,
372};
373
374
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100375/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100376 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
377 */
378#if defined(DEBUG_FSM)
379static void http_silent_debug(int line, struct session *s)
380{
381 int size = 0;
382 size += snprintf(trash + size, sizeof(trash) - size,
383 "[%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",
384 line,
385 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
386 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);
387 write(-1, trash, size);
388 size = 0;
389 size += snprintf(trash + size, sizeof(trash) - size,
390 " %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",
391 line,
392 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
393 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);
394
395 write(-1, trash, size);
396}
397#else
398#define http_silent_debug(l,s) do { } while (0)
399#endif
400
401/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100402 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
403 * CRLF. Text length is measured first, so it cannot be NULL.
404 * The header is also automatically added to the index <hdr_idx>, and the end
405 * of headers is automatically adjusted. The number of bytes added is returned
406 * on success, otherwise <0 is returned indicating an error.
407 */
408int http_header_add_tail(struct buffer *b, struct http_msg *msg,
409 struct hdr_idx *hdr_idx, const char *text)
410{
411 int bytes, len;
412
413 len = strlen(text);
414 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
415 if (!bytes)
416 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100417 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100418 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
419}
420
421/*
422 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
423 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
424 * the buffer is only opened and the space reserved, but nothing is copied.
425 * The header is also automatically added to the index <hdr_idx>, and the end
426 * of headers is automatically adjusted. The number of bytes added is returned
427 * on success, otherwise <0 is returned indicating an error.
428 */
429int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
430 struct hdr_idx *hdr_idx, const char *text, int len)
431{
432 int bytes;
433
434 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
435 if (!bytes)
436 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100437 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100438 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
439}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440
441/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100442 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
443 * If so, returns the position of the first non-space character relative to
444 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
445 * to return a pointer to the place after the first space. Returns 0 if the
446 * header name does not match. Checks are case-insensitive.
447 */
448int http_header_match2(const char *hdr, const char *end,
449 const char *name, int len)
450{
451 const char *val;
452
453 if (hdr + len >= end)
454 return 0;
455 if (hdr[len] != ':')
456 return 0;
457 if (strncasecmp(hdr, name, len) != 0)
458 return 0;
459 val = hdr + len + 1;
460 while (val < end && HTTP_IS_SPHT(*val))
461 val++;
462 if ((val >= end) && (len + 2 <= end - hdr))
463 return len + 2; /* we may replace starting from second space */
464 return val - hdr;
465}
466
Willy Tarreau68085d82010-01-18 14:54:04 +0100467/* Find the end of the header value contained between <s> and <e>. See RFC2616,
468 * par 2.2 for more information. Note that it requires a valid header to return
469 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200470 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100471char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200472{
473 int quoted, qdpair;
474
475 quoted = qdpair = 0;
476 for (; s < e; s++) {
477 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200478 else if (quoted) {
479 if (*s == '\\') qdpair = 1;
480 else if (*s == '"') quoted = 0;
481 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200482 else if (*s == '"') quoted = 1;
483 else if (*s == ',') return s;
484 }
485 return s;
486}
487
488/* Find the first or next occurrence of header <name> in message buffer <sol>
489 * using headers index <idx>, and return it in the <ctx> structure. This
490 * structure holds everything necessary to use the header and find next
491 * occurrence. If its <idx> member is 0, the header is searched from the
492 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100493 * 1 when it finds a value, and 0 when there is no more. It is designed to work
494 * with headers defined as comma-separated lists. As a special case, if ctx->val
495 * is NULL when searching for a new values of a header, the current header is
496 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200497 */
498int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100499 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200500 struct hdr_ctx *ctx)
501{
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 char *eol, *sov;
503 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200504
Willy Tarreau68085d82010-01-18 14:54:04 +0100505 cur_idx = ctx->idx;
506 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 /* We have previously returned a value, let's search
508 * another one on the same line.
509 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100511 ctx->del = ctx->val + ctx->vlen;
512 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200513 eol = sol + idx->v[cur_idx].len;
514
515 if (sov >= eol)
516 /* no more values in this header */
517 goto next_hdr;
518
Willy Tarreau68085d82010-01-18 14:54:04 +0100519 /* values remaining for this header, skip the comma but save it
520 * for later use (eg: for header deletion).
521 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200522 sov++;
523 while (sov < eol && http_is_lws[(unsigned char)*sov])
524 sov++;
525
526 goto return_hdr;
527 }
528
529 /* first request for this header */
530 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100531 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200532 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200533 while (cur_idx) {
534 eol = sol + idx->v[cur_idx].len;
535
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200536 if (len == 0) {
537 /* No argument was passed, we want any header.
538 * To achieve this, we simply build a fake request. */
539 while (sol + len < eol && sol[len] != ':')
540 len++;
541 name = sol;
542 }
543
Willy Tarreau33a7e692007-06-10 19:45:56 +0200544 if ((len < eol - sol) &&
545 (sol[len] == ':') &&
546 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100547 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200548 sov = sol + len + 1;
549 while (sov < eol && http_is_lws[(unsigned char)*sov])
550 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100551
Willy Tarreau33a7e692007-06-10 19:45:56 +0200552 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100553 ctx->prev = old_idx;
554 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200555 ctx->idx = cur_idx;
556 ctx->val = sov - sol;
557
558 eol = find_hdr_value_end(sov, eol);
559 ctx->vlen = eol - sov;
560 return 1;
561 }
562 next_hdr:
563 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100564 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200565 cur_idx = idx->v[cur_idx].next;
566 }
567 return 0;
568}
569
570int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100571 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200572 struct hdr_ctx *ctx)
573{
574 return http_find_header2(name, strlen(name), sol, idx, ctx);
575}
576
Willy Tarreau68085d82010-01-18 14:54:04 +0100577/* Remove one value of a header. This only works on a <ctx> returned by one of
578 * the http_find_header functions. The value is removed, as well as surrounding
579 * commas if any. If the removed value was alone, the whole header is removed.
580 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
581 * message <msg>. The new index is returned. If it is zero, it means there is
582 * no more header, so any processing may stop. The ctx is always left in a form
583 * that can be handled by http_find_header2() to find next occurrence.
584 */
585int http_remove_header2(struct http_msg *msg, struct buffer *buf,
586 struct hdr_idx *idx, struct hdr_ctx *ctx)
587{
588 int cur_idx = ctx->idx;
589 char *sol = ctx->line;
590 struct hdr_idx_elem *hdr;
591 int delta, skip_comma;
592
593 if (!cur_idx)
594 return 0;
595
596 hdr = &idx->v[cur_idx];
597 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) {
598 /* This was the only value of the header, we must now remove it entirely. */
599 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
600 http_msg_move_end(msg, delta);
601 idx->used--;
602 hdr->len = 0; /* unused entry */
603 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
604 ctx->idx = ctx->prev; /* walk back to the end of previous header */
605 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
606 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
607 ctx->vlen = 0;
608 return ctx->idx;
609 }
610
611 /* This was not the only value of this header. We have to remove between
612 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
613 * of the list, we remove the last separator.
614 */
615
616 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
617 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
618 sol + ctx->val + ctx->vlen + skip_comma,
619 NULL, 0);
620 hdr->len += delta;
621 http_msg_move_end(msg, delta);
622 ctx->val = ctx->del;
623 ctx->vlen = 0;
624 return ctx->idx;
625}
626
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100627/* This function handles a server error at the stream interface level. The
628 * stream interface is assumed to be already in a closed state. An optional
629 * message is copied into the input buffer, and an HTTP status code stored.
630 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100631 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100633static void http_server_error(struct session *t, struct stream_interface *si,
634 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200635{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100636 buffer_auto_read(si->ob);
637 buffer_abort(si->ob);
638 buffer_auto_close(si->ob);
639 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200640 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100641 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100642 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100643 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100644 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200645 }
646 if (!(t->flags & SN_ERR_MASK))
647 t->flags |= err;
648 if (!(t->flags & SN_FINST_MASK))
649 t->flags |= finst;
650}
651
Willy Tarreau80587432006-12-24 17:47:20 +0100652/* This function returns the appropriate error location for the given session
653 * and message.
654 */
655
656struct chunk *error_message(struct session *s, int msgnum)
657{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200658 if (s->be->errmsg[msgnum].str)
659 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100660 else if (s->fe->errmsg[msgnum].str)
661 return &s->fe->errmsg[msgnum];
662 else
663 return &http_err_chunks[msgnum];
664}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665
Willy Tarreau53b6c742006-12-17 13:37:46 +0100666/*
667 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
668 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
669 */
670static http_meth_t find_http_meth(const char *str, const int len)
671{
672 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100673 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100674
675 m = ((unsigned)*str - 'A');
676
677 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100678 for (h = http_methods[m]; h->len > 0; h++) {
679 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100680 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100681 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100682 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100683 };
684 return HTTP_METH_OTHER;
685 }
686 return HTTP_METH_NONE;
687
688}
689
Willy Tarreau21d2af32008-02-14 20:25:24 +0100690/* Parse the URI from the given transaction (which is assumed to be in request
691 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
692 * It is returned otherwise.
693 */
694static char *
695http_get_path(struct http_txn *txn)
696{
697 char *ptr, *end;
698
Willy Tarreau962c3f42010-01-10 00:15:35 +0100699 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100700 end = ptr + txn->req.sl.rq.u_l;
701
702 if (ptr >= end)
703 return NULL;
704
705 /* RFC2616, par. 5.1.2 :
706 * Request-URI = "*" | absuri | abspath | authority
707 */
708
709 if (*ptr == '*')
710 return NULL;
711
712 if (isalpha((unsigned char)*ptr)) {
713 /* this is a scheme as described by RFC3986, par. 3.1 */
714 ptr++;
715 while (ptr < end &&
716 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
717 ptr++;
718 /* skip '://' */
719 if (ptr == end || *ptr++ != ':')
720 return NULL;
721 if (ptr == end || *ptr++ != '/')
722 return NULL;
723 if (ptr == end || *ptr++ != '/')
724 return NULL;
725 }
726 /* skip [user[:passwd]@]host[:[port]] */
727
728 while (ptr < end && *ptr != '/')
729 ptr++;
730
731 if (ptr == end)
732 return NULL;
733
734 /* OK, we got the '/' ! */
735 return ptr;
736}
737
Willy Tarreauefb453c2008-10-26 20:49:47 +0100738/* Returns a 302 for a redirectable request. This may only be called just after
739 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
740 * left unchanged and will follow normal proxy processing.
741 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100742void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100743{
744 struct http_txn *txn;
745 struct chunk rdr;
746 char *path;
747 int len;
748
749 /* 1: create the response header */
750 rdr.len = strlen(HTTP_302);
751 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100752 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100753 memcpy(rdr.str, HTTP_302, rdr.len);
754
755 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200756 if (rdr.len + s->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 */
760 if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') {
761 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
762 rdr.len += s->srv->rdr_len;
763 }
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. */
797 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100798 srv_inc_sess_ctr(s->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;
856 char *svid;
857 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
868 if (s->cli_addr.ss_family == AF_INET)
869 inet_ntop(AF_INET,
870 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
871 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +0200872 else if (s->cli_addr.ss_family == AF_INET6)
Emeric Brun3a058f32009-06-30 18:26:00 +0200873 inet_ntop(AF_INET6,
874 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
875 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",
Emeric Brun5bd86a82010-10-22 17:23:04 +0200917 s->cli_addr.ss_family == AF_UNIX ? s->listener->luid :
918 ntohs((s->cli_addr.ss_family == AF_INET) ?
919 ((struct sockaddr_in *)&s->cli_addr)->sin_port :
920 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Emeric Brun3a058f32009-06-30 18:26:00 +0200921 (int)s->logs.accept_date.tv_usec/1000);
922 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
923 goto trunc;
924 h += w;
925
926 w = strlen(fe->id);
927 if (h >= tmpline + sizeof(tmpline) - 4 - w)
928 goto trunc;
929 *(h++) = ' ';
930 *(h++) = '\"';
931 memcpy(h, fe->id, w);
932 h += w;
933 *(h++) = '\"';
934
935 w = strlen(be->id);
936 if (h >= tmpline + sizeof(tmpline) - 4 - w)
937 goto trunc;
938 *(h++) = ' ';
939 *(h++) = '\"';
940 memcpy(h, be->id, w);
941 h += w;
942 *(h++) = '\"';
943
944 svid = (tolog & LW_SVID) ?
945 (s->data_source != DATA_SRC_STATS) ?
946 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
947
948 w = strlen(svid);
949 if (h >= tmpline + sizeof(tmpline) - 4 - w)
950 goto trunc;
951 *(h++) = ' ';
952 *(h++) = '\"';
953 memcpy(h, svid, w);
954 h += w;
955 *(h++) = '\"';
956
957 t_request = -1;
958 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
959 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
960 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
961 " %d %ld %ld %ld %ld",
962 t_request,
963 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
964 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
965 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
966 s->logs.t_close);
967 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
968 goto trunc;
969 h += w;
970
971 if (h >= tmpline + sizeof(tmpline) - 8)
972 goto trunc;
973 *(h++) = ' ';
974 *(h++) = '\"';
975 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
976 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
977 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
978 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
979 *(h++) = '\"';
980
981 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
982 " %d %d %d %d %d %ld %ld",
983 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Willy Tarreauee28de02010-06-01 09:51:00 +0200984 (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries,
Emeric Brun3a058f32009-06-30 18:26:00 +0200985 s->logs.srv_queue_size, s->logs.prx_queue_size);
986
987 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
988 goto trunc;
989 h += w;
990
991 if (txn->cli_cookie) {
992 w = strlen(txn->cli_cookie);
993 if (h >= tmpline + sizeof(tmpline) - 4 - w)
994 goto trunc;
995 *(h++) = ' ';
996 *(h++) = '\"';
997 memcpy(h, txn->cli_cookie, w);
998 h += w;
999 *(h++) = '\"';
1000 } else {
1001 if (h >= tmpline + sizeof(tmpline) - 5)
1002 goto trunc;
1003 memcpy(h, " \"-\"", 4);
1004 h += 4;
1005 }
1006
1007 if (txn->srv_cookie) {
1008 w = strlen(txn->srv_cookie);
1009 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1010 goto trunc;
1011 *(h++) = ' ';
1012 *(h++) = '\"';
1013 memcpy(h, txn->srv_cookie, w);
1014 h += w;
1015 *(h++) = '\"';
1016 } else {
1017 if (h >= tmpline + sizeof(tmpline) - 5)
1018 goto trunc;
1019 memcpy(h, " \"-\"", 4);
1020 h += 4;
1021 }
1022
1023 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1024 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1025 if (h >= sizeof (tmpline) + tmpline - 4)
1026 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001027 if (txn->req.cap[hdr] != NULL) {
1028 *(h++) = ' ';
1029 *(h++) = '\"';
1030 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1031 '#', hdr_encode_map, txn->req.cap[hdr]);
1032 *(h++) = '\"';
1033 } else {
1034 memcpy(h, " \"-\"", 4);
1035 h += 4;
1036 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001037 }
1038 }
1039
1040 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1041 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1042 if (h >= sizeof (tmpline) + tmpline - 4)
1043 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001044 if (txn->rsp.cap[hdr] != NULL) {
1045 *(h++) = ' ';
1046 *(h++) = '\"';
1047 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1048 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1049 *(h++) = '\"';
1050 } else {
1051 memcpy(h, " \"-\"", 4);
1052 h += 4;
1053 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001054 }
1055 }
1056
1057trunc:
1058 *h = '\0';
1059
1060 level = LOG_INFO;
1061 if (err && (fe->options2 & PR_O2_LOGERRORS))
1062 level = LOG_ERR;
1063
1064 send_log(prx_log, level, "%s\n", tmpline);
1065
1066 s->logs.logwait = 0;
1067}
1068
Willy Tarreau42250582007-04-01 01:30:43 +02001069/*
1070 * send a log for the session when we have enough info about it.
1071 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001073void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001074{
Cyril Bontéacd7d632010-11-01 19:26:02 +01001075 char pn[INET6_ADDRSTRLEN];
Willy Tarreau42250582007-04-01 01:30:43 +02001076 struct proxy *fe = s->fe;
1077 struct proxy *be = s->be;
1078 struct proxy *prx_log;
1079 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001080 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001081 char *uri, *h;
1082 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001083 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001084 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001085 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001086 int hdr;
1087
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001088 /* if we don't want to log normal traffic, return now */
1089 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +02001090 (s->req->cons->conn_retries != be->conn_retries) ||
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001091 txn->status >= 500;
1092 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1093 return;
1094
Willy Tarreau42250582007-04-01 01:30:43 +02001095 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1096 return;
1097 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098
Emeric Brun3a058f32009-06-30 18:26:00 +02001099 if (prx_log->options2 & PR_O2_CLFLOG)
1100 return http_sess_clflog(s);
1101
Willy Tarreau42250582007-04-01 01:30:43 +02001102 if (s->cli_addr.ss_family == AF_INET)
1103 inet_ntop(AF_INET,
1104 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1105 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +02001106 else if (s->cli_addr.ss_family == AF_INET6)
Willy Tarreau42250582007-04-01 01:30:43 +02001107 inet_ntop(AF_INET6,
1108 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1109 pn, sizeof(pn));
1110
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001111 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001112
1113 /* FIXME: let's limit ourselves to frontend logging for now. */
1114 tolog = fe->to_log;
1115
1116 h = tmpline;
1117 if (fe->to_log & LW_REQHDR &&
1118 txn->req.cap &&
1119 (h < tmpline + sizeof(tmpline) - 10)) {
1120 *(h++) = ' ';
1121 *(h++) = '{';
1122 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1123 if (hdr)
1124 *(h++) = '|';
1125 if (txn->req.cap[hdr] != NULL)
1126 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1127 '#', hdr_encode_map, txn->req.cap[hdr]);
1128 }
1129 *(h++) = '}';
1130 }
1131
1132 if (fe->to_log & LW_RSPHDR &&
1133 txn->rsp.cap &&
1134 (h < tmpline + sizeof(tmpline) - 7)) {
1135 *(h++) = ' ';
1136 *(h++) = '{';
1137 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1138 if (hdr)
1139 *(h++) = '|';
1140 if (txn->rsp.cap[hdr] != NULL)
1141 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1142 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1143 }
1144 *(h++) = '}';
1145 }
1146
1147 if (h < tmpline + sizeof(tmpline) - 4) {
1148 *(h++) = ' ';
1149 *(h++) = '"';
1150 uri = txn->uri ? txn->uri : "<BADREQ>";
1151 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1152 '#', url_encode_map, uri);
1153 *(h++) = '"';
1154 }
1155 *h = '\0';
1156
1157 svid = (tolog & LW_SVID) ?
1158 (s->data_source != DATA_SRC_STATS) ?
1159 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1160
Willy Tarreau70089872008-06-13 21:12:51 +02001161 t_request = -1;
1162 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1163 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1164
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001165 level = LOG_INFO;
1166 if (err && (fe->options2 & PR_O2_LOGERRORS))
1167 level = LOG_ERR;
1168
1169 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001170 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001171 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1172 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Emeric Brun5bd86a82010-10-22 17:23:04 +02001173 (s->cli_addr.ss_family == AF_UNIX) ? "unix" : pn,
1174 (s->cli_addr.ss_family == AF_UNIX) ? s->listener->luid :
1175 ntohs((s->cli_addr.ss_family == AF_INET) ?
1176 ((struct sockaddr_in *)&s->cli_addr)->sin_port :
1177 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001178 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001179 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001180 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001181 t_request,
1182 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001183 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1184 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1185 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1186 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001187 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001188 txn->cli_cookie ? txn->cli_cookie : "-",
1189 txn->srv_cookie ? txn->srv_cookie : "-",
1190 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1191 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1192 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1193 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1194 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001195 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +02001196 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001197 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1198
1199 s->logs.logwait = 0;
1200}
1201
Willy Tarreau117f59e2007-03-04 18:17:17 +01001202
1203/*
1204 * Capture headers from message starting at <som> according to header list
1205 * <cap_hdr>, and fill the <idx> structure appropriately.
1206 */
1207void capture_headers(char *som, struct hdr_idx *idx,
1208 char **cap, struct cap_hdr *cap_hdr)
1209{
1210 char *eol, *sol, *col, *sov;
1211 int cur_idx;
1212 struct cap_hdr *h;
1213 int len;
1214
1215 sol = som + hdr_idx_first_pos(idx);
1216 cur_idx = hdr_idx_first_idx(idx);
1217
1218 while (cur_idx) {
1219 eol = sol + idx->v[cur_idx].len;
1220
1221 col = sol;
1222 while (col < eol && *col != ':')
1223 col++;
1224
1225 sov = col + 1;
1226 while (sov < eol && http_is_lws[(unsigned char)*sov])
1227 sov++;
1228
1229 for (h = cap_hdr; h; h = h->next) {
1230 if ((h->namelen == col - sol) &&
1231 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1232 if (cap[h->index] == NULL)
1233 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001234 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001235
1236 if (cap[h->index] == NULL) {
1237 Alert("HTTP capture : out of memory.\n");
1238 continue;
1239 }
1240
1241 len = eol - sov;
1242 if (len > h->len)
1243 len = h->len;
1244
1245 memcpy(cap[h->index], sov, len);
1246 cap[h->index][len]=0;
1247 }
1248 }
1249 sol = eol + idx->v[cur_idx].cr + 1;
1250 cur_idx = idx->v[cur_idx].next;
1251 }
1252}
1253
1254
Willy Tarreau42250582007-04-01 01:30:43 +02001255/* either we find an LF at <ptr> or we jump to <bad>.
1256 */
1257#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1258
1259/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1260 * otherwise to <http_msg_ood> with <state> set to <st>.
1261 */
1262#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1263 ptr++; \
1264 if (likely(ptr < end)) \
1265 goto good; \
1266 else { \
1267 state = (st); \
1268 goto http_msg_ood; \
1269 } \
1270 } while (0)
1271
1272
Willy Tarreaubaaee002006-06-26 02:48:02 +02001273/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001274 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001275 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1276 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1277 * will give undefined results.
1278 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1279 * and that msg->sol points to the beginning of the response.
1280 * If a complete line is found (which implies that at least one CR or LF is
1281 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1282 * returned indicating an incomplete line (which does not mean that parts have
1283 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1284 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1285 * upon next call.
1286 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001287 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001288 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1289 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001290 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001291 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001292const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1293 unsigned int state, const char *ptr, const char *end,
1294 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001295{
Willy Tarreau8973c702007-01-21 23:58:29 +01001296 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001297 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001298 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001299 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1301
1302 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001303 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001304 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1305 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001306 state = HTTP_MSG_ERROR;
1307 break;
1308
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001310 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001312 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001313 goto http_msg_rpcode;
1314 }
1315 if (likely(HTTP_IS_SPHT(*ptr)))
1316 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1317 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001318 state = HTTP_MSG_ERROR;
1319 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001320
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001322 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001323 if (likely(!HTTP_IS_LWS(*ptr)))
1324 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1325
1326 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001327 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001328 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1329 }
1330
1331 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001332 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 http_msg_rsp_reason:
1334 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001335 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 msg->sl.st.r_l = 0;
1337 goto http_msg_rpline_eol;
1338
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001340 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001342 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 goto http_msg_rpreason;
1344 }
1345 if (likely(HTTP_IS_SPHT(*ptr)))
1346 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1347 /* so it's a CR/LF, so there is no reason phrase */
1348 goto http_msg_rsp_reason;
1349
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001351 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 if (likely(!HTTP_IS_CRLF(*ptr)))
1353 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001354 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 http_msg_rpline_eol:
1356 /* We have seen the end of line. Note that we do not
1357 * necessarily have the \n yet, but at least we know that we
1358 * have EITHER \r OR \n, otherwise the response would not be
1359 * complete. We can then record the response length and return
1360 * to the caller which will be able to register it.
1361 */
1362 msg->sl.st.l = ptr - msg->sol;
1363 return ptr;
1364
1365#ifdef DEBUG_FULL
1366 default:
1367 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1368 exit(1);
1369#endif
1370 }
1371
1372 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001373 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001374 if (ret_state)
1375 *ret_state = state;
1376 if (ret_ptr)
1377 *ret_ptr = (char *)ptr;
1378 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001379}
1380
Willy Tarreau8973c702007-01-21 23:58:29 +01001381/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 * This function parses a request line between <ptr> and <end>, starting with
1383 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1384 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1385 * will give undefined results.
1386 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1387 * and that msg->sol points to the beginning of the request.
1388 * If a complete line is found (which implies that at least one CR or LF is
1389 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1390 * returned indicating an incomplete line (which does not mean that parts have
1391 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1392 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1393 * upon next call.
1394 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001395 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1397 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001398 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001399 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001400const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1401 unsigned int state, const char *ptr, const char *end,
1402 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001403{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001406 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 if (likely(HTTP_IS_TOKEN(*ptr)))
1408 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001409
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001411 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1413 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 if (likely(HTTP_IS_CRLF(*ptr))) {
1416 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001417 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001419 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001421 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001423 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 msg->sl.rq.v_l = 0;
1425 goto http_msg_rqline_eol;
1426 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001427 state = HTTP_MSG_ERROR;
1428 break;
1429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001431 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001433 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 goto http_msg_rquri;
1435 }
1436 if (likely(HTTP_IS_SPHT(*ptr)))
1437 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1438 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1439 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001440
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001442 http_msg_rquri:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 if (likely(!HTTP_IS_LWS(*ptr)))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001445
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001446 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001447 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1449 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1452 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001453
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001455 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001457 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 goto http_msg_rqver;
1459 }
1460 if (likely(HTTP_IS_SPHT(*ptr)))
1461 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1462 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1463 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001466 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001467 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001469
1470 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001471 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001472 http_msg_rqline_eol:
1473 /* We have seen the end of line. Note that we do not
1474 * necessarily have the \n yet, but at least we know that we
1475 * have EITHER \r OR \n, otherwise the request would not be
1476 * complete. We can then record the request length and return
1477 * to the caller which will be able to register it.
1478 */
1479 msg->sl.rq.l = ptr - msg->sol;
1480 return ptr;
1481 }
1482
1483 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001484 state = HTTP_MSG_ERROR;
1485 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487#ifdef DEBUG_FULL
1488 default:
1489 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1490 exit(1);
1491#endif
1492 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001493
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001495 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 if (ret_state)
1497 *ret_state = state;
1498 if (ret_ptr)
1499 *ret_ptr = (char *)ptr;
1500 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001502
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001503/*
1504 * Returns the data from Authorization header. Function may be called more
1505 * than once so data is stored in txn->auth_data. When no header is found
1506 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1507 * searching again for something we are unable to find anyway.
1508 */
1509
1510char get_http_auth_buff[BUFSIZE];
1511
1512int
1513get_http_auth(struct session *s)
1514{
1515
1516 struct http_txn *txn = &s->txn;
1517 struct chunk auth_method;
1518 struct hdr_ctx ctx;
1519 char *h, *p;
1520 int len;
1521
1522#ifdef DEBUG_AUTH
1523 printf("Auth for session %p: %d\n", s, txn->auth.method);
1524#endif
1525
1526 if (txn->auth.method == HTTP_AUTH_WRONG)
1527 return 0;
1528
1529 if (txn->auth.method)
1530 return 1;
1531
1532 txn->auth.method = HTTP_AUTH_WRONG;
1533
1534 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001535
1536 if (txn->flags & TX_USE_PX_CONN) {
1537 h = "Proxy-Authorization";
1538 len = strlen(h);
1539 } else {
1540 h = "Authorization";
1541 len = strlen(h);
1542 }
1543
1544 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001545 return 0;
1546
1547 h = ctx.line + ctx.val;
1548
1549 p = memchr(h, ' ', ctx.vlen);
1550 if (!p || p == h)
1551 return 0;
1552
1553 chunk_initlen(&auth_method, h, 0, p-h);
1554 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1555
1556 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1557
1558 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1559 get_http_auth_buff, BUFSIZE - 1);
1560
1561 if (len < 0)
1562 return 0;
1563
1564
1565 get_http_auth_buff[len] = '\0';
1566
1567 p = strchr(get_http_auth_buff, ':');
1568
1569 if (!p)
1570 return 0;
1571
1572 txn->auth.user = get_http_auth_buff;
1573 *p = '\0';
1574 txn->auth.pass = p+1;
1575
1576 txn->auth.method = HTTP_AUTH_BASIC;
1577 return 1;
1578 }
1579
1580 return 0;
1581}
1582
Willy Tarreau58f10d72006-12-04 02:26:12 +01001583
Willy Tarreau8973c702007-01-21 23:58:29 +01001584/*
1585 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001586 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001587 * when data are missing and recalled at the exact same location with no
1588 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001589 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001590 * fields. Note that msg->som and msg->sol will be initialized after completing
1591 * the first state, so that none of the msg pointers has to be initialized
1592 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001593 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1595{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001596 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001598
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001599 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 ptr = buf->lr;
1601 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 if (unlikely(ptr >= end))
1604 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001605
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001607 /*
1608 * First, states that are specific to the response only.
1609 * We check them first so that request and headers are
1610 * closer to each other (accessed more often).
1611 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001612 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001613 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001614 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001615 /* we have a start of message, but we have to check
1616 * first if we need to remove some CRLF. We can only
1617 * do this when send_max=0.
1618 */
1619 char *beg = buf->w + buf->send_max;
1620 if (beg >= buf->data + buf->size)
1621 beg -= buf->size;
1622 if (unlikely(ptr != beg)) {
1623 if (buf->send_max)
1624 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001625 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001626 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001627 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001628 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001629 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001630 hdr_idx_init(idx);
1631 state = HTTP_MSG_RPVER;
1632 goto http_msg_rpver;
1633 }
1634
1635 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1636 goto http_msg_invalid;
1637
1638 if (unlikely(*ptr == '\n'))
1639 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1640 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1641 /* stop here */
1642
Willy Tarreau8973c702007-01-21 23:58:29 +01001643 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001644 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001645 EXPECT_LF_HERE(ptr, http_msg_invalid);
1646 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1647 /* stop here */
1648
Willy Tarreau8973c702007-01-21 23:58:29 +01001649 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001650 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001651 case HTTP_MSG_RPVER_SP:
1652 case HTTP_MSG_RPCODE:
1653 case HTTP_MSG_RPCODE_SP:
1654 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001655 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001656 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001657 if (unlikely(!ptr))
1658 return;
1659
1660 /* we have a full response and we know that we have either a CR
1661 * or an LF at <ptr>.
1662 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001663 //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 +01001664 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1665
1666 msg->sol = ptr;
1667 if (likely(*ptr == '\r'))
1668 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1669 goto http_msg_rpline_end;
1670
Willy Tarreau8973c702007-01-21 23:58:29 +01001671 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001672 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001673 /* msg->sol must point to the first of CR or LF. */
1674 EXPECT_LF_HERE(ptr, http_msg_invalid);
1675 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1676 /* stop here */
1677
1678 /*
1679 * Second, states that are specific to the request only
1680 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001682 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001683 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001684 /* we have a start of message, but we have to check
1685 * first if we need to remove some CRLF. We can only
1686 * do this when send_max=0.
1687 */
1688 char *beg = buf->w + buf->send_max;
1689 if (beg >= buf->data + buf->size)
1690 beg -= buf->size;
1691 if (likely(ptr != beg)) {
1692 if (buf->send_max)
1693 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001694 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001695 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001696 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001697 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001698 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001699 /* we will need this when keep-alive will be supported
1700 hdr_idx_init(idx);
1701 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001702 state = HTTP_MSG_RQMETH;
1703 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001705
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1707 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001708
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 if (unlikely(*ptr == '\n'))
1710 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1711 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001712 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001713
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001715 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 EXPECT_LF_HERE(ptr, http_msg_invalid);
1717 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001718 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001719
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001721 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 case HTTP_MSG_RQMETH_SP:
1723 case HTTP_MSG_RQURI:
1724 case HTTP_MSG_RQURI_SP:
1725 case HTTP_MSG_RQVER:
1726 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001727 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 if (unlikely(!ptr))
1729 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 /* we have a full request and we know that we have either a CR
1732 * or an LF at <ptr>.
1733 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001734 //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 +01001735 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001736
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001737 msg->sol = ptr;
1738 if (likely(*ptr == '\r'))
1739 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001741
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001743 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 /* check for HTTP/0.9 request : no version information available.
1745 * msg->sol must point to the first of CR or LF.
1746 */
1747 if (unlikely(msg->sl.rq.v_l == 0))
1748 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001749
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001750 EXPECT_LF_HERE(ptr, http_msg_invalid);
1751 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001752 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001753
Willy Tarreau8973c702007-01-21 23:58:29 +01001754 /*
1755 * Common states below
1756 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001758 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 msg->sol = ptr;
1760 if (likely(!HTTP_IS_CRLF(*ptr))) {
1761 goto http_msg_hdr_name;
1762 }
1763
1764 if (likely(*ptr == '\r'))
1765 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1766 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001767
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001769 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001770 /* assumes msg->sol points to the first char */
1771 if (likely(HTTP_IS_TOKEN(*ptr)))
1772 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001773
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001774 if (likely(*ptr == ':')) {
1775 msg->col = ptr - buf->data;
1776 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1777 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001778
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001779 if (likely(msg->err_pos < -1) || *ptr == '\n')
1780 goto http_msg_invalid;
1781
1782 if (msg->err_pos == -1) /* capture error pointer */
1783 msg->err_pos = ptr - buf->data; /* >= 0 now */
1784
1785 /* and we still accept this non-token character */
1786 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001789 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 /* assumes msg->sol points to the first char and msg->col to the colon */
1791 if (likely(HTTP_IS_SPHT(*ptr)))
1792 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001793
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 /* header value can be basically anything except CR/LF */
1795 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001796
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 if (likely(!HTTP_IS_CRLF(*ptr))) {
1798 goto http_msg_hdr_val;
1799 }
1800
1801 if (likely(*ptr == '\r'))
1802 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1803 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001806 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001807 EXPECT_LF_HERE(ptr, http_msg_invalid);
1808 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001809
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001810 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001811 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001812 if (likely(HTTP_IS_SPHT(*ptr))) {
1813 /* replace HT,CR,LF with spaces */
1814 for (; buf->data+msg->sov < ptr; msg->sov++)
1815 buf->data[msg->sov] = ' ';
1816 goto http_msg_hdr_l1_sp;
1817 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001818 /* we had a header consisting only in spaces ! */
1819 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 goto http_msg_complete_header;
1821
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001823 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 /* assumes msg->sol points to the first char, msg->col to the
1825 * colon, and msg->sov points to the first character of the
1826 * value.
1827 */
1828 if (likely(!HTTP_IS_CRLF(*ptr)))
1829 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001830
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 msg->eol = ptr;
1832 /* Note: we could also copy eol into ->eoh so that we have the
1833 * real header end in case it ends with lots of LWS, but is this
1834 * really needed ?
1835 */
1836 if (likely(*ptr == '\r'))
1837 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1838 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001839
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001841 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001842 EXPECT_LF_HERE(ptr, http_msg_invalid);
1843 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001844
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001845 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001846 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001847 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1848 /* LWS: replace HT,CR,LF with spaces */
1849 for (; msg->eol < ptr; msg->eol++)
1850 *msg->eol = ' ';
1851 goto http_msg_hdr_val;
1852 }
1853 http_msg_complete_header:
1854 /*
1855 * It was a new header, so the last one is finished.
1856 * Assumes msg->sol points to the first char, msg->col to the
1857 * colon, msg->sov points to the first character of the value
1858 * and msg->eol to the first CR or LF so we know how the line
1859 * ends. We insert last header into the index.
1860 */
1861 /*
1862 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1863 write(2, msg->sol, msg->eol-msg->sol);
1864 fprintf(stderr,"\n");
1865 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001866
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1868 idx, idx->tail) < 0))
1869 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001870
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001871 msg->sol = ptr;
1872 if (likely(!HTTP_IS_CRLF(*ptr))) {
1873 goto http_msg_hdr_name;
1874 }
1875
1876 if (likely(*ptr == '\r'))
1877 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1878 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001879
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001880 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001881 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001882 /* Assumes msg->sol points to the first of either CR or LF */
1883 EXPECT_LF_HERE(ptr, http_msg_invalid);
1884 ptr++;
1885 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001886 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001888 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001889 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001890 return;
1891#ifdef DEBUG_FULL
1892 default:
1893 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1894 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001895#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 }
1897 http_msg_ood:
1898 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001899 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001900 buf->lr = ptr;
1901 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001902
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001903 http_msg_invalid:
1904 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001905 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001906 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001907 return;
1908}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001909
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001910/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1911 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1912 * nothing is done and 1 is returned.
1913 */
1914static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1915{
1916 int delta;
1917 char *cur_end;
1918
1919 if (msg->sl.rq.v_l != 0)
1920 return 1;
1921
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001922 cur_end = msg->sol + msg->sl.rq.l;
1923 delta = 0;
1924
1925 if (msg->sl.rq.u_l == 0) {
1926 /* if no URI was set, add "/" */
1927 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1928 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001929 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001930 }
1931 /* add HTTP version */
1932 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001933 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001934 cur_end += delta;
1935 cur_end = (char *)http_parse_reqline(msg, req->data,
1936 HTTP_MSG_RQMETH,
1937 msg->sol, cur_end + 1,
1938 NULL, NULL);
1939 if (unlikely(!cur_end))
1940 return 0;
1941
1942 /* we have a full HTTP/1.0 request now and we know that
1943 * we have either a CR or an LF at <ptr>.
1944 */
1945 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1946 return 1;
1947}
1948
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001949/* Parse the Connection: header of an HTTP request, looking for both "close"
1950 * and "keep-alive" values. If a buffer is provided and we already know that
1951 * some headers may safely be removed, we remove them now. The <to_del> flags
1952 * are used for that :
1953 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1954 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1955 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1956 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1957 * harmless combinations may be removed. Do not call that after changes have
1958 * been processed. If unused, the buffer can be NULL, and no data will be
1959 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001960 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001961void 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 +01001962{
Willy Tarreau5b154472009-12-21 20:11:07 +01001963 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001964 const char *hdr_val = "Connection";
1965 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001966
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001967 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001968 return;
1969
Willy Tarreau88d349d2010-01-25 12:15:43 +01001970 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1971 hdr_val = "Proxy-Connection";
1972 hdr_len = 16;
1973 }
1974
Willy Tarreau5b154472009-12-21 20:11:07 +01001975 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001976 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001977 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001978 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1979 txn->flags |= TX_HDR_CONN_KAL;
1980 if ((to_del & 2) && buf)
1981 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1982 else
1983 txn->flags |= TX_CON_KAL_SET;
1984 }
1985 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1986 txn->flags |= TX_HDR_CONN_CLO;
1987 if ((to_del & 1) && buf)
1988 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1989 else
1990 txn->flags |= TX_CON_CLO_SET;
1991 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001992 }
1993
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001994 txn->flags |= TX_HDR_CONN_PRS;
1995 return;
1996}
Willy Tarreau5b154472009-12-21 20:11:07 +01001997
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001998/* Apply desired changes on the Connection: header. Values may be removed and/or
1999 * added depending on the <wanted> flags, which are exclusively composed of
2000 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2001 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2002 */
2003void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
2004{
2005 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002006 const char *hdr_val = "Connection";
2007 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002008
2009 ctx.idx = 0;
2010
Willy Tarreau88d349d2010-01-25 12:15:43 +01002011
2012 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2013 hdr_val = "Proxy-Connection";
2014 hdr_len = 16;
2015 }
2016
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002017 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002018 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002019 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2020 if (wanted & TX_CON_KAL_SET)
2021 txn->flags |= TX_CON_KAL_SET;
2022 else
2023 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002024 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002025 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2026 if (wanted & TX_CON_CLO_SET)
2027 txn->flags |= TX_CON_CLO_SET;
2028 else
2029 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002030 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002031 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002032
2033 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2034 return;
2035
2036 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2037 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002038 hdr_val = "Connection: close";
2039 hdr_len = 17;
2040 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2041 hdr_val = "Proxy-Connection: close";
2042 hdr_len = 23;
2043 }
2044 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002045 }
2046
2047 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2048 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002049 hdr_val = "Connection: keep-alive";
2050 hdr_len = 22;
2051 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2052 hdr_val = "Proxy-Connection: keep-alive";
2053 hdr_len = 28;
2054 }
2055 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002056 }
2057 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002058}
2059
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2061 * first byte of body, and increments msg->sov by the number of bytes parsed,
2062 * so that we know we can forward between ->som and ->sov. Note that due to
2063 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2064 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002065 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002066 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002067 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002068int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002069{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070 char *ptr = buf->lr;
2071 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002072 unsigned int chunk = 0;
2073
2074 /* The chunk size is in the following form, though we are only
2075 * interested in the size and CRLF :
2076 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2077 */
2078 while (1) {
2079 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002080 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002081 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002082 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002083 if (c < 0) /* not a hex digit anymore */
2084 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085 if (++ptr >= end)
2086 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002087 if (chunk & 0xF000000) /* overflow will occur */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002088 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002089 chunk = (chunk << 4) + c;
2090 }
2091
Willy Tarreaud98cf932009-12-27 22:54:55 +01002092 /* empty size not allowed */
2093 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002094 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095
2096 while (http_is_spht[(unsigned char)*ptr]) {
2097 if (++ptr >= end)
2098 ptr = buf->data;
2099 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002100 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002101 }
2102
Willy Tarreaud98cf932009-12-27 22:54:55 +01002103 /* Up to there, we know that at least one byte is present at *ptr. Check
2104 * for the end of chunk size.
2105 */
2106 while (1) {
2107 if (likely(HTTP_IS_CRLF(*ptr))) {
2108 /* we now have a CR or an LF at ptr */
2109 if (likely(*ptr == '\r')) {
2110 if (++ptr >= end)
2111 ptr = buf->data;
2112 if (ptr == buf->r)
2113 return 0;
2114 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002115
Willy Tarreaud98cf932009-12-27 22:54:55 +01002116 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002117 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002118 if (++ptr >= end)
2119 ptr = buf->data;
2120 /* done */
2121 break;
2122 }
2123 else if (*ptr == ';') {
2124 /* chunk extension, ends at next CRLF */
2125 if (++ptr >= end)
2126 ptr = buf->data;
2127 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002128 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002129
2130 while (!HTTP_IS_CRLF(*ptr)) {
2131 if (++ptr >= end)
2132 ptr = buf->data;
2133 if (ptr == buf->r)
2134 return 0;
2135 }
2136 /* we have a CRLF now, loop above */
2137 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002138 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002139 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002140 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002141 }
2142
Willy Tarreaud98cf932009-12-27 22:54:55 +01002143 /* OK we found our CRLF and now <ptr> points to the next byte,
2144 * which may or may not be present. We save that into ->lr and
2145 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002146 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002147 msg->sov += ptr - buf->lr;
2148 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01002149 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002150 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002151 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002152 error:
2153 msg->err_pos = ptr - buf->data;
2154 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002155}
2156
Willy Tarreaud98cf932009-12-27 22:54:55 +01002157/* This function skips trailers in the buffer <buf> associated with HTTP
2158 * message <msg>. The first visited position is buf->lr. If the end of
2159 * the trailers is found, it is automatically scheduled to be forwarded,
2160 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2161 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002162 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2163 * zero. If a parse error is encountered, the function returns < 0 and does not
2164 * change anything except maybe buf->lr and msg->sov. Note that the message
2165 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2166 * which implies that all non-trailers data have already been scheduled for
2167 * forwarding, and that the difference between msg->som and msg->sov exactly
2168 * matches the length of trailers already parsed and not forwarded. It is also
2169 * important to note that this function is designed to be able to parse wrapped
2170 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002171 */
2172int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2173{
2174 /* we have buf->lr which points to next line. Look for CRLF. */
2175 while (1) {
2176 char *p1 = NULL, *p2 = NULL;
2177 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002178 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002179
2180 /* scan current line and stop at LF or CRLF */
2181 while (1) {
2182 if (ptr == buf->r)
2183 return 0;
2184
2185 if (*ptr == '\n') {
2186 if (!p1)
2187 p1 = ptr;
2188 p2 = ptr;
2189 break;
2190 }
2191
2192 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002193 if (p1) {
2194 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002195 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002196 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 p1 = ptr;
2198 }
2199
2200 ptr++;
2201 if (ptr >= buf->data + buf->size)
2202 ptr = buf->data;
2203 }
2204
2205 /* after LF; point to beginning of next line */
2206 p2++;
2207 if (p2 >= buf->data + buf->size)
2208 p2 = buf->data;
2209
Willy Tarreau638cd022010-01-03 07:42:04 +01002210 bytes = p2 - buf->lr;
2211 if (bytes < 0)
2212 bytes += buf->size;
2213
2214 /* schedule this line for forwarding */
2215 msg->sov += bytes;
2216 if (msg->sov >= buf->size)
2217 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002218
Willy Tarreau638cd022010-01-03 07:42:04 +01002219 if (p1 == buf->lr) {
2220 /* LF/CRLF at beginning of line => end of trailers at p2.
2221 * Everything was scheduled for forwarding, there's nothing
2222 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002223 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002224 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002225 msg->msg_state = HTTP_MSG_DONE;
2226 return 1;
2227 }
2228 /* OK, next line then */
2229 buf->lr = p2;
2230 }
2231}
2232
2233/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2234 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2235 * ->som, buf->lr in order to include this part into the next forwarding phase.
2236 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2237 * not enough data are available, the function does not change anything and
2238 * returns zero. If a parse error is encountered, the function returns < 0 and
2239 * does not change anything. Note: this function is designed to parse wrapped
2240 * CRLF at the end of the buffer.
2241 */
2242int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2243{
2244 char *ptr;
2245 int bytes;
2246
2247 /* NB: we'll check data availabilty at the end. It's not a
2248 * problem because whatever we match first will be checked
2249 * against the correct length.
2250 */
2251 bytes = 1;
2252 ptr = buf->lr;
2253 if (*ptr == '\r') {
2254 bytes++;
2255 ptr++;
2256 if (ptr >= buf->data + buf->size)
2257 ptr = buf->data;
2258 }
2259
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002260 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002261 return 0;
2262
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002263 if (*ptr != '\n') {
2264 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002265 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002266 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002267
2268 ptr++;
2269 if (ptr >= buf->data + buf->size)
2270 ptr = buf->data;
2271 buf->lr = ptr;
2272 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2273 msg->sov = ptr - buf->data;
2274 msg->som = msg->sov - bytes;
2275 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2276 return 1;
2277}
Willy Tarreau5b154472009-12-21 20:11:07 +01002278
Willy Tarreau83e3af02009-12-28 17:39:57 +01002279void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2280{
2281 char *end = buf->data + buf->size;
2282 int off = buf->data + buf->size - buf->w;
2283
2284 /* two possible cases :
2285 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002286 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002287 */
2288 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002289 int block1 = buf->l;
2290 int block2 = 0;
2291 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002292 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002293 block1 = buf->data + buf->size - buf->w;
2294 block2 = buf->r - buf->data;
2295 }
2296 if (block2)
2297 memcpy(swap_buffer, buf->data, block2);
2298 memmove(buf->data, buf->w, block1);
2299 if (block2)
2300 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002301 }
2302
2303 /* adjust all known pointers */
2304 buf->w = buf->data;
2305 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2306 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2307 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2308 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2309
2310 /* adjust relative pointers */
2311 msg->som = 0;
2312 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2313 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2314 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2315
Willy Tarreau83e3af02009-12-28 17:39:57 +01002316 if (msg->err_pos >= 0) {
2317 msg->err_pos += off;
2318 if (msg->err_pos >= buf->size)
2319 msg->err_pos -= buf->size;
2320 }
2321
2322 buf->flags &= ~BF_FULL;
2323 if (buf->l >= buffer_max_len(buf))
2324 buf->flags |= BF_FULL;
2325}
2326
Willy Tarreaud787e662009-07-07 10:14:51 +02002327/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2328 * processing can continue on next analysers, or zero if it either needs more
2329 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2330 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2331 * when it has nothing left to do, and may remove any analyser when it wants to
2332 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002333 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002334int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002335{
Willy Tarreau59234e92008-11-30 23:51:27 +01002336 /*
2337 * We will parse the partial (or complete) lines.
2338 * We will check the request syntax, and also join multi-line
2339 * headers. An index of all the lines will be elaborated while
2340 * parsing.
2341 *
2342 * For the parsing, we use a 28 states FSM.
2343 *
2344 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002345 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002346 * req->data + msg->eoh = end of processed headers / start of current one
2347 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 * req->lr = first non-visited byte
2349 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002350 *
2351 * At end of parsing, we may perform a capture of the error (if any), and
2352 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2353 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2354 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002356
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002358 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002359 struct http_txn *txn = &s->txn;
2360 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002361 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002362
Willy Tarreau6bf17362009-02-24 10:48:35 +01002363 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2364 now_ms, __FUNCTION__,
2365 s,
2366 req,
2367 req->rex, req->wex,
2368 req->flags,
2369 req->l,
2370 req->analysers);
2371
Willy Tarreau52a0c602009-08-16 22:45:38 +02002372 /* we're speaking HTTP here, so let's speak HTTP to the client */
2373 s->srv_error = http_return_srv_error;
2374
Willy Tarreau83e3af02009-12-28 17:39:57 +01002375 /* There's a protected area at the end of the buffer for rewriting
2376 * purposes. We don't want to start to parse the request if the
2377 * protected area is affected, because we may have to move processed
2378 * data later, which is much more complicated.
2379 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002380 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002381 if ((txn->flags & TX_NOT_FIRST) &&
2382 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002383 req->r < req->lr ||
2384 req->r > req->data + req->size - global.tune.maxrewrite)) {
2385 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002386 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2387 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002388 /* some data has still not left the buffer, wake us once that's done */
2389 buffer_dont_connect(req);
2390 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2391 return 0;
2392 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002393 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002394 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002395 }
2396
Willy Tarreau065e8332010-01-08 00:30:20 +01002397 /* Note that we have the same problem with the response ; we
2398 * may want to send a redirect, error or anything which requires
2399 * some spare space. So we'll ensure that we have at least
2400 * maxrewrite bytes available in the response buffer before
2401 * processing that one. This will only affect pipelined
2402 * keep-alive requests.
2403 */
2404 if ((txn->flags & TX_NOT_FIRST) &&
2405 unlikely((s->rep->flags & BF_FULL) ||
2406 s->rep->r < s->rep->lr ||
2407 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2408 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002409 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2410 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002411 /* don't let a connection request be initiated */
2412 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002413 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002414 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002415 return 0;
2416 }
2417 }
2418
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002419 if (likely(req->lr < req->r))
2420 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002421 }
2422
Willy Tarreau59234e92008-11-30 23:51:27 +01002423 /* 1: we might have to print this header in debug mode */
2424 if (unlikely((global.mode & MODE_DEBUG) &&
2425 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002426 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002427 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002428 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002429
Willy Tarreau663308b2010-06-07 14:06:08 +02002430 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002431 eol = sol + msg->sl.rq.l;
2432 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002433
Willy Tarreau59234e92008-11-30 23:51:27 +01002434 sol += hdr_idx_first_pos(&txn->hdr_idx);
2435 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002436
Willy Tarreau59234e92008-11-30 23:51:27 +01002437 while (cur_idx) {
2438 eol = sol + txn->hdr_idx.v[cur_idx].len;
2439 debug_hdr("clihdr", s, sol, eol);
2440 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2441 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002442 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002443 }
2444
Willy Tarreau58f10d72006-12-04 02:26:12 +01002445
Willy Tarreau59234e92008-11-30 23:51:27 +01002446 /*
2447 * Now we quickly check if we have found a full valid request.
2448 * If not so, we check the FD and buffer states before leaving.
2449 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002450 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002451 * requests are checked first. When waiting for a second request
2452 * on a keep-alive session, if we encounter and error, close, t/o,
2453 * we note the error in the session flags but don't set any state.
2454 * Since the error will be noted there, it will not be counted by
2455 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002456 * Last, we may increase some tracked counters' http request errors on
2457 * the cases that are deliberately the client's fault. For instance,
2458 * a timeout or connection reset is not counted as an error. However
2459 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002460 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002461
Willy Tarreau655dce92009-11-08 13:10:58 +01002462 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002463 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002464 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002465 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002466 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002467 session_inc_http_req_ctr(s);
2468 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002469 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002470 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002471 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002472
Willy Tarreau59234e92008-11-30 23:51:27 +01002473 /* 1: Since we are in header mode, if there's no space
2474 * left for headers, we won't be able to free more
2475 * later, so the session will never terminate. We
2476 * must terminate it now.
2477 */
2478 if (unlikely(req->flags & BF_FULL)) {
2479 /* FIXME: check if URI is set and return Status
2480 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002481 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002482 session_inc_http_req_ctr(s);
2483 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002484 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002485 goto return_bad_req;
2486 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002487
Willy Tarreau59234e92008-11-30 23:51:27 +01002488 /* 2: have we encountered a read error ? */
2489 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002490 if (!(s->flags & SN_ERR_MASK))
2491 s->flags |= SN_ERR_CLICL;
2492
Willy Tarreaufcffa692010-01-10 14:21:19 +01002493 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002494 goto failed_keep_alive;
2495
Willy Tarreau59234e92008-11-30 23:51:27 +01002496 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002497 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002498 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002499 session_inc_http_err_ctr(s);
2500 }
2501
Willy Tarreau59234e92008-11-30 23:51:27 +01002502 msg->msg_state = HTTP_MSG_ERROR;
2503 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002504
Willy Tarreauda7ff642010-06-23 11:44:09 +02002505 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002506 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002507 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002508 if (s->listener->counters)
2509 s->listener->counters->failed_req++;
2510
Willy Tarreau59234e92008-11-30 23:51:27 +01002511 if (!(s->flags & SN_FINST_MASK))
2512 s->flags |= SN_FINST_R;
2513 return 0;
2514 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002515
Willy Tarreau59234e92008-11-30 23:51:27 +01002516 /* 3: has the read timeout expired ? */
2517 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002518 if (!(s->flags & SN_ERR_MASK))
2519 s->flags |= SN_ERR_CLITO;
2520
Willy Tarreaufcffa692010-01-10 14:21:19 +01002521 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002522 goto failed_keep_alive;
2523
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002525 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002526 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002527 session_inc_http_err_ctr(s);
2528 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002529 txn->status = 408;
2530 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2531 msg->msg_state = HTTP_MSG_ERROR;
2532 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002533
Willy Tarreauda7ff642010-06-23 11:44:09 +02002534 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002535 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002536 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002537 if (s->listener->counters)
2538 s->listener->counters->failed_req++;
2539
Willy Tarreau59234e92008-11-30 23:51:27 +01002540 if (!(s->flags & SN_FINST_MASK))
2541 s->flags |= SN_FINST_R;
2542 return 0;
2543 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002544
Willy Tarreau59234e92008-11-30 23:51:27 +01002545 /* 4: have we encountered a close ? */
2546 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002547 if (!(s->flags & SN_ERR_MASK))
2548 s->flags |= SN_ERR_CLICL;
2549
Willy Tarreaufcffa692010-01-10 14:21:19 +01002550 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002551 goto failed_keep_alive;
2552
Willy Tarreau4076a152009-04-02 15:18:36 +02002553 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002554 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002555 txn->status = 400;
2556 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2557 msg->msg_state = HTTP_MSG_ERROR;
2558 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002559
Willy Tarreauda7ff642010-06-23 11:44:09 +02002560 session_inc_http_err_ctr(s);
2561 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002562 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002563 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002564 if (s->listener->counters)
2565 s->listener->counters->failed_req++;
2566
Willy Tarreau59234e92008-11-30 23:51:27 +01002567 if (!(s->flags & SN_FINST_MASK))
2568 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002569 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002570 }
2571
Willy Tarreau520d95e2009-09-19 21:04:57 +02002572 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002573 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002574 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002575
Willy Tarreaufcffa692010-01-10 14:21:19 +01002576 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2577 /* If the client starts to talk, let's fall back to
2578 * request timeout processing.
2579 */
2580 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002581 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002582 }
2583
Willy Tarreau59234e92008-11-30 23:51:27 +01002584 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002585 if (!tick_isset(req->analyse_exp)) {
2586 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2587 (txn->flags & TX_WAIT_NEXT_RQ) &&
2588 tick_isset(s->be->timeout.httpka))
2589 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2590 else
2591 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2592 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002593
Willy Tarreau59234e92008-11-30 23:51:27 +01002594 /* we're not ready yet */
2595 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002596
2597 failed_keep_alive:
2598 /* Here we process low-level errors for keep-alive requests. In
2599 * short, if the request is not the first one and it experiences
2600 * a timeout, read error or shutdown, we just silently close so
2601 * that the client can try again.
2602 */
2603 txn->status = 0;
2604 msg->msg_state = HTTP_MSG_RQBEFORE;
2605 req->analysers = 0;
2606 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002607 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002608 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002609 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002611
Willy Tarreaud787e662009-07-07 10:14:51 +02002612 /* OK now we have a complete HTTP request with indexed headers. Let's
2613 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002614 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2615 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2616 * points to the CRLF of the request line. req->lr points to the first
2617 * byte after the last LF. msg->col and msg->sov point to the first
2618 * byte of data. msg->eol cannot be trusted because it may have been
2619 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002620 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002621
Willy Tarreauda7ff642010-06-23 11:44:09 +02002622 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002623 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2624
Willy Tarreaub16a5742010-01-10 14:46:16 +01002625 if (txn->flags & TX_WAIT_NEXT_RQ) {
2626 /* kill the pending keep-alive timeout */
2627 txn->flags &= ~TX_WAIT_NEXT_RQ;
2628 req->analyse_exp = TICK_ETERNITY;
2629 }
2630
2631
Willy Tarreaud787e662009-07-07 10:14:51 +02002632 /* Maybe we found in invalid header name while we were configured not
2633 * to block on that, so we have to capture it now.
2634 */
2635 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002636 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002637
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 /*
2639 * 1: identify the method
2640 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002641 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002642
2643 /* we can make use of server redirect on GET and HEAD */
2644 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2645 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002646
Willy Tarreau59234e92008-11-30 23:51:27 +01002647 /*
2648 * 2: check if the URI matches the monitor_uri.
2649 * We have to do this for every request which gets in, because
2650 * the monitor-uri is defined by the frontend.
2651 */
2652 if (unlikely((s->fe->monitor_uri_len != 0) &&
2653 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002654 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002655 s->fe->monitor_uri,
2656 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002657 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002658 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002659 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002661
Willy Tarreau59234e92008-11-30 23:51:27 +01002662 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002663
Willy Tarreau59234e92008-11-30 23:51:27 +01002664 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002665 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2666 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002667
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 ret = acl_pass(ret);
2669 if (cond->pol == ACL_COND_UNLESS)
2670 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002671
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 if (ret) {
2673 /* we fail this request, let's return 503 service unavail */
2674 txn->status = 503;
2675 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2676 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002677 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002678 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002679
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 /* nothing to fail, let's reply normaly */
2681 txn->status = 200;
2682 stream_int_retnclose(req->prod, &http_200_chunk);
2683 goto return_prx_cond;
2684 }
2685
2686 /*
2687 * 3: Maybe we have to copy the original REQURI for the logs ?
2688 * Note: we cannot log anymore if the request has been
2689 * classified as invalid.
2690 */
2691 if (unlikely(s->logs.logwait & LW_REQ)) {
2692 /* we have a complete HTTP request that we must log */
2693 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2694 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002695
Willy Tarreau59234e92008-11-30 23:51:27 +01002696 if (urilen >= REQURI_LEN)
2697 urilen = REQURI_LEN - 1;
2698 memcpy(txn->uri, &req->data[msg->som], urilen);
2699 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002700
Willy Tarreau59234e92008-11-30 23:51:27 +01002701 if (!(s->logs.logwait &= ~LW_REQ))
2702 s->do_log(s);
2703 } else {
2704 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002705 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002706 }
Willy Tarreau06619262006-12-17 08:37:22 +01002707
Willy Tarreau59234e92008-11-30 23:51:27 +01002708 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002709 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2710 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002711
Willy Tarreau5b154472009-12-21 20:11:07 +01002712 /* ... and check if the request is HTTP/1.1 or above */
2713 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002714 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2715 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2716 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002717 txn->flags |= TX_REQ_VER_11;
2718
2719 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002720 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002721
Willy Tarreau88d349d2010-01-25 12:15:43 +01002722 /* if the frontend has "option http-use-proxy-header", we'll check if
2723 * we have what looks like a proxied connection instead of a connection,
2724 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2725 * Note that this is *not* RFC-compliant, however browsers and proxies
2726 * happen to do that despite being non-standard :-(
2727 * We consider that a request not beginning with either '/' or '*' is
2728 * a proxied connection, which covers both "scheme://location" and
2729 * CONNECT ip:port.
2730 */
2731 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2732 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2733 txn->flags |= TX_USE_PX_CONN;
2734
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002735 /* transfer length unknown*/
2736 txn->flags &= ~TX_REQ_XFER_LEN;
2737
Willy Tarreau59234e92008-11-30 23:51:27 +01002738 /* 5: we may need to capture headers */
2739 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002740 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002741 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002742
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002743 /* 6: determine the transfer-length.
2744 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2745 * the presence of a message-body in a REQUEST and its transfer length
2746 * must be determined that way (in order of precedence) :
2747 * 1. The presence of a message-body in a request is signaled by the
2748 * inclusion of a Content-Length or Transfer-Encoding header field
2749 * in the request's header fields. When a request message contains
2750 * both a message-body of non-zero length and a method that does
2751 * not define any semantics for that request message-body, then an
2752 * origin server SHOULD either ignore the message-body or respond
2753 * with an appropriate error message (e.g., 413). A proxy or
2754 * gateway, when presented the same request, SHOULD either forward
2755 * the request inbound with the message- body or ignore the
2756 * message-body when determining a response.
2757 *
2758 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2759 * and the "chunked" transfer-coding (Section 6.2) is used, the
2760 * transfer-length is defined by the use of this transfer-coding.
2761 * If a Transfer-Encoding header field is present and the "chunked"
2762 * transfer-coding is not present, the transfer-length is defined
2763 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002764 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002765 * 3. If a Content-Length header field is present, its decimal value in
2766 * OCTETs represents both the entity-length and the transfer-length.
2767 * If a message is received with both a Transfer-Encoding header
2768 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002769 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002770 * 4. By the server closing the connection. (Closing the connection
2771 * cannot be used to indicate the end of a request body, since that
2772 * would leave no possibility for the server to send back a response.)
2773 *
2774 * Whenever a transfer-coding is applied to a message-body, the set of
2775 * transfer-codings MUST include "chunked", unless the message indicates
2776 * it is terminated by closing the connection. When the "chunked"
2777 * transfer-coding is used, it MUST be the last transfer-coding applied
2778 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002779 */
2780
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002781 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002782 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002783 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002784 while ((txn->flags & TX_REQ_VER_11) &&
2785 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002786 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2787 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2788 else if (txn->flags & TX_REQ_TE_CHNK) {
2789 /* bad transfer-encoding (chunked followed by something else) */
2790 use_close_only = 1;
2791 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2792 break;
2793 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002794 }
2795
Willy Tarreau32b47f42009-10-18 20:55:02 +02002796 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002797 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002798 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2799 signed long long cl;
2800
2801 if (!ctx.vlen)
2802 goto return_bad_req;
2803
2804 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2805 goto return_bad_req; /* parse failure */
2806
2807 if (cl < 0)
2808 goto return_bad_req;
2809
2810 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2811 goto return_bad_req; /* already specified, was different */
2812
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002813 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002814 msg->hdr_content_len = cl;
2815 }
2816
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002817 /* bodyless requests have a known length */
2818 if (!use_close_only)
2819 txn->flags |= TX_REQ_XFER_LEN;
2820
Willy Tarreaud787e662009-07-07 10:14:51 +02002821 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002822 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002823 req->analyse_exp = TICK_ETERNITY;
2824 return 1;
2825
2826 return_bad_req:
2827 /* We centralize bad requests processing here */
2828 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2829 /* we detected a parsing error. We want to archive this request
2830 * in the dedicated proxy area for later troubleshooting.
2831 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002832 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002833 }
2834
2835 txn->req.msg_state = HTTP_MSG_ERROR;
2836 txn->status = 400;
2837 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002838
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002839 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002840 if (s->listener->counters)
2841 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002842
2843 return_prx_cond:
2844 if (!(s->flags & SN_ERR_MASK))
2845 s->flags |= SN_ERR_PRXCOND;
2846 if (!(s->flags & SN_FINST_MASK))
2847 s->flags |= SN_FINST_R;
2848
2849 req->analysers = 0;
2850 req->analyse_exp = TICK_ETERNITY;
2851 return 0;
2852}
2853
Cyril Bonté70be45d2010-10-12 00:14:35 +02002854/* We reached the stats page through a POST request.
2855 * Parse the posted data and enable/disable servers if necessary.
2856 * Returns 0 if request was parsed.
2857 * Returns 1 if there was a problem parsing the posted data.
2858 */
2859int http_process_req_stat_post(struct session *s, struct buffer *req)
2860{
2861 struct http_txn *txn = &s->txn;
2862 struct proxy *px;
2863 struct server *sv;
2864
2865 char *backend = NULL;
2866 int action = 0;
2867
2868 char *first_param, *cur_param, *next_param, *end_params;
2869
2870 first_param = req->data + txn->req.eoh + 2;
2871 end_params = first_param + txn->req.hdr_content_len;
2872
2873 cur_param = next_param = end_params;
2874
2875 if (end_params >= req->data + req->size) {
2876 /* Prevent buffer overflow */
2877 s->data_ctx.stats.st_code = STAT_STATUS_EXCD;
2878 return 1;
2879 }
2880 else if (end_params > req->data + req->l) {
2881 /* This condition also rejects a request with Expect: 100-continue */
2882 s->data_ctx.stats.st_code = STAT_STATUS_EXCD;
2883 return 1;
2884 }
2885
2886 *end_params = '\0';
2887
2888 s->data_ctx.stats.st_code = STAT_STATUS_NONE;
2889
2890 /*
2891 * Parse the parameters in reverse order to only store the last value.
2892 * From the html form, the backend and the action are at the end.
2893 */
2894 while (cur_param > first_param) {
2895 char *key, *value;
2896
2897 cur_param--;
2898 if ((*cur_param == '&') || (cur_param == first_param)) {
2899 /* Parse the key */
2900 key = cur_param;
2901 if (cur_param != first_param) {
2902 /* delimit the string for the next loop */
2903 *key++ = '\0';
2904 }
2905
2906 /* Parse the value */
2907 value = key;
2908 while (*value != '\0' && *value != '=') {
2909 value++;
2910 }
2911 if (*value == '=') {
2912 /* Ok, a value is found, we can mark the end of the key */
2913 *value++ = '\0';
2914 }
2915
2916 /* Now we can check the key to see what to do */
2917 if (!backend && strcmp(key, "b") == 0) {
2918 backend = value;
2919 }
2920 else if (!action && strcmp(key, "action") == 0) {
2921 if (strcmp(value, "disable") == 0) {
2922 action = 1;
2923 }
2924 else if (strcmp(value, "enable") == 0) {
2925 action = 2;
2926 } else {
2927 /* unknown action, no need to continue */
2928 break;
2929 }
2930 }
2931 else if (strcmp(key, "s") == 0) {
2932 if (backend && action && get_backend_server(backend, value, &px, &sv)) {
2933 switch (action) {
2934 case 1:
2935 if (! (sv->state & SRV_MAINTAIN)) {
2936 /* Not already in maintenance, we can change the server state */
2937 sv->state |= SRV_MAINTAIN;
2938 set_server_down(sv);
2939 s->data_ctx.stats.st_code = STAT_STATUS_DONE;
2940 }
2941 break;
2942 case 2:
2943 if ((sv->state & SRV_MAINTAIN)) {
2944 /* Already in maintenance, we can change the server state */
2945 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002946 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002947 s->data_ctx.stats.st_code = STAT_STATUS_DONE;
2948 }
2949 break;
2950 }
2951 }
2952 }
2953 next_param = cur_param;
2954 }
2955 }
2956 return 0;
2957}
2958
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002959/* This stream analyser runs all HTTP request processing which is common to
2960 * frontends and backends, which means blocking ACLs, filters, connection-close,
2961 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002962 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002963 * either needs more data or wants to immediately abort the request (eg: deny,
2964 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002965 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002966int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002967{
Willy Tarreaud787e662009-07-07 10:14:51 +02002968 struct http_txn *txn = &s->txn;
2969 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002970 struct acl_cond *cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002971 struct req_acl_rule *req_acl, *req_acl_final = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002972 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002973 struct cond_wordlist *wl;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002974 int del_ka, del_cl, do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002975
Willy Tarreau655dce92009-11-08 13:10:58 +01002976 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002977 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002978 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002979 return 0;
2980 }
2981
Willy Tarreau3a816292009-07-07 10:55:49 +02002982 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002983 req->analyse_exp = TICK_ETERNITY;
2984
2985 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2986 now_ms, __FUNCTION__,
2987 s,
2988 req,
2989 req->rex, req->wex,
2990 req->flags,
2991 req->l,
2992 req->analysers);
2993
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002994 /* first check whether we have some ACLs set to block this request */
2995 list_for_each_entry(cond, &px->block_cond, list) {
2996 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002997
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002998 ret = acl_pass(ret);
2999 if (cond->pol == ACL_COND_UNLESS)
3000 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003001
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003002 if (ret) {
3003 txn->status = 403;
3004 /* let's log the request time */
3005 s->logs.tv_request = now;
3006 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003007 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003008 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003009 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003010 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003011
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003012 do_stats = stats_check_uri(s, px);
3013
3014 list_for_each_entry(req_acl, (do_stats?&px->uri_auth->req_acl:&px->req_acl), list) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003015 int ret = 1;
3016
3017 if (req_acl->action >= PR_REQ_ACL_ACT_MAX)
3018 continue;
3019
3020 /* check condition, but only if attached */
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01003021 if (req_acl->cond) {
3022 ret = acl_exec_cond(req_acl->cond, px, s, txn, ACL_DIR_REQ);
3023 ret = acl_pass(ret);
Willy Tarreau51425942010-02-01 10:40:19 +01003024
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01003025 if (req_acl->cond->pol == ACL_COND_UNLESS)
3026 ret = !ret;
3027 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003028
3029 if (ret) {
3030 req_acl_final = req_acl;
3031 break;
3032 }
3033 }
3034
3035 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_DENY) {
3036 txn->status = 403;
3037 s->logs.tv_request = now;
3038 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003039 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003040 goto return_prx_cond;
3041 }
3042
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003043 /* try headers filters */
3044 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003045 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003046 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003047
Willy Tarreau59234e92008-11-30 23:51:27 +01003048 /* has the request been denied ? */
3049 if (txn->flags & TX_CLDENY) {
3050 /* no need to go further */
3051 txn->status = 403;
3052 /* let's log the request time */
3053 s->logs.tv_request = now;
3054 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003055 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003056 goto return_prx_cond;
3057 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003058
3059 /* When a connection is tarpitted, we use the tarpit timeout,
3060 * which may be the same as the connect timeout if unspecified.
3061 * If unset, then set it to zero because we really want it to
3062 * eventually expire. We build the tarpit as an analyser.
3063 */
3064 if (txn->flags & TX_CLTARPIT) {
3065 buffer_erase(s->req);
3066 /* wipe the request out so that we can drop the connection early
3067 * if the client closes first.
3068 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003069 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003070 req->analysers = 0; /* remove switching rules etc... */
3071 req->analysers |= AN_REQ_HTTP_TARPIT;
3072 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3073 if (!req->analyse_exp)
3074 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003075 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003076 return 1;
3077 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003078 }
Willy Tarreau06619262006-12-17 08:37:22 +01003079
Willy Tarreau5b154472009-12-21 20:11:07 +01003080 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3081 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003082 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3083 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003084 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3085 * avoid to redo the same work if FE and BE have the same settings (common).
3086 * The method consists in checking if options changed between the two calls
3087 * (implying that either one is non-null, or one of them is non-null and we
3088 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003089 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003090
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003091 del_cl = del_ka = 0;
3092
Willy Tarreaudc008c52010-02-01 16:20:08 +01003093 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3094 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3095 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3096 (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 +01003097 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003098
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003099 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3100 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003101 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003102 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3103 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003104 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003105 tmp = TX_CON_WANT_CLO;
3106
Willy Tarreau5b154472009-12-21 20:11:07 +01003107 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3108 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003109
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003110 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3111 /* parse the Connection header and possibly clean it */
3112 int to_del = 0;
3113 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003114 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3115 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003116 to_del |= 2; /* remove "keep-alive" */
3117 if (!(txn->flags & TX_REQ_VER_11))
3118 to_del |= 1; /* remove "close" */
3119 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003120 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003121
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003122 /* check if client or config asks for explicit close in KAL/SCL */
3123 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3124 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3125 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
3126 (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 +01003127 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003128 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
3129 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003130 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3131 }
Willy Tarreau78599912009-10-17 20:12:21 +02003132
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003133 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01003134 list_for_each_entry(wl, &px->req_add, list) {
Willy Tarreau8abd4cd2010-01-31 14:30:44 +01003135 if (wl->cond) {
3136 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
3137 ret = acl_pass(ret);
3138 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3139 ret = !ret;
3140 if (!ret)
3141 continue;
3142 }
3143
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01003144 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003145 goto return_bad_req;
3146 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003147
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003148 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) {
3149 struct chunk msg;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003150 char *realm = req_acl->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003151
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003152 if (!realm)
3153 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3154
Willy Tarreau844a7e72010-01-31 21:46:18 +01003155 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003156 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3157 txn->status = 401;
3158 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003159 /* on 401 we still count one error, because normal browsing
3160 * won't significantly increase the counter but brute force
3161 * attempts will.
3162 */
3163 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003164 goto return_prx_cond;
3165 }
3166
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003167 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003168 struct stats_admin_rule *stats_admin_rule;
3169
3170 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003171 * FIXME!!! that one is rather dangerous, we want to
3172 * make it follow standard rules (eg: clear req->analysers).
3173 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003174
Cyril Bonté474be412010-10-12 00:14:36 +02003175 /* now check whether we have some admin rules for this request */
3176 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3177 int ret = 1;
3178
3179 if (stats_admin_rule->cond) {
3180 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
3181 ret = acl_pass(ret);
3182 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3183 ret = !ret;
3184 }
3185
3186 if (ret) {
3187 /* no rule, or the rule matches */
3188 s->data_ctx.stats.flags |= STAT_ADMIN;
3189 break;
3190 }
3191 }
3192
Cyril Bonté70be45d2010-10-12 00:14:35 +02003193 /* Was the status page requested with a POST ? */
3194 if (txn->meth == HTTP_METH_POST) {
Cyril Bonté474be412010-10-12 00:14:36 +02003195 if (s->data_ctx.stats.flags & STAT_ADMIN) {
3196 http_process_req_stat_post(s, req);
3197 } else {
3198 s->data_ctx.stats.st_code = STAT_STATUS_DENY;
3199 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003200 }
3201
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003202 s->logs.tv_request = now;
3203 s->data_source = DATA_SRC_STATS;
3204 s->data_state = DATA_ST_INIT;
3205 s->task->nice = -32; /* small boost for HTTP statistics */
3206 stream_int_register_handler(s->rep->prod, http_stats_io_handler);
3207 s->rep->prod->private = s;
3208 s->rep->prod->st0 = s->rep->prod->st1 = 0;
3209 req->analysers = 0;
3210
3211 return 0;
3212
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003213 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003214
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003215 /* check whether we have some ACLs set to redirect this request */
3216 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003217 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003218
Willy Tarreauf285f542010-01-03 20:03:03 +01003219 if (rule->cond) {
3220 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3221 ret = acl_pass(ret);
3222 if (rule->cond->pol == ACL_COND_UNLESS)
3223 ret = !ret;
3224 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003225
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003226 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003227 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003228 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003229
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003230 /* build redirect message */
3231 switch(rule->code) {
3232 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003233 msg_fmt = HTTP_303;
3234 break;
3235 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003236 msg_fmt = HTTP_301;
3237 break;
3238 case 302:
3239 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003240 msg_fmt = HTTP_302;
3241 break;
3242 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003243
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003244 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003245 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003246
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003247 switch(rule->type) {
3248 case REDIRECT_TYPE_PREFIX: {
3249 const char *path;
3250 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003251
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003252 path = http_get_path(txn);
3253 /* build message using path */
3254 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003255 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003256 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3257 int qs = 0;
3258 while (qs < pathlen) {
3259 if (path[qs] == '?') {
3260 pathlen = qs;
3261 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003262 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003263 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003264 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003265 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003266 } else {
3267 path = "/";
3268 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003269 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003270
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003271 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003272 goto return_bad_req;
3273
3274 /* add prefix. Note that if prefix == "/", we don't want to
3275 * add anything, otherwise it makes it hard for the user to
3276 * configure a self-redirection.
3277 */
3278 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003279 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3280 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003281 }
3282
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003283 /* add path */
3284 memcpy(rdr.str + rdr.len, path, pathlen);
3285 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003286
3287 /* append a slash at the end of the location is needed and missing */
3288 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3289 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3290 if (rdr.len > rdr.size - 5)
3291 goto return_bad_req;
3292 rdr.str[rdr.len] = '/';
3293 rdr.len++;
3294 }
3295
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003296 break;
3297 }
3298 case REDIRECT_TYPE_LOCATION:
3299 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003300 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003301 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003302
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003303 /* add location */
3304 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3305 rdr.len += rule->rdr_len;
3306 break;
3307 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003308
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003309 if (rule->cookie_len) {
3310 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3311 rdr.len += 14;
3312 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3313 rdr.len += rule->cookie_len;
3314 memcpy(rdr.str + rdr.len, "\r\n", 2);
3315 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003316 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003317
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003318 /* add end of headers and the keep-alive/close status.
3319 * We may choose to set keep-alive if the Location begins
3320 * with a slash, because the client will come back to the
3321 * same server.
3322 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003323 txn->status = rule->code;
3324 /* let's log the request time */
3325 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003326
3327 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3328 (txn->flags & TX_REQ_XFER_LEN) &&
3329 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
3330 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3331 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3332 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003333 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003334 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3335 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3336 rdr.len += 30;
3337 } else {
3338 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3339 rdr.len += 24;
3340 }
Willy Tarreau75661452010-01-10 10:35:01 +01003341 }
3342 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3343 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003344 buffer_write(req->prod->ob, rdr.str, rdr.len);
3345 /* "eat" the request */
3346 buffer_ignore(req, msg->sov - msg->som);
3347 msg->som = msg->sov;
3348 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003349 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3350 txn->req.msg_state = HTTP_MSG_CLOSED;
3351 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003352 break;
3353 } else {
3354 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003355 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3356 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3357 rdr.len += 29;
3358 } else {
3359 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3360 rdr.len += 23;
3361 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003362 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003363 goto return_prx_cond;
3364 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003365 }
3366 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003367
Willy Tarreau2be39392010-01-03 17:24:51 +01003368 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3369 * If this happens, then the data will not come immediately, so we must
3370 * send all what we have without waiting. Note that due to the small gain
3371 * in waiting for the body of the request, it's easier to simply put the
3372 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3373 * itself once used.
3374 */
3375 req->flags |= BF_SEND_DONTWAIT;
3376
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003377 /* that's OK for us now, let's move on to next analysers */
3378 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003379
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003380 return_bad_req:
3381 /* We centralize bad requests processing here */
3382 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3383 /* we detected a parsing error. We want to archive this request
3384 * in the dedicated proxy area for later troubleshooting.
3385 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003386 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003387 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003388
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003389 txn->req.msg_state = HTTP_MSG_ERROR;
3390 txn->status = 400;
3391 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003392
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003393 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003394 if (s->listener->counters)
3395 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003396
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003397 return_prx_cond:
3398 if (!(s->flags & SN_ERR_MASK))
3399 s->flags |= SN_ERR_PRXCOND;
3400 if (!(s->flags & SN_FINST_MASK))
3401 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003402
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003403 req->analysers = 0;
3404 req->analyse_exp = TICK_ETERNITY;
3405 return 0;
3406}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003407
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003408/* This function performs all the processing enabled for the current request.
3409 * It returns 1 if the processing can continue on next analysers, or zero if it
3410 * needs more data, encounters an error, or wants to immediately abort the
3411 * request. It relies on buffers flags, and updates s->req->analysers.
3412 */
3413int http_process_request(struct session *s, struct buffer *req, int an_bit)
3414{
3415 struct http_txn *txn = &s->txn;
3416 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003417
Willy Tarreau655dce92009-11-08 13:10:58 +01003418 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003419 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003420 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003421 return 0;
3422 }
3423
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003424 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3425 now_ms, __FUNCTION__,
3426 s,
3427 req,
3428 req->rex, req->wex,
3429 req->flags,
3430 req->l,
3431 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003432
Willy Tarreau59234e92008-11-30 23:51:27 +01003433 /*
3434 * Right now, we know that we have processed the entire headers
3435 * and that unwanted requests have been filtered out. We can do
3436 * whatever we want with the remaining request. Also, now we
3437 * may have separate values for ->fe, ->be.
3438 */
Willy Tarreau06619262006-12-17 08:37:22 +01003439
Willy Tarreau59234e92008-11-30 23:51:27 +01003440 /*
3441 * If HTTP PROXY is set we simply get remote server address
3442 * parsing incoming request.
3443 */
3444 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003445 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003446 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003447
Willy Tarreau59234e92008-11-30 23:51:27 +01003448 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003449 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003450 * Note that doing so might move headers in the request, but
3451 * the fields will stay coherent and the URI will not move.
3452 * This should only be performed in the backend.
3453 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003454 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003455 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3456 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003457
Willy Tarreau59234e92008-11-30 23:51:27 +01003458 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003459 * 8: the appsession cookie was looked up very early in 1.2,
3460 * so let's do the same now.
3461 */
3462
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003463 /* It needs to look into the URI unless persistence must be ignored */
3464 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003465 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003466 }
3467
3468 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003469 * 9: add X-Forwarded-For if either the frontend or the backend
3470 * asks for it.
3471 */
3472 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3473 if (s->cli_addr.ss_family == AF_INET) {
3474 /* Add an X-Forwarded-For header unless the source IP is
3475 * in the 'except' network range.
3476 */
3477 if ((!s->fe->except_mask.s_addr ||
3478 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3479 != s->fe->except_net.s_addr) &&
3480 (!s->be->except_mask.s_addr ||
3481 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3482 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003483 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003484 unsigned char *pn;
3485 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003486
3487 /* Note: we rely on the backend to get the header name to be used for
3488 * x-forwarded-for, because the header is really meant for the backends.
3489 * However, if the backend did not specify any option, we have to rely
3490 * on the frontend's header name.
3491 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003492 if (s->be->fwdfor_hdr_len) {
3493 len = s->be->fwdfor_hdr_len;
3494 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003495 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003496 len = s->fe->fwdfor_hdr_len;
3497 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003498 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003499 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003500
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003501 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003502 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003503 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003504 }
3505 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003506 else if (s->cli_addr.ss_family == AF_INET6) {
3507 /* FIXME: for the sake of completeness, we should also support
3508 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003509 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003510 int len;
3511 char pn[INET6_ADDRSTRLEN];
3512 inet_ntop(AF_INET6,
3513 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3514 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003515
Willy Tarreau59234e92008-11-30 23:51:27 +01003516 /* Note: we rely on the backend to get the header name to be used for
3517 * x-forwarded-for, because the header is really meant for the backends.
3518 * However, if the backend did not specify any option, we have to rely
3519 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003520 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003521 if (s->be->fwdfor_hdr_len) {
3522 len = s->be->fwdfor_hdr_len;
3523 memcpy(trash, s->be->fwdfor_hdr_name, len);
3524 } else {
3525 len = s->fe->fwdfor_hdr_len;
3526 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003527 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003528 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003529
Willy Tarreau59234e92008-11-30 23:51:27 +01003530 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003531 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003532 goto return_bad_req;
3533 }
3534 }
3535
3536 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003537 * 10: add X-Original-To if either the frontend or the backend
3538 * asks for it.
3539 */
3540 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3541
3542 /* FIXME: don't know if IPv6 can handle that case too. */
3543 if (s->cli_addr.ss_family == AF_INET) {
3544 /* Add an X-Original-To header unless the destination IP is
3545 * in the 'except' network range.
3546 */
3547 if (!(s->flags & SN_FRT_ADDR_SET))
3548 get_frt_addr(s);
3549
Emeric Brun5bd86a82010-10-22 17:23:04 +02003550 if (s->frt_addr.ss_family == AF_INET &&
3551 ((!s->fe->except_mask_to.s_addr ||
3552 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3553 != s->fe->except_to.s_addr) &&
3554 (!s->be->except_mask_to.s_addr ||
3555 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3556 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003557 int len;
3558 unsigned char *pn;
3559 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3560
3561 /* Note: we rely on the backend to get the header name to be used for
3562 * x-original-to, because the header is really meant for the backends.
3563 * However, if the backend did not specify any option, we have to rely
3564 * on the frontend's header name.
3565 */
3566 if (s->be->orgto_hdr_len) {
3567 len = s->be->orgto_hdr_len;
3568 memcpy(trash, s->be->orgto_hdr_name, len);
3569 } else {
3570 len = s->fe->orgto_hdr_len;
3571 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003572 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003573 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3574
3575 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003576 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003577 goto return_bad_req;
3578 }
3579 }
3580 }
3581
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003582 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3583 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003584 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003585 unsigned int want_flags = 0;
3586
3587 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003588 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3589 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3590 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003591 want_flags |= TX_CON_CLO_SET;
3592 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003593 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3594 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3595 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003596 want_flags |= TX_CON_KAL_SET;
3597 }
3598
3599 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3600 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003601 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003602
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003603
Willy Tarreau522d6c02009-12-06 18:49:18 +01003604 /* If we have no server assigned yet and we're balancing on url_param
3605 * with a POST request, we may be interested in checking the body for
3606 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003607 */
3608 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3609 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003610 s->be->url_param_post_limit != 0 &&
3611 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003612 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003613 buffer_dont_connect(req);
3614 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003615 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003616
Willy Tarreaud98cf932009-12-27 22:54:55 +01003617 if (txn->flags & TX_REQ_XFER_LEN)
3618 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003619
Willy Tarreau59234e92008-11-30 23:51:27 +01003620 /*************************************************************
3621 * OK, that's finished for the headers. We have done what we *
3622 * could. Let's switch to the DATA state. *
3623 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003624 req->analyse_exp = TICK_ETERNITY;
3625 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003626
Willy Tarreau59234e92008-11-30 23:51:27 +01003627 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003628 /* OK let's go on with the BODY now */
3629 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003630
Willy Tarreau59234e92008-11-30 23:51:27 +01003631 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003632 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003633 /* we detected a parsing error. We want to archive this request
3634 * in the dedicated proxy area for later troubleshooting.
3635 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003636 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003637 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003638
Willy Tarreau59234e92008-11-30 23:51:27 +01003639 txn->req.msg_state = HTTP_MSG_ERROR;
3640 txn->status = 400;
3641 req->analysers = 0;
3642 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003643
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003644 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003645 if (s->listener->counters)
3646 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003647
Willy Tarreau59234e92008-11-30 23:51:27 +01003648 if (!(s->flags & SN_ERR_MASK))
3649 s->flags |= SN_ERR_PRXCOND;
3650 if (!(s->flags & SN_FINST_MASK))
3651 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003652 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003653}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003654
Willy Tarreau60b85b02008-11-30 23:28:40 +01003655/* This function is an analyser which processes the HTTP tarpit. It always
3656 * returns zero, at the beginning because it prevents any other processing
3657 * from occurring, and at the end because it terminates the request.
3658 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003659int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003660{
3661 struct http_txn *txn = &s->txn;
3662
3663 /* This connection is being tarpitted. The CLIENT side has
3664 * already set the connect expiration date to the right
3665 * timeout. We just have to check that the client is still
3666 * there and that the timeout has not expired.
3667 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003668 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003669 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3670 !tick_is_expired(req->analyse_exp, now_ms))
3671 return 0;
3672
3673 /* We will set the queue timer to the time spent, just for
3674 * logging purposes. We fake a 500 server error, so that the
3675 * attacker will not suspect his connection has been tarpitted.
3676 * It will not cause trouble to the logs because we can exclude
3677 * the tarpitted connections by filtering on the 'PT' status flags.
3678 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003679 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3680
3681 txn->status = 500;
3682 if (req->flags != BF_READ_ERROR)
3683 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3684
3685 req->analysers = 0;
3686 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003687
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003688 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003689 if (s->listener->counters)
3690 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003691
Willy Tarreau60b85b02008-11-30 23:28:40 +01003692 if (!(s->flags & SN_ERR_MASK))
3693 s->flags |= SN_ERR_PRXCOND;
3694 if (!(s->flags & SN_FINST_MASK))
3695 s->flags |= SN_FINST_T;
3696 return 0;
3697}
3698
Willy Tarreaud34af782008-11-30 23:36:37 +01003699/* This function is an analyser which processes the HTTP request body. It looks
3700 * for parameters to be used for the load balancing algorithm (url_param). It
3701 * must only be called after the standard HTTP request processing has occurred,
3702 * because it expects the request to be parsed. It returns zero if it needs to
3703 * read more data, or 1 once it has completed its analysis.
3704 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003705int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003706{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003707 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003708 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003709 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003710
3711 /* We have to parse the HTTP request body to find any required data.
3712 * "balance url_param check_post" should have been the only way to get
3713 * into this. We were brought here after HTTP header analysis, so all
3714 * related structures are ready.
3715 */
3716
Willy Tarreau522d6c02009-12-06 18:49:18 +01003717 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3718 goto missing_data;
3719
3720 if (msg->msg_state < HTTP_MSG_100_SENT) {
3721 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3722 * send an HTTP/1.1 100 Continue intermediate response.
3723 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003724 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003725 struct hdr_ctx ctx;
3726 ctx.idx = 0;
3727 /* Expect is allowed in 1.1, look for it */
3728 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3729 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3730 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3731 }
3732 }
3733 msg->msg_state = HTTP_MSG_100_SENT;
3734 }
3735
3736 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003737 /* we have msg->col and msg->sov which both point to the first
3738 * byte of message body. msg->som still points to the beginning
3739 * of the message. We must save the body in req->lr because it
3740 * survives buffer re-alignments.
3741 */
3742 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003743 if (txn->flags & TX_REQ_TE_CHNK)
3744 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3745 else
3746 msg->msg_state = HTTP_MSG_DATA;
3747 }
3748
3749 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003750 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003751 * set ->sov and ->lr to point to the body and switch to DATA or
3752 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003753 */
3754 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003755
Willy Tarreau115acb92009-12-26 13:56:06 +01003756 if (!ret)
3757 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003758 else if (ret < 0) {
3759 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003760 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003761 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003762 }
3763
Willy Tarreaud98cf932009-12-27 22:54:55 +01003764 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003765 * We have the first non-header byte in msg->col, which is either the
3766 * beginning of the chunk size or of the data. The first data byte is in
3767 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3768 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003769 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003770
3771 if (msg->hdr_content_len < limit)
3772 limit = msg->hdr_content_len;
3773
Willy Tarreau7c96f672009-12-27 22:47:25 +01003774 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003775 goto http_end;
3776
3777 missing_data:
3778 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003779 if (req->flags & BF_FULL) {
3780 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003781 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003782 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003783
Willy Tarreau522d6c02009-12-06 18:49:18 +01003784 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3785 txn->status = 408;
3786 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003787
3788 if (!(s->flags & SN_ERR_MASK))
3789 s->flags |= SN_ERR_CLITO;
3790 if (!(s->flags & SN_FINST_MASK))
3791 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003792 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003793 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003794
3795 /* we get here if we need to wait for more data */
3796 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003797 /* Not enough data. We'll re-use the http-request
3798 * timeout here. Ideally, we should set the timeout
3799 * relative to the accept() date. We just set the
3800 * request timeout once at the beginning of the
3801 * request.
3802 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003803 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003804 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003805 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003806 return 0;
3807 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003808
3809 http_end:
3810 /* The situation will not evolve, so let's give up on the analysis. */
3811 s->logs.tv_request = now; /* update the request timer to reflect full request */
3812 req->analysers &= ~an_bit;
3813 req->analyse_exp = TICK_ETERNITY;
3814 return 1;
3815
3816 return_bad_req: /* let's centralize all bad requests */
3817 txn->req.msg_state = HTTP_MSG_ERROR;
3818 txn->status = 400;
3819 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3820
Willy Tarreau79ebac62010-06-07 13:47:49 +02003821 if (!(s->flags & SN_ERR_MASK))
3822 s->flags |= SN_ERR_PRXCOND;
3823 if (!(s->flags & SN_FINST_MASK))
3824 s->flags |= SN_FINST_R;
3825
Willy Tarreau522d6c02009-12-06 18:49:18 +01003826 return_err_msg:
3827 req->analysers = 0;
3828 s->fe->counters.failed_req++;
3829 if (s->listener->counters)
3830 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003831 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003832}
3833
Willy Tarreau610ecce2010-01-04 21:15:02 +01003834/* Terminate current transaction and prepare a new one. This is very tricky
3835 * right now but it works.
3836 */
3837void http_end_txn_clean_session(struct session *s)
3838{
3839 /* FIXME: We need a more portable way of releasing a backend's and a
3840 * server's connections. We need a safer way to reinitialize buffer
3841 * flags. We also need a more accurate method for computing per-request
3842 * data.
3843 */
3844 http_silent_debug(__LINE__, s);
3845
3846 s->req->cons->flags |= SI_FL_NOLINGER;
3847 s->req->cons->shutr(s->req->cons);
3848 s->req->cons->shutw(s->req->cons);
3849
3850 http_silent_debug(__LINE__, s);
3851
3852 if (s->flags & SN_BE_ASSIGNED)
3853 s->be->beconn--;
3854
3855 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3856 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003857 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003858
3859 if (s->txn.status) {
3860 int n;
3861
3862 n = s->txn.status / 100;
3863 if (n < 1 || n > 5)
3864 n = 0;
3865
3866 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01003867 s->fe->counters.fe.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003868
Willy Tarreau24657792010-02-26 10:30:28 +01003869 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003870 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01003871 s->be->counters.be.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003872 }
3873
3874 /* don't count other requests' data */
3875 s->logs.bytes_in -= s->req->l - s->req->send_max;
3876 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3877
3878 /* let's do a final log if we need it */
3879 if (s->logs.logwait &&
3880 !(s->flags & SN_MONITOR) &&
3881 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3882 s->do_log(s);
3883 }
3884
3885 s->logs.accept_date = date; /* user-visible date for logging */
3886 s->logs.tv_accept = now; /* corrected date for internal use */
3887 tv_zero(&s->logs.tv_request);
3888 s->logs.t_queue = -1;
3889 s->logs.t_connect = -1;
3890 s->logs.t_data = -1;
3891 s->logs.t_close = 0;
3892 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3893 s->logs.srv_queue_size = 0; /* we will get this number soon */
3894
3895 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3896 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3897
3898 if (s->pend_pos)
3899 pendconn_free(s->pend_pos);
3900
3901 if (s->srv) {
3902 if (s->flags & SN_CURR_SESS) {
3903 s->flags &= ~SN_CURR_SESS;
3904 s->srv->cur_sess--;
3905 }
3906 if (may_dequeue_tasks(s->srv, s->be))
3907 process_srv_queue(s->srv);
3908 }
3909
3910 if (unlikely(s->srv_conn))
3911 sess_change_server(s, NULL);
3912 s->srv = NULL;
3913
3914 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3915 s->req->cons->fd = -1; /* just to help with debugging */
3916 s->req->cons->err_type = SI_ET_NONE;
3917 s->req->cons->err_loc = NULL;
3918 s->req->cons->exp = TICK_ETERNITY;
3919 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003920 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3921 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003922 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 +01003923 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3924 s->txn.meth = 0;
3925 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003926 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003927 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003928 s->req->cons->flags |= SI_FL_INDEP_STR;
3929
3930 /* if the request buffer is not empty, it means we're
3931 * about to process another request, so send pending
3932 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003933 * Just don't do this if the buffer is close to be full,
3934 * because the request will wait for it to flush a little
3935 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003936 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003937 if (s->req->l > s->req->send_max) {
3938 if (s->rep->send_max &&
3939 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003940 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3941 s->rep->flags |= BF_EXPECT_MORE;
3942 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003943
3944 /* we're removing the analysers, we MUST re-enable events detection */
3945 buffer_auto_read(s->req);
3946 buffer_auto_close(s->req);
3947 buffer_auto_read(s->rep);
3948 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003949
3950 /* make ->lr point to the first non-forwarded byte */
3951 s->req->lr = s->req->w + s->req->send_max;
3952 if (s->req->lr >= s->req->data + s->req->size)
3953 s->req->lr -= s->req->size;
3954 s->rep->lr = s->rep->w + s->rep->send_max;
3955 if (s->rep->lr >= s->rep->data + s->rep->size)
3956 s->rep->lr -= s->req->size;
3957
Willy Tarreau342b11c2010-11-24 16:22:09 +01003958 s->req->analysers = s->listener->analysers;
3959 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003960 s->rep->analysers = 0;
3961
3962 http_silent_debug(__LINE__, s);
3963}
3964
3965
3966/* This function updates the request state machine according to the response
3967 * state machine and buffer flags. It returns 1 if it changes anything (flag
3968 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3969 * it is only used to find when a request/response couple is complete. Both
3970 * this function and its equivalent should loop until both return zero. It
3971 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3972 */
3973int http_sync_req_state(struct session *s)
3974{
3975 struct buffer *buf = s->req;
3976 struct http_txn *txn = &s->txn;
3977 unsigned int old_flags = buf->flags;
3978 unsigned int old_state = txn->req.msg_state;
3979
3980 http_silent_debug(__LINE__, s);
3981 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3982 return 0;
3983
3984 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003985 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003986 * We can shut the read side unless we want to abort_on_close,
3987 * or we have a POST request. The issue with POST requests is
3988 * that some browsers still send a CRLF after the request, and
3989 * this CRLF must be read so that it does not remain in the kernel
3990 * buffers, otherwise a close could cause an RST on some systems
3991 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003992 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003993 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003994 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003995
3996 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3997 goto wait_other_side;
3998
3999 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4000 /* The server has not finished to respond, so we
4001 * don't want to move in order not to upset it.
4002 */
4003 goto wait_other_side;
4004 }
4005
4006 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4007 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004008 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 txn->req.msg_state = HTTP_MSG_TUNNEL;
4010 goto wait_other_side;
4011 }
4012
4013 /* When we get here, it means that both the request and the
4014 * response have finished receiving. Depending on the connection
4015 * mode, we'll have to wait for the last bytes to leave in either
4016 * direction, and sometimes for a close to be effective.
4017 */
4018
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004019 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4020 /* Server-close mode : queue a connection close to the server */
4021 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004023 }
4024 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4025 /* Option forceclose is set, or either side wants to close,
4026 * let's enforce it now that we're not expecting any new
4027 * data to come. The caller knows the session is complete
4028 * once both states are CLOSED.
4029 */
4030 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004031 buffer_shutr_now(buf);
4032 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004034 }
4035 else {
4036 /* The last possible modes are keep-alive and tunnel. Since tunnel
4037 * mode does not set the body analyser, we can't reach this place
4038 * in tunnel mode, so we're left with keep-alive only.
4039 * This mode is currently not implemented, we switch to tunnel mode.
4040 */
4041 buffer_auto_read(buf);
4042 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004043 }
4044
4045 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4046 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004047 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4048
Willy Tarreau610ecce2010-01-04 21:15:02 +01004049 if (!(buf->flags & BF_OUT_EMPTY)) {
4050 txn->req.msg_state = HTTP_MSG_CLOSING;
4051 goto http_msg_closing;
4052 }
4053 else {
4054 txn->req.msg_state = HTTP_MSG_CLOSED;
4055 goto http_msg_closed;
4056 }
4057 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004058 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004059 }
4060
4061 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4062 http_msg_closing:
4063 /* nothing else to forward, just waiting for the output buffer
4064 * to be empty and for the shutw_now to take effect.
4065 */
4066 if (buf->flags & BF_OUT_EMPTY) {
4067 txn->req.msg_state = HTTP_MSG_CLOSED;
4068 goto http_msg_closed;
4069 }
4070 else if (buf->flags & BF_SHUTW) {
4071 txn->req.msg_state = HTTP_MSG_ERROR;
4072 goto wait_other_side;
4073 }
4074 }
4075
4076 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4077 http_msg_closed:
4078 goto wait_other_side;
4079 }
4080
4081 wait_other_side:
4082 http_silent_debug(__LINE__, s);
4083 return txn->req.msg_state != old_state || buf->flags != old_flags;
4084}
4085
4086
4087/* This function updates the response state machine according to the request
4088 * state machine and buffer flags. It returns 1 if it changes anything (flag
4089 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4090 * it is only used to find when a request/response couple is complete. Both
4091 * this function and its equivalent should loop until both return zero. It
4092 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4093 */
4094int http_sync_res_state(struct session *s)
4095{
4096 struct buffer *buf = s->rep;
4097 struct http_txn *txn = &s->txn;
4098 unsigned int old_flags = buf->flags;
4099 unsigned int old_state = txn->rsp.msg_state;
4100
4101 http_silent_debug(__LINE__, s);
4102 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4103 return 0;
4104
4105 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4106 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004107 * still monitor the server connection for a possible close
4108 * while the request is being uploaded, so we don't disable
4109 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004110 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004111 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004112
4113 if (txn->req.msg_state == HTTP_MSG_ERROR)
4114 goto wait_other_side;
4115
4116 if (txn->req.msg_state < HTTP_MSG_DONE) {
4117 /* The client seems to still be sending data, probably
4118 * because we got an error response during an upload.
4119 * We have the choice of either breaking the connection
4120 * or letting it pass through. Let's do the later.
4121 */
4122 goto wait_other_side;
4123 }
4124
4125 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4126 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004127 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004128 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4129 goto wait_other_side;
4130 }
4131
4132 /* When we get here, it means that both the request and the
4133 * response have finished receiving. Depending on the connection
4134 * mode, we'll have to wait for the last bytes to leave in either
4135 * direction, and sometimes for a close to be effective.
4136 */
4137
4138 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4139 /* Server-close mode : shut read and wait for the request
4140 * side to close its output buffer. The caller will detect
4141 * when we're in DONE and the other is in CLOSED and will
4142 * catch that for the final cleanup.
4143 */
4144 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4145 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004146 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004147 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4148 /* Option forceclose is set, or either side wants to close,
4149 * let's enforce it now that we're not expecting any new
4150 * data to come. The caller knows the session is complete
4151 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004152 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004153 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4154 buffer_shutr_now(buf);
4155 buffer_shutw_now(buf);
4156 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004157 }
4158 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004159 /* The last possible modes are keep-alive and tunnel. Since tunnel
4160 * mode does not set the body analyser, we can't reach this place
4161 * in tunnel mode, so we're left with keep-alive only.
4162 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004163 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004164 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004165 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004166 }
4167
4168 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4169 /* if we've just closed an output, let's switch */
4170 if (!(buf->flags & BF_OUT_EMPTY)) {
4171 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4172 goto http_msg_closing;
4173 }
4174 else {
4175 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4176 goto http_msg_closed;
4177 }
4178 }
4179 goto wait_other_side;
4180 }
4181
4182 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4183 http_msg_closing:
4184 /* nothing else to forward, just waiting for the output buffer
4185 * to be empty and for the shutw_now to take effect.
4186 */
4187 if (buf->flags & BF_OUT_EMPTY) {
4188 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4189 goto http_msg_closed;
4190 }
4191 else if (buf->flags & BF_SHUTW) {
4192 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreauae526782010-03-04 20:34:23 +01004193 s->be->counters.cli_aborts++;
4194 if (s->srv)
4195 s->srv->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004196 goto wait_other_side;
4197 }
4198 }
4199
4200 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4201 http_msg_closed:
4202 /* drop any pending data */
4203 buffer_ignore(buf, buf->l - buf->send_max);
4204 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004205 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004206 goto wait_other_side;
4207 }
4208
4209 wait_other_side:
4210 http_silent_debug(__LINE__, s);
4211 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4212}
4213
4214
4215/* Resync the request and response state machines. Return 1 if either state
4216 * changes.
4217 */
4218int http_resync_states(struct session *s)
4219{
4220 struct http_txn *txn = &s->txn;
4221 int old_req_state = txn->req.msg_state;
4222 int old_res_state = txn->rsp.msg_state;
4223
4224 http_silent_debug(__LINE__, s);
4225 http_sync_req_state(s);
4226 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004227 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004228 if (!http_sync_res_state(s))
4229 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004230 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004231 if (!http_sync_req_state(s))
4232 break;
4233 }
4234 http_silent_debug(__LINE__, s);
4235 /* OK, both state machines agree on a compatible state.
4236 * There are a few cases we're interested in :
4237 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4238 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4239 * directions, so let's simply disable both analysers.
4240 * - HTTP_MSG_CLOSED on the response only means we must abort the
4241 * request.
4242 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4243 * with server-close mode means we've completed one request and we
4244 * must re-initialize the server connection.
4245 */
4246
4247 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4248 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4249 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4250 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4251 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004252 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004253 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004254 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004255 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004256 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004257 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004258 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4259 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004260 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004261 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004262 s->rep->analysers = 0;
4263 buffer_auto_close(s->rep);
4264 buffer_auto_read(s->rep);
4265 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004266 buffer_abort(s->req);
4267 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004268 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004269 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004270 }
4271 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4272 txn->rsp.msg_state == HTTP_MSG_DONE &&
4273 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4274 /* server-close: terminate this server connection and
4275 * reinitialize a fresh-new transaction.
4276 */
4277 http_end_txn_clean_session(s);
4278 }
4279
4280 http_silent_debug(__LINE__, s);
4281 return txn->req.msg_state != old_req_state ||
4282 txn->rsp.msg_state != old_res_state;
4283}
4284
Willy Tarreaud98cf932009-12-27 22:54:55 +01004285/* This function is an analyser which forwards request body (including chunk
4286 * sizes if any). It is called as soon as we must forward, even if we forward
4287 * zero byte. The only situation where it must not be called is when we're in
4288 * tunnel mode and we want to forward till the close. It's used both to forward
4289 * remaining data and to resync after end of body. It expects the msg_state to
4290 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4291 * read more data, or 1 once we can go on with next request or end the session.
4292 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4293 * bytes of pending data + the headers if not already done (between som and sov).
4294 * It eventually adjusts som to match sov after the data in between have been sent.
4295 */
4296int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4297{
4298 struct http_txn *txn = &s->txn;
4299 struct http_msg *msg = &s->txn.req;
4300
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004301 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4302 return 0;
4303
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004304 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4305 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004306 /* Output closed while we were sending data. We must abort and
4307 * wake the other side up.
4308 */
4309 msg->msg_state = HTTP_MSG_ERROR;
4310 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004311 return 1;
4312 }
4313
Willy Tarreau4fe41902010-06-07 22:27:41 +02004314 /* in most states, we should abort in case of early close */
4315 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004316
4317 /* Note that we don't have to send 100-continue back because we don't
4318 * need the data to complete our job, and it's up to the server to
4319 * decide whether to return 100, 417 or anything else in return of
4320 * an "Expect: 100-continue" header.
4321 */
4322
4323 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4324 /* we have msg->col and msg->sov which both point to the first
4325 * byte of message body. msg->som still points to the beginning
4326 * of the message. We must save the body in req->lr because it
4327 * survives buffer re-alignments.
4328 */
4329 req->lr = req->data + msg->sov;
4330 if (txn->flags & TX_REQ_TE_CHNK)
4331 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4332 else {
4333 msg->msg_state = HTTP_MSG_DATA;
4334 }
4335 }
4336
Willy Tarreaud98cf932009-12-27 22:54:55 +01004337 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004338 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004339 /* we may have some data pending */
4340 if (msg->hdr_content_len || msg->som != msg->sov) {
4341 int bytes = msg->sov - msg->som;
4342 if (bytes < 0) /* sov may have wrapped at the end */
4343 bytes += req->size;
4344 buffer_forward(req, bytes + msg->hdr_content_len);
4345 msg->hdr_content_len = 0; /* don't forward that again */
4346 msg->som = msg->sov;
4347 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004348
Willy Tarreaucaabe412010-01-03 23:08:28 +01004349 if (msg->msg_state == HTTP_MSG_DATA) {
4350 /* must still forward */
4351 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004352 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004353
4354 /* nothing left to forward */
4355 if (txn->flags & TX_REQ_TE_CHNK)
4356 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004357 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004358 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004359 }
4360 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004361 /* read the chunk size and assign it to ->hdr_content_len, then
4362 * set ->sov and ->lr to point to the body and switch to DATA or
4363 * TRAILERS state.
4364 */
4365 int ret = http_parse_chunk_size(req, msg);
4366
4367 if (!ret)
4368 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004369 else if (ret < 0) {
4370 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004371 if (msg->err_pos >= 0)
4372 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004373 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004374 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004375 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreau57f5c122010-12-02 00:37:14 +01004376 /* Don't set a PUSH at the end of that chunk if it's not the last one */
4377 if (msg->msg_state == HTTP_MSG_DATA)
4378 req->flags |= BF_EXPECT_MORE;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004379 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004380 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4381 /* we want the CRLF after the data */
4382 int ret;
4383
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004384 req->lr = req->w + req->send_max;
4385 if (req->lr >= req->data + req->size)
4386 req->lr -= req->size;
4387
Willy Tarreaud98cf932009-12-27 22:54:55 +01004388 ret = http_skip_chunk_crlf(req, msg);
4389
4390 if (ret == 0)
4391 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004392 else if (ret < 0) {
4393 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004394 if (msg->err_pos >= 0)
4395 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004396 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004397 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004398 /* we're in MSG_CHUNK_SIZE now */
4399 }
4400 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4401 int ret = http_forward_trailers(req, msg);
4402
4403 if (ret == 0)
4404 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004405 else if (ret < 0) {
4406 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004407 if (msg->err_pos >= 0)
4408 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004409 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004410 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004411 /* we're in HTTP_MSG_DONE now */
4412 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004413 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004414 int old_state = msg->msg_state;
4415
Willy Tarreau610ecce2010-01-04 21:15:02 +01004416 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004417 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004418 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4419 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4420 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004421 if (http_resync_states(s)) {
4422 /* some state changes occurred, maybe the analyser
4423 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004424 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004425 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4426 if (req->flags & BF_SHUTW) {
4427 /* request errors are most likely due to
4428 * the server aborting the transfer.
4429 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004430 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004431 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004432 if (msg->err_pos >= 0)
4433 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004434 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004435 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004436 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004437 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004438
4439 /* If "option abortonclose" is set on the backend, we
4440 * want to monitor the client's connection and forward
4441 * any shutdown notification to the server, which will
4442 * decide whether to close or to go on processing the
4443 * request.
4444 */
4445 if (s->be->options & PR_O_ABRT_CLOSE) {
4446 buffer_auto_read(req);
4447 buffer_auto_close(req);
4448 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004449 else if (s->txn.meth == HTTP_METH_POST) {
4450 /* POST requests may require to read extra CRLF
4451 * sent by broken browsers and which could cause
4452 * an RST to be sent upon close on some systems
4453 * (eg: Linux).
4454 */
4455 buffer_auto_read(req);
4456 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004457
Willy Tarreau610ecce2010-01-04 21:15:02 +01004458 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004459 }
4460 }
4461
Willy Tarreaud98cf932009-12-27 22:54:55 +01004462 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004463 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004464 if (req->flags & BF_SHUTR) {
4465 if (!(s->flags & SN_ERR_MASK))
4466 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004467 if (!(s->flags & SN_FINST_MASK)) {
4468 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4469 s->flags |= SN_FINST_H;
4470 else
4471 s->flags |= SN_FINST_D;
4472 }
4473
4474 s->fe->counters.cli_aborts++;
4475 if (s->fe != s->be)
4476 s->be->counters.cli_aborts++;
4477 if (s->srv)
4478 s->srv->counters.cli_aborts++;
4479
4480 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004481 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004482
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004483 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004484 if (req->flags & BF_SHUTW)
4485 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004486
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004487 /* When TE: chunked is used, we need to get there again to parse remaining
4488 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4489 */
4490 if (txn->flags & TX_REQ_TE_CHNK)
4491 buffer_dont_close(req);
4492
Willy Tarreau610ecce2010-01-04 21:15:02 +01004493 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004494 return 0;
4495
Willy Tarreaud98cf932009-12-27 22:54:55 +01004496 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004497 s->fe->counters.failed_req++;
4498 if (s->listener->counters)
4499 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004500 return_bad_req_stats_ok:
4501 txn->req.msg_state = HTTP_MSG_ERROR;
4502 if (txn->status) {
4503 /* Note: we don't send any error if some data were already sent */
4504 stream_int_retnclose(req->prod, NULL);
4505 } else {
4506 txn->status = 400;
4507 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4508 }
4509 req->analysers = 0;
4510 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004511
4512 if (!(s->flags & SN_ERR_MASK))
4513 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004514 if (!(s->flags & SN_FINST_MASK)) {
4515 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4516 s->flags |= SN_FINST_H;
4517 else
4518 s->flags |= SN_FINST_D;
4519 }
4520 return 0;
4521
4522 aborted_xfer:
4523 txn->req.msg_state = HTTP_MSG_ERROR;
4524 if (txn->status) {
4525 /* Note: we don't send any error if some data were already sent */
4526 stream_int_retnclose(req->prod, NULL);
4527 } else {
4528 txn->status = 502;
4529 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4530 }
4531 req->analysers = 0;
4532 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4533
4534 s->fe->counters.srv_aborts++;
4535 if (s->fe != s->be)
4536 s->be->counters.srv_aborts++;
4537 if (s->srv)
4538 s->srv->counters.srv_aborts++;
4539
4540 if (!(s->flags & SN_ERR_MASK))
4541 s->flags |= SN_ERR_SRVCL;
4542 if (!(s->flags & SN_FINST_MASK)) {
4543 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4544 s->flags |= SN_FINST_H;
4545 else
4546 s->flags |= SN_FINST_D;
4547 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004548 return 0;
4549}
4550
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004551/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4552 * processing can continue on next analysers, or zero if it either needs more
4553 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4554 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4555 * when it has nothing left to do, and may remove any analyser when it wants to
4556 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004557 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004558int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004559{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004560 struct http_txn *txn = &s->txn;
4561 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004562 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004563 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004564 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004565 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004566
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004567 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 +02004568 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004569 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004570 rep,
4571 rep->rex, rep->wex,
4572 rep->flags,
4573 rep->l,
4574 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004575
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004576 /*
4577 * Now parse the partial (or complete) lines.
4578 * We will check the response syntax, and also join multi-line
4579 * headers. An index of all the lines will be elaborated while
4580 * parsing.
4581 *
4582 * For the parsing, we use a 28 states FSM.
4583 *
4584 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004585 * rep->data + msg->som = beginning of response
4586 * rep->data + msg->eoh = end of processed headers / start of current one
4587 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004588 * rep->lr = first non-visited byte
4589 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004590 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004591 */
4592
Willy Tarreau83e3af02009-12-28 17:39:57 +01004593 /* There's a protected area at the end of the buffer for rewriting
4594 * purposes. We don't want to start to parse the request if the
4595 * protected area is affected, because we may have to move processed
4596 * data later, which is much more complicated.
4597 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004598 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4599 if (unlikely((rep->flags & BF_FULL) ||
4600 rep->r < rep->lr ||
4601 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4602 if (rep->send_max) {
4603 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004604 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4605 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004606 buffer_dont_close(rep);
4607 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4608 return 0;
4609 }
4610 if (rep->l <= rep->size - global.tune.maxrewrite)
4611 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004612 }
4613
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004614 if (likely(rep->lr < rep->r))
4615 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004616 }
4617
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004618 /* 1: we might have to print this header in debug mode */
4619 if (unlikely((global.mode & MODE_DEBUG) &&
4620 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004621 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004622 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004623 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004624
Willy Tarreau663308b2010-06-07 14:06:08 +02004625 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004626 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004627 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004628
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629 sol += hdr_idx_first_pos(&txn->hdr_idx);
4630 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004631
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004632 while (cur_idx) {
4633 eol = sol + txn->hdr_idx.v[cur_idx].len;
4634 debug_hdr("srvhdr", s, sol, eol);
4635 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4636 cur_idx = txn->hdr_idx.v[cur_idx].next;
4637 }
4638 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004639
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004640 /*
4641 * Now we quickly check if we have found a full valid response.
4642 * If not so, we check the FD and buffer states before leaving.
4643 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004644 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004645 * responses are checked first.
4646 *
4647 * Depending on whether the client is still there or not, we
4648 * may send an error response back or not. Note that normally
4649 * we should only check for HTTP status there, and check I/O
4650 * errors somewhere else.
4651 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004652
Willy Tarreau655dce92009-11-08 13:10:58 +01004653 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004654 /* Invalid response */
4655 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4656 /* we detected a parsing error. We want to archive this response
4657 * in the dedicated proxy area for later troubleshooting.
4658 */
4659 hdr_response_bad:
4660 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004661 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004662
4663 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004664 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004665 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004666 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4667 }
Willy Tarreau64648412010-03-05 10:41:54 +01004668 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004669 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004670 rep->analysers = 0;
4671 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004672 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004673 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004674 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4675
4676 if (!(s->flags & SN_ERR_MASK))
4677 s->flags |= SN_ERR_PRXCOND;
4678 if (!(s->flags & SN_FINST_MASK))
4679 s->flags |= SN_FINST_H;
4680
4681 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004682 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004683
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004684 /* too large response does not fit in buffer. */
4685 else if (rep->flags & BF_FULL) {
4686 goto hdr_response_bad;
4687 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004688
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004689 /* read error */
4690 else if (rep->flags & BF_READ_ERROR) {
4691 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004692 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004693
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004694 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004695 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004696 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004697 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4698 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004699
Willy Tarreau90deb182010-01-07 00:20:41 +01004700 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004701 rep->analysers = 0;
4702 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004703 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004704 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004705 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004706
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004707 if (!(s->flags & SN_ERR_MASK))
4708 s->flags |= SN_ERR_SRVCL;
4709 if (!(s->flags & SN_FINST_MASK))
4710 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004711 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004712 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004713
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004714 /* read timeout : return a 504 to the client. */
4715 else if (rep->flags & BF_READ_TIMEOUT) {
4716 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004717 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004718
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004719 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004720 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004721 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004722 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4723 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004724
Willy Tarreau90deb182010-01-07 00:20:41 +01004725 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004726 rep->analysers = 0;
4727 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004728 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004729 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004730 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004731
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004732 if (!(s->flags & SN_ERR_MASK))
4733 s->flags |= SN_ERR_SRVTO;
4734 if (!(s->flags & SN_FINST_MASK))
4735 s->flags |= SN_FINST_H;
4736 return 0;
4737 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004738
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004739 /* close from server */
4740 else if (rep->flags & BF_SHUTR) {
4741 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004742 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004743
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004744 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004745 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004746 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004747 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4748 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004749
Willy Tarreau90deb182010-01-07 00:20:41 +01004750 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004751 rep->analysers = 0;
4752 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004753 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004754 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004755 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004756
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004757 if (!(s->flags & SN_ERR_MASK))
4758 s->flags |= SN_ERR_SRVCL;
4759 if (!(s->flags & SN_FINST_MASK))
4760 s->flags |= SN_FINST_H;
4761 return 0;
4762 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004763
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004764 /* write error to client (we don't send any message then) */
4765 else if (rep->flags & BF_WRITE_ERROR) {
4766 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004767 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004768
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004769 s->be->counters.failed_resp++;
4770 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004771 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004772
4773 if (!(s->flags & SN_ERR_MASK))
4774 s->flags |= SN_ERR_CLICL;
4775 if (!(s->flags & SN_FINST_MASK))
4776 s->flags |= SN_FINST_H;
4777
4778 /* process_session() will take care of the error */
4779 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004780 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004781
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004782 buffer_dont_close(rep);
4783 return 0;
4784 }
4785
4786 /* More interesting part now : we know that we have a complete
4787 * response which at least looks like HTTP. We have an indicator
4788 * of each header's length, so we can parse them quickly.
4789 */
4790
4791 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004792 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004793
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004794 /*
4795 * 1: get the status code
4796 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004797 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004798 if (n < 1 || n > 5)
4799 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004800 /* when the client triggers a 4xx from the server, it's most often due
4801 * to a missing object or permission. These events should be tracked
4802 * because if they happen often, it may indicate a brute force or a
4803 * vulnerability scan.
4804 */
4805 if (n == 4)
4806 session_inc_http_err_ctr(s);
4807
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004808 if (s->srv)
4809 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004810
Willy Tarreau5b154472009-12-21 20:11:07 +01004811 /* check if the response is HTTP/1.1 or above */
4812 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004813 ((msg->sol[5] > '1') ||
4814 ((msg->sol[5] == '1') &&
4815 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004816 txn->flags |= TX_RES_VER_11;
4817
4818 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004819 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 +01004820
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004821 /* transfer length unknown*/
4822 txn->flags &= ~TX_RES_XFER_LEN;
4823
Willy Tarreau962c3f42010-01-10 00:15:35 +01004824 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004825
Willy Tarreau39650402010-03-15 19:44:39 +01004826 /* Adjust server's health based on status code. Note: status codes 501
4827 * and 505 are triggered on demand by client request, so we must not
4828 * count them as server failures.
4829 */
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004830 if (s->srv) {
4831 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
4832 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4833 else
4834 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4835 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004836
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004837 /*
4838 * 2: check for cacheability.
4839 */
4840
4841 switch (txn->status) {
4842 case 200:
4843 case 203:
4844 case 206:
4845 case 300:
4846 case 301:
4847 case 410:
4848 /* RFC2616 @13.4:
4849 * "A response received with a status code of
4850 * 200, 203, 206, 300, 301 or 410 MAY be stored
4851 * by a cache (...) unless a cache-control
4852 * directive prohibits caching."
4853 *
4854 * RFC2616 @9.5: POST method :
4855 * "Responses to this method are not cacheable,
4856 * unless the response includes appropriate
4857 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004858 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004859 if (likely(txn->meth != HTTP_METH_POST) &&
4860 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4861 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4862 break;
4863 default:
4864 break;
4865 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004866
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004867 /*
4868 * 3: we may need to capture headers
4869 */
4870 s->logs.logwait &= ~LW_RESP;
4871 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004872 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004873 txn->rsp.cap, s->fe->rsp_cap);
4874
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004875 /* 4: determine the transfer-length.
4876 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4877 * the presence of a message-body in a RESPONSE and its transfer length
4878 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004879 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004880 * All responses to the HEAD request method MUST NOT include a
4881 * message-body, even though the presence of entity-header fields
4882 * might lead one to believe they do. All 1xx (informational), 204
4883 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4884 * message-body. All other responses do include a message-body,
4885 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004886 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004887 * 1. Any response which "MUST NOT" include a message-body (such as the
4888 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4889 * always terminated by the first empty line after the header fields,
4890 * regardless of the entity-header fields present in the message.
4891 *
4892 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4893 * the "chunked" transfer-coding (Section 6.2) is used, the
4894 * transfer-length is defined by the use of this transfer-coding.
4895 * If a Transfer-Encoding header field is present and the "chunked"
4896 * transfer-coding is not present, the transfer-length is defined by
4897 * the sender closing the connection.
4898 *
4899 * 3. If a Content-Length header field is present, its decimal value in
4900 * OCTETs represents both the entity-length and the transfer-length.
4901 * If a message is received with both a Transfer-Encoding header
4902 * field and a Content-Length header field, the latter MUST be ignored.
4903 *
4904 * 4. If the message uses the media type "multipart/byteranges", and
4905 * the transfer-length is not otherwise specified, then this self-
4906 * delimiting media type defines the transfer-length. This media
4907 * type MUST NOT be used unless the sender knows that the recipient
4908 * can parse it; the presence in a request of a Range header with
4909 * multiple byte-range specifiers from a 1.1 client implies that the
4910 * client can parse multipart/byteranges responses.
4911 *
4912 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004913 */
4914
4915 /* Skip parsing if no content length is possible. The response flags
4916 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004917 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004918 * FIXME: should we parse anyway and return an error on chunked encoding ?
4919 */
4920 if (txn->meth == HTTP_METH_HEAD ||
4921 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004922 txn->status == 204 || txn->status == 304) {
4923 txn->flags |= TX_RES_XFER_LEN;
4924 goto skip_content_length;
4925 }
4926
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004927 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004928 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004929 while ((txn->flags & TX_RES_VER_11) &&
4930 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004931 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4932 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4933 else if (txn->flags & TX_RES_TE_CHNK) {
4934 /* bad transfer-encoding (chunked followed by something else) */
4935 use_close_only = 1;
4936 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4937 break;
4938 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004939 }
4940
4941 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4942 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004943 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004944 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4945 signed long long cl;
4946
4947 if (!ctx.vlen)
4948 goto hdr_response_bad;
4949
4950 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4951 goto hdr_response_bad; /* parse failure */
4952
4953 if (cl < 0)
4954 goto hdr_response_bad;
4955
4956 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4957 goto hdr_response_bad; /* already specified, was different */
4958
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004959 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004960 msg->hdr_content_len = cl;
4961 }
4962
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004963 /* FIXME: we should also implement the multipart/byterange method.
4964 * For now on, we resort to close mode in this case (unknown length).
4965 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004966skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004967
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004968 /* end of job, return OK */
4969 rep->analysers &= ~an_bit;
4970 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004971 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004972 return 1;
4973}
4974
4975/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004976 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4977 * and updates t->rep->analysers. It might make sense to explode it into several
4978 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004979 */
4980int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4981{
4982 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004983 struct http_msg *msg = &txn->rsp;
4984 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004985 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004986
4987 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4988 now_ms, __FUNCTION__,
4989 t,
4990 rep,
4991 rep->rex, rep->wex,
4992 rep->flags,
4993 rep->l,
4994 rep->analysers);
4995
Willy Tarreau655dce92009-11-08 13:10:58 +01004996 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004997 return 0;
4998
4999 rep->analysers &= ~an_bit;
5000 rep->analyse_exp = TICK_ETERNITY;
5001
Willy Tarreau5b154472009-12-21 20:11:07 +01005002 /* Now we have to check if we need to modify the Connection header.
5003 * This is more difficult on the response than it is on the request,
5004 * because we can have two different HTTP versions and we don't know
5005 * how the client will interprete a response. For instance, let's say
5006 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5007 * HTTP/1.1 response without any header. Maybe it will bound itself to
5008 * HTTP/1.0 because it only knows about it, and will consider the lack
5009 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5010 * the lack of header as a keep-alive. Thus we will use two flags
5011 * indicating how a request MAY be understood by the client. In case
5012 * of multiple possibilities, we'll fix the header to be explicit. If
5013 * ambiguous cases such as both close and keepalive are seen, then we
5014 * will fall back to explicit close. Note that we won't take risks with
5015 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005016 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005017 */
5018
Willy Tarreaudc008c52010-02-01 16:20:08 +01005019 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5020 txn->status == 101)) {
5021 /* Either we've established an explicit tunnel, or we're
5022 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005023 * to understand the next protocols. We have to switch to tunnel
5024 * mode, so that we transfer the request and responses then let
5025 * this protocol pass unmodified. When we later implement specific
5026 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005027 * header which contains information about that protocol for
5028 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005029 */
5030 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5031 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005032 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5033 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5034 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005035 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005036
Willy Tarreau60466522010-01-18 19:08:45 +01005037 /* on unknown transfer length, we must close */
5038 if (!(txn->flags & TX_RES_XFER_LEN) &&
5039 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5040 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005041
Willy Tarreau60466522010-01-18 19:08:45 +01005042 /* now adjust header transformations depending on current state */
5043 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5044 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5045 to_del |= 2; /* remove "keep-alive" on any response */
5046 if (!(txn->flags & TX_RES_VER_11))
5047 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005048 }
Willy Tarreau60466522010-01-18 19:08:45 +01005049 else { /* SCL / KAL */
5050 to_del |= 1; /* remove "close" on any response */
5051 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
5052 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005053 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005054
Willy Tarreau60466522010-01-18 19:08:45 +01005055 /* Parse and remove some headers from the connection header */
5056 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005057
Willy Tarreau60466522010-01-18 19:08:45 +01005058 /* Some keep-alive responses are converted to Server-close if
5059 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005060 */
Willy Tarreau60466522010-01-18 19:08:45 +01005061 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5062 if ((txn->flags & TX_HDR_CONN_CLO) ||
5063 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
5064 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005065 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005066 }
5067
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005068 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005069 /*
5070 * 3: we will have to evaluate the filters.
5071 * As opposed to version 1.2, now they will be evaluated in the
5072 * filters order and not in the header order. This means that
5073 * each filter has to be validated among all headers.
5074 *
5075 * Filters are tried with ->be first, then with ->fe if it is
5076 * different from ->be.
5077 */
5078
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005079 cur_proxy = t->be;
5080 while (1) {
5081 struct proxy *rule_set = cur_proxy;
5082
5083 /* try headers filters */
5084 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005085 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005086 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005087 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005088 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005089 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
5090 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005091 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005092 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005093 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005094 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005095 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01005096 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005097 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005098 if (!(t->flags & SN_ERR_MASK))
5099 t->flags |= SN_ERR_PRXCOND;
5100 if (!(t->flags & SN_FINST_MASK))
5101 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005102 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005103 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005104 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005105
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005106 /* has the response been denied ? */
5107 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01005108 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005109 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005110
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005111 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005112 if (t->listener->counters)
5113 t->listener->counters->denied_resp++;
5114
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005115 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005116 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005117
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005118 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005119 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005120 if (txn->status < 200)
5121 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005122 if (wl->cond) {
5123 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5124 ret = acl_pass(ret);
5125 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5126 ret = !ret;
5127 if (!ret)
5128 continue;
5129 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005130 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005131 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005132 }
5133
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005134 /* check whether we're already working on the frontend */
5135 if (cur_proxy == t->fe)
5136 break;
5137 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005138 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005139
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005140 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005141 * We may be facing a 100-continue response, in which case this
5142 * is not the right response, and we're waiting for the next one.
5143 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005144 * next one.
5145 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005146 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005147 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005148 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005149 msg->msg_state = HTTP_MSG_RPBEFORE;
5150 txn->status = 0;
5151 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5152 return 1;
5153 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005154 else if (unlikely(txn->status < 200))
5155 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005156
5157 /* we don't have any 1xx status code now */
5158
5159 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005160 * 4: check for server cookie.
5161 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005162 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5163 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005164 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005165
Willy Tarreaubaaee002006-06-26 02:48:02 +02005166
Willy Tarreaua15645d2007-03-18 16:22:39 +01005167 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005168 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005169 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005170 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005171 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005172
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005173 /*
5174 * 6: add server cookie in the response if needed
5175 */
Willy Tarreauef4f3912010-10-07 21:00:29 +02005176 if ((t->srv) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005177 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005178 (!(t->flags & SN_DIRECT) ||
5179 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5180 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5181 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5182 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005183 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5184 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005185 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005186 /* the server is known, it's not the one the client requested, or the
5187 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005188 * insert a set-cookie here, except if we want to insert only on POST
5189 * requests and this one isn't. Note that servers which don't have cookies
5190 * (eg: some backup servers) will return a full cookie removal request.
5191 */
Willy Tarreauef4f3912010-10-07 21:00:29 +02005192 if (!t->srv->cookie) {
5193 len = sprintf(trash,
5194 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5195 t->be->cookie_name);
5196 }
5197 else {
5198 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, t->srv->cookie);
5199
5200 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5201 /* emit last_date, which is mandatory */
5202 trash[len++] = COOKIE_DELIM_DATE;
5203 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5204 if (t->be->cookie_maxlife) {
5205 /* emit first_date, which is either the original one or
5206 * the current date.
5207 */
5208 trash[len++] = COOKIE_DELIM_DATE;
5209 s30tob64(txn->cookie_first_date ?
5210 txn->cookie_first_date >> 2 :
5211 (date.tv_sec+3) >> 2, trash + len);
5212 len += 5;
5213 }
5214 }
5215 len += sprintf(trash + len, "; path=/");
5216 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005217
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005218 if (t->be->cookie_domain)
5219 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005220
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005221 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005222 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005223 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005224
Willy Tarreauf1348312010-10-07 15:54:11 +02005225 txn->flags &= ~TX_SCK_MASK;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005226 if (t->srv->cookie && (t->flags & SN_DIRECT))
5227 /* the server did not change, only the date was updated */
5228 txn->flags |= TX_SCK_UPDATED;
5229 else
5230 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005231
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005232 /* Here, we will tell an eventual cache on the client side that we don't
5233 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5234 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5235 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5236 */
5237 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005238
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005239 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5240
5241 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005242 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005243 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005244 }
5245 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005246
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005247 /*
5248 * 7: check if result will be cacheable with a cookie.
5249 * We'll block the response if security checks have caught
5250 * nasty things such as a cacheable cookie.
5251 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005252 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5253 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005254 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005255
5256 /* we're in presence of a cacheable response containing
5257 * a set-cookie header. We'll block it as requested by
5258 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005259 */
Willy Tarreau8365f932009-03-15 23:11:49 +01005260 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005261 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005262
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005263 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005264 if (t->listener->counters)
5265 t->listener->counters->denied_resp++;
5266
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005267 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5268 t->be->id, t->srv?t->srv->id:"<dispatch>");
5269 send_log(t->be, LOG_ALERT,
5270 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5271 t->be->id, t->srv?t->srv->id:"<dispatch>");
5272 goto return_srv_prx_502;
5273 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005274
5275 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005276 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005277 */
Willy Tarreau60466522010-01-18 19:08:45 +01005278 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5279 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5280 unsigned int want_flags = 0;
5281
5282 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5283 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5284 /* we want a keep-alive response here. Keep-alive header
5285 * required if either side is not 1.1.
5286 */
5287 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5288 want_flags |= TX_CON_KAL_SET;
5289 }
5290 else {
5291 /* we want a close response here. Close header required if
5292 * the server is 1.1, regardless of the client.
5293 */
5294 if (txn->flags & TX_RES_VER_11)
5295 want_flags |= TX_CON_CLO_SET;
5296 }
5297
5298 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5299 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005300 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005301
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005302 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005303 if ((txn->flags & TX_RES_XFER_LEN) ||
5304 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005305 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005306
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005307 /*************************************************************
5308 * OK, that's finished for the headers. We have done what we *
5309 * could. Let's switch to the DATA state. *
5310 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005311
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005312 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005313
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005314 /* if the user wants to log as soon as possible, without counting
5315 * bytes from the server, then this is the right moment. We have
5316 * to temporarily assign bytes_out to log what we currently have.
5317 */
5318 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5319 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5320 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005321 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005322 t->logs.bytes_out = 0;
5323 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005324
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005325 /* Note: we must not try to cheat by jumping directly to DATA,
5326 * otherwise we would not let the client side wake up.
5327 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005328
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005329 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005330 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005331 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005332}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005333
Willy Tarreaud98cf932009-12-27 22:54:55 +01005334/* This function is an analyser which forwards response body (including chunk
5335 * sizes if any). It is called as soon as we must forward, even if we forward
5336 * zero byte. The only situation where it must not be called is when we're in
5337 * tunnel mode and we want to forward till the close. It's used both to forward
5338 * remaining data and to resync after end of body. It expects the msg_state to
5339 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5340 * read more data, or 1 once we can go on with next request or end the session.
5341 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
5342 * bytes of pending data + the headers if not already done (between som and sov).
5343 * It eventually adjusts som to match sov after the data in between have been sent.
5344 */
5345int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5346{
5347 struct http_txn *txn = &s->txn;
5348 struct http_msg *msg = &s->txn.rsp;
5349
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005350 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5351 return 0;
5352
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005353 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005354 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005355 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005356 /* Output closed while we were sending data. We must abort and
5357 * wake the other side up.
5358 */
5359 msg->msg_state = HTTP_MSG_ERROR;
5360 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005361 return 1;
5362 }
5363
Willy Tarreau4fe41902010-06-07 22:27:41 +02005364 /* in most states, we should abort in case of early close */
5365 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005366
Willy Tarreaud98cf932009-12-27 22:54:55 +01005367 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5368 /* we have msg->col and msg->sov which both point to the first
5369 * byte of message body. msg->som still points to the beginning
5370 * of the message. We must save the body in req->lr because it
5371 * survives buffer re-alignments.
5372 */
5373 res->lr = res->data + msg->sov;
5374 if (txn->flags & TX_RES_TE_CHNK)
5375 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5376 else {
5377 msg->msg_state = HTTP_MSG_DATA;
5378 }
5379 }
5380
Willy Tarreaud98cf932009-12-27 22:54:55 +01005381 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005382 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005383 /* we may have some data pending */
5384 if (msg->hdr_content_len || msg->som != msg->sov) {
5385 int bytes = msg->sov - msg->som;
5386 if (bytes < 0) /* sov may have wrapped at the end */
5387 bytes += res->size;
5388 buffer_forward(res, bytes + msg->hdr_content_len);
5389 msg->hdr_content_len = 0; /* don't forward that again */
5390 msg->som = msg->sov;
5391 }
5392
Willy Tarreaucaabe412010-01-03 23:08:28 +01005393 if (msg->msg_state == HTTP_MSG_DATA) {
5394 /* must still forward */
5395 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005396 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005397
5398 /* nothing left to forward */
5399 if (txn->flags & TX_RES_TE_CHNK)
5400 msg->msg_state = HTTP_MSG_DATA_CRLF;
5401 else
5402 msg->msg_state = HTTP_MSG_DONE;
5403 }
5404 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005405 /* read the chunk size and assign it to ->hdr_content_len, then
5406 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5407 */
5408 int ret = http_parse_chunk_size(res, msg);
5409
5410 if (!ret)
5411 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005412 else if (ret < 0) {
5413 if (msg->err_pos >= 0)
5414 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005415 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005416 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005417 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreau57f5c122010-12-02 00:37:14 +01005418 /* Don't set a PUSH at the end of that chunk if it's not the last one */
5419 if (msg->msg_state == HTTP_MSG_DATA)
5420 res->flags |= BF_EXPECT_MORE;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005421 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005422 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5423 /* we want the CRLF after the data */
5424 int ret;
5425
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005426 res->lr = res->w + res->send_max;
5427 if (res->lr >= res->data + res->size)
5428 res->lr -= res->size;
5429
Willy Tarreaud98cf932009-12-27 22:54:55 +01005430 ret = http_skip_chunk_crlf(res, msg);
5431
5432 if (!ret)
5433 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005434 else if (ret < 0) {
5435 if (msg->err_pos >= 0)
5436 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005437 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005438 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005439 /* we're in MSG_CHUNK_SIZE now */
5440 }
5441 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5442 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005443
Willy Tarreaud98cf932009-12-27 22:54:55 +01005444 if (ret == 0)
5445 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005446 else if (ret < 0) {
5447 if (msg->err_pos >= 0)
5448 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005449 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005450 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005451 /* we're in HTTP_MSG_DONE now */
5452 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005453 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005454 int old_state = msg->msg_state;
5455
Willy Tarreau610ecce2010-01-04 21:15:02 +01005456 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005457 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005458 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5459 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5460 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005461 if (http_resync_states(s)) {
5462 http_silent_debug(__LINE__, s);
5463 /* some state changes occurred, maybe the analyser
5464 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005465 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005466 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5467 if (res->flags & BF_SHUTW) {
5468 /* response errors are most likely due to
5469 * the client aborting the transfer.
5470 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005471 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005472 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005473 if (msg->err_pos >= 0)
5474 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005475 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005476 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005477 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005478 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005479 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005480 }
5481 }
5482
Willy Tarreaud98cf932009-12-27 22:54:55 +01005483 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005484 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005485 if (res->flags & BF_SHUTR) {
5486 if (!(s->flags & SN_ERR_MASK))
5487 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01005488 s->be->counters.srv_aborts++;
5489 if (s->srv)
5490 s->srv->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005491 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005492 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005493
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005494 if (res->flags & BF_SHUTW)
5495 goto aborted_xfer;
5496
Willy Tarreau40dba092010-03-04 18:14:51 +01005497 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005498 if (!s->req->analysers)
5499 goto return_bad_res;
5500
Willy Tarreaud98cf932009-12-27 22:54:55 +01005501 /* forward the chunk size as well as any pending data */
5502 if (msg->hdr_content_len || msg->som != msg->sov) {
5503 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
5504 msg->hdr_content_len = 0; /* don't forward that again */
5505 msg->som = msg->sov;
5506 }
5507
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005508 /* When TE: chunked is used, we need to get there again to parse remaining
5509 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5510 * Similarly, with keep-alive on the client side, we don't want to forward a
5511 * close.
5512 */
5513 if ((txn->flags & TX_RES_TE_CHNK) ||
5514 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5515 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5516 buffer_dont_close(res);
5517
Willy Tarreaud98cf932009-12-27 22:54:55 +01005518 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005519 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005520 return 0;
5521
Willy Tarreau40dba092010-03-04 18:14:51 +01005522 return_bad_res: /* let's centralize all bad responses */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005523 s->be->counters.failed_resp++;
5524 if (s->srv)
5525 s->srv->counters.failed_resp++;
5526
5527 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005528 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005529 /* don't send any error message as we're in the body */
5530 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005531 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005532 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5533 if (s->srv)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005534 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005535
5536 if (!(s->flags & SN_ERR_MASK))
5537 s->flags |= SN_ERR_PRXCOND;
5538 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005539 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005540 return 0;
5541
5542 aborted_xfer:
5543 txn->rsp.msg_state = HTTP_MSG_ERROR;
5544 /* don't send any error message as we're in the body */
5545 stream_int_retnclose(res->cons, NULL);
5546 res->analysers = 0;
5547 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5548
5549 s->fe->counters.cli_aborts++;
5550 if (s->fe != s->be)
5551 s->be->counters.cli_aborts++;
5552 if (s->srv)
5553 s->srv->counters.cli_aborts++;
5554
5555 if (!(s->flags & SN_ERR_MASK))
5556 s->flags |= SN_ERR_CLICL;
5557 if (!(s->flags & SN_FINST_MASK))
5558 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005559 return 0;
5560}
5561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005562/* Iterate the same filter through all request headers.
5563 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005564 * Since it can manage the switch to another backend, it updates the per-proxy
5565 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005566 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005567int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005568{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005569 char term;
5570 char *cur_ptr, *cur_end, *cur_next;
5571 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005572 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005573 struct hdr_idx_elem *cur_hdr;
5574 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005575
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005576 last_hdr = 0;
5577
Willy Tarreau962c3f42010-01-10 00:15:35 +01005578 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005579 old_idx = 0;
5580
5581 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005582 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005583 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005584 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005585 (exp->action == ACT_ALLOW ||
5586 exp->action == ACT_DENY ||
5587 exp->action == ACT_TARPIT))
5588 return 0;
5589
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005590 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005591 if (!cur_idx)
5592 break;
5593
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005594 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005595 cur_ptr = cur_next;
5596 cur_end = cur_ptr + cur_hdr->len;
5597 cur_next = cur_end + cur_hdr->cr + 1;
5598
5599 /* Now we have one header between cur_ptr and cur_end,
5600 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005601 */
5602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005603 /* The annoying part is that pattern matching needs
5604 * that we modify the contents to null-terminate all
5605 * strings before testing them.
5606 */
5607
5608 term = *cur_end;
5609 *cur_end = '\0';
5610
5611 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5612 switch (exp->action) {
5613 case ACT_SETBE:
5614 /* It is not possible to jump a second time.
5615 * FIXME: should we return an HTTP/500 here so that
5616 * the admin knows there's a problem ?
5617 */
5618 if (t->be != t->fe)
5619 break;
5620
5621 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005622 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005623 last_hdr = 1;
5624 break;
5625
5626 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005627 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005628 last_hdr = 1;
5629 break;
5630
5631 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005632 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005633 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005634
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005635 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005636 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005637 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005638
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005639 break;
5640
5641 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005642 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005643 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005644
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005645 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005646 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005647 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005648
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005649 break;
5650
5651 case ACT_REPLACE:
5652 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5653 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5654 /* FIXME: if the user adds a newline in the replacement, the
5655 * index will not be recalculated for now, and the new line
5656 * will not be counted as a new header.
5657 */
5658
5659 cur_end += delta;
5660 cur_next += delta;
5661 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005662 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005663 break;
5664
5665 case ACT_REMOVE:
5666 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5667 cur_next += delta;
5668
Willy Tarreaufa355d42009-11-29 18:12:29 +01005669 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005670 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5671 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005672 cur_hdr->len = 0;
5673 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005674 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005675 break;
5676
5677 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005678 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005679 if (cur_end)
5680 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005682 /* keep the link from this header to next one in case of later
5683 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005684 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005685 old_idx = cur_idx;
5686 }
5687 return 0;
5688}
5689
5690
5691/* Apply the filter to the request line.
5692 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5693 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005694 * Since it can manage the switch to another backend, it updates the per-proxy
5695 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005696 */
5697int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5698{
5699 char term;
5700 char *cur_ptr, *cur_end;
5701 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005702 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005703 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005704
Willy Tarreau58f10d72006-12-04 02:26:12 +01005705
Willy Tarreau3d300592007-03-18 18:34:41 +01005706 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005707 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005708 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005709 (exp->action == ACT_ALLOW ||
5710 exp->action == ACT_DENY ||
5711 exp->action == ACT_TARPIT))
5712 return 0;
5713 else if (exp->action == ACT_REMOVE)
5714 return 0;
5715
5716 done = 0;
5717
Willy Tarreau962c3f42010-01-10 00:15:35 +01005718 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005719 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005720
5721 /* Now we have the request line between cur_ptr and cur_end */
5722
5723 /* The annoying part is that pattern matching needs
5724 * that we modify the contents to null-terminate all
5725 * strings before testing them.
5726 */
5727
5728 term = *cur_end;
5729 *cur_end = '\0';
5730
5731 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5732 switch (exp->action) {
5733 case ACT_SETBE:
5734 /* It is not possible to jump a second time.
5735 * FIXME: should we return an HTTP/500 here so that
5736 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005737 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005738 if (t->be != t->fe)
5739 break;
5740
5741 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005742 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005743 done = 1;
5744 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005745
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005746 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005747 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005748 done = 1;
5749 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005751 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005752 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005753
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005754 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005755 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005756 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005757
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005758 done = 1;
5759 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005760
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005761 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005762 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005763
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005764 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005765 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005766 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005767
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005768 done = 1;
5769 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005770
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005771 case ACT_REPLACE:
5772 *cur_end = term; /* restore the string terminator */
5773 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5774 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5775 /* FIXME: if the user adds a newline in the replacement, the
5776 * index will not be recalculated for now, and the new line
5777 * will not be counted as a new header.
5778 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005779
Willy Tarreaufa355d42009-11-29 18:12:29 +01005780 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005781 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005782 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005783 HTTP_MSG_RQMETH,
5784 cur_ptr, cur_end + 1,
5785 NULL, NULL);
5786 if (unlikely(!cur_end))
5787 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005788
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005789 /* we have a full request and we know that we have either a CR
5790 * or an LF at <ptr>.
5791 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005792 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5793 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005794 /* there is no point trying this regex on headers */
5795 return 1;
5796 }
5797 }
5798 *cur_end = term; /* restore the string terminator */
5799 return done;
5800}
Willy Tarreau97de6242006-12-27 17:18:38 +01005801
Willy Tarreau58f10d72006-12-04 02:26:12 +01005802
Willy Tarreau58f10d72006-12-04 02:26:12 +01005803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005804/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005805 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005806 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005807 * unparsable request. Since it can manage the switch to another backend, it
5808 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005809 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005810int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005811{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005812 struct http_txn *txn = &s->txn;
5813 struct hdr_exp *exp;
5814
5815 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005816 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005817
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005818 /*
5819 * The interleaving of transformations and verdicts
5820 * makes it difficult to decide to continue or stop
5821 * the evaluation.
5822 */
5823
Willy Tarreau6c123b12010-01-28 20:22:06 +01005824 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5825 break;
5826
Willy Tarreau3d300592007-03-18 18:34:41 +01005827 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005828 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005829 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005830 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005831
5832 /* if this filter had a condition, evaluate it now and skip to
5833 * next filter if the condition does not match.
5834 */
5835 if (exp->cond) {
5836 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5837 ret = acl_pass(ret);
5838 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5839 ret = !ret;
5840
5841 if (!ret)
5842 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005843 }
5844
5845 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005846 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005847 if (unlikely(ret < 0))
5848 return -1;
5849
5850 if (likely(ret == 0)) {
5851 /* The filter did not match the request, it can be
5852 * iterated through all headers.
5853 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005854 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005855 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005856 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005857 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005858}
5859
5860
Willy Tarreaua15645d2007-03-18 16:22:39 +01005861
Willy Tarreau58f10d72006-12-04 02:26:12 +01005862/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005863 * Try to retrieve the server associated to the appsession.
5864 * If the server is found, it's assigned to the session.
5865 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005866void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005867 struct http_txn *txn = &t->txn;
5868 appsess *asession = NULL;
5869 char *sessid_temp = NULL;
5870
Cyril Bontéb21570a2009-11-29 20:04:48 +01005871 if (len > t->be->appsession_len) {
5872 len = t->be->appsession_len;
5873 }
5874
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005875 if (t->be->options2 & PR_O2_AS_REQL) {
5876 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005877 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005878 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005879 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005880 }
5881
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005882 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005883 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5884 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5885 return;
5886 }
5887
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005888 memcpy(txn->sessid, buf, len);
5889 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005890 }
5891
5892 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5893 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5894 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5895 return;
5896 }
5897
Cyril Bontéb21570a2009-11-29 20:04:48 +01005898 memcpy(sessid_temp, buf, len);
5899 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005900
5901 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5902 /* free previously allocated memory */
5903 pool_free2(apools.sessid, sessid_temp);
5904
5905 if (asession != NULL) {
5906 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5907 if (!(t->be->options2 & PR_O2_AS_REQL))
5908 asession->request_count++;
5909
5910 if (asession->serverid != NULL) {
5911 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005912
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005913 while (srv) {
5914 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005915 if ((srv->state & SRV_RUNNING) ||
5916 (t->be->options & PR_O_PERSIST) ||
5917 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005918 /* we found the server and it's usable */
5919 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005920 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005921 t->flags |= SN_DIRECT | SN_ASSIGNED;
5922 t->srv = srv;
5923 break;
5924 } else {
5925 txn->flags &= ~TX_CK_MASK;
5926 txn->flags |= TX_CK_DOWN;
5927 }
5928 }
5929 srv = srv->next;
5930 }
5931 }
5932 }
5933}
5934
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005935/* Find the end of a cookie value contained between <s> and <e>. It works the
5936 * same way as with headers above except that the semi-colon also ends a token.
5937 * See RFC2965 for more information. Note that it requires a valid header to
5938 * return a valid result.
5939 */
5940char *find_cookie_value_end(char *s, const char *e)
5941{
5942 int quoted, qdpair;
5943
5944 quoted = qdpair = 0;
5945 for (; s < e; s++) {
5946 if (qdpair) qdpair = 0;
5947 else if (quoted) {
5948 if (*s == '\\') qdpair = 1;
5949 else if (*s == '"') quoted = 0;
5950 }
5951 else if (*s == '"') quoted = 1;
5952 else if (*s == ',' || *s == ';') return s;
5953 }
5954 return s;
5955}
5956
5957/* Delete a value in a header between delimiters <from> and <next> in buffer
5958 * <buf>. The number of characters displaced is returned, and the pointer to
5959 * the first delimiter is updated if required. The function tries as much as
5960 * possible to respect the following principles :
5961 * - replace <from> delimiter by the <next> one unless <from> points to a
5962 * colon, in which case <next> is simply removed
5963 * - set exactly one space character after the new first delimiter, unless
5964 * there are not enough characters in the block being moved to do so.
5965 * - remove unneeded spaces before the previous delimiter and after the new
5966 * one.
5967 *
5968 * It is the caller's responsibility to ensure that :
5969 * - <from> points to a valid delimiter or the colon ;
5970 * - <next> points to a valid delimiter or the final CR/LF ;
5971 * - there are non-space chars before <from> ;
5972 * - there is a CR/LF at or after <next>.
5973 */
5974int del_hdr_value(struct buffer *buf, char **from, char *next)
5975{
5976 char *prev = *from;
5977
5978 if (*prev == ':') {
5979 /* We're removing the first value, preserve the colon and add a
5980 * space if possible.
5981 */
5982 if (!http_is_crlf[(unsigned char)*next])
5983 next++;
5984 prev++;
5985 if (prev < next)
5986 *prev++ = ' ';
5987
5988 while (http_is_spht[(unsigned char)*next])
5989 next++;
5990 } else {
5991 /* Remove useless spaces before the old delimiter. */
5992 while (http_is_spht[(unsigned char)*(prev-1)])
5993 prev--;
5994 *from = prev;
5995
5996 /* copy the delimiter and if possible a space if we're
5997 * not at the end of the line.
5998 */
5999 if (!http_is_crlf[(unsigned char)*next]) {
6000 *prev++ = *next++;
6001 if (prev + 1 < next)
6002 *prev++ = ' ';
6003 while (http_is_spht[(unsigned char)*next])
6004 next++;
6005 }
6006 }
6007 return buffer_replace2(buf, prev, next, NULL, 0);
6008}
6009
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006010/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006011 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006012 * desirable to call it only when needed. This code is quite complex because
6013 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6014 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006015 */
6016void manage_client_side_cookies(struct session *t, struct buffer *req)
6017{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006018 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006019 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006020 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006021 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6022 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006023
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006024 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006025 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006026 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006027
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006028 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006029 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006030 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006031
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006032 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006033 hdr_beg = hdr_next;
6034 hdr_end = hdr_beg + cur_hdr->len;
6035 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006036
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006037 /* We have one full header between hdr_beg and hdr_end, and the
6038 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006039 * "Cookie:" headers.
6040 */
6041
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006042 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006043 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006044 old_idx = cur_idx;
6045 continue;
6046 }
6047
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006048 del_from = NULL; /* nothing to be deleted */
6049 preserve_hdr = 0; /* assume we may kill the whole header */
6050
Willy Tarreau58f10d72006-12-04 02:26:12 +01006051 /* Now look for cookies. Conforming to RFC2109, we have to support
6052 * attributes whose name begin with a '$', and associate them with
6053 * the right cookie, if we want to delete this cookie.
6054 * So there are 3 cases for each cookie read :
6055 * 1) it's a special attribute, beginning with a '$' : ignore it.
6056 * 2) it's a server id cookie that we *MAY* want to delete : save
6057 * some pointers on it (last semi-colon, beginning of cookie...)
6058 * 3) it's an application cookie : we *MAY* have to delete a previous
6059 * "special" cookie.
6060 * At the end of loop, if a "special" cookie remains, we may have to
6061 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006062 * *MUST* delete it.
6063 *
6064 * Note: RFC2965 is unclear about the processing of spaces around
6065 * the equal sign in the ATTR=VALUE form. A careful inspection of
6066 * the RFC explicitly allows spaces before it, and not within the
6067 * tokens (attrs or values). An inspection of RFC2109 allows that
6068 * too but section 10.1.3 lets one think that spaces may be allowed
6069 * after the equal sign too, resulting in some (rare) buggy
6070 * implementations trying to do that. So let's do what servers do.
6071 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6072 * allowed quoted strings in values, with any possible character
6073 * after a backslash, including control chars and delimitors, which
6074 * causes parsing to become ambiguous. Browsers also allow spaces
6075 * within values even without quotes.
6076 *
6077 * We have to keep multiple pointers in order to support cookie
6078 * removal at the beginning, middle or end of header without
6079 * corrupting the header. All of these headers are valid :
6080 *
6081 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6082 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6083 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6084 * | | | | | | | | |
6085 * | | | | | | | | hdr_end <--+
6086 * | | | | | | | +--> next
6087 * | | | | | | +----> val_end
6088 * | | | | | +-----------> val_beg
6089 * | | | | +--------------> equal
6090 * | | | +----------------> att_end
6091 * | | +---------------------> att_beg
6092 * | +--------------------------> prev
6093 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006094 */
6095
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006096 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6097 /* Iterate through all cookies on this line */
6098
6099 /* find att_beg */
6100 att_beg = prev + 1;
6101 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6102 att_beg++;
6103
6104 /* find att_end : this is the first character after the last non
6105 * space before the equal. It may be equal to hdr_end.
6106 */
6107 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006108
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006109 while (equal < hdr_end) {
6110 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006111 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006112 if (http_is_spht[(unsigned char)*equal++])
6113 continue;
6114 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006115 }
6116
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006117 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6118 * is between <att_beg> and <equal>, both may be identical.
6119 */
6120
6121 /* look for end of cookie if there is an equal sign */
6122 if (equal < hdr_end && *equal == '=') {
6123 /* look for the beginning of the value */
6124 val_beg = equal + 1;
6125 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6126 val_beg++;
6127
6128 /* find the end of the value, respecting quotes */
6129 next = find_cookie_value_end(val_beg, hdr_end);
6130
6131 /* make val_end point to the first white space or delimitor after the value */
6132 val_end = next;
6133 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6134 val_end--;
6135 } else {
6136 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006137 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006138
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006139 /* We have nothing to do with attributes beginning with '$'. However,
6140 * they will automatically be removed if a header before them is removed,
6141 * since they're supposed to be linked together.
6142 */
6143 if (*att_beg == '$')
6144 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006145
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006146 /* Ignore cookies with no equal sign */
6147 if (equal == next) {
6148 /* This is not our cookie, so we must preserve it. But if we already
6149 * scheduled another cookie for removal, we cannot remove the
6150 * complete header, but we can remove the previous block itself.
6151 */
6152 preserve_hdr = 1;
6153 if (del_from != NULL) {
6154 int delta = del_hdr_value(req, &del_from, prev);
6155 val_end += delta;
6156 next += delta;
6157 hdr_end += delta;
6158 hdr_next += delta;
6159 cur_hdr->len += delta;
6160 http_msg_move_end(&txn->req, delta);
6161 prev = del_from;
6162 del_from = NULL;
6163 }
6164 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006165 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006166
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006167 /* if there are spaces around the equal sign, we need to
6168 * strip them otherwise we'll get trouble for cookie captures,
6169 * or even for rewrites. Since this happens extremely rarely,
6170 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006171 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006172 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6173 int stripped_before = 0;
6174 int stripped_after = 0;
6175
6176 if (att_end != equal) {
6177 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6178 equal += stripped_before;
6179 val_beg += stripped_before;
6180 }
6181
6182 if (val_beg > equal + 1) {
6183 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6184 val_beg += stripped_after;
6185 stripped_before += stripped_after;
6186 }
6187
6188 val_end += stripped_before;
6189 next += stripped_before;
6190 hdr_end += stripped_before;
6191 hdr_next += stripped_before;
6192 cur_hdr->len += stripped_before;
6193 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006194 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006195 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006196
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006197 /* First, let's see if we want to capture this cookie. We check
6198 * that we don't already have a client side cookie, because we
6199 * can only capture one. Also as an optimisation, we ignore
6200 * cookies shorter than the declared name.
6201 */
6202 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6203 (val_end - att_beg >= t->fe->capture_namelen) &&
6204 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6205 int log_len = val_end - att_beg;
6206
6207 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6208 Alert("HTTP logging : out of memory.\n");
6209 } else {
6210 if (log_len > t->fe->capture_len)
6211 log_len = t->fe->capture_len;
6212 memcpy(txn->cli_cookie, att_beg, log_len);
6213 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006214 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006215 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006216
Willy Tarreaubca99692010-10-06 19:25:55 +02006217 /* Persistence cookies in passive, rewrite or insert mode have the
6218 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006219 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006220 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006221 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006222 * For cookies in prefix mode, the form is :
6223 *
6224 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006225 */
6226 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6227 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6228 struct server *srv = t->be->srv;
6229 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006230
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006231 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6232 * have the server ID between val_beg and delim, and the original cookie between
6233 * delim+1 and val_end. Otherwise, delim==val_end :
6234 *
6235 * Cookie: NAME=SRV; # in all but prefix modes
6236 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6237 * | || || | |+-> next
6238 * | || || | +--> val_end
6239 * | || || +---------> delim
6240 * | || |+------------> val_beg
6241 * | || +-------------> att_end = equal
6242 * | |+-----------------> att_beg
6243 * | +------------------> prev
6244 * +-------------------------> hdr_beg
6245 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006246
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006247 if (t->be->options & PR_O_COOK_PFX) {
6248 for (delim = val_beg; delim < val_end; delim++)
6249 if (*delim == COOKIE_DELIM)
6250 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006251 } else {
6252 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006253 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006254 /* Now check if the cookie contains a date field, which would
6255 * appear after a vertical bar ('|') just after the server name
6256 * and before the delimiter.
6257 */
6258 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6259 if (vbar1) {
6260 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006261 * right is the last seen date. It is a base64 encoded
6262 * 30-bit value representing the UNIX date since the
6263 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006264 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006265 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006266 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006267 if (val_end - vbar1 >= 5) {
6268 val = b64tos30(vbar1);
6269 if (val > 0)
6270 txn->cookie_last_date = val << 2;
6271 }
6272 /* look for a second vertical bar */
6273 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6274 if (vbar1 && (val_end - vbar1 > 5)) {
6275 val = b64tos30(vbar1 + 1);
6276 if (val > 0)
6277 txn->cookie_first_date = val << 2;
6278 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006279 }
6280 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006281
Willy Tarreauf64d1412010-10-07 20:06:11 +02006282 /* if the cookie has an expiration date and the proxy wants to check
6283 * it, then we do that now. We first check if the cookie is too old,
6284 * then only if it has expired. We detect strict overflow because the
6285 * time resolution here is not great (4 seconds). Cookies with dates
6286 * in the future are ignored if their offset is beyond one day. This
6287 * allows an admin to fix timezone issues without expiring everyone
6288 * and at the same time avoids keeping unwanted side effects for too
6289 * long.
6290 */
6291 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006292 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6293 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006294 txn->flags &= ~TX_CK_MASK;
6295 txn->flags |= TX_CK_OLD;
6296 delim = val_beg; // let's pretend we have not found the cookie
6297 txn->cookie_first_date = 0;
6298 txn->cookie_last_date = 0;
6299 }
6300 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006301 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6302 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006303 txn->flags &= ~TX_CK_MASK;
6304 txn->flags |= TX_CK_EXPIRED;
6305 delim = val_beg; // let's pretend we have not found the cookie
6306 txn->cookie_first_date = 0;
6307 txn->cookie_last_date = 0;
6308 }
6309
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006310 /* Here, we'll look for the first running server which supports the cookie.
6311 * This allows to share a same cookie between several servers, for example
6312 * to dedicate backup servers to specific servers only.
6313 * However, to prevent clients from sticking to cookie-less backup server
6314 * when they have incidentely learned an empty cookie, we simply ignore
6315 * empty cookies and mark them as invalid.
6316 * The same behaviour is applied when persistence must be ignored.
6317 */
6318 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6319 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006320
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006321 while (srv) {
6322 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6323 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6324 if ((srv->state & SRV_RUNNING) ||
6325 (t->be->options & PR_O_PERSIST) ||
6326 (t->flags & SN_FORCE_PRST)) {
6327 /* we found the server and we can use it */
6328 txn->flags &= ~TX_CK_MASK;
6329 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6330 t->flags |= SN_DIRECT | SN_ASSIGNED;
6331 t->srv = srv;
6332 break;
6333 } else {
6334 /* we found a server, but it's down,
6335 * mark it as such and go on in case
6336 * another one is available.
6337 */
6338 txn->flags &= ~TX_CK_MASK;
6339 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006340 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006341 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006342 srv = srv->next;
6343 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006344
Willy Tarreauf64d1412010-10-07 20:06:11 +02006345 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006346 /* no server matched this cookie */
6347 txn->flags &= ~TX_CK_MASK;
6348 txn->flags |= TX_CK_INVALID;
6349 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006350
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006351 /* depending on the cookie mode, we may have to either :
6352 * - delete the complete cookie if we're in insert+indirect mode, so that
6353 * the server never sees it ;
6354 * - remove the server id from the cookie value, and tag the cookie as an
6355 * application cookie so that it does not get accidentely removed later,
6356 * if we're in cookie prefix mode
6357 */
6358 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6359 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006360
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006361 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6362 val_end += delta;
6363 next += delta;
6364 hdr_end += delta;
6365 hdr_next += delta;
6366 cur_hdr->len += delta;
6367 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006368
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006369 del_from = NULL;
6370 preserve_hdr = 1; /* we want to keep this cookie */
6371 }
6372 else if (del_from == NULL &&
6373 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6374 del_from = prev;
6375 }
6376 } else {
6377 /* This is not our cookie, so we must preserve it. But if we already
6378 * scheduled another cookie for removal, we cannot remove the
6379 * complete header, but we can remove the previous block itself.
6380 */
6381 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006382
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006383 if (del_from != NULL) {
6384 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006385 if (att_beg >= del_from)
6386 att_beg += delta;
6387 if (att_end >= del_from)
6388 att_end += delta;
6389 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006390 val_end += delta;
6391 next += delta;
6392 hdr_end += delta;
6393 hdr_next += delta;
6394 cur_hdr->len += delta;
6395 http_msg_move_end(&txn->req, delta);
6396 prev = del_from;
6397 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006398 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006399 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006400
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006401 /* Look for the appsession cookie unless persistence must be ignored */
6402 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6403 int cmp_len, value_len;
6404 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006405
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006406 if (t->be->options2 & PR_O2_AS_PFX) {
6407 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6408 value_begin = att_beg + t->be->appsession_name_len;
6409 value_len = val_end - att_beg - t->be->appsession_name_len;
6410 } else {
6411 cmp_len = att_end - att_beg;
6412 value_begin = val_beg;
6413 value_len = val_end - val_beg;
6414 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006415
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006416 /* let's see if the cookie is our appcookie */
6417 if (cmp_len == t->be->appsession_name_len &&
6418 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6419 manage_client_side_appsession(t, value_begin, value_len);
6420 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006421 }
6422
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006423 /* continue with next cookie on this header line */
6424 att_beg = next;
6425 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006426
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006427 /* There are no more cookies on this line.
6428 * We may still have one (or several) marked for deletion at the
6429 * end of the line. We must do this now in two ways :
6430 * - if some cookies must be preserved, we only delete from the
6431 * mark to the end of line ;
6432 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006433 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006434 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006435 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006436 if (preserve_hdr) {
6437 delta = del_hdr_value(req, &del_from, hdr_end);
6438 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006439 cur_hdr->len += delta;
6440 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006441 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006442
6443 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006444 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6445 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006446 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006447 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006448 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006449 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006450 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006451 }
6452
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006453 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006454 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006455 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006456}
6457
6458
Willy Tarreaua15645d2007-03-18 16:22:39 +01006459/* Iterate the same filter through all response headers contained in <rtr>.
6460 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6461 */
6462int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6463{
6464 char term;
6465 char *cur_ptr, *cur_end, *cur_next;
6466 int cur_idx, old_idx, last_hdr;
6467 struct http_txn *txn = &t->txn;
6468 struct hdr_idx_elem *cur_hdr;
6469 int len, delta;
6470
6471 last_hdr = 0;
6472
Willy Tarreau962c3f42010-01-10 00:15:35 +01006473 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006474 old_idx = 0;
6475
6476 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006477 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006478 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006479 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006480 (exp->action == ACT_ALLOW ||
6481 exp->action == ACT_DENY))
6482 return 0;
6483
6484 cur_idx = txn->hdr_idx.v[old_idx].next;
6485 if (!cur_idx)
6486 break;
6487
6488 cur_hdr = &txn->hdr_idx.v[cur_idx];
6489 cur_ptr = cur_next;
6490 cur_end = cur_ptr + cur_hdr->len;
6491 cur_next = cur_end + cur_hdr->cr + 1;
6492
6493 /* Now we have one header between cur_ptr and cur_end,
6494 * and the next header starts at cur_next.
6495 */
6496
6497 /* The annoying part is that pattern matching needs
6498 * that we modify the contents to null-terminate all
6499 * strings before testing them.
6500 */
6501
6502 term = *cur_end;
6503 *cur_end = '\0';
6504
6505 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6506 switch (exp->action) {
6507 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006508 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006509 last_hdr = 1;
6510 break;
6511
6512 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006513 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006514 last_hdr = 1;
6515 break;
6516
6517 case ACT_REPLACE:
6518 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6519 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6520 /* FIXME: if the user adds a newline in the replacement, the
6521 * index will not be recalculated for now, and the new line
6522 * will not be counted as a new header.
6523 */
6524
6525 cur_end += delta;
6526 cur_next += delta;
6527 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006528 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006529 break;
6530
6531 case ACT_REMOVE:
6532 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6533 cur_next += delta;
6534
Willy Tarreaufa355d42009-11-29 18:12:29 +01006535 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006536 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6537 txn->hdr_idx.used--;
6538 cur_hdr->len = 0;
6539 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006540 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006541 break;
6542
6543 }
6544 }
6545 if (cur_end)
6546 *cur_end = term; /* restore the string terminator */
6547
6548 /* keep the link from this header to next one in case of later
6549 * removal of next header.
6550 */
6551 old_idx = cur_idx;
6552 }
6553 return 0;
6554}
6555
6556
6557/* Apply the filter to the status line in the response buffer <rtr>.
6558 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6559 * or -1 if a replacement resulted in an invalid status line.
6560 */
6561int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6562{
6563 char term;
6564 char *cur_ptr, *cur_end;
6565 int done;
6566 struct http_txn *txn = &t->txn;
6567 int len, delta;
6568
6569
Willy Tarreau3d300592007-03-18 18:34:41 +01006570 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006571 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006572 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006573 (exp->action == ACT_ALLOW ||
6574 exp->action == ACT_DENY))
6575 return 0;
6576 else if (exp->action == ACT_REMOVE)
6577 return 0;
6578
6579 done = 0;
6580
Willy Tarreau962c3f42010-01-10 00:15:35 +01006581 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006582 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006583
6584 /* Now we have the status line between cur_ptr and cur_end */
6585
6586 /* The annoying part is that pattern matching needs
6587 * that we modify the contents to null-terminate all
6588 * strings before testing them.
6589 */
6590
6591 term = *cur_end;
6592 *cur_end = '\0';
6593
6594 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6595 switch (exp->action) {
6596 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006597 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006598 done = 1;
6599 break;
6600
6601 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006602 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006603 done = 1;
6604 break;
6605
6606 case ACT_REPLACE:
6607 *cur_end = term; /* restore the string terminator */
6608 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6609 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6610 /* FIXME: if the user adds a newline in the replacement, the
6611 * index will not be recalculated for now, and the new line
6612 * will not be counted as a new header.
6613 */
6614
Willy Tarreaufa355d42009-11-29 18:12:29 +01006615 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006616 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006617 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006618 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006619 cur_ptr, cur_end + 1,
6620 NULL, NULL);
6621 if (unlikely(!cur_end))
6622 return -1;
6623
6624 /* we have a full respnse and we know that we have either a CR
6625 * or an LF at <ptr>.
6626 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006627 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006628 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629 /* there is no point trying this regex on headers */
6630 return 1;
6631 }
6632 }
6633 *cur_end = term; /* restore the string terminator */
6634 return done;
6635}
6636
6637
6638
6639/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006640 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6642 * unparsable response.
6643 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006644int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006645{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006646 struct http_txn *txn = &s->txn;
6647 struct hdr_exp *exp;
6648
6649 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006650 int ret;
6651
6652 /*
6653 * The interleaving of transformations and verdicts
6654 * makes it difficult to decide to continue or stop
6655 * the evaluation.
6656 */
6657
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006658 if (txn->flags & TX_SVDENY)
6659 break;
6660
Willy Tarreau3d300592007-03-18 18:34:41 +01006661 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006662 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6663 exp->action == ACT_PASS)) {
6664 exp = exp->next;
6665 continue;
6666 }
6667
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006668 /* if this filter had a condition, evaluate it now and skip to
6669 * next filter if the condition does not match.
6670 */
6671 if (exp->cond) {
6672 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6673 ret = acl_pass(ret);
6674 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6675 ret = !ret;
6676 if (!ret)
6677 continue;
6678 }
6679
Willy Tarreaua15645d2007-03-18 16:22:39 +01006680 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006681 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006682 if (unlikely(ret < 0))
6683 return -1;
6684
6685 if (likely(ret == 0)) {
6686 /* The filter did not match the response, it can be
6687 * iterated through all headers.
6688 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006689 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006690 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006691 }
6692 return 0;
6693}
6694
6695
Willy Tarreaua15645d2007-03-18 16:22:39 +01006696/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006697 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006698 * desirable to call it only when needed. This function is also used when we
6699 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006700 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006701void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006702{
6703 struct http_txn *txn = &t->txn;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006704 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006705 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006706 char *hdr_beg, *hdr_end, *hdr_next;
6707 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708
Willy Tarreaua15645d2007-03-18 16:22:39 +01006709 /* Iterate through the headers.
6710 * we start with the start line.
6711 */
6712 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006713 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006714
6715 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6716 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006717 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006718
6719 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006720 hdr_beg = hdr_next;
6721 hdr_end = hdr_beg + cur_hdr->len;
6722 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006723
Willy Tarreau24581ba2010-08-31 22:39:35 +02006724 /* We have one full header between hdr_beg and hdr_end, and the
6725 * next header starts at hdr_next. We're only interested in
6726 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006727 */
6728
Willy Tarreau24581ba2010-08-31 22:39:35 +02006729 is_cookie2 = 0;
6730 prev = hdr_beg + 10;
6731 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006732 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006733 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6734 if (!val) {
6735 old_idx = cur_idx;
6736 continue;
6737 }
6738 is_cookie2 = 1;
6739 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006740 }
6741
Willy Tarreau24581ba2010-08-31 22:39:35 +02006742 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6743 * <prev> points to the colon.
6744 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006745 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006746
Willy Tarreau24581ba2010-08-31 22:39:35 +02006747 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6748 * check-cache is enabled) and we are not interested in checking
6749 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006750 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006751 if (t->be->cookie_name == NULL &&
6752 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006753 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006754 return;
6755
Willy Tarreau24581ba2010-08-31 22:39:35 +02006756 /* OK so now we know we have to process this response cookie.
6757 * The format of the Set-Cookie header is slightly different
6758 * from the format of the Cookie header in that it does not
6759 * support the comma as a cookie delimiter (thus the header
6760 * cannot be folded) because the Expires attribute described in
6761 * the original Netscape's spec may contain an unquoted date
6762 * with a comma inside. We have to live with this because
6763 * many browsers don't support Max-Age and some browsers don't
6764 * support quoted strings. However the Set-Cookie2 header is
6765 * clean.
6766 *
6767 * We have to keep multiple pointers in order to support cookie
6768 * removal at the beginning, middle or end of header without
6769 * corrupting the header (in case of set-cookie2). A special
6770 * pointer, <scav> points to the beginning of the set-cookie-av
6771 * fields after the first semi-colon. The <next> pointer points
6772 * either to the end of line (set-cookie) or next unquoted comma
6773 * (set-cookie2). All of these headers are valid :
6774 *
6775 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6776 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6777 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6778 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6779 * | | | | | | | | | |
6780 * | | | | | | | | +-> next hdr_end <--+
6781 * | | | | | | | +------------> scav
6782 * | | | | | | +--------------> val_end
6783 * | | | | | +--------------------> val_beg
6784 * | | | | +----------------------> equal
6785 * | | | +------------------------> att_end
6786 * | | +----------------------------> att_beg
6787 * | +------------------------------> prev
6788 * +-----------------------------------------> hdr_beg
6789 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790
Willy Tarreau24581ba2010-08-31 22:39:35 +02006791 for (; prev < hdr_end; prev = next) {
6792 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006793
Willy Tarreau24581ba2010-08-31 22:39:35 +02006794 /* find att_beg */
6795 att_beg = prev + 1;
6796 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6797 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006798
Willy Tarreau24581ba2010-08-31 22:39:35 +02006799 /* find att_end : this is the first character after the last non
6800 * space before the equal. It may be equal to hdr_end.
6801 */
6802 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803
Willy Tarreau24581ba2010-08-31 22:39:35 +02006804 while (equal < hdr_end) {
6805 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6806 break;
6807 if (http_is_spht[(unsigned char)*equal++])
6808 continue;
6809 att_end = equal;
6810 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006811
Willy Tarreau24581ba2010-08-31 22:39:35 +02006812 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6813 * is between <att_beg> and <equal>, both may be identical.
6814 */
6815
6816 /* look for end of cookie if there is an equal sign */
6817 if (equal < hdr_end && *equal == '=') {
6818 /* look for the beginning of the value */
6819 val_beg = equal + 1;
6820 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6821 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006822
Willy Tarreau24581ba2010-08-31 22:39:35 +02006823 /* find the end of the value, respecting quotes */
6824 next = find_cookie_value_end(val_beg, hdr_end);
6825
6826 /* make val_end point to the first white space or delimitor after the value */
6827 val_end = next;
6828 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6829 val_end--;
6830 } else {
6831 /* <equal> points to next comma, semi-colon or EOL */
6832 val_beg = val_end = next = equal;
6833 }
6834
6835 if (next < hdr_end) {
6836 /* Set-Cookie2 supports multiple cookies, and <next> points to
6837 * a colon or semi-colon before the end. So skip all attr-value
6838 * pairs and look for the next comma. For Set-Cookie, since
6839 * commas are permitted in values, skip to the end.
6840 */
6841 if (is_cookie2)
6842 next = find_hdr_value_end(next, hdr_end);
6843 else
6844 next = hdr_end;
6845 }
6846
6847 /* Now everything is as on the diagram above */
6848
6849 /* Ignore cookies with no equal sign */
6850 if (equal == val_end)
6851 continue;
6852
6853 /* If there are spaces around the equal sign, we need to
6854 * strip them otherwise we'll get trouble for cookie captures,
6855 * or even for rewrites. Since this happens extremely rarely,
6856 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006857 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006858 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6859 int stripped_before = 0;
6860 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006861
Willy Tarreau24581ba2010-08-31 22:39:35 +02006862 if (att_end != equal) {
6863 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6864 equal += stripped_before;
6865 val_beg += stripped_before;
6866 }
6867
6868 if (val_beg > equal + 1) {
6869 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6870 val_beg += stripped_after;
6871 stripped_before += stripped_after;
6872 }
6873
6874 val_end += stripped_before;
6875 next += stripped_before;
6876 hdr_end += stripped_before;
6877 hdr_next += stripped_before;
6878 cur_hdr->len += stripped_before;
6879 http_msg_move_end(&txn->req, stripped_before);
6880 }
6881
6882 /* First, let's see if we want to capture this cookie. We check
6883 * that we don't already have a server side cookie, because we
6884 * can only capture one. Also as an optimisation, we ignore
6885 * cookies shorter than the declared name.
6886 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006887 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006888 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006889 (val_end - att_beg >= t->fe->capture_namelen) &&
6890 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6891 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006892 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006893 Alert("HTTP logging : out of memory.\n");
6894 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006895 else {
6896 if (log_len > t->fe->capture_len)
6897 log_len = t->fe->capture_len;
6898 memcpy(txn->srv_cookie, att_beg, log_len);
6899 txn->srv_cookie[log_len] = 0;
6900 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006901 }
6902
6903 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006904 if (!(t->flags & SN_IGNORE_PRST) &&
6905 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6906 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006907 /* assume passive cookie by default */
6908 txn->flags &= ~TX_SCK_MASK;
6909 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006910
6911 /* If the cookie is in insert mode on a known server, we'll delete
6912 * this occurrence because we'll insert another one later.
6913 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006914 * a direct access.
6915 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006916 if (t->be->options2 & PR_O2_COOK_PSV) {
6917 /* The "preserve" flag was set, we don't want to touch the
6918 * server's cookie.
6919 */
6920 }
6921 else if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006922 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006923 /* this cookie must be deleted */
6924 if (*prev == ':' && next == hdr_end) {
6925 /* whole header */
6926 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6927 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6928 txn->hdr_idx.used--;
6929 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006930 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006931 hdr_next += delta;
6932 http_msg_move_end(&txn->rsp, delta);
6933 /* note: while both invalid now, <next> and <hdr_end>
6934 * are still equal, so the for() will stop as expected.
6935 */
6936 } else {
6937 /* just remove the value */
6938 int delta = del_hdr_value(res, &prev, next);
6939 next = prev;
6940 hdr_end += delta;
6941 hdr_next += delta;
6942 cur_hdr->len += delta;
6943 http_msg_move_end(&txn->rsp, delta);
6944 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006945 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006946 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006947 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006948 }
6949 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006950 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006951 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006952 * with this server since we know it.
6953 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006954 delta = buffer_replace2(res, val_beg, val_end, t->srv->cookie, t->srv->cklen);
6955 next += delta;
6956 hdr_end += delta;
6957 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006958 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006959 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006960
Willy Tarreauf1348312010-10-07 15:54:11 +02006961 txn->flags &= ~TX_SCK_MASK;
6962 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006963 }
6964 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006965 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006966 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006967 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006968 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006969 delta = buffer_replace2(res, val_beg, val_beg, t->srv->cookie, t->srv->cklen + 1);
6970 next += delta;
6971 hdr_end += delta;
6972 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006973 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006974 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006975
Willy Tarreau24581ba2010-08-31 22:39:35 +02006976 val_beg[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006977 txn->flags &= ~TX_SCK_MASK;
6978 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006979 }
6980 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006981 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6982 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006983 int cmp_len, value_len;
6984 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006985
Cyril Bontéb21570a2009-11-29 20:04:48 +01006986 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006987 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6988 value_begin = att_beg + t->be->appsession_name_len;
6989 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006990 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006991 cmp_len = att_end - att_beg;
6992 value_begin = val_beg;
6993 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006994 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006995
Cyril Bonté17530c32010-04-06 21:11:10 +02006996 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006997 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6998 /* free a possibly previously allocated memory */
6999 pool_free2(apools.sessid, txn->sessid);
7000
Cyril Bontéb21570a2009-11-29 20:04:48 +01007001 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007002 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007003 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7004 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7005 return;
7006 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007007 memcpy(txn->sessid, value_begin, value_len);
7008 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007009 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007010 }
7011 /* that's done for this cookie, check the next one on the same
7012 * line when next != hdr_end (only if is_cookie2).
7013 */
7014 }
7015 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007016 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007017 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007018
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007019 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007020 appsess *asession = NULL;
7021 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007022 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007023 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007024 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007025 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7026 Alert("Not enough Memory process_srv():asession:calloc().\n");
7027 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7028 return;
7029 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007030 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7031
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007032 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7033 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7034 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007035 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007036 return;
7037 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007038 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007039 asession->sessid[t->be->appsession_len] = 0;
7040
Willy Tarreau1fac7532010-01-09 19:23:06 +01007041 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007042 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007043 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007044 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007045 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007046 return;
7047 }
7048 asession->serverid[0] = '\0';
7049 memcpy(asession->serverid, t->srv->id, server_id_len);
7050
7051 asession->request_count = 0;
7052 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7053 }
7054
7055 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7056 asession->request_count++;
7057 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007058}
7059
7060
Willy Tarreaua15645d2007-03-18 16:22:39 +01007061/*
7062 * Check if response is cacheable or not. Updates t->flags.
7063 */
7064void check_response_for_cacheability(struct session *t, struct buffer *rtr)
7065{
7066 struct http_txn *txn = &t->txn;
7067 char *p1, *p2;
7068
7069 char *cur_ptr, *cur_end, *cur_next;
7070 int cur_idx;
7071
Willy Tarreau5df51872007-11-25 16:20:08 +01007072 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007073 return;
7074
7075 /* Iterate through the headers.
7076 * we start with the start line.
7077 */
7078 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007079 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007080
7081 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7082 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007083 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007084
7085 cur_hdr = &txn->hdr_idx.v[cur_idx];
7086 cur_ptr = cur_next;
7087 cur_end = cur_ptr + cur_hdr->len;
7088 cur_next = cur_end + cur_hdr->cr + 1;
7089
7090 /* We have one full header between cur_ptr and cur_end, and the
7091 * next header starts at cur_next. We're only interested in
7092 * "Cookie:" headers.
7093 */
7094
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007095 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7096 if (val) {
7097 if ((cur_end - (cur_ptr + val) >= 8) &&
7098 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7099 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7100 return;
7101 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007102 }
7103
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007104 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7105 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007106 continue;
7107
7108 /* OK, right now we know we have a cache-control header at cur_ptr */
7109
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007110 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007111
7112 if (p1 >= cur_end) /* no more info */
7113 continue;
7114
7115 /* p1 is at the beginning of the value */
7116 p2 = p1;
7117
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007118 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007119 p2++;
7120
7121 /* we have a complete value between p1 and p2 */
7122 if (p2 < cur_end && *p2 == '=') {
7123 /* we have something of the form no-cache="set-cookie" */
7124 if ((cur_end - p1 >= 21) &&
7125 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7126 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007127 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007128 continue;
7129 }
7130
7131 /* OK, so we know that either p2 points to the end of string or to a comma */
7132 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7133 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7134 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7135 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007136 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007137 return;
7138 }
7139
7140 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007141 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007142 continue;
7143 }
7144 }
7145}
7146
7147
Willy Tarreau58f10d72006-12-04 02:26:12 +01007148/*
7149 * Try to retrieve a known appsession in the URI, then the associated server.
7150 * If the server is found, it's assigned to the session.
7151 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007152void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007153{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007154 char *end_params, *first_param, *cur_param, *next_param;
7155 char separator;
7156 int value_len;
7157
7158 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007159
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007160 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007161 (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 +01007162 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007163 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007164
Cyril Bontéb21570a2009-11-29 20:04:48 +01007165 first_param = NULL;
7166 switch (mode) {
7167 case PR_O2_AS_M_PP:
7168 first_param = memchr(begin, ';', len);
7169 break;
7170 case PR_O2_AS_M_QS:
7171 first_param = memchr(begin, '?', len);
7172 break;
7173 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007174
Cyril Bontéb21570a2009-11-29 20:04:48 +01007175 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007176 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007177 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007178
Cyril Bontéb21570a2009-11-29 20:04:48 +01007179 switch (mode) {
7180 case PR_O2_AS_M_PP:
7181 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7182 end_params = (char *) begin + len;
7183 }
7184 separator = ';';
7185 break;
7186 case PR_O2_AS_M_QS:
7187 end_params = (char *) begin + len;
7188 separator = '&';
7189 break;
7190 default:
7191 /* unknown mode, shouldn't happen */
7192 return;
7193 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007194
Cyril Bontéb21570a2009-11-29 20:04:48 +01007195 cur_param = next_param = end_params;
7196 while (cur_param > first_param) {
7197 cur_param--;
7198 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7199 /* let's see if this is the appsession parameter */
7200 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7201 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7202 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7203 /* Cool... it's the right one */
7204 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7205 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7206 if (value_len > 0) {
7207 manage_client_side_appsession(t, cur_param, value_len);
7208 }
7209 break;
7210 }
7211 next_param = cur_param;
7212 }
7213 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007214#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007215 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007216 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007217#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007218}
7219
Willy Tarreaub2513902006-12-17 14:52:38 +01007220/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007221 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007222 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007223 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007224 * It is assumed that the request is either a HEAD, GET, or POST and that the
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007225 * t->be->uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007226 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007227 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007228 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007229int stats_check_uri(struct session *t, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007230{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007231 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01007232 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007233 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007234
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007235 if (!uri_auth)
7236 return 0;
7237
Cyril Bonté70be45d2010-10-12 00:14:35 +02007238 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007239 return 0;
7240
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007241 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
7242
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007243 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007244 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007245 return 0;
7246
Willy Tarreau962c3f42010-01-10 00:15:35 +01007247 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007248
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007249 /* the URI is in h */
7250 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007251 return 0;
7252
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007253 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007254 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007255 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007256 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007257 break;
7258 }
7259 h++;
7260 }
7261
7262 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007263 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7264 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007265 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007266 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007267 break;
7268 }
7269 h++;
7270 }
7271 }
7272
Willy Tarreau962c3f42010-01-10 00:15:35 +01007273 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7274 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007275 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007276 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007277 break;
7278 }
7279 h++;
7280 }
7281
Cyril Bonté70be45d2010-10-12 00:14:35 +02007282 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7283 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7284 if (memcmp(h, ";st=", 4) == 0) {
7285 h += 4;
7286
7287 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
7288 t->data_ctx.stats.st_code = STAT_STATUS_DONE;
7289 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
7290 t->data_ctx.stats.st_code = STAT_STATUS_NONE;
7291 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
7292 t->data_ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007293 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
7294 t->data_ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007295 else
7296 t->data_ctx.stats.st_code = STAT_STATUS_UNKN;
7297 break;
7298 }
7299 h++;
7300 }
7301
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007302 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
7303
Willy Tarreaub2513902006-12-17 14:52:38 +01007304 return 1;
7305}
7306
Willy Tarreau4076a152009-04-02 15:18:36 +02007307/*
7308 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007309 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7310 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007311 */
7312void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7313 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007314 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007315{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007316 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7317 int len1 = buf->size - msg->som;
7318 es->len = buf->r - (buf->data + msg->som) + buf->size;
7319 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7320 if (es->len > len1 && len1 < sizeof(es->buf))
7321 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7322 }
7323 else {
7324 es->len = buf->r - (buf->data + msg->som);
7325 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7326 }
7327
Willy Tarreau4076a152009-04-02 15:18:36 +02007328 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007329 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007330 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007331 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007332 else
7333 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7334
Willy Tarreau4076a152009-04-02 15:18:36 +02007335 es->when = date; // user-visible date
7336 es->sid = s->uniq_id;
7337 es->srv = s->srv;
7338 es->oe = other_end;
7339 es->src = s->cli_addr;
Willy Tarreau078272e2010-12-12 12:46:33 +01007340 es->state = state;
7341 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007342 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007343}
Willy Tarreaub2513902006-12-17 14:52:38 +01007344
Willy Tarreaubce70882009-09-07 11:51:47 +02007345/* return the IP address pointed to by occurrence <occ> of header <hname> in
7346 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
7347 * occurrence number corresponding to this value is returned. If <occ> is
7348 * strictly negative, the occurrence number before the end corresponding to
7349 * this value is returned. If <occ> is null, any value is returned, so it is
7350 * not recommended to use it that way. Negative occurrences are limited to
7351 * a small value because it is required to keep them in memory while scanning.
7352 * IP address 0.0.0.0 is returned if no match is found.
7353 */
7354unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
7355{
7356 struct hdr_ctx ctx;
7357 unsigned int hdr_hist[MAX_HDR_HISTORY];
7358 unsigned int hist_ptr;
7359 int found = 0;
7360
7361 ctx.idx = 0;
7362 if (occ >= 0) {
7363 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7364 occ--;
7365 if (occ <= 0) {
7366 found = 1;
7367 break;
7368 }
7369 }
7370 if (!found)
7371 return 0;
7372 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7373 }
7374
7375 /* negative occurrence, we scan all the list then walk back */
7376 if (-occ > MAX_HDR_HISTORY)
7377 return 0;
7378
7379 hist_ptr = 0;
7380 hdr_hist[hist_ptr] = 0;
7381 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7382 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7383 if (hist_ptr >= MAX_HDR_HISTORY)
7384 hist_ptr = 0;
7385 found++;
7386 }
7387 if (-occ > found)
7388 return 0;
7389 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7390 * find occurrence -occ, so we have to check [hist_ptr+occ].
7391 */
7392 hist_ptr += occ;
7393 if (hist_ptr >= MAX_HDR_HISTORY)
7394 hist_ptr -= MAX_HDR_HISTORY;
7395 return hdr_hist[hist_ptr];
7396}
7397
Willy Tarreaubaaee002006-06-26 02:48:02 +02007398/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007399 * Print a debug line with a header
7400 */
7401void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7402{
7403 int len, max;
7404 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007405 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007406 max = end - start;
7407 UBOUND(max, sizeof(trash) - len - 1);
7408 len += strlcpy2(trash + len, start, max + 1);
7409 trash[len++] = '\n';
7410 write(1, trash, len);
7411}
7412
Willy Tarreau0937bc42009-12-22 15:03:09 +01007413/*
7414 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7415 * the required fields are properly allocated and that we only need to (re)init
7416 * them. This should be used before processing any new request.
7417 */
7418void http_init_txn(struct session *s)
7419{
7420 struct http_txn *txn = &s->txn;
7421 struct proxy *fe = s->fe;
7422
7423 txn->flags = 0;
7424 txn->status = -1;
7425
Willy Tarreauf64d1412010-10-07 20:06:11 +02007426 txn->cookie_first_date = 0;
7427 txn->cookie_last_date = 0;
7428
Willy Tarreau0937bc42009-12-22 15:03:09 +01007429 txn->req.sol = txn->req.eol = NULL;
7430 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7431 txn->rsp.sol = txn->rsp.eol = NULL;
7432 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
7433 txn->req.hdr_content_len = 0LL;
7434 txn->rsp.hdr_content_len = 0LL;
7435 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7436 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007437
7438 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007439
7440 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7441 if (fe->options2 & PR_O2_REQBUG_OK)
7442 txn->req.err_pos = -1; /* let buggy requests pass */
7443
Willy Tarreau46023632010-01-07 22:51:47 +01007444 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007445 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7446
Willy Tarreau46023632010-01-07 22:51:47 +01007447 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007448 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7449
7450 if (txn->hdr_idx.v)
7451 hdr_idx_init(&txn->hdr_idx);
7452}
7453
7454/* to be used at the end of a transaction */
7455void http_end_txn(struct session *s)
7456{
7457 struct http_txn *txn = &s->txn;
7458
7459 /* these ones will have been dynamically allocated */
7460 pool_free2(pool2_requri, txn->uri);
7461 pool_free2(pool2_capture, txn->cli_cookie);
7462 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007463 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007464
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007465 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007466 txn->uri = NULL;
7467 txn->srv_cookie = NULL;
7468 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007469
7470 if (txn->req.cap) {
7471 struct cap_hdr *h;
7472 for (h = s->fe->req_cap; h; h = h->next)
7473 pool_free2(h->pool, txn->req.cap[h->index]);
7474 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7475 }
7476
7477 if (txn->rsp.cap) {
7478 struct cap_hdr *h;
7479 for (h = s->fe->rsp_cap; h; h = h->next)
7480 pool_free2(h->pool, txn->rsp.cap[h->index]);
7481 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7482 }
7483
Willy Tarreau0937bc42009-12-22 15:03:09 +01007484}
7485
7486/* to be used at the end of a transaction to prepare a new one */
7487void http_reset_txn(struct session *s)
7488{
7489 http_end_txn(s);
7490 http_init_txn(s);
7491
7492 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007493 s->logs.logwait = s->fe->to_log;
7494 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007495 /* re-init store persistence */
7496 s->store_count = 0;
7497
Willy Tarreau0937bc42009-12-22 15:03:09 +01007498 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007499
7500 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7501
Willy Tarreau739cfba2010-01-25 23:11:14 +01007502 /* We must trim any excess data from the response buffer, because we
7503 * may have blocked an invalid response from a server that we don't
7504 * want to accidentely forward once we disable the analysers, nor do
7505 * we want those data to come along with next response. A typical
7506 * example of such data would be from a buggy server responding to
7507 * a HEAD with some data, or sending more than the advertised
7508 * content-length.
7509 */
7510 if (unlikely(s->rep->l > s->rep->send_max)) {
7511 s->rep->l = s->rep->send_max;
7512 s->rep->r = s->rep->w + s->rep->l;
7513 if (s->rep->r >= s->rep->data + s->rep->size)
7514 s->rep->r -= s->rep->size;
7515 }
7516
Willy Tarreau0937bc42009-12-22 15:03:09 +01007517 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007518 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007519
Willy Tarreaud04e8582010-05-31 12:31:35 +02007520 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007521 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007522
7523 s->req->rex = TICK_ETERNITY;
7524 s->req->wex = TICK_ETERNITY;
7525 s->req->analyse_exp = TICK_ETERNITY;
7526 s->rep->rex = TICK_ETERNITY;
7527 s->rep->wex = TICK_ETERNITY;
7528 s->rep->analyse_exp = TICK_ETERNITY;
7529}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007530
Willy Tarreau8797c062007-05-07 00:55:35 +02007531/************************************************************************/
7532/* The code below is dedicated to ACL parsing and matching */
7533/************************************************************************/
7534
7535
7536
7537
7538/* 1. Check on METHOD
7539 * We use the pre-parsed method if it is known, and store its number as an
7540 * integer. If it is unknown, we use the pointer and the length.
7541 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007542static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007543{
7544 int len, meth;
7545
Willy Tarreauae8b7962007-06-09 23:10:04 +02007546 len = strlen(*text);
7547 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007548
7549 pattern->val.i = meth;
7550 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007551 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007552 if (!pattern->ptr.str)
7553 return 0;
7554 pattern->len = len;
7555 }
7556 return 1;
7557}
7558
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007559static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007560acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7561 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007562{
7563 int meth;
7564 struct http_txn *txn = l7;
7565
Willy Tarreaub6866442008-07-14 23:54:42 +02007566 if (!txn)
7567 return 0;
7568
Willy Tarreau655dce92009-11-08 13:10:58 +01007569 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007570 return 0;
7571
Willy Tarreau8797c062007-05-07 00:55:35 +02007572 meth = txn->meth;
7573 test->i = meth;
7574 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007575 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7576 /* ensure the indexes are not affected */
7577 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007578 test->len = txn->req.sl.rq.m_l;
7579 test->ptr = txn->req.sol;
7580 }
7581 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7582 return 1;
7583}
7584
7585static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7586{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007587 int icase;
7588
Willy Tarreau8797c062007-05-07 00:55:35 +02007589 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02007590 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007591
7592 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02007593 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007594
7595 /* Other method, we must compare the strings */
7596 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02007597 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007598
7599 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
7600 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
7601 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007602 return ACL_PAT_FAIL;
7603 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007604}
7605
7606/* 2. Check on Request/Status Version
7607 * We simply compare strings here.
7608 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007609static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007610{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007611 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007612 if (!pattern->ptr.str)
7613 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007614 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007615 return 1;
7616}
7617
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007618static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007619acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7620 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007621{
7622 struct http_txn *txn = l7;
7623 char *ptr;
7624 int len;
7625
Willy Tarreaub6866442008-07-14 23:54:42 +02007626 if (!txn)
7627 return 0;
7628
Willy Tarreau655dce92009-11-08 13:10:58 +01007629 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007630 return 0;
7631
Willy Tarreau8797c062007-05-07 00:55:35 +02007632 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007633 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007634
7635 while ((len-- > 0) && (*ptr++ != '/'));
7636 if (len <= 0)
7637 return 0;
7638
7639 test->ptr = ptr;
7640 test->len = len;
7641
7642 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7643 return 1;
7644}
7645
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007646static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007647acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7648 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007649{
7650 struct http_txn *txn = l7;
7651 char *ptr;
7652 int len;
7653
Willy Tarreaub6866442008-07-14 23:54:42 +02007654 if (!txn)
7655 return 0;
7656
Willy Tarreau655dce92009-11-08 13:10:58 +01007657 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007658 return 0;
7659
Willy Tarreau8797c062007-05-07 00:55:35 +02007660 len = txn->rsp.sl.st.v_l;
7661 ptr = txn->rsp.sol;
7662
7663 while ((len-- > 0) && (*ptr++ != '/'));
7664 if (len <= 0)
7665 return 0;
7666
7667 test->ptr = ptr;
7668 test->len = len;
7669
7670 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7671 return 1;
7672}
7673
7674/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007675static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007676acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7677 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007678{
7679 struct http_txn *txn = l7;
7680 char *ptr;
7681 int len;
7682
Willy Tarreaub6866442008-07-14 23:54:42 +02007683 if (!txn)
7684 return 0;
7685
Willy Tarreau655dce92009-11-08 13:10:58 +01007686 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007687 return 0;
7688
Willy Tarreau8797c062007-05-07 00:55:35 +02007689 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007690 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007691
7692 test->i = __strl2ui(ptr, len);
7693 test->flags = ACL_TEST_F_VOL_1ST;
7694 return 1;
7695}
7696
7697/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007698static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007699acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7700 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007701{
7702 struct http_txn *txn = l7;
7703
Willy Tarreaub6866442008-07-14 23:54:42 +02007704 if (!txn)
7705 return 0;
7706
Willy Tarreau655dce92009-11-08 13:10:58 +01007707 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007708 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007709
Willy Tarreauc11416f2007-06-17 16:58:38 +02007710 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7711 /* ensure the indexes are not affected */
7712 return 0;
7713
Willy Tarreau8797c062007-05-07 00:55:35 +02007714 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007715 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007716
Willy Tarreauf3d25982007-05-08 22:45:09 +02007717 /* we do not need to set READ_ONLY because the data is in a buffer */
7718 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007719 return 1;
7720}
7721
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007722static int
7723acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7724 struct acl_expr *expr, struct acl_test *test)
7725{
7726 struct http_txn *txn = l7;
7727
Willy Tarreaub6866442008-07-14 23:54:42 +02007728 if (!txn)
7729 return 0;
7730
Willy Tarreau655dce92009-11-08 13:10:58 +01007731 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007732 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007733
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007734 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7735 /* ensure the indexes are not affected */
7736 return 0;
7737
7738 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007739 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007740 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
7741 test->i = AF_INET;
7742
7743 /*
7744 * If we are parsing url in frontend space, we prepare backend stage
7745 * to not parse again the same url ! optimization lazyness...
7746 */
7747 if (px->options & PR_O_HTTP_PROXY)
7748 l4->flags |= SN_ADDR_SET;
7749
7750 test->flags = ACL_TEST_F_READ_ONLY;
7751 return 1;
7752}
7753
7754static int
7755acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7756 struct acl_expr *expr, struct acl_test *test)
7757{
7758 struct http_txn *txn = l7;
7759
Willy Tarreaub6866442008-07-14 23:54:42 +02007760 if (!txn)
7761 return 0;
7762
Willy Tarreau655dce92009-11-08 13:10:58 +01007763 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007764 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007765
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007766 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7767 /* ensure the indexes are not affected */
7768 return 0;
7769
7770 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007771 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007772 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
7773
7774 if (px->options & PR_O_HTTP_PROXY)
7775 l4->flags |= SN_ADDR_SET;
7776
7777 test->flags = ACL_TEST_F_READ_ONLY;
7778 return 1;
7779}
7780
Willy Tarreauc11416f2007-06-17 16:58:38 +02007781/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7782 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7783 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007784static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007785acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007786 struct acl_expr *expr, struct acl_test *test)
7787{
7788 struct http_txn *txn = l7;
7789 struct hdr_idx *idx = &txn->hdr_idx;
7790 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007791
Willy Tarreaub6866442008-07-14 23:54:42 +02007792 if (!txn)
7793 return 0;
7794
Willy Tarreau33a7e692007-06-10 19:45:56 +02007795 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7796 /* search for header from the beginning */
7797 ctx->idx = 0;
7798
Willy Tarreau33a7e692007-06-10 19:45:56 +02007799 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7800 test->flags |= ACL_TEST_F_FETCH_MORE;
7801 test->flags |= ACL_TEST_F_VOL_HDR;
7802 test->len = ctx->vlen;
7803 test->ptr = (char *)ctx->line + ctx->val;
7804 return 1;
7805 }
7806
7807 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7808 test->flags |= ACL_TEST_F_VOL_HDR;
7809 return 0;
7810}
7811
Willy Tarreau33a7e692007-06-10 19:45:56 +02007812static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007813acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7814 struct acl_expr *expr, struct acl_test *test)
7815{
7816 struct http_txn *txn = l7;
7817
Willy Tarreaub6866442008-07-14 23:54:42 +02007818 if (!txn)
7819 return 0;
7820
Willy Tarreau655dce92009-11-08 13:10:58 +01007821 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007822 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007823
Willy Tarreauc11416f2007-06-17 16:58:38 +02007824 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7825 /* ensure the indexes are not affected */
7826 return 0;
7827
7828 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7829}
7830
7831static int
7832acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7833 struct acl_expr *expr, struct acl_test *test)
7834{
7835 struct http_txn *txn = l7;
7836
Willy Tarreaub6866442008-07-14 23:54:42 +02007837 if (!txn)
7838 return 0;
7839
Willy Tarreau655dce92009-11-08 13:10:58 +01007840 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007841 return 0;
7842
7843 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7844}
7845
7846/* 6. Check on HTTP header count. The number of occurrences is returned.
7847 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7848 */
7849static int
7850acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007851 struct acl_expr *expr, struct acl_test *test)
7852{
7853 struct http_txn *txn = l7;
7854 struct hdr_idx *idx = &txn->hdr_idx;
7855 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007856 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007857
Willy Tarreaub6866442008-07-14 23:54:42 +02007858 if (!txn)
7859 return 0;
7860
Willy Tarreau33a7e692007-06-10 19:45:56 +02007861 ctx.idx = 0;
7862 cnt = 0;
7863 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7864 cnt++;
7865
7866 test->i = cnt;
7867 test->flags = ACL_TEST_F_VOL_HDR;
7868 return 1;
7869}
7870
Willy Tarreauc11416f2007-06-17 16:58:38 +02007871static int
7872acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7873 struct acl_expr *expr, struct acl_test *test)
7874{
7875 struct http_txn *txn = l7;
7876
Willy Tarreaub6866442008-07-14 23:54:42 +02007877 if (!txn)
7878 return 0;
7879
Willy Tarreau655dce92009-11-08 13:10:58 +01007880 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007881 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007882
Willy Tarreauc11416f2007-06-17 16:58:38 +02007883 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7884 /* ensure the indexes are not affected */
7885 return 0;
7886
7887 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7888}
7889
7890static int
7891acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7892 struct acl_expr *expr, struct acl_test *test)
7893{
7894 struct http_txn *txn = l7;
7895
Willy Tarreaub6866442008-07-14 23:54:42 +02007896 if (!txn)
7897 return 0;
7898
Willy Tarreau655dce92009-11-08 13:10:58 +01007899 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007900 return 0;
7901
7902 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7903}
7904
Willy Tarreau33a7e692007-06-10 19:45:56 +02007905/* 7. Check on HTTP header's integer value. The integer value is returned.
7906 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007907 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007908 */
7909static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007910acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007911 struct acl_expr *expr, struct acl_test *test)
7912{
7913 struct http_txn *txn = l7;
7914 struct hdr_idx *idx = &txn->hdr_idx;
7915 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007916
Willy Tarreaub6866442008-07-14 23:54:42 +02007917 if (!txn)
7918 return 0;
7919
Willy Tarreau33a7e692007-06-10 19:45:56 +02007920 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7921 /* search for header from the beginning */
7922 ctx->idx = 0;
7923
Willy Tarreau33a7e692007-06-10 19:45:56 +02007924 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7925 test->flags |= ACL_TEST_F_FETCH_MORE;
7926 test->flags |= ACL_TEST_F_VOL_HDR;
7927 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
7928 return 1;
7929 }
7930
7931 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7932 test->flags |= ACL_TEST_F_VOL_HDR;
7933 return 0;
7934}
7935
Willy Tarreauc11416f2007-06-17 16:58:38 +02007936static int
7937acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7938 struct acl_expr *expr, struct acl_test *test)
7939{
7940 struct http_txn *txn = l7;
7941
Willy Tarreaub6866442008-07-14 23:54:42 +02007942 if (!txn)
7943 return 0;
7944
Willy Tarreau655dce92009-11-08 13:10:58 +01007945 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007946 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007947
Willy Tarreauc11416f2007-06-17 16:58:38 +02007948 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7949 /* ensure the indexes are not affected */
7950 return 0;
7951
7952 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7953}
7954
7955static int
7956acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7957 struct acl_expr *expr, struct acl_test *test)
7958{
7959 struct http_txn *txn = l7;
7960
Willy Tarreaub6866442008-07-14 23:54:42 +02007961 if (!txn)
7962 return 0;
7963
Willy Tarreau655dce92009-11-08 13:10:58 +01007964 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007965 return 0;
7966
7967 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7968}
7969
Willy Tarreau106f9792009-09-19 07:54:16 +02007970/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7971 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7972 */
7973static int
7974acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7975 struct acl_expr *expr, struct acl_test *test)
7976{
7977 struct http_txn *txn = l7;
7978 struct hdr_idx *idx = &txn->hdr_idx;
7979 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7980
7981 if (!txn)
7982 return 0;
7983
7984 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7985 /* search for header from the beginning */
7986 ctx->idx = 0;
7987
7988 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7989 test->flags |= ACL_TEST_F_FETCH_MORE;
7990 test->flags |= ACL_TEST_F_VOL_HDR;
7991 /* Same optimization as url_ip */
7992 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
7993 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
7994 test->ptr = (void *)&l4->srv_addr.sin_addr;
7995 test->i = AF_INET;
7996 return 1;
7997 }
7998
7999 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8000 test->flags |= ACL_TEST_F_VOL_HDR;
8001 return 0;
8002}
8003
8004static int
8005acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8006 struct acl_expr *expr, struct acl_test *test)
8007{
8008 struct http_txn *txn = l7;
8009
8010 if (!txn)
8011 return 0;
8012
Willy Tarreau655dce92009-11-08 13:10:58 +01008013 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008014 return 0;
8015
8016 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8017 /* ensure the indexes are not affected */
8018 return 0;
8019
8020 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8021}
8022
8023static int
8024acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8025 struct acl_expr *expr, struct acl_test *test)
8026{
8027 struct http_txn *txn = l7;
8028
8029 if (!txn)
8030 return 0;
8031
Willy Tarreau655dce92009-11-08 13:10:58 +01008032 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008033 return 0;
8034
8035 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8036}
8037
Willy Tarreau737b0c12007-06-10 21:28:46 +02008038/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8039 * the first '/' after the possible hostname, and ends before the possible '?'.
8040 */
8041static int
8042acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8043 struct acl_expr *expr, struct acl_test *test)
8044{
8045 struct http_txn *txn = l7;
8046 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008047
Willy Tarreaub6866442008-07-14 23:54:42 +02008048 if (!txn)
8049 return 0;
8050
Willy Tarreau655dce92009-11-08 13:10:58 +01008051 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008052 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008053
Willy Tarreauc11416f2007-06-17 16:58:38 +02008054 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8055 /* ensure the indexes are not affected */
8056 return 0;
8057
Willy Tarreau962c3f42010-01-10 00:15:35 +01008058 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008059 ptr = http_get_path(txn);
8060 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008061 return 0;
8062
8063 /* OK, we got the '/' ! */
8064 test->ptr = ptr;
8065
8066 while (ptr < end && *ptr != '?')
8067 ptr++;
8068
8069 test->len = ptr - test->ptr;
8070
8071 /* we do not need to set READ_ONLY because the data is in a buffer */
8072 test->flags = ACL_TEST_F_VOL_1ST;
8073 return 1;
8074}
8075
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008076static int
8077acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8078 struct acl_expr *expr, struct acl_test *test)
8079{
8080 struct buffer *req = s->req;
8081 struct http_txn *txn = &s->txn;
8082 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008083
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008084 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8085 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8086 */
8087
8088 if (!s || !req)
8089 return 0;
8090
Willy Tarreau655dce92009-11-08 13:10:58 +01008091 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008092 /* Already decoded as OK */
8093 test->flags |= ACL_TEST_F_SET_RES_PASS;
8094 return 1;
8095 }
8096
8097 /* Try to decode HTTP request */
8098 if (likely(req->lr < req->r))
8099 http_msg_analyzer(req, msg, &txn->hdr_idx);
8100
Willy Tarreau655dce92009-11-08 13:10:58 +01008101 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008102 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8103 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8104 return 1;
8105 }
8106 /* wait for final state */
8107 test->flags |= ACL_TEST_F_MAY_CHANGE;
8108 return 0;
8109 }
8110
8111 /* OK we got a valid HTTP request. We have some minor preparation to
8112 * perform so that further checks can rely on HTTP tests.
8113 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008114 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008115 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8116 s->flags |= SN_REDIRECTABLE;
8117
8118 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8119 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8120 return 1;
8121 }
8122
8123 test->flags |= ACL_TEST_F_SET_RES_PASS;
8124 return 1;
8125}
8126
Willy Tarreau7f18e522010-10-22 20:04:13 +02008127/* return a valid test if the current request is the first one on the connection */
8128static int
8129acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8130 struct acl_expr *expr, struct acl_test *test)
8131{
8132 if (!s)
8133 return 0;
8134
8135 if (s->txn.flags & TX_NOT_FIRST)
8136 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8137 else
8138 test->flags |= ACL_TEST_F_SET_RES_PASS;
8139
8140 return 1;
8141}
8142
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008143static int
8144acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8145 struct acl_expr *expr, struct acl_test *test)
8146{
8147
8148 if (!s)
8149 return 0;
8150
8151 if (!get_http_auth(s))
8152 return 0;
8153
8154 test->ctx.a[0] = expr->arg.ul;
8155 test->ctx.a[1] = s->txn.auth.user;
8156 test->ctx.a[2] = s->txn.auth.pass;
8157
8158 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8159
8160 return 1;
8161}
Willy Tarreau8797c062007-05-07 00:55:35 +02008162
8163/************************************************************************/
8164/* All supported keywords must be declared here. */
8165/************************************************************************/
8166
8167/* Note: must not be declared <const> as its list will be overwritten */
8168static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008169 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8170
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008171 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008172 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8173 { "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 +02008174 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008175
Willy Tarreauc4262962010-05-10 23:42:40 +02008176 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008177 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8178 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8179 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8180 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8181 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8182 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008183 { "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 +02008184 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008185
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008186 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008187 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008188 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8189 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8190 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8191 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8192 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8193 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8194 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
8195 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008196 { "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 +02008197
Willy Tarreauc4262962010-05-10 23:42:40 +02008198 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008199 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8200 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8201 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8202 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8203 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8204 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8205 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
8206 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008207 { "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 +02008208
Willy Tarreauc4262962010-05-10 23:42:40 +02008209 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008210 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8211 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8212 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8213 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8214 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8215 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008216
Willy Tarreauf3d25982007-05-08 22:45:09 +02008217#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008218 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8219 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8220 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8221 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8222 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8223 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8224 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8225
Willy Tarreau8797c062007-05-07 00:55:35 +02008226 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8227 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8228 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8229 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8230 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8231 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8232 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8233 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008234#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008235
Willy Tarreau7f18e522010-10-22 20:04:13 +02008236 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8237 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8238 { "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 +02008239 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008240}};
8241
Willy Tarreau4a568972010-05-12 08:08:50 +02008242/************************************************************************/
8243/* The code below is dedicated to pattern fetching and matching */
8244/************************************************************************/
8245
8246/* extract the IP address from the last occurrence of specified header. Note
8247 * that we should normally first extract the string then convert it to IP,
8248 * but right now we have all the functions to do this seemlessly, and we will
8249 * be able to change that later without touching the configuration.
8250 */
8251static int
8252pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02008253 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008254{
8255 struct http_txn *txn = l7;
8256
Emeric Brun485479d2010-09-23 18:02:19 +02008257 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 +02008258 return data->ip.s_addr != 0;
8259}
8260
David Cournapeau16023ee2010-12-23 20:55:41 +09008261/*
8262 * Given a path string and its length, find the position of beginning of the
8263 * query string. Returns NULL if no query string is found in the path.
8264 *
8265 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8266 *
8267 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8268 */
8269static inline char *find_query_string(char *path, size_t path_l)
8270{
8271 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008272
David Cournapeau16023ee2010-12-23 20:55:41 +09008273 p = memchr(path, '?', path_l);
8274 return p ? p + 1 : NULL;
8275}
8276
8277static inline int is_param_delimiter(char c)
8278{
8279 return c == '&' || c == ';';
8280}
8281
8282/*
8283 * Given a url parameter, find the starting position of the first occurence,
8284 * or NULL if the parameter is not found.
8285 *
8286 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8287 * the function will return query_string+8.
8288 */
8289static char*
8290find_url_param_pos(char* query_string, size_t query_string_l,
8291 char* url_param_name, size_t url_param_name_l)
8292{
8293 char *pos, *last;
8294
8295 pos = query_string;
8296 last = query_string + query_string_l - url_param_name_l - 1;
8297
8298 while (pos <= last) {
8299 if (pos[url_param_name_l] == '=') {
8300 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8301 return pos;
8302 pos += url_param_name_l + 1;
8303 }
8304 while (pos <= last && !is_param_delimiter(*pos))
8305 pos++;
8306 pos++;
8307 }
8308 return NULL;
8309}
8310
8311/*
8312 * Given a url parameter name, returns its value and size into *value and
8313 * *value_l respectively, and returns non-zero. If the parameter is not found,
8314 * zero is returned and value/value_l are not touched.
8315 */
8316static int
8317find_url_param_value(char* path, size_t path_l,
8318 char* url_param_name, size_t url_param_name_l,
8319 char** value, size_t* value_l)
8320{
8321 char *query_string, *qs_end;
8322 char *arg_start;
8323 char *value_start, *value_end;
8324
8325 query_string = find_query_string(path, path_l);
8326 if (!query_string)
8327 return 0;
8328
8329 qs_end = path + path_l;
8330 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8331 url_param_name, url_param_name_l);
8332 if (!arg_start)
8333 return 0;
8334
8335 value_start = arg_start + url_param_name_l + 1;
8336 value_end = value_start;
8337
8338 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8339 value_end++;
8340
8341 *value = value_start;
8342 *value_l = value_end - value_start;
8343 return 1;
8344}
8345
8346static int
8347pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8348 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8349{
8350 struct http_txn *txn = l7;
8351 struct http_msg *msg = &txn->req;
8352 char *url_param_value;
8353 size_t url_param_value_l;
8354
8355 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8356 arg_p->data.str.str, arg_p->data.str.len,
8357 &url_param_value, &url_param_value_l))
8358 return 0;
8359
8360 data->str.str = url_param_value;
8361 data->str.len = url_param_value_l;
8362 return 1;
8363}
8364
Emeric Brun485479d2010-09-23 18:02:19 +02008365
Willy Tarreau4a568972010-05-12 08:08:50 +02008366/************************************************************************/
8367/* All supported keywords must be declared here. */
8368/************************************************************************/
8369/* Note: must not be declared <const> as its list will be overwritten */
8370static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Emeric Brun485479d2010-09-23 18:02:19 +02008371 { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008372 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Emeric Brun485479d2010-09-23 18:02:19 +02008373 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008374}};
8375
Willy Tarreau8797c062007-05-07 00:55:35 +02008376
8377__attribute__((constructor))
8378static void __http_protocol_init(void)
8379{
8380 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008381 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008382}
8383
8384
Willy Tarreau58f10d72006-12-04 02:26:12 +01008385/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008386 * Local variables:
8387 * c-indent-level: 8
8388 * c-basic-offset: 8
8389 * End:
8390 */