blob: 30177eeeefafaf8c5fff0877183369119f557896 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010027#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/compat.h>
29#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010030#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreau8797c062007-05-07 00:55:35 +020042#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010043#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/backend.h>
45#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010046#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020049#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010051#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020052#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020053#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010055#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010059#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020060#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/task.h>
62
Willy Tarreau522d6c02009-12-06 18:49:18 +010063const char HTTP_100[] =
64 "HTTP/1.1 100 Continue\r\n\r\n";
65
66const struct chunk http_100_chunk = {
67 .str = (char *)&HTTP_100,
68 .len = sizeof(HTTP_100)-1
69};
70
Willy Tarreau1c47f852006-07-09 08:22:27 +020071/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010072const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020073 "HTTP/1.0 200 OK\r\n"
74 "Cache-Control: no-cache\r\n"
75 "Connection: close\r\n"
76 "Content-Type: text/html\r\n"
77 "\r\n"
78 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
79
Willy Tarreau0f772532006-12-23 20:51:41 +010080const struct chunk http_200_chunk = {
81 .str = (char *)&HTTP_200,
82 .len = sizeof(HTTP_200)-1
83};
84
Willy Tarreaua9679ac2010-01-03 17:32:57 +010085/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020086const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020090 "Location: "; /* not terminated since it will be concatenated with the URL */
91
Willy Tarreau0f772532006-12-23 20:51:41 +010092const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010095 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010096 "Location: "; /* not terminated since it will be concatenated with the URL */
97
98/* same as 302 except that the browser MUST retry with the GET method */
99const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100100 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100101 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100102 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
106const char *HTTP_401_fmt =
107 "HTTP/1.0 401 Unauthorized\r\n"
108 "Cache-Control: no-cache\r\n"
109 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200110 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
112 "\r\n"
113 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
114
Willy Tarreau844a7e72010-01-31 21:46:18 +0100115const char *HTTP_407_fmt =
116 "HTTP/1.0 407 Unauthorized\r\n"
117 "Cache-Control: no-cache\r\n"
118 "Connection: close\r\n"
119 "Content-Type: text/html\r\n"
120 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
121 "\r\n"
122 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
123
Willy Tarreau0f772532006-12-23 20:51:41 +0100124
125const int http_err_codes[HTTP_ERR_SIZE] = {
126 [HTTP_ERR_400] = 400,
127 [HTTP_ERR_403] = 403,
128 [HTTP_ERR_408] = 408,
129 [HTTP_ERR_500] = 500,
130 [HTTP_ERR_502] = 502,
131 [HTTP_ERR_503] = 503,
132 [HTTP_ERR_504] = 504,
133};
134
Willy Tarreau80587432006-12-24 17:47:20 +0100135static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100137 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 "Cache-Control: no-cache\r\n"
139 "Connection: close\r\n"
140 "Content-Type: text/html\r\n"
141 "\r\n"
142 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
143
144 [HTTP_ERR_403] =
145 "HTTP/1.0 403 Forbidden\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
151
152 [HTTP_ERR_408] =
153 "HTTP/1.0 408 Request Time-out\r\n"
154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
159
160 [HTTP_ERR_500] =
161 "HTTP/1.0 500 Server Error\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
167
168 [HTTP_ERR_502] =
169 "HTTP/1.0 502 Bad Gateway\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
175
176 [HTTP_ERR_503] =
177 "HTTP/1.0 503 Service Unavailable\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
183
184 [HTTP_ERR_504] =
185 "HTTP/1.0 504 Gateway Time-out\r\n"
186 "Cache-Control: no-cache\r\n"
187 "Connection: close\r\n"
188 "Content-Type: text/html\r\n"
189 "\r\n"
190 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
191
192};
193
Willy Tarreau80587432006-12-24 17:47:20 +0100194/* We must put the messages here since GCC cannot initialize consts depending
195 * on strlen().
196 */
197struct chunk http_err_chunks[HTTP_ERR_SIZE];
198
Willy Tarreau42250582007-04-01 01:30:43 +0200199#define FD_SETS_ARE_BITFIELDS
200#ifdef FD_SETS_ARE_BITFIELDS
201/*
202 * This map is used with all the FD_* macros to check whether a particular bit
203 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
204 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
205 * byte should be encoded. Be careful to always pass bytes from 0 to 255
206 * exclusively to the macros.
207 */
208fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
209fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
210
211#else
212#error "Check if your OS uses bitfields for fd_sets"
213#endif
214
Willy Tarreau80587432006-12-24 17:47:20 +0100215void init_proto_http()
216{
Willy Tarreau42250582007-04-01 01:30:43 +0200217 int i;
218 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100219 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200220
Willy Tarreau80587432006-12-24 17:47:20 +0100221 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
222 if (!http_err_msgs[msg]) {
223 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
224 abort();
225 }
226
227 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
228 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
229 }
Willy Tarreau42250582007-04-01 01:30:43 +0200230
231 /* initialize the log header encoding map : '{|}"#' should be encoded with
232 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
233 * URL encoding only requires '"', '#' to be encoded as well as non-
234 * printable characters above.
235 */
236 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
237 memset(url_encode_map, 0, sizeof(url_encode_map));
238 for (i = 0; i < 32; i++) {
239 FD_SET(i, hdr_encode_map);
240 FD_SET(i, url_encode_map);
241 }
242 for (i = 127; i < 256; i++) {
243 FD_SET(i, hdr_encode_map);
244 FD_SET(i, url_encode_map);
245 }
246
247 tmp = "\"#{|}";
248 while (*tmp) {
249 FD_SET(*tmp, hdr_encode_map);
250 tmp++;
251 }
252
253 tmp = "\"#";
254 while (*tmp) {
255 FD_SET(*tmp, url_encode_map);
256 tmp++;
257 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200258
259 /* memory allocations */
260 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200261 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100262}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263
Willy Tarreau53b6c742006-12-17 13:37:46 +0100264/*
265 * We have 26 list of methods (1 per first letter), each of which can have
266 * up to 3 entries (2 valid, 1 null).
267 */
268struct http_method_desc {
269 http_meth_t meth;
270 int len;
271 const char text[8];
272};
273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100274const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100275 ['C' - 'A'] = {
276 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
277 },
278 ['D' - 'A'] = {
279 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
280 },
281 ['G' - 'A'] = {
282 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
283 },
284 ['H' - 'A'] = {
285 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
286 },
287 ['P' - 'A'] = {
288 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
289 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
290 },
291 ['T' - 'A'] = {
292 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
293 },
294 /* rest is empty like this :
295 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
296 */
297};
298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100299/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200300 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100301 * RFC2616 for those chars.
302 */
303
304const char http_is_spht[256] = {
305 [' '] = 1, ['\t'] = 1,
306};
307
308const char http_is_crlf[256] = {
309 ['\r'] = 1, ['\n'] = 1,
310};
311
312const char http_is_lws[256] = {
313 [' '] = 1, ['\t'] = 1,
314 ['\r'] = 1, ['\n'] = 1,
315};
316
317const char http_is_sep[256] = {
318 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
319 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
320 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
321 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
322 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
323};
324
325const char http_is_ctl[256] = {
326 [0 ... 31] = 1,
327 [127] = 1,
328};
329
330/*
331 * A token is any ASCII char that is neither a separator nor a CTL char.
332 * Do not overwrite values in assignment since gcc-2.95 will not handle
333 * them correctly. Instead, define every non-CTL char's status.
334 */
335const char http_is_token[256] = {
336 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
337 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
338 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
339 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
340 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
341 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
342 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
343 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
344 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
345 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
346 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
347 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
348 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
349 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
350 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
351 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
352 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
353 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
354 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
355 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
356 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
357 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
358 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
359 ['|'] = 1, ['}'] = 0, ['~'] = 1,
360};
361
362
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100363/*
364 * An http ver_token is any ASCII which can be found in an HTTP version,
365 * which includes 'H', 'T', 'P', '/', '.' and any digit.
366 */
367const char http_is_ver_token[256] = {
368 ['.'] = 1, ['/'] = 1,
369 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
370 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
371 ['H'] = 1, ['P'] = 1, ['T'] = 1,
372};
373
374
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100375/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100376 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
377 */
378#if defined(DEBUG_FSM)
379static void http_silent_debug(int line, struct session *s)
380{
381 int size = 0;
382 size += snprintf(trash + size, sizeof(trash) - size,
383 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n",
384 line,
385 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
386 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags);
387 write(-1, trash, size);
388 size = 0;
389 size += snprintf(trash + size, sizeof(trash) - size,
390 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n",
391 line,
392 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
393 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward);
394
395 write(-1, trash, size);
396}
397#else
398#define http_silent_debug(l,s) do { } while (0)
399#endif
400
401/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100402 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
403 * CRLF. Text length is measured first, so it cannot be NULL.
404 * The header is also automatically added to the index <hdr_idx>, and the end
405 * of headers is automatically adjusted. The number of bytes added is returned
406 * on success, otherwise <0 is returned indicating an error.
407 */
408int http_header_add_tail(struct buffer *b, struct http_msg *msg,
409 struct hdr_idx *hdr_idx, const char *text)
410{
411 int bytes, len;
412
413 len = strlen(text);
414 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
415 if (!bytes)
416 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100417 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100418 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
419}
420
421/*
422 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
423 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
424 * the buffer is only opened and the space reserved, but nothing is copied.
425 * The header is also automatically added to the index <hdr_idx>, and the end
426 * of headers is automatically adjusted. The number of bytes added is returned
427 * on success, otherwise <0 is returned indicating an error.
428 */
429int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
430 struct hdr_idx *hdr_idx, const char *text, int len)
431{
432 int bytes;
433
434 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
435 if (!bytes)
436 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100437 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100438 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
439}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440
441/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100442 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
443 * If so, returns the position of the first non-space character relative to
444 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
445 * to return a pointer to the place after the first space. Returns 0 if the
446 * header name does not match. Checks are case-insensitive.
447 */
448int http_header_match2(const char *hdr, const char *end,
449 const char *name, int len)
450{
451 const char *val;
452
453 if (hdr + len >= end)
454 return 0;
455 if (hdr[len] != ':')
456 return 0;
457 if (strncasecmp(hdr, name, len) != 0)
458 return 0;
459 val = hdr + len + 1;
460 while (val < end && HTTP_IS_SPHT(*val))
461 val++;
462 if ((val >= end) && (len + 2 <= end - hdr))
463 return len + 2; /* we may replace starting from second space */
464 return val - hdr;
465}
466
Willy Tarreau68085d82010-01-18 14:54:04 +0100467/* Find the end of the header value contained between <s> and <e>. See RFC2616,
468 * par 2.2 for more information. Note that it requires a valid header to return
469 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200470 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100471char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200472{
473 int quoted, qdpair;
474
475 quoted = qdpair = 0;
476 for (; s < e; s++) {
477 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200478 else if (quoted) {
479 if (*s == '\\') qdpair = 1;
480 else if (*s == '"') quoted = 0;
481 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200482 else if (*s == '"') quoted = 1;
483 else if (*s == ',') return s;
484 }
485 return s;
486}
487
488/* Find the first or next occurrence of header <name> in message buffer <sol>
489 * using headers index <idx>, and return it in the <ctx> structure. This
490 * structure holds everything necessary to use the header and find next
491 * occurrence. If its <idx> member is 0, the header is searched from the
492 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100493 * 1 when it finds a value, and 0 when there is no more. It is designed to work
494 * with headers defined as comma-separated lists. As a special case, if ctx->val
495 * is NULL when searching for a new values of a header, the current header is
496 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200497 */
498int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100499 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200500 struct hdr_ctx *ctx)
501{
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 char *eol, *sov;
503 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200504
Willy Tarreau68085d82010-01-18 14:54:04 +0100505 cur_idx = ctx->idx;
506 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 /* We have previously returned a value, let's search
508 * another one on the same line.
509 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100511 ctx->del = ctx->val + ctx->vlen;
512 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200513 eol = sol + idx->v[cur_idx].len;
514
515 if (sov >= eol)
516 /* no more values in this header */
517 goto next_hdr;
518
Willy Tarreau68085d82010-01-18 14:54:04 +0100519 /* values remaining for this header, skip the comma but save it
520 * for later use (eg: for header deletion).
521 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200522 sov++;
523 while (sov < eol && http_is_lws[(unsigned char)*sov])
524 sov++;
525
526 goto return_hdr;
527 }
528
529 /* first request for this header */
530 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100531 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200532 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200533 while (cur_idx) {
534 eol = sol + idx->v[cur_idx].len;
535
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200536 if (len == 0) {
537 /* No argument was passed, we want any header.
538 * To achieve this, we simply build a fake request. */
539 while (sol + len < eol && sol[len] != ':')
540 len++;
541 name = sol;
542 }
543
Willy Tarreau33a7e692007-06-10 19:45:56 +0200544 if ((len < eol - sol) &&
545 (sol[len] == ':') &&
546 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100547 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200548 sov = sol + len + 1;
549 while (sov < eol && http_is_lws[(unsigned char)*sov])
550 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100551
Willy Tarreau33a7e692007-06-10 19:45:56 +0200552 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100553 ctx->prev = old_idx;
554 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200555 ctx->idx = cur_idx;
556 ctx->val = sov - sol;
557
558 eol = find_hdr_value_end(sov, eol);
559 ctx->vlen = eol - sov;
560 return 1;
561 }
562 next_hdr:
563 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100564 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200565 cur_idx = idx->v[cur_idx].next;
566 }
567 return 0;
568}
569
570int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100571 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200572 struct hdr_ctx *ctx)
573{
574 return http_find_header2(name, strlen(name), sol, idx, ctx);
575}
576
Willy Tarreau68085d82010-01-18 14:54:04 +0100577/* Remove one value of a header. This only works on a <ctx> returned by one of
578 * the http_find_header functions. The value is removed, as well as surrounding
579 * commas if any. If the removed value was alone, the whole header is removed.
580 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
581 * message <msg>. The new index is returned. If it is zero, it means there is
582 * no more header, so any processing may stop. The ctx is always left in a form
583 * that can be handled by http_find_header2() to find next occurrence.
584 */
585int http_remove_header2(struct http_msg *msg, struct buffer *buf,
586 struct hdr_idx *idx, struct hdr_ctx *ctx)
587{
588 int cur_idx = ctx->idx;
589 char *sol = ctx->line;
590 struct hdr_idx_elem *hdr;
591 int delta, skip_comma;
592
593 if (!cur_idx)
594 return 0;
595
596 hdr = &idx->v[cur_idx];
597 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) {
598 /* This was the only value of the header, we must now remove it entirely. */
599 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
600 http_msg_move_end(msg, delta);
601 idx->used--;
602 hdr->len = 0; /* unused entry */
603 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100604 if (idx->tail == ctx->idx)
605 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100606 ctx->idx = ctx->prev; /* walk back to the end of previous header */
607 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
608 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
609 ctx->vlen = 0;
610 return ctx->idx;
611 }
612
613 /* This was not the only value of this header. We have to remove between
614 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
615 * of the list, we remove the last separator.
616 */
617
618 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
619 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
620 sol + ctx->val + ctx->vlen + skip_comma,
621 NULL, 0);
622 hdr->len += delta;
623 http_msg_move_end(msg, delta);
624 ctx->val = ctx->del;
625 ctx->vlen = 0;
626 return ctx->idx;
627}
628
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100629/* This function handles a server error at the stream interface level. The
630 * stream interface is assumed to be already in a closed state. An optional
631 * message is copied into the input buffer, and an HTTP status code stored.
632 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100633 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100635static void http_server_error(struct session *t, struct stream_interface *si,
636 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100638 buffer_auto_read(si->ob);
639 buffer_abort(si->ob);
640 buffer_auto_close(si->ob);
641 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200642 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100643 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100644 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100645 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100646 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647 }
648 if (!(t->flags & SN_ERR_MASK))
649 t->flags |= err;
650 if (!(t->flags & SN_FINST_MASK))
651 t->flags |= finst;
652}
653
Willy Tarreau80587432006-12-24 17:47:20 +0100654/* This function returns the appropriate error location for the given session
655 * and message.
656 */
657
658struct chunk *error_message(struct session *s, int msgnum)
659{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200660 if (s->be->errmsg[msgnum].str)
661 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100662 else if (s->fe->errmsg[msgnum].str)
663 return &s->fe->errmsg[msgnum];
664 else
665 return &http_err_chunks[msgnum];
666}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667
Willy Tarreau53b6c742006-12-17 13:37:46 +0100668/*
669 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
670 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
671 */
672static http_meth_t find_http_meth(const char *str, const int len)
673{
674 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100675 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100676
677 m = ((unsigned)*str - 'A');
678
679 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100680 for (h = http_methods[m]; h->len > 0; h++) {
681 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100682 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100683 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100684 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100685 };
686 return HTTP_METH_OTHER;
687 }
688 return HTTP_METH_NONE;
689
690}
691
Willy Tarreau21d2af32008-02-14 20:25:24 +0100692/* Parse the URI from the given transaction (which is assumed to be in request
693 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
694 * It is returned otherwise.
695 */
696static char *
697http_get_path(struct http_txn *txn)
698{
699 char *ptr, *end;
700
Willy Tarreau962c3f42010-01-10 00:15:35 +0100701 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100702 end = ptr + txn->req.sl.rq.u_l;
703
704 if (ptr >= end)
705 return NULL;
706
707 /* RFC2616, par. 5.1.2 :
708 * Request-URI = "*" | absuri | abspath | authority
709 */
710
711 if (*ptr == '*')
712 return NULL;
713
714 if (isalpha((unsigned char)*ptr)) {
715 /* this is a scheme as described by RFC3986, par. 3.1 */
716 ptr++;
717 while (ptr < end &&
718 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
719 ptr++;
720 /* skip '://' */
721 if (ptr == end || *ptr++ != ':')
722 return NULL;
723 if (ptr == end || *ptr++ != '/')
724 return NULL;
725 if (ptr == end || *ptr++ != '/')
726 return NULL;
727 }
728 /* skip [user[:passwd]@]host[:[port]] */
729
730 while (ptr < end && *ptr != '/')
731 ptr++;
732
733 if (ptr == end)
734 return NULL;
735
736 /* OK, we got the '/' ! */
737 return ptr;
738}
739
Willy Tarreauefb453c2008-10-26 20:49:47 +0100740/* Returns a 302 for a redirectable request. This may only be called just after
741 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
742 * left unchanged and will follow normal proxy processing.
743 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100745{
746 struct http_txn *txn;
747 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100748 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100749 char *path;
750 int len;
751
752 /* 1: create the response header */
753 rdr.len = strlen(HTTP_302);
754 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100755 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100756 memcpy(rdr.str, HTTP_302, rdr.len);
757
Willy Tarreau827aee92011-03-10 16:55:02 +0100758 srv = target_srv(&s->target);
759
Willy Tarreauefb453c2008-10-26 20:49:47 +0100760 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100761 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100762 return;
763
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100764 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100765 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
766 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
767 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100768 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100769
770 /* 3: add the request URI */
771 txn = &s->txn;
772 path = http_get_path(txn);
773 if (!path)
774 return;
775
Willy Tarreau962c3f42010-01-10 00:15:35 +0100776 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200777 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100778 return;
779
780 memcpy(rdr.str + rdr.len, path, len);
781 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100782
783 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
784 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
785 rdr.len += 29;
786 } else {
787 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
788 rdr.len += 23;
789 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100790
791 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100792 si->shutr(si);
793 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100794 si->err_type = SI_ET_NONE;
795 si->err_loc = NULL;
796 si->state = SI_ST_CLO;
797
798 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100799 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100800
801 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100802 if (srv)
803 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100804}
805
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100806/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100807 * that the server side is closed. Note that err_type is actually a
808 * bitmask, where almost only aborts may be cumulated with other
809 * values. We consider that aborted operations are more important
810 * than timeouts or errors due to the fact that nobody else in the
811 * logs might explain incomplete retries. All others should avoid
812 * being cumulated. It should normally not be possible to have multiple
813 * aborts at once, but just in case, the first one in sequence is reported.
814 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100815void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100816{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100817 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100818
819 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100820 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
821 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100822 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100823 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
824 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100825 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100826 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
827 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100828 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100829 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
830 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100831 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100832 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
833 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100834 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100835 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
836 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100837 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100838 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
839 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100840}
841
Willy Tarreau42250582007-04-01 01:30:43 +0200842extern const char sess_term_cond[8];
843extern const char sess_fin_state[8];
844extern const char *monthname[12];
Willy Tarreauf1348312010-10-07 15:54:11 +0200845const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */
846const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
847 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
848 Set-cookie Updated, unknown, unknown */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200849struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200850struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100851
Emeric Brun3a058f32009-06-30 18:26:00 +0200852void http_sess_clflog(struct session *s)
853{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100854 char pn[INET6_ADDRSTRLEN];
Emeric Brun3a058f32009-06-30 18:26:00 +0200855 struct proxy *fe = s->fe;
856 struct proxy *be = s->be;
857 struct proxy *prx_log;
858 struct http_txn *txn = &s->txn;
859 int tolog, level, err;
860 char *uri, *h;
Willy Tarreau71904a42011-02-13 14:30:26 +0100861 const char *svid;
Emeric Brun3a058f32009-06-30 18:26:00 +0200862 struct tm tm;
863 static char tmpline[MAX_SYSLOG_LEN];
864 int hdr;
865 size_t w;
866 int t_request;
867
868 prx_log = fe;
869 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +0200870 (s->req->cons->conn_retries != be->conn_retries) ||
Emeric Brun3a058f32009-06-30 18:26:00 +0200871 txn->status >= 500;
872
Willy Tarreau957c0a52011-03-03 17:42:23 +0100873 if (s->req->prod->addr.c.from.ss_family == AF_INET)
Emeric Brun3a058f32009-06-30 18:26:00 +0200874 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100875 (const void *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr,
Emeric Brun3a058f32009-06-30 18:26:00 +0200876 pn, sizeof(pn));
Willy Tarreau957c0a52011-03-03 17:42:23 +0100877 else if (s->req->prod->addr.c.from.ss_family == AF_INET6)
Emeric Brun3a058f32009-06-30 18:26:00 +0200878 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100879 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr,
Emeric Brun3a058f32009-06-30 18:26:00 +0200880 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +0200881 else
882 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
Emeric Brun3a058f32009-06-30 18:26:00 +0200883
884 get_gmtime(s->logs.accept_date.tv_sec, &tm);
885
886 /* FIXME: let's limit ourselves to frontend logging for now. */
887 tolog = fe->to_log;
888
889 h = tmpline;
890
891 w = snprintf(h, sizeof(tmpline),
892 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
893 pn,
894 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
895 tm.tm_hour, tm.tm_min, tm.tm_sec);
896 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
897 goto trunc;
898 h += w;
899
900 if (h >= tmpline + sizeof(tmpline) - 4)
901 goto trunc;
902
903 *(h++) = ' ';
904 *(h++) = '\"';
905 uri = txn->uri ? txn->uri : "<BADREQ>";
906 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
907 '#', url_encode_map, uri);
908 *(h++) = '\"';
909
910 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
911 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
912 goto trunc;
913 h += w;
914
915 if (h >= tmpline + sizeof(tmpline) - 9)
916 goto trunc;
917 memcpy(h, " \"-\" \"-\"", 8);
918 h += 8;
919
920 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
921 " %d %03d",
Willy Tarreau957c0a52011-03-03 17:42:23 +0100922 s->req->prod->addr.c.from.ss_family == AF_UNIX ? s->listener->luid :
923 ntohs((s->req->prod->addr.c.from.ss_family == AF_INET) ?
924 ((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_port :
925 ((struct sockaddr_in6 *)&s->req->prod->addr.c.from)->sin6_port),
Emeric Brun3a058f32009-06-30 18:26:00 +0200926 (int)s->logs.accept_date.tv_usec/1000);
927 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
928 goto trunc;
929 h += w;
930
931 w = strlen(fe->id);
932 if (h >= tmpline + sizeof(tmpline) - 4 - w)
933 goto trunc;
934 *(h++) = ' ';
935 *(h++) = '\"';
936 memcpy(h, fe->id, w);
937 h += w;
938 *(h++) = '\"';
939
940 w = strlen(be->id);
941 if (h >= tmpline + sizeof(tmpline) - 4 - w)
942 goto trunc;
943 *(h++) = ' ';
944 *(h++) = '\"';
945 memcpy(h, be->id, w);
946 h += w;
947 *(h++) = '\"';
948
Willy Tarreau71904a42011-02-13 14:30:26 +0100949 if (!(tolog & LW_SVID))
950 svid = "-";
951 else switch (s->req->cons->target.type) {
952 case TARG_TYPE_SERVER:
953 svid = s->req->cons->target.ptr.s->id;
954 break;
955 case TARG_TYPE_APPLET:
956 svid = s->req->cons->target.ptr.a->name;
957 break;
958 default:
959 svid = "<NOSRV>";
960 break;
961 }
Emeric Brun3a058f32009-06-30 18:26:00 +0200962
963 w = strlen(svid);
964 if (h >= tmpline + sizeof(tmpline) - 4 - w)
965 goto trunc;
966 *(h++) = ' ';
967 *(h++) = '\"';
968 memcpy(h, svid, w);
969 h += w;
970 *(h++) = '\"';
971
972 t_request = -1;
973 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
974 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
975 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
976 " %d %ld %ld %ld %ld",
977 t_request,
978 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
979 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
980 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
981 s->logs.t_close);
982 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
983 goto trunc;
984 h += w;
985
986 if (h >= tmpline + sizeof(tmpline) - 8)
987 goto trunc;
988 *(h++) = ' ';
989 *(h++) = '\"';
990 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
991 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
992 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
993 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
994 *(h++) = '\"';
995
996 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
997 " %d %d %d %d %d %ld %ld",
Willy Tarreau827aee92011-03-10 16:55:02 +0100998 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Willy Tarreauee28de02010-06-01 09:51:00 +0200999 (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries,
Emeric Brun3a058f32009-06-30 18:26:00 +02001000 s->logs.srv_queue_size, s->logs.prx_queue_size);
1001
1002 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
1003 goto trunc;
1004 h += w;
1005
1006 if (txn->cli_cookie) {
1007 w = strlen(txn->cli_cookie);
1008 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1009 goto trunc;
1010 *(h++) = ' ';
1011 *(h++) = '\"';
1012 memcpy(h, txn->cli_cookie, w);
1013 h += w;
1014 *(h++) = '\"';
1015 } else {
1016 if (h >= tmpline + sizeof(tmpline) - 5)
1017 goto trunc;
1018 memcpy(h, " \"-\"", 4);
1019 h += 4;
1020 }
1021
1022 if (txn->srv_cookie) {
1023 w = strlen(txn->srv_cookie);
1024 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1025 goto trunc;
1026 *(h++) = ' ';
1027 *(h++) = '\"';
1028 memcpy(h, txn->srv_cookie, w);
1029 h += w;
1030 *(h++) = '\"';
1031 } else {
1032 if (h >= tmpline + sizeof(tmpline) - 5)
1033 goto trunc;
1034 memcpy(h, " \"-\"", 4);
1035 h += 4;
1036 }
1037
1038 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1039 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1040 if (h >= sizeof (tmpline) + tmpline - 4)
1041 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001042 if (txn->req.cap[hdr] != NULL) {
1043 *(h++) = ' ';
1044 *(h++) = '\"';
1045 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1046 '#', hdr_encode_map, txn->req.cap[hdr]);
1047 *(h++) = '\"';
1048 } else {
1049 memcpy(h, " \"-\"", 4);
1050 h += 4;
1051 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001052 }
1053 }
1054
1055 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1056 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1057 if (h >= sizeof (tmpline) + tmpline - 4)
1058 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001059 if (txn->rsp.cap[hdr] != NULL) {
1060 *(h++) = ' ';
1061 *(h++) = '\"';
1062 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1063 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1064 *(h++) = '\"';
1065 } else {
1066 memcpy(h, " \"-\"", 4);
1067 h += 4;
1068 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001069 }
1070 }
1071
1072trunc:
1073 *h = '\0';
1074
1075 level = LOG_INFO;
1076 if (err && (fe->options2 & PR_O2_LOGERRORS))
1077 level = LOG_ERR;
1078
1079 send_log(prx_log, level, "%s\n", tmpline);
1080
1081 s->logs.logwait = 0;
1082}
1083
Willy Tarreau42250582007-04-01 01:30:43 +02001084/*
1085 * send a log for the session when we have enough info about it.
1086 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001088void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001089{
Cyril Bontéacd7d632010-11-01 19:26:02 +01001090 char pn[INET6_ADDRSTRLEN];
Willy Tarreau42250582007-04-01 01:30:43 +02001091 struct proxy *fe = s->fe;
1092 struct proxy *be = s->be;
1093 struct proxy *prx_log;
1094 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001095 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001096 char *uri, *h;
Willy Tarreau71904a42011-02-13 14:30:26 +01001097 const char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001098 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001099 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001100 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001101 int hdr;
1102
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001103 /* if we don't want to log normal traffic, return now */
1104 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +02001105 (s->req->cons->conn_retries != be->conn_retries) ||
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001106 txn->status >= 500;
1107 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1108 return;
1109
Willy Tarreau42250582007-04-01 01:30:43 +02001110 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1111 return;
1112 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113
Emeric Brun3a058f32009-06-30 18:26:00 +02001114 if (prx_log->options2 & PR_O2_CLFLOG)
1115 return http_sess_clflog(s);
1116
Willy Tarreau957c0a52011-03-03 17:42:23 +01001117 if (s->req->prod->addr.c.from.ss_family == AF_INET)
Willy Tarreau42250582007-04-01 01:30:43 +02001118 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +01001119 (const void *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr,
Willy Tarreau42250582007-04-01 01:30:43 +02001120 pn, sizeof(pn));
Willy Tarreau957c0a52011-03-03 17:42:23 +01001121 else if (s->req->prod->addr.c.from.ss_family == AF_INET6)
Willy Tarreau42250582007-04-01 01:30:43 +02001122 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01001123 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr,
Willy Tarreau42250582007-04-01 01:30:43 +02001124 pn, sizeof(pn));
1125
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001126 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001127
1128 /* FIXME: let's limit ourselves to frontend logging for now. */
1129 tolog = fe->to_log;
1130
1131 h = tmpline;
1132 if (fe->to_log & LW_REQHDR &&
1133 txn->req.cap &&
1134 (h < tmpline + sizeof(tmpline) - 10)) {
1135 *(h++) = ' ';
1136 *(h++) = '{';
1137 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1138 if (hdr)
1139 *(h++) = '|';
1140 if (txn->req.cap[hdr] != NULL)
1141 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1142 '#', hdr_encode_map, txn->req.cap[hdr]);
1143 }
1144 *(h++) = '}';
1145 }
1146
1147 if (fe->to_log & LW_RSPHDR &&
1148 txn->rsp.cap &&
1149 (h < tmpline + sizeof(tmpline) - 7)) {
1150 *(h++) = ' ';
1151 *(h++) = '{';
1152 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1153 if (hdr)
1154 *(h++) = '|';
1155 if (txn->rsp.cap[hdr] != NULL)
1156 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1157 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1158 }
1159 *(h++) = '}';
1160 }
1161
1162 if (h < tmpline + sizeof(tmpline) - 4) {
1163 *(h++) = ' ';
1164 *(h++) = '"';
1165 uri = txn->uri ? txn->uri : "<BADREQ>";
1166 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1167 '#', url_encode_map, uri);
1168 *(h++) = '"';
1169 }
1170 *h = '\0';
1171
Willy Tarreau71904a42011-02-13 14:30:26 +01001172 if (!(tolog & LW_SVID))
1173 svid = "-";
1174 else switch (s->req->cons->target.type) {
1175 case TARG_TYPE_SERVER:
1176 svid = s->req->cons->target.ptr.s->id;
1177 break;
1178 case TARG_TYPE_APPLET:
1179 svid = s->req->cons->target.ptr.a->name;
1180 break;
1181 default:
1182 svid = "<NOSRV>";
1183 break;
1184 }
Willy Tarreau42250582007-04-01 01:30:43 +02001185
Willy Tarreau70089872008-06-13 21:12:51 +02001186 t_request = -1;
1187 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1188 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1189
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001190 level = LOG_INFO;
1191 if (err && (fe->options2 & PR_O2_LOGERRORS))
1192 level = LOG_ERR;
1193
1194 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001195 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001196 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1197 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau957c0a52011-03-03 17:42:23 +01001198 (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? "unix" : pn,
1199 (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? s->listener->luid :
1200 ntohs((s->req->prod->addr.c.from.ss_family == AF_INET) ?
1201 ((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_port :
1202 ((struct sockaddr_in6 *)&s->req->prod->addr.c.from)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001203 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001204 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001205 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001206 t_request,
1207 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001208 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1209 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1210 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1211 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001212 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001213 txn->cli_cookie ? txn->cli_cookie : "-",
1214 txn->srv_cookie ? txn->srv_cookie : "-",
1215 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1216 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1217 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1218 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
Willy Tarreau827aee92011-03-10 16:55:02 +01001219 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001220 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +02001221 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001222 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1223
1224 s->logs.logwait = 0;
1225}
1226
Willy Tarreau117f59e2007-03-04 18:17:17 +01001227
1228/*
1229 * Capture headers from message starting at <som> according to header list
1230 * <cap_hdr>, and fill the <idx> structure appropriately.
1231 */
1232void capture_headers(char *som, struct hdr_idx *idx,
1233 char **cap, struct cap_hdr *cap_hdr)
1234{
1235 char *eol, *sol, *col, *sov;
1236 int cur_idx;
1237 struct cap_hdr *h;
1238 int len;
1239
1240 sol = som + hdr_idx_first_pos(idx);
1241 cur_idx = hdr_idx_first_idx(idx);
1242
1243 while (cur_idx) {
1244 eol = sol + idx->v[cur_idx].len;
1245
1246 col = sol;
1247 while (col < eol && *col != ':')
1248 col++;
1249
1250 sov = col + 1;
1251 while (sov < eol && http_is_lws[(unsigned char)*sov])
1252 sov++;
1253
1254 for (h = cap_hdr; h; h = h->next) {
1255 if ((h->namelen == col - sol) &&
1256 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1257 if (cap[h->index] == NULL)
1258 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001259 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001260
1261 if (cap[h->index] == NULL) {
1262 Alert("HTTP capture : out of memory.\n");
1263 continue;
1264 }
1265
1266 len = eol - sov;
1267 if (len > h->len)
1268 len = h->len;
1269
1270 memcpy(cap[h->index], sov, len);
1271 cap[h->index][len]=0;
1272 }
1273 }
1274 sol = eol + idx->v[cur_idx].cr + 1;
1275 cur_idx = idx->v[cur_idx].next;
1276 }
1277}
1278
1279
Willy Tarreau42250582007-04-01 01:30:43 +02001280/* either we find an LF at <ptr> or we jump to <bad>.
1281 */
1282#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1283
1284/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1285 * otherwise to <http_msg_ood> with <state> set to <st>.
1286 */
1287#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1288 ptr++; \
1289 if (likely(ptr < end)) \
1290 goto good; \
1291 else { \
1292 state = (st); \
1293 goto http_msg_ood; \
1294 } \
1295 } while (0)
1296
1297
Willy Tarreaubaaee002006-06-26 02:48:02 +02001298/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001299 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1301 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1302 * will give undefined results.
1303 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1304 * and that msg->sol points to the beginning of the response.
1305 * If a complete line is found (which implies that at least one CR or LF is
1306 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1307 * returned indicating an incomplete line (which does not mean that parts have
1308 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1309 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1310 * upon next call.
1311 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001312 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001313 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1314 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001315 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001317const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1318 unsigned int state, const char *ptr, const char *end,
1319 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001320{
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001322 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001323 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001324 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001325 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1326
1327 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001328 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001329 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1330 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001331 state = HTTP_MSG_ERROR;
1332 break;
1333
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001335 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001337 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 goto http_msg_rpcode;
1339 }
1340 if (likely(HTTP_IS_SPHT(*ptr)))
1341 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1342 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001343 state = HTTP_MSG_ERROR;
1344 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001345
Willy Tarreau8973c702007-01-21 23:58:29 +01001346 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001347 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 if (likely(!HTTP_IS_LWS(*ptr)))
1349 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1350
1351 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001352 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1354 }
1355
1356 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001357 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001358 http_msg_rsp_reason:
1359 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001360 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001361 msg->sl.st.r_l = 0;
1362 goto http_msg_rpline_eol;
1363
Willy Tarreau8973c702007-01-21 23:58:29 +01001364 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001365 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001366 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001367 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001368 goto http_msg_rpreason;
1369 }
1370 if (likely(HTTP_IS_SPHT(*ptr)))
1371 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1372 /* so it's a CR/LF, so there is no reason phrase */
1373 goto http_msg_rsp_reason;
1374
Willy Tarreau8973c702007-01-21 23:58:29 +01001375 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001376 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001377 if (likely(!HTTP_IS_CRLF(*ptr)))
1378 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001379 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001380 http_msg_rpline_eol:
1381 /* We have seen the end of line. Note that we do not
1382 * necessarily have the \n yet, but at least we know that we
1383 * have EITHER \r OR \n, otherwise the response would not be
1384 * complete. We can then record the response length and return
1385 * to the caller which will be able to register it.
1386 */
1387 msg->sl.st.l = ptr - msg->sol;
1388 return ptr;
1389
1390#ifdef DEBUG_FULL
1391 default:
1392 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1393 exit(1);
1394#endif
1395 }
1396
1397 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001398 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001399 if (ret_state)
1400 *ret_state = state;
1401 if (ret_ptr)
1402 *ret_ptr = (char *)ptr;
1403 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001404}
1405
Willy Tarreau8973c702007-01-21 23:58:29 +01001406/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 * This function parses a request line between <ptr> and <end>, starting with
1408 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1409 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1410 * will give undefined results.
1411 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1412 * and that msg->sol points to the beginning of the request.
1413 * If a complete line is found (which implies that at least one CR or LF is
1414 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1415 * returned indicating an incomplete line (which does not mean that parts have
1416 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1417 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1418 * upon next call.
1419 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001420 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1422 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001423 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001424 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001425const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1426 unsigned int state, const char *ptr, const char *end,
1427 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001428{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001431 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 if (likely(HTTP_IS_TOKEN(*ptr)))
1433 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001434
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001436 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1438 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 if (likely(HTTP_IS_CRLF(*ptr))) {
1441 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001442 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001444 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001446 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001447 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001448 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 msg->sl.rq.v_l = 0;
1450 goto http_msg_rqline_eol;
1451 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001452 state = HTTP_MSG_ERROR;
1453 break;
1454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001456 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001458 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 goto http_msg_rquri;
1460 }
1461 if (likely(HTTP_IS_SPHT(*ptr)))
1462 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1463 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1464 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001467 http_msg_rquri:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 if (likely(!HTTP_IS_LWS(*ptr)))
1469 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001472 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1474 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1477 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001478
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001479 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001480 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001482 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 goto http_msg_rqver;
1484 }
1485 if (likely(HTTP_IS_SPHT(*ptr)))
1486 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1487 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1488 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001489
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001491 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001492 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001494
1495 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001496 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001497 http_msg_rqline_eol:
1498 /* We have seen the end of line. Note that we do not
1499 * necessarily have the \n yet, but at least we know that we
1500 * have EITHER \r OR \n, otherwise the request would not be
1501 * complete. We can then record the request length and return
1502 * to the caller which will be able to register it.
1503 */
1504 msg->sl.rq.l = ptr - msg->sol;
1505 return ptr;
1506 }
1507
1508 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001509 state = HTTP_MSG_ERROR;
1510 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001511
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512#ifdef DEBUG_FULL
1513 default:
1514 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1515 exit(1);
1516#endif
1517 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001518
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001520 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 if (ret_state)
1522 *ret_state = state;
1523 if (ret_ptr)
1524 *ret_ptr = (char *)ptr;
1525 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001527
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001528/*
1529 * Returns the data from Authorization header. Function may be called more
1530 * than once so data is stored in txn->auth_data. When no header is found
1531 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1532 * searching again for something we are unable to find anyway.
1533 */
1534
1535char get_http_auth_buff[BUFSIZE];
1536
1537int
1538get_http_auth(struct session *s)
1539{
1540
1541 struct http_txn *txn = &s->txn;
1542 struct chunk auth_method;
1543 struct hdr_ctx ctx;
1544 char *h, *p;
1545 int len;
1546
1547#ifdef DEBUG_AUTH
1548 printf("Auth for session %p: %d\n", s, txn->auth.method);
1549#endif
1550
1551 if (txn->auth.method == HTTP_AUTH_WRONG)
1552 return 0;
1553
1554 if (txn->auth.method)
1555 return 1;
1556
1557 txn->auth.method = HTTP_AUTH_WRONG;
1558
1559 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001560
1561 if (txn->flags & TX_USE_PX_CONN) {
1562 h = "Proxy-Authorization";
1563 len = strlen(h);
1564 } else {
1565 h = "Authorization";
1566 len = strlen(h);
1567 }
1568
1569 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001570 return 0;
1571
1572 h = ctx.line + ctx.val;
1573
1574 p = memchr(h, ' ', ctx.vlen);
1575 if (!p || p == h)
1576 return 0;
1577
1578 chunk_initlen(&auth_method, h, 0, p-h);
1579 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1580
1581 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1582
1583 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1584 get_http_auth_buff, BUFSIZE - 1);
1585
1586 if (len < 0)
1587 return 0;
1588
1589
1590 get_http_auth_buff[len] = '\0';
1591
1592 p = strchr(get_http_auth_buff, ':');
1593
1594 if (!p)
1595 return 0;
1596
1597 txn->auth.user = get_http_auth_buff;
1598 *p = '\0';
1599 txn->auth.pass = p+1;
1600
1601 txn->auth.method = HTTP_AUTH_BASIC;
1602 return 1;
1603 }
1604
1605 return 0;
1606}
1607
Willy Tarreau58f10d72006-12-04 02:26:12 +01001608
Willy Tarreau8973c702007-01-21 23:58:29 +01001609/*
1610 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001611 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001612 * when data are missing and recalled at the exact same location with no
1613 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001614 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001615 * fields. Note that msg->som and msg->sol will be initialized after completing
1616 * the first state, so that none of the msg pointers has to be initialized
1617 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001618 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1620{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001621 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001622 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001623
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001624 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 ptr = buf->lr;
1626 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 if (unlikely(ptr >= end))
1629 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001632 /*
1633 * First, states that are specific to the response only.
1634 * We check them first so that request and headers are
1635 * closer to each other (accessed more often).
1636 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001637 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001638 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001639 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001640 /* we have a start of message, but we have to check
1641 * first if we need to remove some CRLF. We can only
1642 * do this when send_max=0.
1643 */
1644 char *beg = buf->w + buf->send_max;
1645 if (beg >= buf->data + buf->size)
1646 beg -= buf->size;
1647 if (unlikely(ptr != beg)) {
1648 if (buf->send_max)
1649 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001650 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001651 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001652 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001653 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001654 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001655 hdr_idx_init(idx);
1656 state = HTTP_MSG_RPVER;
1657 goto http_msg_rpver;
1658 }
1659
1660 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1661 goto http_msg_invalid;
1662
1663 if (unlikely(*ptr == '\n'))
1664 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1665 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1666 /* stop here */
1667
Willy Tarreau8973c702007-01-21 23:58:29 +01001668 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001669 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001670 EXPECT_LF_HERE(ptr, http_msg_invalid);
1671 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1672 /* stop here */
1673
Willy Tarreau8973c702007-01-21 23:58:29 +01001674 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001675 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001676 case HTTP_MSG_RPVER_SP:
1677 case HTTP_MSG_RPCODE:
1678 case HTTP_MSG_RPCODE_SP:
1679 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001680 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001681 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001682 if (unlikely(!ptr))
1683 return;
1684
1685 /* we have a full response and we know that we have either a CR
1686 * or an LF at <ptr>.
1687 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001688 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001689 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1690
1691 msg->sol = ptr;
1692 if (likely(*ptr == '\r'))
1693 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1694 goto http_msg_rpline_end;
1695
Willy Tarreau8973c702007-01-21 23:58:29 +01001696 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001697 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001698 /* msg->sol must point to the first of CR or LF. */
1699 EXPECT_LF_HERE(ptr, http_msg_invalid);
1700 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1701 /* stop here */
1702
1703 /*
1704 * Second, states that are specific to the request only
1705 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001707 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001709 /* we have a start of message, but we have to check
1710 * first if we need to remove some CRLF. We can only
1711 * do this when send_max=0.
1712 */
1713 char *beg = buf->w + buf->send_max;
1714 if (beg >= buf->data + buf->size)
1715 beg -= buf->size;
1716 if (likely(ptr != beg)) {
1717 if (buf->send_max)
1718 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001719 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001720 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001722 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001723 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001724 /* we will need this when keep-alive will be supported
1725 hdr_idx_init(idx);
1726 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001727 state = HTTP_MSG_RQMETH;
1728 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1732 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001733
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 if (unlikely(*ptr == '\n'))
1735 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1736 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001737 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001738
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001740 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741 EXPECT_LF_HERE(ptr, http_msg_invalid);
1742 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001743 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001744
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001746 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 case HTTP_MSG_RQMETH_SP:
1748 case HTTP_MSG_RQURI:
1749 case HTTP_MSG_RQURI_SP:
1750 case HTTP_MSG_RQVER:
1751 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001752 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 if (unlikely(!ptr))
1754 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001755
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 /* we have a full request and we know that we have either a CR
1757 * or an LF at <ptr>.
1758 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001759 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001761
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001762 msg->sol = ptr;
1763 if (likely(*ptr == '\r'))
1764 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001765 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001766
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001767 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001768 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001769 /* check for HTTP/0.9 request : no version information available.
1770 * msg->sol must point to the first of CR or LF.
1771 */
1772 if (unlikely(msg->sl.rq.v_l == 0))
1773 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001774
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001775 EXPECT_LF_HERE(ptr, http_msg_invalid);
1776 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001777 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001778
Willy Tarreau8973c702007-01-21 23:58:29 +01001779 /*
1780 * Common states below
1781 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001782 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001783 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 msg->sol = ptr;
1785 if (likely(!HTTP_IS_CRLF(*ptr))) {
1786 goto http_msg_hdr_name;
1787 }
1788
1789 if (likely(*ptr == '\r'))
1790 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1791 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001792
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001793 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001794 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001795 /* assumes msg->sol points to the first char */
1796 if (likely(HTTP_IS_TOKEN(*ptr)))
1797 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001798
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001799 if (likely(*ptr == ':')) {
1800 msg->col = ptr - buf->data;
1801 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1802 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001803
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001804 if (likely(msg->err_pos < -1) || *ptr == '\n')
1805 goto http_msg_invalid;
1806
1807 if (msg->err_pos == -1) /* capture error pointer */
1808 msg->err_pos = ptr - buf->data; /* >= 0 now */
1809
1810 /* and we still accept this non-token character */
1811 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001812
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001813 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001814 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001815 /* assumes msg->sol points to the first char and msg->col to the colon */
1816 if (likely(HTTP_IS_SPHT(*ptr)))
1817 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001818
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001819 /* header value can be basically anything except CR/LF */
1820 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001821
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 if (likely(!HTTP_IS_CRLF(*ptr))) {
1823 goto http_msg_hdr_val;
1824 }
1825
1826 if (likely(*ptr == '\r'))
1827 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1828 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001829
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001831 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001832 EXPECT_LF_HERE(ptr, http_msg_invalid);
1833 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001834
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001836 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001837 if (likely(HTTP_IS_SPHT(*ptr))) {
1838 /* replace HT,CR,LF with spaces */
1839 for (; buf->data+msg->sov < ptr; msg->sov++)
1840 buf->data[msg->sov] = ' ';
1841 goto http_msg_hdr_l1_sp;
1842 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001843 /* we had a header consisting only in spaces ! */
1844 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001845 goto http_msg_complete_header;
1846
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001847 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001848 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 /* assumes msg->sol points to the first char, msg->col to the
1850 * colon, and msg->sov points to the first character of the
1851 * value.
1852 */
1853 if (likely(!HTTP_IS_CRLF(*ptr)))
1854 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001855
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001856 msg->eol = ptr;
1857 /* Note: we could also copy eol into ->eoh so that we have the
1858 * real header end in case it ends with lots of LWS, but is this
1859 * really needed ?
1860 */
1861 if (likely(*ptr == '\r'))
1862 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1863 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001864
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001865 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001866 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 EXPECT_LF_HERE(ptr, http_msg_invalid);
1868 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001869
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001870 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001871 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001872 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1873 /* LWS: replace HT,CR,LF with spaces */
1874 for (; msg->eol < ptr; msg->eol++)
1875 *msg->eol = ' ';
1876 goto http_msg_hdr_val;
1877 }
1878 http_msg_complete_header:
1879 /*
1880 * It was a new header, so the last one is finished.
1881 * Assumes msg->sol points to the first char, msg->col to the
1882 * colon, msg->sov points to the first character of the value
1883 * and msg->eol to the first CR or LF so we know how the line
1884 * ends. We insert last header into the index.
1885 */
1886 /*
1887 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1888 write(2, msg->sol, msg->eol-msg->sol);
1889 fprintf(stderr,"\n");
1890 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001891
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1893 idx, idx->tail) < 0))
1894 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001895
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 msg->sol = ptr;
1897 if (likely(!HTTP_IS_CRLF(*ptr))) {
1898 goto http_msg_hdr_name;
1899 }
1900
1901 if (likely(*ptr == '\r'))
1902 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1903 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001904
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001905 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001906 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001907 /* Assumes msg->sol points to the first of either CR or LF */
1908 EXPECT_LF_HERE(ptr, http_msg_invalid);
1909 ptr++;
1910 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001911 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001912 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001913 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001914 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001915 return;
1916#ifdef DEBUG_FULL
1917 default:
1918 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1919 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001920#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001921 }
1922 http_msg_ood:
1923 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001924 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001925 buf->lr = ptr;
1926 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001927
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001928 http_msg_invalid:
1929 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001930 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001931 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001932 return;
1933}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001934
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001935/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1936 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1937 * nothing is done and 1 is returned.
1938 */
1939static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1940{
1941 int delta;
1942 char *cur_end;
1943
1944 if (msg->sl.rq.v_l != 0)
1945 return 1;
1946
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001947 cur_end = msg->sol + msg->sl.rq.l;
1948 delta = 0;
1949
1950 if (msg->sl.rq.u_l == 0) {
1951 /* if no URI was set, add "/" */
1952 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1953 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001954 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001955 }
1956 /* add HTTP version */
1957 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001958 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001959 cur_end += delta;
1960 cur_end = (char *)http_parse_reqline(msg, req->data,
1961 HTTP_MSG_RQMETH,
1962 msg->sol, cur_end + 1,
1963 NULL, NULL);
1964 if (unlikely(!cur_end))
1965 return 0;
1966
1967 /* we have a full HTTP/1.0 request now and we know that
1968 * we have either a CR or an LF at <ptr>.
1969 */
1970 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1971 return 1;
1972}
1973
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001974/* Parse the Connection: header of an HTTP request, looking for both "close"
1975 * and "keep-alive" values. If a buffer is provided and we already know that
1976 * some headers may safely be removed, we remove them now. The <to_del> flags
1977 * are used for that :
1978 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1979 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1980 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1981 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1982 * harmless combinations may be removed. Do not call that after changes have
1983 * been processed. If unused, the buffer can be NULL, and no data will be
1984 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001985 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001986void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001987{
Willy Tarreau5b154472009-12-21 20:11:07 +01001988 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001989 const char *hdr_val = "Connection";
1990 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001991
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001992 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001993 return;
1994
Willy Tarreau88d349d2010-01-25 12:15:43 +01001995 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1996 hdr_val = "Proxy-Connection";
1997 hdr_len = 16;
1998 }
1999
Willy Tarreau5b154472009-12-21 20:11:07 +01002000 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002001 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002002 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002003 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2004 txn->flags |= TX_HDR_CONN_KAL;
2005 if ((to_del & 2) && buf)
2006 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
2007 else
2008 txn->flags |= TX_CON_KAL_SET;
2009 }
2010 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2011 txn->flags |= TX_HDR_CONN_CLO;
2012 if ((to_del & 1) && buf)
2013 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
2014 else
2015 txn->flags |= TX_CON_CLO_SET;
2016 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002017 }
2018
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002019 txn->flags |= TX_HDR_CONN_PRS;
2020 return;
2021}
Willy Tarreau5b154472009-12-21 20:11:07 +01002022
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002023/* Apply desired changes on the Connection: header. Values may be removed and/or
2024 * added depending on the <wanted> flags, which are exclusively composed of
2025 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2026 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2027 */
2028void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
2029{
2030 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002031 const char *hdr_val = "Connection";
2032 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002033
2034 ctx.idx = 0;
2035
Willy Tarreau88d349d2010-01-25 12:15:43 +01002036
2037 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2038 hdr_val = "Proxy-Connection";
2039 hdr_len = 16;
2040 }
2041
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002042 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002043 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002044 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2045 if (wanted & TX_CON_KAL_SET)
2046 txn->flags |= TX_CON_KAL_SET;
2047 else
2048 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002049 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002050 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2051 if (wanted & TX_CON_CLO_SET)
2052 txn->flags |= TX_CON_CLO_SET;
2053 else
2054 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002055 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002056 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002057
2058 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2059 return;
2060
2061 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2062 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002063 hdr_val = "Connection: close";
2064 hdr_len = 17;
2065 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2066 hdr_val = "Proxy-Connection: close";
2067 hdr_len = 23;
2068 }
2069 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002070 }
2071
2072 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2073 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002074 hdr_val = "Connection: keep-alive";
2075 hdr_len = 22;
2076 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2077 hdr_val = "Proxy-Connection: keep-alive";
2078 hdr_len = 28;
2079 }
2080 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002081 }
2082 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002083}
2084
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2086 * first byte of body, and increments msg->sov by the number of bytes parsed,
2087 * so that we know we can forward between ->som and ->sov. Note that due to
2088 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2089 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002090 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002091 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002092 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002093int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002094{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 char *ptr = buf->lr;
2096 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002097 unsigned int chunk = 0;
2098
2099 /* The chunk size is in the following form, though we are only
2100 * interested in the size and CRLF :
2101 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2102 */
2103 while (1) {
2104 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002105 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002106 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002107 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002108 if (c < 0) /* not a hex digit anymore */
2109 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002110 if (++ptr >= end)
2111 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002112 if (chunk & 0xF000000) /* overflow will occur */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002113 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002114 chunk = (chunk << 4) + c;
2115 }
2116
Willy Tarreaud98cf932009-12-27 22:54:55 +01002117 /* empty size not allowed */
2118 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002119 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002120
2121 while (http_is_spht[(unsigned char)*ptr]) {
2122 if (++ptr >= end)
2123 ptr = buf->data;
2124 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002125 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002126 }
2127
Willy Tarreaud98cf932009-12-27 22:54:55 +01002128 /* Up to there, we know that at least one byte is present at *ptr. Check
2129 * for the end of chunk size.
2130 */
2131 while (1) {
2132 if (likely(HTTP_IS_CRLF(*ptr))) {
2133 /* we now have a CR or an LF at ptr */
2134 if (likely(*ptr == '\r')) {
2135 if (++ptr >= end)
2136 ptr = buf->data;
2137 if (ptr == buf->r)
2138 return 0;
2139 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002140
Willy Tarreaud98cf932009-12-27 22:54:55 +01002141 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002142 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002143 if (++ptr >= end)
2144 ptr = buf->data;
2145 /* done */
2146 break;
2147 }
2148 else if (*ptr == ';') {
2149 /* chunk extension, ends at next CRLF */
2150 if (++ptr >= end)
2151 ptr = buf->data;
2152 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002153 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002154
2155 while (!HTTP_IS_CRLF(*ptr)) {
2156 if (++ptr >= end)
2157 ptr = buf->data;
2158 if (ptr == buf->r)
2159 return 0;
2160 }
2161 /* we have a CRLF now, loop above */
2162 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002163 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002164 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002165 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002166 }
2167
Willy Tarreaud98cf932009-12-27 22:54:55 +01002168 /* OK we found our CRLF and now <ptr> points to the next byte,
2169 * which may or may not be present. We save that into ->lr and
2170 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002171 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002172 msg->sov += ptr - buf->lr;
2173 buf->lr = ptr;
Willy Tarreau124d9912011-03-01 20:30:48 +01002174 msg->chunk_len = chunk;
2175 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002176 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002177 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002178 error:
2179 msg->err_pos = ptr - buf->data;
2180 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002181}
2182
Willy Tarreaud98cf932009-12-27 22:54:55 +01002183/* This function skips trailers in the buffer <buf> associated with HTTP
2184 * message <msg>. The first visited position is buf->lr. If the end of
2185 * the trailers is found, it is automatically scheduled to be forwarded,
2186 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2187 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002188 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2189 * zero. If a parse error is encountered, the function returns < 0 and does not
2190 * change anything except maybe buf->lr and msg->sov. Note that the message
2191 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2192 * which implies that all non-trailers data have already been scheduled for
2193 * forwarding, and that the difference between msg->som and msg->sov exactly
2194 * matches the length of trailers already parsed and not forwarded. It is also
2195 * important to note that this function is designed to be able to parse wrapped
2196 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 */
2198int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2199{
2200 /* we have buf->lr which points to next line. Look for CRLF. */
2201 while (1) {
2202 char *p1 = NULL, *p2 = NULL;
2203 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002204 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002205
2206 /* scan current line and stop at LF or CRLF */
2207 while (1) {
2208 if (ptr == buf->r)
2209 return 0;
2210
2211 if (*ptr == '\n') {
2212 if (!p1)
2213 p1 = ptr;
2214 p2 = ptr;
2215 break;
2216 }
2217
2218 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002219 if (p1) {
2220 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002221 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002222 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002223 p1 = ptr;
2224 }
2225
2226 ptr++;
2227 if (ptr >= buf->data + buf->size)
2228 ptr = buf->data;
2229 }
2230
2231 /* after LF; point to beginning of next line */
2232 p2++;
2233 if (p2 >= buf->data + buf->size)
2234 p2 = buf->data;
2235
Willy Tarreau638cd022010-01-03 07:42:04 +01002236 bytes = p2 - buf->lr;
2237 if (bytes < 0)
2238 bytes += buf->size;
2239
2240 /* schedule this line for forwarding */
2241 msg->sov += bytes;
2242 if (msg->sov >= buf->size)
2243 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002244
Willy Tarreau638cd022010-01-03 07:42:04 +01002245 if (p1 == buf->lr) {
2246 /* LF/CRLF at beginning of line => end of trailers at p2.
2247 * Everything was scheduled for forwarding, there's nothing
2248 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002249 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002250 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002251 msg->msg_state = HTTP_MSG_DONE;
2252 return 1;
2253 }
2254 /* OK, next line then */
2255 buf->lr = p2;
2256 }
2257}
2258
2259/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2260 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2261 * ->som, buf->lr in order to include this part into the next forwarding phase.
2262 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2263 * not enough data are available, the function does not change anything and
2264 * returns zero. If a parse error is encountered, the function returns < 0 and
2265 * does not change anything. Note: this function is designed to parse wrapped
2266 * CRLF at the end of the buffer.
2267 */
2268int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2269{
2270 char *ptr;
2271 int bytes;
2272
2273 /* NB: we'll check data availabilty at the end. It's not a
2274 * problem because whatever we match first will be checked
2275 * against the correct length.
2276 */
2277 bytes = 1;
2278 ptr = buf->lr;
2279 if (*ptr == '\r') {
2280 bytes++;
2281 ptr++;
2282 if (ptr >= buf->data + buf->size)
2283 ptr = buf->data;
2284 }
2285
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002286 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002287 return 0;
2288
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002289 if (*ptr != '\n') {
2290 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002291 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002292 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002293
2294 ptr++;
2295 if (ptr >= buf->data + buf->size)
2296 ptr = buf->data;
2297 buf->lr = ptr;
2298 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2299 msg->sov = ptr - buf->data;
2300 msg->som = msg->sov - bytes;
2301 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2302 return 1;
2303}
Willy Tarreau5b154472009-12-21 20:11:07 +01002304
Willy Tarreau83e3af02009-12-28 17:39:57 +01002305void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2306{
2307 char *end = buf->data + buf->size;
2308 int off = buf->data + buf->size - buf->w;
2309
2310 /* two possible cases :
2311 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002312 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002313 */
2314 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002315 int block1 = buf->l;
2316 int block2 = 0;
2317 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002318 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002319 block1 = buf->data + buf->size - buf->w;
2320 block2 = buf->r - buf->data;
2321 }
2322 if (block2)
2323 memcpy(swap_buffer, buf->data, block2);
2324 memmove(buf->data, buf->w, block1);
2325 if (block2)
2326 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002327 }
2328
2329 /* adjust all known pointers */
2330 buf->w = buf->data;
2331 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2332 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2333 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2334 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2335
2336 /* adjust relative pointers */
2337 msg->som = 0;
2338 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2339 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2340 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2341
Willy Tarreau83e3af02009-12-28 17:39:57 +01002342 if (msg->err_pos >= 0) {
2343 msg->err_pos += off;
2344 if (msg->err_pos >= buf->size)
2345 msg->err_pos -= buf->size;
2346 }
2347
2348 buf->flags &= ~BF_FULL;
2349 if (buf->l >= buffer_max_len(buf))
2350 buf->flags |= BF_FULL;
2351}
2352
Willy Tarreaud787e662009-07-07 10:14:51 +02002353/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2354 * processing can continue on next analysers, or zero if it either needs more
2355 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2356 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2357 * when it has nothing left to do, and may remove any analyser when it wants to
2358 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002359 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002360int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002361{
Willy Tarreau59234e92008-11-30 23:51:27 +01002362 /*
2363 * We will parse the partial (or complete) lines.
2364 * We will check the request syntax, and also join multi-line
2365 * headers. An index of all the lines will be elaborated while
2366 * parsing.
2367 *
2368 * For the parsing, we use a 28 states FSM.
2369 *
2370 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002371 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002372 * req->data + msg->eoh = end of processed headers / start of current one
2373 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002374 * req->lr = first non-visited byte
2375 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002376 *
2377 * At end of parsing, we may perform a capture of the error (if any), and
2378 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2379 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2380 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002382
Willy Tarreau59234e92008-11-30 23:51:27 +01002383 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002384 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 struct http_txn *txn = &s->txn;
2386 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002387 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002388
Willy Tarreau6bf17362009-02-24 10:48:35 +01002389 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2390 now_ms, __FUNCTION__,
2391 s,
2392 req,
2393 req->rex, req->wex,
2394 req->flags,
2395 req->l,
2396 req->analysers);
2397
Willy Tarreau52a0c602009-08-16 22:45:38 +02002398 /* we're speaking HTTP here, so let's speak HTTP to the client */
2399 s->srv_error = http_return_srv_error;
2400
Willy Tarreau83e3af02009-12-28 17:39:57 +01002401 /* There's a protected area at the end of the buffer for rewriting
2402 * purposes. We don't want to start to parse the request if the
2403 * protected area is affected, because we may have to move processed
2404 * data later, which is much more complicated.
2405 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002406 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002407 if ((txn->flags & TX_NOT_FIRST) &&
2408 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002409 req->r < req->lr ||
2410 req->r > req->data + req->size - global.tune.maxrewrite)) {
2411 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002412 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2413 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002414 /* some data has still not left the buffer, wake us once that's done */
2415 buffer_dont_connect(req);
2416 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2417 return 0;
2418 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002419 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002420 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002421 }
2422
Willy Tarreau065e8332010-01-08 00:30:20 +01002423 /* Note that we have the same problem with the response ; we
2424 * may want to send a redirect, error or anything which requires
2425 * some spare space. So we'll ensure that we have at least
2426 * maxrewrite bytes available in the response buffer before
2427 * processing that one. This will only affect pipelined
2428 * keep-alive requests.
2429 */
2430 if ((txn->flags & TX_NOT_FIRST) &&
2431 unlikely((s->rep->flags & BF_FULL) ||
2432 s->rep->r < s->rep->lr ||
2433 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2434 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002435 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2436 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002437 /* don't let a connection request be initiated */
2438 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002439 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002440 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002441 return 0;
2442 }
2443 }
2444
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002445 if (likely(req->lr < req->r))
2446 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002447 }
2448
Willy Tarreau59234e92008-11-30 23:51:27 +01002449 /* 1: we might have to print this header in debug mode */
2450 if (unlikely((global.mode & MODE_DEBUG) &&
2451 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002452 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002453 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002454 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002455
Willy Tarreau663308b2010-06-07 14:06:08 +02002456 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002457 eol = sol + msg->sl.rq.l;
2458 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002459
Willy Tarreau59234e92008-11-30 23:51:27 +01002460 sol += hdr_idx_first_pos(&txn->hdr_idx);
2461 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002462
Willy Tarreau59234e92008-11-30 23:51:27 +01002463 while (cur_idx) {
2464 eol = sol + txn->hdr_idx.v[cur_idx].len;
2465 debug_hdr("clihdr", s, sol, eol);
2466 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2467 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002468 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002469 }
2470
Willy Tarreau58f10d72006-12-04 02:26:12 +01002471
Willy Tarreau59234e92008-11-30 23:51:27 +01002472 /*
2473 * Now we quickly check if we have found a full valid request.
2474 * If not so, we check the FD and buffer states before leaving.
2475 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002476 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002477 * requests are checked first. When waiting for a second request
2478 * on a keep-alive session, if we encounter and error, close, t/o,
2479 * we note the error in the session flags but don't set any state.
2480 * Since the error will be noted there, it will not be counted by
2481 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002482 * Last, we may increase some tracked counters' http request errors on
2483 * the cases that are deliberately the client's fault. For instance,
2484 * a timeout or connection reset is not counted as an error. However
2485 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002486 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002487
Willy Tarreau655dce92009-11-08 13:10:58 +01002488 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002489 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002490 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002491 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002492 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002493 session_inc_http_req_ctr(s);
2494 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002495 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002496 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002497 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002498
Willy Tarreau59234e92008-11-30 23:51:27 +01002499 /* 1: Since we are in header mode, if there's no space
2500 * left for headers, we won't be able to free more
2501 * later, so the session will never terminate. We
2502 * must terminate it now.
2503 */
2504 if (unlikely(req->flags & BF_FULL)) {
2505 /* FIXME: check if URI is set and return Status
2506 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002507 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002508 session_inc_http_req_ctr(s);
2509 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002510 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002511 goto return_bad_req;
2512 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002513
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 /* 2: have we encountered a read error ? */
2515 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002516 if (!(s->flags & SN_ERR_MASK))
2517 s->flags |= SN_ERR_CLICL;
2518
Willy Tarreaufcffa692010-01-10 14:21:19 +01002519 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002520 goto failed_keep_alive;
2521
Willy Tarreau59234e92008-11-30 23:51:27 +01002522 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002523 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002524 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002525 session_inc_http_err_ctr(s);
2526 }
2527
Willy Tarreau59234e92008-11-30 23:51:27 +01002528 msg->msg_state = HTTP_MSG_ERROR;
2529 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002530
Willy Tarreauda7ff642010-06-23 11:44:09 +02002531 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002532 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002533 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002534 if (s->listener->counters)
2535 s->listener->counters->failed_req++;
2536
Willy Tarreau59234e92008-11-30 23:51:27 +01002537 if (!(s->flags & SN_FINST_MASK))
2538 s->flags |= SN_FINST_R;
2539 return 0;
2540 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002541
Willy Tarreau59234e92008-11-30 23:51:27 +01002542 /* 3: has the read timeout expired ? */
2543 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002544 if (!(s->flags & SN_ERR_MASK))
2545 s->flags |= SN_ERR_CLITO;
2546
Willy Tarreaufcffa692010-01-10 14:21:19 +01002547 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002548 goto failed_keep_alive;
2549
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002551 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002552 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002553 session_inc_http_err_ctr(s);
2554 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002555 txn->status = 408;
2556 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2557 msg->msg_state = HTTP_MSG_ERROR;
2558 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002559
Willy Tarreauda7ff642010-06-23 11:44:09 +02002560 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002561 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002562 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002563 if (s->listener->counters)
2564 s->listener->counters->failed_req++;
2565
Willy Tarreau59234e92008-11-30 23:51:27 +01002566 if (!(s->flags & SN_FINST_MASK))
2567 s->flags |= SN_FINST_R;
2568 return 0;
2569 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002570
Willy Tarreau59234e92008-11-30 23:51:27 +01002571 /* 4: have we encountered a close ? */
2572 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002573 if (!(s->flags & SN_ERR_MASK))
2574 s->flags |= SN_ERR_CLICL;
2575
Willy Tarreaufcffa692010-01-10 14:21:19 +01002576 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002577 goto failed_keep_alive;
2578
Willy Tarreau4076a152009-04-02 15:18:36 +02002579 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002580 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002581 txn->status = 400;
2582 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2583 msg->msg_state = HTTP_MSG_ERROR;
2584 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002585
Willy Tarreauda7ff642010-06-23 11:44:09 +02002586 session_inc_http_err_ctr(s);
2587 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002588 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002589 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002590 if (s->listener->counters)
2591 s->listener->counters->failed_req++;
2592
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 if (!(s->flags & SN_FINST_MASK))
2594 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002595 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002596 }
2597
Willy Tarreau520d95e2009-09-19 21:04:57 +02002598 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002599 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002600 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002601
Willy Tarreaufcffa692010-01-10 14:21:19 +01002602 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2603 /* If the client starts to talk, let's fall back to
2604 * request timeout processing.
2605 */
2606 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002607 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002608 }
2609
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002611 if (!tick_isset(req->analyse_exp)) {
2612 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2613 (txn->flags & TX_WAIT_NEXT_RQ) &&
2614 tick_isset(s->be->timeout.httpka))
2615 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2616 else
2617 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2618 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002619
Willy Tarreau59234e92008-11-30 23:51:27 +01002620 /* we're not ready yet */
2621 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002622
2623 failed_keep_alive:
2624 /* Here we process low-level errors for keep-alive requests. In
2625 * short, if the request is not the first one and it experiences
2626 * a timeout, read error or shutdown, we just silently close so
2627 * that the client can try again.
2628 */
2629 txn->status = 0;
2630 msg->msg_state = HTTP_MSG_RQBEFORE;
2631 req->analysers = 0;
2632 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002633 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002634 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002635 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002636 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002637
Willy Tarreaud787e662009-07-07 10:14:51 +02002638 /* OK now we have a complete HTTP request with indexed headers. Let's
2639 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002640 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2641 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2642 * points to the CRLF of the request line. req->lr points to the first
2643 * byte after the last LF. msg->col and msg->sov point to the first
2644 * byte of data. msg->eol cannot be trusted because it may have been
2645 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002646 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002647
Willy Tarreauda7ff642010-06-23 11:44:09 +02002648 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002649 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2650
Willy Tarreaub16a5742010-01-10 14:46:16 +01002651 if (txn->flags & TX_WAIT_NEXT_RQ) {
2652 /* kill the pending keep-alive timeout */
2653 txn->flags &= ~TX_WAIT_NEXT_RQ;
2654 req->analyse_exp = TICK_ETERNITY;
2655 }
2656
2657
Willy Tarreaud787e662009-07-07 10:14:51 +02002658 /* Maybe we found in invalid header name while we were configured not
2659 * to block on that, so we have to capture it now.
2660 */
2661 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002662 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002663
Willy Tarreau59234e92008-11-30 23:51:27 +01002664 /*
2665 * 1: identify the method
2666 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002667 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002668
2669 /* we can make use of server redirect on GET and HEAD */
2670 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2671 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002672
Willy Tarreau59234e92008-11-30 23:51:27 +01002673 /*
2674 * 2: check if the URI matches the monitor_uri.
2675 * We have to do this for every request which gets in, because
2676 * the monitor-uri is defined by the frontend.
2677 */
2678 if (unlikely((s->fe->monitor_uri_len != 0) &&
2679 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002680 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002681 s->fe->monitor_uri,
2682 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002683 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002685 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002686 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002687
Willy Tarreau59234e92008-11-30 23:51:27 +01002688 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002689
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002691 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2692 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002693
Willy Tarreau59234e92008-11-30 23:51:27 +01002694 ret = acl_pass(ret);
2695 if (cond->pol == ACL_COND_UNLESS)
2696 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002697
Willy Tarreau59234e92008-11-30 23:51:27 +01002698 if (ret) {
2699 /* we fail this request, let's return 503 service unavail */
2700 txn->status = 503;
2701 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2702 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002703 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002704 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002705
Willy Tarreau59234e92008-11-30 23:51:27 +01002706 /* nothing to fail, let's reply normaly */
2707 txn->status = 200;
2708 stream_int_retnclose(req->prod, &http_200_chunk);
2709 goto return_prx_cond;
2710 }
2711
2712 /*
2713 * 3: Maybe we have to copy the original REQURI for the logs ?
2714 * Note: we cannot log anymore if the request has been
2715 * classified as invalid.
2716 */
2717 if (unlikely(s->logs.logwait & LW_REQ)) {
2718 /* we have a complete HTTP request that we must log */
2719 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2720 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002721
Willy Tarreau59234e92008-11-30 23:51:27 +01002722 if (urilen >= REQURI_LEN)
2723 urilen = REQURI_LEN - 1;
2724 memcpy(txn->uri, &req->data[msg->som], urilen);
2725 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002726
Willy Tarreau59234e92008-11-30 23:51:27 +01002727 if (!(s->logs.logwait &= ~LW_REQ))
2728 s->do_log(s);
2729 } else {
2730 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002731 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 }
Willy Tarreau06619262006-12-17 08:37:22 +01002733
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002735 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2736 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002737
Willy Tarreau5b154472009-12-21 20:11:07 +01002738 /* ... and check if the request is HTTP/1.1 or above */
2739 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002740 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2741 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2742 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002743 txn->flags |= TX_REQ_VER_11;
2744
2745 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002746 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002747
Willy Tarreau88d349d2010-01-25 12:15:43 +01002748 /* if the frontend has "option http-use-proxy-header", we'll check if
2749 * we have what looks like a proxied connection instead of a connection,
2750 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2751 * Note that this is *not* RFC-compliant, however browsers and proxies
2752 * happen to do that despite being non-standard :-(
2753 * We consider that a request not beginning with either '/' or '*' is
2754 * a proxied connection, which covers both "scheme://location" and
2755 * CONNECT ip:port.
2756 */
2757 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2758 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2759 txn->flags |= TX_USE_PX_CONN;
2760
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002761 /* transfer length unknown*/
2762 txn->flags &= ~TX_REQ_XFER_LEN;
2763
Willy Tarreau59234e92008-11-30 23:51:27 +01002764 /* 5: we may need to capture headers */
2765 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002766 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002767 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002768
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002769 /* 6: determine the transfer-length.
2770 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2771 * the presence of a message-body in a REQUEST and its transfer length
2772 * must be determined that way (in order of precedence) :
2773 * 1. The presence of a message-body in a request is signaled by the
2774 * inclusion of a Content-Length or Transfer-Encoding header field
2775 * in the request's header fields. When a request message contains
2776 * both a message-body of non-zero length and a method that does
2777 * not define any semantics for that request message-body, then an
2778 * origin server SHOULD either ignore the message-body or respond
2779 * with an appropriate error message (e.g., 413). A proxy or
2780 * gateway, when presented the same request, SHOULD either forward
2781 * the request inbound with the message- body or ignore the
2782 * message-body when determining a response.
2783 *
2784 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2785 * and the "chunked" transfer-coding (Section 6.2) is used, the
2786 * transfer-length is defined by the use of this transfer-coding.
2787 * If a Transfer-Encoding header field is present and the "chunked"
2788 * transfer-coding is not present, the transfer-length is defined
2789 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002790 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002791 * 3. If a Content-Length header field is present, its decimal value in
2792 * OCTETs represents both the entity-length and the transfer-length.
2793 * If a message is received with both a Transfer-Encoding header
2794 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002795 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002796 * 4. By the server closing the connection. (Closing the connection
2797 * cannot be used to indicate the end of a request body, since that
2798 * would leave no possibility for the server to send back a response.)
2799 *
2800 * Whenever a transfer-coding is applied to a message-body, the set of
2801 * transfer-codings MUST include "chunked", unless the message indicates
2802 * it is terminated by closing the connection. When the "chunked"
2803 * transfer-coding is used, it MUST be the last transfer-coding applied
2804 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002805 */
2806
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002807 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002808 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002809 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002810 while ((txn->flags & TX_REQ_VER_11) &&
2811 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002812 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2813 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2814 else if (txn->flags & TX_REQ_TE_CHNK) {
2815 /* bad transfer-encoding (chunked followed by something else) */
2816 use_close_only = 1;
2817 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2818 break;
2819 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002820 }
2821
Willy Tarreau32b47f42009-10-18 20:55:02 +02002822 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002823 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002824 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2825 signed long long cl;
2826
2827 if (!ctx.vlen)
2828 goto return_bad_req;
2829
2830 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2831 goto return_bad_req; /* parse failure */
2832
2833 if (cl < 0)
2834 goto return_bad_req;
2835
Willy Tarreau124d9912011-03-01 20:30:48 +01002836 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl))
Willy Tarreau32b47f42009-10-18 20:55:02 +02002837 goto return_bad_req; /* already specified, was different */
2838
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002839 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002840 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002841 }
2842
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002843 /* bodyless requests have a known length */
2844 if (!use_close_only)
2845 txn->flags |= TX_REQ_XFER_LEN;
2846
Willy Tarreaud787e662009-07-07 10:14:51 +02002847 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002848 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002849 req->analyse_exp = TICK_ETERNITY;
2850 return 1;
2851
2852 return_bad_req:
2853 /* We centralize bad requests processing here */
2854 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2855 /* we detected a parsing error. We want to archive this request
2856 * in the dedicated proxy area for later troubleshooting.
2857 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002858 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002859 }
2860
2861 txn->req.msg_state = HTTP_MSG_ERROR;
2862 txn->status = 400;
2863 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002864
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002865 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002866 if (s->listener->counters)
2867 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002868
2869 return_prx_cond:
2870 if (!(s->flags & SN_ERR_MASK))
2871 s->flags |= SN_ERR_PRXCOND;
2872 if (!(s->flags & SN_FINST_MASK))
2873 s->flags |= SN_FINST_R;
2874
2875 req->analysers = 0;
2876 req->analyse_exp = TICK_ETERNITY;
2877 return 0;
2878}
2879
Cyril Bonté70be45d2010-10-12 00:14:35 +02002880/* We reached the stats page through a POST request.
2881 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002882 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002883 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002884int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002885{
Cyril Bonté70be45d2010-10-12 00:14:35 +02002886 struct proxy *px;
2887 struct server *sv;
2888
2889 char *backend = NULL;
2890 int action = 0;
2891
2892 char *first_param, *cur_param, *next_param, *end_params;
2893
2894 first_param = req->data + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002895 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002896
2897 cur_param = next_param = end_params;
2898
Cyril Bonté23b39d92011-02-10 22:54:44 +01002899 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002900 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002901 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002902 return 1;
2903 }
2904 else if (end_params > req->data + req->l) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002905 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002906 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002907 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002908 }
2909
2910 *end_params = '\0';
2911
Willy Tarreau295a8372011-03-10 11:25:07 +01002912 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002913
2914 /*
2915 * Parse the parameters in reverse order to only store the last value.
2916 * From the html form, the backend and the action are at the end.
2917 */
2918 while (cur_param > first_param) {
2919 char *key, *value;
2920
2921 cur_param--;
2922 if ((*cur_param == '&') || (cur_param == first_param)) {
2923 /* Parse the key */
2924 key = cur_param;
2925 if (cur_param != first_param) {
2926 /* delimit the string for the next loop */
2927 *key++ = '\0';
2928 }
2929
2930 /* Parse the value */
2931 value = key;
2932 while (*value != '\0' && *value != '=') {
2933 value++;
2934 }
2935 if (*value == '=') {
2936 /* Ok, a value is found, we can mark the end of the key */
2937 *value++ = '\0';
2938 }
2939
2940 /* Now we can check the key to see what to do */
2941 if (!backend && strcmp(key, "b") == 0) {
2942 backend = value;
2943 }
2944 else if (!action && strcmp(key, "action") == 0) {
2945 if (strcmp(value, "disable") == 0) {
2946 action = 1;
2947 }
2948 else if (strcmp(value, "enable") == 0) {
2949 action = 2;
2950 } else {
2951 /* unknown action, no need to continue */
2952 break;
2953 }
2954 }
2955 else if (strcmp(key, "s") == 0) {
2956 if (backend && action && get_backend_server(backend, value, &px, &sv)) {
2957 switch (action) {
2958 case 1:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002959 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002960 /* Not already in maintenance, we can change the server state */
2961 sv->state |= SRV_MAINTAIN;
2962 set_server_down(sv);
Willy Tarreau295a8372011-03-10 11:25:07 +01002963 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002964 }
2965 break;
2966 case 2:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002967 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002968 /* Already in maintenance, we can change the server state */
2969 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002970 sv->health = sv->rise; /* up, but will fall down at first failure */
Willy Tarreau295a8372011-03-10 11:25:07 +01002971 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002972 }
2973 break;
2974 }
2975 }
2976 }
2977 next_param = cur_param;
2978 }
2979 }
Cyril Bonté23b39d92011-02-10 22:54:44 +01002980 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002981}
2982
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002983/* returns a pointer to the first rule which forbids access (deny or http_auth),
2984 * or NULL if everything's OK.
2985 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002986static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002987http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2988{
Willy Tarreauff011f22011-01-06 17:51:27 +01002989 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002990
Willy Tarreauff011f22011-01-06 17:51:27 +01002991 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002992 int ret = 1;
2993
Willy Tarreauff011f22011-01-06 17:51:27 +01002994 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002995 continue;
2996
2997 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002998 if (rule->cond) {
2999 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003000 ret = acl_pass(ret);
3001
Willy Tarreauff011f22011-01-06 17:51:27 +01003002 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003003 ret = !ret;
3004 }
3005
3006 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003007 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003008 return NULL; /* no problem */
3009 else
Willy Tarreauff011f22011-01-06 17:51:27 +01003010 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003011 }
3012 }
3013 return NULL;
3014}
3015
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003016/* This stream analyser runs all HTTP request processing which is common to
3017 * frontends and backends, which means blocking ACLs, filters, connection-close,
3018 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003019 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003020 * either needs more data or wants to immediately abort the request (eg: deny,
3021 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003022 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003023int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003024{
Willy Tarreaud787e662009-07-07 10:14:51 +02003025 struct http_txn *txn = &s->txn;
3026 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003027 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003028 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003029 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003030 struct cond_wordlist *wl;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003031 int del_ka, del_cl, do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003032
Willy Tarreau655dce92009-11-08 13:10:58 +01003033 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003034 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003035 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003036 return 0;
3037 }
3038
Willy Tarreau3a816292009-07-07 10:55:49 +02003039 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003040 req->analyse_exp = TICK_ETERNITY;
3041
3042 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3043 now_ms, __FUNCTION__,
3044 s,
3045 req,
3046 req->rex, req->wex,
3047 req->flags,
3048 req->l,
3049 req->analysers);
3050
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003051 /* first check whether we have some ACLs set to block this request */
3052 list_for_each_entry(cond, &px->block_cond, list) {
3053 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003054
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003055 ret = acl_pass(ret);
3056 if (cond->pol == ACL_COND_UNLESS)
3057 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003058
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003059 if (ret) {
3060 txn->status = 403;
3061 /* let's log the request time */
3062 s->logs.tv_request = now;
3063 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003064 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003065 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003066 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003067 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003068
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003069 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01003070 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003071
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003072 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003073 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003074 do_stats = stats_check_uri(s->rep->prod, txn, px);
3075 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01003076 http_req_last_rule = http_check_access_rule(px, &px->uri_auth->http_req_rules, s, txn);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003077 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003078 else
3079 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003080
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003081 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01003082 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003083 txn->status = 403;
3084 s->logs.tv_request = now;
3085 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003086 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003087 s->fe->fe_counters.denied_req++;
3088 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3089 s->be->be_counters.denied_req++;
3090 if (s->listener->counters)
3091 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003092 goto return_prx_cond;
3093 }
3094
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003095 /* try headers filters */
3096 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003097 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003098 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003099
Willy Tarreau59234e92008-11-30 23:51:27 +01003100 /* has the request been denied ? */
3101 if (txn->flags & TX_CLDENY) {
3102 /* no need to go further */
3103 txn->status = 403;
3104 /* let's log the request time */
3105 s->logs.tv_request = now;
3106 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003107 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003108 goto return_prx_cond;
3109 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003110
3111 /* When a connection is tarpitted, we use the tarpit timeout,
3112 * which may be the same as the connect timeout if unspecified.
3113 * If unset, then set it to zero because we really want it to
3114 * eventually expire. We build the tarpit as an analyser.
3115 */
3116 if (txn->flags & TX_CLTARPIT) {
3117 buffer_erase(s->req);
3118 /* wipe the request out so that we can drop the connection early
3119 * if the client closes first.
3120 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003121 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003122 req->analysers = 0; /* remove switching rules etc... */
3123 req->analysers |= AN_REQ_HTTP_TARPIT;
3124 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3125 if (!req->analyse_exp)
3126 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003127 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003128 return 1;
3129 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003130 }
Willy Tarreau06619262006-12-17 08:37:22 +01003131
Willy Tarreau5b154472009-12-21 20:11:07 +01003132 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3133 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003134 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3135 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003136 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3137 * avoid to redo the same work if FE and BE have the same settings (common).
3138 * The method consists in checking if options changed between the two calls
3139 * (implying that either one is non-null, or one of them is non-null and we
3140 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003141 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003142
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003143 del_cl = del_ka = 0;
3144
Willy Tarreaudc008c52010-02-01 16:20:08 +01003145 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3146 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3147 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3148 (s->be->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)))) {
Willy Tarreau5b154472009-12-21 20:11:07 +01003149 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003150
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003151 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3152 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003153 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003154 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3155 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003156 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003157 tmp = TX_CON_WANT_CLO;
3158
Willy Tarreau5b154472009-12-21 20:11:07 +01003159 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3160 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003161
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003162 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3163 /* parse the Connection header and possibly clean it */
3164 int to_del = 0;
3165 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003166 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3167 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003168 to_del |= 2; /* remove "keep-alive" */
3169 if (!(txn->flags & TX_REQ_VER_11))
3170 to_del |= 1; /* remove "close" */
3171 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003172 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003173
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003174 /* check if client or config asks for explicit close in KAL/SCL */
3175 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3176 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3177 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
3178 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003179 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003180 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
3181 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003182 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3183 }
Willy Tarreau78599912009-10-17 20:12:21 +02003184
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003185 /* we can be blocked here because the request needs to be authenticated,
3186 * either to pass or to access stats.
3187 */
Willy Tarreauff011f22011-01-06 17:51:27 +01003188 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003189 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01003190 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003191
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003192 if (!realm)
3193 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3194
Willy Tarreau844a7e72010-01-31 21:46:18 +01003195 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003196 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3197 txn->status = 401;
3198 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003199 /* on 401 we still count one error, because normal browsing
3200 * won't significantly increase the counter but brute force
3201 * attempts will.
3202 */
3203 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003204 goto return_prx_cond;
3205 }
3206
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003207 /* add request headers from the rule sets in the same order */
3208 list_for_each_entry(wl, &px->req_add, list) {
3209 if (wl->cond) {
3210 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
3211 ret = acl_pass(ret);
3212 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3213 ret = !ret;
3214 if (!ret)
3215 continue;
3216 }
3217
3218 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
3219 goto return_bad_req;
3220 }
3221
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003222 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003223 struct stats_admin_rule *stats_admin_rule;
3224
3225 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003226 * FIXME!!! that one is rather dangerous, we want to
3227 * make it follow standard rules (eg: clear req->analysers).
3228 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003229
Cyril Bonté474be412010-10-12 00:14:36 +02003230 /* now check whether we have some admin rules for this request */
3231 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3232 int ret = 1;
3233
3234 if (stats_admin_rule->cond) {
3235 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
3236 ret = acl_pass(ret);
3237 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3238 ret = !ret;
3239 }
3240
3241 if (ret) {
3242 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01003243 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02003244 break;
3245 }
3246 }
3247
Cyril Bonté70be45d2010-10-12 00:14:35 +02003248 /* Was the status page requested with a POST ? */
3249 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003250 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003251 if (msg->msg_state < HTTP_MSG_100_SENT) {
3252 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3253 * send an HTTP/1.1 100 Continue intermediate response.
3254 */
3255 if (txn->flags & TX_REQ_VER_11) {
3256 struct hdr_ctx ctx;
3257 ctx.idx = 0;
3258 /* Expect is allowed in 1.1, look for it */
3259 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3260 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3261 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3262 }
3263 }
3264 msg->msg_state = HTTP_MSG_100_SENT;
3265 s->logs.tv_request = now; /* update the request timer to reflect full request */
3266 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003267 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003268 /* we need more data */
3269 req->analysers |= an_bit;
3270 buffer_dont_connect(req);
3271 return 0;
3272 }
Cyril Bonté474be412010-10-12 00:14:36 +02003273 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003274 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003275 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003276 }
3277
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003278 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003279 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003280 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreaubc4af052011-02-13 13:25:14 +01003281 s->rep->prod->applet.private = s;
3282 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003283 req->analysers = 0;
3284
3285 return 0;
3286
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003287 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003288
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003289 /* check whether we have some ACLs set to redirect this request */
3290 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003291 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003292
Willy Tarreauf285f542010-01-03 20:03:03 +01003293 if (rule->cond) {
3294 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3295 ret = acl_pass(ret);
3296 if (rule->cond->pol == ACL_COND_UNLESS)
3297 ret = !ret;
3298 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003299
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003300 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003301 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003302 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003303
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003304 /* build redirect message */
3305 switch(rule->code) {
3306 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003307 msg_fmt = HTTP_303;
3308 break;
3309 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003310 msg_fmt = HTTP_301;
3311 break;
3312 case 302:
3313 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003314 msg_fmt = HTTP_302;
3315 break;
3316 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003317
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003318 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003319 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003320
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003321 switch(rule->type) {
3322 case REDIRECT_TYPE_PREFIX: {
3323 const char *path;
3324 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003325
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003326 path = http_get_path(txn);
3327 /* build message using path */
3328 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003329 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003330 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3331 int qs = 0;
3332 while (qs < pathlen) {
3333 if (path[qs] == '?') {
3334 pathlen = qs;
3335 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003336 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003337 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003338 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003339 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003340 } else {
3341 path = "/";
3342 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003343 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003344
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003345 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003346 goto return_bad_req;
3347
3348 /* add prefix. Note that if prefix == "/", we don't want to
3349 * add anything, otherwise it makes it hard for the user to
3350 * configure a self-redirection.
3351 */
3352 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003353 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3354 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003355 }
3356
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003357 /* add path */
3358 memcpy(rdr.str + rdr.len, path, pathlen);
3359 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003360
3361 /* append a slash at the end of the location is needed and missing */
3362 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3363 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3364 if (rdr.len > rdr.size - 5)
3365 goto return_bad_req;
3366 rdr.str[rdr.len] = '/';
3367 rdr.len++;
3368 }
3369
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003370 break;
3371 }
3372 case REDIRECT_TYPE_LOCATION:
3373 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003374 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003375 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003376
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003377 /* add location */
3378 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3379 rdr.len += rule->rdr_len;
3380 break;
3381 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003382
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003383 if (rule->cookie_len) {
3384 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3385 rdr.len += 14;
3386 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3387 rdr.len += rule->cookie_len;
3388 memcpy(rdr.str + rdr.len, "\r\n", 2);
3389 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003390 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003391
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003392 /* add end of headers and the keep-alive/close status.
3393 * We may choose to set keep-alive if the Location begins
3394 * with a slash, because the client will come back to the
3395 * same server.
3396 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003397 txn->status = rule->code;
3398 /* let's log the request time */
3399 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003400
3401 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3402 (txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau124d9912011-03-01 20:30:48 +01003403 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003404 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3405 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3406 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003407 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003408 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3409 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3410 rdr.len += 30;
3411 } else {
3412 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3413 rdr.len += 24;
3414 }
Willy Tarreau75661452010-01-10 10:35:01 +01003415 }
3416 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3417 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003418 buffer_write(req->prod->ob, rdr.str, rdr.len);
3419 /* "eat" the request */
3420 buffer_ignore(req, msg->sov - msg->som);
3421 msg->som = msg->sov;
3422 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003423 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3424 txn->req.msg_state = HTTP_MSG_CLOSED;
3425 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003426 break;
3427 } else {
3428 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003429 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3430 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3431 rdr.len += 29;
3432 } else {
3433 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3434 rdr.len += 23;
3435 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003436 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003437 goto return_prx_cond;
3438 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003439 }
3440 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003441
Willy Tarreau2be39392010-01-03 17:24:51 +01003442 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3443 * If this happens, then the data will not come immediately, so we must
3444 * send all what we have without waiting. Note that due to the small gain
3445 * in waiting for the body of the request, it's easier to simply put the
3446 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3447 * itself once used.
3448 */
3449 req->flags |= BF_SEND_DONTWAIT;
3450
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003451 /* that's OK for us now, let's move on to next analysers */
3452 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003453
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003454 return_bad_req:
3455 /* We centralize bad requests processing here */
3456 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3457 /* we detected a parsing error. We want to archive this request
3458 * in the dedicated proxy area for later troubleshooting.
3459 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003460 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003461 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003462
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003463 txn->req.msg_state = HTTP_MSG_ERROR;
3464 txn->status = 400;
3465 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003466
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003467 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003468 if (s->listener->counters)
3469 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003470
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003471 return_prx_cond:
3472 if (!(s->flags & SN_ERR_MASK))
3473 s->flags |= SN_ERR_PRXCOND;
3474 if (!(s->flags & SN_FINST_MASK))
3475 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003476
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003477 req->analysers = 0;
3478 req->analyse_exp = TICK_ETERNITY;
3479 return 0;
3480}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003481
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003482/* This function performs all the processing enabled for the current request.
3483 * It returns 1 if the processing can continue on next analysers, or zero if it
3484 * needs more data, encounters an error, or wants to immediately abort the
3485 * request. It relies on buffers flags, and updates s->req->analysers.
3486 */
3487int http_process_request(struct session *s, struct buffer *req, int an_bit)
3488{
3489 struct http_txn *txn = &s->txn;
3490 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003491
Willy Tarreau655dce92009-11-08 13:10:58 +01003492 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003493 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003494 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003495 return 0;
3496 }
3497
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003498 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3499 now_ms, __FUNCTION__,
3500 s,
3501 req,
3502 req->rex, req->wex,
3503 req->flags,
3504 req->l,
3505 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003506
Willy Tarreau59234e92008-11-30 23:51:27 +01003507 /*
3508 * Right now, we know that we have processed the entire headers
3509 * and that unwanted requests have been filtered out. We can do
3510 * whatever we want with the remaining request. Also, now we
3511 * may have separate values for ->fe, ->be.
3512 */
Willy Tarreau06619262006-12-17 08:37:22 +01003513
Willy Tarreau59234e92008-11-30 23:51:27 +01003514 /*
3515 * If HTTP PROXY is set we simply get remote server address
3516 * parsing incoming request.
3517 */
3518 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau957c0a52011-03-03 17:42:23 +01003519 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.s.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003520 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003521
Willy Tarreau59234e92008-11-30 23:51:27 +01003522 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003523 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003524 * Note that doing so might move headers in the request, but
3525 * the fields will stay coherent and the URI will not move.
3526 * This should only be performed in the backend.
3527 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003528 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003529 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3530 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003531
Willy Tarreau59234e92008-11-30 23:51:27 +01003532 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003533 * 8: the appsession cookie was looked up very early in 1.2,
3534 * so let's do the same now.
3535 */
3536
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003537 /* It needs to look into the URI unless persistence must be ignored */
3538 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003539 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003540 }
3541
3542 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003543 * 9: add X-Forwarded-For if either the frontend or the backend
3544 * asks for it.
3545 */
3546 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau957c0a52011-03-03 17:42:23 +01003547 if (s->req->prod->addr.c.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003548 /* Add an X-Forwarded-For header unless the source IP is
3549 * in the 'except' network range.
3550 */
3551 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003552 (((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003553 != s->fe->except_net.s_addr) &&
3554 (!s->be->except_mask.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003555 (((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003556 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003557 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003558 unsigned char *pn;
Willy Tarreau957c0a52011-03-03 17:42:23 +01003559 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003560
3561 /* Note: we rely on the backend to get the header name to be used for
3562 * x-forwarded-for, because the header is really meant for the backends.
3563 * However, if the backend did not specify any option, we have to rely
3564 * on the frontend's header name.
3565 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003566 if (s->be->fwdfor_hdr_len) {
3567 len = s->be->fwdfor_hdr_len;
3568 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003569 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003570 len = s->fe->fwdfor_hdr_len;
3571 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003572 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003573 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003574
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003575 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003576 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003577 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003578 }
3579 }
Willy Tarreau957c0a52011-03-03 17:42:23 +01003580 else if (s->req->prod->addr.c.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003581 /* FIXME: for the sake of completeness, we should also support
3582 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003583 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003584 int len;
3585 char pn[INET6_ADDRSTRLEN];
3586 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +01003587 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003588 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003589
Willy Tarreau59234e92008-11-30 23:51:27 +01003590 /* Note: we rely on the backend to get the header name to be used for
3591 * x-forwarded-for, because the header is really meant for the backends.
3592 * However, if the backend did not specify any option, we have to rely
3593 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003594 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003595 if (s->be->fwdfor_hdr_len) {
3596 len = s->be->fwdfor_hdr_len;
3597 memcpy(trash, s->be->fwdfor_hdr_name, len);
3598 } else {
3599 len = s->fe->fwdfor_hdr_len;
3600 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003601 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003602 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003603
Willy Tarreau59234e92008-11-30 23:51:27 +01003604 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003605 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003606 goto return_bad_req;
3607 }
3608 }
3609
3610 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003611 * 10: add X-Original-To if either the frontend or the backend
3612 * asks for it.
3613 */
3614 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3615
3616 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau957c0a52011-03-03 17:42:23 +01003617 if (s->req->prod->addr.c.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003618 /* Add an X-Original-To header unless the destination IP is
3619 * in the 'except' network range.
3620 */
3621 if (!(s->flags & SN_FRT_ADDR_SET))
3622 get_frt_addr(s);
3623
Willy Tarreau957c0a52011-03-03 17:42:23 +01003624 if (s->req->prod->addr.c.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003625 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003626 (((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003627 != s->fe->except_to.s_addr) &&
3628 (!s->be->except_mask_to.s_addr ||
Willy Tarreau957c0a52011-03-03 17:42:23 +01003629 (((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003630 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003631 int len;
3632 unsigned char *pn;
Willy Tarreau957c0a52011-03-03 17:42:23 +01003633 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003634
3635 /* Note: we rely on the backend to get the header name to be used for
3636 * x-original-to, because the header is really meant for the backends.
3637 * However, if the backend did not specify any option, we have to rely
3638 * on the frontend's header name.
3639 */
3640 if (s->be->orgto_hdr_len) {
3641 len = s->be->orgto_hdr_len;
3642 memcpy(trash, s->be->orgto_hdr_name, len);
3643 } else {
3644 len = s->fe->orgto_hdr_len;
3645 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003646 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003647 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3648
3649 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003650 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003651 goto return_bad_req;
3652 }
3653 }
3654 }
3655
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003656 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3657 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003658 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003659 unsigned int want_flags = 0;
3660
3661 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003662 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3663 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3664 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003665 want_flags |= TX_CON_CLO_SET;
3666 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003667 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3668 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3669 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003670 want_flags |= TX_CON_KAL_SET;
3671 }
3672
3673 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3674 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003675 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003676
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003677
Willy Tarreau522d6c02009-12-06 18:49:18 +01003678 /* If we have no server assigned yet and we're balancing on url_param
3679 * with a POST request, we may be interested in checking the body for
3680 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003681 */
3682 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3683 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003684 s->be->url_param_post_limit != 0 &&
Willy Tarreau61a21a32011-03-01 20:35:49 +01003685 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003686 buffer_dont_connect(req);
3687 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003688 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003689
Willy Tarreaud98cf932009-12-27 22:54:55 +01003690 if (txn->flags & TX_REQ_XFER_LEN)
3691 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003692
Willy Tarreau59234e92008-11-30 23:51:27 +01003693 /*************************************************************
3694 * OK, that's finished for the headers. We have done what we *
3695 * could. Let's switch to the DATA state. *
3696 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003697 req->analyse_exp = TICK_ETERNITY;
3698 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003699
Willy Tarreau59234e92008-11-30 23:51:27 +01003700 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003701 /* OK let's go on with the BODY now */
3702 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003703
Willy Tarreau59234e92008-11-30 23:51:27 +01003704 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003705 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003706 /* we detected a parsing error. We want to archive this request
3707 * in the dedicated proxy area for later troubleshooting.
3708 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003709 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003710 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003711
Willy Tarreau59234e92008-11-30 23:51:27 +01003712 txn->req.msg_state = HTTP_MSG_ERROR;
3713 txn->status = 400;
3714 req->analysers = 0;
3715 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003716
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003717 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003718 if (s->listener->counters)
3719 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003720
Willy Tarreau59234e92008-11-30 23:51:27 +01003721 if (!(s->flags & SN_ERR_MASK))
3722 s->flags |= SN_ERR_PRXCOND;
3723 if (!(s->flags & SN_FINST_MASK))
3724 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003725 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003726}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003727
Willy Tarreau60b85b02008-11-30 23:28:40 +01003728/* This function is an analyser which processes the HTTP tarpit. It always
3729 * returns zero, at the beginning because it prevents any other processing
3730 * from occurring, and at the end because it terminates the request.
3731 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003732int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003733{
3734 struct http_txn *txn = &s->txn;
3735
3736 /* This connection is being tarpitted. The CLIENT side has
3737 * already set the connect expiration date to the right
3738 * timeout. We just have to check that the client is still
3739 * there and that the timeout has not expired.
3740 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003741 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003742 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3743 !tick_is_expired(req->analyse_exp, now_ms))
3744 return 0;
3745
3746 /* We will set the queue timer to the time spent, just for
3747 * logging purposes. We fake a 500 server error, so that the
3748 * attacker will not suspect his connection has been tarpitted.
3749 * It will not cause trouble to the logs because we can exclude
3750 * the tarpitted connections by filtering on the 'PT' status flags.
3751 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003752 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3753
3754 txn->status = 500;
3755 if (req->flags != BF_READ_ERROR)
3756 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3757
3758 req->analysers = 0;
3759 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003760
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003761 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003762 if (s->listener->counters)
3763 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003764
Willy Tarreau60b85b02008-11-30 23:28:40 +01003765 if (!(s->flags & SN_ERR_MASK))
3766 s->flags |= SN_ERR_PRXCOND;
3767 if (!(s->flags & SN_FINST_MASK))
3768 s->flags |= SN_FINST_T;
3769 return 0;
3770}
3771
Willy Tarreaud34af782008-11-30 23:36:37 +01003772/* This function is an analyser which processes the HTTP request body. It looks
3773 * for parameters to be used for the load balancing algorithm (url_param). It
3774 * must only be called after the standard HTTP request processing has occurred,
3775 * because it expects the request to be parsed. It returns zero if it needs to
3776 * read more data, or 1 once it has completed its analysis.
3777 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003778int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003779{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003780 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003781 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003782 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003783
3784 /* We have to parse the HTTP request body to find any required data.
3785 * "balance url_param check_post" should have been the only way to get
3786 * into this. We were brought here after HTTP header analysis, so all
3787 * related structures are ready.
3788 */
3789
Willy Tarreau522d6c02009-12-06 18:49:18 +01003790 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3791 goto missing_data;
3792
3793 if (msg->msg_state < HTTP_MSG_100_SENT) {
3794 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3795 * send an HTTP/1.1 100 Continue intermediate response.
3796 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003797 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003798 struct hdr_ctx ctx;
3799 ctx.idx = 0;
3800 /* Expect is allowed in 1.1, look for it */
3801 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3802 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3803 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3804 }
3805 }
3806 msg->msg_state = HTTP_MSG_100_SENT;
3807 }
3808
3809 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003810 /* we have msg->col and msg->sov which both point to the first
3811 * byte of message body. msg->som still points to the beginning
3812 * of the message. We must save the body in req->lr because it
3813 * survives buffer re-alignments.
3814 */
3815 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003816 if (txn->flags & TX_REQ_TE_CHNK)
3817 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3818 else
3819 msg->msg_state = HTTP_MSG_DATA;
3820 }
3821
3822 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003823 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003824 * set ->sov and ->lr to point to the body and switch to DATA or
3825 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003826 */
3827 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003828
Willy Tarreau115acb92009-12-26 13:56:06 +01003829 if (!ret)
3830 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003831 else if (ret < 0) {
3832 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003833 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003834 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003835 }
3836
Willy Tarreaud98cf932009-12-27 22:54:55 +01003837 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003838 * We have the first non-header byte in msg->col, which is either the
3839 * beginning of the chunk size or of the data. The first data byte is in
3840 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3841 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003842 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003843
Willy Tarreau124d9912011-03-01 20:30:48 +01003844 if (msg->body_len < limit)
3845 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003846
Willy Tarreau7c96f672009-12-27 22:47:25 +01003847 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003848 goto http_end;
3849
3850 missing_data:
3851 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003852 if (req->flags & BF_FULL) {
3853 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003854 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003855 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003856
Willy Tarreau522d6c02009-12-06 18:49:18 +01003857 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3858 txn->status = 408;
3859 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003860
3861 if (!(s->flags & SN_ERR_MASK))
3862 s->flags |= SN_ERR_CLITO;
3863 if (!(s->flags & SN_FINST_MASK))
3864 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003865 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003866 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003867
3868 /* we get here if we need to wait for more data */
3869 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003870 /* Not enough data. We'll re-use the http-request
3871 * timeout here. Ideally, we should set the timeout
3872 * relative to the accept() date. We just set the
3873 * request timeout once at the beginning of the
3874 * request.
3875 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003876 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003877 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003878 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003879 return 0;
3880 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003881
3882 http_end:
3883 /* The situation will not evolve, so let's give up on the analysis. */
3884 s->logs.tv_request = now; /* update the request timer to reflect full request */
3885 req->analysers &= ~an_bit;
3886 req->analyse_exp = TICK_ETERNITY;
3887 return 1;
3888
3889 return_bad_req: /* let's centralize all bad requests */
3890 txn->req.msg_state = HTTP_MSG_ERROR;
3891 txn->status = 400;
3892 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3893
Willy Tarreau79ebac62010-06-07 13:47:49 +02003894 if (!(s->flags & SN_ERR_MASK))
3895 s->flags |= SN_ERR_PRXCOND;
3896 if (!(s->flags & SN_FINST_MASK))
3897 s->flags |= SN_FINST_R;
3898
Willy Tarreau522d6c02009-12-06 18:49:18 +01003899 return_err_msg:
3900 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003901 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003902 if (s->listener->counters)
3903 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003904 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003905}
3906
Willy Tarreau610ecce2010-01-04 21:15:02 +01003907/* Terminate current transaction and prepare a new one. This is very tricky
3908 * right now but it works.
3909 */
3910void http_end_txn_clean_session(struct session *s)
3911{
3912 /* FIXME: We need a more portable way of releasing a backend's and a
3913 * server's connections. We need a safer way to reinitialize buffer
3914 * flags. We also need a more accurate method for computing per-request
3915 * data.
3916 */
3917 http_silent_debug(__LINE__, s);
3918
3919 s->req->cons->flags |= SI_FL_NOLINGER;
3920 s->req->cons->shutr(s->req->cons);
3921 s->req->cons->shutw(s->req->cons);
3922
3923 http_silent_debug(__LINE__, s);
3924
3925 if (s->flags & SN_BE_ASSIGNED)
3926 s->be->beconn--;
3927
3928 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3929 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003930 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003931
3932 if (s->txn.status) {
3933 int n;
3934
3935 n = s->txn.status / 100;
3936 if (n < 1 || n > 5)
3937 n = 0;
3938
3939 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003940 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003941
Willy Tarreau24657792010-02-26 10:30:28 +01003942 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003943 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003944 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003945 }
3946
3947 /* don't count other requests' data */
3948 s->logs.bytes_in -= s->req->l - s->req->send_max;
3949 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3950
3951 /* let's do a final log if we need it */
3952 if (s->logs.logwait &&
3953 !(s->flags & SN_MONITOR) &&
3954 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3955 s->do_log(s);
3956 }
3957
3958 s->logs.accept_date = date; /* user-visible date for logging */
3959 s->logs.tv_accept = now; /* corrected date for internal use */
3960 tv_zero(&s->logs.tv_request);
3961 s->logs.t_queue = -1;
3962 s->logs.t_connect = -1;
3963 s->logs.t_data = -1;
3964 s->logs.t_close = 0;
3965 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3966 s->logs.srv_queue_size = 0; /* we will get this number soon */
3967
3968 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3969 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3970
3971 if (s->pend_pos)
3972 pendconn_free(s->pend_pos);
3973
Willy Tarreau827aee92011-03-10 16:55:02 +01003974 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003975 if (s->flags & SN_CURR_SESS) {
3976 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003977 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003978 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003979 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3980 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003981 }
3982
3983 if (unlikely(s->srv_conn))
3984 sess_change_server(s, NULL);
Willy Tarreau9e000c62011-03-10 14:03:36 +01003985 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003986
3987 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3988 s->req->cons->fd = -1; /* just to help with debugging */
3989 s->req->cons->err_type = SI_ET_NONE;
3990 s->req->cons->err_loc = NULL;
3991 s->req->cons->exp = TICK_ETERNITY;
3992 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003993 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3994 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 +02003995 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 +01003996 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3997 s->txn.meth = 0;
3998 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003999 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004000 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004001 s->req->cons->flags |= SI_FL_INDEP_STR;
4002
4003 /* if the request buffer is not empty, it means we're
4004 * about to process another request, so send pending
4005 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004006 * Just don't do this if the buffer is close to be full,
4007 * because the request will wait for it to flush a little
4008 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 */
Willy Tarreau065e8332010-01-08 00:30:20 +01004010 if (s->req->l > s->req->send_max) {
4011 if (s->rep->send_max &&
4012 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01004013 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
4014 s->rep->flags |= BF_EXPECT_MORE;
4015 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004016
4017 /* we're removing the analysers, we MUST re-enable events detection */
4018 buffer_auto_read(s->req);
4019 buffer_auto_close(s->req);
4020 buffer_auto_read(s->rep);
4021 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022
4023 /* make ->lr point to the first non-forwarded byte */
4024 s->req->lr = s->req->w + s->req->send_max;
4025 if (s->req->lr >= s->req->data + s->req->size)
4026 s->req->lr -= s->req->size;
4027 s->rep->lr = s->rep->w + s->rep->send_max;
4028 if (s->rep->lr >= s->rep->data + s->rep->size)
4029 s->rep->lr -= s->req->size;
4030
Willy Tarreau342b11c2010-11-24 16:22:09 +01004031 s->req->analysers = s->listener->analysers;
4032 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033 s->rep->analysers = 0;
4034
4035 http_silent_debug(__LINE__, s);
4036}
4037
4038
4039/* This function updates the request state machine according to the response
4040 * state machine and buffer flags. It returns 1 if it changes anything (flag
4041 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4042 * it is only used to find when a request/response couple is complete. Both
4043 * this function and its equivalent should loop until both return zero. It
4044 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4045 */
4046int http_sync_req_state(struct session *s)
4047{
4048 struct buffer *buf = s->req;
4049 struct http_txn *txn = &s->txn;
4050 unsigned int old_flags = buf->flags;
4051 unsigned int old_state = txn->req.msg_state;
4052
4053 http_silent_debug(__LINE__, s);
4054 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4055 return 0;
4056
4057 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004058 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004059 * We can shut the read side unless we want to abort_on_close,
4060 * or we have a POST request. The issue with POST requests is
4061 * that some browsers still send a CRLF after the request, and
4062 * this CRLF must be read so that it does not remain in the kernel
4063 * buffers, otherwise a close could cause an RST on some systems
4064 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004065 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004066 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01004067 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004068
4069 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4070 goto wait_other_side;
4071
4072 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4073 /* The server has not finished to respond, so we
4074 * don't want to move in order not to upset it.
4075 */
4076 goto wait_other_side;
4077 }
4078
4079 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4080 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004081 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004082 txn->req.msg_state = HTTP_MSG_TUNNEL;
4083 goto wait_other_side;
4084 }
4085
4086 /* When we get here, it means that both the request and the
4087 * response have finished receiving. Depending on the connection
4088 * mode, we'll have to wait for the last bytes to leave in either
4089 * direction, and sometimes for a close to be effective.
4090 */
4091
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004092 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4093 /* Server-close mode : queue a connection close to the server */
4094 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004095 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004096 }
4097 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4098 /* Option forceclose is set, or either side wants to close,
4099 * let's enforce it now that we're not expecting any new
4100 * data to come. The caller knows the session is complete
4101 * once both states are CLOSED.
4102 */
4103 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004104 buffer_shutr_now(buf);
4105 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004106 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004107 }
4108 else {
4109 /* The last possible modes are keep-alive and tunnel. Since tunnel
4110 * mode does not set the body analyser, we can't reach this place
4111 * in tunnel mode, so we're left with keep-alive only.
4112 * This mode is currently not implemented, we switch to tunnel mode.
4113 */
4114 buffer_auto_read(buf);
4115 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004116 }
4117
4118 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4119 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004120 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4121
Willy Tarreau610ecce2010-01-04 21:15:02 +01004122 if (!(buf->flags & BF_OUT_EMPTY)) {
4123 txn->req.msg_state = HTTP_MSG_CLOSING;
4124 goto http_msg_closing;
4125 }
4126 else {
4127 txn->req.msg_state = HTTP_MSG_CLOSED;
4128 goto http_msg_closed;
4129 }
4130 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004131 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004132 }
4133
4134 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4135 http_msg_closing:
4136 /* nothing else to forward, just waiting for the output buffer
4137 * to be empty and for the shutw_now to take effect.
4138 */
4139 if (buf->flags & BF_OUT_EMPTY) {
4140 txn->req.msg_state = HTTP_MSG_CLOSED;
4141 goto http_msg_closed;
4142 }
4143 else if (buf->flags & BF_SHUTW) {
4144 txn->req.msg_state = HTTP_MSG_ERROR;
4145 goto wait_other_side;
4146 }
4147 }
4148
4149 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4150 http_msg_closed:
4151 goto wait_other_side;
4152 }
4153
4154 wait_other_side:
4155 http_silent_debug(__LINE__, s);
4156 return txn->req.msg_state != old_state || buf->flags != old_flags;
4157}
4158
4159
4160/* This function updates the response state machine according to the request
4161 * state machine and buffer flags. It returns 1 if it changes anything (flag
4162 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4163 * it is only used to find when a request/response couple is complete. Both
4164 * this function and its equivalent should loop until both return zero. It
4165 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4166 */
4167int http_sync_res_state(struct session *s)
4168{
4169 struct buffer *buf = s->rep;
4170 struct http_txn *txn = &s->txn;
4171 unsigned int old_flags = buf->flags;
4172 unsigned int old_state = txn->rsp.msg_state;
4173
4174 http_silent_debug(__LINE__, s);
4175 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4176 return 0;
4177
4178 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4179 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004180 * still monitor the server connection for a possible close
4181 * while the request is being uploaded, so we don't disable
4182 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004183 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004184 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004185
4186 if (txn->req.msg_state == HTTP_MSG_ERROR)
4187 goto wait_other_side;
4188
4189 if (txn->req.msg_state < HTTP_MSG_DONE) {
4190 /* The client seems to still be sending data, probably
4191 * because we got an error response during an upload.
4192 * We have the choice of either breaking the connection
4193 * or letting it pass through. Let's do the later.
4194 */
4195 goto wait_other_side;
4196 }
4197
4198 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4199 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004200 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004201 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4202 goto wait_other_side;
4203 }
4204
4205 /* When we get here, it means that both the request and the
4206 * response have finished receiving. Depending on the connection
4207 * mode, we'll have to wait for the last bytes to leave in either
4208 * direction, and sometimes for a close to be effective.
4209 */
4210
4211 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4212 /* Server-close mode : shut read and wait for the request
4213 * side to close its output buffer. The caller will detect
4214 * when we're in DONE and the other is in CLOSED and will
4215 * catch that for the final cleanup.
4216 */
4217 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4218 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004219 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004220 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4221 /* Option forceclose is set, or either side wants to close,
4222 * let's enforce it now that we're not expecting any new
4223 * data to come. The caller knows the session is complete
4224 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004225 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004226 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4227 buffer_shutr_now(buf);
4228 buffer_shutw_now(buf);
4229 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004230 }
4231 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004232 /* The last possible modes are keep-alive and tunnel. Since tunnel
4233 * mode does not set the body analyser, we can't reach this place
4234 * in tunnel mode, so we're left with keep-alive only.
4235 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004236 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004237 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004238 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004239 }
4240
4241 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4242 /* if we've just closed an output, let's switch */
4243 if (!(buf->flags & BF_OUT_EMPTY)) {
4244 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4245 goto http_msg_closing;
4246 }
4247 else {
4248 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4249 goto http_msg_closed;
4250 }
4251 }
4252 goto wait_other_side;
4253 }
4254
4255 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4256 http_msg_closing:
4257 /* nothing else to forward, just waiting for the output buffer
4258 * to be empty and for the shutw_now to take effect.
4259 */
4260 if (buf->flags & BF_OUT_EMPTY) {
4261 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4262 goto http_msg_closed;
4263 }
4264 else if (buf->flags & BF_SHUTW) {
4265 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004266 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004267 if (target_srv(&s->target))
4268 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004269 goto wait_other_side;
4270 }
4271 }
4272
4273 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4274 http_msg_closed:
4275 /* drop any pending data */
4276 buffer_ignore(buf, buf->l - buf->send_max);
4277 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004278 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004279 goto wait_other_side;
4280 }
4281
4282 wait_other_side:
4283 http_silent_debug(__LINE__, s);
4284 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4285}
4286
4287
4288/* Resync the request and response state machines. Return 1 if either state
4289 * changes.
4290 */
4291int http_resync_states(struct session *s)
4292{
4293 struct http_txn *txn = &s->txn;
4294 int old_req_state = txn->req.msg_state;
4295 int old_res_state = txn->rsp.msg_state;
4296
4297 http_silent_debug(__LINE__, s);
4298 http_sync_req_state(s);
4299 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004300 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004301 if (!http_sync_res_state(s))
4302 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004303 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004304 if (!http_sync_req_state(s))
4305 break;
4306 }
4307 http_silent_debug(__LINE__, s);
4308 /* OK, both state machines agree on a compatible state.
4309 * There are a few cases we're interested in :
4310 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4311 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4312 * directions, so let's simply disable both analysers.
4313 * - HTTP_MSG_CLOSED on the response only means we must abort the
4314 * request.
4315 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4316 * with server-close mode means we've completed one request and we
4317 * must re-initialize the server connection.
4318 */
4319
4320 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4321 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4322 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4323 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4324 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004325 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004326 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004327 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004328 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004329 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004330 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004331 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4332 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004333 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004334 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004335 s->rep->analysers = 0;
4336 buffer_auto_close(s->rep);
4337 buffer_auto_read(s->rep);
4338 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004339 buffer_abort(s->req);
4340 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004341 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004342 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004343 }
4344 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4345 txn->rsp.msg_state == HTTP_MSG_DONE &&
4346 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4347 /* server-close: terminate this server connection and
4348 * reinitialize a fresh-new transaction.
4349 */
4350 http_end_txn_clean_session(s);
4351 }
4352
4353 http_silent_debug(__LINE__, s);
4354 return txn->req.msg_state != old_req_state ||
4355 txn->rsp.msg_state != old_res_state;
4356}
4357
Willy Tarreaud98cf932009-12-27 22:54:55 +01004358/* This function is an analyser which forwards request body (including chunk
4359 * sizes if any). It is called as soon as we must forward, even if we forward
4360 * zero byte. The only situation where it must not be called is when we're in
4361 * tunnel mode and we want to forward till the close. It's used both to forward
4362 * remaining data and to resync after end of body. It expects the msg_state to
4363 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4364 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004365 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004366 * bytes of pending data + the headers if not already done (between som and sov).
4367 * It eventually adjusts som to match sov after the data in between have been sent.
4368 */
4369int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4370{
4371 struct http_txn *txn = &s->txn;
4372 struct http_msg *msg = &s->txn.req;
4373
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004374 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4375 return 0;
4376
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004377 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4378 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004379 /* Output closed while we were sending data. We must abort and
4380 * wake the other side up.
4381 */
4382 msg->msg_state = HTTP_MSG_ERROR;
4383 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004384 return 1;
4385 }
4386
Willy Tarreau4fe41902010-06-07 22:27:41 +02004387 /* in most states, we should abort in case of early close */
4388 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004389
4390 /* Note that we don't have to send 100-continue back because we don't
4391 * need the data to complete our job, and it's up to the server to
4392 * decide whether to return 100, 417 or anything else in return of
4393 * an "Expect: 100-continue" header.
4394 */
4395
4396 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4397 /* we have msg->col and msg->sov which both point to the first
4398 * byte of message body. msg->som still points to the beginning
4399 * of the message. We must save the body in req->lr because it
4400 * survives buffer re-alignments.
4401 */
4402 req->lr = req->data + msg->sov;
4403 if (txn->flags & TX_REQ_TE_CHNK)
4404 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4405 else {
4406 msg->msg_state = HTTP_MSG_DATA;
4407 }
4408 }
4409
Willy Tarreaud98cf932009-12-27 22:54:55 +01004410 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004411 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004412 /* we may have some data pending */
Willy Tarreau124d9912011-03-01 20:30:48 +01004413 if (msg->chunk_len || msg->som != msg->sov) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004414 int bytes = msg->sov - msg->som;
4415 if (bytes < 0) /* sov may have wrapped at the end */
4416 bytes += req->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01004417 buffer_forward(req, bytes + msg->chunk_len);
4418 msg->chunk_len = 0; /* don't forward that again */
Willy Tarreau638cd022010-01-03 07:42:04 +01004419 msg->som = msg->sov;
4420 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004421
Willy Tarreaucaabe412010-01-03 23:08:28 +01004422 if (msg->msg_state == HTTP_MSG_DATA) {
4423 /* must still forward */
4424 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004425 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004426
4427 /* nothing left to forward */
4428 if (txn->flags & TX_REQ_TE_CHNK)
4429 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004430 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004431 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004432 }
4433 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004434 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004435 * set ->sov and ->lr to point to the body and switch to DATA or
4436 * TRAILERS state.
4437 */
4438 int ret = http_parse_chunk_size(req, msg);
4439
4440 if (!ret)
4441 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004442 else if (ret < 0) {
4443 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004444 if (msg->err_pos >= 0)
4445 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004446 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004447 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004448 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreau57f5c122010-12-02 00:37:14 +01004449 /* Don't set a PUSH at the end of that chunk if it's not the last one */
4450 if (msg->msg_state == HTTP_MSG_DATA)
4451 req->flags |= BF_EXPECT_MORE;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004452 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004453 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4454 /* we want the CRLF after the data */
4455 int ret;
4456
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004457 req->lr = req->w + req->send_max;
4458 if (req->lr >= req->data + req->size)
4459 req->lr -= req->size;
4460
Willy Tarreaud98cf932009-12-27 22:54:55 +01004461 ret = http_skip_chunk_crlf(req, msg);
4462
4463 if (ret == 0)
4464 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004465 else if (ret < 0) {
4466 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004467 if (msg->err_pos >= 0)
4468 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004469 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004470 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004471 /* we're in MSG_CHUNK_SIZE now */
4472 }
4473 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4474 int ret = http_forward_trailers(req, msg);
4475
4476 if (ret == 0)
4477 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004478 else if (ret < 0) {
4479 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004480 if (msg->err_pos >= 0)
4481 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004482 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004483 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004484 /* we're in HTTP_MSG_DONE now */
4485 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004486 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004487 int old_state = msg->msg_state;
4488
Willy Tarreau610ecce2010-01-04 21:15:02 +01004489 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004490 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004491 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4492 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4493 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004494 if (http_resync_states(s)) {
4495 /* some state changes occurred, maybe the analyser
4496 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004497 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004498 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4499 if (req->flags & BF_SHUTW) {
4500 /* request errors are most likely due to
4501 * the server aborting the transfer.
4502 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004503 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004504 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004505 if (msg->err_pos >= 0)
4506 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004507 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004508 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004509 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004510 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004511
4512 /* If "option abortonclose" is set on the backend, we
4513 * want to monitor the client's connection and forward
4514 * any shutdown notification to the server, which will
4515 * decide whether to close or to go on processing the
4516 * request.
4517 */
4518 if (s->be->options & PR_O_ABRT_CLOSE) {
4519 buffer_auto_read(req);
4520 buffer_auto_close(req);
4521 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004522 else if (s->txn.meth == HTTP_METH_POST) {
4523 /* POST requests may require to read extra CRLF
4524 * sent by broken browsers and which could cause
4525 * an RST to be sent upon close on some systems
4526 * (eg: Linux).
4527 */
4528 buffer_auto_read(req);
4529 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004530
Willy Tarreau610ecce2010-01-04 21:15:02 +01004531 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004532 }
4533 }
4534
Willy Tarreaud98cf932009-12-27 22:54:55 +01004535 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004536 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004537 if (req->flags & BF_SHUTR) {
4538 if (!(s->flags & SN_ERR_MASK))
4539 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004540 if (!(s->flags & SN_FINST_MASK)) {
4541 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4542 s->flags |= SN_FINST_H;
4543 else
4544 s->flags |= SN_FINST_D;
4545 }
4546
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004547 s->fe->fe_counters.cli_aborts++;
4548 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004549 if (target_srv(&s->target))
4550 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004551
4552 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004553 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004554
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004555 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004556 if (req->flags & BF_SHUTW)
4557 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004558
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004559 /* When TE: chunked is used, we need to get there again to parse remaining
4560 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4561 */
4562 if (txn->flags & TX_REQ_TE_CHNK)
4563 buffer_dont_close(req);
4564
Willy Tarreau610ecce2010-01-04 21:15:02 +01004565 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004566 return 0;
4567
Willy Tarreaud98cf932009-12-27 22:54:55 +01004568 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004569 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004570 if (s->listener->counters)
4571 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004572 return_bad_req_stats_ok:
4573 txn->req.msg_state = HTTP_MSG_ERROR;
4574 if (txn->status) {
4575 /* Note: we don't send any error if some data were already sent */
4576 stream_int_retnclose(req->prod, NULL);
4577 } else {
4578 txn->status = 400;
4579 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4580 }
4581 req->analysers = 0;
4582 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004583
4584 if (!(s->flags & SN_ERR_MASK))
4585 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004586 if (!(s->flags & SN_FINST_MASK)) {
4587 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4588 s->flags |= SN_FINST_H;
4589 else
4590 s->flags |= SN_FINST_D;
4591 }
4592 return 0;
4593
4594 aborted_xfer:
4595 txn->req.msg_state = HTTP_MSG_ERROR;
4596 if (txn->status) {
4597 /* Note: we don't send any error if some data were already sent */
4598 stream_int_retnclose(req->prod, NULL);
4599 } else {
4600 txn->status = 502;
4601 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4602 }
4603 req->analysers = 0;
4604 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4605
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004606 s->fe->fe_counters.srv_aborts++;
4607 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004608 if (target_srv(&s->target))
4609 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004610
4611 if (!(s->flags & SN_ERR_MASK))
4612 s->flags |= SN_ERR_SRVCL;
4613 if (!(s->flags & SN_FINST_MASK)) {
4614 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4615 s->flags |= SN_FINST_H;
4616 else
4617 s->flags |= SN_FINST_D;
4618 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004619 return 0;
4620}
4621
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004622/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4623 * processing can continue on next analysers, or zero if it either needs more
4624 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4625 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4626 * when it has nothing left to do, and may remove any analyser when it wants to
4627 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004628 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004630{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004631 struct http_txn *txn = &s->txn;
4632 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004633 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004634 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004635 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004636 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004637
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004638 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 +02004639 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004640 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004641 rep,
4642 rep->rex, rep->wex,
4643 rep->flags,
4644 rep->l,
4645 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004646
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004647 /*
4648 * Now parse the partial (or complete) lines.
4649 * We will check the response syntax, and also join multi-line
4650 * headers. An index of all the lines will be elaborated while
4651 * parsing.
4652 *
4653 * For the parsing, we use a 28 states FSM.
4654 *
4655 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004656 * rep->data + msg->som = beginning of response
4657 * rep->data + msg->eoh = end of processed headers / start of current one
4658 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004659 * rep->lr = first non-visited byte
4660 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004661 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004662 */
4663
Willy Tarreau83e3af02009-12-28 17:39:57 +01004664 /* There's a protected area at the end of the buffer for rewriting
4665 * purposes. We don't want to start to parse the request if the
4666 * protected area is affected, because we may have to move processed
4667 * data later, which is much more complicated.
4668 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004669 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4670 if (unlikely((rep->flags & BF_FULL) ||
4671 rep->r < rep->lr ||
4672 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4673 if (rep->send_max) {
4674 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004675 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4676 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004677 buffer_dont_close(rep);
4678 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4679 return 0;
4680 }
4681 if (rep->l <= rep->size - global.tune.maxrewrite)
4682 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004683 }
4684
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004685 if (likely(rep->lr < rep->r))
4686 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004687 }
4688
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004689 /* 1: we might have to print this header in debug mode */
4690 if (unlikely((global.mode & MODE_DEBUG) &&
4691 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004692 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004693 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004694 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004695
Willy Tarreau663308b2010-06-07 14:06:08 +02004696 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004697 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004698 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004699
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004700 sol += hdr_idx_first_pos(&txn->hdr_idx);
4701 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004702
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004703 while (cur_idx) {
4704 eol = sol + txn->hdr_idx.v[cur_idx].len;
4705 debug_hdr("srvhdr", s, sol, eol);
4706 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4707 cur_idx = txn->hdr_idx.v[cur_idx].next;
4708 }
4709 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004710
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004711 /*
4712 * Now we quickly check if we have found a full valid response.
4713 * If not so, we check the FD and buffer states before leaving.
4714 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004715 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004716 * responses are checked first.
4717 *
4718 * Depending on whether the client is still there or not, we
4719 * may send an error response back or not. Note that normally
4720 * we should only check for HTTP status there, and check I/O
4721 * errors somewhere else.
4722 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004723
Willy Tarreau655dce92009-11-08 13:10:58 +01004724 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004725 /* Invalid response */
4726 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4727 /* we detected a parsing error. We want to archive this response
4728 * in the dedicated proxy area for later troubleshooting.
4729 */
4730 hdr_response_bad:
4731 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004732 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004733
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004734 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004735 if (target_srv(&s->target)) {
4736 target_srv(&s->target)->counters.failed_resp++;
4737 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004738 }
Willy Tarreau64648412010-03-05 10:41:54 +01004739 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004740 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004741 rep->analysers = 0;
4742 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004743 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004744 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004745 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4746
4747 if (!(s->flags & SN_ERR_MASK))
4748 s->flags |= SN_ERR_PRXCOND;
4749 if (!(s->flags & SN_FINST_MASK))
4750 s->flags |= SN_FINST_H;
4751
4752 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004753 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004754
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004755 /* too large response does not fit in buffer. */
4756 else if (rep->flags & BF_FULL) {
4757 goto hdr_response_bad;
4758 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004759
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004760 /* read error */
4761 else if (rep->flags & BF_READ_ERROR) {
4762 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004763 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004764
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004765 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004766 if (target_srv(&s->target)) {
4767 target_srv(&s->target)->counters.failed_resp++;
4768 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004769 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004770
Willy Tarreau90deb182010-01-07 00:20:41 +01004771 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004772 rep->analysers = 0;
4773 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004774 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004775 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004776 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004777
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004778 if (!(s->flags & SN_ERR_MASK))
4779 s->flags |= SN_ERR_SRVCL;
4780 if (!(s->flags & SN_FINST_MASK))
4781 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004782 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004783 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004784
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004785 /* read timeout : return a 504 to the client. */
4786 else if (rep->flags & BF_READ_TIMEOUT) {
4787 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004788 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004789
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004790 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004791 if (target_srv(&s->target)) {
4792 target_srv(&s->target)->counters.failed_resp++;
4793 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004794 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004795
Willy Tarreau90deb182010-01-07 00:20:41 +01004796 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004797 rep->analysers = 0;
4798 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004799 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004800 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004801 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004802
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004803 if (!(s->flags & SN_ERR_MASK))
4804 s->flags |= SN_ERR_SRVTO;
4805 if (!(s->flags & SN_FINST_MASK))
4806 s->flags |= SN_FINST_H;
4807 return 0;
4808 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004809
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004810 /* close from server */
4811 else if (rep->flags & BF_SHUTR) {
4812 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004813 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004814
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004815 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004816 if (target_srv(&s->target)) {
4817 target_srv(&s->target)->counters.failed_resp++;
4818 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004819 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004820
Willy Tarreau90deb182010-01-07 00:20:41 +01004821 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004822 rep->analysers = 0;
4823 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004824 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004825 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004826 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004827
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004828 if (!(s->flags & SN_ERR_MASK))
4829 s->flags |= SN_ERR_SRVCL;
4830 if (!(s->flags & SN_FINST_MASK))
4831 s->flags |= SN_FINST_H;
4832 return 0;
4833 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004834
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004835 /* write error to client (we don't send any message then) */
4836 else if (rep->flags & BF_WRITE_ERROR) {
4837 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004838 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004839
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004840 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004841 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004842 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004843
4844 if (!(s->flags & SN_ERR_MASK))
4845 s->flags |= SN_ERR_CLICL;
4846 if (!(s->flags & SN_FINST_MASK))
4847 s->flags |= SN_FINST_H;
4848
4849 /* process_session() will take care of the error */
4850 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004851 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004852
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004853 buffer_dont_close(rep);
4854 return 0;
4855 }
4856
4857 /* More interesting part now : we know that we have a complete
4858 * response which at least looks like HTTP. We have an indicator
4859 * of each header's length, so we can parse them quickly.
4860 */
4861
4862 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004863 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004864
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004865 /*
4866 * 1: get the status code
4867 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004868 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004869 if (n < 1 || n > 5)
4870 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004871 /* when the client triggers a 4xx from the server, it's most often due
4872 * to a missing object or permission. These events should be tracked
4873 * because if they happen often, it may indicate a brute force or a
4874 * vulnerability scan.
4875 */
4876 if (n == 4)
4877 session_inc_http_err_ctr(s);
4878
Willy Tarreau827aee92011-03-10 16:55:02 +01004879 if (target_srv(&s->target))
4880 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004881
Willy Tarreau5b154472009-12-21 20:11:07 +01004882 /* check if the response is HTTP/1.1 or above */
4883 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004884 ((msg->sol[5] > '1') ||
4885 ((msg->sol[5] == '1') &&
4886 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004887 txn->flags |= TX_RES_VER_11;
4888
4889 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004890 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 +01004891
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004892 /* transfer length unknown*/
4893 txn->flags &= ~TX_RES_XFER_LEN;
4894
Willy Tarreau962c3f42010-01-10 00:15:35 +01004895 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004896
Willy Tarreau39650402010-03-15 19:44:39 +01004897 /* Adjust server's health based on status code. Note: status codes 501
4898 * and 505 are triggered on demand by client request, so we must not
4899 * count them as server failures.
4900 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004901 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004902 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004903 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004904 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004905 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004906 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004907
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004908 /*
4909 * 2: check for cacheability.
4910 */
4911
4912 switch (txn->status) {
4913 case 200:
4914 case 203:
4915 case 206:
4916 case 300:
4917 case 301:
4918 case 410:
4919 /* RFC2616 @13.4:
4920 * "A response received with a status code of
4921 * 200, 203, 206, 300, 301 or 410 MAY be stored
4922 * by a cache (...) unless a cache-control
4923 * directive prohibits caching."
4924 *
4925 * RFC2616 @9.5: POST method :
4926 * "Responses to this method are not cacheable,
4927 * unless the response includes appropriate
4928 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004929 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004930 if (likely(txn->meth != HTTP_METH_POST) &&
4931 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4932 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4933 break;
4934 default:
4935 break;
4936 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004937
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004938 /*
4939 * 3: we may need to capture headers
4940 */
4941 s->logs.logwait &= ~LW_RESP;
4942 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004943 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004944 txn->rsp.cap, s->fe->rsp_cap);
4945
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004946 /* 4: determine the transfer-length.
4947 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4948 * the presence of a message-body in a RESPONSE and its transfer length
4949 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004950 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004951 * All responses to the HEAD request method MUST NOT include a
4952 * message-body, even though the presence of entity-header fields
4953 * might lead one to believe they do. All 1xx (informational), 204
4954 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4955 * message-body. All other responses do include a message-body,
4956 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004957 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004958 * 1. Any response which "MUST NOT" include a message-body (such as the
4959 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4960 * always terminated by the first empty line after the header fields,
4961 * regardless of the entity-header fields present in the message.
4962 *
4963 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4964 * the "chunked" transfer-coding (Section 6.2) is used, the
4965 * transfer-length is defined by the use of this transfer-coding.
4966 * If a Transfer-Encoding header field is present and the "chunked"
4967 * transfer-coding is not present, the transfer-length is defined by
4968 * the sender closing the connection.
4969 *
4970 * 3. If a Content-Length header field is present, its decimal value in
4971 * OCTETs represents both the entity-length and the transfer-length.
4972 * If a message is received with both a Transfer-Encoding header
4973 * field and a Content-Length header field, the latter MUST be ignored.
4974 *
4975 * 4. If the message uses the media type "multipart/byteranges", and
4976 * the transfer-length is not otherwise specified, then this self-
4977 * delimiting media type defines the transfer-length. This media
4978 * type MUST NOT be used unless the sender knows that the recipient
4979 * can parse it; the presence in a request of a Range header with
4980 * multiple byte-range specifiers from a 1.1 client implies that the
4981 * client can parse multipart/byteranges responses.
4982 *
4983 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004984 */
4985
4986 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004987 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004988 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004989 * FIXME: should we parse anyway and return an error on chunked encoding ?
4990 */
4991 if (txn->meth == HTTP_METH_HEAD ||
4992 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004993 txn->status == 204 || txn->status == 304) {
4994 txn->flags |= TX_RES_XFER_LEN;
4995 goto skip_content_length;
4996 }
4997
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004998 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004999 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01005000 while ((txn->flags & TX_RES_VER_11) &&
5001 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005002 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
5003 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5004 else if (txn->flags & TX_RES_TE_CHNK) {
5005 /* bad transfer-encoding (chunked followed by something else) */
5006 use_close_only = 1;
5007 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5008 break;
5009 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005010 }
5011
5012 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5013 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005014 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005015 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
5016 signed long long cl;
5017
5018 if (!ctx.vlen)
5019 goto hdr_response_bad;
5020
5021 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
5022 goto hdr_response_bad; /* parse failure */
5023
5024 if (cl < 0)
5025 goto hdr_response_bad;
5026
Willy Tarreau124d9912011-03-01 20:30:48 +01005027 if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl))
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005028 goto hdr_response_bad; /* already specified, was different */
5029
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005030 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005031 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005032 }
5033
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005034 /* FIXME: we should also implement the multipart/byterange method.
5035 * For now on, we resort to close mode in this case (unknown length).
5036 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005037skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005038
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005039 /* end of job, return OK */
5040 rep->analysers &= ~an_bit;
5041 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01005042 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005043 return 1;
5044}
5045
5046/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005047 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5048 * and updates t->rep->analysers. It might make sense to explode it into several
5049 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005050 */
5051int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
5052{
5053 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005054 struct http_msg *msg = &txn->rsp;
5055 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005056 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005057
5058 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
5059 now_ms, __FUNCTION__,
5060 t,
5061 rep,
5062 rep->rex, rep->wex,
5063 rep->flags,
5064 rep->l,
5065 rep->analysers);
5066
Willy Tarreau655dce92009-11-08 13:10:58 +01005067 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005068 return 0;
5069
5070 rep->analysers &= ~an_bit;
5071 rep->analyse_exp = TICK_ETERNITY;
5072
Willy Tarreau5b154472009-12-21 20:11:07 +01005073 /* Now we have to check if we need to modify the Connection header.
5074 * This is more difficult on the response than it is on the request,
5075 * because we can have two different HTTP versions and we don't know
5076 * how the client will interprete a response. For instance, let's say
5077 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5078 * HTTP/1.1 response without any header. Maybe it will bound itself to
5079 * HTTP/1.0 because it only knows about it, and will consider the lack
5080 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5081 * the lack of header as a keep-alive. Thus we will use two flags
5082 * indicating how a request MAY be understood by the client. In case
5083 * of multiple possibilities, we'll fix the header to be explicit. If
5084 * ambiguous cases such as both close and keepalive are seen, then we
5085 * will fall back to explicit close. Note that we won't take risks with
5086 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005087 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005088 */
5089
Willy Tarreaudc008c52010-02-01 16:20:08 +01005090 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5091 txn->status == 101)) {
5092 /* Either we've established an explicit tunnel, or we're
5093 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005094 * to understand the next protocols. We have to switch to tunnel
5095 * mode, so that we transfer the request and responses then let
5096 * this protocol pass unmodified. When we later implement specific
5097 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005098 * header which contains information about that protocol for
5099 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005100 */
5101 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5102 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005103 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5104 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5105 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005106 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005107
Willy Tarreau60466522010-01-18 19:08:45 +01005108 /* on unknown transfer length, we must close */
5109 if (!(txn->flags & TX_RES_XFER_LEN) &&
5110 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5111 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005112
Willy Tarreau60466522010-01-18 19:08:45 +01005113 /* now adjust header transformations depending on current state */
5114 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5115 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5116 to_del |= 2; /* remove "keep-alive" on any response */
5117 if (!(txn->flags & TX_RES_VER_11))
5118 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005119 }
Willy Tarreau60466522010-01-18 19:08:45 +01005120 else { /* SCL / KAL */
5121 to_del |= 1; /* remove "close" on any response */
5122 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
5123 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005124 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005125
Willy Tarreau60466522010-01-18 19:08:45 +01005126 /* Parse and remove some headers from the connection header */
5127 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005128
Willy Tarreau60466522010-01-18 19:08:45 +01005129 /* Some keep-alive responses are converted to Server-close if
5130 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005131 */
Willy Tarreau60466522010-01-18 19:08:45 +01005132 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5133 if ((txn->flags & TX_HDR_CONN_CLO) ||
5134 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
5135 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005136 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005137 }
5138
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005139 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005140 /*
5141 * 3: we will have to evaluate the filters.
5142 * As opposed to version 1.2, now they will be evaluated in the
5143 * filters order and not in the header order. This means that
5144 * each filter has to be validated among all headers.
5145 *
5146 * Filters are tried with ->be first, then with ->fe if it is
5147 * different from ->be.
5148 */
5149
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005150 cur_proxy = t->be;
5151 while (1) {
5152 struct proxy *rule_set = cur_proxy;
5153
5154 /* try headers filters */
5155 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005156 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005157 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005158 if (target_srv(&t->target)) {
5159 target_srv(&t->target)->counters.failed_resp++;
5160 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005161 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005162 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005163 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005164 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005165 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005166 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01005167 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005168 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005169 if (!(t->flags & SN_ERR_MASK))
5170 t->flags |= SN_ERR_PRXCOND;
5171 if (!(t->flags & SN_FINST_MASK))
5172 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005173 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005174 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005175 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005176
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005177 /* has the response been denied ? */
5178 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005179 if (target_srv(&t->target))
5180 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005181
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005182 t->be->be_counters.denied_resp++;
5183 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005184 if (t->listener->counters)
5185 t->listener->counters->denied_resp++;
5186
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005187 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005188 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005189
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005190 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005191 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005192 if (txn->status < 200)
5193 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005194 if (wl->cond) {
5195 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5196 ret = acl_pass(ret);
5197 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5198 ret = !ret;
5199 if (!ret)
5200 continue;
5201 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005202 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005203 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005204 }
5205
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005206 /* check whether we're already working on the frontend */
5207 if (cur_proxy == t->fe)
5208 break;
5209 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005210 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005211
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005212 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005213 * We may be facing a 100-continue response, in which case this
5214 * is not the right response, and we're waiting for the next one.
5215 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005216 * next one.
5217 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005218 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005219 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005220 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005221 msg->msg_state = HTTP_MSG_RPBEFORE;
5222 txn->status = 0;
5223 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5224 return 1;
5225 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005226 else if (unlikely(txn->status < 200))
5227 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005228
5229 /* we don't have any 1xx status code now */
5230
5231 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005232 * 4: check for server cookie.
5233 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005234 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5235 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005236 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005237
Willy Tarreaubaaee002006-06-26 02:48:02 +02005238
Willy Tarreaua15645d2007-03-18 16:22:39 +01005239 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005240 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005241 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005242 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005243 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005244
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005245 /*
5246 * 6: add server cookie in the response if needed
5247 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005248 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005249 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005250 (!(t->flags & SN_DIRECT) ||
5251 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5252 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5253 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5254 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005255 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5256 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005257 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005258 /* the server is known, it's not the one the client requested, or the
5259 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005260 * insert a set-cookie here, except if we want to insert only on POST
5261 * requests and this one isn't. Note that servers which don't have cookies
5262 * (eg: some backup servers) will return a full cookie removal request.
5263 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005264 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005265 len = sprintf(trash,
5266 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5267 t->be->cookie_name);
5268 }
5269 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005270 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005271
5272 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5273 /* emit last_date, which is mandatory */
5274 trash[len++] = COOKIE_DELIM_DATE;
5275 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5276 if (t->be->cookie_maxlife) {
5277 /* emit first_date, which is either the original one or
5278 * the current date.
5279 */
5280 trash[len++] = COOKIE_DELIM_DATE;
5281 s30tob64(txn->cookie_first_date ?
5282 txn->cookie_first_date >> 2 :
5283 (date.tv_sec+3) >> 2, trash + len);
5284 len += 5;
5285 }
5286 }
5287 len += sprintf(trash + len, "; path=/");
5288 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005289
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005290 if (t->be->cookie_domain)
5291 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005292
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005293 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005294 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005295 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005296
Willy Tarreauf1348312010-10-07 15:54:11 +02005297 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005298 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005299 /* the server did not change, only the date was updated */
5300 txn->flags |= TX_SCK_UPDATED;
5301 else
5302 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005303
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005304 /* Here, we will tell an eventual cache on the client side that we don't
5305 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5306 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5307 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5308 */
5309 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005310
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005311 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5312
5313 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005314 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005315 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005316 }
5317 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005318
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005319 /*
5320 * 7: check if result will be cacheable with a cookie.
5321 * We'll block the response if security checks have caught
5322 * nasty things such as a cacheable cookie.
5323 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005324 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5325 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005326 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005327
5328 /* we're in presence of a cacheable response containing
5329 * a set-cookie header. We'll block it as requested by
5330 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005331 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005332 if (target_srv(&t->target))
5333 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005334
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005335 t->be->be_counters.denied_resp++;
5336 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005337 if (t->listener->counters)
5338 t->listener->counters->denied_resp++;
5339
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005340 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005341 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005342 send_log(t->be, LOG_ALERT,
5343 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005344 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005345 goto return_srv_prx_502;
5346 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005347
5348 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005349 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005350 */
Willy Tarreau60466522010-01-18 19:08:45 +01005351 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5352 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5353 unsigned int want_flags = 0;
5354
5355 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5356 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5357 /* we want a keep-alive response here. Keep-alive header
5358 * required if either side is not 1.1.
5359 */
5360 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5361 want_flags |= TX_CON_KAL_SET;
5362 }
5363 else {
5364 /* we want a close response here. Close header required if
5365 * the server is 1.1, regardless of the client.
5366 */
5367 if (txn->flags & TX_RES_VER_11)
5368 want_flags |= TX_CON_CLO_SET;
5369 }
5370
5371 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5372 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005373 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005374
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005375 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005376 if ((txn->flags & TX_RES_XFER_LEN) ||
5377 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005378 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005379
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005380 /*************************************************************
5381 * OK, that's finished for the headers. We have done what we *
5382 * could. Let's switch to the DATA state. *
5383 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005384
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005385 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005386
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005387 /* if the user wants to log as soon as possible, without counting
5388 * bytes from the server, then this is the right moment. We have
5389 * to temporarily assign bytes_out to log what we currently have.
5390 */
5391 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5392 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5393 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005394 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005395 t->logs.bytes_out = 0;
5396 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005397
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005398 /* Note: we must not try to cheat by jumping directly to DATA,
5399 * otherwise we would not let the client side wake up.
5400 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005401
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005402 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005403 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005404 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005405}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005406
Willy Tarreaud98cf932009-12-27 22:54:55 +01005407/* This function is an analyser which forwards response body (including chunk
5408 * sizes if any). It is called as soon as we must forward, even if we forward
5409 * zero byte. The only situation where it must not be called is when we're in
5410 * tunnel mode and we want to forward till the close. It's used both to forward
5411 * remaining data and to resync after end of body. It expects the msg_state to
5412 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5413 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005414 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005415 * bytes of pending data + the headers if not already done (between som and sov).
5416 * It eventually adjusts som to match sov after the data in between have been sent.
5417 */
5418int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5419{
5420 struct http_txn *txn = &s->txn;
5421 struct http_msg *msg = &s->txn.rsp;
5422
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005423 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5424 return 0;
5425
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005426 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005427 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005428 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005429 /* Output closed while we were sending data. We must abort and
5430 * wake the other side up.
5431 */
5432 msg->msg_state = HTTP_MSG_ERROR;
5433 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005434 return 1;
5435 }
5436
Willy Tarreau4fe41902010-06-07 22:27:41 +02005437 /* in most states, we should abort in case of early close */
5438 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005439
Willy Tarreaud98cf932009-12-27 22:54:55 +01005440 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5441 /* we have msg->col and msg->sov which both point to the first
5442 * byte of message body. msg->som still points to the beginning
5443 * of the message. We must save the body in req->lr because it
5444 * survives buffer re-alignments.
5445 */
5446 res->lr = res->data + msg->sov;
5447 if (txn->flags & TX_RES_TE_CHNK)
5448 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5449 else {
5450 msg->msg_state = HTTP_MSG_DATA;
5451 }
5452 }
5453
Willy Tarreaud98cf932009-12-27 22:54:55 +01005454 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005455 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005456 /* we may have some data pending */
Willy Tarreau124d9912011-03-01 20:30:48 +01005457 if (msg->chunk_len || msg->som != msg->sov) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005458 int bytes = msg->sov - msg->som;
5459 if (bytes < 0) /* sov may have wrapped at the end */
5460 bytes += res->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01005461 buffer_forward(res, bytes + msg->chunk_len);
5462 msg->chunk_len = 0; /* don't forward that again */
Willy Tarreau638cd022010-01-03 07:42:04 +01005463 msg->som = msg->sov;
5464 }
5465
Willy Tarreaucaabe412010-01-03 23:08:28 +01005466 if (msg->msg_state == HTTP_MSG_DATA) {
5467 /* must still forward */
5468 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005469 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005470
5471 /* nothing left to forward */
5472 if (txn->flags & TX_RES_TE_CHNK)
5473 msg->msg_state = HTTP_MSG_DATA_CRLF;
5474 else
5475 msg->msg_state = HTTP_MSG_DONE;
5476 }
5477 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005478 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005479 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5480 */
5481 int ret = http_parse_chunk_size(res, msg);
5482
5483 if (!ret)
5484 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005485 else if (ret < 0) {
5486 if (msg->err_pos >= 0)
5487 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005488 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005489 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005490 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreau57f5c122010-12-02 00:37:14 +01005491 /* Don't set a PUSH at the end of that chunk if it's not the last one */
5492 if (msg->msg_state == HTTP_MSG_DATA)
5493 res->flags |= BF_EXPECT_MORE;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005494 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005495 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5496 /* we want the CRLF after the data */
5497 int ret;
5498
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005499 res->lr = res->w + res->send_max;
5500 if (res->lr >= res->data + res->size)
5501 res->lr -= res->size;
5502
Willy Tarreaud98cf932009-12-27 22:54:55 +01005503 ret = http_skip_chunk_crlf(res, msg);
5504
5505 if (!ret)
5506 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005507 else if (ret < 0) {
5508 if (msg->err_pos >= 0)
5509 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005510 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005511 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005512 /* we're in MSG_CHUNK_SIZE now */
5513 }
5514 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5515 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005516
Willy Tarreaud98cf932009-12-27 22:54:55 +01005517 if (ret == 0)
5518 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005519 else if (ret < 0) {
5520 if (msg->err_pos >= 0)
5521 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005522 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005523 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005524 /* we're in HTTP_MSG_DONE now */
5525 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005526 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005527 int old_state = msg->msg_state;
5528
Willy Tarreau610ecce2010-01-04 21:15:02 +01005529 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005530 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005531 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5532 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5533 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005534 if (http_resync_states(s)) {
5535 http_silent_debug(__LINE__, s);
5536 /* some state changes occurred, maybe the analyser
5537 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005538 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005539 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5540 if (res->flags & BF_SHUTW) {
5541 /* response errors are most likely due to
5542 * the client aborting the transfer.
5543 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005544 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005545 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005546 if (msg->err_pos >= 0)
5547 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005548 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005549 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005550 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005551 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005552 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005553 }
5554 }
5555
Willy Tarreaud98cf932009-12-27 22:54:55 +01005556 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005557 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005558 if (res->flags & BF_SHUTR) {
5559 if (!(s->flags & SN_ERR_MASK))
5560 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005561 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005562 if (target_srv(&s->target))
5563 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005564 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005565 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005566
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005567 if (res->flags & BF_SHUTW)
5568 goto aborted_xfer;
5569
Willy Tarreau40dba092010-03-04 18:14:51 +01005570 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005571 if (!s->req->analysers)
5572 goto return_bad_res;
5573
Willy Tarreaud98cf932009-12-27 22:54:55 +01005574 /* forward the chunk size as well as any pending data */
Willy Tarreau124d9912011-03-01 20:30:48 +01005575 if (msg->chunk_len || msg->som != msg->sov) {
Willy Tarreauacd20f82011-03-01 20:04:36 +01005576 int bytes = msg->sov - msg->som;
5577 if (bytes < 0) /* sov may have wrapped at the end */
5578 bytes += res->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01005579 buffer_forward(res, msg->sov - msg->som + msg->chunk_len);
5580 msg->chunk_len = 0; /* don't forward that again */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005581 msg->som = msg->sov;
5582 }
5583
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005584 /* When TE: chunked is used, we need to get there again to parse remaining
5585 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5586 * Similarly, with keep-alive on the client side, we don't want to forward a
5587 * close.
5588 */
5589 if ((txn->flags & TX_RES_TE_CHNK) ||
5590 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5591 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5592 buffer_dont_close(res);
5593
Willy Tarreaud98cf932009-12-27 22:54:55 +01005594 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005595 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005596 return 0;
5597
Willy Tarreau40dba092010-03-04 18:14:51 +01005598 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005599 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005600 if (target_srv(&s->target))
5601 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005602
5603 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005604 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005605 /* don't send any error message as we're in the body */
5606 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005607 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005608 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005609 if (target_srv(&s->target))
5610 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005611
5612 if (!(s->flags & SN_ERR_MASK))
5613 s->flags |= SN_ERR_PRXCOND;
5614 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005615 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005616 return 0;
5617
5618 aborted_xfer:
5619 txn->rsp.msg_state = HTTP_MSG_ERROR;
5620 /* don't send any error message as we're in the body */
5621 stream_int_retnclose(res->cons, NULL);
5622 res->analysers = 0;
5623 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5624
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005625 s->fe->fe_counters.cli_aborts++;
5626 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005627 if (target_srv(&s->target))
5628 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005629
5630 if (!(s->flags & SN_ERR_MASK))
5631 s->flags |= SN_ERR_CLICL;
5632 if (!(s->flags & SN_FINST_MASK))
5633 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005634 return 0;
5635}
5636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005637/* Iterate the same filter through all request headers.
5638 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005639 * Since it can manage the switch to another backend, it updates the per-proxy
5640 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005641 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005642int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005643{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005644 char term;
5645 char *cur_ptr, *cur_end, *cur_next;
5646 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005647 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005648 struct hdr_idx_elem *cur_hdr;
5649 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005650
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005651 last_hdr = 0;
5652
Willy Tarreau962c3f42010-01-10 00:15:35 +01005653 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005654 old_idx = 0;
5655
5656 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005657 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005658 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005659 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005660 (exp->action == ACT_ALLOW ||
5661 exp->action == ACT_DENY ||
5662 exp->action == ACT_TARPIT))
5663 return 0;
5664
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005665 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005666 if (!cur_idx)
5667 break;
5668
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005669 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005670 cur_ptr = cur_next;
5671 cur_end = cur_ptr + cur_hdr->len;
5672 cur_next = cur_end + cur_hdr->cr + 1;
5673
5674 /* Now we have one header between cur_ptr and cur_end,
5675 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005676 */
5677
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005678 /* The annoying part is that pattern matching needs
5679 * that we modify the contents to null-terminate all
5680 * strings before testing them.
5681 */
5682
5683 term = *cur_end;
5684 *cur_end = '\0';
5685
5686 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5687 switch (exp->action) {
5688 case ACT_SETBE:
5689 /* It is not possible to jump a second time.
5690 * FIXME: should we return an HTTP/500 here so that
5691 * the admin knows there's a problem ?
5692 */
5693 if (t->be != t->fe)
5694 break;
5695
5696 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005697 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005698 last_hdr = 1;
5699 break;
5700
5701 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005702 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005703 last_hdr = 1;
5704 break;
5705
5706 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005707 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005708 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005709
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005710 t->fe->fe_counters.denied_req++;
5711 if (t->fe != t->be)
5712 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005713 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005714 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005715
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005716 break;
5717
5718 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005719 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005720 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005721
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005722 t->fe->fe_counters.denied_req++;
5723 if (t->fe != t->be)
5724 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005725 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005726 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005727
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005728 break;
5729
5730 case ACT_REPLACE:
5731 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5732 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5733 /* FIXME: if the user adds a newline in the replacement, the
5734 * index will not be recalculated for now, and the new line
5735 * will not be counted as a new header.
5736 */
5737
5738 cur_end += delta;
5739 cur_next += delta;
5740 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005741 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005742 break;
5743
5744 case ACT_REMOVE:
5745 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5746 cur_next += delta;
5747
Willy Tarreaufa355d42009-11-29 18:12:29 +01005748 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005749 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5750 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005751 cur_hdr->len = 0;
5752 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005753 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005754 break;
5755
5756 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005757 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005758 if (cur_end)
5759 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005760
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005761 /* keep the link from this header to next one in case of later
5762 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005763 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005764 old_idx = cur_idx;
5765 }
5766 return 0;
5767}
5768
5769
5770/* Apply the filter to the request line.
5771 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5772 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005773 * Since it can manage the switch to another backend, it updates the per-proxy
5774 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005775 */
5776int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5777{
5778 char term;
5779 char *cur_ptr, *cur_end;
5780 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005781 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005782 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005783
Willy Tarreau58f10d72006-12-04 02:26:12 +01005784
Willy Tarreau3d300592007-03-18 18:34:41 +01005785 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005786 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005787 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005788 (exp->action == ACT_ALLOW ||
5789 exp->action == ACT_DENY ||
5790 exp->action == ACT_TARPIT))
5791 return 0;
5792 else if (exp->action == ACT_REMOVE)
5793 return 0;
5794
5795 done = 0;
5796
Willy Tarreau962c3f42010-01-10 00:15:35 +01005797 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005798 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005799
5800 /* Now we have the request line between cur_ptr and cur_end */
5801
5802 /* The annoying part is that pattern matching needs
5803 * that we modify the contents to null-terminate all
5804 * strings before testing them.
5805 */
5806
5807 term = *cur_end;
5808 *cur_end = '\0';
5809
5810 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5811 switch (exp->action) {
5812 case ACT_SETBE:
5813 /* It is not possible to jump a second time.
5814 * FIXME: should we return an HTTP/500 here so that
5815 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005816 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005817 if (t->be != t->fe)
5818 break;
5819
5820 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005821 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005822 done = 1;
5823 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005824
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005825 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005826 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005827 done = 1;
5828 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005829
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005830 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005831 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005832
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005833 t->fe->fe_counters.denied_req++;
5834 if (t->fe != t->be)
5835 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005836 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005837 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005838
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005839 done = 1;
5840 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005841
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005842 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005843 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005844
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005845 t->fe->fe_counters.denied_req++;
5846 if (t->fe != t->be)
5847 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005848 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005849 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005850
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005851 done = 1;
5852 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005853
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005854 case ACT_REPLACE:
5855 *cur_end = term; /* restore the string terminator */
5856 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5857 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5858 /* FIXME: if the user adds a newline in the replacement, the
5859 * index will not be recalculated for now, and the new line
5860 * will not be counted as a new header.
5861 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005862
Willy Tarreaufa355d42009-11-29 18:12:29 +01005863 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005864 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005865 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005866 HTTP_MSG_RQMETH,
5867 cur_ptr, cur_end + 1,
5868 NULL, NULL);
5869 if (unlikely(!cur_end))
5870 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005871
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005872 /* we have a full request and we know that we have either a CR
5873 * or an LF at <ptr>.
5874 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005875 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5876 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005877 /* there is no point trying this regex on headers */
5878 return 1;
5879 }
5880 }
5881 *cur_end = term; /* restore the string terminator */
5882 return done;
5883}
Willy Tarreau97de6242006-12-27 17:18:38 +01005884
Willy Tarreau58f10d72006-12-04 02:26:12 +01005885
Willy Tarreau58f10d72006-12-04 02:26:12 +01005886
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005887/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005888 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005889 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005890 * unparsable request. Since it can manage the switch to another backend, it
5891 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005892 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005893int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005894{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005895 struct http_txn *txn = &s->txn;
5896 struct hdr_exp *exp;
5897
5898 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005899 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005900
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005901 /*
5902 * The interleaving of transformations and verdicts
5903 * makes it difficult to decide to continue or stop
5904 * the evaluation.
5905 */
5906
Willy Tarreau6c123b12010-01-28 20:22:06 +01005907 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5908 break;
5909
Willy Tarreau3d300592007-03-18 18:34:41 +01005910 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005911 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005912 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005913 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005914
5915 /* if this filter had a condition, evaluate it now and skip to
5916 * next filter if the condition does not match.
5917 */
5918 if (exp->cond) {
5919 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5920 ret = acl_pass(ret);
5921 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5922 ret = !ret;
5923
5924 if (!ret)
5925 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005926 }
5927
5928 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005929 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005930 if (unlikely(ret < 0))
5931 return -1;
5932
5933 if (likely(ret == 0)) {
5934 /* The filter did not match the request, it can be
5935 * iterated through all headers.
5936 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005937 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005938 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005939 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005940 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005941}
5942
5943
Willy Tarreaua15645d2007-03-18 16:22:39 +01005944
Willy Tarreau58f10d72006-12-04 02:26:12 +01005945/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005946 * Try to retrieve the server associated to the appsession.
5947 * If the server is found, it's assigned to the session.
5948 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005949void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005950 struct http_txn *txn = &t->txn;
5951 appsess *asession = NULL;
5952 char *sessid_temp = NULL;
5953
Cyril Bontéb21570a2009-11-29 20:04:48 +01005954 if (len > t->be->appsession_len) {
5955 len = t->be->appsession_len;
5956 }
5957
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005958 if (t->be->options2 & PR_O2_AS_REQL) {
5959 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005960 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005961 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005962 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005963 }
5964
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005965 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005966 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5967 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5968 return;
5969 }
5970
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005971 memcpy(txn->sessid, buf, len);
5972 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005973 }
5974
5975 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5976 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5977 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5978 return;
5979 }
5980
Cyril Bontéb21570a2009-11-29 20:04:48 +01005981 memcpy(sessid_temp, buf, len);
5982 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005983
5984 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5985 /* free previously allocated memory */
5986 pool_free2(apools.sessid, sessid_temp);
5987
5988 if (asession != NULL) {
5989 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5990 if (!(t->be->options2 & PR_O2_AS_REQL))
5991 asession->request_count++;
5992
5993 if (asession->serverid != NULL) {
5994 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005995
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005996 while (srv) {
5997 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005998 if ((srv->state & SRV_RUNNING) ||
5999 (t->be->options & PR_O_PERSIST) ||
6000 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006001 /* we found the server and it's usable */
6002 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006003 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006004 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006005 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01006006
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006007 break;
6008 } else {
6009 txn->flags &= ~TX_CK_MASK;
6010 txn->flags |= TX_CK_DOWN;
6011 }
6012 }
6013 srv = srv->next;
6014 }
6015 }
6016 }
6017}
6018
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006019/* Find the end of a cookie value contained between <s> and <e>. It works the
6020 * same way as with headers above except that the semi-colon also ends a token.
6021 * See RFC2965 for more information. Note that it requires a valid header to
6022 * return a valid result.
6023 */
6024char *find_cookie_value_end(char *s, const char *e)
6025{
6026 int quoted, qdpair;
6027
6028 quoted = qdpair = 0;
6029 for (; s < e; s++) {
6030 if (qdpair) qdpair = 0;
6031 else if (quoted) {
6032 if (*s == '\\') qdpair = 1;
6033 else if (*s == '"') quoted = 0;
6034 }
6035 else if (*s == '"') quoted = 1;
6036 else if (*s == ',' || *s == ';') return s;
6037 }
6038 return s;
6039}
6040
6041/* Delete a value in a header between delimiters <from> and <next> in buffer
6042 * <buf>. The number of characters displaced is returned, and the pointer to
6043 * the first delimiter is updated if required. The function tries as much as
6044 * possible to respect the following principles :
6045 * - replace <from> delimiter by the <next> one unless <from> points to a
6046 * colon, in which case <next> is simply removed
6047 * - set exactly one space character after the new first delimiter, unless
6048 * there are not enough characters in the block being moved to do so.
6049 * - remove unneeded spaces before the previous delimiter and after the new
6050 * one.
6051 *
6052 * It is the caller's responsibility to ensure that :
6053 * - <from> points to a valid delimiter or the colon ;
6054 * - <next> points to a valid delimiter or the final CR/LF ;
6055 * - there are non-space chars before <from> ;
6056 * - there is a CR/LF at or after <next>.
6057 */
6058int del_hdr_value(struct buffer *buf, char **from, char *next)
6059{
6060 char *prev = *from;
6061
6062 if (*prev == ':') {
6063 /* We're removing the first value, preserve the colon and add a
6064 * space if possible.
6065 */
6066 if (!http_is_crlf[(unsigned char)*next])
6067 next++;
6068 prev++;
6069 if (prev < next)
6070 *prev++ = ' ';
6071
6072 while (http_is_spht[(unsigned char)*next])
6073 next++;
6074 } else {
6075 /* Remove useless spaces before the old delimiter. */
6076 while (http_is_spht[(unsigned char)*(prev-1)])
6077 prev--;
6078 *from = prev;
6079
6080 /* copy the delimiter and if possible a space if we're
6081 * not at the end of the line.
6082 */
6083 if (!http_is_crlf[(unsigned char)*next]) {
6084 *prev++ = *next++;
6085 if (prev + 1 < next)
6086 *prev++ = ' ';
6087 while (http_is_spht[(unsigned char)*next])
6088 next++;
6089 }
6090 }
6091 return buffer_replace2(buf, prev, next, NULL, 0);
6092}
6093
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006094/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006095 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006096 * desirable to call it only when needed. This code is quite complex because
6097 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6098 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006099 */
6100void manage_client_side_cookies(struct session *t, struct buffer *req)
6101{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006102 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006103 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006104 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006105 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6106 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006107
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006108 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006109 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006110 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006111
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006112 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006113 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006114 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006115
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006116 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006117 hdr_beg = hdr_next;
6118 hdr_end = hdr_beg + cur_hdr->len;
6119 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006120
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006121 /* We have one full header between hdr_beg and hdr_end, and the
6122 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006123 * "Cookie:" headers.
6124 */
6125
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006126 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006127 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006128 old_idx = cur_idx;
6129 continue;
6130 }
6131
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006132 del_from = NULL; /* nothing to be deleted */
6133 preserve_hdr = 0; /* assume we may kill the whole header */
6134
Willy Tarreau58f10d72006-12-04 02:26:12 +01006135 /* Now look for cookies. Conforming to RFC2109, we have to support
6136 * attributes whose name begin with a '$', and associate them with
6137 * the right cookie, if we want to delete this cookie.
6138 * So there are 3 cases for each cookie read :
6139 * 1) it's a special attribute, beginning with a '$' : ignore it.
6140 * 2) it's a server id cookie that we *MAY* want to delete : save
6141 * some pointers on it (last semi-colon, beginning of cookie...)
6142 * 3) it's an application cookie : we *MAY* have to delete a previous
6143 * "special" cookie.
6144 * At the end of loop, if a "special" cookie remains, we may have to
6145 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006146 * *MUST* delete it.
6147 *
6148 * Note: RFC2965 is unclear about the processing of spaces around
6149 * the equal sign in the ATTR=VALUE form. A careful inspection of
6150 * the RFC explicitly allows spaces before it, and not within the
6151 * tokens (attrs or values). An inspection of RFC2109 allows that
6152 * too but section 10.1.3 lets one think that spaces may be allowed
6153 * after the equal sign too, resulting in some (rare) buggy
6154 * implementations trying to do that. So let's do what servers do.
6155 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6156 * allowed quoted strings in values, with any possible character
6157 * after a backslash, including control chars and delimitors, which
6158 * causes parsing to become ambiguous. Browsers also allow spaces
6159 * within values even without quotes.
6160 *
6161 * We have to keep multiple pointers in order to support cookie
6162 * removal at the beginning, middle or end of header without
6163 * corrupting the header. All of these headers are valid :
6164 *
6165 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6166 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6167 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6168 * | | | | | | | | |
6169 * | | | | | | | | hdr_end <--+
6170 * | | | | | | | +--> next
6171 * | | | | | | +----> val_end
6172 * | | | | | +-----------> val_beg
6173 * | | | | +--------------> equal
6174 * | | | +----------------> att_end
6175 * | | +---------------------> att_beg
6176 * | +--------------------------> prev
6177 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006178 */
6179
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006180 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6181 /* Iterate through all cookies on this line */
6182
6183 /* find att_beg */
6184 att_beg = prev + 1;
6185 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6186 att_beg++;
6187
6188 /* find att_end : this is the first character after the last non
6189 * space before the equal. It may be equal to hdr_end.
6190 */
6191 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006192
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006193 while (equal < hdr_end) {
6194 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006195 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006196 if (http_is_spht[(unsigned char)*equal++])
6197 continue;
6198 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006199 }
6200
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006201 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6202 * is between <att_beg> and <equal>, both may be identical.
6203 */
6204
6205 /* look for end of cookie if there is an equal sign */
6206 if (equal < hdr_end && *equal == '=') {
6207 /* look for the beginning of the value */
6208 val_beg = equal + 1;
6209 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6210 val_beg++;
6211
6212 /* find the end of the value, respecting quotes */
6213 next = find_cookie_value_end(val_beg, hdr_end);
6214
6215 /* make val_end point to the first white space or delimitor after the value */
6216 val_end = next;
6217 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6218 val_end--;
6219 } else {
6220 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006221 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006222
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006223 /* We have nothing to do with attributes beginning with '$'. However,
6224 * they will automatically be removed if a header before them is removed,
6225 * since they're supposed to be linked together.
6226 */
6227 if (*att_beg == '$')
6228 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006229
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006230 /* Ignore cookies with no equal sign */
6231 if (equal == next) {
6232 /* This is not our cookie, so we must preserve it. But if we already
6233 * scheduled another cookie for removal, we cannot remove the
6234 * complete header, but we can remove the previous block itself.
6235 */
6236 preserve_hdr = 1;
6237 if (del_from != NULL) {
6238 int delta = del_hdr_value(req, &del_from, prev);
6239 val_end += delta;
6240 next += delta;
6241 hdr_end += delta;
6242 hdr_next += delta;
6243 cur_hdr->len += delta;
6244 http_msg_move_end(&txn->req, delta);
6245 prev = del_from;
6246 del_from = NULL;
6247 }
6248 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006249 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006250
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006251 /* if there are spaces around the equal sign, we need to
6252 * strip them otherwise we'll get trouble for cookie captures,
6253 * or even for rewrites. Since this happens extremely rarely,
6254 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006255 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6257 int stripped_before = 0;
6258 int stripped_after = 0;
6259
6260 if (att_end != equal) {
6261 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6262 equal += stripped_before;
6263 val_beg += stripped_before;
6264 }
6265
6266 if (val_beg > equal + 1) {
6267 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6268 val_beg += stripped_after;
6269 stripped_before += stripped_after;
6270 }
6271
6272 val_end += stripped_before;
6273 next += stripped_before;
6274 hdr_end += stripped_before;
6275 hdr_next += stripped_before;
6276 cur_hdr->len += stripped_before;
6277 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006278 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006279 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006280
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006281 /* First, let's see if we want to capture this cookie. We check
6282 * that we don't already have a client side cookie, because we
6283 * can only capture one. Also as an optimisation, we ignore
6284 * cookies shorter than the declared name.
6285 */
6286 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6287 (val_end - att_beg >= t->fe->capture_namelen) &&
6288 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6289 int log_len = val_end - att_beg;
6290
6291 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6292 Alert("HTTP logging : out of memory.\n");
6293 } else {
6294 if (log_len > t->fe->capture_len)
6295 log_len = t->fe->capture_len;
6296 memcpy(txn->cli_cookie, att_beg, log_len);
6297 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006298 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006299 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006300
Willy Tarreaubca99692010-10-06 19:25:55 +02006301 /* Persistence cookies in passive, rewrite or insert mode have the
6302 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006303 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006304 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006305 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006306 * For cookies in prefix mode, the form is :
6307 *
6308 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006309 */
6310 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6311 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6312 struct server *srv = t->be->srv;
6313 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006314
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006315 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6316 * have the server ID between val_beg and delim, and the original cookie between
6317 * delim+1 and val_end. Otherwise, delim==val_end :
6318 *
6319 * Cookie: NAME=SRV; # in all but prefix modes
6320 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6321 * | || || | |+-> next
6322 * | || || | +--> val_end
6323 * | || || +---------> delim
6324 * | || |+------------> val_beg
6325 * | || +-------------> att_end = equal
6326 * | |+-----------------> att_beg
6327 * | +------------------> prev
6328 * +-------------------------> hdr_beg
6329 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006330
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006331 if (t->be->options & PR_O_COOK_PFX) {
6332 for (delim = val_beg; delim < val_end; delim++)
6333 if (*delim == COOKIE_DELIM)
6334 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006335 } else {
6336 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006337 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006338 /* Now check if the cookie contains a date field, which would
6339 * appear after a vertical bar ('|') just after the server name
6340 * and before the delimiter.
6341 */
6342 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6343 if (vbar1) {
6344 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006345 * right is the last seen date. It is a base64 encoded
6346 * 30-bit value representing the UNIX date since the
6347 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006348 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006349 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006350 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006351 if (val_end - vbar1 >= 5) {
6352 val = b64tos30(vbar1);
6353 if (val > 0)
6354 txn->cookie_last_date = val << 2;
6355 }
6356 /* look for a second vertical bar */
6357 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6358 if (vbar1 && (val_end - vbar1 > 5)) {
6359 val = b64tos30(vbar1 + 1);
6360 if (val > 0)
6361 txn->cookie_first_date = val << 2;
6362 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006363 }
6364 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006365
Willy Tarreauf64d1412010-10-07 20:06:11 +02006366 /* if the cookie has an expiration date and the proxy wants to check
6367 * it, then we do that now. We first check if the cookie is too old,
6368 * then only if it has expired. We detect strict overflow because the
6369 * time resolution here is not great (4 seconds). Cookies with dates
6370 * in the future are ignored if their offset is beyond one day. This
6371 * allows an admin to fix timezone issues without expiring everyone
6372 * and at the same time avoids keeping unwanted side effects for too
6373 * long.
6374 */
6375 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006376 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6377 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006378 txn->flags &= ~TX_CK_MASK;
6379 txn->flags |= TX_CK_OLD;
6380 delim = val_beg; // let's pretend we have not found the cookie
6381 txn->cookie_first_date = 0;
6382 txn->cookie_last_date = 0;
6383 }
6384 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006385 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6386 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006387 txn->flags &= ~TX_CK_MASK;
6388 txn->flags |= TX_CK_EXPIRED;
6389 delim = val_beg; // let's pretend we have not found the cookie
6390 txn->cookie_first_date = 0;
6391 txn->cookie_last_date = 0;
6392 }
6393
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006394 /* Here, we'll look for the first running server which supports the cookie.
6395 * This allows to share a same cookie between several servers, for example
6396 * to dedicate backup servers to specific servers only.
6397 * However, to prevent clients from sticking to cookie-less backup server
6398 * when they have incidentely learned an empty cookie, we simply ignore
6399 * empty cookies and mark them as invalid.
6400 * The same behaviour is applied when persistence must be ignored.
6401 */
6402 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6403 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006404
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006405 while (srv) {
6406 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6407 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6408 if ((srv->state & SRV_RUNNING) ||
6409 (t->be->options & PR_O_PERSIST) ||
6410 (t->flags & SN_FORCE_PRST)) {
6411 /* we found the server and we can use it */
6412 txn->flags &= ~TX_CK_MASK;
6413 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6414 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006415 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006416 break;
6417 } else {
6418 /* we found a server, but it's down,
6419 * mark it as such and go on in case
6420 * another one is available.
6421 */
6422 txn->flags &= ~TX_CK_MASK;
6423 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006424 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006425 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006426 srv = srv->next;
6427 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006428
Willy Tarreauf64d1412010-10-07 20:06:11 +02006429 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006430 /* no server matched this cookie */
6431 txn->flags &= ~TX_CK_MASK;
6432 txn->flags |= TX_CK_INVALID;
6433 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006434
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006435 /* depending on the cookie mode, we may have to either :
6436 * - delete the complete cookie if we're in insert+indirect mode, so that
6437 * the server never sees it ;
6438 * - remove the server id from the cookie value, and tag the cookie as an
6439 * application cookie so that it does not get accidentely removed later,
6440 * if we're in cookie prefix mode
6441 */
6442 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6443 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006444
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006445 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6446 val_end += delta;
6447 next += delta;
6448 hdr_end += delta;
6449 hdr_next += delta;
6450 cur_hdr->len += delta;
6451 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006452
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006453 del_from = NULL;
6454 preserve_hdr = 1; /* we want to keep this cookie */
6455 }
6456 else if (del_from == NULL &&
6457 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6458 del_from = prev;
6459 }
6460 } else {
6461 /* This is not our cookie, so we must preserve it. But if we already
6462 * scheduled another cookie for removal, we cannot remove the
6463 * complete header, but we can remove the previous block itself.
6464 */
6465 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006466
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006467 if (del_from != NULL) {
6468 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006469 if (att_beg >= del_from)
6470 att_beg += delta;
6471 if (att_end >= del_from)
6472 att_end += delta;
6473 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006474 val_end += delta;
6475 next += delta;
6476 hdr_end += delta;
6477 hdr_next += delta;
6478 cur_hdr->len += delta;
6479 http_msg_move_end(&txn->req, delta);
6480 prev = del_from;
6481 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006482 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006483 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006484
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006485 /* Look for the appsession cookie unless persistence must be ignored */
6486 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6487 int cmp_len, value_len;
6488 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006489
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006490 if (t->be->options2 & PR_O2_AS_PFX) {
6491 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6492 value_begin = att_beg + t->be->appsession_name_len;
6493 value_len = val_end - att_beg - t->be->appsession_name_len;
6494 } else {
6495 cmp_len = att_end - att_beg;
6496 value_begin = val_beg;
6497 value_len = val_end - val_beg;
6498 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006499
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006500 /* let's see if the cookie is our appcookie */
6501 if (cmp_len == t->be->appsession_name_len &&
6502 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6503 manage_client_side_appsession(t, value_begin, value_len);
6504 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006505 }
6506
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006507 /* continue with next cookie on this header line */
6508 att_beg = next;
6509 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006510
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006511 /* There are no more cookies on this line.
6512 * We may still have one (or several) marked for deletion at the
6513 * end of the line. We must do this now in two ways :
6514 * - if some cookies must be preserved, we only delete from the
6515 * mark to the end of line ;
6516 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006517 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006518 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006519 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006520 if (preserve_hdr) {
6521 delta = del_hdr_value(req, &del_from, hdr_end);
6522 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006523 cur_hdr->len += delta;
6524 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006525 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006526
6527 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006528 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6529 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006530 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006531 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006532 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006533 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006534 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006535 }
6536
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006537 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006538 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006539 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006540}
6541
6542
Willy Tarreaua15645d2007-03-18 16:22:39 +01006543/* Iterate the same filter through all response headers contained in <rtr>.
6544 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6545 */
6546int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6547{
6548 char term;
6549 char *cur_ptr, *cur_end, *cur_next;
6550 int cur_idx, old_idx, last_hdr;
6551 struct http_txn *txn = &t->txn;
6552 struct hdr_idx_elem *cur_hdr;
6553 int len, delta;
6554
6555 last_hdr = 0;
6556
Willy Tarreau962c3f42010-01-10 00:15:35 +01006557 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006558 old_idx = 0;
6559
6560 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006561 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006562 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006563 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006564 (exp->action == ACT_ALLOW ||
6565 exp->action == ACT_DENY))
6566 return 0;
6567
6568 cur_idx = txn->hdr_idx.v[old_idx].next;
6569 if (!cur_idx)
6570 break;
6571
6572 cur_hdr = &txn->hdr_idx.v[cur_idx];
6573 cur_ptr = cur_next;
6574 cur_end = cur_ptr + cur_hdr->len;
6575 cur_next = cur_end + cur_hdr->cr + 1;
6576
6577 /* Now we have one header between cur_ptr and cur_end,
6578 * and the next header starts at cur_next.
6579 */
6580
6581 /* The annoying part is that pattern matching needs
6582 * that we modify the contents to null-terminate all
6583 * strings before testing them.
6584 */
6585
6586 term = *cur_end;
6587 *cur_end = '\0';
6588
6589 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6590 switch (exp->action) {
6591 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006592 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006593 last_hdr = 1;
6594 break;
6595
6596 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006597 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006598 last_hdr = 1;
6599 break;
6600
6601 case ACT_REPLACE:
6602 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6603 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6604 /* FIXME: if the user adds a newline in the replacement, the
6605 * index will not be recalculated for now, and the new line
6606 * will not be counted as a new header.
6607 */
6608
6609 cur_end += delta;
6610 cur_next += delta;
6611 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006612 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006613 break;
6614
6615 case ACT_REMOVE:
6616 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6617 cur_next += delta;
6618
Willy Tarreaufa355d42009-11-29 18:12:29 +01006619 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006620 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6621 txn->hdr_idx.used--;
6622 cur_hdr->len = 0;
6623 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006624 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006625 break;
6626
6627 }
6628 }
6629 if (cur_end)
6630 *cur_end = term; /* restore the string terminator */
6631
6632 /* keep the link from this header to next one in case of later
6633 * removal of next header.
6634 */
6635 old_idx = cur_idx;
6636 }
6637 return 0;
6638}
6639
6640
6641/* Apply the filter to the status line in the response buffer <rtr>.
6642 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6643 * or -1 if a replacement resulted in an invalid status line.
6644 */
6645int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6646{
6647 char term;
6648 char *cur_ptr, *cur_end;
6649 int done;
6650 struct http_txn *txn = &t->txn;
6651 int len, delta;
6652
6653
Willy Tarreau3d300592007-03-18 18:34:41 +01006654 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006655 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006656 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006657 (exp->action == ACT_ALLOW ||
6658 exp->action == ACT_DENY))
6659 return 0;
6660 else if (exp->action == ACT_REMOVE)
6661 return 0;
6662
6663 done = 0;
6664
Willy Tarreau962c3f42010-01-10 00:15:35 +01006665 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006666 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006667
6668 /* Now we have the status line between cur_ptr and cur_end */
6669
6670 /* The annoying part is that pattern matching needs
6671 * that we modify the contents to null-terminate all
6672 * strings before testing them.
6673 */
6674
6675 term = *cur_end;
6676 *cur_end = '\0';
6677
6678 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6679 switch (exp->action) {
6680 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006681 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006682 done = 1;
6683 break;
6684
6685 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006686 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006687 done = 1;
6688 break;
6689
6690 case ACT_REPLACE:
6691 *cur_end = term; /* restore the string terminator */
6692 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6693 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6694 /* FIXME: if the user adds a newline in the replacement, the
6695 * index will not be recalculated for now, and the new line
6696 * will not be counted as a new header.
6697 */
6698
Willy Tarreaufa355d42009-11-29 18:12:29 +01006699 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006700 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006701 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006702 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006703 cur_ptr, cur_end + 1,
6704 NULL, NULL);
6705 if (unlikely(!cur_end))
6706 return -1;
6707
6708 /* we have a full respnse and we know that we have either a CR
6709 * or an LF at <ptr>.
6710 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006711 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006712 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006713 /* there is no point trying this regex on headers */
6714 return 1;
6715 }
6716 }
6717 *cur_end = term; /* restore the string terminator */
6718 return done;
6719}
6720
6721
6722
6723/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006724 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006725 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6726 * unparsable response.
6727 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006728int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006729{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006730 struct http_txn *txn = &s->txn;
6731 struct hdr_exp *exp;
6732
6733 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006734 int ret;
6735
6736 /*
6737 * The interleaving of transformations and verdicts
6738 * makes it difficult to decide to continue or stop
6739 * the evaluation.
6740 */
6741
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006742 if (txn->flags & TX_SVDENY)
6743 break;
6744
Willy Tarreau3d300592007-03-18 18:34:41 +01006745 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006746 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6747 exp->action == ACT_PASS)) {
6748 exp = exp->next;
6749 continue;
6750 }
6751
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006752 /* if this filter had a condition, evaluate it now and skip to
6753 * next filter if the condition does not match.
6754 */
6755 if (exp->cond) {
6756 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6757 ret = acl_pass(ret);
6758 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6759 ret = !ret;
6760 if (!ret)
6761 continue;
6762 }
6763
Willy Tarreaua15645d2007-03-18 16:22:39 +01006764 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006765 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006766 if (unlikely(ret < 0))
6767 return -1;
6768
6769 if (likely(ret == 0)) {
6770 /* The filter did not match the response, it can be
6771 * iterated through all headers.
6772 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006773 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006774 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006775 }
6776 return 0;
6777}
6778
6779
Willy Tarreaua15645d2007-03-18 16:22:39 +01006780/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006781 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006782 * desirable to call it only when needed. This function is also used when we
6783 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006784 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006785void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006786{
6787 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006788 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006789 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006791 char *hdr_beg, *hdr_end, *hdr_next;
6792 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006793
Willy Tarreaua15645d2007-03-18 16:22:39 +01006794 /* Iterate through the headers.
6795 * we start with the start line.
6796 */
6797 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006798 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006799
6800 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6801 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006802 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803
6804 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006805 hdr_beg = hdr_next;
6806 hdr_end = hdr_beg + cur_hdr->len;
6807 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006808
Willy Tarreau24581ba2010-08-31 22:39:35 +02006809 /* We have one full header between hdr_beg and hdr_end, and the
6810 * next header starts at hdr_next. We're only interested in
6811 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006812 */
6813
Willy Tarreau24581ba2010-08-31 22:39:35 +02006814 is_cookie2 = 0;
6815 prev = hdr_beg + 10;
6816 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006817 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006818 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6819 if (!val) {
6820 old_idx = cur_idx;
6821 continue;
6822 }
6823 is_cookie2 = 1;
6824 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006825 }
6826
Willy Tarreau24581ba2010-08-31 22:39:35 +02006827 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6828 * <prev> points to the colon.
6829 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006830 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006831
Willy Tarreau24581ba2010-08-31 22:39:35 +02006832 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6833 * check-cache is enabled) and we are not interested in checking
6834 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006835 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006836 if (t->be->cookie_name == NULL &&
6837 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006838 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006839 return;
6840
Willy Tarreau24581ba2010-08-31 22:39:35 +02006841 /* OK so now we know we have to process this response cookie.
6842 * The format of the Set-Cookie header is slightly different
6843 * from the format of the Cookie header in that it does not
6844 * support the comma as a cookie delimiter (thus the header
6845 * cannot be folded) because the Expires attribute described in
6846 * the original Netscape's spec may contain an unquoted date
6847 * with a comma inside. We have to live with this because
6848 * many browsers don't support Max-Age and some browsers don't
6849 * support quoted strings. However the Set-Cookie2 header is
6850 * clean.
6851 *
6852 * We have to keep multiple pointers in order to support cookie
6853 * removal at the beginning, middle or end of header without
6854 * corrupting the header (in case of set-cookie2). A special
6855 * pointer, <scav> points to the beginning of the set-cookie-av
6856 * fields after the first semi-colon. The <next> pointer points
6857 * either to the end of line (set-cookie) or next unquoted comma
6858 * (set-cookie2). All of these headers are valid :
6859 *
6860 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6861 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6862 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6863 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6864 * | | | | | | | | | |
6865 * | | | | | | | | +-> next hdr_end <--+
6866 * | | | | | | | +------------> scav
6867 * | | | | | | +--------------> val_end
6868 * | | | | | +--------------------> val_beg
6869 * | | | | +----------------------> equal
6870 * | | | +------------------------> att_end
6871 * | | +----------------------------> att_beg
6872 * | +------------------------------> prev
6873 * +-----------------------------------------> hdr_beg
6874 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006875
Willy Tarreau24581ba2010-08-31 22:39:35 +02006876 for (; prev < hdr_end; prev = next) {
6877 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006878
Willy Tarreau24581ba2010-08-31 22:39:35 +02006879 /* find att_beg */
6880 att_beg = prev + 1;
6881 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6882 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006883
Willy Tarreau24581ba2010-08-31 22:39:35 +02006884 /* find att_end : this is the first character after the last non
6885 * space before the equal. It may be equal to hdr_end.
6886 */
6887 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006888
Willy Tarreau24581ba2010-08-31 22:39:35 +02006889 while (equal < hdr_end) {
6890 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6891 break;
6892 if (http_is_spht[(unsigned char)*equal++])
6893 continue;
6894 att_end = equal;
6895 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006896
Willy Tarreau24581ba2010-08-31 22:39:35 +02006897 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6898 * is between <att_beg> and <equal>, both may be identical.
6899 */
6900
6901 /* look for end of cookie if there is an equal sign */
6902 if (equal < hdr_end && *equal == '=') {
6903 /* look for the beginning of the value */
6904 val_beg = equal + 1;
6905 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6906 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006907
Willy Tarreau24581ba2010-08-31 22:39:35 +02006908 /* find the end of the value, respecting quotes */
6909 next = find_cookie_value_end(val_beg, hdr_end);
6910
6911 /* make val_end point to the first white space or delimitor after the value */
6912 val_end = next;
6913 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6914 val_end--;
6915 } else {
6916 /* <equal> points to next comma, semi-colon or EOL */
6917 val_beg = val_end = next = equal;
6918 }
6919
6920 if (next < hdr_end) {
6921 /* Set-Cookie2 supports multiple cookies, and <next> points to
6922 * a colon or semi-colon before the end. So skip all attr-value
6923 * pairs and look for the next comma. For Set-Cookie, since
6924 * commas are permitted in values, skip to the end.
6925 */
6926 if (is_cookie2)
6927 next = find_hdr_value_end(next, hdr_end);
6928 else
6929 next = hdr_end;
6930 }
6931
6932 /* Now everything is as on the diagram above */
6933
6934 /* Ignore cookies with no equal sign */
6935 if (equal == val_end)
6936 continue;
6937
6938 /* If there are spaces around the equal sign, we need to
6939 * strip them otherwise we'll get trouble for cookie captures,
6940 * or even for rewrites. Since this happens extremely rarely,
6941 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006942 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006943 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6944 int stripped_before = 0;
6945 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006946
Willy Tarreau24581ba2010-08-31 22:39:35 +02006947 if (att_end != equal) {
6948 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6949 equal += stripped_before;
6950 val_beg += stripped_before;
6951 }
6952
6953 if (val_beg > equal + 1) {
6954 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6955 val_beg += stripped_after;
6956 stripped_before += stripped_after;
6957 }
6958
6959 val_end += stripped_before;
6960 next += stripped_before;
6961 hdr_end += stripped_before;
6962 hdr_next += stripped_before;
6963 cur_hdr->len += stripped_before;
6964 http_msg_move_end(&txn->req, stripped_before);
6965 }
6966
6967 /* First, let's see if we want to capture this cookie. We check
6968 * that we don't already have a server side cookie, because we
6969 * can only capture one. Also as an optimisation, we ignore
6970 * cookies shorter than the declared name.
6971 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006972 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006973 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006974 (val_end - att_beg >= t->fe->capture_namelen) &&
6975 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6976 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006977 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006978 Alert("HTTP logging : out of memory.\n");
6979 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006980 else {
6981 if (log_len > t->fe->capture_len)
6982 log_len = t->fe->capture_len;
6983 memcpy(txn->srv_cookie, att_beg, log_len);
6984 txn->srv_cookie[log_len] = 0;
6985 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006986 }
6987
Willy Tarreau827aee92011-03-10 16:55:02 +01006988 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006989 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006990 if (!(t->flags & SN_IGNORE_PRST) &&
6991 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6992 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006993 /* assume passive cookie by default */
6994 txn->flags &= ~TX_SCK_MASK;
6995 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006996
6997 /* If the cookie is in insert mode on a known server, we'll delete
6998 * this occurrence because we'll insert another one later.
6999 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007000 * a direct access.
7001 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007002 if (t->be->options2 & PR_O2_COOK_PSV) {
7003 /* The "preserve" flag was set, we don't want to touch the
7004 * server's cookie.
7005 */
7006 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007007 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007008 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007009 /* this cookie must be deleted */
7010 if (*prev == ':' && next == hdr_end) {
7011 /* whole header */
7012 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
7013 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7014 txn->hdr_idx.used--;
7015 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007016 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007017 hdr_next += delta;
7018 http_msg_move_end(&txn->rsp, delta);
7019 /* note: while both invalid now, <next> and <hdr_end>
7020 * are still equal, so the for() will stop as expected.
7021 */
7022 } else {
7023 /* just remove the value */
7024 int delta = del_hdr_value(res, &prev, next);
7025 next = prev;
7026 hdr_end += delta;
7027 hdr_next += delta;
7028 cur_hdr->len += delta;
7029 http_msg_move_end(&txn->rsp, delta);
7030 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007031 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007032 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007033 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007034 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007035 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007036 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007037 * with this server since we know it.
7038 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007039 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007040 next += delta;
7041 hdr_end += delta;
7042 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007043 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007044 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007045
Willy Tarreauf1348312010-10-07 15:54:11 +02007046 txn->flags &= ~TX_SCK_MASK;
7047 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007048 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007049 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007050 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007051 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007052 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007053 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007054 next += delta;
7055 hdr_end += delta;
7056 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007057 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007058 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007059
Willy Tarreau827aee92011-03-10 16:55:02 +01007060 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007061 txn->flags &= ~TX_SCK_MASK;
7062 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007063 }
7064 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007065 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7066 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007067 int cmp_len, value_len;
7068 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007069
Cyril Bontéb21570a2009-11-29 20:04:48 +01007070 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007071 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7072 value_begin = att_beg + t->be->appsession_name_len;
7073 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007074 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007075 cmp_len = att_end - att_beg;
7076 value_begin = val_beg;
7077 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007078 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007079
Cyril Bonté17530c32010-04-06 21:11:10 +02007080 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007081 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7082 /* free a possibly previously allocated memory */
7083 pool_free2(apools.sessid, txn->sessid);
7084
Cyril Bontéb21570a2009-11-29 20:04:48 +01007085 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007086 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007087 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7088 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7089 return;
7090 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007091 memcpy(txn->sessid, value_begin, value_len);
7092 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007093 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007094 }
7095 /* that's done for this cookie, check the next one on the same
7096 * line when next != hdr_end (only if is_cookie2).
7097 */
7098 }
7099 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007100 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007101 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007102
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007103 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007104 appsess *asession = NULL;
7105 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007106 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007107 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007108 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007109 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7110 Alert("Not enough Memory process_srv():asession:calloc().\n");
7111 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7112 return;
7113 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007114 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7115
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007116 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7117 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7118 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007119 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007120 return;
7121 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007122 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007123 asession->sessid[t->be->appsession_len] = 0;
7124
Willy Tarreau827aee92011-03-10 16:55:02 +01007125 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007126 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007127 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007128 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007129 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007130 return;
7131 }
7132 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01007133 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007134
7135 asession->request_count = 0;
7136 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7137 }
7138
7139 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7140 asession->request_count++;
7141 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007142}
7143
7144
Willy Tarreaua15645d2007-03-18 16:22:39 +01007145/*
7146 * Check if response is cacheable or not. Updates t->flags.
7147 */
7148void check_response_for_cacheability(struct session *t, struct buffer *rtr)
7149{
7150 struct http_txn *txn = &t->txn;
7151 char *p1, *p2;
7152
7153 char *cur_ptr, *cur_end, *cur_next;
7154 int cur_idx;
7155
Willy Tarreau5df51872007-11-25 16:20:08 +01007156 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007157 return;
7158
7159 /* Iterate through the headers.
7160 * we start with the start line.
7161 */
7162 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007163 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007164
7165 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7166 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007167 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007168
7169 cur_hdr = &txn->hdr_idx.v[cur_idx];
7170 cur_ptr = cur_next;
7171 cur_end = cur_ptr + cur_hdr->len;
7172 cur_next = cur_end + cur_hdr->cr + 1;
7173
7174 /* We have one full header between cur_ptr and cur_end, and the
7175 * next header starts at cur_next. We're only interested in
7176 * "Cookie:" headers.
7177 */
7178
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007179 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7180 if (val) {
7181 if ((cur_end - (cur_ptr + val) >= 8) &&
7182 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7183 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7184 return;
7185 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007186 }
7187
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007188 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7189 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007190 continue;
7191
7192 /* OK, right now we know we have a cache-control header at cur_ptr */
7193
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007194 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007195
7196 if (p1 >= cur_end) /* no more info */
7197 continue;
7198
7199 /* p1 is at the beginning of the value */
7200 p2 = p1;
7201
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007202 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203 p2++;
7204
7205 /* we have a complete value between p1 and p2 */
7206 if (p2 < cur_end && *p2 == '=') {
7207 /* we have something of the form no-cache="set-cookie" */
7208 if ((cur_end - p1 >= 21) &&
7209 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7210 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007211 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007212 continue;
7213 }
7214
7215 /* OK, so we know that either p2 points to the end of string or to a comma */
7216 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7217 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7218 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7219 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007220 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007221 return;
7222 }
7223
7224 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007225 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007226 continue;
7227 }
7228 }
7229}
7230
7231
Willy Tarreau58f10d72006-12-04 02:26:12 +01007232/*
7233 * Try to retrieve a known appsession in the URI, then the associated server.
7234 * If the server is found, it's assigned to the session.
7235 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007236void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007237{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007238 char *end_params, *first_param, *cur_param, *next_param;
7239 char separator;
7240 int value_len;
7241
7242 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007243
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007244 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007245 (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 +01007246 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007247 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007248
Cyril Bontéb21570a2009-11-29 20:04:48 +01007249 first_param = NULL;
7250 switch (mode) {
7251 case PR_O2_AS_M_PP:
7252 first_param = memchr(begin, ';', len);
7253 break;
7254 case PR_O2_AS_M_QS:
7255 first_param = memchr(begin, '?', len);
7256 break;
7257 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007258
Cyril Bontéb21570a2009-11-29 20:04:48 +01007259 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007260 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007261 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007262
Cyril Bontéb21570a2009-11-29 20:04:48 +01007263 switch (mode) {
7264 case PR_O2_AS_M_PP:
7265 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7266 end_params = (char *) begin + len;
7267 }
7268 separator = ';';
7269 break;
7270 case PR_O2_AS_M_QS:
7271 end_params = (char *) begin + len;
7272 separator = '&';
7273 break;
7274 default:
7275 /* unknown mode, shouldn't happen */
7276 return;
7277 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007278
Cyril Bontéb21570a2009-11-29 20:04:48 +01007279 cur_param = next_param = end_params;
7280 while (cur_param > first_param) {
7281 cur_param--;
7282 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7283 /* let's see if this is the appsession parameter */
7284 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7285 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7286 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7287 /* Cool... it's the right one */
7288 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7289 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7290 if (value_len > 0) {
7291 manage_client_side_appsession(t, cur_param, value_len);
7292 }
7293 break;
7294 }
7295 next_param = cur_param;
7296 }
7297 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007298#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007299 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007300 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007301#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007302}
7303
Willy Tarreaub2513902006-12-17 14:52:38 +01007304/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007305 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007306 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007307 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007308 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007309 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007310 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007311 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007312 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007313int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007314{
7315 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007316 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007317
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007318 if (!uri_auth)
7319 return 0;
7320
Cyril Bonté70be45d2010-10-12 00:14:35 +02007321 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007322 return 0;
7323
Willy Tarreau295a8372011-03-10 11:25:07 +01007324 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007326 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007327 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007328 return 0;
7329
Willy Tarreau962c3f42010-01-10 00:15:35 +01007330 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007331
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007332 /* the URI is in h */
7333 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007334 return 0;
7335
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007336 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007337 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007338 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007339 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007340 break;
7341 }
7342 h++;
7343 }
7344
7345 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007346 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7347 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007348 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007349 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007350 break;
7351 }
7352 h++;
7353 }
7354 }
7355
Willy Tarreau962c3f42010-01-10 00:15:35 +01007356 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7357 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007358 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007359 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007360 break;
7361 }
7362 h++;
7363 }
7364
Cyril Bonté70be45d2010-10-12 00:14:35 +02007365 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7366 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7367 if (memcmp(h, ";st=", 4) == 0) {
7368 h += 4;
7369
7370 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007371 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007372 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007373 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007374 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007375 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007376 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007377 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007378 else
Willy Tarreau295a8372011-03-10 11:25:07 +01007379 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007380 break;
7381 }
7382 h++;
7383 }
7384
Willy Tarreau295a8372011-03-10 11:25:07 +01007385 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007386
Willy Tarreaub2513902006-12-17 14:52:38 +01007387 return 1;
7388}
7389
Willy Tarreau4076a152009-04-02 15:18:36 +02007390/*
7391 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007392 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7393 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007394 */
7395void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7396 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007397 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007398{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007399 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7400 int len1 = buf->size - msg->som;
7401 es->len = buf->r - (buf->data + msg->som) + buf->size;
7402 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7403 if (es->len > len1 && len1 < sizeof(es->buf))
7404 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7405 }
7406 else {
7407 es->len = buf->r - (buf->data + msg->som);
7408 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7409 }
7410
Willy Tarreau4076a152009-04-02 15:18:36 +02007411 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007412 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007413 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007414 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007415 else
7416 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7417
Willy Tarreau4076a152009-04-02 15:18:36 +02007418 es->when = date; // user-visible date
7419 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007420 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007421 es->oe = other_end;
Willy Tarreau957c0a52011-03-03 17:42:23 +01007422 es->src = s->req->prod->addr.c.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007423 es->state = state;
7424 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007425 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007426}
Willy Tarreaub2513902006-12-17 14:52:38 +01007427
Willy Tarreaubce70882009-09-07 11:51:47 +02007428/* return the IP address pointed to by occurrence <occ> of header <hname> in
7429 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
7430 * occurrence number corresponding to this value is returned. If <occ> is
7431 * strictly negative, the occurrence number before the end corresponding to
7432 * this value is returned. If <occ> is null, any value is returned, so it is
7433 * not recommended to use it that way. Negative occurrences are limited to
7434 * a small value because it is required to keep them in memory while scanning.
7435 * IP address 0.0.0.0 is returned if no match is found.
7436 */
7437unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
7438{
7439 struct hdr_ctx ctx;
7440 unsigned int hdr_hist[MAX_HDR_HISTORY];
7441 unsigned int hist_ptr;
7442 int found = 0;
7443
7444 ctx.idx = 0;
7445 if (occ >= 0) {
7446 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7447 occ--;
7448 if (occ <= 0) {
7449 found = 1;
7450 break;
7451 }
7452 }
7453 if (!found)
7454 return 0;
7455 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7456 }
7457
7458 /* negative occurrence, we scan all the list then walk back */
7459 if (-occ > MAX_HDR_HISTORY)
7460 return 0;
7461
7462 hist_ptr = 0;
7463 hdr_hist[hist_ptr] = 0;
7464 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7465 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7466 if (hist_ptr >= MAX_HDR_HISTORY)
7467 hist_ptr = 0;
7468 found++;
7469 }
7470 if (-occ > found)
7471 return 0;
7472 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7473 * find occurrence -occ, so we have to check [hist_ptr+occ].
7474 */
7475 hist_ptr += occ;
7476 if (hist_ptr >= MAX_HDR_HISTORY)
7477 hist_ptr -= MAX_HDR_HISTORY;
7478 return hdr_hist[hist_ptr];
7479}
7480
Willy Tarreaubaaee002006-06-26 02:48:02 +02007481/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007482 * Print a debug line with a header
7483 */
7484void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7485{
7486 int len, max;
7487 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007488 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007489 max = end - start;
7490 UBOUND(max, sizeof(trash) - len - 1);
7491 len += strlcpy2(trash + len, start, max + 1);
7492 trash[len++] = '\n';
7493 write(1, trash, len);
7494}
7495
Willy Tarreau0937bc42009-12-22 15:03:09 +01007496/*
7497 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7498 * the required fields are properly allocated and that we only need to (re)init
7499 * them. This should be used before processing any new request.
7500 */
7501void http_init_txn(struct session *s)
7502{
7503 struct http_txn *txn = &s->txn;
7504 struct proxy *fe = s->fe;
7505
7506 txn->flags = 0;
7507 txn->status = -1;
7508
Willy Tarreauf64d1412010-10-07 20:06:11 +02007509 txn->cookie_first_date = 0;
7510 txn->cookie_last_date = 0;
7511
Willy Tarreau0937bc42009-12-22 15:03:09 +01007512 txn->req.sol = txn->req.eol = NULL;
7513 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7514 txn->rsp.sol = txn->rsp.eol = NULL;
7515 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007516 txn->req.chunk_len = 0LL;
7517 txn->req.body_len = 0LL;
7518 txn->rsp.chunk_len = 0LL;
7519 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007520 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7521 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007522
7523 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007524
7525 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7526 if (fe->options2 & PR_O2_REQBUG_OK)
7527 txn->req.err_pos = -1; /* let buggy requests pass */
7528
Willy Tarreau46023632010-01-07 22:51:47 +01007529 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007530 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7531
Willy Tarreau46023632010-01-07 22:51:47 +01007532 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007533 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7534
7535 if (txn->hdr_idx.v)
7536 hdr_idx_init(&txn->hdr_idx);
7537}
7538
7539/* to be used at the end of a transaction */
7540void http_end_txn(struct session *s)
7541{
7542 struct http_txn *txn = &s->txn;
7543
7544 /* these ones will have been dynamically allocated */
7545 pool_free2(pool2_requri, txn->uri);
7546 pool_free2(pool2_capture, txn->cli_cookie);
7547 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007548 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007549
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007550 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007551 txn->uri = NULL;
7552 txn->srv_cookie = NULL;
7553 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007554
7555 if (txn->req.cap) {
7556 struct cap_hdr *h;
7557 for (h = s->fe->req_cap; h; h = h->next)
7558 pool_free2(h->pool, txn->req.cap[h->index]);
7559 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7560 }
7561
7562 if (txn->rsp.cap) {
7563 struct cap_hdr *h;
7564 for (h = s->fe->rsp_cap; h; h = h->next)
7565 pool_free2(h->pool, txn->rsp.cap[h->index]);
7566 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7567 }
7568
Willy Tarreau0937bc42009-12-22 15:03:09 +01007569}
7570
7571/* to be used at the end of a transaction to prepare a new one */
7572void http_reset_txn(struct session *s)
7573{
7574 http_end_txn(s);
7575 http_init_txn(s);
7576
7577 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007578 s->logs.logwait = s->fe->to_log;
Willy Tarreau827aee92011-03-10 16:55:02 +01007579 s->srv_conn = NULL;
Willy Tarreau9e000c62011-03-10 14:03:36 +01007580 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007581 /* re-init store persistence */
7582 s->store_count = 0;
7583
Willy Tarreau0937bc42009-12-22 15:03:09 +01007584 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007585
7586 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7587
Willy Tarreau739cfba2010-01-25 23:11:14 +01007588 /* We must trim any excess data from the response buffer, because we
7589 * may have blocked an invalid response from a server that we don't
7590 * want to accidentely forward once we disable the analysers, nor do
7591 * we want those data to come along with next response. A typical
7592 * example of such data would be from a buggy server responding to
7593 * a HEAD with some data, or sending more than the advertised
7594 * content-length.
7595 */
7596 if (unlikely(s->rep->l > s->rep->send_max)) {
7597 s->rep->l = s->rep->send_max;
7598 s->rep->r = s->rep->w + s->rep->l;
7599 if (s->rep->r >= s->rep->data + s->rep->size)
7600 s->rep->r -= s->rep->size;
7601 }
7602
Willy Tarreau0937bc42009-12-22 15:03:09 +01007603 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007604 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007605
Willy Tarreaud04e8582010-05-31 12:31:35 +02007606 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007607 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007608
7609 s->req->rex = TICK_ETERNITY;
7610 s->req->wex = TICK_ETERNITY;
7611 s->req->analyse_exp = TICK_ETERNITY;
7612 s->rep->rex = TICK_ETERNITY;
7613 s->rep->wex = TICK_ETERNITY;
7614 s->rep->analyse_exp = TICK_ETERNITY;
7615}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007616
Willy Tarreauff011f22011-01-06 17:51:27 +01007617void free_http_req_rules(struct list *r) {
7618 struct http_req_rule *tr, *pr;
7619
7620 list_for_each_entry_safe(pr, tr, r, list) {
7621 LIST_DEL(&pr->list);
7622 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7623 free(pr->http_auth.realm);
7624
7625 free(pr);
7626 }
7627}
7628
7629struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7630{
7631 struct http_req_rule *rule;
7632 int cur_arg;
7633
7634 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7635 if (!rule) {
7636 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7637 return NULL;
7638 }
7639
7640 if (!*args[0]) {
7641 goto req_error_parsing;
7642 } else if (!strcmp(args[0], "allow")) {
7643 rule->action = HTTP_REQ_ACT_ALLOW;
7644 cur_arg = 1;
7645 } else if (!strcmp(args[0], "deny")) {
7646 rule->action = HTTP_REQ_ACT_DENY;
7647 cur_arg = 1;
7648 } else if (!strcmp(args[0], "auth")) {
7649 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7650 cur_arg = 1;
7651
7652 while(*args[cur_arg]) {
7653 if (!strcmp(args[cur_arg], "realm")) {
7654 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7655 cur_arg+=2;
7656 continue;
7657 } else
7658 break;
7659 }
7660 } else {
7661req_error_parsing:
7662 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7663 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7664 return NULL;
7665 }
7666
7667 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7668 struct acl_cond *cond;
7669
7670 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7671 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7672 file, linenum, args[0]);
7673 return NULL;
7674 }
7675 rule->cond = cond;
7676 }
7677 else if (*args[cur_arg]) {
7678 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7679 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7680 file, linenum, args[0], args[cur_arg]);
7681 return NULL;
7682 }
7683
7684 return rule;
7685}
7686
Willy Tarreau8797c062007-05-07 00:55:35 +02007687/************************************************************************/
7688/* The code below is dedicated to ACL parsing and matching */
7689/************************************************************************/
7690
7691
7692
7693
7694/* 1. Check on METHOD
7695 * We use the pre-parsed method if it is known, and store its number as an
7696 * integer. If it is unknown, we use the pointer and the length.
7697 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007698static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007699{
7700 int len, meth;
7701
Willy Tarreauae8b7962007-06-09 23:10:04 +02007702 len = strlen(*text);
7703 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007704
7705 pattern->val.i = meth;
7706 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007707 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007708 if (!pattern->ptr.str)
7709 return 0;
7710 pattern->len = len;
7711 }
7712 return 1;
7713}
7714
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007715static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007716acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7717 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007718{
7719 int meth;
7720 struct http_txn *txn = l7;
7721
Willy Tarreaub6866442008-07-14 23:54:42 +02007722 if (!txn)
7723 return 0;
7724
Willy Tarreau655dce92009-11-08 13:10:58 +01007725 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007726 return 0;
7727
Willy Tarreau8797c062007-05-07 00:55:35 +02007728 meth = txn->meth;
7729 test->i = meth;
7730 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007731 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7732 /* ensure the indexes are not affected */
7733 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007734 test->len = txn->req.sl.rq.m_l;
7735 test->ptr = txn->req.sol;
7736 }
7737 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7738 return 1;
7739}
7740
7741static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7742{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007743 int icase;
7744
Willy Tarreau8797c062007-05-07 00:55:35 +02007745 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02007746 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007747
7748 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02007749 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007750
7751 /* Other method, we must compare the strings */
7752 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02007753 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007754
7755 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
7756 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
7757 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007758 return ACL_PAT_FAIL;
7759 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007760}
7761
7762/* 2. Check on Request/Status Version
7763 * We simply compare strings here.
7764 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007765static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007766{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007767 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007768 if (!pattern->ptr.str)
7769 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007770 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007771 return 1;
7772}
7773
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007774static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007775acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7776 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007777{
7778 struct http_txn *txn = l7;
7779 char *ptr;
7780 int len;
7781
Willy Tarreaub6866442008-07-14 23:54:42 +02007782 if (!txn)
7783 return 0;
7784
Willy Tarreau655dce92009-11-08 13:10:58 +01007785 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007786 return 0;
7787
Willy Tarreau8797c062007-05-07 00:55:35 +02007788 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007789 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007790
7791 while ((len-- > 0) && (*ptr++ != '/'));
7792 if (len <= 0)
7793 return 0;
7794
7795 test->ptr = ptr;
7796 test->len = len;
7797
7798 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7799 return 1;
7800}
7801
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007802static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007803acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7804 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007805{
7806 struct http_txn *txn = l7;
7807 char *ptr;
7808 int len;
7809
Willy Tarreaub6866442008-07-14 23:54:42 +02007810 if (!txn)
7811 return 0;
7812
Willy Tarreau655dce92009-11-08 13:10:58 +01007813 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007814 return 0;
7815
Willy Tarreau8797c062007-05-07 00:55:35 +02007816 len = txn->rsp.sl.st.v_l;
7817 ptr = txn->rsp.sol;
7818
7819 while ((len-- > 0) && (*ptr++ != '/'));
7820 if (len <= 0)
7821 return 0;
7822
7823 test->ptr = ptr;
7824 test->len = len;
7825
7826 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7827 return 1;
7828}
7829
7830/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007831static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007832acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7833 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007834{
7835 struct http_txn *txn = l7;
7836 char *ptr;
7837 int len;
7838
Willy Tarreaub6866442008-07-14 23:54:42 +02007839 if (!txn)
7840 return 0;
7841
Willy Tarreau655dce92009-11-08 13:10:58 +01007842 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007843 return 0;
7844
Willy Tarreau8797c062007-05-07 00:55:35 +02007845 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007846 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007847
7848 test->i = __strl2ui(ptr, len);
7849 test->flags = ACL_TEST_F_VOL_1ST;
7850 return 1;
7851}
7852
7853/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007854static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007855acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7856 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007857{
7858 struct http_txn *txn = l7;
7859
Willy Tarreaub6866442008-07-14 23:54:42 +02007860 if (!txn)
7861 return 0;
7862
Willy Tarreau655dce92009-11-08 13:10:58 +01007863 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007864 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007865
Willy Tarreauc11416f2007-06-17 16:58:38 +02007866 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7867 /* ensure the indexes are not affected */
7868 return 0;
7869
Willy Tarreau8797c062007-05-07 00:55:35 +02007870 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007871 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007872
Willy Tarreauf3d25982007-05-08 22:45:09 +02007873 /* we do not need to set READ_ONLY because the data is in a buffer */
7874 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007875 return 1;
7876}
7877
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007878static int
7879acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7880 struct acl_expr *expr, struct acl_test *test)
7881{
7882 struct http_txn *txn = l7;
7883
Willy Tarreaub6866442008-07-14 23:54:42 +02007884 if (!txn)
7885 return 0;
7886
Willy Tarreau655dce92009-11-08 13:10:58 +01007887 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007888 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007889
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007890 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7891 /* ensure the indexes are not affected */
7892 return 0;
7893
7894 /* Parse HTTP request */
Willy Tarreau957c0a52011-03-03 17:42:23 +01007895 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to);
7896 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007897 test->i = AF_INET;
7898
7899 /*
7900 * If we are parsing url in frontend space, we prepare backend stage
7901 * to not parse again the same url ! optimization lazyness...
7902 */
7903 if (px->options & PR_O_HTTP_PROXY)
7904 l4->flags |= SN_ADDR_SET;
7905
7906 test->flags = ACL_TEST_F_READ_ONLY;
7907 return 1;
7908}
7909
7910static int
7911acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7912 struct acl_expr *expr, struct acl_test *test)
7913{
7914 struct http_txn *txn = l7;
7915
Willy Tarreaub6866442008-07-14 23:54:42 +02007916 if (!txn)
7917 return 0;
7918
Willy Tarreau655dce92009-11-08 13:10:58 +01007919 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007920 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007921
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007922 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7923 /* ensure the indexes are not affected */
7924 return 0;
7925
7926 /* Same optimization as url_ip */
Willy Tarreau957c0a52011-03-03 17:42:23 +01007927 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to);
7928 test->i = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007929
7930 if (px->options & PR_O_HTTP_PROXY)
7931 l4->flags |= SN_ADDR_SET;
7932
7933 test->flags = ACL_TEST_F_READ_ONLY;
7934 return 1;
7935}
7936
Willy Tarreauc11416f2007-06-17 16:58:38 +02007937/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7938 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7939 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007940static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007941acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007942 struct acl_expr *expr, struct acl_test *test)
7943{
7944 struct http_txn *txn = l7;
7945 struct hdr_idx *idx = &txn->hdr_idx;
7946 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007947
Willy Tarreaub6866442008-07-14 23:54:42 +02007948 if (!txn)
7949 return 0;
7950
Willy Tarreau33a7e692007-06-10 19:45:56 +02007951 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7952 /* search for header from the beginning */
7953 ctx->idx = 0;
7954
Willy Tarreau33a7e692007-06-10 19:45:56 +02007955 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7956 test->flags |= ACL_TEST_F_FETCH_MORE;
7957 test->flags |= ACL_TEST_F_VOL_HDR;
7958 test->len = ctx->vlen;
7959 test->ptr = (char *)ctx->line + ctx->val;
7960 return 1;
7961 }
7962
7963 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7964 test->flags |= ACL_TEST_F_VOL_HDR;
7965 return 0;
7966}
7967
Willy Tarreau33a7e692007-06-10 19:45:56 +02007968static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007969acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7970 struct acl_expr *expr, struct acl_test *test)
7971{
7972 struct http_txn *txn = l7;
7973
Willy Tarreaub6866442008-07-14 23:54:42 +02007974 if (!txn)
7975 return 0;
7976
Willy Tarreau655dce92009-11-08 13:10:58 +01007977 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007978 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007979
Willy Tarreauc11416f2007-06-17 16:58:38 +02007980 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7981 /* ensure the indexes are not affected */
7982 return 0;
7983
7984 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7985}
7986
7987static int
7988acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7989 struct acl_expr *expr, struct acl_test *test)
7990{
7991 struct http_txn *txn = l7;
7992
Willy Tarreaub6866442008-07-14 23:54:42 +02007993 if (!txn)
7994 return 0;
7995
Willy Tarreau655dce92009-11-08 13:10:58 +01007996 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007997 return 0;
7998
7999 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
8000}
8001
8002/* 6. Check on HTTP header count. The number of occurrences is returned.
8003 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8004 */
8005static int
8006acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008007 struct acl_expr *expr, struct acl_test *test)
8008{
8009 struct http_txn *txn = l7;
8010 struct hdr_idx *idx = &txn->hdr_idx;
8011 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008012 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008013
Willy Tarreaub6866442008-07-14 23:54:42 +02008014 if (!txn)
8015 return 0;
8016
Willy Tarreau33a7e692007-06-10 19:45:56 +02008017 ctx.idx = 0;
8018 cnt = 0;
8019 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
8020 cnt++;
8021
8022 test->i = cnt;
8023 test->flags = ACL_TEST_F_VOL_HDR;
8024 return 1;
8025}
8026
Willy Tarreauc11416f2007-06-17 16:58:38 +02008027static int
8028acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8029 struct acl_expr *expr, struct acl_test *test)
8030{
8031 struct http_txn *txn = l7;
8032
Willy Tarreaub6866442008-07-14 23:54:42 +02008033 if (!txn)
8034 return 0;
8035
Willy Tarreau655dce92009-11-08 13:10:58 +01008036 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008037 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008038
Willy Tarreauc11416f2007-06-17 16:58:38 +02008039 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8040 /* ensure the indexes are not affected */
8041 return 0;
8042
8043 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
8044}
8045
8046static int
8047acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8048 struct acl_expr *expr, struct acl_test *test)
8049{
8050 struct http_txn *txn = l7;
8051
Willy Tarreaub6866442008-07-14 23:54:42 +02008052 if (!txn)
8053 return 0;
8054
Willy Tarreau655dce92009-11-08 13:10:58 +01008055 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008056 return 0;
8057
8058 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
8059}
8060
Willy Tarreau33a7e692007-06-10 19:45:56 +02008061/* 7. Check on HTTP header's integer value. The integer value is returned.
8062 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008063 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008064 */
8065static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02008066acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008067 struct acl_expr *expr, struct acl_test *test)
8068{
8069 struct http_txn *txn = l7;
8070 struct hdr_idx *idx = &txn->hdr_idx;
8071 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008072
Willy Tarreaub6866442008-07-14 23:54:42 +02008073 if (!txn)
8074 return 0;
8075
Willy Tarreau33a7e692007-06-10 19:45:56 +02008076 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8077 /* search for header from the beginning */
8078 ctx->idx = 0;
8079
Willy Tarreau33a7e692007-06-10 19:45:56 +02008080 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8081 test->flags |= ACL_TEST_F_FETCH_MORE;
8082 test->flags |= ACL_TEST_F_VOL_HDR;
8083 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
8084 return 1;
8085 }
8086
8087 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8088 test->flags |= ACL_TEST_F_VOL_HDR;
8089 return 0;
8090}
8091
Willy Tarreauc11416f2007-06-17 16:58:38 +02008092static int
8093acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8094 struct acl_expr *expr, struct acl_test *test)
8095{
8096 struct http_txn *txn = l7;
8097
Willy Tarreaub6866442008-07-14 23:54:42 +02008098 if (!txn)
8099 return 0;
8100
Willy Tarreau655dce92009-11-08 13:10:58 +01008101 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008102 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008103
Willy Tarreauc11416f2007-06-17 16:58:38 +02008104 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8105 /* ensure the indexes are not affected */
8106 return 0;
8107
8108 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
8109}
8110
8111static int
8112acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8113 struct acl_expr *expr, struct acl_test *test)
8114{
8115 struct http_txn *txn = l7;
8116
Willy Tarreaub6866442008-07-14 23:54:42 +02008117 if (!txn)
8118 return 0;
8119
Willy Tarreau655dce92009-11-08 13:10:58 +01008120 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008121 return 0;
8122
8123 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
8124}
8125
Willy Tarreau106f9792009-09-19 07:54:16 +02008126/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
8127 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8128 */
8129static int
8130acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
8131 struct acl_expr *expr, struct acl_test *test)
8132{
8133 struct http_txn *txn = l7;
8134 struct hdr_idx *idx = &txn->hdr_idx;
8135 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
8136
8137 if (!txn)
8138 return 0;
8139
8140 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8141 /* search for header from the beginning */
8142 ctx->idx = 0;
8143
8144 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8145 test->flags |= ACL_TEST_F_FETCH_MORE;
8146 test->flags |= ACL_TEST_F_VOL_HDR;
8147 /* Same optimization as url_ip */
David du Colombier6f5ccb12011-03-10 22:26:24 +01008148 memset(&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr, 0, sizeof(((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr));
8149 url2ipv4((char *)ctx->line + ctx->val, &((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr);
8150 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr;
Willy Tarreau106f9792009-09-19 07:54:16 +02008151 test->i = AF_INET;
8152 return 1;
8153 }
8154
8155 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8156 test->flags |= ACL_TEST_F_VOL_HDR;
8157 return 0;
8158}
8159
8160static int
8161acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8162 struct acl_expr *expr, struct acl_test *test)
8163{
8164 struct http_txn *txn = l7;
8165
8166 if (!txn)
8167 return 0;
8168
Willy Tarreau655dce92009-11-08 13:10:58 +01008169 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008170 return 0;
8171
8172 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8173 /* ensure the indexes are not affected */
8174 return 0;
8175
8176 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8177}
8178
8179static int
8180acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8181 struct acl_expr *expr, struct acl_test *test)
8182{
8183 struct http_txn *txn = l7;
8184
8185 if (!txn)
8186 return 0;
8187
Willy Tarreau655dce92009-11-08 13:10:58 +01008188 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008189 return 0;
8190
8191 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8192}
8193
Willy Tarreau737b0c12007-06-10 21:28:46 +02008194/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8195 * the first '/' after the possible hostname, and ends before the possible '?'.
8196 */
8197static int
8198acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8199 struct acl_expr *expr, struct acl_test *test)
8200{
8201 struct http_txn *txn = l7;
8202 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008203
Willy Tarreaub6866442008-07-14 23:54:42 +02008204 if (!txn)
8205 return 0;
8206
Willy Tarreau655dce92009-11-08 13:10:58 +01008207 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008208 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008209
Willy Tarreauc11416f2007-06-17 16:58:38 +02008210 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8211 /* ensure the indexes are not affected */
8212 return 0;
8213
Willy Tarreau962c3f42010-01-10 00:15:35 +01008214 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008215 ptr = http_get_path(txn);
8216 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008217 return 0;
8218
8219 /* OK, we got the '/' ! */
8220 test->ptr = ptr;
8221
8222 while (ptr < end && *ptr != '?')
8223 ptr++;
8224
8225 test->len = ptr - test->ptr;
8226
8227 /* we do not need to set READ_ONLY because the data is in a buffer */
8228 test->flags = ACL_TEST_F_VOL_1ST;
8229 return 1;
8230}
8231
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008232static int
8233acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8234 struct acl_expr *expr, struct acl_test *test)
8235{
8236 struct buffer *req = s->req;
8237 struct http_txn *txn = &s->txn;
8238 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008239
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008240 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8241 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8242 */
8243
8244 if (!s || !req)
8245 return 0;
8246
Willy Tarreau655dce92009-11-08 13:10:58 +01008247 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008248 /* Already decoded as OK */
8249 test->flags |= ACL_TEST_F_SET_RES_PASS;
8250 return 1;
8251 }
8252
8253 /* Try to decode HTTP request */
8254 if (likely(req->lr < req->r))
8255 http_msg_analyzer(req, msg, &txn->hdr_idx);
8256
Willy Tarreau655dce92009-11-08 13:10:58 +01008257 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008258 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8259 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8260 return 1;
8261 }
8262 /* wait for final state */
8263 test->flags |= ACL_TEST_F_MAY_CHANGE;
8264 return 0;
8265 }
8266
8267 /* OK we got a valid HTTP request. We have some minor preparation to
8268 * perform so that further checks can rely on HTTP tests.
8269 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008270 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008271 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8272 s->flags |= SN_REDIRECTABLE;
8273
8274 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8275 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8276 return 1;
8277 }
8278
8279 test->flags |= ACL_TEST_F_SET_RES_PASS;
8280 return 1;
8281}
8282
Willy Tarreau7f18e522010-10-22 20:04:13 +02008283/* return a valid test if the current request is the first one on the connection */
8284static int
8285acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8286 struct acl_expr *expr, struct acl_test *test)
8287{
8288 if (!s)
8289 return 0;
8290
8291 if (s->txn.flags & TX_NOT_FIRST)
8292 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8293 else
8294 test->flags |= ACL_TEST_F_SET_RES_PASS;
8295
8296 return 1;
8297}
8298
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008299static int
8300acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8301 struct acl_expr *expr, struct acl_test *test)
8302{
8303
8304 if (!s)
8305 return 0;
8306
8307 if (!get_http_auth(s))
8308 return 0;
8309
8310 test->ctx.a[0] = expr->arg.ul;
8311 test->ctx.a[1] = s->txn.auth.user;
8312 test->ctx.a[2] = s->txn.auth.pass;
8313
8314 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8315
8316 return 1;
8317}
Willy Tarreau8797c062007-05-07 00:55:35 +02008318
8319/************************************************************************/
8320/* All supported keywords must be declared here. */
8321/************************************************************************/
8322
8323/* Note: must not be declared <const> as its list will be overwritten */
8324static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008325 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8326
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008327 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008328 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8329 { "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 +02008330 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008331
Willy Tarreauc4262962010-05-10 23:42:40 +02008332 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008333 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8334 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8335 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8336 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8337 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8338 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008339 { "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 +02008340 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008341
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008342 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008343 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008344 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8345 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8346 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8347 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8348 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8349 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8350 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
8351 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008352 { "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 +02008353
Willy Tarreauc4262962010-05-10 23:42:40 +02008354 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008355 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8356 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8357 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8358 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8359 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8360 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8361 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
8362 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008363 { "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 +02008364
Willy Tarreauc4262962010-05-10 23:42:40 +02008365 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008366 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8367 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8368 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8369 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8370 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8371 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008372
Willy Tarreauf3d25982007-05-08 22:45:09 +02008373#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008374 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8375 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8376 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8377 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8378 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8379 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8380 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8381
Willy Tarreau8797c062007-05-07 00:55:35 +02008382 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8383 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8384 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8385 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8386 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8387 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8388 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8389 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008390#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008391
Willy Tarreau7f18e522010-10-22 20:04:13 +02008392 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8393 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8394 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008395 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008396}};
8397
Willy Tarreau4a568972010-05-12 08:08:50 +02008398/************************************************************************/
8399/* The code below is dedicated to pattern fetching and matching */
8400/************************************************************************/
8401
8402/* extract the IP address from the last occurrence of specified header. Note
8403 * that we should normally first extract the string then convert it to IP,
8404 * but right now we have all the functions to do this seemlessly, and we will
8405 * be able to change that later without touching the configuration.
8406 */
8407static int
8408pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02008409 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008410{
8411 struct http_txn *txn = l7;
8412
Emeric Brun485479d2010-09-23 18:02:19 +02008413 data->ip.s_addr = htonl(get_ip_from_hdr2(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx, -1));
Willy Tarreau4a568972010-05-12 08:08:50 +02008414 return data->ip.s_addr != 0;
8415}
8416
David Cournapeau16023ee2010-12-23 20:55:41 +09008417/*
8418 * Given a path string and its length, find the position of beginning of the
8419 * query string. Returns NULL if no query string is found in the path.
8420 *
8421 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8422 *
8423 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8424 */
8425static inline char *find_query_string(char *path, size_t path_l)
8426{
8427 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008428
David Cournapeau16023ee2010-12-23 20:55:41 +09008429 p = memchr(path, '?', path_l);
8430 return p ? p + 1 : NULL;
8431}
8432
8433static inline int is_param_delimiter(char c)
8434{
8435 return c == '&' || c == ';';
8436}
8437
8438/*
8439 * Given a url parameter, find the starting position of the first occurence,
8440 * or NULL if the parameter is not found.
8441 *
8442 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8443 * the function will return query_string+8.
8444 */
8445static char*
8446find_url_param_pos(char* query_string, size_t query_string_l,
8447 char* url_param_name, size_t url_param_name_l)
8448{
8449 char *pos, *last;
8450
8451 pos = query_string;
8452 last = query_string + query_string_l - url_param_name_l - 1;
8453
8454 while (pos <= last) {
8455 if (pos[url_param_name_l] == '=') {
8456 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8457 return pos;
8458 pos += url_param_name_l + 1;
8459 }
8460 while (pos <= last && !is_param_delimiter(*pos))
8461 pos++;
8462 pos++;
8463 }
8464 return NULL;
8465}
8466
8467/*
8468 * Given a url parameter name, returns its value and size into *value and
8469 * *value_l respectively, and returns non-zero. If the parameter is not found,
8470 * zero is returned and value/value_l are not touched.
8471 */
8472static int
8473find_url_param_value(char* path, size_t path_l,
8474 char* url_param_name, size_t url_param_name_l,
8475 char** value, size_t* value_l)
8476{
8477 char *query_string, *qs_end;
8478 char *arg_start;
8479 char *value_start, *value_end;
8480
8481 query_string = find_query_string(path, path_l);
8482 if (!query_string)
8483 return 0;
8484
8485 qs_end = path + path_l;
8486 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8487 url_param_name, url_param_name_l);
8488 if (!arg_start)
8489 return 0;
8490
8491 value_start = arg_start + url_param_name_l + 1;
8492 value_end = value_start;
8493
8494 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8495 value_end++;
8496
8497 *value = value_start;
8498 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008499 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008500}
8501
8502static int
8503pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8504 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8505{
8506 struct http_txn *txn = l7;
8507 struct http_msg *msg = &txn->req;
8508 char *url_param_value;
8509 size_t url_param_value_l;
8510
8511 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8512 arg_p->data.str.str, arg_p->data.str.len,
8513 &url_param_value, &url_param_value_l))
8514 return 0;
8515
8516 data->str.str = url_param_value;
8517 data->str.len = url_param_value_l;
8518 return 1;
8519}
8520
Emeric Brun485479d2010-09-23 18:02:19 +02008521
Willy Tarreau4a568972010-05-12 08:08:50 +02008522/************************************************************************/
8523/* All supported keywords must be declared here. */
8524/************************************************************************/
8525/* Note: must not be declared <const> as its list will be overwritten */
8526static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Emeric Brun485479d2010-09-23 18:02:19 +02008527 { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008528 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Emeric Brun485479d2010-09-23 18:02:19 +02008529 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008530}};
8531
Willy Tarreau8797c062007-05-07 00:55:35 +02008532
8533__attribute__((constructor))
8534static void __http_protocol_init(void)
8535{
8536 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008537 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008538}
8539
8540
Willy Tarreau58f10d72006-12-04 02:26:12 +01008541/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008542 * Local variables:
8543 * c-indent-level: 8
8544 * c-basic-offset: 8
8545 * End:
8546 */