blob: ed31323246fc843fa1494ce8811113334ae6f435 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010027#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/compat.h>
29#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010030#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
39#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreau8797c062007-05-07 00:55:35 +020042#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010043#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/backend.h>
45#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010046#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020049#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010051#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020052#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020053#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020054#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010055#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010059#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020060#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/task.h>
62
Willy Tarreau522d6c02009-12-06 18:49:18 +010063const char HTTP_100[] =
64 "HTTP/1.1 100 Continue\r\n\r\n";
65
66const struct chunk http_100_chunk = {
67 .str = (char *)&HTTP_100,
68 .len = sizeof(HTTP_100)-1
69};
70
Willy Tarreau1c47f852006-07-09 08:22:27 +020071/* This is used by remote monitoring */
Willy Tarreau0f772532006-12-23 20:51:41 +010072const char HTTP_200[] =
Willy Tarreau1c47f852006-07-09 08:22:27 +020073 "HTTP/1.0 200 OK\r\n"
74 "Cache-Control: no-cache\r\n"
75 "Connection: close\r\n"
76 "Content-Type: text/html\r\n"
77 "\r\n"
78 "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n";
79
Willy Tarreau0f772532006-12-23 20:51:41 +010080const struct chunk http_200_chunk = {
81 .str = (char *)&HTTP_200,
82 .len = sizeof(HTTP_200)-1
83};
84
Willy Tarreaua9679ac2010-01-03 17:32:57 +010085/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020086const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020090 "Location: "; /* not terminated since it will be concatenated with the URL */
91
Willy Tarreau0f772532006-12-23 20:51:41 +010092const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010095 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010096 "Location: "; /* not terminated since it will be concatenated with the URL */
97
98/* same as 302 except that the browser MUST retry with the GET method */
99const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100100 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100101 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100102 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100103 "Location: "; /* not terminated since it will be concatenated with the URL */
104
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
106const char *HTTP_401_fmt =
107 "HTTP/1.0 401 Unauthorized\r\n"
108 "Cache-Control: no-cache\r\n"
109 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200110 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
112 "\r\n"
113 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
114
Willy Tarreau844a7e72010-01-31 21:46:18 +0100115const char *HTTP_407_fmt =
116 "HTTP/1.0 407 Unauthorized\r\n"
117 "Cache-Control: no-cache\r\n"
118 "Connection: close\r\n"
119 "Content-Type: text/html\r\n"
120 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
121 "\r\n"
122 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
123
Willy Tarreau0f772532006-12-23 20:51:41 +0100124
125const int http_err_codes[HTTP_ERR_SIZE] = {
126 [HTTP_ERR_400] = 400,
127 [HTTP_ERR_403] = 403,
128 [HTTP_ERR_408] = 408,
129 [HTTP_ERR_500] = 500,
130 [HTTP_ERR_502] = 502,
131 [HTTP_ERR_503] = 503,
132 [HTTP_ERR_504] = 504,
133};
134
Willy Tarreau80587432006-12-24 17:47:20 +0100135static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreau0f772532006-12-23 20:51:41 +0100136 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100137 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 "Cache-Control: no-cache\r\n"
139 "Connection: close\r\n"
140 "Content-Type: text/html\r\n"
141 "\r\n"
142 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
143
144 [HTTP_ERR_403] =
145 "HTTP/1.0 403 Forbidden\r\n"
146 "Cache-Control: no-cache\r\n"
147 "Connection: close\r\n"
148 "Content-Type: text/html\r\n"
149 "\r\n"
150 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
151
152 [HTTP_ERR_408] =
153 "HTTP/1.0 408 Request Time-out\r\n"
154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
159
160 [HTTP_ERR_500] =
161 "HTTP/1.0 500 Server Error\r\n"
162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
167
168 [HTTP_ERR_502] =
169 "HTTP/1.0 502 Bad Gateway\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
175
176 [HTTP_ERR_503] =
177 "HTTP/1.0 503 Service Unavailable\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
183
184 [HTTP_ERR_504] =
185 "HTTP/1.0 504 Gateway Time-out\r\n"
186 "Cache-Control: no-cache\r\n"
187 "Connection: close\r\n"
188 "Content-Type: text/html\r\n"
189 "\r\n"
190 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
191
192};
193
Willy Tarreau80587432006-12-24 17:47:20 +0100194/* We must put the messages here since GCC cannot initialize consts depending
195 * on strlen().
196 */
197struct chunk http_err_chunks[HTTP_ERR_SIZE];
198
Willy Tarreau42250582007-04-01 01:30:43 +0200199#define FD_SETS_ARE_BITFIELDS
200#ifdef FD_SETS_ARE_BITFIELDS
201/*
202 * This map is used with all the FD_* macros to check whether a particular bit
203 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
204 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
205 * byte should be encoded. Be careful to always pass bytes from 0 to 255
206 * exclusively to the macros.
207 */
208fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
209fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
210
211#else
212#error "Check if your OS uses bitfields for fd_sets"
213#endif
214
Willy Tarreau80587432006-12-24 17:47:20 +0100215void init_proto_http()
216{
Willy Tarreau42250582007-04-01 01:30:43 +0200217 int i;
218 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100219 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200220
Willy Tarreau80587432006-12-24 17:47:20 +0100221 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
222 if (!http_err_msgs[msg]) {
223 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
224 abort();
225 }
226
227 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
228 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
229 }
Willy Tarreau42250582007-04-01 01:30:43 +0200230
231 /* initialize the log header encoding map : '{|}"#' should be encoded with
232 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
233 * URL encoding only requires '"', '#' to be encoded as well as non-
234 * printable characters above.
235 */
236 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
237 memset(url_encode_map, 0, sizeof(url_encode_map));
238 for (i = 0; i < 32; i++) {
239 FD_SET(i, hdr_encode_map);
240 FD_SET(i, url_encode_map);
241 }
242 for (i = 127; i < 256; i++) {
243 FD_SET(i, hdr_encode_map);
244 FD_SET(i, url_encode_map);
245 }
246
247 tmp = "\"#{|}";
248 while (*tmp) {
249 FD_SET(*tmp, hdr_encode_map);
250 tmp++;
251 }
252
253 tmp = "\"#";
254 while (*tmp) {
255 FD_SET(*tmp, url_encode_map);
256 tmp++;
257 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200258
259 /* memory allocations */
260 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200261 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100262}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263
Willy Tarreau53b6c742006-12-17 13:37:46 +0100264/*
265 * We have 26 list of methods (1 per first letter), each of which can have
266 * up to 3 entries (2 valid, 1 null).
267 */
268struct http_method_desc {
269 http_meth_t meth;
270 int len;
271 const char text[8];
272};
273
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100274const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100275 ['C' - 'A'] = {
276 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
277 },
278 ['D' - 'A'] = {
279 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
280 },
281 ['G' - 'A'] = {
282 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
283 },
284 ['H' - 'A'] = {
285 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
286 },
287 ['P' - 'A'] = {
288 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
289 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
290 },
291 ['T' - 'A'] = {
292 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
293 },
294 /* rest is empty like this :
295 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
296 */
297};
298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100299/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200300 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100301 * RFC2616 for those chars.
302 */
303
304const char http_is_spht[256] = {
305 [' '] = 1, ['\t'] = 1,
306};
307
308const char http_is_crlf[256] = {
309 ['\r'] = 1, ['\n'] = 1,
310};
311
312const char http_is_lws[256] = {
313 [' '] = 1, ['\t'] = 1,
314 ['\r'] = 1, ['\n'] = 1,
315};
316
317const char http_is_sep[256] = {
318 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
319 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
320 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
321 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
322 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
323};
324
325const char http_is_ctl[256] = {
326 [0 ... 31] = 1,
327 [127] = 1,
328};
329
330/*
331 * A token is any ASCII char that is neither a separator nor a CTL char.
332 * Do not overwrite values in assignment since gcc-2.95 will not handle
333 * them correctly. Instead, define every non-CTL char's status.
334 */
335const char http_is_token[256] = {
336 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
337 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
338 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
339 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
340 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
341 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
342 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
343 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
344 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
345 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
346 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
347 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
348 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
349 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
350 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
351 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
352 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
353 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
354 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
355 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
356 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
357 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
358 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
359 ['|'] = 1, ['}'] = 0, ['~'] = 1,
360};
361
362
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100363/*
364 * An http ver_token is any ASCII which can be found in an HTTP version,
365 * which includes 'H', 'T', 'P', '/', '.' and any digit.
366 */
367const char http_is_ver_token[256] = {
368 ['.'] = 1, ['/'] = 1,
369 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
370 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
371 ['H'] = 1, ['P'] = 1, ['T'] = 1,
372};
373
374
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100375/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100376 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
377 */
378#if defined(DEBUG_FSM)
379static void http_silent_debug(int line, struct session *s)
380{
381 int size = 0;
382 size += snprintf(trash + size, sizeof(trash) - size,
383 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n",
384 line,
385 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
386 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags);
387 write(-1, trash, size);
388 size = 0;
389 size += snprintf(trash + size, sizeof(trash) - size,
390 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n",
391 line,
392 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
393 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward);
394
395 write(-1, trash, size);
396}
397#else
398#define http_silent_debug(l,s) do { } while (0)
399#endif
400
401/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100402 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
403 * CRLF. Text length is measured first, so it cannot be NULL.
404 * The header is also automatically added to the index <hdr_idx>, and the end
405 * of headers is automatically adjusted. The number of bytes added is returned
406 * on success, otherwise <0 is returned indicating an error.
407 */
408int http_header_add_tail(struct buffer *b, struct http_msg *msg,
409 struct hdr_idx *hdr_idx, const char *text)
410{
411 int bytes, len;
412
413 len = strlen(text);
414 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
415 if (!bytes)
416 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100417 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100418 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
419}
420
421/*
422 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
423 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
424 * the buffer is only opened and the space reserved, but nothing is copied.
425 * The header is also automatically added to the index <hdr_idx>, and the end
426 * of headers is automatically adjusted. The number of bytes added is returned
427 * on success, otherwise <0 is returned indicating an error.
428 */
429int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
430 struct hdr_idx *hdr_idx, const char *text, int len)
431{
432 int bytes;
433
434 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
435 if (!bytes)
436 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100437 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100438 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
439}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440
441/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100442 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
443 * If so, returns the position of the first non-space character relative to
444 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
445 * to return a pointer to the place after the first space. Returns 0 if the
446 * header name does not match. Checks are case-insensitive.
447 */
448int http_header_match2(const char *hdr, const char *end,
449 const char *name, int len)
450{
451 const char *val;
452
453 if (hdr + len >= end)
454 return 0;
455 if (hdr[len] != ':')
456 return 0;
457 if (strncasecmp(hdr, name, len) != 0)
458 return 0;
459 val = hdr + len + 1;
460 while (val < end && HTTP_IS_SPHT(*val))
461 val++;
462 if ((val >= end) && (len + 2 <= end - hdr))
463 return len + 2; /* we may replace starting from second space */
464 return val - hdr;
465}
466
Willy Tarreau68085d82010-01-18 14:54:04 +0100467/* Find the end of the header value contained between <s> and <e>. See RFC2616,
468 * par 2.2 for more information. Note that it requires a valid header to return
469 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200470 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100471char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200472{
473 int quoted, qdpair;
474
475 quoted = qdpair = 0;
476 for (; s < e; s++) {
477 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200478 else if (quoted) {
479 if (*s == '\\') qdpair = 1;
480 else if (*s == '"') quoted = 0;
481 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200482 else if (*s == '"') quoted = 1;
483 else if (*s == ',') return s;
484 }
485 return s;
486}
487
488/* Find the first or next occurrence of header <name> in message buffer <sol>
489 * using headers index <idx>, and return it in the <ctx> structure. This
490 * structure holds everything necessary to use the header and find next
491 * occurrence. If its <idx> member is 0, the header is searched from the
492 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100493 * 1 when it finds a value, and 0 when there is no more. It is designed to work
494 * with headers defined as comma-separated lists. As a special case, if ctx->val
495 * is NULL when searching for a new values of a header, the current header is
496 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200497 */
498int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100499 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200500 struct hdr_ctx *ctx)
501{
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 char *eol, *sov;
503 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200504
Willy Tarreau68085d82010-01-18 14:54:04 +0100505 cur_idx = ctx->idx;
506 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 /* We have previously returned a value, let's search
508 * another one on the same line.
509 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 sol = ctx->line;
Willy Tarreau68085d82010-01-18 14:54:04 +0100511 ctx->del = ctx->val + ctx->vlen;
512 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200513 eol = sol + idx->v[cur_idx].len;
514
515 if (sov >= eol)
516 /* no more values in this header */
517 goto next_hdr;
518
Willy Tarreau68085d82010-01-18 14:54:04 +0100519 /* values remaining for this header, skip the comma but save it
520 * for later use (eg: for header deletion).
521 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200522 sov++;
523 while (sov < eol && http_is_lws[(unsigned char)*sov])
524 sov++;
525
526 goto return_hdr;
527 }
528
529 /* first request for this header */
530 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100531 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200532 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200533 while (cur_idx) {
534 eol = sol + idx->v[cur_idx].len;
535
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200536 if (len == 0) {
537 /* No argument was passed, we want any header.
538 * To achieve this, we simply build a fake request. */
539 while (sol + len < eol && sol[len] != ':')
540 len++;
541 name = sol;
542 }
543
Willy Tarreau33a7e692007-06-10 19:45:56 +0200544 if ((len < eol - sol) &&
545 (sol[len] == ':') &&
546 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100547 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200548 sov = sol + len + 1;
549 while (sov < eol && http_is_lws[(unsigned char)*sov])
550 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100551
Willy Tarreau33a7e692007-06-10 19:45:56 +0200552 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100553 ctx->prev = old_idx;
554 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200555 ctx->idx = cur_idx;
556 ctx->val = sov - sol;
557
558 eol = find_hdr_value_end(sov, eol);
559 ctx->vlen = eol - sov;
560 return 1;
561 }
562 next_hdr:
563 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100564 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200565 cur_idx = idx->v[cur_idx].next;
566 }
567 return 0;
568}
569
570int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100571 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200572 struct hdr_ctx *ctx)
573{
574 return http_find_header2(name, strlen(name), sol, idx, ctx);
575}
576
Willy Tarreau68085d82010-01-18 14:54:04 +0100577/* Remove one value of a header. This only works on a <ctx> returned by one of
578 * the http_find_header functions. The value is removed, as well as surrounding
579 * commas if any. If the removed value was alone, the whole header is removed.
580 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
581 * message <msg>. The new index is returned. If it is zero, it means there is
582 * no more header, so any processing may stop. The ctx is always left in a form
583 * that can be handled by http_find_header2() to find next occurrence.
584 */
585int http_remove_header2(struct http_msg *msg, struct buffer *buf,
586 struct hdr_idx *idx, struct hdr_ctx *ctx)
587{
588 int cur_idx = ctx->idx;
589 char *sol = ctx->line;
590 struct hdr_idx_elem *hdr;
591 int delta, skip_comma;
592
593 if (!cur_idx)
594 return 0;
595
596 hdr = &idx->v[cur_idx];
597 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) {
598 /* This was the only value of the header, we must now remove it entirely. */
599 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
600 http_msg_move_end(msg, delta);
601 idx->used--;
602 hdr->len = 0; /* unused entry */
603 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
604 ctx->idx = ctx->prev; /* walk back to the end of previous header */
605 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
606 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
607 ctx->vlen = 0;
608 return ctx->idx;
609 }
610
611 /* This was not the only value of this header. We have to remove between
612 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
613 * of the list, we remove the last separator.
614 */
615
616 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
617 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
618 sol + ctx->val + ctx->vlen + skip_comma,
619 NULL, 0);
620 hdr->len += delta;
621 http_msg_move_end(msg, delta);
622 ctx->val = ctx->del;
623 ctx->vlen = 0;
624 return ctx->idx;
625}
626
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100627/* This function handles a server error at the stream interface level. The
628 * stream interface is assumed to be already in a closed state. An optional
629 * message is copied into the input buffer, and an HTTP status code stored.
630 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100631 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200632 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100633static void http_server_error(struct session *t, struct stream_interface *si,
634 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200635{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100636 buffer_auto_read(si->ob);
637 buffer_abort(si->ob);
638 buffer_auto_close(si->ob);
639 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200640 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100641 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100642 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100643 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100644 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200645 }
646 if (!(t->flags & SN_ERR_MASK))
647 t->flags |= err;
648 if (!(t->flags & SN_FINST_MASK))
649 t->flags |= finst;
650}
651
Willy Tarreau80587432006-12-24 17:47:20 +0100652/* This function returns the appropriate error location for the given session
653 * and message.
654 */
655
656struct chunk *error_message(struct session *s, int msgnum)
657{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200658 if (s->be->errmsg[msgnum].str)
659 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100660 else if (s->fe->errmsg[msgnum].str)
661 return &s->fe->errmsg[msgnum];
662 else
663 return &http_err_chunks[msgnum];
664}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665
Willy Tarreau53b6c742006-12-17 13:37:46 +0100666/*
667 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
668 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
669 */
670static http_meth_t find_http_meth(const char *str, const int len)
671{
672 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100673 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100674
675 m = ((unsigned)*str - 'A');
676
677 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100678 for (h = http_methods[m]; h->len > 0; h++) {
679 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100680 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100681 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100682 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100683 };
684 return HTTP_METH_OTHER;
685 }
686 return HTTP_METH_NONE;
687
688}
689
Willy Tarreau21d2af32008-02-14 20:25:24 +0100690/* Parse the URI from the given transaction (which is assumed to be in request
691 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
692 * It is returned otherwise.
693 */
694static char *
695http_get_path(struct http_txn *txn)
696{
697 char *ptr, *end;
698
Willy Tarreau962c3f42010-01-10 00:15:35 +0100699 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100700 end = ptr + txn->req.sl.rq.u_l;
701
702 if (ptr >= end)
703 return NULL;
704
705 /* RFC2616, par. 5.1.2 :
706 * Request-URI = "*" | absuri | abspath | authority
707 */
708
709 if (*ptr == '*')
710 return NULL;
711
712 if (isalpha((unsigned char)*ptr)) {
713 /* this is a scheme as described by RFC3986, par. 3.1 */
714 ptr++;
715 while (ptr < end &&
716 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
717 ptr++;
718 /* skip '://' */
719 if (ptr == end || *ptr++ != ':')
720 return NULL;
721 if (ptr == end || *ptr++ != '/')
722 return NULL;
723 if (ptr == end || *ptr++ != '/')
724 return NULL;
725 }
726 /* skip [user[:passwd]@]host[:[port]] */
727
728 while (ptr < end && *ptr != '/')
729 ptr++;
730
731 if (ptr == end)
732 return NULL;
733
734 /* OK, we got the '/' ! */
735 return ptr;
736}
737
Willy Tarreauefb453c2008-10-26 20:49:47 +0100738/* Returns a 302 for a redirectable request. This may only be called just after
739 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
740 * left unchanged and will follow normal proxy processing.
741 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100742void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100743{
744 struct http_txn *txn;
745 struct chunk rdr;
746 char *path;
747 int len;
748
749 /* 1: create the response header */
750 rdr.len = strlen(HTTP_302);
751 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100752 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100753 memcpy(rdr.str, HTTP_302, rdr.len);
754
755 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200756 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100757 return;
758
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100759 /* special prefix "/" means don't change URL */
760 if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') {
761 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
762 rdr.len += s->srv->rdr_len;
763 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764
765 /* 3: add the request URI */
766 txn = &s->txn;
767 path = http_get_path(txn);
768 if (!path)
769 return;
770
Willy Tarreau962c3f42010-01-10 00:15:35 +0100771 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200772 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 return;
774
775 memcpy(rdr.str + rdr.len, path, len);
776 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100777
778 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
779 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
780 rdr.len += 29;
781 } else {
782 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
783 rdr.len += 23;
784 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100785
786 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100787 si->shutr(si);
788 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 si->err_type = SI_ET_NONE;
790 si->err_loc = NULL;
791 si->state = SI_ST_CLO;
792
793 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100794 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100795
796 /* FIXME: we should increase a counter of redirects per server and per backend. */
797 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100798 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100799}
800
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100801/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100802 * that the server side is closed. Note that err_type is actually a
803 * bitmask, where almost only aborts may be cumulated with other
804 * values. We consider that aborted operations are more important
805 * than timeouts or errors due to the fact that nobody else in the
806 * logs might explain incomplete retries. All others should avoid
807 * being cumulated. It should normally not be possible to have multiple
808 * aborts at once, but just in case, the first one in sequence is reported.
809 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100810void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100811{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100812 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813
814 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100815 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
816 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100818 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
819 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100820 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100821 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
822 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100823 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100824 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
825 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100826 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100827 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
828 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100829 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100830 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
831 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100832 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100833 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
834 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100835}
836
Willy Tarreau42250582007-04-01 01:30:43 +0200837extern const char sess_term_cond[8];
838extern const char sess_fin_state[8];
839extern const char *monthname[12];
Willy Tarreauf1348312010-10-07 15:54:11 +0200840const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */
841const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
842 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
843 Set-cookie Updated, unknown, unknown */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200844struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200845struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100846
Emeric Brun3a058f32009-06-30 18:26:00 +0200847void http_sess_clflog(struct session *s)
848{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100849 char pn[INET6_ADDRSTRLEN];
Emeric Brun3a058f32009-06-30 18:26:00 +0200850 struct proxy *fe = s->fe;
851 struct proxy *be = s->be;
852 struct proxy *prx_log;
853 struct http_txn *txn = &s->txn;
854 int tolog, level, err;
855 char *uri, *h;
856 char *svid;
857 struct tm tm;
858 static char tmpline[MAX_SYSLOG_LEN];
859 int hdr;
860 size_t w;
861 int t_request;
862
863 prx_log = fe;
864 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +0200865 (s->req->cons->conn_retries != be->conn_retries) ||
Emeric Brun3a058f32009-06-30 18:26:00 +0200866 txn->status >= 500;
867
868 if (s->cli_addr.ss_family == AF_INET)
869 inet_ntop(AF_INET,
870 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
871 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +0200872 else if (s->cli_addr.ss_family == AF_INET6)
Emeric Brun3a058f32009-06-30 18:26:00 +0200873 inet_ntop(AF_INET6,
874 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
875 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +0200876 else
877 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
Emeric Brun3a058f32009-06-30 18:26:00 +0200878
879 get_gmtime(s->logs.accept_date.tv_sec, &tm);
880
881 /* FIXME: let's limit ourselves to frontend logging for now. */
882 tolog = fe->to_log;
883
884 h = tmpline;
885
886 w = snprintf(h, sizeof(tmpline),
887 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
888 pn,
889 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
890 tm.tm_hour, tm.tm_min, tm.tm_sec);
891 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
892 goto trunc;
893 h += w;
894
895 if (h >= tmpline + sizeof(tmpline) - 4)
896 goto trunc;
897
898 *(h++) = ' ';
899 *(h++) = '\"';
900 uri = txn->uri ? txn->uri : "<BADREQ>";
901 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
902 '#', url_encode_map, uri);
903 *(h++) = '\"';
904
905 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
906 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
907 goto trunc;
908 h += w;
909
910 if (h >= tmpline + sizeof(tmpline) - 9)
911 goto trunc;
912 memcpy(h, " \"-\" \"-\"", 8);
913 h += 8;
914
915 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
916 " %d %03d",
Emeric Brun5bd86a82010-10-22 17:23:04 +0200917 s->cli_addr.ss_family == AF_UNIX ? s->listener->luid :
918 ntohs((s->cli_addr.ss_family == AF_INET) ?
919 ((struct sockaddr_in *)&s->cli_addr)->sin_port :
920 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Emeric Brun3a058f32009-06-30 18:26:00 +0200921 (int)s->logs.accept_date.tv_usec/1000);
922 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
923 goto trunc;
924 h += w;
925
926 w = strlen(fe->id);
927 if (h >= tmpline + sizeof(tmpline) - 4 - w)
928 goto trunc;
929 *(h++) = ' ';
930 *(h++) = '\"';
931 memcpy(h, fe->id, w);
932 h += w;
933 *(h++) = '\"';
934
935 w = strlen(be->id);
936 if (h >= tmpline + sizeof(tmpline) - 4 - w)
937 goto trunc;
938 *(h++) = ' ';
939 *(h++) = '\"';
940 memcpy(h, be->id, w);
941 h += w;
942 *(h++) = '\"';
943
944 svid = (tolog & LW_SVID) ?
945 (s->data_source != DATA_SRC_STATS) ?
946 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
947
948 w = strlen(svid);
949 if (h >= tmpline + sizeof(tmpline) - 4 - w)
950 goto trunc;
951 *(h++) = ' ';
952 *(h++) = '\"';
953 memcpy(h, svid, w);
954 h += w;
955 *(h++) = '\"';
956
957 t_request = -1;
958 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
959 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
960 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
961 " %d %ld %ld %ld %ld",
962 t_request,
963 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
964 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
965 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
966 s->logs.t_close);
967 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
968 goto trunc;
969 h += w;
970
971 if (h >= tmpline + sizeof(tmpline) - 8)
972 goto trunc;
973 *(h++) = ' ';
974 *(h++) = '\"';
975 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
976 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
977 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
978 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
979 *(h++) = '\"';
980
981 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
982 " %d %d %d %d %d %ld %ld",
983 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Willy Tarreauee28de02010-06-01 09:51:00 +0200984 (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries,
Emeric Brun3a058f32009-06-30 18:26:00 +0200985 s->logs.srv_queue_size, s->logs.prx_queue_size);
986
987 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
988 goto trunc;
989 h += w;
990
991 if (txn->cli_cookie) {
992 w = strlen(txn->cli_cookie);
993 if (h >= tmpline + sizeof(tmpline) - 4 - w)
994 goto trunc;
995 *(h++) = ' ';
996 *(h++) = '\"';
997 memcpy(h, txn->cli_cookie, w);
998 h += w;
999 *(h++) = '\"';
1000 } else {
1001 if (h >= tmpline + sizeof(tmpline) - 5)
1002 goto trunc;
1003 memcpy(h, " \"-\"", 4);
1004 h += 4;
1005 }
1006
1007 if (txn->srv_cookie) {
1008 w = strlen(txn->srv_cookie);
1009 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1010 goto trunc;
1011 *(h++) = ' ';
1012 *(h++) = '\"';
1013 memcpy(h, txn->srv_cookie, w);
1014 h += w;
1015 *(h++) = '\"';
1016 } else {
1017 if (h >= tmpline + sizeof(tmpline) - 5)
1018 goto trunc;
1019 memcpy(h, " \"-\"", 4);
1020 h += 4;
1021 }
1022
1023 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1024 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1025 if (h >= sizeof (tmpline) + tmpline - 4)
1026 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001027 if (txn->req.cap[hdr] != NULL) {
1028 *(h++) = ' ';
1029 *(h++) = '\"';
1030 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1031 '#', hdr_encode_map, txn->req.cap[hdr]);
1032 *(h++) = '\"';
1033 } else {
1034 memcpy(h, " \"-\"", 4);
1035 h += 4;
1036 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001037 }
1038 }
1039
1040 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1041 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1042 if (h >= sizeof (tmpline) + tmpline - 4)
1043 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001044 if (txn->rsp.cap[hdr] != NULL) {
1045 *(h++) = ' ';
1046 *(h++) = '\"';
1047 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1048 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1049 *(h++) = '\"';
1050 } else {
1051 memcpy(h, " \"-\"", 4);
1052 h += 4;
1053 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001054 }
1055 }
1056
1057trunc:
1058 *h = '\0';
1059
1060 level = LOG_INFO;
1061 if (err && (fe->options2 & PR_O2_LOGERRORS))
1062 level = LOG_ERR;
1063
1064 send_log(prx_log, level, "%s\n", tmpline);
1065
1066 s->logs.logwait = 0;
1067}
1068
Willy Tarreau42250582007-04-01 01:30:43 +02001069/*
1070 * send a log for the session when we have enough info about it.
1071 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001073void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001074{
Cyril Bontéacd7d632010-11-01 19:26:02 +01001075 char pn[INET6_ADDRSTRLEN];
Willy Tarreau42250582007-04-01 01:30:43 +02001076 struct proxy *fe = s->fe;
1077 struct proxy *be = s->be;
1078 struct proxy *prx_log;
1079 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001080 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001081 char *uri, *h;
1082 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001083 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001084 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001085 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001086 int hdr;
1087
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001088 /* if we don't want to log normal traffic, return now */
1089 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +02001090 (s->req->cons->conn_retries != be->conn_retries) ||
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001091 txn->status >= 500;
1092 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1093 return;
1094
Willy Tarreau42250582007-04-01 01:30:43 +02001095 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1096 return;
1097 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098
Emeric Brun3a058f32009-06-30 18:26:00 +02001099 if (prx_log->options2 & PR_O2_CLFLOG)
1100 return http_sess_clflog(s);
1101
Willy Tarreau42250582007-04-01 01:30:43 +02001102 if (s->cli_addr.ss_family == AF_INET)
1103 inet_ntop(AF_INET,
1104 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1105 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +02001106 else if (s->cli_addr.ss_family == AF_INET6)
Willy Tarreau42250582007-04-01 01:30:43 +02001107 inet_ntop(AF_INET6,
1108 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1109 pn, sizeof(pn));
1110
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001111 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001112
1113 /* FIXME: let's limit ourselves to frontend logging for now. */
1114 tolog = fe->to_log;
1115
1116 h = tmpline;
1117 if (fe->to_log & LW_REQHDR &&
1118 txn->req.cap &&
1119 (h < tmpline + sizeof(tmpline) - 10)) {
1120 *(h++) = ' ';
1121 *(h++) = '{';
1122 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1123 if (hdr)
1124 *(h++) = '|';
1125 if (txn->req.cap[hdr] != NULL)
1126 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1127 '#', hdr_encode_map, txn->req.cap[hdr]);
1128 }
1129 *(h++) = '}';
1130 }
1131
1132 if (fe->to_log & LW_RSPHDR &&
1133 txn->rsp.cap &&
1134 (h < tmpline + sizeof(tmpline) - 7)) {
1135 *(h++) = ' ';
1136 *(h++) = '{';
1137 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1138 if (hdr)
1139 *(h++) = '|';
1140 if (txn->rsp.cap[hdr] != NULL)
1141 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1142 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1143 }
1144 *(h++) = '}';
1145 }
1146
1147 if (h < tmpline + sizeof(tmpline) - 4) {
1148 *(h++) = ' ';
1149 *(h++) = '"';
1150 uri = txn->uri ? txn->uri : "<BADREQ>";
1151 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1152 '#', url_encode_map, uri);
1153 *(h++) = '"';
1154 }
1155 *h = '\0';
1156
1157 svid = (tolog & LW_SVID) ?
1158 (s->data_source != DATA_SRC_STATS) ?
1159 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1160
Willy Tarreau70089872008-06-13 21:12:51 +02001161 t_request = -1;
1162 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1163 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1164
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001165 level = LOG_INFO;
1166 if (err && (fe->options2 & PR_O2_LOGERRORS))
1167 level = LOG_ERR;
1168
1169 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001170 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001171 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1172 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Emeric Brun5bd86a82010-10-22 17:23:04 +02001173 (s->cli_addr.ss_family == AF_UNIX) ? "unix" : pn,
1174 (s->cli_addr.ss_family == AF_UNIX) ? s->listener->luid :
1175 ntohs((s->cli_addr.ss_family == AF_INET) ?
1176 ((struct sockaddr_in *)&s->cli_addr)->sin_port :
1177 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001178 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001179 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001180 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001181 t_request,
1182 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001183 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1184 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1185 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1186 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001187 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001188 txn->cli_cookie ? txn->cli_cookie : "-",
1189 txn->srv_cookie ? txn->srv_cookie : "-",
1190 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1191 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1192 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1193 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1194 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001195 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +02001196 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001197 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1198
1199 s->logs.logwait = 0;
1200}
1201
Willy Tarreau117f59e2007-03-04 18:17:17 +01001202
1203/*
1204 * Capture headers from message starting at <som> according to header list
1205 * <cap_hdr>, and fill the <idx> structure appropriately.
1206 */
1207void capture_headers(char *som, struct hdr_idx *idx,
1208 char **cap, struct cap_hdr *cap_hdr)
1209{
1210 char *eol, *sol, *col, *sov;
1211 int cur_idx;
1212 struct cap_hdr *h;
1213 int len;
1214
1215 sol = som + hdr_idx_first_pos(idx);
1216 cur_idx = hdr_idx_first_idx(idx);
1217
1218 while (cur_idx) {
1219 eol = sol + idx->v[cur_idx].len;
1220
1221 col = sol;
1222 while (col < eol && *col != ':')
1223 col++;
1224
1225 sov = col + 1;
1226 while (sov < eol && http_is_lws[(unsigned char)*sov])
1227 sov++;
1228
1229 for (h = cap_hdr; h; h = h->next) {
1230 if ((h->namelen == col - sol) &&
1231 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1232 if (cap[h->index] == NULL)
1233 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001234 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001235
1236 if (cap[h->index] == NULL) {
1237 Alert("HTTP capture : out of memory.\n");
1238 continue;
1239 }
1240
1241 len = eol - sov;
1242 if (len > h->len)
1243 len = h->len;
1244
1245 memcpy(cap[h->index], sov, len);
1246 cap[h->index][len]=0;
1247 }
1248 }
1249 sol = eol + idx->v[cur_idx].cr + 1;
1250 cur_idx = idx->v[cur_idx].next;
1251 }
1252}
1253
1254
Willy Tarreau42250582007-04-01 01:30:43 +02001255/* either we find an LF at <ptr> or we jump to <bad>.
1256 */
1257#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1258
1259/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1260 * otherwise to <http_msg_ood> with <state> set to <st>.
1261 */
1262#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1263 ptr++; \
1264 if (likely(ptr < end)) \
1265 goto good; \
1266 else { \
1267 state = (st); \
1268 goto http_msg_ood; \
1269 } \
1270 } while (0)
1271
1272
Willy Tarreaubaaee002006-06-26 02:48:02 +02001273/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001274 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001275 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1276 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1277 * will give undefined results.
1278 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1279 * and that msg->sol points to the beginning of the response.
1280 * If a complete line is found (which implies that at least one CR or LF is
1281 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1282 * returned indicating an incomplete line (which does not mean that parts have
1283 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1284 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1285 * upon next call.
1286 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001287 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001288 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1289 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001290 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001291 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001292const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1293 unsigned int state, const char *ptr, const char *end,
1294 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001295{
Willy Tarreau8973c702007-01-21 23:58:29 +01001296 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001297 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001298 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001299 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1301
1302 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001303 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001304 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1305 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001306 state = HTTP_MSG_ERROR;
1307 break;
1308
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001310 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001312 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001313 goto http_msg_rpcode;
1314 }
1315 if (likely(HTTP_IS_SPHT(*ptr)))
1316 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1317 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001318 state = HTTP_MSG_ERROR;
1319 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001320
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001322 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001323 if (likely(!HTTP_IS_LWS(*ptr)))
1324 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1325
1326 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001327 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001328 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1329 }
1330
1331 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001332 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 http_msg_rsp_reason:
1334 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001335 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 msg->sl.st.r_l = 0;
1337 goto http_msg_rpline_eol;
1338
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001340 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001342 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 goto http_msg_rpreason;
1344 }
1345 if (likely(HTTP_IS_SPHT(*ptr)))
1346 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1347 /* so it's a CR/LF, so there is no reason phrase */
1348 goto http_msg_rsp_reason;
1349
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001351 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 if (likely(!HTTP_IS_CRLF(*ptr)))
1353 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001354 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 http_msg_rpline_eol:
1356 /* We have seen the end of line. Note that we do not
1357 * necessarily have the \n yet, but at least we know that we
1358 * have EITHER \r OR \n, otherwise the response would not be
1359 * complete. We can then record the response length and return
1360 * to the caller which will be able to register it.
1361 */
1362 msg->sl.st.l = ptr - msg->sol;
1363 return ptr;
1364
1365#ifdef DEBUG_FULL
1366 default:
1367 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1368 exit(1);
1369#endif
1370 }
1371
1372 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001373 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001374 if (ret_state)
1375 *ret_state = state;
1376 if (ret_ptr)
1377 *ret_ptr = (char *)ptr;
1378 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001379}
1380
Willy Tarreau8973c702007-01-21 23:58:29 +01001381/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 * This function parses a request line between <ptr> and <end>, starting with
1383 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1384 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1385 * will give undefined results.
1386 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1387 * and that msg->sol points to the beginning of the request.
1388 * If a complete line is found (which implies that at least one CR or LF is
1389 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1390 * returned indicating an incomplete line (which does not mean that parts have
1391 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1392 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1393 * upon next call.
1394 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001395 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1397 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001398 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001399 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001400const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1401 unsigned int state, const char *ptr, const char *end,
1402 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001403{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001406 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 if (likely(HTTP_IS_TOKEN(*ptr)))
1408 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001409
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001411 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1413 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 if (likely(HTTP_IS_CRLF(*ptr))) {
1416 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001417 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001419 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001421 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001423 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 msg->sl.rq.v_l = 0;
1425 goto http_msg_rqline_eol;
1426 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001427 state = HTTP_MSG_ERROR;
1428 break;
1429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001431 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001433 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 goto http_msg_rquri;
1435 }
1436 if (likely(HTTP_IS_SPHT(*ptr)))
1437 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1438 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1439 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001440
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001442 http_msg_rquri:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 if (likely(!HTTP_IS_LWS(*ptr)))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001445
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001446 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001447 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1449 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1452 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001453
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001455 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001457 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 goto http_msg_rqver;
1459 }
1460 if (likely(HTTP_IS_SPHT(*ptr)))
1461 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1462 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1463 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001466 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001467 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001469
1470 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001471 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001472 http_msg_rqline_eol:
1473 /* We have seen the end of line. Note that we do not
1474 * necessarily have the \n yet, but at least we know that we
1475 * have EITHER \r OR \n, otherwise the request would not be
1476 * complete. We can then record the request length and return
1477 * to the caller which will be able to register it.
1478 */
1479 msg->sl.rq.l = ptr - msg->sol;
1480 return ptr;
1481 }
1482
1483 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001484 state = HTTP_MSG_ERROR;
1485 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487#ifdef DEBUG_FULL
1488 default:
1489 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1490 exit(1);
1491#endif
1492 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001493
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001495 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 if (ret_state)
1497 *ret_state = state;
1498 if (ret_ptr)
1499 *ret_ptr = (char *)ptr;
1500 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001502
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001503/*
1504 * Returns the data from Authorization header. Function may be called more
1505 * than once so data is stored in txn->auth_data. When no header is found
1506 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1507 * searching again for something we are unable to find anyway.
1508 */
1509
1510char get_http_auth_buff[BUFSIZE];
1511
1512int
1513get_http_auth(struct session *s)
1514{
1515
1516 struct http_txn *txn = &s->txn;
1517 struct chunk auth_method;
1518 struct hdr_ctx ctx;
1519 char *h, *p;
1520 int len;
1521
1522#ifdef DEBUG_AUTH
1523 printf("Auth for session %p: %d\n", s, txn->auth.method);
1524#endif
1525
1526 if (txn->auth.method == HTTP_AUTH_WRONG)
1527 return 0;
1528
1529 if (txn->auth.method)
1530 return 1;
1531
1532 txn->auth.method = HTTP_AUTH_WRONG;
1533
1534 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001535
1536 if (txn->flags & TX_USE_PX_CONN) {
1537 h = "Proxy-Authorization";
1538 len = strlen(h);
1539 } else {
1540 h = "Authorization";
1541 len = strlen(h);
1542 }
1543
1544 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001545 return 0;
1546
1547 h = ctx.line + ctx.val;
1548
1549 p = memchr(h, ' ', ctx.vlen);
1550 if (!p || p == h)
1551 return 0;
1552
1553 chunk_initlen(&auth_method, h, 0, p-h);
1554 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1555
1556 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1557
1558 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1559 get_http_auth_buff, BUFSIZE - 1);
1560
1561 if (len < 0)
1562 return 0;
1563
1564
1565 get_http_auth_buff[len] = '\0';
1566
1567 p = strchr(get_http_auth_buff, ':');
1568
1569 if (!p)
1570 return 0;
1571
1572 txn->auth.user = get_http_auth_buff;
1573 *p = '\0';
1574 txn->auth.pass = p+1;
1575
1576 txn->auth.method = HTTP_AUTH_BASIC;
1577 return 1;
1578 }
1579
1580 return 0;
1581}
1582
Willy Tarreau58f10d72006-12-04 02:26:12 +01001583
Willy Tarreau8973c702007-01-21 23:58:29 +01001584/*
1585 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001586 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001587 * when data are missing and recalled at the exact same location with no
1588 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001589 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001590 * fields. Note that msg->som and msg->sol will be initialized after completing
1591 * the first state, so that none of the msg pointers has to be initialized
1592 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001593 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1595{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001596 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001598
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001599 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 ptr = buf->lr;
1601 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 if (unlikely(ptr >= end))
1604 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001605
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001607 /*
1608 * First, states that are specific to the response only.
1609 * We check them first so that request and headers are
1610 * closer to each other (accessed more often).
1611 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001612 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001613 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001614 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001615 /* we have a start of message, but we have to check
1616 * first if we need to remove some CRLF. We can only
1617 * do this when send_max=0.
1618 */
1619 char *beg = buf->w + buf->send_max;
1620 if (beg >= buf->data + buf->size)
1621 beg -= buf->size;
1622 if (unlikely(ptr != beg)) {
1623 if (buf->send_max)
1624 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001625 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001626 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001627 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001628 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001629 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001630 hdr_idx_init(idx);
1631 state = HTTP_MSG_RPVER;
1632 goto http_msg_rpver;
1633 }
1634
1635 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1636 goto http_msg_invalid;
1637
1638 if (unlikely(*ptr == '\n'))
1639 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1640 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1641 /* stop here */
1642
Willy Tarreau8973c702007-01-21 23:58:29 +01001643 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001644 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001645 EXPECT_LF_HERE(ptr, http_msg_invalid);
1646 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1647 /* stop here */
1648
Willy Tarreau8973c702007-01-21 23:58:29 +01001649 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001650 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001651 case HTTP_MSG_RPVER_SP:
1652 case HTTP_MSG_RPCODE:
1653 case HTTP_MSG_RPCODE_SP:
1654 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001655 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001656 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001657 if (unlikely(!ptr))
1658 return;
1659
1660 /* we have a full response and we know that we have either a CR
1661 * or an LF at <ptr>.
1662 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001663 //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 +01001664 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1665
1666 msg->sol = ptr;
1667 if (likely(*ptr == '\r'))
1668 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1669 goto http_msg_rpline_end;
1670
Willy Tarreau8973c702007-01-21 23:58:29 +01001671 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001672 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001673 /* msg->sol must point to the first of CR or LF. */
1674 EXPECT_LF_HERE(ptr, http_msg_invalid);
1675 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1676 /* stop here */
1677
1678 /*
1679 * Second, states that are specific to the request only
1680 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001682 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001683 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001684 /* we have a start of message, but we have to check
1685 * first if we need to remove some CRLF. We can only
1686 * do this when send_max=0.
1687 */
1688 char *beg = buf->w + buf->send_max;
1689 if (beg >= buf->data + buf->size)
1690 beg -= buf->size;
1691 if (likely(ptr != beg)) {
1692 if (buf->send_max)
1693 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001694 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001695 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001696 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001697 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001698 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001699 /* we will need this when keep-alive will be supported
1700 hdr_idx_init(idx);
1701 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001702 state = HTTP_MSG_RQMETH;
1703 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001705
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1707 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001708
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 if (unlikely(*ptr == '\n'))
1710 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1711 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001712 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001713
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001715 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 EXPECT_LF_HERE(ptr, http_msg_invalid);
1717 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001718 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001719
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001721 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 case HTTP_MSG_RQMETH_SP:
1723 case HTTP_MSG_RQURI:
1724 case HTTP_MSG_RQURI_SP:
1725 case HTTP_MSG_RQVER:
1726 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001727 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 if (unlikely(!ptr))
1729 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 /* we have a full request and we know that we have either a CR
1732 * or an LF at <ptr>.
1733 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001734 //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 +01001735 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001736
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001737 msg->sol = ptr;
1738 if (likely(*ptr == '\r'))
1739 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001741
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001743 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 /* check for HTTP/0.9 request : no version information available.
1745 * msg->sol must point to the first of CR or LF.
1746 */
1747 if (unlikely(msg->sl.rq.v_l == 0))
1748 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001749
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001750 EXPECT_LF_HERE(ptr, http_msg_invalid);
1751 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001752 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001753
Willy Tarreau8973c702007-01-21 23:58:29 +01001754 /*
1755 * Common states below
1756 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001758 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 msg->sol = ptr;
1760 if (likely(!HTTP_IS_CRLF(*ptr))) {
1761 goto http_msg_hdr_name;
1762 }
1763
1764 if (likely(*ptr == '\r'))
1765 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1766 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001767
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001769 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001770 /* assumes msg->sol points to the first char */
1771 if (likely(HTTP_IS_TOKEN(*ptr)))
1772 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001773
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001774 if (likely(*ptr == ':')) {
1775 msg->col = ptr - buf->data;
1776 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1777 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001778
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001779 if (likely(msg->err_pos < -1) || *ptr == '\n')
1780 goto http_msg_invalid;
1781
1782 if (msg->err_pos == -1) /* capture error pointer */
1783 msg->err_pos = ptr - buf->data; /* >= 0 now */
1784
1785 /* and we still accept this non-token character */
1786 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001787
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001788 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001789 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 /* assumes msg->sol points to the first char and msg->col to the colon */
1791 if (likely(HTTP_IS_SPHT(*ptr)))
1792 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001793
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001794 /* header value can be basically anything except CR/LF */
1795 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001796
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 if (likely(!HTTP_IS_CRLF(*ptr))) {
1798 goto http_msg_hdr_val;
1799 }
1800
1801 if (likely(*ptr == '\r'))
1802 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1803 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001804
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001806 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001807 EXPECT_LF_HERE(ptr, http_msg_invalid);
1808 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001809
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001810 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001811 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001812 if (likely(HTTP_IS_SPHT(*ptr))) {
1813 /* replace HT,CR,LF with spaces */
1814 for (; buf->data+msg->sov < ptr; msg->sov++)
1815 buf->data[msg->sov] = ' ';
1816 goto http_msg_hdr_l1_sp;
1817 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001818 /* we had a header consisting only in spaces ! */
1819 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001820 goto http_msg_complete_header;
1821
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001823 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 /* assumes msg->sol points to the first char, msg->col to the
1825 * colon, and msg->sov points to the first character of the
1826 * value.
1827 */
1828 if (likely(!HTTP_IS_CRLF(*ptr)))
1829 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001830
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 msg->eol = ptr;
1832 /* Note: we could also copy eol into ->eoh so that we have the
1833 * real header end in case it ends with lots of LWS, but is this
1834 * really needed ?
1835 */
1836 if (likely(*ptr == '\r'))
1837 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1838 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001839
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001841 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001842 EXPECT_LF_HERE(ptr, http_msg_invalid);
1843 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001844
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001845 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001846 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001847 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1848 /* LWS: replace HT,CR,LF with spaces */
1849 for (; msg->eol < ptr; msg->eol++)
1850 *msg->eol = ' ';
1851 goto http_msg_hdr_val;
1852 }
1853 http_msg_complete_header:
1854 /*
1855 * It was a new header, so the last one is finished.
1856 * Assumes msg->sol points to the first char, msg->col to the
1857 * colon, msg->sov points to the first character of the value
1858 * and msg->eol to the first CR or LF so we know how the line
1859 * ends. We insert last header into the index.
1860 */
1861 /*
1862 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1863 write(2, msg->sol, msg->eol-msg->sol);
1864 fprintf(stderr,"\n");
1865 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001866
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001867 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1868 idx, idx->tail) < 0))
1869 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001870
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001871 msg->sol = ptr;
1872 if (likely(!HTTP_IS_CRLF(*ptr))) {
1873 goto http_msg_hdr_name;
1874 }
1875
1876 if (likely(*ptr == '\r'))
1877 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1878 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001879
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001880 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001881 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001882 /* Assumes msg->sol points to the first of either CR or LF */
1883 EXPECT_LF_HERE(ptr, http_msg_invalid);
1884 ptr++;
1885 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001886 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001888 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001889 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001890 return;
1891#ifdef DEBUG_FULL
1892 default:
1893 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1894 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001895#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 }
1897 http_msg_ood:
1898 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001899 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001900 buf->lr = ptr;
1901 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001902
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001903 http_msg_invalid:
1904 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001905 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001906 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001907 return;
1908}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001909
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001910/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1911 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1912 * nothing is done and 1 is returned.
1913 */
1914static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1915{
1916 int delta;
1917 char *cur_end;
1918
1919 if (msg->sl.rq.v_l != 0)
1920 return 1;
1921
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001922 cur_end = msg->sol + msg->sl.rq.l;
1923 delta = 0;
1924
1925 if (msg->sl.rq.u_l == 0) {
1926 /* if no URI was set, add "/" */
1927 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1928 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001929 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001930 }
1931 /* add HTTP version */
1932 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001933 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001934 cur_end += delta;
1935 cur_end = (char *)http_parse_reqline(msg, req->data,
1936 HTTP_MSG_RQMETH,
1937 msg->sol, cur_end + 1,
1938 NULL, NULL);
1939 if (unlikely(!cur_end))
1940 return 0;
1941
1942 /* we have a full HTTP/1.0 request now and we know that
1943 * we have either a CR or an LF at <ptr>.
1944 */
1945 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1946 return 1;
1947}
1948
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001949/* Parse the Connection: header of an HTTP request, looking for both "close"
1950 * and "keep-alive" values. If a buffer is provided and we already know that
1951 * some headers may safely be removed, we remove them now. The <to_del> flags
1952 * are used for that :
1953 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1954 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1955 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1956 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1957 * harmless combinations may be removed. Do not call that after changes have
1958 * been processed. If unused, the buffer can be NULL, and no data will be
1959 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001960 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001961void 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 +01001962{
Willy Tarreau5b154472009-12-21 20:11:07 +01001963 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001964 const char *hdr_val = "Connection";
1965 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001966
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001967 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001968 return;
1969
Willy Tarreau88d349d2010-01-25 12:15:43 +01001970 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1971 hdr_val = "Proxy-Connection";
1972 hdr_len = 16;
1973 }
1974
Willy Tarreau5b154472009-12-21 20:11:07 +01001975 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001976 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001977 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001978 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1979 txn->flags |= TX_HDR_CONN_KAL;
1980 if ((to_del & 2) && buf)
1981 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1982 else
1983 txn->flags |= TX_CON_KAL_SET;
1984 }
1985 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1986 txn->flags |= TX_HDR_CONN_CLO;
1987 if ((to_del & 1) && buf)
1988 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1989 else
1990 txn->flags |= TX_CON_CLO_SET;
1991 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001992 }
1993
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001994 txn->flags |= TX_HDR_CONN_PRS;
1995 return;
1996}
Willy Tarreau5b154472009-12-21 20:11:07 +01001997
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001998/* Apply desired changes on the Connection: header. Values may be removed and/or
1999 * added depending on the <wanted> flags, which are exclusively composed of
2000 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2001 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2002 */
2003void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
2004{
2005 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002006 const char *hdr_val = "Connection";
2007 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002008
2009 ctx.idx = 0;
2010
Willy Tarreau88d349d2010-01-25 12:15:43 +01002011
2012 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2013 hdr_val = "Proxy-Connection";
2014 hdr_len = 16;
2015 }
2016
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002017 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002018 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002019 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2020 if (wanted & TX_CON_KAL_SET)
2021 txn->flags |= TX_CON_KAL_SET;
2022 else
2023 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002024 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002025 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2026 if (wanted & TX_CON_CLO_SET)
2027 txn->flags |= TX_CON_CLO_SET;
2028 else
2029 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002030 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002031 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002032
2033 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2034 return;
2035
2036 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2037 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002038 hdr_val = "Connection: close";
2039 hdr_len = 17;
2040 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2041 hdr_val = "Proxy-Connection: close";
2042 hdr_len = 23;
2043 }
2044 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002045 }
2046
2047 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2048 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002049 hdr_val = "Connection: keep-alive";
2050 hdr_len = 22;
2051 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2052 hdr_val = "Proxy-Connection: keep-alive";
2053 hdr_len = 28;
2054 }
2055 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002056 }
2057 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002058}
2059
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2061 * first byte of body, and increments msg->sov by the number of bytes parsed,
2062 * so that we know we can forward between ->som and ->sov. Note that due to
2063 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2064 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002065 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002066 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002067 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002068int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002069{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070 char *ptr = buf->lr;
2071 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002072 unsigned int chunk = 0;
2073
2074 /* The chunk size is in the following form, though we are only
2075 * interested in the size and CRLF :
2076 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2077 */
2078 while (1) {
2079 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002080 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002081 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002082 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002083 if (c < 0) /* not a hex digit anymore */
2084 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085 if (++ptr >= end)
2086 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002087 if (chunk & 0xF000000) /* overflow will occur */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002088 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002089 chunk = (chunk << 4) + c;
2090 }
2091
Willy Tarreaud98cf932009-12-27 22:54:55 +01002092 /* empty size not allowed */
2093 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002094 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095
2096 while (http_is_spht[(unsigned char)*ptr]) {
2097 if (++ptr >= end)
2098 ptr = buf->data;
2099 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002100 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002101 }
2102
Willy Tarreaud98cf932009-12-27 22:54:55 +01002103 /* Up to there, we know that at least one byte is present at *ptr. Check
2104 * for the end of chunk size.
2105 */
2106 while (1) {
2107 if (likely(HTTP_IS_CRLF(*ptr))) {
2108 /* we now have a CR or an LF at ptr */
2109 if (likely(*ptr == '\r')) {
2110 if (++ptr >= end)
2111 ptr = buf->data;
2112 if (ptr == buf->r)
2113 return 0;
2114 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002115
Willy Tarreaud98cf932009-12-27 22:54:55 +01002116 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002117 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002118 if (++ptr >= end)
2119 ptr = buf->data;
2120 /* done */
2121 break;
2122 }
2123 else if (*ptr == ';') {
2124 /* chunk extension, ends at next CRLF */
2125 if (++ptr >= end)
2126 ptr = buf->data;
2127 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002128 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002129
2130 while (!HTTP_IS_CRLF(*ptr)) {
2131 if (++ptr >= end)
2132 ptr = buf->data;
2133 if (ptr == buf->r)
2134 return 0;
2135 }
2136 /* we have a CRLF now, loop above */
2137 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002138 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002139 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002140 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002141 }
2142
Willy Tarreaud98cf932009-12-27 22:54:55 +01002143 /* OK we found our CRLF and now <ptr> points to the next byte,
2144 * which may or may not be present. We save that into ->lr and
2145 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002146 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002147 msg->sov += ptr - buf->lr;
2148 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01002149 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002150 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002151 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002152 error:
2153 msg->err_pos = ptr - buf->data;
2154 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002155}
2156
Willy Tarreaud98cf932009-12-27 22:54:55 +01002157/* This function skips trailers in the buffer <buf> associated with HTTP
2158 * message <msg>. The first visited position is buf->lr. If the end of
2159 * the trailers is found, it is automatically scheduled to be forwarded,
2160 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2161 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002162 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2163 * zero. If a parse error is encountered, the function returns < 0 and does not
2164 * change anything except maybe buf->lr and msg->sov. Note that the message
2165 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2166 * which implies that all non-trailers data have already been scheduled for
2167 * forwarding, and that the difference between msg->som and msg->sov exactly
2168 * matches the length of trailers already parsed and not forwarded. It is also
2169 * important to note that this function is designed to be able to parse wrapped
2170 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002171 */
2172int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2173{
2174 /* we have buf->lr which points to next line. Look for CRLF. */
2175 while (1) {
2176 char *p1 = NULL, *p2 = NULL;
2177 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002178 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002179
2180 /* scan current line and stop at LF or CRLF */
2181 while (1) {
2182 if (ptr == buf->r)
2183 return 0;
2184
2185 if (*ptr == '\n') {
2186 if (!p1)
2187 p1 = ptr;
2188 p2 = ptr;
2189 break;
2190 }
2191
2192 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002193 if (p1) {
2194 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002195 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002196 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 p1 = ptr;
2198 }
2199
2200 ptr++;
2201 if (ptr >= buf->data + buf->size)
2202 ptr = buf->data;
2203 }
2204
2205 /* after LF; point to beginning of next line */
2206 p2++;
2207 if (p2 >= buf->data + buf->size)
2208 p2 = buf->data;
2209
Willy Tarreau638cd022010-01-03 07:42:04 +01002210 bytes = p2 - buf->lr;
2211 if (bytes < 0)
2212 bytes += buf->size;
2213
2214 /* schedule this line for forwarding */
2215 msg->sov += bytes;
2216 if (msg->sov >= buf->size)
2217 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002218
Willy Tarreau638cd022010-01-03 07:42:04 +01002219 if (p1 == buf->lr) {
2220 /* LF/CRLF at beginning of line => end of trailers at p2.
2221 * Everything was scheduled for forwarding, there's nothing
2222 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002223 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002224 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002225 msg->msg_state = HTTP_MSG_DONE;
2226 return 1;
2227 }
2228 /* OK, next line then */
2229 buf->lr = p2;
2230 }
2231}
2232
2233/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2234 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2235 * ->som, buf->lr in order to include this part into the next forwarding phase.
2236 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2237 * not enough data are available, the function does not change anything and
2238 * returns zero. If a parse error is encountered, the function returns < 0 and
2239 * does not change anything. Note: this function is designed to parse wrapped
2240 * CRLF at the end of the buffer.
2241 */
2242int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2243{
2244 char *ptr;
2245 int bytes;
2246
2247 /* NB: we'll check data availabilty at the end. It's not a
2248 * problem because whatever we match first will be checked
2249 * against the correct length.
2250 */
2251 bytes = 1;
2252 ptr = buf->lr;
2253 if (*ptr == '\r') {
2254 bytes++;
2255 ptr++;
2256 if (ptr >= buf->data + buf->size)
2257 ptr = buf->data;
2258 }
2259
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002260 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002261 return 0;
2262
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002263 if (*ptr != '\n') {
2264 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002265 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002266 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002267
2268 ptr++;
2269 if (ptr >= buf->data + buf->size)
2270 ptr = buf->data;
2271 buf->lr = ptr;
2272 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2273 msg->sov = ptr - buf->data;
2274 msg->som = msg->sov - bytes;
2275 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2276 return 1;
2277}
Willy Tarreau5b154472009-12-21 20:11:07 +01002278
Willy Tarreau83e3af02009-12-28 17:39:57 +01002279void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2280{
2281 char *end = buf->data + buf->size;
2282 int off = buf->data + buf->size - buf->w;
2283
2284 /* two possible cases :
2285 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002286 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002287 */
2288 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002289 int block1 = buf->l;
2290 int block2 = 0;
2291 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002292 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002293 block1 = buf->data + buf->size - buf->w;
2294 block2 = buf->r - buf->data;
2295 }
2296 if (block2)
2297 memcpy(swap_buffer, buf->data, block2);
2298 memmove(buf->data, buf->w, block1);
2299 if (block2)
2300 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002301 }
2302
2303 /* adjust all known pointers */
2304 buf->w = buf->data;
2305 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2306 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2307 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2308 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2309
2310 /* adjust relative pointers */
2311 msg->som = 0;
2312 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2313 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2314 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2315
Willy Tarreau83e3af02009-12-28 17:39:57 +01002316 if (msg->err_pos >= 0) {
2317 msg->err_pos += off;
2318 if (msg->err_pos >= buf->size)
2319 msg->err_pos -= buf->size;
2320 }
2321
2322 buf->flags &= ~BF_FULL;
2323 if (buf->l >= buffer_max_len(buf))
2324 buf->flags |= BF_FULL;
2325}
2326
Willy Tarreaud787e662009-07-07 10:14:51 +02002327/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2328 * processing can continue on next analysers, or zero if it either needs more
2329 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2330 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2331 * when it has nothing left to do, and may remove any analyser when it wants to
2332 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002333 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002334int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002335{
Willy Tarreau59234e92008-11-30 23:51:27 +01002336 /*
2337 * We will parse the partial (or complete) lines.
2338 * We will check the request syntax, and also join multi-line
2339 * headers. An index of all the lines will be elaborated while
2340 * parsing.
2341 *
2342 * For the parsing, we use a 28 states FSM.
2343 *
2344 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002345 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002346 * req->data + msg->eoh = end of processed headers / start of current one
2347 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 * req->lr = first non-visited byte
2349 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002350 *
2351 * At end of parsing, we may perform a capture of the error (if any), and
2352 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2353 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2354 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002356
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002358 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002359 struct http_txn *txn = &s->txn;
2360 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002361 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002362
Willy Tarreau6bf17362009-02-24 10:48:35 +01002363 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2364 now_ms, __FUNCTION__,
2365 s,
2366 req,
2367 req->rex, req->wex,
2368 req->flags,
2369 req->l,
2370 req->analysers);
2371
Willy Tarreau52a0c602009-08-16 22:45:38 +02002372 /* we're speaking HTTP here, so let's speak HTTP to the client */
2373 s->srv_error = http_return_srv_error;
2374
Willy Tarreau83e3af02009-12-28 17:39:57 +01002375 /* There's a protected area at the end of the buffer for rewriting
2376 * purposes. We don't want to start to parse the request if the
2377 * protected area is affected, because we may have to move processed
2378 * data later, which is much more complicated.
2379 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002380 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002381 if ((txn->flags & TX_NOT_FIRST) &&
2382 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002383 req->r < req->lr ||
2384 req->r > req->data + req->size - global.tune.maxrewrite)) {
2385 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002386 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2387 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002388 /* some data has still not left the buffer, wake us once that's done */
2389 buffer_dont_connect(req);
2390 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2391 return 0;
2392 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002393 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002394 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002395 }
2396
Willy Tarreau065e8332010-01-08 00:30:20 +01002397 /* Note that we have the same problem with the response ; we
2398 * may want to send a redirect, error or anything which requires
2399 * some spare space. So we'll ensure that we have at least
2400 * maxrewrite bytes available in the response buffer before
2401 * processing that one. This will only affect pipelined
2402 * keep-alive requests.
2403 */
2404 if ((txn->flags & TX_NOT_FIRST) &&
2405 unlikely((s->rep->flags & BF_FULL) ||
2406 s->rep->r < s->rep->lr ||
2407 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2408 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002409 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2410 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002411 /* don't let a connection request be initiated */
2412 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002413 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002414 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002415 return 0;
2416 }
2417 }
2418
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002419 if (likely(req->lr < req->r))
2420 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002421 }
2422
Willy Tarreau59234e92008-11-30 23:51:27 +01002423 /* 1: we might have to print this header in debug mode */
2424 if (unlikely((global.mode & MODE_DEBUG) &&
2425 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002426 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002427 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002428 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002429
Willy Tarreau663308b2010-06-07 14:06:08 +02002430 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002431 eol = sol + msg->sl.rq.l;
2432 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002433
Willy Tarreau59234e92008-11-30 23:51:27 +01002434 sol += hdr_idx_first_pos(&txn->hdr_idx);
2435 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002436
Willy Tarreau59234e92008-11-30 23:51:27 +01002437 while (cur_idx) {
2438 eol = sol + txn->hdr_idx.v[cur_idx].len;
2439 debug_hdr("clihdr", s, sol, eol);
2440 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2441 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002442 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002443 }
2444
Willy Tarreau58f10d72006-12-04 02:26:12 +01002445
Willy Tarreau59234e92008-11-30 23:51:27 +01002446 /*
2447 * Now we quickly check if we have found a full valid request.
2448 * If not so, we check the FD and buffer states before leaving.
2449 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002450 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002451 * requests are checked first. When waiting for a second request
2452 * on a keep-alive session, if we encounter and error, close, t/o,
2453 * we note the error in the session flags but don't set any state.
2454 * Since the error will be noted there, it will not be counted by
2455 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002456 * Last, we may increase some tracked counters' http request errors on
2457 * the cases that are deliberately the client's fault. For instance,
2458 * a timeout or connection reset is not counted as an error. However
2459 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002460 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002461
Willy Tarreau655dce92009-11-08 13:10:58 +01002462 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002463 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002464 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002465 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002466 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002467 session_inc_http_req_ctr(s);
2468 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002469 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002470 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002471 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002472
Willy Tarreau59234e92008-11-30 23:51:27 +01002473 /* 1: Since we are in header mode, if there's no space
2474 * left for headers, we won't be able to free more
2475 * later, so the session will never terminate. We
2476 * must terminate it now.
2477 */
2478 if (unlikely(req->flags & BF_FULL)) {
2479 /* FIXME: check if URI is set and return Status
2480 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002481 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002482 session_inc_http_req_ctr(s);
2483 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002484 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002485 goto return_bad_req;
2486 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002487
Willy Tarreau59234e92008-11-30 23:51:27 +01002488 /* 2: have we encountered a read error ? */
2489 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002490 if (!(s->flags & SN_ERR_MASK))
2491 s->flags |= SN_ERR_CLICL;
2492
Willy Tarreaufcffa692010-01-10 14:21:19 +01002493 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002494 goto failed_keep_alive;
2495
Willy Tarreau59234e92008-11-30 23:51:27 +01002496 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002497 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002498 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002499 session_inc_http_err_ctr(s);
2500 }
2501
Willy Tarreau59234e92008-11-30 23:51:27 +01002502 msg->msg_state = HTTP_MSG_ERROR;
2503 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002504
Willy Tarreauda7ff642010-06-23 11:44:09 +02002505 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002506 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002507 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002508 if (s->listener->counters)
2509 s->listener->counters->failed_req++;
2510
Willy Tarreau59234e92008-11-30 23:51:27 +01002511 if (!(s->flags & SN_FINST_MASK))
2512 s->flags |= SN_FINST_R;
2513 return 0;
2514 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002515
Willy Tarreau59234e92008-11-30 23:51:27 +01002516 /* 3: has the read timeout expired ? */
2517 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002518 if (!(s->flags & SN_ERR_MASK))
2519 s->flags |= SN_ERR_CLITO;
2520
Willy Tarreaufcffa692010-01-10 14:21:19 +01002521 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002522 goto failed_keep_alive;
2523
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002525 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002526 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002527 session_inc_http_err_ctr(s);
2528 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002529 txn->status = 408;
2530 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2531 msg->msg_state = HTTP_MSG_ERROR;
2532 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002533
Willy Tarreauda7ff642010-06-23 11:44:09 +02002534 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002535 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002536 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002537 if (s->listener->counters)
2538 s->listener->counters->failed_req++;
2539
Willy Tarreau59234e92008-11-30 23:51:27 +01002540 if (!(s->flags & SN_FINST_MASK))
2541 s->flags |= SN_FINST_R;
2542 return 0;
2543 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002544
Willy Tarreau59234e92008-11-30 23:51:27 +01002545 /* 4: have we encountered a close ? */
2546 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002547 if (!(s->flags & SN_ERR_MASK))
2548 s->flags |= SN_ERR_CLICL;
2549
Willy Tarreaufcffa692010-01-10 14:21:19 +01002550 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002551 goto failed_keep_alive;
2552
Willy Tarreau4076a152009-04-02 15:18:36 +02002553 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002554 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002555 txn->status = 400;
2556 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
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_err_ctr(s);
2561 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002562 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002563 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002564 if (s->listener->counters)
2565 s->listener->counters->failed_req++;
2566
Willy Tarreau59234e92008-11-30 23:51:27 +01002567 if (!(s->flags & SN_FINST_MASK))
2568 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002569 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002570 }
2571
Willy Tarreau520d95e2009-09-19 21:04:57 +02002572 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002573 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002574 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002575
Willy Tarreaufcffa692010-01-10 14:21:19 +01002576 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2577 /* If the client starts to talk, let's fall back to
2578 * request timeout processing.
2579 */
2580 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002581 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002582 }
2583
Willy Tarreau59234e92008-11-30 23:51:27 +01002584 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002585 if (!tick_isset(req->analyse_exp)) {
2586 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2587 (txn->flags & TX_WAIT_NEXT_RQ) &&
2588 tick_isset(s->be->timeout.httpka))
2589 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2590 else
2591 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2592 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002593
Willy Tarreau59234e92008-11-30 23:51:27 +01002594 /* we're not ready yet */
2595 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002596
2597 failed_keep_alive:
2598 /* Here we process low-level errors for keep-alive requests. In
2599 * short, if the request is not the first one and it experiences
2600 * a timeout, read error or shutdown, we just silently close so
2601 * that the client can try again.
2602 */
2603 txn->status = 0;
2604 msg->msg_state = HTTP_MSG_RQBEFORE;
2605 req->analysers = 0;
2606 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002607 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002608 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002609 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002610 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002611
Willy Tarreaud787e662009-07-07 10:14:51 +02002612 /* OK now we have a complete HTTP request with indexed headers. Let's
2613 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002614 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2615 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2616 * points to the CRLF of the request line. req->lr points to the first
2617 * byte after the last LF. msg->col and msg->sov point to the first
2618 * byte of data. msg->eol cannot be trusted because it may have been
2619 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002620 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002621
Willy Tarreauda7ff642010-06-23 11:44:09 +02002622 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002623 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2624
Willy Tarreaub16a5742010-01-10 14:46:16 +01002625 if (txn->flags & TX_WAIT_NEXT_RQ) {
2626 /* kill the pending keep-alive timeout */
2627 txn->flags &= ~TX_WAIT_NEXT_RQ;
2628 req->analyse_exp = TICK_ETERNITY;
2629 }
2630
2631
Willy Tarreaud787e662009-07-07 10:14:51 +02002632 /* Maybe we found in invalid header name while we were configured not
2633 * to block on that, so we have to capture it now.
2634 */
2635 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002636 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002637
Willy Tarreau59234e92008-11-30 23:51:27 +01002638 /*
2639 * 1: identify the method
2640 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002641 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002642
2643 /* we can make use of server redirect on GET and HEAD */
2644 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2645 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002646
Willy Tarreau59234e92008-11-30 23:51:27 +01002647 /*
2648 * 2: check if the URI matches the monitor_uri.
2649 * We have to do this for every request which gets in, because
2650 * the monitor-uri is defined by the frontend.
2651 */
2652 if (unlikely((s->fe->monitor_uri_len != 0) &&
2653 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002654 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002655 s->fe->monitor_uri,
2656 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002657 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002658 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002659 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002661
Willy Tarreau59234e92008-11-30 23:51:27 +01002662 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002663
Willy Tarreau59234e92008-11-30 23:51:27 +01002664 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002665 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2666 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002667
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 ret = acl_pass(ret);
2669 if (cond->pol == ACL_COND_UNLESS)
2670 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002671
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 if (ret) {
2673 /* we fail this request, let's return 503 service unavail */
2674 txn->status = 503;
2675 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2676 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002677 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002678 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002679
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 /* nothing to fail, let's reply normaly */
2681 txn->status = 200;
2682 stream_int_retnclose(req->prod, &http_200_chunk);
2683 goto return_prx_cond;
2684 }
2685
2686 /*
2687 * 3: Maybe we have to copy the original REQURI for the logs ?
2688 * Note: we cannot log anymore if the request has been
2689 * classified as invalid.
2690 */
2691 if (unlikely(s->logs.logwait & LW_REQ)) {
2692 /* we have a complete HTTP request that we must log */
2693 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2694 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002695
Willy Tarreau59234e92008-11-30 23:51:27 +01002696 if (urilen >= REQURI_LEN)
2697 urilen = REQURI_LEN - 1;
2698 memcpy(txn->uri, &req->data[msg->som], urilen);
2699 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002700
Willy Tarreau59234e92008-11-30 23:51:27 +01002701 if (!(s->logs.logwait &= ~LW_REQ))
2702 s->do_log(s);
2703 } else {
2704 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002705 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002706 }
Willy Tarreau06619262006-12-17 08:37:22 +01002707
Willy Tarreau59234e92008-11-30 23:51:27 +01002708 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002709 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2710 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002711
Willy Tarreau5b154472009-12-21 20:11:07 +01002712 /* ... and check if the request is HTTP/1.1 or above */
2713 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002714 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2715 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2716 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002717 txn->flags |= TX_REQ_VER_11;
2718
2719 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002720 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002721
Willy Tarreau88d349d2010-01-25 12:15:43 +01002722 /* if the frontend has "option http-use-proxy-header", we'll check if
2723 * we have what looks like a proxied connection instead of a connection,
2724 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2725 * Note that this is *not* RFC-compliant, however browsers and proxies
2726 * happen to do that despite being non-standard :-(
2727 * We consider that a request not beginning with either '/' or '*' is
2728 * a proxied connection, which covers both "scheme://location" and
2729 * CONNECT ip:port.
2730 */
2731 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2732 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2733 txn->flags |= TX_USE_PX_CONN;
2734
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002735 /* transfer length unknown*/
2736 txn->flags &= ~TX_REQ_XFER_LEN;
2737
Willy Tarreau59234e92008-11-30 23:51:27 +01002738 /* 5: we may need to capture headers */
2739 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002740 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002741 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002742
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002743 /* 6: determine the transfer-length.
2744 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2745 * the presence of a message-body in a REQUEST and its transfer length
2746 * must be determined that way (in order of precedence) :
2747 * 1. The presence of a message-body in a request is signaled by the
2748 * inclusion of a Content-Length or Transfer-Encoding header field
2749 * in the request's header fields. When a request message contains
2750 * both a message-body of non-zero length and a method that does
2751 * not define any semantics for that request message-body, then an
2752 * origin server SHOULD either ignore the message-body or respond
2753 * with an appropriate error message (e.g., 413). A proxy or
2754 * gateway, when presented the same request, SHOULD either forward
2755 * the request inbound with the message- body or ignore the
2756 * message-body when determining a response.
2757 *
2758 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2759 * and the "chunked" transfer-coding (Section 6.2) is used, the
2760 * transfer-length is defined by the use of this transfer-coding.
2761 * If a Transfer-Encoding header field is present and the "chunked"
2762 * transfer-coding is not present, the transfer-length is defined
2763 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002764 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002765 * 3. If a Content-Length header field is present, its decimal value in
2766 * OCTETs represents both the entity-length and the transfer-length.
2767 * If a message is received with both a Transfer-Encoding header
2768 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002769 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002770 * 4. By the server closing the connection. (Closing the connection
2771 * cannot be used to indicate the end of a request body, since that
2772 * would leave no possibility for the server to send back a response.)
2773 *
2774 * Whenever a transfer-coding is applied to a message-body, the set of
2775 * transfer-codings MUST include "chunked", unless the message indicates
2776 * it is terminated by closing the connection. When the "chunked"
2777 * transfer-coding is used, it MUST be the last transfer-coding applied
2778 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002779 */
2780
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002781 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002782 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002783 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002784 while ((txn->flags & TX_REQ_VER_11) &&
2785 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002786 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2787 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2788 else if (txn->flags & TX_REQ_TE_CHNK) {
2789 /* bad transfer-encoding (chunked followed by something else) */
2790 use_close_only = 1;
2791 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2792 break;
2793 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002794 }
2795
Willy Tarreau32b47f42009-10-18 20:55:02 +02002796 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002797 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002798 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2799 signed long long cl;
2800
2801 if (!ctx.vlen)
2802 goto return_bad_req;
2803
2804 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2805 goto return_bad_req; /* parse failure */
2806
2807 if (cl < 0)
2808 goto return_bad_req;
2809
2810 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2811 goto return_bad_req; /* already specified, was different */
2812
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002813 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002814 msg->hdr_content_len = cl;
2815 }
2816
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002817 /* bodyless requests have a known length */
2818 if (!use_close_only)
2819 txn->flags |= TX_REQ_XFER_LEN;
2820
Willy Tarreaud787e662009-07-07 10:14:51 +02002821 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002822 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002823 req->analyse_exp = TICK_ETERNITY;
2824 return 1;
2825
2826 return_bad_req:
2827 /* We centralize bad requests processing here */
2828 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2829 /* we detected a parsing error. We want to archive this request
2830 * in the dedicated proxy area for later troubleshooting.
2831 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002832 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002833 }
2834
2835 txn->req.msg_state = HTTP_MSG_ERROR;
2836 txn->status = 400;
2837 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002838
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002839 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002840 if (s->listener->counters)
2841 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002842
2843 return_prx_cond:
2844 if (!(s->flags & SN_ERR_MASK))
2845 s->flags |= SN_ERR_PRXCOND;
2846 if (!(s->flags & SN_FINST_MASK))
2847 s->flags |= SN_FINST_R;
2848
2849 req->analysers = 0;
2850 req->analyse_exp = TICK_ETERNITY;
2851 return 0;
2852}
2853
Cyril Bonté70be45d2010-10-12 00:14:35 +02002854/* We reached the stats page through a POST request.
2855 * Parse the posted data and enable/disable servers if necessary.
2856 * Returns 0 if request was parsed.
2857 * Returns 1 if there was a problem parsing the posted data.
2858 */
2859int http_process_req_stat_post(struct session *s, struct buffer *req)
2860{
2861 struct http_txn *txn = &s->txn;
2862 struct proxy *px;
2863 struct server *sv;
2864
2865 char *backend = NULL;
2866 int action = 0;
2867
2868 char *first_param, *cur_param, *next_param, *end_params;
2869
2870 first_param = req->data + txn->req.eoh + 2;
2871 end_params = first_param + txn->req.hdr_content_len;
2872
2873 cur_param = next_param = end_params;
2874
2875 if (end_params >= req->data + req->size) {
2876 /* Prevent buffer overflow */
2877 s->data_ctx.stats.st_code = STAT_STATUS_EXCD;
2878 return 1;
2879 }
2880 else if (end_params > req->data + req->l) {
2881 /* This condition also rejects a request with Expect: 100-continue */
2882 s->data_ctx.stats.st_code = STAT_STATUS_EXCD;
2883 return 1;
2884 }
2885
2886 *end_params = '\0';
2887
2888 s->data_ctx.stats.st_code = STAT_STATUS_NONE;
2889
2890 /*
2891 * Parse the parameters in reverse order to only store the last value.
2892 * From the html form, the backend and the action are at the end.
2893 */
2894 while (cur_param > first_param) {
2895 char *key, *value;
2896
2897 cur_param--;
2898 if ((*cur_param == '&') || (cur_param == first_param)) {
2899 /* Parse the key */
2900 key = cur_param;
2901 if (cur_param != first_param) {
2902 /* delimit the string for the next loop */
2903 *key++ = '\0';
2904 }
2905
2906 /* Parse the value */
2907 value = key;
2908 while (*value != '\0' && *value != '=') {
2909 value++;
2910 }
2911 if (*value == '=') {
2912 /* Ok, a value is found, we can mark the end of the key */
2913 *value++ = '\0';
2914 }
2915
2916 /* Now we can check the key to see what to do */
2917 if (!backend && strcmp(key, "b") == 0) {
2918 backend = value;
2919 }
2920 else if (!action && strcmp(key, "action") == 0) {
2921 if (strcmp(value, "disable") == 0) {
2922 action = 1;
2923 }
2924 else if (strcmp(value, "enable") == 0) {
2925 action = 2;
2926 } else {
2927 /* unknown action, no need to continue */
2928 break;
2929 }
2930 }
2931 else if (strcmp(key, "s") == 0) {
2932 if (backend && action && get_backend_server(backend, value, &px, &sv)) {
2933 switch (action) {
2934 case 1:
2935 if (! (sv->state & SRV_MAINTAIN)) {
2936 /* Not already in maintenance, we can change the server state */
2937 sv->state |= SRV_MAINTAIN;
2938 set_server_down(sv);
2939 s->data_ctx.stats.st_code = STAT_STATUS_DONE;
2940 }
2941 break;
2942 case 2:
2943 if ((sv->state & SRV_MAINTAIN)) {
2944 /* Already in maintenance, we can change the server state */
2945 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002946 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002947 s->data_ctx.stats.st_code = STAT_STATUS_DONE;
2948 }
2949 break;
2950 }
2951 }
2952 }
2953 next_param = cur_param;
2954 }
2955 }
2956 return 0;
2957}
2958
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002959/* This stream analyser runs all HTTP request processing which is common to
2960 * frontends and backends, which means blocking ACLs, filters, connection-close,
2961 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002962 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002963 * either needs more data or wants to immediately abort the request (eg: deny,
2964 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002965 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002966int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002967{
Willy Tarreaud787e662009-07-07 10:14:51 +02002968 struct http_txn *txn = &s->txn;
2969 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002970 struct acl_cond *cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002971 struct req_acl_rule *req_acl, *req_acl_final = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002972 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002973 struct cond_wordlist *wl;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002974 int del_ka, del_cl, do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002975
Willy Tarreau655dce92009-11-08 13:10:58 +01002976 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002977 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002978 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002979 return 0;
2980 }
2981
Willy Tarreau3a816292009-07-07 10:55:49 +02002982 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002983 req->analyse_exp = TICK_ETERNITY;
2984
2985 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2986 now_ms, __FUNCTION__,
2987 s,
2988 req,
2989 req->rex, req->wex,
2990 req->flags,
2991 req->l,
2992 req->analysers);
2993
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002994 /* first check whether we have some ACLs set to block this request */
2995 list_for_each_entry(cond, &px->block_cond, list) {
2996 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002997
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002998 ret = acl_pass(ret);
2999 if (cond->pol == ACL_COND_UNLESS)
3000 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003001
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003002 if (ret) {
3003 txn->status = 403;
3004 /* let's log the request time */
3005 s->logs.tv_request = now;
3006 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003007 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003008 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003009 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003010 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003011
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003012 do_stats = stats_check_uri(s, px);
3013
3014 list_for_each_entry(req_acl, (do_stats?&px->uri_auth->req_acl:&px->req_acl), list) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003015 int ret = 1;
3016
3017 if (req_acl->action >= PR_REQ_ACL_ACT_MAX)
3018 continue;
3019
3020 /* check condition, but only if attached */
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01003021 if (req_acl->cond) {
3022 ret = acl_exec_cond(req_acl->cond, px, s, txn, ACL_DIR_REQ);
3023 ret = acl_pass(ret);
Willy Tarreau51425942010-02-01 10:40:19 +01003024
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01003025 if (req_acl->cond->pol == ACL_COND_UNLESS)
3026 ret = !ret;
3027 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003028
3029 if (ret) {
3030 req_acl_final = req_acl;
3031 break;
3032 }
3033 }
3034
3035 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_DENY) {
3036 txn->status = 403;
3037 s->logs.tv_request = now;
3038 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003039 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003040 goto return_prx_cond;
3041 }
3042
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003043 /* try headers filters */
3044 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003045 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003046 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003047
Willy Tarreau59234e92008-11-30 23:51:27 +01003048 /* has the request been denied ? */
3049 if (txn->flags & TX_CLDENY) {
3050 /* no need to go further */
3051 txn->status = 403;
3052 /* let's log the request time */
3053 s->logs.tv_request = now;
3054 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003055 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003056 goto return_prx_cond;
3057 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003058
3059 /* When a connection is tarpitted, we use the tarpit timeout,
3060 * which may be the same as the connect timeout if unspecified.
3061 * If unset, then set it to zero because we really want it to
3062 * eventually expire. We build the tarpit as an analyser.
3063 */
3064 if (txn->flags & TX_CLTARPIT) {
3065 buffer_erase(s->req);
3066 /* wipe the request out so that we can drop the connection early
3067 * if the client closes first.
3068 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003069 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003070 req->analysers = 0; /* remove switching rules etc... */
3071 req->analysers |= AN_REQ_HTTP_TARPIT;
3072 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3073 if (!req->analyse_exp)
3074 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003075 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003076 return 1;
3077 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003078 }
Willy Tarreau06619262006-12-17 08:37:22 +01003079
Willy Tarreau5b154472009-12-21 20:11:07 +01003080 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3081 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003082 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3083 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003084 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3085 * avoid to redo the same work if FE and BE have the same settings (common).
3086 * The method consists in checking if options changed between the two calls
3087 * (implying that either one is non-null, or one of them is non-null and we
3088 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003089 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003090
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003091 del_cl = del_ka = 0;
3092
Willy Tarreaudc008c52010-02-01 16:20:08 +01003093 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3094 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3095 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3096 (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 +01003097 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003098
Willy Tarreau5b154472009-12-21 20:11:07 +01003099 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE)
3100 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003101 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3102 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003103 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003104 tmp = TX_CON_WANT_CLO;
3105
Willy Tarreau5b154472009-12-21 20:11:07 +01003106 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3107 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003108
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003109 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3110 /* parse the Connection header and possibly clean it */
3111 int to_del = 0;
3112 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003113 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3114 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003115 to_del |= 2; /* remove "keep-alive" */
3116 if (!(txn->flags & TX_REQ_VER_11))
3117 to_del |= 1; /* remove "close" */
3118 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003119 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003120
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003121 /* check if client or config asks for explicit close in KAL/SCL */
3122 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3123 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3124 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
3125 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
Willy Tarreau22a95342010-09-29 14:31:41 +02003126 (((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) && /* httpclose without pretend-ka... */
3127 1/*!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)*/) || /* ... +any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003128 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
3129 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003130 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3131 }
Willy Tarreau78599912009-10-17 20:12:21 +02003132
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003133 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01003134 list_for_each_entry(wl, &px->req_add, list) {
Willy Tarreau8abd4cd2010-01-31 14:30:44 +01003135 if (wl->cond) {
3136 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
3137 ret = acl_pass(ret);
3138 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3139 ret = !ret;
3140 if (!ret)
3141 continue;
3142 }
3143
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01003144 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003145 goto return_bad_req;
3146 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003147
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003148 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) {
3149 struct chunk msg;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003150 char *realm = req_acl->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003151
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003152 if (!realm)
3153 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3154
Willy Tarreau844a7e72010-01-31 21:46:18 +01003155 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003156 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3157 txn->status = 401;
3158 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003159 /* on 401 we still count one error, because normal browsing
3160 * won't significantly increase the counter but brute force
3161 * attempts will.
3162 */
3163 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003164 goto return_prx_cond;
3165 }
3166
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003167 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003168 struct stats_admin_rule *stats_admin_rule;
3169
3170 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003171 * FIXME!!! that one is rather dangerous, we want to
3172 * make it follow standard rules (eg: clear req->analysers).
3173 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003174
Cyril Bonté474be412010-10-12 00:14:36 +02003175 /* now check whether we have some admin rules for this request */
3176 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3177 int ret = 1;
3178
3179 if (stats_admin_rule->cond) {
3180 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
3181 ret = acl_pass(ret);
3182 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3183 ret = !ret;
3184 }
3185
3186 if (ret) {
3187 /* no rule, or the rule matches */
3188 s->data_ctx.stats.flags |= STAT_ADMIN;
3189 break;
3190 }
3191 }
3192
Cyril Bonté70be45d2010-10-12 00:14:35 +02003193 /* Was the status page requested with a POST ? */
3194 if (txn->meth == HTTP_METH_POST) {
Cyril Bonté474be412010-10-12 00:14:36 +02003195 if (s->data_ctx.stats.flags & STAT_ADMIN) {
3196 http_process_req_stat_post(s, req);
3197 } else {
3198 s->data_ctx.stats.st_code = STAT_STATUS_DENY;
3199 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003200 }
3201
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003202 s->logs.tv_request = now;
3203 s->data_source = DATA_SRC_STATS;
3204 s->data_state = DATA_ST_INIT;
3205 s->task->nice = -32; /* small boost for HTTP statistics */
3206 stream_int_register_handler(s->rep->prod, http_stats_io_handler);
3207 s->rep->prod->private = s;
3208 s->rep->prod->st0 = s->rep->prod->st1 = 0;
3209 req->analysers = 0;
3210
3211 return 0;
3212
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003213 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003214
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003215 /* check whether we have some ACLs set to redirect this request */
3216 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003217 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003218
Willy Tarreauf285f542010-01-03 20:03:03 +01003219 if (rule->cond) {
3220 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3221 ret = acl_pass(ret);
3222 if (rule->cond->pol == ACL_COND_UNLESS)
3223 ret = !ret;
3224 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003225
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003226 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003227 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003228 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003229
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003230 /* build redirect message */
3231 switch(rule->code) {
3232 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003233 msg_fmt = HTTP_303;
3234 break;
3235 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003236 msg_fmt = HTTP_301;
3237 break;
3238 case 302:
3239 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003240 msg_fmt = HTTP_302;
3241 break;
3242 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003243
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003244 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003245 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003246
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003247 switch(rule->type) {
3248 case REDIRECT_TYPE_PREFIX: {
3249 const char *path;
3250 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003251
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003252 path = http_get_path(txn);
3253 /* build message using path */
3254 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003255 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003256 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3257 int qs = 0;
3258 while (qs < pathlen) {
3259 if (path[qs] == '?') {
3260 pathlen = qs;
3261 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003262 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003263 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003264 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003265 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003266 } else {
3267 path = "/";
3268 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003269 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003270
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003271 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003272 goto return_bad_req;
3273
3274 /* add prefix. Note that if prefix == "/", we don't want to
3275 * add anything, otherwise it makes it hard for the user to
3276 * configure a self-redirection.
3277 */
3278 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003279 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3280 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003281 }
3282
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003283 /* add path */
3284 memcpy(rdr.str + rdr.len, path, pathlen);
3285 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003286
3287 /* append a slash at the end of the location is needed and missing */
3288 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3289 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3290 if (rdr.len > rdr.size - 5)
3291 goto return_bad_req;
3292 rdr.str[rdr.len] = '/';
3293 rdr.len++;
3294 }
3295
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003296 break;
3297 }
3298 case REDIRECT_TYPE_LOCATION:
3299 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003300 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003301 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003302
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003303 /* add location */
3304 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3305 rdr.len += rule->rdr_len;
3306 break;
3307 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003308
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003309 if (rule->cookie_len) {
3310 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3311 rdr.len += 14;
3312 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3313 rdr.len += rule->cookie_len;
3314 memcpy(rdr.str + rdr.len, "\r\n", 2);
3315 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003316 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003317
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003318 /* add end of headers and the keep-alive/close status.
3319 * We may choose to set keep-alive if the Location begins
3320 * with a slash, because the client will come back to the
3321 * same server.
3322 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003323 txn->status = rule->code;
3324 /* let's log the request time */
3325 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003326
3327 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3328 (txn->flags & TX_REQ_XFER_LEN) &&
3329 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
3330 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3331 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3332 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003333 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003334 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3335 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3336 rdr.len += 30;
3337 } else {
3338 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3339 rdr.len += 24;
3340 }
Willy Tarreau75661452010-01-10 10:35:01 +01003341 }
3342 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3343 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003344 buffer_write(req->prod->ob, rdr.str, rdr.len);
3345 /* "eat" the request */
3346 buffer_ignore(req, msg->sov - msg->som);
3347 msg->som = msg->sov;
3348 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003349 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3350 txn->req.msg_state = HTTP_MSG_CLOSED;
3351 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003352 break;
3353 } else {
3354 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003355 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3356 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3357 rdr.len += 29;
3358 } else {
3359 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3360 rdr.len += 23;
3361 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003362 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003363 goto return_prx_cond;
3364 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003365 }
3366 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003367
Willy Tarreau2be39392010-01-03 17:24:51 +01003368 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3369 * If this happens, then the data will not come immediately, so we must
3370 * send all what we have without waiting. Note that due to the small gain
3371 * in waiting for the body of the request, it's easier to simply put the
3372 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3373 * itself once used.
3374 */
3375 req->flags |= BF_SEND_DONTWAIT;
3376
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003377 /* that's OK for us now, let's move on to next analysers */
3378 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003379
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003380 return_bad_req:
3381 /* We centralize bad requests processing here */
3382 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3383 /* we detected a parsing error. We want to archive this request
3384 * in the dedicated proxy area for later troubleshooting.
3385 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003386 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003387 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003388
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003389 txn->req.msg_state = HTTP_MSG_ERROR;
3390 txn->status = 400;
3391 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003392
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003393 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003394 if (s->listener->counters)
3395 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003396
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003397 return_prx_cond:
3398 if (!(s->flags & SN_ERR_MASK))
3399 s->flags |= SN_ERR_PRXCOND;
3400 if (!(s->flags & SN_FINST_MASK))
3401 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003402
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003403 req->analysers = 0;
3404 req->analyse_exp = TICK_ETERNITY;
3405 return 0;
3406}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003407
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003408/* This function performs all the processing enabled for the current request.
3409 * It returns 1 if the processing can continue on next analysers, or zero if it
3410 * needs more data, encounters an error, or wants to immediately abort the
3411 * request. It relies on buffers flags, and updates s->req->analysers.
3412 */
3413int http_process_request(struct session *s, struct buffer *req, int an_bit)
3414{
3415 struct http_txn *txn = &s->txn;
3416 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003417
Willy Tarreau655dce92009-11-08 13:10:58 +01003418 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003419 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003420 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003421 return 0;
3422 }
3423
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003424 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3425 now_ms, __FUNCTION__,
3426 s,
3427 req,
3428 req->rex, req->wex,
3429 req->flags,
3430 req->l,
3431 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003432
Willy Tarreau59234e92008-11-30 23:51:27 +01003433 /*
3434 * Right now, we know that we have processed the entire headers
3435 * and that unwanted requests have been filtered out. We can do
3436 * whatever we want with the remaining request. Also, now we
3437 * may have separate values for ->fe, ->be.
3438 */
Willy Tarreau06619262006-12-17 08:37:22 +01003439
Willy Tarreau59234e92008-11-30 23:51:27 +01003440 /*
3441 * If HTTP PROXY is set we simply get remote server address
3442 * parsing incoming request.
3443 */
3444 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003445 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003446 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003447
Willy Tarreau59234e92008-11-30 23:51:27 +01003448 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003449 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003450 * Note that doing so might move headers in the request, but
3451 * the fields will stay coherent and the URI will not move.
3452 * This should only be performed in the backend.
3453 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003454 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003455 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3456 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003457
Willy Tarreau59234e92008-11-30 23:51:27 +01003458 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003459 * 8: the appsession cookie was looked up very early in 1.2,
3460 * so let's do the same now.
3461 */
3462
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003463 /* It needs to look into the URI unless persistence must be ignored */
3464 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003465 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003466 }
3467
3468 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003469 * 9: add X-Forwarded-For if either the frontend or the backend
3470 * asks for it.
3471 */
3472 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3473 if (s->cli_addr.ss_family == AF_INET) {
3474 /* Add an X-Forwarded-For header unless the source IP is
3475 * in the 'except' network range.
3476 */
3477 if ((!s->fe->except_mask.s_addr ||
3478 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3479 != s->fe->except_net.s_addr) &&
3480 (!s->be->except_mask.s_addr ||
3481 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3482 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003483 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003484 unsigned char *pn;
3485 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003486
3487 /* Note: we rely on the backend to get the header name to be used for
3488 * x-forwarded-for, because the header is really meant for the backends.
3489 * However, if the backend did not specify any option, we have to rely
3490 * on the frontend's header name.
3491 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003492 if (s->be->fwdfor_hdr_len) {
3493 len = s->be->fwdfor_hdr_len;
3494 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003495 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003496 len = s->fe->fwdfor_hdr_len;
3497 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003498 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003499 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003500
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003501 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003502 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003503 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003504 }
3505 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003506 else if (s->cli_addr.ss_family == AF_INET6) {
3507 /* FIXME: for the sake of completeness, we should also support
3508 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003509 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003510 int len;
3511 char pn[INET6_ADDRSTRLEN];
3512 inet_ntop(AF_INET6,
3513 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3514 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003515
Willy Tarreau59234e92008-11-30 23:51:27 +01003516 /* Note: we rely on the backend to get the header name to be used for
3517 * x-forwarded-for, because the header is really meant for the backends.
3518 * However, if the backend did not specify any option, we have to rely
3519 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003520 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003521 if (s->be->fwdfor_hdr_len) {
3522 len = s->be->fwdfor_hdr_len;
3523 memcpy(trash, s->be->fwdfor_hdr_name, len);
3524 } else {
3525 len = s->fe->fwdfor_hdr_len;
3526 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003527 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003528 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003529
Willy Tarreau59234e92008-11-30 23:51:27 +01003530 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003531 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003532 goto return_bad_req;
3533 }
3534 }
3535
3536 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003537 * 10: add X-Original-To if either the frontend or the backend
3538 * asks for it.
3539 */
3540 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3541
3542 /* FIXME: don't know if IPv6 can handle that case too. */
3543 if (s->cli_addr.ss_family == AF_INET) {
3544 /* Add an X-Original-To header unless the destination IP is
3545 * in the 'except' network range.
3546 */
3547 if (!(s->flags & SN_FRT_ADDR_SET))
3548 get_frt_addr(s);
3549
Emeric Brun5bd86a82010-10-22 17:23:04 +02003550 if (s->frt_addr.ss_family == AF_INET &&
3551 ((!s->fe->except_mask_to.s_addr ||
3552 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3553 != s->fe->except_to.s_addr) &&
3554 (!s->be->except_mask_to.s_addr ||
3555 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3556 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003557 int len;
3558 unsigned char *pn;
3559 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3560
3561 /* Note: we rely on the backend to get the header name to be used for
3562 * x-original-to, 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 */
3566 if (s->be->orgto_hdr_len) {
3567 len = s->be->orgto_hdr_len;
3568 memcpy(trash, s->be->orgto_hdr_name, len);
3569 } else {
3570 len = s->fe->orgto_hdr_len;
3571 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003572 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003573 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3574
3575 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003576 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003577 goto return_bad_req;
3578 }
3579 }
3580 }
3581
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003582 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3583 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02003584 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) ||
3585 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003586 unsigned int want_flags = 0;
3587
3588 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003589 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3590 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3591 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003592 want_flags |= TX_CON_CLO_SET;
3593 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003594 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3595 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3596 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003597 want_flags |= TX_CON_KAL_SET;
3598 }
3599
3600 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3601 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003602 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003603
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003604
Willy Tarreau522d6c02009-12-06 18:49:18 +01003605 /* If we have no server assigned yet and we're balancing on url_param
3606 * with a POST request, we may be interested in checking the body for
3607 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003608 */
3609 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3610 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003611 s->be->url_param_post_limit != 0 &&
3612 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003613 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003614 buffer_dont_connect(req);
3615 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003616 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003617
Willy Tarreaud98cf932009-12-27 22:54:55 +01003618 if (txn->flags & TX_REQ_XFER_LEN)
3619 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003620
Willy Tarreau59234e92008-11-30 23:51:27 +01003621 /*************************************************************
3622 * OK, that's finished for the headers. We have done what we *
3623 * could. Let's switch to the DATA state. *
3624 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003625 req->analyse_exp = TICK_ETERNITY;
3626 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003627
Willy Tarreau59234e92008-11-30 23:51:27 +01003628 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003629 /* OK let's go on with the BODY now */
3630 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003631
Willy Tarreau59234e92008-11-30 23:51:27 +01003632 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003633 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003634 /* we detected a parsing error. We want to archive this request
3635 * in the dedicated proxy area for later troubleshooting.
3636 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003637 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003638 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003639
Willy Tarreau59234e92008-11-30 23:51:27 +01003640 txn->req.msg_state = HTTP_MSG_ERROR;
3641 txn->status = 400;
3642 req->analysers = 0;
3643 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003644
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003645 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003646 if (s->listener->counters)
3647 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003648
Willy Tarreau59234e92008-11-30 23:51:27 +01003649 if (!(s->flags & SN_ERR_MASK))
3650 s->flags |= SN_ERR_PRXCOND;
3651 if (!(s->flags & SN_FINST_MASK))
3652 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003653 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003654}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003655
Willy Tarreau60b85b02008-11-30 23:28:40 +01003656/* This function is an analyser which processes the HTTP tarpit. It always
3657 * returns zero, at the beginning because it prevents any other processing
3658 * from occurring, and at the end because it terminates the request.
3659 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003660int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003661{
3662 struct http_txn *txn = &s->txn;
3663
3664 /* This connection is being tarpitted. The CLIENT side has
3665 * already set the connect expiration date to the right
3666 * timeout. We just have to check that the client is still
3667 * there and that the timeout has not expired.
3668 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003669 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003670 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3671 !tick_is_expired(req->analyse_exp, now_ms))
3672 return 0;
3673
3674 /* We will set the queue timer to the time spent, just for
3675 * logging purposes. We fake a 500 server error, so that the
3676 * attacker will not suspect his connection has been tarpitted.
3677 * It will not cause trouble to the logs because we can exclude
3678 * the tarpitted connections by filtering on the 'PT' status flags.
3679 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003680 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3681
3682 txn->status = 500;
3683 if (req->flags != BF_READ_ERROR)
3684 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3685
3686 req->analysers = 0;
3687 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003688
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003689 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003690 if (s->listener->counters)
3691 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003692
Willy Tarreau60b85b02008-11-30 23:28:40 +01003693 if (!(s->flags & SN_ERR_MASK))
3694 s->flags |= SN_ERR_PRXCOND;
3695 if (!(s->flags & SN_FINST_MASK))
3696 s->flags |= SN_FINST_T;
3697 return 0;
3698}
3699
Willy Tarreaud34af782008-11-30 23:36:37 +01003700/* This function is an analyser which processes the HTTP request body. It looks
3701 * for parameters to be used for the load balancing algorithm (url_param). It
3702 * must only be called after the standard HTTP request processing has occurred,
3703 * because it expects the request to be parsed. It returns zero if it needs to
3704 * read more data, or 1 once it has completed its analysis.
3705 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003706int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003707{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003708 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003709 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003710 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003711
3712 /* We have to parse the HTTP request body to find any required data.
3713 * "balance url_param check_post" should have been the only way to get
3714 * into this. We were brought here after HTTP header analysis, so all
3715 * related structures are ready.
3716 */
3717
Willy Tarreau522d6c02009-12-06 18:49:18 +01003718 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3719 goto missing_data;
3720
3721 if (msg->msg_state < HTTP_MSG_100_SENT) {
3722 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3723 * send an HTTP/1.1 100 Continue intermediate response.
3724 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003725 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003726 struct hdr_ctx ctx;
3727 ctx.idx = 0;
3728 /* Expect is allowed in 1.1, look for it */
3729 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3730 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3731 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3732 }
3733 }
3734 msg->msg_state = HTTP_MSG_100_SENT;
3735 }
3736
3737 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003738 /* we have msg->col and msg->sov which both point to the first
3739 * byte of message body. msg->som still points to the beginning
3740 * of the message. We must save the body in req->lr because it
3741 * survives buffer re-alignments.
3742 */
3743 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003744 if (txn->flags & TX_REQ_TE_CHNK)
3745 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3746 else
3747 msg->msg_state = HTTP_MSG_DATA;
3748 }
3749
3750 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003751 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003752 * set ->sov and ->lr to point to the body and switch to DATA or
3753 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003754 */
3755 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003756
Willy Tarreau115acb92009-12-26 13:56:06 +01003757 if (!ret)
3758 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003759 else if (ret < 0) {
3760 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003761 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003762 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003763 }
3764
Willy Tarreaud98cf932009-12-27 22:54:55 +01003765 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003766 * We have the first non-header byte in msg->col, which is either the
3767 * beginning of the chunk size or of the data. The first data byte is in
3768 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3769 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003770 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003771
3772 if (msg->hdr_content_len < limit)
3773 limit = msg->hdr_content_len;
3774
Willy Tarreau7c96f672009-12-27 22:47:25 +01003775 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003776 goto http_end;
3777
3778 missing_data:
3779 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003780 if (req->flags & BF_FULL) {
3781 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003782 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003783 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003784
Willy Tarreau522d6c02009-12-06 18:49:18 +01003785 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3786 txn->status = 408;
3787 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003788
3789 if (!(s->flags & SN_ERR_MASK))
3790 s->flags |= SN_ERR_CLITO;
3791 if (!(s->flags & SN_FINST_MASK))
3792 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003793 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003794 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003795
3796 /* we get here if we need to wait for more data */
3797 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003798 /* Not enough data. We'll re-use the http-request
3799 * timeout here. Ideally, we should set the timeout
3800 * relative to the accept() date. We just set the
3801 * request timeout once at the beginning of the
3802 * request.
3803 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003804 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003805 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003806 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003807 return 0;
3808 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003809
3810 http_end:
3811 /* The situation will not evolve, so let's give up on the analysis. */
3812 s->logs.tv_request = now; /* update the request timer to reflect full request */
3813 req->analysers &= ~an_bit;
3814 req->analyse_exp = TICK_ETERNITY;
3815 return 1;
3816
3817 return_bad_req: /* let's centralize all bad requests */
3818 txn->req.msg_state = HTTP_MSG_ERROR;
3819 txn->status = 400;
3820 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3821
Willy Tarreau79ebac62010-06-07 13:47:49 +02003822 if (!(s->flags & SN_ERR_MASK))
3823 s->flags |= SN_ERR_PRXCOND;
3824 if (!(s->flags & SN_FINST_MASK))
3825 s->flags |= SN_FINST_R;
3826
Willy Tarreau522d6c02009-12-06 18:49:18 +01003827 return_err_msg:
3828 req->analysers = 0;
3829 s->fe->counters.failed_req++;
3830 if (s->listener->counters)
3831 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003832 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003833}
3834
Willy Tarreau610ecce2010-01-04 21:15:02 +01003835/* Terminate current transaction and prepare a new one. This is very tricky
3836 * right now but it works.
3837 */
3838void http_end_txn_clean_session(struct session *s)
3839{
3840 /* FIXME: We need a more portable way of releasing a backend's and a
3841 * server's connections. We need a safer way to reinitialize buffer
3842 * flags. We also need a more accurate method for computing per-request
3843 * data.
3844 */
3845 http_silent_debug(__LINE__, s);
3846
3847 s->req->cons->flags |= SI_FL_NOLINGER;
3848 s->req->cons->shutr(s->req->cons);
3849 s->req->cons->shutw(s->req->cons);
3850
3851 http_silent_debug(__LINE__, s);
3852
3853 if (s->flags & SN_BE_ASSIGNED)
3854 s->be->beconn--;
3855
3856 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3857 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003858 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003859
3860 if (s->txn.status) {
3861 int n;
3862
3863 n = s->txn.status / 100;
3864 if (n < 1 || n > 5)
3865 n = 0;
3866
3867 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01003868 s->fe->counters.fe.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003869
Willy Tarreau24657792010-02-26 10:30:28 +01003870 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003871 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01003872 s->be->counters.be.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003873 }
3874
3875 /* don't count other requests' data */
3876 s->logs.bytes_in -= s->req->l - s->req->send_max;
3877 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3878
3879 /* let's do a final log if we need it */
3880 if (s->logs.logwait &&
3881 !(s->flags & SN_MONITOR) &&
3882 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3883 s->do_log(s);
3884 }
3885
3886 s->logs.accept_date = date; /* user-visible date for logging */
3887 s->logs.tv_accept = now; /* corrected date for internal use */
3888 tv_zero(&s->logs.tv_request);
3889 s->logs.t_queue = -1;
3890 s->logs.t_connect = -1;
3891 s->logs.t_data = -1;
3892 s->logs.t_close = 0;
3893 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3894 s->logs.srv_queue_size = 0; /* we will get this number soon */
3895
3896 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3897 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3898
3899 if (s->pend_pos)
3900 pendconn_free(s->pend_pos);
3901
3902 if (s->srv) {
3903 if (s->flags & SN_CURR_SESS) {
3904 s->flags &= ~SN_CURR_SESS;
3905 s->srv->cur_sess--;
3906 }
3907 if (may_dequeue_tasks(s->srv, s->be))
3908 process_srv_queue(s->srv);
3909 }
3910
3911 if (unlikely(s->srv_conn))
3912 sess_change_server(s, NULL);
3913 s->srv = NULL;
3914
3915 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3916 s->req->cons->fd = -1; /* just to help with debugging */
3917 s->req->cons->err_type = SI_ET_NONE;
3918 s->req->cons->err_loc = NULL;
3919 s->req->cons->exp = TICK_ETERNITY;
3920 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003921 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3922 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 +02003923 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 +01003924 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3925 s->txn.meth = 0;
3926 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003927 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003928 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003929 s->req->cons->flags |= SI_FL_INDEP_STR;
3930
3931 /* if the request buffer is not empty, it means we're
3932 * about to process another request, so send pending
3933 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003934 * Just don't do this if the buffer is close to be full,
3935 * because the request will wait for it to flush a little
3936 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003937 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003938 if (s->req->l > s->req->send_max) {
3939 if (s->rep->send_max &&
3940 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003941 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3942 s->rep->flags |= BF_EXPECT_MORE;
3943 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003944
3945 /* we're removing the analysers, we MUST re-enable events detection */
3946 buffer_auto_read(s->req);
3947 buffer_auto_close(s->req);
3948 buffer_auto_read(s->rep);
3949 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003950
3951 /* make ->lr point to the first non-forwarded byte */
3952 s->req->lr = s->req->w + s->req->send_max;
3953 if (s->req->lr >= s->req->data + s->req->size)
3954 s->req->lr -= s->req->size;
3955 s->rep->lr = s->rep->w + s->rep->send_max;
3956 if (s->rep->lr >= s->rep->data + s->rep->size)
3957 s->rep->lr -= s->req->size;
3958
Willy Tarreau342b11c2010-11-24 16:22:09 +01003959 s->req->analysers = s->listener->analysers;
3960 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003961 s->rep->analysers = 0;
3962
3963 http_silent_debug(__LINE__, s);
3964}
3965
3966
3967/* This function updates the request state machine according to the response
3968 * state machine and buffer flags. It returns 1 if it changes anything (flag
3969 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3970 * it is only used to find when a request/response couple is complete. Both
3971 * this function and its equivalent should loop until both return zero. It
3972 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3973 */
3974int http_sync_req_state(struct session *s)
3975{
3976 struct buffer *buf = s->req;
3977 struct http_txn *txn = &s->txn;
3978 unsigned int old_flags = buf->flags;
3979 unsigned int old_state = txn->req.msg_state;
3980
3981 http_silent_debug(__LINE__, s);
3982 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3983 return 0;
3984
3985 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003986 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003987 * We can shut the read side unless we want to abort_on_close,
3988 * or we have a POST request. The issue with POST requests is
3989 * that some browsers still send a CRLF after the request, and
3990 * this CRLF must be read so that it does not remain in the kernel
3991 * buffers, otherwise a close could cause an RST on some systems
3992 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003993 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003994 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003995 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003996
3997 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3998 goto wait_other_side;
3999
4000 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4001 /* The server has not finished to respond, so we
4002 * don't want to move in order not to upset it.
4003 */
4004 goto wait_other_side;
4005 }
4006
4007 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4008 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004009 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004010 txn->req.msg_state = HTTP_MSG_TUNNEL;
4011 goto wait_other_side;
4012 }
4013
4014 /* When we get here, it means that both the request and the
4015 * response have finished receiving. Depending on the connection
4016 * mode, we'll have to wait for the last bytes to leave in either
4017 * direction, and sometimes for a close to be effective.
4018 */
4019
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004020 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4021 /* Server-close mode : queue a connection close to the server */
4022 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004023 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004024 }
4025 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4026 /* Option forceclose is set, or either side wants to close,
4027 * let's enforce it now that we're not expecting any new
4028 * data to come. The caller knows the session is complete
4029 * once both states are CLOSED.
4030 */
4031 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004032 buffer_shutr_now(buf);
4033 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004034 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004035 }
4036 else {
4037 /* The last possible modes are keep-alive and tunnel. Since tunnel
4038 * mode does not set the body analyser, we can't reach this place
4039 * in tunnel mode, so we're left with keep-alive only.
4040 * This mode is currently not implemented, we switch to tunnel mode.
4041 */
4042 buffer_auto_read(buf);
4043 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004044 }
4045
4046 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4047 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004048 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4049
Willy Tarreau610ecce2010-01-04 21:15:02 +01004050 if (!(buf->flags & BF_OUT_EMPTY)) {
4051 txn->req.msg_state = HTTP_MSG_CLOSING;
4052 goto http_msg_closing;
4053 }
4054 else {
4055 txn->req.msg_state = HTTP_MSG_CLOSED;
4056 goto http_msg_closed;
4057 }
4058 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004059 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004060 }
4061
4062 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4063 http_msg_closing:
4064 /* nothing else to forward, just waiting for the output buffer
4065 * to be empty and for the shutw_now to take effect.
4066 */
4067 if (buf->flags & BF_OUT_EMPTY) {
4068 txn->req.msg_state = HTTP_MSG_CLOSED;
4069 goto http_msg_closed;
4070 }
4071 else if (buf->flags & BF_SHUTW) {
4072 txn->req.msg_state = HTTP_MSG_ERROR;
4073 goto wait_other_side;
4074 }
4075 }
4076
4077 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4078 http_msg_closed:
4079 goto wait_other_side;
4080 }
4081
4082 wait_other_side:
4083 http_silent_debug(__LINE__, s);
4084 return txn->req.msg_state != old_state || buf->flags != old_flags;
4085}
4086
4087
4088/* This function updates the response state machine according to the request
4089 * state machine and buffer flags. It returns 1 if it changes anything (flag
4090 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4091 * it is only used to find when a request/response couple is complete. Both
4092 * this function and its equivalent should loop until both return zero. It
4093 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4094 */
4095int http_sync_res_state(struct session *s)
4096{
4097 struct buffer *buf = s->rep;
4098 struct http_txn *txn = &s->txn;
4099 unsigned int old_flags = buf->flags;
4100 unsigned int old_state = txn->rsp.msg_state;
4101
4102 http_silent_debug(__LINE__, s);
4103 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4104 return 0;
4105
4106 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4107 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004108 * still monitor the server connection for a possible close
4109 * while the request is being uploaded, so we don't disable
4110 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004111 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004112 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004113
4114 if (txn->req.msg_state == HTTP_MSG_ERROR)
4115 goto wait_other_side;
4116
4117 if (txn->req.msg_state < HTTP_MSG_DONE) {
4118 /* The client seems to still be sending data, probably
4119 * because we got an error response during an upload.
4120 * We have the choice of either breaking the connection
4121 * or letting it pass through. Let's do the later.
4122 */
4123 goto wait_other_side;
4124 }
4125
4126 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4127 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004128 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004129 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4130 goto wait_other_side;
4131 }
4132
4133 /* When we get here, it means that both the request and the
4134 * response have finished receiving. Depending on the connection
4135 * mode, we'll have to wait for the last bytes to leave in either
4136 * direction, and sometimes for a close to be effective.
4137 */
4138
4139 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4140 /* Server-close mode : shut read and wait for the request
4141 * side to close its output buffer. The caller will detect
4142 * when we're in DONE and the other is in CLOSED and will
4143 * catch that for the final cleanup.
4144 */
4145 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4146 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004147 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004148 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4149 /* Option forceclose is set, or either side wants to close,
4150 * let's enforce it now that we're not expecting any new
4151 * data to come. The caller knows the session is complete
4152 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004153 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004154 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4155 buffer_shutr_now(buf);
4156 buffer_shutw_now(buf);
4157 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004158 }
4159 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004160 /* The last possible modes are keep-alive and tunnel. Since tunnel
4161 * mode does not set the body analyser, we can't reach this place
4162 * in tunnel mode, so we're left with keep-alive only.
4163 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004164 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004165 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004166 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004167 }
4168
4169 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4170 /* if we've just closed an output, let's switch */
4171 if (!(buf->flags & BF_OUT_EMPTY)) {
4172 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4173 goto http_msg_closing;
4174 }
4175 else {
4176 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4177 goto http_msg_closed;
4178 }
4179 }
4180 goto wait_other_side;
4181 }
4182
4183 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4184 http_msg_closing:
4185 /* nothing else to forward, just waiting for the output buffer
4186 * to be empty and for the shutw_now to take effect.
4187 */
4188 if (buf->flags & BF_OUT_EMPTY) {
4189 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4190 goto http_msg_closed;
4191 }
4192 else if (buf->flags & BF_SHUTW) {
4193 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreauae526782010-03-04 20:34:23 +01004194 s->be->counters.cli_aborts++;
4195 if (s->srv)
4196 s->srv->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004197 goto wait_other_side;
4198 }
4199 }
4200
4201 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4202 http_msg_closed:
4203 /* drop any pending data */
4204 buffer_ignore(buf, buf->l - buf->send_max);
4205 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004206 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004207 goto wait_other_side;
4208 }
4209
4210 wait_other_side:
4211 http_silent_debug(__LINE__, s);
4212 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4213}
4214
4215
4216/* Resync the request and response state machines. Return 1 if either state
4217 * changes.
4218 */
4219int http_resync_states(struct session *s)
4220{
4221 struct http_txn *txn = &s->txn;
4222 int old_req_state = txn->req.msg_state;
4223 int old_res_state = txn->rsp.msg_state;
4224
4225 http_silent_debug(__LINE__, s);
4226 http_sync_req_state(s);
4227 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004228 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004229 if (!http_sync_res_state(s))
4230 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004231 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004232 if (!http_sync_req_state(s))
4233 break;
4234 }
4235 http_silent_debug(__LINE__, s);
4236 /* OK, both state machines agree on a compatible state.
4237 * There are a few cases we're interested in :
4238 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4239 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4240 * directions, so let's simply disable both analysers.
4241 * - HTTP_MSG_CLOSED on the response only means we must abort the
4242 * request.
4243 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4244 * with server-close mode means we've completed one request and we
4245 * must re-initialize the server connection.
4246 */
4247
4248 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4249 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4250 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4251 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4252 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004253 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004254 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004255 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004256 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004257 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004258 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004259 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4260 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004261 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004262 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004263 s->rep->analysers = 0;
4264 buffer_auto_close(s->rep);
4265 buffer_auto_read(s->rep);
4266 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004267 buffer_abort(s->req);
4268 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004269 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004270 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004271 }
4272 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4273 txn->rsp.msg_state == HTTP_MSG_DONE &&
4274 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4275 /* server-close: terminate this server connection and
4276 * reinitialize a fresh-new transaction.
4277 */
4278 http_end_txn_clean_session(s);
4279 }
4280
4281 http_silent_debug(__LINE__, s);
4282 return txn->req.msg_state != old_req_state ||
4283 txn->rsp.msg_state != old_res_state;
4284}
4285
Willy Tarreaud98cf932009-12-27 22:54:55 +01004286/* This function is an analyser which forwards request body (including chunk
4287 * sizes if any). It is called as soon as we must forward, even if we forward
4288 * zero byte. The only situation where it must not be called is when we're in
4289 * tunnel mode and we want to forward till the close. It's used both to forward
4290 * remaining data and to resync after end of body. It expects the msg_state to
4291 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4292 * read more data, or 1 once we can go on with next request or end the session.
4293 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4294 * bytes of pending data + the headers if not already done (between som and sov).
4295 * It eventually adjusts som to match sov after the data in between have been sent.
4296 */
4297int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4298{
4299 struct http_txn *txn = &s->txn;
4300 struct http_msg *msg = &s->txn.req;
4301
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004302 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4303 return 0;
4304
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004305 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4306 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004307 /* Output closed while we were sending data. We must abort and
4308 * wake the other side up.
4309 */
4310 msg->msg_state = HTTP_MSG_ERROR;
4311 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004312 return 1;
4313 }
4314
Willy Tarreau4fe41902010-06-07 22:27:41 +02004315 /* in most states, we should abort in case of early close */
4316 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004317
4318 /* Note that we don't have to send 100-continue back because we don't
4319 * need the data to complete our job, and it's up to the server to
4320 * decide whether to return 100, 417 or anything else in return of
4321 * an "Expect: 100-continue" header.
4322 */
4323
4324 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4325 /* we have msg->col and msg->sov which both point to the first
4326 * byte of message body. msg->som still points to the beginning
4327 * of the message. We must save the body in req->lr because it
4328 * survives buffer re-alignments.
4329 */
4330 req->lr = req->data + msg->sov;
4331 if (txn->flags & TX_REQ_TE_CHNK)
4332 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4333 else {
4334 msg->msg_state = HTTP_MSG_DATA;
4335 }
4336 }
4337
Willy Tarreaud98cf932009-12-27 22:54:55 +01004338 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004339 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004340 /* we may have some data pending */
4341 if (msg->hdr_content_len || msg->som != msg->sov) {
4342 int bytes = msg->sov - msg->som;
4343 if (bytes < 0) /* sov may have wrapped at the end */
4344 bytes += req->size;
4345 buffer_forward(req, bytes + msg->hdr_content_len);
4346 msg->hdr_content_len = 0; /* don't forward that again */
4347 msg->som = msg->sov;
4348 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004349
Willy Tarreaucaabe412010-01-03 23:08:28 +01004350 if (msg->msg_state == HTTP_MSG_DATA) {
4351 /* must still forward */
4352 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004353 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004354
4355 /* nothing left to forward */
4356 if (txn->flags & TX_REQ_TE_CHNK)
4357 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004358 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004359 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004360 }
4361 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004362 /* read the chunk size and assign it to ->hdr_content_len, then
4363 * set ->sov and ->lr to point to the body and switch to DATA or
4364 * TRAILERS state.
4365 */
4366 int ret = http_parse_chunk_size(req, msg);
4367
4368 if (!ret)
4369 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004370 else if (ret < 0) {
4371 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004372 if (msg->err_pos >= 0)
4373 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004374 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004375 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004376 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreau57f5c122010-12-02 00:37:14 +01004377 /* Don't set a PUSH at the end of that chunk if it's not the last one */
4378 if (msg->msg_state == HTTP_MSG_DATA)
4379 req->flags |= BF_EXPECT_MORE;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004380 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004381 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4382 /* we want the CRLF after the data */
4383 int ret;
4384
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004385 req->lr = req->w + req->send_max;
4386 if (req->lr >= req->data + req->size)
4387 req->lr -= req->size;
4388
Willy Tarreaud98cf932009-12-27 22:54:55 +01004389 ret = http_skip_chunk_crlf(req, msg);
4390
4391 if (ret == 0)
4392 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004393 else if (ret < 0) {
4394 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004395 if (msg->err_pos >= 0)
4396 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004397 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004398 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004399 /* we're in MSG_CHUNK_SIZE now */
4400 }
4401 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4402 int ret = http_forward_trailers(req, msg);
4403
4404 if (ret == 0)
4405 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004406 else if (ret < 0) {
4407 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004408 if (msg->err_pos >= 0)
4409 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004410 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004411 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004412 /* we're in HTTP_MSG_DONE now */
4413 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004414 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004415 int old_state = msg->msg_state;
4416
Willy Tarreau610ecce2010-01-04 21:15:02 +01004417 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004418 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004419 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4420 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4421 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004422 if (http_resync_states(s)) {
4423 /* some state changes occurred, maybe the analyser
4424 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004425 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004426 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4427 if (req->flags & BF_SHUTW) {
4428 /* request errors are most likely due to
4429 * the server aborting the transfer.
4430 */
4431 if (!(s->flags & SN_ERR_MASK))
4432 s->flags |= SN_ERR_SRVCL;
4433 if (!(s->flags & SN_FINST_MASK))
4434 s->flags |= SN_FINST_D;
4435 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004436 if (msg->err_pos >= 0)
4437 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004438 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004439 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004440 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004441 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004442
4443 /* If "option abortonclose" is set on the backend, we
4444 * want to monitor the client's connection and forward
4445 * any shutdown notification to the server, which will
4446 * decide whether to close or to go on processing the
4447 * request.
4448 */
4449 if (s->be->options & PR_O_ABRT_CLOSE) {
4450 buffer_auto_read(req);
4451 buffer_auto_close(req);
4452 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004453 else if (s->txn.meth == HTTP_METH_POST) {
4454 /* POST requests may require to read extra CRLF
4455 * sent by broken browsers and which could cause
4456 * an RST to be sent upon close on some systems
4457 * (eg: Linux).
4458 */
4459 buffer_auto_read(req);
4460 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004461
Willy Tarreau610ecce2010-01-04 21:15:02 +01004462 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004463 }
4464 }
4465
Willy Tarreaud98cf932009-12-27 22:54:55 +01004466 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004467 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004468 if (req->flags & BF_SHUTR) {
4469 if (!(s->flags & SN_ERR_MASK))
4470 s->flags |= SN_ERR_CLICL;
4471 if (!(s->flags & SN_FINST_MASK))
4472 s->flags |= SN_FINST_D;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004473 goto return_bad_req;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004474 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004475
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004476 /* waiting for the last bits to leave the buffer */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004477 if (req->flags & BF_SHUTW) {
4478 if (!(s->flags & SN_ERR_MASK))
4479 s->flags |= SN_ERR_SRVCL;
4480 if (!(s->flags & SN_FINST_MASK))
4481 s->flags |= SN_FINST_D;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004482 goto return_bad_req;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004483 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004484
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004485 /* When TE: chunked is used, we need to get there again to parse remaining
4486 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4487 */
4488 if (txn->flags & TX_REQ_TE_CHNK)
4489 buffer_dont_close(req);
4490
Willy Tarreau610ecce2010-01-04 21:15:02 +01004491 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004492 return 0;
4493
Willy Tarreaud98cf932009-12-27 22:54:55 +01004494 return_bad_req: /* let's centralize all bad requests */
4495 txn->req.msg_state = HTTP_MSG_ERROR;
4496 txn->status = 400;
4497 /* Note: we don't send any error if some data were already sent */
Willy Tarreau148d0992010-01-10 10:21:21 +01004498 stream_int_retnclose(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004499 req->analysers = 0;
4500 s->fe->counters.failed_req++;
4501 if (s->listener->counters)
4502 s->listener->counters->failed_req++;
4503
4504 if (!(s->flags & SN_ERR_MASK))
4505 s->flags |= SN_ERR_PRXCOND;
4506 if (!(s->flags & SN_FINST_MASK))
4507 s->flags |= SN_FINST_R;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004508 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004509 return 0;
4510}
4511
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004512/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4513 * processing can continue on next analysers, or zero if it either needs more
4514 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4515 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4516 * when it has nothing left to do, and may remove any analyser when it wants to
4517 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004518 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004519int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004520{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004521 struct http_txn *txn = &s->txn;
4522 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004523 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004524 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004525 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004526 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004527
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004528 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 +02004529 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004530 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004531 rep,
4532 rep->rex, rep->wex,
4533 rep->flags,
4534 rep->l,
4535 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004536
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004537 /*
4538 * Now parse the partial (or complete) lines.
4539 * We will check the response syntax, and also join multi-line
4540 * headers. An index of all the lines will be elaborated while
4541 * parsing.
4542 *
4543 * For the parsing, we use a 28 states FSM.
4544 *
4545 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004546 * rep->data + msg->som = beginning of response
4547 * rep->data + msg->eoh = end of processed headers / start of current one
4548 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004549 * rep->lr = first non-visited byte
4550 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004551 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004552 */
4553
Willy Tarreau83e3af02009-12-28 17:39:57 +01004554 /* There's a protected area at the end of the buffer for rewriting
4555 * purposes. We don't want to start to parse the request if the
4556 * protected area is affected, because we may have to move processed
4557 * data later, which is much more complicated.
4558 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004559 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4560 if (unlikely((rep->flags & BF_FULL) ||
4561 rep->r < rep->lr ||
4562 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4563 if (rep->send_max) {
4564 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004565 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4566 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004567 buffer_dont_close(rep);
4568 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4569 return 0;
4570 }
4571 if (rep->l <= rep->size - global.tune.maxrewrite)
4572 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004573 }
4574
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004575 if (likely(rep->lr < rep->r))
4576 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004577 }
4578
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004579 /* 1: we might have to print this header in debug mode */
4580 if (unlikely((global.mode & MODE_DEBUG) &&
4581 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004582 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004583 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004584 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004585
Willy Tarreau663308b2010-06-07 14:06:08 +02004586 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004587 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004588 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004589
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004590 sol += hdr_idx_first_pos(&txn->hdr_idx);
4591 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004592
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004593 while (cur_idx) {
4594 eol = sol + txn->hdr_idx.v[cur_idx].len;
4595 debug_hdr("srvhdr", s, sol, eol);
4596 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4597 cur_idx = txn->hdr_idx.v[cur_idx].next;
4598 }
4599 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004600
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004601 /*
4602 * Now we quickly check if we have found a full valid response.
4603 * If not so, we check the FD and buffer states before leaving.
4604 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004605 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004606 * responses are checked first.
4607 *
4608 * Depending on whether the client is still there or not, we
4609 * may send an error response back or not. Note that normally
4610 * we should only check for HTTP status there, and check I/O
4611 * errors somewhere else.
4612 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004613
Willy Tarreau655dce92009-11-08 13:10:58 +01004614 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004615 /* Invalid response */
4616 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4617 /* we detected a parsing error. We want to archive this response
4618 * in the dedicated proxy area for later troubleshooting.
4619 */
4620 hdr_response_bad:
4621 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004622 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004623
4624 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004625 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004626 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004627 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4628 }
Willy Tarreau64648412010-03-05 10:41:54 +01004629 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004630 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004631 rep->analysers = 0;
4632 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004633 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004634 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004635 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4636
4637 if (!(s->flags & SN_ERR_MASK))
4638 s->flags |= SN_ERR_PRXCOND;
4639 if (!(s->flags & SN_FINST_MASK))
4640 s->flags |= SN_FINST_H;
4641
4642 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004643 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004644
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004645 /* too large response does not fit in buffer. */
4646 else if (rep->flags & BF_FULL) {
4647 goto hdr_response_bad;
4648 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004649
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004650 /* read error */
4651 else if (rep->flags & BF_READ_ERROR) {
4652 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004653 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004654
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004655 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004656 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004657 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004658 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4659 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004660
Willy Tarreau90deb182010-01-07 00:20:41 +01004661 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004662 rep->analysers = 0;
4663 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004664 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004665 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004666 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004667
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004668 if (!(s->flags & SN_ERR_MASK))
4669 s->flags |= SN_ERR_SRVCL;
4670 if (!(s->flags & SN_FINST_MASK))
4671 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004672 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004673 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004674
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004675 /* read timeout : return a 504 to the client. */
4676 else if (rep->flags & BF_READ_TIMEOUT) {
4677 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004678 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004679
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004680 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004681 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004682 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004683 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4684 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004685
Willy Tarreau90deb182010-01-07 00:20:41 +01004686 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004687 rep->analysers = 0;
4688 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004689 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004690 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004691 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004692
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004693 if (!(s->flags & SN_ERR_MASK))
4694 s->flags |= SN_ERR_SRVTO;
4695 if (!(s->flags & SN_FINST_MASK))
4696 s->flags |= SN_FINST_H;
4697 return 0;
4698 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004699
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004700 /* close from server */
4701 else if (rep->flags & BF_SHUTR) {
4702 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004703 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004704
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004705 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004706 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004707 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004708 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4709 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004710
Willy Tarreau90deb182010-01-07 00:20:41 +01004711 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004712 rep->analysers = 0;
4713 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004714 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004715 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004716 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004717
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004718 if (!(s->flags & SN_ERR_MASK))
4719 s->flags |= SN_ERR_SRVCL;
4720 if (!(s->flags & SN_FINST_MASK))
4721 s->flags |= SN_FINST_H;
4722 return 0;
4723 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004724
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004725 /* write error to client (we don't send any message then) */
4726 else if (rep->flags & BF_WRITE_ERROR) {
4727 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004728 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004729
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004730 s->be->counters.failed_resp++;
4731 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004732 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004733
4734 if (!(s->flags & SN_ERR_MASK))
4735 s->flags |= SN_ERR_CLICL;
4736 if (!(s->flags & SN_FINST_MASK))
4737 s->flags |= SN_FINST_H;
4738
4739 /* process_session() will take care of the error */
4740 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004741 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004742
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004743 buffer_dont_close(rep);
4744 return 0;
4745 }
4746
4747 /* More interesting part now : we know that we have a complete
4748 * response which at least looks like HTTP. We have an indicator
4749 * of each header's length, so we can parse them quickly.
4750 */
4751
4752 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004753 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004754
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004755 /*
4756 * 1: get the status code
4757 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004758 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004759 if (n < 1 || n > 5)
4760 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004761 /* when the client triggers a 4xx from the server, it's most often due
4762 * to a missing object or permission. These events should be tracked
4763 * because if they happen often, it may indicate a brute force or a
4764 * vulnerability scan.
4765 */
4766 if (n == 4)
4767 session_inc_http_err_ctr(s);
4768
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004769 if (s->srv)
4770 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004771
Willy Tarreau5b154472009-12-21 20:11:07 +01004772 /* check if the response is HTTP/1.1 or above */
4773 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004774 ((msg->sol[5] > '1') ||
4775 ((msg->sol[5] == '1') &&
4776 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004777 txn->flags |= TX_RES_VER_11;
4778
4779 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004780 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 +01004781
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004782 /* transfer length unknown*/
4783 txn->flags &= ~TX_RES_XFER_LEN;
4784
Willy Tarreau962c3f42010-01-10 00:15:35 +01004785 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004786
Willy Tarreau39650402010-03-15 19:44:39 +01004787 /* Adjust server's health based on status code. Note: status codes 501
4788 * and 505 are triggered on demand by client request, so we must not
4789 * count them as server failures.
4790 */
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004791 if (s->srv) {
4792 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
4793 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4794 else
4795 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4796 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004797
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004798 /*
4799 * 2: check for cacheability.
4800 */
4801
4802 switch (txn->status) {
4803 case 200:
4804 case 203:
4805 case 206:
4806 case 300:
4807 case 301:
4808 case 410:
4809 /* RFC2616 @13.4:
4810 * "A response received with a status code of
4811 * 200, 203, 206, 300, 301 or 410 MAY be stored
4812 * by a cache (...) unless a cache-control
4813 * directive prohibits caching."
4814 *
4815 * RFC2616 @9.5: POST method :
4816 * "Responses to this method are not cacheable,
4817 * unless the response includes appropriate
4818 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004819 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004820 if (likely(txn->meth != HTTP_METH_POST) &&
4821 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4822 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4823 break;
4824 default:
4825 break;
4826 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004827
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004828 /*
4829 * 3: we may need to capture headers
4830 */
4831 s->logs.logwait &= ~LW_RESP;
4832 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004833 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004834 txn->rsp.cap, s->fe->rsp_cap);
4835
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004836 /* 4: determine the transfer-length.
4837 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4838 * the presence of a message-body in a RESPONSE and its transfer length
4839 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004840 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004841 * All responses to the HEAD request method MUST NOT include a
4842 * message-body, even though the presence of entity-header fields
4843 * might lead one to believe they do. All 1xx (informational), 204
4844 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4845 * message-body. All other responses do include a message-body,
4846 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004847 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004848 * 1. Any response which "MUST NOT" include a message-body (such as the
4849 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4850 * always terminated by the first empty line after the header fields,
4851 * regardless of the entity-header fields present in the message.
4852 *
4853 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4854 * the "chunked" transfer-coding (Section 6.2) is used, the
4855 * transfer-length is defined by the use of this transfer-coding.
4856 * If a Transfer-Encoding header field is present and the "chunked"
4857 * transfer-coding is not present, the transfer-length is defined by
4858 * the sender closing the connection.
4859 *
4860 * 3. If a Content-Length header field is present, its decimal value in
4861 * OCTETs represents both the entity-length and the transfer-length.
4862 * If a message is received with both a Transfer-Encoding header
4863 * field and a Content-Length header field, the latter MUST be ignored.
4864 *
4865 * 4. If the message uses the media type "multipart/byteranges", and
4866 * the transfer-length is not otherwise specified, then this self-
4867 * delimiting media type defines the transfer-length. This media
4868 * type MUST NOT be used unless the sender knows that the recipient
4869 * can parse it; the presence in a request of a Range header with
4870 * multiple byte-range specifiers from a 1.1 client implies that the
4871 * client can parse multipart/byteranges responses.
4872 *
4873 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004874 */
4875
4876 /* Skip parsing if no content length is possible. The response flags
4877 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004878 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004879 * FIXME: should we parse anyway and return an error on chunked encoding ?
4880 */
4881 if (txn->meth == HTTP_METH_HEAD ||
4882 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004883 txn->status == 204 || txn->status == 304) {
4884 txn->flags |= TX_RES_XFER_LEN;
4885 goto skip_content_length;
4886 }
4887
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004888 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004889 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004890 while ((txn->flags & TX_RES_VER_11) &&
4891 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004892 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4893 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4894 else if (txn->flags & TX_RES_TE_CHNK) {
4895 /* bad transfer-encoding (chunked followed by something else) */
4896 use_close_only = 1;
4897 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4898 break;
4899 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004900 }
4901
4902 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4903 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004904 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004905 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4906 signed long long cl;
4907
4908 if (!ctx.vlen)
4909 goto hdr_response_bad;
4910
4911 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4912 goto hdr_response_bad; /* parse failure */
4913
4914 if (cl < 0)
4915 goto hdr_response_bad;
4916
4917 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4918 goto hdr_response_bad; /* already specified, was different */
4919
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004920 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004921 msg->hdr_content_len = cl;
4922 }
4923
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004924 /* FIXME: we should also implement the multipart/byterange method.
4925 * For now on, we resort to close mode in this case (unknown length).
4926 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004927skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004928
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004929 /* end of job, return OK */
4930 rep->analysers &= ~an_bit;
4931 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004932 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004933 return 1;
4934}
4935
4936/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004937 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4938 * and updates t->rep->analysers. It might make sense to explode it into several
4939 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004940 */
4941int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4942{
4943 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004944 struct http_msg *msg = &txn->rsp;
4945 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004946 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004947
4948 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4949 now_ms, __FUNCTION__,
4950 t,
4951 rep,
4952 rep->rex, rep->wex,
4953 rep->flags,
4954 rep->l,
4955 rep->analysers);
4956
Willy Tarreau655dce92009-11-08 13:10:58 +01004957 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004958 return 0;
4959
4960 rep->analysers &= ~an_bit;
4961 rep->analyse_exp = TICK_ETERNITY;
4962
Willy Tarreau5b154472009-12-21 20:11:07 +01004963 /* Now we have to check if we need to modify the Connection header.
4964 * This is more difficult on the response than it is on the request,
4965 * because we can have two different HTTP versions and we don't know
4966 * how the client will interprete a response. For instance, let's say
4967 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4968 * HTTP/1.1 response without any header. Maybe it will bound itself to
4969 * HTTP/1.0 because it only knows about it, and will consider the lack
4970 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4971 * the lack of header as a keep-alive. Thus we will use two flags
4972 * indicating how a request MAY be understood by the client. In case
4973 * of multiple possibilities, we'll fix the header to be explicit. If
4974 * ambiguous cases such as both close and keepalive are seen, then we
4975 * will fall back to explicit close. Note that we won't take risks with
4976 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004977 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004978 */
4979
Willy Tarreaudc008c52010-02-01 16:20:08 +01004980 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4981 txn->status == 101)) {
4982 /* Either we've established an explicit tunnel, or we're
4983 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004984 * to understand the next protocols. We have to switch to tunnel
4985 * mode, so that we transfer the request and responses then let
4986 * this protocol pass unmodified. When we later implement specific
4987 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004988 * header which contains information about that protocol for
4989 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004990 */
4991 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4992 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004993 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4994 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4995 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004996 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004997
Willy Tarreau60466522010-01-18 19:08:45 +01004998 /* on unknown transfer length, we must close */
4999 if (!(txn->flags & TX_RES_XFER_LEN) &&
5000 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5001 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005002
Willy Tarreau60466522010-01-18 19:08:45 +01005003 /* now adjust header transformations depending on current state */
5004 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5005 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5006 to_del |= 2; /* remove "keep-alive" on any response */
5007 if (!(txn->flags & TX_RES_VER_11))
5008 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005009 }
Willy Tarreau60466522010-01-18 19:08:45 +01005010 else { /* SCL / KAL */
5011 to_del |= 1; /* remove "close" on any response */
5012 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
5013 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005014 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005015
Willy Tarreau60466522010-01-18 19:08:45 +01005016 /* Parse and remove some headers from the connection header */
5017 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005018
Willy Tarreau60466522010-01-18 19:08:45 +01005019 /* Some keep-alive responses are converted to Server-close if
5020 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005021 */
Willy Tarreau60466522010-01-18 19:08:45 +01005022 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5023 if ((txn->flags & TX_HDR_CONN_CLO) ||
5024 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
5025 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005026 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005027 }
5028
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005029 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005030 /*
5031 * 3: we will have to evaluate the filters.
5032 * As opposed to version 1.2, now they will be evaluated in the
5033 * filters order and not in the header order. This means that
5034 * each filter has to be validated among all headers.
5035 *
5036 * Filters are tried with ->be first, then with ->fe if it is
5037 * different from ->be.
5038 */
5039
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005040 cur_proxy = t->be;
5041 while (1) {
5042 struct proxy *rule_set = cur_proxy;
5043
5044 /* try headers filters */
5045 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005046 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005047 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005048 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005049 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005050 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
5051 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005052 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005053 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005054 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005055 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005056 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01005057 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005058 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005059 if (!(t->flags & SN_ERR_MASK))
5060 t->flags |= SN_ERR_PRXCOND;
5061 if (!(t->flags & SN_FINST_MASK))
5062 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005063 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005064 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005065 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005066
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005067 /* has the response been denied ? */
5068 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01005069 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005070 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005071
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005072 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005073 if (t->listener->counters)
5074 t->listener->counters->denied_resp++;
5075
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005076 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005077 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005078
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005079 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005080 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005081 if (txn->status < 200)
5082 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005083 if (wl->cond) {
5084 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5085 ret = acl_pass(ret);
5086 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5087 ret = !ret;
5088 if (!ret)
5089 continue;
5090 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005091 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005092 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005093 }
5094
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005095 /* check whether we're already working on the frontend */
5096 if (cur_proxy == t->fe)
5097 break;
5098 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005099 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005100
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005101 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005102 * We may be facing a 100-continue response, in which case this
5103 * is not the right response, and we're waiting for the next one.
5104 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005105 * next one.
5106 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005107 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005108 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005109 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005110 msg->msg_state = HTTP_MSG_RPBEFORE;
5111 txn->status = 0;
5112 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5113 return 1;
5114 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005115 else if (unlikely(txn->status < 200))
5116 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005117
5118 /* we don't have any 1xx status code now */
5119
5120 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005121 * 4: check for server cookie.
5122 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005123 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5124 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005125 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005126
Willy Tarreaubaaee002006-06-26 02:48:02 +02005127
Willy Tarreaua15645d2007-03-18 16:22:39 +01005128 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005129 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005130 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005131 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005132 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005133
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005134 /*
5135 * 6: add server cookie in the response if needed
5136 */
Willy Tarreauef4f3912010-10-07 21:00:29 +02005137 if ((t->srv) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005138 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005139 (!(t->flags & SN_DIRECT) ||
5140 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5141 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5142 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5143 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005144 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5145 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005146 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005147 /* the server is known, it's not the one the client requested, or the
5148 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005149 * insert a set-cookie here, except if we want to insert only on POST
5150 * requests and this one isn't. Note that servers which don't have cookies
5151 * (eg: some backup servers) will return a full cookie removal request.
5152 */
Willy Tarreauef4f3912010-10-07 21:00:29 +02005153 if (!t->srv->cookie) {
5154 len = sprintf(trash,
5155 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5156 t->be->cookie_name);
5157 }
5158 else {
5159 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, t->srv->cookie);
5160
5161 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5162 /* emit last_date, which is mandatory */
5163 trash[len++] = COOKIE_DELIM_DATE;
5164 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5165 if (t->be->cookie_maxlife) {
5166 /* emit first_date, which is either the original one or
5167 * the current date.
5168 */
5169 trash[len++] = COOKIE_DELIM_DATE;
5170 s30tob64(txn->cookie_first_date ?
5171 txn->cookie_first_date >> 2 :
5172 (date.tv_sec+3) >> 2, trash + len);
5173 len += 5;
5174 }
5175 }
5176 len += sprintf(trash + len, "; path=/");
5177 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005178
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005179 if (t->be->cookie_domain)
5180 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005181
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005182 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005183 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005184 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005185
Willy Tarreauf1348312010-10-07 15:54:11 +02005186 txn->flags &= ~TX_SCK_MASK;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005187 if (t->srv->cookie && (t->flags & SN_DIRECT))
5188 /* the server did not change, only the date was updated */
5189 txn->flags |= TX_SCK_UPDATED;
5190 else
5191 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005192
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005193 /* Here, we will tell an eventual cache on the client side that we don't
5194 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5195 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5196 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5197 */
5198 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005199
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005200 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5201
5202 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005203 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005204 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005205 }
5206 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005207
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005208 /*
5209 * 7: check if result will be cacheable with a cookie.
5210 * We'll block the response if security checks have caught
5211 * nasty things such as a cacheable cookie.
5212 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005213 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5214 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005215 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005216
5217 /* we're in presence of a cacheable response containing
5218 * a set-cookie header. We'll block it as requested by
5219 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005220 */
Willy Tarreau8365f932009-03-15 23:11:49 +01005221 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005222 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005223
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005224 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005225 if (t->listener->counters)
5226 t->listener->counters->denied_resp++;
5227
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005228 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5229 t->be->id, t->srv?t->srv->id:"<dispatch>");
5230 send_log(t->be, LOG_ALERT,
5231 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5232 t->be->id, t->srv?t->srv->id:"<dispatch>");
5233 goto return_srv_prx_502;
5234 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005235
5236 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005237 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005238 */
Willy Tarreau60466522010-01-18 19:08:45 +01005239 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5240 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5241 unsigned int want_flags = 0;
5242
5243 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5244 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5245 /* we want a keep-alive response here. Keep-alive header
5246 * required if either side is not 1.1.
5247 */
5248 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5249 want_flags |= TX_CON_KAL_SET;
5250 }
5251 else {
5252 /* we want a close response here. Close header required if
5253 * the server is 1.1, regardless of the client.
5254 */
5255 if (txn->flags & TX_RES_VER_11)
5256 want_flags |= TX_CON_CLO_SET;
5257 }
5258
5259 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5260 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005261 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005262
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005263 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005264 if ((txn->flags & TX_RES_XFER_LEN) ||
5265 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005266 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005267
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005268 /*************************************************************
5269 * OK, that's finished for the headers. We have done what we *
5270 * could. Let's switch to the DATA state. *
5271 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005272
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005273 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005274
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005275 /* if the user wants to log as soon as possible, without counting
5276 * bytes from the server, then this is the right moment. We have
5277 * to temporarily assign bytes_out to log what we currently have.
5278 */
5279 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5280 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5281 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005282 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005283 t->logs.bytes_out = 0;
5284 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005285
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005286 /* Note: we must not try to cheat by jumping directly to DATA,
5287 * otherwise we would not let the client side wake up.
5288 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005289
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005290 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005291 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005292 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005293}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005294
Willy Tarreaud98cf932009-12-27 22:54:55 +01005295/* This function is an analyser which forwards response body (including chunk
5296 * sizes if any). It is called as soon as we must forward, even if we forward
5297 * zero byte. The only situation where it must not be called is when we're in
5298 * tunnel mode and we want to forward till the close. It's used both to forward
5299 * remaining data and to resync after end of body. It expects the msg_state to
5300 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5301 * read more data, or 1 once we can go on with next request or end the session.
5302 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
5303 * bytes of pending data + the headers if not already done (between som and sov).
5304 * It eventually adjusts som to match sov after the data in between have been sent.
5305 */
5306int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5307{
5308 struct http_txn *txn = &s->txn;
5309 struct http_msg *msg = &s->txn.rsp;
5310
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005311 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5312 return 0;
5313
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005314 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005315 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005316 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005317 /* Output closed while we were sending data. We must abort and
5318 * wake the other side up.
5319 */
5320 msg->msg_state = HTTP_MSG_ERROR;
5321 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005322 return 1;
5323 }
5324
Willy Tarreau4fe41902010-06-07 22:27:41 +02005325 /* in most states, we should abort in case of early close */
5326 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005327
Willy Tarreaud98cf932009-12-27 22:54:55 +01005328 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5329 /* we have msg->col and msg->sov which both point to the first
5330 * byte of message body. msg->som still points to the beginning
5331 * of the message. We must save the body in req->lr because it
5332 * survives buffer re-alignments.
5333 */
5334 res->lr = res->data + msg->sov;
5335 if (txn->flags & TX_RES_TE_CHNK)
5336 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5337 else {
5338 msg->msg_state = HTTP_MSG_DATA;
5339 }
5340 }
5341
Willy Tarreaud98cf932009-12-27 22:54:55 +01005342 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005343 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005344 /* we may have some data pending */
5345 if (msg->hdr_content_len || msg->som != msg->sov) {
5346 int bytes = msg->sov - msg->som;
5347 if (bytes < 0) /* sov may have wrapped at the end */
5348 bytes += res->size;
5349 buffer_forward(res, bytes + msg->hdr_content_len);
5350 msg->hdr_content_len = 0; /* don't forward that again */
5351 msg->som = msg->sov;
5352 }
5353
Willy Tarreaucaabe412010-01-03 23:08:28 +01005354 if (msg->msg_state == HTTP_MSG_DATA) {
5355 /* must still forward */
5356 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005357 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005358
5359 /* nothing left to forward */
5360 if (txn->flags & TX_RES_TE_CHNK)
5361 msg->msg_state = HTTP_MSG_DATA_CRLF;
5362 else
5363 msg->msg_state = HTTP_MSG_DONE;
5364 }
5365 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005366 /* read the chunk size and assign it to ->hdr_content_len, then
5367 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5368 */
5369 int ret = http_parse_chunk_size(res, msg);
5370
5371 if (!ret)
5372 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005373 else if (ret < 0) {
5374 if (msg->err_pos >= 0)
5375 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005376 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005377 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005378 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreau57f5c122010-12-02 00:37:14 +01005379 /* Don't set a PUSH at the end of that chunk if it's not the last one */
5380 if (msg->msg_state == HTTP_MSG_DATA)
5381 res->flags |= BF_EXPECT_MORE;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005382 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005383 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5384 /* we want the CRLF after the data */
5385 int ret;
5386
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005387 res->lr = res->w + res->send_max;
5388 if (res->lr >= res->data + res->size)
5389 res->lr -= res->size;
5390
Willy Tarreaud98cf932009-12-27 22:54:55 +01005391 ret = http_skip_chunk_crlf(res, msg);
5392
5393 if (!ret)
5394 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005395 else if (ret < 0) {
5396 if (msg->err_pos >= 0)
5397 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005398 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005399 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005400 /* we're in MSG_CHUNK_SIZE now */
5401 }
5402 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5403 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005404
Willy Tarreaud98cf932009-12-27 22:54:55 +01005405 if (ret == 0)
5406 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005407 else if (ret < 0) {
5408 if (msg->err_pos >= 0)
5409 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005410 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005411 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005412 /* we're in HTTP_MSG_DONE now */
5413 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005414 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005415 int old_state = msg->msg_state;
5416
Willy Tarreau610ecce2010-01-04 21:15:02 +01005417 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005418 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005419 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5420 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5421 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005422 if (http_resync_states(s)) {
5423 http_silent_debug(__LINE__, s);
5424 /* some state changes occurred, maybe the analyser
5425 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005426 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005427 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5428 if (res->flags & BF_SHUTW) {
5429 /* response errors are most likely due to
5430 * the client aborting the transfer.
5431 */
5432 if (!(s->flags & SN_ERR_MASK))
5433 s->flags |= SN_ERR_CLICL;
5434 if (!(s->flags & SN_FINST_MASK))
5435 s->flags |= SN_FINST_D;
5436 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005437 if (msg->err_pos >= 0)
5438 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005439 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005440 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005441 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005442 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005443 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005444 }
5445 }
5446
Willy Tarreaud98cf932009-12-27 22:54:55 +01005447 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005448 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005449 if (res->flags & BF_SHUTR) {
5450 if (!(s->flags & SN_ERR_MASK))
5451 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01005452 s->be->counters.srv_aborts++;
5453 if (s->srv)
5454 s->srv->counters.srv_aborts++;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005455 goto return_bad_res;
Willy Tarreau40dba092010-03-04 18:14:51 +01005456 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005457
Willy Tarreau40dba092010-03-04 18:14:51 +01005458 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005459 if (!s->req->analysers)
5460 goto return_bad_res;
5461
Willy Tarreaud98cf932009-12-27 22:54:55 +01005462 /* forward the chunk size as well as any pending data */
5463 if (msg->hdr_content_len || msg->som != msg->sov) {
5464 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
5465 msg->hdr_content_len = 0; /* don't forward that again */
5466 msg->som = msg->sov;
5467 }
5468
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005469 /* When TE: chunked is used, we need to get there again to parse remaining
5470 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5471 * Similarly, with keep-alive on the client side, we don't want to forward a
5472 * close.
5473 */
5474 if ((txn->flags & TX_RES_TE_CHNK) ||
5475 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5476 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5477 buffer_dont_close(res);
5478
Willy Tarreaud98cf932009-12-27 22:54:55 +01005479 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005480 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005481 return 0;
5482
Willy Tarreau40dba092010-03-04 18:14:51 +01005483 return_bad_res: /* let's centralize all bad responses */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005484 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005485 /* don't send any error message as we're in the body */
5486 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005487 res->analysers = 0;
5488 s->be->counters.failed_resp++;
5489 if (s->srv) {
5490 s->srv->counters.failed_resp++;
5491 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
5492 }
5493
5494 if (!(s->flags & SN_ERR_MASK))
5495 s->flags |= SN_ERR_PRXCOND;
5496 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005497 s->flags |= SN_FINST_D;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005498 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005499 return 0;
5500}
5501
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005502/* Iterate the same filter through all request headers.
5503 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005504 * Since it can manage the switch to another backend, it updates the per-proxy
5505 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005506 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005507int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005508{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005509 char term;
5510 char *cur_ptr, *cur_end, *cur_next;
5511 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005512 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005513 struct hdr_idx_elem *cur_hdr;
5514 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005515
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005516 last_hdr = 0;
5517
Willy Tarreau962c3f42010-01-10 00:15:35 +01005518 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005519 old_idx = 0;
5520
5521 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005522 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005523 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005524 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005525 (exp->action == ACT_ALLOW ||
5526 exp->action == ACT_DENY ||
5527 exp->action == ACT_TARPIT))
5528 return 0;
5529
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005530 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005531 if (!cur_idx)
5532 break;
5533
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005534 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005535 cur_ptr = cur_next;
5536 cur_end = cur_ptr + cur_hdr->len;
5537 cur_next = cur_end + cur_hdr->cr + 1;
5538
5539 /* Now we have one header between cur_ptr and cur_end,
5540 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005541 */
5542
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005543 /* The annoying part is that pattern matching needs
5544 * that we modify the contents to null-terminate all
5545 * strings before testing them.
5546 */
5547
5548 term = *cur_end;
5549 *cur_end = '\0';
5550
5551 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5552 switch (exp->action) {
5553 case ACT_SETBE:
5554 /* It is not possible to jump a second time.
5555 * FIXME: should we return an HTTP/500 here so that
5556 * the admin knows there's a problem ?
5557 */
5558 if (t->be != t->fe)
5559 break;
5560
5561 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005562 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005563 last_hdr = 1;
5564 break;
5565
5566 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005567 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005568 last_hdr = 1;
5569 break;
5570
5571 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005572 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005573 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005574
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005575 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005576 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005577 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005578
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005579 break;
5580
5581 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005582 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005583 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005584
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005585 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005586 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005587 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005588
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005589 break;
5590
5591 case ACT_REPLACE:
5592 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5593 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5594 /* FIXME: if the user adds a newline in the replacement, the
5595 * index will not be recalculated for now, and the new line
5596 * will not be counted as a new header.
5597 */
5598
5599 cur_end += delta;
5600 cur_next += delta;
5601 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005602 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005603 break;
5604
5605 case ACT_REMOVE:
5606 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5607 cur_next += delta;
5608
Willy Tarreaufa355d42009-11-29 18:12:29 +01005609 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005610 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5611 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005612 cur_hdr->len = 0;
5613 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005614 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005615 break;
5616
5617 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005618 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005619 if (cur_end)
5620 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005621
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005622 /* keep the link from this header to next one in case of later
5623 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005624 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005625 old_idx = cur_idx;
5626 }
5627 return 0;
5628}
5629
5630
5631/* Apply the filter to the request line.
5632 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5633 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005634 * Since it can manage the switch to another backend, it updates the per-proxy
5635 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005636 */
5637int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5638{
5639 char term;
5640 char *cur_ptr, *cur_end;
5641 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005642 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005643 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005644
Willy Tarreau58f10d72006-12-04 02:26:12 +01005645
Willy Tarreau3d300592007-03-18 18:34:41 +01005646 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005647 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005648 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005649 (exp->action == ACT_ALLOW ||
5650 exp->action == ACT_DENY ||
5651 exp->action == ACT_TARPIT))
5652 return 0;
5653 else if (exp->action == ACT_REMOVE)
5654 return 0;
5655
5656 done = 0;
5657
Willy Tarreau962c3f42010-01-10 00:15:35 +01005658 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005659 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005660
5661 /* Now we have the request line between cur_ptr and cur_end */
5662
5663 /* The annoying part is that pattern matching needs
5664 * that we modify the contents to null-terminate all
5665 * strings before testing them.
5666 */
5667
5668 term = *cur_end;
5669 *cur_end = '\0';
5670
5671 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5672 switch (exp->action) {
5673 case ACT_SETBE:
5674 /* It is not possible to jump a second time.
5675 * FIXME: should we return an HTTP/500 here so that
5676 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005677 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005678 if (t->be != t->fe)
5679 break;
5680
5681 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005682 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005683 done = 1;
5684 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005685
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005686 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005687 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005688 done = 1;
5689 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005690
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005691 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005692 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005693
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005694 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005695 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005696 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005697
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005698 done = 1;
5699 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005700
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005701 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005702 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005703
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005704 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005705 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005706 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005707
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005708 done = 1;
5709 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005710
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005711 case ACT_REPLACE:
5712 *cur_end = term; /* restore the string terminator */
5713 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5714 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5715 /* FIXME: if the user adds a newline in the replacement, the
5716 * index will not be recalculated for now, and the new line
5717 * will not be counted as a new header.
5718 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005719
Willy Tarreaufa355d42009-11-29 18:12:29 +01005720 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005721 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005722 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005723 HTTP_MSG_RQMETH,
5724 cur_ptr, cur_end + 1,
5725 NULL, NULL);
5726 if (unlikely(!cur_end))
5727 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005728
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005729 /* we have a full request and we know that we have either a CR
5730 * or an LF at <ptr>.
5731 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005732 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5733 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005734 /* there is no point trying this regex on headers */
5735 return 1;
5736 }
5737 }
5738 *cur_end = term; /* restore the string terminator */
5739 return done;
5740}
Willy Tarreau97de6242006-12-27 17:18:38 +01005741
Willy Tarreau58f10d72006-12-04 02:26:12 +01005742
Willy Tarreau58f10d72006-12-04 02:26:12 +01005743
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005744/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005745 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005746 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005747 * unparsable request. Since it can manage the switch to another backend, it
5748 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005749 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005750int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005751{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005752 struct http_txn *txn = &s->txn;
5753 struct hdr_exp *exp;
5754
5755 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005756 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005757
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005758 /*
5759 * The interleaving of transformations and verdicts
5760 * makes it difficult to decide to continue or stop
5761 * the evaluation.
5762 */
5763
Willy Tarreau6c123b12010-01-28 20:22:06 +01005764 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5765 break;
5766
Willy Tarreau3d300592007-03-18 18:34:41 +01005767 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005768 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005769 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005770 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005771
5772 /* if this filter had a condition, evaluate it now and skip to
5773 * next filter if the condition does not match.
5774 */
5775 if (exp->cond) {
5776 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5777 ret = acl_pass(ret);
5778 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5779 ret = !ret;
5780
5781 if (!ret)
5782 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005783 }
5784
5785 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005786 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005787 if (unlikely(ret < 0))
5788 return -1;
5789
5790 if (likely(ret == 0)) {
5791 /* The filter did not match the request, it can be
5792 * iterated through all headers.
5793 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005794 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005795 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005796 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005797 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005798}
5799
5800
Willy Tarreaua15645d2007-03-18 16:22:39 +01005801
Willy Tarreau58f10d72006-12-04 02:26:12 +01005802/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005803 * Try to retrieve the server associated to the appsession.
5804 * If the server is found, it's assigned to the session.
5805 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005806void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005807 struct http_txn *txn = &t->txn;
5808 appsess *asession = NULL;
5809 char *sessid_temp = NULL;
5810
Cyril Bontéb21570a2009-11-29 20:04:48 +01005811 if (len > t->be->appsession_len) {
5812 len = t->be->appsession_len;
5813 }
5814
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005815 if (t->be->options2 & PR_O2_AS_REQL) {
5816 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005817 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005818 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005819 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005820 }
5821
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005822 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005823 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5824 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5825 return;
5826 }
5827
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005828 memcpy(txn->sessid, buf, len);
5829 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005830 }
5831
5832 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5833 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5834 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5835 return;
5836 }
5837
Cyril Bontéb21570a2009-11-29 20:04:48 +01005838 memcpy(sessid_temp, buf, len);
5839 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005840
5841 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5842 /* free previously allocated memory */
5843 pool_free2(apools.sessid, sessid_temp);
5844
5845 if (asession != NULL) {
5846 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5847 if (!(t->be->options2 & PR_O2_AS_REQL))
5848 asession->request_count++;
5849
5850 if (asession->serverid != NULL) {
5851 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005852
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005853 while (srv) {
5854 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005855 if ((srv->state & SRV_RUNNING) ||
5856 (t->be->options & PR_O_PERSIST) ||
5857 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005858 /* we found the server and it's usable */
5859 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005860 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005861 t->flags |= SN_DIRECT | SN_ASSIGNED;
5862 t->srv = srv;
5863 break;
5864 } else {
5865 txn->flags &= ~TX_CK_MASK;
5866 txn->flags |= TX_CK_DOWN;
5867 }
5868 }
5869 srv = srv->next;
5870 }
5871 }
5872 }
5873}
5874
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005875/* Find the end of a cookie value contained between <s> and <e>. It works the
5876 * same way as with headers above except that the semi-colon also ends a token.
5877 * See RFC2965 for more information. Note that it requires a valid header to
5878 * return a valid result.
5879 */
5880char *find_cookie_value_end(char *s, const char *e)
5881{
5882 int quoted, qdpair;
5883
5884 quoted = qdpair = 0;
5885 for (; s < e; s++) {
5886 if (qdpair) qdpair = 0;
5887 else if (quoted) {
5888 if (*s == '\\') qdpair = 1;
5889 else if (*s == '"') quoted = 0;
5890 }
5891 else if (*s == '"') quoted = 1;
5892 else if (*s == ',' || *s == ';') return s;
5893 }
5894 return s;
5895}
5896
5897/* Delete a value in a header between delimiters <from> and <next> in buffer
5898 * <buf>. The number of characters displaced is returned, and the pointer to
5899 * the first delimiter is updated if required. The function tries as much as
5900 * possible to respect the following principles :
5901 * - replace <from> delimiter by the <next> one unless <from> points to a
5902 * colon, in which case <next> is simply removed
5903 * - set exactly one space character after the new first delimiter, unless
5904 * there are not enough characters in the block being moved to do so.
5905 * - remove unneeded spaces before the previous delimiter and after the new
5906 * one.
5907 *
5908 * It is the caller's responsibility to ensure that :
5909 * - <from> points to a valid delimiter or the colon ;
5910 * - <next> points to a valid delimiter or the final CR/LF ;
5911 * - there are non-space chars before <from> ;
5912 * - there is a CR/LF at or after <next>.
5913 */
5914int del_hdr_value(struct buffer *buf, char **from, char *next)
5915{
5916 char *prev = *from;
5917
5918 if (*prev == ':') {
5919 /* We're removing the first value, preserve the colon and add a
5920 * space if possible.
5921 */
5922 if (!http_is_crlf[(unsigned char)*next])
5923 next++;
5924 prev++;
5925 if (prev < next)
5926 *prev++ = ' ';
5927
5928 while (http_is_spht[(unsigned char)*next])
5929 next++;
5930 } else {
5931 /* Remove useless spaces before the old delimiter. */
5932 while (http_is_spht[(unsigned char)*(prev-1)])
5933 prev--;
5934 *from = prev;
5935
5936 /* copy the delimiter and if possible a space if we're
5937 * not at the end of the line.
5938 */
5939 if (!http_is_crlf[(unsigned char)*next]) {
5940 *prev++ = *next++;
5941 if (prev + 1 < next)
5942 *prev++ = ' ';
5943 while (http_is_spht[(unsigned char)*next])
5944 next++;
5945 }
5946 }
5947 return buffer_replace2(buf, prev, next, NULL, 0);
5948}
5949
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005950/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005951 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005952 * desirable to call it only when needed. This code is quite complex because
5953 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5954 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005955 */
5956void manage_client_side_cookies(struct session *t, struct buffer *req)
5957{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005958 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005959 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005960 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005961 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5962 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005963
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005964 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005965 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005966 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005967
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005968 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005969 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005970 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005971
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005972 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005973 hdr_beg = hdr_next;
5974 hdr_end = hdr_beg + cur_hdr->len;
5975 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005976
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005977 /* We have one full header between hdr_beg and hdr_end, and the
5978 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005979 * "Cookie:" headers.
5980 */
5981
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005982 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005983 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005984 old_idx = cur_idx;
5985 continue;
5986 }
5987
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005988 del_from = NULL; /* nothing to be deleted */
5989 preserve_hdr = 0; /* assume we may kill the whole header */
5990
Willy Tarreau58f10d72006-12-04 02:26:12 +01005991 /* Now look for cookies. Conforming to RFC2109, we have to support
5992 * attributes whose name begin with a '$', and associate them with
5993 * the right cookie, if we want to delete this cookie.
5994 * So there are 3 cases for each cookie read :
5995 * 1) it's a special attribute, beginning with a '$' : ignore it.
5996 * 2) it's a server id cookie that we *MAY* want to delete : save
5997 * some pointers on it (last semi-colon, beginning of cookie...)
5998 * 3) it's an application cookie : we *MAY* have to delete a previous
5999 * "special" cookie.
6000 * At the end of loop, if a "special" cookie remains, we may have to
6001 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006002 * *MUST* delete it.
6003 *
6004 * Note: RFC2965 is unclear about the processing of spaces around
6005 * the equal sign in the ATTR=VALUE form. A careful inspection of
6006 * the RFC explicitly allows spaces before it, and not within the
6007 * tokens (attrs or values). An inspection of RFC2109 allows that
6008 * too but section 10.1.3 lets one think that spaces may be allowed
6009 * after the equal sign too, resulting in some (rare) buggy
6010 * implementations trying to do that. So let's do what servers do.
6011 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6012 * allowed quoted strings in values, with any possible character
6013 * after a backslash, including control chars and delimitors, which
6014 * causes parsing to become ambiguous. Browsers also allow spaces
6015 * within values even without quotes.
6016 *
6017 * We have to keep multiple pointers in order to support cookie
6018 * removal at the beginning, middle or end of header without
6019 * corrupting the header. All of these headers are valid :
6020 *
6021 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6022 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6023 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6024 * | | | | | | | | |
6025 * | | | | | | | | hdr_end <--+
6026 * | | | | | | | +--> next
6027 * | | | | | | +----> val_end
6028 * | | | | | +-----------> val_beg
6029 * | | | | +--------------> equal
6030 * | | | +----------------> att_end
6031 * | | +---------------------> att_beg
6032 * | +--------------------------> prev
6033 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006034 */
6035
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006036 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6037 /* Iterate through all cookies on this line */
6038
6039 /* find att_beg */
6040 att_beg = prev + 1;
6041 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6042 att_beg++;
6043
6044 /* find att_end : this is the first character after the last non
6045 * space before the equal. It may be equal to hdr_end.
6046 */
6047 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006048
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006049 while (equal < hdr_end) {
6050 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006051 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006052 if (http_is_spht[(unsigned char)*equal++])
6053 continue;
6054 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006055 }
6056
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006057 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6058 * is between <att_beg> and <equal>, both may be identical.
6059 */
6060
6061 /* look for end of cookie if there is an equal sign */
6062 if (equal < hdr_end && *equal == '=') {
6063 /* look for the beginning of the value */
6064 val_beg = equal + 1;
6065 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6066 val_beg++;
6067
6068 /* find the end of the value, respecting quotes */
6069 next = find_cookie_value_end(val_beg, hdr_end);
6070
6071 /* make val_end point to the first white space or delimitor after the value */
6072 val_end = next;
6073 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6074 val_end--;
6075 } else {
6076 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006077 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006078
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006079 /* We have nothing to do with attributes beginning with '$'. However,
6080 * they will automatically be removed if a header before them is removed,
6081 * since they're supposed to be linked together.
6082 */
6083 if (*att_beg == '$')
6084 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006085
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006086 /* Ignore cookies with no equal sign */
6087 if (equal == next) {
6088 /* This is not our cookie, so we must preserve it. But if we already
6089 * scheduled another cookie for removal, we cannot remove the
6090 * complete header, but we can remove the previous block itself.
6091 */
6092 preserve_hdr = 1;
6093 if (del_from != NULL) {
6094 int delta = del_hdr_value(req, &del_from, prev);
6095 val_end += delta;
6096 next += delta;
6097 hdr_end += delta;
6098 hdr_next += delta;
6099 cur_hdr->len += delta;
6100 http_msg_move_end(&txn->req, delta);
6101 prev = del_from;
6102 del_from = NULL;
6103 }
6104 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006105 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006106
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006107 /* if there are spaces around the equal sign, we need to
6108 * strip them otherwise we'll get trouble for cookie captures,
6109 * or even for rewrites. Since this happens extremely rarely,
6110 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006111 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006112 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6113 int stripped_before = 0;
6114 int stripped_after = 0;
6115
6116 if (att_end != equal) {
6117 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6118 equal += stripped_before;
6119 val_beg += stripped_before;
6120 }
6121
6122 if (val_beg > equal + 1) {
6123 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6124 val_beg += stripped_after;
6125 stripped_before += stripped_after;
6126 }
6127
6128 val_end += stripped_before;
6129 next += stripped_before;
6130 hdr_end += stripped_before;
6131 hdr_next += stripped_before;
6132 cur_hdr->len += stripped_before;
6133 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006134 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006135 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006136
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006137 /* First, let's see if we want to capture this cookie. We check
6138 * that we don't already have a client side cookie, because we
6139 * can only capture one. Also as an optimisation, we ignore
6140 * cookies shorter than the declared name.
6141 */
6142 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6143 (val_end - att_beg >= t->fe->capture_namelen) &&
6144 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6145 int log_len = val_end - att_beg;
6146
6147 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6148 Alert("HTTP logging : out of memory.\n");
6149 } else {
6150 if (log_len > t->fe->capture_len)
6151 log_len = t->fe->capture_len;
6152 memcpy(txn->cli_cookie, att_beg, log_len);
6153 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006154 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006155 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006156
Willy Tarreaubca99692010-10-06 19:25:55 +02006157 /* Persistence cookies in passive, rewrite or insert mode have the
6158 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006159 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006160 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006161 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006162 * For cookies in prefix mode, the form is :
6163 *
6164 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006165 */
6166 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6167 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6168 struct server *srv = t->be->srv;
6169 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006170
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006171 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6172 * have the server ID between val_beg and delim, and the original cookie between
6173 * delim+1 and val_end. Otherwise, delim==val_end :
6174 *
6175 * Cookie: NAME=SRV; # in all but prefix modes
6176 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6177 * | || || | |+-> next
6178 * | || || | +--> val_end
6179 * | || || +---------> delim
6180 * | || |+------------> val_beg
6181 * | || +-------------> att_end = equal
6182 * | |+-----------------> att_beg
6183 * | +------------------> prev
6184 * +-------------------------> hdr_beg
6185 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006186
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006187 if (t->be->options & PR_O_COOK_PFX) {
6188 for (delim = val_beg; delim < val_end; delim++)
6189 if (*delim == COOKIE_DELIM)
6190 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006191 } else {
6192 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006193 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006194 /* Now check if the cookie contains a date field, which would
6195 * appear after a vertical bar ('|') just after the server name
6196 * and before the delimiter.
6197 */
6198 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6199 if (vbar1) {
6200 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006201 * right is the last seen date. It is a base64 encoded
6202 * 30-bit value representing the UNIX date since the
6203 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006204 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006205 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006206 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006207 if (val_end - vbar1 >= 5) {
6208 val = b64tos30(vbar1);
6209 if (val > 0)
6210 txn->cookie_last_date = val << 2;
6211 }
6212 /* look for a second vertical bar */
6213 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6214 if (vbar1 && (val_end - vbar1 > 5)) {
6215 val = b64tos30(vbar1 + 1);
6216 if (val > 0)
6217 txn->cookie_first_date = val << 2;
6218 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006219 }
6220 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006221
Willy Tarreauf64d1412010-10-07 20:06:11 +02006222 /* if the cookie has an expiration date and the proxy wants to check
6223 * it, then we do that now. We first check if the cookie is too old,
6224 * then only if it has expired. We detect strict overflow because the
6225 * time resolution here is not great (4 seconds). Cookies with dates
6226 * in the future are ignored if their offset is beyond one day. This
6227 * allows an admin to fix timezone issues without expiring everyone
6228 * and at the same time avoids keeping unwanted side effects for too
6229 * long.
6230 */
6231 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006232 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6233 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006234 txn->flags &= ~TX_CK_MASK;
6235 txn->flags |= TX_CK_OLD;
6236 delim = val_beg; // let's pretend we have not found the cookie
6237 txn->cookie_first_date = 0;
6238 txn->cookie_last_date = 0;
6239 }
6240 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006241 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6242 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006243 txn->flags &= ~TX_CK_MASK;
6244 txn->flags |= TX_CK_EXPIRED;
6245 delim = val_beg; // let's pretend we have not found the cookie
6246 txn->cookie_first_date = 0;
6247 txn->cookie_last_date = 0;
6248 }
6249
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006250 /* Here, we'll look for the first running server which supports the cookie.
6251 * This allows to share a same cookie between several servers, for example
6252 * to dedicate backup servers to specific servers only.
6253 * However, to prevent clients from sticking to cookie-less backup server
6254 * when they have incidentely learned an empty cookie, we simply ignore
6255 * empty cookies and mark them as invalid.
6256 * The same behaviour is applied when persistence must be ignored.
6257 */
6258 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6259 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006260
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006261 while (srv) {
6262 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6263 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6264 if ((srv->state & SRV_RUNNING) ||
6265 (t->be->options & PR_O_PERSIST) ||
6266 (t->flags & SN_FORCE_PRST)) {
6267 /* we found the server and we can use it */
6268 txn->flags &= ~TX_CK_MASK;
6269 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6270 t->flags |= SN_DIRECT | SN_ASSIGNED;
6271 t->srv = srv;
6272 break;
6273 } else {
6274 /* we found a server, but it's down,
6275 * mark it as such and go on in case
6276 * another one is available.
6277 */
6278 txn->flags &= ~TX_CK_MASK;
6279 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006280 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006281 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006282 srv = srv->next;
6283 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006284
Willy Tarreauf64d1412010-10-07 20:06:11 +02006285 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006286 /* no server matched this cookie */
6287 txn->flags &= ~TX_CK_MASK;
6288 txn->flags |= TX_CK_INVALID;
6289 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006290
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006291 /* depending on the cookie mode, we may have to either :
6292 * - delete the complete cookie if we're in insert+indirect mode, so that
6293 * the server never sees it ;
6294 * - remove the server id from the cookie value, and tag the cookie as an
6295 * application cookie so that it does not get accidentely removed later,
6296 * if we're in cookie prefix mode
6297 */
6298 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6299 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006300
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006301 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6302 val_end += delta;
6303 next += delta;
6304 hdr_end += delta;
6305 hdr_next += delta;
6306 cur_hdr->len += delta;
6307 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006308
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006309 del_from = NULL;
6310 preserve_hdr = 1; /* we want to keep this cookie */
6311 }
6312 else if (del_from == NULL &&
6313 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6314 del_from = prev;
6315 }
6316 } else {
6317 /* This is not our cookie, so we must preserve it. But if we already
6318 * scheduled another cookie for removal, we cannot remove the
6319 * complete header, but we can remove the previous block itself.
6320 */
6321 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006322
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006323 if (del_from != NULL) {
6324 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006325 if (att_beg >= del_from)
6326 att_beg += delta;
6327 if (att_end >= del_from)
6328 att_end += delta;
6329 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006330 val_end += delta;
6331 next += delta;
6332 hdr_end += delta;
6333 hdr_next += delta;
6334 cur_hdr->len += delta;
6335 http_msg_move_end(&txn->req, delta);
6336 prev = del_from;
6337 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006338 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006339 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006340
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006341 /* Look for the appsession cookie unless persistence must be ignored */
6342 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6343 int cmp_len, value_len;
6344 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006345
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006346 if (t->be->options2 & PR_O2_AS_PFX) {
6347 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6348 value_begin = att_beg + t->be->appsession_name_len;
6349 value_len = val_end - att_beg - t->be->appsession_name_len;
6350 } else {
6351 cmp_len = att_end - att_beg;
6352 value_begin = val_beg;
6353 value_len = val_end - val_beg;
6354 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006355
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006356 /* let's see if the cookie is our appcookie */
6357 if (cmp_len == t->be->appsession_name_len &&
6358 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6359 manage_client_side_appsession(t, value_begin, value_len);
6360 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006361 }
6362
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006363 /* continue with next cookie on this header line */
6364 att_beg = next;
6365 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006366
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006367 /* There are no more cookies on this line.
6368 * We may still have one (or several) marked for deletion at the
6369 * end of the line. We must do this now in two ways :
6370 * - if some cookies must be preserved, we only delete from the
6371 * mark to the end of line ;
6372 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006373 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006374 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006375 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006376 if (preserve_hdr) {
6377 delta = del_hdr_value(req, &del_from, hdr_end);
6378 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006379 cur_hdr->len += delta;
6380 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006381 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006382
6383 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006384 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6385 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006386 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006387 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006388 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006389 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006390 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006391 }
6392
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006393 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006394 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006395 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006396}
6397
6398
Willy Tarreaua15645d2007-03-18 16:22:39 +01006399/* Iterate the same filter through all response headers contained in <rtr>.
6400 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6401 */
6402int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6403{
6404 char term;
6405 char *cur_ptr, *cur_end, *cur_next;
6406 int cur_idx, old_idx, last_hdr;
6407 struct http_txn *txn = &t->txn;
6408 struct hdr_idx_elem *cur_hdr;
6409 int len, delta;
6410
6411 last_hdr = 0;
6412
Willy Tarreau962c3f42010-01-10 00:15:35 +01006413 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006414 old_idx = 0;
6415
6416 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006417 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006418 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006419 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006420 (exp->action == ACT_ALLOW ||
6421 exp->action == ACT_DENY))
6422 return 0;
6423
6424 cur_idx = txn->hdr_idx.v[old_idx].next;
6425 if (!cur_idx)
6426 break;
6427
6428 cur_hdr = &txn->hdr_idx.v[cur_idx];
6429 cur_ptr = cur_next;
6430 cur_end = cur_ptr + cur_hdr->len;
6431 cur_next = cur_end + cur_hdr->cr + 1;
6432
6433 /* Now we have one header between cur_ptr and cur_end,
6434 * and the next header starts at cur_next.
6435 */
6436
6437 /* The annoying part is that pattern matching needs
6438 * that we modify the contents to null-terminate all
6439 * strings before testing them.
6440 */
6441
6442 term = *cur_end;
6443 *cur_end = '\0';
6444
6445 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6446 switch (exp->action) {
6447 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006448 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006449 last_hdr = 1;
6450 break;
6451
6452 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006453 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006454 last_hdr = 1;
6455 break;
6456
6457 case ACT_REPLACE:
6458 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6459 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6460 /* FIXME: if the user adds a newline in the replacement, the
6461 * index will not be recalculated for now, and the new line
6462 * will not be counted as a new header.
6463 */
6464
6465 cur_end += delta;
6466 cur_next += delta;
6467 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006468 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006469 break;
6470
6471 case ACT_REMOVE:
6472 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6473 cur_next += delta;
6474
Willy Tarreaufa355d42009-11-29 18:12:29 +01006475 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006476 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6477 txn->hdr_idx.used--;
6478 cur_hdr->len = 0;
6479 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006480 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006481 break;
6482
6483 }
6484 }
6485 if (cur_end)
6486 *cur_end = term; /* restore the string terminator */
6487
6488 /* keep the link from this header to next one in case of later
6489 * removal of next header.
6490 */
6491 old_idx = cur_idx;
6492 }
6493 return 0;
6494}
6495
6496
6497/* Apply the filter to the status line in the response buffer <rtr>.
6498 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6499 * or -1 if a replacement resulted in an invalid status line.
6500 */
6501int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6502{
6503 char term;
6504 char *cur_ptr, *cur_end;
6505 int done;
6506 struct http_txn *txn = &t->txn;
6507 int len, delta;
6508
6509
Willy Tarreau3d300592007-03-18 18:34:41 +01006510 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006511 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006512 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006513 (exp->action == ACT_ALLOW ||
6514 exp->action == ACT_DENY))
6515 return 0;
6516 else if (exp->action == ACT_REMOVE)
6517 return 0;
6518
6519 done = 0;
6520
Willy Tarreau962c3f42010-01-10 00:15:35 +01006521 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006522 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006523
6524 /* Now we have the status line between cur_ptr and cur_end */
6525
6526 /* The annoying part is that pattern matching needs
6527 * that we modify the contents to null-terminate all
6528 * strings before testing them.
6529 */
6530
6531 term = *cur_end;
6532 *cur_end = '\0';
6533
6534 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6535 switch (exp->action) {
6536 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006537 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006538 done = 1;
6539 break;
6540
6541 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006542 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006543 done = 1;
6544 break;
6545
6546 case ACT_REPLACE:
6547 *cur_end = term; /* restore the string terminator */
6548 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6549 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6550 /* FIXME: if the user adds a newline in the replacement, the
6551 * index will not be recalculated for now, and the new line
6552 * will not be counted as a new header.
6553 */
6554
Willy Tarreaufa355d42009-11-29 18:12:29 +01006555 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006556 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006557 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006558 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006559 cur_ptr, cur_end + 1,
6560 NULL, NULL);
6561 if (unlikely(!cur_end))
6562 return -1;
6563
6564 /* we have a full respnse and we know that we have either a CR
6565 * or an LF at <ptr>.
6566 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006567 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006568 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006569 /* there is no point trying this regex on headers */
6570 return 1;
6571 }
6572 }
6573 *cur_end = term; /* restore the string terminator */
6574 return done;
6575}
6576
6577
6578
6579/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006580 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006581 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6582 * unparsable response.
6583 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006584int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006585{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006586 struct http_txn *txn = &s->txn;
6587 struct hdr_exp *exp;
6588
6589 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006590 int ret;
6591
6592 /*
6593 * The interleaving of transformations and verdicts
6594 * makes it difficult to decide to continue or stop
6595 * the evaluation.
6596 */
6597
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006598 if (txn->flags & TX_SVDENY)
6599 break;
6600
Willy Tarreau3d300592007-03-18 18:34:41 +01006601 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006602 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6603 exp->action == ACT_PASS)) {
6604 exp = exp->next;
6605 continue;
6606 }
6607
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006608 /* if this filter had a condition, evaluate it now and skip to
6609 * next filter if the condition does not match.
6610 */
6611 if (exp->cond) {
6612 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6613 ret = acl_pass(ret);
6614 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6615 ret = !ret;
6616 if (!ret)
6617 continue;
6618 }
6619
Willy Tarreaua15645d2007-03-18 16:22:39 +01006620 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006621 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006622 if (unlikely(ret < 0))
6623 return -1;
6624
6625 if (likely(ret == 0)) {
6626 /* The filter did not match the response, it can be
6627 * iterated through all headers.
6628 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006629 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006630 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006631 }
6632 return 0;
6633}
6634
6635
Willy Tarreaua15645d2007-03-18 16:22:39 +01006636/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006637 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006638 * desirable to call it only when needed. This function is also used when we
6639 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006640 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006641void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006642{
6643 struct http_txn *txn = &t->txn;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006644 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006645 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006646 char *hdr_beg, *hdr_end, *hdr_next;
6647 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006648
Willy Tarreaua15645d2007-03-18 16:22:39 +01006649 /* Iterate through the headers.
6650 * we start with the start line.
6651 */
6652 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006653 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006654
6655 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6656 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006657 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006658
6659 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006660 hdr_beg = hdr_next;
6661 hdr_end = hdr_beg + cur_hdr->len;
6662 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006663
Willy Tarreau24581ba2010-08-31 22:39:35 +02006664 /* We have one full header between hdr_beg and hdr_end, and the
6665 * next header starts at hdr_next. We're only interested in
6666 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006667 */
6668
Willy Tarreau24581ba2010-08-31 22:39:35 +02006669 is_cookie2 = 0;
6670 prev = hdr_beg + 10;
6671 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006672 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006673 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6674 if (!val) {
6675 old_idx = cur_idx;
6676 continue;
6677 }
6678 is_cookie2 = 1;
6679 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006680 }
6681
Willy Tarreau24581ba2010-08-31 22:39:35 +02006682 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6683 * <prev> points to the colon.
6684 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006685 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006686
Willy Tarreau24581ba2010-08-31 22:39:35 +02006687 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6688 * check-cache is enabled) and we are not interested in checking
6689 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006690 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006691 if (t->be->cookie_name == NULL &&
6692 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006693 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006694 return;
6695
Willy Tarreau24581ba2010-08-31 22:39:35 +02006696 /* OK so now we know we have to process this response cookie.
6697 * The format of the Set-Cookie header is slightly different
6698 * from the format of the Cookie header in that it does not
6699 * support the comma as a cookie delimiter (thus the header
6700 * cannot be folded) because the Expires attribute described in
6701 * the original Netscape's spec may contain an unquoted date
6702 * with a comma inside. We have to live with this because
6703 * many browsers don't support Max-Age and some browsers don't
6704 * support quoted strings. However the Set-Cookie2 header is
6705 * clean.
6706 *
6707 * We have to keep multiple pointers in order to support cookie
6708 * removal at the beginning, middle or end of header without
6709 * corrupting the header (in case of set-cookie2). A special
6710 * pointer, <scav> points to the beginning of the set-cookie-av
6711 * fields after the first semi-colon. The <next> pointer points
6712 * either to the end of line (set-cookie) or next unquoted comma
6713 * (set-cookie2). All of these headers are valid :
6714 *
6715 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6716 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6717 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6718 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6719 * | | | | | | | | | |
6720 * | | | | | | | | +-> next hdr_end <--+
6721 * | | | | | | | +------------> scav
6722 * | | | | | | +--------------> val_end
6723 * | | | | | +--------------------> val_beg
6724 * | | | | +----------------------> equal
6725 * | | | +------------------------> att_end
6726 * | | +----------------------------> att_beg
6727 * | +------------------------------> prev
6728 * +-----------------------------------------> hdr_beg
6729 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006730
Willy Tarreau24581ba2010-08-31 22:39:35 +02006731 for (; prev < hdr_end; prev = next) {
6732 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006733
Willy Tarreau24581ba2010-08-31 22:39:35 +02006734 /* find att_beg */
6735 att_beg = prev + 1;
6736 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6737 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006738
Willy Tarreau24581ba2010-08-31 22:39:35 +02006739 /* find att_end : this is the first character after the last non
6740 * space before the equal. It may be equal to hdr_end.
6741 */
6742 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006743
Willy Tarreau24581ba2010-08-31 22:39:35 +02006744 while (equal < hdr_end) {
6745 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6746 break;
6747 if (http_is_spht[(unsigned char)*equal++])
6748 continue;
6749 att_end = equal;
6750 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006751
Willy Tarreau24581ba2010-08-31 22:39:35 +02006752 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6753 * is between <att_beg> and <equal>, both may be identical.
6754 */
6755
6756 /* look for end of cookie if there is an equal sign */
6757 if (equal < hdr_end && *equal == '=') {
6758 /* look for the beginning of the value */
6759 val_beg = equal + 1;
6760 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6761 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006762
Willy Tarreau24581ba2010-08-31 22:39:35 +02006763 /* find the end of the value, respecting quotes */
6764 next = find_cookie_value_end(val_beg, hdr_end);
6765
6766 /* make val_end point to the first white space or delimitor after the value */
6767 val_end = next;
6768 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6769 val_end--;
6770 } else {
6771 /* <equal> points to next comma, semi-colon or EOL */
6772 val_beg = val_end = next = equal;
6773 }
6774
6775 if (next < hdr_end) {
6776 /* Set-Cookie2 supports multiple cookies, and <next> points to
6777 * a colon or semi-colon before the end. So skip all attr-value
6778 * pairs and look for the next comma. For Set-Cookie, since
6779 * commas are permitted in values, skip to the end.
6780 */
6781 if (is_cookie2)
6782 next = find_hdr_value_end(next, hdr_end);
6783 else
6784 next = hdr_end;
6785 }
6786
6787 /* Now everything is as on the diagram above */
6788
6789 /* Ignore cookies with no equal sign */
6790 if (equal == val_end)
6791 continue;
6792
6793 /* If there are spaces around the equal sign, we need to
6794 * strip them otherwise we'll get trouble for cookie captures,
6795 * or even for rewrites. Since this happens extremely rarely,
6796 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006797 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006798 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6799 int stripped_before = 0;
6800 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006801
Willy Tarreau24581ba2010-08-31 22:39:35 +02006802 if (att_end != equal) {
6803 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6804 equal += stripped_before;
6805 val_beg += stripped_before;
6806 }
6807
6808 if (val_beg > equal + 1) {
6809 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6810 val_beg += stripped_after;
6811 stripped_before += stripped_after;
6812 }
6813
6814 val_end += stripped_before;
6815 next += stripped_before;
6816 hdr_end += stripped_before;
6817 hdr_next += stripped_before;
6818 cur_hdr->len += stripped_before;
6819 http_msg_move_end(&txn->req, stripped_before);
6820 }
6821
6822 /* First, let's see if we want to capture this cookie. We check
6823 * that we don't already have a server side cookie, because we
6824 * can only capture one. Also as an optimisation, we ignore
6825 * cookies shorter than the declared name.
6826 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006827 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006828 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006829 (val_end - att_beg >= t->fe->capture_namelen) &&
6830 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6831 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006832 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833 Alert("HTTP logging : out of memory.\n");
6834 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006835 else {
6836 if (log_len > t->fe->capture_len)
6837 log_len = t->fe->capture_len;
6838 memcpy(txn->srv_cookie, att_beg, log_len);
6839 txn->srv_cookie[log_len] = 0;
6840 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006841 }
6842
6843 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006844 if (!(t->flags & SN_IGNORE_PRST) &&
6845 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6846 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006847 /* assume passive cookie by default */
6848 txn->flags &= ~TX_SCK_MASK;
6849 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006850
6851 /* If the cookie is in insert mode on a known server, we'll delete
6852 * this occurrence because we'll insert another one later.
6853 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006854 * a direct access.
6855 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006856 if (t->be->options2 & PR_O2_COOK_PSV) {
6857 /* The "preserve" flag was set, we don't want to touch the
6858 * server's cookie.
6859 */
6860 }
6861 else if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006862 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006863 /* this cookie must be deleted */
6864 if (*prev == ':' && next == hdr_end) {
6865 /* whole header */
6866 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6867 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6868 txn->hdr_idx.used--;
6869 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006870 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006871 hdr_next += delta;
6872 http_msg_move_end(&txn->rsp, delta);
6873 /* note: while both invalid now, <next> and <hdr_end>
6874 * are still equal, so the for() will stop as expected.
6875 */
6876 } else {
6877 /* just remove the value */
6878 int delta = del_hdr_value(res, &prev, next);
6879 next = prev;
6880 hdr_end += delta;
6881 hdr_next += delta;
6882 cur_hdr->len += delta;
6883 http_msg_move_end(&txn->rsp, delta);
6884 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006885 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006886 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006887 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006888 }
6889 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006890 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006891 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006892 * with this server since we know it.
6893 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006894 delta = buffer_replace2(res, val_beg, val_end, t->srv->cookie, t->srv->cklen);
6895 next += delta;
6896 hdr_end += delta;
6897 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006898 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006899 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006900
Willy Tarreauf1348312010-10-07 15:54:11 +02006901 txn->flags &= ~TX_SCK_MASK;
6902 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006903 }
6904 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006905 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006906 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006907 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006908 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006909 delta = buffer_replace2(res, val_beg, val_beg, t->srv->cookie, t->srv->cklen + 1);
6910 next += delta;
6911 hdr_end += delta;
6912 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006913 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006914 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006915
Willy Tarreau24581ba2010-08-31 22:39:35 +02006916 val_beg[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006917 txn->flags &= ~TX_SCK_MASK;
6918 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006919 }
6920 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006921 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6922 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006923 int cmp_len, value_len;
6924 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006925
Cyril Bontéb21570a2009-11-29 20:04:48 +01006926 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006927 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6928 value_begin = att_beg + t->be->appsession_name_len;
6929 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006930 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006931 cmp_len = att_end - att_beg;
6932 value_begin = val_beg;
6933 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006934 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006935
Cyril Bonté17530c32010-04-06 21:11:10 +02006936 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006937 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6938 /* free a possibly previously allocated memory */
6939 pool_free2(apools.sessid, txn->sessid);
6940
Cyril Bontéb21570a2009-11-29 20:04:48 +01006941 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006942 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006943 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6944 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6945 return;
6946 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006947 memcpy(txn->sessid, value_begin, value_len);
6948 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006949 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006950 }
6951 /* that's done for this cookie, check the next one on the same
6952 * line when next != hdr_end (only if is_cookie2).
6953 */
6954 }
6955 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006956 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006957 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006958
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006959 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006960 appsess *asession = NULL;
6961 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006962 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006963 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006964 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006965 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6966 Alert("Not enough Memory process_srv():asession:calloc().\n");
6967 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6968 return;
6969 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006970 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6971
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006972 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6973 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6974 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006975 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006976 return;
6977 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006978 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006979 asession->sessid[t->be->appsession_len] = 0;
6980
Willy Tarreau1fac7532010-01-09 19:23:06 +01006981 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006982 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006983 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006984 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006985 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006986 return;
6987 }
6988 asession->serverid[0] = '\0';
6989 memcpy(asession->serverid, t->srv->id, server_id_len);
6990
6991 asession->request_count = 0;
6992 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6993 }
6994
6995 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6996 asession->request_count++;
6997 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006998}
6999
7000
Willy Tarreaua15645d2007-03-18 16:22:39 +01007001/*
7002 * Check if response is cacheable or not. Updates t->flags.
7003 */
7004void check_response_for_cacheability(struct session *t, struct buffer *rtr)
7005{
7006 struct http_txn *txn = &t->txn;
7007 char *p1, *p2;
7008
7009 char *cur_ptr, *cur_end, *cur_next;
7010 int cur_idx;
7011
Willy Tarreau5df51872007-11-25 16:20:08 +01007012 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007013 return;
7014
7015 /* Iterate through the headers.
7016 * we start with the start line.
7017 */
7018 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007019 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007020
7021 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7022 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007023 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007024
7025 cur_hdr = &txn->hdr_idx.v[cur_idx];
7026 cur_ptr = cur_next;
7027 cur_end = cur_ptr + cur_hdr->len;
7028 cur_next = cur_end + cur_hdr->cr + 1;
7029
7030 /* We have one full header between cur_ptr and cur_end, and the
7031 * next header starts at cur_next. We're only interested in
7032 * "Cookie:" headers.
7033 */
7034
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007035 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7036 if (val) {
7037 if ((cur_end - (cur_ptr + val) >= 8) &&
7038 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7039 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7040 return;
7041 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007042 }
7043
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007044 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7045 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007046 continue;
7047
7048 /* OK, right now we know we have a cache-control header at cur_ptr */
7049
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007050 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007051
7052 if (p1 >= cur_end) /* no more info */
7053 continue;
7054
7055 /* p1 is at the beginning of the value */
7056 p2 = p1;
7057
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007058 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007059 p2++;
7060
7061 /* we have a complete value between p1 and p2 */
7062 if (p2 < cur_end && *p2 == '=') {
7063 /* we have something of the form no-cache="set-cookie" */
7064 if ((cur_end - p1 >= 21) &&
7065 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7066 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007067 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007068 continue;
7069 }
7070
7071 /* OK, so we know that either p2 points to the end of string or to a comma */
7072 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7073 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7074 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7075 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007076 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007077 return;
7078 }
7079
7080 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007081 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007082 continue;
7083 }
7084 }
7085}
7086
7087
Willy Tarreau58f10d72006-12-04 02:26:12 +01007088/*
7089 * Try to retrieve a known appsession in the URI, then the associated server.
7090 * If the server is found, it's assigned to the session.
7091 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007092void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007093{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007094 char *end_params, *first_param, *cur_param, *next_param;
7095 char separator;
7096 int value_len;
7097
7098 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007099
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007100 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007101 (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 +01007102 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007103 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007104
Cyril Bontéb21570a2009-11-29 20:04:48 +01007105 first_param = NULL;
7106 switch (mode) {
7107 case PR_O2_AS_M_PP:
7108 first_param = memchr(begin, ';', len);
7109 break;
7110 case PR_O2_AS_M_QS:
7111 first_param = memchr(begin, '?', len);
7112 break;
7113 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007114
Cyril Bontéb21570a2009-11-29 20:04:48 +01007115 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007116 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007117 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007118
Cyril Bontéb21570a2009-11-29 20:04:48 +01007119 switch (mode) {
7120 case PR_O2_AS_M_PP:
7121 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7122 end_params = (char *) begin + len;
7123 }
7124 separator = ';';
7125 break;
7126 case PR_O2_AS_M_QS:
7127 end_params = (char *) begin + len;
7128 separator = '&';
7129 break;
7130 default:
7131 /* unknown mode, shouldn't happen */
7132 return;
7133 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007134
Cyril Bontéb21570a2009-11-29 20:04:48 +01007135 cur_param = next_param = end_params;
7136 while (cur_param > first_param) {
7137 cur_param--;
7138 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7139 /* let's see if this is the appsession parameter */
7140 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7141 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7142 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7143 /* Cool... it's the right one */
7144 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7145 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7146 if (value_len > 0) {
7147 manage_client_side_appsession(t, cur_param, value_len);
7148 }
7149 break;
7150 }
7151 next_param = cur_param;
7152 }
7153 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007154#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007155 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007156 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007157#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007158}
7159
Willy Tarreaub2513902006-12-17 14:52:38 +01007160/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007161 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007162 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007163 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007164 * It is assumed that the request is either a HEAD, GET, or POST and that the
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007165 * t->be->uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007166 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007167 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007168 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007169int stats_check_uri(struct session *t, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007170{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007171 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01007172 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007173 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007174
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007175 if (!uri_auth)
7176 return 0;
7177
Cyril Bonté70be45d2010-10-12 00:14:35 +02007178 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007179 return 0;
7180
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007181 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
7182
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007183 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007184 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007185 return 0;
7186
Willy Tarreau962c3f42010-01-10 00:15:35 +01007187 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007188
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007189 /* the URI is in h */
7190 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007191 return 0;
7192
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007193 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007194 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007195 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007196 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007197 break;
7198 }
7199 h++;
7200 }
7201
7202 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007203 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7204 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007205 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007206 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007207 break;
7208 }
7209 h++;
7210 }
7211 }
7212
Willy Tarreau962c3f42010-01-10 00:15:35 +01007213 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7214 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007215 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007216 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007217 break;
7218 }
7219 h++;
7220 }
7221
Cyril Bonté70be45d2010-10-12 00:14:35 +02007222 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7223 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7224 if (memcmp(h, ";st=", 4) == 0) {
7225 h += 4;
7226
7227 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
7228 t->data_ctx.stats.st_code = STAT_STATUS_DONE;
7229 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
7230 t->data_ctx.stats.st_code = STAT_STATUS_NONE;
7231 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
7232 t->data_ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007233 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
7234 t->data_ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007235 else
7236 t->data_ctx.stats.st_code = STAT_STATUS_UNKN;
7237 break;
7238 }
7239 h++;
7240 }
7241
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007242 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
7243
Willy Tarreaub2513902006-12-17 14:52:38 +01007244 return 1;
7245}
7246
Willy Tarreau4076a152009-04-02 15:18:36 +02007247/*
7248 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007249 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7250 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007251 */
7252void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7253 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007254 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007255{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007256 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7257 int len1 = buf->size - msg->som;
7258 es->len = buf->r - (buf->data + msg->som) + buf->size;
7259 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7260 if (es->len > len1 && len1 < sizeof(es->buf))
7261 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7262 }
7263 else {
7264 es->len = buf->r - (buf->data + msg->som);
7265 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7266 }
7267
Willy Tarreau4076a152009-04-02 15:18:36 +02007268 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007269 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007270 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007271 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007272 else
7273 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7274
Willy Tarreau4076a152009-04-02 15:18:36 +02007275 es->when = date; // user-visible date
7276 es->sid = s->uniq_id;
7277 es->srv = s->srv;
7278 es->oe = other_end;
7279 es->src = s->cli_addr;
Willy Tarreau078272e2010-12-12 12:46:33 +01007280 es->state = state;
7281 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007282 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007283}
Willy Tarreaub2513902006-12-17 14:52:38 +01007284
Willy Tarreaubce70882009-09-07 11:51:47 +02007285/* return the IP address pointed to by occurrence <occ> of header <hname> in
7286 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
7287 * occurrence number corresponding to this value is returned. If <occ> is
7288 * strictly negative, the occurrence number before the end corresponding to
7289 * this value is returned. If <occ> is null, any value is returned, so it is
7290 * not recommended to use it that way. Negative occurrences are limited to
7291 * a small value because it is required to keep them in memory while scanning.
7292 * IP address 0.0.0.0 is returned if no match is found.
7293 */
7294unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
7295{
7296 struct hdr_ctx ctx;
7297 unsigned int hdr_hist[MAX_HDR_HISTORY];
7298 unsigned int hist_ptr;
7299 int found = 0;
7300
7301 ctx.idx = 0;
7302 if (occ >= 0) {
7303 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7304 occ--;
7305 if (occ <= 0) {
7306 found = 1;
7307 break;
7308 }
7309 }
7310 if (!found)
7311 return 0;
7312 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7313 }
7314
7315 /* negative occurrence, we scan all the list then walk back */
7316 if (-occ > MAX_HDR_HISTORY)
7317 return 0;
7318
7319 hist_ptr = 0;
7320 hdr_hist[hist_ptr] = 0;
7321 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7322 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7323 if (hist_ptr >= MAX_HDR_HISTORY)
7324 hist_ptr = 0;
7325 found++;
7326 }
7327 if (-occ > found)
7328 return 0;
7329 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7330 * find occurrence -occ, so we have to check [hist_ptr+occ].
7331 */
7332 hist_ptr += occ;
7333 if (hist_ptr >= MAX_HDR_HISTORY)
7334 hist_ptr -= MAX_HDR_HISTORY;
7335 return hdr_hist[hist_ptr];
7336}
7337
Willy Tarreaubaaee002006-06-26 02:48:02 +02007338/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007339 * Print a debug line with a header
7340 */
7341void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7342{
7343 int len, max;
7344 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007345 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007346 max = end - start;
7347 UBOUND(max, sizeof(trash) - len - 1);
7348 len += strlcpy2(trash + len, start, max + 1);
7349 trash[len++] = '\n';
7350 write(1, trash, len);
7351}
7352
Willy Tarreau0937bc42009-12-22 15:03:09 +01007353/*
7354 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7355 * the required fields are properly allocated and that we only need to (re)init
7356 * them. This should be used before processing any new request.
7357 */
7358void http_init_txn(struct session *s)
7359{
7360 struct http_txn *txn = &s->txn;
7361 struct proxy *fe = s->fe;
7362
7363 txn->flags = 0;
7364 txn->status = -1;
7365
Willy Tarreauf64d1412010-10-07 20:06:11 +02007366 txn->cookie_first_date = 0;
7367 txn->cookie_last_date = 0;
7368
Willy Tarreau0937bc42009-12-22 15:03:09 +01007369 txn->req.sol = txn->req.eol = NULL;
7370 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7371 txn->rsp.sol = txn->rsp.eol = NULL;
7372 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
7373 txn->req.hdr_content_len = 0LL;
7374 txn->rsp.hdr_content_len = 0LL;
7375 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7376 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007377
7378 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007379
7380 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7381 if (fe->options2 & PR_O2_REQBUG_OK)
7382 txn->req.err_pos = -1; /* let buggy requests pass */
7383
Willy Tarreau46023632010-01-07 22:51:47 +01007384 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007385 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7386
Willy Tarreau46023632010-01-07 22:51:47 +01007387 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007388 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7389
7390 if (txn->hdr_idx.v)
7391 hdr_idx_init(&txn->hdr_idx);
7392}
7393
7394/* to be used at the end of a transaction */
7395void http_end_txn(struct session *s)
7396{
7397 struct http_txn *txn = &s->txn;
7398
7399 /* these ones will have been dynamically allocated */
7400 pool_free2(pool2_requri, txn->uri);
7401 pool_free2(pool2_capture, txn->cli_cookie);
7402 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007403 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007404
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007405 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007406 txn->uri = NULL;
7407 txn->srv_cookie = NULL;
7408 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007409
7410 if (txn->req.cap) {
7411 struct cap_hdr *h;
7412 for (h = s->fe->req_cap; h; h = h->next)
7413 pool_free2(h->pool, txn->req.cap[h->index]);
7414 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7415 }
7416
7417 if (txn->rsp.cap) {
7418 struct cap_hdr *h;
7419 for (h = s->fe->rsp_cap; h; h = h->next)
7420 pool_free2(h->pool, txn->rsp.cap[h->index]);
7421 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7422 }
7423
Willy Tarreau0937bc42009-12-22 15:03:09 +01007424}
7425
7426/* to be used at the end of a transaction to prepare a new one */
7427void http_reset_txn(struct session *s)
7428{
7429 http_end_txn(s);
7430 http_init_txn(s);
7431
7432 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007433 s->logs.logwait = s->fe->to_log;
7434 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007435 /* re-init store persistence */
7436 s->store_count = 0;
7437
Willy Tarreau0937bc42009-12-22 15:03:09 +01007438 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007439
7440 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7441
Willy Tarreau739cfba2010-01-25 23:11:14 +01007442 /* We must trim any excess data from the response buffer, because we
7443 * may have blocked an invalid response from a server that we don't
7444 * want to accidentely forward once we disable the analysers, nor do
7445 * we want those data to come along with next response. A typical
7446 * example of such data would be from a buggy server responding to
7447 * a HEAD with some data, or sending more than the advertised
7448 * content-length.
7449 */
7450 if (unlikely(s->rep->l > s->rep->send_max)) {
7451 s->rep->l = s->rep->send_max;
7452 s->rep->r = s->rep->w + s->rep->l;
7453 if (s->rep->r >= s->rep->data + s->rep->size)
7454 s->rep->r -= s->rep->size;
7455 }
7456
Willy Tarreau0937bc42009-12-22 15:03:09 +01007457 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007458 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007459
Willy Tarreaud04e8582010-05-31 12:31:35 +02007460 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007461 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007462
7463 s->req->rex = TICK_ETERNITY;
7464 s->req->wex = TICK_ETERNITY;
7465 s->req->analyse_exp = TICK_ETERNITY;
7466 s->rep->rex = TICK_ETERNITY;
7467 s->rep->wex = TICK_ETERNITY;
7468 s->rep->analyse_exp = TICK_ETERNITY;
7469}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007470
Willy Tarreau8797c062007-05-07 00:55:35 +02007471/************************************************************************/
7472/* The code below is dedicated to ACL parsing and matching */
7473/************************************************************************/
7474
7475
7476
7477
7478/* 1. Check on METHOD
7479 * We use the pre-parsed method if it is known, and store its number as an
7480 * integer. If it is unknown, we use the pointer and the length.
7481 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007482static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007483{
7484 int len, meth;
7485
Willy Tarreauae8b7962007-06-09 23:10:04 +02007486 len = strlen(*text);
7487 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007488
7489 pattern->val.i = meth;
7490 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007491 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007492 if (!pattern->ptr.str)
7493 return 0;
7494 pattern->len = len;
7495 }
7496 return 1;
7497}
7498
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007499static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007500acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7501 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007502{
7503 int meth;
7504 struct http_txn *txn = l7;
7505
Willy Tarreaub6866442008-07-14 23:54:42 +02007506 if (!txn)
7507 return 0;
7508
Willy Tarreau655dce92009-11-08 13:10:58 +01007509 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007510 return 0;
7511
Willy Tarreau8797c062007-05-07 00:55:35 +02007512 meth = txn->meth;
7513 test->i = meth;
7514 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007515 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7516 /* ensure the indexes are not affected */
7517 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007518 test->len = txn->req.sl.rq.m_l;
7519 test->ptr = txn->req.sol;
7520 }
7521 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7522 return 1;
7523}
7524
7525static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7526{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007527 int icase;
7528
Willy Tarreau8797c062007-05-07 00:55:35 +02007529 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02007530 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007531
7532 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02007533 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007534
7535 /* Other method, we must compare the strings */
7536 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02007537 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007538
7539 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
7540 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
7541 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007542 return ACL_PAT_FAIL;
7543 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007544}
7545
7546/* 2. Check on Request/Status Version
7547 * We simply compare strings here.
7548 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007549static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007550{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007551 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007552 if (!pattern->ptr.str)
7553 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007554 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007555 return 1;
7556}
7557
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007558static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007559acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7560 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007561{
7562 struct http_txn *txn = l7;
7563 char *ptr;
7564 int len;
7565
Willy Tarreaub6866442008-07-14 23:54:42 +02007566 if (!txn)
7567 return 0;
7568
Willy Tarreau655dce92009-11-08 13:10:58 +01007569 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007570 return 0;
7571
Willy Tarreau8797c062007-05-07 00:55:35 +02007572 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007573 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007574
7575 while ((len-- > 0) && (*ptr++ != '/'));
7576 if (len <= 0)
7577 return 0;
7578
7579 test->ptr = ptr;
7580 test->len = len;
7581
7582 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7583 return 1;
7584}
7585
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007586static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007587acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7588 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007589{
7590 struct http_txn *txn = l7;
7591 char *ptr;
7592 int len;
7593
Willy Tarreaub6866442008-07-14 23:54:42 +02007594 if (!txn)
7595 return 0;
7596
Willy Tarreau655dce92009-11-08 13:10:58 +01007597 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007598 return 0;
7599
Willy Tarreau8797c062007-05-07 00:55:35 +02007600 len = txn->rsp.sl.st.v_l;
7601 ptr = txn->rsp.sol;
7602
7603 while ((len-- > 0) && (*ptr++ != '/'));
7604 if (len <= 0)
7605 return 0;
7606
7607 test->ptr = ptr;
7608 test->len = len;
7609
7610 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7611 return 1;
7612}
7613
7614/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007615static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007616acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7617 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007618{
7619 struct http_txn *txn = l7;
7620 char *ptr;
7621 int len;
7622
Willy Tarreaub6866442008-07-14 23:54:42 +02007623 if (!txn)
7624 return 0;
7625
Willy Tarreau655dce92009-11-08 13:10:58 +01007626 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007627 return 0;
7628
Willy Tarreau8797c062007-05-07 00:55:35 +02007629 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007630 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007631
7632 test->i = __strl2ui(ptr, len);
7633 test->flags = ACL_TEST_F_VOL_1ST;
7634 return 1;
7635}
7636
7637/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007638static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007639acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7640 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007641{
7642 struct http_txn *txn = l7;
7643
Willy Tarreaub6866442008-07-14 23:54:42 +02007644 if (!txn)
7645 return 0;
7646
Willy Tarreau655dce92009-11-08 13:10:58 +01007647 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007648 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007649
Willy Tarreauc11416f2007-06-17 16:58:38 +02007650 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7651 /* ensure the indexes are not affected */
7652 return 0;
7653
Willy Tarreau8797c062007-05-07 00:55:35 +02007654 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007655 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007656
Willy Tarreauf3d25982007-05-08 22:45:09 +02007657 /* we do not need to set READ_ONLY because the data is in a buffer */
7658 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007659 return 1;
7660}
7661
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007662static int
7663acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7664 struct acl_expr *expr, struct acl_test *test)
7665{
7666 struct http_txn *txn = l7;
7667
Willy Tarreaub6866442008-07-14 23:54:42 +02007668 if (!txn)
7669 return 0;
7670
Willy Tarreau655dce92009-11-08 13:10:58 +01007671 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007672 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007673
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007674 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7675 /* ensure the indexes are not affected */
7676 return 0;
7677
7678 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007679 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007680 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
7681 test->i = AF_INET;
7682
7683 /*
7684 * If we are parsing url in frontend space, we prepare backend stage
7685 * to not parse again the same url ! optimization lazyness...
7686 */
7687 if (px->options & PR_O_HTTP_PROXY)
7688 l4->flags |= SN_ADDR_SET;
7689
7690 test->flags = ACL_TEST_F_READ_ONLY;
7691 return 1;
7692}
7693
7694static int
7695acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7696 struct acl_expr *expr, struct acl_test *test)
7697{
7698 struct http_txn *txn = l7;
7699
Willy Tarreaub6866442008-07-14 23:54:42 +02007700 if (!txn)
7701 return 0;
7702
Willy Tarreau655dce92009-11-08 13:10:58 +01007703 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007704 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007705
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007706 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7707 /* ensure the indexes are not affected */
7708 return 0;
7709
7710 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007711 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007712 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
7713
7714 if (px->options & PR_O_HTTP_PROXY)
7715 l4->flags |= SN_ADDR_SET;
7716
7717 test->flags = ACL_TEST_F_READ_ONLY;
7718 return 1;
7719}
7720
Willy Tarreauc11416f2007-06-17 16:58:38 +02007721/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7722 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7723 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007724static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007725acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007726 struct acl_expr *expr, struct acl_test *test)
7727{
7728 struct http_txn *txn = l7;
7729 struct hdr_idx *idx = &txn->hdr_idx;
7730 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007731
Willy Tarreaub6866442008-07-14 23:54:42 +02007732 if (!txn)
7733 return 0;
7734
Willy Tarreau33a7e692007-06-10 19:45:56 +02007735 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7736 /* search for header from the beginning */
7737 ctx->idx = 0;
7738
Willy Tarreau33a7e692007-06-10 19:45:56 +02007739 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7740 test->flags |= ACL_TEST_F_FETCH_MORE;
7741 test->flags |= ACL_TEST_F_VOL_HDR;
7742 test->len = ctx->vlen;
7743 test->ptr = (char *)ctx->line + ctx->val;
7744 return 1;
7745 }
7746
7747 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7748 test->flags |= ACL_TEST_F_VOL_HDR;
7749 return 0;
7750}
7751
Willy Tarreau33a7e692007-06-10 19:45:56 +02007752static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007753acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7754 struct acl_expr *expr, struct acl_test *test)
7755{
7756 struct http_txn *txn = l7;
7757
Willy Tarreaub6866442008-07-14 23:54:42 +02007758 if (!txn)
7759 return 0;
7760
Willy Tarreau655dce92009-11-08 13:10:58 +01007761 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007762 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007763
Willy Tarreauc11416f2007-06-17 16:58:38 +02007764 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7765 /* ensure the indexes are not affected */
7766 return 0;
7767
7768 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7769}
7770
7771static int
7772acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7773 struct acl_expr *expr, struct acl_test *test)
7774{
7775 struct http_txn *txn = l7;
7776
Willy Tarreaub6866442008-07-14 23:54:42 +02007777 if (!txn)
7778 return 0;
7779
Willy Tarreau655dce92009-11-08 13:10:58 +01007780 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007781 return 0;
7782
7783 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7784}
7785
7786/* 6. Check on HTTP header count. The number of occurrences is returned.
7787 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7788 */
7789static int
7790acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007791 struct acl_expr *expr, struct acl_test *test)
7792{
7793 struct http_txn *txn = l7;
7794 struct hdr_idx *idx = &txn->hdr_idx;
7795 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007796 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007797
Willy Tarreaub6866442008-07-14 23:54:42 +02007798 if (!txn)
7799 return 0;
7800
Willy Tarreau33a7e692007-06-10 19:45:56 +02007801 ctx.idx = 0;
7802 cnt = 0;
7803 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7804 cnt++;
7805
7806 test->i = cnt;
7807 test->flags = ACL_TEST_F_VOL_HDR;
7808 return 1;
7809}
7810
Willy Tarreauc11416f2007-06-17 16:58:38 +02007811static int
7812acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7813 struct acl_expr *expr, struct acl_test *test)
7814{
7815 struct http_txn *txn = l7;
7816
Willy Tarreaub6866442008-07-14 23:54:42 +02007817 if (!txn)
7818 return 0;
7819
Willy Tarreau655dce92009-11-08 13:10:58 +01007820 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007821 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007822
Willy Tarreauc11416f2007-06-17 16:58:38 +02007823 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7824 /* ensure the indexes are not affected */
7825 return 0;
7826
7827 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7828}
7829
7830static int
7831acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7832 struct acl_expr *expr, struct acl_test *test)
7833{
7834 struct http_txn *txn = l7;
7835
Willy Tarreaub6866442008-07-14 23:54:42 +02007836 if (!txn)
7837 return 0;
7838
Willy Tarreau655dce92009-11-08 13:10:58 +01007839 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007840 return 0;
7841
7842 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7843}
7844
Willy Tarreau33a7e692007-06-10 19:45:56 +02007845/* 7. Check on HTTP header's integer value. The integer value is returned.
7846 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007847 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007848 */
7849static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007850acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007851 struct acl_expr *expr, struct acl_test *test)
7852{
7853 struct http_txn *txn = l7;
7854 struct hdr_idx *idx = &txn->hdr_idx;
7855 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007856
Willy Tarreaub6866442008-07-14 23:54:42 +02007857 if (!txn)
7858 return 0;
7859
Willy Tarreau33a7e692007-06-10 19:45:56 +02007860 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7861 /* search for header from the beginning */
7862 ctx->idx = 0;
7863
Willy Tarreau33a7e692007-06-10 19:45:56 +02007864 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7865 test->flags |= ACL_TEST_F_FETCH_MORE;
7866 test->flags |= ACL_TEST_F_VOL_HDR;
7867 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
7868 return 1;
7869 }
7870
7871 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7872 test->flags |= ACL_TEST_F_VOL_HDR;
7873 return 0;
7874}
7875
Willy Tarreauc11416f2007-06-17 16:58:38 +02007876static int
7877acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7878 struct acl_expr *expr, struct acl_test *test)
7879{
7880 struct http_txn *txn = l7;
7881
Willy Tarreaub6866442008-07-14 23:54:42 +02007882 if (!txn)
7883 return 0;
7884
Willy Tarreau655dce92009-11-08 13:10:58 +01007885 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007886 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007887
Willy Tarreauc11416f2007-06-17 16:58:38 +02007888 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7889 /* ensure the indexes are not affected */
7890 return 0;
7891
7892 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7893}
7894
7895static int
7896acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7897 struct acl_expr *expr, struct acl_test *test)
7898{
7899 struct http_txn *txn = l7;
7900
Willy Tarreaub6866442008-07-14 23:54:42 +02007901 if (!txn)
7902 return 0;
7903
Willy Tarreau655dce92009-11-08 13:10:58 +01007904 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007905 return 0;
7906
7907 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7908}
7909
Willy Tarreau106f9792009-09-19 07:54:16 +02007910/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7911 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7912 */
7913static int
7914acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7915 struct acl_expr *expr, struct acl_test *test)
7916{
7917 struct http_txn *txn = l7;
7918 struct hdr_idx *idx = &txn->hdr_idx;
7919 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7920
7921 if (!txn)
7922 return 0;
7923
7924 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7925 /* search for header from the beginning */
7926 ctx->idx = 0;
7927
7928 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7929 test->flags |= ACL_TEST_F_FETCH_MORE;
7930 test->flags |= ACL_TEST_F_VOL_HDR;
7931 /* Same optimization as url_ip */
7932 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
7933 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
7934 test->ptr = (void *)&l4->srv_addr.sin_addr;
7935 test->i = AF_INET;
7936 return 1;
7937 }
7938
7939 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7940 test->flags |= ACL_TEST_F_VOL_HDR;
7941 return 0;
7942}
7943
7944static int
7945acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7946 struct acl_expr *expr, struct acl_test *test)
7947{
7948 struct http_txn *txn = l7;
7949
7950 if (!txn)
7951 return 0;
7952
Willy Tarreau655dce92009-11-08 13:10:58 +01007953 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007954 return 0;
7955
7956 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7957 /* ensure the indexes are not affected */
7958 return 0;
7959
7960 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
7961}
7962
7963static int
7964acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7965 struct acl_expr *expr, struct acl_test *test)
7966{
7967 struct http_txn *txn = l7;
7968
7969 if (!txn)
7970 return 0;
7971
Willy Tarreau655dce92009-11-08 13:10:58 +01007972 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02007973 return 0;
7974
7975 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
7976}
7977
Willy Tarreau737b0c12007-06-10 21:28:46 +02007978/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7979 * the first '/' after the possible hostname, and ends before the possible '?'.
7980 */
7981static int
7982acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
7983 struct acl_expr *expr, struct acl_test *test)
7984{
7985 struct http_txn *txn = l7;
7986 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007987
Willy Tarreaub6866442008-07-14 23:54:42 +02007988 if (!txn)
7989 return 0;
7990
Willy Tarreau655dce92009-11-08 13:10:58 +01007991 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007992 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007993
Willy Tarreauc11416f2007-06-17 16:58:38 +02007994 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7995 /* ensure the indexes are not affected */
7996 return 0;
7997
Willy Tarreau962c3f42010-01-10 00:15:35 +01007998 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007999 ptr = http_get_path(txn);
8000 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008001 return 0;
8002
8003 /* OK, we got the '/' ! */
8004 test->ptr = ptr;
8005
8006 while (ptr < end && *ptr != '?')
8007 ptr++;
8008
8009 test->len = ptr - test->ptr;
8010
8011 /* we do not need to set READ_ONLY because the data is in a buffer */
8012 test->flags = ACL_TEST_F_VOL_1ST;
8013 return 1;
8014}
8015
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008016static int
8017acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8018 struct acl_expr *expr, struct acl_test *test)
8019{
8020 struct buffer *req = s->req;
8021 struct http_txn *txn = &s->txn;
8022 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008023
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008024 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8025 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8026 */
8027
8028 if (!s || !req)
8029 return 0;
8030
Willy Tarreau655dce92009-11-08 13:10:58 +01008031 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008032 /* Already decoded as OK */
8033 test->flags |= ACL_TEST_F_SET_RES_PASS;
8034 return 1;
8035 }
8036
8037 /* Try to decode HTTP request */
8038 if (likely(req->lr < req->r))
8039 http_msg_analyzer(req, msg, &txn->hdr_idx);
8040
Willy Tarreau655dce92009-11-08 13:10:58 +01008041 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008042 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8043 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8044 return 1;
8045 }
8046 /* wait for final state */
8047 test->flags |= ACL_TEST_F_MAY_CHANGE;
8048 return 0;
8049 }
8050
8051 /* OK we got a valid HTTP request. We have some minor preparation to
8052 * perform so that further checks can rely on HTTP tests.
8053 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008054 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008055 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8056 s->flags |= SN_REDIRECTABLE;
8057
8058 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8059 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8060 return 1;
8061 }
8062
8063 test->flags |= ACL_TEST_F_SET_RES_PASS;
8064 return 1;
8065}
8066
Willy Tarreau7f18e522010-10-22 20:04:13 +02008067/* return a valid test if the current request is the first one on the connection */
8068static int
8069acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8070 struct acl_expr *expr, struct acl_test *test)
8071{
8072 if (!s)
8073 return 0;
8074
8075 if (s->txn.flags & TX_NOT_FIRST)
8076 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8077 else
8078 test->flags |= ACL_TEST_F_SET_RES_PASS;
8079
8080 return 1;
8081}
8082
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008083static int
8084acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8085 struct acl_expr *expr, struct acl_test *test)
8086{
8087
8088 if (!s)
8089 return 0;
8090
8091 if (!get_http_auth(s))
8092 return 0;
8093
8094 test->ctx.a[0] = expr->arg.ul;
8095 test->ctx.a[1] = s->txn.auth.user;
8096 test->ctx.a[2] = s->txn.auth.pass;
8097
8098 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8099
8100 return 1;
8101}
Willy Tarreau8797c062007-05-07 00:55:35 +02008102
8103/************************************************************************/
8104/* All supported keywords must be declared here. */
8105/************************************************************************/
8106
8107/* Note: must not be declared <const> as its list will be overwritten */
8108static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008109 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8110
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008111 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008112 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8113 { "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 +02008114 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008115
Willy Tarreauc4262962010-05-10 23:42:40 +02008116 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008117 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8118 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8119 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8120 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8121 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8122 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008123 { "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 +02008124 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008125
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008126 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008127 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008128 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8129 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8130 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8131 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8132 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8133 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8134 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
8135 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008136 { "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 +02008137
Willy Tarreauc4262962010-05-10 23:42:40 +02008138 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008139 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8140 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8141 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8142 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8143 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8144 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8145 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
8146 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008147 { "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 +02008148
Willy Tarreauc4262962010-05-10 23:42:40 +02008149 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008150 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8151 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8152 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8153 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8154 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8155 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008156
Willy Tarreauf3d25982007-05-08 22:45:09 +02008157#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008158 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8159 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8160 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8161 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8162 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8163 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8164 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8165
Willy Tarreau8797c062007-05-07 00:55:35 +02008166 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8167 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8168 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8169 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8170 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8171 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8172 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8173 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008174#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008175
Willy Tarreau7f18e522010-10-22 20:04:13 +02008176 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8177 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8178 { "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 +02008179 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008180}};
8181
Willy Tarreau4a568972010-05-12 08:08:50 +02008182/************************************************************************/
8183/* The code below is dedicated to pattern fetching and matching */
8184/************************************************************************/
8185
8186/* extract the IP address from the last occurrence of specified header. Note
8187 * that we should normally first extract the string then convert it to IP,
8188 * but right now we have all the functions to do this seemlessly, and we will
8189 * be able to change that later without touching the configuration.
8190 */
8191static int
8192pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02008193 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008194{
8195 struct http_txn *txn = l7;
8196
Emeric Brun485479d2010-09-23 18:02:19 +02008197 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 +02008198 return data->ip.s_addr != 0;
8199}
8200
Emeric Brun485479d2010-09-23 18:02:19 +02008201
8202
Willy Tarreau4a568972010-05-12 08:08:50 +02008203/************************************************************************/
8204/* All supported keywords must be declared here. */
8205/************************************************************************/
8206/* Note: must not be declared <const> as its list will be overwritten */
8207static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Emeric Brun485479d2010-09-23 18:02:19 +02008208 { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
8209 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008210}};
8211
Willy Tarreau8797c062007-05-07 00:55:35 +02008212
8213__attribute__((constructor))
8214static void __http_protocol_init(void)
8215{
8216 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008217 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008218}
8219
8220
Willy Tarreau58f10d72006-12-04 02:26:12 +01008221/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008222 * Local variables:
8223 * c-indent-level: 8
8224 * c-basic-offset: 8
8225 * End:
8226 */