blob: 5e3975e2faf0d5209dfec85111f0fdf01704f48f [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010027#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/compat.h>
29#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010030#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreau8797c062007-05-07 00:55:35 +020042#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010043#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/backend.h>
45#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010046#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020049#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010051#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020052#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020053#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010055#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010059#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020060#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/task.h>
62
Willy Tarreau522d6c02009-12-06 18:49:18 +010063const char HTTP_100[] =
64 "HTTP/1.1 100 Continue\r\n\r\n";
65
66const struct chunk http_100_chunk = {
67 .str = (char *)&HTTP_100,
68 .len = sizeof(HTTP_100)-1
69};
70
Willy 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;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100604 if (idx->tail == ctx->idx)
605 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100606 ctx->idx = ctx->prev; /* walk back to the end of previous header */
607 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
608 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
609 ctx->vlen = 0;
610 return ctx->idx;
611 }
612
613 /* This was not the only value of this header. We have to remove between
614 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
615 * of the list, we remove the last separator.
616 */
617
618 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
619 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
620 sol + ctx->val + ctx->vlen + skip_comma,
621 NULL, 0);
622 hdr->len += delta;
623 http_msg_move_end(msg, delta);
624 ctx->val = ctx->del;
625 ctx->vlen = 0;
626 return ctx->idx;
627}
628
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100629/* This function handles a server error at the stream interface level. The
630 * stream interface is assumed to be already in a closed state. An optional
631 * message is copied into the input buffer, and an HTTP status code stored.
632 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100633 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100635static void http_server_error(struct session *t, struct stream_interface *si,
636 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100638 buffer_auto_read(si->ob);
639 buffer_abort(si->ob);
640 buffer_auto_close(si->ob);
641 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200642 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100643 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100644 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100645 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100646 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647 }
648 if (!(t->flags & SN_ERR_MASK))
649 t->flags |= err;
650 if (!(t->flags & SN_FINST_MASK))
651 t->flags |= finst;
652}
653
Willy Tarreau80587432006-12-24 17:47:20 +0100654/* This function returns the appropriate error location for the given session
655 * and message.
656 */
657
658struct chunk *error_message(struct session *s, int msgnum)
659{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200660 if (s->be->errmsg[msgnum].str)
661 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100662 else if (s->fe->errmsg[msgnum].str)
663 return &s->fe->errmsg[msgnum];
664 else
665 return &http_err_chunks[msgnum];
666}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667
Willy Tarreau53b6c742006-12-17 13:37:46 +0100668/*
669 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
670 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
671 */
672static http_meth_t find_http_meth(const char *str, const int len)
673{
674 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100675 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100676
677 m = ((unsigned)*str - 'A');
678
679 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100680 for (h = http_methods[m]; h->len > 0; h++) {
681 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100682 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100683 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100684 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100685 };
686 return HTTP_METH_OTHER;
687 }
688 return HTTP_METH_NONE;
689
690}
691
Willy Tarreau21d2af32008-02-14 20:25:24 +0100692/* Parse the URI from the given transaction (which is assumed to be in request
693 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
694 * It is returned otherwise.
695 */
696static char *
697http_get_path(struct http_txn *txn)
698{
699 char *ptr, *end;
700
Willy Tarreau962c3f42010-01-10 00:15:35 +0100701 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100702 end = ptr + txn->req.sl.rq.u_l;
703
704 if (ptr >= end)
705 return NULL;
706
707 /* RFC2616, par. 5.1.2 :
708 * Request-URI = "*" | absuri | abspath | authority
709 */
710
711 if (*ptr == '*')
712 return NULL;
713
714 if (isalpha((unsigned char)*ptr)) {
715 /* this is a scheme as described by RFC3986, par. 3.1 */
716 ptr++;
717 while (ptr < end &&
718 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
719 ptr++;
720 /* skip '://' */
721 if (ptr == end || *ptr++ != ':')
722 return NULL;
723 if (ptr == end || *ptr++ != '/')
724 return NULL;
725 if (ptr == end || *ptr++ != '/')
726 return NULL;
727 }
728 /* skip [user[:passwd]@]host[:[port]] */
729
730 while (ptr < end && *ptr != '/')
731 ptr++;
732
733 if (ptr == end)
734 return NULL;
735
736 /* OK, we got the '/' ! */
737 return ptr;
738}
739
Willy Tarreauefb453c2008-10-26 20:49:47 +0100740/* Returns a 302 for a redirectable request. This may only be called just after
741 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
742 * left unchanged and will follow normal proxy processing.
743 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100745{
746 struct http_txn *txn;
747 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100748 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100749 char *path;
750 int len;
751
752 /* 1: create the response header */
753 rdr.len = strlen(HTTP_302);
754 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100755 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100756 memcpy(rdr.str, HTTP_302, rdr.len);
757
Willy Tarreau827aee92011-03-10 16:55:02 +0100758 srv = target_srv(&s->target);
759
Willy Tarreauefb453c2008-10-26 20:49:47 +0100760 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100761 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100762 return;
763
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100764 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100765 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
766 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
767 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100768 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100769
770 /* 3: add the request URI */
771 txn = &s->txn;
772 path = http_get_path(txn);
773 if (!path)
774 return;
775
Willy Tarreau962c3f42010-01-10 00:15:35 +0100776 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200777 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100778 return;
779
780 memcpy(rdr.str + rdr.len, path, len);
781 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100782
783 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
784 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
785 rdr.len += 29;
786 } else {
787 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
788 rdr.len += 23;
789 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100790
791 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100792 si->shutr(si);
793 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100794 si->err_type = SI_ET_NONE;
795 si->err_loc = NULL;
796 si->state = SI_ST_CLO;
797
798 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100799 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100800
801 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100802 if (srv)
803 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100804}
805
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100806/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100807 * that the server side is closed. Note that err_type is actually a
808 * bitmask, where almost only aborts may be cumulated with other
809 * values. We consider that aborted operations are more important
810 * than timeouts or errors due to the fact that nobody else in the
811 * logs might explain incomplete retries. All others should avoid
812 * being cumulated. It should normally not be possible to have multiple
813 * aborts at once, but just in case, the first one in sequence is reported.
814 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100815void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100816{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100817 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100818
819 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100820 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
821 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100822 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100823 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
824 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100825 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100826 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
827 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100828 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100829 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
830 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100831 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100832 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
833 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100834 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100835 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
836 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100837 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100838 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
839 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100840}
841
Willy Tarreau42250582007-04-01 01:30:43 +0200842extern const char sess_term_cond[8];
843extern const char sess_fin_state[8];
844extern const char *monthname[12];
Willy Tarreauf1348312010-10-07 15:54:11 +0200845const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */
846const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
847 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
848 Set-cookie Updated, unknown, unknown */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200849struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200850struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100851
Emeric Brun3a058f32009-06-30 18:26:00 +0200852void http_sess_clflog(struct session *s)
853{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100854 char pn[INET6_ADDRSTRLEN];
Emeric Brun3a058f32009-06-30 18:26:00 +0200855 struct proxy *fe = s->fe;
856 struct proxy *be = s->be;
857 struct proxy *prx_log;
858 struct http_txn *txn = &s->txn;
859 int tolog, level, err;
860 char *uri, *h;
Willy Tarreau71904a42011-02-13 14:30:26 +0100861 const char *svid;
Emeric Brun3a058f32009-06-30 18:26:00 +0200862 struct tm tm;
863 static char tmpline[MAX_SYSLOG_LEN];
864 int hdr;
865 size_t w;
866 int t_request;
867
868 prx_log = fe;
869 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +0200870 (s->req->cons->conn_retries != be->conn_retries) ||
Emeric Brun3a058f32009-06-30 18:26:00 +0200871 txn->status >= 500;
872
Willy Tarreau957c0a52011-03-03 17:42:23 +0100873 if (s->req->prod->addr.c.from.ss_family == AF_INET)
Emeric Brun3a058f32009-06-30 18:26:00 +0200874 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100875 (const void *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr,
Emeric Brun3a058f32009-06-30 18:26:00 +0200876 pn, sizeof(pn));
Willy Tarreau957c0a52011-03-03 17:42:23 +0100877 else if (s->req->prod->addr.c.from.ss_family == AF_INET6)
Emeric Brun3a058f32009-06-30 18:26:00 +0200878 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100879 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr,
Emeric Brun3a058f32009-06-30 18:26:00 +0200880 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +0200881 else
882 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
Emeric Brun3a058f32009-06-30 18:26:00 +0200883
884 get_gmtime(s->logs.accept_date.tv_sec, &tm);
885
886 /* FIXME: let's limit ourselves to frontend logging for now. */
887 tolog = fe->to_log;
888
889 h = tmpline;
890
891 w = snprintf(h, sizeof(tmpline),
892 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
893 pn,
894 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
895 tm.tm_hour, tm.tm_min, tm.tm_sec);
896 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
897 goto trunc;
898 h += w;
899
900 if (h >= tmpline + sizeof(tmpline) - 4)
901 goto trunc;
902
903 *(h++) = ' ';
904 *(h++) = '\"';
905 uri = txn->uri ? txn->uri : "<BADREQ>";
906 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
907 '#', url_encode_map, uri);
908 *(h++) = '\"';
909
910 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
911 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
912 goto trunc;
913 h += w;
914
915 if (h >= tmpline + sizeof(tmpline) - 9)
916 goto trunc;
917 memcpy(h, " \"-\" \"-\"", 8);
918 h += 8;
919
920 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
921 " %d %03d",
Willy Tarreau957c0a52011-03-03 17:42:23 +0100922 s->req->prod->addr.c.from.ss_family == AF_UNIX ? s->listener->luid :
923 ntohs((s->req->prod->addr.c.from.ss_family == AF_INET) ?
924 ((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_port :
925 ((struct sockaddr_in6 *)&s->req->prod->addr.c.from)->sin6_port),
Emeric Brun3a058f32009-06-30 18:26:00 +0200926 (int)s->logs.accept_date.tv_usec/1000);
927 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
928 goto trunc;
929 h += w;
930
931 w = strlen(fe->id);
932 if (h >= tmpline + sizeof(tmpline) - 4 - w)
933 goto trunc;
934 *(h++) = ' ';
935 *(h++) = '\"';
936 memcpy(h, fe->id, w);
937 h += w;
938 *(h++) = '\"';
939
940 w = strlen(be->id);
941 if (h >= tmpline + sizeof(tmpline) - 4 - w)
942 goto trunc;
943 *(h++) = ' ';
944 *(h++) = '\"';
945 memcpy(h, be->id, w);
946 h += w;
947 *(h++) = '\"';
948
Willy Tarreau71904a42011-02-13 14:30:26 +0100949 if (!(tolog & LW_SVID))
950 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200951 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100952 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200953 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100954 break;
955 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200956 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100957 break;
958 default:
959 svid = "<NOSRV>";
960 break;
961 }
Emeric Brun3a058f32009-06-30 18:26:00 +0200962
963 w = strlen(svid);
964 if (h >= tmpline + sizeof(tmpline) - 4 - w)
965 goto trunc;
966 *(h++) = ' ';
967 *(h++) = '\"';
968 memcpy(h, svid, w);
969 h += w;
970 *(h++) = '\"';
971
972 t_request = -1;
973 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
974 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
975 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
976 " %d %ld %ld %ld %ld",
977 t_request,
978 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
979 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
980 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
981 s->logs.t_close);
982 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
983 goto trunc;
984 h += w;
985
986 if (h >= tmpline + sizeof(tmpline) - 8)
987 goto trunc;
988 *(h++) = ' ';
989 *(h++) = '\"';
990 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
991 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
992 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
993 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
994 *(h++) = '\"';
995
996 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
997 " %d %d %d %d %d %ld %ld",
Willy Tarreau827aee92011-03-10 16:55:02 +0100998 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Willy Tarreauee28de02010-06-01 09:51:00 +0200999 (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries,
Emeric Brun3a058f32009-06-30 18:26:00 +02001000 s->logs.srv_queue_size, s->logs.prx_queue_size);
1001
1002 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
1003 goto trunc;
1004 h += w;
1005
1006 if (txn->cli_cookie) {
1007 w = strlen(txn->cli_cookie);
1008 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1009 goto trunc;
1010 *(h++) = ' ';
1011 *(h++) = '\"';
1012 memcpy(h, txn->cli_cookie, w);
1013 h += w;
1014 *(h++) = '\"';
1015 } else {
1016 if (h >= tmpline + sizeof(tmpline) - 5)
1017 goto trunc;
1018 memcpy(h, " \"-\"", 4);
1019 h += 4;
1020 }
1021
1022 if (txn->srv_cookie) {
1023 w = strlen(txn->srv_cookie);
1024 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1025 goto trunc;
1026 *(h++) = ' ';
1027 *(h++) = '\"';
1028 memcpy(h, txn->srv_cookie, w);
1029 h += w;
1030 *(h++) = '\"';
1031 } else {
1032 if (h >= tmpline + sizeof(tmpline) - 5)
1033 goto trunc;
1034 memcpy(h, " \"-\"", 4);
1035 h += 4;
1036 }
1037
1038 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1039 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1040 if (h >= sizeof (tmpline) + tmpline - 4)
1041 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001042 if (txn->req.cap[hdr] != NULL) {
1043 *(h++) = ' ';
1044 *(h++) = '\"';
1045 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1046 '#', hdr_encode_map, txn->req.cap[hdr]);
1047 *(h++) = '\"';
1048 } else {
1049 memcpy(h, " \"-\"", 4);
1050 h += 4;
1051 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001052 }
1053 }
1054
1055 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1056 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1057 if (h >= sizeof (tmpline) + tmpline - 4)
1058 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001059 if (txn->rsp.cap[hdr] != NULL) {
1060 *(h++) = ' ';
1061 *(h++) = '\"';
1062 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1063 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1064 *(h++) = '\"';
1065 } else {
1066 memcpy(h, " \"-\"", 4);
1067 h += 4;
1068 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001069 }
1070 }
1071
1072trunc:
1073 *h = '\0';
1074
1075 level = LOG_INFO;
1076 if (err && (fe->options2 & PR_O2_LOGERRORS))
1077 level = LOG_ERR;
1078
1079 send_log(prx_log, level, "%s\n", tmpline);
1080
1081 s->logs.logwait = 0;
1082}
1083
Willy Tarreau42250582007-04-01 01:30:43 +02001084/*
1085 * send a log for the session when we have enough info about it.
1086 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001088void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001089{
Cyril Bontéacd7d632010-11-01 19:26:02 +01001090 char pn[INET6_ADDRSTRLEN];
Willy Tarreau42250582007-04-01 01:30:43 +02001091 struct proxy *fe = s->fe;
1092 struct proxy *be = s->be;
1093 struct proxy *prx_log;
1094 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001095 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001096 char *uri, *h;
Willy Tarreau71904a42011-02-13 14:30:26 +01001097 const char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001098 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001099 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001100 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001101 int hdr;
1102
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001103 /* if we don't want to log normal traffic, return now */
1104 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +02001105 (s->req->cons->conn_retries != be->conn_retries) ||
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001106 txn->status >= 500;
1107 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1108 return;
1109
Willy Tarreau42250582007-04-01 01:30:43 +02001110 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1111 return;
1112 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113
Emeric Brun3a058f32009-06-30 18:26:00 +02001114 if (prx_log->options2 & PR_O2_CLFLOG)
1115 return http_sess_clflog(s);
1116
Willy Tarreau957c0a52011-03-03 17:42:23 +01001117 if (s->req->prod->addr.c.from.ss_family == AF_INET)
Willy Tarreau42250582007-04-01 01:30:43 +02001118 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01001119 (const void *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr,
Willy Tarreau42250582007-04-01 01:30:43 +02001120 pn, sizeof(pn));
Willy Tarreau957c0a52011-03-03 17:42:23 +01001121 else if (s->req->prod->addr.c.from.ss_family == AF_INET6)
Willy Tarreau42250582007-04-01 01:30:43 +02001122 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01001123 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr,
Willy Tarreau42250582007-04-01 01:30:43 +02001124 pn, sizeof(pn));
1125
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001126 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001127
1128 /* FIXME: let's limit ourselves to frontend logging for now. */
1129 tolog = fe->to_log;
1130
1131 h = tmpline;
1132 if (fe->to_log & LW_REQHDR &&
1133 txn->req.cap &&
1134 (h < tmpline + sizeof(tmpline) - 10)) {
1135 *(h++) = ' ';
1136 *(h++) = '{';
1137 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1138 if (hdr)
1139 *(h++) = '|';
1140 if (txn->req.cap[hdr] != NULL)
1141 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1142 '#', hdr_encode_map, txn->req.cap[hdr]);
1143 }
1144 *(h++) = '}';
1145 }
1146
1147 if (fe->to_log & LW_RSPHDR &&
1148 txn->rsp.cap &&
1149 (h < tmpline + sizeof(tmpline) - 7)) {
1150 *(h++) = ' ';
1151 *(h++) = '{';
1152 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1153 if (hdr)
1154 *(h++) = '|';
1155 if (txn->rsp.cap[hdr] != NULL)
1156 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1157 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1158 }
1159 *(h++) = '}';
1160 }
1161
1162 if (h < tmpline + sizeof(tmpline) - 4) {
1163 *(h++) = ' ';
1164 *(h++) = '"';
1165 uri = txn->uri ? txn->uri : "<BADREQ>";
1166 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1167 '#', url_encode_map, uri);
1168 *(h++) = '"';
1169 }
1170 *h = '\0';
1171
Willy Tarreau71904a42011-02-13 14:30:26 +01001172 if (!(tolog & LW_SVID))
1173 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001174 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +01001175 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001176 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +01001177 break;
1178 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001179 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +01001180 break;
1181 default:
1182 svid = "<NOSRV>";
1183 break;
1184 }
Willy Tarreau42250582007-04-01 01:30:43 +02001185
Willy Tarreau70089872008-06-13 21:12:51 +02001186 t_request = -1;
1187 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1188 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1189
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001190 level = LOG_INFO;
1191 if (err && (fe->options2 & PR_O2_LOGERRORS))
1192 level = LOG_ERR;
1193
1194 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001195 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001196 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1197 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau957c0a52011-03-03 17:42:23 +01001198 (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? "unix" : pn,
1199 (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? s->listener->luid :
1200 ntohs((s->req->prod->addr.c.from.ss_family == AF_INET) ?
1201 ((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_port :
1202 ((struct sockaddr_in6 *)&s->req->prod->addr.c.from)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001203 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001204 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001205 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001206 t_request,
1207 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001208 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1209 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1210 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1211 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001212 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001213 txn->cli_cookie ? txn->cli_cookie : "-",
1214 txn->srv_cookie ? txn->srv_cookie : "-",
1215 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1216 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1217 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1218 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
Willy Tarreau827aee92011-03-10 16:55:02 +01001219 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001220 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +02001221 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001222 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1223
1224 s->logs.logwait = 0;
1225}
1226
Willy Tarreau117f59e2007-03-04 18:17:17 +01001227
1228/*
1229 * Capture headers from message starting at <som> according to header list
1230 * <cap_hdr>, and fill the <idx> structure appropriately.
1231 */
1232void capture_headers(char *som, struct hdr_idx *idx,
1233 char **cap, struct cap_hdr *cap_hdr)
1234{
1235 char *eol, *sol, *col, *sov;
1236 int cur_idx;
1237 struct cap_hdr *h;
1238 int len;
1239
1240 sol = som + hdr_idx_first_pos(idx);
1241 cur_idx = hdr_idx_first_idx(idx);
1242
1243 while (cur_idx) {
1244 eol = sol + idx->v[cur_idx].len;
1245
1246 col = sol;
1247 while (col < eol && *col != ':')
1248 col++;
1249
1250 sov = col + 1;
1251 while (sov < eol && http_is_lws[(unsigned char)*sov])
1252 sov++;
1253
1254 for (h = cap_hdr; h; h = h->next) {
1255 if ((h->namelen == col - sol) &&
1256 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1257 if (cap[h->index] == NULL)
1258 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001259 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001260
1261 if (cap[h->index] == NULL) {
1262 Alert("HTTP capture : out of memory.\n");
1263 continue;
1264 }
1265
1266 len = eol - sov;
1267 if (len > h->len)
1268 len = h->len;
1269
1270 memcpy(cap[h->index], sov, len);
1271 cap[h->index][len]=0;
1272 }
1273 }
1274 sol = eol + idx->v[cur_idx].cr + 1;
1275 cur_idx = idx->v[cur_idx].next;
1276 }
1277}
1278
1279
Willy Tarreau42250582007-04-01 01:30:43 +02001280/* either we find an LF at <ptr> or we jump to <bad>.
1281 */
1282#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1283
1284/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1285 * otherwise to <http_msg_ood> with <state> set to <st>.
1286 */
1287#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1288 ptr++; \
1289 if (likely(ptr < end)) \
1290 goto good; \
1291 else { \
1292 state = (st); \
1293 goto http_msg_ood; \
1294 } \
1295 } while (0)
1296
1297
Willy Tarreaubaaee002006-06-26 02:48:02 +02001298/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001299 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1301 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1302 * will give undefined results.
1303 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1304 * and that msg->sol points to the beginning of the response.
1305 * If a complete line is found (which implies that at least one CR or LF is
1306 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1307 * returned indicating an incomplete line (which does not mean that parts have
1308 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1309 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1310 * upon next call.
1311 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001312 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001313 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1314 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001315 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001317const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1318 unsigned int state, const char *ptr, const char *end,
1319 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001320{
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001322 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001323 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001324 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001325 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1326
1327 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001328 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001329 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1330 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001331 state = HTTP_MSG_ERROR;
1332 break;
1333
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001335 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001337 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 goto http_msg_rpcode;
1339 }
1340 if (likely(HTTP_IS_SPHT(*ptr)))
1341 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1342 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001343 state = HTTP_MSG_ERROR;
1344 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001345
Willy Tarreau8973c702007-01-21 23:58:29 +01001346 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001347 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 if (likely(!HTTP_IS_LWS(*ptr)))
1349 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1350
1351 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001352 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1354 }
1355
1356 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001357 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001358 http_msg_rsp_reason:
1359 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001360 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001361 msg->sl.st.r_l = 0;
1362 goto http_msg_rpline_eol;
1363
Willy Tarreau8973c702007-01-21 23:58:29 +01001364 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001365 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001366 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001367 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001368 goto http_msg_rpreason;
1369 }
1370 if (likely(HTTP_IS_SPHT(*ptr)))
1371 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1372 /* so it's a CR/LF, so there is no reason phrase */
1373 goto http_msg_rsp_reason;
1374
Willy Tarreau8973c702007-01-21 23:58:29 +01001375 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001376 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001377 if (likely(!HTTP_IS_CRLF(*ptr)))
1378 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001379 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001380 http_msg_rpline_eol:
1381 /* We have seen the end of line. Note that we do not
1382 * necessarily have the \n yet, but at least we know that we
1383 * have EITHER \r OR \n, otherwise the response would not be
1384 * complete. We can then record the response length and return
1385 * to the caller which will be able to register it.
1386 */
1387 msg->sl.st.l = ptr - msg->sol;
1388 return ptr;
1389
1390#ifdef DEBUG_FULL
1391 default:
1392 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1393 exit(1);
1394#endif
1395 }
1396
1397 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001398 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001399 if (ret_state)
1400 *ret_state = state;
1401 if (ret_ptr)
1402 *ret_ptr = (char *)ptr;
1403 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001404}
1405
Willy Tarreau8973c702007-01-21 23:58:29 +01001406/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 * This function parses a request line between <ptr> and <end>, starting with
1408 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1409 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1410 * will give undefined results.
1411 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1412 * and that msg->sol points to the beginning of the request.
1413 * If a complete line is found (which implies that at least one CR or LF is
1414 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1415 * returned indicating an incomplete line (which does not mean that parts have
1416 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1417 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1418 * upon next call.
1419 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001420 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1422 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001423 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001424 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001425const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1426 unsigned int state, const char *ptr, const char *end,
1427 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001428{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001431 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 if (likely(HTTP_IS_TOKEN(*ptr)))
1433 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001434
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001436 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1438 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 if (likely(HTTP_IS_CRLF(*ptr))) {
1441 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001442 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001444 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001446 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001448 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 msg->sl.rq.v_l = 0;
1450 goto http_msg_rqline_eol;
1451 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001452 state = HTTP_MSG_ERROR;
1453 break;
1454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001456 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001458 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 goto http_msg_rquri;
1460 }
1461 if (likely(HTTP_IS_SPHT(*ptr)))
1462 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1463 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1464 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001467 http_msg_rquri:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 if (likely(!HTTP_IS_LWS(*ptr)))
1469 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001472 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1474 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1477 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001478
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001479 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001480 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001482 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 goto http_msg_rqver;
1484 }
1485 if (likely(HTTP_IS_SPHT(*ptr)))
1486 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1487 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1488 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001489
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001491 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001492 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001494
1495 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001496 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001497 http_msg_rqline_eol:
1498 /* We have seen the end of line. Note that we do not
1499 * necessarily have the \n yet, but at least we know that we
1500 * have EITHER \r OR \n, otherwise the request would not be
1501 * complete. We can then record the request length and return
1502 * to the caller which will be able to register it.
1503 */
1504 msg->sl.rq.l = ptr - msg->sol;
1505 return ptr;
1506 }
1507
1508 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001509 state = HTTP_MSG_ERROR;
1510 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001511
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512#ifdef DEBUG_FULL
1513 default:
1514 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1515 exit(1);
1516#endif
1517 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001518
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001520 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 if (ret_state)
1522 *ret_state = state;
1523 if (ret_ptr)
1524 *ret_ptr = (char *)ptr;
1525 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001527
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001528/*
1529 * Returns the data from Authorization header. Function may be called more
1530 * than once so data is stored in txn->auth_data. When no header is found
1531 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1532 * searching again for something we are unable to find anyway.
1533 */
1534
1535char get_http_auth_buff[BUFSIZE];
1536
1537int
1538get_http_auth(struct session *s)
1539{
1540
1541 struct http_txn *txn = &s->txn;
1542 struct chunk auth_method;
1543 struct hdr_ctx ctx;
1544 char *h, *p;
1545 int len;
1546
1547#ifdef DEBUG_AUTH
1548 printf("Auth for session %p: %d\n", s, txn->auth.method);
1549#endif
1550
1551 if (txn->auth.method == HTTP_AUTH_WRONG)
1552 return 0;
1553
1554 if (txn->auth.method)
1555 return 1;
1556
1557 txn->auth.method = HTTP_AUTH_WRONG;
1558
1559 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001560
1561 if (txn->flags & TX_USE_PX_CONN) {
1562 h = "Proxy-Authorization";
1563 len = strlen(h);
1564 } else {
1565 h = "Authorization";
1566 len = strlen(h);
1567 }
1568
1569 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001570 return 0;
1571
1572 h = ctx.line + ctx.val;
1573
1574 p = memchr(h, ' ', ctx.vlen);
1575 if (!p || p == h)
1576 return 0;
1577
1578 chunk_initlen(&auth_method, h, 0, p-h);
1579 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1580
1581 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1582
1583 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1584 get_http_auth_buff, BUFSIZE - 1);
1585
1586 if (len < 0)
1587 return 0;
1588
1589
1590 get_http_auth_buff[len] = '\0';
1591
1592 p = strchr(get_http_auth_buff, ':');
1593
1594 if (!p)
1595 return 0;
1596
1597 txn->auth.user = get_http_auth_buff;
1598 *p = '\0';
1599 txn->auth.pass = p+1;
1600
1601 txn->auth.method = HTTP_AUTH_BASIC;
1602 return 1;
1603 }
1604
1605 return 0;
1606}
1607
Willy Tarreau58f10d72006-12-04 02:26:12 +01001608
Willy Tarreau8973c702007-01-21 23:58:29 +01001609/*
1610 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001611 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001612 * when data are missing and recalled at the exact same location with no
1613 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001614 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001615 * fields. Note that msg->som and msg->sol will be initialized after completing
1616 * the first state, so that none of the msg pointers has to be initialized
1617 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001618 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1620{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001621 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001622 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001623
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001624 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 ptr = buf->lr;
1626 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 if (unlikely(ptr >= end))
1629 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001632 /*
1633 * First, states that are specific to the response only.
1634 * We check them first so that request and headers are
1635 * closer to each other (accessed more often).
1636 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001637 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001638 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001639 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001640 /* we have a start of message, but we have to check
1641 * first if we need to remove some CRLF. We can only
1642 * do this when send_max=0.
1643 */
1644 char *beg = buf->w + buf->send_max;
1645 if (beg >= buf->data + buf->size)
1646 beg -= buf->size;
1647 if (unlikely(ptr != beg)) {
1648 if (buf->send_max)
1649 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001650 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001651 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001652 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001653 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001654 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001655 hdr_idx_init(idx);
1656 state = HTTP_MSG_RPVER;
1657 goto http_msg_rpver;
1658 }
1659
1660 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1661 goto http_msg_invalid;
1662
1663 if (unlikely(*ptr == '\n'))
1664 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1665 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1666 /* stop here */
1667
Willy Tarreau8973c702007-01-21 23:58:29 +01001668 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001669 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001670 EXPECT_LF_HERE(ptr, http_msg_invalid);
1671 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1672 /* stop here */
1673
Willy Tarreau8973c702007-01-21 23:58:29 +01001674 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001675 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001676 case HTTP_MSG_RPVER_SP:
1677 case HTTP_MSG_RPCODE:
1678 case HTTP_MSG_RPCODE_SP:
1679 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001680 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001681 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001682 if (unlikely(!ptr))
1683 return;
1684
1685 /* we have a full response and we know that we have either a CR
1686 * or an LF at <ptr>.
1687 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001688 //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 +01001689 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1690
1691 msg->sol = ptr;
1692 if (likely(*ptr == '\r'))
1693 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1694 goto http_msg_rpline_end;
1695
Willy Tarreau8973c702007-01-21 23:58:29 +01001696 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001697 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001698 /* msg->sol must point to the first of CR or LF. */
1699 EXPECT_LF_HERE(ptr, http_msg_invalid);
1700 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1701 /* stop here */
1702
1703 /*
1704 * Second, states that are specific to the request only
1705 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001707 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001709 /* we have a start of message, but we have to check
1710 * first if we need to remove some CRLF. We can only
1711 * do this when send_max=0.
1712 */
1713 char *beg = buf->w + buf->send_max;
1714 if (beg >= buf->data + buf->size)
1715 beg -= buf->size;
1716 if (likely(ptr != beg)) {
1717 if (buf->send_max)
1718 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001719 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001720 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001722 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001723 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001724 /* we will need this when keep-alive will be supported
1725 hdr_idx_init(idx);
1726 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001727 state = HTTP_MSG_RQMETH;
1728 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1732 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 if (unlikely(*ptr == '\n'))
1735 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1736 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001737 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001738
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001740 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741 EXPECT_LF_HERE(ptr, http_msg_invalid);
1742 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001743 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001744
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001746 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 case HTTP_MSG_RQMETH_SP:
1748 case HTTP_MSG_RQURI:
1749 case HTTP_MSG_RQURI_SP:
1750 case HTTP_MSG_RQVER:
1751 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001752 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 if (unlikely(!ptr))
1754 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001755
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 /* we have a full request and we know that we have either a CR
1757 * or an LF at <ptr>.
1758 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001759 //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 +01001760 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001761
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 msg->sol = ptr;
1763 if (likely(*ptr == '\r'))
1764 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001765 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001766
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001767 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001768 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001769 /* check for HTTP/0.9 request : no version information available.
1770 * msg->sol must point to the first of CR or LF.
1771 */
1772 if (unlikely(msg->sl.rq.v_l == 0))
1773 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001774
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775 EXPECT_LF_HERE(ptr, http_msg_invalid);
1776 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001777 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001778
Willy Tarreau8973c702007-01-21 23:58:29 +01001779 /*
1780 * Common states below
1781 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001782 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001783 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 msg->sol = ptr;
1785 if (likely(!HTTP_IS_CRLF(*ptr))) {
1786 goto http_msg_hdr_name;
1787 }
1788
1789 if (likely(*ptr == '\r'))
1790 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1791 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001792
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001794 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001795 /* assumes msg->sol points to the first char */
1796 if (likely(HTTP_IS_TOKEN(*ptr)))
1797 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001798
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001799 if (likely(*ptr == ':')) {
1800 msg->col = ptr - buf->data;
1801 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1802 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001803
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001804 if (likely(msg->err_pos < -1) || *ptr == '\n')
1805 goto http_msg_invalid;
1806
1807 if (msg->err_pos == -1) /* capture error pointer */
1808 msg->err_pos = ptr - buf->data; /* >= 0 now */
1809
1810 /* and we still accept this non-token character */
1811 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001812
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001813 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001814 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 /* assumes msg->sol points to the first char and msg->col to the colon */
1816 if (likely(HTTP_IS_SPHT(*ptr)))
1817 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001818
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001819 /* header value can be basically anything except CR/LF */
1820 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001821
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 if (likely(!HTTP_IS_CRLF(*ptr))) {
1823 goto http_msg_hdr_val;
1824 }
1825
1826 if (likely(*ptr == '\r'))
1827 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1828 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001829
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001831 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001832 EXPECT_LF_HERE(ptr, http_msg_invalid);
1833 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001834
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001836 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001837 if (likely(HTTP_IS_SPHT(*ptr))) {
1838 /* replace HT,CR,LF with spaces */
1839 for (; buf->data+msg->sov < ptr; msg->sov++)
1840 buf->data[msg->sov] = ' ';
1841 goto http_msg_hdr_l1_sp;
1842 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001843 /* we had a header consisting only in spaces ! */
1844 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001845 goto http_msg_complete_header;
1846
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001847 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001848 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 /* assumes msg->sol points to the first char, msg->col to the
1850 * colon, and msg->sov points to the first character of the
1851 * value.
1852 */
1853 if (likely(!HTTP_IS_CRLF(*ptr)))
1854 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001855
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001856 msg->eol = ptr;
1857 /* Note: we could also copy eol into ->eoh so that we have the
1858 * real header end in case it ends with lots of LWS, but is this
1859 * really needed ?
1860 */
1861 if (likely(*ptr == '\r'))
1862 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1863 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001864
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001865 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001866 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 EXPECT_LF_HERE(ptr, http_msg_invalid);
1868 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001869
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001870 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001871 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001872 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1873 /* LWS: replace HT,CR,LF with spaces */
1874 for (; msg->eol < ptr; msg->eol++)
1875 *msg->eol = ' ';
1876 goto http_msg_hdr_val;
1877 }
1878 http_msg_complete_header:
1879 /*
1880 * It was a new header, so the last one is finished.
1881 * Assumes msg->sol points to the first char, msg->col to the
1882 * colon, msg->sov points to the first character of the value
1883 * and msg->eol to the first CR or LF so we know how the line
1884 * ends. We insert last header into the index.
1885 */
1886 /*
1887 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1888 write(2, msg->sol, msg->eol-msg->sol);
1889 fprintf(stderr,"\n");
1890 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001891
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1893 idx, idx->tail) < 0))
1894 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001895
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 msg->sol = ptr;
1897 if (likely(!HTTP_IS_CRLF(*ptr))) {
1898 goto http_msg_hdr_name;
1899 }
1900
1901 if (likely(*ptr == '\r'))
1902 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1903 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001904
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001905 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001906 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001907 /* Assumes msg->sol points to the first of either CR or LF */
1908 EXPECT_LF_HERE(ptr, http_msg_invalid);
1909 ptr++;
1910 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001911 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001912 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001913 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001914 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001915 return;
1916#ifdef DEBUG_FULL
1917 default:
1918 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1919 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001920#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001921 }
1922 http_msg_ood:
1923 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001924 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001925 buf->lr = ptr;
1926 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001927
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001928 http_msg_invalid:
1929 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001930 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001931 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001932 return;
1933}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001934
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001935/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1936 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1937 * nothing is done and 1 is returned.
1938 */
1939static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1940{
1941 int delta;
1942 char *cur_end;
1943
1944 if (msg->sl.rq.v_l != 0)
1945 return 1;
1946
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001947 cur_end = msg->sol + msg->sl.rq.l;
1948 delta = 0;
1949
1950 if (msg->sl.rq.u_l == 0) {
1951 /* if no URI was set, add "/" */
1952 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1953 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001954 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001955 }
1956 /* add HTTP version */
1957 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001958 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001959 cur_end += delta;
1960 cur_end = (char *)http_parse_reqline(msg, req->data,
1961 HTTP_MSG_RQMETH,
1962 msg->sol, cur_end + 1,
1963 NULL, NULL);
1964 if (unlikely(!cur_end))
1965 return 0;
1966
1967 /* we have a full HTTP/1.0 request now and we know that
1968 * we have either a CR or an LF at <ptr>.
1969 */
1970 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1971 return 1;
1972}
1973
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001974/* Parse the Connection: header of an HTTP request, looking for both "close"
1975 * and "keep-alive" values. If a buffer is provided and we already know that
1976 * some headers may safely be removed, we remove them now. The <to_del> flags
1977 * are used for that :
1978 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1979 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1980 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1981 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1982 * harmless combinations may be removed. Do not call that after changes have
1983 * been processed. If unused, the buffer can be NULL, and no data will be
1984 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001985 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001986void 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 +01001987{
Willy Tarreau5b154472009-12-21 20:11:07 +01001988 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001989 const char *hdr_val = "Connection";
1990 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001991
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001992 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001993 return;
1994
Willy Tarreau88d349d2010-01-25 12:15:43 +01001995 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1996 hdr_val = "Proxy-Connection";
1997 hdr_len = 16;
1998 }
1999
Willy Tarreau5b154472009-12-21 20:11:07 +01002000 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002001 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002002 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002003 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2004 txn->flags |= TX_HDR_CONN_KAL;
2005 if ((to_del & 2) && buf)
2006 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
2007 else
2008 txn->flags |= TX_CON_KAL_SET;
2009 }
2010 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2011 txn->flags |= TX_HDR_CONN_CLO;
2012 if ((to_del & 1) && buf)
2013 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
2014 else
2015 txn->flags |= TX_CON_CLO_SET;
2016 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002017 }
2018
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002019 txn->flags |= TX_HDR_CONN_PRS;
2020 return;
2021}
Willy Tarreau5b154472009-12-21 20:11:07 +01002022
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002023/* Apply desired changes on the Connection: header. Values may be removed and/or
2024 * added depending on the <wanted> flags, which are exclusively composed of
2025 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2026 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2027 */
2028void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
2029{
2030 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002031 const char *hdr_val = "Connection";
2032 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002033
2034 ctx.idx = 0;
2035
Willy Tarreau88d349d2010-01-25 12:15:43 +01002036
2037 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2038 hdr_val = "Proxy-Connection";
2039 hdr_len = 16;
2040 }
2041
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002042 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002043 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002044 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2045 if (wanted & TX_CON_KAL_SET)
2046 txn->flags |= TX_CON_KAL_SET;
2047 else
2048 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002049 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002050 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2051 if (wanted & TX_CON_CLO_SET)
2052 txn->flags |= TX_CON_CLO_SET;
2053 else
2054 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002055 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002056 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002057
2058 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2059 return;
2060
2061 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2062 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002063 hdr_val = "Connection: close";
2064 hdr_len = 17;
2065 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2066 hdr_val = "Proxy-Connection: close";
2067 hdr_len = 23;
2068 }
2069 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002070 }
2071
2072 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2073 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002074 hdr_val = "Connection: keep-alive";
2075 hdr_len = 22;
2076 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2077 hdr_val = "Proxy-Connection: keep-alive";
2078 hdr_len = 28;
2079 }
2080 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002081 }
2082 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002083}
2084
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2086 * first byte of body, and increments msg->sov by the number of bytes parsed,
2087 * so that we know we can forward between ->som and ->sov. Note that due to
2088 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2089 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002090 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002091 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002092 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002093int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002094{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 char *ptr = buf->lr;
2096 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002097 unsigned int chunk = 0;
2098
2099 /* The chunk size is in the following form, though we are only
2100 * interested in the size and CRLF :
2101 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2102 */
2103 while (1) {
2104 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002105 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002106 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002107 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002108 if (c < 0) /* not a hex digit anymore */
2109 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002110 if (++ptr >= end)
2111 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002112 if (chunk & 0xF000000) /* overflow will occur */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002113 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002114 chunk = (chunk << 4) + c;
2115 }
2116
Willy Tarreaud98cf932009-12-27 22:54:55 +01002117 /* empty size not allowed */
2118 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002119 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002120
2121 while (http_is_spht[(unsigned char)*ptr]) {
2122 if (++ptr >= end)
2123 ptr = buf->data;
2124 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002125 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002126 }
2127
Willy Tarreaud98cf932009-12-27 22:54:55 +01002128 /* Up to there, we know that at least one byte is present at *ptr. Check
2129 * for the end of chunk size.
2130 */
2131 while (1) {
2132 if (likely(HTTP_IS_CRLF(*ptr))) {
2133 /* we now have a CR or an LF at ptr */
2134 if (likely(*ptr == '\r')) {
2135 if (++ptr >= end)
2136 ptr = buf->data;
2137 if (ptr == buf->r)
2138 return 0;
2139 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002140
Willy Tarreaud98cf932009-12-27 22:54:55 +01002141 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002142 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002143 if (++ptr >= end)
2144 ptr = buf->data;
2145 /* done */
2146 break;
2147 }
2148 else if (*ptr == ';') {
2149 /* chunk extension, ends at next CRLF */
2150 if (++ptr >= end)
2151 ptr = buf->data;
2152 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002153 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002154
2155 while (!HTTP_IS_CRLF(*ptr)) {
2156 if (++ptr >= end)
2157 ptr = buf->data;
2158 if (ptr == buf->r)
2159 return 0;
2160 }
2161 /* we have a CRLF now, loop above */
2162 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002163 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002164 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002165 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002166 }
2167
Willy Tarreaud98cf932009-12-27 22:54:55 +01002168 /* OK we found our CRLF and now <ptr> points to the next byte,
2169 * which may or may not be present. We save that into ->lr and
2170 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002171 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002172 msg->sov += ptr - buf->lr;
2173 buf->lr = ptr;
Willy Tarreau124d9912011-03-01 20:30:48 +01002174 msg->chunk_len = chunk;
2175 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002176 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002177 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002178 error:
2179 msg->err_pos = ptr - buf->data;
2180 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002181}
2182
Willy Tarreaud98cf932009-12-27 22:54:55 +01002183/* This function skips trailers in the buffer <buf> associated with HTTP
2184 * message <msg>. The first visited position is buf->lr. If the end of
2185 * the trailers is found, it is automatically scheduled to be forwarded,
2186 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2187 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002188 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2189 * zero. If a parse error is encountered, the function returns < 0 and does not
2190 * change anything except maybe buf->lr and msg->sov. Note that the message
2191 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2192 * which implies that all non-trailers data have already been scheduled for
2193 * forwarding, and that the difference between msg->som and msg->sov exactly
2194 * matches the length of trailers already parsed and not forwarded. It is also
2195 * important to note that this function is designed to be able to parse wrapped
2196 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 */
2198int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2199{
2200 /* we have buf->lr which points to next line. Look for CRLF. */
2201 while (1) {
2202 char *p1 = NULL, *p2 = NULL;
2203 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002204 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002205
2206 /* scan current line and stop at LF or CRLF */
2207 while (1) {
2208 if (ptr == buf->r)
2209 return 0;
2210
2211 if (*ptr == '\n') {
2212 if (!p1)
2213 p1 = ptr;
2214 p2 = ptr;
2215 break;
2216 }
2217
2218 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002219 if (p1) {
2220 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002221 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002222 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002223 p1 = ptr;
2224 }
2225
2226 ptr++;
2227 if (ptr >= buf->data + buf->size)
2228 ptr = buf->data;
2229 }
2230
2231 /* after LF; point to beginning of next line */
2232 p2++;
2233 if (p2 >= buf->data + buf->size)
2234 p2 = buf->data;
2235
Willy Tarreau638cd022010-01-03 07:42:04 +01002236 bytes = p2 - buf->lr;
2237 if (bytes < 0)
2238 bytes += buf->size;
2239
2240 /* schedule this line for forwarding */
2241 msg->sov += bytes;
2242 if (msg->sov >= buf->size)
2243 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002244
Willy Tarreau638cd022010-01-03 07:42:04 +01002245 if (p1 == buf->lr) {
2246 /* LF/CRLF at beginning of line => end of trailers at p2.
2247 * Everything was scheduled for forwarding, there's nothing
2248 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002249 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002250 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002251 msg->msg_state = HTTP_MSG_DONE;
2252 return 1;
2253 }
2254 /* OK, next line then */
2255 buf->lr = p2;
2256 }
2257}
2258
2259/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2260 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2261 * ->som, buf->lr in order to include this part into the next forwarding phase.
2262 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2263 * not enough data are available, the function does not change anything and
2264 * returns zero. If a parse error is encountered, the function returns < 0 and
2265 * does not change anything. Note: this function is designed to parse wrapped
2266 * CRLF at the end of the buffer.
2267 */
2268int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2269{
2270 char *ptr;
2271 int bytes;
2272
2273 /* NB: we'll check data availabilty at the end. It's not a
2274 * problem because whatever we match first will be checked
2275 * against the correct length.
2276 */
2277 bytes = 1;
2278 ptr = buf->lr;
2279 if (*ptr == '\r') {
2280 bytes++;
2281 ptr++;
2282 if (ptr >= buf->data + buf->size)
2283 ptr = buf->data;
2284 }
2285
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002286 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002287 return 0;
2288
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002289 if (*ptr != '\n') {
2290 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002291 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002292 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002293
2294 ptr++;
2295 if (ptr >= buf->data + buf->size)
2296 ptr = buf->data;
2297 buf->lr = ptr;
2298 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2299 msg->sov = ptr - buf->data;
2300 msg->som = msg->sov - bytes;
2301 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2302 return 1;
2303}
Willy Tarreau5b154472009-12-21 20:11:07 +01002304
Willy Tarreau83e3af02009-12-28 17:39:57 +01002305void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2306{
2307 char *end = buf->data + buf->size;
2308 int off = buf->data + buf->size - buf->w;
2309
2310 /* two possible cases :
2311 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002312 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002313 */
2314 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002315 int block1 = buf->l;
2316 int block2 = 0;
2317 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002318 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002319 block1 = buf->data + buf->size - buf->w;
2320 block2 = buf->r - buf->data;
2321 }
2322 if (block2)
2323 memcpy(swap_buffer, buf->data, block2);
2324 memmove(buf->data, buf->w, block1);
2325 if (block2)
2326 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002327 }
2328
2329 /* adjust all known pointers */
2330 buf->w = buf->data;
2331 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2332 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2333 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2334 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2335
2336 /* adjust relative pointers */
2337 msg->som = 0;
2338 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2339 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2340 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2341
Willy Tarreau83e3af02009-12-28 17:39:57 +01002342 if (msg->err_pos >= 0) {
2343 msg->err_pos += off;
2344 if (msg->err_pos >= buf->size)
2345 msg->err_pos -= buf->size;
2346 }
2347
2348 buf->flags &= ~BF_FULL;
2349 if (buf->l >= buffer_max_len(buf))
2350 buf->flags |= BF_FULL;
2351}
2352
Willy Tarreaud787e662009-07-07 10:14:51 +02002353/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2354 * processing can continue on next analysers, or zero if it either needs more
2355 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2356 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2357 * when it has nothing left to do, and may remove any analyser when it wants to
2358 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002359 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002360int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002361{
Willy Tarreau59234e92008-11-30 23:51:27 +01002362 /*
2363 * We will parse the partial (or complete) lines.
2364 * We will check the request syntax, and also join multi-line
2365 * headers. An index of all the lines will be elaborated while
2366 * parsing.
2367 *
2368 * For the parsing, we use a 28 states FSM.
2369 *
2370 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002371 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002372 * req->data + msg->eoh = end of processed headers / start of current one
2373 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002374 * req->lr = first non-visited byte
2375 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002376 *
2377 * At end of parsing, we may perform a capture of the error (if any), and
2378 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2379 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2380 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002382
Willy Tarreau59234e92008-11-30 23:51:27 +01002383 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002384 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 struct http_txn *txn = &s->txn;
2386 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002387 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002388
Willy Tarreau6bf17362009-02-24 10:48:35 +01002389 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2390 now_ms, __FUNCTION__,
2391 s,
2392 req,
2393 req->rex, req->wex,
2394 req->flags,
2395 req->l,
2396 req->analysers);
2397
Willy Tarreau52a0c602009-08-16 22:45:38 +02002398 /* we're speaking HTTP here, so let's speak HTTP to the client */
2399 s->srv_error = http_return_srv_error;
2400
Willy Tarreau83e3af02009-12-28 17:39:57 +01002401 /* There's a protected area at the end of the buffer for rewriting
2402 * purposes. We don't want to start to parse the request if the
2403 * protected area is affected, because we may have to move processed
2404 * data later, which is much more complicated.
2405 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002406 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002407 if ((txn->flags & TX_NOT_FIRST) &&
2408 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002409 req->r < req->lr ||
2410 req->r > req->data + req->size - global.tune.maxrewrite)) {
2411 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002412 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2413 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002414 /* some data has still not left the buffer, wake us once that's done */
2415 buffer_dont_connect(req);
2416 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2417 return 0;
2418 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002419 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002420 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002421 }
2422
Willy Tarreau065e8332010-01-08 00:30:20 +01002423 /* Note that we have the same problem with the response ; we
2424 * may want to send a redirect, error or anything which requires
2425 * some spare space. So we'll ensure that we have at least
2426 * maxrewrite bytes available in the response buffer before
2427 * processing that one. This will only affect pipelined
2428 * keep-alive requests.
2429 */
2430 if ((txn->flags & TX_NOT_FIRST) &&
2431 unlikely((s->rep->flags & BF_FULL) ||
2432 s->rep->r < s->rep->lr ||
2433 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2434 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002435 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2436 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002437 /* don't let a connection request be initiated */
2438 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002439 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002440 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002441 return 0;
2442 }
2443 }
2444
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002445 if (likely(req->lr < req->r))
2446 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002447 }
2448
Willy Tarreau59234e92008-11-30 23:51:27 +01002449 /* 1: we might have to print this header in debug mode */
2450 if (unlikely((global.mode & MODE_DEBUG) &&
2451 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002452 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002453 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002454 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002455
Willy Tarreau663308b2010-06-07 14:06:08 +02002456 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002457 eol = sol + msg->sl.rq.l;
2458 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002459
Willy Tarreau59234e92008-11-30 23:51:27 +01002460 sol += hdr_idx_first_pos(&txn->hdr_idx);
2461 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002462
Willy Tarreau59234e92008-11-30 23:51:27 +01002463 while (cur_idx) {
2464 eol = sol + txn->hdr_idx.v[cur_idx].len;
2465 debug_hdr("clihdr", s, sol, eol);
2466 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2467 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002468 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002469 }
2470
Willy Tarreau58f10d72006-12-04 02:26:12 +01002471
Willy Tarreau59234e92008-11-30 23:51:27 +01002472 /*
2473 * Now we quickly check if we have found a full valid request.
2474 * If not so, we check the FD and buffer states before leaving.
2475 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002476 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002477 * requests are checked first. When waiting for a second request
2478 * on a keep-alive session, if we encounter and error, close, t/o,
2479 * we note the error in the session flags but don't set any state.
2480 * Since the error will be noted there, it will not be counted by
2481 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002482 * Last, we may increase some tracked counters' http request errors on
2483 * the cases that are deliberately the client's fault. For instance,
2484 * a timeout or connection reset is not counted as an error. However
2485 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002486 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002487
Willy Tarreau655dce92009-11-08 13:10:58 +01002488 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002489 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002490 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002491 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002492 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002493 session_inc_http_req_ctr(s);
2494 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002495 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002496 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002497 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002498
Willy Tarreau59234e92008-11-30 23:51:27 +01002499 /* 1: Since we are in header mode, if there's no space
2500 * left for headers, we won't be able to free more
2501 * later, so the session will never terminate. We
2502 * must terminate it now.
2503 */
2504 if (unlikely(req->flags & BF_FULL)) {
2505 /* FIXME: check if URI is set and return Status
2506 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002507 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002508 session_inc_http_req_ctr(s);
2509 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002510 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002511 goto return_bad_req;
2512 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002513
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 /* 2: have we encountered a read error ? */
2515 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002516 if (!(s->flags & SN_ERR_MASK))
2517 s->flags |= SN_ERR_CLICL;
2518
Willy Tarreaufcffa692010-01-10 14:21:19 +01002519 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002520 goto failed_keep_alive;
2521
Willy Tarreau59234e92008-11-30 23:51:27 +01002522 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002523 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002524 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002525 session_inc_http_err_ctr(s);
2526 }
2527
Willy Tarreau59234e92008-11-30 23:51:27 +01002528 msg->msg_state = HTTP_MSG_ERROR;
2529 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002530
Willy Tarreauda7ff642010-06-23 11:44:09 +02002531 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002532 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002533 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002534 if (s->listener->counters)
2535 s->listener->counters->failed_req++;
2536
Willy Tarreau59234e92008-11-30 23:51:27 +01002537 if (!(s->flags & SN_FINST_MASK))
2538 s->flags |= SN_FINST_R;
2539 return 0;
2540 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002541
Willy Tarreau59234e92008-11-30 23:51:27 +01002542 /* 3: has the read timeout expired ? */
2543 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002544 if (!(s->flags & SN_ERR_MASK))
2545 s->flags |= SN_ERR_CLITO;
2546
Willy Tarreaufcffa692010-01-10 14:21:19 +01002547 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002548 goto failed_keep_alive;
2549
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002551 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002552 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002553 session_inc_http_err_ctr(s);
2554 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002555 txn->status = 408;
2556 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
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_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002561 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002562 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002563 if (s->listener->counters)
2564 s->listener->counters->failed_req++;
2565
Willy Tarreau59234e92008-11-30 23:51:27 +01002566 if (!(s->flags & SN_FINST_MASK))
2567 s->flags |= SN_FINST_R;
2568 return 0;
2569 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002570
Willy Tarreau59234e92008-11-30 23:51:27 +01002571 /* 4: have we encountered a close ? */
2572 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002573 if (!(s->flags & SN_ERR_MASK))
2574 s->flags |= SN_ERR_CLICL;
2575
Willy Tarreaufcffa692010-01-10 14:21:19 +01002576 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002577 goto failed_keep_alive;
2578
Willy Tarreau4076a152009-04-02 15:18:36 +02002579 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002580 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002581 txn->status = 400;
2582 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2583 msg->msg_state = HTTP_MSG_ERROR;
2584 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002585
Willy Tarreauda7ff642010-06-23 11:44:09 +02002586 session_inc_http_err_ctr(s);
2587 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002588 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002589 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002590 if (s->listener->counters)
2591 s->listener->counters->failed_req++;
2592
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 if (!(s->flags & SN_FINST_MASK))
2594 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002595 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002596 }
2597
Willy Tarreau520d95e2009-09-19 21:04:57 +02002598 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002599 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002600 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002601
Willy Tarreaufcffa692010-01-10 14:21:19 +01002602 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2603 /* If the client starts to talk, let's fall back to
2604 * request timeout processing.
2605 */
2606 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002607 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002608 }
2609
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002611 if (!tick_isset(req->analyse_exp)) {
2612 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2613 (txn->flags & TX_WAIT_NEXT_RQ) &&
2614 tick_isset(s->be->timeout.httpka))
2615 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2616 else
2617 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2618 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002619
Willy Tarreau59234e92008-11-30 23:51:27 +01002620 /* we're not ready yet */
2621 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002622
2623 failed_keep_alive:
2624 /* Here we process low-level errors for keep-alive requests. In
2625 * short, if the request is not the first one and it experiences
2626 * a timeout, read error or shutdown, we just silently close so
2627 * that the client can try again.
2628 */
2629 txn->status = 0;
2630 msg->msg_state = HTTP_MSG_RQBEFORE;
2631 req->analysers = 0;
2632 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002633 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002634 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002635 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002636 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002637
Willy Tarreaud787e662009-07-07 10:14:51 +02002638 /* OK now we have a complete HTTP request with indexed headers. Let's
2639 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002640 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2641 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2642 * points to the CRLF of the request line. req->lr points to the first
2643 * byte after the last LF. msg->col and msg->sov point to the first
2644 * byte of data. msg->eol cannot be trusted because it may have been
2645 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002646 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002647
Willy Tarreauda7ff642010-06-23 11:44:09 +02002648 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002649 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2650
Willy Tarreaub16a5742010-01-10 14:46:16 +01002651 if (txn->flags & TX_WAIT_NEXT_RQ) {
2652 /* kill the pending keep-alive timeout */
2653 txn->flags &= ~TX_WAIT_NEXT_RQ;
2654 req->analyse_exp = TICK_ETERNITY;
2655 }
2656
2657
Willy Tarreaud787e662009-07-07 10:14:51 +02002658 /* Maybe we found in invalid header name while we were configured not
2659 * to block on that, so we have to capture it now.
2660 */
2661 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002662 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002663
Willy Tarreau59234e92008-11-30 23:51:27 +01002664 /*
2665 * 1: identify the method
2666 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002667 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002668
2669 /* we can make use of server redirect on GET and HEAD */
2670 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2671 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002672
Willy Tarreau59234e92008-11-30 23:51:27 +01002673 /*
2674 * 2: check if the URI matches the monitor_uri.
2675 * We have to do this for every request which gets in, because
2676 * the monitor-uri is defined by the frontend.
2677 */
2678 if (unlikely((s->fe->monitor_uri_len != 0) &&
2679 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002680 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002681 s->fe->monitor_uri,
2682 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002683 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002685 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002686 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002687
Willy Tarreau59234e92008-11-30 23:51:27 +01002688 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002689
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002691 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2692 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002693
Willy Tarreau59234e92008-11-30 23:51:27 +01002694 ret = acl_pass(ret);
2695 if (cond->pol == ACL_COND_UNLESS)
2696 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002697
Willy Tarreau59234e92008-11-30 23:51:27 +01002698 if (ret) {
2699 /* we fail this request, let's return 503 service unavail */
2700 txn->status = 503;
2701 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2702 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002703 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002704 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002705
Willy Tarreau59234e92008-11-30 23:51:27 +01002706 /* nothing to fail, let's reply normaly */
2707 txn->status = 200;
2708 stream_int_retnclose(req->prod, &http_200_chunk);
2709 goto return_prx_cond;
2710 }
2711
2712 /*
2713 * 3: Maybe we have to copy the original REQURI for the logs ?
2714 * Note: we cannot log anymore if the request has been
2715 * classified as invalid.
2716 */
2717 if (unlikely(s->logs.logwait & LW_REQ)) {
2718 /* we have a complete HTTP request that we must log */
2719 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2720 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002721
Willy Tarreau59234e92008-11-30 23:51:27 +01002722 if (urilen >= REQURI_LEN)
2723 urilen = REQURI_LEN - 1;
2724 memcpy(txn->uri, &req->data[msg->som], urilen);
2725 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002726
Willy Tarreau59234e92008-11-30 23:51:27 +01002727 if (!(s->logs.logwait &= ~LW_REQ))
2728 s->do_log(s);
2729 } else {
2730 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002731 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 }
Willy Tarreau06619262006-12-17 08:37:22 +01002733
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002735 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2736 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002737
Willy Tarreau5b154472009-12-21 20:11:07 +01002738 /* ... and check if the request is HTTP/1.1 or above */
2739 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002740 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2741 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2742 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002743 txn->flags |= TX_REQ_VER_11;
2744
2745 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002746 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002747
Willy Tarreau88d349d2010-01-25 12:15:43 +01002748 /* if the frontend has "option http-use-proxy-header", we'll check if
2749 * we have what looks like a proxied connection instead of a connection,
2750 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2751 * Note that this is *not* RFC-compliant, however browsers and proxies
2752 * happen to do that despite being non-standard :-(
2753 * We consider that a request not beginning with either '/' or '*' is
2754 * a proxied connection, which covers both "scheme://location" and
2755 * CONNECT ip:port.
2756 */
2757 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2758 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2759 txn->flags |= TX_USE_PX_CONN;
2760
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002761 /* transfer length unknown*/
2762 txn->flags &= ~TX_REQ_XFER_LEN;
2763
Willy Tarreau59234e92008-11-30 23:51:27 +01002764 /* 5: we may need to capture headers */
2765 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002766 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002767 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002768
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002769 /* 6: determine the transfer-length.
2770 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2771 * the presence of a message-body in a REQUEST and its transfer length
2772 * must be determined that way (in order of precedence) :
2773 * 1. The presence of a message-body in a request is signaled by the
2774 * inclusion of a Content-Length or Transfer-Encoding header field
2775 * in the request's header fields. When a request message contains
2776 * both a message-body of non-zero length and a method that does
2777 * not define any semantics for that request message-body, then an
2778 * origin server SHOULD either ignore the message-body or respond
2779 * with an appropriate error message (e.g., 413). A proxy or
2780 * gateway, when presented the same request, SHOULD either forward
2781 * the request inbound with the message- body or ignore the
2782 * message-body when determining a response.
2783 *
2784 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2785 * and the "chunked" transfer-coding (Section 6.2) is used, the
2786 * transfer-length is defined by the use of this transfer-coding.
2787 * If a Transfer-Encoding header field is present and the "chunked"
2788 * transfer-coding is not present, the transfer-length is defined
2789 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002790 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002791 * 3. If a Content-Length header field is present, its decimal value in
2792 * OCTETs represents both the entity-length and the transfer-length.
2793 * If a message is received with both a Transfer-Encoding header
2794 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002795 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002796 * 4. By the server closing the connection. (Closing the connection
2797 * cannot be used to indicate the end of a request body, since that
2798 * would leave no possibility for the server to send back a response.)
2799 *
2800 * Whenever a transfer-coding is applied to a message-body, the set of
2801 * transfer-codings MUST include "chunked", unless the message indicates
2802 * it is terminated by closing the connection. When the "chunked"
2803 * transfer-coding is used, it MUST be the last transfer-coding applied
2804 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002805 */
2806
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002807 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002808 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002809 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002810 while ((txn->flags & TX_REQ_VER_11) &&
2811 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002812 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2813 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2814 else if (txn->flags & TX_REQ_TE_CHNK) {
2815 /* bad transfer-encoding (chunked followed by something else) */
2816 use_close_only = 1;
2817 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2818 break;
2819 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002820 }
2821
Willy Tarreau32b47f42009-10-18 20:55:02 +02002822 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002823 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002824 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2825 signed long long cl;
2826
2827 if (!ctx.vlen)
2828 goto return_bad_req;
2829
2830 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2831 goto return_bad_req; /* parse failure */
2832
2833 if (cl < 0)
2834 goto return_bad_req;
2835
Willy Tarreau124d9912011-03-01 20:30:48 +01002836 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl))
Willy Tarreau32b47f42009-10-18 20:55:02 +02002837 goto return_bad_req; /* already specified, was different */
2838
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002839 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002840 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002841 }
2842
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002843 /* bodyless requests have a known length */
2844 if (!use_close_only)
2845 txn->flags |= TX_REQ_XFER_LEN;
2846
Willy Tarreaud787e662009-07-07 10:14:51 +02002847 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002848 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002849 req->analyse_exp = TICK_ETERNITY;
2850 return 1;
2851
2852 return_bad_req:
2853 /* We centralize bad requests processing here */
2854 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2855 /* we detected a parsing error. We want to archive this request
2856 * in the dedicated proxy area for later troubleshooting.
2857 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002858 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002859 }
2860
2861 txn->req.msg_state = HTTP_MSG_ERROR;
2862 txn->status = 400;
2863 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002864
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002865 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002866 if (s->listener->counters)
2867 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002868
2869 return_prx_cond:
2870 if (!(s->flags & SN_ERR_MASK))
2871 s->flags |= SN_ERR_PRXCOND;
2872 if (!(s->flags & SN_FINST_MASK))
2873 s->flags |= SN_FINST_R;
2874
2875 req->analysers = 0;
2876 req->analyse_exp = TICK_ETERNITY;
2877 return 0;
2878}
2879
Cyril Bonté70be45d2010-10-12 00:14:35 +02002880/* We reached the stats page through a POST request.
2881 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002882 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002883 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002884int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002885{
Cyril Bonté70be45d2010-10-12 00:14:35 +02002886 struct proxy *px;
2887 struct server *sv;
2888
2889 char *backend = NULL;
2890 int action = 0;
2891
2892 char *first_param, *cur_param, *next_param, *end_params;
2893
2894 first_param = req->data + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002895 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002896
2897 cur_param = next_param = end_params;
2898
Cyril Bonté23b39d92011-02-10 22:54:44 +01002899 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002900 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002901 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002902 return 1;
2903 }
2904 else if (end_params > req->data + req->l) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002905 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002906 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002907 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002908 }
2909
2910 *end_params = '\0';
2911
Willy Tarreau295a8372011-03-10 11:25:07 +01002912 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002913
2914 /*
2915 * Parse the parameters in reverse order to only store the last value.
2916 * From the html form, the backend and the action are at the end.
2917 */
2918 while (cur_param > first_param) {
2919 char *key, *value;
2920
2921 cur_param--;
2922 if ((*cur_param == '&') || (cur_param == first_param)) {
2923 /* Parse the key */
2924 key = cur_param;
2925 if (cur_param != first_param) {
2926 /* delimit the string for the next loop */
2927 *key++ = '\0';
2928 }
2929
2930 /* Parse the value */
2931 value = key;
2932 while (*value != '\0' && *value != '=') {
2933 value++;
2934 }
2935 if (*value == '=') {
2936 /* Ok, a value is found, we can mark the end of the key */
2937 *value++ = '\0';
2938 }
2939
2940 /* Now we can check the key to see what to do */
2941 if (!backend && strcmp(key, "b") == 0) {
2942 backend = value;
2943 }
2944 else if (!action && strcmp(key, "action") == 0) {
2945 if (strcmp(value, "disable") == 0) {
2946 action = 1;
2947 }
2948 else if (strcmp(value, "enable") == 0) {
2949 action = 2;
2950 } else {
2951 /* unknown action, no need to continue */
2952 break;
2953 }
2954 }
2955 else if (strcmp(key, "s") == 0) {
2956 if (backend && action && get_backend_server(backend, value, &px, &sv)) {
2957 switch (action) {
2958 case 1:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002959 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002960 /* Not already in maintenance, we can change the server state */
2961 sv->state |= SRV_MAINTAIN;
2962 set_server_down(sv);
Willy Tarreau295a8372011-03-10 11:25:07 +01002963 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002964 }
2965 break;
2966 case 2:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002967 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002968 /* Already in maintenance, we can change the server state */
2969 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002970 sv->health = sv->rise; /* up, but will fall down at first failure */
Willy Tarreau295a8372011-03-10 11:25:07 +01002971 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002972 }
2973 break;
2974 }
2975 }
2976 }
2977 next_param = cur_param;
2978 }
2979 }
Cyril Bonté23b39d92011-02-10 22:54:44 +01002980 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002981}
2982
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002983/* returns a pointer to the first rule which forbids access (deny or http_auth),
2984 * or NULL if everything's OK.
2985 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002986static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002987http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2988{
Willy Tarreauff011f22011-01-06 17:51:27 +01002989 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002990
Willy Tarreauff011f22011-01-06 17:51:27 +01002991 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002992 int ret = 1;
2993
Willy Tarreauff011f22011-01-06 17:51:27 +01002994 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002995 continue;
2996
2997 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002998 if (rule->cond) {
2999 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003000 ret = acl_pass(ret);
3001
Willy Tarreauff011f22011-01-06 17:51:27 +01003002 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003003 ret = !ret;
3004 }
3005
3006 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003007 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003008 return NULL; /* no problem */
3009 else
Willy Tarreauff011f22011-01-06 17:51:27 +01003010 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003011 }
3012 }
3013 return NULL;
3014}
3015
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003016/* This stream analyser runs all HTTP request processing which is common to
3017 * frontends and backends, which means blocking ACLs, filters, connection-close,
3018 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003019 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003020 * either needs more data or wants to immediately abort the request (eg: deny,
3021 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003022 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003023int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003024{
Willy Tarreaud787e662009-07-07 10:14:51 +02003025 struct http_txn *txn = &s->txn;
3026 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003027 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003028 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003029 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003030 struct cond_wordlist *wl;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003031 int del_ka, del_cl, do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003032
Willy Tarreau655dce92009-11-08 13:10:58 +01003033 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003034 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003035 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003036 return 0;
3037 }
3038
Willy Tarreau3a816292009-07-07 10:55:49 +02003039 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003040 req->analyse_exp = TICK_ETERNITY;
3041
3042 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3043 now_ms, __FUNCTION__,
3044 s,
3045 req,
3046 req->rex, req->wex,
3047 req->flags,
3048 req->l,
3049 req->analysers);
3050
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003051 /* first check whether we have some ACLs set to block this request */
3052 list_for_each_entry(cond, &px->block_cond, list) {
3053 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003054
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003055 ret = acl_pass(ret);
3056 if (cond->pol == ACL_COND_UNLESS)
3057 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003058
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003059 if (ret) {
3060 txn->status = 403;
3061 /* let's log the request time */
3062 s->logs.tv_request = now;
3063 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003064 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003065 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003066 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003067 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003068
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003069 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01003070 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003071
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003072 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003073 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003074 do_stats = stats_check_uri(s->rep->prod, txn, px);
3075 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01003076 http_req_last_rule = http_check_access_rule(px, &px->uri_auth->http_req_rules, s, txn);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003077 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003078 else
3079 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003080
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003081 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01003082 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003083 txn->status = 403;
3084 s->logs.tv_request = now;
3085 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003086 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003087 s->fe->fe_counters.denied_req++;
3088 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3089 s->be->be_counters.denied_req++;
3090 if (s->listener->counters)
3091 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003092 goto return_prx_cond;
3093 }
3094
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003095 /* try headers filters */
3096 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003097 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003098 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003099
Willy Tarreau59234e92008-11-30 23:51:27 +01003100 /* has the request been denied ? */
3101 if (txn->flags & TX_CLDENY) {
3102 /* no need to go further */
3103 txn->status = 403;
3104 /* let's log the request time */
3105 s->logs.tv_request = now;
3106 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003107 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003108 goto return_prx_cond;
3109 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003110
3111 /* When a connection is tarpitted, we use the tarpit timeout,
3112 * which may be the same as the connect timeout if unspecified.
3113 * If unset, then set it to zero because we really want it to
3114 * eventually expire. We build the tarpit as an analyser.
3115 */
3116 if (txn->flags & TX_CLTARPIT) {
3117 buffer_erase(s->req);
3118 /* wipe the request out so that we can drop the connection early
3119 * if the client closes first.
3120 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003121 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003122 req->analysers = 0; /* remove switching rules etc... */
3123 req->analysers |= AN_REQ_HTTP_TARPIT;
3124 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3125 if (!req->analyse_exp)
3126 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003127 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003128 return 1;
3129 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003130 }
Willy Tarreau06619262006-12-17 08:37:22 +01003131
Willy Tarreau5b154472009-12-21 20:11:07 +01003132 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3133 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003134 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3135 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003136 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3137 * avoid to redo the same work if FE and BE have the same settings (common).
3138 * The method consists in checking if options changed between the two calls
3139 * (implying that either one is non-null, or one of them is non-null and we
3140 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003141 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003142
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003143 del_cl = del_ka = 0;
3144
Willy Tarreaudc008c52010-02-01 16:20:08 +01003145 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3146 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3147 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3148 (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 +01003149 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003150
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003151 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3152 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003153 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003154 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3155 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003156 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003157 tmp = TX_CON_WANT_CLO;
3158
Willy Tarreau5b154472009-12-21 20:11:07 +01003159 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3160 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003161
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003162 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3163 /* parse the Connection header and possibly clean it */
3164 int to_del = 0;
3165 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003166 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3167 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003168 to_del |= 2; /* remove "keep-alive" */
3169 if (!(txn->flags & TX_REQ_VER_11))
3170 to_del |= 1; /* remove "close" */
3171 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003172 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003173
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003174 /* check if client or config asks for explicit close in KAL/SCL */
3175 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3176 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3177 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
3178 (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 +01003179 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003180 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
3181 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003182 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3183 }
Willy Tarreau78599912009-10-17 20:12:21 +02003184
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003185 /* we can be blocked here because the request needs to be authenticated,
3186 * either to pass or to access stats.
3187 */
Willy Tarreauff011f22011-01-06 17:51:27 +01003188 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003189 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01003190 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003191
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003192 if (!realm)
3193 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3194
Willy Tarreau844a7e72010-01-31 21:46:18 +01003195 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003196 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3197 txn->status = 401;
3198 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003199 /* on 401 we still count one error, because normal browsing
3200 * won't significantly increase the counter but brute force
3201 * attempts will.
3202 */
3203 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003204 goto return_prx_cond;
3205 }
3206
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003207 /* add request headers from the rule sets in the same order */
3208 list_for_each_entry(wl, &px->req_add, list) {
3209 if (wl->cond) {
3210 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
3211 ret = acl_pass(ret);
3212 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3213 ret = !ret;
3214 if (!ret)
3215 continue;
3216 }
3217
3218 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
3219 goto return_bad_req;
3220 }
3221
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003222 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003223 struct stats_admin_rule *stats_admin_rule;
3224
3225 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003226 * FIXME!!! that one is rather dangerous, we want to
3227 * make it follow standard rules (eg: clear req->analysers).
3228 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003229
Cyril Bonté474be412010-10-12 00:14:36 +02003230 /* now check whether we have some admin rules for this request */
3231 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3232 int ret = 1;
3233
3234 if (stats_admin_rule->cond) {
3235 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
3236 ret = acl_pass(ret);
3237 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3238 ret = !ret;
3239 }
3240
3241 if (ret) {
3242 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01003243 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02003244 break;
3245 }
3246 }
3247
Cyril Bonté70be45d2010-10-12 00:14:35 +02003248 /* Was the status page requested with a POST ? */
3249 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003250 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003251 if (msg->msg_state < HTTP_MSG_100_SENT) {
3252 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3253 * send an HTTP/1.1 100 Continue intermediate response.
3254 */
3255 if (txn->flags & TX_REQ_VER_11) {
3256 struct hdr_ctx ctx;
3257 ctx.idx = 0;
3258 /* Expect is allowed in 1.1, look for it */
3259 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3260 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3261 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3262 }
3263 }
3264 msg->msg_state = HTTP_MSG_100_SENT;
3265 s->logs.tv_request = now; /* update the request timer to reflect full request */
3266 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003267 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003268 /* we need more data */
3269 req->analysers |= an_bit;
3270 buffer_dont_connect(req);
3271 return 0;
3272 }
Cyril Bonté474be412010-10-12 00:14:36 +02003273 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003274 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003275 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003276 }
3277
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003278 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003279 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003280 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003281 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003282 s->rep->prod->applet.private = s;
3283 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003284 req->analysers = 0;
3285
3286 return 0;
3287
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003288 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003289
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003290 /* check whether we have some ACLs set to redirect this request */
3291 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003292 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003293
Willy Tarreauf285f542010-01-03 20:03:03 +01003294 if (rule->cond) {
3295 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3296 ret = acl_pass(ret);
3297 if (rule->cond->pol == ACL_COND_UNLESS)
3298 ret = !ret;
3299 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003300
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003301 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003302 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003303 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003304
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003305 /* build redirect message */
3306 switch(rule->code) {
3307 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003308 msg_fmt = HTTP_303;
3309 break;
3310 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003311 msg_fmt = HTTP_301;
3312 break;
3313 case 302:
3314 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003315 msg_fmt = HTTP_302;
3316 break;
3317 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003318
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003319 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003320 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003321
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003322 switch(rule->type) {
3323 case REDIRECT_TYPE_PREFIX: {
3324 const char *path;
3325 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003326
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003327 path = http_get_path(txn);
3328 /* build message using path */
3329 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003330 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003331 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3332 int qs = 0;
3333 while (qs < pathlen) {
3334 if (path[qs] == '?') {
3335 pathlen = qs;
3336 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003337 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003338 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003339 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003340 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003341 } else {
3342 path = "/";
3343 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003344 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003345
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003346 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003347 goto return_bad_req;
3348
3349 /* add prefix. Note that if prefix == "/", we don't want to
3350 * add anything, otherwise it makes it hard for the user to
3351 * configure a self-redirection.
3352 */
3353 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003354 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3355 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003356 }
3357
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003358 /* add path */
3359 memcpy(rdr.str + rdr.len, path, pathlen);
3360 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003361
3362 /* append a slash at the end of the location is needed and missing */
3363 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3364 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3365 if (rdr.len > rdr.size - 5)
3366 goto return_bad_req;
3367 rdr.str[rdr.len] = '/';
3368 rdr.len++;
3369 }
3370
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003371 break;
3372 }
3373 case REDIRECT_TYPE_LOCATION:
3374 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003375 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003376 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003377
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003378 /* add location */
3379 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3380 rdr.len += rule->rdr_len;
3381 break;
3382 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003383
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003384 if (rule->cookie_len) {
3385 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3386 rdr.len += 14;
3387 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3388 rdr.len += rule->cookie_len;
3389 memcpy(rdr.str + rdr.len, "\r\n", 2);
3390 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003391 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003392
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003393 /* add end of headers and the keep-alive/close status.
3394 * We may choose to set keep-alive if the Location begins
3395 * with a slash, because the client will come back to the
3396 * same server.
3397 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003398 txn->status = rule->code;
3399 /* let's log the request time */
3400 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003401
3402 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3403 (txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau124d9912011-03-01 20:30:48 +01003404 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003405 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3406 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3407 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003408 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003409 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3410 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3411 rdr.len += 30;
3412 } else {
3413 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3414 rdr.len += 24;
3415 }
Willy Tarreau75661452010-01-10 10:35:01 +01003416 }
3417 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3418 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003419 buffer_write(req->prod->ob, rdr.str, rdr.len);
3420 /* "eat" the request */
3421 buffer_ignore(req, msg->sov - msg->som);
3422 msg->som = msg->sov;
3423 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003424 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3425 txn->req.msg_state = HTTP_MSG_CLOSED;
3426 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003427 break;
3428 } else {
3429 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003430 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3431 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3432 rdr.len += 29;
3433 } else {
3434 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3435 rdr.len += 23;
3436 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003437 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003438 goto return_prx_cond;
3439 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003440 }
3441 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003442
Willy Tarreau2be39392010-01-03 17:24:51 +01003443 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3444 * If this happens, then the data will not come immediately, so we must
3445 * send all what we have without waiting. Note that due to the small gain
3446 * in waiting for the body of the request, it's easier to simply put the
3447 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3448 * itself once used.
3449 */
3450 req->flags |= BF_SEND_DONTWAIT;
3451
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003452 /* that's OK for us now, let's move on to next analysers */
3453 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003454
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003455 return_bad_req:
3456 /* We centralize bad requests processing here */
3457 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3458 /* we detected a parsing error. We want to archive this request
3459 * in the dedicated proxy area for later troubleshooting.
3460 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003461 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003462 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003463
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003464 txn->req.msg_state = HTTP_MSG_ERROR;
3465 txn->status = 400;
3466 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003467
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003468 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003469 if (s->listener->counters)
3470 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003471
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003472 return_prx_cond:
3473 if (!(s->flags & SN_ERR_MASK))
3474 s->flags |= SN_ERR_PRXCOND;
3475 if (!(s->flags & SN_FINST_MASK))
3476 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003477
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003478 req->analysers = 0;
3479 req->analyse_exp = TICK_ETERNITY;
3480 return 0;
3481}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003482
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003483/* This function performs all the processing enabled for the current request.
3484 * It returns 1 if the processing can continue on next analysers, or zero if it
3485 * needs more data, encounters an error, or wants to immediately abort the
3486 * request. It relies on buffers flags, and updates s->req->analysers.
3487 */
3488int http_process_request(struct session *s, struct buffer *req, int an_bit)
3489{
3490 struct http_txn *txn = &s->txn;
3491 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003492
Willy Tarreau655dce92009-11-08 13:10:58 +01003493 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003494 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003495 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003496 return 0;
3497 }
3498
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003499 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3500 now_ms, __FUNCTION__,
3501 s,
3502 req,
3503 req->rex, req->wex,
3504 req->flags,
3505 req->l,
3506 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003507
Willy Tarreau59234e92008-11-30 23:51:27 +01003508 /*
3509 * Right now, we know that we have processed the entire headers
3510 * and that unwanted requests have been filtered out. We can do
3511 * whatever we want with the remaining request. Also, now we
3512 * may have separate values for ->fe, ->be.
3513 */
Willy Tarreau06619262006-12-17 08:37:22 +01003514
Willy Tarreau59234e92008-11-30 23:51:27 +01003515 /*
3516 * If HTTP PROXY is set we simply get remote server address
3517 * parsing incoming request.
3518 */
3519 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau957c0a52011-03-03 17:42:23 +01003520 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.s.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003521 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003522
Willy Tarreau59234e92008-11-30 23:51:27 +01003523 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003524 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003525 * Note that doing so might move headers in the request, but
3526 * the fields will stay coherent and the URI will not move.
3527 * This should only be performed in the backend.
3528 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003529 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003530 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3531 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003532
Willy Tarreau59234e92008-11-30 23:51:27 +01003533 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003534 * 8: the appsession cookie was looked up very early in 1.2,
3535 * so let's do the same now.
3536 */
3537
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003538 /* It needs to look into the URI unless persistence must be ignored */
3539 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003540 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003541 }
3542
3543 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003544 * 9: add X-Forwarded-For if either the frontend or the backend
3545 * asks for it.
3546 */
3547 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau957c0a52011-03-03 17:42:23 +01003548 if (s->req->prod->addr.c.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003549 /* Add an X-Forwarded-For header unless the source IP is
3550 * in the 'except' network range.
3551 */
3552 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003553 (((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003554 != s->fe->except_net.s_addr) &&
3555 (!s->be->except_mask.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003556 (((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003557 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003558 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003559 unsigned char *pn;
Willy Tarreau957c0a52011-03-03 17:42:23 +01003560 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003561
3562 /* Note: we rely on the backend to get the header name to be used for
3563 * x-forwarded-for, because the header is really meant for the backends.
3564 * However, if the backend did not specify any option, we have to rely
3565 * on the frontend's header name.
3566 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003567 if (s->be->fwdfor_hdr_len) {
3568 len = s->be->fwdfor_hdr_len;
3569 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003570 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003571 len = s->fe->fwdfor_hdr_len;
3572 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003573 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003574 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003575
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003576 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003577 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003578 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003579 }
3580 }
Willy Tarreau957c0a52011-03-03 17:42:23 +01003581 else if (s->req->prod->addr.c.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003582 /* FIXME: for the sake of completeness, we should also support
3583 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003584 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003585 int len;
3586 char pn[INET6_ADDRSTRLEN];
3587 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003588 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003589 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003590
Willy Tarreau59234e92008-11-30 23:51:27 +01003591 /* Note: we rely on the backend to get the header name to be used for
3592 * x-forwarded-for, because the header is really meant for the backends.
3593 * However, if the backend did not specify any option, we have to rely
3594 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003595 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003596 if (s->be->fwdfor_hdr_len) {
3597 len = s->be->fwdfor_hdr_len;
3598 memcpy(trash, s->be->fwdfor_hdr_name, len);
3599 } else {
3600 len = s->fe->fwdfor_hdr_len;
3601 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003602 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003603 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003604
Willy Tarreau59234e92008-11-30 23:51:27 +01003605 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003606 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003607 goto return_bad_req;
3608 }
3609 }
3610
3611 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003612 * 10: add X-Original-To if either the frontend or the backend
3613 * asks for it.
3614 */
3615 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3616
3617 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau957c0a52011-03-03 17:42:23 +01003618 if (s->req->prod->addr.c.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003619 /* Add an X-Original-To header unless the destination IP is
3620 * in the 'except' network range.
3621 */
3622 if (!(s->flags & SN_FRT_ADDR_SET))
3623 get_frt_addr(s);
3624
Willy Tarreau957c0a52011-03-03 17:42:23 +01003625 if (s->req->prod->addr.c.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003626 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003627 (((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003628 != s->fe->except_to.s_addr) &&
3629 (!s->be->except_mask_to.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003630 (((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003631 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003632 int len;
3633 unsigned char *pn;
Willy Tarreau957c0a52011-03-03 17:42:23 +01003634 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003635
3636 /* Note: we rely on the backend to get the header name to be used for
3637 * x-original-to, because the header is really meant for the backends.
3638 * However, if the backend did not specify any option, we have to rely
3639 * on the frontend's header name.
3640 */
3641 if (s->be->orgto_hdr_len) {
3642 len = s->be->orgto_hdr_len;
3643 memcpy(trash, s->be->orgto_hdr_name, len);
3644 } else {
3645 len = s->fe->orgto_hdr_len;
3646 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003647 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003648 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3649
3650 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003651 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003652 goto return_bad_req;
3653 }
3654 }
3655 }
3656
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003657 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3658 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003659 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003660 unsigned int want_flags = 0;
3661
3662 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003663 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3664 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3665 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003666 want_flags |= TX_CON_CLO_SET;
3667 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003668 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3669 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3670 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003671 want_flags |= TX_CON_KAL_SET;
3672 }
3673
3674 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3675 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003676 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003677
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003678
Willy Tarreau522d6c02009-12-06 18:49:18 +01003679 /* If we have no server assigned yet and we're balancing on url_param
3680 * with a POST request, we may be interested in checking the body for
3681 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003682 */
3683 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3684 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003685 s->be->url_param_post_limit != 0 &&
Willy Tarreau61a21a32011-03-01 20:35:49 +01003686 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003687 buffer_dont_connect(req);
3688 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003689 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003690
Willy Tarreaud98cf932009-12-27 22:54:55 +01003691 if (txn->flags & TX_REQ_XFER_LEN)
3692 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003693
Willy Tarreau59234e92008-11-30 23:51:27 +01003694 /*************************************************************
3695 * OK, that's finished for the headers. We have done what we *
3696 * could. Let's switch to the DATA state. *
3697 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003698 req->analyse_exp = TICK_ETERNITY;
3699 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003700
Willy Tarreau59234e92008-11-30 23:51:27 +01003701 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003702 /* OK let's go on with the BODY now */
3703 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003704
Willy Tarreau59234e92008-11-30 23:51:27 +01003705 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003706 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003707 /* we detected a parsing error. We want to archive this request
3708 * in the dedicated proxy area for later troubleshooting.
3709 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003710 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003711 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003712
Willy Tarreau59234e92008-11-30 23:51:27 +01003713 txn->req.msg_state = HTTP_MSG_ERROR;
3714 txn->status = 400;
3715 req->analysers = 0;
3716 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003717
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003718 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003719 if (s->listener->counters)
3720 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003721
Willy Tarreau59234e92008-11-30 23:51:27 +01003722 if (!(s->flags & SN_ERR_MASK))
3723 s->flags |= SN_ERR_PRXCOND;
3724 if (!(s->flags & SN_FINST_MASK))
3725 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003726 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003727}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003728
Willy Tarreau60b85b02008-11-30 23:28:40 +01003729/* This function is an analyser which processes the HTTP tarpit. It always
3730 * returns zero, at the beginning because it prevents any other processing
3731 * from occurring, and at the end because it terminates the request.
3732 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003733int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003734{
3735 struct http_txn *txn = &s->txn;
3736
3737 /* This connection is being tarpitted. The CLIENT side has
3738 * already set the connect expiration date to the right
3739 * timeout. We just have to check that the client is still
3740 * there and that the timeout has not expired.
3741 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003742 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003743 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3744 !tick_is_expired(req->analyse_exp, now_ms))
3745 return 0;
3746
3747 /* We will set the queue timer to the time spent, just for
3748 * logging purposes. We fake a 500 server error, so that the
3749 * attacker will not suspect his connection has been tarpitted.
3750 * It will not cause trouble to the logs because we can exclude
3751 * the tarpitted connections by filtering on the 'PT' status flags.
3752 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003753 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3754
3755 txn->status = 500;
3756 if (req->flags != BF_READ_ERROR)
3757 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3758
3759 req->analysers = 0;
3760 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003761
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003762 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003763 if (s->listener->counters)
3764 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003765
Willy Tarreau60b85b02008-11-30 23:28:40 +01003766 if (!(s->flags & SN_ERR_MASK))
3767 s->flags |= SN_ERR_PRXCOND;
3768 if (!(s->flags & SN_FINST_MASK))
3769 s->flags |= SN_FINST_T;
3770 return 0;
3771}
3772
Willy Tarreaud34af782008-11-30 23:36:37 +01003773/* This function is an analyser which processes the HTTP request body. It looks
3774 * for parameters to be used for the load balancing algorithm (url_param). It
3775 * must only be called after the standard HTTP request processing has occurred,
3776 * because it expects the request to be parsed. It returns zero if it needs to
3777 * read more data, or 1 once it has completed its analysis.
3778 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003779int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003780{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003781 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003782 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003783 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003784
3785 /* We have to parse the HTTP request body to find any required data.
3786 * "balance url_param check_post" should have been the only way to get
3787 * into this. We were brought here after HTTP header analysis, so all
3788 * related structures are ready.
3789 */
3790
Willy Tarreau522d6c02009-12-06 18:49:18 +01003791 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3792 goto missing_data;
3793
3794 if (msg->msg_state < HTTP_MSG_100_SENT) {
3795 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3796 * send an HTTP/1.1 100 Continue intermediate response.
3797 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003798 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003799 struct hdr_ctx ctx;
3800 ctx.idx = 0;
3801 /* Expect is allowed in 1.1, look for it */
3802 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3803 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3804 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3805 }
3806 }
3807 msg->msg_state = HTTP_MSG_100_SENT;
3808 }
3809
3810 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003811 /* we have msg->col and msg->sov which both point to the first
3812 * byte of message body. msg->som still points to the beginning
3813 * of the message. We must save the body in req->lr because it
3814 * survives buffer re-alignments.
3815 */
3816 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003817 if (txn->flags & TX_REQ_TE_CHNK)
3818 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3819 else
3820 msg->msg_state = HTTP_MSG_DATA;
3821 }
3822
3823 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003824 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003825 * set ->sov and ->lr to point to the body and switch to DATA or
3826 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003827 */
3828 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003829
Willy Tarreau115acb92009-12-26 13:56:06 +01003830 if (!ret)
3831 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003832 else if (ret < 0) {
3833 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003834 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003835 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003836 }
3837
Willy Tarreaud98cf932009-12-27 22:54:55 +01003838 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003839 * We have the first non-header byte in msg->col, which is either the
3840 * beginning of the chunk size or of the data. The first data byte is in
3841 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3842 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003843 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003844
Willy Tarreau124d9912011-03-01 20:30:48 +01003845 if (msg->body_len < limit)
3846 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003847
Willy Tarreau7c96f672009-12-27 22:47:25 +01003848 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003849 goto http_end;
3850
3851 missing_data:
3852 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003853 if (req->flags & BF_FULL) {
3854 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003855 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003856 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003857
Willy Tarreau522d6c02009-12-06 18:49:18 +01003858 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3859 txn->status = 408;
3860 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003861
3862 if (!(s->flags & SN_ERR_MASK))
3863 s->flags |= SN_ERR_CLITO;
3864 if (!(s->flags & SN_FINST_MASK))
3865 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003866 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003867 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003868
3869 /* we get here if we need to wait for more data */
3870 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003871 /* Not enough data. We'll re-use the http-request
3872 * timeout here. Ideally, we should set the timeout
3873 * relative to the accept() date. We just set the
3874 * request timeout once at the beginning of the
3875 * request.
3876 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003877 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003878 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003879 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003880 return 0;
3881 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003882
3883 http_end:
3884 /* The situation will not evolve, so let's give up on the analysis. */
3885 s->logs.tv_request = now; /* update the request timer to reflect full request */
3886 req->analysers &= ~an_bit;
3887 req->analyse_exp = TICK_ETERNITY;
3888 return 1;
3889
3890 return_bad_req: /* let's centralize all bad requests */
3891 txn->req.msg_state = HTTP_MSG_ERROR;
3892 txn->status = 400;
3893 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3894
Willy Tarreau79ebac62010-06-07 13:47:49 +02003895 if (!(s->flags & SN_ERR_MASK))
3896 s->flags |= SN_ERR_PRXCOND;
3897 if (!(s->flags & SN_FINST_MASK))
3898 s->flags |= SN_FINST_R;
3899
Willy Tarreau522d6c02009-12-06 18:49:18 +01003900 return_err_msg:
3901 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003902 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003903 if (s->listener->counters)
3904 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003905 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003906}
3907
Willy Tarreau610ecce2010-01-04 21:15:02 +01003908/* Terminate current transaction and prepare a new one. This is very tricky
3909 * right now but it works.
3910 */
3911void http_end_txn_clean_session(struct session *s)
3912{
3913 /* FIXME: We need a more portable way of releasing a backend's and a
3914 * server's connections. We need a safer way to reinitialize buffer
3915 * flags. We also need a more accurate method for computing per-request
3916 * data.
3917 */
3918 http_silent_debug(__LINE__, s);
3919
3920 s->req->cons->flags |= SI_FL_NOLINGER;
3921 s->req->cons->shutr(s->req->cons);
3922 s->req->cons->shutw(s->req->cons);
3923
3924 http_silent_debug(__LINE__, s);
3925
3926 if (s->flags & SN_BE_ASSIGNED)
3927 s->be->beconn--;
3928
3929 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3930 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003931 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003932
3933 if (s->txn.status) {
3934 int n;
3935
3936 n = s->txn.status / 100;
3937 if (n < 1 || n > 5)
3938 n = 0;
3939
3940 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003941 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003942
Willy Tarreau24657792010-02-26 10:30:28 +01003943 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003944 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003945 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003946 }
3947
3948 /* don't count other requests' data */
3949 s->logs.bytes_in -= s->req->l - s->req->send_max;
3950 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3951
3952 /* let's do a final log if we need it */
3953 if (s->logs.logwait &&
3954 !(s->flags & SN_MONITOR) &&
3955 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3956 s->do_log(s);
3957 }
3958
3959 s->logs.accept_date = date; /* user-visible date for logging */
3960 s->logs.tv_accept = now; /* corrected date for internal use */
3961 tv_zero(&s->logs.tv_request);
3962 s->logs.t_queue = -1;
3963 s->logs.t_connect = -1;
3964 s->logs.t_data = -1;
3965 s->logs.t_close = 0;
3966 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3967 s->logs.srv_queue_size = 0; /* we will get this number soon */
3968
3969 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3970 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3971
3972 if (s->pend_pos)
3973 pendconn_free(s->pend_pos);
3974
Willy Tarreau827aee92011-03-10 16:55:02 +01003975 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003976 if (s->flags & SN_CURR_SESS) {
3977 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003978 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003979 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003980 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3981 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003982 }
3983
3984 if (unlikely(s->srv_conn))
3985 sess_change_server(s, NULL);
Willy Tarreau9e000c62011-03-10 14:03:36 +01003986 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003987
3988 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3989 s->req->cons->fd = -1; /* just to help with debugging */
3990 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003991 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003992 s->req->cons->err_loc = NULL;
3993 s->req->cons->exp = TICK_ETERNITY;
3994 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003995 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3996 s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003997 s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003998 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3999 s->txn.meth = 0;
4000 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004001 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004002 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004003 s->req->cons->flags |= SI_FL_INDEP_STR;
4004
4005 /* if the request buffer is not empty, it means we're
4006 * about to process another request, so send pending
4007 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004008 * Just don't do this if the buffer is close to be full,
4009 * because the request will wait for it to flush a little
4010 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004011 */
Willy Tarreau065e8332010-01-08 00:30:20 +01004012 if (s->req->l > s->req->send_max) {
4013 if (s->rep->send_max &&
4014 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01004015 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
4016 s->rep->flags |= BF_EXPECT_MORE;
4017 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004018
4019 /* we're removing the analysers, we MUST re-enable events detection */
4020 buffer_auto_read(s->req);
4021 buffer_auto_close(s->req);
4022 buffer_auto_read(s->rep);
4023 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004024
4025 /* make ->lr point to the first non-forwarded byte */
4026 s->req->lr = s->req->w + s->req->send_max;
4027 if (s->req->lr >= s->req->data + s->req->size)
4028 s->req->lr -= s->req->size;
4029 s->rep->lr = s->rep->w + s->rep->send_max;
4030 if (s->rep->lr >= s->rep->data + s->rep->size)
4031 s->rep->lr -= s->req->size;
4032
Willy Tarreau342b11c2010-11-24 16:22:09 +01004033 s->req->analysers = s->listener->analysers;
4034 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004035 s->rep->analysers = 0;
4036
4037 http_silent_debug(__LINE__, s);
4038}
4039
4040
4041/* This function updates the request state machine according to the response
4042 * state machine and buffer flags. It returns 1 if it changes anything (flag
4043 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4044 * it is only used to find when a request/response couple is complete. Both
4045 * this function and its equivalent should loop until both return zero. It
4046 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4047 */
4048int http_sync_req_state(struct session *s)
4049{
4050 struct buffer *buf = s->req;
4051 struct http_txn *txn = &s->txn;
4052 unsigned int old_flags = buf->flags;
4053 unsigned int old_state = txn->req.msg_state;
4054
4055 http_silent_debug(__LINE__, s);
4056 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4057 return 0;
4058
4059 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004060 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004061 * We can shut the read side unless we want to abort_on_close,
4062 * or we have a POST request. The issue with POST requests is
4063 * that some browsers still send a CRLF after the request, and
4064 * this CRLF must be read so that it does not remain in the kernel
4065 * buffers, otherwise a close could cause an RST on some systems
4066 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004067 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004068 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01004069 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004070
4071 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4072 goto wait_other_side;
4073
4074 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4075 /* The server has not finished to respond, so we
4076 * don't want to move in order not to upset it.
4077 */
4078 goto wait_other_side;
4079 }
4080
4081 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4082 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004083 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004084 txn->req.msg_state = HTTP_MSG_TUNNEL;
4085 goto wait_other_side;
4086 }
4087
4088 /* When we get here, it means that both the request and the
4089 * response have finished receiving. Depending on the connection
4090 * mode, we'll have to wait for the last bytes to leave in either
4091 * direction, and sometimes for a close to be effective.
4092 */
4093
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004094 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4095 /* Server-close mode : queue a connection close to the server */
4096 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004097 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004098 }
4099 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4100 /* Option forceclose is set, or either side wants to close,
4101 * let's enforce it now that we're not expecting any new
4102 * data to come. The caller knows the session is complete
4103 * once both states are CLOSED.
4104 */
4105 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004106 buffer_shutr_now(buf);
4107 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004108 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004109 }
4110 else {
4111 /* The last possible modes are keep-alive and tunnel. Since tunnel
4112 * mode does not set the body analyser, we can't reach this place
4113 * in tunnel mode, so we're left with keep-alive only.
4114 * This mode is currently not implemented, we switch to tunnel mode.
4115 */
4116 buffer_auto_read(buf);
4117 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004118 }
4119
4120 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4121 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004122 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4123
Willy Tarreau610ecce2010-01-04 21:15:02 +01004124 if (!(buf->flags & BF_OUT_EMPTY)) {
4125 txn->req.msg_state = HTTP_MSG_CLOSING;
4126 goto http_msg_closing;
4127 }
4128 else {
4129 txn->req.msg_state = HTTP_MSG_CLOSED;
4130 goto http_msg_closed;
4131 }
4132 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004133 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004134 }
4135
4136 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4137 http_msg_closing:
4138 /* nothing else to forward, just waiting for the output buffer
4139 * to be empty and for the shutw_now to take effect.
4140 */
4141 if (buf->flags & BF_OUT_EMPTY) {
4142 txn->req.msg_state = HTTP_MSG_CLOSED;
4143 goto http_msg_closed;
4144 }
4145 else if (buf->flags & BF_SHUTW) {
4146 txn->req.msg_state = HTTP_MSG_ERROR;
4147 goto wait_other_side;
4148 }
4149 }
4150
4151 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4152 http_msg_closed:
4153 goto wait_other_side;
4154 }
4155
4156 wait_other_side:
4157 http_silent_debug(__LINE__, s);
4158 return txn->req.msg_state != old_state || buf->flags != old_flags;
4159}
4160
4161
4162/* This function updates the response state machine according to the request
4163 * state machine and buffer flags. It returns 1 if it changes anything (flag
4164 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4165 * it is only used to find when a request/response couple is complete. Both
4166 * this function and its equivalent should loop until both return zero. It
4167 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4168 */
4169int http_sync_res_state(struct session *s)
4170{
4171 struct buffer *buf = s->rep;
4172 struct http_txn *txn = &s->txn;
4173 unsigned int old_flags = buf->flags;
4174 unsigned int old_state = txn->rsp.msg_state;
4175
4176 http_silent_debug(__LINE__, s);
4177 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4178 return 0;
4179
4180 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4181 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004182 * still monitor the server connection for a possible close
4183 * while the request is being uploaded, so we don't disable
4184 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004185 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004186 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004187
4188 if (txn->req.msg_state == HTTP_MSG_ERROR)
4189 goto wait_other_side;
4190
4191 if (txn->req.msg_state < HTTP_MSG_DONE) {
4192 /* The client seems to still be sending data, probably
4193 * because we got an error response during an upload.
4194 * We have the choice of either breaking the connection
4195 * or letting it pass through. Let's do the later.
4196 */
4197 goto wait_other_side;
4198 }
4199
4200 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4201 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004202 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004203 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4204 goto wait_other_side;
4205 }
4206
4207 /* When we get here, it means that both the request and the
4208 * response have finished receiving. Depending on the connection
4209 * mode, we'll have to wait for the last bytes to leave in either
4210 * direction, and sometimes for a close to be effective.
4211 */
4212
4213 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4214 /* Server-close mode : shut read and wait for the request
4215 * side to close its output buffer. The caller will detect
4216 * when we're in DONE and the other is in CLOSED and will
4217 * catch that for the final cleanup.
4218 */
4219 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4220 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004221 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004222 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4223 /* Option forceclose is set, or either side wants to close,
4224 * let's enforce it now that we're not expecting any new
4225 * data to come. The caller knows the session is complete
4226 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004227 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004228 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4229 buffer_shutr_now(buf);
4230 buffer_shutw_now(buf);
4231 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004232 }
4233 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004234 /* The last possible modes are keep-alive and tunnel. Since tunnel
4235 * mode does not set the body analyser, we can't reach this place
4236 * in tunnel mode, so we're left with keep-alive only.
4237 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004238 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004239 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004240 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004241 }
4242
4243 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4244 /* if we've just closed an output, let's switch */
4245 if (!(buf->flags & BF_OUT_EMPTY)) {
4246 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4247 goto http_msg_closing;
4248 }
4249 else {
4250 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4251 goto http_msg_closed;
4252 }
4253 }
4254 goto wait_other_side;
4255 }
4256
4257 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4258 http_msg_closing:
4259 /* nothing else to forward, just waiting for the output buffer
4260 * to be empty and for the shutw_now to take effect.
4261 */
4262 if (buf->flags & BF_OUT_EMPTY) {
4263 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4264 goto http_msg_closed;
4265 }
4266 else if (buf->flags & BF_SHUTW) {
4267 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004268 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004269 if (target_srv(&s->target))
4270 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004271 goto wait_other_side;
4272 }
4273 }
4274
4275 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4276 http_msg_closed:
4277 /* drop any pending data */
4278 buffer_ignore(buf, buf->l - buf->send_max);
4279 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004280 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004281 goto wait_other_side;
4282 }
4283
4284 wait_other_side:
4285 http_silent_debug(__LINE__, s);
4286 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4287}
4288
4289
4290/* Resync the request and response state machines. Return 1 if either state
4291 * changes.
4292 */
4293int http_resync_states(struct session *s)
4294{
4295 struct http_txn *txn = &s->txn;
4296 int old_req_state = txn->req.msg_state;
4297 int old_res_state = txn->rsp.msg_state;
4298
4299 http_silent_debug(__LINE__, s);
4300 http_sync_req_state(s);
4301 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004302 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004303 if (!http_sync_res_state(s))
4304 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004305 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004306 if (!http_sync_req_state(s))
4307 break;
4308 }
4309 http_silent_debug(__LINE__, s);
4310 /* OK, both state machines agree on a compatible state.
4311 * There are a few cases we're interested in :
4312 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4313 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4314 * directions, so let's simply disable both analysers.
4315 * - HTTP_MSG_CLOSED on the response only means we must abort the
4316 * request.
4317 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4318 * with server-close mode means we've completed one request and we
4319 * must re-initialize the server connection.
4320 */
4321
4322 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4323 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4324 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4325 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4326 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004327 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004328 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004329 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004330 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004331 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004332 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004333 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4334 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004335 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004336 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004337 s->rep->analysers = 0;
4338 buffer_auto_close(s->rep);
4339 buffer_auto_read(s->rep);
4340 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004341 buffer_abort(s->req);
4342 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004343 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004344 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004345 }
4346 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4347 txn->rsp.msg_state == HTTP_MSG_DONE &&
4348 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4349 /* server-close: terminate this server connection and
4350 * reinitialize a fresh-new transaction.
4351 */
4352 http_end_txn_clean_session(s);
4353 }
4354
4355 http_silent_debug(__LINE__, s);
4356 return txn->req.msg_state != old_req_state ||
4357 txn->rsp.msg_state != old_res_state;
4358}
4359
Willy Tarreaud98cf932009-12-27 22:54:55 +01004360/* This function is an analyser which forwards request body (including chunk
4361 * sizes if any). It is called as soon as we must forward, even if we forward
4362 * zero byte. The only situation where it must not be called is when we're in
4363 * tunnel mode and we want to forward till the close. It's used both to forward
4364 * remaining data and to resync after end of body. It expects the msg_state to
4365 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4366 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004367 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004368 * bytes of pending data + the headers if not already done (between som and sov).
4369 * It eventually adjusts som to match sov after the data in between have been sent.
4370 */
4371int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4372{
4373 struct http_txn *txn = &s->txn;
4374 struct http_msg *msg = &s->txn.req;
4375
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004376 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4377 return 0;
4378
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004379 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4380 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004381 /* Output closed while we were sending data. We must abort and
4382 * wake the other side up.
4383 */
4384 msg->msg_state = HTTP_MSG_ERROR;
4385 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004386 return 1;
4387 }
4388
Willy Tarreau4fe41902010-06-07 22:27:41 +02004389 /* in most states, we should abort in case of early close */
4390 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004391
4392 /* Note that we don't have to send 100-continue back because we don't
4393 * need the data to complete our job, and it's up to the server to
4394 * decide whether to return 100, 417 or anything else in return of
4395 * an "Expect: 100-continue" header.
4396 */
4397
4398 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4399 /* we have msg->col and msg->sov which both point to the first
4400 * byte of message body. msg->som still points to the beginning
4401 * of the message. We must save the body in req->lr because it
4402 * survives buffer re-alignments.
4403 */
4404 req->lr = req->data + msg->sov;
4405 if (txn->flags & TX_REQ_TE_CHNK)
4406 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4407 else {
4408 msg->msg_state = HTTP_MSG_DATA;
4409 }
4410 }
4411
Willy Tarreaud98cf932009-12-27 22:54:55 +01004412 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004413 int bytes;
4414
Willy Tarreau610ecce2010-01-04 21:15:02 +01004415 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004416 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004417 bytes = msg->sov - msg->som;
4418 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004419 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004420 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4421 bytes += req->size;
4422 msg->chunk_len += (unsigned int)bytes;
4423 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004424 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004425
Willy Tarreaucaabe412010-01-03 23:08:28 +01004426 if (msg->msg_state == HTTP_MSG_DATA) {
4427 /* must still forward */
4428 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004429 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004430
4431 /* nothing left to forward */
4432 if (txn->flags & TX_REQ_TE_CHNK)
4433 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004434 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004435 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004436 }
4437 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004438 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004439 * set ->sov and ->lr to point to the body and switch to DATA or
4440 * TRAILERS state.
4441 */
4442 int ret = http_parse_chunk_size(req, msg);
4443
4444 if (!ret)
4445 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004446 else if (ret < 0) {
4447 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004448 if (msg->err_pos >= 0)
4449 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004450 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004451 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004452 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreau57f5c122010-12-02 00:37:14 +01004453 /* Don't set a PUSH at the end of that chunk if it's not the last one */
4454 if (msg->msg_state == HTTP_MSG_DATA)
4455 req->flags |= BF_EXPECT_MORE;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004456 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004457 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4458 /* we want the CRLF after the data */
4459 int ret;
4460
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004461 req->lr = req->w + req->send_max;
4462 if (req->lr >= req->data + req->size)
4463 req->lr -= req->size;
4464
Willy Tarreaud98cf932009-12-27 22:54:55 +01004465 ret = http_skip_chunk_crlf(req, msg);
4466
4467 if (ret == 0)
4468 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004469 else if (ret < 0) {
4470 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004471 if (msg->err_pos >= 0)
4472 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004473 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004474 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004475 /* we're in MSG_CHUNK_SIZE now */
4476 }
4477 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4478 int ret = http_forward_trailers(req, msg);
4479
4480 if (ret == 0)
4481 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004482 else if (ret < 0) {
4483 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004484 if (msg->err_pos >= 0)
4485 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004486 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004487 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004488 /* we're in HTTP_MSG_DONE now */
4489 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004490 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004491 int old_state = msg->msg_state;
4492
Willy Tarreau610ecce2010-01-04 21:15:02 +01004493 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004494 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004495 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4496 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4497 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004498 if (http_resync_states(s)) {
4499 /* some state changes occurred, maybe the analyser
4500 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004501 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004502 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4503 if (req->flags & BF_SHUTW) {
4504 /* request errors are most likely due to
4505 * the server aborting the transfer.
4506 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004507 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004508 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004509 if (msg->err_pos >= 0)
4510 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004511 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004512 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004513 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004514 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004515
4516 /* If "option abortonclose" is set on the backend, we
4517 * want to monitor the client's connection and forward
4518 * any shutdown notification to the server, which will
4519 * decide whether to close or to go on processing the
4520 * request.
4521 */
4522 if (s->be->options & PR_O_ABRT_CLOSE) {
4523 buffer_auto_read(req);
4524 buffer_auto_close(req);
4525 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004526 else if (s->txn.meth == HTTP_METH_POST) {
4527 /* POST requests may require to read extra CRLF
4528 * sent by broken browsers and which could cause
4529 * an RST to be sent upon close on some systems
4530 * (eg: Linux).
4531 */
4532 buffer_auto_read(req);
4533 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004534
Willy Tarreau610ecce2010-01-04 21:15:02 +01004535 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004536 }
4537 }
4538
Willy Tarreaud98cf932009-12-27 22:54:55 +01004539 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004540 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004541 if (req->flags & BF_SHUTR) {
4542 if (!(s->flags & SN_ERR_MASK))
4543 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004544 if (!(s->flags & SN_FINST_MASK)) {
4545 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4546 s->flags |= SN_FINST_H;
4547 else
4548 s->flags |= SN_FINST_D;
4549 }
4550
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004551 s->fe->fe_counters.cli_aborts++;
4552 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004553 if (target_srv(&s->target))
4554 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004555
4556 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004557 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004558
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004559 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004560 if (req->flags & BF_SHUTW)
4561 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004562
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004563 /* When TE: chunked is used, we need to get there again to parse remaining
4564 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4565 */
4566 if (txn->flags & TX_REQ_TE_CHNK)
4567 buffer_dont_close(req);
4568
Willy Tarreau610ecce2010-01-04 21:15:02 +01004569 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004570 return 0;
4571
Willy Tarreaud98cf932009-12-27 22:54:55 +01004572 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004573 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004574 if (s->listener->counters)
4575 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004576 return_bad_req_stats_ok:
4577 txn->req.msg_state = HTTP_MSG_ERROR;
4578 if (txn->status) {
4579 /* Note: we don't send any error if some data were already sent */
4580 stream_int_retnclose(req->prod, NULL);
4581 } else {
4582 txn->status = 400;
4583 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4584 }
4585 req->analysers = 0;
4586 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004587
4588 if (!(s->flags & SN_ERR_MASK))
4589 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004590 if (!(s->flags & SN_FINST_MASK)) {
4591 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4592 s->flags |= SN_FINST_H;
4593 else
4594 s->flags |= SN_FINST_D;
4595 }
4596 return 0;
4597
4598 aborted_xfer:
4599 txn->req.msg_state = HTTP_MSG_ERROR;
4600 if (txn->status) {
4601 /* Note: we don't send any error if some data were already sent */
4602 stream_int_retnclose(req->prod, NULL);
4603 } else {
4604 txn->status = 502;
4605 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4606 }
4607 req->analysers = 0;
4608 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4609
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004610 s->fe->fe_counters.srv_aborts++;
4611 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004612 if (target_srv(&s->target))
4613 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004614
4615 if (!(s->flags & SN_ERR_MASK))
4616 s->flags |= SN_ERR_SRVCL;
4617 if (!(s->flags & SN_FINST_MASK)) {
4618 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4619 s->flags |= SN_FINST_H;
4620 else
4621 s->flags |= SN_FINST_D;
4622 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004623 return 0;
4624}
4625
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004626/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4627 * processing can continue on next analysers, or zero if it either needs more
4628 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4629 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4630 * when it has nothing left to do, and may remove any analyser when it wants to
4631 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004632 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004633int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004634{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004635 struct http_txn *txn = &s->txn;
4636 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004637 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004638 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004639 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004640 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004641
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004642 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 +02004643 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004644 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004645 rep,
4646 rep->rex, rep->wex,
4647 rep->flags,
4648 rep->l,
4649 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004650
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004651 /*
4652 * Now parse the partial (or complete) lines.
4653 * We will check the response syntax, and also join multi-line
4654 * headers. An index of all the lines will be elaborated while
4655 * parsing.
4656 *
4657 * For the parsing, we use a 28 states FSM.
4658 *
4659 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004660 * rep->data + msg->som = beginning of response
4661 * rep->data + msg->eoh = end of processed headers / start of current one
4662 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004663 * rep->lr = first non-visited byte
4664 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004665 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004666 */
4667
Willy Tarreau83e3af02009-12-28 17:39:57 +01004668 /* There's a protected area at the end of the buffer for rewriting
4669 * purposes. We don't want to start to parse the request if the
4670 * protected area is affected, because we may have to move processed
4671 * data later, which is much more complicated.
4672 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004673 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4674 if (unlikely((rep->flags & BF_FULL) ||
4675 rep->r < rep->lr ||
4676 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4677 if (rep->send_max) {
4678 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004679 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4680 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004681 buffer_dont_close(rep);
4682 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4683 return 0;
4684 }
4685 if (rep->l <= rep->size - global.tune.maxrewrite)
4686 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004687 }
4688
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004689 if (likely(rep->lr < rep->r))
4690 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004691 }
4692
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004693 /* 1: we might have to print this header in debug mode */
4694 if (unlikely((global.mode & MODE_DEBUG) &&
4695 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004696 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004697 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004698 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004699
Willy Tarreau663308b2010-06-07 14:06:08 +02004700 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004701 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004702 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004703
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004704 sol += hdr_idx_first_pos(&txn->hdr_idx);
4705 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004706
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004707 while (cur_idx) {
4708 eol = sol + txn->hdr_idx.v[cur_idx].len;
4709 debug_hdr("srvhdr", s, sol, eol);
4710 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4711 cur_idx = txn->hdr_idx.v[cur_idx].next;
4712 }
4713 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004714
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004715 /*
4716 * Now we quickly check if we have found a full valid response.
4717 * If not so, we check the FD and buffer states before leaving.
4718 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004719 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004720 * responses are checked first.
4721 *
4722 * Depending on whether the client is still there or not, we
4723 * may send an error response back or not. Note that normally
4724 * we should only check for HTTP status there, and check I/O
4725 * errors somewhere else.
4726 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004727
Willy Tarreau655dce92009-11-08 13:10:58 +01004728 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004729 /* Invalid response */
4730 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4731 /* we detected a parsing error. We want to archive this response
4732 * in the dedicated proxy area for later troubleshooting.
4733 */
4734 hdr_response_bad:
4735 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004736 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004737
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004738 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004739 if (target_srv(&s->target)) {
4740 target_srv(&s->target)->counters.failed_resp++;
4741 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004742 }
Willy Tarreau64648412010-03-05 10:41:54 +01004743 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004744 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004745 rep->analysers = 0;
4746 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004747 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004748 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004749 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4750
4751 if (!(s->flags & SN_ERR_MASK))
4752 s->flags |= SN_ERR_PRXCOND;
4753 if (!(s->flags & SN_FINST_MASK))
4754 s->flags |= SN_FINST_H;
4755
4756 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004757 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004758
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004759 /* too large response does not fit in buffer. */
4760 else if (rep->flags & BF_FULL) {
4761 goto hdr_response_bad;
4762 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004763
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004764 /* read error */
4765 else if (rep->flags & BF_READ_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);
Willy Tarreau4076a152009-04-02 15:18:36 +02004768
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004769 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004770 if (target_srv(&s->target)) {
4771 target_srv(&s->target)->counters.failed_resp++;
4772 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004773 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004774
Willy Tarreau90deb182010-01-07 00:20:41 +01004775 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004776 rep->analysers = 0;
4777 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004778 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004779 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004780 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004781
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004782 if (!(s->flags & SN_ERR_MASK))
4783 s->flags |= SN_ERR_SRVCL;
4784 if (!(s->flags & SN_FINST_MASK))
4785 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004786 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004787 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004788
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004789 /* read timeout : return a 504 to the client. */
4790 else if (rep->flags & BF_READ_TIMEOUT) {
4791 if (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 Tarreau21d2af32008-02-14 20:25:24 +01004793
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004794 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004795 if (target_srv(&s->target)) {
4796 target_srv(&s->target)->counters.failed_resp++;
4797 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004798 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004799
Willy Tarreau90deb182010-01-07 00:20:41 +01004800 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004801 rep->analysers = 0;
4802 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004803 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004804 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004805 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004806
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004807 if (!(s->flags & SN_ERR_MASK))
4808 s->flags |= SN_ERR_SRVTO;
4809 if (!(s->flags & SN_FINST_MASK))
4810 s->flags |= SN_FINST_H;
4811 return 0;
4812 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004813
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004814 /* close from server */
4815 else if (rep->flags & BF_SHUTR) {
4816 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004817 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004818
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004819 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004820 if (target_srv(&s->target)) {
4821 target_srv(&s->target)->counters.failed_resp++;
4822 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004823 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004824
Willy Tarreau90deb182010-01-07 00:20:41 +01004825 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004826 rep->analysers = 0;
4827 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004828 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004829 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004830 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004831
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004832 if (!(s->flags & SN_ERR_MASK))
4833 s->flags |= SN_ERR_SRVCL;
4834 if (!(s->flags & SN_FINST_MASK))
4835 s->flags |= SN_FINST_H;
4836 return 0;
4837 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004838
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004839 /* write error to client (we don't send any message then) */
4840 else if (rep->flags & BF_WRITE_ERROR) {
4841 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004842 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004843
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004844 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004845 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004846 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004847
4848 if (!(s->flags & SN_ERR_MASK))
4849 s->flags |= SN_ERR_CLICL;
4850 if (!(s->flags & SN_FINST_MASK))
4851 s->flags |= SN_FINST_H;
4852
4853 /* process_session() will take care of the error */
4854 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004855 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004856
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004857 buffer_dont_close(rep);
4858 return 0;
4859 }
4860
4861 /* More interesting part now : we know that we have a complete
4862 * response which at least looks like HTTP. We have an indicator
4863 * of each header's length, so we can parse them quickly.
4864 */
4865
4866 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004867 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004868
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004869 /*
4870 * 1: get the status code
4871 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004872 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004873 if (n < 1 || n > 5)
4874 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004875 /* when the client triggers a 4xx from the server, it's most often due
4876 * to a missing object or permission. These events should be tracked
4877 * because if they happen often, it may indicate a brute force or a
4878 * vulnerability scan.
4879 */
4880 if (n == 4)
4881 session_inc_http_err_ctr(s);
4882
Willy Tarreau827aee92011-03-10 16:55:02 +01004883 if (target_srv(&s->target))
4884 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004885
Willy Tarreau5b154472009-12-21 20:11:07 +01004886 /* check if the response is HTTP/1.1 or above */
4887 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004888 ((msg->sol[5] > '1') ||
4889 ((msg->sol[5] == '1') &&
4890 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004891 txn->flags |= TX_RES_VER_11;
4892
4893 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004894 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 +01004895
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004896 /* transfer length unknown*/
4897 txn->flags &= ~TX_RES_XFER_LEN;
4898
Willy Tarreau962c3f42010-01-10 00:15:35 +01004899 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004900
Willy Tarreau39650402010-03-15 19:44:39 +01004901 /* Adjust server's health based on status code. Note: status codes 501
4902 * and 505 are triggered on demand by client request, so we must not
4903 * count them as server failures.
4904 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004905 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004906 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004907 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004908 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004909 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004910 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004911
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004912 /*
4913 * 2: check for cacheability.
4914 */
4915
4916 switch (txn->status) {
4917 case 200:
4918 case 203:
4919 case 206:
4920 case 300:
4921 case 301:
4922 case 410:
4923 /* RFC2616 @13.4:
4924 * "A response received with a status code of
4925 * 200, 203, 206, 300, 301 or 410 MAY be stored
4926 * by a cache (...) unless a cache-control
4927 * directive prohibits caching."
4928 *
4929 * RFC2616 @9.5: POST method :
4930 * "Responses to this method are not cacheable,
4931 * unless the response includes appropriate
4932 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004933 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004934 if (likely(txn->meth != HTTP_METH_POST) &&
4935 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4936 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4937 break;
4938 default:
4939 break;
4940 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004941
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004942 /*
4943 * 3: we may need to capture headers
4944 */
4945 s->logs.logwait &= ~LW_RESP;
4946 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004947 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004948 txn->rsp.cap, s->fe->rsp_cap);
4949
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004950 /* 4: determine the transfer-length.
4951 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4952 * the presence of a message-body in a RESPONSE and its transfer length
4953 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004954 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004955 * All responses to the HEAD request method MUST NOT include a
4956 * message-body, even though the presence of entity-header fields
4957 * might lead one to believe they do. All 1xx (informational), 204
4958 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4959 * message-body. All other responses do include a message-body,
4960 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004961 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004962 * 1. Any response which "MUST NOT" include a message-body (such as the
4963 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4964 * always terminated by the first empty line after the header fields,
4965 * regardless of the entity-header fields present in the message.
4966 *
4967 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4968 * the "chunked" transfer-coding (Section 6.2) is used, the
4969 * transfer-length is defined by the use of this transfer-coding.
4970 * If a Transfer-Encoding header field is present and the "chunked"
4971 * transfer-coding is not present, the transfer-length is defined by
4972 * the sender closing the connection.
4973 *
4974 * 3. If a Content-Length header field is present, its decimal value in
4975 * OCTETs represents both the entity-length and the transfer-length.
4976 * If a message is received with both a Transfer-Encoding header
4977 * field and a Content-Length header field, the latter MUST be ignored.
4978 *
4979 * 4. If the message uses the media type "multipart/byteranges", and
4980 * the transfer-length is not otherwise specified, then this self-
4981 * delimiting media type defines the transfer-length. This media
4982 * type MUST NOT be used unless the sender knows that the recipient
4983 * can parse it; the presence in a request of a Range header with
4984 * multiple byte-range specifiers from a 1.1 client implies that the
4985 * client can parse multipart/byteranges responses.
4986 *
4987 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004988 */
4989
4990 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004991 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004992 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004993 * FIXME: should we parse anyway and return an error on chunked encoding ?
4994 */
4995 if (txn->meth == HTTP_METH_HEAD ||
4996 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004997 txn->status == 204 || txn->status == 304) {
4998 txn->flags |= TX_RES_XFER_LEN;
4999 goto skip_content_length;
5000 }
5001
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005002 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005003 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01005004 while ((txn->flags & TX_RES_VER_11) &&
5005 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005006 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
5007 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5008 else if (txn->flags & TX_RES_TE_CHNK) {
5009 /* bad transfer-encoding (chunked followed by something else) */
5010 use_close_only = 1;
5011 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5012 break;
5013 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005014 }
5015
5016 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5017 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005018 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005019 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
5020 signed long long cl;
5021
5022 if (!ctx.vlen)
5023 goto hdr_response_bad;
5024
5025 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
5026 goto hdr_response_bad; /* parse failure */
5027
5028 if (cl < 0)
5029 goto hdr_response_bad;
5030
Willy Tarreau124d9912011-03-01 20:30:48 +01005031 if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl))
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005032 goto hdr_response_bad; /* already specified, was different */
5033
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005034 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005035 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005036 }
5037
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005038 /* FIXME: we should also implement the multipart/byterange method.
5039 * For now on, we resort to close mode in this case (unknown length).
5040 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005041skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005042
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005043 /* end of job, return OK */
5044 rep->analysers &= ~an_bit;
5045 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01005046 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005047 return 1;
5048}
5049
5050/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005051 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5052 * and updates t->rep->analysers. It might make sense to explode it into several
5053 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005054 */
5055int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
5056{
5057 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005058 struct http_msg *msg = &txn->rsp;
5059 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005060 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005061
5062 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
5063 now_ms, __FUNCTION__,
5064 t,
5065 rep,
5066 rep->rex, rep->wex,
5067 rep->flags,
5068 rep->l,
5069 rep->analysers);
5070
Willy Tarreau655dce92009-11-08 13:10:58 +01005071 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005072 return 0;
5073
5074 rep->analysers &= ~an_bit;
5075 rep->analyse_exp = TICK_ETERNITY;
5076
Willy Tarreau5b154472009-12-21 20:11:07 +01005077 /* Now we have to check if we need to modify the Connection header.
5078 * This is more difficult on the response than it is on the request,
5079 * because we can have two different HTTP versions and we don't know
5080 * how the client will interprete a response. For instance, let's say
5081 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5082 * HTTP/1.1 response without any header. Maybe it will bound itself to
5083 * HTTP/1.0 because it only knows about it, and will consider the lack
5084 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5085 * the lack of header as a keep-alive. Thus we will use two flags
5086 * indicating how a request MAY be understood by the client. In case
5087 * of multiple possibilities, we'll fix the header to be explicit. If
5088 * ambiguous cases such as both close and keepalive are seen, then we
5089 * will fall back to explicit close. Note that we won't take risks with
5090 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005091 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005092 */
5093
Willy Tarreaudc008c52010-02-01 16:20:08 +01005094 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5095 txn->status == 101)) {
5096 /* Either we've established an explicit tunnel, or we're
5097 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005098 * to understand the next protocols. We have to switch to tunnel
5099 * mode, so that we transfer the request and responses then let
5100 * this protocol pass unmodified. When we later implement specific
5101 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005102 * header which contains information about that protocol for
5103 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005104 */
5105 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5106 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005107 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5108 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5109 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005110 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005111
Willy Tarreau60466522010-01-18 19:08:45 +01005112 /* on unknown transfer length, we must close */
5113 if (!(txn->flags & TX_RES_XFER_LEN) &&
5114 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5115 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005116
Willy Tarreau60466522010-01-18 19:08:45 +01005117 /* now adjust header transformations depending on current state */
5118 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5119 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5120 to_del |= 2; /* remove "keep-alive" on any response */
5121 if (!(txn->flags & TX_RES_VER_11))
5122 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005123 }
Willy Tarreau60466522010-01-18 19:08:45 +01005124 else { /* SCL / KAL */
5125 to_del |= 1; /* remove "close" on any response */
5126 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
5127 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005128 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005129
Willy Tarreau60466522010-01-18 19:08:45 +01005130 /* Parse and remove some headers from the connection header */
5131 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005132
Willy Tarreau60466522010-01-18 19:08:45 +01005133 /* Some keep-alive responses are converted to Server-close if
5134 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005135 */
Willy Tarreau60466522010-01-18 19:08:45 +01005136 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5137 if ((txn->flags & TX_HDR_CONN_CLO) ||
5138 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
5139 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005140 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005141 }
5142
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005143 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005144 /*
5145 * 3: we will have to evaluate the filters.
5146 * As opposed to version 1.2, now they will be evaluated in the
5147 * filters order and not in the header order. This means that
5148 * each filter has to be validated among all headers.
5149 *
5150 * Filters are tried with ->be first, then with ->fe if it is
5151 * different from ->be.
5152 */
5153
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005154 cur_proxy = t->be;
5155 while (1) {
5156 struct proxy *rule_set = cur_proxy;
5157
5158 /* try headers filters */
5159 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005160 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005161 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005162 if (target_srv(&t->target)) {
5163 target_srv(&t->target)->counters.failed_resp++;
5164 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005165 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005166 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005167 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005168 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005169 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005170 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01005171 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005172 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005173 if (!(t->flags & SN_ERR_MASK))
5174 t->flags |= SN_ERR_PRXCOND;
5175 if (!(t->flags & SN_FINST_MASK))
5176 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005177 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005178 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005179 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005180
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005181 /* has the response been denied ? */
5182 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005183 if (target_srv(&t->target))
5184 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005185
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005186 t->be->be_counters.denied_resp++;
5187 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005188 if (t->listener->counters)
5189 t->listener->counters->denied_resp++;
5190
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005191 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005192 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005193
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005194 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005195 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005196 if (txn->status < 200)
5197 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005198 if (wl->cond) {
5199 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5200 ret = acl_pass(ret);
5201 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5202 ret = !ret;
5203 if (!ret)
5204 continue;
5205 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005206 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005207 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005208 }
5209
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005210 /* check whether we're already working on the frontend */
5211 if (cur_proxy == t->fe)
5212 break;
5213 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005214 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005215
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005216 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005217 * We may be facing a 100-continue response, in which case this
5218 * is not the right response, and we're waiting for the next one.
5219 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005220 * next one.
5221 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005222 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005223 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005224 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005225 msg->msg_state = HTTP_MSG_RPBEFORE;
5226 txn->status = 0;
5227 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5228 return 1;
5229 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005230 else if (unlikely(txn->status < 200))
5231 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005232
5233 /* we don't have any 1xx status code now */
5234
5235 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005236 * 4: check for server cookie.
5237 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005238 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5239 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005240 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005241
Willy Tarreaubaaee002006-06-26 02:48:02 +02005242
Willy Tarreaua15645d2007-03-18 16:22:39 +01005243 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005244 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005245 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005246 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005247 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005248
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005249 /*
5250 * 6: add server cookie in the response if needed
5251 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005252 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005253 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005254 (!(t->flags & SN_DIRECT) ||
5255 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5256 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5257 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5258 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005259 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5260 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005261 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005262 /* the server is known, it's not the one the client requested, or the
5263 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005264 * insert a set-cookie here, except if we want to insert only on POST
5265 * requests and this one isn't. Note that servers which don't have cookies
5266 * (eg: some backup servers) will return a full cookie removal request.
5267 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005268 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005269 len = sprintf(trash,
5270 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5271 t->be->cookie_name);
5272 }
5273 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005274 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005275
5276 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5277 /* emit last_date, which is mandatory */
5278 trash[len++] = COOKIE_DELIM_DATE;
5279 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5280 if (t->be->cookie_maxlife) {
5281 /* emit first_date, which is either the original one or
5282 * the current date.
5283 */
5284 trash[len++] = COOKIE_DELIM_DATE;
5285 s30tob64(txn->cookie_first_date ?
5286 txn->cookie_first_date >> 2 :
5287 (date.tv_sec+3) >> 2, trash + len);
5288 len += 5;
5289 }
5290 }
5291 len += sprintf(trash + len, "; path=/");
5292 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005293
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005294 if (t->be->cookie_domain)
5295 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005296
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005297 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005298 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005299 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005300
Willy Tarreauf1348312010-10-07 15:54:11 +02005301 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005302 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005303 /* the server did not change, only the date was updated */
5304 txn->flags |= TX_SCK_UPDATED;
5305 else
5306 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005307
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005308 /* Here, we will tell an eventual cache on the client side that we don't
5309 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5310 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5311 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5312 */
5313 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005314
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005315 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5316
5317 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005318 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005319 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005320 }
5321 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005322
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005323 /*
5324 * 7: check if result will be cacheable with a cookie.
5325 * We'll block the response if security checks have caught
5326 * nasty things such as a cacheable cookie.
5327 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005328 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5329 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005330 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005331
5332 /* we're in presence of a cacheable response containing
5333 * a set-cookie header. We'll block it as requested by
5334 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005335 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005336 if (target_srv(&t->target))
5337 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005338
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005339 t->be->be_counters.denied_resp++;
5340 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005341 if (t->listener->counters)
5342 t->listener->counters->denied_resp++;
5343
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005344 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005345 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005346 send_log(t->be, LOG_ALERT,
5347 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005348 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005349 goto return_srv_prx_502;
5350 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005351
5352 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005353 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005354 */
Willy Tarreau60466522010-01-18 19:08:45 +01005355 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5356 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5357 unsigned int want_flags = 0;
5358
5359 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5360 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5361 /* we want a keep-alive response here. Keep-alive header
5362 * required if either side is not 1.1.
5363 */
5364 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5365 want_flags |= TX_CON_KAL_SET;
5366 }
5367 else {
5368 /* we want a close response here. Close header required if
5369 * the server is 1.1, regardless of the client.
5370 */
5371 if (txn->flags & TX_RES_VER_11)
5372 want_flags |= TX_CON_CLO_SET;
5373 }
5374
5375 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5376 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005377 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005378
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005379 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005380 if ((txn->flags & TX_RES_XFER_LEN) ||
5381 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005382 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005383
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005384 /*************************************************************
5385 * OK, that's finished for the headers. We have done what we *
5386 * could. Let's switch to the DATA state. *
5387 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005388
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005389 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005390
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005391 /* if the user wants to log as soon as possible, without counting
5392 * bytes from the server, then this is the right moment. We have
5393 * to temporarily assign bytes_out to log what we currently have.
5394 */
5395 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5396 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5397 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005398 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005399 t->logs.bytes_out = 0;
5400 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005401
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005402 /* Note: we must not try to cheat by jumping directly to DATA,
5403 * otherwise we would not let the client side wake up.
5404 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005405
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005406 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005407 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005408 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005409}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005410
Willy Tarreaud98cf932009-12-27 22:54:55 +01005411/* This function is an analyser which forwards response body (including chunk
5412 * sizes if any). It is called as soon as we must forward, even if we forward
5413 * zero byte. The only situation where it must not be called is when we're in
5414 * tunnel mode and we want to forward till the close. It's used both to forward
5415 * remaining data and to resync after end of body. It expects the msg_state to
5416 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5417 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005418 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005419 * bytes of pending data + the headers if not already done (between som and sov).
5420 * It eventually adjusts som to match sov after the data in between have been sent.
5421 */
5422int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5423{
5424 struct http_txn *txn = &s->txn;
5425 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005426 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005427
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005428 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5429 return 0;
5430
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005431 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005432 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005433 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005434 /* Output closed while we were sending data. We must abort and
5435 * wake the other side up.
5436 */
5437 msg->msg_state = HTTP_MSG_ERROR;
5438 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005439 return 1;
5440 }
5441
Willy Tarreau4fe41902010-06-07 22:27:41 +02005442 /* in most states, we should abort in case of early close */
5443 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005444
Willy Tarreaud98cf932009-12-27 22:54:55 +01005445 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5446 /* we have msg->col and msg->sov which both point to the first
5447 * byte of message body. msg->som still points to the beginning
5448 * of the message. We must save the body in req->lr because it
5449 * survives buffer re-alignments.
5450 */
5451 res->lr = res->data + msg->sov;
5452 if (txn->flags & TX_RES_TE_CHNK)
5453 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5454 else {
5455 msg->msg_state = HTTP_MSG_DATA;
5456 }
5457 }
5458
Willy Tarreaud98cf932009-12-27 22:54:55 +01005459 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005460 int bytes;
5461
Willy Tarreau610ecce2010-01-04 21:15:02 +01005462 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005463 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005464 bytes = msg->sov - msg->som;
5465 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005466 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005467 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5468 bytes += res->size;
5469 msg->chunk_len += (unsigned int)bytes;
5470 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005471 }
5472
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005473
Willy Tarreaucaabe412010-01-03 23:08:28 +01005474 if (msg->msg_state == HTTP_MSG_DATA) {
5475 /* must still forward */
5476 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005477 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005478
5479 /* nothing left to forward */
5480 if (txn->flags & TX_RES_TE_CHNK)
5481 msg->msg_state = HTTP_MSG_DATA_CRLF;
5482 else
5483 msg->msg_state = HTTP_MSG_DONE;
5484 }
5485 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005486 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005487 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5488 */
5489 int ret = http_parse_chunk_size(res, msg);
5490
5491 if (!ret)
5492 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005493 else if (ret < 0) {
5494 if (msg->err_pos >= 0)
5495 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005496 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005497 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005498 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreau57f5c122010-12-02 00:37:14 +01005499 /* Don't set a PUSH at the end of that chunk if it's not the last one */
5500 if (msg->msg_state == HTTP_MSG_DATA)
5501 res->flags |= BF_EXPECT_MORE;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005502 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005503 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5504 /* we want the CRLF after the data */
5505 int ret;
5506
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005507 res->lr = res->w + res->send_max;
5508 if (res->lr >= res->data + res->size)
5509 res->lr -= res->size;
5510
Willy Tarreaud98cf932009-12-27 22:54:55 +01005511 ret = http_skip_chunk_crlf(res, msg);
5512
5513 if (!ret)
5514 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005515 else if (ret < 0) {
5516 if (msg->err_pos >= 0)
5517 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005518 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005519 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005520 /* we're in MSG_CHUNK_SIZE now */
5521 }
5522 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5523 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005524
Willy Tarreaud98cf932009-12-27 22:54:55 +01005525 if (ret == 0)
5526 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005527 else if (ret < 0) {
5528 if (msg->err_pos >= 0)
5529 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005530 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005531 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005532 /* we're in HTTP_MSG_DONE now */
5533 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005534 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005535 int old_state = msg->msg_state;
5536
Willy Tarreau610ecce2010-01-04 21:15:02 +01005537 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005538 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005539 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5540 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5541 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005542 if (http_resync_states(s)) {
5543 http_silent_debug(__LINE__, s);
5544 /* some state changes occurred, maybe the analyser
5545 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005546 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005547 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5548 if (res->flags & BF_SHUTW) {
5549 /* response errors are most likely due to
5550 * the client aborting the transfer.
5551 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005552 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005553 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005554 if (msg->err_pos >= 0)
5555 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005556 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005557 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005558 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005559 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005560 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005561 }
5562 }
5563
Willy Tarreaud98cf932009-12-27 22:54:55 +01005564 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005565 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005566 if (res->flags & BF_SHUTR) {
5567 if (!(s->flags & SN_ERR_MASK))
5568 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005569 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005570 if (target_srv(&s->target))
5571 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005572 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005573 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005574
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005575 if (res->flags & BF_SHUTW)
5576 goto aborted_xfer;
5577
Willy Tarreau40dba092010-03-04 18:14:51 +01005578 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579 if (!s->req->analysers)
5580 goto return_bad_res;
5581
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005582 /* forward any pending data */
5583 bytes = msg->sov - msg->som;
5584 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005585 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005586 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5587 bytes += res->size;
5588 msg->chunk_len += (unsigned int)bytes;
5589 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005590 }
5591
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005592 /* When TE: chunked is used, we need to get there again to parse remaining
5593 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5594 * Similarly, with keep-alive on the client side, we don't want to forward a
5595 * close.
5596 */
5597 if ((txn->flags & TX_RES_TE_CHNK) ||
5598 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5599 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5600 buffer_dont_close(res);
5601
Willy Tarreaud98cf932009-12-27 22:54:55 +01005602 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005603 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005604 return 0;
5605
Willy Tarreau40dba092010-03-04 18:14:51 +01005606 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005607 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005608 if (target_srv(&s->target))
5609 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005610
5611 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005612 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005613 /* don't send any error message as we're in the body */
5614 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005615 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005616 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005617 if (target_srv(&s->target))
5618 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005619
5620 if (!(s->flags & SN_ERR_MASK))
5621 s->flags |= SN_ERR_PRXCOND;
5622 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005623 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005624 return 0;
5625
5626 aborted_xfer:
5627 txn->rsp.msg_state = HTTP_MSG_ERROR;
5628 /* don't send any error message as we're in the body */
5629 stream_int_retnclose(res->cons, NULL);
5630 res->analysers = 0;
5631 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5632
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005633 s->fe->fe_counters.cli_aborts++;
5634 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005635 if (target_srv(&s->target))
5636 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005637
5638 if (!(s->flags & SN_ERR_MASK))
5639 s->flags |= SN_ERR_CLICL;
5640 if (!(s->flags & SN_FINST_MASK))
5641 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005642 return 0;
5643}
5644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005645/* Iterate the same filter through all request headers.
5646 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005647 * Since it can manage the switch to another backend, it updates the per-proxy
5648 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005649 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005650int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005651{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005652 char term;
5653 char *cur_ptr, *cur_end, *cur_next;
5654 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005655 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005656 struct hdr_idx_elem *cur_hdr;
5657 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005658
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005659 last_hdr = 0;
5660
Willy Tarreau962c3f42010-01-10 00:15:35 +01005661 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005662 old_idx = 0;
5663
5664 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005665 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005666 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005667 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005668 (exp->action == ACT_ALLOW ||
5669 exp->action == ACT_DENY ||
5670 exp->action == ACT_TARPIT))
5671 return 0;
5672
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005673 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005674 if (!cur_idx)
5675 break;
5676
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005677 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005678 cur_ptr = cur_next;
5679 cur_end = cur_ptr + cur_hdr->len;
5680 cur_next = cur_end + cur_hdr->cr + 1;
5681
5682 /* Now we have one header between cur_ptr and cur_end,
5683 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005684 */
5685
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005686 /* The annoying part is that pattern matching needs
5687 * that we modify the contents to null-terminate all
5688 * strings before testing them.
5689 */
5690
5691 term = *cur_end;
5692 *cur_end = '\0';
5693
5694 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5695 switch (exp->action) {
5696 case ACT_SETBE:
5697 /* It is not possible to jump a second time.
5698 * FIXME: should we return an HTTP/500 here so that
5699 * the admin knows there's a problem ?
5700 */
5701 if (t->be != t->fe)
5702 break;
5703
5704 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005705 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005706 last_hdr = 1;
5707 break;
5708
5709 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005710 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005711 last_hdr = 1;
5712 break;
5713
5714 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005715 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005716 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005717
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005718 t->fe->fe_counters.denied_req++;
5719 if (t->fe != t->be)
5720 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005721 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005722 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005723
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005724 break;
5725
5726 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005727 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005728 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005729
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005730 t->fe->fe_counters.denied_req++;
5731 if (t->fe != t->be)
5732 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005733 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005734 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005735
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005736 break;
5737
5738 case ACT_REPLACE:
5739 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5740 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5741 /* FIXME: if the user adds a newline in the replacement, the
5742 * index will not be recalculated for now, and the new line
5743 * will not be counted as a new header.
5744 */
5745
5746 cur_end += delta;
5747 cur_next += delta;
5748 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005749 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005750 break;
5751
5752 case ACT_REMOVE:
5753 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5754 cur_next += delta;
5755
Willy Tarreaufa355d42009-11-29 18:12:29 +01005756 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005757 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5758 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005759 cur_hdr->len = 0;
5760 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005761 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005762 break;
5763
5764 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005765 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005766 if (cur_end)
5767 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005768
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005769 /* keep the link from this header to next one in case of later
5770 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005771 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005772 old_idx = cur_idx;
5773 }
5774 return 0;
5775}
5776
5777
5778/* Apply the filter to the request line.
5779 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5780 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005781 * Since it can manage the switch to another backend, it updates the per-proxy
5782 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005783 */
5784int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5785{
5786 char term;
5787 char *cur_ptr, *cur_end;
5788 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005789 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005790 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005791
Willy Tarreau58f10d72006-12-04 02:26:12 +01005792
Willy Tarreau3d300592007-03-18 18:34:41 +01005793 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005794 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005795 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005796 (exp->action == ACT_ALLOW ||
5797 exp->action == ACT_DENY ||
5798 exp->action == ACT_TARPIT))
5799 return 0;
5800 else if (exp->action == ACT_REMOVE)
5801 return 0;
5802
5803 done = 0;
5804
Willy Tarreau962c3f42010-01-10 00:15:35 +01005805 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005806 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005807
5808 /* Now we have the request line between cur_ptr and cur_end */
5809
5810 /* The annoying part is that pattern matching needs
5811 * that we modify the contents to null-terminate all
5812 * strings before testing them.
5813 */
5814
5815 term = *cur_end;
5816 *cur_end = '\0';
5817
5818 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5819 switch (exp->action) {
5820 case ACT_SETBE:
5821 /* It is not possible to jump a second time.
5822 * FIXME: should we return an HTTP/500 here so that
5823 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005824 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005825 if (t->be != t->fe)
5826 break;
5827
5828 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005829 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005830 done = 1;
5831 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005832
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005833 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005834 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005835 done = 1;
5836 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005837
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005838 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005839 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005840
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005841 t->fe->fe_counters.denied_req++;
5842 if (t->fe != t->be)
5843 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005844 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005845 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005846
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005847 done = 1;
5848 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005849
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005850 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005851 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005852
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005853 t->fe->fe_counters.denied_req++;
5854 if (t->fe != t->be)
5855 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005856 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005857 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005858
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005859 done = 1;
5860 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005861
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005862 case ACT_REPLACE:
5863 *cur_end = term; /* restore the string terminator */
5864 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5865 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5866 /* FIXME: if the user adds a newline in the replacement, the
5867 * index will not be recalculated for now, and the new line
5868 * will not be counted as a new header.
5869 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005870
Willy Tarreaufa355d42009-11-29 18:12:29 +01005871 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005872 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005873 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005874 HTTP_MSG_RQMETH,
5875 cur_ptr, cur_end + 1,
5876 NULL, NULL);
5877 if (unlikely(!cur_end))
5878 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005879
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005880 /* we have a full request and we know that we have either a CR
5881 * or an LF at <ptr>.
5882 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005883 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5884 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005885 /* there is no point trying this regex on headers */
5886 return 1;
5887 }
5888 }
5889 *cur_end = term; /* restore the string terminator */
5890 return done;
5891}
Willy Tarreau97de6242006-12-27 17:18:38 +01005892
Willy Tarreau58f10d72006-12-04 02:26:12 +01005893
Willy Tarreau58f10d72006-12-04 02:26:12 +01005894
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005895/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005896 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005897 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005898 * unparsable request. Since it can manage the switch to another backend, it
5899 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005900 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005901int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005902{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005903 struct http_txn *txn = &s->txn;
5904 struct hdr_exp *exp;
5905
5906 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005907 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005908
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005909 /*
5910 * The interleaving of transformations and verdicts
5911 * makes it difficult to decide to continue or stop
5912 * the evaluation.
5913 */
5914
Willy Tarreau6c123b12010-01-28 20:22:06 +01005915 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5916 break;
5917
Willy Tarreau3d300592007-03-18 18:34:41 +01005918 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005919 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005920 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005921 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005922
5923 /* if this filter had a condition, evaluate it now and skip to
5924 * next filter if the condition does not match.
5925 */
5926 if (exp->cond) {
5927 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5928 ret = acl_pass(ret);
5929 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5930 ret = !ret;
5931
5932 if (!ret)
5933 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005934 }
5935
5936 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005937 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005938 if (unlikely(ret < 0))
5939 return -1;
5940
5941 if (likely(ret == 0)) {
5942 /* The filter did not match the request, it can be
5943 * iterated through all headers.
5944 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005945 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005946 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005947 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005948 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005949}
5950
5951
Willy Tarreaua15645d2007-03-18 16:22:39 +01005952
Willy Tarreau58f10d72006-12-04 02:26:12 +01005953/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005954 * Try to retrieve the server associated to the appsession.
5955 * If the server is found, it's assigned to the session.
5956 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005957void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005958 struct http_txn *txn = &t->txn;
5959 appsess *asession = NULL;
5960 char *sessid_temp = NULL;
5961
Cyril Bontéb21570a2009-11-29 20:04:48 +01005962 if (len > t->be->appsession_len) {
5963 len = t->be->appsession_len;
5964 }
5965
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005966 if (t->be->options2 & PR_O2_AS_REQL) {
5967 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005968 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005969 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005970 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005971 }
5972
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005973 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005974 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5975 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5976 return;
5977 }
5978
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005979 memcpy(txn->sessid, buf, len);
5980 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005981 }
5982
5983 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5984 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5985 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5986 return;
5987 }
5988
Cyril Bontéb21570a2009-11-29 20:04:48 +01005989 memcpy(sessid_temp, buf, len);
5990 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005991
5992 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5993 /* free previously allocated memory */
5994 pool_free2(apools.sessid, sessid_temp);
5995
5996 if (asession != NULL) {
5997 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5998 if (!(t->be->options2 & PR_O2_AS_REQL))
5999 asession->request_count++;
6000
6001 if (asession->serverid != NULL) {
6002 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006003
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006004 while (srv) {
6005 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006006 if ((srv->state & SRV_RUNNING) ||
6007 (t->be->options & PR_O_PERSIST) ||
6008 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006009 /* we found the server and it's usable */
6010 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006011 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006012 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006013 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01006014
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006015 break;
6016 } else {
6017 txn->flags &= ~TX_CK_MASK;
6018 txn->flags |= TX_CK_DOWN;
6019 }
6020 }
6021 srv = srv->next;
6022 }
6023 }
6024 }
6025}
6026
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006027/* Find the end of a cookie value contained between <s> and <e>. It works the
6028 * same way as with headers above except that the semi-colon also ends a token.
6029 * See RFC2965 for more information. Note that it requires a valid header to
6030 * return a valid result.
6031 */
6032char *find_cookie_value_end(char *s, const char *e)
6033{
6034 int quoted, qdpair;
6035
6036 quoted = qdpair = 0;
6037 for (; s < e; s++) {
6038 if (qdpair) qdpair = 0;
6039 else if (quoted) {
6040 if (*s == '\\') qdpair = 1;
6041 else if (*s == '"') quoted = 0;
6042 }
6043 else if (*s == '"') quoted = 1;
6044 else if (*s == ',' || *s == ';') return s;
6045 }
6046 return s;
6047}
6048
6049/* Delete a value in a header between delimiters <from> and <next> in buffer
6050 * <buf>. The number of characters displaced is returned, and the pointer to
6051 * the first delimiter is updated if required. The function tries as much as
6052 * possible to respect the following principles :
6053 * - replace <from> delimiter by the <next> one unless <from> points to a
6054 * colon, in which case <next> is simply removed
6055 * - set exactly one space character after the new first delimiter, unless
6056 * there are not enough characters in the block being moved to do so.
6057 * - remove unneeded spaces before the previous delimiter and after the new
6058 * one.
6059 *
6060 * It is the caller's responsibility to ensure that :
6061 * - <from> points to a valid delimiter or the colon ;
6062 * - <next> points to a valid delimiter or the final CR/LF ;
6063 * - there are non-space chars before <from> ;
6064 * - there is a CR/LF at or after <next>.
6065 */
6066int del_hdr_value(struct buffer *buf, char **from, char *next)
6067{
6068 char *prev = *from;
6069
6070 if (*prev == ':') {
6071 /* We're removing the first value, preserve the colon and add a
6072 * space if possible.
6073 */
6074 if (!http_is_crlf[(unsigned char)*next])
6075 next++;
6076 prev++;
6077 if (prev < next)
6078 *prev++ = ' ';
6079
6080 while (http_is_spht[(unsigned char)*next])
6081 next++;
6082 } else {
6083 /* Remove useless spaces before the old delimiter. */
6084 while (http_is_spht[(unsigned char)*(prev-1)])
6085 prev--;
6086 *from = prev;
6087
6088 /* copy the delimiter and if possible a space if we're
6089 * not at the end of the line.
6090 */
6091 if (!http_is_crlf[(unsigned char)*next]) {
6092 *prev++ = *next++;
6093 if (prev + 1 < next)
6094 *prev++ = ' ';
6095 while (http_is_spht[(unsigned char)*next])
6096 next++;
6097 }
6098 }
6099 return buffer_replace2(buf, prev, next, NULL, 0);
6100}
6101
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006102/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006103 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006104 * desirable to call it only when needed. This code is quite complex because
6105 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6106 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006107 */
6108void manage_client_side_cookies(struct session *t, struct buffer *req)
6109{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006110 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006111 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006112 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006113 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6114 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006115
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006116 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006117 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006118 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006119
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006120 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006121 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006122 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006123
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006124 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006125 hdr_beg = hdr_next;
6126 hdr_end = hdr_beg + cur_hdr->len;
6127 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006128
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006129 /* We have one full header between hdr_beg and hdr_end, and the
6130 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006131 * "Cookie:" headers.
6132 */
6133
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006134 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006135 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006136 old_idx = cur_idx;
6137 continue;
6138 }
6139
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006140 del_from = NULL; /* nothing to be deleted */
6141 preserve_hdr = 0; /* assume we may kill the whole header */
6142
Willy Tarreau58f10d72006-12-04 02:26:12 +01006143 /* Now look for cookies. Conforming to RFC2109, we have to support
6144 * attributes whose name begin with a '$', and associate them with
6145 * the right cookie, if we want to delete this cookie.
6146 * So there are 3 cases for each cookie read :
6147 * 1) it's a special attribute, beginning with a '$' : ignore it.
6148 * 2) it's a server id cookie that we *MAY* want to delete : save
6149 * some pointers on it (last semi-colon, beginning of cookie...)
6150 * 3) it's an application cookie : we *MAY* have to delete a previous
6151 * "special" cookie.
6152 * At the end of loop, if a "special" cookie remains, we may have to
6153 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006154 * *MUST* delete it.
6155 *
6156 * Note: RFC2965 is unclear about the processing of spaces around
6157 * the equal sign in the ATTR=VALUE form. A careful inspection of
6158 * the RFC explicitly allows spaces before it, and not within the
6159 * tokens (attrs or values). An inspection of RFC2109 allows that
6160 * too but section 10.1.3 lets one think that spaces may be allowed
6161 * after the equal sign too, resulting in some (rare) buggy
6162 * implementations trying to do that. So let's do what servers do.
6163 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6164 * allowed quoted strings in values, with any possible character
6165 * after a backslash, including control chars and delimitors, which
6166 * causes parsing to become ambiguous. Browsers also allow spaces
6167 * within values even without quotes.
6168 *
6169 * We have to keep multiple pointers in order to support cookie
6170 * removal at the beginning, middle or end of header without
6171 * corrupting the header. All of these headers are valid :
6172 *
6173 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6174 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6175 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6176 * | | | | | | | | |
6177 * | | | | | | | | hdr_end <--+
6178 * | | | | | | | +--> next
6179 * | | | | | | +----> val_end
6180 * | | | | | +-----------> val_beg
6181 * | | | | +--------------> equal
6182 * | | | +----------------> att_end
6183 * | | +---------------------> att_beg
6184 * | +--------------------------> prev
6185 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006186 */
6187
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006188 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6189 /* Iterate through all cookies on this line */
6190
6191 /* find att_beg */
6192 att_beg = prev + 1;
6193 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6194 att_beg++;
6195
6196 /* find att_end : this is the first character after the last non
6197 * space before the equal. It may be equal to hdr_end.
6198 */
6199 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006200
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006201 while (equal < hdr_end) {
6202 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006203 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006204 if (http_is_spht[(unsigned char)*equal++])
6205 continue;
6206 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006207 }
6208
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006209 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6210 * is between <att_beg> and <equal>, both may be identical.
6211 */
6212
6213 /* look for end of cookie if there is an equal sign */
6214 if (equal < hdr_end && *equal == '=') {
6215 /* look for the beginning of the value */
6216 val_beg = equal + 1;
6217 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6218 val_beg++;
6219
6220 /* find the end of the value, respecting quotes */
6221 next = find_cookie_value_end(val_beg, hdr_end);
6222
6223 /* make val_end point to the first white space or delimitor after the value */
6224 val_end = next;
6225 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6226 val_end--;
6227 } else {
6228 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006229 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006230
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006231 /* We have nothing to do with attributes beginning with '$'. However,
6232 * they will automatically be removed if a header before them is removed,
6233 * since they're supposed to be linked together.
6234 */
6235 if (*att_beg == '$')
6236 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006237
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006238 /* Ignore cookies with no equal sign */
6239 if (equal == next) {
6240 /* This is not our cookie, so we must preserve it. But if we already
6241 * scheduled another cookie for removal, we cannot remove the
6242 * complete header, but we can remove the previous block itself.
6243 */
6244 preserve_hdr = 1;
6245 if (del_from != NULL) {
6246 int delta = del_hdr_value(req, &del_from, prev);
6247 val_end += delta;
6248 next += delta;
6249 hdr_end += delta;
6250 hdr_next += delta;
6251 cur_hdr->len += delta;
6252 http_msg_move_end(&txn->req, delta);
6253 prev = del_from;
6254 del_from = NULL;
6255 }
6256 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006257 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006258
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006259 /* if there are spaces around the equal sign, we need to
6260 * strip them otherwise we'll get trouble for cookie captures,
6261 * or even for rewrites. Since this happens extremely rarely,
6262 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006263 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006264 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6265 int stripped_before = 0;
6266 int stripped_after = 0;
6267
6268 if (att_end != equal) {
6269 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6270 equal += stripped_before;
6271 val_beg += stripped_before;
6272 }
6273
6274 if (val_beg > equal + 1) {
6275 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6276 val_beg += stripped_after;
6277 stripped_before += stripped_after;
6278 }
6279
6280 val_end += stripped_before;
6281 next += stripped_before;
6282 hdr_end += stripped_before;
6283 hdr_next += stripped_before;
6284 cur_hdr->len += stripped_before;
6285 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006286 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006287 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006288
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006289 /* First, let's see if we want to capture this cookie. We check
6290 * that we don't already have a client side cookie, because we
6291 * can only capture one. Also as an optimisation, we ignore
6292 * cookies shorter than the declared name.
6293 */
6294 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6295 (val_end - att_beg >= t->fe->capture_namelen) &&
6296 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6297 int log_len = val_end - att_beg;
6298
6299 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6300 Alert("HTTP logging : out of memory.\n");
6301 } else {
6302 if (log_len > t->fe->capture_len)
6303 log_len = t->fe->capture_len;
6304 memcpy(txn->cli_cookie, att_beg, log_len);
6305 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006306 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006307 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006308
Willy Tarreaubca99692010-10-06 19:25:55 +02006309 /* Persistence cookies in passive, rewrite or insert mode have the
6310 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006311 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006312 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006313 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006314 * For cookies in prefix mode, the form is :
6315 *
6316 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006317 */
6318 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6319 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6320 struct server *srv = t->be->srv;
6321 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006322
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006323 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6324 * have the server ID between val_beg and delim, and the original cookie between
6325 * delim+1 and val_end. Otherwise, delim==val_end :
6326 *
6327 * Cookie: NAME=SRV; # in all but prefix modes
6328 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6329 * | || || | |+-> next
6330 * | || || | +--> val_end
6331 * | || || +---------> delim
6332 * | || |+------------> val_beg
6333 * | || +-------------> att_end = equal
6334 * | |+-----------------> att_beg
6335 * | +------------------> prev
6336 * +-------------------------> hdr_beg
6337 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006338
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006339 if (t->be->options & PR_O_COOK_PFX) {
6340 for (delim = val_beg; delim < val_end; delim++)
6341 if (*delim == COOKIE_DELIM)
6342 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006343 } else {
6344 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006345 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006346 /* Now check if the cookie contains a date field, which would
6347 * appear after a vertical bar ('|') just after the server name
6348 * and before the delimiter.
6349 */
6350 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6351 if (vbar1) {
6352 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006353 * right is the last seen date. It is a base64 encoded
6354 * 30-bit value representing the UNIX date since the
6355 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006356 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006357 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006358 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006359 if (val_end - vbar1 >= 5) {
6360 val = b64tos30(vbar1);
6361 if (val > 0)
6362 txn->cookie_last_date = val << 2;
6363 }
6364 /* look for a second vertical bar */
6365 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6366 if (vbar1 && (val_end - vbar1 > 5)) {
6367 val = b64tos30(vbar1 + 1);
6368 if (val > 0)
6369 txn->cookie_first_date = val << 2;
6370 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006371 }
6372 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006373
Willy Tarreauf64d1412010-10-07 20:06:11 +02006374 /* if the cookie has an expiration date and the proxy wants to check
6375 * it, then we do that now. We first check if the cookie is too old,
6376 * then only if it has expired. We detect strict overflow because the
6377 * time resolution here is not great (4 seconds). Cookies with dates
6378 * in the future are ignored if their offset is beyond one day. This
6379 * allows an admin to fix timezone issues without expiring everyone
6380 * and at the same time avoids keeping unwanted side effects for too
6381 * long.
6382 */
6383 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006384 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6385 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006386 txn->flags &= ~TX_CK_MASK;
6387 txn->flags |= TX_CK_OLD;
6388 delim = val_beg; // let's pretend we have not found the cookie
6389 txn->cookie_first_date = 0;
6390 txn->cookie_last_date = 0;
6391 }
6392 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006393 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6394 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006395 txn->flags &= ~TX_CK_MASK;
6396 txn->flags |= TX_CK_EXPIRED;
6397 delim = val_beg; // let's pretend we have not found the cookie
6398 txn->cookie_first_date = 0;
6399 txn->cookie_last_date = 0;
6400 }
6401
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006402 /* Here, we'll look for the first running server which supports the cookie.
6403 * This allows to share a same cookie between several servers, for example
6404 * to dedicate backup servers to specific servers only.
6405 * However, to prevent clients from sticking to cookie-less backup server
6406 * when they have incidentely learned an empty cookie, we simply ignore
6407 * empty cookies and mark them as invalid.
6408 * The same behaviour is applied when persistence must be ignored.
6409 */
6410 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6411 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006412
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006413 while (srv) {
6414 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6415 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6416 if ((srv->state & SRV_RUNNING) ||
6417 (t->be->options & PR_O_PERSIST) ||
6418 (t->flags & SN_FORCE_PRST)) {
6419 /* we found the server and we can use it */
6420 txn->flags &= ~TX_CK_MASK;
6421 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6422 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006423 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006424 break;
6425 } else {
6426 /* we found a server, but it's down,
6427 * mark it as such and go on in case
6428 * another one is available.
6429 */
6430 txn->flags &= ~TX_CK_MASK;
6431 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006432 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006433 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006434 srv = srv->next;
6435 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006436
Willy Tarreauf64d1412010-10-07 20:06:11 +02006437 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006438 /* no server matched this cookie */
6439 txn->flags &= ~TX_CK_MASK;
6440 txn->flags |= TX_CK_INVALID;
6441 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006442
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006443 /* depending on the cookie mode, we may have to either :
6444 * - delete the complete cookie if we're in insert+indirect mode, so that
6445 * the server never sees it ;
6446 * - remove the server id from the cookie value, and tag the cookie as an
6447 * application cookie so that it does not get accidentely removed later,
6448 * if we're in cookie prefix mode
6449 */
6450 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6451 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006452
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006453 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6454 val_end += delta;
6455 next += delta;
6456 hdr_end += delta;
6457 hdr_next += delta;
6458 cur_hdr->len += delta;
6459 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006460
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006461 del_from = NULL;
6462 preserve_hdr = 1; /* we want to keep this cookie */
6463 }
6464 else if (del_from == NULL &&
6465 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6466 del_from = prev;
6467 }
6468 } else {
6469 /* This is not our cookie, so we must preserve it. But if we already
6470 * scheduled another cookie for removal, we cannot remove the
6471 * complete header, but we can remove the previous block itself.
6472 */
6473 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006474
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006475 if (del_from != NULL) {
6476 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006477 if (att_beg >= del_from)
6478 att_beg += delta;
6479 if (att_end >= del_from)
6480 att_end += delta;
6481 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006482 val_end += delta;
6483 next += delta;
6484 hdr_end += delta;
6485 hdr_next += delta;
6486 cur_hdr->len += delta;
6487 http_msg_move_end(&txn->req, delta);
6488 prev = del_from;
6489 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006490 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006491 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006492
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006493 /* Look for the appsession cookie unless persistence must be ignored */
6494 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6495 int cmp_len, value_len;
6496 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006497
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006498 if (t->be->options2 & PR_O2_AS_PFX) {
6499 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6500 value_begin = att_beg + t->be->appsession_name_len;
6501 value_len = val_end - att_beg - t->be->appsession_name_len;
6502 } else {
6503 cmp_len = att_end - att_beg;
6504 value_begin = val_beg;
6505 value_len = val_end - val_beg;
6506 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006507
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006508 /* let's see if the cookie is our appcookie */
6509 if (cmp_len == t->be->appsession_name_len &&
6510 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6511 manage_client_side_appsession(t, value_begin, value_len);
6512 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006513 }
6514
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006515 /* continue with next cookie on this header line */
6516 att_beg = next;
6517 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006518
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006519 /* There are no more cookies on this line.
6520 * We may still have one (or several) marked for deletion at the
6521 * end of the line. We must do this now in two ways :
6522 * - if some cookies must be preserved, we only delete from the
6523 * mark to the end of line ;
6524 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006525 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006526 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006527 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006528 if (preserve_hdr) {
6529 delta = del_hdr_value(req, &del_from, hdr_end);
6530 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006531 cur_hdr->len += delta;
6532 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006533 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006534
6535 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006536 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6537 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006538 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006539 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006540 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006541 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006542 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006543 }
6544
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006545 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006546 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006547 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006548}
6549
6550
Willy Tarreaua15645d2007-03-18 16:22:39 +01006551/* Iterate the same filter through all response headers contained in <rtr>.
6552 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6553 */
6554int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6555{
6556 char term;
6557 char *cur_ptr, *cur_end, *cur_next;
6558 int cur_idx, old_idx, last_hdr;
6559 struct http_txn *txn = &t->txn;
6560 struct hdr_idx_elem *cur_hdr;
6561 int len, delta;
6562
6563 last_hdr = 0;
6564
Willy Tarreau962c3f42010-01-10 00:15:35 +01006565 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006566 old_idx = 0;
6567
6568 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006569 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006570 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006571 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006572 (exp->action == ACT_ALLOW ||
6573 exp->action == ACT_DENY))
6574 return 0;
6575
6576 cur_idx = txn->hdr_idx.v[old_idx].next;
6577 if (!cur_idx)
6578 break;
6579
6580 cur_hdr = &txn->hdr_idx.v[cur_idx];
6581 cur_ptr = cur_next;
6582 cur_end = cur_ptr + cur_hdr->len;
6583 cur_next = cur_end + cur_hdr->cr + 1;
6584
6585 /* Now we have one header between cur_ptr and cur_end,
6586 * and the next header starts at cur_next.
6587 */
6588
6589 /* The annoying part is that pattern matching needs
6590 * that we modify the contents to null-terminate all
6591 * strings before testing them.
6592 */
6593
6594 term = *cur_end;
6595 *cur_end = '\0';
6596
6597 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6598 switch (exp->action) {
6599 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006600 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006601 last_hdr = 1;
6602 break;
6603
6604 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006605 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006606 last_hdr = 1;
6607 break;
6608
6609 case ACT_REPLACE:
6610 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6611 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6612 /* FIXME: if the user adds a newline in the replacement, the
6613 * index will not be recalculated for now, and the new line
6614 * will not be counted as a new header.
6615 */
6616
6617 cur_end += delta;
6618 cur_next += delta;
6619 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006620 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006621 break;
6622
6623 case ACT_REMOVE:
6624 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6625 cur_next += delta;
6626
Willy Tarreaufa355d42009-11-29 18:12:29 +01006627 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006628 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6629 txn->hdr_idx.used--;
6630 cur_hdr->len = 0;
6631 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006632 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006633 break;
6634
6635 }
6636 }
6637 if (cur_end)
6638 *cur_end = term; /* restore the string terminator */
6639
6640 /* keep the link from this header to next one in case of later
6641 * removal of next header.
6642 */
6643 old_idx = cur_idx;
6644 }
6645 return 0;
6646}
6647
6648
6649/* Apply the filter to the status line in the response buffer <rtr>.
6650 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6651 * or -1 if a replacement resulted in an invalid status line.
6652 */
6653int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6654{
6655 char term;
6656 char *cur_ptr, *cur_end;
6657 int done;
6658 struct http_txn *txn = &t->txn;
6659 int len, delta;
6660
6661
Willy Tarreau3d300592007-03-18 18:34:41 +01006662 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006663 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006664 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006665 (exp->action == ACT_ALLOW ||
6666 exp->action == ACT_DENY))
6667 return 0;
6668 else if (exp->action == ACT_REMOVE)
6669 return 0;
6670
6671 done = 0;
6672
Willy Tarreau962c3f42010-01-10 00:15:35 +01006673 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006674 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006675
6676 /* Now we have the status line between cur_ptr and cur_end */
6677
6678 /* The annoying part is that pattern matching needs
6679 * that we modify the contents to null-terminate all
6680 * strings before testing them.
6681 */
6682
6683 term = *cur_end;
6684 *cur_end = '\0';
6685
6686 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6687 switch (exp->action) {
6688 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006689 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006690 done = 1;
6691 break;
6692
6693 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006694 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006695 done = 1;
6696 break;
6697
6698 case ACT_REPLACE:
6699 *cur_end = term; /* restore the string terminator */
6700 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6701 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6702 /* FIXME: if the user adds a newline in the replacement, the
6703 * index will not be recalculated for now, and the new line
6704 * will not be counted as a new header.
6705 */
6706
Willy Tarreaufa355d42009-11-29 18:12:29 +01006707 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006709 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006710 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006711 cur_ptr, cur_end + 1,
6712 NULL, NULL);
6713 if (unlikely(!cur_end))
6714 return -1;
6715
6716 /* we have a full respnse and we know that we have either a CR
6717 * or an LF at <ptr>.
6718 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006719 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006720 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006721 /* there is no point trying this regex on headers */
6722 return 1;
6723 }
6724 }
6725 *cur_end = term; /* restore the string terminator */
6726 return done;
6727}
6728
6729
6730
6731/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006732 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006733 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6734 * unparsable response.
6735 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006736int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006737{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006738 struct http_txn *txn = &s->txn;
6739 struct hdr_exp *exp;
6740
6741 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006742 int ret;
6743
6744 /*
6745 * The interleaving of transformations and verdicts
6746 * makes it difficult to decide to continue or stop
6747 * the evaluation.
6748 */
6749
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006750 if (txn->flags & TX_SVDENY)
6751 break;
6752
Willy Tarreau3d300592007-03-18 18:34:41 +01006753 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006754 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6755 exp->action == ACT_PASS)) {
6756 exp = exp->next;
6757 continue;
6758 }
6759
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006760 /* if this filter had a condition, evaluate it now and skip to
6761 * next filter if the condition does not match.
6762 */
6763 if (exp->cond) {
6764 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6765 ret = acl_pass(ret);
6766 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6767 ret = !ret;
6768 if (!ret)
6769 continue;
6770 }
6771
Willy Tarreaua15645d2007-03-18 16:22:39 +01006772 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006773 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006774 if (unlikely(ret < 0))
6775 return -1;
6776
6777 if (likely(ret == 0)) {
6778 /* The filter did not match the response, it can be
6779 * iterated through all headers.
6780 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006781 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006782 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006783 }
6784 return 0;
6785}
6786
6787
Willy Tarreaua15645d2007-03-18 16:22:39 +01006788/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006789 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006790 * desirable to call it only when needed. This function is also used when we
6791 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006792 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006793void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006794{
6795 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006796 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006797 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006798 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006799 char *hdr_beg, *hdr_end, *hdr_next;
6800 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006801
Willy Tarreaua15645d2007-03-18 16:22:39 +01006802 /* Iterate through the headers.
6803 * we start with the start line.
6804 */
6805 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006806 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006807
6808 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6809 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006810 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006811
6812 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006813 hdr_beg = hdr_next;
6814 hdr_end = hdr_beg + cur_hdr->len;
6815 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006816
Willy Tarreau24581ba2010-08-31 22:39:35 +02006817 /* We have one full header between hdr_beg and hdr_end, and the
6818 * next header starts at hdr_next. We're only interested in
6819 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006820 */
6821
Willy Tarreau24581ba2010-08-31 22:39:35 +02006822 is_cookie2 = 0;
6823 prev = hdr_beg + 10;
6824 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006825 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006826 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6827 if (!val) {
6828 old_idx = cur_idx;
6829 continue;
6830 }
6831 is_cookie2 = 1;
6832 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833 }
6834
Willy Tarreau24581ba2010-08-31 22:39:35 +02006835 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6836 * <prev> points to the colon.
6837 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006838 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006839
Willy Tarreau24581ba2010-08-31 22:39:35 +02006840 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6841 * check-cache is enabled) and we are not interested in checking
6842 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006843 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006844 if (t->be->cookie_name == NULL &&
6845 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006846 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006847 return;
6848
Willy Tarreau24581ba2010-08-31 22:39:35 +02006849 /* OK so now we know we have to process this response cookie.
6850 * The format of the Set-Cookie header is slightly different
6851 * from the format of the Cookie header in that it does not
6852 * support the comma as a cookie delimiter (thus the header
6853 * cannot be folded) because the Expires attribute described in
6854 * the original Netscape's spec may contain an unquoted date
6855 * with a comma inside. We have to live with this because
6856 * many browsers don't support Max-Age and some browsers don't
6857 * support quoted strings. However the Set-Cookie2 header is
6858 * clean.
6859 *
6860 * We have to keep multiple pointers in order to support cookie
6861 * removal at the beginning, middle or end of header without
6862 * corrupting the header (in case of set-cookie2). A special
6863 * pointer, <scav> points to the beginning of the set-cookie-av
6864 * fields after the first semi-colon. The <next> pointer points
6865 * either to the end of line (set-cookie) or next unquoted comma
6866 * (set-cookie2). All of these headers are valid :
6867 *
6868 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6869 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6870 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6871 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6872 * | | | | | | | | | |
6873 * | | | | | | | | +-> next hdr_end <--+
6874 * | | | | | | | +------------> scav
6875 * | | | | | | +--------------> val_end
6876 * | | | | | +--------------------> val_beg
6877 * | | | | +----------------------> equal
6878 * | | | +------------------------> att_end
6879 * | | +----------------------------> att_beg
6880 * | +------------------------------> prev
6881 * +-----------------------------------------> hdr_beg
6882 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006883
Willy Tarreau24581ba2010-08-31 22:39:35 +02006884 for (; prev < hdr_end; prev = next) {
6885 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006886
Willy Tarreau24581ba2010-08-31 22:39:35 +02006887 /* find att_beg */
6888 att_beg = prev + 1;
6889 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6890 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006891
Willy Tarreau24581ba2010-08-31 22:39:35 +02006892 /* find att_end : this is the first character after the last non
6893 * space before the equal. It may be equal to hdr_end.
6894 */
6895 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006896
Willy Tarreau24581ba2010-08-31 22:39:35 +02006897 while (equal < hdr_end) {
6898 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6899 break;
6900 if (http_is_spht[(unsigned char)*equal++])
6901 continue;
6902 att_end = equal;
6903 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006904
Willy Tarreau24581ba2010-08-31 22:39:35 +02006905 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6906 * is between <att_beg> and <equal>, both may be identical.
6907 */
6908
6909 /* look for end of cookie if there is an equal sign */
6910 if (equal < hdr_end && *equal == '=') {
6911 /* look for the beginning of the value */
6912 val_beg = equal + 1;
6913 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6914 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006915
Willy Tarreau24581ba2010-08-31 22:39:35 +02006916 /* find the end of the value, respecting quotes */
6917 next = find_cookie_value_end(val_beg, hdr_end);
6918
6919 /* make val_end point to the first white space or delimitor after the value */
6920 val_end = next;
6921 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6922 val_end--;
6923 } else {
6924 /* <equal> points to next comma, semi-colon or EOL */
6925 val_beg = val_end = next = equal;
6926 }
6927
6928 if (next < hdr_end) {
6929 /* Set-Cookie2 supports multiple cookies, and <next> points to
6930 * a colon or semi-colon before the end. So skip all attr-value
6931 * pairs and look for the next comma. For Set-Cookie, since
6932 * commas are permitted in values, skip to the end.
6933 */
6934 if (is_cookie2)
6935 next = find_hdr_value_end(next, hdr_end);
6936 else
6937 next = hdr_end;
6938 }
6939
6940 /* Now everything is as on the diagram above */
6941
6942 /* Ignore cookies with no equal sign */
6943 if (equal == val_end)
6944 continue;
6945
6946 /* If there are spaces around the equal sign, we need to
6947 * strip them otherwise we'll get trouble for cookie captures,
6948 * or even for rewrites. Since this happens extremely rarely,
6949 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006950 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006951 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6952 int stripped_before = 0;
6953 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006954
Willy Tarreau24581ba2010-08-31 22:39:35 +02006955 if (att_end != equal) {
6956 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6957 equal += stripped_before;
6958 val_beg += stripped_before;
6959 }
6960
6961 if (val_beg > equal + 1) {
6962 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6963 val_beg += stripped_after;
6964 stripped_before += stripped_after;
6965 }
6966
6967 val_end += stripped_before;
6968 next += stripped_before;
6969 hdr_end += stripped_before;
6970 hdr_next += stripped_before;
6971 cur_hdr->len += stripped_before;
6972 http_msg_move_end(&txn->req, stripped_before);
6973 }
6974
6975 /* First, let's see if we want to capture this cookie. We check
6976 * that we don't already have a server side cookie, because we
6977 * can only capture one. Also as an optimisation, we ignore
6978 * cookies shorter than the declared name.
6979 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006980 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006981 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006982 (val_end - att_beg >= t->fe->capture_namelen) &&
6983 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6984 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006985 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006986 Alert("HTTP logging : out of memory.\n");
6987 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006988 else {
6989 if (log_len > t->fe->capture_len)
6990 log_len = t->fe->capture_len;
6991 memcpy(txn->srv_cookie, att_beg, log_len);
6992 txn->srv_cookie[log_len] = 0;
6993 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006994 }
6995
Willy Tarreau827aee92011-03-10 16:55:02 +01006996 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006997 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006998 if (!(t->flags & SN_IGNORE_PRST) &&
6999 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7000 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007001 /* assume passive cookie by default */
7002 txn->flags &= ~TX_SCK_MASK;
7003 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007004
7005 /* If the cookie is in insert mode on a known server, we'll delete
7006 * this occurrence because we'll insert another one later.
7007 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007008 * a direct access.
7009 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007010 if (t->be->options2 & PR_O2_COOK_PSV) {
7011 /* The "preserve" flag was set, we don't want to touch the
7012 * server's cookie.
7013 */
7014 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007015 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007016 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007017 /* this cookie must be deleted */
7018 if (*prev == ':' && next == hdr_end) {
7019 /* whole header */
7020 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
7021 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7022 txn->hdr_idx.used--;
7023 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007024 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007025 hdr_next += delta;
7026 http_msg_move_end(&txn->rsp, delta);
7027 /* note: while both invalid now, <next> and <hdr_end>
7028 * are still equal, so the for() will stop as expected.
7029 */
7030 } else {
7031 /* just remove the value */
7032 int delta = del_hdr_value(res, &prev, next);
7033 next = prev;
7034 hdr_end += delta;
7035 hdr_next += delta;
7036 cur_hdr->len += delta;
7037 http_msg_move_end(&txn->rsp, delta);
7038 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007039 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007040 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007041 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007042 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007043 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007044 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007045 * with this server since we know it.
7046 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007047 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007048 next += delta;
7049 hdr_end += delta;
7050 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007051 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007052 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007053
Willy Tarreauf1348312010-10-07 15:54:11 +02007054 txn->flags &= ~TX_SCK_MASK;
7055 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007056 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007057 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007058 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007059 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007060 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007061 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007062 next += delta;
7063 hdr_end += delta;
7064 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007065 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007066 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007067
Willy Tarreau827aee92011-03-10 16:55:02 +01007068 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007069 txn->flags &= ~TX_SCK_MASK;
7070 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007071 }
7072 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007073 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7074 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007075 int cmp_len, value_len;
7076 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007077
Cyril Bontéb21570a2009-11-29 20:04:48 +01007078 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007079 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7080 value_begin = att_beg + t->be->appsession_name_len;
7081 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007082 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007083 cmp_len = att_end - att_beg;
7084 value_begin = val_beg;
7085 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007086 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007087
Cyril Bonté17530c32010-04-06 21:11:10 +02007088 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007089 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7090 /* free a possibly previously allocated memory */
7091 pool_free2(apools.sessid, txn->sessid);
7092
Cyril Bontéb21570a2009-11-29 20:04:48 +01007093 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007094 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007095 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7096 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7097 return;
7098 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007099 memcpy(txn->sessid, value_begin, value_len);
7100 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007101 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007102 }
7103 /* that's done for this cookie, check the next one on the same
7104 * line when next != hdr_end (only if is_cookie2).
7105 */
7106 }
7107 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007108 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007109 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007110
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007111 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007112 appsess *asession = NULL;
7113 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007114 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007115 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007116 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007117 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7118 Alert("Not enough Memory process_srv():asession:calloc().\n");
7119 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7120 return;
7121 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007122 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7123
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007124 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7125 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7126 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007127 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007128 return;
7129 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007130 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007131 asession->sessid[t->be->appsession_len] = 0;
7132
Willy Tarreau827aee92011-03-10 16:55:02 +01007133 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007134 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007135 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007136 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007137 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007138 return;
7139 }
7140 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01007141 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007142
7143 asession->request_count = 0;
7144 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7145 }
7146
7147 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7148 asession->request_count++;
7149 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007150}
7151
7152
Willy Tarreaua15645d2007-03-18 16:22:39 +01007153/*
7154 * Check if response is cacheable or not. Updates t->flags.
7155 */
7156void check_response_for_cacheability(struct session *t, struct buffer *rtr)
7157{
7158 struct http_txn *txn = &t->txn;
7159 char *p1, *p2;
7160
7161 char *cur_ptr, *cur_end, *cur_next;
7162 int cur_idx;
7163
Willy Tarreau5df51872007-11-25 16:20:08 +01007164 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007165 return;
7166
7167 /* Iterate through the headers.
7168 * we start with the start line.
7169 */
7170 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007171 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007172
7173 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7174 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007175 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176
7177 cur_hdr = &txn->hdr_idx.v[cur_idx];
7178 cur_ptr = cur_next;
7179 cur_end = cur_ptr + cur_hdr->len;
7180 cur_next = cur_end + cur_hdr->cr + 1;
7181
7182 /* We have one full header between cur_ptr and cur_end, and the
7183 * next header starts at cur_next. We're only interested in
7184 * "Cookie:" headers.
7185 */
7186
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007187 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7188 if (val) {
7189 if ((cur_end - (cur_ptr + val) >= 8) &&
7190 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7191 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7192 return;
7193 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007194 }
7195
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007196 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7197 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007198 continue;
7199
7200 /* OK, right now we know we have a cache-control header at cur_ptr */
7201
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007202 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203
7204 if (p1 >= cur_end) /* no more info */
7205 continue;
7206
7207 /* p1 is at the beginning of the value */
7208 p2 = p1;
7209
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007210 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007211 p2++;
7212
7213 /* we have a complete value between p1 and p2 */
7214 if (p2 < cur_end && *p2 == '=') {
7215 /* we have something of the form no-cache="set-cookie" */
7216 if ((cur_end - p1 >= 21) &&
7217 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7218 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007219 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007220 continue;
7221 }
7222
7223 /* OK, so we know that either p2 points to the end of string or to a comma */
7224 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7225 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7226 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7227 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007228 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007229 return;
7230 }
7231
7232 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007233 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007234 continue;
7235 }
7236 }
7237}
7238
7239
Willy Tarreau58f10d72006-12-04 02:26:12 +01007240/*
7241 * Try to retrieve a known appsession in the URI, then the associated server.
7242 * If the server is found, it's assigned to the session.
7243 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007244void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007245{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007246 char *end_params, *first_param, *cur_param, *next_param;
7247 char separator;
7248 int value_len;
7249
7250 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007251
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007252 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007253 (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 +01007254 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007255 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007256
Cyril Bontéb21570a2009-11-29 20:04:48 +01007257 first_param = NULL;
7258 switch (mode) {
7259 case PR_O2_AS_M_PP:
7260 first_param = memchr(begin, ';', len);
7261 break;
7262 case PR_O2_AS_M_QS:
7263 first_param = memchr(begin, '?', len);
7264 break;
7265 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007266
Cyril Bontéb21570a2009-11-29 20:04:48 +01007267 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007268 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007269 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007270
Cyril Bontéb21570a2009-11-29 20:04:48 +01007271 switch (mode) {
7272 case PR_O2_AS_M_PP:
7273 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7274 end_params = (char *) begin + len;
7275 }
7276 separator = ';';
7277 break;
7278 case PR_O2_AS_M_QS:
7279 end_params = (char *) begin + len;
7280 separator = '&';
7281 break;
7282 default:
7283 /* unknown mode, shouldn't happen */
7284 return;
7285 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007286
Cyril Bontéb21570a2009-11-29 20:04:48 +01007287 cur_param = next_param = end_params;
7288 while (cur_param > first_param) {
7289 cur_param--;
7290 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7291 /* let's see if this is the appsession parameter */
7292 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7293 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7294 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7295 /* Cool... it's the right one */
7296 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7297 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7298 if (value_len > 0) {
7299 manage_client_side_appsession(t, cur_param, value_len);
7300 }
7301 break;
7302 }
7303 next_param = cur_param;
7304 }
7305 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007306#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007307 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007308 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007309#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007310}
7311
Willy Tarreaub2513902006-12-17 14:52:38 +01007312/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007313 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007314 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007315 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007316 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007317 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007318 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007319 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007320 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007321int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007322{
7323 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007324 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007325
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007326 if (!uri_auth)
7327 return 0;
7328
Cyril Bonté70be45d2010-10-12 00:14:35 +02007329 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007330 return 0;
7331
Willy Tarreau295a8372011-03-10 11:25:07 +01007332 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007333
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007334 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007335 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007336 return 0;
7337
Willy Tarreau962c3f42010-01-10 00:15:35 +01007338 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007339
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007340 /* the URI is in h */
7341 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007342 return 0;
7343
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007344 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007345 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007346 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007347 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007348 break;
7349 }
7350 h++;
7351 }
7352
7353 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007354 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7355 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007356 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007357 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007358 break;
7359 }
7360 h++;
7361 }
7362 }
7363
Willy Tarreau962c3f42010-01-10 00:15:35 +01007364 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7365 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007366 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007367 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007368 break;
7369 }
7370 h++;
7371 }
7372
Cyril Bonté70be45d2010-10-12 00:14:35 +02007373 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7374 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7375 if (memcmp(h, ";st=", 4) == 0) {
7376 h += 4;
7377
7378 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007379 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007380 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007381 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007382 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007383 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007384 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007385 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007386 else
Willy Tarreau295a8372011-03-10 11:25:07 +01007387 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007388 break;
7389 }
7390 h++;
7391 }
7392
Willy Tarreau295a8372011-03-10 11:25:07 +01007393 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007394
Willy Tarreaub2513902006-12-17 14:52:38 +01007395 return 1;
7396}
7397
Willy Tarreau4076a152009-04-02 15:18:36 +02007398/*
7399 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007400 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7401 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007402 */
7403void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7404 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007405 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007406{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007407 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7408 int len1 = buf->size - msg->som;
7409 es->len = buf->r - (buf->data + msg->som) + buf->size;
7410 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7411 if (es->len > len1 && len1 < sizeof(es->buf))
7412 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7413 }
7414 else {
7415 es->len = buf->r - (buf->data + msg->som);
7416 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7417 }
7418
Willy Tarreau4076a152009-04-02 15:18:36 +02007419 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007420 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007421 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007422 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007423 else
7424 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7425
Willy Tarreau4076a152009-04-02 15:18:36 +02007426 es->when = date; // user-visible date
7427 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007428 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007429 es->oe = other_end;
Willy Tarreau957c0a52011-03-03 17:42:23 +01007430 es->src = s->req->prod->addr.c.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007431 es->state = state;
7432 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007433 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007434}
Willy Tarreaub2513902006-12-17 14:52:38 +01007435
Willy Tarreaubce70882009-09-07 11:51:47 +02007436/* return the IP address pointed to by occurrence <occ> of header <hname> in
7437 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
7438 * occurrence number corresponding to this value is returned. If <occ> is
7439 * strictly negative, the occurrence number before the end corresponding to
7440 * this value is returned. If <occ> is null, any value is returned, so it is
7441 * not recommended to use it that way. Negative occurrences are limited to
7442 * a small value because it is required to keep them in memory while scanning.
7443 * IP address 0.0.0.0 is returned if no match is found.
7444 */
7445unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
7446{
7447 struct hdr_ctx ctx;
7448 unsigned int hdr_hist[MAX_HDR_HISTORY];
7449 unsigned int hist_ptr;
7450 int found = 0;
7451
7452 ctx.idx = 0;
7453 if (occ >= 0) {
7454 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7455 occ--;
7456 if (occ <= 0) {
7457 found = 1;
7458 break;
7459 }
7460 }
7461 if (!found)
7462 return 0;
7463 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7464 }
7465
7466 /* negative occurrence, we scan all the list then walk back */
7467 if (-occ > MAX_HDR_HISTORY)
7468 return 0;
7469
7470 hist_ptr = 0;
7471 hdr_hist[hist_ptr] = 0;
7472 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7473 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7474 if (hist_ptr >= MAX_HDR_HISTORY)
7475 hist_ptr = 0;
7476 found++;
7477 }
7478 if (-occ > found)
7479 return 0;
7480 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7481 * find occurrence -occ, so we have to check [hist_ptr+occ].
7482 */
7483 hist_ptr += occ;
7484 if (hist_ptr >= MAX_HDR_HISTORY)
7485 hist_ptr -= MAX_HDR_HISTORY;
7486 return hdr_hist[hist_ptr];
7487}
7488
Willy Tarreaubaaee002006-06-26 02:48:02 +02007489/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007490 * Print a debug line with a header
7491 */
7492void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7493{
7494 int len, max;
7495 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007496 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007497 max = end - start;
7498 UBOUND(max, sizeof(trash) - len - 1);
7499 len += strlcpy2(trash + len, start, max + 1);
7500 trash[len++] = '\n';
7501 write(1, trash, len);
7502}
7503
Willy Tarreau0937bc42009-12-22 15:03:09 +01007504/*
7505 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7506 * the required fields are properly allocated and that we only need to (re)init
7507 * them. This should be used before processing any new request.
7508 */
7509void http_init_txn(struct session *s)
7510{
7511 struct http_txn *txn = &s->txn;
7512 struct proxy *fe = s->fe;
7513
7514 txn->flags = 0;
7515 txn->status = -1;
7516
Willy Tarreauf64d1412010-10-07 20:06:11 +02007517 txn->cookie_first_date = 0;
7518 txn->cookie_last_date = 0;
7519
Willy Tarreau0937bc42009-12-22 15:03:09 +01007520 txn->req.sol = txn->req.eol = NULL;
7521 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7522 txn->rsp.sol = txn->rsp.eol = NULL;
7523 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007524 txn->req.chunk_len = 0LL;
7525 txn->req.body_len = 0LL;
7526 txn->rsp.chunk_len = 0LL;
7527 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007528 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7529 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007530
7531 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007532
7533 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7534 if (fe->options2 & PR_O2_REQBUG_OK)
7535 txn->req.err_pos = -1; /* let buggy requests pass */
7536
Willy Tarreau46023632010-01-07 22:51:47 +01007537 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007538 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7539
Willy Tarreau46023632010-01-07 22:51:47 +01007540 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007541 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7542
7543 if (txn->hdr_idx.v)
7544 hdr_idx_init(&txn->hdr_idx);
7545}
7546
7547/* to be used at the end of a transaction */
7548void http_end_txn(struct session *s)
7549{
7550 struct http_txn *txn = &s->txn;
7551
7552 /* these ones will have been dynamically allocated */
7553 pool_free2(pool2_requri, txn->uri);
7554 pool_free2(pool2_capture, txn->cli_cookie);
7555 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007556 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007557
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007558 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007559 txn->uri = NULL;
7560 txn->srv_cookie = NULL;
7561 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007562
7563 if (txn->req.cap) {
7564 struct cap_hdr *h;
7565 for (h = s->fe->req_cap; h; h = h->next)
7566 pool_free2(h->pool, txn->req.cap[h->index]);
7567 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7568 }
7569
7570 if (txn->rsp.cap) {
7571 struct cap_hdr *h;
7572 for (h = s->fe->rsp_cap; h; h = h->next)
7573 pool_free2(h->pool, txn->rsp.cap[h->index]);
7574 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7575 }
7576
Willy Tarreau0937bc42009-12-22 15:03:09 +01007577}
7578
7579/* to be used at the end of a transaction to prepare a new one */
7580void http_reset_txn(struct session *s)
7581{
7582 http_end_txn(s);
7583 http_init_txn(s);
7584
7585 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007586 s->logs.logwait = s->fe->to_log;
Willy Tarreau827aee92011-03-10 16:55:02 +01007587 s->srv_conn = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +01007588 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007589 /* re-init store persistence */
7590 s->store_count = 0;
7591
Willy Tarreau0937bc42009-12-22 15:03:09 +01007592 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007593
7594 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7595
Willy Tarreau739cfba2010-01-25 23:11:14 +01007596 /* We must trim any excess data from the response buffer, because we
7597 * may have blocked an invalid response from a server that we don't
7598 * want to accidentely forward once we disable the analysers, nor do
7599 * we want those data to come along with next response. A typical
7600 * example of such data would be from a buggy server responding to
7601 * a HEAD with some data, or sending more than the advertised
7602 * content-length.
7603 */
7604 if (unlikely(s->rep->l > s->rep->send_max)) {
7605 s->rep->l = s->rep->send_max;
7606 s->rep->r = s->rep->w + s->rep->l;
7607 if (s->rep->r >= s->rep->data + s->rep->size)
7608 s->rep->r -= s->rep->size;
7609 }
7610
Willy Tarreau0937bc42009-12-22 15:03:09 +01007611 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007612 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007613
Willy Tarreaud04e8582010-05-31 12:31:35 +02007614 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007615 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007616
7617 s->req->rex = TICK_ETERNITY;
7618 s->req->wex = TICK_ETERNITY;
7619 s->req->analyse_exp = TICK_ETERNITY;
7620 s->rep->rex = TICK_ETERNITY;
7621 s->rep->wex = TICK_ETERNITY;
7622 s->rep->analyse_exp = TICK_ETERNITY;
7623}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007624
Willy Tarreauff011f22011-01-06 17:51:27 +01007625void free_http_req_rules(struct list *r) {
7626 struct http_req_rule *tr, *pr;
7627
7628 list_for_each_entry_safe(pr, tr, r, list) {
7629 LIST_DEL(&pr->list);
7630 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7631 free(pr->http_auth.realm);
7632
7633 free(pr);
7634 }
7635}
7636
7637struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7638{
7639 struct http_req_rule *rule;
7640 int cur_arg;
7641
7642 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7643 if (!rule) {
7644 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7645 return NULL;
7646 }
7647
7648 if (!*args[0]) {
7649 goto req_error_parsing;
7650 } else if (!strcmp(args[0], "allow")) {
7651 rule->action = HTTP_REQ_ACT_ALLOW;
7652 cur_arg = 1;
7653 } else if (!strcmp(args[0], "deny")) {
7654 rule->action = HTTP_REQ_ACT_DENY;
7655 cur_arg = 1;
7656 } else if (!strcmp(args[0], "auth")) {
7657 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7658 cur_arg = 1;
7659
7660 while(*args[cur_arg]) {
7661 if (!strcmp(args[cur_arg], "realm")) {
7662 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7663 cur_arg+=2;
7664 continue;
7665 } else
7666 break;
7667 }
7668 } else {
7669req_error_parsing:
7670 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7671 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7672 return NULL;
7673 }
7674
7675 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7676 struct acl_cond *cond;
7677
7678 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7679 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7680 file, linenum, args[0]);
7681 return NULL;
7682 }
7683 rule->cond = cond;
7684 }
7685 else if (*args[cur_arg]) {
7686 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7687 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7688 file, linenum, args[0], args[cur_arg]);
7689 return NULL;
7690 }
7691
7692 return rule;
7693}
7694
Willy Tarreau8797c062007-05-07 00:55:35 +02007695/************************************************************************/
7696/* The code below is dedicated to ACL parsing and matching */
7697/************************************************************************/
7698
7699
7700
7701
7702/* 1. Check on METHOD
7703 * We use the pre-parsed method if it is known, and store its number as an
7704 * integer. If it is unknown, we use the pointer and the length.
7705 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007706static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007707{
7708 int len, meth;
7709
Willy Tarreauae8b7962007-06-09 23:10:04 +02007710 len = strlen(*text);
7711 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007712
7713 pattern->val.i = meth;
7714 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007715 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007716 if (!pattern->ptr.str)
7717 return 0;
7718 pattern->len = len;
7719 }
7720 return 1;
7721}
7722
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007723static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007724acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7725 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007726{
7727 int meth;
7728 struct http_txn *txn = l7;
7729
Willy Tarreaub6866442008-07-14 23:54:42 +02007730 if (!txn)
7731 return 0;
7732
Willy Tarreau655dce92009-11-08 13:10:58 +01007733 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007734 return 0;
7735
Willy Tarreau8797c062007-05-07 00:55:35 +02007736 meth = txn->meth;
7737 test->i = meth;
7738 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007739 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7740 /* ensure the indexes are not affected */
7741 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007742 test->len = txn->req.sl.rq.m_l;
7743 test->ptr = txn->req.sol;
7744 }
7745 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7746 return 1;
7747}
7748
7749static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7750{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007751 int icase;
7752
Willy Tarreau8797c062007-05-07 00:55:35 +02007753 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02007754 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007755
7756 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02007757 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007758
7759 /* Other method, we must compare the strings */
7760 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02007761 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007762
7763 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
7764 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
7765 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007766 return ACL_PAT_FAIL;
7767 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007768}
7769
7770/* 2. Check on Request/Status Version
7771 * We simply compare strings here.
7772 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007773static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007774{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007775 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007776 if (!pattern->ptr.str)
7777 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007778 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007779 return 1;
7780}
7781
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007782static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007783acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7784 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007785{
7786 struct http_txn *txn = l7;
7787 char *ptr;
7788 int len;
7789
Willy Tarreaub6866442008-07-14 23:54:42 +02007790 if (!txn)
7791 return 0;
7792
Willy Tarreau655dce92009-11-08 13:10:58 +01007793 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007794 return 0;
7795
Willy Tarreau8797c062007-05-07 00:55:35 +02007796 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007797 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007798
7799 while ((len-- > 0) && (*ptr++ != '/'));
7800 if (len <= 0)
7801 return 0;
7802
7803 test->ptr = ptr;
7804 test->len = len;
7805
7806 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7807 return 1;
7808}
7809
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007810static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007811acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7812 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007813{
7814 struct http_txn *txn = l7;
7815 char *ptr;
7816 int len;
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->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007822 return 0;
7823
Willy Tarreau8797c062007-05-07 00:55:35 +02007824 len = txn->rsp.sl.st.v_l;
7825 ptr = txn->rsp.sol;
7826
7827 while ((len-- > 0) && (*ptr++ != '/'));
7828 if (len <= 0)
7829 return 0;
7830
7831 test->ptr = ptr;
7832 test->len = len;
7833
7834 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7835 return 1;
7836}
7837
7838/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007839static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007840acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7841 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007842{
7843 struct http_txn *txn = l7;
7844 char *ptr;
7845 int len;
7846
Willy Tarreaub6866442008-07-14 23:54:42 +02007847 if (!txn)
7848 return 0;
7849
Willy Tarreau655dce92009-11-08 13:10:58 +01007850 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007851 return 0;
7852
Willy Tarreau8797c062007-05-07 00:55:35 +02007853 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007854 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007855
7856 test->i = __strl2ui(ptr, len);
7857 test->flags = ACL_TEST_F_VOL_1ST;
7858 return 1;
7859}
7860
7861/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007862static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007863acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7864 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007865{
7866 struct http_txn *txn = l7;
7867
Willy Tarreaub6866442008-07-14 23:54:42 +02007868 if (!txn)
7869 return 0;
7870
Willy Tarreau655dce92009-11-08 13:10:58 +01007871 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007872 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007873
Willy Tarreauc11416f2007-06-17 16:58:38 +02007874 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7875 /* ensure the indexes are not affected */
7876 return 0;
7877
Willy Tarreau8797c062007-05-07 00:55:35 +02007878 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007879 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007880
Willy Tarreauf3d25982007-05-08 22:45:09 +02007881 /* we do not need to set READ_ONLY because the data is in a buffer */
7882 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007883 return 1;
7884}
7885
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007886static int
7887acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7888 struct acl_expr *expr, struct acl_test *test)
7889{
7890 struct http_txn *txn = l7;
7891
Willy Tarreaub6866442008-07-14 23:54:42 +02007892 if (!txn)
7893 return 0;
7894
Willy Tarreau655dce92009-11-08 13:10:58 +01007895 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007896 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007897
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007898 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7899 /* ensure the indexes are not affected */
7900 return 0;
7901
7902 /* Parse HTTP request */
Willy Tarreau957c0a52011-03-03 17:42:23 +01007903 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to);
7904 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007905 test->i = AF_INET;
7906
7907 /*
7908 * If we are parsing url in frontend space, we prepare backend stage
7909 * to not parse again the same url ! optimization lazyness...
7910 */
7911 if (px->options & PR_O_HTTP_PROXY)
7912 l4->flags |= SN_ADDR_SET;
7913
7914 test->flags = ACL_TEST_F_READ_ONLY;
7915 return 1;
7916}
7917
7918static int
7919acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7920 struct acl_expr *expr, struct acl_test *test)
7921{
7922 struct http_txn *txn = l7;
7923
Willy Tarreaub6866442008-07-14 23:54:42 +02007924 if (!txn)
7925 return 0;
7926
Willy Tarreau655dce92009-11-08 13:10:58 +01007927 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007928 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007929
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007930 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7931 /* ensure the indexes are not affected */
7932 return 0;
7933
7934 /* Same optimization as url_ip */
Willy Tarreau957c0a52011-03-03 17:42:23 +01007935 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to);
7936 test->i = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007937
7938 if (px->options & PR_O_HTTP_PROXY)
7939 l4->flags |= SN_ADDR_SET;
7940
7941 test->flags = ACL_TEST_F_READ_ONLY;
7942 return 1;
7943}
7944
Willy Tarreauc11416f2007-06-17 16:58:38 +02007945/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7946 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7947 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007948static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007949acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007950 struct acl_expr *expr, struct acl_test *test)
7951{
7952 struct http_txn *txn = l7;
7953 struct hdr_idx *idx = &txn->hdr_idx;
7954 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007955
Willy Tarreaub6866442008-07-14 23:54:42 +02007956 if (!txn)
7957 return 0;
7958
Willy Tarreau33a7e692007-06-10 19:45:56 +02007959 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7960 /* search for header from the beginning */
7961 ctx->idx = 0;
7962
Willy Tarreau33a7e692007-06-10 19:45:56 +02007963 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7964 test->flags |= ACL_TEST_F_FETCH_MORE;
7965 test->flags |= ACL_TEST_F_VOL_HDR;
7966 test->len = ctx->vlen;
7967 test->ptr = (char *)ctx->line + ctx->val;
7968 return 1;
7969 }
7970
7971 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7972 test->flags |= ACL_TEST_F_VOL_HDR;
7973 return 0;
7974}
7975
Willy Tarreau33a7e692007-06-10 19:45:56 +02007976static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007977acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7978 struct acl_expr *expr, struct acl_test *test)
7979{
7980 struct http_txn *txn = l7;
7981
Willy Tarreaub6866442008-07-14 23:54:42 +02007982 if (!txn)
7983 return 0;
7984
Willy Tarreau655dce92009-11-08 13:10:58 +01007985 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007986 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007987
Willy Tarreauc11416f2007-06-17 16:58:38 +02007988 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7989 /* ensure the indexes are not affected */
7990 return 0;
7991
7992 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7993}
7994
7995static int
7996acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7997 struct acl_expr *expr, struct acl_test *test)
7998{
7999 struct http_txn *txn = l7;
8000
Willy Tarreaub6866442008-07-14 23:54:42 +02008001 if (!txn)
8002 return 0;
8003
Willy Tarreau655dce92009-11-08 13:10:58 +01008004 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008005 return 0;
8006
8007 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
8008}
8009
8010/* 6. Check on HTTP header count. The number of occurrences is returned.
8011 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8012 */
8013static int
8014acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008015 struct acl_expr *expr, struct acl_test *test)
8016{
8017 struct http_txn *txn = l7;
8018 struct hdr_idx *idx = &txn->hdr_idx;
8019 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008020 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008021
Willy Tarreaub6866442008-07-14 23:54:42 +02008022 if (!txn)
8023 return 0;
8024
Willy Tarreau33a7e692007-06-10 19:45:56 +02008025 ctx.idx = 0;
8026 cnt = 0;
8027 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
8028 cnt++;
8029
8030 test->i = cnt;
8031 test->flags = ACL_TEST_F_VOL_HDR;
8032 return 1;
8033}
8034
Willy Tarreauc11416f2007-06-17 16:58:38 +02008035static int
8036acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8037 struct acl_expr *expr, struct acl_test *test)
8038{
8039 struct http_txn *txn = l7;
8040
Willy Tarreaub6866442008-07-14 23:54:42 +02008041 if (!txn)
8042 return 0;
8043
Willy Tarreau655dce92009-11-08 13:10:58 +01008044 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008045 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008046
Willy Tarreauc11416f2007-06-17 16:58:38 +02008047 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8048 /* ensure the indexes are not affected */
8049 return 0;
8050
8051 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
8052}
8053
8054static int
8055acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8056 struct acl_expr *expr, struct acl_test *test)
8057{
8058 struct http_txn *txn = l7;
8059
Willy Tarreaub6866442008-07-14 23:54:42 +02008060 if (!txn)
8061 return 0;
8062
Willy Tarreau655dce92009-11-08 13:10:58 +01008063 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008064 return 0;
8065
8066 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
8067}
8068
Willy Tarreau33a7e692007-06-10 19:45:56 +02008069/* 7. Check on HTTP header's integer value. The integer value is returned.
8070 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008071 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008072 */
8073static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02008074acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008075 struct acl_expr *expr, struct acl_test *test)
8076{
8077 struct http_txn *txn = l7;
8078 struct hdr_idx *idx = &txn->hdr_idx;
8079 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008080
Willy Tarreaub6866442008-07-14 23:54:42 +02008081 if (!txn)
8082 return 0;
8083
Willy Tarreau33a7e692007-06-10 19:45:56 +02008084 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8085 /* search for header from the beginning */
8086 ctx->idx = 0;
8087
Willy Tarreau33a7e692007-06-10 19:45:56 +02008088 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8089 test->flags |= ACL_TEST_F_FETCH_MORE;
8090 test->flags |= ACL_TEST_F_VOL_HDR;
8091 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
8092 return 1;
8093 }
8094
8095 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8096 test->flags |= ACL_TEST_F_VOL_HDR;
8097 return 0;
8098}
8099
Willy Tarreauc11416f2007-06-17 16:58:38 +02008100static int
8101acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8102 struct acl_expr *expr, struct acl_test *test)
8103{
8104 struct http_txn *txn = l7;
8105
Willy Tarreaub6866442008-07-14 23:54:42 +02008106 if (!txn)
8107 return 0;
8108
Willy Tarreau655dce92009-11-08 13:10:58 +01008109 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008110 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008111
Willy Tarreauc11416f2007-06-17 16:58:38 +02008112 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8113 /* ensure the indexes are not affected */
8114 return 0;
8115
8116 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
8117}
8118
8119static int
8120acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8121 struct acl_expr *expr, struct acl_test *test)
8122{
8123 struct http_txn *txn = l7;
8124
Willy Tarreaub6866442008-07-14 23:54:42 +02008125 if (!txn)
8126 return 0;
8127
Willy Tarreau655dce92009-11-08 13:10:58 +01008128 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008129 return 0;
8130
8131 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
8132}
8133
Willy Tarreau106f9792009-09-19 07:54:16 +02008134/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
8135 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8136 */
8137static int
8138acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
8139 struct acl_expr *expr, struct acl_test *test)
8140{
8141 struct http_txn *txn = l7;
8142 struct hdr_idx *idx = &txn->hdr_idx;
8143 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
8144
8145 if (!txn)
8146 return 0;
8147
8148 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8149 /* search for header from the beginning */
8150 ctx->idx = 0;
8151
8152 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8153 test->flags |= ACL_TEST_F_FETCH_MORE;
8154 test->flags |= ACL_TEST_F_VOL_HDR;
8155 /* Same optimization as url_ip */
David du Colombier6f5ccb12011-03-10 22:26:24 +01008156 memset(&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr, 0, sizeof(((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr));
8157 url2ipv4((char *)ctx->line + ctx->val, &((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr);
8158 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr;
Willy Tarreau106f9792009-09-19 07:54:16 +02008159 test->i = AF_INET;
8160 return 1;
8161 }
8162
8163 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8164 test->flags |= ACL_TEST_F_VOL_HDR;
8165 return 0;
8166}
8167
8168static int
8169acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8170 struct acl_expr *expr, struct acl_test *test)
8171{
8172 struct http_txn *txn = l7;
8173
8174 if (!txn)
8175 return 0;
8176
Willy Tarreau655dce92009-11-08 13:10:58 +01008177 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008178 return 0;
8179
8180 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8181 /* ensure the indexes are not affected */
8182 return 0;
8183
8184 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8185}
8186
8187static int
8188acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8189 struct acl_expr *expr, struct acl_test *test)
8190{
8191 struct http_txn *txn = l7;
8192
8193 if (!txn)
8194 return 0;
8195
Willy Tarreau655dce92009-11-08 13:10:58 +01008196 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008197 return 0;
8198
8199 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8200}
8201
Willy Tarreau737b0c12007-06-10 21:28:46 +02008202/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8203 * the first '/' after the possible hostname, and ends before the possible '?'.
8204 */
8205static int
8206acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8207 struct acl_expr *expr, struct acl_test *test)
8208{
8209 struct http_txn *txn = l7;
8210 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008211
Willy Tarreaub6866442008-07-14 23:54:42 +02008212 if (!txn)
8213 return 0;
8214
Willy Tarreau655dce92009-11-08 13:10:58 +01008215 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008216 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008217
Willy Tarreauc11416f2007-06-17 16:58:38 +02008218 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8219 /* ensure the indexes are not affected */
8220 return 0;
8221
Willy Tarreau962c3f42010-01-10 00:15:35 +01008222 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008223 ptr = http_get_path(txn);
8224 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008225 return 0;
8226
8227 /* OK, we got the '/' ! */
8228 test->ptr = ptr;
8229
8230 while (ptr < end && *ptr != '?')
8231 ptr++;
8232
8233 test->len = ptr - test->ptr;
8234
8235 /* we do not need to set READ_ONLY because the data is in a buffer */
8236 test->flags = ACL_TEST_F_VOL_1ST;
8237 return 1;
8238}
8239
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008240static int
8241acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8242 struct acl_expr *expr, struct acl_test *test)
8243{
8244 struct buffer *req = s->req;
8245 struct http_txn *txn = &s->txn;
8246 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008247
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008248 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8249 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8250 */
8251
8252 if (!s || !req)
8253 return 0;
8254
Willy Tarreau655dce92009-11-08 13:10:58 +01008255 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008256 /* Already decoded as OK */
8257 test->flags |= ACL_TEST_F_SET_RES_PASS;
8258 return 1;
8259 }
8260
8261 /* Try to decode HTTP request */
8262 if (likely(req->lr < req->r))
8263 http_msg_analyzer(req, msg, &txn->hdr_idx);
8264
Willy Tarreau655dce92009-11-08 13:10:58 +01008265 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008266 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8267 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8268 return 1;
8269 }
8270 /* wait for final state */
8271 test->flags |= ACL_TEST_F_MAY_CHANGE;
8272 return 0;
8273 }
8274
8275 /* OK we got a valid HTTP request. We have some minor preparation to
8276 * perform so that further checks can rely on HTTP tests.
8277 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008278 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008279 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8280 s->flags |= SN_REDIRECTABLE;
8281
8282 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8283 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8284 return 1;
8285 }
8286
8287 test->flags |= ACL_TEST_F_SET_RES_PASS;
8288 return 1;
8289}
8290
Willy Tarreau7f18e522010-10-22 20:04:13 +02008291/* return a valid test if the current request is the first one on the connection */
8292static int
8293acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8294 struct acl_expr *expr, struct acl_test *test)
8295{
8296 if (!s)
8297 return 0;
8298
8299 if (s->txn.flags & TX_NOT_FIRST)
8300 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8301 else
8302 test->flags |= ACL_TEST_F_SET_RES_PASS;
8303
8304 return 1;
8305}
8306
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008307static int
8308acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8309 struct acl_expr *expr, struct acl_test *test)
8310{
8311
8312 if (!s)
8313 return 0;
8314
8315 if (!get_http_auth(s))
8316 return 0;
8317
8318 test->ctx.a[0] = expr->arg.ul;
8319 test->ctx.a[1] = s->txn.auth.user;
8320 test->ctx.a[2] = s->txn.auth.pass;
8321
8322 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8323
8324 return 1;
8325}
Willy Tarreau8797c062007-05-07 00:55:35 +02008326
8327/************************************************************************/
8328/* All supported keywords must be declared here. */
8329/************************************************************************/
8330
8331/* Note: must not be declared <const> as its list will be overwritten */
8332static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008333 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8334
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008335 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008336 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8337 { "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 +02008338 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008339
Willy Tarreauc4262962010-05-10 23:42:40 +02008340 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008341 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8342 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8343 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8344 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8345 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8346 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008347 { "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 +02008348 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008349
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008350 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008351 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008352 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8353 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8354 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8355 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8356 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8357 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8358 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
8359 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008360 { "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 +02008361
Willy Tarreauc4262962010-05-10 23:42:40 +02008362 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008363 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8364 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8365 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8366 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8367 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8368 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8369 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
8370 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008371 { "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 +02008372
Willy Tarreauc4262962010-05-10 23:42:40 +02008373 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008374 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8375 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8376 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8377 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8378 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8379 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008380
Willy Tarreauf3d25982007-05-08 22:45:09 +02008381#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008382 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8383 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8384 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8385 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8386 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8387 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8388 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8389
Willy Tarreau8797c062007-05-07 00:55:35 +02008390 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8391 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8392 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8393 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8394 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8395 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8396 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8397 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008398#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008399
Willy Tarreau7f18e522010-10-22 20:04:13 +02008400 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8401 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8402 { "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 +02008403 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008404}};
8405
Willy Tarreau4a568972010-05-12 08:08:50 +02008406/************************************************************************/
8407/* The code below is dedicated to pattern fetching and matching */
8408/************************************************************************/
8409
8410/* extract the IP address from the last occurrence of specified header. Note
8411 * that we should normally first extract the string then convert it to IP,
8412 * but right now we have all the functions to do this seemlessly, and we will
8413 * be able to change that later without touching the configuration.
8414 */
8415static int
8416pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02008417 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008418{
8419 struct http_txn *txn = l7;
8420
Emeric Brun485479d2010-09-23 18:02:19 +02008421 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 +02008422 return data->ip.s_addr != 0;
8423}
8424
David Cournapeau16023ee2010-12-23 20:55:41 +09008425/*
8426 * Given a path string and its length, find the position of beginning of the
8427 * query string. Returns NULL if no query string is found in the path.
8428 *
8429 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8430 *
8431 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8432 */
8433static inline char *find_query_string(char *path, size_t path_l)
8434{
8435 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008436
David Cournapeau16023ee2010-12-23 20:55:41 +09008437 p = memchr(path, '?', path_l);
8438 return p ? p + 1 : NULL;
8439}
8440
8441static inline int is_param_delimiter(char c)
8442{
8443 return c == '&' || c == ';';
8444}
8445
8446/*
8447 * Given a url parameter, find the starting position of the first occurence,
8448 * or NULL if the parameter is not found.
8449 *
8450 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8451 * the function will return query_string+8.
8452 */
8453static char*
8454find_url_param_pos(char* query_string, size_t query_string_l,
8455 char* url_param_name, size_t url_param_name_l)
8456{
8457 char *pos, *last;
8458
8459 pos = query_string;
8460 last = query_string + query_string_l - url_param_name_l - 1;
8461
8462 while (pos <= last) {
8463 if (pos[url_param_name_l] == '=') {
8464 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8465 return pos;
8466 pos += url_param_name_l + 1;
8467 }
8468 while (pos <= last && !is_param_delimiter(*pos))
8469 pos++;
8470 pos++;
8471 }
8472 return NULL;
8473}
8474
8475/*
8476 * Given a url parameter name, returns its value and size into *value and
8477 * *value_l respectively, and returns non-zero. If the parameter is not found,
8478 * zero is returned and value/value_l are not touched.
8479 */
8480static int
8481find_url_param_value(char* path, size_t path_l,
8482 char* url_param_name, size_t url_param_name_l,
8483 char** value, size_t* value_l)
8484{
8485 char *query_string, *qs_end;
8486 char *arg_start;
8487 char *value_start, *value_end;
8488
8489 query_string = find_query_string(path, path_l);
8490 if (!query_string)
8491 return 0;
8492
8493 qs_end = path + path_l;
8494 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8495 url_param_name, url_param_name_l);
8496 if (!arg_start)
8497 return 0;
8498
8499 value_start = arg_start + url_param_name_l + 1;
8500 value_end = value_start;
8501
8502 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8503 value_end++;
8504
8505 *value = value_start;
8506 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008507 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008508}
8509
8510static int
8511pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8512 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8513{
8514 struct http_txn *txn = l7;
8515 struct http_msg *msg = &txn->req;
8516 char *url_param_value;
8517 size_t url_param_value_l;
8518
8519 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8520 arg_p->data.str.str, arg_p->data.str.len,
8521 &url_param_value, &url_param_value_l))
8522 return 0;
8523
8524 data->str.str = url_param_value;
8525 data->str.len = url_param_value_l;
8526 return 1;
8527}
8528
Emeric Brun485479d2010-09-23 18:02:19 +02008529
Willy Tarreau4a568972010-05-12 08:08:50 +02008530/************************************************************************/
8531/* All supported keywords must be declared here. */
8532/************************************************************************/
8533/* Note: must not be declared <const> as its list will be overwritten */
8534static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Emeric Brun485479d2010-09-23 18:02:19 +02008535 { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008536 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Emeric Brun485479d2010-09-23 18:02:19 +02008537 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008538}};
8539
Willy Tarreau8797c062007-05-07 00:55:35 +02008540
8541__attribute__((constructor))
8542static void __http_protocol_init(void)
8543{
8544 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008545 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008546}
8547
8548
Willy Tarreau58f10d72006-12-04 02:26:12 +01008549/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008550 * Local variables:
8551 * c-indent-level: 8
8552 * c-basic-offset: 8
8553 * End:
8554 */