blob: c00aced6cc92f645e21b62eb12a29a806a1cfd6a [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 Tarreau610ecce2010-01-04 21:15:02 +01004413 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004414 /* we may have some data pending */
Willy Tarreau124d9912011-03-01 20:30:48 +01004415 if (msg->chunk_len || msg->som != msg->sov) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004416 int bytes = msg->sov - msg->som;
4417 if (bytes < 0) /* sov may have wrapped at the end */
4418 bytes += req->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01004419 buffer_forward(req, bytes + msg->chunk_len);
4420 msg->chunk_len = 0; /* don't forward that again */
Willy Tarreau638cd022010-01-03 07:42:04 +01004421 msg->som = msg->sov;
4422 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004423
Willy Tarreaucaabe412010-01-03 23:08:28 +01004424 if (msg->msg_state == HTTP_MSG_DATA) {
4425 /* must still forward */
4426 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004427 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004428
4429 /* nothing left to forward */
4430 if (txn->flags & TX_REQ_TE_CHNK)
4431 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004432 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004433 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004434 }
4435 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004436 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004437 * set ->sov and ->lr to point to the body and switch to DATA or
4438 * TRAILERS state.
4439 */
4440 int ret = http_parse_chunk_size(req, msg);
4441
4442 if (!ret)
4443 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004444 else if (ret < 0) {
4445 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004446 if (msg->err_pos >= 0)
4447 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004448 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004449 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004450 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreau57f5c122010-12-02 00:37:14 +01004451 /* Don't set a PUSH at the end of that chunk if it's not the last one */
4452 if (msg->msg_state == HTTP_MSG_DATA)
4453 req->flags |= BF_EXPECT_MORE;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004454 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004455 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4456 /* we want the CRLF after the data */
4457 int ret;
4458
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004459 req->lr = req->w + req->send_max;
4460 if (req->lr >= req->data + req->size)
4461 req->lr -= req->size;
4462
Willy Tarreaud98cf932009-12-27 22:54:55 +01004463 ret = http_skip_chunk_crlf(req, msg);
4464
4465 if (ret == 0)
4466 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004467 else if (ret < 0) {
4468 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004469 if (msg->err_pos >= 0)
4470 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004471 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004472 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004473 /* we're in MSG_CHUNK_SIZE now */
4474 }
4475 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4476 int ret = http_forward_trailers(req, msg);
4477
4478 if (ret == 0)
4479 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004480 else if (ret < 0) {
4481 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004482 if (msg->err_pos >= 0)
4483 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004484 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004485 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004486 /* we're in HTTP_MSG_DONE now */
4487 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004488 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004489 int old_state = msg->msg_state;
4490
Willy Tarreau610ecce2010-01-04 21:15:02 +01004491 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004492 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004493 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4494 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4495 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004496 if (http_resync_states(s)) {
4497 /* some state changes occurred, maybe the analyser
4498 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004499 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004500 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4501 if (req->flags & BF_SHUTW) {
4502 /* request errors are most likely due to
4503 * the server aborting the transfer.
4504 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004505 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004506 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004507 if (msg->err_pos >= 0)
4508 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004509 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004510 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004511 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004512 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004513
4514 /* If "option abortonclose" is set on the backend, we
4515 * want to monitor the client's connection and forward
4516 * any shutdown notification to the server, which will
4517 * decide whether to close or to go on processing the
4518 * request.
4519 */
4520 if (s->be->options & PR_O_ABRT_CLOSE) {
4521 buffer_auto_read(req);
4522 buffer_auto_close(req);
4523 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004524 else if (s->txn.meth == HTTP_METH_POST) {
4525 /* POST requests may require to read extra CRLF
4526 * sent by broken browsers and which could cause
4527 * an RST to be sent upon close on some systems
4528 * (eg: Linux).
4529 */
4530 buffer_auto_read(req);
4531 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004532
Willy Tarreau610ecce2010-01-04 21:15:02 +01004533 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004534 }
4535 }
4536
Willy Tarreaud98cf932009-12-27 22:54:55 +01004537 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004538 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004539 if (req->flags & BF_SHUTR) {
4540 if (!(s->flags & SN_ERR_MASK))
4541 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004542 if (!(s->flags & SN_FINST_MASK)) {
4543 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4544 s->flags |= SN_FINST_H;
4545 else
4546 s->flags |= SN_FINST_D;
4547 }
4548
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004549 s->fe->fe_counters.cli_aborts++;
4550 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004551 if (target_srv(&s->target))
4552 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004553
4554 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004555 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004556
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004557 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004558 if (req->flags & BF_SHUTW)
4559 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004560
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004561 /* When TE: chunked is used, we need to get there again to parse remaining
4562 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4563 */
4564 if (txn->flags & TX_REQ_TE_CHNK)
4565 buffer_dont_close(req);
4566
Willy Tarreau610ecce2010-01-04 21:15:02 +01004567 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004568 return 0;
4569
Willy Tarreaud98cf932009-12-27 22:54:55 +01004570 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004571 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004572 if (s->listener->counters)
4573 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004574 return_bad_req_stats_ok:
4575 txn->req.msg_state = HTTP_MSG_ERROR;
4576 if (txn->status) {
4577 /* Note: we don't send any error if some data were already sent */
4578 stream_int_retnclose(req->prod, NULL);
4579 } else {
4580 txn->status = 400;
4581 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4582 }
4583 req->analysers = 0;
4584 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004585
4586 if (!(s->flags & SN_ERR_MASK))
4587 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004588 if (!(s->flags & SN_FINST_MASK)) {
4589 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4590 s->flags |= SN_FINST_H;
4591 else
4592 s->flags |= SN_FINST_D;
4593 }
4594 return 0;
4595
4596 aborted_xfer:
4597 txn->req.msg_state = HTTP_MSG_ERROR;
4598 if (txn->status) {
4599 /* Note: we don't send any error if some data were already sent */
4600 stream_int_retnclose(req->prod, NULL);
4601 } else {
4602 txn->status = 502;
4603 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4604 }
4605 req->analysers = 0;
4606 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4607
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004608 s->fe->fe_counters.srv_aborts++;
4609 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004610 if (target_srv(&s->target))
4611 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004612
4613 if (!(s->flags & SN_ERR_MASK))
4614 s->flags |= SN_ERR_SRVCL;
4615 if (!(s->flags & SN_FINST_MASK)) {
4616 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4617 s->flags |= SN_FINST_H;
4618 else
4619 s->flags |= SN_FINST_D;
4620 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004621 return 0;
4622}
4623
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004624/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4625 * processing can continue on next analysers, or zero if it either needs more
4626 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4627 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4628 * when it has nothing left to do, and may remove any analyser when it wants to
4629 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004630 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004631int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004632{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004633 struct http_txn *txn = &s->txn;
4634 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004635 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004636 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004637 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004638 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004639
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004640 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 +02004641 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004642 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004643 rep,
4644 rep->rex, rep->wex,
4645 rep->flags,
4646 rep->l,
4647 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004648
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004649 /*
4650 * Now parse the partial (or complete) lines.
4651 * We will check the response syntax, and also join multi-line
4652 * headers. An index of all the lines will be elaborated while
4653 * parsing.
4654 *
4655 * For the parsing, we use a 28 states FSM.
4656 *
4657 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004658 * rep->data + msg->som = beginning of response
4659 * rep->data + msg->eoh = end of processed headers / start of current one
4660 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004661 * rep->lr = first non-visited byte
4662 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004663 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004664 */
4665
Willy Tarreau83e3af02009-12-28 17:39:57 +01004666 /* There's a protected area at the end of the buffer for rewriting
4667 * purposes. We don't want to start to parse the request if the
4668 * protected area is affected, because we may have to move processed
4669 * data later, which is much more complicated.
4670 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004671 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4672 if (unlikely((rep->flags & BF_FULL) ||
4673 rep->r < rep->lr ||
4674 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4675 if (rep->send_max) {
4676 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004677 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4678 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004679 buffer_dont_close(rep);
4680 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4681 return 0;
4682 }
4683 if (rep->l <= rep->size - global.tune.maxrewrite)
4684 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004685 }
4686
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004687 if (likely(rep->lr < rep->r))
4688 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004689 }
4690
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004691 /* 1: we might have to print this header in debug mode */
4692 if (unlikely((global.mode & MODE_DEBUG) &&
4693 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004694 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004695 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004696 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004697
Willy Tarreau663308b2010-06-07 14:06:08 +02004698 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004699 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004700 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004701
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004702 sol += hdr_idx_first_pos(&txn->hdr_idx);
4703 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004704
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004705 while (cur_idx) {
4706 eol = sol + txn->hdr_idx.v[cur_idx].len;
4707 debug_hdr("srvhdr", s, sol, eol);
4708 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4709 cur_idx = txn->hdr_idx.v[cur_idx].next;
4710 }
4711 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004712
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004713 /*
4714 * Now we quickly check if we have found a full valid response.
4715 * If not so, we check the FD and buffer states before leaving.
4716 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004717 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004718 * responses are checked first.
4719 *
4720 * Depending on whether the client is still there or not, we
4721 * may send an error response back or not. Note that normally
4722 * we should only check for HTTP status there, and check I/O
4723 * errors somewhere else.
4724 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004725
Willy Tarreau655dce92009-11-08 13:10:58 +01004726 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004727 /* Invalid response */
4728 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4729 /* we detected a parsing error. We want to archive this response
4730 * in the dedicated proxy area for later troubleshooting.
4731 */
4732 hdr_response_bad:
4733 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004734 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004735
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004736 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004737 if (target_srv(&s->target)) {
4738 target_srv(&s->target)->counters.failed_resp++;
4739 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004740 }
Willy Tarreau64648412010-03-05 10:41:54 +01004741 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004742 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004743 rep->analysers = 0;
4744 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004745 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004746 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004747 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4748
4749 if (!(s->flags & SN_ERR_MASK))
4750 s->flags |= SN_ERR_PRXCOND;
4751 if (!(s->flags & SN_FINST_MASK))
4752 s->flags |= SN_FINST_H;
4753
4754 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004755 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004756
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004757 /* too large response does not fit in buffer. */
4758 else if (rep->flags & BF_FULL) {
4759 goto hdr_response_bad;
4760 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004761
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004762 /* read error */
4763 else if (rep->flags & BF_READ_ERROR) {
4764 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004765 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004766
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004767 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004768 if (target_srv(&s->target)) {
4769 target_srv(&s->target)->counters.failed_resp++;
4770 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004771 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004772
Willy Tarreau90deb182010-01-07 00:20:41 +01004773 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004774 rep->analysers = 0;
4775 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004776 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004777 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004778 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004779
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004780 if (!(s->flags & SN_ERR_MASK))
4781 s->flags |= SN_ERR_SRVCL;
4782 if (!(s->flags & SN_FINST_MASK))
4783 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004784 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004785 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004786
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004787 /* read timeout : return a 504 to the client. */
4788 else if (rep->flags & BF_READ_TIMEOUT) {
4789 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004790 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004791
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004792 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004793 if (target_srv(&s->target)) {
4794 target_srv(&s->target)->counters.failed_resp++;
4795 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004796 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004797
Willy Tarreau90deb182010-01-07 00:20:41 +01004798 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004799 rep->analysers = 0;
4800 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004801 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004802 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004803 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004804
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004805 if (!(s->flags & SN_ERR_MASK))
4806 s->flags |= SN_ERR_SRVTO;
4807 if (!(s->flags & SN_FINST_MASK))
4808 s->flags |= SN_FINST_H;
4809 return 0;
4810 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004811
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004812 /* close from server */
4813 else if (rep->flags & BF_SHUTR) {
4814 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004815 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004816
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004817 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004818 if (target_srv(&s->target)) {
4819 target_srv(&s->target)->counters.failed_resp++;
4820 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004821 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004822
Willy Tarreau90deb182010-01-07 00:20:41 +01004823 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004824 rep->analysers = 0;
4825 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004826 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004827 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004828 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004829
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004830 if (!(s->flags & SN_ERR_MASK))
4831 s->flags |= SN_ERR_SRVCL;
4832 if (!(s->flags & SN_FINST_MASK))
4833 s->flags |= SN_FINST_H;
4834 return 0;
4835 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004836
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004837 /* write error to client (we don't send any message then) */
4838 else if (rep->flags & BF_WRITE_ERROR) {
4839 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004840 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004841
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004842 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004843 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004844 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004845
4846 if (!(s->flags & SN_ERR_MASK))
4847 s->flags |= SN_ERR_CLICL;
4848 if (!(s->flags & SN_FINST_MASK))
4849 s->flags |= SN_FINST_H;
4850
4851 /* process_session() will take care of the error */
4852 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004853 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004854
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004855 buffer_dont_close(rep);
4856 return 0;
4857 }
4858
4859 /* More interesting part now : we know that we have a complete
4860 * response which at least looks like HTTP. We have an indicator
4861 * of each header's length, so we can parse them quickly.
4862 */
4863
4864 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004865 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004866
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004867 /*
4868 * 1: get the status code
4869 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004870 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004871 if (n < 1 || n > 5)
4872 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004873 /* when the client triggers a 4xx from the server, it's most often due
4874 * to a missing object or permission. These events should be tracked
4875 * because if they happen often, it may indicate a brute force or a
4876 * vulnerability scan.
4877 */
4878 if (n == 4)
4879 session_inc_http_err_ctr(s);
4880
Willy Tarreau827aee92011-03-10 16:55:02 +01004881 if (target_srv(&s->target))
4882 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004883
Willy Tarreau5b154472009-12-21 20:11:07 +01004884 /* check if the response is HTTP/1.1 or above */
4885 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004886 ((msg->sol[5] > '1') ||
4887 ((msg->sol[5] == '1') &&
4888 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004889 txn->flags |= TX_RES_VER_11;
4890
4891 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004892 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 +01004893
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004894 /* transfer length unknown*/
4895 txn->flags &= ~TX_RES_XFER_LEN;
4896
Willy Tarreau962c3f42010-01-10 00:15:35 +01004897 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004898
Willy Tarreau39650402010-03-15 19:44:39 +01004899 /* Adjust server's health based on status code. Note: status codes 501
4900 * and 505 are triggered on demand by client request, so we must not
4901 * count them as server failures.
4902 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004903 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004904 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004905 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004906 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004907 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004908 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004909
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004910 /*
4911 * 2: check for cacheability.
4912 */
4913
4914 switch (txn->status) {
4915 case 200:
4916 case 203:
4917 case 206:
4918 case 300:
4919 case 301:
4920 case 410:
4921 /* RFC2616 @13.4:
4922 * "A response received with a status code of
4923 * 200, 203, 206, 300, 301 or 410 MAY be stored
4924 * by a cache (...) unless a cache-control
4925 * directive prohibits caching."
4926 *
4927 * RFC2616 @9.5: POST method :
4928 * "Responses to this method are not cacheable,
4929 * unless the response includes appropriate
4930 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004931 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004932 if (likely(txn->meth != HTTP_METH_POST) &&
4933 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4934 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4935 break;
4936 default:
4937 break;
4938 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004939
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004940 /*
4941 * 3: we may need to capture headers
4942 */
4943 s->logs.logwait &= ~LW_RESP;
4944 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004945 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004946 txn->rsp.cap, s->fe->rsp_cap);
4947
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004948 /* 4: determine the transfer-length.
4949 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4950 * the presence of a message-body in a RESPONSE and its transfer length
4951 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004952 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004953 * All responses to the HEAD request method MUST NOT include a
4954 * message-body, even though the presence of entity-header fields
4955 * might lead one to believe they do. All 1xx (informational), 204
4956 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4957 * message-body. All other responses do include a message-body,
4958 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004959 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004960 * 1. Any response which "MUST NOT" include a message-body (such as the
4961 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4962 * always terminated by the first empty line after the header fields,
4963 * regardless of the entity-header fields present in the message.
4964 *
4965 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4966 * the "chunked" transfer-coding (Section 6.2) is used, the
4967 * transfer-length is defined by the use of this transfer-coding.
4968 * If a Transfer-Encoding header field is present and the "chunked"
4969 * transfer-coding is not present, the transfer-length is defined by
4970 * the sender closing the connection.
4971 *
4972 * 3. If a Content-Length header field is present, its decimal value in
4973 * OCTETs represents both the entity-length and the transfer-length.
4974 * If a message is received with both a Transfer-Encoding header
4975 * field and a Content-Length header field, the latter MUST be ignored.
4976 *
4977 * 4. If the message uses the media type "multipart/byteranges", and
4978 * the transfer-length is not otherwise specified, then this self-
4979 * delimiting media type defines the transfer-length. This media
4980 * type MUST NOT be used unless the sender knows that the recipient
4981 * can parse it; the presence in a request of a Range header with
4982 * multiple byte-range specifiers from a 1.1 client implies that the
4983 * client can parse multipart/byteranges responses.
4984 *
4985 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004986 */
4987
4988 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004989 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004990 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004991 * FIXME: should we parse anyway and return an error on chunked encoding ?
4992 */
4993 if (txn->meth == HTTP_METH_HEAD ||
4994 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004995 txn->status == 204 || txn->status == 304) {
4996 txn->flags |= TX_RES_XFER_LEN;
4997 goto skip_content_length;
4998 }
4999
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005000 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005001 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01005002 while ((txn->flags & TX_RES_VER_11) &&
5003 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005004 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
5005 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5006 else if (txn->flags & TX_RES_TE_CHNK) {
5007 /* bad transfer-encoding (chunked followed by something else) */
5008 use_close_only = 1;
5009 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5010 break;
5011 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005012 }
5013
5014 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5015 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005016 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005017 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
5018 signed long long cl;
5019
5020 if (!ctx.vlen)
5021 goto hdr_response_bad;
5022
5023 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
5024 goto hdr_response_bad; /* parse failure */
5025
5026 if (cl < 0)
5027 goto hdr_response_bad;
5028
Willy Tarreau124d9912011-03-01 20:30:48 +01005029 if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl))
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005030 goto hdr_response_bad; /* already specified, was different */
5031
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005032 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005033 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005034 }
5035
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005036 /* FIXME: we should also implement the multipart/byterange method.
5037 * For now on, we resort to close mode in this case (unknown length).
5038 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005039skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005040
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005041 /* end of job, return OK */
5042 rep->analysers &= ~an_bit;
5043 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01005044 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005045 return 1;
5046}
5047
5048/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005049 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5050 * and updates t->rep->analysers. It might make sense to explode it into several
5051 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005052 */
5053int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
5054{
5055 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005056 struct http_msg *msg = &txn->rsp;
5057 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005058 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005059
5060 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
5061 now_ms, __FUNCTION__,
5062 t,
5063 rep,
5064 rep->rex, rep->wex,
5065 rep->flags,
5066 rep->l,
5067 rep->analysers);
5068
Willy Tarreau655dce92009-11-08 13:10:58 +01005069 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005070 return 0;
5071
5072 rep->analysers &= ~an_bit;
5073 rep->analyse_exp = TICK_ETERNITY;
5074
Willy Tarreau5b154472009-12-21 20:11:07 +01005075 /* Now we have to check if we need to modify the Connection header.
5076 * This is more difficult on the response than it is on the request,
5077 * because we can have two different HTTP versions and we don't know
5078 * how the client will interprete a response. For instance, let's say
5079 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5080 * HTTP/1.1 response without any header. Maybe it will bound itself to
5081 * HTTP/1.0 because it only knows about it, and will consider the lack
5082 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5083 * the lack of header as a keep-alive. Thus we will use two flags
5084 * indicating how a request MAY be understood by the client. In case
5085 * of multiple possibilities, we'll fix the header to be explicit. If
5086 * ambiguous cases such as both close and keepalive are seen, then we
5087 * will fall back to explicit close. Note that we won't take risks with
5088 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005089 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005090 */
5091
Willy Tarreaudc008c52010-02-01 16:20:08 +01005092 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5093 txn->status == 101)) {
5094 /* Either we've established an explicit tunnel, or we're
5095 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005096 * to understand the next protocols. We have to switch to tunnel
5097 * mode, so that we transfer the request and responses then let
5098 * this protocol pass unmodified. When we later implement specific
5099 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005100 * header which contains information about that protocol for
5101 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005102 */
5103 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5104 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005105 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5106 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5107 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005108 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005109
Willy Tarreau60466522010-01-18 19:08:45 +01005110 /* on unknown transfer length, we must close */
5111 if (!(txn->flags & TX_RES_XFER_LEN) &&
5112 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5113 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005114
Willy Tarreau60466522010-01-18 19:08:45 +01005115 /* now adjust header transformations depending on current state */
5116 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5117 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5118 to_del |= 2; /* remove "keep-alive" on any response */
5119 if (!(txn->flags & TX_RES_VER_11))
5120 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005121 }
Willy Tarreau60466522010-01-18 19:08:45 +01005122 else { /* SCL / KAL */
5123 to_del |= 1; /* remove "close" on any response */
5124 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
5125 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005126 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005127
Willy Tarreau60466522010-01-18 19:08:45 +01005128 /* Parse and remove some headers from the connection header */
5129 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005130
Willy Tarreau60466522010-01-18 19:08:45 +01005131 /* Some keep-alive responses are converted to Server-close if
5132 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005133 */
Willy Tarreau60466522010-01-18 19:08:45 +01005134 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5135 if ((txn->flags & TX_HDR_CONN_CLO) ||
5136 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
5137 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005138 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005139 }
5140
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005141 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005142 /*
5143 * 3: we will have to evaluate the filters.
5144 * As opposed to version 1.2, now they will be evaluated in the
5145 * filters order and not in the header order. This means that
5146 * each filter has to be validated among all headers.
5147 *
5148 * Filters are tried with ->be first, then with ->fe if it is
5149 * different from ->be.
5150 */
5151
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005152 cur_proxy = t->be;
5153 while (1) {
5154 struct proxy *rule_set = cur_proxy;
5155
5156 /* try headers filters */
5157 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005158 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005159 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005160 if (target_srv(&t->target)) {
5161 target_srv(&t->target)->counters.failed_resp++;
5162 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005163 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005164 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005165 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005166 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005167 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005168 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01005169 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005170 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005171 if (!(t->flags & SN_ERR_MASK))
5172 t->flags |= SN_ERR_PRXCOND;
5173 if (!(t->flags & SN_FINST_MASK))
5174 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005175 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005176 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005177 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005178
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005179 /* has the response been denied ? */
5180 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005181 if (target_srv(&t->target))
5182 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005183
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005184 t->be->be_counters.denied_resp++;
5185 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005186 if (t->listener->counters)
5187 t->listener->counters->denied_resp++;
5188
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005189 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005190 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005191
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005192 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005193 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005194 if (txn->status < 200)
5195 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005196 if (wl->cond) {
5197 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5198 ret = acl_pass(ret);
5199 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5200 ret = !ret;
5201 if (!ret)
5202 continue;
5203 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005204 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005205 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005206 }
5207
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005208 /* check whether we're already working on the frontend */
5209 if (cur_proxy == t->fe)
5210 break;
5211 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005212 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005213
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005214 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005215 * We may be facing a 100-continue response, in which case this
5216 * is not the right response, and we're waiting for the next one.
5217 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005218 * next one.
5219 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005220 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005221 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005222 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005223 msg->msg_state = HTTP_MSG_RPBEFORE;
5224 txn->status = 0;
5225 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5226 return 1;
5227 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005228 else if (unlikely(txn->status < 200))
5229 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005230
5231 /* we don't have any 1xx status code now */
5232
5233 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005234 * 4: check for server cookie.
5235 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005236 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5237 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005238 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005239
Willy Tarreaubaaee002006-06-26 02:48:02 +02005240
Willy Tarreaua15645d2007-03-18 16:22:39 +01005241 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005242 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005243 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005244 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005245 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005246
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005247 /*
5248 * 6: add server cookie in the response if needed
5249 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005250 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005251 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005252 (!(t->flags & SN_DIRECT) ||
5253 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5254 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5255 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5256 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005257 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5258 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005259 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005260 /* the server is known, it's not the one the client requested, or the
5261 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005262 * insert a set-cookie here, except if we want to insert only on POST
5263 * requests and this one isn't. Note that servers which don't have cookies
5264 * (eg: some backup servers) will return a full cookie removal request.
5265 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005266 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005267 len = sprintf(trash,
5268 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5269 t->be->cookie_name);
5270 }
5271 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005272 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005273
5274 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5275 /* emit last_date, which is mandatory */
5276 trash[len++] = COOKIE_DELIM_DATE;
5277 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5278 if (t->be->cookie_maxlife) {
5279 /* emit first_date, which is either the original one or
5280 * the current date.
5281 */
5282 trash[len++] = COOKIE_DELIM_DATE;
5283 s30tob64(txn->cookie_first_date ?
5284 txn->cookie_first_date >> 2 :
5285 (date.tv_sec+3) >> 2, trash + len);
5286 len += 5;
5287 }
5288 }
5289 len += sprintf(trash + len, "; path=/");
5290 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005291
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005292 if (t->be->cookie_domain)
5293 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005294
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005295 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005296 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005297 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005298
Willy Tarreauf1348312010-10-07 15:54:11 +02005299 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005300 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005301 /* the server did not change, only the date was updated */
5302 txn->flags |= TX_SCK_UPDATED;
5303 else
5304 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005305
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005306 /* Here, we will tell an eventual cache on the client side that we don't
5307 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5308 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5309 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5310 */
5311 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005312
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005313 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5314
5315 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005316 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005317 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005318 }
5319 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005320
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005321 /*
5322 * 7: check if result will be cacheable with a cookie.
5323 * We'll block the response if security checks have caught
5324 * nasty things such as a cacheable cookie.
5325 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005326 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5327 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005328 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005329
5330 /* we're in presence of a cacheable response containing
5331 * a set-cookie header. We'll block it as requested by
5332 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005333 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005334 if (target_srv(&t->target))
5335 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005336
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005337 t->be->be_counters.denied_resp++;
5338 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005339 if (t->listener->counters)
5340 t->listener->counters->denied_resp++;
5341
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005342 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005343 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005344 send_log(t->be, LOG_ALERT,
5345 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005346 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005347 goto return_srv_prx_502;
5348 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005349
5350 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005351 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005352 */
Willy Tarreau60466522010-01-18 19:08:45 +01005353 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5354 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5355 unsigned int want_flags = 0;
5356
5357 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5358 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5359 /* we want a keep-alive response here. Keep-alive header
5360 * required if either side is not 1.1.
5361 */
5362 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5363 want_flags |= TX_CON_KAL_SET;
5364 }
5365 else {
5366 /* we want a close response here. Close header required if
5367 * the server is 1.1, regardless of the client.
5368 */
5369 if (txn->flags & TX_RES_VER_11)
5370 want_flags |= TX_CON_CLO_SET;
5371 }
5372
5373 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5374 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005375 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005376
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005377 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005378 if ((txn->flags & TX_RES_XFER_LEN) ||
5379 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005380 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005381
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005382 /*************************************************************
5383 * OK, that's finished for the headers. We have done what we *
5384 * could. Let's switch to the DATA state. *
5385 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005386
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005387 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005388
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005389 /* if the user wants to log as soon as possible, without counting
5390 * bytes from the server, then this is the right moment. We have
5391 * to temporarily assign bytes_out to log what we currently have.
5392 */
5393 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5394 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5395 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005396 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005397 t->logs.bytes_out = 0;
5398 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005399
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005400 /* Note: we must not try to cheat by jumping directly to DATA,
5401 * otherwise we would not let the client side wake up.
5402 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005403
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005404 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005405 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005406 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005407}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005408
Willy Tarreaud98cf932009-12-27 22:54:55 +01005409/* This function is an analyser which forwards response body (including chunk
5410 * sizes if any). It is called as soon as we must forward, even if we forward
5411 * zero byte. The only situation where it must not be called is when we're in
5412 * tunnel mode and we want to forward till the close. It's used both to forward
5413 * remaining data and to resync after end of body. It expects the msg_state to
5414 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5415 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005416 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005417 * bytes of pending data + the headers if not already done (between som and sov).
5418 * It eventually adjusts som to match sov after the data in between have been sent.
5419 */
5420int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5421{
5422 struct http_txn *txn = &s->txn;
5423 struct http_msg *msg = &s->txn.rsp;
5424
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005425 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5426 return 0;
5427
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005428 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005429 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005430 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005431 /* Output closed while we were sending data. We must abort and
5432 * wake the other side up.
5433 */
5434 msg->msg_state = HTTP_MSG_ERROR;
5435 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005436 return 1;
5437 }
5438
Willy Tarreau4fe41902010-06-07 22:27:41 +02005439 /* in most states, we should abort in case of early close */
5440 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005441
Willy Tarreaud98cf932009-12-27 22:54:55 +01005442 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5443 /* we have msg->col and msg->sov which both point to the first
5444 * byte of message body. msg->som still points to the beginning
5445 * of the message. We must save the body in req->lr because it
5446 * survives buffer re-alignments.
5447 */
5448 res->lr = res->data + msg->sov;
5449 if (txn->flags & TX_RES_TE_CHNK)
5450 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5451 else {
5452 msg->msg_state = HTTP_MSG_DATA;
5453 }
5454 }
5455
Willy Tarreaud98cf932009-12-27 22:54:55 +01005456 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005457 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005458 /* we may have some data pending */
Willy Tarreau124d9912011-03-01 20:30:48 +01005459 if (msg->chunk_len || msg->som != msg->sov) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005460 int bytes = msg->sov - msg->som;
5461 if (bytes < 0) /* sov may have wrapped at the end */
5462 bytes += res->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01005463 buffer_forward(res, bytes + msg->chunk_len);
5464 msg->chunk_len = 0; /* don't forward that again */
Willy Tarreau638cd022010-01-03 07:42:04 +01005465 msg->som = msg->sov;
5466 }
5467
Willy Tarreaucaabe412010-01-03 23:08:28 +01005468 if (msg->msg_state == HTTP_MSG_DATA) {
5469 /* must still forward */
5470 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005471 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005472
5473 /* nothing left to forward */
5474 if (txn->flags & TX_RES_TE_CHNK)
5475 msg->msg_state = HTTP_MSG_DATA_CRLF;
5476 else
5477 msg->msg_state = HTTP_MSG_DONE;
5478 }
5479 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005480 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005481 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5482 */
5483 int ret = http_parse_chunk_size(res, msg);
5484
5485 if (!ret)
5486 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005487 else if (ret < 0) {
5488 if (msg->err_pos >= 0)
5489 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005490 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005491 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005492 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreau57f5c122010-12-02 00:37:14 +01005493 /* Don't set a PUSH at the end of that chunk if it's not the last one */
5494 if (msg->msg_state == HTTP_MSG_DATA)
5495 res->flags |= BF_EXPECT_MORE;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005496 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005497 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5498 /* we want the CRLF after the data */
5499 int ret;
5500
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005501 res->lr = res->w + res->send_max;
5502 if (res->lr >= res->data + res->size)
5503 res->lr -= res->size;
5504
Willy Tarreaud98cf932009-12-27 22:54:55 +01005505 ret = http_skip_chunk_crlf(res, msg);
5506
5507 if (!ret)
5508 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005509 else if (ret < 0) {
5510 if (msg->err_pos >= 0)
5511 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005512 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005513 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005514 /* we're in MSG_CHUNK_SIZE now */
5515 }
5516 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5517 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005518
Willy Tarreaud98cf932009-12-27 22:54:55 +01005519 if (ret == 0)
5520 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005521 else if (ret < 0) {
5522 if (msg->err_pos >= 0)
5523 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005524 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005525 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005526 /* we're in HTTP_MSG_DONE now */
5527 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005528 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005529 int old_state = msg->msg_state;
5530
Willy Tarreau610ecce2010-01-04 21:15:02 +01005531 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005532 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005533 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5534 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5535 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005536 if (http_resync_states(s)) {
5537 http_silent_debug(__LINE__, s);
5538 /* some state changes occurred, maybe the analyser
5539 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005540 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005541 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5542 if (res->flags & BF_SHUTW) {
5543 /* response errors are most likely due to
5544 * the client aborting the transfer.
5545 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005546 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005547 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005548 if (msg->err_pos >= 0)
5549 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005550 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005551 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005552 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005553 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005554 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005555 }
5556 }
5557
Willy Tarreaud98cf932009-12-27 22:54:55 +01005558 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005559 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005560 if (res->flags & BF_SHUTR) {
5561 if (!(s->flags & SN_ERR_MASK))
5562 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005563 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005564 if (target_srv(&s->target))
5565 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005566 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005567 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005568
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005569 if (res->flags & BF_SHUTW)
5570 goto aborted_xfer;
5571
Willy Tarreau40dba092010-03-04 18:14:51 +01005572 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005573 if (!s->req->analysers)
5574 goto return_bad_res;
5575
Willy Tarreaud98cf932009-12-27 22:54:55 +01005576 /* forward the chunk size as well as any pending data */
Willy Tarreau124d9912011-03-01 20:30:48 +01005577 if (msg->chunk_len || msg->som != msg->sov) {
Willy Tarreauacd20f82011-03-01 20:04:36 +01005578 int bytes = msg->sov - msg->som;
5579 if (bytes < 0) /* sov may have wrapped at the end */
5580 bytes += res->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01005581 buffer_forward(res, msg->sov - msg->som + msg->chunk_len);
5582 msg->chunk_len = 0; /* don't forward that again */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005583 msg->som = msg->sov;
5584 }
5585
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005586 /* When TE: chunked is used, we need to get there again to parse remaining
5587 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5588 * Similarly, with keep-alive on the client side, we don't want to forward a
5589 * close.
5590 */
5591 if ((txn->flags & TX_RES_TE_CHNK) ||
5592 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5593 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5594 buffer_dont_close(res);
5595
Willy Tarreaud98cf932009-12-27 22:54:55 +01005596 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005597 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005598 return 0;
5599
Willy Tarreau40dba092010-03-04 18:14:51 +01005600 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005601 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005602 if (target_srv(&s->target))
5603 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005604
5605 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005606 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005607 /* don't send any error message as we're in the body */
5608 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005609 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005610 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005611 if (target_srv(&s->target))
5612 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005613
5614 if (!(s->flags & SN_ERR_MASK))
5615 s->flags |= SN_ERR_PRXCOND;
5616 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005617 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005618 return 0;
5619
5620 aborted_xfer:
5621 txn->rsp.msg_state = HTTP_MSG_ERROR;
5622 /* don't send any error message as we're in the body */
5623 stream_int_retnclose(res->cons, NULL);
5624 res->analysers = 0;
5625 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5626
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005627 s->fe->fe_counters.cli_aborts++;
5628 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005629 if (target_srv(&s->target))
5630 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005631
5632 if (!(s->flags & SN_ERR_MASK))
5633 s->flags |= SN_ERR_CLICL;
5634 if (!(s->flags & SN_FINST_MASK))
5635 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005636 return 0;
5637}
5638
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005639/* Iterate the same filter through all request headers.
5640 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005641 * Since it can manage the switch to another backend, it updates the per-proxy
5642 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005643 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005644int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005645{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005646 char term;
5647 char *cur_ptr, *cur_end, *cur_next;
5648 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005649 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005650 struct hdr_idx_elem *cur_hdr;
5651 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005653 last_hdr = 0;
5654
Willy Tarreau962c3f42010-01-10 00:15:35 +01005655 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005656 old_idx = 0;
5657
5658 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005659 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005660 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005661 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005662 (exp->action == ACT_ALLOW ||
5663 exp->action == ACT_DENY ||
5664 exp->action == ACT_TARPIT))
5665 return 0;
5666
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005667 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005668 if (!cur_idx)
5669 break;
5670
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005671 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005672 cur_ptr = cur_next;
5673 cur_end = cur_ptr + cur_hdr->len;
5674 cur_next = cur_end + cur_hdr->cr + 1;
5675
5676 /* Now we have one header between cur_ptr and cur_end,
5677 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005678 */
5679
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005680 /* The annoying part is that pattern matching needs
5681 * that we modify the contents to null-terminate all
5682 * strings before testing them.
5683 */
5684
5685 term = *cur_end;
5686 *cur_end = '\0';
5687
5688 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5689 switch (exp->action) {
5690 case ACT_SETBE:
5691 /* It is not possible to jump a second time.
5692 * FIXME: should we return an HTTP/500 here so that
5693 * the admin knows there's a problem ?
5694 */
5695 if (t->be != t->fe)
5696 break;
5697
5698 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005699 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005700 last_hdr = 1;
5701 break;
5702
5703 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005704 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005705 last_hdr = 1;
5706 break;
5707
5708 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005709 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005710 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005711
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005712 t->fe->fe_counters.denied_req++;
5713 if (t->fe != t->be)
5714 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005715 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005716 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005717
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005718 break;
5719
5720 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005721 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005722 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005723
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005724 t->fe->fe_counters.denied_req++;
5725 if (t->fe != t->be)
5726 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005727 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005728 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005729
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005730 break;
5731
5732 case ACT_REPLACE:
5733 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5734 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5735 /* FIXME: if the user adds a newline in the replacement, the
5736 * index will not be recalculated for now, and the new line
5737 * will not be counted as a new header.
5738 */
5739
5740 cur_end += delta;
5741 cur_next += delta;
5742 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005743 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005744 break;
5745
5746 case ACT_REMOVE:
5747 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5748 cur_next += delta;
5749
Willy Tarreaufa355d42009-11-29 18:12:29 +01005750 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005751 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5752 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005753 cur_hdr->len = 0;
5754 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005755 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005756 break;
5757
5758 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005759 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005760 if (cur_end)
5761 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005762
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005763 /* keep the link from this header to next one in case of later
5764 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005765 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005766 old_idx = cur_idx;
5767 }
5768 return 0;
5769}
5770
5771
5772/* Apply the filter to the request line.
5773 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5774 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005775 * Since it can manage the switch to another backend, it updates the per-proxy
5776 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005777 */
5778int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5779{
5780 char term;
5781 char *cur_ptr, *cur_end;
5782 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005783 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005784 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005785
Willy Tarreau58f10d72006-12-04 02:26:12 +01005786
Willy Tarreau3d300592007-03-18 18:34:41 +01005787 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005788 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005789 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005790 (exp->action == ACT_ALLOW ||
5791 exp->action == ACT_DENY ||
5792 exp->action == ACT_TARPIT))
5793 return 0;
5794 else if (exp->action == ACT_REMOVE)
5795 return 0;
5796
5797 done = 0;
5798
Willy Tarreau962c3f42010-01-10 00:15:35 +01005799 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005800 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005801
5802 /* Now we have the request line between cur_ptr and cur_end */
5803
5804 /* The annoying part is that pattern matching needs
5805 * that we modify the contents to null-terminate all
5806 * strings before testing them.
5807 */
5808
5809 term = *cur_end;
5810 *cur_end = '\0';
5811
5812 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5813 switch (exp->action) {
5814 case ACT_SETBE:
5815 /* It is not possible to jump a second time.
5816 * FIXME: should we return an HTTP/500 here so that
5817 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005818 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005819 if (t->be != t->fe)
5820 break;
5821
5822 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005823 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005824 done = 1;
5825 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005826
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005827 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005828 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005829 done = 1;
5830 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005831
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005832 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005833 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005834
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005835 t->fe->fe_counters.denied_req++;
5836 if (t->fe != t->be)
5837 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005838 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005839 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005840
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005841 done = 1;
5842 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005843
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005844 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005845 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005846
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005847 t->fe->fe_counters.denied_req++;
5848 if (t->fe != t->be)
5849 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005850 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005851 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005852
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005853 done = 1;
5854 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005855
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005856 case ACT_REPLACE:
5857 *cur_end = term; /* restore the string terminator */
5858 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5859 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5860 /* FIXME: if the user adds a newline in the replacement, the
5861 * index will not be recalculated for now, and the new line
5862 * will not be counted as a new header.
5863 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005864
Willy Tarreaufa355d42009-11-29 18:12:29 +01005865 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005866 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005867 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005868 HTTP_MSG_RQMETH,
5869 cur_ptr, cur_end + 1,
5870 NULL, NULL);
5871 if (unlikely(!cur_end))
5872 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005873
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005874 /* we have a full request and we know that we have either a CR
5875 * or an LF at <ptr>.
5876 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005877 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5878 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005879 /* there is no point trying this regex on headers */
5880 return 1;
5881 }
5882 }
5883 *cur_end = term; /* restore the string terminator */
5884 return done;
5885}
Willy Tarreau97de6242006-12-27 17:18:38 +01005886
Willy Tarreau58f10d72006-12-04 02:26:12 +01005887
Willy Tarreau58f10d72006-12-04 02:26:12 +01005888
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005889/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005890 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005891 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005892 * unparsable request. Since it can manage the switch to another backend, it
5893 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005894 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005895int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005896{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005897 struct http_txn *txn = &s->txn;
5898 struct hdr_exp *exp;
5899
5900 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005901 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005902
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005903 /*
5904 * The interleaving of transformations and verdicts
5905 * makes it difficult to decide to continue or stop
5906 * the evaluation.
5907 */
5908
Willy Tarreau6c123b12010-01-28 20:22:06 +01005909 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5910 break;
5911
Willy Tarreau3d300592007-03-18 18:34:41 +01005912 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005913 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005914 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005915 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005916
5917 /* if this filter had a condition, evaluate it now and skip to
5918 * next filter if the condition does not match.
5919 */
5920 if (exp->cond) {
5921 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5922 ret = acl_pass(ret);
5923 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5924 ret = !ret;
5925
5926 if (!ret)
5927 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005928 }
5929
5930 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005931 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005932 if (unlikely(ret < 0))
5933 return -1;
5934
5935 if (likely(ret == 0)) {
5936 /* The filter did not match the request, it can be
5937 * iterated through all headers.
5938 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005939 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005940 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005941 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005942 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005943}
5944
5945
Willy Tarreaua15645d2007-03-18 16:22:39 +01005946
Willy Tarreau58f10d72006-12-04 02:26:12 +01005947/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005948 * Try to retrieve the server associated to the appsession.
5949 * If the server is found, it's assigned to the session.
5950 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005951void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005952 struct http_txn *txn = &t->txn;
5953 appsess *asession = NULL;
5954 char *sessid_temp = NULL;
5955
Cyril Bontéb21570a2009-11-29 20:04:48 +01005956 if (len > t->be->appsession_len) {
5957 len = t->be->appsession_len;
5958 }
5959
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005960 if (t->be->options2 & PR_O2_AS_REQL) {
5961 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005962 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005963 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005964 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005965 }
5966
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005967 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005968 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5969 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5970 return;
5971 }
5972
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005973 memcpy(txn->sessid, buf, len);
5974 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005975 }
5976
5977 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5978 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5979 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5980 return;
5981 }
5982
Cyril Bontéb21570a2009-11-29 20:04:48 +01005983 memcpy(sessid_temp, buf, len);
5984 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005985
5986 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5987 /* free previously allocated memory */
5988 pool_free2(apools.sessid, sessid_temp);
5989
5990 if (asession != NULL) {
5991 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5992 if (!(t->be->options2 & PR_O2_AS_REQL))
5993 asession->request_count++;
5994
5995 if (asession->serverid != NULL) {
5996 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005997
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005998 while (srv) {
5999 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006000 if ((srv->state & SRV_RUNNING) ||
6001 (t->be->options & PR_O_PERSIST) ||
6002 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006003 /* we found the server and it's usable */
6004 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006005 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006006 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006007 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01006008
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006009 break;
6010 } else {
6011 txn->flags &= ~TX_CK_MASK;
6012 txn->flags |= TX_CK_DOWN;
6013 }
6014 }
6015 srv = srv->next;
6016 }
6017 }
6018 }
6019}
6020
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006021/* Find the end of a cookie value contained between <s> and <e>. It works the
6022 * same way as with headers above except that the semi-colon also ends a token.
6023 * See RFC2965 for more information. Note that it requires a valid header to
6024 * return a valid result.
6025 */
6026char *find_cookie_value_end(char *s, const char *e)
6027{
6028 int quoted, qdpair;
6029
6030 quoted = qdpair = 0;
6031 for (; s < e; s++) {
6032 if (qdpair) qdpair = 0;
6033 else if (quoted) {
6034 if (*s == '\\') qdpair = 1;
6035 else if (*s == '"') quoted = 0;
6036 }
6037 else if (*s == '"') quoted = 1;
6038 else if (*s == ',' || *s == ';') return s;
6039 }
6040 return s;
6041}
6042
6043/* Delete a value in a header between delimiters <from> and <next> in buffer
6044 * <buf>. The number of characters displaced is returned, and the pointer to
6045 * the first delimiter is updated if required. The function tries as much as
6046 * possible to respect the following principles :
6047 * - replace <from> delimiter by the <next> one unless <from> points to a
6048 * colon, in which case <next> is simply removed
6049 * - set exactly one space character after the new first delimiter, unless
6050 * there are not enough characters in the block being moved to do so.
6051 * - remove unneeded spaces before the previous delimiter and after the new
6052 * one.
6053 *
6054 * It is the caller's responsibility to ensure that :
6055 * - <from> points to a valid delimiter or the colon ;
6056 * - <next> points to a valid delimiter or the final CR/LF ;
6057 * - there are non-space chars before <from> ;
6058 * - there is a CR/LF at or after <next>.
6059 */
6060int del_hdr_value(struct buffer *buf, char **from, char *next)
6061{
6062 char *prev = *from;
6063
6064 if (*prev == ':') {
6065 /* We're removing the first value, preserve the colon and add a
6066 * space if possible.
6067 */
6068 if (!http_is_crlf[(unsigned char)*next])
6069 next++;
6070 prev++;
6071 if (prev < next)
6072 *prev++ = ' ';
6073
6074 while (http_is_spht[(unsigned char)*next])
6075 next++;
6076 } else {
6077 /* Remove useless spaces before the old delimiter. */
6078 while (http_is_spht[(unsigned char)*(prev-1)])
6079 prev--;
6080 *from = prev;
6081
6082 /* copy the delimiter and if possible a space if we're
6083 * not at the end of the line.
6084 */
6085 if (!http_is_crlf[(unsigned char)*next]) {
6086 *prev++ = *next++;
6087 if (prev + 1 < next)
6088 *prev++ = ' ';
6089 while (http_is_spht[(unsigned char)*next])
6090 next++;
6091 }
6092 }
6093 return buffer_replace2(buf, prev, next, NULL, 0);
6094}
6095
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006096/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006097 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006098 * desirable to call it only when needed. This code is quite complex because
6099 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6100 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006101 */
6102void manage_client_side_cookies(struct session *t, struct buffer *req)
6103{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006104 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006105 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006106 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006107 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6108 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006109
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006110 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006111 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006112 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006113
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006114 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006115 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006116 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006117
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006118 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006119 hdr_beg = hdr_next;
6120 hdr_end = hdr_beg + cur_hdr->len;
6121 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006122
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006123 /* We have one full header between hdr_beg and hdr_end, and the
6124 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006125 * "Cookie:" headers.
6126 */
6127
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006128 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006129 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006130 old_idx = cur_idx;
6131 continue;
6132 }
6133
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006134 del_from = NULL; /* nothing to be deleted */
6135 preserve_hdr = 0; /* assume we may kill the whole header */
6136
Willy Tarreau58f10d72006-12-04 02:26:12 +01006137 /* Now look for cookies. Conforming to RFC2109, we have to support
6138 * attributes whose name begin with a '$', and associate them with
6139 * the right cookie, if we want to delete this cookie.
6140 * So there are 3 cases for each cookie read :
6141 * 1) it's a special attribute, beginning with a '$' : ignore it.
6142 * 2) it's a server id cookie that we *MAY* want to delete : save
6143 * some pointers on it (last semi-colon, beginning of cookie...)
6144 * 3) it's an application cookie : we *MAY* have to delete a previous
6145 * "special" cookie.
6146 * At the end of loop, if a "special" cookie remains, we may have to
6147 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006148 * *MUST* delete it.
6149 *
6150 * Note: RFC2965 is unclear about the processing of spaces around
6151 * the equal sign in the ATTR=VALUE form. A careful inspection of
6152 * the RFC explicitly allows spaces before it, and not within the
6153 * tokens (attrs or values). An inspection of RFC2109 allows that
6154 * too but section 10.1.3 lets one think that spaces may be allowed
6155 * after the equal sign too, resulting in some (rare) buggy
6156 * implementations trying to do that. So let's do what servers do.
6157 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6158 * allowed quoted strings in values, with any possible character
6159 * after a backslash, including control chars and delimitors, which
6160 * causes parsing to become ambiguous. Browsers also allow spaces
6161 * within values even without quotes.
6162 *
6163 * We have to keep multiple pointers in order to support cookie
6164 * removal at the beginning, middle or end of header without
6165 * corrupting the header. All of these headers are valid :
6166 *
6167 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6168 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6169 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6170 * | | | | | | | | |
6171 * | | | | | | | | hdr_end <--+
6172 * | | | | | | | +--> next
6173 * | | | | | | +----> val_end
6174 * | | | | | +-----------> val_beg
6175 * | | | | +--------------> equal
6176 * | | | +----------------> att_end
6177 * | | +---------------------> att_beg
6178 * | +--------------------------> prev
6179 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006180 */
6181
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006182 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6183 /* Iterate through all cookies on this line */
6184
6185 /* find att_beg */
6186 att_beg = prev + 1;
6187 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6188 att_beg++;
6189
6190 /* find att_end : this is the first character after the last non
6191 * space before the equal. It may be equal to hdr_end.
6192 */
6193 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006194
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006195 while (equal < hdr_end) {
6196 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006197 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006198 if (http_is_spht[(unsigned char)*equal++])
6199 continue;
6200 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006201 }
6202
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006203 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6204 * is between <att_beg> and <equal>, both may be identical.
6205 */
6206
6207 /* look for end of cookie if there is an equal sign */
6208 if (equal < hdr_end && *equal == '=') {
6209 /* look for the beginning of the value */
6210 val_beg = equal + 1;
6211 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6212 val_beg++;
6213
6214 /* find the end of the value, respecting quotes */
6215 next = find_cookie_value_end(val_beg, hdr_end);
6216
6217 /* make val_end point to the first white space or delimitor after the value */
6218 val_end = next;
6219 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6220 val_end--;
6221 } else {
6222 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006223 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006224
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006225 /* We have nothing to do with attributes beginning with '$'. However,
6226 * they will automatically be removed if a header before them is removed,
6227 * since they're supposed to be linked together.
6228 */
6229 if (*att_beg == '$')
6230 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006231
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006232 /* Ignore cookies with no equal sign */
6233 if (equal == next) {
6234 /* This is not our cookie, so we must preserve it. But if we already
6235 * scheduled another cookie for removal, we cannot remove the
6236 * complete header, but we can remove the previous block itself.
6237 */
6238 preserve_hdr = 1;
6239 if (del_from != NULL) {
6240 int delta = del_hdr_value(req, &del_from, prev);
6241 val_end += delta;
6242 next += delta;
6243 hdr_end += delta;
6244 hdr_next += delta;
6245 cur_hdr->len += delta;
6246 http_msg_move_end(&txn->req, delta);
6247 prev = del_from;
6248 del_from = NULL;
6249 }
6250 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006251 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006252
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006253 /* if there are spaces around the equal sign, we need to
6254 * strip them otherwise we'll get trouble for cookie captures,
6255 * or even for rewrites. Since this happens extremely rarely,
6256 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006258 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6259 int stripped_before = 0;
6260 int stripped_after = 0;
6261
6262 if (att_end != equal) {
6263 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6264 equal += stripped_before;
6265 val_beg += stripped_before;
6266 }
6267
6268 if (val_beg > equal + 1) {
6269 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6270 val_beg += stripped_after;
6271 stripped_before += stripped_after;
6272 }
6273
6274 val_end += stripped_before;
6275 next += stripped_before;
6276 hdr_end += stripped_before;
6277 hdr_next += stripped_before;
6278 cur_hdr->len += stripped_before;
6279 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006280 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006281 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006282
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006283 /* First, let's see if we want to capture this cookie. We check
6284 * that we don't already have a client side cookie, because we
6285 * can only capture one. Also as an optimisation, we ignore
6286 * cookies shorter than the declared name.
6287 */
6288 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6289 (val_end - att_beg >= t->fe->capture_namelen) &&
6290 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6291 int log_len = val_end - att_beg;
6292
6293 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6294 Alert("HTTP logging : out of memory.\n");
6295 } else {
6296 if (log_len > t->fe->capture_len)
6297 log_len = t->fe->capture_len;
6298 memcpy(txn->cli_cookie, att_beg, log_len);
6299 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006300 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006301 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006302
Willy Tarreaubca99692010-10-06 19:25:55 +02006303 /* Persistence cookies in passive, rewrite or insert mode have the
6304 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006305 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006306 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006307 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006308 * For cookies in prefix mode, the form is :
6309 *
6310 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006311 */
6312 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6313 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6314 struct server *srv = t->be->srv;
6315 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006316
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006317 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6318 * have the server ID between val_beg and delim, and the original cookie between
6319 * delim+1 and val_end. Otherwise, delim==val_end :
6320 *
6321 * Cookie: NAME=SRV; # in all but prefix modes
6322 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6323 * | || || | |+-> next
6324 * | || || | +--> val_end
6325 * | || || +---------> delim
6326 * | || |+------------> val_beg
6327 * | || +-------------> att_end = equal
6328 * | |+-----------------> att_beg
6329 * | +------------------> prev
6330 * +-------------------------> hdr_beg
6331 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006332
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006333 if (t->be->options & PR_O_COOK_PFX) {
6334 for (delim = val_beg; delim < val_end; delim++)
6335 if (*delim == COOKIE_DELIM)
6336 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006337 } else {
6338 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006339 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006340 /* Now check if the cookie contains a date field, which would
6341 * appear after a vertical bar ('|') just after the server name
6342 * and before the delimiter.
6343 */
6344 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6345 if (vbar1) {
6346 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006347 * right is the last seen date. It is a base64 encoded
6348 * 30-bit value representing the UNIX date since the
6349 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006350 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006351 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006352 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006353 if (val_end - vbar1 >= 5) {
6354 val = b64tos30(vbar1);
6355 if (val > 0)
6356 txn->cookie_last_date = val << 2;
6357 }
6358 /* look for a second vertical bar */
6359 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6360 if (vbar1 && (val_end - vbar1 > 5)) {
6361 val = b64tos30(vbar1 + 1);
6362 if (val > 0)
6363 txn->cookie_first_date = val << 2;
6364 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006365 }
6366 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006367
Willy Tarreauf64d1412010-10-07 20:06:11 +02006368 /* if the cookie has an expiration date and the proxy wants to check
6369 * it, then we do that now. We first check if the cookie is too old,
6370 * then only if it has expired. We detect strict overflow because the
6371 * time resolution here is not great (4 seconds). Cookies with dates
6372 * in the future are ignored if their offset is beyond one day. This
6373 * allows an admin to fix timezone issues without expiring everyone
6374 * and at the same time avoids keeping unwanted side effects for too
6375 * long.
6376 */
6377 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006378 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6379 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006380 txn->flags &= ~TX_CK_MASK;
6381 txn->flags |= TX_CK_OLD;
6382 delim = val_beg; // let's pretend we have not found the cookie
6383 txn->cookie_first_date = 0;
6384 txn->cookie_last_date = 0;
6385 }
6386 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006387 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6388 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006389 txn->flags &= ~TX_CK_MASK;
6390 txn->flags |= TX_CK_EXPIRED;
6391 delim = val_beg; // let's pretend we have not found the cookie
6392 txn->cookie_first_date = 0;
6393 txn->cookie_last_date = 0;
6394 }
6395
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006396 /* Here, we'll look for the first running server which supports the cookie.
6397 * This allows to share a same cookie between several servers, for example
6398 * to dedicate backup servers to specific servers only.
6399 * However, to prevent clients from sticking to cookie-less backup server
6400 * when they have incidentely learned an empty cookie, we simply ignore
6401 * empty cookies and mark them as invalid.
6402 * The same behaviour is applied when persistence must be ignored.
6403 */
6404 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6405 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006406
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006407 while (srv) {
6408 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6409 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6410 if ((srv->state & SRV_RUNNING) ||
6411 (t->be->options & PR_O_PERSIST) ||
6412 (t->flags & SN_FORCE_PRST)) {
6413 /* we found the server and we can use it */
6414 txn->flags &= ~TX_CK_MASK;
6415 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6416 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006417 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006418 break;
6419 } else {
6420 /* we found a server, but it's down,
6421 * mark it as such and go on in case
6422 * another one is available.
6423 */
6424 txn->flags &= ~TX_CK_MASK;
6425 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006426 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006427 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006428 srv = srv->next;
6429 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006430
Willy Tarreauf64d1412010-10-07 20:06:11 +02006431 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006432 /* no server matched this cookie */
6433 txn->flags &= ~TX_CK_MASK;
6434 txn->flags |= TX_CK_INVALID;
6435 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006436
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006437 /* depending on the cookie mode, we may have to either :
6438 * - delete the complete cookie if we're in insert+indirect mode, so that
6439 * the server never sees it ;
6440 * - remove the server id from the cookie value, and tag the cookie as an
6441 * application cookie so that it does not get accidentely removed later,
6442 * if we're in cookie prefix mode
6443 */
6444 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6445 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006446
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006447 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6448 val_end += delta;
6449 next += delta;
6450 hdr_end += delta;
6451 hdr_next += delta;
6452 cur_hdr->len += delta;
6453 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006454
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006455 del_from = NULL;
6456 preserve_hdr = 1; /* we want to keep this cookie */
6457 }
6458 else if (del_from == NULL &&
6459 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6460 del_from = prev;
6461 }
6462 } else {
6463 /* This is not our cookie, so we must preserve it. But if we already
6464 * scheduled another cookie for removal, we cannot remove the
6465 * complete header, but we can remove the previous block itself.
6466 */
6467 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006468
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006469 if (del_from != NULL) {
6470 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006471 if (att_beg >= del_from)
6472 att_beg += delta;
6473 if (att_end >= del_from)
6474 att_end += delta;
6475 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006476 val_end += delta;
6477 next += delta;
6478 hdr_end += delta;
6479 hdr_next += delta;
6480 cur_hdr->len += delta;
6481 http_msg_move_end(&txn->req, delta);
6482 prev = del_from;
6483 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006484 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006485 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006486
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006487 /* Look for the appsession cookie unless persistence must be ignored */
6488 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6489 int cmp_len, value_len;
6490 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006491
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006492 if (t->be->options2 & PR_O2_AS_PFX) {
6493 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6494 value_begin = att_beg + t->be->appsession_name_len;
6495 value_len = val_end - att_beg - t->be->appsession_name_len;
6496 } else {
6497 cmp_len = att_end - att_beg;
6498 value_begin = val_beg;
6499 value_len = val_end - val_beg;
6500 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006501
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006502 /* let's see if the cookie is our appcookie */
6503 if (cmp_len == t->be->appsession_name_len &&
6504 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6505 manage_client_side_appsession(t, value_begin, value_len);
6506 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006507 }
6508
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006509 /* continue with next cookie on this header line */
6510 att_beg = next;
6511 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006512
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006513 /* There are no more cookies on this line.
6514 * We may still have one (or several) marked for deletion at the
6515 * end of the line. We must do this now in two ways :
6516 * - if some cookies must be preserved, we only delete from the
6517 * mark to the end of line ;
6518 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006519 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006520 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006521 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006522 if (preserve_hdr) {
6523 delta = del_hdr_value(req, &del_from, hdr_end);
6524 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006525 cur_hdr->len += delta;
6526 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006527 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006528
6529 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006530 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6531 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006532 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006533 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006534 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006535 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006536 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006537 }
6538
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006539 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006540 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006541 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006542}
6543
6544
Willy Tarreaua15645d2007-03-18 16:22:39 +01006545/* Iterate the same filter through all response headers contained in <rtr>.
6546 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6547 */
6548int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6549{
6550 char term;
6551 char *cur_ptr, *cur_end, *cur_next;
6552 int cur_idx, old_idx, last_hdr;
6553 struct http_txn *txn = &t->txn;
6554 struct hdr_idx_elem *cur_hdr;
6555 int len, delta;
6556
6557 last_hdr = 0;
6558
Willy Tarreau962c3f42010-01-10 00:15:35 +01006559 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006560 old_idx = 0;
6561
6562 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006563 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006564 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006565 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006566 (exp->action == ACT_ALLOW ||
6567 exp->action == ACT_DENY))
6568 return 0;
6569
6570 cur_idx = txn->hdr_idx.v[old_idx].next;
6571 if (!cur_idx)
6572 break;
6573
6574 cur_hdr = &txn->hdr_idx.v[cur_idx];
6575 cur_ptr = cur_next;
6576 cur_end = cur_ptr + cur_hdr->len;
6577 cur_next = cur_end + cur_hdr->cr + 1;
6578
6579 /* Now we have one header between cur_ptr and cur_end,
6580 * and the next header starts at cur_next.
6581 */
6582
6583 /* The annoying part is that pattern matching needs
6584 * that we modify the contents to null-terminate all
6585 * strings before testing them.
6586 */
6587
6588 term = *cur_end;
6589 *cur_end = '\0';
6590
6591 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6592 switch (exp->action) {
6593 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006594 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006595 last_hdr = 1;
6596 break;
6597
6598 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006599 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006600 last_hdr = 1;
6601 break;
6602
6603 case ACT_REPLACE:
6604 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6605 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6606 /* FIXME: if the user adds a newline in the replacement, the
6607 * index will not be recalculated for now, and the new line
6608 * will not be counted as a new header.
6609 */
6610
6611 cur_end += delta;
6612 cur_next += delta;
6613 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006614 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006615 break;
6616
6617 case ACT_REMOVE:
6618 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6619 cur_next += delta;
6620
Willy Tarreaufa355d42009-11-29 18:12:29 +01006621 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006622 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6623 txn->hdr_idx.used--;
6624 cur_hdr->len = 0;
6625 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006626 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006627 break;
6628
6629 }
6630 }
6631 if (cur_end)
6632 *cur_end = term; /* restore the string terminator */
6633
6634 /* keep the link from this header to next one in case of later
6635 * removal of next header.
6636 */
6637 old_idx = cur_idx;
6638 }
6639 return 0;
6640}
6641
6642
6643/* Apply the filter to the status line in the response buffer <rtr>.
6644 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6645 * or -1 if a replacement resulted in an invalid status line.
6646 */
6647int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6648{
6649 char term;
6650 char *cur_ptr, *cur_end;
6651 int done;
6652 struct http_txn *txn = &t->txn;
6653 int len, delta;
6654
6655
Willy Tarreau3d300592007-03-18 18:34:41 +01006656 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006657 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006658 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006659 (exp->action == ACT_ALLOW ||
6660 exp->action == ACT_DENY))
6661 return 0;
6662 else if (exp->action == ACT_REMOVE)
6663 return 0;
6664
6665 done = 0;
6666
Willy Tarreau962c3f42010-01-10 00:15:35 +01006667 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006668 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006669
6670 /* Now we have the status line between cur_ptr and cur_end */
6671
6672 /* The annoying part is that pattern matching needs
6673 * that we modify the contents to null-terminate all
6674 * strings before testing them.
6675 */
6676
6677 term = *cur_end;
6678 *cur_end = '\0';
6679
6680 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6681 switch (exp->action) {
6682 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006683 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006684 done = 1;
6685 break;
6686
6687 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006688 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006689 done = 1;
6690 break;
6691
6692 case ACT_REPLACE:
6693 *cur_end = term; /* restore the string terminator */
6694 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6695 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6696 /* FIXME: if the user adds a newline in the replacement, the
6697 * index will not be recalculated for now, and the new line
6698 * will not be counted as a new header.
6699 */
6700
Willy Tarreaufa355d42009-11-29 18:12:29 +01006701 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006702 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006703 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006704 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006705 cur_ptr, cur_end + 1,
6706 NULL, NULL);
6707 if (unlikely(!cur_end))
6708 return -1;
6709
6710 /* we have a full respnse and we know that we have either a CR
6711 * or an LF at <ptr>.
6712 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006713 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006714 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006715 /* there is no point trying this regex on headers */
6716 return 1;
6717 }
6718 }
6719 *cur_end = term; /* restore the string terminator */
6720 return done;
6721}
6722
6723
6724
6725/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006726 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006727 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6728 * unparsable response.
6729 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006730int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006731{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006732 struct http_txn *txn = &s->txn;
6733 struct hdr_exp *exp;
6734
6735 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006736 int ret;
6737
6738 /*
6739 * The interleaving of transformations and verdicts
6740 * makes it difficult to decide to continue or stop
6741 * the evaluation.
6742 */
6743
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006744 if (txn->flags & TX_SVDENY)
6745 break;
6746
Willy Tarreau3d300592007-03-18 18:34:41 +01006747 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006748 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6749 exp->action == ACT_PASS)) {
6750 exp = exp->next;
6751 continue;
6752 }
6753
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006754 /* if this filter had a condition, evaluate it now and skip to
6755 * next filter if the condition does not match.
6756 */
6757 if (exp->cond) {
6758 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6759 ret = acl_pass(ret);
6760 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6761 ret = !ret;
6762 if (!ret)
6763 continue;
6764 }
6765
Willy Tarreaua15645d2007-03-18 16:22:39 +01006766 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006767 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006768 if (unlikely(ret < 0))
6769 return -1;
6770
6771 if (likely(ret == 0)) {
6772 /* The filter did not match the response, it can be
6773 * iterated through all headers.
6774 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006775 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006776 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006777 }
6778 return 0;
6779}
6780
6781
Willy Tarreaua15645d2007-03-18 16:22:39 +01006782/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006783 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006784 * desirable to call it only when needed. This function is also used when we
6785 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006786 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006787void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006788{
6789 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006790 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006791 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006792 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006793 char *hdr_beg, *hdr_end, *hdr_next;
6794 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006795
Willy Tarreaua15645d2007-03-18 16:22:39 +01006796 /* Iterate through the headers.
6797 * we start with the start line.
6798 */
6799 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006800 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006801
6802 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6803 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006804 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006805
6806 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006807 hdr_beg = hdr_next;
6808 hdr_end = hdr_beg + cur_hdr->len;
6809 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006810
Willy Tarreau24581ba2010-08-31 22:39:35 +02006811 /* We have one full header between hdr_beg and hdr_end, and the
6812 * next header starts at hdr_next. We're only interested in
6813 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006814 */
6815
Willy Tarreau24581ba2010-08-31 22:39:35 +02006816 is_cookie2 = 0;
6817 prev = hdr_beg + 10;
6818 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006819 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006820 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6821 if (!val) {
6822 old_idx = cur_idx;
6823 continue;
6824 }
6825 is_cookie2 = 1;
6826 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006827 }
6828
Willy Tarreau24581ba2010-08-31 22:39:35 +02006829 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6830 * <prev> points to the colon.
6831 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006832 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833
Willy Tarreau24581ba2010-08-31 22:39:35 +02006834 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6835 * check-cache is enabled) and we are not interested in checking
6836 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006837 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006838 if (t->be->cookie_name == NULL &&
6839 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006840 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006841 return;
6842
Willy Tarreau24581ba2010-08-31 22:39:35 +02006843 /* OK so now we know we have to process this response cookie.
6844 * The format of the Set-Cookie header is slightly different
6845 * from the format of the Cookie header in that it does not
6846 * support the comma as a cookie delimiter (thus the header
6847 * cannot be folded) because the Expires attribute described in
6848 * the original Netscape's spec may contain an unquoted date
6849 * with a comma inside. We have to live with this because
6850 * many browsers don't support Max-Age and some browsers don't
6851 * support quoted strings. However the Set-Cookie2 header is
6852 * clean.
6853 *
6854 * We have to keep multiple pointers in order to support cookie
6855 * removal at the beginning, middle or end of header without
6856 * corrupting the header (in case of set-cookie2). A special
6857 * pointer, <scav> points to the beginning of the set-cookie-av
6858 * fields after the first semi-colon. The <next> pointer points
6859 * either to the end of line (set-cookie) or next unquoted comma
6860 * (set-cookie2). All of these headers are valid :
6861 *
6862 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6863 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6864 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6865 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6866 * | | | | | | | | | |
6867 * | | | | | | | | +-> next hdr_end <--+
6868 * | | | | | | | +------------> scav
6869 * | | | | | | +--------------> val_end
6870 * | | | | | +--------------------> val_beg
6871 * | | | | +----------------------> equal
6872 * | | | +------------------------> att_end
6873 * | | +----------------------------> att_beg
6874 * | +------------------------------> prev
6875 * +-----------------------------------------> hdr_beg
6876 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006877
Willy Tarreau24581ba2010-08-31 22:39:35 +02006878 for (; prev < hdr_end; prev = next) {
6879 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006880
Willy Tarreau24581ba2010-08-31 22:39:35 +02006881 /* find att_beg */
6882 att_beg = prev + 1;
6883 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6884 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006885
Willy Tarreau24581ba2010-08-31 22:39:35 +02006886 /* find att_end : this is the first character after the last non
6887 * space before the equal. It may be equal to hdr_end.
6888 */
6889 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006890
Willy Tarreau24581ba2010-08-31 22:39:35 +02006891 while (equal < hdr_end) {
6892 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6893 break;
6894 if (http_is_spht[(unsigned char)*equal++])
6895 continue;
6896 att_end = equal;
6897 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006898
Willy Tarreau24581ba2010-08-31 22:39:35 +02006899 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6900 * is between <att_beg> and <equal>, both may be identical.
6901 */
6902
6903 /* look for end of cookie if there is an equal sign */
6904 if (equal < hdr_end && *equal == '=') {
6905 /* look for the beginning of the value */
6906 val_beg = equal + 1;
6907 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6908 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006909
Willy Tarreau24581ba2010-08-31 22:39:35 +02006910 /* find the end of the value, respecting quotes */
6911 next = find_cookie_value_end(val_beg, hdr_end);
6912
6913 /* make val_end point to the first white space or delimitor after the value */
6914 val_end = next;
6915 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6916 val_end--;
6917 } else {
6918 /* <equal> points to next comma, semi-colon or EOL */
6919 val_beg = val_end = next = equal;
6920 }
6921
6922 if (next < hdr_end) {
6923 /* Set-Cookie2 supports multiple cookies, and <next> points to
6924 * a colon or semi-colon before the end. So skip all attr-value
6925 * pairs and look for the next comma. For Set-Cookie, since
6926 * commas are permitted in values, skip to the end.
6927 */
6928 if (is_cookie2)
6929 next = find_hdr_value_end(next, hdr_end);
6930 else
6931 next = hdr_end;
6932 }
6933
6934 /* Now everything is as on the diagram above */
6935
6936 /* Ignore cookies with no equal sign */
6937 if (equal == val_end)
6938 continue;
6939
6940 /* If there are spaces around the equal sign, we need to
6941 * strip them otherwise we'll get trouble for cookie captures,
6942 * or even for rewrites. Since this happens extremely rarely,
6943 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006944 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006945 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6946 int stripped_before = 0;
6947 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006948
Willy Tarreau24581ba2010-08-31 22:39:35 +02006949 if (att_end != equal) {
6950 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6951 equal += stripped_before;
6952 val_beg += stripped_before;
6953 }
6954
6955 if (val_beg > equal + 1) {
6956 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6957 val_beg += stripped_after;
6958 stripped_before += stripped_after;
6959 }
6960
6961 val_end += stripped_before;
6962 next += stripped_before;
6963 hdr_end += stripped_before;
6964 hdr_next += stripped_before;
6965 cur_hdr->len += stripped_before;
6966 http_msg_move_end(&txn->req, stripped_before);
6967 }
6968
6969 /* First, let's see if we want to capture this cookie. We check
6970 * that we don't already have a server side cookie, because we
6971 * can only capture one. Also as an optimisation, we ignore
6972 * cookies shorter than the declared name.
6973 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006974 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006975 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006976 (val_end - att_beg >= t->fe->capture_namelen) &&
6977 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6978 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006979 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006980 Alert("HTTP logging : out of memory.\n");
6981 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006982 else {
6983 if (log_len > t->fe->capture_len)
6984 log_len = t->fe->capture_len;
6985 memcpy(txn->srv_cookie, att_beg, log_len);
6986 txn->srv_cookie[log_len] = 0;
6987 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006988 }
6989
Willy Tarreau827aee92011-03-10 16:55:02 +01006990 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006991 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006992 if (!(t->flags & SN_IGNORE_PRST) &&
6993 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6994 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006995 /* assume passive cookie by default */
6996 txn->flags &= ~TX_SCK_MASK;
6997 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006998
6999 /* If the cookie is in insert mode on a known server, we'll delete
7000 * this occurrence because we'll insert another one later.
7001 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007002 * a direct access.
7003 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007004 if (t->be->options2 & PR_O2_COOK_PSV) {
7005 /* The "preserve" flag was set, we don't want to touch the
7006 * server's cookie.
7007 */
7008 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007009 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007010 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007011 /* this cookie must be deleted */
7012 if (*prev == ':' && next == hdr_end) {
7013 /* whole header */
7014 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
7015 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7016 txn->hdr_idx.used--;
7017 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007018 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007019 hdr_next += delta;
7020 http_msg_move_end(&txn->rsp, delta);
7021 /* note: while both invalid now, <next> and <hdr_end>
7022 * are still equal, so the for() will stop as expected.
7023 */
7024 } else {
7025 /* just remove the value */
7026 int delta = del_hdr_value(res, &prev, next);
7027 next = prev;
7028 hdr_end += delta;
7029 hdr_next += delta;
7030 cur_hdr->len += delta;
7031 http_msg_move_end(&txn->rsp, delta);
7032 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007033 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007034 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007035 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007036 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007037 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007038 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007039 * with this server since we know it.
7040 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007041 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007042 next += delta;
7043 hdr_end += delta;
7044 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007045 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007046 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007047
Willy Tarreauf1348312010-10-07 15:54:11 +02007048 txn->flags &= ~TX_SCK_MASK;
7049 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007050 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007051 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007052 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007053 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007054 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007055 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007056 next += delta;
7057 hdr_end += delta;
7058 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007059 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007060 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007061
Willy Tarreau827aee92011-03-10 16:55:02 +01007062 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007063 txn->flags &= ~TX_SCK_MASK;
7064 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007065 }
7066 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007067 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7068 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007069 int cmp_len, value_len;
7070 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007071
Cyril Bontéb21570a2009-11-29 20:04:48 +01007072 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007073 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7074 value_begin = att_beg + t->be->appsession_name_len;
7075 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007076 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007077 cmp_len = att_end - att_beg;
7078 value_begin = val_beg;
7079 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007080 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007081
Cyril Bonté17530c32010-04-06 21:11:10 +02007082 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007083 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7084 /* free a possibly previously allocated memory */
7085 pool_free2(apools.sessid, txn->sessid);
7086
Cyril Bontéb21570a2009-11-29 20:04:48 +01007087 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007088 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007089 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7090 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7091 return;
7092 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007093 memcpy(txn->sessid, value_begin, value_len);
7094 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007095 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007096 }
7097 /* that's done for this cookie, check the next one on the same
7098 * line when next != hdr_end (only if is_cookie2).
7099 */
7100 }
7101 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007102 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007103 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007104
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007105 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007106 appsess *asession = NULL;
7107 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007108 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007109 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007110 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007111 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7112 Alert("Not enough Memory process_srv():asession:calloc().\n");
7113 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7114 return;
7115 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007116 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7117
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007118 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7119 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7120 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007121 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007122 return;
7123 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007124 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007125 asession->sessid[t->be->appsession_len] = 0;
7126
Willy Tarreau827aee92011-03-10 16:55:02 +01007127 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007128 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007129 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007130 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007131 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007132 return;
7133 }
7134 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01007135 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007136
7137 asession->request_count = 0;
7138 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7139 }
7140
7141 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7142 asession->request_count++;
7143 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007144}
7145
7146
Willy Tarreaua15645d2007-03-18 16:22:39 +01007147/*
7148 * Check if response is cacheable or not. Updates t->flags.
7149 */
7150void check_response_for_cacheability(struct session *t, struct buffer *rtr)
7151{
7152 struct http_txn *txn = &t->txn;
7153 char *p1, *p2;
7154
7155 char *cur_ptr, *cur_end, *cur_next;
7156 int cur_idx;
7157
Willy Tarreau5df51872007-11-25 16:20:08 +01007158 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007159 return;
7160
7161 /* Iterate through the headers.
7162 * we start with the start line.
7163 */
7164 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007165 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007166
7167 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7168 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007169 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007170
7171 cur_hdr = &txn->hdr_idx.v[cur_idx];
7172 cur_ptr = cur_next;
7173 cur_end = cur_ptr + cur_hdr->len;
7174 cur_next = cur_end + cur_hdr->cr + 1;
7175
7176 /* We have one full header between cur_ptr and cur_end, and the
7177 * next header starts at cur_next. We're only interested in
7178 * "Cookie:" headers.
7179 */
7180
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007181 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7182 if (val) {
7183 if ((cur_end - (cur_ptr + val) >= 8) &&
7184 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7185 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7186 return;
7187 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007188 }
7189
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007190 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7191 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007192 continue;
7193
7194 /* OK, right now we know we have a cache-control header at cur_ptr */
7195
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007196 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007197
7198 if (p1 >= cur_end) /* no more info */
7199 continue;
7200
7201 /* p1 is at the beginning of the value */
7202 p2 = p1;
7203
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007204 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007205 p2++;
7206
7207 /* we have a complete value between p1 and p2 */
7208 if (p2 < cur_end && *p2 == '=') {
7209 /* we have something of the form no-cache="set-cookie" */
7210 if ((cur_end - p1 >= 21) &&
7211 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7212 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007213 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007214 continue;
7215 }
7216
7217 /* OK, so we know that either p2 points to the end of string or to a comma */
7218 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7219 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7220 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7221 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007222 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007223 return;
7224 }
7225
7226 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007227 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007228 continue;
7229 }
7230 }
7231}
7232
7233
Willy Tarreau58f10d72006-12-04 02:26:12 +01007234/*
7235 * Try to retrieve a known appsession in the URI, then the associated server.
7236 * If the server is found, it's assigned to the session.
7237 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007238void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007239{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007240 char *end_params, *first_param, *cur_param, *next_param;
7241 char separator;
7242 int value_len;
7243
7244 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007245
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007246 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007247 (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 +01007248 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007249 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007250
Cyril Bontéb21570a2009-11-29 20:04:48 +01007251 first_param = NULL;
7252 switch (mode) {
7253 case PR_O2_AS_M_PP:
7254 first_param = memchr(begin, ';', len);
7255 break;
7256 case PR_O2_AS_M_QS:
7257 first_param = memchr(begin, '?', len);
7258 break;
7259 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007260
Cyril Bontéb21570a2009-11-29 20:04:48 +01007261 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007262 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007263 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007264
Cyril Bontéb21570a2009-11-29 20:04:48 +01007265 switch (mode) {
7266 case PR_O2_AS_M_PP:
7267 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7268 end_params = (char *) begin + len;
7269 }
7270 separator = ';';
7271 break;
7272 case PR_O2_AS_M_QS:
7273 end_params = (char *) begin + len;
7274 separator = '&';
7275 break;
7276 default:
7277 /* unknown mode, shouldn't happen */
7278 return;
7279 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007280
Cyril Bontéb21570a2009-11-29 20:04:48 +01007281 cur_param = next_param = end_params;
7282 while (cur_param > first_param) {
7283 cur_param--;
7284 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7285 /* let's see if this is the appsession parameter */
7286 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7287 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7288 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7289 /* Cool... it's the right one */
7290 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7291 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7292 if (value_len > 0) {
7293 manage_client_side_appsession(t, cur_param, value_len);
7294 }
7295 break;
7296 }
7297 next_param = cur_param;
7298 }
7299 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007300#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007301 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007302 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007303#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007304}
7305
Willy Tarreaub2513902006-12-17 14:52:38 +01007306/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007307 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007308 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007309 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007310 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007311 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007312 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007313 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007314 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007315int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007316{
7317 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007318 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007319
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007320 if (!uri_auth)
7321 return 0;
7322
Cyril Bonté70be45d2010-10-12 00:14:35 +02007323 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007324 return 0;
7325
Willy Tarreau295a8372011-03-10 11:25:07 +01007326 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007327
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007328 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007329 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007330 return 0;
7331
Willy Tarreau962c3f42010-01-10 00:15:35 +01007332 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007333
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007334 /* the URI is in h */
7335 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007336 return 0;
7337
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007338 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007339 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007340 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007341 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007342 break;
7343 }
7344 h++;
7345 }
7346
7347 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007348 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7349 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007350 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007351 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007352 break;
7353 }
7354 h++;
7355 }
7356 }
7357
Willy Tarreau962c3f42010-01-10 00:15:35 +01007358 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7359 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007360 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007361 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007362 break;
7363 }
7364 h++;
7365 }
7366
Cyril Bonté70be45d2010-10-12 00:14:35 +02007367 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7368 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7369 if (memcmp(h, ";st=", 4) == 0) {
7370 h += 4;
7371
7372 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007373 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007374 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007375 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007376 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007377 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007378 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007379 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007380 else
Willy Tarreau295a8372011-03-10 11:25:07 +01007381 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007382 break;
7383 }
7384 h++;
7385 }
7386
Willy Tarreau295a8372011-03-10 11:25:07 +01007387 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007388
Willy Tarreaub2513902006-12-17 14:52:38 +01007389 return 1;
7390}
7391
Willy Tarreau4076a152009-04-02 15:18:36 +02007392/*
7393 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007394 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7395 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007396 */
7397void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7398 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007399 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007400{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007401 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7402 int len1 = buf->size - msg->som;
7403 es->len = buf->r - (buf->data + msg->som) + buf->size;
7404 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7405 if (es->len > len1 && len1 < sizeof(es->buf))
7406 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7407 }
7408 else {
7409 es->len = buf->r - (buf->data + msg->som);
7410 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7411 }
7412
Willy Tarreau4076a152009-04-02 15:18:36 +02007413 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007414 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007415 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007416 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007417 else
7418 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7419
Willy Tarreau4076a152009-04-02 15:18:36 +02007420 es->when = date; // user-visible date
7421 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007422 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007423 es->oe = other_end;
Willy Tarreau957c0a52011-03-03 17:42:23 +01007424 es->src = s->req->prod->addr.c.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007425 es->state = state;
7426 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007427 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007428}
Willy Tarreaub2513902006-12-17 14:52:38 +01007429
Willy Tarreaubce70882009-09-07 11:51:47 +02007430/* return the IP address pointed to by occurrence <occ> of header <hname> in
7431 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
7432 * occurrence number corresponding to this value is returned. If <occ> is
7433 * strictly negative, the occurrence number before the end corresponding to
7434 * this value is returned. If <occ> is null, any value is returned, so it is
7435 * not recommended to use it that way. Negative occurrences are limited to
7436 * a small value because it is required to keep them in memory while scanning.
7437 * IP address 0.0.0.0 is returned if no match is found.
7438 */
7439unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
7440{
7441 struct hdr_ctx ctx;
7442 unsigned int hdr_hist[MAX_HDR_HISTORY];
7443 unsigned int hist_ptr;
7444 int found = 0;
7445
7446 ctx.idx = 0;
7447 if (occ >= 0) {
7448 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7449 occ--;
7450 if (occ <= 0) {
7451 found = 1;
7452 break;
7453 }
7454 }
7455 if (!found)
7456 return 0;
7457 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7458 }
7459
7460 /* negative occurrence, we scan all the list then walk back */
7461 if (-occ > MAX_HDR_HISTORY)
7462 return 0;
7463
7464 hist_ptr = 0;
7465 hdr_hist[hist_ptr] = 0;
7466 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7467 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7468 if (hist_ptr >= MAX_HDR_HISTORY)
7469 hist_ptr = 0;
7470 found++;
7471 }
7472 if (-occ > found)
7473 return 0;
7474 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7475 * find occurrence -occ, so we have to check [hist_ptr+occ].
7476 */
7477 hist_ptr += occ;
7478 if (hist_ptr >= MAX_HDR_HISTORY)
7479 hist_ptr -= MAX_HDR_HISTORY;
7480 return hdr_hist[hist_ptr];
7481}
7482
Willy Tarreaubaaee002006-06-26 02:48:02 +02007483/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007484 * Print a debug line with a header
7485 */
7486void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7487{
7488 int len, max;
7489 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007490 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007491 max = end - start;
7492 UBOUND(max, sizeof(trash) - len - 1);
7493 len += strlcpy2(trash + len, start, max + 1);
7494 trash[len++] = '\n';
7495 write(1, trash, len);
7496}
7497
Willy Tarreau0937bc42009-12-22 15:03:09 +01007498/*
7499 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7500 * the required fields are properly allocated and that we only need to (re)init
7501 * them. This should be used before processing any new request.
7502 */
7503void http_init_txn(struct session *s)
7504{
7505 struct http_txn *txn = &s->txn;
7506 struct proxy *fe = s->fe;
7507
7508 txn->flags = 0;
7509 txn->status = -1;
7510
Willy Tarreauf64d1412010-10-07 20:06:11 +02007511 txn->cookie_first_date = 0;
7512 txn->cookie_last_date = 0;
7513
Willy Tarreau0937bc42009-12-22 15:03:09 +01007514 txn->req.sol = txn->req.eol = NULL;
7515 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7516 txn->rsp.sol = txn->rsp.eol = NULL;
7517 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007518 txn->req.chunk_len = 0LL;
7519 txn->req.body_len = 0LL;
7520 txn->rsp.chunk_len = 0LL;
7521 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007522 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7523 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007524
7525 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007526
7527 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7528 if (fe->options2 & PR_O2_REQBUG_OK)
7529 txn->req.err_pos = -1; /* let buggy requests pass */
7530
Willy Tarreau46023632010-01-07 22:51:47 +01007531 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007532 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7533
Willy Tarreau46023632010-01-07 22:51:47 +01007534 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007535 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7536
7537 if (txn->hdr_idx.v)
7538 hdr_idx_init(&txn->hdr_idx);
7539}
7540
7541/* to be used at the end of a transaction */
7542void http_end_txn(struct session *s)
7543{
7544 struct http_txn *txn = &s->txn;
7545
7546 /* these ones will have been dynamically allocated */
7547 pool_free2(pool2_requri, txn->uri);
7548 pool_free2(pool2_capture, txn->cli_cookie);
7549 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007550 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007551
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007552 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007553 txn->uri = NULL;
7554 txn->srv_cookie = NULL;
7555 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007556
7557 if (txn->req.cap) {
7558 struct cap_hdr *h;
7559 for (h = s->fe->req_cap; h; h = h->next)
7560 pool_free2(h->pool, txn->req.cap[h->index]);
7561 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7562 }
7563
7564 if (txn->rsp.cap) {
7565 struct cap_hdr *h;
7566 for (h = s->fe->rsp_cap; h; h = h->next)
7567 pool_free2(h->pool, txn->rsp.cap[h->index]);
7568 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7569 }
7570
Willy Tarreau0937bc42009-12-22 15:03:09 +01007571}
7572
7573/* to be used at the end of a transaction to prepare a new one */
7574void http_reset_txn(struct session *s)
7575{
7576 http_end_txn(s);
7577 http_init_txn(s);
7578
7579 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007580 s->logs.logwait = s->fe->to_log;
Willy Tarreau827aee92011-03-10 16:55:02 +01007581 s->srv_conn = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +01007582 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007583 /* re-init store persistence */
7584 s->store_count = 0;
7585
Willy Tarreau0937bc42009-12-22 15:03:09 +01007586 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007587
7588 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7589
Willy Tarreau739cfba2010-01-25 23:11:14 +01007590 /* We must trim any excess data from the response buffer, because we
7591 * may have blocked an invalid response from a server that we don't
7592 * want to accidentely forward once we disable the analysers, nor do
7593 * we want those data to come along with next response. A typical
7594 * example of such data would be from a buggy server responding to
7595 * a HEAD with some data, or sending more than the advertised
7596 * content-length.
7597 */
7598 if (unlikely(s->rep->l > s->rep->send_max)) {
7599 s->rep->l = s->rep->send_max;
7600 s->rep->r = s->rep->w + s->rep->l;
7601 if (s->rep->r >= s->rep->data + s->rep->size)
7602 s->rep->r -= s->rep->size;
7603 }
7604
Willy Tarreau0937bc42009-12-22 15:03:09 +01007605 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007606 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007607
Willy Tarreaud04e8582010-05-31 12:31:35 +02007608 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007609 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007610
7611 s->req->rex = TICK_ETERNITY;
7612 s->req->wex = TICK_ETERNITY;
7613 s->req->analyse_exp = TICK_ETERNITY;
7614 s->rep->rex = TICK_ETERNITY;
7615 s->rep->wex = TICK_ETERNITY;
7616 s->rep->analyse_exp = TICK_ETERNITY;
7617}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007618
Willy Tarreauff011f22011-01-06 17:51:27 +01007619void free_http_req_rules(struct list *r) {
7620 struct http_req_rule *tr, *pr;
7621
7622 list_for_each_entry_safe(pr, tr, r, list) {
7623 LIST_DEL(&pr->list);
7624 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7625 free(pr->http_auth.realm);
7626
7627 free(pr);
7628 }
7629}
7630
7631struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7632{
7633 struct http_req_rule *rule;
7634 int cur_arg;
7635
7636 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7637 if (!rule) {
7638 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7639 return NULL;
7640 }
7641
7642 if (!*args[0]) {
7643 goto req_error_parsing;
7644 } else if (!strcmp(args[0], "allow")) {
7645 rule->action = HTTP_REQ_ACT_ALLOW;
7646 cur_arg = 1;
7647 } else if (!strcmp(args[0], "deny")) {
7648 rule->action = HTTP_REQ_ACT_DENY;
7649 cur_arg = 1;
7650 } else if (!strcmp(args[0], "auth")) {
7651 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7652 cur_arg = 1;
7653
7654 while(*args[cur_arg]) {
7655 if (!strcmp(args[cur_arg], "realm")) {
7656 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7657 cur_arg+=2;
7658 continue;
7659 } else
7660 break;
7661 }
7662 } else {
7663req_error_parsing:
7664 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7665 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7666 return NULL;
7667 }
7668
7669 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7670 struct acl_cond *cond;
7671
7672 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7673 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7674 file, linenum, args[0]);
7675 return NULL;
7676 }
7677 rule->cond = cond;
7678 }
7679 else if (*args[cur_arg]) {
7680 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7681 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7682 file, linenum, args[0], args[cur_arg]);
7683 return NULL;
7684 }
7685
7686 return rule;
7687}
7688
Willy Tarreau8797c062007-05-07 00:55:35 +02007689/************************************************************************/
7690/* The code below is dedicated to ACL parsing and matching */
7691/************************************************************************/
7692
7693
7694
7695
7696/* 1. Check on METHOD
7697 * We use the pre-parsed method if it is known, and store its number as an
7698 * integer. If it is unknown, we use the pointer and the length.
7699 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007700static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007701{
7702 int len, meth;
7703
Willy Tarreauae8b7962007-06-09 23:10:04 +02007704 len = strlen(*text);
7705 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007706
7707 pattern->val.i = meth;
7708 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007709 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007710 if (!pattern->ptr.str)
7711 return 0;
7712 pattern->len = len;
7713 }
7714 return 1;
7715}
7716
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007717static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007718acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7719 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007720{
7721 int meth;
7722 struct http_txn *txn = l7;
7723
Willy Tarreaub6866442008-07-14 23:54:42 +02007724 if (!txn)
7725 return 0;
7726
Willy Tarreau655dce92009-11-08 13:10:58 +01007727 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007728 return 0;
7729
Willy Tarreau8797c062007-05-07 00:55:35 +02007730 meth = txn->meth;
7731 test->i = meth;
7732 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007733 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7734 /* ensure the indexes are not affected */
7735 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007736 test->len = txn->req.sl.rq.m_l;
7737 test->ptr = txn->req.sol;
7738 }
7739 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7740 return 1;
7741}
7742
7743static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7744{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007745 int icase;
7746
Willy Tarreau8797c062007-05-07 00:55:35 +02007747 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02007748 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007749
7750 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02007751 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007752
7753 /* Other method, we must compare the strings */
7754 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02007755 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007756
7757 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
7758 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
7759 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007760 return ACL_PAT_FAIL;
7761 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007762}
7763
7764/* 2. Check on Request/Status Version
7765 * We simply compare strings here.
7766 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007767static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007768{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007769 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007770 if (!pattern->ptr.str)
7771 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007772 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007773 return 1;
7774}
7775
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007776static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007777acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7778 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007779{
7780 struct http_txn *txn = l7;
7781 char *ptr;
7782 int len;
7783
Willy Tarreaub6866442008-07-14 23:54:42 +02007784 if (!txn)
7785 return 0;
7786
Willy Tarreau655dce92009-11-08 13:10:58 +01007787 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007788 return 0;
7789
Willy Tarreau8797c062007-05-07 00:55:35 +02007790 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007791 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007792
7793 while ((len-- > 0) && (*ptr++ != '/'));
7794 if (len <= 0)
7795 return 0;
7796
7797 test->ptr = ptr;
7798 test->len = len;
7799
7800 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7801 return 1;
7802}
7803
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007804static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007805acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7806 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007807{
7808 struct http_txn *txn = l7;
7809 char *ptr;
7810 int len;
7811
Willy Tarreaub6866442008-07-14 23:54:42 +02007812 if (!txn)
7813 return 0;
7814
Willy Tarreau655dce92009-11-08 13:10:58 +01007815 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007816 return 0;
7817
Willy Tarreau8797c062007-05-07 00:55:35 +02007818 len = txn->rsp.sl.st.v_l;
7819 ptr = txn->rsp.sol;
7820
7821 while ((len-- > 0) && (*ptr++ != '/'));
7822 if (len <= 0)
7823 return 0;
7824
7825 test->ptr = ptr;
7826 test->len = len;
7827
7828 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7829 return 1;
7830}
7831
7832/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007833static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007834acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7835 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007836{
7837 struct http_txn *txn = l7;
7838 char *ptr;
7839 int len;
7840
Willy Tarreaub6866442008-07-14 23:54:42 +02007841 if (!txn)
7842 return 0;
7843
Willy Tarreau655dce92009-11-08 13:10:58 +01007844 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007845 return 0;
7846
Willy Tarreau8797c062007-05-07 00:55:35 +02007847 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007848 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007849
7850 test->i = __strl2ui(ptr, len);
7851 test->flags = ACL_TEST_F_VOL_1ST;
7852 return 1;
7853}
7854
7855/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007856static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007857acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7858 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007859{
7860 struct http_txn *txn = l7;
7861
Willy Tarreaub6866442008-07-14 23:54:42 +02007862 if (!txn)
7863 return 0;
7864
Willy Tarreau655dce92009-11-08 13:10:58 +01007865 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007866 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007867
Willy Tarreauc11416f2007-06-17 16:58:38 +02007868 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7869 /* ensure the indexes are not affected */
7870 return 0;
7871
Willy Tarreau8797c062007-05-07 00:55:35 +02007872 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007873 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007874
Willy Tarreauf3d25982007-05-08 22:45:09 +02007875 /* we do not need to set READ_ONLY because the data is in a buffer */
7876 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007877 return 1;
7878}
7879
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007880static int
7881acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7882 struct acl_expr *expr, struct acl_test *test)
7883{
7884 struct http_txn *txn = l7;
7885
Willy Tarreaub6866442008-07-14 23:54:42 +02007886 if (!txn)
7887 return 0;
7888
Willy Tarreau655dce92009-11-08 13:10:58 +01007889 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007890 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007891
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007892 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7893 /* ensure the indexes are not affected */
7894 return 0;
7895
7896 /* Parse HTTP request */
Willy Tarreau957c0a52011-03-03 17:42:23 +01007897 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to);
7898 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007899 test->i = AF_INET;
7900
7901 /*
7902 * If we are parsing url in frontend space, we prepare backend stage
7903 * to not parse again the same url ! optimization lazyness...
7904 */
7905 if (px->options & PR_O_HTTP_PROXY)
7906 l4->flags |= SN_ADDR_SET;
7907
7908 test->flags = ACL_TEST_F_READ_ONLY;
7909 return 1;
7910}
7911
7912static int
7913acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7914 struct acl_expr *expr, struct acl_test *test)
7915{
7916 struct http_txn *txn = l7;
7917
Willy Tarreaub6866442008-07-14 23:54:42 +02007918 if (!txn)
7919 return 0;
7920
Willy Tarreau655dce92009-11-08 13:10:58 +01007921 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007922 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007923
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007924 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7925 /* ensure the indexes are not affected */
7926 return 0;
7927
7928 /* Same optimization as url_ip */
Willy Tarreau957c0a52011-03-03 17:42:23 +01007929 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to);
7930 test->i = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007931
7932 if (px->options & PR_O_HTTP_PROXY)
7933 l4->flags |= SN_ADDR_SET;
7934
7935 test->flags = ACL_TEST_F_READ_ONLY;
7936 return 1;
7937}
7938
Willy Tarreauc11416f2007-06-17 16:58:38 +02007939/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7940 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7941 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007942static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007943acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007944 struct acl_expr *expr, struct acl_test *test)
7945{
7946 struct http_txn *txn = l7;
7947 struct hdr_idx *idx = &txn->hdr_idx;
7948 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007949
Willy Tarreaub6866442008-07-14 23:54:42 +02007950 if (!txn)
7951 return 0;
7952
Willy Tarreau33a7e692007-06-10 19:45:56 +02007953 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7954 /* search for header from the beginning */
7955 ctx->idx = 0;
7956
Willy Tarreau33a7e692007-06-10 19:45:56 +02007957 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7958 test->flags |= ACL_TEST_F_FETCH_MORE;
7959 test->flags |= ACL_TEST_F_VOL_HDR;
7960 test->len = ctx->vlen;
7961 test->ptr = (char *)ctx->line + ctx->val;
7962 return 1;
7963 }
7964
7965 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7966 test->flags |= ACL_TEST_F_VOL_HDR;
7967 return 0;
7968}
7969
Willy Tarreau33a7e692007-06-10 19:45:56 +02007970static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007971acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7972 struct acl_expr *expr, struct acl_test *test)
7973{
7974 struct http_txn *txn = l7;
7975
Willy Tarreaub6866442008-07-14 23:54:42 +02007976 if (!txn)
7977 return 0;
7978
Willy Tarreau655dce92009-11-08 13:10:58 +01007979 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007980 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007981
Willy Tarreauc11416f2007-06-17 16:58:38 +02007982 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7983 /* ensure the indexes are not affected */
7984 return 0;
7985
7986 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7987}
7988
7989static int
7990acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7991 struct acl_expr *expr, struct acl_test *test)
7992{
7993 struct http_txn *txn = l7;
7994
Willy Tarreaub6866442008-07-14 23:54:42 +02007995 if (!txn)
7996 return 0;
7997
Willy Tarreau655dce92009-11-08 13:10:58 +01007998 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007999 return 0;
8000
8001 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
8002}
8003
8004/* 6. Check on HTTP header count. The number of occurrences is returned.
8005 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8006 */
8007static int
8008acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008009 struct acl_expr *expr, struct acl_test *test)
8010{
8011 struct http_txn *txn = l7;
8012 struct hdr_idx *idx = &txn->hdr_idx;
8013 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008014 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008015
Willy Tarreaub6866442008-07-14 23:54:42 +02008016 if (!txn)
8017 return 0;
8018
Willy Tarreau33a7e692007-06-10 19:45:56 +02008019 ctx.idx = 0;
8020 cnt = 0;
8021 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
8022 cnt++;
8023
8024 test->i = cnt;
8025 test->flags = ACL_TEST_F_VOL_HDR;
8026 return 1;
8027}
8028
Willy Tarreauc11416f2007-06-17 16:58:38 +02008029static int
8030acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8031 struct acl_expr *expr, struct acl_test *test)
8032{
8033 struct http_txn *txn = l7;
8034
Willy Tarreaub6866442008-07-14 23:54:42 +02008035 if (!txn)
8036 return 0;
8037
Willy Tarreau655dce92009-11-08 13:10:58 +01008038 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008039 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008040
Willy Tarreauc11416f2007-06-17 16:58:38 +02008041 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8042 /* ensure the indexes are not affected */
8043 return 0;
8044
8045 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
8046}
8047
8048static int
8049acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8050 struct acl_expr *expr, struct acl_test *test)
8051{
8052 struct http_txn *txn = l7;
8053
Willy Tarreaub6866442008-07-14 23:54:42 +02008054 if (!txn)
8055 return 0;
8056
Willy Tarreau655dce92009-11-08 13:10:58 +01008057 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008058 return 0;
8059
8060 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
8061}
8062
Willy Tarreau33a7e692007-06-10 19:45:56 +02008063/* 7. Check on HTTP header's integer value. The integer value is returned.
8064 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008065 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008066 */
8067static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02008068acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008069 struct acl_expr *expr, struct acl_test *test)
8070{
8071 struct http_txn *txn = l7;
8072 struct hdr_idx *idx = &txn->hdr_idx;
8073 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008074
Willy Tarreaub6866442008-07-14 23:54:42 +02008075 if (!txn)
8076 return 0;
8077
Willy Tarreau33a7e692007-06-10 19:45:56 +02008078 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8079 /* search for header from the beginning */
8080 ctx->idx = 0;
8081
Willy Tarreau33a7e692007-06-10 19:45:56 +02008082 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8083 test->flags |= ACL_TEST_F_FETCH_MORE;
8084 test->flags |= ACL_TEST_F_VOL_HDR;
8085 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
8086 return 1;
8087 }
8088
8089 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8090 test->flags |= ACL_TEST_F_VOL_HDR;
8091 return 0;
8092}
8093
Willy Tarreauc11416f2007-06-17 16:58:38 +02008094static int
8095acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8096 struct acl_expr *expr, struct acl_test *test)
8097{
8098 struct http_txn *txn = l7;
8099
Willy Tarreaub6866442008-07-14 23:54:42 +02008100 if (!txn)
8101 return 0;
8102
Willy Tarreau655dce92009-11-08 13:10:58 +01008103 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008104 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008105
Willy Tarreauc11416f2007-06-17 16:58:38 +02008106 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8107 /* ensure the indexes are not affected */
8108 return 0;
8109
8110 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
8111}
8112
8113static int
8114acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8115 struct acl_expr *expr, struct acl_test *test)
8116{
8117 struct http_txn *txn = l7;
8118
Willy Tarreaub6866442008-07-14 23:54:42 +02008119 if (!txn)
8120 return 0;
8121
Willy Tarreau655dce92009-11-08 13:10:58 +01008122 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008123 return 0;
8124
8125 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
8126}
8127
Willy Tarreau106f9792009-09-19 07:54:16 +02008128/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
8129 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8130 */
8131static int
8132acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
8133 struct acl_expr *expr, struct acl_test *test)
8134{
8135 struct http_txn *txn = l7;
8136 struct hdr_idx *idx = &txn->hdr_idx;
8137 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
8138
8139 if (!txn)
8140 return 0;
8141
8142 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8143 /* search for header from the beginning */
8144 ctx->idx = 0;
8145
8146 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8147 test->flags |= ACL_TEST_F_FETCH_MORE;
8148 test->flags |= ACL_TEST_F_VOL_HDR;
8149 /* Same optimization as url_ip */
David du Colombier6f5ccb12011-03-10 22:26:24 +01008150 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));
8151 url2ipv4((char *)ctx->line + ctx->val, &((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr);
8152 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr;
Willy Tarreau106f9792009-09-19 07:54:16 +02008153 test->i = AF_INET;
8154 return 1;
8155 }
8156
8157 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8158 test->flags |= ACL_TEST_F_VOL_HDR;
8159 return 0;
8160}
8161
8162static int
8163acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8164 struct acl_expr *expr, struct acl_test *test)
8165{
8166 struct http_txn *txn = l7;
8167
8168 if (!txn)
8169 return 0;
8170
Willy Tarreau655dce92009-11-08 13:10:58 +01008171 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008172 return 0;
8173
8174 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8175 /* ensure the indexes are not affected */
8176 return 0;
8177
8178 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8179}
8180
8181static int
8182acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8183 struct acl_expr *expr, struct acl_test *test)
8184{
8185 struct http_txn *txn = l7;
8186
8187 if (!txn)
8188 return 0;
8189
Willy Tarreau655dce92009-11-08 13:10:58 +01008190 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008191 return 0;
8192
8193 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8194}
8195
Willy Tarreau737b0c12007-06-10 21:28:46 +02008196/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8197 * the first '/' after the possible hostname, and ends before the possible '?'.
8198 */
8199static int
8200acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8201 struct acl_expr *expr, struct acl_test *test)
8202{
8203 struct http_txn *txn = l7;
8204 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008205
Willy Tarreaub6866442008-07-14 23:54:42 +02008206 if (!txn)
8207 return 0;
8208
Willy Tarreau655dce92009-11-08 13:10:58 +01008209 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008210 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008211
Willy Tarreauc11416f2007-06-17 16:58:38 +02008212 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8213 /* ensure the indexes are not affected */
8214 return 0;
8215
Willy Tarreau962c3f42010-01-10 00:15:35 +01008216 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008217 ptr = http_get_path(txn);
8218 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008219 return 0;
8220
8221 /* OK, we got the '/' ! */
8222 test->ptr = ptr;
8223
8224 while (ptr < end && *ptr != '?')
8225 ptr++;
8226
8227 test->len = ptr - test->ptr;
8228
8229 /* we do not need to set READ_ONLY because the data is in a buffer */
8230 test->flags = ACL_TEST_F_VOL_1ST;
8231 return 1;
8232}
8233
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008234static int
8235acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8236 struct acl_expr *expr, struct acl_test *test)
8237{
8238 struct buffer *req = s->req;
8239 struct http_txn *txn = &s->txn;
8240 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008241
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008242 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8243 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8244 */
8245
8246 if (!s || !req)
8247 return 0;
8248
Willy Tarreau655dce92009-11-08 13:10:58 +01008249 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008250 /* Already decoded as OK */
8251 test->flags |= ACL_TEST_F_SET_RES_PASS;
8252 return 1;
8253 }
8254
8255 /* Try to decode HTTP request */
8256 if (likely(req->lr < req->r))
8257 http_msg_analyzer(req, msg, &txn->hdr_idx);
8258
Willy Tarreau655dce92009-11-08 13:10:58 +01008259 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008260 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8261 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8262 return 1;
8263 }
8264 /* wait for final state */
8265 test->flags |= ACL_TEST_F_MAY_CHANGE;
8266 return 0;
8267 }
8268
8269 /* OK we got a valid HTTP request. We have some minor preparation to
8270 * perform so that further checks can rely on HTTP tests.
8271 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008272 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008273 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8274 s->flags |= SN_REDIRECTABLE;
8275
8276 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8277 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8278 return 1;
8279 }
8280
8281 test->flags |= ACL_TEST_F_SET_RES_PASS;
8282 return 1;
8283}
8284
Willy Tarreau7f18e522010-10-22 20:04:13 +02008285/* return a valid test if the current request is the first one on the connection */
8286static int
8287acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8288 struct acl_expr *expr, struct acl_test *test)
8289{
8290 if (!s)
8291 return 0;
8292
8293 if (s->txn.flags & TX_NOT_FIRST)
8294 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8295 else
8296 test->flags |= ACL_TEST_F_SET_RES_PASS;
8297
8298 return 1;
8299}
8300
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008301static int
8302acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8303 struct acl_expr *expr, struct acl_test *test)
8304{
8305
8306 if (!s)
8307 return 0;
8308
8309 if (!get_http_auth(s))
8310 return 0;
8311
8312 test->ctx.a[0] = expr->arg.ul;
8313 test->ctx.a[1] = s->txn.auth.user;
8314 test->ctx.a[2] = s->txn.auth.pass;
8315
8316 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8317
8318 return 1;
8319}
Willy Tarreau8797c062007-05-07 00:55:35 +02008320
8321/************************************************************************/
8322/* All supported keywords must be declared here. */
8323/************************************************************************/
8324
8325/* Note: must not be declared <const> as its list will be overwritten */
8326static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008327 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8328
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008329 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008330 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8331 { "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 +02008332 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008333
Willy Tarreauc4262962010-05-10 23:42:40 +02008334 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008335 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8336 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8337 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8338 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8339 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8340 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008341 { "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 +02008342 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008343
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008344 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008345 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008346 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8347 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8348 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8349 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8350 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8351 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8352 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
8353 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008354 { "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 +02008355
Willy Tarreauc4262962010-05-10 23:42:40 +02008356 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008357 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8358 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8359 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8360 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8361 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8362 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8363 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
8364 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008365 { "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 +02008366
Willy Tarreauc4262962010-05-10 23:42:40 +02008367 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008368 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8369 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8370 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8371 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8372 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8373 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008374
Willy Tarreauf3d25982007-05-08 22:45:09 +02008375#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008376 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8377 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8378 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8379 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8380 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8381 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8382 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8383
Willy Tarreau8797c062007-05-07 00:55:35 +02008384 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8385 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8386 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8387 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8388 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8389 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8390 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8391 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008392#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008393
Willy Tarreau7f18e522010-10-22 20:04:13 +02008394 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8395 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8396 { "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 +02008397 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008398}};
8399
Willy Tarreau4a568972010-05-12 08:08:50 +02008400/************************************************************************/
8401/* The code below is dedicated to pattern fetching and matching */
8402/************************************************************************/
8403
8404/* extract the IP address from the last occurrence of specified header. Note
8405 * that we should normally first extract the string then convert it to IP,
8406 * but right now we have all the functions to do this seemlessly, and we will
8407 * be able to change that later without touching the configuration.
8408 */
8409static int
8410pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02008411 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008412{
8413 struct http_txn *txn = l7;
8414
Emeric Brun485479d2010-09-23 18:02:19 +02008415 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 +02008416 return data->ip.s_addr != 0;
8417}
8418
David Cournapeau16023ee2010-12-23 20:55:41 +09008419/*
8420 * Given a path string and its length, find the position of beginning of the
8421 * query string. Returns NULL if no query string is found in the path.
8422 *
8423 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8424 *
8425 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8426 */
8427static inline char *find_query_string(char *path, size_t path_l)
8428{
8429 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008430
David Cournapeau16023ee2010-12-23 20:55:41 +09008431 p = memchr(path, '?', path_l);
8432 return p ? p + 1 : NULL;
8433}
8434
8435static inline int is_param_delimiter(char c)
8436{
8437 return c == '&' || c == ';';
8438}
8439
8440/*
8441 * Given a url parameter, find the starting position of the first occurence,
8442 * or NULL if the parameter is not found.
8443 *
8444 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8445 * the function will return query_string+8.
8446 */
8447static char*
8448find_url_param_pos(char* query_string, size_t query_string_l,
8449 char* url_param_name, size_t url_param_name_l)
8450{
8451 char *pos, *last;
8452
8453 pos = query_string;
8454 last = query_string + query_string_l - url_param_name_l - 1;
8455
8456 while (pos <= last) {
8457 if (pos[url_param_name_l] == '=') {
8458 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8459 return pos;
8460 pos += url_param_name_l + 1;
8461 }
8462 while (pos <= last && !is_param_delimiter(*pos))
8463 pos++;
8464 pos++;
8465 }
8466 return NULL;
8467}
8468
8469/*
8470 * Given a url parameter name, returns its value and size into *value and
8471 * *value_l respectively, and returns non-zero. If the parameter is not found,
8472 * zero is returned and value/value_l are not touched.
8473 */
8474static int
8475find_url_param_value(char* path, size_t path_l,
8476 char* url_param_name, size_t url_param_name_l,
8477 char** value, size_t* value_l)
8478{
8479 char *query_string, *qs_end;
8480 char *arg_start;
8481 char *value_start, *value_end;
8482
8483 query_string = find_query_string(path, path_l);
8484 if (!query_string)
8485 return 0;
8486
8487 qs_end = path + path_l;
8488 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8489 url_param_name, url_param_name_l);
8490 if (!arg_start)
8491 return 0;
8492
8493 value_start = arg_start + url_param_name_l + 1;
8494 value_end = value_start;
8495
8496 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8497 value_end++;
8498
8499 *value = value_start;
8500 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008501 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008502}
8503
8504static int
8505pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8506 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8507{
8508 struct http_txn *txn = l7;
8509 struct http_msg *msg = &txn->req;
8510 char *url_param_value;
8511 size_t url_param_value_l;
8512
8513 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8514 arg_p->data.str.str, arg_p->data.str.len,
8515 &url_param_value, &url_param_value_l))
8516 return 0;
8517
8518 data->str.str = url_param_value;
8519 data->str.len = url_param_value_l;
8520 return 1;
8521}
8522
Emeric Brun485479d2010-09-23 18:02:19 +02008523
Willy Tarreau4a568972010-05-12 08:08:50 +02008524/************************************************************************/
8525/* All supported keywords must be declared here. */
8526/************************************************************************/
8527/* Note: must not be declared <const> as its list will be overwritten */
8528static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Emeric Brun485479d2010-09-23 18:02:19 +02008529 { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008530 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Emeric Brun485479d2010-09-23 18:02:19 +02008531 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008532}};
8533
Willy Tarreau8797c062007-05-07 00:55:35 +02008534
8535__attribute__((constructor))
8536static void __http_protocol_init(void)
8537{
8538 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008539 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008540}
8541
8542
Willy Tarreau58f10d72006-12-04 02:26:12 +01008543/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008544 * Local variables:
8545 * c-indent-level: 8
8546 * c-basic-offset: 8
8547 * End:
8548 */