blob: f8b46f661a9f7f9575108be0e4149fdec2295b07 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010027#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/compat.h>
29#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010030#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreau8797c062007-05-07 00:55:35 +020042#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010043#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/backend.h>
45#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010046#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020049#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010051#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020052#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020053#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010055#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010059#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020060#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/task.h>
62
Willy Tarreau522d6c02009-12-06 18:49:18 +010063const char HTTP_100[] =
64 "HTTP/1.1 100 Continue\r\n\r\n";
65
66const struct chunk http_100_chunk = {
67 .str = (char *)&HTTP_100,
68 .len = sizeof(HTTP_100)-1
69};
70
Willy Tarreau1c47f852006-07-09 08:22:27 +020071/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010072const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020073 "HTTP/1.0 200 OK\r\n"
74 "Cache-Control: no-cache\r\n"
75 "Connection: close\r\n"
76 "Content-Type: text/html\r\n"
77 "\r\n"
78 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
79
Willy Tarreau0f772532006-12-23 20:51:41 +010080const struct chunk http_200_chunk = {
81 .str = (char *)&HTTP_200,
82 .len = sizeof(HTTP_200)-1
83};
84
Willy Tarreaua9679ac2010-01-03 17:32:57 +010085/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020086const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020090 "Location: "; /* not terminated since it will be concatenated with the URL */
91
Willy Tarreau0f772532006-12-23 20:51:41 +010092const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010095 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010096 "Location: "; /* not terminated since it will be concatenated with the URL */
97
98/* same as 302 except that the browser MUST retry with the GET method */
99const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100100 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100101 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100102 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
106const char *HTTP_401_fmt =
107 "HTTP/1.0 401 Unauthorized\r\n"
108 "Cache-Control: no-cache\r\n"
109 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200110 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
112 "\r\n"
113 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
114
Willy Tarreau844a7e72010-01-31 21:46:18 +0100115const char *HTTP_407_fmt =
116 "HTTP/1.0 407 Unauthorized\r\n"
117 "Cache-Control: no-cache\r\n"
118 "Connection: close\r\n"
119 "Content-Type: text/html\r\n"
120 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
121 "\r\n"
122 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
123
Willy Tarreau0f772532006-12-23 20:51:41 +0100124
125const int http_err_codes[HTTP_ERR_SIZE] = {
126 [HTTP_ERR_400] = 400,
127 [HTTP_ERR_403] = 403,
128 [HTTP_ERR_408] = 408,
129 [HTTP_ERR_500] = 500,
130 [HTTP_ERR_502] = 502,
131 [HTTP_ERR_503] = 503,
132 [HTTP_ERR_504] = 504,
133};
134
Willy Tarreau80587432006-12-24 17:47:20 +0100135static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100137 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 "Cache-Control: no-cache\r\n"
139 "Connection: close\r\n"
140 "Content-Type: text/html\r\n"
141 "\r\n"
142 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
143
144 [HTTP_ERR_403] =
145 "HTTP/1.0 403 Forbidden\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
151
152 [HTTP_ERR_408] =
153 "HTTP/1.0 408 Request Time-out\r\n"
154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
159
160 [HTTP_ERR_500] =
161 "HTTP/1.0 500 Server Error\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
167
168 [HTTP_ERR_502] =
169 "HTTP/1.0 502 Bad Gateway\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
175
176 [HTTP_ERR_503] =
177 "HTTP/1.0 503 Service Unavailable\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
183
184 [HTTP_ERR_504] =
185 "HTTP/1.0 504 Gateway Time-out\r\n"
186 "Cache-Control: no-cache\r\n"
187 "Connection: close\r\n"
188 "Content-Type: text/html\r\n"
189 "\r\n"
190 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
191
192};
193
Willy Tarreau80587432006-12-24 17:47:20 +0100194/* We must put the messages here since GCC cannot initialize consts depending
195 * on strlen().
196 */
197struct chunk http_err_chunks[HTTP_ERR_SIZE];
198
Willy Tarreau42250582007-04-01 01:30:43 +0200199#define FD_SETS_ARE_BITFIELDS
200#ifdef FD_SETS_ARE_BITFIELDS
201/*
202 * This map is used with all the FD_* macros to check whether a particular bit
203 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
204 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
205 * byte should be encoded. Be careful to always pass bytes from 0 to 255
206 * exclusively to the macros.
207 */
208fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
209fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
210
211#else
212#error "Check if your OS uses bitfields for fd_sets"
213#endif
214
Willy Tarreau80587432006-12-24 17:47:20 +0100215void init_proto_http()
216{
Willy Tarreau42250582007-04-01 01:30:43 +0200217 int i;
218 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100219 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200220
Willy Tarreau80587432006-12-24 17:47:20 +0100221 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
222 if (!http_err_msgs[msg]) {
223 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
224 abort();
225 }
226
227 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
228 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
229 }
Willy Tarreau42250582007-04-01 01:30:43 +0200230
231 /* initialize the log header encoding map : '{|}"#' should be encoded with
232 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
233 * URL encoding only requires '"', '#' to be encoded as well as non-
234 * printable characters above.
235 */
236 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
237 memset(url_encode_map, 0, sizeof(url_encode_map));
238 for (i = 0; i < 32; i++) {
239 FD_SET(i, hdr_encode_map);
240 FD_SET(i, url_encode_map);
241 }
242 for (i = 127; i < 256; i++) {
243 FD_SET(i, hdr_encode_map);
244 FD_SET(i, url_encode_map);
245 }
246
247 tmp = "\"#{|}";
248 while (*tmp) {
249 FD_SET(*tmp, hdr_encode_map);
250 tmp++;
251 }
252
253 tmp = "\"#";
254 while (*tmp) {
255 FD_SET(*tmp, url_encode_map);
256 tmp++;
257 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200258
259 /* memory allocations */
260 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200261 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100262}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263
Willy Tarreau53b6c742006-12-17 13:37:46 +0100264/*
265 * We have 26 list of methods (1 per first letter), each of which can have
266 * up to 3 entries (2 valid, 1 null).
267 */
268struct http_method_desc {
269 http_meth_t meth;
270 int len;
271 const char text[8];
272};
273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100274const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100275 ['C' - 'A'] = {
276 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
277 },
278 ['D' - 'A'] = {
279 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
280 },
281 ['G' - 'A'] = {
282 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
283 },
284 ['H' - 'A'] = {
285 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
286 },
287 ['P' - 'A'] = {
288 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
289 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
290 },
291 ['T' - 'A'] = {
292 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
293 },
294 /* rest is empty like this :
295 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
296 */
297};
298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100299/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200300 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100301 * RFC2616 for those chars.
302 */
303
304const char http_is_spht[256] = {
305 [' '] = 1, ['\t'] = 1,
306};
307
308const char http_is_crlf[256] = {
309 ['\r'] = 1, ['\n'] = 1,
310};
311
312const char http_is_lws[256] = {
313 [' '] = 1, ['\t'] = 1,
314 ['\r'] = 1, ['\n'] = 1,
315};
316
317const char http_is_sep[256] = {
318 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
319 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
320 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
321 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
322 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
323};
324
325const char http_is_ctl[256] = {
326 [0 ... 31] = 1,
327 [127] = 1,
328};
329
330/*
331 * A token is any ASCII char that is neither a separator nor a CTL char.
332 * Do not overwrite values in assignment since gcc-2.95 will not handle
333 * them correctly. Instead, define every non-CTL char's status.
334 */
335const char http_is_token[256] = {
336 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
337 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
338 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
339 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
340 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
341 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
342 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
343 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
344 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
345 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
346 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
347 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
348 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
349 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
350 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
351 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
352 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
353 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
354 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
355 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
356 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
357 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
358 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
359 ['|'] = 1, ['}'] = 0, ['~'] = 1,
360};
361
362
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100363/*
364 * An http ver_token is any ASCII which can be found in an HTTP version,
365 * which includes 'H', 'T', 'P', '/', '.' and any digit.
366 */
367const char http_is_ver_token[256] = {
368 ['.'] = 1, ['/'] = 1,
369 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
370 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
371 ['H'] = 1, ['P'] = 1, ['T'] = 1,
372};
373
374
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100375/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100376 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
377 */
378#if defined(DEBUG_FSM)
379static void http_silent_debug(int line, struct session *s)
380{
381 int size = 0;
382 size += snprintf(trash + size, sizeof(trash) - size,
383 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n",
384 line,
385 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
386 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags);
387 write(-1, trash, size);
388 size = 0;
389 size += snprintf(trash + size, sizeof(trash) - size,
390 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n",
391 line,
392 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
393 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward);
394
395 write(-1, trash, size);
396}
397#else
398#define http_silent_debug(l,s) do { } while (0)
399#endif
400
401/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100402 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
403 * CRLF. Text length is measured first, so it cannot be NULL.
404 * The header is also automatically added to the index <hdr_idx>, and the end
405 * of headers is automatically adjusted. The number of bytes added is returned
406 * on success, otherwise <0 is returned indicating an error.
407 */
408int http_header_add_tail(struct buffer *b, struct http_msg *msg,
409 struct hdr_idx *hdr_idx, const char *text)
410{
411 int bytes, len;
412
413 len = strlen(text);
414 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
415 if (!bytes)
416 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100417 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100418 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
419}
420
421/*
422 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
423 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
424 * the buffer is only opened and the space reserved, but nothing is copied.
425 * The header is also automatically added to the index <hdr_idx>, and the end
426 * of headers is automatically adjusted. The number of bytes added is returned
427 * on success, otherwise <0 is returned indicating an error.
428 */
429int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
430 struct hdr_idx *hdr_idx, const char *text, int len)
431{
432 int bytes;
433
434 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
435 if (!bytes)
436 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100437 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100438 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
439}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440
441/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100442 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
443 * If so, returns the position of the first non-space character relative to
444 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
445 * to return a pointer to the place after the first space. Returns 0 if the
446 * header name does not match. Checks are case-insensitive.
447 */
448int http_header_match2(const char *hdr, const char *end,
449 const char *name, int len)
450{
451 const char *val;
452
453 if (hdr + len >= end)
454 return 0;
455 if (hdr[len] != ':')
456 return 0;
457 if (strncasecmp(hdr, name, len) != 0)
458 return 0;
459 val = hdr + len + 1;
460 while (val < end && HTTP_IS_SPHT(*val))
461 val++;
462 if ((val >= end) && (len + 2 <= end - hdr))
463 return len + 2; /* we may replace starting from second space */
464 return val - hdr;
465}
466
Willy Tarreau68085d82010-01-18 14:54:04 +0100467/* Find the end of the header value contained between <s> and <e>. See RFC2616,
468 * par 2.2 for more information. Note that it requires a valid header to return
469 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200470 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100471char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200472{
473 int quoted, qdpair;
474
475 quoted = qdpair = 0;
476 for (; s < e; s++) {
477 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200478 else if (quoted) {
479 if (*s == '\\') qdpair = 1;
480 else if (*s == '"') quoted = 0;
481 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200482 else if (*s == '"') quoted = 1;
483 else if (*s == ',') return s;
484 }
485 return s;
486}
487
488/* Find the first or next occurrence of header <name> in message buffer <sol>
489 * using headers index <idx>, and return it in the <ctx> structure. This
490 * structure holds everything necessary to use the header and find next
491 * occurrence. If its <idx> member is 0, the header is searched from the
492 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100493 * 1 when it finds a value, and 0 when there is no more. It is designed to work
494 * with headers defined as comma-separated lists. As a special case, if ctx->val
495 * is NULL when searching for a new values of a header, the current header is
496 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200497 */
498int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100499 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200500 struct hdr_ctx *ctx)
501{
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 char *eol, *sov;
503 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200504
Willy Tarreau68085d82010-01-18 14:54:04 +0100505 cur_idx = ctx->idx;
506 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 /* We have previously returned a value, let's search
508 * another one on the same line.
509 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100511 ctx->del = ctx->val + ctx->vlen;
512 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200513 eol = sol + idx->v[cur_idx].len;
514
515 if (sov >= eol)
516 /* no more values in this header */
517 goto next_hdr;
518
Willy Tarreau68085d82010-01-18 14:54:04 +0100519 /* values remaining for this header, skip the comma but save it
520 * for later use (eg: for header deletion).
521 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200522 sov++;
523 while (sov < eol && http_is_lws[(unsigned char)*sov])
524 sov++;
525
526 goto return_hdr;
527 }
528
529 /* first request for this header */
530 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100531 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200532 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200533 while (cur_idx) {
534 eol = sol + idx->v[cur_idx].len;
535
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200536 if (len == 0) {
537 /* No argument was passed, we want any header.
538 * To achieve this, we simply build a fake request. */
539 while (sol + len < eol && sol[len] != ':')
540 len++;
541 name = sol;
542 }
543
Willy Tarreau33a7e692007-06-10 19:45:56 +0200544 if ((len < eol - sol) &&
545 (sol[len] == ':') &&
546 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100547 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200548 sov = sol + len + 1;
549 while (sov < eol && http_is_lws[(unsigned char)*sov])
550 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100551
Willy Tarreau33a7e692007-06-10 19:45:56 +0200552 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100553 ctx->prev = old_idx;
554 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200555 ctx->idx = cur_idx;
556 ctx->val = sov - sol;
557
558 eol = find_hdr_value_end(sov, eol);
559 ctx->vlen = eol - sov;
560 return 1;
561 }
562 next_hdr:
563 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100564 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200565 cur_idx = idx->v[cur_idx].next;
566 }
567 return 0;
568}
569
570int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100571 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200572 struct hdr_ctx *ctx)
573{
574 return http_find_header2(name, strlen(name), sol, idx, ctx);
575}
576
Willy Tarreau68085d82010-01-18 14:54:04 +0100577/* Remove one value of a header. This only works on a <ctx> returned by one of
578 * the http_find_header functions. The value is removed, as well as surrounding
579 * commas if any. If the removed value was alone, the whole header is removed.
580 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
581 * message <msg>. The new index is returned. If it is zero, it means there is
582 * no more header, so any processing may stop. The ctx is always left in a form
583 * that can be handled by http_find_header2() to find next occurrence.
584 */
585int http_remove_header2(struct http_msg *msg, struct buffer *buf,
586 struct hdr_idx *idx, struct hdr_ctx *ctx)
587{
588 int cur_idx = ctx->idx;
589 char *sol = ctx->line;
590 struct hdr_idx_elem *hdr;
591 int delta, skip_comma;
592
593 if (!cur_idx)
594 return 0;
595
596 hdr = &idx->v[cur_idx];
597 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) {
598 /* This was the only value of the header, we must now remove it entirely. */
599 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
600 http_msg_move_end(msg, delta);
601 idx->used--;
602 hdr->len = 0; /* unused entry */
603 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
604 ctx->idx = ctx->prev; /* walk back to the end of previous header */
605 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
606 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
607 ctx->vlen = 0;
608 return ctx->idx;
609 }
610
611 /* This was not the only value of this header. We have to remove between
612 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
613 * of the list, we remove the last separator.
614 */
615
616 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
617 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
618 sol + ctx->val + ctx->vlen + skip_comma,
619 NULL, 0);
620 hdr->len += delta;
621 http_msg_move_end(msg, delta);
622 ctx->val = ctx->del;
623 ctx->vlen = 0;
624 return ctx->idx;
625}
626
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100627/* This function handles a server error at the stream interface level. The
628 * stream interface is assumed to be already in a closed state. An optional
629 * message is copied into the input buffer, and an HTTP status code stored.
630 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100631 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100633static void http_server_error(struct session *t, struct stream_interface *si,
634 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200635{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100636 buffer_auto_read(si->ob);
637 buffer_abort(si->ob);
638 buffer_auto_close(si->ob);
639 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200640 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100641 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100642 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100643 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100644 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200645 }
646 if (!(t->flags & SN_ERR_MASK))
647 t->flags |= err;
648 if (!(t->flags & SN_FINST_MASK))
649 t->flags |= finst;
650}
651
Willy Tarreau80587432006-12-24 17:47:20 +0100652/* This function returns the appropriate error location for the given session
653 * and message.
654 */
655
656struct chunk *error_message(struct session *s, int msgnum)
657{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200658 if (s->be->errmsg[msgnum].str)
659 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100660 else if (s->fe->errmsg[msgnum].str)
661 return &s->fe->errmsg[msgnum];
662 else
663 return &http_err_chunks[msgnum];
664}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665
Willy Tarreau53b6c742006-12-17 13:37:46 +0100666/*
667 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
668 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
669 */
670static http_meth_t find_http_meth(const char *str, const int len)
671{
672 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100673 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100674
675 m = ((unsigned)*str - 'A');
676
677 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100678 for (h = http_methods[m]; h->len > 0; h++) {
679 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100680 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100681 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100682 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100683 };
684 return HTTP_METH_OTHER;
685 }
686 return HTTP_METH_NONE;
687
688}
689
Willy Tarreau21d2af32008-02-14 20:25:24 +0100690/* Parse the URI from the given transaction (which is assumed to be in request
691 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
692 * It is returned otherwise.
693 */
694static char *
695http_get_path(struct http_txn *txn)
696{
697 char *ptr, *end;
698
Willy Tarreau962c3f42010-01-10 00:15:35 +0100699 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100700 end = ptr + txn->req.sl.rq.u_l;
701
702 if (ptr >= end)
703 return NULL;
704
705 /* RFC2616, par. 5.1.2 :
706 * Request-URI = "*" | absuri | abspath | authority
707 */
708
709 if (*ptr == '*')
710 return NULL;
711
712 if (isalpha((unsigned char)*ptr)) {
713 /* this is a scheme as described by RFC3986, par. 3.1 */
714 ptr++;
715 while (ptr < end &&
716 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
717 ptr++;
718 /* skip '://' */
719 if (ptr == end || *ptr++ != ':')
720 return NULL;
721 if (ptr == end || *ptr++ != '/')
722 return NULL;
723 if (ptr == end || *ptr++ != '/')
724 return NULL;
725 }
726 /* skip [user[:passwd]@]host[:[port]] */
727
728 while (ptr < end && *ptr != '/')
729 ptr++;
730
731 if (ptr == end)
732 return NULL;
733
734 /* OK, we got the '/' ! */
735 return ptr;
736}
737
Willy Tarreauefb453c2008-10-26 20:49:47 +0100738/* Returns a 302 for a redirectable request. This may only be called just after
739 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
740 * left unchanged and will follow normal proxy processing.
741 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100742void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100743{
744 struct http_txn *txn;
745 struct chunk rdr;
746 char *path;
747 int len;
748
749 /* 1: create the response header */
750 rdr.len = strlen(HTTP_302);
751 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100752 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100753 memcpy(rdr.str, HTTP_302, rdr.len);
754
755 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200756 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100757 return;
758
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100759 /* special prefix "/" means don't change URL */
760 if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') {
761 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
762 rdr.len += s->srv->rdr_len;
763 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764
765 /* 3: add the request URI */
766 txn = &s->txn;
767 path = http_get_path(txn);
768 if (!path)
769 return;
770
Willy Tarreau962c3f42010-01-10 00:15:35 +0100771 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200772 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 return;
774
775 memcpy(rdr.str + rdr.len, path, len);
776 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100777
778 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
779 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
780 rdr.len += 29;
781 } else {
782 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
783 rdr.len += 23;
784 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100785
786 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100787 si->shutr(si);
788 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 si->err_type = SI_ET_NONE;
790 si->err_loc = NULL;
791 si->state = SI_ST_CLO;
792
793 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100794 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100795
796 /* FIXME: we should increase a counter of redirects per server and per backend. */
797 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100798 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100799}
800
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100801/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100802 * that the server side is closed. Note that err_type is actually a
803 * bitmask, where almost only aborts may be cumulated with other
804 * values. We consider that aborted operations are more important
805 * than timeouts or errors due to the fact that nobody else in the
806 * logs might explain incomplete retries. All others should avoid
807 * being cumulated. It should normally not be possible to have multiple
808 * aborts at once, but just in case, the first one in sequence is reported.
809 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100810void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100811{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100812 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813
814 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100815 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
816 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100818 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
819 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100820 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100821 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
822 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100823 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100824 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
825 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100826 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100827 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
828 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100829 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100830 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
831 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100832 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100833 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
834 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100835}
836
Willy Tarreau42250582007-04-01 01:30:43 +0200837extern const char sess_term_cond[8];
838extern const char sess_fin_state[8];
839extern const char *monthname[12];
840const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */
841const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown,
842 Set-cookie seen and left unchanged (passive), Set-cookie Deleted,
843 unknown, Set-cookie Rewritten */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200844struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200845struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100846
Emeric Brun3a058f32009-06-30 18:26:00 +0200847void http_sess_clflog(struct session *s)
848{
849 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
850 struct proxy *fe = s->fe;
851 struct proxy *be = s->be;
852 struct proxy *prx_log;
853 struct http_txn *txn = &s->txn;
854 int tolog, level, err;
855 char *uri, *h;
856 char *svid;
857 struct tm tm;
858 static char tmpline[MAX_SYSLOG_LEN];
859 int hdr;
860 size_t w;
861 int t_request;
862
863 prx_log = fe;
864 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +0200865 (s->req->cons->conn_retries != be->conn_retries) ||
Emeric Brun3a058f32009-06-30 18:26:00 +0200866 txn->status >= 500;
867
868 if (s->cli_addr.ss_family == AF_INET)
869 inet_ntop(AF_INET,
870 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
871 pn, sizeof(pn));
872 else
873 inet_ntop(AF_INET6,
874 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
875 pn, sizeof(pn));
876
877 get_gmtime(s->logs.accept_date.tv_sec, &tm);
878
879 /* FIXME: let's limit ourselves to frontend logging for now. */
880 tolog = fe->to_log;
881
882 h = tmpline;
883
884 w = snprintf(h, sizeof(tmpline),
885 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
886 pn,
887 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
888 tm.tm_hour, tm.tm_min, tm.tm_sec);
889 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
890 goto trunc;
891 h += w;
892
893 if (h >= tmpline + sizeof(tmpline) - 4)
894 goto trunc;
895
896 *(h++) = ' ';
897 *(h++) = '\"';
898 uri = txn->uri ? txn->uri : "<BADREQ>";
899 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
900 '#', url_encode_map, uri);
901 *(h++) = '\"';
902
903 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
904 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
905 goto trunc;
906 h += w;
907
908 if (h >= tmpline + sizeof(tmpline) - 9)
909 goto trunc;
910 memcpy(h, " \"-\" \"-\"", 8);
911 h += 8;
912
913 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
914 " %d %03d",
915 (s->cli_addr.ss_family == AF_INET) ?
916 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
917 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
918 (int)s->logs.accept_date.tv_usec/1000);
919 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
920 goto trunc;
921 h += w;
922
923 w = strlen(fe->id);
924 if (h >= tmpline + sizeof(tmpline) - 4 - w)
925 goto trunc;
926 *(h++) = ' ';
927 *(h++) = '\"';
928 memcpy(h, fe->id, w);
929 h += w;
930 *(h++) = '\"';
931
932 w = strlen(be->id);
933 if (h >= tmpline + sizeof(tmpline) - 4 - w)
934 goto trunc;
935 *(h++) = ' ';
936 *(h++) = '\"';
937 memcpy(h, be->id, w);
938 h += w;
939 *(h++) = '\"';
940
941 svid = (tolog & LW_SVID) ?
942 (s->data_source != DATA_SRC_STATS) ?
943 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
944
945 w = strlen(svid);
946 if (h >= tmpline + sizeof(tmpline) - 4 - w)
947 goto trunc;
948 *(h++) = ' ';
949 *(h++) = '\"';
950 memcpy(h, svid, w);
951 h += w;
952 *(h++) = '\"';
953
954 t_request = -1;
955 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
956 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
957 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
958 " %d %ld %ld %ld %ld",
959 t_request,
960 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
961 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
962 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
963 s->logs.t_close);
964 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
965 goto trunc;
966 h += w;
967
968 if (h >= tmpline + sizeof(tmpline) - 8)
969 goto trunc;
970 *(h++) = ' ';
971 *(h++) = '\"';
972 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
973 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
974 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
975 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
976 *(h++) = '\"';
977
978 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
979 " %d %d %d %d %d %ld %ld",
980 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Willy Tarreauee28de02010-06-01 09:51:00 +0200981 (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries,
Emeric Brun3a058f32009-06-30 18:26:00 +0200982 s->logs.srv_queue_size, s->logs.prx_queue_size);
983
984 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
985 goto trunc;
986 h += w;
987
988 if (txn->cli_cookie) {
989 w = strlen(txn->cli_cookie);
990 if (h >= tmpline + sizeof(tmpline) - 4 - w)
991 goto trunc;
992 *(h++) = ' ';
993 *(h++) = '\"';
994 memcpy(h, txn->cli_cookie, w);
995 h += w;
996 *(h++) = '\"';
997 } else {
998 if (h >= tmpline + sizeof(tmpline) - 5)
999 goto trunc;
1000 memcpy(h, " \"-\"", 4);
1001 h += 4;
1002 }
1003
1004 if (txn->srv_cookie) {
1005 w = strlen(txn->srv_cookie);
1006 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1007 goto trunc;
1008 *(h++) = ' ';
1009 *(h++) = '\"';
1010 memcpy(h, txn->srv_cookie, w);
1011 h += w;
1012 *(h++) = '\"';
1013 } else {
1014 if (h >= tmpline + sizeof(tmpline) - 5)
1015 goto trunc;
1016 memcpy(h, " \"-\"", 4);
1017 h += 4;
1018 }
1019
1020 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1021 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1022 if (h >= sizeof (tmpline) + tmpline - 4)
1023 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001024 if (txn->req.cap[hdr] != NULL) {
1025 *(h++) = ' ';
1026 *(h++) = '\"';
1027 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1028 '#', hdr_encode_map, txn->req.cap[hdr]);
1029 *(h++) = '\"';
1030 } else {
1031 memcpy(h, " \"-\"", 4);
1032 h += 4;
1033 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001034 }
1035 }
1036
1037 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1038 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1039 if (h >= sizeof (tmpline) + tmpline - 4)
1040 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001041 if (txn->rsp.cap[hdr] != NULL) {
1042 *(h++) = ' ';
1043 *(h++) = '\"';
1044 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1045 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1046 *(h++) = '\"';
1047 } else {
1048 memcpy(h, " \"-\"", 4);
1049 h += 4;
1050 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001051 }
1052 }
1053
1054trunc:
1055 *h = '\0';
1056
1057 level = LOG_INFO;
1058 if (err && (fe->options2 & PR_O2_LOGERRORS))
1059 level = LOG_ERR;
1060
1061 send_log(prx_log, level, "%s\n", tmpline);
1062
1063 s->logs.logwait = 0;
1064}
1065
Willy Tarreau42250582007-04-01 01:30:43 +02001066/*
1067 * send a log for the session when we have enough info about it.
1068 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001069 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001070void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001071{
1072 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
1073 struct proxy *fe = s->fe;
1074 struct proxy *be = s->be;
1075 struct proxy *prx_log;
1076 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001077 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001078 char *uri, *h;
1079 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001080 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001081 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001082 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001083 int hdr;
1084
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001085 /* if we don't want to log normal traffic, return now */
1086 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +02001087 (s->req->cons->conn_retries != be->conn_retries) ||
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001088 txn->status >= 500;
1089 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1090 return;
1091
Willy Tarreau42250582007-04-01 01:30:43 +02001092 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1093 return;
1094 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095
Emeric Brun3a058f32009-06-30 18:26:00 +02001096 if (prx_log->options2 & PR_O2_CLFLOG)
1097 return http_sess_clflog(s);
1098
Willy Tarreau42250582007-04-01 01:30:43 +02001099 if (s->cli_addr.ss_family == AF_INET)
1100 inet_ntop(AF_INET,
1101 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1102 pn, sizeof(pn));
1103 else
1104 inet_ntop(AF_INET6,
1105 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1106 pn, sizeof(pn));
1107
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001108 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001109
1110 /* FIXME: let's limit ourselves to frontend logging for now. */
1111 tolog = fe->to_log;
1112
1113 h = tmpline;
1114 if (fe->to_log & LW_REQHDR &&
1115 txn->req.cap &&
1116 (h < tmpline + sizeof(tmpline) - 10)) {
1117 *(h++) = ' ';
1118 *(h++) = '{';
1119 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1120 if (hdr)
1121 *(h++) = '|';
1122 if (txn->req.cap[hdr] != NULL)
1123 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1124 '#', hdr_encode_map, txn->req.cap[hdr]);
1125 }
1126 *(h++) = '}';
1127 }
1128
1129 if (fe->to_log & LW_RSPHDR &&
1130 txn->rsp.cap &&
1131 (h < tmpline + sizeof(tmpline) - 7)) {
1132 *(h++) = ' ';
1133 *(h++) = '{';
1134 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1135 if (hdr)
1136 *(h++) = '|';
1137 if (txn->rsp.cap[hdr] != NULL)
1138 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1139 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1140 }
1141 *(h++) = '}';
1142 }
1143
1144 if (h < tmpline + sizeof(tmpline) - 4) {
1145 *(h++) = ' ';
1146 *(h++) = '"';
1147 uri = txn->uri ? txn->uri : "<BADREQ>";
1148 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1149 '#', url_encode_map, uri);
1150 *(h++) = '"';
1151 }
1152 *h = '\0';
1153
1154 svid = (tolog & LW_SVID) ?
1155 (s->data_source != DATA_SRC_STATS) ?
1156 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1157
Willy Tarreau70089872008-06-13 21:12:51 +02001158 t_request = -1;
1159 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1160 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1161
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001162 level = LOG_INFO;
1163 if (err && (fe->options2 & PR_O2_LOGERRORS))
1164 level = LOG_ERR;
1165
1166 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001167 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001168 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1169 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau42250582007-04-01 01:30:43 +02001170 pn,
1171 (s->cli_addr.ss_family == AF_INET) ?
1172 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
1173 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001174 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001175 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001176 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001177 t_request,
1178 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001179 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1180 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1181 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1182 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001183 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001184 txn->cli_cookie ? txn->cli_cookie : "-",
1185 txn->srv_cookie ? txn->srv_cookie : "-",
1186 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1187 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1188 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1189 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1190 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001191 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +02001192 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001193 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1194
1195 s->logs.logwait = 0;
1196}
1197
Willy Tarreau117f59e2007-03-04 18:17:17 +01001198
1199/*
1200 * Capture headers from message starting at <som> according to header list
1201 * <cap_hdr>, and fill the <idx> structure appropriately.
1202 */
1203void capture_headers(char *som, struct hdr_idx *idx,
1204 char **cap, struct cap_hdr *cap_hdr)
1205{
1206 char *eol, *sol, *col, *sov;
1207 int cur_idx;
1208 struct cap_hdr *h;
1209 int len;
1210
1211 sol = som + hdr_idx_first_pos(idx);
1212 cur_idx = hdr_idx_first_idx(idx);
1213
1214 while (cur_idx) {
1215 eol = sol + idx->v[cur_idx].len;
1216
1217 col = sol;
1218 while (col < eol && *col != ':')
1219 col++;
1220
1221 sov = col + 1;
1222 while (sov < eol && http_is_lws[(unsigned char)*sov])
1223 sov++;
1224
1225 for (h = cap_hdr; h; h = h->next) {
1226 if ((h->namelen == col - sol) &&
1227 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1228 if (cap[h->index] == NULL)
1229 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001230 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001231
1232 if (cap[h->index] == NULL) {
1233 Alert("HTTP capture : out of memory.\n");
1234 continue;
1235 }
1236
1237 len = eol - sov;
1238 if (len > h->len)
1239 len = h->len;
1240
1241 memcpy(cap[h->index], sov, len);
1242 cap[h->index][len]=0;
1243 }
1244 }
1245 sol = eol + idx->v[cur_idx].cr + 1;
1246 cur_idx = idx->v[cur_idx].next;
1247 }
1248}
1249
1250
Willy Tarreau42250582007-04-01 01:30:43 +02001251/* either we find an LF at <ptr> or we jump to <bad>.
1252 */
1253#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1254
1255/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1256 * otherwise to <http_msg_ood> with <state> set to <st>.
1257 */
1258#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1259 ptr++; \
1260 if (likely(ptr < end)) \
1261 goto good; \
1262 else { \
1263 state = (st); \
1264 goto http_msg_ood; \
1265 } \
1266 } while (0)
1267
1268
Willy Tarreaubaaee002006-06-26 02:48:02 +02001269/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001270 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001271 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1272 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1273 * will give undefined results.
1274 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1275 * and that msg->sol points to the beginning of the response.
1276 * If a complete line is found (which implies that at least one CR or LF is
1277 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1278 * returned indicating an incomplete line (which does not mean that parts have
1279 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1280 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1281 * upon next call.
1282 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001283 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1285 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001286 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001287 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001288const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1289 unsigned int state, const char *ptr, const char *end,
1290 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001291{
Willy Tarreau8973c702007-01-21 23:58:29 +01001292 switch (state) {
1293 http_msg_rpver:
1294 case HTTP_MSG_RPVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001295 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001296 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1297
1298 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001299 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1301 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001302 state = HTTP_MSG_ERROR;
1303 break;
1304
Willy Tarreau8973c702007-01-21 23:58:29 +01001305 http_msg_rpver_sp:
1306 case HTTP_MSG_RPVER_SP:
1307 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001308 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 goto http_msg_rpcode;
1310 }
1311 if (likely(HTTP_IS_SPHT(*ptr)))
1312 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1313 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001314 state = HTTP_MSG_ERROR;
1315 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001316
1317 http_msg_rpcode:
1318 case HTTP_MSG_RPCODE:
1319 if (likely(!HTTP_IS_LWS(*ptr)))
1320 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1321
1322 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001323 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001324 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1325 }
1326
1327 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001328 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001329 http_msg_rsp_reason:
1330 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001331 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 msg->sl.st.r_l = 0;
1333 goto http_msg_rpline_eol;
1334
1335 http_msg_rpcode_sp:
1336 case HTTP_MSG_RPCODE_SP:
1337 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001338 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 goto http_msg_rpreason;
1340 }
1341 if (likely(HTTP_IS_SPHT(*ptr)))
1342 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1343 /* so it's a CR/LF, so there is no reason phrase */
1344 goto http_msg_rsp_reason;
1345
1346 http_msg_rpreason:
1347 case HTTP_MSG_RPREASON:
1348 if (likely(!HTTP_IS_CRLF(*ptr)))
1349 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001350 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001351 http_msg_rpline_eol:
1352 /* We have seen the end of line. Note that we do not
1353 * necessarily have the \n yet, but at least we know that we
1354 * have EITHER \r OR \n, otherwise the response would not be
1355 * complete. We can then record the response length and return
1356 * to the caller which will be able to register it.
1357 */
1358 msg->sl.st.l = ptr - msg->sol;
1359 return ptr;
1360
1361#ifdef DEBUG_FULL
1362 default:
1363 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1364 exit(1);
1365#endif
1366 }
1367
1368 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001369 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001370 if (ret_state)
1371 *ret_state = state;
1372 if (ret_ptr)
1373 *ret_ptr = (char *)ptr;
1374 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001375}
1376
Willy Tarreau8973c702007-01-21 23:58:29 +01001377/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001378 * This function parses a request line between <ptr> and <end>, starting with
1379 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1380 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1381 * will give undefined results.
1382 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1383 * and that msg->sol points to the beginning of the request.
1384 * If a complete line is found (which implies that at least one CR or LF is
1385 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1386 * returned indicating an incomplete line (which does not mean that parts have
1387 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1388 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1389 * upon next call.
1390 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001391 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1393 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001394 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001395 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001396const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1397 unsigned int state, const char *ptr, const char *end,
1398 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001399{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 switch (state) {
1401 http_msg_rqmeth:
1402 case HTTP_MSG_RQMETH:
1403 if (likely(HTTP_IS_TOKEN(*ptr)))
1404 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001405
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001407 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1409 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 if (likely(HTTP_IS_CRLF(*ptr))) {
1412 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001413 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001415 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001417 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001419 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 msg->sl.rq.v_l = 0;
1421 goto http_msg_rqline_eol;
1422 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001423 state = HTTP_MSG_ERROR;
1424 break;
1425
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 http_msg_rqmeth_sp:
1427 case HTTP_MSG_RQMETH_SP:
1428 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001429 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 goto http_msg_rquri;
1431 }
1432 if (likely(HTTP_IS_SPHT(*ptr)))
1433 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1434 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1435 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 http_msg_rquri:
1438 case HTTP_MSG_RQURI:
1439 if (likely(!HTTP_IS_LWS(*ptr)))
1440 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001441
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001443 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1445 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1448 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 http_msg_rquri_sp:
1451 case HTTP_MSG_RQURI_SP:
1452 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001453 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 goto http_msg_rqver;
1455 }
1456 if (likely(HTTP_IS_SPHT(*ptr)))
1457 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1458 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1459 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 http_msg_rqver:
1462 case HTTP_MSG_RQVER:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001463 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001465
1466 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001467 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001468 http_msg_rqline_eol:
1469 /* We have seen the end of line. Note that we do not
1470 * necessarily have the \n yet, but at least we know that we
1471 * have EITHER \r OR \n, otherwise the request would not be
1472 * complete. We can then record the request length and return
1473 * to the caller which will be able to register it.
1474 */
1475 msg->sl.rq.l = ptr - msg->sol;
1476 return ptr;
1477 }
1478
1479 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001480 state = HTTP_MSG_ERROR;
1481 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483#ifdef DEBUG_FULL
1484 default:
1485 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1486 exit(1);
1487#endif
1488 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001489
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001491 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 if (ret_state)
1493 *ret_state = state;
1494 if (ret_ptr)
1495 *ret_ptr = (char *)ptr;
1496 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001498
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001499/*
1500 * Returns the data from Authorization header. Function may be called more
1501 * than once so data is stored in txn->auth_data. When no header is found
1502 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1503 * searching again for something we are unable to find anyway.
1504 */
1505
1506char get_http_auth_buff[BUFSIZE];
1507
1508int
1509get_http_auth(struct session *s)
1510{
1511
1512 struct http_txn *txn = &s->txn;
1513 struct chunk auth_method;
1514 struct hdr_ctx ctx;
1515 char *h, *p;
1516 int len;
1517
1518#ifdef DEBUG_AUTH
1519 printf("Auth for session %p: %d\n", s, txn->auth.method);
1520#endif
1521
1522 if (txn->auth.method == HTTP_AUTH_WRONG)
1523 return 0;
1524
1525 if (txn->auth.method)
1526 return 1;
1527
1528 txn->auth.method = HTTP_AUTH_WRONG;
1529
1530 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001531
1532 if (txn->flags & TX_USE_PX_CONN) {
1533 h = "Proxy-Authorization";
1534 len = strlen(h);
1535 } else {
1536 h = "Authorization";
1537 len = strlen(h);
1538 }
1539
1540 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001541 return 0;
1542
1543 h = ctx.line + ctx.val;
1544
1545 p = memchr(h, ' ', ctx.vlen);
1546 if (!p || p == h)
1547 return 0;
1548
1549 chunk_initlen(&auth_method, h, 0, p-h);
1550 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1551
1552 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1553
1554 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1555 get_http_auth_buff, BUFSIZE - 1);
1556
1557 if (len < 0)
1558 return 0;
1559
1560
1561 get_http_auth_buff[len] = '\0';
1562
1563 p = strchr(get_http_auth_buff, ':');
1564
1565 if (!p)
1566 return 0;
1567
1568 txn->auth.user = get_http_auth_buff;
1569 *p = '\0';
1570 txn->auth.pass = p+1;
1571
1572 txn->auth.method = HTTP_AUTH_BASIC;
1573 return 1;
1574 }
1575
1576 return 0;
1577}
1578
Willy Tarreau58f10d72006-12-04 02:26:12 +01001579
Willy Tarreau8973c702007-01-21 23:58:29 +01001580/*
1581 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001582 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001583 * when data are missing and recalled at the exact same location with no
1584 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001585 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001586 * fields. Note that msg->som and msg->sol will be initialized after completing
1587 * the first state, so that none of the msg pointers has to be initialized
1588 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001589 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1591{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001592 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001594
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001595 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 ptr = buf->lr;
1597 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001598
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 if (unlikely(ptr >= end))
1600 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001601
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001603 /*
1604 * First, states that are specific to the response only.
1605 * We check them first so that request and headers are
1606 * closer to each other (accessed more often).
1607 */
1608 http_msg_rpbefore:
1609 case HTTP_MSG_RPBEFORE:
1610 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001611 /* we have a start of message, but we have to check
1612 * first if we need to remove some CRLF. We can only
1613 * do this when send_max=0.
1614 */
1615 char *beg = buf->w + buf->send_max;
1616 if (beg >= buf->data + buf->size)
1617 beg -= buf->size;
1618 if (unlikely(ptr != beg)) {
1619 if (buf->send_max)
1620 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001621 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001622 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001623 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001624 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001625 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001626 hdr_idx_init(idx);
1627 state = HTTP_MSG_RPVER;
1628 goto http_msg_rpver;
1629 }
1630
1631 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1632 goto http_msg_invalid;
1633
1634 if (unlikely(*ptr == '\n'))
1635 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1636 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1637 /* stop here */
1638
1639 http_msg_rpbefore_cr:
1640 case HTTP_MSG_RPBEFORE_CR:
1641 EXPECT_LF_HERE(ptr, http_msg_invalid);
1642 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1643 /* stop here */
1644
1645 http_msg_rpver:
1646 case HTTP_MSG_RPVER:
1647 case HTTP_MSG_RPVER_SP:
1648 case HTTP_MSG_RPCODE:
1649 case HTTP_MSG_RPCODE_SP:
1650 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001651 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001652 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001653 if (unlikely(!ptr))
1654 return;
1655
1656 /* we have a full response and we know that we have either a CR
1657 * or an LF at <ptr>.
1658 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001659 //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 +01001660 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1661
1662 msg->sol = ptr;
1663 if (likely(*ptr == '\r'))
1664 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1665 goto http_msg_rpline_end;
1666
1667 http_msg_rpline_end:
1668 case HTTP_MSG_RPLINE_END:
1669 /* msg->sol must point to the first of CR or LF. */
1670 EXPECT_LF_HERE(ptr, http_msg_invalid);
1671 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1672 /* stop here */
1673
1674 /*
1675 * Second, states that are specific to the request only
1676 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001677 http_msg_rqbefore:
1678 case HTTP_MSG_RQBEFORE:
1679 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001680 /* we have a start of message, but we have to check
1681 * first if we need to remove some CRLF. We can only
1682 * do this when send_max=0.
1683 */
1684 char *beg = buf->w + buf->send_max;
1685 if (beg >= buf->data + buf->size)
1686 beg -= buf->size;
1687 if (likely(ptr != beg)) {
1688 if (buf->send_max)
1689 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001690 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001691 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001693 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001694 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001695 /* we will need this when keep-alive will be supported
1696 hdr_idx_init(idx);
1697 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001698 state = HTTP_MSG_RQMETH;
1699 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001701
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001702 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1703 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001704
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 if (unlikely(*ptr == '\n'))
1706 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1707 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001708 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001709
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 http_msg_rqbefore_cr:
1711 case HTTP_MSG_RQBEFORE_CR:
1712 EXPECT_LF_HERE(ptr, http_msg_invalid);
1713 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001714 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001715
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 http_msg_rqmeth:
1717 case HTTP_MSG_RQMETH:
1718 case HTTP_MSG_RQMETH_SP:
1719 case HTTP_MSG_RQURI:
1720 case HTTP_MSG_RQURI_SP:
1721 case HTTP_MSG_RQVER:
1722 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001723 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 if (unlikely(!ptr))
1725 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001726
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 /* we have a full request and we know that we have either a CR
1728 * or an LF at <ptr>.
1729 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001730 //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 +01001731 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001732
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 msg->sol = ptr;
1734 if (likely(*ptr == '\r'))
1735 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001736 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001737
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 http_msg_rqline_end:
1739 case HTTP_MSG_RQLINE_END:
1740 /* check for HTTP/0.9 request : no version information available.
1741 * msg->sol must point to the first of CR or LF.
1742 */
1743 if (unlikely(msg->sl.rq.v_l == 0))
1744 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001745
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 EXPECT_LF_HERE(ptr, http_msg_invalid);
1747 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001748 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001749
Willy Tarreau8973c702007-01-21 23:58:29 +01001750 /*
1751 * Common states below
1752 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 http_msg_hdr_first:
1754 case HTTP_MSG_HDR_FIRST:
1755 msg->sol = ptr;
1756 if (likely(!HTTP_IS_CRLF(*ptr))) {
1757 goto http_msg_hdr_name;
1758 }
1759
1760 if (likely(*ptr == '\r'))
1761 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1762 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001763
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001764 http_msg_hdr_name:
1765 case HTTP_MSG_HDR_NAME:
1766 /* assumes msg->sol points to the first char */
1767 if (likely(HTTP_IS_TOKEN(*ptr)))
1768 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001769
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001770 if (likely(*ptr == ':')) {
1771 msg->col = ptr - buf->data;
1772 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1773 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001774
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001775 if (likely(msg->err_pos < -1) || *ptr == '\n')
1776 goto http_msg_invalid;
1777
1778 if (msg->err_pos == -1) /* capture error pointer */
1779 msg->err_pos = ptr - buf->data; /* >= 0 now */
1780
1781 /* and we still accept this non-token character */
1782 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001783
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 http_msg_hdr_l1_sp:
1785 case HTTP_MSG_HDR_L1_SP:
1786 /* assumes msg->sol points to the first char and msg->col to the colon */
1787 if (likely(HTTP_IS_SPHT(*ptr)))
1788 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001789
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 /* header value can be basically anything except CR/LF */
1791 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001792
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 if (likely(!HTTP_IS_CRLF(*ptr))) {
1794 goto http_msg_hdr_val;
1795 }
1796
1797 if (likely(*ptr == '\r'))
1798 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1799 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001800
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001801 http_msg_hdr_l1_lf:
1802 case HTTP_MSG_HDR_L1_LF:
1803 EXPECT_LF_HERE(ptr, http_msg_invalid);
1804 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001805
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 http_msg_hdr_l1_lws:
1807 case HTTP_MSG_HDR_L1_LWS:
1808 if (likely(HTTP_IS_SPHT(*ptr))) {
1809 /* replace HT,CR,LF with spaces */
1810 for (; buf->data+msg->sov < ptr; msg->sov++)
1811 buf->data[msg->sov] = ' ';
1812 goto http_msg_hdr_l1_sp;
1813 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001814 /* we had a header consisting only in spaces ! */
1815 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001816 goto http_msg_complete_header;
1817
1818 http_msg_hdr_val:
1819 case HTTP_MSG_HDR_VAL:
1820 /* assumes msg->sol points to the first char, msg->col to the
1821 * colon, and msg->sov points to the first character of the
1822 * value.
1823 */
1824 if (likely(!HTTP_IS_CRLF(*ptr)))
1825 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001826
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001827 msg->eol = ptr;
1828 /* Note: we could also copy eol into ->eoh so that we have the
1829 * real header end in case it ends with lots of LWS, but is this
1830 * really needed ?
1831 */
1832 if (likely(*ptr == '\r'))
1833 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1834 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001835
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001836 http_msg_hdr_l2_lf:
1837 case HTTP_MSG_HDR_L2_LF:
1838 EXPECT_LF_HERE(ptr, http_msg_invalid);
1839 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001840
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001841 http_msg_hdr_l2_lws:
1842 case HTTP_MSG_HDR_L2_LWS:
1843 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1844 /* LWS: replace HT,CR,LF with spaces */
1845 for (; msg->eol < ptr; msg->eol++)
1846 *msg->eol = ' ';
1847 goto http_msg_hdr_val;
1848 }
1849 http_msg_complete_header:
1850 /*
1851 * It was a new header, so the last one is finished.
1852 * Assumes msg->sol points to the first char, msg->col to the
1853 * colon, msg->sov points to the first character of the value
1854 * and msg->eol to the first CR or LF so we know how the line
1855 * ends. We insert last header into the index.
1856 */
1857 /*
1858 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1859 write(2, msg->sol, msg->eol-msg->sol);
1860 fprintf(stderr,"\n");
1861 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001862
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001863 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1864 idx, idx->tail) < 0))
1865 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001866
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 msg->sol = ptr;
1868 if (likely(!HTTP_IS_CRLF(*ptr))) {
1869 goto http_msg_hdr_name;
1870 }
1871
1872 if (likely(*ptr == '\r'))
1873 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1874 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001875
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001876 http_msg_last_lf:
1877 case HTTP_MSG_LAST_LF:
1878 /* Assumes msg->sol points to the first of either CR or LF */
1879 EXPECT_LF_HERE(ptr, http_msg_invalid);
1880 ptr++;
1881 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001882 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001884 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001885 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001886 return;
1887#ifdef DEBUG_FULL
1888 default:
1889 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1890 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001891#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 }
1893 http_msg_ood:
1894 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001895 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 buf->lr = ptr;
1897 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001898
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001899 http_msg_invalid:
1900 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001901 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001902 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001903 return;
1904}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001905
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001906/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1907 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1908 * nothing is done and 1 is returned.
1909 */
1910static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1911{
1912 int delta;
1913 char *cur_end;
1914
1915 if (msg->sl.rq.v_l != 0)
1916 return 1;
1917
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001918 cur_end = msg->sol + msg->sl.rq.l;
1919 delta = 0;
1920
1921 if (msg->sl.rq.u_l == 0) {
1922 /* if no URI was set, add "/" */
1923 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1924 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001925 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001926 }
1927 /* add HTTP version */
1928 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001929 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001930 cur_end += delta;
1931 cur_end = (char *)http_parse_reqline(msg, req->data,
1932 HTTP_MSG_RQMETH,
1933 msg->sol, cur_end + 1,
1934 NULL, NULL);
1935 if (unlikely(!cur_end))
1936 return 0;
1937
1938 /* we have a full HTTP/1.0 request now and we know that
1939 * we have either a CR or an LF at <ptr>.
1940 */
1941 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1942 return 1;
1943}
1944
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001945/* Parse the Connection: header of an HTTP request, looking for both "close"
1946 * and "keep-alive" values. If a buffer is provided and we already know that
1947 * some headers may safely be removed, we remove them now. The <to_del> flags
1948 * are used for that :
1949 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1950 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1951 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1952 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1953 * harmless combinations may be removed. Do not call that after changes have
1954 * been processed. If unused, the buffer can be NULL, and no data will be
1955 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001956 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001957void 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 +01001958{
Willy Tarreau5b154472009-12-21 20:11:07 +01001959 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001960 const char *hdr_val = "Connection";
1961 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001962
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001963 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001964 return;
1965
Willy Tarreau88d349d2010-01-25 12:15:43 +01001966 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1967 hdr_val = "Proxy-Connection";
1968 hdr_len = 16;
1969 }
1970
Willy Tarreau5b154472009-12-21 20:11:07 +01001971 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001972 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001973 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001974 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1975 txn->flags |= TX_HDR_CONN_KAL;
1976 if ((to_del & 2) && buf)
1977 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1978 else
1979 txn->flags |= TX_CON_KAL_SET;
1980 }
1981 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1982 txn->flags |= TX_HDR_CONN_CLO;
1983 if ((to_del & 1) && buf)
1984 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1985 else
1986 txn->flags |= TX_CON_CLO_SET;
1987 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001988 }
1989
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001990 txn->flags |= TX_HDR_CONN_PRS;
1991 return;
1992}
Willy Tarreau5b154472009-12-21 20:11:07 +01001993
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001994/* Apply desired changes on the Connection: header. Values may be removed and/or
1995 * added depending on the <wanted> flags, which are exclusively composed of
1996 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1997 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1998 */
1999void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
2000{
2001 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002002 const char *hdr_val = "Connection";
2003 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002004
2005 ctx.idx = 0;
2006
Willy Tarreau88d349d2010-01-25 12:15:43 +01002007
2008 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2009 hdr_val = "Proxy-Connection";
2010 hdr_len = 16;
2011 }
2012
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002013 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002014 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002015 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2016 if (wanted & TX_CON_KAL_SET)
2017 txn->flags |= TX_CON_KAL_SET;
2018 else
2019 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002020 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002021 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2022 if (wanted & TX_CON_CLO_SET)
2023 txn->flags |= TX_CON_CLO_SET;
2024 else
2025 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002026 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002027 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002028
2029 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2030 return;
2031
2032 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2033 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002034 hdr_val = "Connection: close";
2035 hdr_len = 17;
2036 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2037 hdr_val = "Proxy-Connection: close";
2038 hdr_len = 23;
2039 }
2040 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002041 }
2042
2043 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2044 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002045 hdr_val = "Connection: keep-alive";
2046 hdr_len = 22;
2047 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2048 hdr_val = "Proxy-Connection: keep-alive";
2049 hdr_len = 28;
2050 }
2051 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002052 }
2053 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002054}
2055
Willy Tarreaud98cf932009-12-27 22:54:55 +01002056/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2057 * first byte of body, and increments msg->sov by the number of bytes parsed,
2058 * so that we know we can forward between ->som and ->sov. Note that due to
2059 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2060 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002061 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002062 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002063 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002064int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002065{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002066 char *ptr = buf->lr;
2067 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002068 unsigned int chunk = 0;
2069
2070 /* The chunk size is in the following form, though we are only
2071 * interested in the size and CRLF :
2072 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2073 */
2074 while (1) {
2075 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002076 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002077 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002078 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002079 if (c < 0) /* not a hex digit anymore */
2080 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002081 if (++ptr >= end)
2082 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002083 if (chunk & 0xF000000) /* overflow will occur */
2084 return -1;
2085 chunk = (chunk << 4) + c;
2086 }
2087
Willy Tarreaud98cf932009-12-27 22:54:55 +01002088 /* empty size not allowed */
2089 if (ptr == buf->lr)
2090 return -1;
2091
2092 while (http_is_spht[(unsigned char)*ptr]) {
2093 if (++ptr >= end)
2094 ptr = buf->data;
2095 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002096 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002097 }
2098
Willy Tarreaud98cf932009-12-27 22:54:55 +01002099 /* Up to there, we know that at least one byte is present at *ptr. Check
2100 * for the end of chunk size.
2101 */
2102 while (1) {
2103 if (likely(HTTP_IS_CRLF(*ptr))) {
2104 /* we now have a CR or an LF at ptr */
2105 if (likely(*ptr == '\r')) {
2106 if (++ptr >= end)
2107 ptr = buf->data;
2108 if (ptr == buf->r)
2109 return 0;
2110 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002111
Willy Tarreaud98cf932009-12-27 22:54:55 +01002112 if (*ptr != '\n')
2113 return -1;
2114 if (++ptr >= end)
2115 ptr = buf->data;
2116 /* done */
2117 break;
2118 }
2119 else if (*ptr == ';') {
2120 /* chunk extension, ends at next CRLF */
2121 if (++ptr >= end)
2122 ptr = buf->data;
2123 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002124 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002125
2126 while (!HTTP_IS_CRLF(*ptr)) {
2127 if (++ptr >= end)
2128 ptr = buf->data;
2129 if (ptr == buf->r)
2130 return 0;
2131 }
2132 /* we have a CRLF now, loop above */
2133 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002134 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002135 else
Willy Tarreau115acb92009-12-26 13:56:06 +01002136 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002137 }
2138
Willy Tarreaud98cf932009-12-27 22:54:55 +01002139 /* OK we found our CRLF and now <ptr> points to the next byte,
2140 * which may or may not be present. We save that into ->lr and
2141 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002142 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002143 msg->sov += ptr - buf->lr;
2144 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01002145 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002146 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002147 return 1;
2148}
2149
Willy Tarreaud98cf932009-12-27 22:54:55 +01002150/* This function skips trailers in the buffer <buf> associated with HTTP
2151 * message <msg>. The first visited position is buf->lr. If the end of
2152 * the trailers is found, it is automatically scheduled to be forwarded,
2153 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2154 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002155 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2156 * zero. If a parse error is encountered, the function returns < 0 and does not
2157 * change anything except maybe buf->lr and msg->sov. Note that the message
2158 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2159 * which implies that all non-trailers data have already been scheduled for
2160 * forwarding, and that the difference between msg->som and msg->sov exactly
2161 * matches the length of trailers already parsed and not forwarded. It is also
2162 * important to note that this function is designed to be able to parse wrapped
2163 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002164 */
2165int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2166{
2167 /* we have buf->lr which points to next line. Look for CRLF. */
2168 while (1) {
2169 char *p1 = NULL, *p2 = NULL;
2170 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002171 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002172
2173 /* scan current line and stop at LF or CRLF */
2174 while (1) {
2175 if (ptr == buf->r)
2176 return 0;
2177
2178 if (*ptr == '\n') {
2179 if (!p1)
2180 p1 = ptr;
2181 p2 = ptr;
2182 break;
2183 }
2184
2185 if (*ptr == '\r') {
2186 if (p1)
2187 return -1;
2188 p1 = ptr;
2189 }
2190
2191 ptr++;
2192 if (ptr >= buf->data + buf->size)
2193 ptr = buf->data;
2194 }
2195
2196 /* after LF; point to beginning of next line */
2197 p2++;
2198 if (p2 >= buf->data + buf->size)
2199 p2 = buf->data;
2200
Willy Tarreau638cd022010-01-03 07:42:04 +01002201 bytes = p2 - buf->lr;
2202 if (bytes < 0)
2203 bytes += buf->size;
2204
2205 /* schedule this line for forwarding */
2206 msg->sov += bytes;
2207 if (msg->sov >= buf->size)
2208 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002209
Willy Tarreau638cd022010-01-03 07:42:04 +01002210 if (p1 == buf->lr) {
2211 /* LF/CRLF at beginning of line => end of trailers at p2.
2212 * Everything was scheduled for forwarding, there's nothing
2213 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002214 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002215 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002216 msg->msg_state = HTTP_MSG_DONE;
2217 return 1;
2218 }
2219 /* OK, next line then */
2220 buf->lr = p2;
2221 }
2222}
2223
2224/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2225 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2226 * ->som, buf->lr in order to include this part into the next forwarding phase.
2227 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2228 * not enough data are available, the function does not change anything and
2229 * returns zero. If a parse error is encountered, the function returns < 0 and
2230 * does not change anything. Note: this function is designed to parse wrapped
2231 * CRLF at the end of the buffer.
2232 */
2233int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2234{
2235 char *ptr;
2236 int bytes;
2237
2238 /* NB: we'll check data availabilty at the end. It's not a
2239 * problem because whatever we match first will be checked
2240 * against the correct length.
2241 */
2242 bytes = 1;
2243 ptr = buf->lr;
2244 if (*ptr == '\r') {
2245 bytes++;
2246 ptr++;
2247 if (ptr >= buf->data + buf->size)
2248 ptr = buf->data;
2249 }
2250
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002251 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002252 return 0;
2253
2254 if (*ptr != '\n')
2255 return -1;
2256
2257 ptr++;
2258 if (ptr >= buf->data + buf->size)
2259 ptr = buf->data;
2260 buf->lr = ptr;
2261 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2262 msg->sov = ptr - buf->data;
2263 msg->som = msg->sov - bytes;
2264 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2265 return 1;
2266}
Willy Tarreau5b154472009-12-21 20:11:07 +01002267
Willy Tarreau83e3af02009-12-28 17:39:57 +01002268void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2269{
2270 char *end = buf->data + buf->size;
2271 int off = buf->data + buf->size - buf->w;
2272
2273 /* two possible cases :
2274 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002275 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002276 */
2277 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002278 int block1 = buf->l;
2279 int block2 = 0;
2280 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002281 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002282 block1 = buf->data + buf->size - buf->w;
2283 block2 = buf->r - buf->data;
2284 }
2285 if (block2)
2286 memcpy(swap_buffer, buf->data, block2);
2287 memmove(buf->data, buf->w, block1);
2288 if (block2)
2289 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002290 }
2291
2292 /* adjust all known pointers */
2293 buf->w = buf->data;
2294 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2295 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2296 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2297 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2298
2299 /* adjust relative pointers */
2300 msg->som = 0;
2301 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2302 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2303 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2304
Willy Tarreau83e3af02009-12-28 17:39:57 +01002305 if (msg->err_pos >= 0) {
2306 msg->err_pos += off;
2307 if (msg->err_pos >= buf->size)
2308 msg->err_pos -= buf->size;
2309 }
2310
2311 buf->flags &= ~BF_FULL;
2312 if (buf->l >= buffer_max_len(buf))
2313 buf->flags |= BF_FULL;
2314}
2315
Willy Tarreaud787e662009-07-07 10:14:51 +02002316/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2317 * processing can continue on next analysers, or zero if it either needs more
2318 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2319 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2320 * when it has nothing left to do, and may remove any analyser when it wants to
2321 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002322 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002323int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002324{
Willy Tarreau59234e92008-11-30 23:51:27 +01002325 /*
2326 * We will parse the partial (or complete) lines.
2327 * We will check the request syntax, and also join multi-line
2328 * headers. An index of all the lines will be elaborated while
2329 * parsing.
2330 *
2331 * For the parsing, we use a 28 states FSM.
2332 *
2333 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002334 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002335 * req->data + msg->eoh = end of processed headers / start of current one
2336 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002337 * req->lr = first non-visited byte
2338 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002339 *
2340 * At end of parsing, we may perform a capture of the error (if any), and
2341 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2342 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2343 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002345
Willy Tarreau59234e92008-11-30 23:51:27 +01002346 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002347 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 struct http_txn *txn = &s->txn;
2349 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002350 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002351
Willy Tarreau6bf17362009-02-24 10:48:35 +01002352 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2353 now_ms, __FUNCTION__,
2354 s,
2355 req,
2356 req->rex, req->wex,
2357 req->flags,
2358 req->l,
2359 req->analysers);
2360
Willy Tarreau52a0c602009-08-16 22:45:38 +02002361 /* we're speaking HTTP here, so let's speak HTTP to the client */
2362 s->srv_error = http_return_srv_error;
2363
Willy Tarreau83e3af02009-12-28 17:39:57 +01002364 /* There's a protected area at the end of the buffer for rewriting
2365 * purposes. We don't want to start to parse the request if the
2366 * protected area is affected, because we may have to move processed
2367 * data later, which is much more complicated.
2368 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002369 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002370 if ((txn->flags & TX_NOT_FIRST) &&
2371 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002372 req->r < req->lr ||
2373 req->r > req->data + req->size - global.tune.maxrewrite)) {
2374 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002375 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2376 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002377 /* some data has still not left the buffer, wake us once that's done */
2378 buffer_dont_connect(req);
2379 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2380 return 0;
2381 }
2382 if (req->l <= req->size - global.tune.maxrewrite)
2383 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002384 }
2385
Willy Tarreau065e8332010-01-08 00:30:20 +01002386 /* Note that we have the same problem with the response ; we
2387 * may want to send a redirect, error or anything which requires
2388 * some spare space. So we'll ensure that we have at least
2389 * maxrewrite bytes available in the response buffer before
2390 * processing that one. This will only affect pipelined
2391 * keep-alive requests.
2392 */
2393 if ((txn->flags & TX_NOT_FIRST) &&
2394 unlikely((s->rep->flags & BF_FULL) ||
2395 s->rep->r < s->rep->lr ||
2396 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2397 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002398 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2399 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002400 /* don't let a connection request be initiated */
2401 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002402 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau065e8332010-01-08 00:30:20 +01002403 return 0;
2404 }
2405 }
2406
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002407 if (likely(req->lr < req->r))
2408 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002409 }
2410
Willy Tarreau59234e92008-11-30 23:51:27 +01002411 /* 1: we might have to print this header in debug mode */
2412 if (unlikely((global.mode & MODE_DEBUG) &&
2413 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002414 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002415 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002416 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002417
Willy Tarreau663308b2010-06-07 14:06:08 +02002418 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002419 eol = sol + msg->sl.rq.l;
2420 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002421
Willy Tarreau59234e92008-11-30 23:51:27 +01002422 sol += hdr_idx_first_pos(&txn->hdr_idx);
2423 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002424
Willy Tarreau59234e92008-11-30 23:51:27 +01002425 while (cur_idx) {
2426 eol = sol + txn->hdr_idx.v[cur_idx].len;
2427 debug_hdr("clihdr", s, sol, eol);
2428 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2429 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002430 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002431 }
2432
Willy Tarreau58f10d72006-12-04 02:26:12 +01002433
Willy Tarreau59234e92008-11-30 23:51:27 +01002434 /*
2435 * Now we quickly check if we have found a full valid request.
2436 * If not so, we check the FD and buffer states before leaving.
2437 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002438 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002439 * requests are checked first. When waiting for a second request
2440 * on a keep-alive session, if we encounter and error, close, t/o,
2441 * we note the error in the session flags but don't set any state.
2442 * Since the error will be noted there, it will not be counted by
2443 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002444 * Last, we may increase some tracked counters' http request errors on
2445 * the cases that are deliberately the client's fault. For instance,
2446 * a timeout or connection reset is not counted as an error. However
2447 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002448 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002449
Willy Tarreau655dce92009-11-08 13:10:58 +01002450 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002451 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002452 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002453 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002454 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002455 session_inc_http_req_ctr(s);
2456 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002457 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002458 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002459 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002460
Willy Tarreau59234e92008-11-30 23:51:27 +01002461 /* 1: Since we are in header mode, if there's no space
2462 * left for headers, we won't be able to free more
2463 * later, so the session will never terminate. We
2464 * must terminate it now.
2465 */
2466 if (unlikely(req->flags & BF_FULL)) {
2467 /* FIXME: check if URI is set and return Status
2468 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002469 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002470 session_inc_http_req_ctr(s);
2471 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002472 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002473 goto return_bad_req;
2474 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002475
Willy Tarreau59234e92008-11-30 23:51:27 +01002476 /* 2: have we encountered a read error ? */
2477 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002478 if (!(s->flags & SN_ERR_MASK))
2479 s->flags |= SN_ERR_CLICL;
2480
Willy Tarreaufcffa692010-01-10 14:21:19 +01002481 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002482 goto failed_keep_alive;
2483
Willy Tarreau59234e92008-11-30 23:51:27 +01002484 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002485 if (msg->err_pos >= 0) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002486 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002487 session_inc_http_err_ctr(s);
2488 }
2489
Willy Tarreau59234e92008-11-30 23:51:27 +01002490 msg->msg_state = HTTP_MSG_ERROR;
2491 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002492
Willy Tarreauda7ff642010-06-23 11:44:09 +02002493 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002494 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002495 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002496 if (s->listener->counters)
2497 s->listener->counters->failed_req++;
2498
Willy Tarreau59234e92008-11-30 23:51:27 +01002499 if (!(s->flags & SN_FINST_MASK))
2500 s->flags |= SN_FINST_R;
2501 return 0;
2502 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002503
Willy Tarreau59234e92008-11-30 23:51:27 +01002504 /* 3: has the read timeout expired ? */
2505 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002506 if (!(s->flags & SN_ERR_MASK))
2507 s->flags |= SN_ERR_CLITO;
2508
Willy Tarreaufcffa692010-01-10 14:21:19 +01002509 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002510 goto failed_keep_alive;
2511
Willy Tarreau59234e92008-11-30 23:51:27 +01002512 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002513 if (msg->err_pos >= 0) {
Willy Tarreau4076a152009-04-02 15:18:36 +02002514 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002515 session_inc_http_err_ctr(s);
2516 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002517 txn->status = 408;
2518 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2519 msg->msg_state = HTTP_MSG_ERROR;
2520 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002521
Willy Tarreauda7ff642010-06-23 11:44:09 +02002522 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002523 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002524 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002525 if (s->listener->counters)
2526 s->listener->counters->failed_req++;
2527
Willy Tarreau59234e92008-11-30 23:51:27 +01002528 if (!(s->flags & SN_FINST_MASK))
2529 s->flags |= SN_FINST_R;
2530 return 0;
2531 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002532
Willy Tarreau59234e92008-11-30 23:51:27 +01002533 /* 4: have we encountered a close ? */
2534 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002535 if (!(s->flags & SN_ERR_MASK))
2536 s->flags |= SN_ERR_CLICL;
2537
Willy Tarreaufcffa692010-01-10 14:21:19 +01002538 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002539 goto failed_keep_alive;
2540
Willy Tarreau4076a152009-04-02 15:18:36 +02002541 if (msg->err_pos >= 0)
2542 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002543 txn->status = 400;
2544 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2545 msg->msg_state = HTTP_MSG_ERROR;
2546 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002547
Willy Tarreauda7ff642010-06-23 11:44:09 +02002548 session_inc_http_err_ctr(s);
2549 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002550 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002551 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002552 if (s->listener->counters)
2553 s->listener->counters->failed_req++;
2554
Willy Tarreau59234e92008-11-30 23:51:27 +01002555 if (!(s->flags & SN_FINST_MASK))
2556 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002557 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002558 }
2559
Willy Tarreau520d95e2009-09-19 21:04:57 +02002560 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002561 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002562 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002563
Willy Tarreaufcffa692010-01-10 14:21:19 +01002564 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2565 /* If the client starts to talk, let's fall back to
2566 * request timeout processing.
2567 */
2568 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002569 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002570 }
2571
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002573 if (!tick_isset(req->analyse_exp)) {
2574 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2575 (txn->flags & TX_WAIT_NEXT_RQ) &&
2576 tick_isset(s->be->timeout.httpka))
2577 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2578 else
2579 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2580 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002581
Willy Tarreau59234e92008-11-30 23:51:27 +01002582 /* we're not ready yet */
2583 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002584
2585 failed_keep_alive:
2586 /* Here we process low-level errors for keep-alive requests. In
2587 * short, if the request is not the first one and it experiences
2588 * a timeout, read error or shutdown, we just silently close so
2589 * that the client can try again.
2590 */
2591 txn->status = 0;
2592 msg->msg_state = HTTP_MSG_RQBEFORE;
2593 req->analysers = 0;
2594 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002595 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002596 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002597 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002598 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002599
Willy Tarreaud787e662009-07-07 10:14:51 +02002600 /* OK now we have a complete HTTP request with indexed headers. Let's
2601 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002602 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2603 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2604 * points to the CRLF of the request line. req->lr points to the first
2605 * byte after the last LF. msg->col and msg->sov point to the first
2606 * byte of data. msg->eol cannot be trusted because it may have been
2607 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002608 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002609
Willy Tarreauda7ff642010-06-23 11:44:09 +02002610 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002611 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2612
Willy Tarreaub16a5742010-01-10 14:46:16 +01002613 if (txn->flags & TX_WAIT_NEXT_RQ) {
2614 /* kill the pending keep-alive timeout */
2615 txn->flags &= ~TX_WAIT_NEXT_RQ;
2616 req->analyse_exp = TICK_ETERNITY;
2617 }
2618
2619
Willy Tarreaud787e662009-07-07 10:14:51 +02002620 /* Maybe we found in invalid header name while we were configured not
2621 * to block on that, so we have to capture it now.
2622 */
2623 if (unlikely(msg->err_pos >= 0))
Willy Tarreau4076a152009-04-02 15:18:36 +02002624 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2625
Willy Tarreau59234e92008-11-30 23:51:27 +01002626 /*
2627 * 1: identify the method
2628 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002629 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002630
2631 /* we can make use of server redirect on GET and HEAD */
2632 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2633 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002634
Willy Tarreau59234e92008-11-30 23:51:27 +01002635 /*
2636 * 2: check if the URI matches the monitor_uri.
2637 * We have to do this for every request which gets in, because
2638 * the monitor-uri is defined by the frontend.
2639 */
2640 if (unlikely((s->fe->monitor_uri_len != 0) &&
2641 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002642 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002643 s->fe->monitor_uri,
2644 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002645 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002646 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002647 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002649
Willy Tarreau59234e92008-11-30 23:51:27 +01002650 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002651
Willy Tarreau59234e92008-11-30 23:51:27 +01002652 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002653 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2654 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002655
Willy Tarreau59234e92008-11-30 23:51:27 +01002656 ret = acl_pass(ret);
2657 if (cond->pol == ACL_COND_UNLESS)
2658 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002659
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 if (ret) {
2661 /* we fail this request, let's return 503 service unavail */
2662 txn->status = 503;
2663 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2664 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002665 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002666 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002667
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 /* nothing to fail, let's reply normaly */
2669 txn->status = 200;
2670 stream_int_retnclose(req->prod, &http_200_chunk);
2671 goto return_prx_cond;
2672 }
2673
2674 /*
2675 * 3: Maybe we have to copy the original REQURI for the logs ?
2676 * Note: we cannot log anymore if the request has been
2677 * classified as invalid.
2678 */
2679 if (unlikely(s->logs.logwait & LW_REQ)) {
2680 /* we have a complete HTTP request that we must log */
2681 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2682 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002683
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 if (urilen >= REQURI_LEN)
2685 urilen = REQURI_LEN - 1;
2686 memcpy(txn->uri, &req->data[msg->som], urilen);
2687 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002688
Willy Tarreau59234e92008-11-30 23:51:27 +01002689 if (!(s->logs.logwait &= ~LW_REQ))
2690 s->do_log(s);
2691 } else {
2692 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002693 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002694 }
Willy Tarreau06619262006-12-17 08:37:22 +01002695
Willy Tarreau59234e92008-11-30 23:51:27 +01002696 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002697 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2698 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002699
Willy Tarreau5b154472009-12-21 20:11:07 +01002700 /* ... and check if the request is HTTP/1.1 or above */
2701 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002702 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2703 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2704 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002705 txn->flags |= TX_REQ_VER_11;
2706
2707 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002708 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002709
Willy Tarreau88d349d2010-01-25 12:15:43 +01002710 /* if the frontend has "option http-use-proxy-header", we'll check if
2711 * we have what looks like a proxied connection instead of a connection,
2712 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2713 * Note that this is *not* RFC-compliant, however browsers and proxies
2714 * happen to do that despite being non-standard :-(
2715 * We consider that a request not beginning with either '/' or '*' is
2716 * a proxied connection, which covers both "scheme://location" and
2717 * CONNECT ip:port.
2718 */
2719 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2720 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2721 txn->flags |= TX_USE_PX_CONN;
2722
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002723 /* transfer length unknown*/
2724 txn->flags &= ~TX_REQ_XFER_LEN;
2725
Willy Tarreau59234e92008-11-30 23:51:27 +01002726 /* 5: we may need to capture headers */
2727 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002728 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002729 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002730
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002731 /* 6: determine the transfer-length.
2732 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2733 * the presence of a message-body in a REQUEST and its transfer length
2734 * must be determined that way (in order of precedence) :
2735 * 1. The presence of a message-body in a request is signaled by the
2736 * inclusion of a Content-Length or Transfer-Encoding header field
2737 * in the request's header fields. When a request message contains
2738 * both a message-body of non-zero length and a method that does
2739 * not define any semantics for that request message-body, then an
2740 * origin server SHOULD either ignore the message-body or respond
2741 * with an appropriate error message (e.g., 413). A proxy or
2742 * gateway, when presented the same request, SHOULD either forward
2743 * the request inbound with the message- body or ignore the
2744 * message-body when determining a response.
2745 *
2746 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2747 * and the "chunked" transfer-coding (Section 6.2) is used, the
2748 * transfer-length is defined by the use of this transfer-coding.
2749 * If a Transfer-Encoding header field is present and the "chunked"
2750 * transfer-coding is not present, the transfer-length is defined
2751 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002752 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002753 * 3. If a Content-Length header field is present, its decimal value in
2754 * OCTETs represents both the entity-length and the transfer-length.
2755 * If a message is received with both a Transfer-Encoding header
2756 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002757 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002758 * 4. By the server closing the connection. (Closing the connection
2759 * cannot be used to indicate the end of a request body, since that
2760 * would leave no possibility for the server to send back a response.)
2761 *
2762 * Whenever a transfer-coding is applied to a message-body, the set of
2763 * transfer-codings MUST include "chunked", unless the message indicates
2764 * it is terminated by closing the connection. When the "chunked"
2765 * transfer-coding is used, it MUST be the last transfer-coding applied
2766 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002767 */
2768
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002769 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002770 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002771 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002772 while ((txn->flags & TX_REQ_VER_11) &&
2773 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002774 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2775 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2776 else if (txn->flags & TX_REQ_TE_CHNK) {
2777 /* bad transfer-encoding (chunked followed by something else) */
2778 use_close_only = 1;
2779 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2780 break;
2781 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002782 }
2783
Willy Tarreau32b47f42009-10-18 20:55:02 +02002784 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002785 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002786 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2787 signed long long cl;
2788
2789 if (!ctx.vlen)
2790 goto return_bad_req;
2791
2792 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2793 goto return_bad_req; /* parse failure */
2794
2795 if (cl < 0)
2796 goto return_bad_req;
2797
2798 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2799 goto return_bad_req; /* already specified, was different */
2800
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002801 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002802 msg->hdr_content_len = cl;
2803 }
2804
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002805 /* bodyless requests have a known length */
2806 if (!use_close_only)
2807 txn->flags |= TX_REQ_XFER_LEN;
2808
Willy Tarreaud787e662009-07-07 10:14:51 +02002809 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002810 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002811 req->analyse_exp = TICK_ETERNITY;
2812 return 1;
2813
2814 return_bad_req:
2815 /* We centralize bad requests processing here */
2816 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2817 /* we detected a parsing error. We want to archive this request
2818 * in the dedicated proxy area for later troubleshooting.
2819 */
2820 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
2821 }
2822
2823 txn->req.msg_state = HTTP_MSG_ERROR;
2824 txn->status = 400;
2825 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002826
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002827 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002828 if (s->listener->counters)
2829 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002830
2831 return_prx_cond:
2832 if (!(s->flags & SN_ERR_MASK))
2833 s->flags |= SN_ERR_PRXCOND;
2834 if (!(s->flags & SN_FINST_MASK))
2835 s->flags |= SN_FINST_R;
2836
2837 req->analysers = 0;
2838 req->analyse_exp = TICK_ETERNITY;
2839 return 0;
2840}
2841
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002842/* This stream analyser runs all HTTP request processing which is common to
2843 * frontends and backends, which means blocking ACLs, filters, connection-close,
2844 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002845 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002846 * either needs more data or wants to immediately abort the request (eg: deny,
2847 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002848 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002849int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002850{
Willy Tarreaud787e662009-07-07 10:14:51 +02002851 struct http_txn *txn = &s->txn;
2852 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002853 struct acl_cond *cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002854 struct req_acl_rule *req_acl, *req_acl_final = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002855 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002856 struct cond_wordlist *wl;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002857 int del_ka, del_cl, do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002858
Willy Tarreau655dce92009-11-08 13:10:58 +01002859 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002860 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002861 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002862 return 0;
2863 }
2864
Willy Tarreau3a816292009-07-07 10:55:49 +02002865 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002866 req->analyse_exp = TICK_ETERNITY;
2867
2868 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2869 now_ms, __FUNCTION__,
2870 s,
2871 req,
2872 req->rex, req->wex,
2873 req->flags,
2874 req->l,
2875 req->analysers);
2876
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002877 /* first check whether we have some ACLs set to block this request */
2878 list_for_each_entry(cond, &px->block_cond, list) {
2879 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002880
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002881 ret = acl_pass(ret);
2882 if (cond->pol == ACL_COND_UNLESS)
2883 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002884
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002885 if (ret) {
2886 txn->status = 403;
2887 /* let's log the request time */
2888 s->logs.tv_request = now;
2889 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002890 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002891 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002892 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002893 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002894
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002895 do_stats = stats_check_uri(s, px);
2896
2897 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 +01002898 int ret = 1;
2899
2900 if (req_acl->action >= PR_REQ_ACL_ACT_MAX)
2901 continue;
2902
2903 /* check condition, but only if attached */
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01002904 if (req_acl->cond) {
2905 ret = acl_exec_cond(req_acl->cond, px, s, txn, ACL_DIR_REQ);
2906 ret = acl_pass(ret);
Willy Tarreau51425942010-02-01 10:40:19 +01002907
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01002908 if (req_acl->cond->pol == ACL_COND_UNLESS)
2909 ret = !ret;
2910 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002911
2912 if (ret) {
2913 req_acl_final = req_acl;
2914 break;
2915 }
2916 }
2917
2918 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_DENY) {
2919 txn->status = 403;
2920 s->logs.tv_request = now;
2921 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002922 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002923 goto return_prx_cond;
2924 }
2925
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002926 /* try headers filters */
2927 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002928 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002929 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002930
Willy Tarreau59234e92008-11-30 23:51:27 +01002931 /* has the request been denied ? */
2932 if (txn->flags & TX_CLDENY) {
2933 /* no need to go further */
2934 txn->status = 403;
2935 /* let's log the request time */
2936 s->logs.tv_request = now;
2937 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002938 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002939 goto return_prx_cond;
2940 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002941
2942 /* When a connection is tarpitted, we use the tarpit timeout,
2943 * which may be the same as the connect timeout if unspecified.
2944 * If unset, then set it to zero because we really want it to
2945 * eventually expire. We build the tarpit as an analyser.
2946 */
2947 if (txn->flags & TX_CLTARPIT) {
2948 buffer_erase(s->req);
2949 /* wipe the request out so that we can drop the connection early
2950 * if the client closes first.
2951 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002952 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002953 req->analysers = 0; /* remove switching rules etc... */
2954 req->analysers |= AN_REQ_HTTP_TARPIT;
2955 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2956 if (!req->analyse_exp)
2957 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002958 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002959 return 1;
2960 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002961 }
Willy Tarreau06619262006-12-17 08:37:22 +01002962
Willy Tarreau5b154472009-12-21 20:11:07 +01002963 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2964 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002965 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2966 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002967 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2968 * avoid to redo the same work if FE and BE have the same settings (common).
2969 * The method consists in checking if options changed between the two calls
2970 * (implying that either one is non-null, or one of them is non-null and we
2971 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002972 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002973
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002974 del_cl = del_ka = 0;
2975
Willy Tarreaudc008c52010-02-01 16:20:08 +01002976 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2977 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2978 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2979 (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 +01002980 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002981
Willy Tarreau5b154472009-12-21 20:11:07 +01002982 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
2983 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002984 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2985 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002986 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002987 tmp = TX_CON_WANT_CLO;
2988
Willy Tarreau5b154472009-12-21 20:11:07 +01002989 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2990 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002991
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002992 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2993 /* parse the Connection header and possibly clean it */
2994 int to_del = 0;
2995 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002996 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2997 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002998 to_del |= 2; /* remove "keep-alive" */
2999 if (!(txn->flags & TX_REQ_VER_11))
3000 to_del |= 1; /* remove "close" */
3001 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003002 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003003
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003004 /* check if client or config asks for explicit close in KAL/SCL */
3005 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3006 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3007 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
3008 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
3009 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003010 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
3011 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003012 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3013 }
Willy Tarreau78599912009-10-17 20:12:21 +02003014
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003015 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01003016 list_for_each_entry(wl, &px->req_add, list) {
Willy Tarreau8abd4cd2010-01-31 14:30:44 +01003017 if (wl->cond) {
3018 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
3019 ret = acl_pass(ret);
3020 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3021 ret = !ret;
3022 if (!ret)
3023 continue;
3024 }
3025
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01003026 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003027 goto return_bad_req;
3028 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003029
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003030 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) {
3031 struct chunk msg;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003032 char *realm = req_acl->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003033
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003034 if (!realm)
3035 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3036
Willy Tarreau844a7e72010-01-31 21:46:18 +01003037 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003038 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3039 txn->status = 401;
3040 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003041 /* on 401 we still count one error, because normal browsing
3042 * won't significantly increase the counter but brute force
3043 * attempts will.
3044 */
3045 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003046 goto return_prx_cond;
3047 }
3048
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003049 if (do_stats) {
3050 /* We need to provied stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003051 * FIXME!!! that one is rather dangerous, we want to
3052 * make it follow standard rules (eg: clear req->analysers).
3053 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003054
3055 s->logs.tv_request = now;
3056 s->data_source = DATA_SRC_STATS;
3057 s->data_state = DATA_ST_INIT;
3058 s->task->nice = -32; /* small boost for HTTP statistics */
3059 stream_int_register_handler(s->rep->prod, http_stats_io_handler);
3060 s->rep->prod->private = s;
3061 s->rep->prod->st0 = s->rep->prod->st1 = 0;
3062 req->analysers = 0;
3063
3064 return 0;
3065
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003066 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003067
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003068 /* check whether we have some ACLs set to redirect this request */
3069 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003070 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003071
Willy Tarreauf285f542010-01-03 20:03:03 +01003072 if (rule->cond) {
3073 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3074 ret = acl_pass(ret);
3075 if (rule->cond->pol == ACL_COND_UNLESS)
3076 ret = !ret;
3077 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003078
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003079 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003080 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003081 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003082
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003083 /* build redirect message */
3084 switch(rule->code) {
3085 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003086 msg_fmt = HTTP_303;
3087 break;
3088 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003089 msg_fmt = HTTP_301;
3090 break;
3091 case 302:
3092 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003093 msg_fmt = HTTP_302;
3094 break;
3095 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003096
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003097 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003098 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003099
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003100 switch(rule->type) {
3101 case REDIRECT_TYPE_PREFIX: {
3102 const char *path;
3103 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003104
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003105 path = http_get_path(txn);
3106 /* build message using path */
3107 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003108 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003109 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3110 int qs = 0;
3111 while (qs < pathlen) {
3112 if (path[qs] == '?') {
3113 pathlen = qs;
3114 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003115 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003116 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003117 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003118 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003119 } else {
3120 path = "/";
3121 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003122 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003123
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003124 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003125 goto return_bad_req;
3126
3127 /* add prefix. Note that if prefix == "/", we don't want to
3128 * add anything, otherwise it makes it hard for the user to
3129 * configure a self-redirection.
3130 */
3131 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003132 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3133 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003134 }
3135
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003136 /* add path */
3137 memcpy(rdr.str + rdr.len, path, pathlen);
3138 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003139
3140 /* append a slash at the end of the location is needed and missing */
3141 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3142 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3143 if (rdr.len > rdr.size - 5)
3144 goto return_bad_req;
3145 rdr.str[rdr.len] = '/';
3146 rdr.len++;
3147 }
3148
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003149 break;
3150 }
3151 case REDIRECT_TYPE_LOCATION:
3152 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003153 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003154 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003155
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003156 /* add location */
3157 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3158 rdr.len += rule->rdr_len;
3159 break;
3160 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003161
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003162 if (rule->cookie_len) {
3163 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3164 rdr.len += 14;
3165 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3166 rdr.len += rule->cookie_len;
3167 memcpy(rdr.str + rdr.len, "\r\n", 2);
3168 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003169 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003170
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003171 /* add end of headers and the keep-alive/close status.
3172 * We may choose to set keep-alive if the Location begins
3173 * with a slash, because the client will come back to the
3174 * same server.
3175 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003176 txn->status = rule->code;
3177 /* let's log the request time */
3178 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003179
3180 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3181 (txn->flags & TX_REQ_XFER_LEN) &&
3182 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
3183 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3184 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3185 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003186 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003187 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3188 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3189 rdr.len += 30;
3190 } else {
3191 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3192 rdr.len += 24;
3193 }
Willy Tarreau75661452010-01-10 10:35:01 +01003194 }
3195 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3196 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003197 buffer_write(req->prod->ob, rdr.str, rdr.len);
3198 /* "eat" the request */
3199 buffer_ignore(req, msg->sov - msg->som);
3200 msg->som = msg->sov;
3201 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003202 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3203 txn->req.msg_state = HTTP_MSG_CLOSED;
3204 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003205 break;
3206 } else {
3207 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003208 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3209 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3210 rdr.len += 29;
3211 } else {
3212 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3213 rdr.len += 23;
3214 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003215 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003216 goto return_prx_cond;
3217 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003218 }
3219 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003220
Willy Tarreau2be39392010-01-03 17:24:51 +01003221 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3222 * If this happens, then the data will not come immediately, so we must
3223 * send all what we have without waiting. Note that due to the small gain
3224 * in waiting for the body of the request, it's easier to simply put the
3225 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3226 * itself once used.
3227 */
3228 req->flags |= BF_SEND_DONTWAIT;
3229
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003230 /* that's OK for us now, let's move on to next analysers */
3231 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003232
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003233 return_bad_req:
3234 /* We centralize bad requests processing here */
3235 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3236 /* we detected a parsing error. We want to archive this request
3237 * in the dedicated proxy area for later troubleshooting.
3238 */
3239 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
3240 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003241
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003242 txn->req.msg_state = HTTP_MSG_ERROR;
3243 txn->status = 400;
3244 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003245
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003246 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003247 if (s->listener->counters)
3248 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003249
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003250 return_prx_cond:
3251 if (!(s->flags & SN_ERR_MASK))
3252 s->flags |= SN_ERR_PRXCOND;
3253 if (!(s->flags & SN_FINST_MASK))
3254 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003255
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003256 req->analysers = 0;
3257 req->analyse_exp = TICK_ETERNITY;
3258 return 0;
3259}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003260
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003261/* This function performs all the processing enabled for the current request.
3262 * It returns 1 if the processing can continue on next analysers, or zero if it
3263 * needs more data, encounters an error, or wants to immediately abort the
3264 * request. It relies on buffers flags, and updates s->req->analysers.
3265 */
3266int http_process_request(struct session *s, struct buffer *req, int an_bit)
3267{
3268 struct http_txn *txn = &s->txn;
3269 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003270
Willy Tarreau655dce92009-11-08 13:10:58 +01003271 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003272 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003273 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003274 return 0;
3275 }
3276
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003277 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3278 now_ms, __FUNCTION__,
3279 s,
3280 req,
3281 req->rex, req->wex,
3282 req->flags,
3283 req->l,
3284 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003285
Willy Tarreau59234e92008-11-30 23:51:27 +01003286 /*
3287 * Right now, we know that we have processed the entire headers
3288 * and that unwanted requests have been filtered out. We can do
3289 * whatever we want with the remaining request. Also, now we
3290 * may have separate values for ->fe, ->be.
3291 */
Willy Tarreau06619262006-12-17 08:37:22 +01003292
Willy Tarreau59234e92008-11-30 23:51:27 +01003293 /*
3294 * If HTTP PROXY is set we simply get remote server address
3295 * parsing incoming request.
3296 */
3297 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003298 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003299 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003300
Willy Tarreau59234e92008-11-30 23:51:27 +01003301 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003302 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003303 * Note that doing so might move headers in the request, but
3304 * the fields will stay coherent and the URI will not move.
3305 * This should only be performed in the backend.
3306 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003307 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003308 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3309 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003310
Willy Tarreau59234e92008-11-30 23:51:27 +01003311 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003312 * 8: the appsession cookie was looked up very early in 1.2,
3313 * so let's do the same now.
3314 */
3315
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003316 /* It needs to look into the URI unless persistence must be ignored */
3317 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003318 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003319 }
3320
3321 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003322 * 9: add X-Forwarded-For if either the frontend or the backend
3323 * asks for it.
3324 */
3325 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3326 if (s->cli_addr.ss_family == AF_INET) {
3327 /* Add an X-Forwarded-For header unless the source IP is
3328 * in the 'except' network range.
3329 */
3330 if ((!s->fe->except_mask.s_addr ||
3331 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3332 != s->fe->except_net.s_addr) &&
3333 (!s->be->except_mask.s_addr ||
3334 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3335 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003336 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003337 unsigned char *pn;
3338 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003339
3340 /* Note: we rely on the backend to get the header name to be used for
3341 * x-forwarded-for, because the header is really meant for the backends.
3342 * However, if the backend did not specify any option, we have to rely
3343 * on the frontend's header name.
3344 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003345 if (s->be->fwdfor_hdr_len) {
3346 len = s->be->fwdfor_hdr_len;
3347 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003348 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003349 len = s->fe->fwdfor_hdr_len;
3350 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003351 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003352 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003353
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003354 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003355 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003356 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003357 }
3358 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003359 else if (s->cli_addr.ss_family == AF_INET6) {
3360 /* FIXME: for the sake of completeness, we should also support
3361 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003362 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003363 int len;
3364 char pn[INET6_ADDRSTRLEN];
3365 inet_ntop(AF_INET6,
3366 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3367 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003368
Willy Tarreau59234e92008-11-30 23:51:27 +01003369 /* Note: we rely on the backend to get the header name to be used for
3370 * x-forwarded-for, because the header is really meant for the backends.
3371 * However, if the backend did not specify any option, we have to rely
3372 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003373 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003374 if (s->be->fwdfor_hdr_len) {
3375 len = s->be->fwdfor_hdr_len;
3376 memcpy(trash, s->be->fwdfor_hdr_name, len);
3377 } else {
3378 len = s->fe->fwdfor_hdr_len;
3379 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003380 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003381 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003382
Willy Tarreau59234e92008-11-30 23:51:27 +01003383 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003384 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003385 goto return_bad_req;
3386 }
3387 }
3388
3389 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003390 * 10: add X-Original-To if either the frontend or the backend
3391 * asks for it.
3392 */
3393 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3394
3395 /* FIXME: don't know if IPv6 can handle that case too. */
3396 if (s->cli_addr.ss_family == AF_INET) {
3397 /* Add an X-Original-To header unless the destination IP is
3398 * in the 'except' network range.
3399 */
3400 if (!(s->flags & SN_FRT_ADDR_SET))
3401 get_frt_addr(s);
3402
3403 if ((!s->fe->except_mask_to.s_addr ||
3404 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3405 != s->fe->except_to.s_addr) &&
3406 (!s->be->except_mask_to.s_addr ||
3407 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3408 != s->be->except_to.s_addr)) {
3409 int len;
3410 unsigned char *pn;
3411 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3412
3413 /* Note: we rely on the backend to get the header name to be used for
3414 * x-original-to, because the header is really meant for the backends.
3415 * However, if the backend did not specify any option, we have to rely
3416 * on the frontend's header name.
3417 */
3418 if (s->be->orgto_hdr_len) {
3419 len = s->be->orgto_hdr_len;
3420 memcpy(trash, s->be->orgto_hdr_name, len);
3421 } else {
3422 len = s->fe->orgto_hdr_len;
3423 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003424 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003425 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3426
3427 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003428 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003429 goto return_bad_req;
3430 }
3431 }
3432 }
3433
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003434 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3435 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3436 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
3437 unsigned int want_flags = 0;
3438
3439 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003440 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3441 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) ||
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003442 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))
3443 want_flags |= TX_CON_CLO_SET;
3444 } else {
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003445 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3446 (((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA) &&
3447 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003448 want_flags |= TX_CON_KAL_SET;
3449 }
3450
3451 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3452 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003453 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003454
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003455
Willy Tarreau522d6c02009-12-06 18:49:18 +01003456 /* If we have no server assigned yet and we're balancing on url_param
3457 * with a POST request, we may be interested in checking the body for
3458 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003459 */
3460 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3461 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003462 s->be->url_param_post_limit != 0 &&
3463 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003464 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003465 buffer_dont_connect(req);
3466 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003467 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003468
Willy Tarreaud98cf932009-12-27 22:54:55 +01003469 if (txn->flags & TX_REQ_XFER_LEN)
3470 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003471
Willy Tarreau59234e92008-11-30 23:51:27 +01003472 /*************************************************************
3473 * OK, that's finished for the headers. We have done what we *
3474 * could. Let's switch to the DATA state. *
3475 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003476 req->analyse_exp = TICK_ETERNITY;
3477 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003478
Willy Tarreau59234e92008-11-30 23:51:27 +01003479 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003480 /* OK let's go on with the BODY now */
3481 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003482
Willy Tarreau59234e92008-11-30 23:51:27 +01003483 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003484 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003485 /* we detected a parsing error. We want to archive this request
3486 * in the dedicated proxy area for later troubleshooting.
3487 */
Willy Tarreau4076a152009-04-02 15:18:36 +02003488 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003489 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003490
Willy Tarreau59234e92008-11-30 23:51:27 +01003491 txn->req.msg_state = HTTP_MSG_ERROR;
3492 txn->status = 400;
3493 req->analysers = 0;
3494 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003495
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003496 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003497 if (s->listener->counters)
3498 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003499
Willy Tarreau59234e92008-11-30 23:51:27 +01003500 if (!(s->flags & SN_ERR_MASK))
3501 s->flags |= SN_ERR_PRXCOND;
3502 if (!(s->flags & SN_FINST_MASK))
3503 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003504 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003505}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003506
Willy Tarreau60b85b02008-11-30 23:28:40 +01003507/* This function is an analyser which processes the HTTP tarpit. It always
3508 * returns zero, at the beginning because it prevents any other processing
3509 * from occurring, and at the end because it terminates the request.
3510 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003511int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003512{
3513 struct http_txn *txn = &s->txn;
3514
3515 /* This connection is being tarpitted. The CLIENT side has
3516 * already set the connect expiration date to the right
3517 * timeout. We just have to check that the client is still
3518 * there and that the timeout has not expired.
3519 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003520 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003521 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3522 !tick_is_expired(req->analyse_exp, now_ms))
3523 return 0;
3524
3525 /* We will set the queue timer to the time spent, just for
3526 * logging purposes. We fake a 500 server error, so that the
3527 * attacker will not suspect his connection has been tarpitted.
3528 * It will not cause trouble to the logs because we can exclude
3529 * the tarpitted connections by filtering on the 'PT' status flags.
3530 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003531 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3532
3533 txn->status = 500;
3534 if (req->flags != BF_READ_ERROR)
3535 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3536
3537 req->analysers = 0;
3538 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003539
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003540 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003541 if (s->listener->counters)
3542 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003543
Willy Tarreau60b85b02008-11-30 23:28:40 +01003544 if (!(s->flags & SN_ERR_MASK))
3545 s->flags |= SN_ERR_PRXCOND;
3546 if (!(s->flags & SN_FINST_MASK))
3547 s->flags |= SN_FINST_T;
3548 return 0;
3549}
3550
Willy Tarreaud34af782008-11-30 23:36:37 +01003551/* This function is an analyser which processes the HTTP request body. It looks
3552 * for parameters to be used for the load balancing algorithm (url_param). It
3553 * must only be called after the standard HTTP request processing has occurred,
3554 * because it expects the request to be parsed. It returns zero if it needs to
3555 * read more data, or 1 once it has completed its analysis.
3556 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003557int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003558{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003559 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003560 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003561 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003562
3563 /* We have to parse the HTTP request body to find any required data.
3564 * "balance url_param check_post" should have been the only way to get
3565 * into this. We were brought here after HTTP header analysis, so all
3566 * related structures are ready.
3567 */
3568
Willy Tarreau522d6c02009-12-06 18:49:18 +01003569 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3570 goto missing_data;
3571
3572 if (msg->msg_state < HTTP_MSG_100_SENT) {
3573 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3574 * send an HTTP/1.1 100 Continue intermediate response.
3575 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003576 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003577 struct hdr_ctx ctx;
3578 ctx.idx = 0;
3579 /* Expect is allowed in 1.1, look for it */
3580 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3581 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3582 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3583 }
3584 }
3585 msg->msg_state = HTTP_MSG_100_SENT;
3586 }
3587
3588 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003589 /* we have msg->col and msg->sov which both point to the first
3590 * byte of message body. msg->som still points to the beginning
3591 * of the message. We must save the body in req->lr because it
3592 * survives buffer re-alignments.
3593 */
3594 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003595 if (txn->flags & TX_REQ_TE_CHNK)
3596 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3597 else
3598 msg->msg_state = HTTP_MSG_DATA;
3599 }
3600
3601 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003602 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003603 * set ->sov and ->lr to point to the body and switch to DATA or
3604 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003605 */
3606 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003607
Willy Tarreau115acb92009-12-26 13:56:06 +01003608 if (!ret)
3609 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003610 else if (ret < 0) {
3611 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003612 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003613 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003614 }
3615
Willy Tarreaud98cf932009-12-27 22:54:55 +01003616 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003617 * We have the first non-header byte in msg->col, which is either the
3618 * beginning of the chunk size or of the data. The first data byte is in
3619 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3620 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003621 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003622
3623 if (msg->hdr_content_len < limit)
3624 limit = msg->hdr_content_len;
3625
Willy Tarreau7c96f672009-12-27 22:47:25 +01003626 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003627 goto http_end;
3628
3629 missing_data:
3630 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003631 if (req->flags & BF_FULL) {
3632 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003633 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003634 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003635
Willy Tarreau522d6c02009-12-06 18:49:18 +01003636 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3637 txn->status = 408;
3638 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003639
3640 if (!(s->flags & SN_ERR_MASK))
3641 s->flags |= SN_ERR_CLITO;
3642 if (!(s->flags & SN_FINST_MASK))
3643 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003644 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003645 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003646
3647 /* we get here if we need to wait for more data */
3648 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003649 /* Not enough data. We'll re-use the http-request
3650 * timeout here. Ideally, we should set the timeout
3651 * relative to the accept() date. We just set the
3652 * request timeout once at the beginning of the
3653 * request.
3654 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003655 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003656 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003657 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003658 return 0;
3659 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003660
3661 http_end:
3662 /* The situation will not evolve, so let's give up on the analysis. */
3663 s->logs.tv_request = now; /* update the request timer to reflect full request */
3664 req->analysers &= ~an_bit;
3665 req->analyse_exp = TICK_ETERNITY;
3666 return 1;
3667
3668 return_bad_req: /* let's centralize all bad requests */
3669 txn->req.msg_state = HTTP_MSG_ERROR;
3670 txn->status = 400;
3671 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3672
Willy Tarreau79ebac62010-06-07 13:47:49 +02003673 if (!(s->flags & SN_ERR_MASK))
3674 s->flags |= SN_ERR_PRXCOND;
3675 if (!(s->flags & SN_FINST_MASK))
3676 s->flags |= SN_FINST_R;
3677
Willy Tarreau522d6c02009-12-06 18:49:18 +01003678 return_err_msg:
3679 req->analysers = 0;
3680 s->fe->counters.failed_req++;
3681 if (s->listener->counters)
3682 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003683 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003684}
3685
Willy Tarreau610ecce2010-01-04 21:15:02 +01003686/* Terminate current transaction and prepare a new one. This is very tricky
3687 * right now but it works.
3688 */
3689void http_end_txn_clean_session(struct session *s)
3690{
3691 /* FIXME: We need a more portable way of releasing a backend's and a
3692 * server's connections. We need a safer way to reinitialize buffer
3693 * flags. We also need a more accurate method for computing per-request
3694 * data.
3695 */
3696 http_silent_debug(__LINE__, s);
3697
3698 s->req->cons->flags |= SI_FL_NOLINGER;
3699 s->req->cons->shutr(s->req->cons);
3700 s->req->cons->shutw(s->req->cons);
3701
3702 http_silent_debug(__LINE__, s);
3703
3704 if (s->flags & SN_BE_ASSIGNED)
3705 s->be->beconn--;
3706
3707 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3708 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003709 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003710
3711 if (s->txn.status) {
3712 int n;
3713
3714 n = s->txn.status / 100;
3715 if (n < 1 || n > 5)
3716 n = 0;
3717
3718 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01003719 s->fe->counters.fe.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003720
Willy Tarreau24657792010-02-26 10:30:28 +01003721 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003722 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01003723 s->be->counters.be.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003724 }
3725
3726 /* don't count other requests' data */
3727 s->logs.bytes_in -= s->req->l - s->req->send_max;
3728 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3729
3730 /* let's do a final log if we need it */
3731 if (s->logs.logwait &&
3732 !(s->flags & SN_MONITOR) &&
3733 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3734 s->do_log(s);
3735 }
3736
3737 s->logs.accept_date = date; /* user-visible date for logging */
3738 s->logs.tv_accept = now; /* corrected date for internal use */
3739 tv_zero(&s->logs.tv_request);
3740 s->logs.t_queue = -1;
3741 s->logs.t_connect = -1;
3742 s->logs.t_data = -1;
3743 s->logs.t_close = 0;
3744 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3745 s->logs.srv_queue_size = 0; /* we will get this number soon */
3746
3747 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3748 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3749
3750 if (s->pend_pos)
3751 pendconn_free(s->pend_pos);
3752
3753 if (s->srv) {
3754 if (s->flags & SN_CURR_SESS) {
3755 s->flags &= ~SN_CURR_SESS;
3756 s->srv->cur_sess--;
3757 }
3758 if (may_dequeue_tasks(s->srv, s->be))
3759 process_srv_queue(s->srv);
3760 }
3761
3762 if (unlikely(s->srv_conn))
3763 sess_change_server(s, NULL);
3764 s->srv = NULL;
3765
3766 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3767 s->req->cons->fd = -1; /* just to help with debugging */
3768 s->req->cons->err_type = SI_ET_NONE;
3769 s->req->cons->err_loc = NULL;
3770 s->req->cons->exp = TICK_ETERNITY;
3771 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003772 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3773 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 +02003774 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 +01003775 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3776 s->txn.meth = 0;
3777 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003778 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003779 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003780 s->req->cons->flags |= SI_FL_INDEP_STR;
3781
3782 /* if the request buffer is not empty, it means we're
3783 * about to process another request, so send pending
3784 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003785 * Just don't do this if the buffer is close to be full,
3786 * because the request will wait for it to flush a little
3787 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003788 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003789 if (s->req->l > s->req->send_max) {
3790 if (s->rep->send_max &&
3791 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003792 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3793 s->rep->flags |= BF_EXPECT_MORE;
3794 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003795
3796 /* we're removing the analysers, we MUST re-enable events detection */
3797 buffer_auto_read(s->req);
3798 buffer_auto_close(s->req);
3799 buffer_auto_read(s->rep);
3800 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003801
3802 /* make ->lr point to the first non-forwarded byte */
3803 s->req->lr = s->req->w + s->req->send_max;
3804 if (s->req->lr >= s->req->data + s->req->size)
3805 s->req->lr -= s->req->size;
3806 s->rep->lr = s->rep->w + s->rep->send_max;
3807 if (s->rep->lr >= s->rep->data + s->rep->size)
3808 s->rep->lr -= s->req->size;
3809
Willy Tarreau6df7a0e2010-08-03 11:52:10 +02003810 s->req->analysers |= s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003811 s->rep->analysers = 0;
3812
3813 http_silent_debug(__LINE__, s);
3814}
3815
3816
3817/* This function updates the request state machine according to the response
3818 * state machine and buffer flags. It returns 1 if it changes anything (flag
3819 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3820 * it is only used to find when a request/response couple is complete. Both
3821 * this function and its equivalent should loop until both return zero. It
3822 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3823 */
3824int http_sync_req_state(struct session *s)
3825{
3826 struct buffer *buf = s->req;
3827 struct http_txn *txn = &s->txn;
3828 unsigned int old_flags = buf->flags;
3829 unsigned int old_state = txn->req.msg_state;
3830
3831 http_silent_debug(__LINE__, s);
3832 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3833 return 0;
3834
3835 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003836 /* No need to read anymore, the request was completely parsed.
3837 * We can shut the read side unless we want to abort_on_close.
3838 */
3839 if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE))
3840 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003841
3842 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3843 goto wait_other_side;
3844
3845 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3846 /* The server has not finished to respond, so we
3847 * don't want to move in order not to upset it.
3848 */
3849 goto wait_other_side;
3850 }
3851
3852 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3853 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003854 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003855 txn->req.msg_state = HTTP_MSG_TUNNEL;
3856 goto wait_other_side;
3857 }
3858
3859 /* When we get here, it means that both the request and the
3860 * response have finished receiving. Depending on the connection
3861 * mode, we'll have to wait for the last bytes to leave in either
3862 * direction, and sometimes for a close to be effective.
3863 */
3864
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003865 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3866 /* Server-close mode : queue a connection close to the server */
3867 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003868 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003869 }
3870 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3871 /* Option forceclose is set, or either side wants to close,
3872 * let's enforce it now that we're not expecting any new
3873 * data to come. The caller knows the session is complete
3874 * once both states are CLOSED.
3875 */
3876 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003877 buffer_shutr_now(buf);
3878 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003879 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003880 }
3881 else {
3882 /* The last possible modes are keep-alive and tunnel. Since tunnel
3883 * mode does not set the body analyser, we can't reach this place
3884 * in tunnel mode, so we're left with keep-alive only.
3885 * This mode is currently not implemented, we switch to tunnel mode.
3886 */
3887 buffer_auto_read(buf);
3888 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003889 }
3890
3891 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3892 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003893 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3894
Willy Tarreau610ecce2010-01-04 21:15:02 +01003895 if (!(buf->flags & BF_OUT_EMPTY)) {
3896 txn->req.msg_state = HTTP_MSG_CLOSING;
3897 goto http_msg_closing;
3898 }
3899 else {
3900 txn->req.msg_state = HTTP_MSG_CLOSED;
3901 goto http_msg_closed;
3902 }
3903 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003904 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003905 }
3906
3907 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3908 http_msg_closing:
3909 /* nothing else to forward, just waiting for the output buffer
3910 * to be empty and for the shutw_now to take effect.
3911 */
3912 if (buf->flags & BF_OUT_EMPTY) {
3913 txn->req.msg_state = HTTP_MSG_CLOSED;
3914 goto http_msg_closed;
3915 }
3916 else if (buf->flags & BF_SHUTW) {
3917 txn->req.msg_state = HTTP_MSG_ERROR;
3918 goto wait_other_side;
3919 }
3920 }
3921
3922 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3923 http_msg_closed:
3924 goto wait_other_side;
3925 }
3926
3927 wait_other_side:
3928 http_silent_debug(__LINE__, s);
3929 return txn->req.msg_state != old_state || buf->flags != old_flags;
3930}
3931
3932
3933/* This function updates the response state machine according to the request
3934 * state machine and buffer flags. It returns 1 if it changes anything (flag
3935 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3936 * it is only used to find when a request/response couple is complete. Both
3937 * this function and its equivalent should loop until both return zero. It
3938 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3939 */
3940int http_sync_res_state(struct session *s)
3941{
3942 struct buffer *buf = s->rep;
3943 struct http_txn *txn = &s->txn;
3944 unsigned int old_flags = buf->flags;
3945 unsigned int old_state = txn->rsp.msg_state;
3946
3947 http_silent_debug(__LINE__, s);
3948 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3949 return 0;
3950
3951 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3952 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003953 * still monitor the server connection for a possible close
3954 * while the request is being uploaded, so we don't disable
3955 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003956 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003957 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003958
3959 if (txn->req.msg_state == HTTP_MSG_ERROR)
3960 goto wait_other_side;
3961
3962 if (txn->req.msg_state < HTTP_MSG_DONE) {
3963 /* The client seems to still be sending data, probably
3964 * because we got an error response during an upload.
3965 * We have the choice of either breaking the connection
3966 * or letting it pass through. Let's do the later.
3967 */
3968 goto wait_other_side;
3969 }
3970
3971 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3972 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003973 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003974 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3975 goto wait_other_side;
3976 }
3977
3978 /* When we get here, it means that both the request and the
3979 * response have finished receiving. Depending on the connection
3980 * mode, we'll have to wait for the last bytes to leave in either
3981 * direction, and sometimes for a close to be effective.
3982 */
3983
3984 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3985 /* Server-close mode : shut read and wait for the request
3986 * side to close its output buffer. The caller will detect
3987 * when we're in DONE and the other is in CLOSED and will
3988 * catch that for the final cleanup.
3989 */
3990 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3991 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003992 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003993 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3994 /* Option forceclose is set, or either side wants to close,
3995 * let's enforce it now that we're not expecting any new
3996 * data to come. The caller knows the session is complete
3997 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003998 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003999 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4000 buffer_shutr_now(buf);
4001 buffer_shutw_now(buf);
4002 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004003 }
4004 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004005 /* The last possible modes are keep-alive and tunnel. Since tunnel
4006 * mode does not set the body analyser, we can't reach this place
4007 * in tunnel mode, so we're left with keep-alive only.
4008 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004010 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004011 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004012 }
4013
4014 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4015 /* if we've just closed an output, let's switch */
4016 if (!(buf->flags & BF_OUT_EMPTY)) {
4017 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4018 goto http_msg_closing;
4019 }
4020 else {
4021 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4022 goto http_msg_closed;
4023 }
4024 }
4025 goto wait_other_side;
4026 }
4027
4028 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4029 http_msg_closing:
4030 /* nothing else to forward, just waiting for the output buffer
4031 * to be empty and for the shutw_now to take effect.
4032 */
4033 if (buf->flags & BF_OUT_EMPTY) {
4034 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4035 goto http_msg_closed;
4036 }
4037 else if (buf->flags & BF_SHUTW) {
4038 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreauae526782010-03-04 20:34:23 +01004039 s->be->counters.cli_aborts++;
4040 if (s->srv)
4041 s->srv->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004042 goto wait_other_side;
4043 }
4044 }
4045
4046 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4047 http_msg_closed:
4048 /* drop any pending data */
4049 buffer_ignore(buf, buf->l - buf->send_max);
4050 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004051 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004052 goto wait_other_side;
4053 }
4054
4055 wait_other_side:
4056 http_silent_debug(__LINE__, s);
4057 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4058}
4059
4060
4061/* Resync the request and response state machines. Return 1 if either state
4062 * changes.
4063 */
4064int http_resync_states(struct session *s)
4065{
4066 struct http_txn *txn = &s->txn;
4067 int old_req_state = txn->req.msg_state;
4068 int old_res_state = txn->rsp.msg_state;
4069
4070 http_silent_debug(__LINE__, s);
4071 http_sync_req_state(s);
4072 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004073 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004074 if (!http_sync_res_state(s))
4075 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004076 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004077 if (!http_sync_req_state(s))
4078 break;
4079 }
4080 http_silent_debug(__LINE__, s);
4081 /* OK, both state machines agree on a compatible state.
4082 * There are a few cases we're interested in :
4083 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4084 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4085 * directions, so let's simply disable both analysers.
4086 * - HTTP_MSG_CLOSED on the response only means we must abort the
4087 * request.
4088 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4089 * with server-close mode means we've completed one request and we
4090 * must re-initialize the server connection.
4091 */
4092
4093 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4094 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4095 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4096 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4097 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004098 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004099 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004100 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004101 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004102 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004103 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004104 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4105 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004106 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004107 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004108 s->rep->analysers = 0;
4109 buffer_auto_close(s->rep);
4110 buffer_auto_read(s->rep);
4111 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004112 buffer_abort(s->req);
4113 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004114 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004115 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004116 }
4117 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4118 txn->rsp.msg_state == HTTP_MSG_DONE &&
4119 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4120 /* server-close: terminate this server connection and
4121 * reinitialize a fresh-new transaction.
4122 */
4123 http_end_txn_clean_session(s);
4124 }
4125
4126 http_silent_debug(__LINE__, s);
4127 return txn->req.msg_state != old_req_state ||
4128 txn->rsp.msg_state != old_res_state;
4129}
4130
Willy Tarreaud98cf932009-12-27 22:54:55 +01004131/* This function is an analyser which forwards request body (including chunk
4132 * sizes if any). It is called as soon as we must forward, even if we forward
4133 * zero byte. The only situation where it must not be called is when we're in
4134 * tunnel mode and we want to forward till the close. It's used both to forward
4135 * remaining data and to resync after end of body. It expects the msg_state to
4136 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4137 * read more data, or 1 once we can go on with next request or end the session.
4138 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4139 * bytes of pending data + the headers if not already done (between som and sov).
4140 * It eventually adjusts som to match sov after the data in between have been sent.
4141 */
4142int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4143{
4144 struct http_txn *txn = &s->txn;
4145 struct http_msg *msg = &s->txn.req;
4146
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004147 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4148 return 0;
4149
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004150 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4151 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004152 /* Output closed while we were sending data. We must abort and
4153 * wake the other side up.
4154 */
4155 msg->msg_state = HTTP_MSG_ERROR;
4156 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004157 return 1;
4158 }
4159
Willy Tarreau4fe41902010-06-07 22:27:41 +02004160 /* in most states, we should abort in case of early close */
4161 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004162
4163 /* Note that we don't have to send 100-continue back because we don't
4164 * need the data to complete our job, and it's up to the server to
4165 * decide whether to return 100, 417 or anything else in return of
4166 * an "Expect: 100-continue" header.
4167 */
4168
4169 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4170 /* we have msg->col and msg->sov which both point to the first
4171 * byte of message body. msg->som still points to the beginning
4172 * of the message. We must save the body in req->lr because it
4173 * survives buffer re-alignments.
4174 */
4175 req->lr = req->data + msg->sov;
4176 if (txn->flags & TX_REQ_TE_CHNK)
4177 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4178 else {
4179 msg->msg_state = HTTP_MSG_DATA;
4180 }
4181 }
4182
Willy Tarreaud98cf932009-12-27 22:54:55 +01004183 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004184 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004185 /* we may have some data pending */
4186 if (msg->hdr_content_len || msg->som != msg->sov) {
4187 int bytes = msg->sov - msg->som;
4188 if (bytes < 0) /* sov may have wrapped at the end */
4189 bytes += req->size;
4190 buffer_forward(req, bytes + msg->hdr_content_len);
4191 msg->hdr_content_len = 0; /* don't forward that again */
4192 msg->som = msg->sov;
4193 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004194
Willy Tarreaucaabe412010-01-03 23:08:28 +01004195 if (msg->msg_state == HTTP_MSG_DATA) {
4196 /* must still forward */
4197 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004198 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004199
4200 /* nothing left to forward */
4201 if (txn->flags & TX_REQ_TE_CHNK)
4202 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004203 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004204 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004205 }
4206 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004207 /* read the chunk size and assign it to ->hdr_content_len, then
4208 * set ->sov and ->lr to point to the body and switch to DATA or
4209 * TRAILERS state.
4210 */
4211 int ret = http_parse_chunk_size(req, msg);
4212
4213 if (!ret)
4214 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004215 else if (ret < 0) {
4216 session_inc_http_err_ctr(s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004217 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004218 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004219 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004220 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004221 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4222 /* we want the CRLF after the data */
4223 int ret;
4224
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004225 req->lr = req->w + req->send_max;
4226 if (req->lr >= req->data + req->size)
4227 req->lr -= req->size;
4228
Willy Tarreaud98cf932009-12-27 22:54:55 +01004229 ret = http_skip_chunk_crlf(req, msg);
4230
4231 if (ret == 0)
4232 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004233 else if (ret < 0) {
4234 session_inc_http_err_ctr(s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004235 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004236 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004237 /* we're in MSG_CHUNK_SIZE now */
4238 }
4239 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4240 int ret = http_forward_trailers(req, msg);
4241
4242 if (ret == 0)
4243 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004244 else if (ret < 0) {
4245 session_inc_http_err_ctr(s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004246 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004247 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004248 /* we're in HTTP_MSG_DONE now */
4249 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004250 else {
4251 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004252 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004253 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4254 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4255 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004256 if (http_resync_states(s)) {
4257 /* some state changes occurred, maybe the analyser
4258 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004259 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004260 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
4261 goto return_bad_req;
4262 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004263 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004264
4265 /* If "option abortonclose" is set on the backend, we
4266 * want to monitor the client's connection and forward
4267 * any shutdown notification to the server, which will
4268 * decide whether to close or to go on processing the
4269 * request.
4270 */
4271 if (s->be->options & PR_O_ABRT_CLOSE) {
4272 buffer_auto_read(req);
4273 buffer_auto_close(req);
4274 }
4275
Willy Tarreau610ecce2010-01-04 21:15:02 +01004276 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004277 }
4278 }
4279
Willy Tarreaud98cf932009-12-27 22:54:55 +01004280 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004281 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004282 if (req->flags & BF_SHUTR) {
4283 if (!(s->flags & SN_ERR_MASK))
4284 s->flags |= SN_ERR_CLICL;
4285 if (!(s->flags & SN_FINST_MASK))
4286 s->flags |= SN_FINST_D;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004287 goto return_bad_req;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004288 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004289
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004290 /* waiting for the last bits to leave the buffer */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004291 if (req->flags & BF_SHUTW) {
4292 if (!(s->flags & SN_ERR_MASK))
4293 s->flags |= SN_ERR_SRVCL;
4294 if (!(s->flags & SN_FINST_MASK))
4295 s->flags |= SN_FINST_D;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004296 goto return_bad_req;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004297 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004298
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004299 /* When TE: chunked is used, we need to get there again to parse remaining
4300 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4301 */
4302 if (txn->flags & TX_REQ_TE_CHNK)
4303 buffer_dont_close(req);
4304
Willy Tarreau610ecce2010-01-04 21:15:02 +01004305 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004306 return 0;
4307
Willy Tarreaud98cf932009-12-27 22:54:55 +01004308 return_bad_req: /* let's centralize all bad requests */
4309 txn->req.msg_state = HTTP_MSG_ERROR;
4310 txn->status = 400;
4311 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01004312 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 +01004313 req->analysers = 0;
4314 s->fe->counters.failed_req++;
4315 if (s->listener->counters)
4316 s->listener->counters->failed_req++;
4317
4318 if (!(s->flags & SN_ERR_MASK))
4319 s->flags |= SN_ERR_PRXCOND;
4320 if (!(s->flags & SN_FINST_MASK))
4321 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004322 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004323 return 0;
4324}
4325
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004326/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4327 * processing can continue on next analysers, or zero if it either needs more
4328 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4329 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4330 * when it has nothing left to do, and may remove any analyser when it wants to
4331 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004332 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004333int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004334{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004335 struct http_txn *txn = &s->txn;
4336 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004337 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004338 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004339 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004340 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004341
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004342 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 +02004343 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004344 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004345 rep,
4346 rep->rex, rep->wex,
4347 rep->flags,
4348 rep->l,
4349 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004350
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004351 /*
4352 * Now parse the partial (or complete) lines.
4353 * We will check the response syntax, and also join multi-line
4354 * headers. An index of all the lines will be elaborated while
4355 * parsing.
4356 *
4357 * For the parsing, we use a 28 states FSM.
4358 *
4359 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004360 * rep->data + msg->som = beginning of response
4361 * rep->data + msg->eoh = end of processed headers / start of current one
4362 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004363 * rep->lr = first non-visited byte
4364 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004365 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004366 */
4367
Willy Tarreau83e3af02009-12-28 17:39:57 +01004368 /* There's a protected area at the end of the buffer for rewriting
4369 * purposes. We don't want to start to parse the request if the
4370 * protected area is affected, because we may have to move processed
4371 * data later, which is much more complicated.
4372 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004373 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4374 if (unlikely((rep->flags & BF_FULL) ||
4375 rep->r < rep->lr ||
4376 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4377 if (rep->send_max) {
4378 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004379 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4380 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004381 buffer_dont_close(rep);
4382 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4383 return 0;
4384 }
4385 if (rep->l <= rep->size - global.tune.maxrewrite)
4386 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004387 }
4388
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004389 if (likely(rep->lr < rep->r))
4390 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004391 }
4392
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004393 /* 1: we might have to print this header in debug mode */
4394 if (unlikely((global.mode & MODE_DEBUG) &&
4395 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004396 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004397 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004398 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004399
Willy Tarreau663308b2010-06-07 14:06:08 +02004400 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004401 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004402 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004403
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004404 sol += hdr_idx_first_pos(&txn->hdr_idx);
4405 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004406
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004407 while (cur_idx) {
4408 eol = sol + txn->hdr_idx.v[cur_idx].len;
4409 debug_hdr("srvhdr", s, sol, eol);
4410 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4411 cur_idx = txn->hdr_idx.v[cur_idx].next;
4412 }
4413 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004414
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004415 /*
4416 * Now we quickly check if we have found a full valid response.
4417 * If not so, we check the FD and buffer states before leaving.
4418 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004419 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004420 * responses are checked first.
4421 *
4422 * Depending on whether the client is still there or not, we
4423 * may send an error response back or not. Note that normally
4424 * we should only check for HTTP status there, and check I/O
4425 * errors somewhere else.
4426 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004427
Willy Tarreau655dce92009-11-08 13:10:58 +01004428 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004429 /* Invalid response */
4430 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4431 /* we detected a parsing error. We want to archive this response
4432 * in the dedicated proxy area for later troubleshooting.
4433 */
4434 hdr_response_bad:
4435 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
4436 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4437
4438 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004439 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004440 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004441 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4442 }
Willy Tarreau64648412010-03-05 10:41:54 +01004443 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004444 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004445 rep->analysers = 0;
4446 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004447 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004448 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004449 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4450
4451 if (!(s->flags & SN_ERR_MASK))
4452 s->flags |= SN_ERR_PRXCOND;
4453 if (!(s->flags & SN_FINST_MASK))
4454 s->flags |= SN_FINST_H;
4455
4456 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004457 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004458
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004459 /* too large response does not fit in buffer. */
4460 else if (rep->flags & BF_FULL) {
4461 goto hdr_response_bad;
4462 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004463
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004464 /* read error */
4465 else if (rep->flags & BF_READ_ERROR) {
4466 if (msg->err_pos >= 0)
4467 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004468
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004469 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004470 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004471 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004472 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4473 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004474
Willy Tarreau90deb182010-01-07 00:20:41 +01004475 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004476 rep->analysers = 0;
4477 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004478 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004479 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004480 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004481
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004482 if (!(s->flags & SN_ERR_MASK))
4483 s->flags |= SN_ERR_SRVCL;
4484 if (!(s->flags & SN_FINST_MASK))
4485 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004486 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004487 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004488
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004489 /* read timeout : return a 504 to the client. */
4490 else if (rep->flags & BF_READ_TIMEOUT) {
4491 if (msg->err_pos >= 0)
4492 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004493
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004494 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004495 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004496 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004497 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4498 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004499
Willy Tarreau90deb182010-01-07 00:20:41 +01004500 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004501 rep->analysers = 0;
4502 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004503 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004504 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004505 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004506
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004507 if (!(s->flags & SN_ERR_MASK))
4508 s->flags |= SN_ERR_SRVTO;
4509 if (!(s->flags & SN_FINST_MASK))
4510 s->flags |= SN_FINST_H;
4511 return 0;
4512 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004513
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004514 /* close from server */
4515 else if (rep->flags & BF_SHUTR) {
4516 if (msg->err_pos >= 0)
4517 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004518
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004519 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004520 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004521 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004522 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4523 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004524
Willy Tarreau90deb182010-01-07 00:20:41 +01004525 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004526 rep->analysers = 0;
4527 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004528 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004529 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004530 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004531
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004532 if (!(s->flags & SN_ERR_MASK))
4533 s->flags |= SN_ERR_SRVCL;
4534 if (!(s->flags & SN_FINST_MASK))
4535 s->flags |= SN_FINST_H;
4536 return 0;
4537 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004538
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004539 /* write error to client (we don't send any message then) */
4540 else if (rep->flags & BF_WRITE_ERROR) {
4541 if (msg->err_pos >= 0)
4542 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004543
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004544 s->be->counters.failed_resp++;
4545 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004546 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004547
4548 if (!(s->flags & SN_ERR_MASK))
4549 s->flags |= SN_ERR_CLICL;
4550 if (!(s->flags & SN_FINST_MASK))
4551 s->flags |= SN_FINST_H;
4552
4553 /* process_session() will take care of the error */
4554 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004555 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004556
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004557 buffer_dont_close(rep);
4558 return 0;
4559 }
4560
4561 /* More interesting part now : we know that we have a complete
4562 * response which at least looks like HTTP. We have an indicator
4563 * of each header's length, so we can parse them quickly.
4564 */
4565
4566 if (unlikely(msg->err_pos >= 0))
4567 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe);
4568
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004569 /*
4570 * 1: get the status code
4571 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004572 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004573 if (n < 1 || n > 5)
4574 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004575 /* when the client triggers a 4xx from the server, it's most often due
4576 * to a missing object or permission. These events should be tracked
4577 * because if they happen often, it may indicate a brute force or a
4578 * vulnerability scan.
4579 */
4580 if (n == 4)
4581 session_inc_http_err_ctr(s);
4582
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004583 if (s->srv)
4584 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004585
Willy Tarreau5b154472009-12-21 20:11:07 +01004586 /* check if the response is HTTP/1.1 or above */
4587 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004588 ((msg->sol[5] > '1') ||
4589 ((msg->sol[5] == '1') &&
4590 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004591 txn->flags |= TX_RES_VER_11;
4592
4593 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004594 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 +01004595
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004596 /* transfer length unknown*/
4597 txn->flags &= ~TX_RES_XFER_LEN;
4598
Willy Tarreau962c3f42010-01-10 00:15:35 +01004599 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004600
Willy Tarreau39650402010-03-15 19:44:39 +01004601 /* Adjust server's health based on status code. Note: status codes 501
4602 * and 505 are triggered on demand by client request, so we must not
4603 * count them as server failures.
4604 */
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004605 if (s->srv) {
4606 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
4607 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4608 else
4609 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4610 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004611
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004612 /*
4613 * 2: check for cacheability.
4614 */
4615
4616 switch (txn->status) {
4617 case 200:
4618 case 203:
4619 case 206:
4620 case 300:
4621 case 301:
4622 case 410:
4623 /* RFC2616 @13.4:
4624 * "A response received with a status code of
4625 * 200, 203, 206, 300, 301 or 410 MAY be stored
4626 * by a cache (...) unless a cache-control
4627 * directive prohibits caching."
4628 *
4629 * RFC2616 @9.5: POST method :
4630 * "Responses to this method are not cacheable,
4631 * unless the response includes appropriate
4632 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004633 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004634 if (likely(txn->meth != HTTP_METH_POST) &&
4635 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4636 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4637 break;
4638 default:
4639 break;
4640 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004641
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004642 /*
4643 * 3: we may need to capture headers
4644 */
4645 s->logs.logwait &= ~LW_RESP;
4646 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004647 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004648 txn->rsp.cap, s->fe->rsp_cap);
4649
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004650 /* 4: determine the transfer-length.
4651 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4652 * the presence of a message-body in a RESPONSE and its transfer length
4653 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004654 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004655 * All responses to the HEAD request method MUST NOT include a
4656 * message-body, even though the presence of entity-header fields
4657 * might lead one to believe they do. All 1xx (informational), 204
4658 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4659 * message-body. All other responses do include a message-body,
4660 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004661 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004662 * 1. Any response which "MUST NOT" include a message-body (such as the
4663 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4664 * always terminated by the first empty line after the header fields,
4665 * regardless of the entity-header fields present in the message.
4666 *
4667 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4668 * the "chunked" transfer-coding (Section 6.2) is used, the
4669 * transfer-length is defined by the use of this transfer-coding.
4670 * If a Transfer-Encoding header field is present and the "chunked"
4671 * transfer-coding is not present, the transfer-length is defined by
4672 * the sender closing the connection.
4673 *
4674 * 3. If a Content-Length header field is present, its decimal value in
4675 * OCTETs represents both the entity-length and the transfer-length.
4676 * If a message is received with both a Transfer-Encoding header
4677 * field and a Content-Length header field, the latter MUST be ignored.
4678 *
4679 * 4. If the message uses the media type "multipart/byteranges", and
4680 * the transfer-length is not otherwise specified, then this self-
4681 * delimiting media type defines the transfer-length. This media
4682 * type MUST NOT be used unless the sender knows that the recipient
4683 * can parse it; the presence in a request of a Range header with
4684 * multiple byte-range specifiers from a 1.1 client implies that the
4685 * client can parse multipart/byteranges responses.
4686 *
4687 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004688 */
4689
4690 /* Skip parsing if no content length is possible. The response flags
4691 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004692 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004693 * FIXME: should we parse anyway and return an error on chunked encoding ?
4694 */
4695 if (txn->meth == HTTP_METH_HEAD ||
4696 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004697 txn->status == 204 || txn->status == 304) {
4698 txn->flags |= TX_RES_XFER_LEN;
4699 goto skip_content_length;
4700 }
4701
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004702 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004703 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004704 while ((txn->flags & TX_RES_VER_11) &&
4705 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004706 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4707 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4708 else if (txn->flags & TX_RES_TE_CHNK) {
4709 /* bad transfer-encoding (chunked followed by something else) */
4710 use_close_only = 1;
4711 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4712 break;
4713 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004714 }
4715
4716 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4717 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004718 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004719 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4720 signed long long cl;
4721
4722 if (!ctx.vlen)
4723 goto hdr_response_bad;
4724
4725 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4726 goto hdr_response_bad; /* parse failure */
4727
4728 if (cl < 0)
4729 goto hdr_response_bad;
4730
4731 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4732 goto hdr_response_bad; /* already specified, was different */
4733
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004734 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004735 msg->hdr_content_len = cl;
4736 }
4737
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004738 /* FIXME: we should also implement the multipart/byterange method.
4739 * For now on, we resort to close mode in this case (unknown length).
4740 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004741skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004742
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004743 /* end of job, return OK */
4744 rep->analysers &= ~an_bit;
4745 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004746 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004747 return 1;
4748}
4749
4750/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004751 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4752 * and updates t->rep->analysers. It might make sense to explode it into several
4753 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004754 */
4755int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4756{
4757 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004758 struct http_msg *msg = &txn->rsp;
4759 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004760 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004761
4762 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4763 now_ms, __FUNCTION__,
4764 t,
4765 rep,
4766 rep->rex, rep->wex,
4767 rep->flags,
4768 rep->l,
4769 rep->analysers);
4770
Willy Tarreau655dce92009-11-08 13:10:58 +01004771 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004772 return 0;
4773
4774 rep->analysers &= ~an_bit;
4775 rep->analyse_exp = TICK_ETERNITY;
4776
Willy Tarreau5b154472009-12-21 20:11:07 +01004777 /* Now we have to check if we need to modify the Connection header.
4778 * This is more difficult on the response than it is on the request,
4779 * because we can have two different HTTP versions and we don't know
4780 * how the client will interprete a response. For instance, let's say
4781 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4782 * HTTP/1.1 response without any header. Maybe it will bound itself to
4783 * HTTP/1.0 because it only knows about it, and will consider the lack
4784 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4785 * the lack of header as a keep-alive. Thus we will use two flags
4786 * indicating how a request MAY be understood by the client. In case
4787 * of multiple possibilities, we'll fix the header to be explicit. If
4788 * ambiguous cases such as both close and keepalive are seen, then we
4789 * will fall back to explicit close. Note that we won't take risks with
4790 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004791 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004792 */
4793
Willy Tarreaudc008c52010-02-01 16:20:08 +01004794 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4795 txn->status == 101)) {
4796 /* Either we've established an explicit tunnel, or we're
4797 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004798 * to understand the next protocols. We have to switch to tunnel
4799 * mode, so that we transfer the request and responses then let
4800 * this protocol pass unmodified. When we later implement specific
4801 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004802 * header which contains information about that protocol for
4803 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004804 */
4805 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4806 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004807 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4808 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4809 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004810 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004811
Willy Tarreau60466522010-01-18 19:08:45 +01004812 /* on unknown transfer length, we must close */
4813 if (!(txn->flags & TX_RES_XFER_LEN) &&
4814 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4815 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004816
Willy Tarreau60466522010-01-18 19:08:45 +01004817 /* now adjust header transformations depending on current state */
4818 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4819 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4820 to_del |= 2; /* remove "keep-alive" on any response */
4821 if (!(txn->flags & TX_RES_VER_11))
4822 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004823 }
Willy Tarreau60466522010-01-18 19:08:45 +01004824 else { /* SCL / KAL */
4825 to_del |= 1; /* remove "close" on any response */
4826 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4827 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004828 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004829
Willy Tarreau60466522010-01-18 19:08:45 +01004830 /* Parse and remove some headers from the connection header */
4831 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004832
Willy Tarreau60466522010-01-18 19:08:45 +01004833 /* Some keep-alive responses are converted to Server-close if
4834 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004835 */
Willy Tarreau60466522010-01-18 19:08:45 +01004836 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4837 if ((txn->flags & TX_HDR_CONN_CLO) ||
4838 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4839 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004840 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004841 }
4842
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004843 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004844 /*
4845 * 3: we will have to evaluate the filters.
4846 * As opposed to version 1.2, now they will be evaluated in the
4847 * filters order and not in the header order. This means that
4848 * each filter has to be validated among all headers.
4849 *
4850 * Filters are tried with ->be first, then with ->fe if it is
4851 * different from ->be.
4852 */
4853
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004854 cur_proxy = t->be;
4855 while (1) {
4856 struct proxy *rule_set = cur_proxy;
4857
4858 /* try headers filters */
4859 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004860 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004861 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004862 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004863 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004864 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
4865 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004866 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004867 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004868 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004869 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004870 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004871 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004872 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004873 if (!(t->flags & SN_ERR_MASK))
4874 t->flags |= SN_ERR_PRXCOND;
4875 if (!(t->flags & SN_FINST_MASK))
4876 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004877 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004878 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004879 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004880
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004881 /* has the response been denied ? */
4882 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01004883 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004884 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004885
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02004886 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004887 if (t->listener->counters)
4888 t->listener->counters->denied_resp++;
4889
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004890 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004891 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004892
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004893 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004894 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004895 if (txn->status < 200)
4896 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004897 if (wl->cond) {
4898 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
4899 ret = acl_pass(ret);
4900 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4901 ret = !ret;
4902 if (!ret)
4903 continue;
4904 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004905 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004906 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004907 }
4908
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004909 /* check whether we're already working on the frontend */
4910 if (cur_proxy == t->fe)
4911 break;
4912 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004913 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004914
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004915 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004916 * We may be facing a 100-continue response, in which case this
4917 * is not the right response, and we're waiting for the next one.
4918 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004919 * next one.
4920 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004921 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004922 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01004923 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004924 msg->msg_state = HTTP_MSG_RPBEFORE;
4925 txn->status = 0;
4926 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
4927 return 1;
4928 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004929 else if (unlikely(txn->status < 200))
4930 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004931
4932 /* we don't have any 1xx status code now */
4933
4934 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004935 * 4: check for server cookie.
4936 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004937 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
4938 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004939 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004940
Willy Tarreaubaaee002006-06-26 02:48:02 +02004941
Willy Tarreaua15645d2007-03-18 16:22:39 +01004942 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004943 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004944 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004945 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004946 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01004947
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004948 /*
4949 * 6: add server cookie in the response if needed
4950 */
4951 if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004952 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
4953 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004954 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004955
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004956 /* the server is known, it's not the one the client requested, we have to
4957 * insert a set-cookie here, except if we want to insert only on POST
4958 * requests and this one isn't. Note that servers which don't have cookies
4959 * (eg: some backup servers) will return a full cookie removal request.
4960 */
4961 len = sprintf(trash, "Set-Cookie: %s=%s; path=/",
4962 t->be->cookie_name,
4963 t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT");
Willy Tarreaubaaee002006-06-26 02:48:02 +02004964
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004965 if (t->be->cookie_domain)
4966 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004967
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004968 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004969 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004970 goto return_bad_resp;
4971 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004972
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004973 /* Here, we will tell an eventual cache on the client side that we don't
4974 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
4975 * Some caches understand the correct form: 'no-cache="set-cookie"', but
4976 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
4977 */
4978 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02004979
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004980 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4981
4982 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01004983 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004984 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01004985 }
4986 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004987
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004988 /*
4989 * 7: check if result will be cacheable with a cookie.
4990 * We'll block the response if security checks have caught
4991 * nasty things such as a cacheable cookie.
4992 */
4993 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) ==
4994 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004995 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004996
4997 /* we're in presence of a cacheable response containing
4998 * a set-cookie header. We'll block it as requested by
4999 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005000 */
Willy Tarreau8365f932009-03-15 23:11:49 +01005001 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005002 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005003
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005004 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005005 if (t->listener->counters)
5006 t->listener->counters->denied_resp++;
5007
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005008 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5009 t->be->id, t->srv?t->srv->id:"<dispatch>");
5010 send_log(t->be, LOG_ALERT,
5011 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5012 t->be->id, t->srv?t->srv->id:"<dispatch>");
5013 goto return_srv_prx_502;
5014 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005015
5016 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005017 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005018 */
Willy Tarreau60466522010-01-18 19:08:45 +01005019 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5020 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5021 unsigned int want_flags = 0;
5022
5023 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5024 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5025 /* we want a keep-alive response here. Keep-alive header
5026 * required if either side is not 1.1.
5027 */
5028 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5029 want_flags |= TX_CON_KAL_SET;
5030 }
5031 else {
5032 /* we want a close response here. Close header required if
5033 * the server is 1.1, regardless of the client.
5034 */
5035 if (txn->flags & TX_RES_VER_11)
5036 want_flags |= TX_CON_CLO_SET;
5037 }
5038
5039 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5040 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005041 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005042
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005043 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005044 if ((txn->flags & TX_RES_XFER_LEN) ||
5045 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005046 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005047
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005048 /*************************************************************
5049 * OK, that's finished for the headers. We have done what we *
5050 * could. Let's switch to the DATA state. *
5051 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005052
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005053 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005054
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005055 /* if the user wants to log as soon as possible, without counting
5056 * bytes from the server, then this is the right moment. We have
5057 * to temporarily assign bytes_out to log what we currently have.
5058 */
5059 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5060 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5061 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005062 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005063 t->logs.bytes_out = 0;
5064 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005065
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005066 /* Note: we must not try to cheat by jumping directly to DATA,
5067 * otherwise we would not let the client side wake up.
5068 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005069
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005070 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005071 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005072 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005073}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005074
Willy Tarreaud98cf932009-12-27 22:54:55 +01005075/* This function is an analyser which forwards response body (including chunk
5076 * sizes if any). It is called as soon as we must forward, even if we forward
5077 * zero byte. The only situation where it must not be called is when we're in
5078 * tunnel mode and we want to forward till the close. It's used both to forward
5079 * remaining data and to resync after end of body. It expects the msg_state to
5080 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5081 * read more data, or 1 once we can go on with next request or end the session.
5082 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
5083 * bytes of pending data + the headers if not already done (between som and sov).
5084 * It eventually adjusts som to match sov after the data in between have been sent.
5085 */
5086int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5087{
5088 struct http_txn *txn = &s->txn;
5089 struct http_msg *msg = &s->txn.rsp;
5090
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005091 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5092 return 0;
5093
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005094 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005095 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005096 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005097 /* Output closed while we were sending data. We must abort and
5098 * wake the other side up.
5099 */
5100 msg->msg_state = HTTP_MSG_ERROR;
5101 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005102 return 1;
5103 }
5104
Willy Tarreau4fe41902010-06-07 22:27:41 +02005105 /* in most states, we should abort in case of early close */
5106 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005107
Willy Tarreaud98cf932009-12-27 22:54:55 +01005108 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5109 /* we have msg->col and msg->sov which both point to the first
5110 * byte of message body. msg->som still points to the beginning
5111 * of the message. We must save the body in req->lr because it
5112 * survives buffer re-alignments.
5113 */
5114 res->lr = res->data + msg->sov;
5115 if (txn->flags & TX_RES_TE_CHNK)
5116 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5117 else {
5118 msg->msg_state = HTTP_MSG_DATA;
5119 }
5120 }
5121
Willy Tarreaud98cf932009-12-27 22:54:55 +01005122 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005123 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005124 /* we may have some data pending */
5125 if (msg->hdr_content_len || msg->som != msg->sov) {
5126 int bytes = msg->sov - msg->som;
5127 if (bytes < 0) /* sov may have wrapped at the end */
5128 bytes += res->size;
5129 buffer_forward(res, bytes + msg->hdr_content_len);
5130 msg->hdr_content_len = 0; /* don't forward that again */
5131 msg->som = msg->sov;
5132 }
5133
Willy Tarreaucaabe412010-01-03 23:08:28 +01005134 if (msg->msg_state == HTTP_MSG_DATA) {
5135 /* must still forward */
5136 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005137 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005138
5139 /* nothing left to forward */
5140 if (txn->flags & TX_RES_TE_CHNK)
5141 msg->msg_state = HTTP_MSG_DATA_CRLF;
5142 else
5143 msg->msg_state = HTTP_MSG_DONE;
5144 }
5145 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005146 /* read the chunk size and assign it to ->hdr_content_len, then
5147 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5148 */
5149 int ret = http_parse_chunk_size(res, msg);
5150
5151 if (!ret)
5152 goto missing_data;
5153 else if (ret < 0)
5154 goto return_bad_res;
5155 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005156 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005157 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5158 /* we want the CRLF after the data */
5159 int ret;
5160
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005161 res->lr = res->w + res->send_max;
5162 if (res->lr >= res->data + res->size)
5163 res->lr -= res->size;
5164
Willy Tarreaud98cf932009-12-27 22:54:55 +01005165 ret = http_skip_chunk_crlf(res, msg);
5166
5167 if (!ret)
5168 goto missing_data;
5169 else if (ret < 0)
5170 goto return_bad_res;
5171 /* we're in MSG_CHUNK_SIZE now */
5172 }
5173 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5174 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005175
Willy Tarreaud98cf932009-12-27 22:54:55 +01005176 if (ret == 0)
5177 goto missing_data;
5178 else if (ret < 0)
5179 goto return_bad_res;
5180 /* we're in HTTP_MSG_DONE now */
5181 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005182 else {
5183 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005184 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005185 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5186 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5187 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005188 if (http_resync_states(s)) {
5189 http_silent_debug(__LINE__, s);
5190 /* some state changes occurred, maybe the analyser
5191 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005192 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005193 if (unlikely(msg->msg_state == HTTP_MSG_ERROR))
5194 goto return_bad_res;
5195 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005196 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005197 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005198 }
5199 }
5200
Willy Tarreaud98cf932009-12-27 22:54:55 +01005201 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005202 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005203 if (res->flags & BF_SHUTR) {
5204 if (!(s->flags & SN_ERR_MASK))
5205 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01005206 s->be->counters.srv_aborts++;
5207 if (s->srv)
5208 s->srv->counters.srv_aborts++;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005209 goto return_bad_res;
Willy Tarreau40dba092010-03-04 18:14:51 +01005210 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005211
Willy Tarreau40dba092010-03-04 18:14:51 +01005212 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005213 if (!s->req->analysers)
5214 goto return_bad_res;
5215
Willy Tarreaud98cf932009-12-27 22:54:55 +01005216 /* forward the chunk size as well as any pending data */
5217 if (msg->hdr_content_len || msg->som != msg->sov) {
5218 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
5219 msg->hdr_content_len = 0; /* don't forward that again */
5220 msg->som = msg->sov;
5221 }
5222
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005223 /* When TE: chunked is used, we need to get there again to parse remaining
5224 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5225 * Similarly, with keep-alive on the client side, we don't want to forward a
5226 * close.
5227 */
5228 if ((txn->flags & TX_RES_TE_CHNK) ||
5229 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5230 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5231 buffer_dont_close(res);
5232
Willy Tarreaud98cf932009-12-27 22:54:55 +01005233 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005234 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005235 return 0;
5236
Willy Tarreau40dba092010-03-04 18:14:51 +01005237 return_bad_res: /* let's centralize all bad responses */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005238 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005239 /* don't send any error message as we're in the body */
5240 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005241 res->analysers = 0;
5242 s->be->counters.failed_resp++;
5243 if (s->srv) {
5244 s->srv->counters.failed_resp++;
5245 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
5246 }
5247
5248 if (!(s->flags & SN_ERR_MASK))
5249 s->flags |= SN_ERR_PRXCOND;
5250 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005251 s->flags |= SN_FINST_D;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005252 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005253 return 0;
5254}
5255
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005256/* Iterate the same filter through all request headers.
5257 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005258 * Since it can manage the switch to another backend, it updates the per-proxy
5259 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005260 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005261int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005262{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005263 char term;
5264 char *cur_ptr, *cur_end, *cur_next;
5265 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005266 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005267 struct hdr_idx_elem *cur_hdr;
5268 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005269
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005270 last_hdr = 0;
5271
Willy Tarreau962c3f42010-01-10 00:15:35 +01005272 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005273 old_idx = 0;
5274
5275 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005276 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005277 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005278 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005279 (exp->action == ACT_ALLOW ||
5280 exp->action == ACT_DENY ||
5281 exp->action == ACT_TARPIT))
5282 return 0;
5283
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005284 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005285 if (!cur_idx)
5286 break;
5287
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005288 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005289 cur_ptr = cur_next;
5290 cur_end = cur_ptr + cur_hdr->len;
5291 cur_next = cur_end + cur_hdr->cr + 1;
5292
5293 /* Now we have one header between cur_ptr and cur_end,
5294 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005295 */
5296
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005297 /* The annoying part is that pattern matching needs
5298 * that we modify the contents to null-terminate all
5299 * strings before testing them.
5300 */
5301
5302 term = *cur_end;
5303 *cur_end = '\0';
5304
5305 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5306 switch (exp->action) {
5307 case ACT_SETBE:
5308 /* It is not possible to jump a second time.
5309 * FIXME: should we return an HTTP/500 here so that
5310 * the admin knows there's a problem ?
5311 */
5312 if (t->be != t->fe)
5313 break;
5314
5315 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005316 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005317 last_hdr = 1;
5318 break;
5319
5320 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005321 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005322 last_hdr = 1;
5323 break;
5324
5325 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005326 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005327 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005328
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005329 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005330 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005331 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005332
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005333 break;
5334
5335 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005336 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005337 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005338
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005339 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005340 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005341 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005343 break;
5344
5345 case ACT_REPLACE:
5346 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5347 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5348 /* FIXME: if the user adds a newline in the replacement, the
5349 * index will not be recalculated for now, and the new line
5350 * will not be counted as a new header.
5351 */
5352
5353 cur_end += delta;
5354 cur_next += delta;
5355 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005356 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005357 break;
5358
5359 case ACT_REMOVE:
5360 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5361 cur_next += delta;
5362
Willy Tarreaufa355d42009-11-29 18:12:29 +01005363 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005364 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5365 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005366 cur_hdr->len = 0;
5367 cur_end = NULL; /* null-term has been rewritten */
5368 break;
5369
5370 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005371 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005372 if (cur_end)
5373 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005374
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005375 /* keep the link from this header to next one in case of later
5376 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005377 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005378 old_idx = cur_idx;
5379 }
5380 return 0;
5381}
5382
5383
5384/* Apply the filter to the request line.
5385 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5386 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005387 * Since it can manage the switch to another backend, it updates the per-proxy
5388 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005389 */
5390int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5391{
5392 char term;
5393 char *cur_ptr, *cur_end;
5394 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005395 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005396 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005397
Willy Tarreau58f10d72006-12-04 02:26:12 +01005398
Willy Tarreau3d300592007-03-18 18:34:41 +01005399 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005400 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005401 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005402 (exp->action == ACT_ALLOW ||
5403 exp->action == ACT_DENY ||
5404 exp->action == ACT_TARPIT))
5405 return 0;
5406 else if (exp->action == ACT_REMOVE)
5407 return 0;
5408
5409 done = 0;
5410
Willy Tarreau962c3f42010-01-10 00:15:35 +01005411 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005412 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005413
5414 /* Now we have the request line between cur_ptr and cur_end */
5415
5416 /* The annoying part is that pattern matching needs
5417 * that we modify the contents to null-terminate all
5418 * strings before testing them.
5419 */
5420
5421 term = *cur_end;
5422 *cur_end = '\0';
5423
5424 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5425 switch (exp->action) {
5426 case ACT_SETBE:
5427 /* It is not possible to jump a second time.
5428 * FIXME: should we return an HTTP/500 here so that
5429 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005430 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005431 if (t->be != t->fe)
5432 break;
5433
5434 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005435 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005436 done = 1;
5437 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005439 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005440 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005441 done = 1;
5442 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005443
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005444 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005445 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005446
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005447 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005448 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005449 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005451 done = 1;
5452 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005453
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005454 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005455 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005456
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005457 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005458 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005459 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005461 done = 1;
5462 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005464 case ACT_REPLACE:
5465 *cur_end = term; /* restore the string terminator */
5466 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5467 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5468 /* FIXME: if the user adds a newline in the replacement, the
5469 * index will not be recalculated for now, and the new line
5470 * will not be counted as a new header.
5471 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005472
Willy Tarreaufa355d42009-11-29 18:12:29 +01005473 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005474 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005475 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005476 HTTP_MSG_RQMETH,
5477 cur_ptr, cur_end + 1,
5478 NULL, NULL);
5479 if (unlikely(!cur_end))
5480 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005481
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005482 /* we have a full request and we know that we have either a CR
5483 * or an LF at <ptr>.
5484 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005485 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5486 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005487 /* there is no point trying this regex on headers */
5488 return 1;
5489 }
5490 }
5491 *cur_end = term; /* restore the string terminator */
5492 return done;
5493}
Willy Tarreau97de6242006-12-27 17:18:38 +01005494
Willy Tarreau58f10d72006-12-04 02:26:12 +01005495
Willy Tarreau58f10d72006-12-04 02:26:12 +01005496
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005497/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005498 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005499 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005500 * unparsable request. Since it can manage the switch to another backend, it
5501 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005502 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005503int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005504{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005505 struct http_txn *txn = &s->txn;
5506 struct hdr_exp *exp;
5507
5508 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005509 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005510
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005511 /*
5512 * The interleaving of transformations and verdicts
5513 * makes it difficult to decide to continue or stop
5514 * the evaluation.
5515 */
5516
Willy Tarreau6c123b12010-01-28 20:22:06 +01005517 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5518 break;
5519
Willy Tarreau3d300592007-03-18 18:34:41 +01005520 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005521 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005522 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005523 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005524
5525 /* if this filter had a condition, evaluate it now and skip to
5526 * next filter if the condition does not match.
5527 */
5528 if (exp->cond) {
5529 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5530 ret = acl_pass(ret);
5531 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5532 ret = !ret;
5533
5534 if (!ret)
5535 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005536 }
5537
5538 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005539 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005540 if (unlikely(ret < 0))
5541 return -1;
5542
5543 if (likely(ret == 0)) {
5544 /* The filter did not match the request, it can be
5545 * iterated through all headers.
5546 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005547 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005548 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005549 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005550 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005551}
5552
5553
Willy Tarreaua15645d2007-03-18 16:22:39 +01005554
Willy Tarreau58f10d72006-12-04 02:26:12 +01005555/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005556 * Try to retrieve the server associated to the appsession.
5557 * If the server is found, it's assigned to the session.
5558 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005559void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005560 struct http_txn *txn = &t->txn;
5561 appsess *asession = NULL;
5562 char *sessid_temp = NULL;
5563
Cyril Bontéb21570a2009-11-29 20:04:48 +01005564 if (len > t->be->appsession_len) {
5565 len = t->be->appsession_len;
5566 }
5567
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005568 if (t->be->options2 & PR_O2_AS_REQL) {
5569 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005570 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005571 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005572 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005573 }
5574
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005575 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005576 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5577 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5578 return;
5579 }
5580
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005581 memcpy(txn->sessid, buf, len);
5582 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005583 }
5584
5585 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5586 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5587 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5588 return;
5589 }
5590
Cyril Bontéb21570a2009-11-29 20:04:48 +01005591 memcpy(sessid_temp, buf, len);
5592 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005593
5594 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5595 /* free previously allocated memory */
5596 pool_free2(apools.sessid, sessid_temp);
5597
5598 if (asession != NULL) {
5599 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5600 if (!(t->be->options2 & PR_O2_AS_REQL))
5601 asession->request_count++;
5602
5603 if (asession->serverid != NULL) {
5604 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005605
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005606 while (srv) {
5607 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005608 if ((srv->state & SRV_RUNNING) ||
5609 (t->be->options & PR_O_PERSIST) ||
5610 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005611 /* we found the server and it's usable */
5612 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005613 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005614 t->flags |= SN_DIRECT | SN_ASSIGNED;
5615 t->srv = srv;
5616 break;
5617 } else {
5618 txn->flags &= ~TX_CK_MASK;
5619 txn->flags |= TX_CK_DOWN;
5620 }
5621 }
5622 srv = srv->next;
5623 }
5624 }
5625 }
5626}
5627
5628/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005629 * Manage client-side cookie. It can impact performance by about 2% so it is
5630 * desirable to call it only when needed.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005631 */
5632void manage_client_side_cookies(struct session *t, struct buffer *req)
5633{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005634 struct http_txn *txn = &t->txn;
Willy Tarreau305ae852010-01-03 19:45:54 +01005635 char *p1, *p2, *p3, *p4, *p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005636 char *del_colon, *del_cookie, *colon;
5637 int app_cookies;
5638
Willy Tarreau58f10d72006-12-04 02:26:12 +01005639 char *cur_ptr, *cur_end, *cur_next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005640 int cur_idx, old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005641
Willy Tarreau2a324282006-12-05 00:05:46 +01005642 /* Iterate through the headers.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005643 * we start with the start line.
5644 */
Willy Tarreau83969f42007-01-22 08:55:47 +01005645 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01005646 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005647
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005648 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005649 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005650 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005651
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005652 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau58f10d72006-12-04 02:26:12 +01005653 cur_ptr = cur_next;
5654 cur_end = cur_ptr + cur_hdr->len;
5655 cur_next = cur_end + cur_hdr->cr + 1;
5656
5657 /* We have one full header between cur_ptr and cur_end, and the
5658 * next header starts at cur_next. We're only interested in
5659 * "Cookie:" headers.
5660 */
5661
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005662 val = http_header_match2(cur_ptr, cur_end, "Cookie", 6);
5663 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005664 old_idx = cur_idx;
5665 continue;
5666 }
5667
5668 /* Now look for cookies. Conforming to RFC2109, we have to support
5669 * attributes whose name begin with a '$', and associate them with
5670 * the right cookie, if we want to delete this cookie.
5671 * So there are 3 cases for each cookie read :
5672 * 1) it's a special attribute, beginning with a '$' : ignore it.
5673 * 2) it's a server id cookie that we *MAY* want to delete : save
5674 * some pointers on it (last semi-colon, beginning of cookie...)
5675 * 3) it's an application cookie : we *MAY* have to delete a previous
5676 * "special" cookie.
5677 * At the end of loop, if a "special" cookie remains, we may have to
5678 * remove it. If no application cookie persists in the header, we
5679 * *MUST* delete it
5680 */
5681
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005682 colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005683
Willy Tarreau58f10d72006-12-04 02:26:12 +01005684 /* del_cookie == NULL => nothing to be deleted */
5685 del_colon = del_cookie = NULL;
5686 app_cookies = 0;
5687
5688 while (p1 < cur_end) {
5689 /* skip spaces and colons, but keep an eye on these ones */
Willy Tarreau305ae852010-01-03 19:45:54 +01005690 resync_name:
Willy Tarreau58f10d72006-12-04 02:26:12 +01005691 while (p1 < cur_end) {
5692 if (*p1 == ';' || *p1 == ',')
5693 colon = p1;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02005694 else if (!isspace((unsigned char)*p1))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005695 break;
5696 p1++;
5697 }
5698
5699 if (p1 == cur_end)
5700 break;
5701
5702 /* p1 is at the beginning of the cookie name */
5703 p2 = p1;
Willy Tarreau305ae852010-01-03 19:45:54 +01005704 while (p2 < cur_end && *p2 != '=') {
5705 if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) {
5706 /* oops, the cookie name was truncated, resync */
5707 p1 = p2;
5708 goto resync_name;
5709 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005710 p2++;
Willy Tarreau305ae852010-01-03 19:45:54 +01005711 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005712
5713 if (p2 == cur_end)
5714 break;
5715
5716 p3 = p2 + 1; /* skips the '=' sign */
5717 if (p3 == cur_end)
5718 break;
5719
Willy Tarreau305ae852010-01-03 19:45:54 +01005720 /* parse the value, stripping leading and trailing spaces but keeping insiders. */
5721 p5 = p4 = p3;
5722 while (p5 < cur_end && *p5 != ';' && *p5 != ',') {
5723 if (!isspace((unsigned char)*p5))
5724 p4 = p5 + 1;
5725 p5++;
5726 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005727
5728 /* here, we have the cookie name between p1 and p2,
5729 * and its value between p3 and p4.
5730 * we can process it :
5731 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005732 * Cookie: NAME=VALUE ;
5733 * | || || |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005734 * | || || +--> p4
5735 * | || |+-------> p3
5736 * | || +--------> p2
5737 * | |+------------> p1
5738 * | +-------------> colon
5739 * +--------------------> cur_ptr
5740 */
5741
5742 if (*p1 == '$') {
5743 /* skip this one */
5744 }
5745 else {
5746 /* first, let's see if we want to capture it */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005747 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005748 txn->cli_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005749 (p4 - p1 >= t->fe->capture_namelen) &&
5750 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005751 int log_len = p4 - p1;
5752
Willy Tarreau086b3b42007-05-13 21:45:51 +02005753 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005754 Alert("HTTP logging : out of memory.\n");
5755 } else {
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005756 if (log_len > t->fe->capture_len)
5757 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01005758 memcpy(txn->cli_cookie, p1, log_len);
5759 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005760 }
5761 }
5762
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005763 if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
5764 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005765 /* Cool... it's the right one */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005766 struct server *srv = t->be->srv;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005767 char *delim;
5768
5769 /* if we're in cookie prefix mode, we'll search the delimitor so that we
5770 * have the server ID betweek p3 and delim, and the original cookie between
5771 * delim+1 and p4. Otherwise, delim==p4 :
5772 *
Willy Tarreau305ae852010-01-03 19:45:54 +01005773 * Cookie: NAME=SRV~VALUE ;
5774 * | || || | |+-> p5
Willy Tarreau58f10d72006-12-04 02:26:12 +01005775 * | || || | +--> p4
5776 * | || || +--------> delim
5777 * | || |+-----------> p3
5778 * | || +------------> p2
5779 * | |+----------------> p1
5780 * | +-----------------> colon
5781 * +------------------------> cur_ptr
5782 */
5783
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005784 if (t->be->options & PR_O_COOK_PFX) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005785 for (delim = p3; delim < p4; delim++)
5786 if (*delim == COOKIE_DELIM)
5787 break;
5788 }
5789 else
5790 delim = p4;
5791
5792
5793 /* Here, we'll look for the first running server which supports the cookie.
5794 * This allows to share a same cookie between several servers, for example
5795 * to dedicate backup servers to specific servers only.
5796 * However, to prevent clients from sticking to cookie-less backup server
5797 * when they have incidentely learned an empty cookie, we simply ignore
5798 * empty cookies and mark them as invalid.
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005799 * The same behaviour is applied when persistence must be ignored.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005800 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005801 if ((delim == p3) || (t->flags & SN_IGNORE_PRST))
Willy Tarreau58f10d72006-12-04 02:26:12 +01005802 srv = NULL;
5803
5804 while (srv) {
Willy Tarreau92f2ab12007-02-02 22:14:47 +01005805 if (srv->cookie && (srv->cklen == delim - p3) &&
5806 !memcmp(p3, srv->cookie, delim - p3)) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005807 if ((srv->state & SRV_RUNNING) ||
5808 (t->be->options & PR_O_PERSIST) ||
5809 (t->flags & SN_FORCE_PRST)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005810 /* we found the server and it's usable */
Willy Tarreau3d300592007-03-18 18:34:41 +01005811 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005812 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreau3d300592007-03-18 18:34:41 +01005813 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005814 t->srv = srv;
5815 break;
5816 } else {
5817 /* we found a server, but it's down */
Willy Tarreau3d300592007-03-18 18:34:41 +01005818 txn->flags &= ~TX_CK_MASK;
5819 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005820 }
5821 }
5822 srv = srv->next;
5823 }
5824
Willy Tarreau3d300592007-03-18 18:34:41 +01005825 if (!srv && !(txn->flags & TX_CK_DOWN)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005826 /* no server matched this cookie */
Willy Tarreau3d300592007-03-18 18:34:41 +01005827 txn->flags &= ~TX_CK_MASK;
5828 txn->flags |= TX_CK_INVALID;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005829 }
5830
5831 /* depending on the cookie mode, we may have to either :
5832 * - delete the complete cookie if we're in insert+indirect mode, so that
5833 * the server never sees it ;
5834 * - remove the server id from the cookie value, and tag the cookie as an
5835 * application cookie so that it does not get accidentely removed later,
5836 * if we're in cookie prefix mode
5837 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005838 if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005839 int delta; /* negative */
5840
5841 delta = buffer_replace2(req, p3, delim + 1, NULL, 0);
5842 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005843 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005844 cur_end += delta;
5845 cur_next += delta;
5846 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005847 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005848
5849 del_cookie = del_colon = NULL;
5850 app_cookies++; /* protect the header from deletion */
5851 }
5852 else if (del_cookie == NULL &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02005853 (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 +01005854 del_cookie = p1;
5855 del_colon = colon;
5856 }
5857 } else {
5858 /* now we know that we must keep this cookie since it's
5859 * not ours. But if we wanted to delete our cookie
5860 * earlier, we cannot remove the complete header, but we
5861 * can remove the previous block itself.
5862 */
5863 app_cookies++;
5864
5865 if (del_cookie != NULL) {
5866 int delta; /* negative */
5867
5868 delta = buffer_replace2(req, del_cookie, p1, NULL, 0);
5869 p4 += delta;
Willy Tarreau305ae852010-01-03 19:45:54 +01005870 p5 += delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005871 cur_end += delta;
5872 cur_next += delta;
5873 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005874 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005875 del_cookie = del_colon = NULL;
5876 }
5877 }
5878
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005879 /* Look for the appsession cookie unless persistence must be ignored */
5880 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01005881 int cmp_len, value_len;
5882 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005883
Cyril Bontéb21570a2009-11-29 20:04:48 +01005884 if (t->be->options2 & PR_O2_AS_PFX) {
5885 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
5886 value_begin = p1 + t->be->appsession_name_len;
5887 value_len = p4 - p1 - t->be->appsession_name_len;
5888 } else {
5889 cmp_len = p2 - p1;
5890 value_begin = p3;
5891 value_len = p4 - p3;
5892 }
5893
5894 /* let's see if the cookie is our appcookie */
Cyril Bonté17530c32010-04-06 21:11:10 +02005895 if ((cmp_len == t->be->appsession_name_len) &&
5896 (memcmp(p1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01005897 /* Cool... it's the right one */
5898 manage_client_side_appsession(t, value_begin, value_len);
5899 }
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02005900#if defined(DEBUG_HASH)
5901 Alert("manage_client_side_cookies\n");
5902 appsession_hash_dump(&(t->be->htbl_proxy));
5903#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01005904 }/* end if ((t->proxy->appsession_name != NULL) ... */
5905 }
5906
5907 /* we'll have to look for another cookie ... */
Willy Tarreau305ae852010-01-03 19:45:54 +01005908 p1 = p5;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005909 } /* while (p1 < cur_end) */
5910
5911 /* There's no more cookie on this line.
5912 * We may have marked the last one(s) for deletion.
5913 * We must do this now in two ways :
5914 * - if there is no app cookie, we simply delete the header ;
5915 * - if there are app cookies, we must delete the end of the
5916 * string properly, including the colon/semi-colon before
5917 * the cookie name.
5918 */
5919 if (del_cookie != NULL) {
5920 int delta;
5921 if (app_cookies) {
5922 delta = buffer_replace2(req, del_colon, cur_end, NULL, 0);
5923 cur_end = del_colon;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005924 cur_hdr->len += delta;
5925 } else {
5926 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005927
5928 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005929 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5930 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005931 cur_hdr->len = 0;
5932 }
Willy Tarreau45e73e32006-12-17 00:05:15 +01005933 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005934 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005935 }
5936
5937 /* keep the link from this header to next one */
5938 old_idx = cur_idx;
5939 } /* end of cookie processing on this header */
5940}
5941
5942
Willy Tarreaua15645d2007-03-18 16:22:39 +01005943/* Iterate the same filter through all response headers contained in <rtr>.
5944 * Returns 1 if this filter can be stopped upon return, otherwise 0.
5945 */
5946int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
5947{
5948 char term;
5949 char *cur_ptr, *cur_end, *cur_next;
5950 int cur_idx, old_idx, last_hdr;
5951 struct http_txn *txn = &t->txn;
5952 struct hdr_idx_elem *cur_hdr;
5953 int len, delta;
5954
5955 last_hdr = 0;
5956
Willy Tarreau962c3f42010-01-10 00:15:35 +01005957 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005958 old_idx = 0;
5959
5960 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005961 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01005962 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005963 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01005964 (exp->action == ACT_ALLOW ||
5965 exp->action == ACT_DENY))
5966 return 0;
5967
5968 cur_idx = txn->hdr_idx.v[old_idx].next;
5969 if (!cur_idx)
5970 break;
5971
5972 cur_hdr = &txn->hdr_idx.v[cur_idx];
5973 cur_ptr = cur_next;
5974 cur_end = cur_ptr + cur_hdr->len;
5975 cur_next = cur_end + cur_hdr->cr + 1;
5976
5977 /* Now we have one header between cur_ptr and cur_end,
5978 * and the next header starts at cur_next.
5979 */
5980
5981 /* The annoying part is that pattern matching needs
5982 * that we modify the contents to null-terminate all
5983 * strings before testing them.
5984 */
5985
5986 term = *cur_end;
5987 *cur_end = '\0';
5988
5989 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5990 switch (exp->action) {
5991 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005992 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005993 last_hdr = 1;
5994 break;
5995
5996 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005997 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005998 last_hdr = 1;
5999 break;
6000
6001 case ACT_REPLACE:
6002 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6003 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6004 /* FIXME: if the user adds a newline in the replacement, the
6005 * index will not be recalculated for now, and the new line
6006 * will not be counted as a new header.
6007 */
6008
6009 cur_end += delta;
6010 cur_next += delta;
6011 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006012 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006013 break;
6014
6015 case ACT_REMOVE:
6016 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6017 cur_next += delta;
6018
Willy Tarreaufa355d42009-11-29 18:12:29 +01006019 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006020 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6021 txn->hdr_idx.used--;
6022 cur_hdr->len = 0;
6023 cur_end = NULL; /* null-term has been rewritten */
6024 break;
6025
6026 }
6027 }
6028 if (cur_end)
6029 *cur_end = term; /* restore the string terminator */
6030
6031 /* keep the link from this header to next one in case of later
6032 * removal of next header.
6033 */
6034 old_idx = cur_idx;
6035 }
6036 return 0;
6037}
6038
6039
6040/* Apply the filter to the status line in the response buffer <rtr>.
6041 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6042 * or -1 if a replacement resulted in an invalid status line.
6043 */
6044int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6045{
6046 char term;
6047 char *cur_ptr, *cur_end;
6048 int done;
6049 struct http_txn *txn = &t->txn;
6050 int len, delta;
6051
6052
Willy Tarreau3d300592007-03-18 18:34:41 +01006053 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006054 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006055 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006056 (exp->action == ACT_ALLOW ||
6057 exp->action == ACT_DENY))
6058 return 0;
6059 else if (exp->action == ACT_REMOVE)
6060 return 0;
6061
6062 done = 0;
6063
Willy Tarreau962c3f42010-01-10 00:15:35 +01006064 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006065 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006066
6067 /* Now we have the status line between cur_ptr and cur_end */
6068
6069 /* The annoying part is that pattern matching needs
6070 * that we modify the contents to null-terminate all
6071 * strings before testing them.
6072 */
6073
6074 term = *cur_end;
6075 *cur_end = '\0';
6076
6077 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6078 switch (exp->action) {
6079 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006080 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006081 done = 1;
6082 break;
6083
6084 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006085 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006086 done = 1;
6087 break;
6088
6089 case ACT_REPLACE:
6090 *cur_end = term; /* restore the string terminator */
6091 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6092 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6093 /* FIXME: if the user adds a newline in the replacement, the
6094 * index will not be recalculated for now, and the new line
6095 * will not be counted as a new header.
6096 */
6097
Willy Tarreaufa355d42009-11-29 18:12:29 +01006098 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006099 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006100 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006101 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006102 cur_ptr, cur_end + 1,
6103 NULL, NULL);
6104 if (unlikely(!cur_end))
6105 return -1;
6106
6107 /* we have a full respnse and we know that we have either a CR
6108 * or an LF at <ptr>.
6109 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006110 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006111 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006112 /* there is no point trying this regex on headers */
6113 return 1;
6114 }
6115 }
6116 *cur_end = term; /* restore the string terminator */
6117 return done;
6118}
6119
6120
6121
6122/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006123 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006124 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6125 * unparsable response.
6126 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006127int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006128{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006129 struct http_txn *txn = &s->txn;
6130 struct hdr_exp *exp;
6131
6132 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006133 int ret;
6134
6135 /*
6136 * The interleaving of transformations and verdicts
6137 * makes it difficult to decide to continue or stop
6138 * the evaluation.
6139 */
6140
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006141 if (txn->flags & TX_SVDENY)
6142 break;
6143
Willy Tarreau3d300592007-03-18 18:34:41 +01006144 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006145 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6146 exp->action == ACT_PASS)) {
6147 exp = exp->next;
6148 continue;
6149 }
6150
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006151 /* if this filter had a condition, evaluate it now and skip to
6152 * next filter if the condition does not match.
6153 */
6154 if (exp->cond) {
6155 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6156 ret = acl_pass(ret);
6157 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6158 ret = !ret;
6159 if (!ret)
6160 continue;
6161 }
6162
Willy Tarreaua15645d2007-03-18 16:22:39 +01006163 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006164 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006165 if (unlikely(ret < 0))
6166 return -1;
6167
6168 if (likely(ret == 0)) {
6169 /* The filter did not match the response, it can be
6170 * iterated through all headers.
6171 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006172 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006173 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006174 }
6175 return 0;
6176}
6177
6178
6179
6180/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006181 * Manage server-side cookies. It can impact performance by about 2% so it is
6182 * desirable to call it only when needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006183 */
6184void manage_server_side_cookies(struct session *t, struct buffer *rtr)
6185{
6186 struct http_txn *txn = &t->txn;
6187 char *p1, *p2, *p3, *p4;
6188
Willy Tarreaua15645d2007-03-18 16:22:39 +01006189 char *cur_ptr, *cur_end, *cur_next;
6190 int cur_idx, old_idx, delta;
6191
Willy Tarreaua15645d2007-03-18 16:22:39 +01006192 /* Iterate through the headers.
6193 * we start with the start line.
6194 */
6195 old_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006196 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006197
6198 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6199 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006200 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006201
6202 cur_hdr = &txn->hdr_idx.v[cur_idx];
6203 cur_ptr = cur_next;
6204 cur_end = cur_ptr + cur_hdr->len;
6205 cur_next = cur_end + cur_hdr->cr + 1;
6206
6207 /* We have one full header between cur_ptr and cur_end, and the
6208 * next header starts at cur_next. We're only interested in
6209 * "Cookie:" headers.
6210 */
6211
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006212 val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10);
6213 if (!val) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006214 old_idx = cur_idx;
6215 continue;
6216 }
6217
6218 /* OK, right now we know we have a set-cookie at cur_ptr */
Willy Tarreau3d300592007-03-18 18:34:41 +01006219 txn->flags |= TX_SCK_ANY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006220
6221
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006222 /* maybe we only wanted to see if there was a set-cookie. Note that
6223 * the cookie capture is declared in the fronend.
6224 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006225 if (t->be->cookie_name == NULL &&
6226 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006227 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006228 return;
6229
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006230 p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006231
6232 while (p1 < cur_end) { /* in fact, we'll break after the first cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006233 if (p1 == cur_end || *p1 == ';') /* end of cookie */
6234 break;
6235
6236 /* p1 is at the beginning of the cookie name */
6237 p2 = p1;
6238
6239 while (p2 < cur_end && *p2 != '=' && *p2 != ';')
6240 p2++;
6241
6242 if (p2 == cur_end || *p2 == ';') /* next cookie */
6243 break;
6244
6245 p3 = p2 + 1; /* skip the '=' sign */
6246 if (p3 == cur_end)
6247 break;
6248
6249 p4 = p3;
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006250 while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';')
Willy Tarreaua15645d2007-03-18 16:22:39 +01006251 p4++;
6252
6253 /* here, we have the cookie name between p1 and p2,
6254 * and its value between p3 and p4.
6255 * we can process it.
6256 */
6257
6258 /* first, let's see if we want to capture it */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006259 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006260 txn->srv_cookie == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006261 (p4 - p1 >= t->fe->capture_namelen) &&
6262 memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006263 int log_len = p4 - p1;
6264
Willy Tarreau086b3b42007-05-13 21:45:51 +02006265 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006266 Alert("HTTP logging : out of memory.\n");
6267 }
6268
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006269 if (log_len > t->fe->capture_len)
6270 log_len = t->fe->capture_len;
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006271 memcpy(txn->srv_cookie, p1, log_len);
6272 txn->srv_cookie[log_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006273 }
6274
6275 /* now check if we need to process it for persistence */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006276 if (!(t->flags & SN_IGNORE_PRST) && (p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006277 (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006278 /* Cool... it's the right one */
Willy Tarreau3d300592007-03-18 18:34:41 +01006279 txn->flags |= TX_SCK_SEEN;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006280
6281 /* If the cookie is in insert mode on a known server, we'll delete
6282 * this occurrence because we'll insert another one later.
6283 * We'll delete it too if the "indirect" option is set and we're in
6284 * a direct access. */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006285 if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
6286 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006287 /* this header must be deleted */
6288 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6289 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6290 txn->hdr_idx.used--;
6291 cur_hdr->len = 0;
6292 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006293 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006294
Willy Tarreau3d300592007-03-18 18:34:41 +01006295 txn->flags |= TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006296 }
6297 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006298 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006299 /* replace bytes p3->p4 with the cookie name associated
6300 * with this server since we know it.
6301 */
6302 delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen);
6303 cur_hdr->len += delta;
6304 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006305 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006306
Willy Tarreau3d300592007-03-18 18:34:41 +01006307 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006308 }
6309 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006310 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006311 /* insert the cookie name associated with this server
6312 * before existing cookie, and insert a delimitor between them..
6313 */
6314 delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1);
6315 cur_hdr->len += delta;
6316 cur_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006317 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006318
6319 p3[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreau3d300592007-03-18 18:34:41 +01006320 txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006321 }
6322 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006323 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6324 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006325 int cmp_len, value_len;
6326 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006327
Cyril Bontéb21570a2009-11-29 20:04:48 +01006328 if (t->be->options2 & PR_O2_AS_PFX) {
6329 cmp_len = MIN(p4 - p1, t->be->appsession_name_len);
6330 value_begin = p1 + t->be->appsession_name_len;
6331 value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len);
6332 } else {
6333 cmp_len = p2 - p1;
6334 value_begin = p3;
6335 value_len = MIN(t->be->appsession_len, p4 - p3);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006336 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006337
Cyril Bonté17530c32010-04-06 21:11:10 +02006338 if ((cmp_len == t->be->appsession_name_len) &&
6339 (memcmp(p1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006340 /* Cool... it's the right one */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006341 if (txn->sessid != NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006342 /* free previously allocated memory as we don't need it anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006343 pool_free2(apools.sessid, txn->sessid);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006344 }
6345 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006346 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006347 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6348 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6349 return;
6350 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006351 memcpy(txn->sessid, value_begin, value_len);
6352 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006353 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006354 } /* end if ((t->be->appsession_name != NULL) ... */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006355 break; /* we don't want to loop again since there cannot be another cookie on the same line */
6356 } /* we're now at the end of the cookie value */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006357 /* keep the link from this header to next one */
6358 old_idx = cur_idx;
6359 } /* end of cookie processing on this header */
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006360
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006361 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006362 appsess *asession = NULL;
6363 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006364 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006365 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006366 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006367 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6368 Alert("Not enough Memory process_srv():asession:calloc().\n");
6369 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6370 return;
6371 }
6372 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6373 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6374 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006375 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006376 return;
6377 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006378 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006379 asession->sessid[t->be->appsession_len] = 0;
6380
Willy Tarreau1fac7532010-01-09 19:23:06 +01006381 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006382 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
6383 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6384 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006385 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006386 return;
6387 }
6388 asession->serverid[0] = '\0';
6389 memcpy(asession->serverid, t->srv->id, server_id_len);
6390
6391 asession->request_count = 0;
6392 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6393 }
6394
6395 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6396 asession->request_count++;
6397 }
6398
6399#if defined(DEBUG_HASH)
Cyril Bonté17530c32010-04-06 21:11:10 +02006400 if (t->be->appsession_name) {
6401 Alert("manage_server_side_cookies\n");
6402 appsession_hash_dump(&(t->be->htbl_proxy));
6403 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006404#endif
Willy Tarreaua15645d2007-03-18 16:22:39 +01006405}
6406
6407
6408
6409/*
6410 * Check if response is cacheable or not. Updates t->flags.
6411 */
6412void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6413{
6414 struct http_txn *txn = &t->txn;
6415 char *p1, *p2;
6416
6417 char *cur_ptr, *cur_end, *cur_next;
6418 int cur_idx;
6419
Willy Tarreau5df51872007-11-25 16:20:08 +01006420 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006421 return;
6422
6423 /* Iterate through the headers.
6424 * we start with the start line.
6425 */
6426 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006427 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006428
6429 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6430 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006431 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006432
6433 cur_hdr = &txn->hdr_idx.v[cur_idx];
6434 cur_ptr = cur_next;
6435 cur_end = cur_ptr + cur_hdr->len;
6436 cur_next = cur_end + cur_hdr->cr + 1;
6437
6438 /* We have one full header between cur_ptr and cur_end, and the
6439 * next header starts at cur_next. We're only interested in
6440 * "Cookie:" headers.
6441 */
6442
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006443 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6444 if (val) {
6445 if ((cur_end - (cur_ptr + val) >= 8) &&
6446 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6447 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6448 return;
6449 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006450 }
6451
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006452 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6453 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006454 continue;
6455
6456 /* OK, right now we know we have a cache-control header at cur_ptr */
6457
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006458 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006459
6460 if (p1 >= cur_end) /* no more info */
6461 continue;
6462
6463 /* p1 is at the beginning of the value */
6464 p2 = p1;
6465
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006466 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006467 p2++;
6468
6469 /* we have a complete value between p1 and p2 */
6470 if (p2 < cur_end && *p2 == '=') {
6471 /* we have something of the form no-cache="set-cookie" */
6472 if ((cur_end - p1 >= 21) &&
6473 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6474 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006475 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006476 continue;
6477 }
6478
6479 /* OK, so we know that either p2 points to the end of string or to a comma */
6480 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
6481 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
6482 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6483 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006484 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006485 return;
6486 }
6487
6488 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006489 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006490 continue;
6491 }
6492 }
6493}
6494
6495
Willy Tarreau58f10d72006-12-04 02:26:12 +01006496/*
6497 * Try to retrieve a known appsession in the URI, then the associated server.
6498 * If the server is found, it's assigned to the session.
6499 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006500void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006501{
Cyril Bontéb21570a2009-11-29 20:04:48 +01006502 char *end_params, *first_param, *cur_param, *next_param;
6503 char separator;
6504 int value_len;
6505
6506 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006507
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006508 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02006509 (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 +01006510 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006511 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006512
Cyril Bontéb21570a2009-11-29 20:04:48 +01006513 first_param = NULL;
6514 switch (mode) {
6515 case PR_O2_AS_M_PP:
6516 first_param = memchr(begin, ';', len);
6517 break;
6518 case PR_O2_AS_M_QS:
6519 first_param = memchr(begin, '?', len);
6520 break;
6521 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006522
Cyril Bontéb21570a2009-11-29 20:04:48 +01006523 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006524 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006525 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006526
Cyril Bontéb21570a2009-11-29 20:04:48 +01006527 switch (mode) {
6528 case PR_O2_AS_M_PP:
6529 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
6530 end_params = (char *) begin + len;
6531 }
6532 separator = ';';
6533 break;
6534 case PR_O2_AS_M_QS:
6535 end_params = (char *) begin + len;
6536 separator = '&';
6537 break;
6538 default:
6539 /* unknown mode, shouldn't happen */
6540 return;
6541 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006542
Cyril Bontéb21570a2009-11-29 20:04:48 +01006543 cur_param = next_param = end_params;
6544 while (cur_param > first_param) {
6545 cur_param--;
6546 if ((cur_param[0] == separator) || (cur_param == first_param)) {
6547 /* let's see if this is the appsession parameter */
6548 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
6549 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
6550 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6551 /* Cool... it's the right one */
6552 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
6553 value_len = MIN(t->be->appsession_len, next_param - cur_param);
6554 if (value_len > 0) {
6555 manage_client_side_appsession(t, cur_param, value_len);
6556 }
6557 break;
6558 }
6559 next_param = cur_param;
6560 }
6561 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006562#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006563 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02006564 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01006565#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01006566}
6567
Willy Tarreaub2513902006-12-17 14:52:38 +01006568/*
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006569 * In a GET or HEAD request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006570 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01006571 *
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006572 * It is assumed that the request is either a HEAD or GET and that the
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006573 * t->be->uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01006574 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006575 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01006576 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006577int stats_check_uri(struct session *t, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01006578{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006579 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01006580 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006581 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01006582
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01006583 if (!uri_auth)
6584 return 0;
6585
6586 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD)
6587 return 0;
6588
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006589 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
6590
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006591 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006592 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01006593 return 0;
6594
Willy Tarreau962c3f42010-01-10 00:15:35 +01006595 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006596
Willy Tarreau0214c3a2007-01-07 13:47:30 +01006597 /* the URI is in h */
6598 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01006599 return 0;
6600
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006601 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006602 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006603 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006604 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006605 break;
6606 }
6607 h++;
6608 }
6609
6610 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01006611 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6612 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006613 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006614 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02006615 break;
6616 }
6617 h++;
6618 }
6619 }
6620
Willy Tarreau962c3f42010-01-10 00:15:35 +01006621 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
6622 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02006623 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006624 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02006625 break;
6626 }
6627 h++;
6628 }
6629
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01006630 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
6631
Willy Tarreaub2513902006-12-17 14:52:38 +01006632 return 1;
6633}
6634
Willy Tarreau4076a152009-04-02 15:18:36 +02006635/*
6636 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01006637 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
6638 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02006639 */
6640void http_capture_bad_message(struct error_snapshot *es, struct session *s,
6641 struct buffer *buf, struct http_msg *msg,
6642 struct proxy *other_end)
6643{
Willy Tarreau2df8d712009-05-01 11:33:17 +02006644 es->len = buf->r - (buf->data + msg->som);
6645 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau4076a152009-04-02 15:18:36 +02006646 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02006647 es->pos = msg->err_pos - msg->som;
Willy Tarreau4076a152009-04-02 15:18:36 +02006648 else
Willy Tarreau2df8d712009-05-01 11:33:17 +02006649 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau4076a152009-04-02 15:18:36 +02006650 es->when = date; // user-visible date
6651 es->sid = s->uniq_id;
6652 es->srv = s->srv;
6653 es->oe = other_end;
6654 es->src = s->cli_addr;
6655}
Willy Tarreaub2513902006-12-17 14:52:38 +01006656
Willy Tarreaubce70882009-09-07 11:51:47 +02006657/* return the IP address pointed to by occurrence <occ> of header <hname> in
6658 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
6659 * occurrence number corresponding to this value is returned. If <occ> is
6660 * strictly negative, the occurrence number before the end corresponding to
6661 * this value is returned. If <occ> is null, any value is returned, so it is
6662 * not recommended to use it that way. Negative occurrences are limited to
6663 * a small value because it is required to keep them in memory while scanning.
6664 * IP address 0.0.0.0 is returned if no match is found.
6665 */
6666unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
6667{
6668 struct hdr_ctx ctx;
6669 unsigned int hdr_hist[MAX_HDR_HISTORY];
6670 unsigned int hist_ptr;
6671 int found = 0;
6672
6673 ctx.idx = 0;
6674 if (occ >= 0) {
6675 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
6676 occ--;
6677 if (occ <= 0) {
6678 found = 1;
6679 break;
6680 }
6681 }
6682 if (!found)
6683 return 0;
6684 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
6685 }
6686
6687 /* negative occurrence, we scan all the list then walk back */
6688 if (-occ > MAX_HDR_HISTORY)
6689 return 0;
6690
6691 hist_ptr = 0;
6692 hdr_hist[hist_ptr] = 0;
6693 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
6694 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
6695 if (hist_ptr >= MAX_HDR_HISTORY)
6696 hist_ptr = 0;
6697 found++;
6698 }
6699 if (-occ > found)
6700 return 0;
6701 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
6702 * find occurrence -occ, so we have to check [hist_ptr+occ].
6703 */
6704 hist_ptr += occ;
6705 if (hist_ptr >= MAX_HDR_HISTORY)
6706 hist_ptr -= MAX_HDR_HISTORY;
6707 return hdr_hist[hist_ptr];
6708}
6709
Willy Tarreaubaaee002006-06-26 02:48:02 +02006710/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01006711 * Print a debug line with a header
6712 */
6713void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
6714{
6715 int len, max;
6716 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02006717 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006718 max = end - start;
6719 UBOUND(max, sizeof(trash) - len - 1);
6720 len += strlcpy2(trash + len, start, max + 1);
6721 trash[len++] = '\n';
6722 write(1, trash, len);
6723}
6724
Willy Tarreau0937bc42009-12-22 15:03:09 +01006725/*
6726 * Initialize a new HTTP transaction for session <s>. It is assumed that all
6727 * the required fields are properly allocated and that we only need to (re)init
6728 * them. This should be used before processing any new request.
6729 */
6730void http_init_txn(struct session *s)
6731{
6732 struct http_txn *txn = &s->txn;
6733 struct proxy *fe = s->fe;
6734
6735 txn->flags = 0;
6736 txn->status = -1;
6737
6738 txn->req.sol = txn->req.eol = NULL;
6739 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
6740 txn->rsp.sol = txn->rsp.eol = NULL;
6741 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
6742 txn->req.hdr_content_len = 0LL;
6743 txn->rsp.hdr_content_len = 0LL;
6744 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
6745 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01006746
6747 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006748
6749 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
6750 if (fe->options2 & PR_O2_REQBUG_OK)
6751 txn->req.err_pos = -1; /* let buggy requests pass */
6752
Willy Tarreau46023632010-01-07 22:51:47 +01006753 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006754 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
6755
Willy Tarreau46023632010-01-07 22:51:47 +01006756 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01006757 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
6758
6759 if (txn->hdr_idx.v)
6760 hdr_idx_init(&txn->hdr_idx);
6761}
6762
6763/* to be used at the end of a transaction */
6764void http_end_txn(struct session *s)
6765{
6766 struct http_txn *txn = &s->txn;
6767
6768 /* these ones will have been dynamically allocated */
6769 pool_free2(pool2_requri, txn->uri);
6770 pool_free2(pool2_capture, txn->cli_cookie);
6771 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006772 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01006773
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006774 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006775 txn->uri = NULL;
6776 txn->srv_cookie = NULL;
6777 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01006778
6779 if (txn->req.cap) {
6780 struct cap_hdr *h;
6781 for (h = s->fe->req_cap; h; h = h->next)
6782 pool_free2(h->pool, txn->req.cap[h->index]);
6783 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
6784 }
6785
6786 if (txn->rsp.cap) {
6787 struct cap_hdr *h;
6788 for (h = s->fe->rsp_cap; h; h = h->next)
6789 pool_free2(h->pool, txn->rsp.cap[h->index]);
6790 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
6791 }
6792
Willy Tarreau0937bc42009-12-22 15:03:09 +01006793}
6794
6795/* to be used at the end of a transaction to prepare a new one */
6796void http_reset_txn(struct session *s)
6797{
6798 http_end_txn(s);
6799 http_init_txn(s);
6800
6801 s->be = s->fe;
6802 s->req->analysers = s->listener->analysers;
6803 s->logs.logwait = s->fe->to_log;
6804 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01006805 /* re-init store persistence */
6806 s->store_count = 0;
6807
Willy Tarreau0937bc42009-12-22 15:03:09 +01006808 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006809
6810 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
6811
Willy Tarreau739cfba2010-01-25 23:11:14 +01006812 /* We must trim any excess data from the response buffer, because we
6813 * may have blocked an invalid response from a server that we don't
6814 * want to accidentely forward once we disable the analysers, nor do
6815 * we want those data to come along with next response. A typical
6816 * example of such data would be from a buggy server responding to
6817 * a HEAD with some data, or sending more than the advertised
6818 * content-length.
6819 */
6820 if (unlikely(s->rep->l > s->rep->send_max)) {
6821 s->rep->l = s->rep->send_max;
6822 s->rep->r = s->rep->w + s->rep->l;
6823 if (s->rep->r >= s->rep->data + s->rep->size)
6824 s->rep->r -= s->rep->size;
6825 }
6826
Willy Tarreau0937bc42009-12-22 15:03:09 +01006827 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02006828 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006829
Willy Tarreaud04e8582010-05-31 12:31:35 +02006830 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006831 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01006832
6833 s->req->rex = TICK_ETERNITY;
6834 s->req->wex = TICK_ETERNITY;
6835 s->req->analyse_exp = TICK_ETERNITY;
6836 s->rep->rex = TICK_ETERNITY;
6837 s->rep->wex = TICK_ETERNITY;
6838 s->rep->analyse_exp = TICK_ETERNITY;
6839}
Willy Tarreau58f10d72006-12-04 02:26:12 +01006840
Willy Tarreau8797c062007-05-07 00:55:35 +02006841/************************************************************************/
6842/* The code below is dedicated to ACL parsing and matching */
6843/************************************************************************/
6844
6845
6846
6847
6848/* 1. Check on METHOD
6849 * We use the pre-parsed method if it is known, and store its number as an
6850 * integer. If it is unknown, we use the pointer and the length.
6851 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006852static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006853{
6854 int len, meth;
6855
Willy Tarreauae8b7962007-06-09 23:10:04 +02006856 len = strlen(*text);
6857 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02006858
6859 pattern->val.i = meth;
6860 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02006861 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006862 if (!pattern->ptr.str)
6863 return 0;
6864 pattern->len = len;
6865 }
6866 return 1;
6867}
6868
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006869static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006870acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
6871 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006872{
6873 int meth;
6874 struct http_txn *txn = l7;
6875
Willy Tarreaub6866442008-07-14 23:54:42 +02006876 if (!txn)
6877 return 0;
6878
Willy Tarreau655dce92009-11-08 13:10:58 +01006879 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006880 return 0;
6881
Willy Tarreau8797c062007-05-07 00:55:35 +02006882 meth = txn->meth;
6883 test->i = meth;
6884 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02006885 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
6886 /* ensure the indexes are not affected */
6887 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02006888 test->len = txn->req.sl.rq.m_l;
6889 test->ptr = txn->req.sol;
6890 }
6891 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6892 return 1;
6893}
6894
6895static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
6896{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006897 int icase;
6898
Willy Tarreau8797c062007-05-07 00:55:35 +02006899 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02006900 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02006901
6902 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02006903 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006904
6905 /* Other method, we must compare the strings */
6906 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02006907 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02006908
6909 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
6910 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
6911 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02006912 return ACL_PAT_FAIL;
6913 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02006914}
6915
6916/* 2. Check on Request/Status Version
6917 * We simply compare strings here.
6918 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02006919static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02006920{
Willy Tarreauae8b7962007-06-09 23:10:04 +02006921 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006922 if (!pattern->ptr.str)
6923 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02006924 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02006925 return 1;
6926}
6927
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006928static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006929acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
6930 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006931{
6932 struct http_txn *txn = l7;
6933 char *ptr;
6934 int len;
6935
Willy Tarreaub6866442008-07-14 23:54:42 +02006936 if (!txn)
6937 return 0;
6938
Willy Tarreau655dce92009-11-08 13:10:58 +01006939 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006940 return 0;
6941
Willy Tarreau8797c062007-05-07 00:55:35 +02006942 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006943 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02006944
6945 while ((len-- > 0) && (*ptr++ != '/'));
6946 if (len <= 0)
6947 return 0;
6948
6949 test->ptr = ptr;
6950 test->len = len;
6951
6952 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6953 return 1;
6954}
6955
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006956static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006957acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
6958 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006959{
6960 struct http_txn *txn = l7;
6961 char *ptr;
6962 int len;
6963
Willy Tarreaub6866442008-07-14 23:54:42 +02006964 if (!txn)
6965 return 0;
6966
Willy Tarreau655dce92009-11-08 13:10:58 +01006967 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006968 return 0;
6969
Willy Tarreau8797c062007-05-07 00:55:35 +02006970 len = txn->rsp.sl.st.v_l;
6971 ptr = txn->rsp.sol;
6972
6973 while ((len-- > 0) && (*ptr++ != '/'));
6974 if (len <= 0)
6975 return 0;
6976
6977 test->ptr = ptr;
6978 test->len = len;
6979
6980 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
6981 return 1;
6982}
6983
6984/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02006985static int
Willy Tarreau97be1452007-06-10 11:47:14 +02006986acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
6987 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02006988{
6989 struct http_txn *txn = l7;
6990 char *ptr;
6991 int len;
6992
Willy Tarreaub6866442008-07-14 23:54:42 +02006993 if (!txn)
6994 return 0;
6995
Willy Tarreau655dce92009-11-08 13:10:58 +01006996 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02006997 return 0;
6998
Willy Tarreau8797c062007-05-07 00:55:35 +02006999 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007000 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007001
7002 test->i = __strl2ui(ptr, len);
7003 test->flags = ACL_TEST_F_VOL_1ST;
7004 return 1;
7005}
7006
7007/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007008static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007009acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7010 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007011{
7012 struct http_txn *txn = l7;
7013
Willy Tarreaub6866442008-07-14 23:54:42 +02007014 if (!txn)
7015 return 0;
7016
Willy Tarreau655dce92009-11-08 13:10:58 +01007017 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007018 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007019
Willy Tarreauc11416f2007-06-17 16:58:38 +02007020 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7021 /* ensure the indexes are not affected */
7022 return 0;
7023
Willy Tarreau8797c062007-05-07 00:55:35 +02007024 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007025 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007026
Willy Tarreauf3d25982007-05-08 22:45:09 +02007027 /* we do not need to set READ_ONLY because the data is in a buffer */
7028 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007029 return 1;
7030}
7031
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007032static int
7033acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7034 struct acl_expr *expr, struct acl_test *test)
7035{
7036 struct http_txn *txn = l7;
7037
Willy Tarreaub6866442008-07-14 23:54:42 +02007038 if (!txn)
7039 return 0;
7040
Willy Tarreau655dce92009-11-08 13:10:58 +01007041 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007042 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007043
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007044 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7045 /* ensure the indexes are not affected */
7046 return 0;
7047
7048 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007049 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 +01007050 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
7051 test->i = AF_INET;
7052
7053 /*
7054 * If we are parsing url in frontend space, we prepare backend stage
7055 * to not parse again the same url ! optimization lazyness...
7056 */
7057 if (px->options & PR_O_HTTP_PROXY)
7058 l4->flags |= SN_ADDR_SET;
7059
7060 test->flags = ACL_TEST_F_READ_ONLY;
7061 return 1;
7062}
7063
7064static int
7065acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7066 struct acl_expr *expr, struct acl_test *test)
7067{
7068 struct http_txn *txn = l7;
7069
Willy Tarreaub6866442008-07-14 23:54:42 +02007070 if (!txn)
7071 return 0;
7072
Willy Tarreau655dce92009-11-08 13:10:58 +01007073 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007074 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007075
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007076 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7077 /* ensure the indexes are not affected */
7078 return 0;
7079
7080 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007081 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 +01007082 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
7083
7084 if (px->options & PR_O_HTTP_PROXY)
7085 l4->flags |= SN_ADDR_SET;
7086
7087 test->flags = ACL_TEST_F_READ_ONLY;
7088 return 1;
7089}
7090
Willy Tarreauc11416f2007-06-17 16:58:38 +02007091/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7092 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7093 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007094static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007095acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007096 struct acl_expr *expr, struct acl_test *test)
7097{
7098 struct http_txn *txn = l7;
7099 struct hdr_idx *idx = &txn->hdr_idx;
7100 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007101
Willy Tarreaub6866442008-07-14 23:54:42 +02007102 if (!txn)
7103 return 0;
7104
Willy Tarreau33a7e692007-06-10 19:45:56 +02007105 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7106 /* search for header from the beginning */
7107 ctx->idx = 0;
7108
Willy Tarreau33a7e692007-06-10 19:45:56 +02007109 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7110 test->flags |= ACL_TEST_F_FETCH_MORE;
7111 test->flags |= ACL_TEST_F_VOL_HDR;
7112 test->len = ctx->vlen;
7113 test->ptr = (char *)ctx->line + ctx->val;
7114 return 1;
7115 }
7116
7117 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7118 test->flags |= ACL_TEST_F_VOL_HDR;
7119 return 0;
7120}
7121
Willy Tarreau33a7e692007-06-10 19:45:56 +02007122static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007123acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7124 struct acl_expr *expr, struct acl_test *test)
7125{
7126 struct http_txn *txn = l7;
7127
Willy Tarreaub6866442008-07-14 23:54:42 +02007128 if (!txn)
7129 return 0;
7130
Willy Tarreau655dce92009-11-08 13:10:58 +01007131 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007132 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007133
Willy Tarreauc11416f2007-06-17 16:58:38 +02007134 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7135 /* ensure the indexes are not affected */
7136 return 0;
7137
7138 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7139}
7140
7141static int
7142acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7143 struct acl_expr *expr, struct acl_test *test)
7144{
7145 struct http_txn *txn = l7;
7146
Willy Tarreaub6866442008-07-14 23:54:42 +02007147 if (!txn)
7148 return 0;
7149
Willy Tarreau655dce92009-11-08 13:10:58 +01007150 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007151 return 0;
7152
7153 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7154}
7155
7156/* 6. Check on HTTP header count. The number of occurrences is returned.
7157 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7158 */
7159static int
7160acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007161 struct acl_expr *expr, struct acl_test *test)
7162{
7163 struct http_txn *txn = l7;
7164 struct hdr_idx *idx = &txn->hdr_idx;
7165 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007166 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007167
Willy Tarreaub6866442008-07-14 23:54:42 +02007168 if (!txn)
7169 return 0;
7170
Willy Tarreau33a7e692007-06-10 19:45:56 +02007171 ctx.idx = 0;
7172 cnt = 0;
7173 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7174 cnt++;
7175
7176 test->i = cnt;
7177 test->flags = ACL_TEST_F_VOL_HDR;
7178 return 1;
7179}
7180
Willy Tarreauc11416f2007-06-17 16:58:38 +02007181static int
7182acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7183 struct acl_expr *expr, struct acl_test *test)
7184{
7185 struct http_txn *txn = l7;
7186
Willy Tarreaub6866442008-07-14 23:54:42 +02007187 if (!txn)
7188 return 0;
7189
Willy Tarreau655dce92009-11-08 13:10:58 +01007190 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007191 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007192
Willy Tarreauc11416f2007-06-17 16:58:38 +02007193 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7194 /* ensure the indexes are not affected */
7195 return 0;
7196
7197 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7198}
7199
7200static int
7201acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7202 struct acl_expr *expr, struct acl_test *test)
7203{
7204 struct http_txn *txn = l7;
7205
Willy Tarreaub6866442008-07-14 23:54:42 +02007206 if (!txn)
7207 return 0;
7208
Willy Tarreau655dce92009-11-08 13:10:58 +01007209 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007210 return 0;
7211
7212 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7213}
7214
Willy Tarreau33a7e692007-06-10 19:45:56 +02007215/* 7. Check on HTTP header's integer value. The integer value is returned.
7216 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007217 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007218 */
7219static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007220acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007221 struct acl_expr *expr, struct acl_test *test)
7222{
7223 struct http_txn *txn = l7;
7224 struct hdr_idx *idx = &txn->hdr_idx;
7225 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007226
Willy Tarreaub6866442008-07-14 23:54:42 +02007227 if (!txn)
7228 return 0;
7229
Willy Tarreau33a7e692007-06-10 19:45:56 +02007230 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7231 /* search for header from the beginning */
7232 ctx->idx = 0;
7233
Willy Tarreau33a7e692007-06-10 19:45:56 +02007234 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7235 test->flags |= ACL_TEST_F_FETCH_MORE;
7236 test->flags |= ACL_TEST_F_VOL_HDR;
7237 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
7238 return 1;
7239 }
7240
7241 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7242 test->flags |= ACL_TEST_F_VOL_HDR;
7243 return 0;
7244}
7245
Willy Tarreauc11416f2007-06-17 16:58:38 +02007246static int
7247acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7248 struct acl_expr *expr, struct acl_test *test)
7249{
7250 struct http_txn *txn = l7;
7251
Willy Tarreaub6866442008-07-14 23:54:42 +02007252 if (!txn)
7253 return 0;
7254
Willy Tarreau655dce92009-11-08 13:10:58 +01007255 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007256 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007257
Willy Tarreauc11416f2007-06-17 16:58:38 +02007258 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7259 /* ensure the indexes are not affected */
7260 return 0;
7261
7262 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7263}
7264
7265static int
7266acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7267 struct acl_expr *expr, struct acl_test *test)
7268{
7269 struct http_txn *txn = l7;
7270
Willy Tarreaub6866442008-07-14 23:54:42 +02007271 if (!txn)
7272 return 0;
7273
Willy Tarreau655dce92009-11-08 13:10:58 +01007274 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007275 return 0;
7276
7277 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7278}
7279
Willy Tarreau106f9792009-09-19 07:54:16 +02007280/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7281 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7282 */
7283static int
7284acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7285 struct acl_expr *expr, struct acl_test *test)
7286{
7287 struct http_txn *txn = l7;
7288 struct hdr_idx *idx = &txn->hdr_idx;
7289 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7290
7291 if (!txn)
7292 return 0;
7293
7294 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7295 /* search for header from the beginning */
7296 ctx->idx = 0;
7297
7298 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7299 test->flags |= ACL_TEST_F_FETCH_MORE;
7300 test->flags |= ACL_TEST_F_VOL_HDR;
7301 /* Same optimization as url_ip */
7302 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
7303 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
7304 test->ptr = (void *)&l4->srv_addr.sin_addr;
7305 test->i = AF_INET;
7306 return 1;
7307 }
7308
7309 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7310 test->flags |= ACL_TEST_F_VOL_HDR;
7311 return 0;
7312}
7313
7314static int
7315acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7316 struct acl_expr *expr, struct acl_test *test)
7317{
7318 struct http_txn *txn = l7;
7319
7320 if (!txn)
7321 return 0;
7322
Willy Tarreau655dce92009-11-08 13:10:58 +01007323 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007324 return 0;
7325
7326 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7327 /* ensure the indexes are not affected */
7328 return 0;
7329
7330 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
7331}
7332
7333static int
7334acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7335 struct acl_expr *expr, struct acl_test *test)
7336{
7337 struct http_txn *txn = l7;
7338
7339 if (!txn)
7340 return 0;
7341
Willy Tarreau655dce92009-11-08 13:10:58 +01007342 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007343 return 0;
7344
7345 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
7346}
7347
Willy Tarreau737b0c12007-06-10 21:28:46 +02007348/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7349 * the first '/' after the possible hostname, and ends before the possible '?'.
7350 */
7351static int
7352acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
7353 struct acl_expr *expr, struct acl_test *test)
7354{
7355 struct http_txn *txn = l7;
7356 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007357
Willy Tarreaub6866442008-07-14 23:54:42 +02007358 if (!txn)
7359 return 0;
7360
Willy Tarreau655dce92009-11-08 13:10:58 +01007361 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007362 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007363
Willy Tarreauc11416f2007-06-17 16:58:38 +02007364 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7365 /* ensure the indexes are not affected */
7366 return 0;
7367
Willy Tarreau962c3f42010-01-10 00:15:35 +01007368 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007369 ptr = http_get_path(txn);
7370 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007371 return 0;
7372
7373 /* OK, we got the '/' ! */
7374 test->ptr = ptr;
7375
7376 while (ptr < end && *ptr != '?')
7377 ptr++;
7378
7379 test->len = ptr - test->ptr;
7380
7381 /* we do not need to set READ_ONLY because the data is in a buffer */
7382 test->flags = ACL_TEST_F_VOL_1ST;
7383 return 1;
7384}
7385
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007386static int
7387acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
7388 struct acl_expr *expr, struct acl_test *test)
7389{
7390 struct buffer *req = s->req;
7391 struct http_txn *txn = &s->txn;
7392 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007393
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007394 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7395 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7396 */
7397
7398 if (!s || !req)
7399 return 0;
7400
Willy Tarreau655dce92009-11-08 13:10:58 +01007401 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007402 /* Already decoded as OK */
7403 test->flags |= ACL_TEST_F_SET_RES_PASS;
7404 return 1;
7405 }
7406
7407 /* Try to decode HTTP request */
7408 if (likely(req->lr < req->r))
7409 http_msg_analyzer(req, msg, &txn->hdr_idx);
7410
Willy Tarreau655dce92009-11-08 13:10:58 +01007411 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007412 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
7413 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7414 return 1;
7415 }
7416 /* wait for final state */
7417 test->flags |= ACL_TEST_F_MAY_CHANGE;
7418 return 0;
7419 }
7420
7421 /* OK we got a valid HTTP request. We have some minor preparation to
7422 * perform so that further checks can rely on HTTP tests.
7423 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007424 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007425 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7426 s->flags |= SN_REDIRECTABLE;
7427
7428 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
7429 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7430 return 1;
7431 }
7432
7433 test->flags |= ACL_TEST_F_SET_RES_PASS;
7434 return 1;
7435}
7436
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007437static int
7438acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
7439 struct acl_expr *expr, struct acl_test *test)
7440{
7441
7442 if (!s)
7443 return 0;
7444
7445 if (!get_http_auth(s))
7446 return 0;
7447
7448 test->ctx.a[0] = expr->arg.ul;
7449 test->ctx.a[1] = s->txn.auth.user;
7450 test->ctx.a[2] = s->txn.auth.pass;
7451
7452 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
7453
7454 return 1;
7455}
Willy Tarreau8797c062007-05-07 00:55:35 +02007456
7457/************************************************************************/
7458/* All supported keywords must be declared here. */
7459/************************************************************************/
7460
7461/* Note: must not be declared <const> as its list will be overwritten */
7462static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007463 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
7464
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007465 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02007466 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
7467 { "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 +02007468 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02007469
Willy Tarreauc4262962010-05-10 23:42:40 +02007470 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007471 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7472 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7473 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7474 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7475 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7476 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02007477 { "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 +02007478 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02007479
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007480 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02007481 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007482 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7483 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7484 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7485 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7486 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7487 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
7488 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
7489 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02007490 { "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 +02007491
Willy Tarreauc4262962010-05-10 23:42:40 +02007492 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007493 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
7494 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
7495 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
7496 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
7497 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
7498 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
7499 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
7500 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02007501 { "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 +02007502
Willy Tarreauc4262962010-05-10 23:42:40 +02007503 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02007504 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
7505 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
7506 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
7507 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
7508 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
7509 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02007510
Willy Tarreauf3d25982007-05-08 22:45:09 +02007511#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02007512 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
7513 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
7514 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
7515 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
7516 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
7517 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
7518 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
7519
Willy Tarreau8797c062007-05-07 00:55:35 +02007520 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
7521 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
7522 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
7523 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
7524 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
7525 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
7526 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
7527 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007528#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02007529
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007530 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth },
7531 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth },
Willy Tarreau8797c062007-05-07 00:55:35 +02007532 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02007533}};
7534
Willy Tarreau4a568972010-05-12 08:08:50 +02007535/************************************************************************/
7536/* The code below is dedicated to pattern fetching and matching */
7537/************************************************************************/
7538
7539/* extract the IP address from the last occurrence of specified header. Note
7540 * that we should normally first extract the string then convert it to IP,
7541 * but right now we have all the functions to do this seemlessly, and we will
7542 * be able to change that later without touching the configuration.
7543 */
7544static int
7545pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7546 const char *arg, int arg_len, union pattern_data *data)
7547{
7548 struct http_txn *txn = l7;
7549
7550 data->ip.s_addr = htonl(get_ip_from_hdr2(&txn->req, arg, arg_len, &txn->hdr_idx, -1));
7551 return data->ip.s_addr != 0;
7552}
7553
7554/************************************************************************/
7555/* All supported keywords must be declared here. */
7556/************************************************************************/
7557/* Note: must not be declared <const> as its list will be overwritten */
7558static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
7559 { "hdr", pattern_fetch_hdr_ip, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
7560 { NULL, NULL, 0, 0 },
7561}};
7562
Willy Tarreau8797c062007-05-07 00:55:35 +02007563
7564__attribute__((constructor))
7565static void __http_protocol_init(void)
7566{
7567 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02007568 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02007569}
7570
7571
Willy Tarreau58f10d72006-12-04 02:26:12 +01007572/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007573 * Local variables:
7574 * c-indent-level: 8
7575 * c-basic-offset: 8
7576 * End:
7577 */