blob: 865eaa494544e5ea6cd61fe4df0a17b6d342b4f5 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010027#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/compat.h>
29#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010030#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreau8797c062007-05-07 00:55:35 +020042#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010043#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/backend.h>
45#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010046#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020049#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010051#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020052#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020053#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010055#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010059#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020060#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/task.h>
62
Willy Tarreau522d6c02009-12-06 18:49:18 +010063const char HTTP_100[] =
64 "HTTP/1.1 100 Continue\r\n\r\n";
65
66const struct chunk http_100_chunk = {
67 .str = (char *)&HTTP_100,
68 .len = sizeof(HTTP_100)-1
69};
70
Willy Tarreau1c47f852006-07-09 08:22:27 +020071/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010072const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020073 "HTTP/1.0 200 OK\r\n"
74 "Cache-Control: no-cache\r\n"
75 "Connection: close\r\n"
76 "Content-Type: text/html\r\n"
77 "\r\n"
78 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
79
Willy Tarreau0f772532006-12-23 20:51:41 +010080const struct chunk http_200_chunk = {
81 .str = (char *)&HTTP_200,
82 .len = sizeof(HTTP_200)-1
83};
84
Willy Tarreaua9679ac2010-01-03 17:32:57 +010085/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020086const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020090 "Location: "; /* not terminated since it will be concatenated with the URL */
91
Willy Tarreau0f772532006-12-23 20:51:41 +010092const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010095 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010096 "Location: "; /* not terminated since it will be concatenated with the URL */
97
98/* same as 302 except that the browser MUST retry with the GET method */
99const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100100 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100101 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100102 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
106const char *HTTP_401_fmt =
107 "HTTP/1.0 401 Unauthorized\r\n"
108 "Cache-Control: no-cache\r\n"
109 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200110 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
112 "\r\n"
113 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
114
Willy Tarreau844a7e72010-01-31 21:46:18 +0100115const char *HTTP_407_fmt =
116 "HTTP/1.0 407 Unauthorized\r\n"
117 "Cache-Control: no-cache\r\n"
118 "Connection: close\r\n"
119 "Content-Type: text/html\r\n"
120 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
121 "\r\n"
122 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
123
Willy Tarreau0f772532006-12-23 20:51:41 +0100124
125const int http_err_codes[HTTP_ERR_SIZE] = {
126 [HTTP_ERR_400] = 400,
127 [HTTP_ERR_403] = 403,
128 [HTTP_ERR_408] = 408,
129 [HTTP_ERR_500] = 500,
130 [HTTP_ERR_502] = 502,
131 [HTTP_ERR_503] = 503,
132 [HTTP_ERR_504] = 504,
133};
134
Willy Tarreau80587432006-12-24 17:47:20 +0100135static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100137 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 "Cache-Control: no-cache\r\n"
139 "Connection: close\r\n"
140 "Content-Type: text/html\r\n"
141 "\r\n"
142 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
143
144 [HTTP_ERR_403] =
145 "HTTP/1.0 403 Forbidden\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
151
152 [HTTP_ERR_408] =
153 "HTTP/1.0 408 Request Time-out\r\n"
154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
159
160 [HTTP_ERR_500] =
161 "HTTP/1.0 500 Server Error\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
167
168 [HTTP_ERR_502] =
169 "HTTP/1.0 502 Bad Gateway\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
175
176 [HTTP_ERR_503] =
177 "HTTP/1.0 503 Service Unavailable\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
183
184 [HTTP_ERR_504] =
185 "HTTP/1.0 504 Gateway Time-out\r\n"
186 "Cache-Control: no-cache\r\n"
187 "Connection: close\r\n"
188 "Content-Type: text/html\r\n"
189 "\r\n"
190 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
191
192};
193
Willy Tarreau80587432006-12-24 17:47:20 +0100194/* We must put the messages here since GCC cannot initialize consts depending
195 * on strlen().
196 */
197struct chunk http_err_chunks[HTTP_ERR_SIZE];
198
Willy Tarreau42250582007-04-01 01:30:43 +0200199#define FD_SETS_ARE_BITFIELDS
200#ifdef FD_SETS_ARE_BITFIELDS
201/*
202 * This map is used with all the FD_* macros to check whether a particular bit
203 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
204 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
205 * byte should be encoded. Be careful to always pass bytes from 0 to 255
206 * exclusively to the macros.
207 */
208fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
209fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
210
211#else
212#error "Check if your OS uses bitfields for fd_sets"
213#endif
214
Willy Tarreau80587432006-12-24 17:47:20 +0100215void init_proto_http()
216{
Willy Tarreau42250582007-04-01 01:30:43 +0200217 int i;
218 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100219 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200220
Willy Tarreau80587432006-12-24 17:47:20 +0100221 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
222 if (!http_err_msgs[msg]) {
223 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
224 abort();
225 }
226
227 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
228 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
229 }
Willy Tarreau42250582007-04-01 01:30:43 +0200230
231 /* initialize the log header encoding map : '{|}"#' should be encoded with
232 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
233 * URL encoding only requires '"', '#' to be encoded as well as non-
234 * printable characters above.
235 */
236 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
237 memset(url_encode_map, 0, sizeof(url_encode_map));
238 for (i = 0; i < 32; i++) {
239 FD_SET(i, hdr_encode_map);
240 FD_SET(i, url_encode_map);
241 }
242 for (i = 127; i < 256; i++) {
243 FD_SET(i, hdr_encode_map);
244 FD_SET(i, url_encode_map);
245 }
246
247 tmp = "\"#{|}";
248 while (*tmp) {
249 FD_SET(*tmp, hdr_encode_map);
250 tmp++;
251 }
252
253 tmp = "\"#";
254 while (*tmp) {
255 FD_SET(*tmp, url_encode_map);
256 tmp++;
257 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200258
259 /* memory allocations */
260 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200261 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100262}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263
Willy Tarreau53b6c742006-12-17 13:37:46 +0100264/*
265 * We have 26 list of methods (1 per first letter), each of which can have
266 * up to 3 entries (2 valid, 1 null).
267 */
268struct http_method_desc {
269 http_meth_t meth;
270 int len;
271 const char text[8];
272};
273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100274const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100275 ['C' - 'A'] = {
276 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
277 },
278 ['D' - 'A'] = {
279 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
280 },
281 ['G' - 'A'] = {
282 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
283 },
284 ['H' - 'A'] = {
285 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
286 },
287 ['P' - 'A'] = {
288 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
289 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
290 },
291 ['T' - 'A'] = {
292 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
293 },
294 /* rest is empty like this :
295 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
296 */
297};
298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100299/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200300 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100301 * RFC2616 for those chars.
302 */
303
304const char http_is_spht[256] = {
305 [' '] = 1, ['\t'] = 1,
306};
307
308const char http_is_crlf[256] = {
309 ['\r'] = 1, ['\n'] = 1,
310};
311
312const char http_is_lws[256] = {
313 [' '] = 1, ['\t'] = 1,
314 ['\r'] = 1, ['\n'] = 1,
315};
316
317const char http_is_sep[256] = {
318 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
319 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
320 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
321 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
322 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
323};
324
325const char http_is_ctl[256] = {
326 [0 ... 31] = 1,
327 [127] = 1,
328};
329
330/*
331 * A token is any ASCII char that is neither a separator nor a CTL char.
332 * Do not overwrite values in assignment since gcc-2.95 will not handle
333 * them correctly. Instead, define every non-CTL char's status.
334 */
335const char http_is_token[256] = {
336 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
337 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
338 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
339 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
340 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
341 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
342 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
343 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
344 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
345 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
346 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
347 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
348 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
349 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
350 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
351 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
352 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
353 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
354 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
355 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
356 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
357 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
358 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
359 ['|'] = 1, ['}'] = 0, ['~'] = 1,
360};
361
362
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100363/*
364 * An http ver_token is any ASCII which can be found in an HTTP version,
365 * which includes 'H', 'T', 'P', '/', '.' and any digit.
366 */
367const char http_is_ver_token[256] = {
368 ['.'] = 1, ['/'] = 1,
369 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
370 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
371 ['H'] = 1, ['P'] = 1, ['T'] = 1,
372};
373
374
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100375/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100376 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
377 */
378#if defined(DEBUG_FSM)
379static void http_silent_debug(int line, struct session *s)
380{
381 int size = 0;
382 size += snprintf(trash + size, sizeof(trash) - size,
383 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n",
384 line,
385 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
386 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags);
387 write(-1, trash, size);
388 size = 0;
389 size += snprintf(trash + size, sizeof(trash) - size,
390 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n",
391 line,
392 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
393 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward);
394
395 write(-1, trash, size);
396}
397#else
398#define http_silent_debug(l,s) do { } while (0)
399#endif
400
401/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100402 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
403 * CRLF. Text length is measured first, so it cannot be NULL.
404 * The header is also automatically added to the index <hdr_idx>, and the end
405 * of headers is automatically adjusted. The number of bytes added is returned
406 * on success, otherwise <0 is returned indicating an error.
407 */
408int http_header_add_tail(struct buffer *b, struct http_msg *msg,
409 struct hdr_idx *hdr_idx, const char *text)
410{
411 int bytes, len;
412
413 len = strlen(text);
414 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
415 if (!bytes)
416 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100417 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100418 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
419}
420
421/*
422 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
423 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
424 * the buffer is only opened and the space reserved, but nothing is copied.
425 * The header is also automatically added to the index <hdr_idx>, and the end
426 * of headers is automatically adjusted. The number of bytes added is returned
427 * on success, otherwise <0 is returned indicating an error.
428 */
429int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
430 struct hdr_idx *hdr_idx, const char *text, int len)
431{
432 int bytes;
433
434 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
435 if (!bytes)
436 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100437 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100438 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
439}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440
441/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100442 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
443 * If so, returns the position of the first non-space character relative to
444 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
445 * to return a pointer to the place after the first space. Returns 0 if the
446 * header name does not match. Checks are case-insensitive.
447 */
448int http_header_match2(const char *hdr, const char *end,
449 const char *name, int len)
450{
451 const char *val;
452
453 if (hdr + len >= end)
454 return 0;
455 if (hdr[len] != ':')
456 return 0;
457 if (strncasecmp(hdr, name, len) != 0)
458 return 0;
459 val = hdr + len + 1;
460 while (val < end && HTTP_IS_SPHT(*val))
461 val++;
462 if ((val >= end) && (len + 2 <= end - hdr))
463 return len + 2; /* we may replace starting from second space */
464 return val - hdr;
465}
466
Willy Tarreau68085d82010-01-18 14:54:04 +0100467/* Find the end of the header value contained between <s> and <e>. See RFC2616,
468 * par 2.2 for more information. Note that it requires a valid header to return
469 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200470 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100471char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200472{
473 int quoted, qdpair;
474
475 quoted = qdpair = 0;
476 for (; s < e; s++) {
477 if (qdpair) qdpair = 0;
478 else if (quoted && *s == '\\') qdpair = 1;
479 else if (quoted && *s == '"') quoted = 0;
480 else if (*s == '"') quoted = 1;
481 else if (*s == ',') return s;
482 }
483 return s;
484}
485
486/* Find the first or next occurrence of header <name> in message buffer <sol>
487 * using headers index <idx>, and return it in the <ctx> structure. This
488 * structure holds everything necessary to use the header and find next
489 * occurrence. If its <idx> member is 0, the header is searched from the
490 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100491 * 1 when it finds a value, and 0 when there is no more. It is designed to work
492 * with headers defined as comma-separated lists. As a special case, if ctx->val
493 * is NULL when searching for a new values of a header, the current header is
494 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200495 */
496int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100497 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200498 struct hdr_ctx *ctx)
499{
Willy Tarreau68085d82010-01-18 14:54:04 +0100500 char *eol, *sov;
501 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200502
Willy Tarreau68085d82010-01-18 14:54:04 +0100503 cur_idx = ctx->idx;
504 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200505 /* We have previously returned a value, let's search
506 * another one on the same line.
507 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100509 ctx->del = ctx->val + ctx->vlen;
510 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200511 eol = sol + idx->v[cur_idx].len;
512
513 if (sov >= eol)
514 /* no more values in this header */
515 goto next_hdr;
516
Willy Tarreau68085d82010-01-18 14:54:04 +0100517 /* values remaining for this header, skip the comma but save it
518 * for later use (eg: for header deletion).
519 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200520 sov++;
521 while (sov < eol && http_is_lws[(unsigned char)*sov])
522 sov++;
523
524 goto return_hdr;
525 }
526
527 /* first request for this header */
528 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100529 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200530 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200531 while (cur_idx) {
532 eol = sol + idx->v[cur_idx].len;
533
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200534 if (len == 0) {
535 /* No argument was passed, we want any header.
536 * To achieve this, we simply build a fake request. */
537 while (sol + len < eol && sol[len] != ':')
538 len++;
539 name = sol;
540 }
541
Willy Tarreau33a7e692007-06-10 19:45:56 +0200542 if ((len < eol - sol) &&
543 (sol[len] == ':') &&
544 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100545 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200546 sov = sol + len + 1;
547 while (sov < eol && http_is_lws[(unsigned char)*sov])
548 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100549
Willy Tarreau33a7e692007-06-10 19:45:56 +0200550 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100551 ctx->prev = old_idx;
552 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200553 ctx->idx = cur_idx;
554 ctx->val = sov - sol;
555
556 eol = find_hdr_value_end(sov, eol);
557 ctx->vlen = eol - sov;
558 return 1;
559 }
560 next_hdr:
561 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100562 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200563 cur_idx = idx->v[cur_idx].next;
564 }
565 return 0;
566}
567
568int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100569 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200570 struct hdr_ctx *ctx)
571{
572 return http_find_header2(name, strlen(name), sol, idx, ctx);
573}
574
Willy Tarreau68085d82010-01-18 14:54:04 +0100575/* Remove one value of a header. This only works on a <ctx> returned by one of
576 * the http_find_header functions. The value is removed, as well as surrounding
577 * commas if any. If the removed value was alone, the whole header is removed.
578 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
579 * message <msg>. The new index is returned. If it is zero, it means there is
580 * no more header, so any processing may stop. The ctx is always left in a form
581 * that can be handled by http_find_header2() to find next occurrence.
582 */
583int http_remove_header2(struct http_msg *msg, struct buffer *buf,
584 struct hdr_idx *idx, struct hdr_ctx *ctx)
585{
586 int cur_idx = ctx->idx;
587 char *sol = ctx->line;
588 struct hdr_idx_elem *hdr;
589 int delta, skip_comma;
590
591 if (!cur_idx)
592 return 0;
593
594 hdr = &idx->v[cur_idx];
595 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) {
596 /* This was the only value of the header, we must now remove it entirely. */
597 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
598 http_msg_move_end(msg, delta);
599 idx->used--;
600 hdr->len = 0; /* unused entry */
601 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
602 ctx->idx = ctx->prev; /* walk back to the end of previous header */
603 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
604 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
605 ctx->vlen = 0;
606 return ctx->idx;
607 }
608
609 /* This was not the only value of this header. We have to remove between
610 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
611 * of the list, we remove the last separator.
612 */
613
614 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
615 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
616 sol + ctx->val + ctx->vlen + skip_comma,
617 NULL, 0);
618 hdr->len += delta;
619 http_msg_move_end(msg, delta);
620 ctx->val = ctx->del;
621 ctx->vlen = 0;
622 return ctx->idx;
623}
624
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100625/* This function handles a server error at the stream interface level. The
626 * stream interface is assumed to be already in a closed state. An optional
627 * message is copied into the input buffer, and an HTTP status code stored.
628 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100629 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200630 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100631static void http_server_error(struct session *t, struct stream_interface *si,
632 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200633{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100634 buffer_auto_read(si->ob);
635 buffer_abort(si->ob);
636 buffer_auto_close(si->ob);
637 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200638 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100639 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100640 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100641 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100642 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200643 }
644 if (!(t->flags & SN_ERR_MASK))
645 t->flags |= err;
646 if (!(t->flags & SN_FINST_MASK))
647 t->flags |= finst;
648}
649
Willy Tarreau80587432006-12-24 17:47:20 +0100650/* This function returns the appropriate error location for the given session
651 * and message.
652 */
653
654struct chunk *error_message(struct session *s, int msgnum)
655{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200656 if (s->be->errmsg[msgnum].str)
657 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100658 else if (s->fe->errmsg[msgnum].str)
659 return &s->fe->errmsg[msgnum];
660 else
661 return &http_err_chunks[msgnum];
662}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200663
Willy Tarreau53b6c742006-12-17 13:37:46 +0100664/*
665 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
666 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
667 */
668static http_meth_t find_http_meth(const char *str, const int len)
669{
670 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100671 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100672
673 m = ((unsigned)*str - 'A');
674
675 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100676 for (h = http_methods[m]; h->len > 0; h++) {
677 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100678 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100679 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100680 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100681 };
682 return HTTP_METH_OTHER;
683 }
684 return HTTP_METH_NONE;
685
686}
687
Willy Tarreau21d2af32008-02-14 20:25:24 +0100688/* Parse the URI from the given transaction (which is assumed to be in request
689 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
690 * It is returned otherwise.
691 */
692static char *
693http_get_path(struct http_txn *txn)
694{
695 char *ptr, *end;
696
Willy Tarreau962c3f42010-01-10 00:15:35 +0100697 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100698 end = ptr + txn->req.sl.rq.u_l;
699
700 if (ptr >= end)
701 return NULL;
702
703 /* RFC2616, par. 5.1.2 :
704 * Request-URI = "*" | absuri | abspath | authority
705 */
706
707 if (*ptr == '*')
708 return NULL;
709
710 if (isalpha((unsigned char)*ptr)) {
711 /* this is a scheme as described by RFC3986, par. 3.1 */
712 ptr++;
713 while (ptr < end &&
714 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
715 ptr++;
716 /* skip '://' */
717 if (ptr == end || *ptr++ != ':')
718 return NULL;
719 if (ptr == end || *ptr++ != '/')
720 return NULL;
721 if (ptr == end || *ptr++ != '/')
722 return NULL;
723 }
724 /* skip [user[:passwd]@]host[:[port]] */
725
726 while (ptr < end && *ptr != '/')
727 ptr++;
728
729 if (ptr == end)
730 return NULL;
731
732 /* OK, we got the '/' ! */
733 return ptr;
734}
735
Willy Tarreauefb453c2008-10-26 20:49:47 +0100736/* Returns a 302 for a redirectable request. This may only be called just after
737 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
738 * left unchanged and will follow normal proxy processing.
739 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100740void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100741{
742 struct http_txn *txn;
743 struct chunk rdr;
744 char *path;
745 int len;
746
747 /* 1: create the response header */
748 rdr.len = strlen(HTTP_302);
749 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100750 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100751 memcpy(rdr.str, HTTP_302, rdr.len);
752
753 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200754 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100755 return;
756
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100757 /* special prefix "/" means don't change URL */
758 if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') {
759 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
760 rdr.len += s->srv->rdr_len;
761 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100762
763 /* 3: add the request URI */
764 txn = &s->txn;
765 path = http_get_path(txn);
766 if (!path)
767 return;
768
Willy Tarreau962c3f42010-01-10 00:15:35 +0100769 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200770 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100771 return;
772
773 memcpy(rdr.str + rdr.len, path, len);
774 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100775
776 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
777 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
778 rdr.len += 29;
779 } else {
780 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
781 rdr.len += 23;
782 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100783
784 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100785 si->shutr(si);
786 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100787 si->err_type = SI_ET_NONE;
788 si->err_loc = NULL;
789 si->state = SI_ST_CLO;
790
791 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100792 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100793
794 /* FIXME: we should increase a counter of redirects per server and per backend. */
795 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100796 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100797}
798
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100799/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100800 * that the server side is closed. Note that err_type is actually a
801 * bitmask, where almost only aborts may be cumulated with other
802 * values. We consider that aborted operations are more important
803 * than timeouts or errors due to the fact that nobody else in the
804 * logs might explain incomplete retries. All others should avoid
805 * being cumulated. It should normally not be possible to have multiple
806 * aborts at once, but just in case, the first one in sequence is reported.
807 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100808void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100809{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100810 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100811
812 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100813 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
814 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100815 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100816 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
817 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100818 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100819 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
820 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100821 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100822 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
823 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100824 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100825 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
826 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100827 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100828 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
829 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100830 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100831 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
832 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100833}
834
Willy Tarreau42250582007-04-01 01:30:43 +0200835extern const char sess_term_cond[8];
836extern const char sess_fin_state[8];
837extern const char *monthname[12];
838const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
839const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
840 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
841 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200842struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200843struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100844
Emeric Brun3a058f32009-06-30 18:26:00 +0200845void http_sess_clflog(struct session *s)
846{
847 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
848 struct proxy *fe = s->fe;
849 struct proxy *be = s->be;
850 struct proxy *prx_log;
851 struct http_txn *txn = &s->txn;
852 int tolog, level, err;
853 char *uri, *h;
854 char *svid;
855 struct tm tm;
856 static char tmpline[MAX_SYSLOG_LEN];
857 int hdr;
858 size_t w;
859 int t_request;
860
861 prx_log = fe;
862 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +0200863 (s->req->cons->conn_retries != be->conn_retries) ||
Emeric Brun3a058f32009-06-30 18:26:00 +0200864 txn->status >= 500;
865
866 if (s->cli_addr.ss_family == AF_INET)
867 inet_ntop(AF_INET,
868 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
869 pn, sizeof(pn));
870 else
871 inet_ntop(AF_INET6,
872 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
873 pn, sizeof(pn));
874
875 get_gmtime(s->logs.accept_date.tv_sec, &tm);
876
877 /* FIXME: let's limit ourselves to frontend logging for now. */
878 tolog = fe->to_log;
879
880 h = tmpline;
881
882 w = snprintf(h, sizeof(tmpline),
883 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
884 pn,
885 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
886 tm.tm_hour, tm.tm_min, tm.tm_sec);
887 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
888 goto trunc;
889 h += w;
890
891 if (h >= tmpline + sizeof(tmpline) - 4)
892 goto trunc;
893
894 *(h++) = ' ';
895 *(h++) = '\"';
896 uri = txn->uri ? txn->uri : "<BADREQ>";
897 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
898 '#', url_encode_map, uri);
899 *(h++) = '\"';
900
901 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
902 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
903 goto trunc;
904 h += w;
905
906 if (h >= tmpline + sizeof(tmpline) - 9)
907 goto trunc;
908 memcpy(h, " \"-\" \"-\"", 8);
909 h += 8;
910
911 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
912 " %d %03d",
913 (s->cli_addr.ss_family == AF_INET) ?
914 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
915 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
916 (int)s->logs.accept_date.tv_usec/1000);
917 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
918 goto trunc;
919 h += w;
920
921 w = strlen(fe->id);
922 if (h >= tmpline + sizeof(tmpline) - 4 - w)
923 goto trunc;
924 *(h++) = ' ';
925 *(h++) = '\"';
926 memcpy(h, fe->id, w);
927 h += w;
928 *(h++) = '\"';
929
930 w = strlen(be->id);
931 if (h >= tmpline + sizeof(tmpline) - 4 - w)
932 goto trunc;
933 *(h++) = ' ';
934 *(h++) = '\"';
935 memcpy(h, be->id, w);
936 h += w;
937 *(h++) = '\"';
938
939 svid = (tolog & LW_SVID) ?
940 (s->data_source != DATA_SRC_STATS) ?
941 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
942
943 w = strlen(svid);
944 if (h >= tmpline + sizeof(tmpline) - 4 - w)
945 goto trunc;
946 *(h++) = ' ';
947 *(h++) = '\"';
948 memcpy(h, svid, w);
949 h += w;
950 *(h++) = '\"';
951
952 t_request = -1;
953 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
954 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
955 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
956 " %d %ld %ld %ld %ld",
957 t_request,
958 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
959 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
960 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
961 s->logs.t_close);
962 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
963 goto trunc;
964 h += w;
965
966 if (h >= tmpline + sizeof(tmpline) - 8)
967 goto trunc;
968 *(h++) = ' ';
969 *(h++) = '\"';
970 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
971 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
972 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
973 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
974 *(h++) = '\"';
975
976 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
977 " %d %d %d %d %d %ld %ld",
978 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Willy Tarreauee28de02010-06-01 09:51:00 +0200979 (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries,
Emeric Brun3a058f32009-06-30 18:26:00 +0200980 s->logs.srv_queue_size, s->logs.prx_queue_size);
981
982 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
983 goto trunc;
984 h += w;
985
986 if (txn->cli_cookie) {
987 w = strlen(txn->cli_cookie);
988 if (h >= tmpline + sizeof(tmpline) - 4 - w)
989 goto trunc;
990 *(h++) = ' ';
991 *(h++) = '\"';
992 memcpy(h, txn->cli_cookie, w);
993 h += w;
994 *(h++) = '\"';
995 } else {
996 if (h >= tmpline + sizeof(tmpline) - 5)
997 goto trunc;
998 memcpy(h, " \"-\"", 4);
999 h += 4;
1000 }
1001
1002 if (txn->srv_cookie) {
1003 w = strlen(txn->srv_cookie);
1004 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1005 goto trunc;
1006 *(h++) = ' ';
1007 *(h++) = '\"';
1008 memcpy(h, txn->srv_cookie, w);
1009 h += w;
1010 *(h++) = '\"';
1011 } else {
1012 if (h >= tmpline + sizeof(tmpline) - 5)
1013 goto trunc;
1014 memcpy(h, " \"-\"", 4);
1015 h += 4;
1016 }
1017
1018 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1019 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1020 if (h >= sizeof (tmpline) + tmpline - 4)
1021 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001022 if (txn->req.cap[hdr] != NULL) {
1023 *(h++) = ' ';
1024 *(h++) = '\"';
1025 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1026 '#', hdr_encode_map, txn->req.cap[hdr]);
1027 *(h++) = '\"';
1028 } else {
1029 memcpy(h, " \"-\"", 4);
1030 h += 4;
1031 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001032 }
1033 }
1034
1035 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1036 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1037 if (h >= sizeof (tmpline) + tmpline - 4)
1038 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001039 if (txn->rsp.cap[hdr] != NULL) {
1040 *(h++) = ' ';
1041 *(h++) = '\"';
1042 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1043 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1044 *(h++) = '\"';
1045 } else {
1046 memcpy(h, " \"-\"", 4);
1047 h += 4;
1048 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001049 }
1050 }
1051
1052trunc:
1053 *h = '\0';
1054
1055 level = LOG_INFO;
1056 if (err && (fe->options2 & PR_O2_LOGERRORS))
1057 level = LOG_ERR;
1058
1059 send_log(prx_log, level, "%s\n", tmpline);
1060
1061 s->logs.logwait = 0;
1062}
1063
Willy Tarreau42250582007-04-01 01:30:43 +02001064/*
1065 * send a log for the session when we have enough info about it.
1066 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001068void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001069{
1070 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1071 struct proxy *fe = s->fe;
1072 struct proxy *be = s->be;
1073 struct proxy *prx_log;
1074 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001075 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001076 char *uri, *h;
1077 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001078 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001079 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001080 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001081 int hdr;
1082
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001083 /* if we don't want to log normal traffic, return now */
1084 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +02001085 (s->req->cons->conn_retries != be->conn_retries) ||
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001086 txn->status >= 500;
1087 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1088 return;
1089
Willy Tarreau42250582007-04-01 01:30:43 +02001090 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1091 return;
1092 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093
Emeric Brun3a058f32009-06-30 18:26:00 +02001094 if (prx_log->options2 & PR_O2_CLFLOG)
1095 return http_sess_clflog(s);
1096
Willy Tarreau42250582007-04-01 01:30:43 +02001097 if (s->cli_addr.ss_family == AF_INET)
1098 inet_ntop(AF_INET,
1099 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1100 pn, sizeof(pn));
1101 else
1102 inet_ntop(AF_INET6,
1103 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1104 pn, sizeof(pn));
1105
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001106 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001107
1108 /* FIXME: let's limit ourselves to frontend logging for now. */
1109 tolog = fe->to_log;
1110
1111 h = tmpline;
1112 if (fe->to_log & LW_REQHDR &&
1113 txn->req.cap &&
1114 (h < tmpline + sizeof(tmpline) - 10)) {
1115 *(h++) = ' ';
1116 *(h++) = '{';
1117 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1118 if (hdr)
1119 *(h++) = '|';
1120 if (txn->req.cap[hdr] != NULL)
1121 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1122 '#', hdr_encode_map, txn->req.cap[hdr]);
1123 }
1124 *(h++) = '}';
1125 }
1126
1127 if (fe->to_log & LW_RSPHDR &&
1128 txn->rsp.cap &&
1129 (h < tmpline + sizeof(tmpline) - 7)) {
1130 *(h++) = ' ';
1131 *(h++) = '{';
1132 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1133 if (hdr)
1134 *(h++) = '|';
1135 if (txn->rsp.cap[hdr] != NULL)
1136 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1137 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1138 }
1139 *(h++) = '}';
1140 }
1141
1142 if (h < tmpline + sizeof(tmpline) - 4) {
1143 *(h++) = ' ';
1144 *(h++) = '"';
1145 uri = txn->uri ? txn->uri : "<BADREQ>";
1146 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1147 '#', url_encode_map, uri);
1148 *(h++) = '"';
1149 }
1150 *h = '\0';
1151
1152 svid = (tolog & LW_SVID) ?
1153 (s->data_source != DATA_SRC_STATS) ?
1154 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1155
Willy Tarreau70089872008-06-13 21:12:51 +02001156 t_request = -1;
1157 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1158 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1159
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001160 level = LOG_INFO;
1161 if (err && (fe->options2 & PR_O2_LOGERRORS))
1162 level = LOG_ERR;
1163
1164 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001165 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001166 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1167 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001168 pn,
1169 (s->cli_addr.ss_family == AF_INET) ?
1170 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1171 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001172 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001173 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001174 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001175 t_request,
1176 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001177 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1178 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1179 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1180 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001181 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001182 txn->cli_cookie ? txn->cli_cookie : "-",
1183 txn->srv_cookie ? txn->srv_cookie : "-",
1184 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1185 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1186 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1187 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1188 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001189 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +02001190 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001191 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1192
1193 s->logs.logwait = 0;
1194}
1195
Willy Tarreau117f59e2007-03-04 18:17:17 +01001196
1197/*
1198 * Capture headers from message starting at <som> according to header list
1199 * <cap_hdr>, and fill the <idx> structure appropriately.
1200 */
1201void capture_headers(char *som, struct hdr_idx *idx,
1202 char **cap, struct cap_hdr *cap_hdr)
1203{
1204 char *eol, *sol, *col, *sov;
1205 int cur_idx;
1206 struct cap_hdr *h;
1207 int len;
1208
1209 sol = som + hdr_idx_first_pos(idx);
1210 cur_idx = hdr_idx_first_idx(idx);
1211
1212 while (cur_idx) {
1213 eol = sol + idx->v[cur_idx].len;
1214
1215 col = sol;
1216 while (col < eol && *col != ':')
1217 col++;
1218
1219 sov = col + 1;
1220 while (sov < eol && http_is_lws[(unsigned char)*sov])
1221 sov++;
1222
1223 for (h = cap_hdr; h; h = h->next) {
1224 if ((h->namelen == col - sol) &&
1225 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1226 if (cap[h->index] == NULL)
1227 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001228 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001229
1230 if (cap[h->index] == NULL) {
1231 Alert("HTTP capture : out of memory.\n");
1232 continue;
1233 }
1234
1235 len = eol - sov;
1236 if (len > h->len)
1237 len = h->len;
1238
1239 memcpy(cap[h->index], sov, len);
1240 cap[h->index][len]=0;
1241 }
1242 }
1243 sol = eol + idx->v[cur_idx].cr + 1;
1244 cur_idx = idx->v[cur_idx].next;
1245 }
1246}
1247
1248
Willy Tarreau42250582007-04-01 01:30:43 +02001249/* either we find an LF at <ptr> or we jump to <bad>.
1250 */
1251#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1252
1253/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1254 * otherwise to <http_msg_ood> with <state> set to <st>.
1255 */
1256#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1257 ptr++; \
1258 if (likely(ptr < end)) \
1259 goto good; \
1260 else { \
1261 state = (st); \
1262 goto http_msg_ood; \
1263 } \
1264 } while (0)
1265
1266
Willy Tarreaubaaee002006-06-26 02:48:02 +02001267/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001268 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001269 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1270 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1271 * will give undefined results.
1272 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1273 * and that msg->sol points to the beginning of the response.
1274 * If a complete line is found (which implies that at least one CR or LF is
1275 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1276 * returned indicating an incomplete line (which does not mean that parts have
1277 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1278 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1279 * upon next call.
1280 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001281 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001282 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1283 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001284 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001285 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001286const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1287 unsigned int state, const char *ptr, const char *end,
1288 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001289{
Willy Tarreau8973c702007-01-21 23:58:29 +01001290 switch (state) {
1291 http_msg_rpver:
1292 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001293 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001294 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1295
1296 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001297 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1299 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001300 state = HTTP_MSG_ERROR;
1301 break;
1302
Willy Tarreau8973c702007-01-21 23:58:29 +01001303 http_msg_rpver_sp:
1304 case HTTP_MSG_RPVER_SP:
1305 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001306 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001307 goto http_msg_rpcode;
1308 }
1309 if (likely(HTTP_IS_SPHT(*ptr)))
1310 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1311 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001312 state = HTTP_MSG_ERROR;
1313 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001314
1315 http_msg_rpcode:
1316 case HTTP_MSG_RPCODE:
1317 if (likely(!HTTP_IS_LWS(*ptr)))
1318 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1319
1320 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001321 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001322 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1323 }
1324
1325 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001326 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001327 http_msg_rsp_reason:
1328 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001329 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001330 msg->sl.st.r_l = 0;
1331 goto http_msg_rpline_eol;
1332
1333 http_msg_rpcode_sp:
1334 case HTTP_MSG_RPCODE_SP:
1335 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001336 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001337 goto http_msg_rpreason;
1338 }
1339 if (likely(HTTP_IS_SPHT(*ptr)))
1340 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1341 /* so it's a CR/LF, so there is no reason phrase */
1342 goto http_msg_rsp_reason;
1343
1344 http_msg_rpreason:
1345 case HTTP_MSG_RPREASON:
1346 if (likely(!HTTP_IS_CRLF(*ptr)))
1347 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001348 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 http_msg_rpline_eol:
1350 /* We have seen the end of line. Note that we do not
1351 * necessarily have the \n yet, but at least we know that we
1352 * have EITHER \r OR \n, otherwise the response would not be
1353 * complete. We can then record the response length and return
1354 * to the caller which will be able to register it.
1355 */
1356 msg->sl.st.l = ptr - msg->sol;
1357 return ptr;
1358
1359#ifdef DEBUG_FULL
1360 default:
1361 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1362 exit(1);
1363#endif
1364 }
1365
1366 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001367 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001368 if (ret_state)
1369 *ret_state = state;
1370 if (ret_ptr)
1371 *ret_ptr = (char *)ptr;
1372 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001373}
1374
Willy Tarreau8973c702007-01-21 23:58:29 +01001375/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 * This function parses a request line between <ptr> and <end>, starting with
1377 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1378 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1379 * will give undefined results.
1380 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1381 * and that msg->sol points to the beginning of the request.
1382 * If a complete line is found (which implies that at least one CR or LF is
1383 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1384 * returned indicating an incomplete line (which does not mean that parts have
1385 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1386 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1387 * upon next call.
1388 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001389 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1391 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001392 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001393 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001394const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1395 unsigned int state, const char *ptr, const char *end,
1396 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001397{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 switch (state) {
1399 http_msg_rqmeth:
1400 case HTTP_MSG_RQMETH:
1401 if (likely(HTTP_IS_TOKEN(*ptr)))
1402 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001405 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1407 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 if (likely(HTTP_IS_CRLF(*ptr))) {
1410 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001411 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001413 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001415 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001417 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 msg->sl.rq.v_l = 0;
1419 goto http_msg_rqline_eol;
1420 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001421 state = HTTP_MSG_ERROR;
1422 break;
1423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 http_msg_rqmeth_sp:
1425 case HTTP_MSG_RQMETH_SP:
1426 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001427 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 goto http_msg_rquri;
1429 }
1430 if (likely(HTTP_IS_SPHT(*ptr)))
1431 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1432 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1433 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001434
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 http_msg_rquri:
1436 case HTTP_MSG_RQURI:
1437 if (likely(!HTTP_IS_LWS(*ptr)))
1438 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001441 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1443 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001444
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1446 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 http_msg_rquri_sp:
1449 case HTTP_MSG_RQURI_SP:
1450 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001451 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452 goto http_msg_rqver;
1453 }
1454 if (likely(HTTP_IS_SPHT(*ptr)))
1455 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1456 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1457 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001458
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 http_msg_rqver:
1460 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001461 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001463
1464 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001465 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001466 http_msg_rqline_eol:
1467 /* We have seen the end of line. Note that we do not
1468 * necessarily have the \n yet, but at least we know that we
1469 * have EITHER \r OR \n, otherwise the request would not be
1470 * complete. We can then record the request length and return
1471 * to the caller which will be able to register it.
1472 */
1473 msg->sl.rq.l = ptr - msg->sol;
1474 return ptr;
1475 }
1476
1477 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001478 state = HTTP_MSG_ERROR;
1479 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481#ifdef DEBUG_FULL
1482 default:
1483 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1484 exit(1);
1485#endif
1486 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001489 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 if (ret_state)
1491 *ret_state = state;
1492 if (ret_ptr)
1493 *ret_ptr = (char *)ptr;
1494 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001496
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001497/*
1498 * Returns the data from Authorization header. Function may be called more
1499 * than once so data is stored in txn->auth_data. When no header is found
1500 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1501 * searching again for something we are unable to find anyway.
1502 */
1503
1504char get_http_auth_buff[BUFSIZE];
1505
1506int
1507get_http_auth(struct session *s)
1508{
1509
1510 struct http_txn *txn = &s->txn;
1511 struct chunk auth_method;
1512 struct hdr_ctx ctx;
1513 char *h, *p;
1514 int len;
1515
1516#ifdef DEBUG_AUTH
1517 printf("Auth for session %p: %d\n", s, txn->auth.method);
1518#endif
1519
1520 if (txn->auth.method == HTTP_AUTH_WRONG)
1521 return 0;
1522
1523 if (txn->auth.method)
1524 return 1;
1525
1526 txn->auth.method = HTTP_AUTH_WRONG;
1527
1528 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001529
1530 if (txn->flags & TX_USE_PX_CONN) {
1531 h = "Proxy-Authorization";
1532 len = strlen(h);
1533 } else {
1534 h = "Authorization";
1535 len = strlen(h);
1536 }
1537
1538 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001539 return 0;
1540
1541 h = ctx.line + ctx.val;
1542
1543 p = memchr(h, ' ', ctx.vlen);
1544 if (!p || p == h)
1545 return 0;
1546
1547 chunk_initlen(&auth_method, h, 0, p-h);
1548 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1549
1550 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1551
1552 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1553 get_http_auth_buff, BUFSIZE - 1);
1554
1555 if (len < 0)
1556 return 0;
1557
1558
1559 get_http_auth_buff[len] = '\0';
1560
1561 p = strchr(get_http_auth_buff, ':');
1562
1563 if (!p)
1564 return 0;
1565
1566 txn->auth.user = get_http_auth_buff;
1567 *p = '\0';
1568 txn->auth.pass = p+1;
1569
1570 txn->auth.method = HTTP_AUTH_BASIC;
1571 return 1;
1572 }
1573
1574 return 0;
1575}
1576
Willy Tarreau58f10d72006-12-04 02:26:12 +01001577
Willy Tarreau8973c702007-01-21 23:58:29 +01001578/*
1579 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001580 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001581 * when data are missing and recalled at the exact same location with no
1582 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001583 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001584 * fields. Note that msg->som and msg->sol will be initialized after completing
1585 * the first state, so that none of the msg pointers has to be initialized
1586 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001587 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1589{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001590 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001592
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001593 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 ptr = buf->lr;
1595 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001596
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 if (unlikely(ptr >= end))
1598 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001601 /*
1602 * First, states that are specific to the response only.
1603 * We check them first so that request and headers are
1604 * closer to each other (accessed more often).
1605 */
1606 http_msg_rpbefore:
1607 case HTTP_MSG_RPBEFORE:
1608 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001609 /* we have a start of message, but we have to check
1610 * first if we need to remove some CRLF. We can only
1611 * do this when send_max=0.
1612 */
1613 char *beg = buf->w + buf->send_max;
1614 if (beg >= buf->data + buf->size)
1615 beg -= buf->size;
1616 if (unlikely(ptr != beg)) {
1617 if (buf->send_max)
1618 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001619 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001620 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001621 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001622 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001623 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001624 hdr_idx_init(idx);
1625 state = HTTP_MSG_RPVER;
1626 goto http_msg_rpver;
1627 }
1628
1629 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1630 goto http_msg_invalid;
1631
1632 if (unlikely(*ptr == '\n'))
1633 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1634 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1635 /* stop here */
1636
1637 http_msg_rpbefore_cr:
1638 case HTTP_MSG_RPBEFORE_CR:
1639 EXPECT_LF_HERE(ptr, http_msg_invalid);
1640 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1641 /* stop here */
1642
1643 http_msg_rpver:
1644 case HTTP_MSG_RPVER:
1645 case HTTP_MSG_RPVER_SP:
1646 case HTTP_MSG_RPCODE:
1647 case HTTP_MSG_RPCODE_SP:
1648 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001649 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001650 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001651 if (unlikely(!ptr))
1652 return;
1653
1654 /* we have a full response and we know that we have either a CR
1655 * or an LF at <ptr>.
1656 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001657 //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 +01001658 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1659
1660 msg->sol = ptr;
1661 if (likely(*ptr == '\r'))
1662 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1663 goto http_msg_rpline_end;
1664
1665 http_msg_rpline_end:
1666 case HTTP_MSG_RPLINE_END:
1667 /* msg->sol must point to the first of CR or LF. */
1668 EXPECT_LF_HERE(ptr, http_msg_invalid);
1669 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1670 /* stop here */
1671
1672 /*
1673 * Second, states that are specific to the request only
1674 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 http_msg_rqbefore:
1676 case HTTP_MSG_RQBEFORE:
1677 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001678 /* we have a start of message, but we have to check
1679 * first if we need to remove some CRLF. We can only
1680 * do this when send_max=0.
1681 */
1682 char *beg = buf->w + buf->send_max;
1683 if (beg >= buf->data + buf->size)
1684 beg -= buf->size;
1685 if (likely(ptr != beg)) {
1686 if (buf->send_max)
1687 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001688 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001689 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001691 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001692 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001693 /* we will need this when keep-alive will be supported
1694 hdr_idx_init(idx);
1695 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001696 state = HTTP_MSG_RQMETH;
1697 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1701 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001702
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 if (unlikely(*ptr == '\n'))
1704 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1705 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001706 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001707
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 http_msg_rqbefore_cr:
1709 case HTTP_MSG_RQBEFORE_CR:
1710 EXPECT_LF_HERE(ptr, http_msg_invalid);
1711 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001712 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001713
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 http_msg_rqmeth:
1715 case HTTP_MSG_RQMETH:
1716 case HTTP_MSG_RQMETH_SP:
1717 case HTTP_MSG_RQURI:
1718 case HTTP_MSG_RQURI_SP:
1719 case HTTP_MSG_RQVER:
1720 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001721 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 if (unlikely(!ptr))
1723 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001724
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 /* we have a full request and we know that we have either a CR
1726 * or an LF at <ptr>.
1727 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001728 //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 +01001729 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 msg->sol = ptr;
1732 if (likely(*ptr == '\r'))
1733 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001735
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001736 http_msg_rqline_end:
1737 case HTTP_MSG_RQLINE_END:
1738 /* check for HTTP/0.9 request : no version information available.
1739 * msg->sol must point to the first of CR or LF.
1740 */
1741 if (unlikely(msg->sl.rq.v_l == 0))
1742 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001743
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 EXPECT_LF_HERE(ptr, http_msg_invalid);
1745 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001746 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001747
Willy Tarreau8973c702007-01-21 23:58:29 +01001748 /*
1749 * Common states below
1750 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 http_msg_hdr_first:
1752 case HTTP_MSG_HDR_FIRST:
1753 msg->sol = ptr;
1754 if (likely(!HTTP_IS_CRLF(*ptr))) {
1755 goto http_msg_hdr_name;
1756 }
1757
1758 if (likely(*ptr == '\r'))
1759 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1760 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001761
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 http_msg_hdr_name:
1763 case HTTP_MSG_HDR_NAME:
1764 /* assumes msg->sol points to the first char */
1765 if (likely(HTTP_IS_TOKEN(*ptr)))
1766 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001767
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 if (likely(*ptr == ':')) {
1769 msg->col = ptr - buf->data;
1770 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1771 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001772
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001773 if (likely(msg->err_pos < -1) || *ptr == '\n')
1774 goto http_msg_invalid;
1775
1776 if (msg->err_pos == -1) /* capture error pointer */
1777 msg->err_pos = ptr - buf->data; /* >= 0 now */
1778
1779 /* and we still accept this non-token character */
1780 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001781
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001782 http_msg_hdr_l1_sp:
1783 case HTTP_MSG_HDR_L1_SP:
1784 /* assumes msg->sol points to the first char and msg->col to the colon */
1785 if (likely(HTTP_IS_SPHT(*ptr)))
1786 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 /* header value can be basically anything except CR/LF */
1789 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001790
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 if (likely(!HTTP_IS_CRLF(*ptr))) {
1792 goto http_msg_hdr_val;
1793 }
1794
1795 if (likely(*ptr == '\r'))
1796 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1797 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001798
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001799 http_msg_hdr_l1_lf:
1800 case HTTP_MSG_HDR_L1_LF:
1801 EXPECT_LF_HERE(ptr, http_msg_invalid);
1802 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001804 http_msg_hdr_l1_lws:
1805 case HTTP_MSG_HDR_L1_LWS:
1806 if (likely(HTTP_IS_SPHT(*ptr))) {
1807 /* replace HT,CR,LF with spaces */
1808 for (; buf->data+msg->sov < ptr; msg->sov++)
1809 buf->data[msg->sov] = ' ';
1810 goto http_msg_hdr_l1_sp;
1811 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001812 /* we had a header consisting only in spaces ! */
1813 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 goto http_msg_complete_header;
1815
1816 http_msg_hdr_val:
1817 case HTTP_MSG_HDR_VAL:
1818 /* assumes msg->sol points to the first char, msg->col to the
1819 * colon, and msg->sov points to the first character of the
1820 * value.
1821 */
1822 if (likely(!HTTP_IS_CRLF(*ptr)))
1823 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001824
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001825 msg->eol = ptr;
1826 /* Note: we could also copy eol into ->eoh so that we have the
1827 * real header end in case it ends with lots of LWS, but is this
1828 * really needed ?
1829 */
1830 if (likely(*ptr == '\r'))
1831 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1832 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001833
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001834 http_msg_hdr_l2_lf:
1835 case HTTP_MSG_HDR_L2_LF:
1836 EXPECT_LF_HERE(ptr, http_msg_invalid);
1837 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001838
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001839 http_msg_hdr_l2_lws:
1840 case HTTP_MSG_HDR_L2_LWS:
1841 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1842 /* LWS: replace HT,CR,LF with spaces */
1843 for (; msg->eol < ptr; msg->eol++)
1844 *msg->eol = ' ';
1845 goto http_msg_hdr_val;
1846 }
1847 http_msg_complete_header:
1848 /*
1849 * It was a new header, so the last one is finished.
1850 * Assumes msg->sol points to the first char, msg->col to the
1851 * colon, msg->sov points to the first character of the value
1852 * and msg->eol to the first CR or LF so we know how the line
1853 * ends. We insert last header into the index.
1854 */
1855 /*
1856 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1857 write(2, msg->sol, msg->eol-msg->sol);
1858 fprintf(stderr,"\n");
1859 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001860
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001861 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1862 idx, idx->tail) < 0))
1863 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001864
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001865 msg->sol = ptr;
1866 if (likely(!HTTP_IS_CRLF(*ptr))) {
1867 goto http_msg_hdr_name;
1868 }
1869
1870 if (likely(*ptr == '\r'))
1871 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1872 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001873
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001874 http_msg_last_lf:
1875 case HTTP_MSG_LAST_LF:
1876 /* Assumes msg->sol points to the first of either CR or LF */
1877 EXPECT_LF_HERE(ptr, http_msg_invalid);
1878 ptr++;
1879 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001880 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001881 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001882 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001883 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001884 return;
1885#ifdef DEBUG_FULL
1886 default:
1887 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1888 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001889#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001890 }
1891 http_msg_ood:
1892 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001893 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001894 buf->lr = ptr;
1895 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001896
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001897 http_msg_invalid:
1898 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001899 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001900 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001901 return;
1902}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001903
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001904/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1905 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1906 * nothing is done and 1 is returned.
1907 */
1908static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1909{
1910 int delta;
1911 char *cur_end;
1912
1913 if (msg->sl.rq.v_l != 0)
1914 return 1;
1915
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001916 cur_end = msg->sol + msg->sl.rq.l;
1917 delta = 0;
1918
1919 if (msg->sl.rq.u_l == 0) {
1920 /* if no URI was set, add "/" */
1921 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1922 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001923 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001924 }
1925 /* add HTTP version */
1926 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001927 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001928 cur_end += delta;
1929 cur_end = (char *)http_parse_reqline(msg, req->data,
1930 HTTP_MSG_RQMETH,
1931 msg->sol, cur_end + 1,
1932 NULL, NULL);
1933 if (unlikely(!cur_end))
1934 return 0;
1935
1936 /* we have a full HTTP/1.0 request now and we know that
1937 * we have either a CR or an LF at <ptr>.
1938 */
1939 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1940 return 1;
1941}
1942
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001943/* Parse the Connection: header of an HTTP request, looking for both "close"
1944 * and "keep-alive" values. If a buffer is provided and we already know that
1945 * some headers may safely be removed, we remove them now. The <to_del> flags
1946 * are used for that :
1947 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1948 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1949 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1950 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1951 * harmless combinations may be removed. Do not call that after changes have
1952 * been processed. If unused, the buffer can be NULL, and no data will be
1953 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001954 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001955void 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 +01001956{
Willy Tarreau5b154472009-12-21 20:11:07 +01001957 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001958 const char *hdr_val = "Connection";
1959 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001960
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001961 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001962 return;
1963
Willy Tarreau88d349d2010-01-25 12:15:43 +01001964 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1965 hdr_val = "Proxy-Connection";
1966 hdr_len = 16;
1967 }
1968
Willy Tarreau5b154472009-12-21 20:11:07 +01001969 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001970 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001971 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001972 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1973 txn->flags |= TX_HDR_CONN_KAL;
1974 if ((to_del & 2) && buf)
1975 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1976 else
1977 txn->flags |= TX_CON_KAL_SET;
1978 }
1979 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1980 txn->flags |= TX_HDR_CONN_CLO;
1981 if ((to_del & 1) && buf)
1982 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1983 else
1984 txn->flags |= TX_CON_CLO_SET;
1985 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001986 }
1987
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001988 txn->flags |= TX_HDR_CONN_PRS;
1989 return;
1990}
Willy Tarreau5b154472009-12-21 20:11:07 +01001991
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001992/* Apply desired changes on the Connection: header. Values may be removed and/or
1993 * added depending on the <wanted> flags, which are exclusively composed of
1994 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1995 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1996 */
1997void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
1998{
1999 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002000 const char *hdr_val = "Connection";
2001 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002002
2003 ctx.idx = 0;
2004
Willy Tarreau88d349d2010-01-25 12:15:43 +01002005
2006 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2007 hdr_val = "Proxy-Connection";
2008 hdr_len = 16;
2009 }
2010
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002011 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002012 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002013 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2014 if (wanted & TX_CON_KAL_SET)
2015 txn->flags |= TX_CON_KAL_SET;
2016 else
2017 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002018 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002019 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2020 if (wanted & TX_CON_CLO_SET)
2021 txn->flags |= TX_CON_CLO_SET;
2022 else
2023 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002024 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002025 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002026
2027 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2028 return;
2029
2030 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2031 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002032 hdr_val = "Connection: close";
2033 hdr_len = 17;
2034 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2035 hdr_val = "Proxy-Connection: close";
2036 hdr_len = 23;
2037 }
2038 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002039 }
2040
2041 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2042 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002043 hdr_val = "Connection: keep-alive";
2044 hdr_len = 22;
2045 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2046 hdr_val = "Proxy-Connection: keep-alive";
2047 hdr_len = 28;
2048 }
2049 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002050 }
2051 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002052}
2053
Willy Tarreaud98cf932009-12-27 22:54:55 +01002054/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2055 * first byte of body, and increments msg->sov by the number of bytes parsed,
2056 * so that we know we can forward between ->som and ->sov. Note that due to
2057 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2058 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002059 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002061 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002062int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002063{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002064 char *ptr = buf->lr;
2065 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002066 unsigned int chunk = 0;
2067
2068 /* The chunk size is in the following form, though we are only
2069 * interested in the size and CRLF :
2070 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2071 */
2072 while (1) {
2073 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002074 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002075 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002076 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002077 if (c < 0) /* not a hex digit anymore */
2078 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002079 if (++ptr >= end)
2080 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002081 if (chunk & 0xF000000) /* overflow will occur */
2082 return -1;
2083 chunk = (chunk << 4) + c;
2084 }
2085
Willy Tarreaud98cf932009-12-27 22:54:55 +01002086 /* empty size not allowed */
2087 if (ptr == buf->lr)
2088 return -1;
2089
2090 while (http_is_spht[(unsigned char)*ptr]) {
2091 if (++ptr >= end)
2092 ptr = buf->data;
2093 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002094 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002095 }
2096
Willy Tarreaud98cf932009-12-27 22:54:55 +01002097 /* Up to there, we know that at least one byte is present at *ptr. Check
2098 * for the end of chunk size.
2099 */
2100 while (1) {
2101 if (likely(HTTP_IS_CRLF(*ptr))) {
2102 /* we now have a CR or an LF at ptr */
2103 if (likely(*ptr == '\r')) {
2104 if (++ptr >= end)
2105 ptr = buf->data;
2106 if (ptr == buf->r)
2107 return 0;
2108 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002109
Willy Tarreaud98cf932009-12-27 22:54:55 +01002110 if (*ptr != '\n')
2111 return -1;
2112 if (++ptr >= end)
2113 ptr = buf->data;
2114 /* done */
2115 break;
2116 }
2117 else if (*ptr == ';') {
2118 /* chunk extension, ends at next CRLF */
2119 if (++ptr >= end)
2120 ptr = buf->data;
2121 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002122 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002123
2124 while (!HTTP_IS_CRLF(*ptr)) {
2125 if (++ptr >= end)
2126 ptr = buf->data;
2127 if (ptr == buf->r)
2128 return 0;
2129 }
2130 /* we have a CRLF now, loop above */
2131 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002132 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002133 else
Willy Tarreau115acb92009-12-26 13:56:06 +01002134 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002135 }
2136
Willy Tarreaud98cf932009-12-27 22:54:55 +01002137 /* OK we found our CRLF and now <ptr> points to the next byte,
2138 * which may or may not be present. We save that into ->lr and
2139 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002140 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002141 msg->sov += ptr - buf->lr;
2142 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01002143 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002144 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002145 return 1;
2146}
2147
Willy Tarreaud98cf932009-12-27 22:54:55 +01002148/* This function skips trailers in the buffer <buf> associated with HTTP
2149 * message <msg>. The first visited position is buf->lr. If the end of
2150 * the trailers is found, it is automatically scheduled to be forwarded,
2151 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2152 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002153 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2154 * zero. If a parse error is encountered, the function returns < 0 and does not
2155 * change anything except maybe buf->lr and msg->sov. Note that the message
2156 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2157 * which implies that all non-trailers data have already been scheduled for
2158 * forwarding, and that the difference between msg->som and msg->sov exactly
2159 * matches the length of trailers already parsed and not forwarded. It is also
2160 * important to note that this function is designed to be able to parse wrapped
2161 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002162 */
2163int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2164{
2165 /* we have buf->lr which points to next line. Look for CRLF. */
2166 while (1) {
2167 char *p1 = NULL, *p2 = NULL;
2168 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002169 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002170
2171 /* scan current line and stop at LF or CRLF */
2172 while (1) {
2173 if (ptr == buf->r)
2174 return 0;
2175
2176 if (*ptr == '\n') {
2177 if (!p1)
2178 p1 = ptr;
2179 p2 = ptr;
2180 break;
2181 }
2182
2183 if (*ptr == '\r') {
2184 if (p1)
2185 return -1;
2186 p1 = ptr;
2187 }
2188
2189 ptr++;
2190 if (ptr >= buf->data + buf->size)
2191 ptr = buf->data;
2192 }
2193
2194 /* after LF; point to beginning of next line */
2195 p2++;
2196 if (p2 >= buf->data + buf->size)
2197 p2 = buf->data;
2198
Willy Tarreau638cd022010-01-03 07:42:04 +01002199 bytes = p2 - buf->lr;
2200 if (bytes < 0)
2201 bytes += buf->size;
2202
2203 /* schedule this line for forwarding */
2204 msg->sov += bytes;
2205 if (msg->sov >= buf->size)
2206 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002207
Willy Tarreau638cd022010-01-03 07:42:04 +01002208 if (p1 == buf->lr) {
2209 /* LF/CRLF at beginning of line => end of trailers at p2.
2210 * Everything was scheduled for forwarding, there's nothing
2211 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002212 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002213 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002214 msg->msg_state = HTTP_MSG_DONE;
2215 return 1;
2216 }
2217 /* OK, next line then */
2218 buf->lr = p2;
2219 }
2220}
2221
2222/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2223 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2224 * ->som, buf->lr in order to include this part into the next forwarding phase.
2225 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2226 * not enough data are available, the function does not change anything and
2227 * returns zero. If a parse error is encountered, the function returns < 0 and
2228 * does not change anything. Note: this function is designed to parse wrapped
2229 * CRLF at the end of the buffer.
2230 */
2231int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2232{
2233 char *ptr;
2234 int bytes;
2235
2236 /* NB: we'll check data availabilty at the end. It's not a
2237 * problem because whatever we match first will be checked
2238 * against the correct length.
2239 */
2240 bytes = 1;
2241 ptr = buf->lr;
2242 if (*ptr == '\r') {
2243 bytes++;
2244 ptr++;
2245 if (ptr >= buf->data + buf->size)
2246 ptr = buf->data;
2247 }
2248
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002249 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002250 return 0;
2251
2252 if (*ptr != '\n')
2253 return -1;
2254
2255 ptr++;
2256 if (ptr >= buf->data + buf->size)
2257 ptr = buf->data;
2258 buf->lr = ptr;
2259 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2260 msg->sov = ptr - buf->data;
2261 msg->som = msg->sov - bytes;
2262 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2263 return 1;
2264}
Willy Tarreau5b154472009-12-21 20:11:07 +01002265
Willy Tarreau83e3af02009-12-28 17:39:57 +01002266void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2267{
2268 char *end = buf->data + buf->size;
2269 int off = buf->data + buf->size - buf->w;
2270
2271 /* two possible cases :
2272 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002273 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002274 */
2275 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002276 int block1 = buf->l;
2277 int block2 = 0;
2278 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002279 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002280 block1 = buf->data + buf->size - buf->w;
2281 block2 = buf->r - buf->data;
2282 }
2283 if (block2)
2284 memcpy(swap_buffer, buf->data, block2);
2285 memmove(buf->data, buf->w, block1);
2286 if (block2)
2287 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002288 }
2289
2290 /* adjust all known pointers */
2291 buf->w = buf->data;
2292 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2293 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2294 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2295 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2296
2297 /* adjust relative pointers */
2298 msg->som = 0;
2299 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2300 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2301 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2302
Willy Tarreau83e3af02009-12-28 17:39:57 +01002303 if (msg->err_pos >= 0) {
2304 msg->err_pos += off;
2305 if (msg->err_pos >= buf->size)
2306 msg->err_pos -= buf->size;
2307 }
2308
2309 buf->flags &= ~BF_FULL;
2310 if (buf->l >= buffer_max_len(buf))
2311 buf->flags |= BF_FULL;
2312}
2313
Willy Tarreaud787e662009-07-07 10:14:51 +02002314/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2315 * processing can continue on next analysers, or zero if it either needs more
2316 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2317 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2318 * when it has nothing left to do, and may remove any analyser when it wants to
2319 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002320 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002321int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002322{
Willy Tarreau59234e92008-11-30 23:51:27 +01002323 /*
2324 * We will parse the partial (or complete) lines.
2325 * We will check the request syntax, and also join multi-line
2326 * headers. An index of all the lines will be elaborated while
2327 * parsing.
2328 *
2329 * For the parsing, we use a 28 states FSM.
2330 *
2331 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002332 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002333 * req->data + msg->eoh = end of processed headers / start of current one
2334 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002335 * req->lr = first non-visited byte
2336 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002337 *
2338 * At end of parsing, we may perform a capture of the error (if any), and
2339 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2340 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2341 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002342 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002343
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002345 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002346 struct http_txn *txn = &s->txn;
2347 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002348 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002349
Willy Tarreau6bf17362009-02-24 10:48:35 +01002350 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2351 now_ms, __FUNCTION__,
2352 s,
2353 req,
2354 req->rex, req->wex,
2355 req->flags,
2356 req->l,
2357 req->analysers);
2358
Willy Tarreau52a0c602009-08-16 22:45:38 +02002359 /* we're speaking HTTP here, so let's speak HTTP to the client */
2360 s->srv_error = http_return_srv_error;
2361
Willy Tarreau83e3af02009-12-28 17:39:57 +01002362 /* There's a protected area at the end of the buffer for rewriting
2363 * purposes. We don't want to start to parse the request if the
2364 * protected area is affected, because we may have to move processed
2365 * data later, which is much more complicated.
2366 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002367 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002368 if ((txn->flags & TX_NOT_FIRST) &&
2369 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002370 req->r < req->lr ||
2371 req->r > req->data + req->size - global.tune.maxrewrite)) {
2372 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002373 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2374 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002375 /* some data has still not left the buffer, wake us once that's done */
2376 buffer_dont_connect(req);
2377 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2378 return 0;
2379 }
2380 if (req->l <= req->size - global.tune.maxrewrite)
2381 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002382 }
2383
Willy Tarreau065e8332010-01-08 00:30:20 +01002384 /* Note that we have the same problem with the response ; we
2385 * may want to send a redirect, error or anything which requires
2386 * some spare space. So we'll ensure that we have at least
2387 * maxrewrite bytes available in the response buffer before
2388 * processing that one. This will only affect pipelined
2389 * keep-alive requests.
2390 */
2391 if ((txn->flags & TX_NOT_FIRST) &&
2392 unlikely((s->rep->flags & BF_FULL) ||
2393 s->rep->r < s->rep->lr ||
2394 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2395 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002396 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2397 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002398 /* don't let a connection request be initiated */
2399 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002400 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau065e8332010-01-08 00:30:20 +01002401 return 0;
2402 }
2403 }
2404
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002405 if (likely(req->lr < req->r))
2406 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002407 }
2408
Willy Tarreau59234e92008-11-30 23:51:27 +01002409 /* 1: we might have to print this header in debug mode */
2410 if (unlikely((global.mode & MODE_DEBUG) &&
2411 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002412 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002413 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002414 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002415
Willy Tarreau663308b2010-06-07 14:06:08 +02002416 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002417 eol = sol + msg->sl.rq.l;
2418 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002419
Willy Tarreau59234e92008-11-30 23:51:27 +01002420 sol += hdr_idx_first_pos(&txn->hdr_idx);
2421 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002422
Willy Tarreau59234e92008-11-30 23:51:27 +01002423 while (cur_idx) {
2424 eol = sol + txn->hdr_idx.v[cur_idx].len;
2425 debug_hdr("clihdr", s, sol, eol);
2426 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2427 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002428 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002429 }
2430
Willy Tarreau58f10d72006-12-04 02:26:12 +01002431
Willy Tarreau59234e92008-11-30 23:51:27 +01002432 /*
2433 * Now we quickly check if we have found a full valid request.
2434 * If not so, we check the FD and buffer states before leaving.
2435 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002436 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002437 * requests are checked first. When waiting for a second request
2438 * on a keep-alive session, if we encounter and error, close, t/o,
2439 * we note the error in the session flags but don't set any state.
2440 * Since the error will be noted there, it will not be counted by
2441 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002442 * Last, we may increase some tracked counters' http request errors on
2443 * the cases that are deliberately the client's fault. For instance,
2444 * a timeout or connection reset is not counted as an error. However
2445 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002446 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002447
Willy Tarreau655dce92009-11-08 13:10:58 +01002448 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002449 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002450 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002451 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002452 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002453 session_inc_http_req_ctr(s);
2454 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002455 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002456 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002457 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002458
Willy Tarreau59234e92008-11-30 23:51:27 +01002459 /* 1: Since we are in header mode, if there's no space
2460 * left for headers, we won't be able to free more
2461 * later, so the session will never terminate. We
2462 * must terminate it now.
2463 */
2464 if (unlikely(req->flags & BF_FULL)) {
2465 /* FIXME: check if URI is set and return Status
2466 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002467 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002468 session_inc_http_req_ctr(s);
2469 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002470 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002471 goto return_bad_req;
2472 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002473
Willy Tarreau59234e92008-11-30 23:51:27 +01002474 /* 2: have we encountered a read error ? */
2475 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002476 if (!(s->flags & SN_ERR_MASK))
2477 s->flags |= SN_ERR_CLICL;
2478
Willy Tarreaufcffa692010-01-10 14:21:19 +01002479 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002480 goto failed_keep_alive;
2481
Willy Tarreau59234e92008-11-30 23:51:27 +01002482 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002483 if (msg->err_pos >= 0) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002484 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002485 session_inc_http_err_ctr(s);
2486 }
2487
Willy Tarreau59234e92008-11-30 23:51:27 +01002488 msg->msg_state = HTTP_MSG_ERROR;
2489 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002490
Willy Tarreauda7ff642010-06-23 11:44:09 +02002491 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002492 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002493 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002494 if (s->listener->counters)
2495 s->listener->counters->failed_req++;
2496
Willy Tarreau59234e92008-11-30 23:51:27 +01002497 if (!(s->flags & SN_FINST_MASK))
2498 s->flags |= SN_FINST_R;
2499 return 0;
2500 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002501
Willy Tarreau59234e92008-11-30 23:51:27 +01002502 /* 3: has the read timeout expired ? */
2503 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002504 if (!(s->flags & SN_ERR_MASK))
2505 s->flags |= SN_ERR_CLITO;
2506
Willy Tarreaufcffa692010-01-10 14:21:19 +01002507 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002508 goto failed_keep_alive;
2509
Willy Tarreau59234e92008-11-30 23:51:27 +01002510 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002511 if (msg->err_pos >= 0) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002512 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002513 session_inc_http_err_ctr(s);
2514 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002515 txn->status = 408;
2516 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2517 msg->msg_state = HTTP_MSG_ERROR;
2518 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002519
Willy Tarreauda7ff642010-06-23 11:44:09 +02002520 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002521 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002522 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002523 if (s->listener->counters)
2524 s->listener->counters->failed_req++;
2525
Willy Tarreau59234e92008-11-30 23:51:27 +01002526 if (!(s->flags & SN_FINST_MASK))
2527 s->flags |= SN_FINST_R;
2528 return 0;
2529 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002530
Willy Tarreau59234e92008-11-30 23:51:27 +01002531 /* 4: have we encountered a close ? */
2532 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002533 if (!(s->flags & SN_ERR_MASK))
2534 s->flags |= SN_ERR_CLICL;
2535
Willy Tarreaufcffa692010-01-10 14:21:19 +01002536 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002537 goto failed_keep_alive;
2538
Willy Tarreau4076a152009-04-02 15:18:36 +02002539 if (msg->err_pos >= 0)
2540 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002541 txn->status = 400;
2542 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2543 msg->msg_state = HTTP_MSG_ERROR;
2544 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002545
Willy Tarreauda7ff642010-06-23 11:44:09 +02002546 session_inc_http_err_ctr(s);
2547 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002548 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002549 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002550 if (s->listener->counters)
2551 s->listener->counters->failed_req++;
2552
Willy Tarreau59234e92008-11-30 23:51:27 +01002553 if (!(s->flags & SN_FINST_MASK))
2554 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002555 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002556 }
2557
Willy Tarreau520d95e2009-09-19 21:04:57 +02002558 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002559 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002560 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002561
Willy Tarreaufcffa692010-01-10 14:21:19 +01002562 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2563 /* If the client starts to talk, let's fall back to
2564 * request timeout processing.
2565 */
2566 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002567 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002568 }
2569
Willy Tarreau59234e92008-11-30 23:51:27 +01002570 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002571 if (!tick_isset(req->analyse_exp)) {
2572 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2573 (txn->flags & TX_WAIT_NEXT_RQ) &&
2574 tick_isset(s->be->timeout.httpka))
2575 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2576 else
2577 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2578 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002579
Willy Tarreau59234e92008-11-30 23:51:27 +01002580 /* we're not ready yet */
2581 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002582
2583 failed_keep_alive:
2584 /* Here we process low-level errors for keep-alive requests. In
2585 * short, if the request is not the first one and it experiences
2586 * a timeout, read error or shutdown, we just silently close so
2587 * that the client can try again.
2588 */
2589 txn->status = 0;
2590 msg->msg_state = HTTP_MSG_RQBEFORE;
2591 req->analysers = 0;
2592 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002593 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002594 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002595 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002596 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002597
Willy Tarreaud787e662009-07-07 10:14:51 +02002598 /* OK now we have a complete HTTP request with indexed headers. Let's
2599 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002600 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2601 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2602 * points to the CRLF of the request line. req->lr points to the first
2603 * byte after the last LF. msg->col and msg->sov point to the first
2604 * byte of data. msg->eol cannot be trusted because it may have been
2605 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002606 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002607
Willy Tarreauda7ff642010-06-23 11:44:09 +02002608 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002609 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2610
Willy Tarreaub16a5742010-01-10 14:46:16 +01002611 if (txn->flags & TX_WAIT_NEXT_RQ) {
2612 /* kill the pending keep-alive timeout */
2613 txn->flags &= ~TX_WAIT_NEXT_RQ;
2614 req->analyse_exp = TICK_ETERNITY;
2615 }
2616
2617
Willy Tarreaud787e662009-07-07 10:14:51 +02002618 /* Maybe we found in invalid header name while we were configured not
2619 * to block on that, so we have to capture it now.
2620 */
2621 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002622 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2623
Willy Tarreau59234e92008-11-30 23:51:27 +01002624 /*
2625 * 1: identify the method
2626 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002627 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002628
2629 /* we can make use of server redirect on GET and HEAD */
2630 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2631 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002632
Willy Tarreau59234e92008-11-30 23:51:27 +01002633 /*
2634 * 2: check if the URI matches the monitor_uri.
2635 * We have to do this for every request which gets in, because
2636 * the monitor-uri is defined by the frontend.
2637 */
2638 if (unlikely((s->fe->monitor_uri_len != 0) &&
2639 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002640 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002641 s->fe->monitor_uri,
2642 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002643 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002644 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002645 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002646 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002647
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002649
Willy Tarreau59234e92008-11-30 23:51:27 +01002650 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002651 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2652 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002653
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 ret = acl_pass(ret);
2655 if (cond->pol == ACL_COND_UNLESS)
2656 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002657
Willy Tarreau59234e92008-11-30 23:51:27 +01002658 if (ret) {
2659 /* we fail this request, let's return 503 service unavail */
2660 txn->status = 503;
2661 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2662 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002663 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002664 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002665
Willy Tarreau59234e92008-11-30 23:51:27 +01002666 /* nothing to fail, let's reply normaly */
2667 txn->status = 200;
2668 stream_int_retnclose(req->prod, &http_200_chunk);
2669 goto return_prx_cond;
2670 }
2671
2672 /*
2673 * 3: Maybe we have to copy the original REQURI for the logs ?
2674 * Note: we cannot log anymore if the request has been
2675 * classified as invalid.
2676 */
2677 if (unlikely(s->logs.logwait & LW_REQ)) {
2678 /* we have a complete HTTP request that we must log */
2679 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2680 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002681
Willy Tarreau59234e92008-11-30 23:51:27 +01002682 if (urilen >= REQURI_LEN)
2683 urilen = REQURI_LEN - 1;
2684 memcpy(txn->uri, &req->data[msg->som], urilen);
2685 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002686
Willy Tarreau59234e92008-11-30 23:51:27 +01002687 if (!(s->logs.logwait &= ~LW_REQ))
2688 s->do_log(s);
2689 } else {
2690 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002691 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002692 }
Willy Tarreau06619262006-12-17 08:37:22 +01002693
Willy Tarreau59234e92008-11-30 23:51:27 +01002694 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002695 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2696 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002697
Willy Tarreau5b154472009-12-21 20:11:07 +01002698 /* ... and check if the request is HTTP/1.1 or above */
2699 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002700 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2701 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2702 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002703 txn->flags |= TX_REQ_VER_11;
2704
2705 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002706 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002707
Willy Tarreau88d349d2010-01-25 12:15:43 +01002708 /* if the frontend has "option http-use-proxy-header", we'll check if
2709 * we have what looks like a proxied connection instead of a connection,
2710 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2711 * Note that this is *not* RFC-compliant, however browsers and proxies
2712 * happen to do that despite being non-standard :-(
2713 * We consider that a request not beginning with either '/' or '*' is
2714 * a proxied connection, which covers both "scheme://location" and
2715 * CONNECT ip:port.
2716 */
2717 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2718 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2719 txn->flags |= TX_USE_PX_CONN;
2720
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002721 /* transfer length unknown*/
2722 txn->flags &= ~TX_REQ_XFER_LEN;
2723
Willy Tarreau59234e92008-11-30 23:51:27 +01002724 /* 5: we may need to capture headers */
2725 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002726 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002727 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002728
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002729 /* 6: determine the transfer-length.
2730 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2731 * the presence of a message-body in a REQUEST and its transfer length
2732 * must be determined that way (in order of precedence) :
2733 * 1. The presence of a message-body in a request is signaled by the
2734 * inclusion of a Content-Length or Transfer-Encoding header field
2735 * in the request's header fields. When a request message contains
2736 * both a message-body of non-zero length and a method that does
2737 * not define any semantics for that request message-body, then an
2738 * origin server SHOULD either ignore the message-body or respond
2739 * with an appropriate error message (e.g., 413). A proxy or
2740 * gateway, when presented the same request, SHOULD either forward
2741 * the request inbound with the message- body or ignore the
2742 * message-body when determining a response.
2743 *
2744 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2745 * and the "chunked" transfer-coding (Section 6.2) is used, the
2746 * transfer-length is defined by the use of this transfer-coding.
2747 * If a Transfer-Encoding header field is present and the "chunked"
2748 * transfer-coding is not present, the transfer-length is defined
2749 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002750 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002751 * 3. If a Content-Length header field is present, its decimal value in
2752 * OCTETs represents both the entity-length and the transfer-length.
2753 * If a message is received with both a Transfer-Encoding header
2754 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002755 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002756 * 4. By the server closing the connection. (Closing the connection
2757 * cannot be used to indicate the end of a request body, since that
2758 * would leave no possibility for the server to send back a response.)
2759 *
2760 * Whenever a transfer-coding is applied to a message-body, the set of
2761 * transfer-codings MUST include "chunked", unless the message indicates
2762 * it is terminated by closing the connection. When the "chunked"
2763 * transfer-coding is used, it MUST be the last transfer-coding applied
2764 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002765 */
2766
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002767 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002768 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002769 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002770 while ((txn->flags & TX_REQ_VER_11) &&
2771 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002772 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2773 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2774 else if (txn->flags & TX_REQ_TE_CHNK) {
2775 /* bad transfer-encoding (chunked followed by something else) */
2776 use_close_only = 1;
2777 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2778 break;
2779 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002780 }
2781
Willy Tarreau32b47f42009-10-18 20:55:02 +02002782 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002783 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002784 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2785 signed long long cl;
2786
2787 if (!ctx.vlen)
2788 goto return_bad_req;
2789
2790 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2791 goto return_bad_req; /* parse failure */
2792
2793 if (cl < 0)
2794 goto return_bad_req;
2795
2796 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2797 goto return_bad_req; /* already specified, was different */
2798
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002799 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002800 msg->hdr_content_len = cl;
2801 }
2802
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002803 /* bodyless requests have a known length */
2804 if (!use_close_only)
2805 txn->flags |= TX_REQ_XFER_LEN;
2806
Willy Tarreaud787e662009-07-07 10:14:51 +02002807 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002808 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002809 req->analyse_exp = TICK_ETERNITY;
2810 return 1;
2811
2812 return_bad_req:
2813 /* We centralize bad requests processing here */
2814 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2815 /* we detected a parsing error. We want to archive this request
2816 * in the dedicated proxy area for later troubleshooting.
2817 */
2818 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2819 }
2820
2821 txn->req.msg_state = HTTP_MSG_ERROR;
2822 txn->status = 400;
2823 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002824
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002825 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002826 if (s->listener->counters)
2827 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002828
2829 return_prx_cond:
2830 if (!(s->flags & SN_ERR_MASK))
2831 s->flags |= SN_ERR_PRXCOND;
2832 if (!(s->flags & SN_FINST_MASK))
2833 s->flags |= SN_FINST_R;
2834
2835 req->analysers = 0;
2836 req->analyse_exp = TICK_ETERNITY;
2837 return 0;
2838}
2839
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002840/* This stream analyser runs all HTTP request processing which is common to
2841 * frontends and backends, which means blocking ACLs, filters, connection-close,
2842 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002843 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002844 * either needs more data or wants to immediately abort the request (eg: deny,
2845 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002846 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002847int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002848{
Willy Tarreaud787e662009-07-07 10:14:51 +02002849 struct http_txn *txn = &s->txn;
2850 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002851 struct acl_cond *cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002852 struct req_acl_rule *req_acl, *req_acl_final = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002853 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002854 struct cond_wordlist *wl;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002855 int del_ka, del_cl, do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002856
Willy Tarreau655dce92009-11-08 13:10:58 +01002857 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002858 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002859 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002860 return 0;
2861 }
2862
Willy Tarreau3a816292009-07-07 10:55:49 +02002863 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002864 req->analyse_exp = TICK_ETERNITY;
2865
2866 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2867 now_ms, __FUNCTION__,
2868 s,
2869 req,
2870 req->rex, req->wex,
2871 req->flags,
2872 req->l,
2873 req->analysers);
2874
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002875 /* first check whether we have some ACLs set to block this request */
2876 list_for_each_entry(cond, &px->block_cond, list) {
2877 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002878
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002879 ret = acl_pass(ret);
2880 if (cond->pol == ACL_COND_UNLESS)
2881 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002882
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002883 if (ret) {
2884 txn->status = 403;
2885 /* let's log the request time */
2886 s->logs.tv_request = now;
2887 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002888 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002889 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002890 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002891 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002892
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002893 do_stats = stats_check_uri(s, px);
2894
2895 list_for_each_entry(req_acl, (do_stats?&px->uri_auth->req_acl:&px->req_acl), list) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002896 int ret = 1;
2897
2898 if (req_acl->action >= PR_REQ_ACL_ACT_MAX)
2899 continue;
2900
2901 /* check condition, but only if attached */
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01002902 if (req_acl->cond) {
2903 ret = acl_exec_cond(req_acl->cond, px, s, txn, ACL_DIR_REQ);
2904 ret = acl_pass(ret);
Willy Tarreau51425942010-02-01 10:40:19 +01002905
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01002906 if (req_acl->cond->pol == ACL_COND_UNLESS)
2907 ret = !ret;
2908 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002909
2910 if (ret) {
2911 req_acl_final = req_acl;
2912 break;
2913 }
2914 }
2915
2916 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_DENY) {
2917 txn->status = 403;
2918 s->logs.tv_request = now;
2919 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002920 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002921 goto return_prx_cond;
2922 }
2923
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002924 /* try headers filters */
2925 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002926 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002927 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002928
Willy Tarreau59234e92008-11-30 23:51:27 +01002929 /* has the request been denied ? */
2930 if (txn->flags & TX_CLDENY) {
2931 /* no need to go further */
2932 txn->status = 403;
2933 /* let's log the request time */
2934 s->logs.tv_request = now;
2935 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002936 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002937 goto return_prx_cond;
2938 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002939
2940 /* When a connection is tarpitted, we use the tarpit timeout,
2941 * which may be the same as the connect timeout if unspecified.
2942 * If unset, then set it to zero because we really want it to
2943 * eventually expire. We build the tarpit as an analyser.
2944 */
2945 if (txn->flags & TX_CLTARPIT) {
2946 buffer_erase(s->req);
2947 /* wipe the request out so that we can drop the connection early
2948 * if the client closes first.
2949 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002950 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002951 req->analysers = 0; /* remove switching rules etc... */
2952 req->analysers |= AN_REQ_HTTP_TARPIT;
2953 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2954 if (!req->analyse_exp)
2955 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002956 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002957 return 1;
2958 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002959 }
Willy Tarreau06619262006-12-17 08:37:22 +01002960
Willy Tarreau5b154472009-12-21 20:11:07 +01002961 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2962 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002963 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2964 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002965 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2966 * avoid to redo the same work if FE and BE have the same settings (common).
2967 * The method consists in checking if options changed between the two calls
2968 * (implying that either one is non-null, or one of them is non-null and we
2969 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002970 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002971
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002972 del_cl = del_ka = 0;
2973
Willy Tarreaudc008c52010-02-01 16:20:08 +01002974 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2975 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2976 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2977 (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 +01002978 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002979
Willy Tarreau5b154472009-12-21 20:11:07 +01002980 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2981 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002982 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2983 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002984 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002985 tmp = TX_CON_WANT_CLO;
2986
Willy Tarreau5b154472009-12-21 20:11:07 +01002987 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2988 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002989
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002990 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2991 /* parse the Connection header and possibly clean it */
2992 int to_del = 0;
2993 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002994 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2995 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002996 to_del |= 2; /* remove "keep-alive" */
2997 if (!(txn->flags & TX_REQ_VER_11))
2998 to_del |= 1; /* remove "close" */
2999 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003000 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003001
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003002 /* check if client or config asks for explicit close in KAL/SCL */
3003 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3004 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3005 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
3006 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
3007 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003008 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
3009 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003010 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3011 }
Willy Tarreau78599912009-10-17 20:12:21 +02003012
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003013 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01003014 list_for_each_entry(wl, &px->req_add, list) {
Willy Tarreau8abd4cd2010-01-31 14:30:44 +01003015 if (wl->cond) {
3016 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
3017 ret = acl_pass(ret);
3018 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3019 ret = !ret;
3020 if (!ret)
3021 continue;
3022 }
3023
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01003024 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003025 goto return_bad_req;
3026 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003027
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003028 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) {
3029 struct chunk msg;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003030 char *realm = req_acl->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003031
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003032 if (!realm)
3033 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3034
Willy Tarreau844a7e72010-01-31 21:46:18 +01003035 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003036 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3037 txn->status = 401;
3038 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003039 /* on 401 we still count one error, because normal browsing
3040 * won't significantly increase the counter but brute force
3041 * attempts will.
3042 */
3043 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003044 goto return_prx_cond;
3045 }
3046
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003047 if (do_stats) {
3048 /* We need to provied stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003049 * FIXME!!! that one is rather dangerous, we want to
3050 * make it follow standard rules (eg: clear req->analysers).
3051 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003052
3053 s->logs.tv_request = now;
3054 s->data_source = DATA_SRC_STATS;
3055 s->data_state = DATA_ST_INIT;
3056 s->task->nice = -32; /* small boost for HTTP statistics */
3057 stream_int_register_handler(s->rep->prod, http_stats_io_handler);
3058 s->rep->prod->private = s;
3059 s->rep->prod->st0 = s->rep->prod->st1 = 0;
3060 req->analysers = 0;
3061
3062 return 0;
3063
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003064 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003065
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003066 /* check whether we have some ACLs set to redirect this request */
3067 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003068 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003069
Willy Tarreauf285f542010-01-03 20:03:03 +01003070 if (rule->cond) {
3071 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3072 ret = acl_pass(ret);
3073 if (rule->cond->pol == ACL_COND_UNLESS)
3074 ret = !ret;
3075 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003076
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003077 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003078 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003079 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003080
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003081 /* build redirect message */
3082 switch(rule->code) {
3083 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003084 msg_fmt = HTTP_303;
3085 break;
3086 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003087 msg_fmt = HTTP_301;
3088 break;
3089 case 302:
3090 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003091 msg_fmt = HTTP_302;
3092 break;
3093 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003094
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003095 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003096 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003097
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003098 switch(rule->type) {
3099 case REDIRECT_TYPE_PREFIX: {
3100 const char *path;
3101 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003102
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003103 path = http_get_path(txn);
3104 /* build message using path */
3105 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003106 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003107 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3108 int qs = 0;
3109 while (qs < pathlen) {
3110 if (path[qs] == '?') {
3111 pathlen = qs;
3112 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003113 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003114 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003115 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003116 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003117 } else {
3118 path = "/";
3119 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003120 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003121
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003122 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003123 goto return_bad_req;
3124
3125 /* add prefix. Note that if prefix == "/", we don't want to
3126 * add anything, otherwise it makes it hard for the user to
3127 * configure a self-redirection.
3128 */
3129 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003130 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3131 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003132 }
3133
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003134 /* add path */
3135 memcpy(rdr.str + rdr.len, path, pathlen);
3136 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003137
3138 /* append a slash at the end of the location is needed and missing */
3139 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3140 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3141 if (rdr.len > rdr.size - 5)
3142 goto return_bad_req;
3143 rdr.str[rdr.len] = '/';
3144 rdr.len++;
3145 }
3146
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003147 break;
3148 }
3149 case REDIRECT_TYPE_LOCATION:
3150 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003151 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003152 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003153
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003154 /* add location */
3155 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3156 rdr.len += rule->rdr_len;
3157 break;
3158 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003159
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003160 if (rule->cookie_len) {
3161 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3162 rdr.len += 14;
3163 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3164 rdr.len += rule->cookie_len;
3165 memcpy(rdr.str + rdr.len, "\r\n", 2);
3166 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003167 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003168
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003169 /* add end of headers and the keep-alive/close status.
3170 * We may choose to set keep-alive if the Location begins
3171 * with a slash, because the client will come back to the
3172 * same server.
3173 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003174 txn->status = rule->code;
3175 /* let's log the request time */
3176 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003177
3178 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3179 (txn->flags & TX_REQ_XFER_LEN) &&
3180 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
3181 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3182 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3183 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003184 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003185 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3186 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3187 rdr.len += 30;
3188 } else {
3189 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3190 rdr.len += 24;
3191 }
Willy Tarreau75661452010-01-10 10:35:01 +01003192 }
3193 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3194 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003195 buffer_write(req->prod->ob, rdr.str, rdr.len);
3196 /* "eat" the request */
3197 buffer_ignore(req, msg->sov - msg->som);
3198 msg->som = msg->sov;
3199 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003200 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3201 txn->req.msg_state = HTTP_MSG_CLOSED;
3202 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003203 break;
3204 } else {
3205 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003206 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3207 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3208 rdr.len += 29;
3209 } else {
3210 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3211 rdr.len += 23;
3212 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003213 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003214 goto return_prx_cond;
3215 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003216 }
3217 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003218
Willy Tarreau2be39392010-01-03 17:24:51 +01003219 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3220 * If this happens, then the data will not come immediately, so we must
3221 * send all what we have without waiting. Note that due to the small gain
3222 * in waiting for the body of the request, it's easier to simply put the
3223 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3224 * itself once used.
3225 */
3226 req->flags |= BF_SEND_DONTWAIT;
3227
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003228 /* that's OK for us now, let's move on to next analysers */
3229 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003230
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003231 return_bad_req:
3232 /* We centralize bad requests processing here */
3233 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3234 /* we detected a parsing error. We want to archive this request
3235 * in the dedicated proxy area for later troubleshooting.
3236 */
3237 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
3238 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003239
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003240 txn->req.msg_state = HTTP_MSG_ERROR;
3241 txn->status = 400;
3242 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003243
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003244 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003245 if (s->listener->counters)
3246 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003247
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003248 return_prx_cond:
3249 if (!(s->flags & SN_ERR_MASK))
3250 s->flags |= SN_ERR_PRXCOND;
3251 if (!(s->flags & SN_FINST_MASK))
3252 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003253
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003254 req->analysers = 0;
3255 req->analyse_exp = TICK_ETERNITY;
3256 return 0;
3257}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003258
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003259/* This function performs all the processing enabled for the current request.
3260 * It returns 1 if the processing can continue on next analysers, or zero if it
3261 * needs more data, encounters an error, or wants to immediately abort the
3262 * request. It relies on buffers flags, and updates s->req->analysers.
3263 */
3264int http_process_request(struct session *s, struct buffer *req, int an_bit)
3265{
3266 struct http_txn *txn = &s->txn;
3267 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003268
Willy Tarreau655dce92009-11-08 13:10:58 +01003269 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003270 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003271 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003272 return 0;
3273 }
3274
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003275 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3276 now_ms, __FUNCTION__,
3277 s,
3278 req,
3279 req->rex, req->wex,
3280 req->flags,
3281 req->l,
3282 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003283
Willy Tarreau59234e92008-11-30 23:51:27 +01003284 /*
3285 * Right now, we know that we have processed the entire headers
3286 * and that unwanted requests have been filtered out. We can do
3287 * whatever we want with the remaining request. Also, now we
3288 * may have separate values for ->fe, ->be.
3289 */
Willy Tarreau06619262006-12-17 08:37:22 +01003290
Willy Tarreau59234e92008-11-30 23:51:27 +01003291 /*
3292 * If HTTP PROXY is set we simply get remote server address
3293 * parsing incoming request.
3294 */
3295 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003296 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003297 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003298
Willy Tarreau59234e92008-11-30 23:51:27 +01003299 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003300 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003301 * Note that doing so might move headers in the request, but
3302 * the fields will stay coherent and the URI will not move.
3303 * This should only be performed in the backend.
3304 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003305 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003306 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3307 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003308
Willy Tarreau59234e92008-11-30 23:51:27 +01003309 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003310 * 8: the appsession cookie was looked up very early in 1.2,
3311 * so let's do the same now.
3312 */
3313
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003314 /* It needs to look into the URI unless persistence must be ignored */
3315 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003316 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003317 }
3318
3319 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003320 * 9: add X-Forwarded-For if either the frontend or the backend
3321 * asks for it.
3322 */
3323 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3324 if (s->cli_addr.ss_family == AF_INET) {
3325 /* Add an X-Forwarded-For header unless the source IP is
3326 * in the 'except' network range.
3327 */
3328 if ((!s->fe->except_mask.s_addr ||
3329 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3330 != s->fe->except_net.s_addr) &&
3331 (!s->be->except_mask.s_addr ||
3332 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3333 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003334 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003335 unsigned char *pn;
3336 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003337
3338 /* Note: we rely on the backend to get the header name to be used for
3339 * x-forwarded-for, because the header is really meant for the backends.
3340 * However, if the backend did not specify any option, we have to rely
3341 * on the frontend's header name.
3342 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003343 if (s->be->fwdfor_hdr_len) {
3344 len = s->be->fwdfor_hdr_len;
3345 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003346 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003347 len = s->fe->fwdfor_hdr_len;
3348 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003349 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003350 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003351
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003352 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003353 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003354 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003355 }
3356 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003357 else if (s->cli_addr.ss_family == AF_INET6) {
3358 /* FIXME: for the sake of completeness, we should also support
3359 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003360 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003361 int len;
3362 char pn[INET6_ADDRSTRLEN];
3363 inet_ntop(AF_INET6,
3364 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3365 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003366
Willy Tarreau59234e92008-11-30 23:51:27 +01003367 /* Note: we rely on the backend to get the header name to be used for
3368 * x-forwarded-for, because the header is really meant for the backends.
3369 * However, if the backend did not specify any option, we have to rely
3370 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003371 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003372 if (s->be->fwdfor_hdr_len) {
3373 len = s->be->fwdfor_hdr_len;
3374 memcpy(trash, s->be->fwdfor_hdr_name, len);
3375 } else {
3376 len = s->fe->fwdfor_hdr_len;
3377 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003378 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003379 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003380
Willy Tarreau59234e92008-11-30 23:51:27 +01003381 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003382 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003383 goto return_bad_req;
3384 }
3385 }
3386
3387 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003388 * 10: add X-Original-To if either the frontend or the backend
3389 * asks for it.
3390 */
3391 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3392
3393 /* FIXME: don't know if IPv6 can handle that case too. */
3394 if (s->cli_addr.ss_family == AF_INET) {
3395 /* Add an X-Original-To header unless the destination IP is
3396 * in the 'except' network range.
3397 */
3398 if (!(s->flags & SN_FRT_ADDR_SET))
3399 get_frt_addr(s);
3400
3401 if ((!s->fe->except_mask_to.s_addr ||
3402 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3403 != s->fe->except_to.s_addr) &&
3404 (!s->be->except_mask_to.s_addr ||
3405 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3406 != s->be->except_to.s_addr)) {
3407 int len;
3408 unsigned char *pn;
3409 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3410
3411 /* Note: we rely on the backend to get the header name to be used for
3412 * x-original-to, because the header is really meant for the backends.
3413 * However, if the backend did not specify any option, we have to rely
3414 * on the frontend's header name.
3415 */
3416 if (s->be->orgto_hdr_len) {
3417 len = s->be->orgto_hdr_len;
3418 memcpy(trash, s->be->orgto_hdr_name, len);
3419 } else {
3420 len = s->fe->orgto_hdr_len;
3421 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003422 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003423 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3424
3425 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003426 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003427 goto return_bad_req;
3428 }
3429 }
3430 }
3431
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003432 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3433 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3434 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
3435 unsigned int want_flags = 0;
3436
3437 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003438 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3439 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) ||
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003440 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))
3441 want_flags |= TX_CON_CLO_SET;
3442 } else {
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003443 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3444 (((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA) &&
3445 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003446 want_flags |= TX_CON_KAL_SET;
3447 }
3448
3449 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3450 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003451 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003452
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003453
Willy Tarreau522d6c02009-12-06 18:49:18 +01003454 /* If we have no server assigned yet and we're balancing on url_param
3455 * with a POST request, we may be interested in checking the body for
3456 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003457 */
3458 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3459 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003460 s->be->url_param_post_limit != 0 &&
3461 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003462 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003463 buffer_dont_connect(req);
3464 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003465 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003466
Willy Tarreaud98cf932009-12-27 22:54:55 +01003467 if (txn->flags & TX_REQ_XFER_LEN)
3468 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003469
Willy Tarreau59234e92008-11-30 23:51:27 +01003470 /*************************************************************
3471 * OK, that's finished for the headers. We have done what we *
3472 * could. Let's switch to the DATA state. *
3473 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003474 req->analyse_exp = TICK_ETERNITY;
3475 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003476
Willy Tarreau59234e92008-11-30 23:51:27 +01003477 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003478 /* OK let's go on with the BODY now */
3479 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003480
Willy Tarreau59234e92008-11-30 23:51:27 +01003481 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003482 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003483 /* we detected a parsing error. We want to archive this request
3484 * in the dedicated proxy area for later troubleshooting.
3485 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003486 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003487 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003488
Willy Tarreau59234e92008-11-30 23:51:27 +01003489 txn->req.msg_state = HTTP_MSG_ERROR;
3490 txn->status = 400;
3491 req->analysers = 0;
3492 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003493
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003494 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003495 if (s->listener->counters)
3496 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003497
Willy Tarreau59234e92008-11-30 23:51:27 +01003498 if (!(s->flags & SN_ERR_MASK))
3499 s->flags |= SN_ERR_PRXCOND;
3500 if (!(s->flags & SN_FINST_MASK))
3501 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003502 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003503}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003504
Willy Tarreau60b85b02008-11-30 23:28:40 +01003505/* This function is an analyser which processes the HTTP tarpit. It always
3506 * returns zero, at the beginning because it prevents any other processing
3507 * from occurring, and at the end because it terminates the request.
3508 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003509int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003510{
3511 struct http_txn *txn = &s->txn;
3512
3513 /* This connection is being tarpitted. The CLIENT side has
3514 * already set the connect expiration date to the right
3515 * timeout. We just have to check that the client is still
3516 * there and that the timeout has not expired.
3517 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003518 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003519 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3520 !tick_is_expired(req->analyse_exp, now_ms))
3521 return 0;
3522
3523 /* We will set the queue timer to the time spent, just for
3524 * logging purposes. We fake a 500 server error, so that the
3525 * attacker will not suspect his connection has been tarpitted.
3526 * It will not cause trouble to the logs because we can exclude
3527 * the tarpitted connections by filtering on the 'PT' status flags.
3528 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003529 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3530
3531 txn->status = 500;
3532 if (req->flags != BF_READ_ERROR)
3533 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3534
3535 req->analysers = 0;
3536 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003537
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003538 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003539 if (s->listener->counters)
3540 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003541
Willy Tarreau60b85b02008-11-30 23:28:40 +01003542 if (!(s->flags & SN_ERR_MASK))
3543 s->flags |= SN_ERR_PRXCOND;
3544 if (!(s->flags & SN_FINST_MASK))
3545 s->flags |= SN_FINST_T;
3546 return 0;
3547}
3548
Willy Tarreaud34af782008-11-30 23:36:37 +01003549/* This function is an analyser which processes the HTTP request body. It looks
3550 * for parameters to be used for the load balancing algorithm (url_param). It
3551 * must only be called after the standard HTTP request processing has occurred,
3552 * because it expects the request to be parsed. It returns zero if it needs to
3553 * read more data, or 1 once it has completed its analysis.
3554 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003555int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003556{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003557 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003558 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003559 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003560
3561 /* We have to parse the HTTP request body to find any required data.
3562 * "balance url_param check_post" should have been the only way to get
3563 * into this. We were brought here after HTTP header analysis, so all
3564 * related structures are ready.
3565 */
3566
Willy Tarreau522d6c02009-12-06 18:49:18 +01003567 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3568 goto missing_data;
3569
3570 if (msg->msg_state < HTTP_MSG_100_SENT) {
3571 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3572 * send an HTTP/1.1 100 Continue intermediate response.
3573 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003574 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003575 struct hdr_ctx ctx;
3576 ctx.idx = 0;
3577 /* Expect is allowed in 1.1, look for it */
3578 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3579 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3580 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3581 }
3582 }
3583 msg->msg_state = HTTP_MSG_100_SENT;
3584 }
3585
3586 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003587 /* we have msg->col and msg->sov which both point to the first
3588 * byte of message body. msg->som still points to the beginning
3589 * of the message. We must save the body in req->lr because it
3590 * survives buffer re-alignments.
3591 */
3592 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003593 if (txn->flags & TX_REQ_TE_CHNK)
3594 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3595 else
3596 msg->msg_state = HTTP_MSG_DATA;
3597 }
3598
3599 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003600 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003601 * set ->sov and ->lr to point to the body and switch to DATA or
3602 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003603 */
3604 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003605
Willy Tarreau115acb92009-12-26 13:56:06 +01003606 if (!ret)
3607 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003608 else if (ret < 0) {
3609 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003610 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003611 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003612 }
3613
Willy Tarreaud98cf932009-12-27 22:54:55 +01003614 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003615 * We have the first non-header byte in msg->col, which is either the
3616 * beginning of the chunk size or of the data. The first data byte is in
3617 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3618 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003619 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003620
3621 if (msg->hdr_content_len < limit)
3622 limit = msg->hdr_content_len;
3623
Willy Tarreau7c96f672009-12-27 22:47:25 +01003624 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003625 goto http_end;
3626
3627 missing_data:
3628 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003629 if (req->flags & BF_FULL) {
3630 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003631 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003632 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003633
Willy Tarreau522d6c02009-12-06 18:49:18 +01003634 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3635 txn->status = 408;
3636 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003637
3638 if (!(s->flags & SN_ERR_MASK))
3639 s->flags |= SN_ERR_CLITO;
3640 if (!(s->flags & SN_FINST_MASK))
3641 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003642 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003643 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003644
3645 /* we get here if we need to wait for more data */
3646 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003647 /* Not enough data. We'll re-use the http-request
3648 * timeout here. Ideally, we should set the timeout
3649 * relative to the accept() date. We just set the
3650 * request timeout once at the beginning of the
3651 * request.
3652 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003653 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003654 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003655 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003656 return 0;
3657 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003658
3659 http_end:
3660 /* The situation will not evolve, so let's give up on the analysis. */
3661 s->logs.tv_request = now; /* update the request timer to reflect full request */
3662 req->analysers &= ~an_bit;
3663 req->analyse_exp = TICK_ETERNITY;
3664 return 1;
3665
3666 return_bad_req: /* let's centralize all bad requests */
3667 txn->req.msg_state = HTTP_MSG_ERROR;
3668 txn->status = 400;
3669 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3670
Willy Tarreau79ebac62010-06-07 13:47:49 +02003671 if (!(s->flags & SN_ERR_MASK))
3672 s->flags |= SN_ERR_PRXCOND;
3673 if (!(s->flags & SN_FINST_MASK))
3674 s->flags |= SN_FINST_R;
3675
Willy Tarreau522d6c02009-12-06 18:49:18 +01003676 return_err_msg:
3677 req->analysers = 0;
3678 s->fe->counters.failed_req++;
3679 if (s->listener->counters)
3680 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003681 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003682}
3683
Willy Tarreau610ecce2010-01-04 21:15:02 +01003684/* Terminate current transaction and prepare a new one. This is very tricky
3685 * right now but it works.
3686 */
3687void http_end_txn_clean_session(struct session *s)
3688{
3689 /* FIXME: We need a more portable way of releasing a backend's and a
3690 * server's connections. We need a safer way to reinitialize buffer
3691 * flags. We also need a more accurate method for computing per-request
3692 * data.
3693 */
3694 http_silent_debug(__LINE__, s);
3695
3696 s->req->cons->flags |= SI_FL_NOLINGER;
3697 s->req->cons->shutr(s->req->cons);
3698 s->req->cons->shutw(s->req->cons);
3699
3700 http_silent_debug(__LINE__, s);
3701
3702 if (s->flags & SN_BE_ASSIGNED)
3703 s->be->beconn--;
3704
3705 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3706 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003707 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003708
3709 if (s->txn.status) {
3710 int n;
3711
3712 n = s->txn.status / 100;
3713 if (n < 1 || n > 5)
3714 n = 0;
3715
3716 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01003717 s->fe->counters.fe.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003718
Willy Tarreau24657792010-02-26 10:30:28 +01003719 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003720 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01003721 s->be->counters.be.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003722 }
3723
3724 /* don't count other requests' data */
3725 s->logs.bytes_in -= s->req->l - s->req->send_max;
3726 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3727
3728 /* let's do a final log if we need it */
3729 if (s->logs.logwait &&
3730 !(s->flags & SN_MONITOR) &&
3731 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3732 s->do_log(s);
3733 }
3734
3735 s->logs.accept_date = date; /* user-visible date for logging */
3736 s->logs.tv_accept = now; /* corrected date for internal use */
3737 tv_zero(&s->logs.tv_request);
3738 s->logs.t_queue = -1;
3739 s->logs.t_connect = -1;
3740 s->logs.t_data = -1;
3741 s->logs.t_close = 0;
3742 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3743 s->logs.srv_queue_size = 0; /* we will get this number soon */
3744
3745 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3746 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3747
3748 if (s->pend_pos)
3749 pendconn_free(s->pend_pos);
3750
3751 if (s->srv) {
3752 if (s->flags & SN_CURR_SESS) {
3753 s->flags &= ~SN_CURR_SESS;
3754 s->srv->cur_sess--;
3755 }
3756 if (may_dequeue_tasks(s->srv, s->be))
3757 process_srv_queue(s->srv);
3758 }
3759
3760 if (unlikely(s->srv_conn))
3761 sess_change_server(s, NULL);
3762 s->srv = NULL;
3763
3764 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3765 s->req->cons->fd = -1; /* just to help with debugging */
3766 s->req->cons->err_type = SI_ET_NONE;
3767 s->req->cons->err_loc = NULL;
3768 s->req->cons->exp = TICK_ETERNITY;
3769 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003770 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3771 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 +02003772 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 +01003773 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3774 s->txn.meth = 0;
3775 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003776 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003777 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003778 s->req->cons->flags |= SI_FL_INDEP_STR;
3779
3780 /* if the request buffer is not empty, it means we're
3781 * about to process another request, so send pending
3782 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003783 * Just don't do this if the buffer is close to be full,
3784 * because the request will wait for it to flush a little
3785 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003786 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003787 if (s->req->l > s->req->send_max) {
3788 if (s->rep->send_max &&
3789 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003790 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3791 s->rep->flags |= BF_EXPECT_MORE;
3792 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003793
3794 /* we're removing the analysers, we MUST re-enable events detection */
3795 buffer_auto_read(s->req);
3796 buffer_auto_close(s->req);
3797 buffer_auto_read(s->rep);
3798 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003799
3800 /* make ->lr point to the first non-forwarded byte */
3801 s->req->lr = s->req->w + s->req->send_max;
3802 if (s->req->lr >= s->req->data + s->req->size)
3803 s->req->lr -= s->req->size;
3804 s->rep->lr = s->rep->w + s->rep->send_max;
3805 if (s->rep->lr >= s->rep->data + s->rep->size)
3806 s->rep->lr -= s->req->size;
3807
Willy Tarreau6df7a0e2010-08-03 11:52:10 +02003808 s->req->analysers |= s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003809 s->rep->analysers = 0;
3810
3811 http_silent_debug(__LINE__, s);
3812}
3813
3814
3815/* This function updates the request state machine according to the response
3816 * state machine and buffer flags. It returns 1 if it changes anything (flag
3817 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3818 * it is only used to find when a request/response couple is complete. Both
3819 * this function and its equivalent should loop until both return zero. It
3820 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3821 */
3822int http_sync_req_state(struct session *s)
3823{
3824 struct buffer *buf = s->req;
3825 struct http_txn *txn = &s->txn;
3826 unsigned int old_flags = buf->flags;
3827 unsigned int old_state = txn->req.msg_state;
3828
3829 http_silent_debug(__LINE__, s);
3830 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3831 return 0;
3832
3833 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003834 /* No need to read anymore, the request was completely parsed.
3835 * We can shut the read side unless we want to abort_on_close.
3836 */
3837 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3838 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003839
3840 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3841 goto wait_other_side;
3842
3843 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3844 /* The server has not finished to respond, so we
3845 * don't want to move in order not to upset it.
3846 */
3847 goto wait_other_side;
3848 }
3849
3850 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3851 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003852 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003853 txn->req.msg_state = HTTP_MSG_TUNNEL;
3854 goto wait_other_side;
3855 }
3856
3857 /* When we get here, it means that both the request and the
3858 * response have finished receiving. Depending on the connection
3859 * mode, we'll have to wait for the last bytes to leave in either
3860 * direction, and sometimes for a close to be effective.
3861 */
3862
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003863 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3864 /* Server-close mode : queue a connection close to the server */
3865 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003866 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003867 }
3868 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3869 /* Option forceclose is set, or either side wants to close,
3870 * let's enforce it now that we're not expecting any new
3871 * data to come. The caller knows the session is complete
3872 * once both states are CLOSED.
3873 */
3874 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003875 buffer_shutr_now(buf);
3876 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003877 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003878 }
3879 else {
3880 /* The last possible modes are keep-alive and tunnel. Since tunnel
3881 * mode does not set the body analyser, we can't reach this place
3882 * in tunnel mode, so we're left with keep-alive only.
3883 * This mode is currently not implemented, we switch to tunnel mode.
3884 */
3885 buffer_auto_read(buf);
3886 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003887 }
3888
3889 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3890 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003891 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3892
Willy Tarreau610ecce2010-01-04 21:15:02 +01003893 if (!(buf->flags & BF_OUT_EMPTY)) {
3894 txn->req.msg_state = HTTP_MSG_CLOSING;
3895 goto http_msg_closing;
3896 }
3897 else {
3898 txn->req.msg_state = HTTP_MSG_CLOSED;
3899 goto http_msg_closed;
3900 }
3901 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003902 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003903 }
3904
3905 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3906 http_msg_closing:
3907 /* nothing else to forward, just waiting for the output buffer
3908 * to be empty and for the shutw_now to take effect.
3909 */
3910 if (buf->flags & BF_OUT_EMPTY) {
3911 txn->req.msg_state = HTTP_MSG_CLOSED;
3912 goto http_msg_closed;
3913 }
3914 else if (buf->flags & BF_SHUTW) {
3915 txn->req.msg_state = HTTP_MSG_ERROR;
3916 goto wait_other_side;
3917 }
3918 }
3919
3920 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3921 http_msg_closed:
3922 goto wait_other_side;
3923 }
3924
3925 wait_other_side:
3926 http_silent_debug(__LINE__, s);
3927 return txn->req.msg_state != old_state || buf->flags != old_flags;
3928}
3929
3930
3931/* This function updates the response state machine according to the request
3932 * state machine and buffer flags. It returns 1 if it changes anything (flag
3933 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3934 * it is only used to find when a request/response couple is complete. Both
3935 * this function and its equivalent should loop until both return zero. It
3936 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3937 */
3938int http_sync_res_state(struct session *s)
3939{
3940 struct buffer *buf = s->rep;
3941 struct http_txn *txn = &s->txn;
3942 unsigned int old_flags = buf->flags;
3943 unsigned int old_state = txn->rsp.msg_state;
3944
3945 http_silent_debug(__LINE__, s);
3946 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3947 return 0;
3948
3949 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3950 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003951 * still monitor the server connection for a possible close
3952 * while the request is being uploaded, so we don't disable
3953 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003954 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003955 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003956
3957 if (txn->req.msg_state == HTTP_MSG_ERROR)
3958 goto wait_other_side;
3959
3960 if (txn->req.msg_state < HTTP_MSG_DONE) {
3961 /* The client seems to still be sending data, probably
3962 * because we got an error response during an upload.
3963 * We have the choice of either breaking the connection
3964 * or letting it pass through. Let's do the later.
3965 */
3966 goto wait_other_side;
3967 }
3968
3969 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3970 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003971 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003972 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3973 goto wait_other_side;
3974 }
3975
3976 /* When we get here, it means that both the request and the
3977 * response have finished receiving. Depending on the connection
3978 * mode, we'll have to wait for the last bytes to leave in either
3979 * direction, and sometimes for a close to be effective.
3980 */
3981
3982 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3983 /* Server-close mode : shut read and wait for the request
3984 * side to close its output buffer. The caller will detect
3985 * when we're in DONE and the other is in CLOSED and will
3986 * catch that for the final cleanup.
3987 */
3988 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3989 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003990 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003991 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3992 /* Option forceclose is set, or either side wants to close,
3993 * let's enforce it now that we're not expecting any new
3994 * data to come. The caller knows the session is complete
3995 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003996 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003997 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
3998 buffer_shutr_now(buf);
3999 buffer_shutw_now(buf);
4000 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004001 }
4002 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004003 /* The last possible modes are keep-alive and tunnel. Since tunnel
4004 * mode does not set the body analyser, we can't reach this place
4005 * in tunnel mode, so we're left with keep-alive only.
4006 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004007 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004008 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004010 }
4011
4012 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4013 /* if we've just closed an output, let's switch */
4014 if (!(buf->flags & BF_OUT_EMPTY)) {
4015 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4016 goto http_msg_closing;
4017 }
4018 else {
4019 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4020 goto http_msg_closed;
4021 }
4022 }
4023 goto wait_other_side;
4024 }
4025
4026 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4027 http_msg_closing:
4028 /* nothing else to forward, just waiting for the output buffer
4029 * to be empty and for the shutw_now to take effect.
4030 */
4031 if (buf->flags & BF_OUT_EMPTY) {
4032 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4033 goto http_msg_closed;
4034 }
4035 else if (buf->flags & BF_SHUTW) {
4036 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreauae526782010-03-04 20:34:23 +01004037 s->be->counters.cli_aborts++;
4038 if (s->srv)
4039 s->srv->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004040 goto wait_other_side;
4041 }
4042 }
4043
4044 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4045 http_msg_closed:
4046 /* drop any pending data */
4047 buffer_ignore(buf, buf->l - buf->send_max);
4048 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004049 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004050 goto wait_other_side;
4051 }
4052
4053 wait_other_side:
4054 http_silent_debug(__LINE__, s);
4055 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4056}
4057
4058
4059/* Resync the request and response state machines. Return 1 if either state
4060 * changes.
4061 */
4062int http_resync_states(struct session *s)
4063{
4064 struct http_txn *txn = &s->txn;
4065 int old_req_state = txn->req.msg_state;
4066 int old_res_state = txn->rsp.msg_state;
4067
4068 http_silent_debug(__LINE__, s);
4069 http_sync_req_state(s);
4070 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004071 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004072 if (!http_sync_res_state(s))
4073 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004074 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004075 if (!http_sync_req_state(s))
4076 break;
4077 }
4078 http_silent_debug(__LINE__, s);
4079 /* OK, both state machines agree on a compatible state.
4080 * There are a few cases we're interested in :
4081 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4082 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4083 * directions, so let's simply disable both analysers.
4084 * - HTTP_MSG_CLOSED on the response only means we must abort the
4085 * request.
4086 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4087 * with server-close mode means we've completed one request and we
4088 * must re-initialize the server connection.
4089 */
4090
4091 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4092 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4093 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4094 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4095 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004096 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004097 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004098 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004099 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004100 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004101 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004102 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4103 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004104 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004105 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004106 s->rep->analysers = 0;
4107 buffer_auto_close(s->rep);
4108 buffer_auto_read(s->rep);
4109 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004110 buffer_abort(s->req);
4111 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004112 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004113 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004114 }
4115 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4116 txn->rsp.msg_state == HTTP_MSG_DONE &&
4117 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4118 /* server-close: terminate this server connection and
4119 * reinitialize a fresh-new transaction.
4120 */
4121 http_end_txn_clean_session(s);
4122 }
4123
4124 http_silent_debug(__LINE__, s);
4125 return txn->req.msg_state != old_req_state ||
4126 txn->rsp.msg_state != old_res_state;
4127}
4128
Willy Tarreaud98cf932009-12-27 22:54:55 +01004129/* This function is an analyser which forwards request body (including chunk
4130 * sizes if any). It is called as soon as we must forward, even if we forward
4131 * zero byte. The only situation where it must not be called is when we're in
4132 * tunnel mode and we want to forward till the close. It's used both to forward
4133 * remaining data and to resync after end of body. It expects the msg_state to
4134 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4135 * read more data, or 1 once we can go on with next request or end the session.
4136 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4137 * bytes of pending data + the headers if not already done (between som and sov).
4138 * It eventually adjusts som to match sov after the data in between have been sent.
4139 */
4140int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4141{
4142 struct http_txn *txn = &s->txn;
4143 struct http_msg *msg = &s->txn.req;
4144
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004145 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4146 return 0;
4147
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004148 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4149 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004150 /* Output closed while we were sending data. We must abort and
4151 * wake the other side up.
4152 */
4153 msg->msg_state = HTTP_MSG_ERROR;
4154 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004155 return 1;
4156 }
4157
Willy Tarreau4fe41902010-06-07 22:27:41 +02004158 /* in most states, we should abort in case of early close */
4159 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004160
4161 /* Note that we don't have to send 100-continue back because we don't
4162 * need the data to complete our job, and it's up to the server to
4163 * decide whether to return 100, 417 or anything else in return of
4164 * an "Expect: 100-continue" header.
4165 */
4166
4167 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4168 /* we have msg->col and msg->sov which both point to the first
4169 * byte of message body. msg->som still points to the beginning
4170 * of the message. We must save the body in req->lr because it
4171 * survives buffer re-alignments.
4172 */
4173 req->lr = req->data + msg->sov;
4174 if (txn->flags & TX_REQ_TE_CHNK)
4175 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4176 else {
4177 msg->msg_state = HTTP_MSG_DATA;
4178 }
4179 }
4180
Willy Tarreaud98cf932009-12-27 22:54:55 +01004181 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004182 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004183 /* we may have some data pending */
4184 if (msg->hdr_content_len || msg->som != msg->sov) {
4185 int bytes = msg->sov - msg->som;
4186 if (bytes < 0) /* sov may have wrapped at the end */
4187 bytes += req->size;
4188 buffer_forward(req, bytes + msg->hdr_content_len);
4189 msg->hdr_content_len = 0; /* don't forward that again */
4190 msg->som = msg->sov;
4191 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004192
Willy Tarreaucaabe412010-01-03 23:08:28 +01004193 if (msg->msg_state == HTTP_MSG_DATA) {
4194 /* must still forward */
4195 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004196 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004197
4198 /* nothing left to forward */
4199 if (txn->flags & TX_REQ_TE_CHNK)
4200 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004201 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004202 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004203 }
4204 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004205 /* read the chunk size and assign it to ->hdr_content_len, then
4206 * set ->sov and ->lr to point to the body and switch to DATA or
4207 * TRAILERS state.
4208 */
4209 int ret = http_parse_chunk_size(req, msg);
4210
4211 if (!ret)
4212 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004213 else if (ret < 0) {
4214 session_inc_http_err_ctr(s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004215 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004216 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004217 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004218 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004219 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4220 /* we want the CRLF after the data */
4221 int ret;
4222
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004223 req->lr = req->w + req->send_max;
4224 if (req->lr >= req->data + req->size)
4225 req->lr -= req->size;
4226
Willy Tarreaud98cf932009-12-27 22:54:55 +01004227 ret = http_skip_chunk_crlf(req, msg);
4228
4229 if (ret == 0)
4230 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004231 else if (ret < 0) {
4232 session_inc_http_err_ctr(s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004233 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004234 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004235 /* we're in MSG_CHUNK_SIZE now */
4236 }
4237 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4238 int ret = http_forward_trailers(req, msg);
4239
4240 if (ret == 0)
4241 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004242 else if (ret < 0) {
4243 session_inc_http_err_ctr(s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004244 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004245 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004246 /* we're in HTTP_MSG_DONE now */
4247 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004248 else {
4249 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004250 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004251 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4252 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4253 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004254 if (http_resync_states(s)) {
4255 /* some state changes occurred, maybe the analyser
4256 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004257 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004258 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4259 goto return_bad_req;
4260 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004261 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004262
4263 /* If "option abortonclose" is set on the backend, we
4264 * want to monitor the client's connection and forward
4265 * any shutdown notification to the server, which will
4266 * decide whether to close or to go on processing the
4267 * request.
4268 */
4269 if (s->be->options & PR_O_ABRT_CLOSE) {
4270 buffer_auto_read(req);
4271 buffer_auto_close(req);
4272 }
4273
Willy Tarreau610ecce2010-01-04 21:15:02 +01004274 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004275 }
4276 }
4277
Willy Tarreaud98cf932009-12-27 22:54:55 +01004278 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004279 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004280 if (req->flags & BF_SHUTR) {
4281 if (!(s->flags & SN_ERR_MASK))
4282 s->flags |= SN_ERR_CLICL;
4283 if (!(s->flags & SN_FINST_MASK))
4284 s->flags |= SN_FINST_D;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004285 goto return_bad_req;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004286 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004287
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004288 /* waiting for the last bits to leave the buffer */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004289 if (req->flags & BF_SHUTW) {
4290 if (!(s->flags & SN_ERR_MASK))
4291 s->flags |= SN_ERR_SRVCL;
4292 if (!(s->flags & SN_FINST_MASK))
4293 s->flags |= SN_FINST_D;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004294 goto return_bad_req;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004295 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004296
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004297 /* When TE: chunked is used, we need to get there again to parse remaining
4298 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4299 */
4300 if (txn->flags & TX_REQ_TE_CHNK)
4301 buffer_dont_close(req);
4302
Willy Tarreau610ecce2010-01-04 21:15:02 +01004303 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004304 return 0;
4305
Willy Tarreaud98cf932009-12-27 22:54:55 +01004306 return_bad_req: /* let's centralize all bad requests */
4307 txn->req.msg_state = HTTP_MSG_ERROR;
4308 txn->status = 400;
4309 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01004310 stream_int_retnclose(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004311 req->analysers = 0;
4312 s->fe->counters.failed_req++;
4313 if (s->listener->counters)
4314 s->listener->counters->failed_req++;
4315
4316 if (!(s->flags & SN_ERR_MASK))
4317 s->flags |= SN_ERR_PRXCOND;
4318 if (!(s->flags & SN_FINST_MASK))
4319 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004320 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004321 return 0;
4322}
4323
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004324/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4325 * processing can continue on next analysers, or zero if it either needs more
4326 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4327 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4328 * when it has nothing left to do, and may remove any analyser when it wants to
4329 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004330 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004331int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004332{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004333 struct http_txn *txn = &s->txn;
4334 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004335 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004336 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004337 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004338 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004339
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004340 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 +02004341 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004342 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004343 rep,
4344 rep->rex, rep->wex,
4345 rep->flags,
4346 rep->l,
4347 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004348
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004349 /*
4350 * Now parse the partial (or complete) lines.
4351 * We will check the response syntax, and also join multi-line
4352 * headers. An index of all the lines will be elaborated while
4353 * parsing.
4354 *
4355 * For the parsing, we use a 28 states FSM.
4356 *
4357 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004358 * rep->data + msg->som = beginning of response
4359 * rep->data + msg->eoh = end of processed headers / start of current one
4360 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004361 * rep->lr = first non-visited byte
4362 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004363 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004364 */
4365
Willy Tarreau83e3af02009-12-28 17:39:57 +01004366 /* There's a protected area at the end of the buffer for rewriting
4367 * purposes. We don't want to start to parse the request if the
4368 * protected area is affected, because we may have to move processed
4369 * data later, which is much more complicated.
4370 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004371 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4372 if (unlikely((rep->flags & BF_FULL) ||
4373 rep->r < rep->lr ||
4374 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4375 if (rep->send_max) {
4376 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004377 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4378 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004379 buffer_dont_close(rep);
4380 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4381 return 0;
4382 }
4383 if (rep->l <= rep->size - global.tune.maxrewrite)
4384 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004385 }
4386
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004387 if (likely(rep->lr < rep->r))
4388 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004389 }
4390
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004391 /* 1: we might have to print this header in debug mode */
4392 if (unlikely((global.mode & MODE_DEBUG) &&
4393 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004394 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004395 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004396 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004397
Willy Tarreau663308b2010-06-07 14:06:08 +02004398 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004399 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004400 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004401
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004402 sol += hdr_idx_first_pos(&txn->hdr_idx);
4403 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004404
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004405 while (cur_idx) {
4406 eol = sol + txn->hdr_idx.v[cur_idx].len;
4407 debug_hdr("srvhdr", s, sol, eol);
4408 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4409 cur_idx = txn->hdr_idx.v[cur_idx].next;
4410 }
4411 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004412
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004413 /*
4414 * Now we quickly check if we have found a full valid response.
4415 * If not so, we check the FD and buffer states before leaving.
4416 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004417 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004418 * responses are checked first.
4419 *
4420 * Depending on whether the client is still there or not, we
4421 * may send an error response back or not. Note that normally
4422 * we should only check for HTTP status there, and check I/O
4423 * errors somewhere else.
4424 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004425
Willy Tarreau655dce92009-11-08 13:10:58 +01004426 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004427 /* Invalid response */
4428 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4429 /* we detected a parsing error. We want to archive this response
4430 * in the dedicated proxy area for later troubleshooting.
4431 */
4432 hdr_response_bad:
4433 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4434 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4435
4436 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004437 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004438 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004439 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4440 }
Willy Tarreau64648412010-03-05 10:41:54 +01004441 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004442 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004443 rep->analysers = 0;
4444 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004445 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004446 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004447 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4448
4449 if (!(s->flags & SN_ERR_MASK))
4450 s->flags |= SN_ERR_PRXCOND;
4451 if (!(s->flags & SN_FINST_MASK))
4452 s->flags |= SN_FINST_H;
4453
4454 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004455 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004456
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004457 /* too large response does not fit in buffer. */
4458 else if (rep->flags & BF_FULL) {
4459 goto hdr_response_bad;
4460 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004461
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004462 /* read error */
4463 else if (rep->flags & BF_READ_ERROR) {
4464 if (msg->err_pos >= 0)
4465 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004466
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004467 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004468 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004469 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004470 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4471 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004472
Willy Tarreau90deb182010-01-07 00:20:41 +01004473 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004474 rep->analysers = 0;
4475 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004476 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004477 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004478 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004479
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004480 if (!(s->flags & SN_ERR_MASK))
4481 s->flags |= SN_ERR_SRVCL;
4482 if (!(s->flags & SN_FINST_MASK))
4483 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004484 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004485 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004486
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004487 /* read timeout : return a 504 to the client. */
4488 else if (rep->flags & BF_READ_TIMEOUT) {
4489 if (msg->err_pos >= 0)
4490 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004491
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004492 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004493 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004494 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004495 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4496 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004497
Willy Tarreau90deb182010-01-07 00:20:41 +01004498 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004499 rep->analysers = 0;
4500 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004501 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004502 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004503 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004504
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004505 if (!(s->flags & SN_ERR_MASK))
4506 s->flags |= SN_ERR_SRVTO;
4507 if (!(s->flags & SN_FINST_MASK))
4508 s->flags |= SN_FINST_H;
4509 return 0;
4510 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004511
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004512 /* close from server */
4513 else if (rep->flags & BF_SHUTR) {
4514 if (msg->err_pos >= 0)
4515 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004516
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004517 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004518 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004519 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004520 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4521 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004522
Willy Tarreau90deb182010-01-07 00:20:41 +01004523 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004524 rep->analysers = 0;
4525 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004526 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004527 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004528 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004529
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004530 if (!(s->flags & SN_ERR_MASK))
4531 s->flags |= SN_ERR_SRVCL;
4532 if (!(s->flags & SN_FINST_MASK))
4533 s->flags |= SN_FINST_H;
4534 return 0;
4535 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004536
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004537 /* write error to client (we don't send any message then) */
4538 else if (rep->flags & BF_WRITE_ERROR) {
4539 if (msg->err_pos >= 0)
4540 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004541
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004542 s->be->counters.failed_resp++;
4543 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004544 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004545
4546 if (!(s->flags & SN_ERR_MASK))
4547 s->flags |= SN_ERR_CLICL;
4548 if (!(s->flags & SN_FINST_MASK))
4549 s->flags |= SN_FINST_H;
4550
4551 /* process_session() will take care of the error */
4552 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004553 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004554
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004555 buffer_dont_close(rep);
4556 return 0;
4557 }
4558
4559 /* More interesting part now : we know that we have a complete
4560 * response which at least looks like HTTP. We have an indicator
4561 * of each header's length, so we can parse them quickly.
4562 */
4563
4564 if (unlikely(msg->err_pos >= 0))
4565 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4566
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004567 /*
4568 * 1: get the status code
4569 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004570 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004571 if (n < 1 || n > 5)
4572 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004573 /* when the client triggers a 4xx from the server, it's most often due
4574 * to a missing object or permission. These events should be tracked
4575 * because if they happen often, it may indicate a brute force or a
4576 * vulnerability scan.
4577 */
4578 if (n == 4)
4579 session_inc_http_err_ctr(s);
4580
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004581 if (s->srv)
4582 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004583
Willy Tarreau5b154472009-12-21 20:11:07 +01004584 /* check if the response is HTTP/1.1 or above */
4585 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004586 ((msg->sol[5] > '1') ||
4587 ((msg->sol[5] == '1') &&
4588 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004589 txn->flags |= TX_RES_VER_11;
4590
4591 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004592 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 +01004593
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004594 /* transfer length unknown*/
4595 txn->flags &= ~TX_RES_XFER_LEN;
4596
Willy Tarreau962c3f42010-01-10 00:15:35 +01004597 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004598
Willy Tarreau39650402010-03-15 19:44:39 +01004599 /* Adjust server's health based on status code. Note: status codes 501
4600 * and 505 are triggered on demand by client request, so we must not
4601 * count them as server failures.
4602 */
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004603 if (s->srv) {
4604 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
4605 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4606 else
4607 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4608 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004609
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004610 /*
4611 * 2: check for cacheability.
4612 */
4613
4614 switch (txn->status) {
4615 case 200:
4616 case 203:
4617 case 206:
4618 case 300:
4619 case 301:
4620 case 410:
4621 /* RFC2616 @13.4:
4622 * "A response received with a status code of
4623 * 200, 203, 206, 300, 301 or 410 MAY be stored
4624 * by a cache (...) unless a cache-control
4625 * directive prohibits caching."
4626 *
4627 * RFC2616 @9.5: POST method :
4628 * "Responses to this method are not cacheable,
4629 * unless the response includes appropriate
4630 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004631 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004632 if (likely(txn->meth != HTTP_METH_POST) &&
4633 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4634 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4635 break;
4636 default:
4637 break;
4638 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004639
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004640 /*
4641 * 3: we may need to capture headers
4642 */
4643 s->logs.logwait &= ~LW_RESP;
4644 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004645 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004646 txn->rsp.cap, s->fe->rsp_cap);
4647
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004648 /* 4: determine the transfer-length.
4649 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4650 * the presence of a message-body in a RESPONSE and its transfer length
4651 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004652 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004653 * All responses to the HEAD request method MUST NOT include a
4654 * message-body, even though the presence of entity-header fields
4655 * might lead one to believe they do. All 1xx (informational), 204
4656 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4657 * message-body. All other responses do include a message-body,
4658 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004659 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004660 * 1. Any response which "MUST NOT" include a message-body (such as the
4661 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4662 * always terminated by the first empty line after the header fields,
4663 * regardless of the entity-header fields present in the message.
4664 *
4665 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4666 * the "chunked" transfer-coding (Section 6.2) is used, the
4667 * transfer-length is defined by the use of this transfer-coding.
4668 * If a Transfer-Encoding header field is present and the "chunked"
4669 * transfer-coding is not present, the transfer-length is defined by
4670 * the sender closing the connection.
4671 *
4672 * 3. If a Content-Length header field is present, its decimal value in
4673 * OCTETs represents both the entity-length and the transfer-length.
4674 * If a message is received with both a Transfer-Encoding header
4675 * field and a Content-Length header field, the latter MUST be ignored.
4676 *
4677 * 4. If the message uses the media type "multipart/byteranges", and
4678 * the transfer-length is not otherwise specified, then this self-
4679 * delimiting media type defines the transfer-length. This media
4680 * type MUST NOT be used unless the sender knows that the recipient
4681 * can parse it; the presence in a request of a Range header with
4682 * multiple byte-range specifiers from a 1.1 client implies that the
4683 * client can parse multipart/byteranges responses.
4684 *
4685 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004686 */
4687
4688 /* Skip parsing if no content length is possible. The response flags
4689 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004690 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004691 * FIXME: should we parse anyway and return an error on chunked encoding ?
4692 */
4693 if (txn->meth == HTTP_METH_HEAD ||
4694 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004695 txn->status == 204 || txn->status == 304) {
4696 txn->flags |= TX_RES_XFER_LEN;
4697 goto skip_content_length;
4698 }
4699
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004700 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004701 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004702 while ((txn->flags & TX_RES_VER_11) &&
4703 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004704 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4705 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4706 else if (txn->flags & TX_RES_TE_CHNK) {
4707 /* bad transfer-encoding (chunked followed by something else) */
4708 use_close_only = 1;
4709 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4710 break;
4711 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004712 }
4713
4714 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4715 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004716 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004717 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4718 signed long long cl;
4719
4720 if (!ctx.vlen)
4721 goto hdr_response_bad;
4722
4723 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4724 goto hdr_response_bad; /* parse failure */
4725
4726 if (cl < 0)
4727 goto hdr_response_bad;
4728
4729 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4730 goto hdr_response_bad; /* already specified, was different */
4731
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004732 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004733 msg->hdr_content_len = cl;
4734 }
4735
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004736 /* FIXME: we should also implement the multipart/byterange method.
4737 * For now on, we resort to close mode in this case (unknown length).
4738 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004739skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004740
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004741 /* end of job, return OK */
4742 rep->analysers &= ~an_bit;
4743 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004744 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004745 return 1;
4746}
4747
4748/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004749 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4750 * and updates t->rep->analysers. It might make sense to explode it into several
4751 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004752 */
4753int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4754{
4755 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004756 struct http_msg *msg = &txn->rsp;
4757 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004758 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004759
4760 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4761 now_ms, __FUNCTION__,
4762 t,
4763 rep,
4764 rep->rex, rep->wex,
4765 rep->flags,
4766 rep->l,
4767 rep->analysers);
4768
Willy Tarreau655dce92009-11-08 13:10:58 +01004769 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004770 return 0;
4771
4772 rep->analysers &= ~an_bit;
4773 rep->analyse_exp = TICK_ETERNITY;
4774
Willy Tarreau5b154472009-12-21 20:11:07 +01004775 /* Now we have to check if we need to modify the Connection header.
4776 * This is more difficult on the response than it is on the request,
4777 * because we can have two different HTTP versions and we don't know
4778 * how the client will interprete a response. For instance, let's say
4779 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4780 * HTTP/1.1 response without any header. Maybe it will bound itself to
4781 * HTTP/1.0 because it only knows about it, and will consider the lack
4782 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4783 * the lack of header as a keep-alive. Thus we will use two flags
4784 * indicating how a request MAY be understood by the client. In case
4785 * of multiple possibilities, we'll fix the header to be explicit. If
4786 * ambiguous cases such as both close and keepalive are seen, then we
4787 * will fall back to explicit close. Note that we won't take risks with
4788 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004789 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004790 */
4791
Willy Tarreaudc008c52010-02-01 16:20:08 +01004792 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4793 txn->status == 101)) {
4794 /* Either we've established an explicit tunnel, or we're
4795 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004796 * to understand the next protocols. We have to switch to tunnel
4797 * mode, so that we transfer the request and responses then let
4798 * this protocol pass unmodified. When we later implement specific
4799 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004800 * header which contains information about that protocol for
4801 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004802 */
4803 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4804 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004805 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4806 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4807 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004808 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004809
Willy Tarreau60466522010-01-18 19:08:45 +01004810 /* on unknown transfer length, we must close */
4811 if (!(txn->flags & TX_RES_XFER_LEN) &&
4812 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4813 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004814
Willy Tarreau60466522010-01-18 19:08:45 +01004815 /* now adjust header transformations depending on current state */
4816 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4817 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4818 to_del |= 2; /* remove "keep-alive" on any response */
4819 if (!(txn->flags & TX_RES_VER_11))
4820 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004821 }
Willy Tarreau60466522010-01-18 19:08:45 +01004822 else { /* SCL / KAL */
4823 to_del |= 1; /* remove "close" on any response */
4824 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4825 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004826 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004827
Willy Tarreau60466522010-01-18 19:08:45 +01004828 /* Parse and remove some headers from the connection header */
4829 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004830
Willy Tarreau60466522010-01-18 19:08:45 +01004831 /* Some keep-alive responses are converted to Server-close if
4832 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004833 */
Willy Tarreau60466522010-01-18 19:08:45 +01004834 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4835 if ((txn->flags & TX_HDR_CONN_CLO) ||
4836 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4837 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004838 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004839 }
4840
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004841 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004842 /*
4843 * 3: we will have to evaluate the filters.
4844 * As opposed to version 1.2, now they will be evaluated in the
4845 * filters order and not in the header order. This means that
4846 * each filter has to be validated among all headers.
4847 *
4848 * Filters are tried with ->be first, then with ->fe if it is
4849 * different from ->be.
4850 */
4851
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004852 cur_proxy = t->be;
4853 while (1) {
4854 struct proxy *rule_set = cur_proxy;
4855
4856 /* try headers filters */
4857 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004858 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004859 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004860 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004861 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004862 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4863 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004864 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004865 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004866 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004867 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004868 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004869 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004870 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004871 if (!(t->flags & SN_ERR_MASK))
4872 t->flags |= SN_ERR_PRXCOND;
4873 if (!(t->flags & SN_FINST_MASK))
4874 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004875 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004876 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004877 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004878
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004879 /* has the response been denied ? */
4880 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004881 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004882 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004883
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004884 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004885 if (t->listener->counters)
4886 t->listener->counters->denied_resp++;
4887
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004888 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004889 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004890
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004891 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004892 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004893 if (txn->status < 200)
4894 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004895 if (wl->cond) {
4896 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
4897 ret = acl_pass(ret);
4898 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4899 ret = !ret;
4900 if (!ret)
4901 continue;
4902 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004903 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004904 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004905 }
4906
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004907 /* check whether we're already working on the frontend */
4908 if (cur_proxy == t->fe)
4909 break;
4910 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004911 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004912
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004913 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004914 * We may be facing a 100-continue response, in which case this
4915 * is not the right response, and we're waiting for the next one.
4916 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004917 * next one.
4918 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004919 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004920 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004921 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004922 msg->msg_state = HTTP_MSG_RPBEFORE;
4923 txn->status = 0;
4924 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4925 return 1;
4926 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004927 else if (unlikely(txn->status < 200))
4928 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004929
4930 /* we don't have any 1xx status code now */
4931
4932 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004933 * 4: check for server cookie.
4934 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004935 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4936 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004937 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004938
Willy Tarreaubaaee002006-06-26 02:48:02 +02004939
Willy Tarreaua15645d2007-03-18 16:22:39 +01004940 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004941 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004942 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004943 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004944 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004945
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004946 /*
4947 * 6: add server cookie in the response if needed
4948 */
4949 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004950 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
4951 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004952 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004953
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004954 /* the server is known, it's not the one the client requested, we have to
4955 * insert a set-cookie here, except if we want to insert only on POST
4956 * requests and this one isn't. Note that servers which don't have cookies
4957 * (eg: some backup servers) will return a full cookie removal request.
4958 */
4959 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4960 t->be->cookie_name,
4961 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004962
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004963 if (t->be->cookie_domain)
4964 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004965
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004966 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004967 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004968 goto return_bad_resp;
4969 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004970
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004971 /* Here, we will tell an eventual cache on the client side that we don't
4972 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4973 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4974 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4975 */
4976 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004977
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004978 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4979
4980 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004981 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004982 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004983 }
4984 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004985
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004986 /*
4987 * 7: check if result will be cacheable with a cookie.
4988 * We'll block the response if security checks have caught
4989 * nasty things such as a cacheable cookie.
4990 */
4991 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4992 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004993 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004994
4995 /* we're in presence of a cacheable response containing
4996 * a set-cookie header. We'll block it as requested by
4997 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004998 */
Willy Tarreau8365f932009-03-15 23:11:49 +01004999 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005000 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005001
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005002 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005003 if (t->listener->counters)
5004 t->listener->counters->denied_resp++;
5005
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005006 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5007 t->be->id, t->srv?t->srv->id:"<dispatch>");
5008 send_log(t->be, LOG_ALERT,
5009 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5010 t->be->id, t->srv?t->srv->id:"<dispatch>");
5011 goto return_srv_prx_502;
5012 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005013
5014 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005015 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005016 */
Willy Tarreau60466522010-01-18 19:08:45 +01005017 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5018 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5019 unsigned int want_flags = 0;
5020
5021 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5022 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5023 /* we want a keep-alive response here. Keep-alive header
5024 * required if either side is not 1.1.
5025 */
5026 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5027 want_flags |= TX_CON_KAL_SET;
5028 }
5029 else {
5030 /* we want a close response here. Close header required if
5031 * the server is 1.1, regardless of the client.
5032 */
5033 if (txn->flags & TX_RES_VER_11)
5034 want_flags |= TX_CON_CLO_SET;
5035 }
5036
5037 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5038 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005039 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005040
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005041 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005042 if ((txn->flags & TX_RES_XFER_LEN) ||
5043 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005044 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005045
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005046 /*************************************************************
5047 * OK, that's finished for the headers. We have done what we *
5048 * could. Let's switch to the DATA state. *
5049 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005050
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005051 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005052
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005053 /* if the user wants to log as soon as possible, without counting
5054 * bytes from the server, then this is the right moment. We have
5055 * to temporarily assign bytes_out to log what we currently have.
5056 */
5057 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5058 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5059 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005060 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005061 t->logs.bytes_out = 0;
5062 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005063
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005064 /* Note: we must not try to cheat by jumping directly to DATA,
5065 * otherwise we would not let the client side wake up.
5066 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005067
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005068 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005069 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005070 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005071}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005072
Willy Tarreaud98cf932009-12-27 22:54:55 +01005073/* This function is an analyser which forwards response body (including chunk
5074 * sizes if any). It is called as soon as we must forward, even if we forward
5075 * zero byte. The only situation where it must not be called is when we're in
5076 * tunnel mode and we want to forward till the close. It's used both to forward
5077 * remaining data and to resync after end of body. It expects the msg_state to
5078 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5079 * read more data, or 1 once we can go on with next request or end the session.
5080 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
5081 * bytes of pending data + the headers if not already done (between som and sov).
5082 * It eventually adjusts som to match sov after the data in between have been sent.
5083 */
5084int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5085{
5086 struct http_txn *txn = &s->txn;
5087 struct http_msg *msg = &s->txn.rsp;
5088
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005089 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5090 return 0;
5091
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005092 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005093 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005094 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005095 /* Output closed while we were sending data. We must abort and
5096 * wake the other side up.
5097 */
5098 msg->msg_state = HTTP_MSG_ERROR;
5099 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005100 return 1;
5101 }
5102
Willy Tarreau4fe41902010-06-07 22:27:41 +02005103 /* in most states, we should abort in case of early close */
5104 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005105
Willy Tarreaud98cf932009-12-27 22:54:55 +01005106 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5107 /* we have msg->col and msg->sov which both point to the first
5108 * byte of message body. msg->som still points to the beginning
5109 * of the message. We must save the body in req->lr because it
5110 * survives buffer re-alignments.
5111 */
5112 res->lr = res->data + msg->sov;
5113 if (txn->flags & TX_RES_TE_CHNK)
5114 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5115 else {
5116 msg->msg_state = HTTP_MSG_DATA;
5117 }
5118 }
5119
Willy Tarreaud98cf932009-12-27 22:54:55 +01005120 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005121 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005122 /* we may have some data pending */
5123 if (msg->hdr_content_len || msg->som != msg->sov) {
5124 int bytes = msg->sov - msg->som;
5125 if (bytes < 0) /* sov may have wrapped at the end */
5126 bytes += res->size;
5127 buffer_forward(res, bytes + msg->hdr_content_len);
5128 msg->hdr_content_len = 0; /* don't forward that again */
5129 msg->som = msg->sov;
5130 }
5131
Willy Tarreaucaabe412010-01-03 23:08:28 +01005132 if (msg->msg_state == HTTP_MSG_DATA) {
5133 /* must still forward */
5134 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005135 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005136
5137 /* nothing left to forward */
5138 if (txn->flags & TX_RES_TE_CHNK)
5139 msg->msg_state = HTTP_MSG_DATA_CRLF;
5140 else
5141 msg->msg_state = HTTP_MSG_DONE;
5142 }
5143 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005144 /* read the chunk size and assign it to ->hdr_content_len, then
5145 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5146 */
5147 int ret = http_parse_chunk_size(res, msg);
5148
5149 if (!ret)
5150 goto missing_data;
5151 else if (ret < 0)
5152 goto return_bad_res;
5153 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005154 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005155 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5156 /* we want the CRLF after the data */
5157 int ret;
5158
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005159 res->lr = res->w + res->send_max;
5160 if (res->lr >= res->data + res->size)
5161 res->lr -= res->size;
5162
Willy Tarreaud98cf932009-12-27 22:54:55 +01005163 ret = http_skip_chunk_crlf(res, msg);
5164
5165 if (!ret)
5166 goto missing_data;
5167 else if (ret < 0)
5168 goto return_bad_res;
5169 /* we're in MSG_CHUNK_SIZE now */
5170 }
5171 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5172 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005173
Willy Tarreaud98cf932009-12-27 22:54:55 +01005174 if (ret == 0)
5175 goto missing_data;
5176 else if (ret < 0)
5177 goto return_bad_res;
5178 /* we're in HTTP_MSG_DONE now */
5179 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005180 else {
5181 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005182 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005183 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5184 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5185 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005186 if (http_resync_states(s)) {
5187 http_silent_debug(__LINE__, s);
5188 /* some state changes occurred, maybe the analyser
5189 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005190 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005191 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
5192 goto return_bad_res;
5193 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005194 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005195 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005196 }
5197 }
5198
Willy Tarreaud98cf932009-12-27 22:54:55 +01005199 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005200 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005201 if (res->flags & BF_SHUTR) {
5202 if (!(s->flags & SN_ERR_MASK))
5203 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01005204 s->be->counters.srv_aborts++;
5205 if (s->srv)
5206 s->srv->counters.srv_aborts++;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005207 goto return_bad_res;
Willy Tarreau40dba092010-03-04 18:14:51 +01005208 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005209
Willy Tarreau40dba092010-03-04 18:14:51 +01005210 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005211 if (!s->req->analysers)
5212 goto return_bad_res;
5213
Willy Tarreaud98cf932009-12-27 22:54:55 +01005214 /* forward the chunk size as well as any pending data */
5215 if (msg->hdr_content_len || msg->som != msg->sov) {
5216 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
5217 msg->hdr_content_len = 0; /* don't forward that again */
5218 msg->som = msg->sov;
5219 }
5220
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005221 /* When TE: chunked is used, we need to get there again to parse remaining
5222 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5223 * Similarly, with keep-alive on the client side, we don't want to forward a
5224 * close.
5225 */
5226 if ((txn->flags & TX_RES_TE_CHNK) ||
5227 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5228 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5229 buffer_dont_close(res);
5230
Willy Tarreaud98cf932009-12-27 22:54:55 +01005231 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005232 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005233 return 0;
5234
Willy Tarreau40dba092010-03-04 18:14:51 +01005235 return_bad_res: /* let's centralize all bad responses */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005236 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005237 /* don't send any error message as we're in the body */
5238 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005239 res->analysers = 0;
5240 s->be->counters.failed_resp++;
5241 if (s->srv) {
5242 s->srv->counters.failed_resp++;
5243 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
5244 }
5245
5246 if (!(s->flags & SN_ERR_MASK))
5247 s->flags |= SN_ERR_PRXCOND;
5248 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005249 s->flags |= SN_FINST_D;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005250 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005251 return 0;
5252}
5253
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005254/* Iterate the same filter through all request headers.
5255 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005256 * Since it can manage the switch to another backend, it updates the per-proxy
5257 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005258 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005259int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005260{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005261 char term;
5262 char *cur_ptr, *cur_end, *cur_next;
5263 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005264 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005265 struct hdr_idx_elem *cur_hdr;
5266 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005267
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005268 last_hdr = 0;
5269
Willy Tarreau962c3f42010-01-10 00:15:35 +01005270 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005271 old_idx = 0;
5272
5273 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005274 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005275 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005276 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005277 (exp->action == ACT_ALLOW ||
5278 exp->action == ACT_DENY ||
5279 exp->action == ACT_TARPIT))
5280 return 0;
5281
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005282 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005283 if (!cur_idx)
5284 break;
5285
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005286 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005287 cur_ptr = cur_next;
5288 cur_end = cur_ptr + cur_hdr->len;
5289 cur_next = cur_end + cur_hdr->cr + 1;
5290
5291 /* Now we have one header between cur_ptr and cur_end,
5292 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005293 */
5294
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005295 /* The annoying part is that pattern matching needs
5296 * that we modify the contents to null-terminate all
5297 * strings before testing them.
5298 */
5299
5300 term = *cur_end;
5301 *cur_end = '\0';
5302
5303 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5304 switch (exp->action) {
5305 case ACT_SETBE:
5306 /* It is not possible to jump a second time.
5307 * FIXME: should we return an HTTP/500 here so that
5308 * the admin knows there's a problem ?
5309 */
5310 if (t->be != t->fe)
5311 break;
5312
5313 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005314 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005315 last_hdr = 1;
5316 break;
5317
5318 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005319 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005320 last_hdr = 1;
5321 break;
5322
5323 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005324 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005325 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005326
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005327 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005328 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005329 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005330
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005331 break;
5332
5333 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005334 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005335 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005336
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005337 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005338 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005339 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005340
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005341 break;
5342
5343 case ACT_REPLACE:
5344 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5345 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5346 /* FIXME: if the user adds a newline in the replacement, the
5347 * index will not be recalculated for now, and the new line
5348 * will not be counted as a new header.
5349 */
5350
5351 cur_end += delta;
5352 cur_next += delta;
5353 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005354 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005355 break;
5356
5357 case ACT_REMOVE:
5358 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5359 cur_next += delta;
5360
Willy Tarreaufa355d42009-11-29 18:12:29 +01005361 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005362 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5363 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005364 cur_hdr->len = 0;
5365 cur_end = NULL; /* null-term has been rewritten */
5366 break;
5367
5368 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005369 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005370 if (cur_end)
5371 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005372
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005373 /* keep the link from this header to next one in case of later
5374 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005375 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005376 old_idx = cur_idx;
5377 }
5378 return 0;
5379}
5380
5381
5382/* Apply the filter to the request line.
5383 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5384 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005385 * Since it can manage the switch to another backend, it updates the per-proxy
5386 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005387 */
5388int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5389{
5390 char term;
5391 char *cur_ptr, *cur_end;
5392 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005393 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005394 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005395
Willy Tarreau58f10d72006-12-04 02:26:12 +01005396
Willy Tarreau3d300592007-03-18 18:34:41 +01005397 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005398 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005399 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005400 (exp->action == ACT_ALLOW ||
5401 exp->action == ACT_DENY ||
5402 exp->action == ACT_TARPIT))
5403 return 0;
5404 else if (exp->action == ACT_REMOVE)
5405 return 0;
5406
5407 done = 0;
5408
Willy Tarreau962c3f42010-01-10 00:15:35 +01005409 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005410 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005411
5412 /* Now we have the request line between cur_ptr and cur_end */
5413
5414 /* The annoying part is that pattern matching needs
5415 * that we modify the contents to null-terminate all
5416 * strings before testing them.
5417 */
5418
5419 term = *cur_end;
5420 *cur_end = '\0';
5421
5422 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5423 switch (exp->action) {
5424 case ACT_SETBE:
5425 /* It is not possible to jump a second time.
5426 * FIXME: should we return an HTTP/500 here so that
5427 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005428 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005429 if (t->be != t->fe)
5430 break;
5431
5432 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005433 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005434 done = 1;
5435 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005437 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005438 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005439 done = 1;
5440 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005441
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005442 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005443 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005444
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005445 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005446 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005447 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005448
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005449 done = 1;
5450 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005451
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005452 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005453 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005454
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005455 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005456 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005457 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005458
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005459 done = 1;
5460 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005462 case ACT_REPLACE:
5463 *cur_end = term; /* restore the string terminator */
5464 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5465 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5466 /* FIXME: if the user adds a newline in the replacement, the
5467 * index will not be recalculated for now, and the new line
5468 * will not be counted as a new header.
5469 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005470
Willy Tarreaufa355d42009-11-29 18:12:29 +01005471 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005472 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005473 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005474 HTTP_MSG_RQMETH,
5475 cur_ptr, cur_end + 1,
5476 NULL, NULL);
5477 if (unlikely(!cur_end))
5478 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005479
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005480 /* we have a full request and we know that we have either a CR
5481 * or an LF at <ptr>.
5482 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005483 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5484 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005485 /* there is no point trying this regex on headers */
5486 return 1;
5487 }
5488 }
5489 *cur_end = term; /* restore the string terminator */
5490 return done;
5491}
Willy Tarreau97de6242006-12-27 17:18:38 +01005492
Willy Tarreau58f10d72006-12-04 02:26:12 +01005493
Willy Tarreau58f10d72006-12-04 02:26:12 +01005494
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005495/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005496 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005497 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005498 * unparsable request. Since it can manage the switch to another backend, it
5499 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005500 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005501int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005502{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005503 struct http_txn *txn = &s->txn;
5504 struct hdr_exp *exp;
5505
5506 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005507 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005509 /*
5510 * The interleaving of transformations and verdicts
5511 * makes it difficult to decide to continue or stop
5512 * the evaluation.
5513 */
5514
Willy Tarreau6c123b12010-01-28 20:22:06 +01005515 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5516 break;
5517
Willy Tarreau3d300592007-03-18 18:34:41 +01005518 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005519 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005520 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005521 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005522
5523 /* if this filter had a condition, evaluate it now and skip to
5524 * next filter if the condition does not match.
5525 */
5526 if (exp->cond) {
5527 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5528 ret = acl_pass(ret);
5529 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5530 ret = !ret;
5531
5532 if (!ret)
5533 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005534 }
5535
5536 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005537 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005538 if (unlikely(ret < 0))
5539 return -1;
5540
5541 if (likely(ret == 0)) {
5542 /* The filter did not match the request, it can be
5543 * iterated through all headers.
5544 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005545 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005546 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005547 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005548 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005549}
5550
5551
Willy Tarreaua15645d2007-03-18 16:22:39 +01005552
Willy Tarreau58f10d72006-12-04 02:26:12 +01005553/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005554 * Try to retrieve the server associated to the appsession.
5555 * If the server is found, it's assigned to the session.
5556 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005557void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005558 struct http_txn *txn = &t->txn;
5559 appsess *asession = NULL;
5560 char *sessid_temp = NULL;
5561
Cyril Bontéb21570a2009-11-29 20:04:48 +01005562 if (len > t->be->appsession_len) {
5563 len = t->be->appsession_len;
5564 }
5565
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005566 if (t->be->options2 & PR_O2_AS_REQL) {
5567 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005568 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005569 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005570 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005571 }
5572
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005573 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005574 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5575 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5576 return;
5577 }
5578
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005579 memcpy(txn->sessid, buf, len);
5580 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005581 }
5582
5583 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5584 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5585 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5586 return;
5587 }
5588
Cyril Bontéb21570a2009-11-29 20:04:48 +01005589 memcpy(sessid_temp, buf, len);
5590 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005591
5592 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5593 /* free previously allocated memory */
5594 pool_free2(apools.sessid, sessid_temp);
5595
5596 if (asession != NULL) {
5597 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5598 if (!(t->be->options2 & PR_O2_AS_REQL))
5599 asession->request_count++;
5600
5601 if (asession->serverid != NULL) {
5602 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005603
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005604 while (srv) {
5605 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005606 if ((srv->state & SRV_RUNNING) ||
5607 (t->be->options & PR_O_PERSIST) ||
5608 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005609 /* we found the server and it's usable */
5610 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005611 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005612 t->flags |= SN_DIRECT | SN_ASSIGNED;
5613 t->srv = srv;
5614 break;
5615 } else {
5616 txn->flags &= ~TX_CK_MASK;
5617 txn->flags |= TX_CK_DOWN;
5618 }
5619 }
5620 srv = srv->next;
5621 }
5622 }
5623 }
5624}
5625
5626/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005627 * Manage client-side cookie. It can impact performance by about 2% so it is
5628 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005629 */
5630void manage_client_side_cookies(struct session *t, struct buffer *req)
5631{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005632 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005633 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005634 char *del_colon, *del_cookie, *colon;
5635 int app_cookies;
5636
Willy Tarreau58f10d72006-12-04 02:26:12 +01005637 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005638 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005639
Willy Tarreau2a324282006-12-05 00:05:46 +01005640 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005641 * we start with the start line.
5642 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005643 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005644 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005645
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005646 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005647 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005648 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005649
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005650 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005651 cur_ptr = cur_next;
5652 cur_end = cur_ptr + cur_hdr->len;
5653 cur_next = cur_end + cur_hdr->cr + 1;
5654
5655 /* We have one full header between cur_ptr and cur_end, and the
5656 * next header starts at cur_next. We're only interested in
5657 * "Cookie:" headers.
5658 */
5659
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005660 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5661 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005662 old_idx = cur_idx;
5663 continue;
5664 }
5665
5666 /* Now look for cookies. Conforming to RFC2109, we have to support
5667 * attributes whose name begin with a '$', and associate them with
5668 * the right cookie, if we want to delete this cookie.
5669 * So there are 3 cases for each cookie read :
5670 * 1) it's a special attribute, beginning with a '$' : ignore it.
5671 * 2) it's a server id cookie that we *MAY* want to delete : save
5672 * some pointers on it (last semi-colon, beginning of cookie...)
5673 * 3) it's an application cookie : we *MAY* have to delete a previous
5674 * "special" cookie.
5675 * At the end of loop, if a "special" cookie remains, we may have to
5676 * remove it. If no application cookie persists in the header, we
5677 * *MUST* delete it
5678 */
5679
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005680 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005681
Willy Tarreau58f10d72006-12-04 02:26:12 +01005682 /* del_cookie == NULL => nothing to be deleted */
5683 del_colon = del_cookie = NULL;
5684 app_cookies = 0;
5685
5686 while (p1 < cur_end) {
5687 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005688 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005689 while (p1 < cur_end) {
5690 if (*p1 == ';' || *p1 == ',')
5691 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005692 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005693 break;
5694 p1++;
5695 }
5696
5697 if (p1 == cur_end)
5698 break;
5699
5700 /* p1 is at the beginning of the cookie name */
5701 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005702 while (p2 < cur_end && *p2 != '=') {
5703 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5704 /* oops, the cookie name was truncated, resync */
5705 p1 = p2;
5706 goto resync_name;
5707 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005708 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005709 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005710
5711 if (p2 == cur_end)
5712 break;
5713
5714 p3 = p2 + 1; /* skips the '=' sign */
5715 if (p3 == cur_end)
5716 break;
5717
Willy Tarreau305ae852010-01-03 19:45:54 +01005718 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5719 p5 = p4 = p3;
5720 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5721 if (!isspace((unsigned char)*p5))
5722 p4 = p5 + 1;
5723 p5++;
5724 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005725
5726 /* here, we have the cookie name between p1 and p2,
5727 * and its value between p3 and p4.
5728 * we can process it :
5729 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005730 * Cookie: NAME=VALUE ;
5731 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005732 * | || || +--> p4
5733 * | || |+-------> p3
5734 * | || +--------> p2
5735 * | |+------------> p1
5736 * | +-------------> colon
5737 * +--------------------> cur_ptr
5738 */
5739
5740 if (*p1 == '$') {
5741 /* skip this one */
5742 }
5743 else {
5744 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005745 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005746 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005747 (p4 - p1 >= t->fe->capture_namelen) &&
5748 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005749 int log_len = p4 - p1;
5750
Willy Tarreau086b3b42007-05-13 21:45:51 +02005751 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005752 Alert("HTTP logging : out of memory.\n");
5753 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005754 if (log_len > t->fe->capture_len)
5755 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005756 memcpy(txn->cli_cookie, p1, log_len);
5757 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005758 }
5759 }
5760
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005761 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5762 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005763 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005764 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005765 char *delim;
5766
5767 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5768 * have the server ID betweek p3 and delim, and the original cookie between
5769 * delim+1 and p4. Otherwise, delim==p4 :
5770 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005771 * Cookie: NAME=SRV~VALUE ;
5772 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005773 * | || || | +--> p4
5774 * | || || +--------> delim
5775 * | || |+-----------> p3
5776 * | || +------------> p2
5777 * | |+----------------> p1
5778 * | +-----------------> colon
5779 * +------------------------> cur_ptr
5780 */
5781
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005782 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005783 for (delim = p3; delim < p4; delim++)
5784 if (*delim == COOKIE_DELIM)
5785 break;
5786 }
5787 else
5788 delim = p4;
5789
5790
5791 /* Here, we'll look for the first running server which supports the cookie.
5792 * This allows to share a same cookie between several servers, for example
5793 * to dedicate backup servers to specific servers only.
5794 * However, to prevent clients from sticking to cookie-less backup server
5795 * when they have incidentely learned an empty cookie, we simply ignore
5796 * empty cookies and mark them as invalid.
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005797 * The same behaviour is applied when persistence must be ignored.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005798 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005799 if ((delim == p3) || (t->flags & SN_IGNORE_PRST))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005800 srv = NULL;
5801
5802 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005803 if (srv->cookie && (srv->cklen == delim - p3) &&
5804 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005805 if ((srv->state & SRV_RUNNING) ||
5806 (t->be->options & PR_O_PERSIST) ||
5807 (t->flags & SN_FORCE_PRST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005808 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005809 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005810 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreau3d300592007-03-18 18:34:41 +01005811 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005812 t->srv = srv;
5813 break;
5814 } else {
5815 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005816 txn->flags &= ~TX_CK_MASK;
5817 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005818 }
5819 }
5820 srv = srv->next;
5821 }
5822
Willy Tarreau3d300592007-03-18 18:34:41 +01005823 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005824 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005825 txn->flags &= ~TX_CK_MASK;
5826 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005827 }
5828
5829 /* depending on the cookie mode, we may have to either :
5830 * - delete the complete cookie if we're in insert+indirect mode, so that
5831 * the server never sees it ;
5832 * - remove the server id from the cookie value, and tag the cookie as an
5833 * application cookie so that it does not get accidentely removed later,
5834 * if we're in cookie prefix mode
5835 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005836 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005837 int delta; /* negative */
5838
5839 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5840 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005841 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005842 cur_end += delta;
5843 cur_next += delta;
5844 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005845 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005846
5847 del_cookie = del_colon = NULL;
5848 app_cookies++; /* protect the header from deletion */
5849 }
5850 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005851 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005852 del_cookie = p1;
5853 del_colon = colon;
5854 }
5855 } else {
5856 /* now we know that we must keep this cookie since it's
5857 * not ours. But if we wanted to delete our cookie
5858 * earlier, we cannot remove the complete header, but we
5859 * can remove the previous block itself.
5860 */
5861 app_cookies++;
5862
5863 if (del_cookie != NULL) {
5864 int delta; /* negative */
5865
5866 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5867 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005868 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005869 cur_end += delta;
5870 cur_next += delta;
5871 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005872 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005873 del_cookie = del_colon = NULL;
5874 }
5875 }
5876
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005877 /* Look for the appsession cookie unless persistence must be ignored */
5878 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01005879 int cmp_len, value_len;
5880 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005881
Cyril Bontéb21570a2009-11-29 20:04:48 +01005882 if (t->be->options2 & PR_O2_AS_PFX) {
5883 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5884 value_begin = p1 + t->be->appsession_name_len;
5885 value_len = p4 - p1 - t->be->appsession_name_len;
5886 } else {
5887 cmp_len = p2 - p1;
5888 value_begin = p3;
5889 value_len = p4 - p3;
5890 }
5891
5892 /* let's see if the cookie is our appcookie */
Cyril Bonté17530c32010-04-06 21:11:10 +02005893 if ((cmp_len == t->be->appsession_name_len) &&
5894 (memcmp(p1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01005895 /* Cool... it's the right one */
5896 manage_client_side_appsession(t, value_begin, value_len);
5897 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005898#if defined(DEBUG_HASH)
5899 Alert("manage_client_side_cookies\n");
5900 appsession_hash_dump(&(t->be->htbl_proxy));
5901#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005902 }/* end if ((t->proxy->appsession_name != NULL) ... */
5903 }
5904
5905 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005906 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005907 } /* while (p1 < cur_end) */
5908
5909 /* There's no more cookie on this line.
5910 * We may have marked the last one(s) for deletion.
5911 * We must do this now in two ways :
5912 * - if there is no app cookie, we simply delete the header ;
5913 * - if there are app cookies, we must delete the end of the
5914 * string properly, including the colon/semi-colon before
5915 * the cookie name.
5916 */
5917 if (del_cookie != NULL) {
5918 int delta;
5919 if (app_cookies) {
5920 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5921 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005922 cur_hdr->len += delta;
5923 } else {
5924 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005925
5926 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005927 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5928 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005929 cur_hdr->len = 0;
5930 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005931 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005932 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005933 }
5934
5935 /* keep the link from this header to next one */
5936 old_idx = cur_idx;
5937 } /* end of cookie processing on this header */
5938}
5939
5940
Willy Tarreaua15645d2007-03-18 16:22:39 +01005941/* Iterate the same filter through all response headers contained in <rtr>.
5942 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5943 */
5944int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5945{
5946 char term;
5947 char *cur_ptr, *cur_end, *cur_next;
5948 int cur_idx, old_idx, last_hdr;
5949 struct http_txn *txn = &t->txn;
5950 struct hdr_idx_elem *cur_hdr;
5951 int len, delta;
5952
5953 last_hdr = 0;
5954
Willy Tarreau962c3f42010-01-10 00:15:35 +01005955 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005956 old_idx = 0;
5957
5958 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005959 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005960 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005961 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005962 (exp->action == ACT_ALLOW ||
5963 exp->action == ACT_DENY))
5964 return 0;
5965
5966 cur_idx = txn->hdr_idx.v[old_idx].next;
5967 if (!cur_idx)
5968 break;
5969
5970 cur_hdr = &txn->hdr_idx.v[cur_idx];
5971 cur_ptr = cur_next;
5972 cur_end = cur_ptr + cur_hdr->len;
5973 cur_next = cur_end + cur_hdr->cr + 1;
5974
5975 /* Now we have one header between cur_ptr and cur_end,
5976 * and the next header starts at cur_next.
5977 */
5978
5979 /* The annoying part is that pattern matching needs
5980 * that we modify the contents to null-terminate all
5981 * strings before testing them.
5982 */
5983
5984 term = *cur_end;
5985 *cur_end = '\0';
5986
5987 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5988 switch (exp->action) {
5989 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005990 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005991 last_hdr = 1;
5992 break;
5993
5994 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005995 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005996 last_hdr = 1;
5997 break;
5998
5999 case ACT_REPLACE:
6000 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6001 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6002 /* FIXME: if the user adds a newline in the replacement, the
6003 * index will not be recalculated for now, and the new line
6004 * will not be counted as a new header.
6005 */
6006
6007 cur_end += delta;
6008 cur_next += delta;
6009 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006010 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006011 break;
6012
6013 case ACT_REMOVE:
6014 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6015 cur_next += delta;
6016
Willy Tarreaufa355d42009-11-29 18:12:29 +01006017 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006018 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6019 txn->hdr_idx.used--;
6020 cur_hdr->len = 0;
6021 cur_end = NULL; /* null-term has been rewritten */
6022 break;
6023
6024 }
6025 }
6026 if (cur_end)
6027 *cur_end = term; /* restore the string terminator */
6028
6029 /* keep the link from this header to next one in case of later
6030 * removal of next header.
6031 */
6032 old_idx = cur_idx;
6033 }
6034 return 0;
6035}
6036
6037
6038/* Apply the filter to the status line in the response buffer <rtr>.
6039 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6040 * or -1 if a replacement resulted in an invalid status line.
6041 */
6042int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6043{
6044 char term;
6045 char *cur_ptr, *cur_end;
6046 int done;
6047 struct http_txn *txn = &t->txn;
6048 int len, delta;
6049
6050
Willy Tarreau3d300592007-03-18 18:34:41 +01006051 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006052 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006053 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006054 (exp->action == ACT_ALLOW ||
6055 exp->action == ACT_DENY))
6056 return 0;
6057 else if (exp->action == ACT_REMOVE)
6058 return 0;
6059
6060 done = 0;
6061
Willy Tarreau962c3f42010-01-10 00:15:35 +01006062 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006063 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006064
6065 /* Now we have the status line between cur_ptr and cur_end */
6066
6067 /* The annoying part is that pattern matching needs
6068 * that we modify the contents to null-terminate all
6069 * strings before testing them.
6070 */
6071
6072 term = *cur_end;
6073 *cur_end = '\0';
6074
6075 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6076 switch (exp->action) {
6077 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006078 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006079 done = 1;
6080 break;
6081
6082 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006083 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006084 done = 1;
6085 break;
6086
6087 case ACT_REPLACE:
6088 *cur_end = term; /* restore the string terminator */
6089 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6090 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6091 /* FIXME: if the user adds a newline in the replacement, the
6092 * index will not be recalculated for now, and the new line
6093 * will not be counted as a new header.
6094 */
6095
Willy Tarreaufa355d42009-11-29 18:12:29 +01006096 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006097 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006098 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006099 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006100 cur_ptr, cur_end + 1,
6101 NULL, NULL);
6102 if (unlikely(!cur_end))
6103 return -1;
6104
6105 /* we have a full respnse and we know that we have either a CR
6106 * or an LF at <ptr>.
6107 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006108 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006109 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006110 /* there is no point trying this regex on headers */
6111 return 1;
6112 }
6113 }
6114 *cur_end = term; /* restore the string terminator */
6115 return done;
6116}
6117
6118
6119
6120/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006121 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006122 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6123 * unparsable response.
6124 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006125int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006126{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006127 struct http_txn *txn = &s->txn;
6128 struct hdr_exp *exp;
6129
6130 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006131 int ret;
6132
6133 /*
6134 * The interleaving of transformations and verdicts
6135 * makes it difficult to decide to continue or stop
6136 * the evaluation.
6137 */
6138
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006139 if (txn->flags & TX_SVDENY)
6140 break;
6141
Willy Tarreau3d300592007-03-18 18:34:41 +01006142 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006143 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6144 exp->action == ACT_PASS)) {
6145 exp = exp->next;
6146 continue;
6147 }
6148
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006149 /* if this filter had a condition, evaluate it now and skip to
6150 * next filter if the condition does not match.
6151 */
6152 if (exp->cond) {
6153 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6154 ret = acl_pass(ret);
6155 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6156 ret = !ret;
6157 if (!ret)
6158 continue;
6159 }
6160
Willy Tarreaua15645d2007-03-18 16:22:39 +01006161 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006162 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006163 if (unlikely(ret < 0))
6164 return -1;
6165
6166 if (likely(ret == 0)) {
6167 /* The filter did not match the response, it can be
6168 * iterated through all headers.
6169 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006170 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006171 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006172 }
6173 return 0;
6174}
6175
6176
6177
6178/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006179 * Manage server-side cookies. It can impact performance by about 2% so it is
6180 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006181 */
6182void manage_server_side_cookies(struct session *t, struct buffer *rtr)
6183{
6184 struct http_txn *txn = &t->txn;
6185 char *p1, *p2, *p3, *p4;
6186
Willy Tarreaua15645d2007-03-18 16:22:39 +01006187 char *cur_ptr, *cur_end, *cur_next;
6188 int cur_idx, old_idx, delta;
6189
Willy Tarreaua15645d2007-03-18 16:22:39 +01006190 /* Iterate through the headers.
6191 * we start with the start line.
6192 */
6193 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006194 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006195
6196 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6197 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006198 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006199
6200 cur_hdr = &txn->hdr_idx.v[cur_idx];
6201 cur_ptr = cur_next;
6202 cur_end = cur_ptr + cur_hdr->len;
6203 cur_next = cur_end + cur_hdr->cr + 1;
6204
6205 /* We have one full header between cur_ptr and cur_end, and the
6206 * next header starts at cur_next. We're only interested in
6207 * "Cookie:" headers.
6208 */
6209
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006210 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
6211 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006212 old_idx = cur_idx;
6213 continue;
6214 }
6215
6216 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01006217 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006218
6219
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006220 /* maybe we only wanted to see if there was a set-cookie. Note that
6221 * the cookie capture is declared in the fronend.
6222 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006223 if (t->be->cookie_name == NULL &&
6224 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006225 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006226 return;
6227
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006228 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006229
6230 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006231 if (p1 == cur_end || *p1 == ';') /* end of cookie */
6232 break;
6233
6234 /* p1 is at the beginning of the cookie name */
6235 p2 = p1;
6236
6237 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
6238 p2++;
6239
6240 if (p2 == cur_end || *p2 == ';') /* next cookie */
6241 break;
6242
6243 p3 = p2 + 1; /* skip the '=' sign */
6244 if (p3 == cur_end)
6245 break;
6246
6247 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006248 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01006249 p4++;
6250
6251 /* here, we have the cookie name between p1 and p2,
6252 * and its value between p3 and p4.
6253 * we can process it.
6254 */
6255
6256 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006257 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006258 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006259 (p4 - p1 >= t->fe->capture_namelen) &&
6260 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006261 int log_len = p4 - p1;
6262
Willy Tarreau086b3b42007-05-13 21:45:51 +02006263 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006264 Alert("HTTP logging : out of memory.\n");
6265 }
6266
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006267 if (log_len > t->fe->capture_len)
6268 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006269 memcpy(txn->srv_cookie, p1, log_len);
6270 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006271 }
6272
6273 /* now check if we need to process it for persistence */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006274 if (!(t->flags & SN_IGNORE_PRST) && (p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006275 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006276 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01006277 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006278
6279 /* If the cookie is in insert mode on a known server, we'll delete
6280 * this occurrence because we'll insert another one later.
6281 * We'll delete it too if the "indirect" option is set and we're in
6282 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006283 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
6284 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006285 /* this header must be deleted */
6286 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6287 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6288 txn->hdr_idx.used--;
6289 cur_hdr->len = 0;
6290 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006291 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006292
Willy Tarreau3d300592007-03-18 18:34:41 +01006293 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006294 }
6295 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006296 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006297 /* replace bytes p3->p4 with the cookie name associated
6298 * with this server since we know it.
6299 */
6300 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
6301 cur_hdr->len += delta;
6302 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006303 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006304
Willy Tarreau3d300592007-03-18 18:34:41 +01006305 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006306 }
6307 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006308 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006309 /* insert the cookie name associated with this server
6310 * before existing cookie, and insert a delimitor between them..
6311 */
6312 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
6313 cur_hdr->len += delta;
6314 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006315 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006316
6317 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01006318 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006319 }
6320 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006321 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6322 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006323 int cmp_len, value_len;
6324 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006325
Cyril Bontéb21570a2009-11-29 20:04:48 +01006326 if (t->be->options2 & PR_O2_AS_PFX) {
6327 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
6328 value_begin = p1 + t->be->appsession_name_len;
6329 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
6330 } else {
6331 cmp_len = p2 - p1;
6332 value_begin = p3;
6333 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006334 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006335
Cyril Bonté17530c32010-04-06 21:11:10 +02006336 if ((cmp_len == t->be->appsession_name_len) &&
6337 (memcmp(p1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006338 /* Cool... it's the right one */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006339 if (txn->sessid != NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006340 /* free previously allocated memory as we don't need it anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006341 pool_free2(apools.sessid, txn->sessid);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006342 }
6343 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006344 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006345 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6346 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6347 return;
6348 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006349 memcpy(txn->sessid, value_begin, value_len);
6350 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006351 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006352 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006353 break; /* we don't want to loop again since there cannot be another cookie on the same line */
6354 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006355 /* keep the link from this header to next one */
6356 old_idx = cur_idx;
6357 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006358
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006359 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006360 appsess *asession = NULL;
6361 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006362 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006363 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006364 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006365 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6366 Alert("Not enough Memory process_srv():asession:calloc().\n");
6367 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6368 return;
6369 }
6370 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6371 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6372 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006373 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006374 return;
6375 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006376 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006377 asession->sessid[t->be->appsession_len] = 0;
6378
Willy Tarreau1fac7532010-01-09 19:23:06 +01006379 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006380 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
6381 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6382 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006383 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006384 return;
6385 }
6386 asession->serverid[0] = '\0';
6387 memcpy(asession->serverid, t->srv->id, server_id_len);
6388
6389 asession->request_count = 0;
6390 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6391 }
6392
6393 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6394 asession->request_count++;
6395 }
6396
6397#if defined(DEBUG_HASH)
Cyril Bonté17530c32010-04-06 21:11:10 +02006398 if (t->be->appsession_name) {
6399 Alert("manage_server_side_cookies\n");
6400 appsession_hash_dump(&(t->be->htbl_proxy));
6401 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006402#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01006403}
6404
6405
6406
6407/*
6408 * Check if response is cacheable or not. Updates t->flags.
6409 */
6410void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6411{
6412 struct http_txn *txn = &t->txn;
6413 char *p1, *p2;
6414
6415 char *cur_ptr, *cur_end, *cur_next;
6416 int cur_idx;
6417
Willy Tarreau5df51872007-11-25 16:20:08 +01006418 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006419 return;
6420
6421 /* Iterate through the headers.
6422 * we start with the start line.
6423 */
6424 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006425 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006426
6427 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6428 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006429 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006430
6431 cur_hdr = &txn->hdr_idx.v[cur_idx];
6432 cur_ptr = cur_next;
6433 cur_end = cur_ptr + cur_hdr->len;
6434 cur_next = cur_end + cur_hdr->cr + 1;
6435
6436 /* We have one full header between cur_ptr and cur_end, and the
6437 * next header starts at cur_next. We're only interested in
6438 * "Cookie:" headers.
6439 */
6440
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006441 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6442 if (val) {
6443 if ((cur_end - (cur_ptr + val) >= 8) &&
6444 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6445 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6446 return;
6447 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006448 }
6449
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006450 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6451 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006452 continue;
6453
6454 /* OK, right now we know we have a cache-control header at cur_ptr */
6455
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006456 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006457
6458 if (p1 >= cur_end) /* no more info */
6459 continue;
6460
6461 /* p1 is at the beginning of the value */
6462 p2 = p1;
6463
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006464 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006465 p2++;
6466
6467 /* we have a complete value between p1 and p2 */
6468 if (p2 < cur_end && *p2 == '=') {
6469 /* we have something of the form no-cache="set-cookie" */
6470 if ((cur_end - p1 >= 21) &&
6471 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6472 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006473 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006474 continue;
6475 }
6476
6477 /* OK, so we know that either p2 points to the end of string or to a comma */
6478 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6479 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6480 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6481 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006482 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006483 return;
6484 }
6485
6486 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006487 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006488 continue;
6489 }
6490 }
6491}
6492
6493
Willy Tarreau58f10d72006-12-04 02:26:12 +01006494/*
6495 * Try to retrieve a known appsession in the URI, then the associated server.
6496 * If the server is found, it's assigned to the session.
6497 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006498void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006499{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006500 char *end_params, *first_param, *cur_param, *next_param;
6501 char separator;
6502 int value_len;
6503
6504 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006505
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006506 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02006507 (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 +01006508 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006509 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006510
Cyril Bontéb21570a2009-11-29 20:04:48 +01006511 first_param = NULL;
6512 switch (mode) {
6513 case PR_O2_AS_M_PP:
6514 first_param = memchr(begin, ';', len);
6515 break;
6516 case PR_O2_AS_M_QS:
6517 first_param = memchr(begin, '?', len);
6518 break;
6519 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006520
Cyril Bontéb21570a2009-11-29 20:04:48 +01006521 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006522 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006523 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006524
Cyril Bontéb21570a2009-11-29 20:04:48 +01006525 switch (mode) {
6526 case PR_O2_AS_M_PP:
6527 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6528 end_params = (char *) begin + len;
6529 }
6530 separator = ';';
6531 break;
6532 case PR_O2_AS_M_QS:
6533 end_params = (char *) begin + len;
6534 separator = '&';
6535 break;
6536 default:
6537 /* unknown mode, shouldn't happen */
6538 return;
6539 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006540
Cyril Bontéb21570a2009-11-29 20:04:48 +01006541 cur_param = next_param = end_params;
6542 while (cur_param > first_param) {
6543 cur_param--;
6544 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6545 /* let's see if this is the appsession parameter */
6546 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6547 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6548 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6549 /* Cool... it's the right one */
6550 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6551 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6552 if (value_len > 0) {
6553 manage_client_side_appsession(t, cur_param, value_len);
6554 }
6555 break;
6556 }
6557 next_param = cur_param;
6558 }
6559 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006560#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006561 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006562 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006563#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006564}
6565
Willy Tarreaub2513902006-12-17 14:52:38 +01006566/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006567 * In a GET or HEAD request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006568 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01006569 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006570 * It is assumed that the request is either a HEAD or GET and that the
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006571 * t->be->uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006572 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006573 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01006574 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006575int stats_check_uri(struct session *t, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01006576{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006577 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006578 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006579 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006580
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006581 if (!uri_auth)
6582 return 0;
6583
6584 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD)
6585 return 0;
6586
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006587 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6588
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006589 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006590 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006591 return 0;
6592
Willy Tarreau962c3f42010-01-10 00:15:35 +01006593 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006594
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006595 /* the URI is in h */
6596 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006597 return 0;
6598
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006599 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006600 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006601 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006602 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006603 break;
6604 }
6605 h++;
6606 }
6607
6608 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006609 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6610 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006611 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006612 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006613 break;
6614 }
6615 h++;
6616 }
6617 }
6618
Willy Tarreau962c3f42010-01-10 00:15:35 +01006619 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6620 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006621 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006622 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006623 break;
6624 }
6625 h++;
6626 }
6627
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006628 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6629
Willy Tarreaub2513902006-12-17 14:52:38 +01006630 return 1;
6631}
6632
Willy Tarreau4076a152009-04-02 15:18:36 +02006633/*
6634 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006635 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6636 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006637 */
6638void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6639 struct buffer *buf, struct http_msg *msg,
6640 struct proxy *other_end)
6641{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006642 es->len = buf->r - (buf->data + msg->som);
6643 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006644 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006645 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006646 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006647 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006648 es->when = date; // user-visible date
6649 es->sid = s->uniq_id;
6650 es->srv = s->srv;
6651 es->oe = other_end;
6652 es->src = s->cli_addr;
6653}
Willy Tarreaub2513902006-12-17 14:52:38 +01006654
Willy Tarreaubce70882009-09-07 11:51:47 +02006655/* return the IP address pointed to by occurrence <occ> of header <hname> in
6656 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
6657 * occurrence number corresponding to this value is returned. If <occ> is
6658 * strictly negative, the occurrence number before the end corresponding to
6659 * this value is returned. If <occ> is null, any value is returned, so it is
6660 * not recommended to use it that way. Negative occurrences are limited to
6661 * a small value because it is required to keep them in memory while scanning.
6662 * IP address 0.0.0.0 is returned if no match is found.
6663 */
6664unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
6665{
6666 struct hdr_ctx ctx;
6667 unsigned int hdr_hist[MAX_HDR_HISTORY];
6668 unsigned int hist_ptr;
6669 int found = 0;
6670
6671 ctx.idx = 0;
6672 if (occ >= 0) {
6673 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
6674 occ--;
6675 if (occ <= 0) {
6676 found = 1;
6677 break;
6678 }
6679 }
6680 if (!found)
6681 return 0;
6682 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
6683 }
6684
6685 /* negative occurrence, we scan all the list then walk back */
6686 if (-occ > MAX_HDR_HISTORY)
6687 return 0;
6688
6689 hist_ptr = 0;
6690 hdr_hist[hist_ptr] = 0;
6691 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
6692 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
6693 if (hist_ptr >= MAX_HDR_HISTORY)
6694 hist_ptr = 0;
6695 found++;
6696 }
6697 if (-occ > found)
6698 return 0;
6699 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
6700 * find occurrence -occ, so we have to check [hist_ptr+occ].
6701 */
6702 hist_ptr += occ;
6703 if (hist_ptr >= MAX_HDR_HISTORY)
6704 hist_ptr -= MAX_HDR_HISTORY;
6705 return hdr_hist[hist_ptr];
6706}
6707
Willy Tarreaubaaee002006-06-26 02:48:02 +02006708/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006709 * Print a debug line with a header
6710 */
6711void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6712{
6713 int len, max;
6714 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006715 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006716 max = end - start;
6717 UBOUND(max, sizeof(trash) - len - 1);
6718 len += strlcpy2(trash + len, start, max + 1);
6719 trash[len++] = '\n';
6720 write(1, trash, len);
6721}
6722
Willy Tarreau0937bc42009-12-22 15:03:09 +01006723/*
6724 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6725 * the required fields are properly allocated and that we only need to (re)init
6726 * them. This should be used before processing any new request.
6727 */
6728void http_init_txn(struct session *s)
6729{
6730 struct http_txn *txn = &s->txn;
6731 struct proxy *fe = s->fe;
6732
6733 txn->flags = 0;
6734 txn->status = -1;
6735
6736 txn->req.sol = txn->req.eol = NULL;
6737 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6738 txn->rsp.sol = txn->rsp.eol = NULL;
6739 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6740 txn->req.hdr_content_len = 0LL;
6741 txn->rsp.hdr_content_len = 0LL;
6742 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6743 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01006744
6745 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006746
6747 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6748 if (fe->options2 & PR_O2_REQBUG_OK)
6749 txn->req.err_pos = -1; /* let buggy requests pass */
6750
Willy Tarreau46023632010-01-07 22:51:47 +01006751 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006752 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6753
Willy Tarreau46023632010-01-07 22:51:47 +01006754 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006755 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6756
6757 if (txn->hdr_idx.v)
6758 hdr_idx_init(&txn->hdr_idx);
6759}
6760
6761/* to be used at the end of a transaction */
6762void http_end_txn(struct session *s)
6763{
6764 struct http_txn *txn = &s->txn;
6765
6766 /* these ones will have been dynamically allocated */
6767 pool_free2(pool2_requri, txn->uri);
6768 pool_free2(pool2_capture, txn->cli_cookie);
6769 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006770 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01006771
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006772 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006773 txn->uri = NULL;
6774 txn->srv_cookie = NULL;
6775 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006776
6777 if (txn->req.cap) {
6778 struct cap_hdr *h;
6779 for (h = s->fe->req_cap; h; h = h->next)
6780 pool_free2(h->pool, txn->req.cap[h->index]);
6781 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6782 }
6783
6784 if (txn->rsp.cap) {
6785 struct cap_hdr *h;
6786 for (h = s->fe->rsp_cap; h; h = h->next)
6787 pool_free2(h->pool, txn->rsp.cap[h->index]);
6788 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6789 }
6790
Willy Tarreau0937bc42009-12-22 15:03:09 +01006791}
6792
6793/* to be used at the end of a transaction to prepare a new one */
6794void http_reset_txn(struct session *s)
6795{
6796 http_end_txn(s);
6797 http_init_txn(s);
6798
6799 s->be = s->fe;
6800 s->req->analysers = s->listener->analysers;
6801 s->logs.logwait = s->fe->to_log;
6802 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01006803 /* re-init store persistence */
6804 s->store_count = 0;
6805
Willy Tarreau0937bc42009-12-22 15:03:09 +01006806 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006807
6808 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6809
Willy Tarreau739cfba2010-01-25 23:11:14 +01006810 /* We must trim any excess data from the response buffer, because we
6811 * may have blocked an invalid response from a server that we don't
6812 * want to accidentely forward once we disable the analysers, nor do
6813 * we want those data to come along with next response. A typical
6814 * example of such data would be from a buggy server responding to
6815 * a HEAD with some data, or sending more than the advertised
6816 * content-length.
6817 */
6818 if (unlikely(s->rep->l > s->rep->send_max)) {
6819 s->rep->l = s->rep->send_max;
6820 s->rep->r = s->rep->w + s->rep->l;
6821 if (s->rep->r >= s->rep->data + s->rep->size)
6822 s->rep->r -= s->rep->size;
6823 }
6824
Willy Tarreau0937bc42009-12-22 15:03:09 +01006825 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02006826 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006827
Willy Tarreaud04e8582010-05-31 12:31:35 +02006828 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006829 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006830
6831 s->req->rex = TICK_ETERNITY;
6832 s->req->wex = TICK_ETERNITY;
6833 s->req->analyse_exp = TICK_ETERNITY;
6834 s->rep->rex = TICK_ETERNITY;
6835 s->rep->wex = TICK_ETERNITY;
6836 s->rep->analyse_exp = TICK_ETERNITY;
6837}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006838
Willy Tarreau8797c062007-05-07 00:55:35 +02006839/************************************************************************/
6840/* The code below is dedicated to ACL parsing and matching */
6841/************************************************************************/
6842
6843
6844
6845
6846/* 1. Check on METHOD
6847 * We use the pre-parsed method if it is known, and store its number as an
6848 * integer. If it is unknown, we use the pointer and the length.
6849 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006850static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006851{
6852 int len, meth;
6853
Willy Tarreauae8b7962007-06-09 23:10:04 +02006854 len = strlen(*text);
6855 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006856
6857 pattern->val.i = meth;
6858 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006859 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006860 if (!pattern->ptr.str)
6861 return 0;
6862 pattern->len = len;
6863 }
6864 return 1;
6865}
6866
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006867static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006868acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6869 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006870{
6871 int meth;
6872 struct http_txn *txn = l7;
6873
Willy Tarreaub6866442008-07-14 23:54:42 +02006874 if (!txn)
6875 return 0;
6876
Willy Tarreau655dce92009-11-08 13:10:58 +01006877 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006878 return 0;
6879
Willy Tarreau8797c062007-05-07 00:55:35 +02006880 meth = txn->meth;
6881 test->i = meth;
6882 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006883 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6884 /* ensure the indexes are not affected */
6885 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006886 test->len = txn->req.sl.rq.m_l;
6887 test->ptr = txn->req.sol;
6888 }
6889 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6890 return 1;
6891}
6892
6893static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6894{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006895 int icase;
6896
Willy Tarreau8797c062007-05-07 00:55:35 +02006897 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006898 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006899
6900 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006901 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006902
6903 /* Other method, we must compare the strings */
6904 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006905 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006906
6907 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6908 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6909 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006910 return ACL_PAT_FAIL;
6911 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006912}
6913
6914/* 2. Check on Request/Status Version
6915 * We simply compare strings here.
6916 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006917static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006918{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006919 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006920 if (!pattern->ptr.str)
6921 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006922 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006923 return 1;
6924}
6925
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006926static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006927acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6928 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006929{
6930 struct http_txn *txn = l7;
6931 char *ptr;
6932 int len;
6933
Willy Tarreaub6866442008-07-14 23:54:42 +02006934 if (!txn)
6935 return 0;
6936
Willy Tarreau655dce92009-11-08 13:10:58 +01006937 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006938 return 0;
6939
Willy Tarreau8797c062007-05-07 00:55:35 +02006940 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006941 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006942
6943 while ((len-- > 0) && (*ptr++ != '/'));
6944 if (len <= 0)
6945 return 0;
6946
6947 test->ptr = ptr;
6948 test->len = len;
6949
6950 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6951 return 1;
6952}
6953
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006954static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006955acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6956 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006957{
6958 struct http_txn *txn = l7;
6959 char *ptr;
6960 int len;
6961
Willy Tarreaub6866442008-07-14 23:54:42 +02006962 if (!txn)
6963 return 0;
6964
Willy Tarreau655dce92009-11-08 13:10:58 +01006965 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006966 return 0;
6967
Willy Tarreau8797c062007-05-07 00:55:35 +02006968 len = txn->rsp.sl.st.v_l;
6969 ptr = txn->rsp.sol;
6970
6971 while ((len-- > 0) && (*ptr++ != '/'));
6972 if (len <= 0)
6973 return 0;
6974
6975 test->ptr = ptr;
6976 test->len = len;
6977
6978 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6979 return 1;
6980}
6981
6982/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006983static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006984acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6985 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006986{
6987 struct http_txn *txn = l7;
6988 char *ptr;
6989 int len;
6990
Willy Tarreaub6866442008-07-14 23:54:42 +02006991 if (!txn)
6992 return 0;
6993
Willy Tarreau655dce92009-11-08 13:10:58 +01006994 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006995 return 0;
6996
Willy Tarreau8797c062007-05-07 00:55:35 +02006997 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006998 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02006999
7000 test->i = __strl2ui(ptr, len);
7001 test->flags = ACL_TEST_F_VOL_1ST;
7002 return 1;
7003}
7004
7005/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007006static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007007acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7008 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007009{
7010 struct http_txn *txn = l7;
7011
Willy Tarreaub6866442008-07-14 23:54:42 +02007012 if (!txn)
7013 return 0;
7014
Willy Tarreau655dce92009-11-08 13:10:58 +01007015 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007016 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007017
Willy Tarreauc11416f2007-06-17 16:58:38 +02007018 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7019 /* ensure the indexes are not affected */
7020 return 0;
7021
Willy Tarreau8797c062007-05-07 00:55:35 +02007022 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007023 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007024
Willy Tarreauf3d25982007-05-08 22:45:09 +02007025 /* we do not need to set READ_ONLY because the data is in a buffer */
7026 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007027 return 1;
7028}
7029
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007030static int
7031acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7032 struct acl_expr *expr, struct acl_test *test)
7033{
7034 struct http_txn *txn = l7;
7035
Willy Tarreaub6866442008-07-14 23:54:42 +02007036 if (!txn)
7037 return 0;
7038
Willy Tarreau655dce92009-11-08 13:10:58 +01007039 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007040 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007041
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007042 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7043 /* ensure the indexes are not affected */
7044 return 0;
7045
7046 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007047 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007048 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
7049 test->i = AF_INET;
7050
7051 /*
7052 * If we are parsing url in frontend space, we prepare backend stage
7053 * to not parse again the same url ! optimization lazyness...
7054 */
7055 if (px->options & PR_O_HTTP_PROXY)
7056 l4->flags |= SN_ADDR_SET;
7057
7058 test->flags = ACL_TEST_F_READ_ONLY;
7059 return 1;
7060}
7061
7062static int
7063acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7064 struct acl_expr *expr, struct acl_test *test)
7065{
7066 struct http_txn *txn = l7;
7067
Willy Tarreaub6866442008-07-14 23:54:42 +02007068 if (!txn)
7069 return 0;
7070
Willy Tarreau655dce92009-11-08 13:10:58 +01007071 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007072 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007073
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007074 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7075 /* ensure the indexes are not affected */
7076 return 0;
7077
7078 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007079 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007080 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
7081
7082 if (px->options & PR_O_HTTP_PROXY)
7083 l4->flags |= SN_ADDR_SET;
7084
7085 test->flags = ACL_TEST_F_READ_ONLY;
7086 return 1;
7087}
7088
Willy Tarreauc11416f2007-06-17 16:58:38 +02007089/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7090 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7091 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007092static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007093acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007094 struct acl_expr *expr, struct acl_test *test)
7095{
7096 struct http_txn *txn = l7;
7097 struct hdr_idx *idx = &txn->hdr_idx;
7098 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007099
Willy Tarreaub6866442008-07-14 23:54:42 +02007100 if (!txn)
7101 return 0;
7102
Willy Tarreau33a7e692007-06-10 19:45:56 +02007103 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7104 /* search for header from the beginning */
7105 ctx->idx = 0;
7106
Willy Tarreau33a7e692007-06-10 19:45:56 +02007107 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7108 test->flags |= ACL_TEST_F_FETCH_MORE;
7109 test->flags |= ACL_TEST_F_VOL_HDR;
7110 test->len = ctx->vlen;
7111 test->ptr = (char *)ctx->line + ctx->val;
7112 return 1;
7113 }
7114
7115 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7116 test->flags |= ACL_TEST_F_VOL_HDR;
7117 return 0;
7118}
7119
Willy Tarreau33a7e692007-06-10 19:45:56 +02007120static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007121acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7122 struct acl_expr *expr, struct acl_test *test)
7123{
7124 struct http_txn *txn = l7;
7125
Willy Tarreaub6866442008-07-14 23:54:42 +02007126 if (!txn)
7127 return 0;
7128
Willy Tarreau655dce92009-11-08 13:10:58 +01007129 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007130 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007131
Willy Tarreauc11416f2007-06-17 16:58:38 +02007132 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7133 /* ensure the indexes are not affected */
7134 return 0;
7135
7136 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7137}
7138
7139static int
7140acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7141 struct acl_expr *expr, struct acl_test *test)
7142{
7143 struct http_txn *txn = l7;
7144
Willy Tarreaub6866442008-07-14 23:54:42 +02007145 if (!txn)
7146 return 0;
7147
Willy Tarreau655dce92009-11-08 13:10:58 +01007148 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007149 return 0;
7150
7151 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7152}
7153
7154/* 6. Check on HTTP header count. The number of occurrences is returned.
7155 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7156 */
7157static int
7158acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007159 struct acl_expr *expr, struct acl_test *test)
7160{
7161 struct http_txn *txn = l7;
7162 struct hdr_idx *idx = &txn->hdr_idx;
7163 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007164 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007165
Willy Tarreaub6866442008-07-14 23:54:42 +02007166 if (!txn)
7167 return 0;
7168
Willy Tarreau33a7e692007-06-10 19:45:56 +02007169 ctx.idx = 0;
7170 cnt = 0;
7171 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7172 cnt++;
7173
7174 test->i = cnt;
7175 test->flags = ACL_TEST_F_VOL_HDR;
7176 return 1;
7177}
7178
Willy Tarreauc11416f2007-06-17 16:58:38 +02007179static int
7180acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7181 struct acl_expr *expr, struct acl_test *test)
7182{
7183 struct http_txn *txn = l7;
7184
Willy Tarreaub6866442008-07-14 23:54:42 +02007185 if (!txn)
7186 return 0;
7187
Willy Tarreau655dce92009-11-08 13:10:58 +01007188 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007189 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007190
Willy Tarreauc11416f2007-06-17 16:58:38 +02007191 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7192 /* ensure the indexes are not affected */
7193 return 0;
7194
7195 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7196}
7197
7198static int
7199acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7200 struct acl_expr *expr, struct acl_test *test)
7201{
7202 struct http_txn *txn = l7;
7203
Willy Tarreaub6866442008-07-14 23:54:42 +02007204 if (!txn)
7205 return 0;
7206
Willy Tarreau655dce92009-11-08 13:10:58 +01007207 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007208 return 0;
7209
7210 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7211}
7212
Willy Tarreau33a7e692007-06-10 19:45:56 +02007213/* 7. Check on HTTP header's integer value. The integer value is returned.
7214 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007215 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007216 */
7217static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007218acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007219 struct acl_expr *expr, struct acl_test *test)
7220{
7221 struct http_txn *txn = l7;
7222 struct hdr_idx *idx = &txn->hdr_idx;
7223 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007224
Willy Tarreaub6866442008-07-14 23:54:42 +02007225 if (!txn)
7226 return 0;
7227
Willy Tarreau33a7e692007-06-10 19:45:56 +02007228 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7229 /* search for header from the beginning */
7230 ctx->idx = 0;
7231
Willy Tarreau33a7e692007-06-10 19:45:56 +02007232 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7233 test->flags |= ACL_TEST_F_FETCH_MORE;
7234 test->flags |= ACL_TEST_F_VOL_HDR;
7235 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
7236 return 1;
7237 }
7238
7239 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7240 test->flags |= ACL_TEST_F_VOL_HDR;
7241 return 0;
7242}
7243
Willy Tarreauc11416f2007-06-17 16:58:38 +02007244static int
7245acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7246 struct acl_expr *expr, struct acl_test *test)
7247{
7248 struct http_txn *txn = l7;
7249
Willy Tarreaub6866442008-07-14 23:54:42 +02007250 if (!txn)
7251 return 0;
7252
Willy Tarreau655dce92009-11-08 13:10:58 +01007253 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007254 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007255
Willy Tarreauc11416f2007-06-17 16:58:38 +02007256 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7257 /* ensure the indexes are not affected */
7258 return 0;
7259
7260 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7261}
7262
7263static int
7264acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7265 struct acl_expr *expr, struct acl_test *test)
7266{
7267 struct http_txn *txn = l7;
7268
Willy Tarreaub6866442008-07-14 23:54:42 +02007269 if (!txn)
7270 return 0;
7271
Willy Tarreau655dce92009-11-08 13:10:58 +01007272 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007273 return 0;
7274
7275 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7276}
7277
Willy Tarreau106f9792009-09-19 07:54:16 +02007278/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7279 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7280 */
7281static int
7282acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7283 struct acl_expr *expr, struct acl_test *test)
7284{
7285 struct http_txn *txn = l7;
7286 struct hdr_idx *idx = &txn->hdr_idx;
7287 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7288
7289 if (!txn)
7290 return 0;
7291
7292 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7293 /* search for header from the beginning */
7294 ctx->idx = 0;
7295
7296 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7297 test->flags |= ACL_TEST_F_FETCH_MORE;
7298 test->flags |= ACL_TEST_F_VOL_HDR;
7299 /* Same optimization as url_ip */
7300 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
7301 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
7302 test->ptr = (void *)&l4->srv_addr.sin_addr;
7303 test->i = AF_INET;
7304 return 1;
7305 }
7306
7307 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7308 test->flags |= ACL_TEST_F_VOL_HDR;
7309 return 0;
7310}
7311
7312static int
7313acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7314 struct acl_expr *expr, struct acl_test *test)
7315{
7316 struct http_txn *txn = l7;
7317
7318 if (!txn)
7319 return 0;
7320
Willy Tarreau655dce92009-11-08 13:10:58 +01007321 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007322 return 0;
7323
7324 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7325 /* ensure the indexes are not affected */
7326 return 0;
7327
7328 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
7329}
7330
7331static int
7332acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7333 struct acl_expr *expr, struct acl_test *test)
7334{
7335 struct http_txn *txn = l7;
7336
7337 if (!txn)
7338 return 0;
7339
Willy Tarreau655dce92009-11-08 13:10:58 +01007340 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007341 return 0;
7342
7343 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
7344}
7345
Willy Tarreau737b0c12007-06-10 21:28:46 +02007346/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7347 * the first '/' after the possible hostname, and ends before the possible '?'.
7348 */
7349static int
7350acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
7351 struct acl_expr *expr, struct acl_test *test)
7352{
7353 struct http_txn *txn = l7;
7354 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007355
Willy Tarreaub6866442008-07-14 23:54:42 +02007356 if (!txn)
7357 return 0;
7358
Willy Tarreau655dce92009-11-08 13:10:58 +01007359 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007360 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007361
Willy Tarreauc11416f2007-06-17 16:58:38 +02007362 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7363 /* ensure the indexes are not affected */
7364 return 0;
7365
Willy Tarreau962c3f42010-01-10 00:15:35 +01007366 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007367 ptr = http_get_path(txn);
7368 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007369 return 0;
7370
7371 /* OK, we got the '/' ! */
7372 test->ptr = ptr;
7373
7374 while (ptr < end && *ptr != '?')
7375 ptr++;
7376
7377 test->len = ptr - test->ptr;
7378
7379 /* we do not need to set READ_ONLY because the data is in a buffer */
7380 test->flags = ACL_TEST_F_VOL_1ST;
7381 return 1;
7382}
7383
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007384static int
7385acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
7386 struct acl_expr *expr, struct acl_test *test)
7387{
7388 struct buffer *req = s->req;
7389 struct http_txn *txn = &s->txn;
7390 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007391
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007392 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7393 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7394 */
7395
7396 if (!s || !req)
7397 return 0;
7398
Willy Tarreau655dce92009-11-08 13:10:58 +01007399 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007400 /* Already decoded as OK */
7401 test->flags |= ACL_TEST_F_SET_RES_PASS;
7402 return 1;
7403 }
7404
7405 /* Try to decode HTTP request */
7406 if (likely(req->lr < req->r))
7407 http_msg_analyzer(req, msg, &txn->hdr_idx);
7408
Willy Tarreau655dce92009-11-08 13:10:58 +01007409 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007410 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
7411 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7412 return 1;
7413 }
7414 /* wait for final state */
7415 test->flags |= ACL_TEST_F_MAY_CHANGE;
7416 return 0;
7417 }
7418
7419 /* OK we got a valid HTTP request. We have some minor preparation to
7420 * perform so that further checks can rely on HTTP tests.
7421 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007422 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007423 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7424 s->flags |= SN_REDIRECTABLE;
7425
7426 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7427 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7428 return 1;
7429 }
7430
7431 test->flags |= ACL_TEST_F_SET_RES_PASS;
7432 return 1;
7433}
7434
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007435static int
7436acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
7437 struct acl_expr *expr, struct acl_test *test)
7438{
7439
7440 if (!s)
7441 return 0;
7442
7443 if (!get_http_auth(s))
7444 return 0;
7445
7446 test->ctx.a[0] = expr->arg.ul;
7447 test->ctx.a[1] = s->txn.auth.user;
7448 test->ctx.a[2] = s->txn.auth.pass;
7449
7450 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
7451
7452 return 1;
7453}
Willy Tarreau8797c062007-05-07 00:55:35 +02007454
7455/************************************************************************/
7456/* All supported keywords must be declared here. */
7457/************************************************************************/
7458
7459/* Note: must not be declared <const> as its list will be overwritten */
7460static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007461 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7462
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007463 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02007464 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
7465 { "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 +02007466 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007467
Willy Tarreauc4262962010-05-10 23:42:40 +02007468 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007469 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7470 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7471 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7472 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7473 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7474 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02007475 { "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 +02007476 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007477
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007478 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02007479 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007480 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7481 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7482 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7483 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7484 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7485 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7486 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7487 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02007488 { "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 +02007489
Willy Tarreauc4262962010-05-10 23:42:40 +02007490 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007491 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7492 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7493 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7494 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7495 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7496 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7497 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7498 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02007499 { "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 +02007500
Willy Tarreauc4262962010-05-10 23:42:40 +02007501 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007502 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7503 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7504 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7505 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7506 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7507 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007508
Willy Tarreauf3d25982007-05-08 22:45:09 +02007509#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007510 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7511 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7512 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7513 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7514 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7515 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7516 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7517
Willy Tarreau8797c062007-05-07 00:55:35 +02007518 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7519 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7520 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7521 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7522 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7523 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7524 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7525 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007526#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02007527
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007528 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth },
7529 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth },
Willy Tarreau8797c062007-05-07 00:55:35 +02007530 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02007531}};
7532
Willy Tarreau4a568972010-05-12 08:08:50 +02007533/************************************************************************/
7534/* The code below is dedicated to pattern fetching and matching */
7535/************************************************************************/
7536
7537/* extract the IP address from the last occurrence of specified header. Note
7538 * that we should normally first extract the string then convert it to IP,
7539 * but right now we have all the functions to do this seemlessly, and we will
7540 * be able to change that later without touching the configuration.
7541 */
7542static int
7543pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7544 const char *arg, int arg_len, union pattern_data *data)
7545{
7546 struct http_txn *txn = l7;
7547
7548 data->ip.s_addr = htonl(get_ip_from_hdr2(&txn->req, arg, arg_len, &txn->hdr_idx, -1));
7549 return data->ip.s_addr != 0;
7550}
7551
7552/************************************************************************/
7553/* All supported keywords must be declared here. */
7554/************************************************************************/
7555/* Note: must not be declared <const> as its list will be overwritten */
7556static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
7557 { "hdr", pattern_fetch_hdr_ip, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
7558 { NULL, NULL, 0, 0 },
7559}};
7560
Willy Tarreau8797c062007-05-07 00:55:35 +02007561
7562__attribute__((constructor))
7563static void __http_protocol_init(void)
7564{
7565 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02007566 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02007567}
7568
7569
Willy Tarreau58f10d72006-12-04 02:26:12 +01007570/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007571 * Local variables:
7572 * c-indent-level: 8
7573 * c-basic-offset: 8
7574 * End:
7575 */