blob: 12bb2a3ece810b27ce8ddf9318b6408a6520446e [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;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100604 if (idx->tail == ctx->idx)
605 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100606 ctx->idx = ctx->prev; /* walk back to the end of previous header */
607 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
608 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
609 ctx->vlen = 0;
610 return ctx->idx;
611 }
612
613 /* This was not the only value of this header. We have to remove between
614 * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry
615 * of the list, we remove the last separator.
616 */
617
618 skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1;
619 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
620 sol + ctx->val + ctx->vlen + skip_comma,
621 NULL, 0);
622 hdr->len += delta;
623 http_msg_move_end(msg, delta);
624 ctx->val = ctx->del;
625 ctx->vlen = 0;
626 return ctx->idx;
627}
628
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100629/* This function handles a server error at the stream interface level. The
630 * stream interface is assumed to be already in a closed state. An optional
631 * message is copied into the input buffer, and an HTTP status code stored.
632 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100633 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200634 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100635static void http_server_error(struct session *t, struct stream_interface *si,
636 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200637{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100638 buffer_auto_read(si->ob);
639 buffer_abort(si->ob);
640 buffer_auto_close(si->ob);
641 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200642 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100643 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100644 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100645 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100646 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647 }
648 if (!(t->flags & SN_ERR_MASK))
649 t->flags |= err;
650 if (!(t->flags & SN_FINST_MASK))
651 t->flags |= finst;
652}
653
Willy Tarreau80587432006-12-24 17:47:20 +0100654/* This function returns the appropriate error location for the given session
655 * and message.
656 */
657
658struct chunk *error_message(struct session *s, int msgnum)
659{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200660 if (s->be->errmsg[msgnum].str)
661 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100662 else if (s->fe->errmsg[msgnum].str)
663 return &s->fe->errmsg[msgnum];
664 else
665 return &http_err_chunks[msgnum];
666}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667
Willy Tarreau53b6c742006-12-17 13:37:46 +0100668/*
669 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
670 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
671 */
672static http_meth_t find_http_meth(const char *str, const int len)
673{
674 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100675 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100676
677 m = ((unsigned)*str - 'A');
678
679 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100680 for (h = http_methods[m]; h->len > 0; h++) {
681 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100682 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100683 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100684 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100685 };
686 return HTTP_METH_OTHER;
687 }
688 return HTTP_METH_NONE;
689
690}
691
Willy Tarreau21d2af32008-02-14 20:25:24 +0100692/* Parse the URI from the given transaction (which is assumed to be in request
693 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
694 * It is returned otherwise.
695 */
696static char *
697http_get_path(struct http_txn *txn)
698{
699 char *ptr, *end;
700
Willy Tarreau962c3f42010-01-10 00:15:35 +0100701 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100702 end = ptr + txn->req.sl.rq.u_l;
703
704 if (ptr >= end)
705 return NULL;
706
707 /* RFC2616, par. 5.1.2 :
708 * Request-URI = "*" | absuri | abspath | authority
709 */
710
711 if (*ptr == '*')
712 return NULL;
713
714 if (isalpha((unsigned char)*ptr)) {
715 /* this is a scheme as described by RFC3986, par. 3.1 */
716 ptr++;
717 while (ptr < end &&
718 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
719 ptr++;
720 /* skip '://' */
721 if (ptr == end || *ptr++ != ':')
722 return NULL;
723 if (ptr == end || *ptr++ != '/')
724 return NULL;
725 if (ptr == end || *ptr++ != '/')
726 return NULL;
727 }
728 /* skip [user[:passwd]@]host[:[port]] */
729
730 while (ptr < end && *ptr != '/')
731 ptr++;
732
733 if (ptr == end)
734 return NULL;
735
736 /* OK, we got the '/' ! */
737 return ptr;
738}
739
Willy Tarreauefb453c2008-10-26 20:49:47 +0100740/* Returns a 302 for a redirectable request. This may only be called just after
741 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
742 * left unchanged and will follow normal proxy processing.
743 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100745{
746 struct http_txn *txn;
747 struct chunk rdr;
748 char *path;
749 int len;
750
751 /* 1: create the response header */
752 rdr.len = strlen(HTTP_302);
753 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100754 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100755 memcpy(rdr.str, HTTP_302, rdr.len);
756
757 /* 2: add the server's prefix */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200758 if (rdr.len + s->srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100759 return;
760
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100761 /* special prefix "/" means don't change URL */
762 if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') {
763 memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len);
764 rdr.len += s->srv->rdr_len;
765 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100766
767 /* 3: add the request URI */
768 txn = &s->txn;
769 path = http_get_path(txn);
770 if (!path)
771 return;
772
Willy Tarreau962c3f42010-01-10 00:15:35 +0100773 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200774 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100775 return;
776
777 memcpy(rdr.str + rdr.len, path, len);
778 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100779
780 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
781 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
782 rdr.len += 29;
783 } else {
784 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
785 rdr.len += 23;
786 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100787
788 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100789 si->shutr(si);
790 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100791 si->err_type = SI_ET_NONE;
792 si->err_loc = NULL;
793 si->state = SI_ST_CLO;
794
795 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100796 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100797
798 /* FIXME: we should increase a counter of redirects per server and per backend. */
799 if (s->srv)
Willy Tarreau7f062c42009-03-05 18:43:00 +0100800 srv_inc_sess_ctr(s->srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100801}
802
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100803/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100804 * that the server side is closed. Note that err_type is actually a
805 * bitmask, where almost only aborts may be cumulated with other
806 * values. We consider that aborted operations are more important
807 * than timeouts or errors due to the fact that nobody else in the
808 * logs might explain incomplete retries. All others should avoid
809 * being cumulated. It should normally not be possible to have multiple
810 * aborts at once, but just in case, the first one in sequence is reported.
811 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100812void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100814 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100815
816 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100817 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
818 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100819 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100820 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
821 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100822 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100823 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
824 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100825 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100826 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
827 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100828 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100829 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
830 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100831 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100832 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
833 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100834 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100835 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
836 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100837}
838
Willy Tarreau42250582007-04-01 01:30:43 +0200839extern const char sess_term_cond[8];
840extern const char sess_fin_state[8];
841extern const char *monthname[12];
Willy Tarreauf1348312010-10-07 15:54:11 +0200842const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */
843const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
844 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
845 Set-cookie Updated, unknown, unknown */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200846struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200847struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100848
Emeric Brun3a058f32009-06-30 18:26:00 +0200849void http_sess_clflog(struct session *s)
850{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100851 char pn[INET6_ADDRSTRLEN];
Emeric Brun3a058f32009-06-30 18:26:00 +0200852 struct proxy *fe = s->fe;
853 struct proxy *be = s->be;
854 struct proxy *prx_log;
855 struct http_txn *txn = &s->txn;
856 int tolog, level, err;
857 char *uri, *h;
858 char *svid;
859 struct tm tm;
860 static char tmpline[MAX_SYSLOG_LEN];
861 int hdr;
862 size_t w;
863 int t_request;
864
865 prx_log = fe;
866 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +0200867 (s->req->cons->conn_retries != be->conn_retries) ||
Emeric Brun3a058f32009-06-30 18:26:00 +0200868 txn->status >= 500;
869
870 if (s->cli_addr.ss_family == AF_INET)
871 inet_ntop(AF_INET,
872 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
873 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +0200874 else if (s->cli_addr.ss_family == AF_INET6)
Emeric Brun3a058f32009-06-30 18:26:00 +0200875 inet_ntop(AF_INET6,
876 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
877 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +0200878 else
879 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
Emeric Brun3a058f32009-06-30 18:26:00 +0200880
881 get_gmtime(s->logs.accept_date.tv_sec, &tm);
882
883 /* FIXME: let's limit ourselves to frontend logging for now. */
884 tolog = fe->to_log;
885
886 h = tmpline;
887
888 w = snprintf(h, sizeof(tmpline),
889 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
890 pn,
891 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
892 tm.tm_hour, tm.tm_min, tm.tm_sec);
893 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
894 goto trunc;
895 h += w;
896
897 if (h >= tmpline + sizeof(tmpline) - 4)
898 goto trunc;
899
900 *(h++) = ' ';
901 *(h++) = '\"';
902 uri = txn->uri ? txn->uri : "<BADREQ>";
903 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
904 '#', url_encode_map, uri);
905 *(h++) = '\"';
906
907 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
908 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
909 goto trunc;
910 h += w;
911
912 if (h >= tmpline + sizeof(tmpline) - 9)
913 goto trunc;
914 memcpy(h, " \"-\" \"-\"", 8);
915 h += 8;
916
917 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
918 " %d %03d",
Emeric Brun5bd86a82010-10-22 17:23:04 +0200919 s->cli_addr.ss_family == AF_UNIX ? s->listener->luid :
920 ntohs((s->cli_addr.ss_family == AF_INET) ?
921 ((struct sockaddr_in *)&s->cli_addr)->sin_port :
922 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Emeric Brun3a058f32009-06-30 18:26:00 +0200923 (int)s->logs.accept_date.tv_usec/1000);
924 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
925 goto trunc;
926 h += w;
927
928 w = strlen(fe->id);
929 if (h >= tmpline + sizeof(tmpline) - 4 - w)
930 goto trunc;
931 *(h++) = ' ';
932 *(h++) = '\"';
933 memcpy(h, fe->id, w);
934 h += w;
935 *(h++) = '\"';
936
937 w = strlen(be->id);
938 if (h >= tmpline + sizeof(tmpline) - 4 - w)
939 goto trunc;
940 *(h++) = ' ';
941 *(h++) = '\"';
942 memcpy(h, be->id, w);
943 h += w;
944 *(h++) = '\"';
945
946 svid = (tolog & LW_SVID) ?
947 (s->data_source != DATA_SRC_STATS) ?
948 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
949
950 w = strlen(svid);
951 if (h >= tmpline + sizeof(tmpline) - 4 - w)
952 goto trunc;
953 *(h++) = ' ';
954 *(h++) = '\"';
955 memcpy(h, svid, w);
956 h += w;
957 *(h++) = '\"';
958
959 t_request = -1;
960 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
961 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
962 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
963 " %d %ld %ld %ld %ld",
964 t_request,
965 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
966 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
967 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
968 s->logs.t_close);
969 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
970 goto trunc;
971 h += w;
972
973 if (h >= tmpline + sizeof(tmpline) - 8)
974 goto trunc;
975 *(h++) = ' ';
976 *(h++) = '\"';
977 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
978 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
979 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
980 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
981 *(h++) = '\"';
982
983 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
984 " %d %d %d %d %d %ld %ld",
985 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Willy Tarreauee28de02010-06-01 09:51:00 +0200986 (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries,
Emeric Brun3a058f32009-06-30 18:26:00 +0200987 s->logs.srv_queue_size, s->logs.prx_queue_size);
988
989 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
990 goto trunc;
991 h += w;
992
993 if (txn->cli_cookie) {
994 w = strlen(txn->cli_cookie);
995 if (h >= tmpline + sizeof(tmpline) - 4 - w)
996 goto trunc;
997 *(h++) = ' ';
998 *(h++) = '\"';
999 memcpy(h, txn->cli_cookie, w);
1000 h += w;
1001 *(h++) = '\"';
1002 } else {
1003 if (h >= tmpline + sizeof(tmpline) - 5)
1004 goto trunc;
1005 memcpy(h, " \"-\"", 4);
1006 h += 4;
1007 }
1008
1009 if (txn->srv_cookie) {
1010 w = strlen(txn->srv_cookie);
1011 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1012 goto trunc;
1013 *(h++) = ' ';
1014 *(h++) = '\"';
1015 memcpy(h, txn->srv_cookie, w);
1016 h += w;
1017 *(h++) = '\"';
1018 } else {
1019 if (h >= tmpline + sizeof(tmpline) - 5)
1020 goto trunc;
1021 memcpy(h, " \"-\"", 4);
1022 h += 4;
1023 }
1024
1025 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1026 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1027 if (h >= sizeof (tmpline) + tmpline - 4)
1028 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001029 if (txn->req.cap[hdr] != NULL) {
1030 *(h++) = ' ';
1031 *(h++) = '\"';
1032 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1033 '#', hdr_encode_map, txn->req.cap[hdr]);
1034 *(h++) = '\"';
1035 } else {
1036 memcpy(h, " \"-\"", 4);
1037 h += 4;
1038 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001039 }
1040 }
1041
1042 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1043 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1044 if (h >= sizeof (tmpline) + tmpline - 4)
1045 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001046 if (txn->rsp.cap[hdr] != NULL) {
1047 *(h++) = ' ';
1048 *(h++) = '\"';
1049 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1050 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1051 *(h++) = '\"';
1052 } else {
1053 memcpy(h, " \"-\"", 4);
1054 h += 4;
1055 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001056 }
1057 }
1058
1059trunc:
1060 *h = '\0';
1061
1062 level = LOG_INFO;
1063 if (err && (fe->options2 & PR_O2_LOGERRORS))
1064 level = LOG_ERR;
1065
1066 send_log(prx_log, level, "%s\n", tmpline);
1067
1068 s->logs.logwait = 0;
1069}
1070
Willy Tarreau42250582007-04-01 01:30:43 +02001071/*
1072 * send a log for the session when we have enough info about it.
1073 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001074 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001075void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001076{
Cyril Bontéacd7d632010-11-01 19:26:02 +01001077 char pn[INET6_ADDRSTRLEN];
Willy Tarreau42250582007-04-01 01:30:43 +02001078 struct proxy *fe = s->fe;
1079 struct proxy *be = s->be;
1080 struct proxy *prx_log;
1081 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001082 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001083 char *uri, *h;
1084 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001085 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001086 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001087 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001088 int hdr;
1089
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001090 /* if we don't want to log normal traffic, return now */
1091 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +02001092 (s->req->cons->conn_retries != be->conn_retries) ||
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001093 txn->status >= 500;
1094 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1095 return;
1096
Willy Tarreau42250582007-04-01 01:30:43 +02001097 if (fe->logfac1 < 0 && fe->logfac2 < 0)
1098 return;
1099 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001100
Emeric Brun3a058f32009-06-30 18:26:00 +02001101 if (prx_log->options2 & PR_O2_CLFLOG)
1102 return http_sess_clflog(s);
1103
Willy Tarreau42250582007-04-01 01:30:43 +02001104 if (s->cli_addr.ss_family == AF_INET)
1105 inet_ntop(AF_INET,
1106 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
1107 pn, sizeof(pn));
Emeric Brun5bd86a82010-10-22 17:23:04 +02001108 else if (s->cli_addr.ss_family == AF_INET6)
Willy Tarreau42250582007-04-01 01:30:43 +02001109 inet_ntop(AF_INET6,
1110 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
1111 pn, sizeof(pn));
1112
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001113 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001114
1115 /* FIXME: let's limit ourselves to frontend logging for now. */
1116 tolog = fe->to_log;
1117
1118 h = tmpline;
1119 if (fe->to_log & LW_REQHDR &&
1120 txn->req.cap &&
1121 (h < tmpline + sizeof(tmpline) - 10)) {
1122 *(h++) = ' ';
1123 *(h++) = '{';
1124 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1125 if (hdr)
1126 *(h++) = '|';
1127 if (txn->req.cap[hdr] != NULL)
1128 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1129 '#', hdr_encode_map, txn->req.cap[hdr]);
1130 }
1131 *(h++) = '}';
1132 }
1133
1134 if (fe->to_log & LW_RSPHDR &&
1135 txn->rsp.cap &&
1136 (h < tmpline + sizeof(tmpline) - 7)) {
1137 *(h++) = ' ';
1138 *(h++) = '{';
1139 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1140 if (hdr)
1141 *(h++) = '|';
1142 if (txn->rsp.cap[hdr] != NULL)
1143 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1144 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1145 }
1146 *(h++) = '}';
1147 }
1148
1149 if (h < tmpline + sizeof(tmpline) - 4) {
1150 *(h++) = ' ';
1151 *(h++) = '"';
1152 uri = txn->uri ? txn->uri : "<BADREQ>";
1153 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1154 '#', url_encode_map, uri);
1155 *(h++) = '"';
1156 }
1157 *h = '\0';
1158
1159 svid = (tolog & LW_SVID) ?
1160 (s->data_source != DATA_SRC_STATS) ?
1161 (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-";
1162
Willy Tarreau70089872008-06-13 21:12:51 +02001163 t_request = -1;
1164 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1165 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1166
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001167 level = LOG_INFO;
1168 if (err && (fe->options2 & PR_O2_LOGERRORS))
1169 level = LOG_ERR;
1170
1171 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001172 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001173 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1174 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Emeric Brun5bd86a82010-10-22 17:23:04 +02001175 (s->cli_addr.ss_family == AF_UNIX) ? "unix" : pn,
1176 (s->cli_addr.ss_family == AF_UNIX) ? s->listener->luid :
1177 ntohs((s->cli_addr.ss_family == AF_INET) ?
1178 ((struct sockaddr_in *)&s->cli_addr)->sin_port :
1179 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +02001180 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001181 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001182 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001183 t_request,
1184 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001185 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1186 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1187 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1188 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001189 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001190 txn->cli_cookie ? txn->cli_cookie : "-",
1191 txn->srv_cookie ? txn->srv_cookie : "-",
1192 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1193 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1194 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1195 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
1196 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001197 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +02001198 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001199 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1200
1201 s->logs.logwait = 0;
1202}
1203
Willy Tarreau117f59e2007-03-04 18:17:17 +01001204
1205/*
1206 * Capture headers from message starting at <som> according to header list
1207 * <cap_hdr>, and fill the <idx> structure appropriately.
1208 */
1209void capture_headers(char *som, struct hdr_idx *idx,
1210 char **cap, struct cap_hdr *cap_hdr)
1211{
1212 char *eol, *sol, *col, *sov;
1213 int cur_idx;
1214 struct cap_hdr *h;
1215 int len;
1216
1217 sol = som + hdr_idx_first_pos(idx);
1218 cur_idx = hdr_idx_first_idx(idx);
1219
1220 while (cur_idx) {
1221 eol = sol + idx->v[cur_idx].len;
1222
1223 col = sol;
1224 while (col < eol && *col != ':')
1225 col++;
1226
1227 sov = col + 1;
1228 while (sov < eol && http_is_lws[(unsigned char)*sov])
1229 sov++;
1230
1231 for (h = cap_hdr; h; h = h->next) {
1232 if ((h->namelen == col - sol) &&
1233 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1234 if (cap[h->index] == NULL)
1235 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001236 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001237
1238 if (cap[h->index] == NULL) {
1239 Alert("HTTP capture : out of memory.\n");
1240 continue;
1241 }
1242
1243 len = eol - sov;
1244 if (len > h->len)
1245 len = h->len;
1246
1247 memcpy(cap[h->index], sov, len);
1248 cap[h->index][len]=0;
1249 }
1250 }
1251 sol = eol + idx->v[cur_idx].cr + 1;
1252 cur_idx = idx->v[cur_idx].next;
1253 }
1254}
1255
1256
Willy Tarreau42250582007-04-01 01:30:43 +02001257/* either we find an LF at <ptr> or we jump to <bad>.
1258 */
1259#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1260
1261/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1262 * otherwise to <http_msg_ood> with <state> set to <st>.
1263 */
1264#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1265 ptr++; \
1266 if (likely(ptr < end)) \
1267 goto good; \
1268 else { \
1269 state = (st); \
1270 goto http_msg_ood; \
1271 } \
1272 } while (0)
1273
1274
Willy Tarreaubaaee002006-06-26 02:48:02 +02001275/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001276 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001277 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1278 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1279 * will give undefined results.
1280 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1281 * and that msg->sol points to the beginning of the response.
1282 * If a complete line is found (which implies that at least one CR or LF is
1283 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1284 * returned indicating an incomplete line (which does not mean that parts have
1285 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1286 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1287 * upon next call.
1288 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001289 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001290 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1291 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001292 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001294const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1295 unsigned int state, const char *ptr, const char *end,
1296 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001297{
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001299 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001300 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001301 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001302 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1303
1304 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001305 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001306 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1307 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001308 state = HTTP_MSG_ERROR;
1309 break;
1310
Willy Tarreau8973c702007-01-21 23:58:29 +01001311 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001312 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001313 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001314 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001315 goto http_msg_rpcode;
1316 }
1317 if (likely(HTTP_IS_SPHT(*ptr)))
1318 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1319 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001320 state = HTTP_MSG_ERROR;
1321 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001322
Willy Tarreau8973c702007-01-21 23:58:29 +01001323 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001324 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001325 if (likely(!HTTP_IS_LWS(*ptr)))
1326 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1327
1328 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001329 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001330 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1331 }
1332
1333 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001334 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001335 http_msg_rsp_reason:
1336 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001337 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 msg->sl.st.r_l = 0;
1339 goto http_msg_rpline_eol;
1340
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001342 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001343 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001344 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 goto http_msg_rpreason;
1346 }
1347 if (likely(HTTP_IS_SPHT(*ptr)))
1348 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1349 /* so it's a CR/LF, so there is no reason phrase */
1350 goto http_msg_rsp_reason;
1351
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001353 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001354 if (likely(!HTTP_IS_CRLF(*ptr)))
1355 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001356 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001357 http_msg_rpline_eol:
1358 /* We have seen the end of line. Note that we do not
1359 * necessarily have the \n yet, but at least we know that we
1360 * have EITHER \r OR \n, otherwise the response would not be
1361 * complete. We can then record the response length and return
1362 * to the caller which will be able to register it.
1363 */
1364 msg->sl.st.l = ptr - msg->sol;
1365 return ptr;
1366
1367#ifdef DEBUG_FULL
1368 default:
1369 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1370 exit(1);
1371#endif
1372 }
1373
1374 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001375 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001376 if (ret_state)
1377 *ret_state = state;
1378 if (ret_ptr)
1379 *ret_ptr = (char *)ptr;
1380 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001381}
1382
Willy Tarreau8973c702007-01-21 23:58:29 +01001383/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 * This function parses a request line between <ptr> and <end>, starting with
1385 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1386 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1387 * will give undefined results.
1388 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1389 * and that msg->sol points to the beginning of the request.
1390 * If a complete line is found (which implies that at least one CR or LF is
1391 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1392 * returned indicating an incomplete line (which does not mean that parts have
1393 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1394 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1395 * upon next call.
1396 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001397 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1399 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001400 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001401 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001402const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1403 unsigned int state, const char *ptr, const char *end,
1404 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001405{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001408 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 if (likely(HTTP_IS_TOKEN(*ptr)))
1410 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001413 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1415 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001416
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 if (likely(HTTP_IS_CRLF(*ptr))) {
1418 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001419 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001421 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001423 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001425 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 msg->sl.rq.v_l = 0;
1427 goto http_msg_rqline_eol;
1428 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001429 state = HTTP_MSG_ERROR;
1430 break;
1431
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001433 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001435 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 goto http_msg_rquri;
1437 }
1438 if (likely(HTTP_IS_SPHT(*ptr)))
1439 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1440 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1441 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001442
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001444 http_msg_rquri:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001445 if (likely(!HTTP_IS_LWS(*ptr)))
1446 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001449 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1451 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1454 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001455
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001456 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001457 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001459 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001460 goto http_msg_rqver;
1461 }
1462 if (likely(HTTP_IS_SPHT(*ptr)))
1463 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1464 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1465 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001466
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001467 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001468 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001469 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001471
1472 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001473 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001474 http_msg_rqline_eol:
1475 /* We have seen the end of line. Note that we do not
1476 * necessarily have the \n yet, but at least we know that we
1477 * have EITHER \r OR \n, otherwise the request would not be
1478 * complete. We can then record the request length and return
1479 * to the caller which will be able to register it.
1480 */
1481 msg->sl.rq.l = ptr - msg->sol;
1482 return ptr;
1483 }
1484
1485 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001486 state = HTTP_MSG_ERROR;
1487 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001488
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489#ifdef DEBUG_FULL
1490 default:
1491 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1492 exit(1);
1493#endif
1494 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001495
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001497 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 if (ret_state)
1499 *ret_state = state;
1500 if (ret_ptr)
1501 *ret_ptr = (char *)ptr;
1502 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001504
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001505/*
1506 * Returns the data from Authorization header. Function may be called more
1507 * than once so data is stored in txn->auth_data. When no header is found
1508 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1509 * searching again for something we are unable to find anyway.
1510 */
1511
1512char get_http_auth_buff[BUFSIZE];
1513
1514int
1515get_http_auth(struct session *s)
1516{
1517
1518 struct http_txn *txn = &s->txn;
1519 struct chunk auth_method;
1520 struct hdr_ctx ctx;
1521 char *h, *p;
1522 int len;
1523
1524#ifdef DEBUG_AUTH
1525 printf("Auth for session %p: %d\n", s, txn->auth.method);
1526#endif
1527
1528 if (txn->auth.method == HTTP_AUTH_WRONG)
1529 return 0;
1530
1531 if (txn->auth.method)
1532 return 1;
1533
1534 txn->auth.method = HTTP_AUTH_WRONG;
1535
1536 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001537
1538 if (txn->flags & TX_USE_PX_CONN) {
1539 h = "Proxy-Authorization";
1540 len = strlen(h);
1541 } else {
1542 h = "Authorization";
1543 len = strlen(h);
1544 }
1545
1546 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001547 return 0;
1548
1549 h = ctx.line + ctx.val;
1550
1551 p = memchr(h, ' ', ctx.vlen);
1552 if (!p || p == h)
1553 return 0;
1554
1555 chunk_initlen(&auth_method, h, 0, p-h);
1556 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1557
1558 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1559
1560 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1561 get_http_auth_buff, BUFSIZE - 1);
1562
1563 if (len < 0)
1564 return 0;
1565
1566
1567 get_http_auth_buff[len] = '\0';
1568
1569 p = strchr(get_http_auth_buff, ':');
1570
1571 if (!p)
1572 return 0;
1573
1574 txn->auth.user = get_http_auth_buff;
1575 *p = '\0';
1576 txn->auth.pass = p+1;
1577
1578 txn->auth.method = HTTP_AUTH_BASIC;
1579 return 1;
1580 }
1581
1582 return 0;
1583}
1584
Willy Tarreau58f10d72006-12-04 02:26:12 +01001585
Willy Tarreau8973c702007-01-21 23:58:29 +01001586/*
1587 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001588 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001589 * when data are missing and recalled at the exact same location with no
1590 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001591 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001592 * fields. Note that msg->som and msg->sol will be initialized after completing
1593 * the first state, so that none of the msg pointers has to be initialized
1594 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001595 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1597{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001598 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001599 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001600
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001601 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602 ptr = buf->lr;
1603 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001604
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 if (unlikely(ptr >= end))
1606 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001607
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001608 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001609 /*
1610 * First, states that are specific to the response only.
1611 * We check them first so that request and headers are
1612 * closer to each other (accessed more often).
1613 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001614 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001615 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001616 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001617 /* we have a start of message, but we have to check
1618 * first if we need to remove some CRLF. We can only
1619 * do this when send_max=0.
1620 */
1621 char *beg = buf->w + buf->send_max;
1622 if (beg >= buf->data + buf->size)
1623 beg -= buf->size;
1624 if (unlikely(ptr != beg)) {
1625 if (buf->send_max)
1626 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001627 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001628 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001629 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001630 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001631 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001632 hdr_idx_init(idx);
1633 state = HTTP_MSG_RPVER;
1634 goto http_msg_rpver;
1635 }
1636
1637 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1638 goto http_msg_invalid;
1639
1640 if (unlikely(*ptr == '\n'))
1641 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1642 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1643 /* stop here */
1644
Willy Tarreau8973c702007-01-21 23:58:29 +01001645 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001646 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001647 EXPECT_LF_HERE(ptr, http_msg_invalid);
1648 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1649 /* stop here */
1650
Willy Tarreau8973c702007-01-21 23:58:29 +01001651 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001652 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001653 case HTTP_MSG_RPVER_SP:
1654 case HTTP_MSG_RPCODE:
1655 case HTTP_MSG_RPCODE_SP:
1656 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001657 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001658 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001659 if (unlikely(!ptr))
1660 return;
1661
1662 /* we have a full response and we know that we have either a CR
1663 * or an LF at <ptr>.
1664 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001665 //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 +01001666 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1667
1668 msg->sol = ptr;
1669 if (likely(*ptr == '\r'))
1670 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1671 goto http_msg_rpline_end;
1672
Willy Tarreau8973c702007-01-21 23:58:29 +01001673 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001674 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001675 /* msg->sol must point to the first of CR or LF. */
1676 EXPECT_LF_HERE(ptr, http_msg_invalid);
1677 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1678 /* stop here */
1679
1680 /*
1681 * Second, states that are specific to the request only
1682 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001683 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001684 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001686 /* we have a start of message, but we have to check
1687 * first if we need to remove some CRLF. We can only
1688 * do this when send_max=0.
1689 */
1690 char *beg = buf->w + buf->send_max;
1691 if (beg >= buf->data + buf->size)
1692 beg -= buf->size;
1693 if (likely(ptr != beg)) {
1694 if (buf->send_max)
1695 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001696 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001697 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001699 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001700 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001701 /* we will need this when keep-alive will be supported
1702 hdr_idx_init(idx);
1703 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001704 state = HTTP_MSG_RQMETH;
1705 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001707
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1709 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001710
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001711 if (unlikely(*ptr == '\n'))
1712 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1713 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001714 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001715
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001716 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001717 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001718 EXPECT_LF_HERE(ptr, http_msg_invalid);
1719 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001720 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001721
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001723 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 case HTTP_MSG_RQMETH_SP:
1725 case HTTP_MSG_RQURI:
1726 case HTTP_MSG_RQURI_SP:
1727 case HTTP_MSG_RQVER:
1728 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001729 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 if (unlikely(!ptr))
1731 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001732
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 /* we have a full request and we know that we have either a CR
1734 * or an LF at <ptr>.
1735 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001736 //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 +01001737 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001738
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 msg->sol = ptr;
1740 if (likely(*ptr == '\r'))
1741 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001743
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001745 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 /* check for HTTP/0.9 request : no version information available.
1747 * msg->sol must point to the first of CR or LF.
1748 */
1749 if (unlikely(msg->sl.rq.v_l == 0))
1750 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001751
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 EXPECT_LF_HERE(ptr, http_msg_invalid);
1753 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001754 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001755
Willy Tarreau8973c702007-01-21 23:58:29 +01001756 /*
1757 * Common states below
1758 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001760 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001761 msg->sol = ptr;
1762 if (likely(!HTTP_IS_CRLF(*ptr))) {
1763 goto http_msg_hdr_name;
1764 }
1765
1766 if (likely(*ptr == '\r'))
1767 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1768 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001769
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001770 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001771 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001772 /* assumes msg->sol points to the first char */
1773 if (likely(HTTP_IS_TOKEN(*ptr)))
1774 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001775
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 if (likely(*ptr == ':')) {
1777 msg->col = ptr - buf->data;
1778 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1779 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001780
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001781 if (likely(msg->err_pos < -1) || *ptr == '\n')
1782 goto http_msg_invalid;
1783
1784 if (msg->err_pos == -1) /* capture error pointer */
1785 msg->err_pos = ptr - buf->data; /* >= 0 now */
1786
1787 /* and we still accept this non-token character */
1788 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001789
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001790 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001791 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001792 /* assumes msg->sol points to the first char and msg->col to the colon */
1793 if (likely(HTTP_IS_SPHT(*ptr)))
1794 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001795
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001796 /* header value can be basically anything except CR/LF */
1797 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001798
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001799 if (likely(!HTTP_IS_CRLF(*ptr))) {
1800 goto http_msg_hdr_val;
1801 }
1802
1803 if (likely(*ptr == '\r'))
1804 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1805 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001806
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001807 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001808 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001809 EXPECT_LF_HERE(ptr, http_msg_invalid);
1810 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001811
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001812 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001813 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 if (likely(HTTP_IS_SPHT(*ptr))) {
1815 /* replace HT,CR,LF with spaces */
1816 for (; buf->data+msg->sov < ptr; msg->sov++)
1817 buf->data[msg->sov] = ' ';
1818 goto http_msg_hdr_l1_sp;
1819 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001820 /* we had a header consisting only in spaces ! */
1821 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001822 goto http_msg_complete_header;
1823
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001825 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 /* assumes msg->sol points to the first char, msg->col to the
1827 * colon, and msg->sov points to the first character of the
1828 * value.
1829 */
1830 if (likely(!HTTP_IS_CRLF(*ptr)))
1831 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001832
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001833 msg->eol = ptr;
1834 /* Note: we could also copy eol into ->eoh so that we have the
1835 * real header end in case it ends with lots of LWS, but is this
1836 * really needed ?
1837 */
1838 if (likely(*ptr == '\r'))
1839 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1840 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001841
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001842 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001843 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001844 EXPECT_LF_HERE(ptr, http_msg_invalid);
1845 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001846
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001847 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001848 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1850 /* LWS: replace HT,CR,LF with spaces */
1851 for (; msg->eol < ptr; msg->eol++)
1852 *msg->eol = ' ';
1853 goto http_msg_hdr_val;
1854 }
1855 http_msg_complete_header:
1856 /*
1857 * It was a new header, so the last one is finished.
1858 * Assumes msg->sol points to the first char, msg->col to the
1859 * colon, msg->sov points to the first character of the value
1860 * and msg->eol to the first CR or LF so we know how the line
1861 * ends. We insert last header into the index.
1862 */
1863 /*
1864 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1865 write(2, msg->sol, msg->eol-msg->sol);
1866 fprintf(stderr,"\n");
1867 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001868
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001869 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1870 idx, idx->tail) < 0))
1871 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001872
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001873 msg->sol = ptr;
1874 if (likely(!HTTP_IS_CRLF(*ptr))) {
1875 goto http_msg_hdr_name;
1876 }
1877
1878 if (likely(*ptr == '\r'))
1879 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1880 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001881
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001882 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001883 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001884 /* Assumes msg->sol points to the first of either CR or LF */
1885 EXPECT_LF_HERE(ptr, http_msg_invalid);
1886 ptr++;
1887 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001888 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001889 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001890 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001891 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 return;
1893#ifdef DEBUG_FULL
1894 default:
1895 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1896 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001897#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001898 }
1899 http_msg_ood:
1900 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001901 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001902 buf->lr = ptr;
1903 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001904
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001905 http_msg_invalid:
1906 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001907 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001908 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909 return;
1910}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001911
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001912/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1913 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1914 * nothing is done and 1 is returned.
1915 */
1916static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1917{
1918 int delta;
1919 char *cur_end;
1920
1921 if (msg->sl.rq.v_l != 0)
1922 return 1;
1923
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001924 cur_end = msg->sol + msg->sl.rq.l;
1925 delta = 0;
1926
1927 if (msg->sl.rq.u_l == 0) {
1928 /* if no URI was set, add "/" */
1929 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1930 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001931 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001932 }
1933 /* add HTTP version */
1934 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001935 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001936 cur_end += delta;
1937 cur_end = (char *)http_parse_reqline(msg, req->data,
1938 HTTP_MSG_RQMETH,
1939 msg->sol, cur_end + 1,
1940 NULL, NULL);
1941 if (unlikely(!cur_end))
1942 return 0;
1943
1944 /* we have a full HTTP/1.0 request now and we know that
1945 * we have either a CR or an LF at <ptr>.
1946 */
1947 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1948 return 1;
1949}
1950
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001951/* Parse the Connection: header of an HTTP request, looking for both "close"
1952 * and "keep-alive" values. If a buffer is provided and we already know that
1953 * some headers may safely be removed, we remove them now. The <to_del> flags
1954 * are used for that :
1955 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1956 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1957 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1958 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1959 * harmless combinations may be removed. Do not call that after changes have
1960 * been processed. If unused, the buffer can be NULL, and no data will be
1961 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001962 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001963void 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 +01001964{
Willy Tarreau5b154472009-12-21 20:11:07 +01001965 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001966 const char *hdr_val = "Connection";
1967 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001968
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001969 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001970 return;
1971
Willy Tarreau88d349d2010-01-25 12:15:43 +01001972 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1973 hdr_val = "Proxy-Connection";
1974 hdr_len = 16;
1975 }
1976
Willy Tarreau5b154472009-12-21 20:11:07 +01001977 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001978 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001979 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001980 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1981 txn->flags |= TX_HDR_CONN_KAL;
1982 if ((to_del & 2) && buf)
1983 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1984 else
1985 txn->flags |= TX_CON_KAL_SET;
1986 }
1987 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1988 txn->flags |= TX_HDR_CONN_CLO;
1989 if ((to_del & 1) && buf)
1990 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1991 else
1992 txn->flags |= TX_CON_CLO_SET;
1993 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001994 }
1995
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001996 txn->flags |= TX_HDR_CONN_PRS;
1997 return;
1998}
Willy Tarreau5b154472009-12-21 20:11:07 +01001999
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002000/* Apply desired changes on the Connection: header. Values may be removed and/or
2001 * added depending on the <wanted> flags, which are exclusively composed of
2002 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2003 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2004 */
2005void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
2006{
2007 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002008 const char *hdr_val = "Connection";
2009 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002010
2011 ctx.idx = 0;
2012
Willy Tarreau88d349d2010-01-25 12:15:43 +01002013
2014 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2015 hdr_val = "Proxy-Connection";
2016 hdr_len = 16;
2017 }
2018
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002019 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002020 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002021 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2022 if (wanted & TX_CON_KAL_SET)
2023 txn->flags |= TX_CON_KAL_SET;
2024 else
2025 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002026 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002027 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2028 if (wanted & TX_CON_CLO_SET)
2029 txn->flags |= TX_CON_CLO_SET;
2030 else
2031 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002032 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002033 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002034
2035 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2036 return;
2037
2038 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2039 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002040 hdr_val = "Connection: close";
2041 hdr_len = 17;
2042 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2043 hdr_val = "Proxy-Connection: close";
2044 hdr_len = 23;
2045 }
2046 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002047 }
2048
2049 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2050 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002051 hdr_val = "Connection: keep-alive";
2052 hdr_len = 22;
2053 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2054 hdr_val = "Proxy-Connection: keep-alive";
2055 hdr_len = 28;
2056 }
2057 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002058 }
2059 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002060}
2061
Willy Tarreaud98cf932009-12-27 22:54:55 +01002062/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2063 * first byte of body, and increments msg->sov by the number of bytes parsed,
2064 * so that we know we can forward between ->som and ->sov. Note that due to
2065 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2066 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002067 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002068 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002069 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002071{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002072 char *ptr = buf->lr;
2073 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002074 unsigned int chunk = 0;
2075
2076 /* The chunk size is in the following form, though we are only
2077 * interested in the size and CRLF :
2078 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2079 */
2080 while (1) {
2081 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002082 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002083 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002084 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002085 if (c < 0) /* not a hex digit anymore */
2086 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002087 if (++ptr >= end)
2088 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002089 if (chunk & 0xF000000) /* overflow will occur */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002090 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002091 chunk = (chunk << 4) + c;
2092 }
2093
Willy Tarreaud98cf932009-12-27 22:54:55 +01002094 /* empty size not allowed */
2095 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002096 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002097
2098 while (http_is_spht[(unsigned char)*ptr]) {
2099 if (++ptr >= end)
2100 ptr = buf->data;
2101 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002102 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002103 }
2104
Willy Tarreaud98cf932009-12-27 22:54:55 +01002105 /* Up to there, we know that at least one byte is present at *ptr. Check
2106 * for the end of chunk size.
2107 */
2108 while (1) {
2109 if (likely(HTTP_IS_CRLF(*ptr))) {
2110 /* we now have a CR or an LF at ptr */
2111 if (likely(*ptr == '\r')) {
2112 if (++ptr >= end)
2113 ptr = buf->data;
2114 if (ptr == buf->r)
2115 return 0;
2116 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002117
Willy Tarreaud98cf932009-12-27 22:54:55 +01002118 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002119 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002120 if (++ptr >= end)
2121 ptr = buf->data;
2122 /* done */
2123 break;
2124 }
2125 else if (*ptr == ';') {
2126 /* chunk extension, ends at next CRLF */
2127 if (++ptr >= end)
2128 ptr = buf->data;
2129 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002130 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002131
2132 while (!HTTP_IS_CRLF(*ptr)) {
2133 if (++ptr >= end)
2134 ptr = buf->data;
2135 if (ptr == buf->r)
2136 return 0;
2137 }
2138 /* we have a CRLF now, loop above */
2139 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002140 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002141 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002142 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002143 }
2144
Willy Tarreaud98cf932009-12-27 22:54:55 +01002145 /* OK we found our CRLF and now <ptr> points to the next byte,
2146 * which may or may not be present. We save that into ->lr and
2147 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002148 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002149 msg->sov += ptr - buf->lr;
2150 buf->lr = ptr;
Willy Tarreau115acb92009-12-26 13:56:06 +01002151 msg->hdr_content_len = chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002152 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002153 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002154 error:
2155 msg->err_pos = ptr - buf->data;
2156 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002157}
2158
Willy Tarreaud98cf932009-12-27 22:54:55 +01002159/* This function skips trailers in the buffer <buf> associated with HTTP
2160 * message <msg>. The first visited position is buf->lr. If the end of
2161 * the trailers is found, it is automatically scheduled to be forwarded,
2162 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2163 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002164 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2165 * zero. If a parse error is encountered, the function returns < 0 and does not
2166 * change anything except maybe buf->lr and msg->sov. Note that the message
2167 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2168 * which implies that all non-trailers data have already been scheduled for
2169 * forwarding, and that the difference between msg->som and msg->sov exactly
2170 * matches the length of trailers already parsed and not forwarded. It is also
2171 * important to note that this function is designed to be able to parse wrapped
2172 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002173 */
2174int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2175{
2176 /* we have buf->lr which points to next line. Look for CRLF. */
2177 while (1) {
2178 char *p1 = NULL, *p2 = NULL;
2179 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002180 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002181
2182 /* scan current line and stop at LF or CRLF */
2183 while (1) {
2184 if (ptr == buf->r)
2185 return 0;
2186
2187 if (*ptr == '\n') {
2188 if (!p1)
2189 p1 = ptr;
2190 p2 = ptr;
2191 break;
2192 }
2193
2194 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002195 if (p1) {
2196 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002198 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002199 p1 = ptr;
2200 }
2201
2202 ptr++;
2203 if (ptr >= buf->data + buf->size)
2204 ptr = buf->data;
2205 }
2206
2207 /* after LF; point to beginning of next line */
2208 p2++;
2209 if (p2 >= buf->data + buf->size)
2210 p2 = buf->data;
2211
Willy Tarreau638cd022010-01-03 07:42:04 +01002212 bytes = p2 - buf->lr;
2213 if (bytes < 0)
2214 bytes += buf->size;
2215
2216 /* schedule this line for forwarding */
2217 msg->sov += bytes;
2218 if (msg->sov >= buf->size)
2219 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002220
Willy Tarreau638cd022010-01-03 07:42:04 +01002221 if (p1 == buf->lr) {
2222 /* LF/CRLF at beginning of line => end of trailers at p2.
2223 * Everything was scheduled for forwarding, there's nothing
2224 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002225 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002226 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002227 msg->msg_state = HTTP_MSG_DONE;
2228 return 1;
2229 }
2230 /* OK, next line then */
2231 buf->lr = p2;
2232 }
2233}
2234
2235/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2236 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2237 * ->som, buf->lr in order to include this part into the next forwarding phase.
2238 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2239 * not enough data are available, the function does not change anything and
2240 * returns zero. If a parse error is encountered, the function returns < 0 and
2241 * does not change anything. Note: this function is designed to parse wrapped
2242 * CRLF at the end of the buffer.
2243 */
2244int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2245{
2246 char *ptr;
2247 int bytes;
2248
2249 /* NB: we'll check data availabilty at the end. It's not a
2250 * problem because whatever we match first will be checked
2251 * against the correct length.
2252 */
2253 bytes = 1;
2254 ptr = buf->lr;
2255 if (*ptr == '\r') {
2256 bytes++;
2257 ptr++;
2258 if (ptr >= buf->data + buf->size)
2259 ptr = buf->data;
2260 }
2261
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002262 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002263 return 0;
2264
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002265 if (*ptr != '\n') {
2266 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002267 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002268 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002269
2270 ptr++;
2271 if (ptr >= buf->data + buf->size)
2272 ptr = buf->data;
2273 buf->lr = ptr;
2274 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2275 msg->sov = ptr - buf->data;
2276 msg->som = msg->sov - bytes;
2277 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2278 return 1;
2279}
Willy Tarreau5b154472009-12-21 20:11:07 +01002280
Willy Tarreau83e3af02009-12-28 17:39:57 +01002281void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2282{
2283 char *end = buf->data + buf->size;
2284 int off = buf->data + buf->size - buf->w;
2285
2286 /* two possible cases :
2287 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002288 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002289 */
2290 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002291 int block1 = buf->l;
2292 int block2 = 0;
2293 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002294 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002295 block1 = buf->data + buf->size - buf->w;
2296 block2 = buf->r - buf->data;
2297 }
2298 if (block2)
2299 memcpy(swap_buffer, buf->data, block2);
2300 memmove(buf->data, buf->w, block1);
2301 if (block2)
2302 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002303 }
2304
2305 /* adjust all known pointers */
2306 buf->w = buf->data;
2307 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2308 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2309 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2310 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2311
2312 /* adjust relative pointers */
2313 msg->som = 0;
2314 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2315 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2316 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2317
Willy Tarreau83e3af02009-12-28 17:39:57 +01002318 if (msg->err_pos >= 0) {
2319 msg->err_pos += off;
2320 if (msg->err_pos >= buf->size)
2321 msg->err_pos -= buf->size;
2322 }
2323
2324 buf->flags &= ~BF_FULL;
2325 if (buf->l >= buffer_max_len(buf))
2326 buf->flags |= BF_FULL;
2327}
2328
Willy Tarreaud787e662009-07-07 10:14:51 +02002329/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2330 * processing can continue on next analysers, or zero if it either needs more
2331 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2332 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2333 * when it has nothing left to do, and may remove any analyser when it wants to
2334 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002335 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002336int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002337{
Willy Tarreau59234e92008-11-30 23:51:27 +01002338 /*
2339 * We will parse the partial (or complete) lines.
2340 * We will check the request syntax, and also join multi-line
2341 * headers. An index of all the lines will be elaborated while
2342 * parsing.
2343 *
2344 * For the parsing, we use a 28 states FSM.
2345 *
2346 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002347 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002348 * req->data + msg->eoh = end of processed headers / start of current one
2349 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002350 * req->lr = first non-visited byte
2351 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002352 *
2353 * At end of parsing, we may perform a capture of the error (if any), and
2354 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2355 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2356 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002358
Willy Tarreau59234e92008-11-30 23:51:27 +01002359 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002360 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002361 struct http_txn *txn = &s->txn;
2362 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002363 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002364
Willy Tarreau6bf17362009-02-24 10:48:35 +01002365 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2366 now_ms, __FUNCTION__,
2367 s,
2368 req,
2369 req->rex, req->wex,
2370 req->flags,
2371 req->l,
2372 req->analysers);
2373
Willy Tarreau52a0c602009-08-16 22:45:38 +02002374 /* we're speaking HTTP here, so let's speak HTTP to the client */
2375 s->srv_error = http_return_srv_error;
2376
Willy Tarreau83e3af02009-12-28 17:39:57 +01002377 /* There's a protected area at the end of the buffer for rewriting
2378 * purposes. We don't want to start to parse the request if the
2379 * protected area is affected, because we may have to move processed
2380 * data later, which is much more complicated.
2381 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002382 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002383 if ((txn->flags & TX_NOT_FIRST) &&
2384 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002385 req->r < req->lr ||
2386 req->r > req->data + req->size - global.tune.maxrewrite)) {
2387 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002388 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2389 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002390 /* some data has still not left the buffer, wake us once that's done */
2391 buffer_dont_connect(req);
2392 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2393 return 0;
2394 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002395 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002396 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002397 }
2398
Willy Tarreau065e8332010-01-08 00:30:20 +01002399 /* Note that we have the same problem with the response ; we
2400 * may want to send a redirect, error or anything which requires
2401 * some spare space. So we'll ensure that we have at least
2402 * maxrewrite bytes available in the response buffer before
2403 * processing that one. This will only affect pipelined
2404 * keep-alive requests.
2405 */
2406 if ((txn->flags & TX_NOT_FIRST) &&
2407 unlikely((s->rep->flags & BF_FULL) ||
2408 s->rep->r < s->rep->lr ||
2409 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2410 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002411 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2412 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002413 /* don't let a connection request be initiated */
2414 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002415 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002416 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002417 return 0;
2418 }
2419 }
2420
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002421 if (likely(req->lr < req->r))
2422 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002423 }
2424
Willy Tarreau59234e92008-11-30 23:51:27 +01002425 /* 1: we might have to print this header in debug mode */
2426 if (unlikely((global.mode & MODE_DEBUG) &&
2427 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002428 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002429 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002430 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002431
Willy Tarreau663308b2010-06-07 14:06:08 +02002432 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002433 eol = sol + msg->sl.rq.l;
2434 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002435
Willy Tarreau59234e92008-11-30 23:51:27 +01002436 sol += hdr_idx_first_pos(&txn->hdr_idx);
2437 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002438
Willy Tarreau59234e92008-11-30 23:51:27 +01002439 while (cur_idx) {
2440 eol = sol + txn->hdr_idx.v[cur_idx].len;
2441 debug_hdr("clihdr", s, sol, eol);
2442 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2443 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002444 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002445 }
2446
Willy Tarreau58f10d72006-12-04 02:26:12 +01002447
Willy Tarreau59234e92008-11-30 23:51:27 +01002448 /*
2449 * Now we quickly check if we have found a full valid request.
2450 * If not so, we check the FD and buffer states before leaving.
2451 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002452 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002453 * requests are checked first. When waiting for a second request
2454 * on a keep-alive session, if we encounter and error, close, t/o,
2455 * we note the error in the session flags but don't set any state.
2456 * Since the error will be noted there, it will not be counted by
2457 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002458 * Last, we may increase some tracked counters' http request errors on
2459 * the cases that are deliberately the client's fault. For instance,
2460 * a timeout or connection reset is not counted as an error. However
2461 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002462 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002463
Willy Tarreau655dce92009-11-08 13:10:58 +01002464 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002465 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002466 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002467 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002468 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002469 session_inc_http_req_ctr(s);
2470 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002471 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002472 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002473 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002474
Willy Tarreau59234e92008-11-30 23:51:27 +01002475 /* 1: Since we are in header mode, if there's no space
2476 * left for headers, we won't be able to free more
2477 * later, so the session will never terminate. We
2478 * must terminate it now.
2479 */
2480 if (unlikely(req->flags & BF_FULL)) {
2481 /* FIXME: check if URI is set and return Status
2482 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002483 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002484 session_inc_http_req_ctr(s);
2485 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002486 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002487 goto return_bad_req;
2488 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002489
Willy Tarreau59234e92008-11-30 23:51:27 +01002490 /* 2: have we encountered a read error ? */
2491 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002492 if (!(s->flags & SN_ERR_MASK))
2493 s->flags |= SN_ERR_CLICL;
2494
Willy Tarreaufcffa692010-01-10 14:21:19 +01002495 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002496 goto failed_keep_alive;
2497
Willy Tarreau59234e92008-11-30 23:51:27 +01002498 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002499 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002500 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002501 session_inc_http_err_ctr(s);
2502 }
2503
Willy Tarreau59234e92008-11-30 23:51:27 +01002504 msg->msg_state = HTTP_MSG_ERROR;
2505 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002506
Willy Tarreauda7ff642010-06-23 11:44:09 +02002507 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002508 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002509 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002510 if (s->listener->counters)
2511 s->listener->counters->failed_req++;
2512
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 if (!(s->flags & SN_FINST_MASK))
2514 s->flags |= SN_FINST_R;
2515 return 0;
2516 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002517
Willy Tarreau59234e92008-11-30 23:51:27 +01002518 /* 3: has the read timeout expired ? */
2519 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002520 if (!(s->flags & SN_ERR_MASK))
2521 s->flags |= SN_ERR_CLITO;
2522
Willy Tarreaufcffa692010-01-10 14:21:19 +01002523 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002524 goto failed_keep_alive;
2525
Willy Tarreau59234e92008-11-30 23:51:27 +01002526 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002527 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002528 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002529 session_inc_http_err_ctr(s);
2530 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002531 txn->status = 408;
2532 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2533 msg->msg_state = HTTP_MSG_ERROR;
2534 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002535
Willy Tarreauda7ff642010-06-23 11:44:09 +02002536 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002537 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002538 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002539 if (s->listener->counters)
2540 s->listener->counters->failed_req++;
2541
Willy Tarreau59234e92008-11-30 23:51:27 +01002542 if (!(s->flags & SN_FINST_MASK))
2543 s->flags |= SN_FINST_R;
2544 return 0;
2545 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002546
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 /* 4: have we encountered a close ? */
2548 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002549 if (!(s->flags & SN_ERR_MASK))
2550 s->flags |= SN_ERR_CLICL;
2551
Willy Tarreaufcffa692010-01-10 14:21:19 +01002552 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002553 goto failed_keep_alive;
2554
Willy Tarreau4076a152009-04-02 15:18:36 +02002555 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002556 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 txn->status = 400;
2558 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2559 msg->msg_state = HTTP_MSG_ERROR;
2560 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002561
Willy Tarreauda7ff642010-06-23 11:44:09 +02002562 session_inc_http_err_ctr(s);
2563 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002564 proxy_inc_fe_req_ctr(s->fe);
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002565 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002566 if (s->listener->counters)
2567 s->listener->counters->failed_req++;
2568
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 if (!(s->flags & SN_FINST_MASK))
2570 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002571 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002572 }
2573
Willy Tarreau520d95e2009-09-19 21:04:57 +02002574 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002575 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002576 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002577
Willy Tarreaufcffa692010-01-10 14:21:19 +01002578 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2579 /* If the client starts to talk, let's fall back to
2580 * request timeout processing.
2581 */
2582 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002583 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002584 }
2585
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002587 if (!tick_isset(req->analyse_exp)) {
2588 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2589 (txn->flags & TX_WAIT_NEXT_RQ) &&
2590 tick_isset(s->be->timeout.httpka))
2591 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2592 else
2593 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2594 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002595
Willy Tarreau59234e92008-11-30 23:51:27 +01002596 /* we're not ready yet */
2597 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002598
2599 failed_keep_alive:
2600 /* Here we process low-level errors for keep-alive requests. In
2601 * short, if the request is not the first one and it experiences
2602 * a timeout, read error or shutdown, we just silently close so
2603 * that the client can try again.
2604 */
2605 txn->status = 0;
2606 msg->msg_state = HTTP_MSG_RQBEFORE;
2607 req->analysers = 0;
2608 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002609 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002610 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002611 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002612 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002613
Willy Tarreaud787e662009-07-07 10:14:51 +02002614 /* OK now we have a complete HTTP request with indexed headers. Let's
2615 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002616 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2617 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2618 * points to the CRLF of the request line. req->lr points to the first
2619 * byte after the last LF. msg->col and msg->sov point to the first
2620 * byte of data. msg->eol cannot be trusted because it may have been
2621 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002622 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002623
Willy Tarreauda7ff642010-06-23 11:44:09 +02002624 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002625 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2626
Willy Tarreaub16a5742010-01-10 14:46:16 +01002627 if (txn->flags & TX_WAIT_NEXT_RQ) {
2628 /* kill the pending keep-alive timeout */
2629 txn->flags &= ~TX_WAIT_NEXT_RQ;
2630 req->analyse_exp = TICK_ETERNITY;
2631 }
2632
2633
Willy Tarreaud787e662009-07-07 10:14:51 +02002634 /* Maybe we found in invalid header name while we were configured not
2635 * to block on that, so we have to capture it now.
2636 */
2637 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002638 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002639
Willy Tarreau59234e92008-11-30 23:51:27 +01002640 /*
2641 * 1: identify the method
2642 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002643 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002644
2645 /* we can make use of server redirect on GET and HEAD */
2646 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2647 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002648
Willy Tarreau59234e92008-11-30 23:51:27 +01002649 /*
2650 * 2: check if the URI matches the monitor_uri.
2651 * We have to do this for every request which gets in, because
2652 * the monitor-uri is defined by the frontend.
2653 */
2654 if (unlikely((s->fe->monitor_uri_len != 0) &&
2655 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002656 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 s->fe->monitor_uri,
2658 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002659 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002661 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002662 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002663
Willy Tarreau59234e92008-11-30 23:51:27 +01002664 s->flags |= SN_MONITOR;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002665
Willy Tarreau59234e92008-11-30 23:51:27 +01002666 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002667 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2668 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002669
Willy Tarreau59234e92008-11-30 23:51:27 +01002670 ret = acl_pass(ret);
2671 if (cond->pol == ACL_COND_UNLESS)
2672 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002673
Willy Tarreau59234e92008-11-30 23:51:27 +01002674 if (ret) {
2675 /* we fail this request, let's return 503 service unavail */
2676 txn->status = 503;
2677 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2678 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002679 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002680 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002681
Willy Tarreau59234e92008-11-30 23:51:27 +01002682 /* nothing to fail, let's reply normaly */
2683 txn->status = 200;
2684 stream_int_retnclose(req->prod, &http_200_chunk);
2685 goto return_prx_cond;
2686 }
2687
2688 /*
2689 * 3: Maybe we have to copy the original REQURI for the logs ?
2690 * Note: we cannot log anymore if the request has been
2691 * classified as invalid.
2692 */
2693 if (unlikely(s->logs.logwait & LW_REQ)) {
2694 /* we have a complete HTTP request that we must log */
2695 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2696 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002697
Willy Tarreau59234e92008-11-30 23:51:27 +01002698 if (urilen >= REQURI_LEN)
2699 urilen = REQURI_LEN - 1;
2700 memcpy(txn->uri, &req->data[msg->som], urilen);
2701 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002702
Willy Tarreau59234e92008-11-30 23:51:27 +01002703 if (!(s->logs.logwait &= ~LW_REQ))
2704 s->do_log(s);
2705 } else {
2706 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002707 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002708 }
Willy Tarreau06619262006-12-17 08:37:22 +01002709
Willy Tarreau59234e92008-11-30 23:51:27 +01002710 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002711 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2712 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002713
Willy Tarreau5b154472009-12-21 20:11:07 +01002714 /* ... and check if the request is HTTP/1.1 or above */
2715 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002716 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2717 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2718 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002719 txn->flags |= TX_REQ_VER_11;
2720
2721 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002722 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002723
Willy Tarreau88d349d2010-01-25 12:15:43 +01002724 /* if the frontend has "option http-use-proxy-header", we'll check if
2725 * we have what looks like a proxied connection instead of a connection,
2726 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2727 * Note that this is *not* RFC-compliant, however browsers and proxies
2728 * happen to do that despite being non-standard :-(
2729 * We consider that a request not beginning with either '/' or '*' is
2730 * a proxied connection, which covers both "scheme://location" and
2731 * CONNECT ip:port.
2732 */
2733 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2734 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2735 txn->flags |= TX_USE_PX_CONN;
2736
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002737 /* transfer length unknown*/
2738 txn->flags &= ~TX_REQ_XFER_LEN;
2739
Willy Tarreau59234e92008-11-30 23:51:27 +01002740 /* 5: we may need to capture headers */
2741 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002742 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002743 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002744
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002745 /* 6: determine the transfer-length.
2746 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2747 * the presence of a message-body in a REQUEST and its transfer length
2748 * must be determined that way (in order of precedence) :
2749 * 1. The presence of a message-body in a request is signaled by the
2750 * inclusion of a Content-Length or Transfer-Encoding header field
2751 * in the request's header fields. When a request message contains
2752 * both a message-body of non-zero length and a method that does
2753 * not define any semantics for that request message-body, then an
2754 * origin server SHOULD either ignore the message-body or respond
2755 * with an appropriate error message (e.g., 413). A proxy or
2756 * gateway, when presented the same request, SHOULD either forward
2757 * the request inbound with the message- body or ignore the
2758 * message-body when determining a response.
2759 *
2760 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2761 * and the "chunked" transfer-coding (Section 6.2) is used, the
2762 * transfer-length is defined by the use of this transfer-coding.
2763 * If a Transfer-Encoding header field is present and the "chunked"
2764 * transfer-coding is not present, the transfer-length is defined
2765 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002766 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002767 * 3. If a Content-Length header field is present, its decimal value in
2768 * OCTETs represents both the entity-length and the transfer-length.
2769 * If a message is received with both a Transfer-Encoding header
2770 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002771 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002772 * 4. By the server closing the connection. (Closing the connection
2773 * cannot be used to indicate the end of a request body, since that
2774 * would leave no possibility for the server to send back a response.)
2775 *
2776 * Whenever a transfer-coding is applied to a message-body, the set of
2777 * transfer-codings MUST include "chunked", unless the message indicates
2778 * it is terminated by closing the connection. When the "chunked"
2779 * transfer-coding is used, it MUST be the last transfer-coding applied
2780 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002781 */
2782
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002783 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002784 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002785 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002786 while ((txn->flags & TX_REQ_VER_11) &&
2787 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002788 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2789 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2790 else if (txn->flags & TX_REQ_TE_CHNK) {
2791 /* bad transfer-encoding (chunked followed by something else) */
2792 use_close_only = 1;
2793 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2794 break;
2795 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002796 }
2797
Willy Tarreau32b47f42009-10-18 20:55:02 +02002798 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002799 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002800 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2801 signed long long cl;
2802
2803 if (!ctx.vlen)
2804 goto return_bad_req;
2805
2806 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
2807 goto return_bad_req; /* parse failure */
2808
2809 if (cl < 0)
2810 goto return_bad_req;
2811
2812 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl))
2813 goto return_bad_req; /* already specified, was different */
2814
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002815 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002816 msg->hdr_content_len = cl;
2817 }
2818
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002819 /* bodyless requests have a known length */
2820 if (!use_close_only)
2821 txn->flags |= TX_REQ_XFER_LEN;
2822
Willy Tarreaud787e662009-07-07 10:14:51 +02002823 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002824 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002825 req->analyse_exp = TICK_ETERNITY;
2826 return 1;
2827
2828 return_bad_req:
2829 /* We centralize bad requests processing here */
2830 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2831 /* we detected a parsing error. We want to archive this request
2832 * in the dedicated proxy area for later troubleshooting.
2833 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002834 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002835 }
2836
2837 txn->req.msg_state = HTTP_MSG_ERROR;
2838 txn->status = 400;
2839 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002840
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002841 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002842 if (s->listener->counters)
2843 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002844
2845 return_prx_cond:
2846 if (!(s->flags & SN_ERR_MASK))
2847 s->flags |= SN_ERR_PRXCOND;
2848 if (!(s->flags & SN_FINST_MASK))
2849 s->flags |= SN_FINST_R;
2850
2851 req->analysers = 0;
2852 req->analyse_exp = TICK_ETERNITY;
2853 return 0;
2854}
2855
Cyril Bonté70be45d2010-10-12 00:14:35 +02002856/* We reached the stats page through a POST request.
2857 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002858 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002859 */
2860int http_process_req_stat_post(struct session *s, struct buffer *req)
2861{
2862 struct http_txn *txn = &s->txn;
2863 struct proxy *px;
2864 struct server *sv;
2865
2866 char *backend = NULL;
2867 int action = 0;
2868
2869 char *first_param, *cur_param, *next_param, *end_params;
2870
2871 first_param = req->data + txn->req.eoh + 2;
2872 end_params = first_param + txn->req.hdr_content_len;
2873
2874 cur_param = next_param = end_params;
2875
Cyril Bonté23b39d92011-02-10 22:54:44 +01002876 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002877 /* Prevent buffer overflow */
2878 s->data_ctx.stats.st_code = STAT_STATUS_EXCD;
2879 return 1;
2880 }
2881 else if (end_params > req->data + req->l) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002882 /* we need more data */
2883 s->data_ctx.stats.st_code = STAT_STATUS_NONE;
2884 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002885 }
2886
2887 *end_params = '\0';
2888
2889 s->data_ctx.stats.st_code = STAT_STATUS_NONE;
2890
2891 /*
2892 * Parse the parameters in reverse order to only store the last value.
2893 * From the html form, the backend and the action are at the end.
2894 */
2895 while (cur_param > first_param) {
2896 char *key, *value;
2897
2898 cur_param--;
2899 if ((*cur_param == '&') || (cur_param == first_param)) {
2900 /* Parse the key */
2901 key = cur_param;
2902 if (cur_param != first_param) {
2903 /* delimit the string for the next loop */
2904 *key++ = '\0';
2905 }
2906
2907 /* Parse the value */
2908 value = key;
2909 while (*value != '\0' && *value != '=') {
2910 value++;
2911 }
2912 if (*value == '=') {
2913 /* Ok, a value is found, we can mark the end of the key */
2914 *value++ = '\0';
2915 }
2916
2917 /* Now we can check the key to see what to do */
2918 if (!backend && strcmp(key, "b") == 0) {
2919 backend = value;
2920 }
2921 else if (!action && strcmp(key, "action") == 0) {
2922 if (strcmp(value, "disable") == 0) {
2923 action = 1;
2924 }
2925 else if (strcmp(value, "enable") == 0) {
2926 action = 2;
2927 } else {
2928 /* unknown action, no need to continue */
2929 break;
2930 }
2931 }
2932 else if (strcmp(key, "s") == 0) {
2933 if (backend && action && get_backend_server(backend, value, &px, &sv)) {
2934 switch (action) {
2935 case 1:
2936 if (! (sv->state & SRV_MAINTAIN)) {
2937 /* Not already in maintenance, we can change the server state */
2938 sv->state |= SRV_MAINTAIN;
2939 set_server_down(sv);
2940 s->data_ctx.stats.st_code = STAT_STATUS_DONE;
2941 }
2942 break;
2943 case 2:
2944 if ((sv->state & SRV_MAINTAIN)) {
2945 /* Already in maintenance, we can change the server state */
2946 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002947 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002948 s->data_ctx.stats.st_code = STAT_STATUS_DONE;
2949 }
2950 break;
2951 }
2952 }
2953 }
2954 next_param = cur_param;
2955 }
2956 }
Cyril Bonté23b39d92011-02-10 22:54:44 +01002957 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002958}
2959
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002960/* This stream analyser runs all HTTP request processing which is common to
2961 * frontends and backends, which means blocking ACLs, filters, connection-close,
2962 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002963 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002964 * either needs more data or wants to immediately abort the request (eg: deny,
2965 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002966 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002967int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002968{
Willy Tarreaud787e662009-07-07 10:14:51 +02002969 struct http_txn *txn = &s->txn;
2970 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002971 struct acl_cond *cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002972 struct req_acl_rule *req_acl, *req_acl_final = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002973 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002974 struct cond_wordlist *wl;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002975 int del_ka, del_cl, do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002976
Willy Tarreau655dce92009-11-08 13:10:58 +01002977 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002978 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002979 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002980 return 0;
2981 }
2982
Willy Tarreau3a816292009-07-07 10:55:49 +02002983 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002984 req->analyse_exp = TICK_ETERNITY;
2985
2986 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2987 now_ms, __FUNCTION__,
2988 s,
2989 req,
2990 req->rex, req->wex,
2991 req->flags,
2992 req->l,
2993 req->analysers);
2994
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002995 /* first check whether we have some ACLs set to block this request */
2996 list_for_each_entry(cond, &px->block_cond, list) {
2997 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002998
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002999 ret = acl_pass(ret);
3000 if (cond->pol == ACL_COND_UNLESS)
3001 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003002
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003003 if (ret) {
3004 txn->status = 403;
3005 /* let's log the request time */
3006 s->logs.tv_request = now;
3007 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003008 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003009 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003010 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003011 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003012
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003013 do_stats = stats_check_uri(s, px);
3014
3015 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 +01003016 int ret = 1;
3017
3018 if (req_acl->action >= PR_REQ_ACL_ACT_MAX)
3019 continue;
3020
3021 /* check condition, but only if attached */
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01003022 if (req_acl->cond) {
3023 ret = acl_exec_cond(req_acl->cond, px, s, txn, ACL_DIR_REQ);
3024 ret = acl_pass(ret);
Willy Tarreau51425942010-02-01 10:40:19 +01003025
Krzysztof Olędzki711ad9e2010-02-01 12:36:53 +01003026 if (req_acl->cond->pol == ACL_COND_UNLESS)
3027 ret = !ret;
3028 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003029
3030 if (ret) {
3031 req_acl_final = req_acl;
3032 break;
3033 }
3034 }
3035
3036 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_DENY) {
3037 txn->status = 403;
3038 s->logs.tv_request = now;
3039 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003040 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003041 goto return_prx_cond;
3042 }
3043
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003044 /* try headers filters */
3045 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003046 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003047 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003048
Willy Tarreau59234e92008-11-30 23:51:27 +01003049 /* has the request been denied ? */
3050 if (txn->flags & TX_CLDENY) {
3051 /* no need to go further */
3052 txn->status = 403;
3053 /* let's log the request time */
3054 s->logs.tv_request = now;
3055 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003056 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003057 goto return_prx_cond;
3058 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003059
3060 /* When a connection is tarpitted, we use the tarpit timeout,
3061 * which may be the same as the connect timeout if unspecified.
3062 * If unset, then set it to zero because we really want it to
3063 * eventually expire. We build the tarpit as an analyser.
3064 */
3065 if (txn->flags & TX_CLTARPIT) {
3066 buffer_erase(s->req);
3067 /* wipe the request out so that we can drop the connection early
3068 * if the client closes first.
3069 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003070 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003071 req->analysers = 0; /* remove switching rules etc... */
3072 req->analysers |= AN_REQ_HTTP_TARPIT;
3073 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3074 if (!req->analyse_exp)
3075 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003076 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003077 return 1;
3078 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003079 }
Willy Tarreau06619262006-12-17 08:37:22 +01003080
Willy Tarreau5b154472009-12-21 20:11:07 +01003081 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3082 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003083 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3084 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003085 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3086 * avoid to redo the same work if FE and BE have the same settings (common).
3087 * The method consists in checking if options changed between the two calls
3088 * (implying that either one is non-null, or one of them is non-null and we
3089 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003090 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003091
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003092 del_cl = del_ka = 0;
3093
Willy Tarreaudc008c52010-02-01 16:20:08 +01003094 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3095 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3096 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3097 (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 +01003098 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003099
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003100 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3101 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003102 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003103 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3104 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003105 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003106 tmp = TX_CON_WANT_CLO;
3107
Willy Tarreau5b154472009-12-21 20:11:07 +01003108 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3109 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003110
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003111 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3112 /* parse the Connection header and possibly clean it */
3113 int to_del = 0;
3114 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003115 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3116 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003117 to_del |= 2; /* remove "keep-alive" */
3118 if (!(txn->flags & TX_REQ_VER_11))
3119 to_del |= 1; /* remove "close" */
3120 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003121 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003122
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003123 /* check if client or config asks for explicit close in KAL/SCL */
3124 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3125 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3126 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
3127 (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003128 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003129 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
3130 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003131 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3132 }
Willy Tarreau78599912009-10-17 20:12:21 +02003133
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003134 /* add request headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01003135 list_for_each_entry(wl, &px->req_add, list) {
Willy Tarreau8abd4cd2010-01-31 14:30:44 +01003136 if (wl->cond) {
3137 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
3138 ret = acl_pass(ret);
3139 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3140 ret = !ret;
3141 if (!ret)
3142 continue;
3143 }
3144
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01003145 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003146 goto return_bad_req;
3147 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003148
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003149 if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) {
3150 struct chunk msg;
Willy Tarreau910ef302011-01-06 16:44:39 +01003151 char *realm = req_acl_final->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003152
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003153 if (!realm)
3154 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3155
Willy Tarreau844a7e72010-01-31 21:46:18 +01003156 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003157 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3158 txn->status = 401;
3159 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003160 /* on 401 we still count one error, because normal browsing
3161 * won't significantly increase the counter but brute force
3162 * attempts will.
3163 */
3164 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003165 goto return_prx_cond;
3166 }
3167
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003168 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003169 struct stats_admin_rule *stats_admin_rule;
3170
3171 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003172 * FIXME!!! that one is rather dangerous, we want to
3173 * make it follow standard rules (eg: clear req->analysers).
3174 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003175
Cyril Bonté474be412010-10-12 00:14:36 +02003176 /* now check whether we have some admin rules for this request */
3177 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3178 int ret = 1;
3179
3180 if (stats_admin_rule->cond) {
3181 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
3182 ret = acl_pass(ret);
3183 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3184 ret = !ret;
3185 }
3186
3187 if (ret) {
3188 /* no rule, or the rule matches */
3189 s->data_ctx.stats.flags |= STAT_ADMIN;
3190 break;
3191 }
3192 }
3193
Cyril Bonté70be45d2010-10-12 00:14:35 +02003194 /* Was the status page requested with a POST ? */
3195 if (txn->meth == HTTP_METH_POST) {
Cyril Bonté474be412010-10-12 00:14:36 +02003196 if (s->data_ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003197 if (msg->msg_state < HTTP_MSG_100_SENT) {
3198 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3199 * send an HTTP/1.1 100 Continue intermediate response.
3200 */
3201 if (txn->flags & TX_REQ_VER_11) {
3202 struct hdr_ctx ctx;
3203 ctx.idx = 0;
3204 /* Expect is allowed in 1.1, look for it */
3205 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3206 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3207 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3208 }
3209 }
3210 msg->msg_state = HTTP_MSG_100_SENT;
3211 s->logs.tv_request = now; /* update the request timer to reflect full request */
3212 }
3213 if (!http_process_req_stat_post(s, req)) {
3214 /* we need more data */
3215 req->analysers |= an_bit;
3216 buffer_dont_connect(req);
3217 return 0;
3218 }
Cyril Bonté474be412010-10-12 00:14:36 +02003219 } else {
3220 s->data_ctx.stats.st_code = STAT_STATUS_DENY;
3221 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003222 }
3223
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003224 s->logs.tv_request = now;
3225 s->data_source = DATA_SRC_STATS;
3226 s->data_state = DATA_ST_INIT;
3227 s->task->nice = -32; /* small boost for HTTP statistics */
3228 stream_int_register_handler(s->rep->prod, http_stats_io_handler);
3229 s->rep->prod->private = s;
3230 s->rep->prod->st0 = s->rep->prod->st1 = 0;
3231 req->analysers = 0;
3232
3233 return 0;
3234
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003235 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003236
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003237 /* check whether we have some ACLs set to redirect this request */
3238 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003239 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003240
Willy Tarreauf285f542010-01-03 20:03:03 +01003241 if (rule->cond) {
3242 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3243 ret = acl_pass(ret);
3244 if (rule->cond->pol == ACL_COND_UNLESS)
3245 ret = !ret;
3246 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003247
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003248 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003249 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003250 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003251
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003252 /* build redirect message */
3253 switch(rule->code) {
3254 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003255 msg_fmt = HTTP_303;
3256 break;
3257 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003258 msg_fmt = HTTP_301;
3259 break;
3260 case 302:
3261 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003262 msg_fmt = HTTP_302;
3263 break;
3264 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003265
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003266 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003267 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003268
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003269 switch(rule->type) {
3270 case REDIRECT_TYPE_PREFIX: {
3271 const char *path;
3272 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003273
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003274 path = http_get_path(txn);
3275 /* build message using path */
3276 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003277 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003278 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3279 int qs = 0;
3280 while (qs < pathlen) {
3281 if (path[qs] == '?') {
3282 pathlen = qs;
3283 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003284 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003285 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003286 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003287 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003288 } else {
3289 path = "/";
3290 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003291 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003292
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003293 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003294 goto return_bad_req;
3295
3296 /* add prefix. Note that if prefix == "/", we don't want to
3297 * add anything, otherwise it makes it hard for the user to
3298 * configure a self-redirection.
3299 */
3300 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003301 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3302 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003303 }
3304
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003305 /* add path */
3306 memcpy(rdr.str + rdr.len, path, pathlen);
3307 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003308
3309 /* append a slash at the end of the location is needed and missing */
3310 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3311 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3312 if (rdr.len > rdr.size - 5)
3313 goto return_bad_req;
3314 rdr.str[rdr.len] = '/';
3315 rdr.len++;
3316 }
3317
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003318 break;
3319 }
3320 case REDIRECT_TYPE_LOCATION:
3321 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003322 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003323 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003324
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003325 /* add location */
3326 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3327 rdr.len += rule->rdr_len;
3328 break;
3329 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003330
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003331 if (rule->cookie_len) {
3332 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3333 rdr.len += 14;
3334 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3335 rdr.len += rule->cookie_len;
3336 memcpy(rdr.str + rdr.len, "\r\n", 2);
3337 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003338 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003339
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003340 /* add end of headers and the keep-alive/close status.
3341 * We may choose to set keep-alive if the Location begins
3342 * with a slash, because the client will come back to the
3343 * same server.
3344 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003345 txn->status = rule->code;
3346 /* let's log the request time */
3347 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003348
3349 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3350 (txn->flags & TX_REQ_XFER_LEN) &&
3351 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len &&
3352 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3353 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3354 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003355 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003356 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3357 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3358 rdr.len += 30;
3359 } else {
3360 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3361 rdr.len += 24;
3362 }
Willy Tarreau75661452010-01-10 10:35:01 +01003363 }
3364 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3365 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003366 buffer_write(req->prod->ob, rdr.str, rdr.len);
3367 /* "eat" the request */
3368 buffer_ignore(req, msg->sov - msg->som);
3369 msg->som = msg->sov;
3370 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003371 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3372 txn->req.msg_state = HTTP_MSG_CLOSED;
3373 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003374 break;
3375 } else {
3376 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003377 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3378 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3379 rdr.len += 29;
3380 } else {
3381 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3382 rdr.len += 23;
3383 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003384 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003385 goto return_prx_cond;
3386 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003387 }
3388 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003389
Willy Tarreau2be39392010-01-03 17:24:51 +01003390 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3391 * If this happens, then the data will not come immediately, so we must
3392 * send all what we have without waiting. Note that due to the small gain
3393 * in waiting for the body of the request, it's easier to simply put the
3394 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3395 * itself once used.
3396 */
3397 req->flags |= BF_SEND_DONTWAIT;
3398
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003399 /* that's OK for us now, let's move on to next analysers */
3400 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003401
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003402 return_bad_req:
3403 /* We centralize bad requests processing here */
3404 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3405 /* we detected a parsing error. We want to archive this request
3406 * in the dedicated proxy area for later troubleshooting.
3407 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003408 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003409 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003410
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003411 txn->req.msg_state = HTTP_MSG_ERROR;
3412 txn->status = 400;
3413 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003414
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003415 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003416 if (s->listener->counters)
3417 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003418
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003419 return_prx_cond:
3420 if (!(s->flags & SN_ERR_MASK))
3421 s->flags |= SN_ERR_PRXCOND;
3422 if (!(s->flags & SN_FINST_MASK))
3423 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003424
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003425 req->analysers = 0;
3426 req->analyse_exp = TICK_ETERNITY;
3427 return 0;
3428}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003429
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003430/* This function performs all the processing enabled for the current request.
3431 * It returns 1 if the processing can continue on next analysers, or zero if it
3432 * needs more data, encounters an error, or wants to immediately abort the
3433 * request. It relies on buffers flags, and updates s->req->analysers.
3434 */
3435int http_process_request(struct session *s, struct buffer *req, int an_bit)
3436{
3437 struct http_txn *txn = &s->txn;
3438 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003439
Willy Tarreau655dce92009-11-08 13:10:58 +01003440 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003441 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003442 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003443 return 0;
3444 }
3445
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003446 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3447 now_ms, __FUNCTION__,
3448 s,
3449 req,
3450 req->rex, req->wex,
3451 req->flags,
3452 req->l,
3453 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003454
Willy Tarreau59234e92008-11-30 23:51:27 +01003455 /*
3456 * Right now, we know that we have processed the entire headers
3457 * and that unwanted requests have been filtered out. We can do
3458 * whatever we want with the remaining request. Also, now we
3459 * may have separate values for ->fe, ->be.
3460 */
Willy Tarreau06619262006-12-17 08:37:22 +01003461
Willy Tarreau59234e92008-11-30 23:51:27 +01003462 /*
3463 * If HTTP PROXY is set we simply get remote server address
3464 * parsing incoming request.
3465 */
3466 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003467 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr);
Willy Tarreau59234e92008-11-30 23:51:27 +01003468 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003469
Willy Tarreau59234e92008-11-30 23:51:27 +01003470 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003471 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003472 * Note that doing so might move headers in the request, but
3473 * the fields will stay coherent and the URI will not move.
3474 * This should only be performed in the backend.
3475 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003476 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003477 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3478 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003479
Willy Tarreau59234e92008-11-30 23:51:27 +01003480 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003481 * 8: the appsession cookie was looked up very early in 1.2,
3482 * so let's do the same now.
3483 */
3484
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003485 /* It needs to look into the URI unless persistence must be ignored */
3486 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003487 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003488 }
3489
3490 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003491 * 9: add X-Forwarded-For if either the frontend or the backend
3492 * asks for it.
3493 */
3494 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
3495 if (s->cli_addr.ss_family == AF_INET) {
3496 /* Add an X-Forwarded-For header unless the source IP is
3497 * in the 'except' network range.
3498 */
3499 if ((!s->fe->except_mask.s_addr ||
3500 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr)
3501 != s->fe->except_net.s_addr) &&
3502 (!s->be->except_mask.s_addr ||
3503 (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr)
3504 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003505 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003506 unsigned char *pn;
3507 pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003508
3509 /* Note: we rely on the backend to get the header name to be used for
3510 * x-forwarded-for, because the header is really meant for the backends.
3511 * However, if the backend did not specify any option, we have to rely
3512 * on the frontend's header name.
3513 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003514 if (s->be->fwdfor_hdr_len) {
3515 len = s->be->fwdfor_hdr_len;
3516 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003517 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003518 len = s->fe->fwdfor_hdr_len;
3519 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003520 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003521 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003522
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003523 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003524 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003525 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003526 }
3527 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003528 else if (s->cli_addr.ss_family == AF_INET6) {
3529 /* FIXME: for the sake of completeness, we should also support
3530 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003531 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003532 int len;
3533 char pn[INET6_ADDRSTRLEN];
3534 inet_ntop(AF_INET6,
3535 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
3536 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003537
Willy Tarreau59234e92008-11-30 23:51:27 +01003538 /* Note: we rely on the backend to get the header name to be used for
3539 * x-forwarded-for, because the header is really meant for the backends.
3540 * However, if the backend did not specify any option, we have to rely
3541 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003542 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003543 if (s->be->fwdfor_hdr_len) {
3544 len = s->be->fwdfor_hdr_len;
3545 memcpy(trash, s->be->fwdfor_hdr_name, len);
3546 } else {
3547 len = s->fe->fwdfor_hdr_len;
3548 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003549 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003550 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003551
Willy Tarreau59234e92008-11-30 23:51:27 +01003552 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003553 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003554 goto return_bad_req;
3555 }
3556 }
3557
3558 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003559 * 10: add X-Original-To if either the frontend or the backend
3560 * asks for it.
3561 */
3562 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3563
3564 /* FIXME: don't know if IPv6 can handle that case too. */
3565 if (s->cli_addr.ss_family == AF_INET) {
3566 /* Add an X-Original-To header unless the destination IP is
3567 * in the 'except' network range.
3568 */
3569 if (!(s->flags & SN_FRT_ADDR_SET))
3570 get_frt_addr(s);
3571
Emeric Brun5bd86a82010-10-22 17:23:04 +02003572 if (s->frt_addr.ss_family == AF_INET &&
3573 ((!s->fe->except_mask_to.s_addr ||
3574 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
3575 != s->fe->except_to.s_addr) &&
3576 (!s->be->except_mask_to.s_addr ||
3577 (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
3578 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003579 int len;
3580 unsigned char *pn;
3581 pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr;
3582
3583 /* Note: we rely on the backend to get the header name to be used for
3584 * x-original-to, because the header is really meant for the backends.
3585 * However, if the backend did not specify any option, we have to rely
3586 * on the frontend's header name.
3587 */
3588 if (s->be->orgto_hdr_len) {
3589 len = s->be->orgto_hdr_len;
3590 memcpy(trash, s->be->orgto_hdr_name, len);
3591 } else {
3592 len = s->fe->orgto_hdr_len;
3593 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003594 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003595 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3596
3597 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003598 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003599 goto return_bad_req;
3600 }
3601 }
3602 }
3603
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003604 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3605 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003606 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003607 unsigned int want_flags = 0;
3608
3609 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003610 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3611 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3612 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003613 want_flags |= TX_CON_CLO_SET;
3614 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003615 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3616 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3617 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003618 want_flags |= TX_CON_KAL_SET;
3619 }
3620
3621 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3622 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003623 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003624
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003625
Willy Tarreau522d6c02009-12-06 18:49:18 +01003626 /* If we have no server assigned yet and we're balancing on url_param
3627 * with a POST request, we may be interested in checking the body for
3628 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003629 */
3630 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3631 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003632 s->be->url_param_post_limit != 0 &&
3633 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01003634 memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003635 buffer_dont_connect(req);
3636 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003637 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003638
Willy Tarreaud98cf932009-12-27 22:54:55 +01003639 if (txn->flags & TX_REQ_XFER_LEN)
3640 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01003641
Willy Tarreau59234e92008-11-30 23:51:27 +01003642 /*************************************************************
3643 * OK, that's finished for the headers. We have done what we *
3644 * could. Let's switch to the DATA state. *
3645 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003646 req->analyse_exp = TICK_ETERNITY;
3647 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003648
Willy Tarreau59234e92008-11-30 23:51:27 +01003649 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003650 /* OK let's go on with the BODY now */
3651 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003652
Willy Tarreau59234e92008-11-30 23:51:27 +01003653 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003654 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003655 /* we detected a parsing error. We want to archive this request
3656 * in the dedicated proxy area for later troubleshooting.
3657 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003658 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003659 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003660
Willy Tarreau59234e92008-11-30 23:51:27 +01003661 txn->req.msg_state = HTTP_MSG_ERROR;
3662 txn->status = 400;
3663 req->analysers = 0;
3664 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003665
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003666 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003667 if (s->listener->counters)
3668 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003669
Willy Tarreau59234e92008-11-30 23:51:27 +01003670 if (!(s->flags & SN_ERR_MASK))
3671 s->flags |= SN_ERR_PRXCOND;
3672 if (!(s->flags & SN_FINST_MASK))
3673 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003674 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003675}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003676
Willy Tarreau60b85b02008-11-30 23:28:40 +01003677/* This function is an analyser which processes the HTTP tarpit. It always
3678 * returns zero, at the beginning because it prevents any other processing
3679 * from occurring, and at the end because it terminates the request.
3680 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003681int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003682{
3683 struct http_txn *txn = &s->txn;
3684
3685 /* This connection is being tarpitted. The CLIENT side has
3686 * already set the connect expiration date to the right
3687 * timeout. We just have to check that the client is still
3688 * there and that the timeout has not expired.
3689 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003690 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003691 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3692 !tick_is_expired(req->analyse_exp, now_ms))
3693 return 0;
3694
3695 /* We will set the queue timer to the time spent, just for
3696 * logging purposes. We fake a 500 server error, so that the
3697 * attacker will not suspect his connection has been tarpitted.
3698 * It will not cause trouble to the logs because we can exclude
3699 * the tarpitted connections by filtering on the 'PT' status flags.
3700 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003701 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3702
3703 txn->status = 500;
3704 if (req->flags != BF_READ_ERROR)
3705 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3706
3707 req->analysers = 0;
3708 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003709
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003710 s->fe->counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003711 if (s->listener->counters)
3712 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003713
Willy Tarreau60b85b02008-11-30 23:28:40 +01003714 if (!(s->flags & SN_ERR_MASK))
3715 s->flags |= SN_ERR_PRXCOND;
3716 if (!(s->flags & SN_FINST_MASK))
3717 s->flags |= SN_FINST_T;
3718 return 0;
3719}
3720
Willy Tarreaud34af782008-11-30 23:36:37 +01003721/* This function is an analyser which processes the HTTP request body. It looks
3722 * for parameters to be used for the load balancing algorithm (url_param). It
3723 * must only be called after the standard HTTP request processing has occurred,
3724 * because it expects the request to be parsed. It returns zero if it needs to
3725 * read more data, or 1 once it has completed its analysis.
3726 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003727int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003728{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003729 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003730 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003731 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003732
3733 /* We have to parse the HTTP request body to find any required data.
3734 * "balance url_param check_post" should have been the only way to get
3735 * into this. We were brought here after HTTP header analysis, so all
3736 * related structures are ready.
3737 */
3738
Willy Tarreau522d6c02009-12-06 18:49:18 +01003739 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3740 goto missing_data;
3741
3742 if (msg->msg_state < HTTP_MSG_100_SENT) {
3743 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3744 * send an HTTP/1.1 100 Continue intermediate response.
3745 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003746 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003747 struct hdr_ctx ctx;
3748 ctx.idx = 0;
3749 /* Expect is allowed in 1.1, look for it */
3750 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3751 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3752 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3753 }
3754 }
3755 msg->msg_state = HTTP_MSG_100_SENT;
3756 }
3757
3758 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003759 /* we have msg->col and msg->sov which both point to the first
3760 * byte of message body. msg->som still points to the beginning
3761 * of the message. We must save the body in req->lr because it
3762 * survives buffer re-alignments.
3763 */
3764 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003765 if (txn->flags & TX_REQ_TE_CHNK)
3766 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3767 else
3768 msg->msg_state = HTTP_MSG_DATA;
3769 }
3770
3771 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau115acb92009-12-26 13:56:06 +01003772 /* read the chunk size and assign it to ->hdr_content_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003773 * set ->sov and ->lr to point to the body and switch to DATA or
3774 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003775 */
3776 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003777
Willy Tarreau115acb92009-12-26 13:56:06 +01003778 if (!ret)
3779 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003780 else if (ret < 0) {
3781 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003782 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003783 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003784 }
3785
Willy Tarreaud98cf932009-12-27 22:54:55 +01003786 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003787 * We have the first non-header byte in msg->col, which is either the
3788 * beginning of the chunk size or of the data. The first data byte is in
3789 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3790 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003791 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003792
3793 if (msg->hdr_content_len < limit)
3794 limit = msg->hdr_content_len;
3795
Willy Tarreau7c96f672009-12-27 22:47:25 +01003796 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003797 goto http_end;
3798
3799 missing_data:
3800 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003801 if (req->flags & BF_FULL) {
3802 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003803 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003804 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003805
Willy Tarreau522d6c02009-12-06 18:49:18 +01003806 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3807 txn->status = 408;
3808 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003809
3810 if (!(s->flags & SN_ERR_MASK))
3811 s->flags |= SN_ERR_CLITO;
3812 if (!(s->flags & SN_FINST_MASK))
3813 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003814 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003815 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003816
3817 /* we get here if we need to wait for more data */
3818 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003819 /* Not enough data. We'll re-use the http-request
3820 * timeout here. Ideally, we should set the timeout
3821 * relative to the accept() date. We just set the
3822 * request timeout once at the beginning of the
3823 * request.
3824 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003825 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003826 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003827 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003828 return 0;
3829 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003830
3831 http_end:
3832 /* The situation will not evolve, so let's give up on the analysis. */
3833 s->logs.tv_request = now; /* update the request timer to reflect full request */
3834 req->analysers &= ~an_bit;
3835 req->analyse_exp = TICK_ETERNITY;
3836 return 1;
3837
3838 return_bad_req: /* let's centralize all bad requests */
3839 txn->req.msg_state = HTTP_MSG_ERROR;
3840 txn->status = 400;
3841 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3842
Willy Tarreau79ebac62010-06-07 13:47:49 +02003843 if (!(s->flags & SN_ERR_MASK))
3844 s->flags |= SN_ERR_PRXCOND;
3845 if (!(s->flags & SN_FINST_MASK))
3846 s->flags |= SN_FINST_R;
3847
Willy Tarreau522d6c02009-12-06 18:49:18 +01003848 return_err_msg:
3849 req->analysers = 0;
3850 s->fe->counters.failed_req++;
3851 if (s->listener->counters)
3852 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003853 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003854}
3855
Willy Tarreau610ecce2010-01-04 21:15:02 +01003856/* Terminate current transaction and prepare a new one. This is very tricky
3857 * right now but it works.
3858 */
3859void http_end_txn_clean_session(struct session *s)
3860{
3861 /* FIXME: We need a more portable way of releasing a backend's and a
3862 * server's connections. We need a safer way to reinitialize buffer
3863 * flags. We also need a more accurate method for computing per-request
3864 * data.
3865 */
3866 http_silent_debug(__LINE__, s);
3867
3868 s->req->cons->flags |= SI_FL_NOLINGER;
3869 s->req->cons->shutr(s->req->cons);
3870 s->req->cons->shutw(s->req->cons);
3871
3872 http_silent_debug(__LINE__, s);
3873
3874 if (s->flags & SN_BE_ASSIGNED)
3875 s->be->beconn--;
3876
3877 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3878 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003879 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003880
3881 if (s->txn.status) {
3882 int n;
3883
3884 n = s->txn.status / 100;
3885 if (n < 1 || n > 5)
3886 n = 0;
3887
3888 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau24657792010-02-26 10:30:28 +01003889 s->fe->counters.fe.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003890
Willy Tarreau24657792010-02-26 10:30:28 +01003891 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003892 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau24657792010-02-26 10:30:28 +01003893 s->be->counters.be.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003894 }
3895
3896 /* don't count other requests' data */
3897 s->logs.bytes_in -= s->req->l - s->req->send_max;
3898 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3899
3900 /* let's do a final log if we need it */
3901 if (s->logs.logwait &&
3902 !(s->flags & SN_MONITOR) &&
3903 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3904 s->do_log(s);
3905 }
3906
3907 s->logs.accept_date = date; /* user-visible date for logging */
3908 s->logs.tv_accept = now; /* corrected date for internal use */
3909 tv_zero(&s->logs.tv_request);
3910 s->logs.t_queue = -1;
3911 s->logs.t_connect = -1;
3912 s->logs.t_data = -1;
3913 s->logs.t_close = 0;
3914 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3915 s->logs.srv_queue_size = 0; /* we will get this number soon */
3916
3917 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3918 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3919
3920 if (s->pend_pos)
3921 pendconn_free(s->pend_pos);
3922
3923 if (s->srv) {
3924 if (s->flags & SN_CURR_SESS) {
3925 s->flags &= ~SN_CURR_SESS;
3926 s->srv->cur_sess--;
3927 }
3928 if (may_dequeue_tasks(s->srv, s->be))
3929 process_srv_queue(s->srv);
3930 }
3931
3932 if (unlikely(s->srv_conn))
3933 sess_change_server(s, NULL);
3934 s->srv = NULL;
3935
3936 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3937 s->req->cons->fd = -1; /* just to help with debugging */
3938 s->req->cons->err_type = SI_ET_NONE;
3939 s->req->cons->err_loc = NULL;
3940 s->req->cons->exp = TICK_ETERNITY;
3941 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau90deb182010-01-07 00:20:41 +01003942 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST);
3943 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 +02003944 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 +01003945 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3946 s->txn.meth = 0;
3947 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003948 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003949 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003950 s->req->cons->flags |= SI_FL_INDEP_STR;
3951
3952 /* if the request buffer is not empty, it means we're
3953 * about to process another request, so send pending
3954 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003955 * Just don't do this if the buffer is close to be full,
3956 * because the request will wait for it to flush a little
3957 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003958 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003959 if (s->req->l > s->req->send_max) {
3960 if (s->rep->send_max &&
3961 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003962 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3963 s->rep->flags |= BF_EXPECT_MORE;
3964 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003965
3966 /* we're removing the analysers, we MUST re-enable events detection */
3967 buffer_auto_read(s->req);
3968 buffer_auto_close(s->req);
3969 buffer_auto_read(s->rep);
3970 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003971
3972 /* make ->lr point to the first non-forwarded byte */
3973 s->req->lr = s->req->w + s->req->send_max;
3974 if (s->req->lr >= s->req->data + s->req->size)
3975 s->req->lr -= s->req->size;
3976 s->rep->lr = s->rep->w + s->rep->send_max;
3977 if (s->rep->lr >= s->rep->data + s->rep->size)
3978 s->rep->lr -= s->req->size;
3979
Willy Tarreau342b11c2010-11-24 16:22:09 +01003980 s->req->analysers = s->listener->analysers;
3981 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003982 s->rep->analysers = 0;
3983
3984 http_silent_debug(__LINE__, s);
3985}
3986
3987
3988/* This function updates the request state machine according to the response
3989 * state machine and buffer flags. It returns 1 if it changes anything (flag
3990 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3991 * it is only used to find when a request/response couple is complete. Both
3992 * this function and its equivalent should loop until both return zero. It
3993 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3994 */
3995int http_sync_req_state(struct session *s)
3996{
3997 struct buffer *buf = s->req;
3998 struct http_txn *txn = &s->txn;
3999 unsigned int old_flags = buf->flags;
4000 unsigned int old_state = txn->req.msg_state;
4001
4002 http_silent_debug(__LINE__, s);
4003 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4004 return 0;
4005
4006 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004007 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004008 * We can shut the read side unless we want to abort_on_close,
4009 * or we have a POST request. The issue with POST requests is
4010 * that some browsers still send a CRLF after the request, and
4011 * this CRLF must be read so that it does not remain in the kernel
4012 * buffers, otherwise a close could cause an RST on some systems
4013 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004014 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004015 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01004016 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004017
4018 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4019 goto wait_other_side;
4020
4021 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4022 /* The server has not finished to respond, so we
4023 * don't want to move in order not to upset it.
4024 */
4025 goto wait_other_side;
4026 }
4027
4028 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4029 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004030 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004031 txn->req.msg_state = HTTP_MSG_TUNNEL;
4032 goto wait_other_side;
4033 }
4034
4035 /* When we get here, it means that both the request and the
4036 * response have finished receiving. Depending on the connection
4037 * mode, we'll have to wait for the last bytes to leave in either
4038 * direction, and sometimes for a close to be effective.
4039 */
4040
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004041 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4042 /* Server-close mode : queue a connection close to the server */
4043 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004044 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004045 }
4046 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4047 /* Option forceclose is set, or either side wants to close,
4048 * let's enforce it now that we're not expecting any new
4049 * data to come. The caller knows the session is complete
4050 * once both states are CLOSED.
4051 */
4052 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004053 buffer_shutr_now(buf);
4054 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004055 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004056 }
4057 else {
4058 /* The last possible modes are keep-alive and tunnel. Since tunnel
4059 * mode does not set the body analyser, we can't reach this place
4060 * in tunnel mode, so we're left with keep-alive only.
4061 * This mode is currently not implemented, we switch to tunnel mode.
4062 */
4063 buffer_auto_read(buf);
4064 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004065 }
4066
4067 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4068 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004069 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4070
Willy Tarreau610ecce2010-01-04 21:15:02 +01004071 if (!(buf->flags & BF_OUT_EMPTY)) {
4072 txn->req.msg_state = HTTP_MSG_CLOSING;
4073 goto http_msg_closing;
4074 }
4075 else {
4076 txn->req.msg_state = HTTP_MSG_CLOSED;
4077 goto http_msg_closed;
4078 }
4079 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004080 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004081 }
4082
4083 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4084 http_msg_closing:
4085 /* nothing else to forward, just waiting for the output buffer
4086 * to be empty and for the shutw_now to take effect.
4087 */
4088 if (buf->flags & BF_OUT_EMPTY) {
4089 txn->req.msg_state = HTTP_MSG_CLOSED;
4090 goto http_msg_closed;
4091 }
4092 else if (buf->flags & BF_SHUTW) {
4093 txn->req.msg_state = HTTP_MSG_ERROR;
4094 goto wait_other_side;
4095 }
4096 }
4097
4098 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4099 http_msg_closed:
4100 goto wait_other_side;
4101 }
4102
4103 wait_other_side:
4104 http_silent_debug(__LINE__, s);
4105 return txn->req.msg_state != old_state || buf->flags != old_flags;
4106}
4107
4108
4109/* This function updates the response state machine according to the request
4110 * state machine and buffer flags. It returns 1 if it changes anything (flag
4111 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4112 * it is only used to find when a request/response couple is complete. Both
4113 * this function and its equivalent should loop until both return zero. It
4114 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4115 */
4116int http_sync_res_state(struct session *s)
4117{
4118 struct buffer *buf = s->rep;
4119 struct http_txn *txn = &s->txn;
4120 unsigned int old_flags = buf->flags;
4121 unsigned int old_state = txn->rsp.msg_state;
4122
4123 http_silent_debug(__LINE__, s);
4124 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4125 return 0;
4126
4127 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4128 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004129 * still monitor the server connection for a possible close
4130 * while the request is being uploaded, so we don't disable
4131 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004132 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004133 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004134
4135 if (txn->req.msg_state == HTTP_MSG_ERROR)
4136 goto wait_other_side;
4137
4138 if (txn->req.msg_state < HTTP_MSG_DONE) {
4139 /* The client seems to still be sending data, probably
4140 * because we got an error response during an upload.
4141 * We have the choice of either breaking the connection
4142 * or letting it pass through. Let's do the later.
4143 */
4144 goto wait_other_side;
4145 }
4146
4147 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4148 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004149 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004150 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4151 goto wait_other_side;
4152 }
4153
4154 /* When we get here, it means that both the request and the
4155 * response have finished receiving. Depending on the connection
4156 * mode, we'll have to wait for the last bytes to leave in either
4157 * direction, and sometimes for a close to be effective.
4158 */
4159
4160 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4161 /* Server-close mode : shut read and wait for the request
4162 * side to close its output buffer. The caller will detect
4163 * when we're in DONE and the other is in CLOSED and will
4164 * catch that for the final cleanup.
4165 */
4166 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4167 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004168 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004169 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4170 /* Option forceclose is set, or either side wants to close,
4171 * let's enforce it now that we're not expecting any new
4172 * data to come. The caller knows the session is complete
4173 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004174 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004175 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4176 buffer_shutr_now(buf);
4177 buffer_shutw_now(buf);
4178 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004179 }
4180 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004181 /* The last possible modes are keep-alive and tunnel. Since tunnel
4182 * mode does not set the body analyser, we can't reach this place
4183 * in tunnel mode, so we're left with keep-alive only.
4184 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004185 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004186 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004187 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004188 }
4189
4190 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4191 /* if we've just closed an output, let's switch */
4192 if (!(buf->flags & BF_OUT_EMPTY)) {
4193 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4194 goto http_msg_closing;
4195 }
4196 else {
4197 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4198 goto http_msg_closed;
4199 }
4200 }
4201 goto wait_other_side;
4202 }
4203
4204 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4205 http_msg_closing:
4206 /* nothing else to forward, just waiting for the output buffer
4207 * to be empty and for the shutw_now to take effect.
4208 */
4209 if (buf->flags & BF_OUT_EMPTY) {
4210 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4211 goto http_msg_closed;
4212 }
4213 else if (buf->flags & BF_SHUTW) {
4214 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreauae526782010-03-04 20:34:23 +01004215 s->be->counters.cli_aborts++;
4216 if (s->srv)
4217 s->srv->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004218 goto wait_other_side;
4219 }
4220 }
4221
4222 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4223 http_msg_closed:
4224 /* drop any pending data */
4225 buffer_ignore(buf, buf->l - buf->send_max);
4226 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004227 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004228 goto wait_other_side;
4229 }
4230
4231 wait_other_side:
4232 http_silent_debug(__LINE__, s);
4233 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4234}
4235
4236
4237/* Resync the request and response state machines. Return 1 if either state
4238 * changes.
4239 */
4240int http_resync_states(struct session *s)
4241{
4242 struct http_txn *txn = &s->txn;
4243 int old_req_state = txn->req.msg_state;
4244 int old_res_state = txn->rsp.msg_state;
4245
4246 http_silent_debug(__LINE__, s);
4247 http_sync_req_state(s);
4248 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004249 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004250 if (!http_sync_res_state(s))
4251 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004252 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004253 if (!http_sync_req_state(s))
4254 break;
4255 }
4256 http_silent_debug(__LINE__, s);
4257 /* OK, both state machines agree on a compatible state.
4258 * There are a few cases we're interested in :
4259 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4260 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4261 * directions, so let's simply disable both analysers.
4262 * - HTTP_MSG_CLOSED on the response only means we must abort the
4263 * request.
4264 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4265 * with server-close mode means we've completed one request and we
4266 * must re-initialize the server connection.
4267 */
4268
4269 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4270 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4271 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4272 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4273 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004274 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004275 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004276 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004277 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004278 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004279 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004280 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4281 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004282 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004283 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004284 s->rep->analysers = 0;
4285 buffer_auto_close(s->rep);
4286 buffer_auto_read(s->rep);
4287 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004288 buffer_abort(s->req);
4289 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004290 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004291 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004292 }
4293 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4294 txn->rsp.msg_state == HTTP_MSG_DONE &&
4295 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4296 /* server-close: terminate this server connection and
4297 * reinitialize a fresh-new transaction.
4298 */
4299 http_end_txn_clean_session(s);
4300 }
4301
4302 http_silent_debug(__LINE__, s);
4303 return txn->req.msg_state != old_req_state ||
4304 txn->rsp.msg_state != old_res_state;
4305}
4306
Willy Tarreaud98cf932009-12-27 22:54:55 +01004307/* This function is an analyser which forwards request body (including chunk
4308 * sizes if any). It is called as soon as we must forward, even if we forward
4309 * zero byte. The only situation where it must not be called is when we're in
4310 * tunnel mode and we want to forward till the close. It's used both to forward
4311 * remaining data and to resync after end of body. It expects the msg_state to
4312 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4313 * read more data, or 1 once we can go on with next request or end the session.
4314 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
4315 * bytes of pending data + the headers if not already done (between som and sov).
4316 * It eventually adjusts som to match sov after the data in between have been sent.
4317 */
4318int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4319{
4320 struct http_txn *txn = &s->txn;
4321 struct http_msg *msg = &s->txn.req;
4322
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004323 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4324 return 0;
4325
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004326 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4327 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004328 /* Output closed while we were sending data. We must abort and
4329 * wake the other side up.
4330 */
4331 msg->msg_state = HTTP_MSG_ERROR;
4332 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004333 return 1;
4334 }
4335
Willy Tarreau4fe41902010-06-07 22:27:41 +02004336 /* in most states, we should abort in case of early close */
4337 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004338
4339 /* Note that we don't have to send 100-continue back because we don't
4340 * need the data to complete our job, and it's up to the server to
4341 * decide whether to return 100, 417 or anything else in return of
4342 * an "Expect: 100-continue" header.
4343 */
4344
4345 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4346 /* we have msg->col and msg->sov which both point to the first
4347 * byte of message body. msg->som still points to the beginning
4348 * of the message. We must save the body in req->lr because it
4349 * survives buffer re-alignments.
4350 */
4351 req->lr = req->data + msg->sov;
4352 if (txn->flags & TX_REQ_TE_CHNK)
4353 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4354 else {
4355 msg->msg_state = HTTP_MSG_DATA;
4356 }
4357 }
4358
Willy Tarreaud98cf932009-12-27 22:54:55 +01004359 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004360 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004361 /* we may have some data pending */
4362 if (msg->hdr_content_len || msg->som != msg->sov) {
4363 int bytes = msg->sov - msg->som;
4364 if (bytes < 0) /* sov may have wrapped at the end */
4365 bytes += req->size;
4366 buffer_forward(req, bytes + msg->hdr_content_len);
4367 msg->hdr_content_len = 0; /* don't forward that again */
4368 msg->som = msg->sov;
4369 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004370
Willy Tarreaucaabe412010-01-03 23:08:28 +01004371 if (msg->msg_state == HTTP_MSG_DATA) {
4372 /* must still forward */
4373 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004374 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004375
4376 /* nothing left to forward */
4377 if (txn->flags & TX_REQ_TE_CHNK)
4378 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004379 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004380 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004381 }
4382 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004383 /* read the chunk size and assign it to ->hdr_content_len, then
4384 * set ->sov and ->lr to point to the body and switch to DATA or
4385 * TRAILERS state.
4386 */
4387 int ret = http_parse_chunk_size(req, msg);
4388
4389 if (!ret)
4390 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004391 else if (ret < 0) {
4392 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004393 if (msg->err_pos >= 0)
4394 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004395 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004396 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004397 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreau57f5c122010-12-02 00:37:14 +01004398 /* Don't set a PUSH at the end of that chunk if it's not the last one */
4399 if (msg->msg_state == HTTP_MSG_DATA)
4400 req->flags |= BF_EXPECT_MORE;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004401 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004402 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4403 /* we want the CRLF after the data */
4404 int ret;
4405
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004406 req->lr = req->w + req->send_max;
4407 if (req->lr >= req->data + req->size)
4408 req->lr -= req->size;
4409
Willy Tarreaud98cf932009-12-27 22:54:55 +01004410 ret = http_skip_chunk_crlf(req, msg);
4411
4412 if (ret == 0)
4413 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004414 else if (ret < 0) {
4415 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004416 if (msg->err_pos >= 0)
4417 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004418 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004419 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004420 /* we're in MSG_CHUNK_SIZE now */
4421 }
4422 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4423 int ret = http_forward_trailers(req, msg);
4424
4425 if (ret == 0)
4426 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004427 else if (ret < 0) {
4428 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004429 if (msg->err_pos >= 0)
4430 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004431 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004432 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004433 /* we're in HTTP_MSG_DONE now */
4434 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004435 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004436 int old_state = msg->msg_state;
4437
Willy Tarreau610ecce2010-01-04 21:15:02 +01004438 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004439 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004440 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4441 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4442 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004443 if (http_resync_states(s)) {
4444 /* some state changes occurred, maybe the analyser
4445 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004446 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004447 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4448 if (req->flags & BF_SHUTW) {
4449 /* request errors are most likely due to
4450 * the server aborting the transfer.
4451 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004452 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004453 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004454 if (msg->err_pos >= 0)
4455 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004456 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004457 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004458 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004459 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004460
4461 /* If "option abortonclose" is set on the backend, we
4462 * want to monitor the client's connection and forward
4463 * any shutdown notification to the server, which will
4464 * decide whether to close or to go on processing the
4465 * request.
4466 */
4467 if (s->be->options & PR_O_ABRT_CLOSE) {
4468 buffer_auto_read(req);
4469 buffer_auto_close(req);
4470 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004471 else if (s->txn.meth == HTTP_METH_POST) {
4472 /* POST requests may require to read extra CRLF
4473 * sent by broken browsers and which could cause
4474 * an RST to be sent upon close on some systems
4475 * (eg: Linux).
4476 */
4477 buffer_auto_read(req);
4478 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004479
Willy Tarreau610ecce2010-01-04 21:15:02 +01004480 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004481 }
4482 }
4483
Willy Tarreaud98cf932009-12-27 22:54:55 +01004484 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004485 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004486 if (req->flags & BF_SHUTR) {
4487 if (!(s->flags & SN_ERR_MASK))
4488 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004489 if (!(s->flags & SN_FINST_MASK)) {
4490 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4491 s->flags |= SN_FINST_H;
4492 else
4493 s->flags |= SN_FINST_D;
4494 }
4495
4496 s->fe->counters.cli_aborts++;
4497 if (s->fe != s->be)
4498 s->be->counters.cli_aborts++;
4499 if (s->srv)
4500 s->srv->counters.cli_aborts++;
4501
4502 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004503 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004504
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004505 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004506 if (req->flags & BF_SHUTW)
4507 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004508
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004509 /* When TE: chunked is used, we need to get there again to parse remaining
4510 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4511 */
4512 if (txn->flags & TX_REQ_TE_CHNK)
4513 buffer_dont_close(req);
4514
Willy Tarreau610ecce2010-01-04 21:15:02 +01004515 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004516 return 0;
4517
Willy Tarreaud98cf932009-12-27 22:54:55 +01004518 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004519 s->fe->counters.failed_req++;
4520 if (s->listener->counters)
4521 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004522 return_bad_req_stats_ok:
4523 txn->req.msg_state = HTTP_MSG_ERROR;
4524 if (txn->status) {
4525 /* Note: we don't send any error if some data were already sent */
4526 stream_int_retnclose(req->prod, NULL);
4527 } else {
4528 txn->status = 400;
4529 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4530 }
4531 req->analysers = 0;
4532 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004533
4534 if (!(s->flags & SN_ERR_MASK))
4535 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004536 if (!(s->flags & SN_FINST_MASK)) {
4537 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4538 s->flags |= SN_FINST_H;
4539 else
4540 s->flags |= SN_FINST_D;
4541 }
4542 return 0;
4543
4544 aborted_xfer:
4545 txn->req.msg_state = HTTP_MSG_ERROR;
4546 if (txn->status) {
4547 /* Note: we don't send any error if some data were already sent */
4548 stream_int_retnclose(req->prod, NULL);
4549 } else {
4550 txn->status = 502;
4551 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4552 }
4553 req->analysers = 0;
4554 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4555
4556 s->fe->counters.srv_aborts++;
4557 if (s->fe != s->be)
4558 s->be->counters.srv_aborts++;
4559 if (s->srv)
4560 s->srv->counters.srv_aborts++;
4561
4562 if (!(s->flags & SN_ERR_MASK))
4563 s->flags |= SN_ERR_SRVCL;
4564 if (!(s->flags & SN_FINST_MASK)) {
4565 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4566 s->flags |= SN_FINST_H;
4567 else
4568 s->flags |= SN_FINST_D;
4569 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004570 return 0;
4571}
4572
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004573/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4574 * processing can continue on next analysers, or zero if it either needs more
4575 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4576 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4577 * when it has nothing left to do, and may remove any analyser when it wants to
4578 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004579 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004580int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004581{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004582 struct http_txn *txn = &s->txn;
4583 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004584 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004585 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004586 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004587 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004588
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004589 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 +02004590 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004591 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004592 rep,
4593 rep->rex, rep->wex,
4594 rep->flags,
4595 rep->l,
4596 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004597
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004598 /*
4599 * Now parse the partial (or complete) lines.
4600 * We will check the response syntax, and also join multi-line
4601 * headers. An index of all the lines will be elaborated while
4602 * parsing.
4603 *
4604 * For the parsing, we use a 28 states FSM.
4605 *
4606 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004607 * rep->data + msg->som = beginning of response
4608 * rep->data + msg->eoh = end of processed headers / start of current one
4609 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004610 * rep->lr = first non-visited byte
4611 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004612 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004613 */
4614
Willy Tarreau83e3af02009-12-28 17:39:57 +01004615 /* There's a protected area at the end of the buffer for rewriting
4616 * purposes. We don't want to start to parse the request if the
4617 * protected area is affected, because we may have to move processed
4618 * data later, which is much more complicated.
4619 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004620 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4621 if (unlikely((rep->flags & BF_FULL) ||
4622 rep->r < rep->lr ||
4623 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4624 if (rep->send_max) {
4625 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004626 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4627 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004628 buffer_dont_close(rep);
4629 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4630 return 0;
4631 }
4632 if (rep->l <= rep->size - global.tune.maxrewrite)
4633 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004634 }
4635
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004636 if (likely(rep->lr < rep->r))
4637 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004638 }
4639
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004640 /* 1: we might have to print this header in debug mode */
4641 if (unlikely((global.mode & MODE_DEBUG) &&
4642 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004643 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004644 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004645 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004646
Willy Tarreau663308b2010-06-07 14:06:08 +02004647 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004648 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004649 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004650
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004651 sol += hdr_idx_first_pos(&txn->hdr_idx);
4652 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004653
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004654 while (cur_idx) {
4655 eol = sol + txn->hdr_idx.v[cur_idx].len;
4656 debug_hdr("srvhdr", s, sol, eol);
4657 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4658 cur_idx = txn->hdr_idx.v[cur_idx].next;
4659 }
4660 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004661
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004662 /*
4663 * Now we quickly check if we have found a full valid response.
4664 * If not so, we check the FD and buffer states before leaving.
4665 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004666 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004667 * responses are checked first.
4668 *
4669 * Depending on whether the client is still there or not, we
4670 * may send an error response back or not. Note that normally
4671 * we should only check for HTTP status there, and check I/O
4672 * errors somewhere else.
4673 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004674
Willy Tarreau655dce92009-11-08 13:10:58 +01004675 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004676 /* Invalid response */
4677 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4678 /* we detected a parsing error. We want to archive this response
4679 * in the dedicated proxy area for later troubleshooting.
4680 */
4681 hdr_response_bad:
4682 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004683 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004684
4685 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004686 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004687 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004688 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
4689 }
Willy Tarreau64648412010-03-05 10:41:54 +01004690 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004691 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004692 rep->analysers = 0;
4693 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004694 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004695 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004696 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4697
4698 if (!(s->flags & SN_ERR_MASK))
4699 s->flags |= SN_ERR_PRXCOND;
4700 if (!(s->flags & SN_FINST_MASK))
4701 s->flags |= SN_FINST_H;
4702
4703 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004704 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004705
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004706 /* too large response does not fit in buffer. */
4707 else if (rep->flags & BF_FULL) {
4708 goto hdr_response_bad;
4709 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004710
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004711 /* read error */
4712 else if (rep->flags & BF_READ_ERROR) {
4713 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004714 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004715
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004716 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004717 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004718 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004719 health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR);
4720 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004721
Willy Tarreau90deb182010-01-07 00:20:41 +01004722 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004723 rep->analysers = 0;
4724 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004725 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004726 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004727 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004728
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004729 if (!(s->flags & SN_ERR_MASK))
4730 s->flags |= SN_ERR_SRVCL;
4731 if (!(s->flags & SN_FINST_MASK))
4732 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004733 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004734 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004735
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004736 /* read timeout : return a 504 to the client. */
4737 else if (rep->flags & BF_READ_TIMEOUT) {
4738 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004739 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004740
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004741 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004742 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004743 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004744 health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT);
4745 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004746
Willy Tarreau90deb182010-01-07 00:20:41 +01004747 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004748 rep->analysers = 0;
4749 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004750 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004751 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004752 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004753
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004754 if (!(s->flags & SN_ERR_MASK))
4755 s->flags |= SN_ERR_SRVTO;
4756 if (!(s->flags & SN_FINST_MASK))
4757 s->flags |= SN_FINST_H;
4758 return 0;
4759 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004760
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004761 /* close from server */
4762 else if (rep->flags & BF_SHUTR) {
4763 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004764 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004765
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004766 s->be->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004767 if (s->srv) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004768 s->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004769 health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE);
4770 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004771
Willy Tarreau90deb182010-01-07 00:20:41 +01004772 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004773 rep->analysers = 0;
4774 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004775 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004776 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004777 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004778
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004779 if (!(s->flags & SN_ERR_MASK))
4780 s->flags |= SN_ERR_SRVCL;
4781 if (!(s->flags & SN_FINST_MASK))
4782 s->flags |= SN_FINST_H;
4783 return 0;
4784 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004785
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004786 /* write error to client (we don't send any message then) */
4787 else if (rep->flags & BF_WRITE_ERROR) {
4788 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004789 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004790
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004791 s->be->counters.failed_resp++;
4792 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004793 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004794
4795 if (!(s->flags & SN_ERR_MASK))
4796 s->flags |= SN_ERR_CLICL;
4797 if (!(s->flags & SN_FINST_MASK))
4798 s->flags |= SN_FINST_H;
4799
4800 /* process_session() will take care of the error */
4801 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004802 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004803
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004804 buffer_dont_close(rep);
4805 return 0;
4806 }
4807
4808 /* More interesting part now : we know that we have a complete
4809 * response which at least looks like HTTP. We have an indicator
4810 * of each header's length, so we can parse them quickly.
4811 */
4812
4813 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004814 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004815
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004816 /*
4817 * 1: get the status code
4818 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004819 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004820 if (n < 1 || n > 5)
4821 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004822 /* when the client triggers a 4xx from the server, it's most often due
4823 * to a missing object or permission. These events should be tracked
4824 * because if they happen often, it may indicate a brute force or a
4825 * vulnerability scan.
4826 */
4827 if (n == 4)
4828 session_inc_http_err_ctr(s);
4829
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004830 if (s->srv)
4831 s->srv->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004832
Willy Tarreau5b154472009-12-21 20:11:07 +01004833 /* check if the response is HTTP/1.1 or above */
4834 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004835 ((msg->sol[5] > '1') ||
4836 ((msg->sol[5] == '1') &&
4837 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004838 txn->flags |= TX_RES_VER_11;
4839
4840 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004841 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 +01004842
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004843 /* transfer length unknown*/
4844 txn->flags &= ~TX_RES_XFER_LEN;
4845
Willy Tarreau962c3f42010-01-10 00:15:35 +01004846 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004847
Willy Tarreau39650402010-03-15 19:44:39 +01004848 /* Adjust server's health based on status code. Note: status codes 501
4849 * and 505 are triggered on demand by client request, so we must not
4850 * count them as server failures.
4851 */
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004852 if (s->srv) {
4853 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
4854 health_adjust(s->srv, HANA_STATUS_HTTP_OK);
4855 else
4856 health_adjust(s->srv, HANA_STATUS_HTTP_STS);
4857 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004858
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004859 /*
4860 * 2: check for cacheability.
4861 */
4862
4863 switch (txn->status) {
4864 case 200:
4865 case 203:
4866 case 206:
4867 case 300:
4868 case 301:
4869 case 410:
4870 /* RFC2616 @13.4:
4871 * "A response received with a status code of
4872 * 200, 203, 206, 300, 301 or 410 MAY be stored
4873 * by a cache (...) unless a cache-control
4874 * directive prohibits caching."
4875 *
4876 * RFC2616 @9.5: POST method :
4877 * "Responses to this method are not cacheable,
4878 * unless the response includes appropriate
4879 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004880 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004881 if (likely(txn->meth != HTTP_METH_POST) &&
4882 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4883 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4884 break;
4885 default:
4886 break;
4887 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004888
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004889 /*
4890 * 3: we may need to capture headers
4891 */
4892 s->logs.logwait &= ~LW_RESP;
4893 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004894 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004895 txn->rsp.cap, s->fe->rsp_cap);
4896
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004897 /* 4: determine the transfer-length.
4898 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4899 * the presence of a message-body in a RESPONSE and its transfer length
4900 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004901 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004902 * All responses to the HEAD request method MUST NOT include a
4903 * message-body, even though the presence of entity-header fields
4904 * might lead one to believe they do. All 1xx (informational), 204
4905 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4906 * message-body. All other responses do include a message-body,
4907 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004908 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004909 * 1. Any response which "MUST NOT" include a message-body (such as the
4910 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4911 * always terminated by the first empty line after the header fields,
4912 * regardless of the entity-header fields present in the message.
4913 *
4914 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4915 * the "chunked" transfer-coding (Section 6.2) is used, the
4916 * transfer-length is defined by the use of this transfer-coding.
4917 * If a Transfer-Encoding header field is present and the "chunked"
4918 * transfer-coding is not present, the transfer-length is defined by
4919 * the sender closing the connection.
4920 *
4921 * 3. If a Content-Length header field is present, its decimal value in
4922 * OCTETs represents both the entity-length and the transfer-length.
4923 * If a message is received with both a Transfer-Encoding header
4924 * field and a Content-Length header field, the latter MUST be ignored.
4925 *
4926 * 4. If the message uses the media type "multipart/byteranges", and
4927 * the transfer-length is not otherwise specified, then this self-
4928 * delimiting media type defines the transfer-length. This media
4929 * type MUST NOT be used unless the sender knows that the recipient
4930 * can parse it; the presence in a request of a Range header with
4931 * multiple byte-range specifiers from a 1.1 client implies that the
4932 * client can parse multipart/byteranges responses.
4933 *
4934 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004935 */
4936
4937 /* Skip parsing if no content length is possible. The response flags
4938 * remain 0 as well as the hdr_content_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004939 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004940 * FIXME: should we parse anyway and return an error on chunked encoding ?
4941 */
4942 if (txn->meth == HTTP_METH_HEAD ||
4943 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004944 txn->status == 204 || txn->status == 304) {
4945 txn->flags |= TX_RES_XFER_LEN;
4946 goto skip_content_length;
4947 }
4948
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004949 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004950 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004951 while ((txn->flags & TX_RES_VER_11) &&
4952 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004953 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4954 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4955 else if (txn->flags & TX_RES_TE_CHNK) {
4956 /* bad transfer-encoding (chunked followed by something else) */
4957 use_close_only = 1;
4958 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4959 break;
4960 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004961 }
4962
4963 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4964 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004965 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004966 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4967 signed long long cl;
4968
4969 if (!ctx.vlen)
4970 goto hdr_response_bad;
4971
4972 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl))
4973 goto hdr_response_bad; /* parse failure */
4974
4975 if (cl < 0)
4976 goto hdr_response_bad;
4977
4978 if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl))
4979 goto hdr_response_bad; /* already specified, was different */
4980
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004981 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004982 msg->hdr_content_len = cl;
4983 }
4984
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004985 /* FIXME: we should also implement the multipart/byterange method.
4986 * For now on, we resort to close mode in this case (unknown length).
4987 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004988skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004989
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004990 /* end of job, return OK */
4991 rep->analysers &= ~an_bit;
4992 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004993 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004994 return 1;
4995}
4996
4997/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004998 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4999 * and updates t->rep->analysers. It might make sense to explode it into several
5000 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005001 */
5002int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
5003{
5004 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005005 struct http_msg *msg = &txn->rsp;
5006 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005007 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005008
5009 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
5010 now_ms, __FUNCTION__,
5011 t,
5012 rep,
5013 rep->rex, rep->wex,
5014 rep->flags,
5015 rep->l,
5016 rep->analysers);
5017
Willy Tarreau655dce92009-11-08 13:10:58 +01005018 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005019 return 0;
5020
5021 rep->analysers &= ~an_bit;
5022 rep->analyse_exp = TICK_ETERNITY;
5023
Willy Tarreau5b154472009-12-21 20:11:07 +01005024 /* Now we have to check if we need to modify the Connection header.
5025 * This is more difficult on the response than it is on the request,
5026 * because we can have two different HTTP versions and we don't know
5027 * how the client will interprete a response. For instance, let's say
5028 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5029 * HTTP/1.1 response without any header. Maybe it will bound itself to
5030 * HTTP/1.0 because it only knows about it, and will consider the lack
5031 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5032 * the lack of header as a keep-alive. Thus we will use two flags
5033 * indicating how a request MAY be understood by the client. In case
5034 * of multiple possibilities, we'll fix the header to be explicit. If
5035 * ambiguous cases such as both close and keepalive are seen, then we
5036 * will fall back to explicit close. Note that we won't take risks with
5037 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005038 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005039 */
5040
Willy Tarreaudc008c52010-02-01 16:20:08 +01005041 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5042 txn->status == 101)) {
5043 /* Either we've established an explicit tunnel, or we're
5044 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005045 * to understand the next protocols. We have to switch to tunnel
5046 * mode, so that we transfer the request and responses then let
5047 * this protocol pass unmodified. When we later implement specific
5048 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005049 * header which contains information about that protocol for
5050 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005051 */
5052 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5053 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005054 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5055 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5056 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005057 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005058
Willy Tarreau60466522010-01-18 19:08:45 +01005059 /* on unknown transfer length, we must close */
5060 if (!(txn->flags & TX_RES_XFER_LEN) &&
5061 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5062 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005063
Willy Tarreau60466522010-01-18 19:08:45 +01005064 /* now adjust header transformations depending on current state */
5065 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5066 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5067 to_del |= 2; /* remove "keep-alive" on any response */
5068 if (!(txn->flags & TX_RES_VER_11))
5069 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005070 }
Willy Tarreau60466522010-01-18 19:08:45 +01005071 else { /* SCL / KAL */
5072 to_del |= 1; /* remove "close" on any response */
5073 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
5074 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005075 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005076
Willy Tarreau60466522010-01-18 19:08:45 +01005077 /* Parse and remove some headers from the connection header */
5078 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005079
Willy Tarreau60466522010-01-18 19:08:45 +01005080 /* Some keep-alive responses are converted to Server-close if
5081 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005082 */
Willy Tarreau60466522010-01-18 19:08:45 +01005083 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5084 if ((txn->flags & TX_HDR_CONN_CLO) ||
5085 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
5086 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005087 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005088 }
5089
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005090 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005091 /*
5092 * 3: we will have to evaluate the filters.
5093 * As opposed to version 1.2, now they will be evaluated in the
5094 * filters order and not in the header order. This means that
5095 * each filter has to be validated among all headers.
5096 *
5097 * Filters are tried with ->be first, then with ->fe if it is
5098 * different from ->be.
5099 */
5100
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005101 cur_proxy = t->be;
5102 while (1) {
5103 struct proxy *rule_set = cur_proxy;
5104
5105 /* try headers filters */
5106 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005107 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005108 return_bad_resp:
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005109 if (t->srv) {
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005110 t->srv->counters.failed_resp++;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005111 health_adjust(t->srv, HANA_STATUS_HTTP_RSP);
5112 }
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005113 cur_proxy->counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005114 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005115 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005116 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005117 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01005118 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005119 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005120 if (!(t->flags & SN_ERR_MASK))
5121 t->flags |= SN_ERR_PRXCOND;
5122 if (!(t->flags & SN_FINST_MASK))
5123 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005124 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005125 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005126 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005127
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005128 /* has the response been denied ? */
5129 if (txn->flags & TX_SVDENY) {
Willy Tarreau8365f932009-03-15 23:11:49 +01005130 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005131 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005132
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005133 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005134 if (t->listener->counters)
5135 t->listener->counters->denied_resp++;
5136
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005137 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005138 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005139
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005140 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005141 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005142 if (txn->status < 200)
5143 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005144 if (wl->cond) {
5145 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5146 ret = acl_pass(ret);
5147 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5148 ret = !ret;
5149 if (!ret)
5150 continue;
5151 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005152 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005153 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005154 }
5155
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005156 /* check whether we're already working on the frontend */
5157 if (cur_proxy == t->fe)
5158 break;
5159 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005160 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005161
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005162 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005163 * We may be facing a 100-continue response, in which case this
5164 * is not the right response, and we're waiting for the next one.
5165 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005166 * next one.
5167 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005168 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005169 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005170 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005171 msg->msg_state = HTTP_MSG_RPBEFORE;
5172 txn->status = 0;
5173 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5174 return 1;
5175 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005176 else if (unlikely(txn->status < 200))
5177 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005178
5179 /* we don't have any 1xx status code now */
5180
5181 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005182 * 4: check for server cookie.
5183 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005184 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5185 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005186 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005187
Willy Tarreaubaaee002006-06-26 02:48:02 +02005188
Willy Tarreaua15645d2007-03-18 16:22:39 +01005189 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005190 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005191 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005192 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005193 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005194
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005195 /*
5196 * 6: add server cookie in the response if needed
5197 */
Willy Tarreauef4f3912010-10-07 21:00:29 +02005198 if ((t->srv) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005199 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005200 (!(t->flags & SN_DIRECT) ||
5201 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5202 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5203 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5204 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005205 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5206 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005207 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005208 /* the server is known, it's not the one the client requested, or the
5209 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005210 * insert a set-cookie here, except if we want to insert only on POST
5211 * requests and this one isn't. Note that servers which don't have cookies
5212 * (eg: some backup servers) will return a full cookie removal request.
5213 */
Willy Tarreauef4f3912010-10-07 21:00:29 +02005214 if (!t->srv->cookie) {
5215 len = sprintf(trash,
5216 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5217 t->be->cookie_name);
5218 }
5219 else {
5220 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, t->srv->cookie);
5221
5222 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5223 /* emit last_date, which is mandatory */
5224 trash[len++] = COOKIE_DELIM_DATE;
5225 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5226 if (t->be->cookie_maxlife) {
5227 /* emit first_date, which is either the original one or
5228 * the current date.
5229 */
5230 trash[len++] = COOKIE_DELIM_DATE;
5231 s30tob64(txn->cookie_first_date ?
5232 txn->cookie_first_date >> 2 :
5233 (date.tv_sec+3) >> 2, trash + len);
5234 len += 5;
5235 }
5236 }
5237 len += sprintf(trash + len, "; path=/");
5238 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005239
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005240 if (t->be->cookie_domain)
5241 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005242
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005243 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005244 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005245 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005246
Willy Tarreauf1348312010-10-07 15:54:11 +02005247 txn->flags &= ~TX_SCK_MASK;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005248 if (t->srv->cookie && (t->flags & SN_DIRECT))
5249 /* the server did not change, only the date was updated */
5250 txn->flags |= TX_SCK_UPDATED;
5251 else
5252 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005253
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005254 /* Here, we will tell an eventual cache on the client side that we don't
5255 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5256 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5257 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5258 */
5259 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005260
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005261 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5262
5263 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005264 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005265 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005266 }
5267 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005268
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005269 /*
5270 * 7: check if result will be cacheable with a cookie.
5271 * We'll block the response if security checks have caught
5272 * nasty things such as a cacheable cookie.
5273 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005274 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5275 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005276 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005277
5278 /* we're in presence of a cacheable response containing
5279 * a set-cookie header. We'll block it as requested by
5280 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005281 */
Willy Tarreau8365f932009-03-15 23:11:49 +01005282 if (t->srv)
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005283 t->srv->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005284
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005285 cur_proxy->counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005286 if (t->listener->counters)
5287 t->listener->counters->denied_resp++;
5288
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005289 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5290 t->be->id, t->srv?t->srv->id:"<dispatch>");
5291 send_log(t->be, LOG_ALERT,
5292 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5293 t->be->id, t->srv?t->srv->id:"<dispatch>");
5294 goto return_srv_prx_502;
5295 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005296
5297 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005298 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005299 */
Willy Tarreau60466522010-01-18 19:08:45 +01005300 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5301 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5302 unsigned int want_flags = 0;
5303
5304 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5305 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5306 /* we want a keep-alive response here. Keep-alive header
5307 * required if either side is not 1.1.
5308 */
5309 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5310 want_flags |= TX_CON_KAL_SET;
5311 }
5312 else {
5313 /* we want a close response here. Close header required if
5314 * the server is 1.1, regardless of the client.
5315 */
5316 if (txn->flags & TX_RES_VER_11)
5317 want_flags |= TX_CON_CLO_SET;
5318 }
5319
5320 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5321 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005322 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005323
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005324 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005325 if ((txn->flags & TX_RES_XFER_LEN) ||
5326 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005327 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005328
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005329 /*************************************************************
5330 * OK, that's finished for the headers. We have done what we *
5331 * could. Let's switch to the DATA state. *
5332 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005333
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005334 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005335
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005336 /* if the user wants to log as soon as possible, without counting
5337 * bytes from the server, then this is the right moment. We have
5338 * to temporarily assign bytes_out to log what we currently have.
5339 */
5340 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5341 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5342 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005343 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005344 t->logs.bytes_out = 0;
5345 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005346
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005347 /* Note: we must not try to cheat by jumping directly to DATA,
5348 * otherwise we would not let the client side wake up.
5349 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005350
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005351 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005352 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005353 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005354}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005355
Willy Tarreaud98cf932009-12-27 22:54:55 +01005356/* This function is an analyser which forwards response body (including chunk
5357 * sizes if any). It is called as soon as we must forward, even if we forward
5358 * zero byte. The only situation where it must not be called is when we're in
5359 * tunnel mode and we want to forward till the close. It's used both to forward
5360 * remaining data and to resync after end of body. It expects the msg_state to
5361 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5362 * read more data, or 1 once we can go on with next request or end the session.
5363 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len
5364 * bytes of pending data + the headers if not already done (between som and sov).
5365 * It eventually adjusts som to match sov after the data in between have been sent.
5366 */
5367int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5368{
5369 struct http_txn *txn = &s->txn;
5370 struct http_msg *msg = &s->txn.rsp;
5371
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005372 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5373 return 0;
5374
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005375 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005376 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005377 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005378 /* Output closed while we were sending data. We must abort and
5379 * wake the other side up.
5380 */
5381 msg->msg_state = HTTP_MSG_ERROR;
5382 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005383 return 1;
5384 }
5385
Willy Tarreau4fe41902010-06-07 22:27:41 +02005386 /* in most states, we should abort in case of early close */
5387 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005388
Willy Tarreaud98cf932009-12-27 22:54:55 +01005389 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5390 /* we have msg->col and msg->sov which both point to the first
5391 * byte of message body. msg->som still points to the beginning
5392 * of the message. We must save the body in req->lr because it
5393 * survives buffer re-alignments.
5394 */
5395 res->lr = res->data + msg->sov;
5396 if (txn->flags & TX_RES_TE_CHNK)
5397 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5398 else {
5399 msg->msg_state = HTTP_MSG_DATA;
5400 }
5401 }
5402
Willy Tarreaud98cf932009-12-27 22:54:55 +01005403 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005404 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005405 /* we may have some data pending */
5406 if (msg->hdr_content_len || msg->som != msg->sov) {
5407 int bytes = msg->sov - msg->som;
5408 if (bytes < 0) /* sov may have wrapped at the end */
5409 bytes += res->size;
5410 buffer_forward(res, bytes + msg->hdr_content_len);
5411 msg->hdr_content_len = 0; /* don't forward that again */
5412 msg->som = msg->sov;
5413 }
5414
Willy Tarreaucaabe412010-01-03 23:08:28 +01005415 if (msg->msg_state == HTTP_MSG_DATA) {
5416 /* must still forward */
5417 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005418 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005419
5420 /* nothing left to forward */
5421 if (txn->flags & TX_RES_TE_CHNK)
5422 msg->msg_state = HTTP_MSG_DATA_CRLF;
5423 else
5424 msg->msg_state = HTTP_MSG_DONE;
5425 }
5426 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005427 /* read the chunk size and assign it to ->hdr_content_len, then
5428 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5429 */
5430 int ret = http_parse_chunk_size(res, msg);
5431
5432 if (!ret)
5433 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005434 else if (ret < 0) {
5435 if (msg->err_pos >= 0)
5436 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005437 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005438 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005439 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreau57f5c122010-12-02 00:37:14 +01005440 /* Don't set a PUSH at the end of that chunk if it's not the last one */
5441 if (msg->msg_state == HTTP_MSG_DATA)
5442 res->flags |= BF_EXPECT_MORE;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005443 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005444 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5445 /* we want the CRLF after the data */
5446 int ret;
5447
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005448 res->lr = res->w + res->send_max;
5449 if (res->lr >= res->data + res->size)
5450 res->lr -= res->size;
5451
Willy Tarreaud98cf932009-12-27 22:54:55 +01005452 ret = http_skip_chunk_crlf(res, msg);
5453
5454 if (!ret)
5455 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005456 else if (ret < 0) {
5457 if (msg->err_pos >= 0)
5458 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005459 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005460 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005461 /* we're in MSG_CHUNK_SIZE now */
5462 }
5463 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5464 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005465
Willy Tarreaud98cf932009-12-27 22:54:55 +01005466 if (ret == 0)
5467 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005468 else if (ret < 0) {
5469 if (msg->err_pos >= 0)
5470 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005471 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005472 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005473 /* we're in HTTP_MSG_DONE now */
5474 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005475 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005476 int old_state = msg->msg_state;
5477
Willy Tarreau610ecce2010-01-04 21:15:02 +01005478 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005479 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005480 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5481 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5482 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005483 if (http_resync_states(s)) {
5484 http_silent_debug(__LINE__, s);
5485 /* some state changes occurred, maybe the analyser
5486 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005487 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005488 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5489 if (res->flags & BF_SHUTW) {
5490 /* response errors are most likely due to
5491 * the client aborting the transfer.
5492 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005493 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005494 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005495 if (msg->err_pos >= 0)
5496 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005497 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005498 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005499 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005500 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005501 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005502 }
5503 }
5504
Willy Tarreaud98cf932009-12-27 22:54:55 +01005505 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005506 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005507 if (res->flags & BF_SHUTR) {
5508 if (!(s->flags & SN_ERR_MASK))
5509 s->flags |= SN_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01005510 s->be->counters.srv_aborts++;
5511 if (s->srv)
5512 s->srv->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005513 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005514 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005515
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005516 if (res->flags & BF_SHUTW)
5517 goto aborted_xfer;
5518
Willy Tarreau40dba092010-03-04 18:14:51 +01005519 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005520 if (!s->req->analysers)
5521 goto return_bad_res;
5522
Willy Tarreaud98cf932009-12-27 22:54:55 +01005523 /* forward the chunk size as well as any pending data */
5524 if (msg->hdr_content_len || msg->som != msg->sov) {
Willy Tarreauacd20f82011-03-01 20:04:36 +01005525 int bytes = msg->sov - msg->som;
5526 if (bytes < 0) /* sov may have wrapped at the end */
5527 bytes += res->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005528 buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len);
5529 msg->hdr_content_len = 0; /* don't forward that again */
5530 msg->som = msg->sov;
5531 }
5532
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005533 /* When TE: chunked is used, we need to get there again to parse remaining
5534 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5535 * Similarly, with keep-alive on the client side, we don't want to forward a
5536 * close.
5537 */
5538 if ((txn->flags & TX_RES_TE_CHNK) ||
5539 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5540 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5541 buffer_dont_close(res);
5542
Willy Tarreaud98cf932009-12-27 22:54:55 +01005543 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005544 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005545 return 0;
5546
Willy Tarreau40dba092010-03-04 18:14:51 +01005547 return_bad_res: /* let's centralize all bad responses */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005548 s->be->counters.failed_resp++;
5549 if (s->srv)
5550 s->srv->counters.failed_resp++;
5551
5552 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005553 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005554 /* don't send any error message as we're in the body */
5555 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005556 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005557 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5558 if (s->srv)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005559 health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005560
5561 if (!(s->flags & SN_ERR_MASK))
5562 s->flags |= SN_ERR_PRXCOND;
5563 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005564 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005565 return 0;
5566
5567 aborted_xfer:
5568 txn->rsp.msg_state = HTTP_MSG_ERROR;
5569 /* don't send any error message as we're in the body */
5570 stream_int_retnclose(res->cons, NULL);
5571 res->analysers = 0;
5572 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5573
5574 s->fe->counters.cli_aborts++;
5575 if (s->fe != s->be)
5576 s->be->counters.cli_aborts++;
5577 if (s->srv)
5578 s->srv->counters.cli_aborts++;
5579
5580 if (!(s->flags & SN_ERR_MASK))
5581 s->flags |= SN_ERR_CLICL;
5582 if (!(s->flags & SN_FINST_MASK))
5583 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005584 return 0;
5585}
5586
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005587/* Iterate the same filter through all request headers.
5588 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005589 * Since it can manage the switch to another backend, it updates the per-proxy
5590 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005591 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005592int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005593{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005594 char term;
5595 char *cur_ptr, *cur_end, *cur_next;
5596 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005597 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005598 struct hdr_idx_elem *cur_hdr;
5599 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005600
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005601 last_hdr = 0;
5602
Willy Tarreau962c3f42010-01-10 00:15:35 +01005603 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005604 old_idx = 0;
5605
5606 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005607 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005608 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005609 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005610 (exp->action == ACT_ALLOW ||
5611 exp->action == ACT_DENY ||
5612 exp->action == ACT_TARPIT))
5613 return 0;
5614
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005615 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005616 if (!cur_idx)
5617 break;
5618
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005619 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005620 cur_ptr = cur_next;
5621 cur_end = cur_ptr + cur_hdr->len;
5622 cur_next = cur_end + cur_hdr->cr + 1;
5623
5624 /* Now we have one header between cur_ptr and cur_end,
5625 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005626 */
5627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005628 /* The annoying part is that pattern matching needs
5629 * that we modify the contents to null-terminate all
5630 * strings before testing them.
5631 */
5632
5633 term = *cur_end;
5634 *cur_end = '\0';
5635
5636 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5637 switch (exp->action) {
5638 case ACT_SETBE:
5639 /* It is not possible to jump a second time.
5640 * FIXME: should we return an HTTP/500 here so that
5641 * the admin knows there's a problem ?
5642 */
5643 if (t->be != t->fe)
5644 break;
5645
5646 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005647 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005648 last_hdr = 1;
5649 break;
5650
5651 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005652 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005653 last_hdr = 1;
5654 break;
5655
5656 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005657 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005658 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005659
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005660 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005661 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005662 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005664 break;
5665
5666 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005667 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005668 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005669
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005670 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005671 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005672 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005673
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005674 break;
5675
5676 case ACT_REPLACE:
5677 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5678 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5679 /* FIXME: if the user adds a newline in the replacement, the
5680 * index will not be recalculated for now, and the new line
5681 * will not be counted as a new header.
5682 */
5683
5684 cur_end += delta;
5685 cur_next += delta;
5686 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005687 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005688 break;
5689
5690 case ACT_REMOVE:
5691 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5692 cur_next += delta;
5693
Willy Tarreaufa355d42009-11-29 18:12:29 +01005694 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005695 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5696 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005697 cur_hdr->len = 0;
5698 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005699 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005700 break;
5701
5702 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005703 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005704 if (cur_end)
5705 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005706
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005707 /* keep the link from this header to next one in case of later
5708 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005709 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005710 old_idx = cur_idx;
5711 }
5712 return 0;
5713}
5714
5715
5716/* Apply the filter to the request line.
5717 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5718 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005719 * Since it can manage the switch to another backend, it updates the per-proxy
5720 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005721 */
5722int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5723{
5724 char term;
5725 char *cur_ptr, *cur_end;
5726 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005727 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005728 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005729
Willy Tarreau58f10d72006-12-04 02:26:12 +01005730
Willy Tarreau3d300592007-03-18 18:34:41 +01005731 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005732 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005733 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005734 (exp->action == ACT_ALLOW ||
5735 exp->action == ACT_DENY ||
5736 exp->action == ACT_TARPIT))
5737 return 0;
5738 else if (exp->action == ACT_REMOVE)
5739 return 0;
5740
5741 done = 0;
5742
Willy Tarreau962c3f42010-01-10 00:15:35 +01005743 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005744 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005745
5746 /* Now we have the request line between cur_ptr and cur_end */
5747
5748 /* The annoying part is that pattern matching needs
5749 * that we modify the contents to null-terminate all
5750 * strings before testing them.
5751 */
5752
5753 term = *cur_end;
5754 *cur_end = '\0';
5755
5756 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5757 switch (exp->action) {
5758 case ACT_SETBE:
5759 /* It is not possible to jump a second time.
5760 * FIXME: should we return an HTTP/500 here so that
5761 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005762 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005763 if (t->be != t->fe)
5764 break;
5765
5766 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005767 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005768 done = 1;
5769 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005770
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005771 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005772 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005773 done = 1;
5774 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005775
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005776 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005777 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005778
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005779 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005780 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005781 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005782
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005783 done = 1;
5784 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005785
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005786 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005787 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005788
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02005789 t->be->counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005790 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005791 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005792
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005793 done = 1;
5794 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005795
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005796 case ACT_REPLACE:
5797 *cur_end = term; /* restore the string terminator */
5798 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5799 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5800 /* FIXME: if the user adds a newline in the replacement, the
5801 * index will not be recalculated for now, and the new line
5802 * will not be counted as a new header.
5803 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005804
Willy Tarreaufa355d42009-11-29 18:12:29 +01005805 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005806 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005807 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005808 HTTP_MSG_RQMETH,
5809 cur_ptr, cur_end + 1,
5810 NULL, NULL);
5811 if (unlikely(!cur_end))
5812 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005813
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005814 /* we have a full request and we know that we have either a CR
5815 * or an LF at <ptr>.
5816 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005817 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5818 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005819 /* there is no point trying this regex on headers */
5820 return 1;
5821 }
5822 }
5823 *cur_end = term; /* restore the string terminator */
5824 return done;
5825}
Willy Tarreau97de6242006-12-27 17:18:38 +01005826
Willy Tarreau58f10d72006-12-04 02:26:12 +01005827
Willy Tarreau58f10d72006-12-04 02:26:12 +01005828
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005829/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005830 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005831 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005832 * unparsable request. Since it can manage the switch to another backend, it
5833 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005834 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005835int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005836{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005837 struct http_txn *txn = &s->txn;
5838 struct hdr_exp *exp;
5839
5840 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005841 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005842
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005843 /*
5844 * The interleaving of transformations and verdicts
5845 * makes it difficult to decide to continue or stop
5846 * the evaluation.
5847 */
5848
Willy Tarreau6c123b12010-01-28 20:22:06 +01005849 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5850 break;
5851
Willy Tarreau3d300592007-03-18 18:34:41 +01005852 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005853 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005854 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005855 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005856
5857 /* if this filter had a condition, evaluate it now and skip to
5858 * next filter if the condition does not match.
5859 */
5860 if (exp->cond) {
5861 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5862 ret = acl_pass(ret);
5863 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5864 ret = !ret;
5865
5866 if (!ret)
5867 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005868 }
5869
5870 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005871 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005872 if (unlikely(ret < 0))
5873 return -1;
5874
5875 if (likely(ret == 0)) {
5876 /* The filter did not match the request, it can be
5877 * iterated through all headers.
5878 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005879 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005880 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005881 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005882 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005883}
5884
5885
Willy Tarreaua15645d2007-03-18 16:22:39 +01005886
Willy Tarreau58f10d72006-12-04 02:26:12 +01005887/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005888 * Try to retrieve the server associated to the appsession.
5889 * If the server is found, it's assigned to the session.
5890 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005891void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005892 struct http_txn *txn = &t->txn;
5893 appsess *asession = NULL;
5894 char *sessid_temp = NULL;
5895
Cyril Bontéb21570a2009-11-29 20:04:48 +01005896 if (len > t->be->appsession_len) {
5897 len = t->be->appsession_len;
5898 }
5899
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005900 if (t->be->options2 & PR_O2_AS_REQL) {
5901 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005902 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005903 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005904 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005905 }
5906
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005907 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005908 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5909 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5910 return;
5911 }
5912
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005913 memcpy(txn->sessid, buf, len);
5914 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005915 }
5916
5917 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5918 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5919 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5920 return;
5921 }
5922
Cyril Bontéb21570a2009-11-29 20:04:48 +01005923 memcpy(sessid_temp, buf, len);
5924 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005925
5926 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5927 /* free previously allocated memory */
5928 pool_free2(apools.sessid, sessid_temp);
5929
5930 if (asession != NULL) {
5931 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5932 if (!(t->be->options2 & PR_O2_AS_REQL))
5933 asession->request_count++;
5934
5935 if (asession->serverid != NULL) {
5936 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005937
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005938 while (srv) {
5939 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005940 if ((srv->state & SRV_RUNNING) ||
5941 (t->be->options & PR_O_PERSIST) ||
5942 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005943 /* we found the server and it's usable */
5944 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005945 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005946 t->flags |= SN_DIRECT | SN_ASSIGNED;
5947 t->srv = srv;
5948 break;
5949 } else {
5950 txn->flags &= ~TX_CK_MASK;
5951 txn->flags |= TX_CK_DOWN;
5952 }
5953 }
5954 srv = srv->next;
5955 }
5956 }
5957 }
5958}
5959
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005960/* Find the end of a cookie value contained between <s> and <e>. It works the
5961 * same way as with headers above except that the semi-colon also ends a token.
5962 * See RFC2965 for more information. Note that it requires a valid header to
5963 * return a valid result.
5964 */
5965char *find_cookie_value_end(char *s, const char *e)
5966{
5967 int quoted, qdpair;
5968
5969 quoted = qdpair = 0;
5970 for (; s < e; s++) {
5971 if (qdpair) qdpair = 0;
5972 else if (quoted) {
5973 if (*s == '\\') qdpair = 1;
5974 else if (*s == '"') quoted = 0;
5975 }
5976 else if (*s == '"') quoted = 1;
5977 else if (*s == ',' || *s == ';') return s;
5978 }
5979 return s;
5980}
5981
5982/* Delete a value in a header between delimiters <from> and <next> in buffer
5983 * <buf>. The number of characters displaced is returned, and the pointer to
5984 * the first delimiter is updated if required. The function tries as much as
5985 * possible to respect the following principles :
5986 * - replace <from> delimiter by the <next> one unless <from> points to a
5987 * colon, in which case <next> is simply removed
5988 * - set exactly one space character after the new first delimiter, unless
5989 * there are not enough characters in the block being moved to do so.
5990 * - remove unneeded spaces before the previous delimiter and after the new
5991 * one.
5992 *
5993 * It is the caller's responsibility to ensure that :
5994 * - <from> points to a valid delimiter or the colon ;
5995 * - <next> points to a valid delimiter or the final CR/LF ;
5996 * - there are non-space chars before <from> ;
5997 * - there is a CR/LF at or after <next>.
5998 */
5999int del_hdr_value(struct buffer *buf, char **from, char *next)
6000{
6001 char *prev = *from;
6002
6003 if (*prev == ':') {
6004 /* We're removing the first value, preserve the colon and add a
6005 * space if possible.
6006 */
6007 if (!http_is_crlf[(unsigned char)*next])
6008 next++;
6009 prev++;
6010 if (prev < next)
6011 *prev++ = ' ';
6012
6013 while (http_is_spht[(unsigned char)*next])
6014 next++;
6015 } else {
6016 /* Remove useless spaces before the old delimiter. */
6017 while (http_is_spht[(unsigned char)*(prev-1)])
6018 prev--;
6019 *from = prev;
6020
6021 /* copy the delimiter and if possible a space if we're
6022 * not at the end of the line.
6023 */
6024 if (!http_is_crlf[(unsigned char)*next]) {
6025 *prev++ = *next++;
6026 if (prev + 1 < next)
6027 *prev++ = ' ';
6028 while (http_is_spht[(unsigned char)*next])
6029 next++;
6030 }
6031 }
6032 return buffer_replace2(buf, prev, next, NULL, 0);
6033}
6034
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006035/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006036 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006037 * desirable to call it only when needed. This code is quite complex because
6038 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6039 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006040 */
6041void manage_client_side_cookies(struct session *t, struct buffer *req)
6042{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006043 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006044 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006045 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006046 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6047 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006048
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006049 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006050 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006051 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006052
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006053 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006054 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006055 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006056
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006057 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006058 hdr_beg = hdr_next;
6059 hdr_end = hdr_beg + cur_hdr->len;
6060 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006061
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006062 /* We have one full header between hdr_beg and hdr_end, and the
6063 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006064 * "Cookie:" headers.
6065 */
6066
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006067 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006068 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006069 old_idx = cur_idx;
6070 continue;
6071 }
6072
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006073 del_from = NULL; /* nothing to be deleted */
6074 preserve_hdr = 0; /* assume we may kill the whole header */
6075
Willy Tarreau58f10d72006-12-04 02:26:12 +01006076 /* Now look for cookies. Conforming to RFC2109, we have to support
6077 * attributes whose name begin with a '$', and associate them with
6078 * the right cookie, if we want to delete this cookie.
6079 * So there are 3 cases for each cookie read :
6080 * 1) it's a special attribute, beginning with a '$' : ignore it.
6081 * 2) it's a server id cookie that we *MAY* want to delete : save
6082 * some pointers on it (last semi-colon, beginning of cookie...)
6083 * 3) it's an application cookie : we *MAY* have to delete a previous
6084 * "special" cookie.
6085 * At the end of loop, if a "special" cookie remains, we may have to
6086 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006087 * *MUST* delete it.
6088 *
6089 * Note: RFC2965 is unclear about the processing of spaces around
6090 * the equal sign in the ATTR=VALUE form. A careful inspection of
6091 * the RFC explicitly allows spaces before it, and not within the
6092 * tokens (attrs or values). An inspection of RFC2109 allows that
6093 * too but section 10.1.3 lets one think that spaces may be allowed
6094 * after the equal sign too, resulting in some (rare) buggy
6095 * implementations trying to do that. So let's do what servers do.
6096 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6097 * allowed quoted strings in values, with any possible character
6098 * after a backslash, including control chars and delimitors, which
6099 * causes parsing to become ambiguous. Browsers also allow spaces
6100 * within values even without quotes.
6101 *
6102 * We have to keep multiple pointers in order to support cookie
6103 * removal at the beginning, middle or end of header without
6104 * corrupting the header. All of these headers are valid :
6105 *
6106 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6107 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6108 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6109 * | | | | | | | | |
6110 * | | | | | | | | hdr_end <--+
6111 * | | | | | | | +--> next
6112 * | | | | | | +----> val_end
6113 * | | | | | +-----------> val_beg
6114 * | | | | +--------------> equal
6115 * | | | +----------------> att_end
6116 * | | +---------------------> att_beg
6117 * | +--------------------------> prev
6118 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006119 */
6120
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006121 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6122 /* Iterate through all cookies on this line */
6123
6124 /* find att_beg */
6125 att_beg = prev + 1;
6126 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6127 att_beg++;
6128
6129 /* find att_end : this is the first character after the last non
6130 * space before the equal. It may be equal to hdr_end.
6131 */
6132 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006133
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006134 while (equal < hdr_end) {
6135 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006136 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006137 if (http_is_spht[(unsigned char)*equal++])
6138 continue;
6139 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006140 }
6141
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006142 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6143 * is between <att_beg> and <equal>, both may be identical.
6144 */
6145
6146 /* look for end of cookie if there is an equal sign */
6147 if (equal < hdr_end && *equal == '=') {
6148 /* look for the beginning of the value */
6149 val_beg = equal + 1;
6150 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6151 val_beg++;
6152
6153 /* find the end of the value, respecting quotes */
6154 next = find_cookie_value_end(val_beg, hdr_end);
6155
6156 /* make val_end point to the first white space or delimitor after the value */
6157 val_end = next;
6158 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6159 val_end--;
6160 } else {
6161 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006162 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006163
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006164 /* We have nothing to do with attributes beginning with '$'. However,
6165 * they will automatically be removed if a header before them is removed,
6166 * since they're supposed to be linked together.
6167 */
6168 if (*att_beg == '$')
6169 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006170
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006171 /* Ignore cookies with no equal sign */
6172 if (equal == next) {
6173 /* This is not our cookie, so we must preserve it. But if we already
6174 * scheduled another cookie for removal, we cannot remove the
6175 * complete header, but we can remove the previous block itself.
6176 */
6177 preserve_hdr = 1;
6178 if (del_from != NULL) {
6179 int delta = del_hdr_value(req, &del_from, prev);
6180 val_end += delta;
6181 next += delta;
6182 hdr_end += delta;
6183 hdr_next += delta;
6184 cur_hdr->len += delta;
6185 http_msg_move_end(&txn->req, delta);
6186 prev = del_from;
6187 del_from = NULL;
6188 }
6189 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006190 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006191
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006192 /* if there are spaces around the equal sign, we need to
6193 * strip them otherwise we'll get trouble for cookie captures,
6194 * or even for rewrites. Since this happens extremely rarely,
6195 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006196 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006197 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6198 int stripped_before = 0;
6199 int stripped_after = 0;
6200
6201 if (att_end != equal) {
6202 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6203 equal += stripped_before;
6204 val_beg += stripped_before;
6205 }
6206
6207 if (val_beg > equal + 1) {
6208 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6209 val_beg += stripped_after;
6210 stripped_before += stripped_after;
6211 }
6212
6213 val_end += stripped_before;
6214 next += stripped_before;
6215 hdr_end += stripped_before;
6216 hdr_next += stripped_before;
6217 cur_hdr->len += stripped_before;
6218 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006219 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006220 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006221
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006222 /* First, let's see if we want to capture this cookie. We check
6223 * that we don't already have a client side cookie, because we
6224 * can only capture one. Also as an optimisation, we ignore
6225 * cookies shorter than the declared name.
6226 */
6227 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6228 (val_end - att_beg >= t->fe->capture_namelen) &&
6229 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6230 int log_len = val_end - att_beg;
6231
6232 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6233 Alert("HTTP logging : out of memory.\n");
6234 } else {
6235 if (log_len > t->fe->capture_len)
6236 log_len = t->fe->capture_len;
6237 memcpy(txn->cli_cookie, att_beg, log_len);
6238 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006239 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006240 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006241
Willy Tarreaubca99692010-10-06 19:25:55 +02006242 /* Persistence cookies in passive, rewrite or insert mode have the
6243 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006244 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006245 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006246 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006247 * For cookies in prefix mode, the form is :
6248 *
6249 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006250 */
6251 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6252 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6253 struct server *srv = t->be->srv;
6254 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006255
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6257 * have the server ID between val_beg and delim, and the original cookie between
6258 * delim+1 and val_end. Otherwise, delim==val_end :
6259 *
6260 * Cookie: NAME=SRV; # in all but prefix modes
6261 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6262 * | || || | |+-> next
6263 * | || || | +--> val_end
6264 * | || || +---------> delim
6265 * | || |+------------> val_beg
6266 * | || +-------------> att_end = equal
6267 * | |+-----------------> att_beg
6268 * | +------------------> prev
6269 * +-------------------------> hdr_beg
6270 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006271
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006272 if (t->be->options & PR_O_COOK_PFX) {
6273 for (delim = val_beg; delim < val_end; delim++)
6274 if (*delim == COOKIE_DELIM)
6275 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006276 } else {
6277 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006278 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006279 /* Now check if the cookie contains a date field, which would
6280 * appear after a vertical bar ('|') just after the server name
6281 * and before the delimiter.
6282 */
6283 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6284 if (vbar1) {
6285 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006286 * right is the last seen date. It is a base64 encoded
6287 * 30-bit value representing the UNIX date since the
6288 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006289 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006290 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006291 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006292 if (val_end - vbar1 >= 5) {
6293 val = b64tos30(vbar1);
6294 if (val > 0)
6295 txn->cookie_last_date = val << 2;
6296 }
6297 /* look for a second vertical bar */
6298 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6299 if (vbar1 && (val_end - vbar1 > 5)) {
6300 val = b64tos30(vbar1 + 1);
6301 if (val > 0)
6302 txn->cookie_first_date = val << 2;
6303 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006304 }
6305 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006306
Willy Tarreauf64d1412010-10-07 20:06:11 +02006307 /* if the cookie has an expiration date and the proxy wants to check
6308 * it, then we do that now. We first check if the cookie is too old,
6309 * then only if it has expired. We detect strict overflow because the
6310 * time resolution here is not great (4 seconds). Cookies with dates
6311 * in the future are ignored if their offset is beyond one day. This
6312 * allows an admin to fix timezone issues without expiring everyone
6313 * and at the same time avoids keeping unwanted side effects for too
6314 * long.
6315 */
6316 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006317 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6318 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006319 txn->flags &= ~TX_CK_MASK;
6320 txn->flags |= TX_CK_OLD;
6321 delim = val_beg; // let's pretend we have not found the cookie
6322 txn->cookie_first_date = 0;
6323 txn->cookie_last_date = 0;
6324 }
6325 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006326 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6327 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006328 txn->flags &= ~TX_CK_MASK;
6329 txn->flags |= TX_CK_EXPIRED;
6330 delim = val_beg; // let's pretend we have not found the cookie
6331 txn->cookie_first_date = 0;
6332 txn->cookie_last_date = 0;
6333 }
6334
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006335 /* Here, we'll look for the first running server which supports the cookie.
6336 * This allows to share a same cookie between several servers, for example
6337 * to dedicate backup servers to specific servers only.
6338 * However, to prevent clients from sticking to cookie-less backup server
6339 * when they have incidentely learned an empty cookie, we simply ignore
6340 * empty cookies and mark them as invalid.
6341 * The same behaviour is applied when persistence must be ignored.
6342 */
6343 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6344 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006345
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006346 while (srv) {
6347 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6348 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6349 if ((srv->state & SRV_RUNNING) ||
6350 (t->be->options & PR_O_PERSIST) ||
6351 (t->flags & SN_FORCE_PRST)) {
6352 /* we found the server and we can use it */
6353 txn->flags &= ~TX_CK_MASK;
6354 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6355 t->flags |= SN_DIRECT | SN_ASSIGNED;
6356 t->srv = srv;
6357 break;
6358 } else {
6359 /* we found a server, but it's down,
6360 * mark it as such and go on in case
6361 * another one is available.
6362 */
6363 txn->flags &= ~TX_CK_MASK;
6364 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006365 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006366 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006367 srv = srv->next;
6368 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006369
Willy Tarreauf64d1412010-10-07 20:06:11 +02006370 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006371 /* no server matched this cookie */
6372 txn->flags &= ~TX_CK_MASK;
6373 txn->flags |= TX_CK_INVALID;
6374 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006375
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006376 /* depending on the cookie mode, we may have to either :
6377 * - delete the complete cookie if we're in insert+indirect mode, so that
6378 * the server never sees it ;
6379 * - remove the server id from the cookie value, and tag the cookie as an
6380 * application cookie so that it does not get accidentely removed later,
6381 * if we're in cookie prefix mode
6382 */
6383 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6384 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006385
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006386 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6387 val_end += delta;
6388 next += delta;
6389 hdr_end += delta;
6390 hdr_next += delta;
6391 cur_hdr->len += delta;
6392 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006393
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006394 del_from = NULL;
6395 preserve_hdr = 1; /* we want to keep this cookie */
6396 }
6397 else if (del_from == NULL &&
6398 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6399 del_from = prev;
6400 }
6401 } else {
6402 /* This is not our cookie, so we must preserve it. But if we already
6403 * scheduled another cookie for removal, we cannot remove the
6404 * complete header, but we can remove the previous block itself.
6405 */
6406 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006407
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006408 if (del_from != NULL) {
6409 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006410 if (att_beg >= del_from)
6411 att_beg += delta;
6412 if (att_end >= del_from)
6413 att_end += delta;
6414 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006415 val_end += delta;
6416 next += delta;
6417 hdr_end += delta;
6418 hdr_next += delta;
6419 cur_hdr->len += delta;
6420 http_msg_move_end(&txn->req, delta);
6421 prev = del_from;
6422 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006423 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006424 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006425
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006426 /* Look for the appsession cookie unless persistence must be ignored */
6427 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6428 int cmp_len, value_len;
6429 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006430
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006431 if (t->be->options2 & PR_O2_AS_PFX) {
6432 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6433 value_begin = att_beg + t->be->appsession_name_len;
6434 value_len = val_end - att_beg - t->be->appsession_name_len;
6435 } else {
6436 cmp_len = att_end - att_beg;
6437 value_begin = val_beg;
6438 value_len = val_end - val_beg;
6439 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006440
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006441 /* let's see if the cookie is our appcookie */
6442 if (cmp_len == t->be->appsession_name_len &&
6443 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6444 manage_client_side_appsession(t, value_begin, value_len);
6445 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006446 }
6447
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006448 /* continue with next cookie on this header line */
6449 att_beg = next;
6450 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006451
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006452 /* There are no more cookies on this line.
6453 * We may still have one (or several) marked for deletion at the
6454 * end of the line. We must do this now in two ways :
6455 * - if some cookies must be preserved, we only delete from the
6456 * mark to the end of line ;
6457 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006458 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006459 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006460 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006461 if (preserve_hdr) {
6462 delta = del_hdr_value(req, &del_from, hdr_end);
6463 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006464 cur_hdr->len += delta;
6465 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006466 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006467
6468 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006469 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6470 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006471 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006472 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006473 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006474 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006475 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006476 }
6477
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006478 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006479 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006480 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006481}
6482
6483
Willy Tarreaua15645d2007-03-18 16:22:39 +01006484/* Iterate the same filter through all response headers contained in <rtr>.
6485 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6486 */
6487int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6488{
6489 char term;
6490 char *cur_ptr, *cur_end, *cur_next;
6491 int cur_idx, old_idx, last_hdr;
6492 struct http_txn *txn = &t->txn;
6493 struct hdr_idx_elem *cur_hdr;
6494 int len, delta;
6495
6496 last_hdr = 0;
6497
Willy Tarreau962c3f42010-01-10 00:15:35 +01006498 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006499 old_idx = 0;
6500
6501 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006502 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006503 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006504 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006505 (exp->action == ACT_ALLOW ||
6506 exp->action == ACT_DENY))
6507 return 0;
6508
6509 cur_idx = txn->hdr_idx.v[old_idx].next;
6510 if (!cur_idx)
6511 break;
6512
6513 cur_hdr = &txn->hdr_idx.v[cur_idx];
6514 cur_ptr = cur_next;
6515 cur_end = cur_ptr + cur_hdr->len;
6516 cur_next = cur_end + cur_hdr->cr + 1;
6517
6518 /* Now we have one header between cur_ptr and cur_end,
6519 * and the next header starts at cur_next.
6520 */
6521
6522 /* The annoying part is that pattern matching needs
6523 * that we modify the contents to null-terminate all
6524 * strings before testing them.
6525 */
6526
6527 term = *cur_end;
6528 *cur_end = '\0';
6529
6530 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6531 switch (exp->action) {
6532 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006533 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006534 last_hdr = 1;
6535 break;
6536
6537 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006538 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006539 last_hdr = 1;
6540 break;
6541
6542 case ACT_REPLACE:
6543 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6544 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6545 /* FIXME: if the user adds a newline in the replacement, the
6546 * index will not be recalculated for now, and the new line
6547 * will not be counted as a new header.
6548 */
6549
6550 cur_end += delta;
6551 cur_next += delta;
6552 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006553 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006554 break;
6555
6556 case ACT_REMOVE:
6557 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6558 cur_next += delta;
6559
Willy Tarreaufa355d42009-11-29 18:12:29 +01006560 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006561 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6562 txn->hdr_idx.used--;
6563 cur_hdr->len = 0;
6564 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006565 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006566 break;
6567
6568 }
6569 }
6570 if (cur_end)
6571 *cur_end = term; /* restore the string terminator */
6572
6573 /* keep the link from this header to next one in case of later
6574 * removal of next header.
6575 */
6576 old_idx = cur_idx;
6577 }
6578 return 0;
6579}
6580
6581
6582/* Apply the filter to the status line in the response buffer <rtr>.
6583 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6584 * or -1 if a replacement resulted in an invalid status line.
6585 */
6586int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6587{
6588 char term;
6589 char *cur_ptr, *cur_end;
6590 int done;
6591 struct http_txn *txn = &t->txn;
6592 int len, delta;
6593
6594
Willy Tarreau3d300592007-03-18 18:34:41 +01006595 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006596 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006597 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006598 (exp->action == ACT_ALLOW ||
6599 exp->action == ACT_DENY))
6600 return 0;
6601 else if (exp->action == ACT_REMOVE)
6602 return 0;
6603
6604 done = 0;
6605
Willy Tarreau962c3f42010-01-10 00:15:35 +01006606 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006607 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006608
6609 /* Now we have the status line between cur_ptr and cur_end */
6610
6611 /* The annoying part is that pattern matching needs
6612 * that we modify the contents to null-terminate all
6613 * strings before testing them.
6614 */
6615
6616 term = *cur_end;
6617 *cur_end = '\0';
6618
6619 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6620 switch (exp->action) {
6621 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006622 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006623 done = 1;
6624 break;
6625
6626 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006627 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006628 done = 1;
6629 break;
6630
6631 case ACT_REPLACE:
6632 *cur_end = term; /* restore the string terminator */
6633 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6634 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6635 /* FIXME: if the user adds a newline in the replacement, the
6636 * index will not be recalculated for now, and the new line
6637 * will not be counted as a new header.
6638 */
6639
Willy Tarreaufa355d42009-11-29 18:12:29 +01006640 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006642 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006643 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006644 cur_ptr, cur_end + 1,
6645 NULL, NULL);
6646 if (unlikely(!cur_end))
6647 return -1;
6648
6649 /* we have a full respnse and we know that we have either a CR
6650 * or an LF at <ptr>.
6651 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006652 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006653 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006654 /* there is no point trying this regex on headers */
6655 return 1;
6656 }
6657 }
6658 *cur_end = term; /* restore the string terminator */
6659 return done;
6660}
6661
6662
6663
6664/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006665 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006666 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6667 * unparsable response.
6668 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006669int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006670{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006671 struct http_txn *txn = &s->txn;
6672 struct hdr_exp *exp;
6673
6674 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006675 int ret;
6676
6677 /*
6678 * The interleaving of transformations and verdicts
6679 * makes it difficult to decide to continue or stop
6680 * the evaluation.
6681 */
6682
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006683 if (txn->flags & TX_SVDENY)
6684 break;
6685
Willy Tarreau3d300592007-03-18 18:34:41 +01006686 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006687 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6688 exp->action == ACT_PASS)) {
6689 exp = exp->next;
6690 continue;
6691 }
6692
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006693 /* if this filter had a condition, evaluate it now and skip to
6694 * next filter if the condition does not match.
6695 */
6696 if (exp->cond) {
6697 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6698 ret = acl_pass(ret);
6699 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6700 ret = !ret;
6701 if (!ret)
6702 continue;
6703 }
6704
Willy Tarreaua15645d2007-03-18 16:22:39 +01006705 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006706 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006707 if (unlikely(ret < 0))
6708 return -1;
6709
6710 if (likely(ret == 0)) {
6711 /* The filter did not match the response, it can be
6712 * iterated through all headers.
6713 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006714 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006715 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006716 }
6717 return 0;
6718}
6719
6720
Willy Tarreaua15645d2007-03-18 16:22:39 +01006721/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006722 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006723 * desirable to call it only when needed. This function is also used when we
6724 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006725 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006726void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006727{
6728 struct http_txn *txn = &t->txn;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006729 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006730 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006731 char *hdr_beg, *hdr_end, *hdr_next;
6732 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006733
Willy Tarreaua15645d2007-03-18 16:22:39 +01006734 /* Iterate through the headers.
6735 * we start with the start line.
6736 */
6737 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006738 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006739
6740 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6741 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006742 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006743
6744 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006745 hdr_beg = hdr_next;
6746 hdr_end = hdr_beg + cur_hdr->len;
6747 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006748
Willy Tarreau24581ba2010-08-31 22:39:35 +02006749 /* We have one full header between hdr_beg and hdr_end, and the
6750 * next header starts at hdr_next. We're only interested in
6751 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006752 */
6753
Willy Tarreau24581ba2010-08-31 22:39:35 +02006754 is_cookie2 = 0;
6755 prev = hdr_beg + 10;
6756 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006757 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006758 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6759 if (!val) {
6760 old_idx = cur_idx;
6761 continue;
6762 }
6763 is_cookie2 = 1;
6764 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006765 }
6766
Willy Tarreau24581ba2010-08-31 22:39:35 +02006767 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6768 * <prev> points to the colon.
6769 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006770 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006771
Willy Tarreau24581ba2010-08-31 22:39:35 +02006772 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6773 * check-cache is enabled) and we are not interested in checking
6774 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006775 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006776 if (t->be->cookie_name == NULL &&
6777 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006778 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006779 return;
6780
Willy Tarreau24581ba2010-08-31 22:39:35 +02006781 /* OK so now we know we have to process this response cookie.
6782 * The format of the Set-Cookie header is slightly different
6783 * from the format of the Cookie header in that it does not
6784 * support the comma as a cookie delimiter (thus the header
6785 * cannot be folded) because the Expires attribute described in
6786 * the original Netscape's spec may contain an unquoted date
6787 * with a comma inside. We have to live with this because
6788 * many browsers don't support Max-Age and some browsers don't
6789 * support quoted strings. However the Set-Cookie2 header is
6790 * clean.
6791 *
6792 * We have to keep multiple pointers in order to support cookie
6793 * removal at the beginning, middle or end of header without
6794 * corrupting the header (in case of set-cookie2). A special
6795 * pointer, <scav> points to the beginning of the set-cookie-av
6796 * fields after the first semi-colon. The <next> pointer points
6797 * either to the end of line (set-cookie) or next unquoted comma
6798 * (set-cookie2). All of these headers are valid :
6799 *
6800 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6801 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6802 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6803 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6804 * | | | | | | | | | |
6805 * | | | | | | | | +-> next hdr_end <--+
6806 * | | | | | | | +------------> scav
6807 * | | | | | | +--------------> val_end
6808 * | | | | | +--------------------> val_beg
6809 * | | | | +----------------------> equal
6810 * | | | +------------------------> att_end
6811 * | | +----------------------------> att_beg
6812 * | +------------------------------> prev
6813 * +-----------------------------------------> hdr_beg
6814 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006815
Willy Tarreau24581ba2010-08-31 22:39:35 +02006816 for (; prev < hdr_end; prev = next) {
6817 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006818
Willy Tarreau24581ba2010-08-31 22:39:35 +02006819 /* find att_beg */
6820 att_beg = prev + 1;
6821 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6822 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006823
Willy Tarreau24581ba2010-08-31 22:39:35 +02006824 /* find att_end : this is the first character after the last non
6825 * space before the equal. It may be equal to hdr_end.
6826 */
6827 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006828
Willy Tarreau24581ba2010-08-31 22:39:35 +02006829 while (equal < hdr_end) {
6830 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6831 break;
6832 if (http_is_spht[(unsigned char)*equal++])
6833 continue;
6834 att_end = equal;
6835 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006836
Willy Tarreau24581ba2010-08-31 22:39:35 +02006837 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6838 * is between <att_beg> and <equal>, both may be identical.
6839 */
6840
6841 /* look for end of cookie if there is an equal sign */
6842 if (equal < hdr_end && *equal == '=') {
6843 /* look for the beginning of the value */
6844 val_beg = equal + 1;
6845 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6846 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006847
Willy Tarreau24581ba2010-08-31 22:39:35 +02006848 /* find the end of the value, respecting quotes */
6849 next = find_cookie_value_end(val_beg, hdr_end);
6850
6851 /* make val_end point to the first white space or delimitor after the value */
6852 val_end = next;
6853 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6854 val_end--;
6855 } else {
6856 /* <equal> points to next comma, semi-colon or EOL */
6857 val_beg = val_end = next = equal;
6858 }
6859
6860 if (next < hdr_end) {
6861 /* Set-Cookie2 supports multiple cookies, and <next> points to
6862 * a colon or semi-colon before the end. So skip all attr-value
6863 * pairs and look for the next comma. For Set-Cookie, since
6864 * commas are permitted in values, skip to the end.
6865 */
6866 if (is_cookie2)
6867 next = find_hdr_value_end(next, hdr_end);
6868 else
6869 next = hdr_end;
6870 }
6871
6872 /* Now everything is as on the diagram above */
6873
6874 /* Ignore cookies with no equal sign */
6875 if (equal == val_end)
6876 continue;
6877
6878 /* If there are spaces around the equal sign, we need to
6879 * strip them otherwise we'll get trouble for cookie captures,
6880 * or even for rewrites. Since this happens extremely rarely,
6881 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006882 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006883 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6884 int stripped_before = 0;
6885 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006886
Willy Tarreau24581ba2010-08-31 22:39:35 +02006887 if (att_end != equal) {
6888 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6889 equal += stripped_before;
6890 val_beg += stripped_before;
6891 }
6892
6893 if (val_beg > equal + 1) {
6894 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6895 val_beg += stripped_after;
6896 stripped_before += stripped_after;
6897 }
6898
6899 val_end += stripped_before;
6900 next += stripped_before;
6901 hdr_end += stripped_before;
6902 hdr_next += stripped_before;
6903 cur_hdr->len += stripped_before;
6904 http_msg_move_end(&txn->req, stripped_before);
6905 }
6906
6907 /* First, let's see if we want to capture this cookie. We check
6908 * that we don't already have a server side cookie, because we
6909 * can only capture one. Also as an optimisation, we ignore
6910 * cookies shorter than the declared name.
6911 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006912 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006913 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006914 (val_end - att_beg >= t->fe->capture_namelen) &&
6915 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6916 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006917 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006918 Alert("HTTP logging : out of memory.\n");
6919 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006920 else {
6921 if (log_len > t->fe->capture_len)
6922 log_len = t->fe->capture_len;
6923 memcpy(txn->srv_cookie, att_beg, log_len);
6924 txn->srv_cookie[log_len] = 0;
6925 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006926 }
6927
6928 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006929 if (!(t->flags & SN_IGNORE_PRST) &&
6930 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6931 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006932 /* assume passive cookie by default */
6933 txn->flags &= ~TX_SCK_MASK;
6934 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006935
6936 /* If the cookie is in insert mode on a known server, we'll delete
6937 * this occurrence because we'll insert another one later.
6938 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006939 * a direct access.
6940 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006941 if (t->be->options2 & PR_O2_COOK_PSV) {
6942 /* The "preserve" flag was set, we don't want to touch the
6943 * server's cookie.
6944 */
6945 }
6946 else if (((t->srv) && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006947 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006948 /* this cookie must be deleted */
6949 if (*prev == ':' && next == hdr_end) {
6950 /* whole header */
6951 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6952 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6953 txn->hdr_idx.used--;
6954 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006955 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006956 hdr_next += delta;
6957 http_msg_move_end(&txn->rsp, delta);
6958 /* note: while both invalid now, <next> and <hdr_end>
6959 * are still equal, so the for() will stop as expected.
6960 */
6961 } else {
6962 /* just remove the value */
6963 int delta = del_hdr_value(res, &prev, next);
6964 next = prev;
6965 hdr_end += delta;
6966 hdr_next += delta;
6967 cur_hdr->len += delta;
6968 http_msg_move_end(&txn->rsp, delta);
6969 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006970 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006971 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006972 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006973 }
6974 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006975 (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006976 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006977 * with this server since we know it.
6978 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006979 delta = buffer_replace2(res, val_beg, val_end, t->srv->cookie, t->srv->cklen);
6980 next += delta;
6981 hdr_end += delta;
6982 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006983 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006984 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006985
Willy Tarreauf1348312010-10-07 15:54:11 +02006986 txn->flags &= ~TX_SCK_MASK;
6987 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006988 }
6989 else if ((t->srv) && (t->srv->cookie) &&
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006990 (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006991 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006992 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006993 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006994 delta = buffer_replace2(res, val_beg, val_beg, t->srv->cookie, t->srv->cklen + 1);
6995 next += delta;
6996 hdr_end += delta;
6997 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006998 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006999 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007000
Willy Tarreau24581ba2010-08-31 22:39:35 +02007001 val_beg[t->srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007002 txn->flags &= ~TX_SCK_MASK;
7003 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007004 }
7005 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007006 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7007 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007008 int cmp_len, value_len;
7009 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007010
Cyril Bontéb21570a2009-11-29 20:04:48 +01007011 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007012 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7013 value_begin = att_beg + t->be->appsession_name_len;
7014 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007015 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007016 cmp_len = att_end - att_beg;
7017 value_begin = val_beg;
7018 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007019 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007020
Cyril Bonté17530c32010-04-06 21:11:10 +02007021 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007022 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7023 /* free a possibly previously allocated memory */
7024 pool_free2(apools.sessid, txn->sessid);
7025
Cyril Bontéb21570a2009-11-29 20:04:48 +01007026 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007027 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007028 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7029 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7030 return;
7031 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007032 memcpy(txn->sessid, value_begin, value_len);
7033 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007034 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007035 }
7036 /* that's done for this cookie, check the next one on the same
7037 * line when next != hdr_end (only if is_cookie2).
7038 */
7039 }
7040 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007041 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007042 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007043
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007044 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007045 appsess *asession = NULL;
7046 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007047 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007048 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007049 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007050 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7051 Alert("Not enough Memory process_srv():asession:calloc().\n");
7052 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7053 return;
7054 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007055 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7056
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007057 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7058 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7059 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007060 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007061 return;
7062 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007063 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007064 asession->sessid[t->be->appsession_len] = 0;
7065
Willy Tarreau1fac7532010-01-09 19:23:06 +01007066 server_id_len = strlen(t->srv->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007067 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007068 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007069 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007070 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007071 return;
7072 }
7073 asession->serverid[0] = '\0';
7074 memcpy(asession->serverid, t->srv->id, server_id_len);
7075
7076 asession->request_count = 0;
7077 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7078 }
7079
7080 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7081 asession->request_count++;
7082 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007083}
7084
7085
Willy Tarreaua15645d2007-03-18 16:22:39 +01007086/*
7087 * Check if response is cacheable or not. Updates t->flags.
7088 */
7089void check_response_for_cacheability(struct session *t, struct buffer *rtr)
7090{
7091 struct http_txn *txn = &t->txn;
7092 char *p1, *p2;
7093
7094 char *cur_ptr, *cur_end, *cur_next;
7095 int cur_idx;
7096
Willy Tarreau5df51872007-11-25 16:20:08 +01007097 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007098 return;
7099
7100 /* Iterate through the headers.
7101 * we start with the start line.
7102 */
7103 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007104 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007105
7106 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7107 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007108 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007109
7110 cur_hdr = &txn->hdr_idx.v[cur_idx];
7111 cur_ptr = cur_next;
7112 cur_end = cur_ptr + cur_hdr->len;
7113 cur_next = cur_end + cur_hdr->cr + 1;
7114
7115 /* We have one full header between cur_ptr and cur_end, and the
7116 * next header starts at cur_next. We're only interested in
7117 * "Cookie:" headers.
7118 */
7119
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007120 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7121 if (val) {
7122 if ((cur_end - (cur_ptr + val) >= 8) &&
7123 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7124 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7125 return;
7126 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007127 }
7128
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007129 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7130 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007131 continue;
7132
7133 /* OK, right now we know we have a cache-control header at cur_ptr */
7134
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007135 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007136
7137 if (p1 >= cur_end) /* no more info */
7138 continue;
7139
7140 /* p1 is at the beginning of the value */
7141 p2 = p1;
7142
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007143 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007144 p2++;
7145
7146 /* we have a complete value between p1 and p2 */
7147 if (p2 < cur_end && *p2 == '=') {
7148 /* we have something of the form no-cache="set-cookie" */
7149 if ((cur_end - p1 >= 21) &&
7150 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7151 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007152 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007153 continue;
7154 }
7155
7156 /* OK, so we know that either p2 points to the end of string or to a comma */
7157 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7158 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7159 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7160 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007161 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007162 return;
7163 }
7164
7165 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007166 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007167 continue;
7168 }
7169 }
7170}
7171
7172
Willy Tarreau58f10d72006-12-04 02:26:12 +01007173/*
7174 * Try to retrieve a known appsession in the URI, then the associated server.
7175 * If the server is found, it's assigned to the session.
7176 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007177void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007178{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007179 char *end_params, *first_param, *cur_param, *next_param;
7180 char separator;
7181 int value_len;
7182
7183 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007184
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007185 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007186 (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 +01007187 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007188 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007189
Cyril Bontéb21570a2009-11-29 20:04:48 +01007190 first_param = NULL;
7191 switch (mode) {
7192 case PR_O2_AS_M_PP:
7193 first_param = memchr(begin, ';', len);
7194 break;
7195 case PR_O2_AS_M_QS:
7196 first_param = memchr(begin, '?', len);
7197 break;
7198 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007199
Cyril Bontéb21570a2009-11-29 20:04:48 +01007200 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007201 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007202 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007203
Cyril Bontéb21570a2009-11-29 20:04:48 +01007204 switch (mode) {
7205 case PR_O2_AS_M_PP:
7206 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7207 end_params = (char *) begin + len;
7208 }
7209 separator = ';';
7210 break;
7211 case PR_O2_AS_M_QS:
7212 end_params = (char *) begin + len;
7213 separator = '&';
7214 break;
7215 default:
7216 /* unknown mode, shouldn't happen */
7217 return;
7218 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007219
Cyril Bontéb21570a2009-11-29 20:04:48 +01007220 cur_param = next_param = end_params;
7221 while (cur_param > first_param) {
7222 cur_param--;
7223 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7224 /* let's see if this is the appsession parameter */
7225 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7226 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7227 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7228 /* Cool... it's the right one */
7229 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7230 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7231 if (value_len > 0) {
7232 manage_client_side_appsession(t, cur_param, value_len);
7233 }
7234 break;
7235 }
7236 next_param = cur_param;
7237 }
7238 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007239#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007240 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007241 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007242#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007243}
7244
Willy Tarreaub2513902006-12-17 14:52:38 +01007245/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007246 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007247 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007248 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007249 * It is assumed that the request is either a HEAD, GET, or POST and that the
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007250 * t->be->uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007251 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007252 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007253 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007254int stats_check_uri(struct session *t, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007255{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007256 struct http_txn *txn = &t->txn;
Willy Tarreaub2513902006-12-17 14:52:38 +01007257 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007258 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007259
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007260 if (!uri_auth)
7261 return 0;
7262
Cyril Bonté70be45d2010-10-12 00:14:35 +02007263 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007264 return 0;
7265
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007266 memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats));
7267
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007268 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007269 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007270 return 0;
7271
Willy Tarreau962c3f42010-01-10 00:15:35 +01007272 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007273
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007274 /* the URI is in h */
7275 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007276 return 0;
7277
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007278 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007279 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007280 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007281 t->data_ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007282 break;
7283 }
7284 h++;
7285 }
7286
7287 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007288 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7289 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007290 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007291 t->data_ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007292 break;
7293 }
7294 h++;
7295 }
7296 }
7297
Willy Tarreau962c3f42010-01-10 00:15:35 +01007298 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7299 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007300 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007301 t->data_ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007302 break;
7303 }
7304 h++;
7305 }
7306
Cyril Bonté70be45d2010-10-12 00:14:35 +02007307 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7308 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7309 if (memcmp(h, ";st=", 4) == 0) {
7310 h += 4;
7311
7312 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
7313 t->data_ctx.stats.st_code = STAT_STATUS_DONE;
7314 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
7315 t->data_ctx.stats.st_code = STAT_STATUS_NONE;
7316 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
7317 t->data_ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007318 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
7319 t->data_ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007320 else
7321 t->data_ctx.stats.st_code = STAT_STATUS_UNKN;
7322 break;
7323 }
7324 h++;
7325 }
7326
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007327 t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
7328
Willy Tarreaub2513902006-12-17 14:52:38 +01007329 return 1;
7330}
7331
Willy Tarreau4076a152009-04-02 15:18:36 +02007332/*
7333 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007334 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7335 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007336 */
7337void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7338 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007339 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007340{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007341 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7342 int len1 = buf->size - msg->som;
7343 es->len = buf->r - (buf->data + msg->som) + buf->size;
7344 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7345 if (es->len > len1 && len1 < sizeof(es->buf))
7346 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7347 }
7348 else {
7349 es->len = buf->r - (buf->data + msg->som);
7350 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7351 }
7352
Willy Tarreau4076a152009-04-02 15:18:36 +02007353 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007354 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007355 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007356 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007357 else
7358 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7359
Willy Tarreau4076a152009-04-02 15:18:36 +02007360 es->when = date; // user-visible date
7361 es->sid = s->uniq_id;
7362 es->srv = s->srv;
7363 es->oe = other_end;
7364 es->src = s->cli_addr;
Willy Tarreau078272e2010-12-12 12:46:33 +01007365 es->state = state;
7366 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007367 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007368}
Willy Tarreaub2513902006-12-17 14:52:38 +01007369
Willy Tarreaubce70882009-09-07 11:51:47 +02007370/* return the IP address pointed to by occurrence <occ> of header <hname> in
7371 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
7372 * occurrence number corresponding to this value is returned. If <occ> is
7373 * strictly negative, the occurrence number before the end corresponding to
7374 * this value is returned. If <occ> is null, any value is returned, so it is
7375 * not recommended to use it that way. Negative occurrences are limited to
7376 * a small value because it is required to keep them in memory while scanning.
7377 * IP address 0.0.0.0 is returned if no match is found.
7378 */
7379unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
7380{
7381 struct hdr_ctx ctx;
7382 unsigned int hdr_hist[MAX_HDR_HISTORY];
7383 unsigned int hist_ptr;
7384 int found = 0;
7385
7386 ctx.idx = 0;
7387 if (occ >= 0) {
7388 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7389 occ--;
7390 if (occ <= 0) {
7391 found = 1;
7392 break;
7393 }
7394 }
7395 if (!found)
7396 return 0;
7397 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7398 }
7399
7400 /* negative occurrence, we scan all the list then walk back */
7401 if (-occ > MAX_HDR_HISTORY)
7402 return 0;
7403
7404 hist_ptr = 0;
7405 hdr_hist[hist_ptr] = 0;
7406 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7407 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7408 if (hist_ptr >= MAX_HDR_HISTORY)
7409 hist_ptr = 0;
7410 found++;
7411 }
7412 if (-occ > found)
7413 return 0;
7414 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7415 * find occurrence -occ, so we have to check [hist_ptr+occ].
7416 */
7417 hist_ptr += occ;
7418 if (hist_ptr >= MAX_HDR_HISTORY)
7419 hist_ptr -= MAX_HDR_HISTORY;
7420 return hdr_hist[hist_ptr];
7421}
7422
Willy Tarreaubaaee002006-06-26 02:48:02 +02007423/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007424 * Print a debug line with a header
7425 */
7426void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7427{
7428 int len, max;
7429 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007430 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007431 max = end - start;
7432 UBOUND(max, sizeof(trash) - len - 1);
7433 len += strlcpy2(trash + len, start, max + 1);
7434 trash[len++] = '\n';
7435 write(1, trash, len);
7436}
7437
Willy Tarreau0937bc42009-12-22 15:03:09 +01007438/*
7439 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7440 * the required fields are properly allocated and that we only need to (re)init
7441 * them. This should be used before processing any new request.
7442 */
7443void http_init_txn(struct session *s)
7444{
7445 struct http_txn *txn = &s->txn;
7446 struct proxy *fe = s->fe;
7447
7448 txn->flags = 0;
7449 txn->status = -1;
7450
Willy Tarreauf64d1412010-10-07 20:06:11 +02007451 txn->cookie_first_date = 0;
7452 txn->cookie_last_date = 0;
7453
Willy Tarreau0937bc42009-12-22 15:03:09 +01007454 txn->req.sol = txn->req.eol = NULL;
7455 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7456 txn->rsp.sol = txn->rsp.eol = NULL;
7457 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
7458 txn->req.hdr_content_len = 0LL;
7459 txn->rsp.hdr_content_len = 0LL;
7460 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7461 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007462
7463 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007464
7465 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7466 if (fe->options2 & PR_O2_REQBUG_OK)
7467 txn->req.err_pos = -1; /* let buggy requests pass */
7468
Willy Tarreau46023632010-01-07 22:51:47 +01007469 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007470 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7471
Willy Tarreau46023632010-01-07 22:51:47 +01007472 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007473 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7474
7475 if (txn->hdr_idx.v)
7476 hdr_idx_init(&txn->hdr_idx);
7477}
7478
7479/* to be used at the end of a transaction */
7480void http_end_txn(struct session *s)
7481{
7482 struct http_txn *txn = &s->txn;
7483
7484 /* these ones will have been dynamically allocated */
7485 pool_free2(pool2_requri, txn->uri);
7486 pool_free2(pool2_capture, txn->cli_cookie);
7487 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007488 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007489
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007490 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007491 txn->uri = NULL;
7492 txn->srv_cookie = NULL;
7493 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007494
7495 if (txn->req.cap) {
7496 struct cap_hdr *h;
7497 for (h = s->fe->req_cap; h; h = h->next)
7498 pool_free2(h->pool, txn->req.cap[h->index]);
7499 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7500 }
7501
7502 if (txn->rsp.cap) {
7503 struct cap_hdr *h;
7504 for (h = s->fe->rsp_cap; h; h = h->next)
7505 pool_free2(h->pool, txn->rsp.cap[h->index]);
7506 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7507 }
7508
Willy Tarreau0937bc42009-12-22 15:03:09 +01007509}
7510
7511/* to be used at the end of a transaction to prepare a new one */
7512void http_reset_txn(struct session *s)
7513{
7514 http_end_txn(s);
7515 http_init_txn(s);
7516
7517 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007518 s->logs.logwait = s->fe->to_log;
7519 s->srv = s->prev_srv = s->srv_conn = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007520 /* re-init store persistence */
7521 s->store_count = 0;
7522
Willy Tarreau0937bc42009-12-22 15:03:09 +01007523 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007524
7525 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7526
Willy Tarreau739cfba2010-01-25 23:11:14 +01007527 /* We must trim any excess data from the response buffer, because we
7528 * may have blocked an invalid response from a server that we don't
7529 * want to accidentely forward once we disable the analysers, nor do
7530 * we want those data to come along with next response. A typical
7531 * example of such data would be from a buggy server responding to
7532 * a HEAD with some data, or sending more than the advertised
7533 * content-length.
7534 */
7535 if (unlikely(s->rep->l > s->rep->send_max)) {
7536 s->rep->l = s->rep->send_max;
7537 s->rep->r = s->rep->w + s->rep->l;
7538 if (s->rep->r >= s->rep->data + s->rep->size)
7539 s->rep->r -= s->rep->size;
7540 }
7541
Willy Tarreau0937bc42009-12-22 15:03:09 +01007542 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007543 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007544
Willy Tarreaud04e8582010-05-31 12:31:35 +02007545 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007546 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007547
7548 s->req->rex = TICK_ETERNITY;
7549 s->req->wex = TICK_ETERNITY;
7550 s->req->analyse_exp = TICK_ETERNITY;
7551 s->rep->rex = TICK_ETERNITY;
7552 s->rep->wex = TICK_ETERNITY;
7553 s->rep->analyse_exp = TICK_ETERNITY;
7554}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007555
Willy Tarreau8797c062007-05-07 00:55:35 +02007556/************************************************************************/
7557/* The code below is dedicated to ACL parsing and matching */
7558/************************************************************************/
7559
7560
7561
7562
7563/* 1. Check on METHOD
7564 * We use the pre-parsed method if it is known, and store its number as an
7565 * integer. If it is unknown, we use the pointer and the length.
7566 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007567static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007568{
7569 int len, meth;
7570
Willy Tarreauae8b7962007-06-09 23:10:04 +02007571 len = strlen(*text);
7572 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007573
7574 pattern->val.i = meth;
7575 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007576 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007577 if (!pattern->ptr.str)
7578 return 0;
7579 pattern->len = len;
7580 }
7581 return 1;
7582}
7583
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007584static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007585acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7586 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007587{
7588 int meth;
7589 struct http_txn *txn = l7;
7590
Willy Tarreaub6866442008-07-14 23:54:42 +02007591 if (!txn)
7592 return 0;
7593
Willy Tarreau655dce92009-11-08 13:10:58 +01007594 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007595 return 0;
7596
Willy Tarreau8797c062007-05-07 00:55:35 +02007597 meth = txn->meth;
7598 test->i = meth;
7599 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007600 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7601 /* ensure the indexes are not affected */
7602 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007603 test->len = txn->req.sl.rq.m_l;
7604 test->ptr = txn->req.sol;
7605 }
7606 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7607 return 1;
7608}
7609
7610static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7611{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007612 int icase;
7613
Willy Tarreau8797c062007-05-07 00:55:35 +02007614 if (test->i != pattern->val.i)
Willy Tarreau11382812008-07-09 16:18:21 +02007615 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007616
7617 if (test->i != HTTP_METH_OTHER)
Willy Tarreau11382812008-07-09 16:18:21 +02007618 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007619
7620 /* Other method, we must compare the strings */
7621 if (pattern->len != test->len)
Willy Tarreau11382812008-07-09 16:18:21 +02007622 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007623
7624 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
7625 if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) ||
7626 (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007627 return ACL_PAT_FAIL;
7628 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007629}
7630
7631/* 2. Check on Request/Status Version
7632 * We simply compare strings here.
7633 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007634static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007635{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007636 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007637 if (!pattern->ptr.str)
7638 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007639 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007640 return 1;
7641}
7642
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007643static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007644acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7645 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007646{
7647 struct http_txn *txn = l7;
7648 char *ptr;
7649 int len;
7650
Willy Tarreaub6866442008-07-14 23:54:42 +02007651 if (!txn)
7652 return 0;
7653
Willy Tarreau655dce92009-11-08 13:10:58 +01007654 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007655 return 0;
7656
Willy Tarreau8797c062007-05-07 00:55:35 +02007657 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007658 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007659
7660 while ((len-- > 0) && (*ptr++ != '/'));
7661 if (len <= 0)
7662 return 0;
7663
7664 test->ptr = ptr;
7665 test->len = len;
7666
7667 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7668 return 1;
7669}
7670
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007671static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007672acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7673 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007674{
7675 struct http_txn *txn = l7;
7676 char *ptr;
7677 int len;
7678
Willy Tarreaub6866442008-07-14 23:54:42 +02007679 if (!txn)
7680 return 0;
7681
Willy Tarreau655dce92009-11-08 13:10:58 +01007682 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007683 return 0;
7684
Willy Tarreau8797c062007-05-07 00:55:35 +02007685 len = txn->rsp.sl.st.v_l;
7686 ptr = txn->rsp.sol;
7687
7688 while ((len-- > 0) && (*ptr++ != '/'));
7689 if (len <= 0)
7690 return 0;
7691
7692 test->ptr = ptr;
7693 test->len = len;
7694
7695 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7696 return 1;
7697}
7698
7699/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007700static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007701acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7702 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007703{
7704 struct http_txn *txn = l7;
7705 char *ptr;
7706 int len;
7707
Willy Tarreaub6866442008-07-14 23:54:42 +02007708 if (!txn)
7709 return 0;
7710
Willy Tarreau655dce92009-11-08 13:10:58 +01007711 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007712 return 0;
7713
Willy Tarreau8797c062007-05-07 00:55:35 +02007714 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007715 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007716
7717 test->i = __strl2ui(ptr, len);
7718 test->flags = ACL_TEST_F_VOL_1ST;
7719 return 1;
7720}
7721
7722/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007723static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007724acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7725 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007726{
7727 struct http_txn *txn = l7;
7728
Willy Tarreaub6866442008-07-14 23:54:42 +02007729 if (!txn)
7730 return 0;
7731
Willy Tarreau655dce92009-11-08 13:10:58 +01007732 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007733 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007734
Willy Tarreauc11416f2007-06-17 16:58:38 +02007735 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7736 /* ensure the indexes are not affected */
7737 return 0;
7738
Willy Tarreau8797c062007-05-07 00:55:35 +02007739 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007740 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007741
Willy Tarreauf3d25982007-05-08 22:45:09 +02007742 /* we do not need to set READ_ONLY because the data is in a buffer */
7743 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007744 return 1;
7745}
7746
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007747static int
7748acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7749 struct acl_expr *expr, struct acl_test *test)
7750{
7751 struct http_txn *txn = l7;
7752
Willy Tarreaub6866442008-07-14 23:54:42 +02007753 if (!txn)
7754 return 0;
7755
Willy Tarreau655dce92009-11-08 13:10:58 +01007756 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007757 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007758
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007759 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7760 /* ensure the indexes are not affected */
7761 return 0;
7762
7763 /* Parse HTTP request */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007764 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 +01007765 test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr;
7766 test->i = AF_INET;
7767
7768 /*
7769 * If we are parsing url in frontend space, we prepare backend stage
7770 * to not parse again the same url ! optimization lazyness...
7771 */
7772 if (px->options & PR_O_HTTP_PROXY)
7773 l4->flags |= SN_ADDR_SET;
7774
7775 test->flags = ACL_TEST_F_READ_ONLY;
7776 return 1;
7777}
7778
7779static int
7780acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7781 struct acl_expr *expr, struct acl_test *test)
7782{
7783 struct http_txn *txn = l7;
7784
Willy Tarreaub6866442008-07-14 23:54:42 +02007785 if (!txn)
7786 return 0;
7787
Willy Tarreau655dce92009-11-08 13:10:58 +01007788 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007789 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007790
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007791 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7792 /* ensure the indexes are not affected */
7793 return 0;
7794
7795 /* Same optimization as url_ip */
Willy Tarreau962c3f42010-01-10 00:15:35 +01007796 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 +01007797 test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port);
7798
7799 if (px->options & PR_O_HTTP_PROXY)
7800 l4->flags |= SN_ADDR_SET;
7801
7802 test->flags = ACL_TEST_F_READ_ONLY;
7803 return 1;
7804}
7805
Willy Tarreauc11416f2007-06-17 16:58:38 +02007806/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7807 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7808 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007809static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007810acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007811 struct acl_expr *expr, struct acl_test *test)
7812{
7813 struct http_txn *txn = l7;
7814 struct hdr_idx *idx = &txn->hdr_idx;
7815 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007816
Willy Tarreaub6866442008-07-14 23:54:42 +02007817 if (!txn)
7818 return 0;
7819
Willy Tarreau33a7e692007-06-10 19:45:56 +02007820 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7821 /* search for header from the beginning */
7822 ctx->idx = 0;
7823
Willy Tarreau33a7e692007-06-10 19:45:56 +02007824 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7825 test->flags |= ACL_TEST_F_FETCH_MORE;
7826 test->flags |= ACL_TEST_F_VOL_HDR;
7827 test->len = ctx->vlen;
7828 test->ptr = (char *)ctx->line + ctx->val;
7829 return 1;
7830 }
7831
7832 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7833 test->flags |= ACL_TEST_F_VOL_HDR;
7834 return 0;
7835}
7836
Willy Tarreau33a7e692007-06-10 19:45:56 +02007837static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007838acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7839 struct acl_expr *expr, struct acl_test *test)
7840{
7841 struct http_txn *txn = l7;
7842
Willy Tarreaub6866442008-07-14 23:54:42 +02007843 if (!txn)
7844 return 0;
7845
Willy Tarreau655dce92009-11-08 13:10:58 +01007846 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007847 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007848
Willy Tarreauc11416f2007-06-17 16:58:38 +02007849 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7850 /* ensure the indexes are not affected */
7851 return 0;
7852
7853 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7854}
7855
7856static int
7857acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7858 struct acl_expr *expr, struct acl_test *test)
7859{
7860 struct http_txn *txn = l7;
7861
Willy Tarreaub6866442008-07-14 23:54:42 +02007862 if (!txn)
7863 return 0;
7864
Willy Tarreau655dce92009-11-08 13:10:58 +01007865 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007866 return 0;
7867
7868 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7869}
7870
7871/* 6. Check on HTTP header count. The number of occurrences is returned.
7872 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7873 */
7874static int
7875acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007876 struct acl_expr *expr, struct acl_test *test)
7877{
7878 struct http_txn *txn = l7;
7879 struct hdr_idx *idx = &txn->hdr_idx;
7880 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007881 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007882
Willy Tarreaub6866442008-07-14 23:54:42 +02007883 if (!txn)
7884 return 0;
7885
Willy Tarreau33a7e692007-06-10 19:45:56 +02007886 ctx.idx = 0;
7887 cnt = 0;
7888 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7889 cnt++;
7890
7891 test->i = cnt;
7892 test->flags = ACL_TEST_F_VOL_HDR;
7893 return 1;
7894}
7895
Willy Tarreauc11416f2007-06-17 16:58:38 +02007896static int
7897acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7898 struct acl_expr *expr, struct acl_test *test)
7899{
7900 struct http_txn *txn = l7;
7901
Willy Tarreaub6866442008-07-14 23:54:42 +02007902 if (!txn)
7903 return 0;
7904
Willy Tarreau655dce92009-11-08 13:10:58 +01007905 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007906 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007907
Willy Tarreauc11416f2007-06-17 16:58:38 +02007908 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7909 /* ensure the indexes are not affected */
7910 return 0;
7911
7912 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7913}
7914
7915static int
7916acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7917 struct acl_expr *expr, struct acl_test *test)
7918{
7919 struct http_txn *txn = l7;
7920
Willy Tarreaub6866442008-07-14 23:54:42 +02007921 if (!txn)
7922 return 0;
7923
Willy Tarreau655dce92009-11-08 13:10:58 +01007924 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007925 return 0;
7926
7927 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7928}
7929
Willy Tarreau33a7e692007-06-10 19:45:56 +02007930/* 7. Check on HTTP header's integer value. The integer value is returned.
7931 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007932 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007933 */
7934static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007935acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007936 struct acl_expr *expr, struct acl_test *test)
7937{
7938 struct http_txn *txn = l7;
7939 struct hdr_idx *idx = &txn->hdr_idx;
7940 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007941
Willy Tarreaub6866442008-07-14 23:54:42 +02007942 if (!txn)
7943 return 0;
7944
Willy Tarreau33a7e692007-06-10 19:45:56 +02007945 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7946 /* search for header from the beginning */
7947 ctx->idx = 0;
7948
Willy Tarreau33a7e692007-06-10 19:45:56 +02007949 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7950 test->flags |= ACL_TEST_F_FETCH_MORE;
7951 test->flags |= ACL_TEST_F_VOL_HDR;
7952 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
7953 return 1;
7954 }
7955
7956 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7957 test->flags |= ACL_TEST_F_VOL_HDR;
7958 return 0;
7959}
7960
Willy Tarreauc11416f2007-06-17 16:58:38 +02007961static int
7962acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7963 struct acl_expr *expr, struct acl_test *test)
7964{
7965 struct http_txn *txn = l7;
7966
Willy Tarreaub6866442008-07-14 23:54:42 +02007967 if (!txn)
7968 return 0;
7969
Willy Tarreau655dce92009-11-08 13:10:58 +01007970 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007971 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007972
Willy Tarreauc11416f2007-06-17 16:58:38 +02007973 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7974 /* ensure the indexes are not affected */
7975 return 0;
7976
7977 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7978}
7979
7980static int
7981acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7982 struct acl_expr *expr, struct acl_test *test)
7983{
7984 struct http_txn *txn = l7;
7985
Willy Tarreaub6866442008-07-14 23:54:42 +02007986 if (!txn)
7987 return 0;
7988
Willy Tarreau655dce92009-11-08 13:10:58 +01007989 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007990 return 0;
7991
7992 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7993}
7994
Willy Tarreau106f9792009-09-19 07:54:16 +02007995/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7996 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7997 */
7998static int
7999acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
8000 struct acl_expr *expr, struct acl_test *test)
8001{
8002 struct http_txn *txn = l7;
8003 struct hdr_idx *idx = &txn->hdr_idx;
8004 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
8005
8006 if (!txn)
8007 return 0;
8008
8009 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8010 /* search for header from the beginning */
8011 ctx->idx = 0;
8012
8013 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8014 test->flags |= ACL_TEST_F_FETCH_MORE;
8015 test->flags |= ACL_TEST_F_VOL_HDR;
8016 /* Same optimization as url_ip */
8017 memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr));
8018 url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr);
8019 test->ptr = (void *)&l4->srv_addr.sin_addr;
8020 test->i = AF_INET;
8021 return 1;
8022 }
8023
8024 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8025 test->flags |= ACL_TEST_F_VOL_HDR;
8026 return 0;
8027}
8028
8029static int
8030acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8031 struct acl_expr *expr, struct acl_test *test)
8032{
8033 struct http_txn *txn = l7;
8034
8035 if (!txn)
8036 return 0;
8037
Willy Tarreau655dce92009-11-08 13:10:58 +01008038 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008039 return 0;
8040
8041 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8042 /* ensure the indexes are not affected */
8043 return 0;
8044
8045 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8046}
8047
8048static int
8049acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8050 struct acl_expr *expr, struct acl_test *test)
8051{
8052 struct http_txn *txn = l7;
8053
8054 if (!txn)
8055 return 0;
8056
Willy Tarreau655dce92009-11-08 13:10:58 +01008057 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008058 return 0;
8059
8060 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8061}
8062
Willy Tarreau737b0c12007-06-10 21:28:46 +02008063/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8064 * the first '/' after the possible hostname, and ends before the possible '?'.
8065 */
8066static int
8067acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8068 struct acl_expr *expr, struct acl_test *test)
8069{
8070 struct http_txn *txn = l7;
8071 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008072
Willy Tarreaub6866442008-07-14 23:54:42 +02008073 if (!txn)
8074 return 0;
8075
Willy Tarreau655dce92009-11-08 13:10:58 +01008076 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008077 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008078
Willy Tarreauc11416f2007-06-17 16:58:38 +02008079 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8080 /* ensure the indexes are not affected */
8081 return 0;
8082
Willy Tarreau962c3f42010-01-10 00:15:35 +01008083 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008084 ptr = http_get_path(txn);
8085 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008086 return 0;
8087
8088 /* OK, we got the '/' ! */
8089 test->ptr = ptr;
8090
8091 while (ptr < end && *ptr != '?')
8092 ptr++;
8093
8094 test->len = ptr - test->ptr;
8095
8096 /* we do not need to set READ_ONLY because the data is in a buffer */
8097 test->flags = ACL_TEST_F_VOL_1ST;
8098 return 1;
8099}
8100
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008101static int
8102acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8103 struct acl_expr *expr, struct acl_test *test)
8104{
8105 struct buffer *req = s->req;
8106 struct http_txn *txn = &s->txn;
8107 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008108
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008109 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8110 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8111 */
8112
8113 if (!s || !req)
8114 return 0;
8115
Willy Tarreau655dce92009-11-08 13:10:58 +01008116 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008117 /* Already decoded as OK */
8118 test->flags |= ACL_TEST_F_SET_RES_PASS;
8119 return 1;
8120 }
8121
8122 /* Try to decode HTTP request */
8123 if (likely(req->lr < req->r))
8124 http_msg_analyzer(req, msg, &txn->hdr_idx);
8125
Willy Tarreau655dce92009-11-08 13:10:58 +01008126 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008127 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8128 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8129 return 1;
8130 }
8131 /* wait for final state */
8132 test->flags |= ACL_TEST_F_MAY_CHANGE;
8133 return 0;
8134 }
8135
8136 /* OK we got a valid HTTP request. We have some minor preparation to
8137 * perform so that further checks can rely on HTTP tests.
8138 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008139 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008140 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8141 s->flags |= SN_REDIRECTABLE;
8142
8143 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8144 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8145 return 1;
8146 }
8147
8148 test->flags |= ACL_TEST_F_SET_RES_PASS;
8149 return 1;
8150}
8151
Willy Tarreau7f18e522010-10-22 20:04:13 +02008152/* return a valid test if the current request is the first one on the connection */
8153static int
8154acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8155 struct acl_expr *expr, struct acl_test *test)
8156{
8157 if (!s)
8158 return 0;
8159
8160 if (s->txn.flags & TX_NOT_FIRST)
8161 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8162 else
8163 test->flags |= ACL_TEST_F_SET_RES_PASS;
8164
8165 return 1;
8166}
8167
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008168static int
8169acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8170 struct acl_expr *expr, struct acl_test *test)
8171{
8172
8173 if (!s)
8174 return 0;
8175
8176 if (!get_http_auth(s))
8177 return 0;
8178
8179 test->ctx.a[0] = expr->arg.ul;
8180 test->ctx.a[1] = s->txn.auth.user;
8181 test->ctx.a[2] = s->txn.auth.pass;
8182
8183 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8184
8185 return 1;
8186}
Willy Tarreau8797c062007-05-07 00:55:35 +02008187
8188/************************************************************************/
8189/* All supported keywords must be declared here. */
8190/************************************************************************/
8191
8192/* Note: must not be declared <const> as its list will be overwritten */
8193static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008194 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8195
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008196 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008197 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8198 { "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 +02008199 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008200
Willy Tarreauc4262962010-05-10 23:42:40 +02008201 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008202 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8203 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8204 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8205 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8206 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8207 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008208 { "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 +02008209 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008210
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008211 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008212 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008213 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8214 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8215 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8216 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8217 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8218 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8219 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
8220 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008221 { "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 +02008222
Willy Tarreauc4262962010-05-10 23:42:40 +02008223 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008224 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8225 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8226 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8227 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8228 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8229 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8230 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
8231 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008232 { "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 +02008233
Willy Tarreauc4262962010-05-10 23:42:40 +02008234 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008235 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8236 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8237 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8238 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8239 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8240 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008241
Willy Tarreauf3d25982007-05-08 22:45:09 +02008242#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008243 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8244 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8245 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8246 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8247 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8248 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8249 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8250
Willy Tarreau8797c062007-05-07 00:55:35 +02008251 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8252 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8253 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8254 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8255 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8256 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8257 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8258 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008259#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008260
Willy Tarreau7f18e522010-10-22 20:04:13 +02008261 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8262 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8263 { "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 +02008264 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008265}};
8266
Willy Tarreau4a568972010-05-12 08:08:50 +02008267/************************************************************************/
8268/* The code below is dedicated to pattern fetching and matching */
8269/************************************************************************/
8270
8271/* extract the IP address from the last occurrence of specified header. Note
8272 * that we should normally first extract the string then convert it to IP,
8273 * but right now we have all the functions to do this seemlessly, and we will
8274 * be able to change that later without touching the configuration.
8275 */
8276static int
8277pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02008278 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008279{
8280 struct http_txn *txn = l7;
8281
Emeric Brun485479d2010-09-23 18:02:19 +02008282 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 +02008283 return data->ip.s_addr != 0;
8284}
8285
David Cournapeau16023ee2010-12-23 20:55:41 +09008286/*
8287 * Given a path string and its length, find the position of beginning of the
8288 * query string. Returns NULL if no query string is found in the path.
8289 *
8290 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8291 *
8292 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8293 */
8294static inline char *find_query_string(char *path, size_t path_l)
8295{
8296 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008297
David Cournapeau16023ee2010-12-23 20:55:41 +09008298 p = memchr(path, '?', path_l);
8299 return p ? p + 1 : NULL;
8300}
8301
8302static inline int is_param_delimiter(char c)
8303{
8304 return c == '&' || c == ';';
8305}
8306
8307/*
8308 * Given a url parameter, find the starting position of the first occurence,
8309 * or NULL if the parameter is not found.
8310 *
8311 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8312 * the function will return query_string+8.
8313 */
8314static char*
8315find_url_param_pos(char* query_string, size_t query_string_l,
8316 char* url_param_name, size_t url_param_name_l)
8317{
8318 char *pos, *last;
8319
8320 pos = query_string;
8321 last = query_string + query_string_l - url_param_name_l - 1;
8322
8323 while (pos <= last) {
8324 if (pos[url_param_name_l] == '=') {
8325 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8326 return pos;
8327 pos += url_param_name_l + 1;
8328 }
8329 while (pos <= last && !is_param_delimiter(*pos))
8330 pos++;
8331 pos++;
8332 }
8333 return NULL;
8334}
8335
8336/*
8337 * Given a url parameter name, returns its value and size into *value and
8338 * *value_l respectively, and returns non-zero. If the parameter is not found,
8339 * zero is returned and value/value_l are not touched.
8340 */
8341static int
8342find_url_param_value(char* path, size_t path_l,
8343 char* url_param_name, size_t url_param_name_l,
8344 char** value, size_t* value_l)
8345{
8346 char *query_string, *qs_end;
8347 char *arg_start;
8348 char *value_start, *value_end;
8349
8350 query_string = find_query_string(path, path_l);
8351 if (!query_string)
8352 return 0;
8353
8354 qs_end = path + path_l;
8355 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8356 url_param_name, url_param_name_l);
8357 if (!arg_start)
8358 return 0;
8359
8360 value_start = arg_start + url_param_name_l + 1;
8361 value_end = value_start;
8362
8363 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8364 value_end++;
8365
8366 *value = value_start;
8367 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008368 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008369}
8370
8371static int
8372pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8373 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8374{
8375 struct http_txn *txn = l7;
8376 struct http_msg *msg = &txn->req;
8377 char *url_param_value;
8378 size_t url_param_value_l;
8379
8380 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8381 arg_p->data.str.str, arg_p->data.str.len,
8382 &url_param_value, &url_param_value_l))
8383 return 0;
8384
8385 data->str.str = url_param_value;
8386 data->str.len = url_param_value_l;
8387 return 1;
8388}
8389
Emeric Brun485479d2010-09-23 18:02:19 +02008390
Willy Tarreau4a568972010-05-12 08:08:50 +02008391/************************************************************************/
8392/* All supported keywords must be declared here. */
8393/************************************************************************/
8394/* Note: must not be declared <const> as its list will be overwritten */
8395static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Emeric Brun485479d2010-09-23 18:02:19 +02008396 { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008397 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Emeric Brun485479d2010-09-23 18:02:19 +02008398 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008399}};
8400
Willy Tarreau8797c062007-05-07 00:55:35 +02008401
8402__attribute__((constructor))
8403static void __http_protocol_init(void)
8404{
8405 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008406 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008407}
8408
8409
Willy Tarreau58f10d72006-12-04 02:26:12 +01008410/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008411 * Local variables:
8412 * c-indent-level: 8
8413 * c-basic-offset: 8
8414 * End:
8415 */